Solaris Software
Loading...
Searching...
No Matches
icm20948.c
Go to the documentation of this file.
1/**
2 * @file icm20948.c
3 * @brief ICM20948 IMU driver + SPP service implementation.
4 *
5 * Implements SPI register access, DMP firmware loading and the full DMP
6 * initialisation sequence. Exposes a single public accessor
7 * (SPP_SERVICES_ICM20948_getProducerContract) so the device can be registered
8 * with the SPP service registry via SPP_SERVICES_PUBSUB_registerProducer().
9 */
10
12
14#include "spp/hal/spi/spi.h"
15#include "spp/hal/gpio/gpio.h"
16#include "spp/hal/time/time.h"
17#include "spp/core/types.h"
23#ifdef SPP_DEBUG_PRINT
24#include <stdio.h>
25#endif
26
27#include <stdio.h>
28#include <string.h>
29#include <math.h>
30
31/* ----------------------------------------------------------------
32 * CONSTANTS
33 * ---------------------------------------------------------------- */
34
35#define K_READ_OP 0x80U /**< SPI read flag — set MSB to signal a read transaction. */
36#define K_WRITE_OP 0x00U /**< SPI write marker (zero). */
37
38#define K_ICM20948_BASE_SAMPLE_RATE 1125L
39#define K_ICM20948_DMP_RUNNING_RATE 225L
40#define K_ICM20948_DMP_DIVIDER (K_ICM20948_BASE_SAMPLE_RATE / K_ICM20948_DMP_RUNNING_RATE)
41#define K_ICM20948_WHO_AM_I_VALUE 0xEAU
42#define K_ICM20948_DMP_PACKET_SIZE_BYTES 42U
43#define K_ICM20948_FIFO_RESET_THRESHOLD 512U
44#define K_ICM20948_DMP_START_ADDR_MSB 0x10U
45#define K_ICM20948_DMP_START_ADDR_LSB 0x00U
46#define K_ICM20948_LOG_TAG "ICM"
47
48/* ----------------------------------------------------------------
49 * PRIVATE TYPES
50 * ---------------------------------------------------------------- */
51
57
58/* ----------------------------------------------------------------
59 * STATIC FUNCTIONS DECLARATIONS
60 * ---------------------------------------------------------------- */
61
63static SPP_RetVal_t SPP_SERVICES_ICM20948_readReg(void *p_spi, spp_uint8_t reg, spp_uint8_t *p_value);
64static SPP_RetVal_t readFifoBurst(void *p_spi, spp_uint8_t *p_dst, spp_uint8_t count);
67static SPP_RetVal_t SPP_SERVICES_ICM20948_dmpWriteBytes(void *p_data, spp_uint16_t addr, const spp_uint8_t *p_bytes,
68 spp_uint8_t len);
73static SPP_RetVal_t ICM20948_dmpWriteOutputConfig(void *p_data, spp_uint16_t outCtl1, spp_uint16_t motionEvent);
80
81/* ----------------------------------------------------------------
82 * VARIABLES
83 * ---------------------------------------------------------------- */
84
85static const spp_uint8_t s_dmp3Image[] = {
86#include "dmpImage.h"
87};
88
90
92 .p_nameProducer = "icm20948",
93 .tiemoutMs = K_ICM20948_TASK_TIMEOUT_MS,
96};
97
98/* Magneto Calibration */
100 {0.02601071f, 0.00000000f, 0.00000000f},
101 {0.00099049f, 0.02536989f, 0.00000000f},
102 {-0.00027743f, -0.00132742f, 0.02539076f},
103};
104
105/* ----------------------------------------------------------------
106 * PUBLIC FUNCTIONS
107 * ---------------------------------------------------------------- */
108
109/**
110 * @brief Returns a pointer to the ICM20948 producer contract.
111 * @return Pointer to the static producer contract.
112 */
117
118/* ----------------------------------------------------------------
119 * STATIC FUNCTIONS
120 * ---------------------------------------------------------------- */
121
122/* ----------------------------------------------------------------
123 * Service — init and acquire
124 * ---------------------------------------------------------------- */
125
126/**
127 * @brief Initialises the ICM20948 interrupt context and registers the GPIO ISR.
128 * @param p_icm Pointer to the ICM20948 interrupt context to initialise.
129 * @return Result of GPIO interrupt configuration and ISR registration.
130 */
132{
133 p_icm->drdyFlag = false;
134 p_icm->isr_ctx.p_flag = &p_icm->drdyFlag;
136
137 if (ret == K_SPP_OK)
138 {
139 ret = SPP_HAL_GPIO_registerIsr(p_icm->intPin, (void *)&p_icm->isr_ctx);
140 }
141
142 return ret;
143}
144
145/**
146 * @brief Service init callback — sets hardware config, registers GPIO ISR, loads DMP firmware.
147 * @return K_SPP_OK on success, K_SPP_ERROR on init failure.
148 */
150{
152
157 s_icmData.seq = 0U;
158
162
164
165 if (ret == K_SPP_OK)
166 {
168 if (s_icmData.p_spi == NULL)
169 {
170 printf("ICM20948 INIT FAIL: SPI handle\n");
171 return K_SPP_ERROR;
172 }
173 }
174
175 if (ret == K_SPP_OK)
176 {
178 }
179
180 if (ret == K_SPP_OK)
181 {
183 }
184
185 if (ret != K_SPP_OK)
186 {
187 printf("ICM20948 INIT FAIL ret=%d\n", (int)ret);
188 }
189
190 return ret;
191}
192
193/**
194 * @brief Reads DMP FIFO data and publishes an SPP packet.
195 *
196 * Checks the DRDY flag and publishes accelerometer, gyroscope
197 * and quaternion data in an 11-float payload (44 bytes).
198 *
199 * @param kpid Assigned producer KPID.
200 * @return K_SPP_OK on success or no new data, error code on
201 * packet allocation or preparation failure.
202 */
204{
205 ICM20948_t *p_ctx = &s_icmData;
206
207 if (!p_ctx->icmData.drdyFlag)
208 {
209 return K_SPP_OK;
210 }
211 p_ctx->icmData.drdyFlag = false;
212 p_ctx->lastData.dataReady = false;
213
215
216 if (!p_ctx->lastData.dataReady)
217 {
218 return K_SPP_OK;
219 }
220
222 if (p_pkt1 == NULL)
223 {
224#ifdef SPP_DEBUG_PRINT
225 SPP_LOGI(K_ICM20948_LOG_TAG, "No free packet");
226#endif
227 return K_SPP_ERROR;
228 }
229
230 float payload1[11] = {
231 p_ctx->lastData.ax, p_ctx->lastData.ay, p_ctx->lastData.az, p_ctx->lastData.gx,
232 p_ctx->lastData.gy, p_ctx->lastData.gz, p_ctx->lastData.qw, p_ctx->lastData.qx,
233 p_ctx->lastData.qy, p_ctx->lastData.qz, p_ctx->lastData.accuracy,
234 };
235
236#ifdef SPP_DEBUG_PRINT
238 "[ICM] A:[%.2f %.2f %.2f]g G:[%.1f %.1f %.1f]dps M:[%.1f %.1f "
239 "%.1f]uT"
240 " Q:[w=%.3f x=%.3f y=%.3f z=%.3f] ACC:%f\n",
241 p_ctx->lastData.ax, p_ctx->lastData.ay, p_ctx->lastData.az, p_ctx->lastData.gx, p_ctx->lastData.gy,
242 p_ctx->lastData.gz, p_ctx->lastData.mx, p_ctx->lastData.my, p_ctx->lastData.mz, p_ctx->lastData.qw,
243 p_ctx->lastData.qx, p_ctx->lastData.qy, p_ctx->lastData.qz, p_ctx->lastData.accuracy);
244#endif
245
246 SPP_RetVal_t ret =
247 SPP_SERVICES_DATABANK_packetData(p_pkt1, kpid.value, p_ctx->seq++, payload1, (spp_uint16_t)sizeof(payload1));
248 if (ret != K_SPP_OK)
249 {
250 SPP_LOGE(K_ICM20948_LOG_TAG, "packetData failed ret=%d", (int)ret);
252 return ret;
253 }
254
256
257 return K_SPP_OK;
258}
259
260/* ----------------------------------------------------------------
261 * Driver — private helpers
262 * ---------------------------------------------------------------- */
263
264/**
265 * @brief Writes a single byte to an ICM-20948 register over SPI.
266 * @param p_spi SPI device handle.
267 * @param reg Register address.
268 * @param value Value to write.
269 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
270 */
272{
273 spp_uint8_t txBuffer[2] = {K_WRITE_OP | reg, value};
274 return SPP_HAL_SPI_transmit(p_spi, txBuffer, 2U);
275}
276
277/**
278 * @brief Reads a single byte from an ICM-20948 register over SPI.
279 *
280 * The HAL always issues a 3-byte SPI frame for reads (addr + 2 data bytes);
281 * ICM-20948 has no dummy byte so data lands at buf[1]. buf[2] absorbs the
282 * extra byte clocked out — must exist in the array to avoid a stack write.
283 *
284 * @param p_spi SPI device handle.
285 * @param reg Register address.
286 * @param p_value Output: register value (may be NULL to discard).
287 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
288 */
290{
291 spp_uint8_t txRxBuffer[3] = {K_READ_OP | reg, K_WRITE_OP, K_WRITE_OP};
292 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spi, txRxBuffer, 3U);
293
294 if (p_value != NULL)
295 {
296 *p_value = txRxBuffer[1];
297 }
298
299 return ret;
300}
301
302/**
303 * @brief Reads @p count bytes sequentially from the FIFO_R_W register.
304 *
305 * The HAL issues 3-byte read frames (address + 2 data bytes), yielding 2 FIFO
306 * bytes per frame. @p count must be even; K_ICM20948_DMP_PACKET_SIZE_BYTES
307 * satisfies this requirement.
308 *
309 * @param p_spi SPI device handle.
310 * @param p_dst Destination buffer (must be at least @p count bytes).
311 * @param count Number of bytes to read (must be even).
312 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
313 */
314static SPP_RetVal_t readFifoBurst(void *p_spi, spp_uint8_t *p_dst, spp_uint8_t count)
315{
316 spp_uint8_t frame[3];
317 spp_uint8_t i = 0U;
318 while (i < count)
319 {
321 frame[1] = 0U;
322 frame[2] = 0U;
323 SPP_RetVal_t ret = SPP_HAL_SPI_transmit(p_spi, frame, 3U);
324 if (ret != K_SPP_OK)
325 {
326 return ret;
327 }
328 p_dst[i] = frame[1];
329 i++;
330 if (i < count)
331 {
332 p_dst[i] = frame[2];
333 i++;
334 }
335 }
336 return K_SPP_OK;
337}
338
339/**
340 * @brief Selects the active register bank by writing REG_BANK_SEL.
341 * @param p_spi SPI device handle.
342 * @param regBank Target bank (K_ICM20948_REG_BANK_0 … _3).
343 * @return K_SPP_OK on success, K_SPP_ERROR on invalid bank or SPI failure.
344 */
346{
347 ICM20948_RegBankSel_t regBankSel = {.value = 0U};
348
349 switch (regBank)
350 {
352 regBankSel.bits.bankSel = 0U;
353 break;
355 regBankSel.bits.bankSel = 1U;
356 break;
358 regBankSel.bits.bankSel = 2U;
359 break;
361 regBankSel.bits.bankSel = 3U;
362 break;
363 default:
364 return K_SPP_ERROR;
365 }
366
368}
369
370/**
371 * @brief Resets all five FIFO streams by toggling the FIFO_RST bits.
372 * @param p_spi SPI device handle.
373 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
374 */
376{
377 ICM20948_RegFifoRst_t fifoResetReg = {.value = 0U};
378 SPP_RetVal_t ret;
379
380 fifoResetReg.bits.fifoRst0 = 1U;
381 fifoResetReg.bits.fifoRst1 = 1U;
382 fifoResetReg.bits.fifoRst2 = 1U;
383 fifoResetReg.bits.fifoRst3 = 1U;
384 fifoResetReg.bits.fifoRst4 = 1U;
385
387 if (ret != K_SPP_OK)
388 {
389 return ret;
390 }
391
392 fifoResetReg.bits.fifoRst0 = 0U;
393
395}
396
397/**
398 * @brief Writes @p len bytes to DMP memory starting at address @p addr.
399 * @param p_data SPI device handle.
400 * @param addr DMP memory start address.
401 * @param p_bytes Pointer to byte array to write.
402 * @param len Number of bytes to write.
403 * @return K_SPP_OK on success, K_SPP_ERROR_NULL_POINTER or K_SPP_ERROR on failure.
404 */
406 spp_uint8_t len)
407{
408 void *p_spi = p_data;
409 SPP_RetVal_t ret;
410
411 if ((p_spi == NULL) || (p_bytes == NULL))
412 {
414 }
415
416 for (spp_uint8_t i = 0U; i < len; i++)
417 {
419 if (ret != K_SPP_OK)
420 {
421 return ret;
422 }
423
425 if (ret != K_SPP_OK)
426 {
427 return ret;
428 }
429
431 if (ret != K_SPP_OK)
432 {
433 return ret;
434 }
435 }
436
437 return K_SPP_OK;
438}
439
440/**
441 * @brief Writes a 32-bit big-endian value to DMP memory at @p addr.
442 * @param p_data SPI device handle.
443 * @param addr DMP memory address.
444 * @param value 32-bit value to write.
445 * @return K_SPP_OK on success, K_SPP_ERROR on failure.
446 */
448{
449 spp_uint8_t bytes[4] = {(spp_uint8_t)(value >> 24), (spp_uint8_t)(value >> 16), (spp_uint8_t)(value >> 8),
450 (spp_uint8_t)value};
451
452 return SPP_SERVICES_ICM20948_dmpWriteBytes(p_data, addr, bytes, 4U);
453}
454
455/**
456 * @brief Writes a 16-bit big-endian value to DMP memory at @p addr.
457 * @param p_data SPI device handle.
458 * @param addr DMP memory address.
459 * @param value 16-bit value to write.
460 * @return K_SPP_OK on success, K_SPP_ERROR on failure.
461 */
463{
464 spp_uint8_t bytes[2] = {(spp_uint8_t)(value >> 8), (spp_uint8_t)value};
465
466 return SPP_SERVICES_ICM20948_dmpWriteBytes(p_data, addr, bytes, 2U);
467}
468
469/**
470 * @brief Toggles PWR_MGMT_1 into low-power wake-cycle mode then back to normal.
471 * @param p_data SPI device handle.
472 * @return K_SPP_OK on success, K_SPP_ERROR_NULL_POINTER or K_SPP_ERROR on failure.
473 */
475{
476 void *p_spi = p_data;
477 ICM20948_RegPwrMgmt1_t pwrMgmt1Reg = {.value = 0U};
478 SPP_RetVal_t ret;
479
480 if (p_spi == NULL)
481 {
483 }
484
485 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
486 pwrMgmt1Reg.bits.lpEn = 1U;
487
489 if (ret != K_SPP_OK)
490 {
491 return ret;
492 }
493
494 pwrMgmt1Reg.value = 0U;
495 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
496
498}
499
500/**
501 * @brief Computes the DMP gyroscope scale factor from the PLL trim value.
502 * @param pll PLL timebase correction value read from bank 1.
503 * @return Scaled gyroscope scale factor for the DMP.
504 */
506{
507 spp_int32_t t = 102870L + 81L * (spp_int32_t)pll;
508 spp_int32_t a = (1L << 30) / t;
509 spp_int32_t r = (1L << 30) - a * t;
510 spp_int32_t v = a * 797L * K_ICM20948_DMP_DIVIDER;
511
512 v += (spp_int32_t)(((spp_int64_t)a * 1011387LL * K_ICM20948_DMP_DIVIDER) >> 20);
513 v += r * 797L * K_ICM20948_DMP_DIVIDER / t;
514 v += (spp_int32_t)(((spp_int64_t)r * 1011387LL * K_ICM20948_DMP_DIVIDER) >> 20) / t;
515
516 return v << 1;
517}
518
519/**
520 * @brief Programs the DMP output control and motion event registers.
521 * @param p_data SPI device handle.
522 * @param outCtl1 DATA_OUT_CTL1 bitmask (selects sensor outputs).
523 * @param motionEvent MOTION_EVENT_CTL bitmask.
524 * @return K_SPP_OK on success, K_SPP_ERROR on SPI failure.
525 */
526static SPP_RetVal_t ICM20948_dmpWriteOutputConfig(void *p_data, spp_uint16_t outCtl1, spp_uint16_t motionEvent)
527{
528 SPP_RetVal_t ret;
529
531 if (ret != K_SPP_OK)
532 {
533 return ret;
534 }
535
537 if (ret != K_SPP_OK)
538 {
539 return ret;
540 }
541
543 if (ret != K_SPP_OK)
544 {
545 return ret;
546 }
547
549 if (ret != K_SPP_OK)
550 {
551 return ret;
552 }
553
554 return K_SPP_OK;
555}
556
557/* ----------------------------------------------------------------
558 * Driver — DMP firmware load
559 * ---------------------------------------------------------------- */
560
561/**
562 * @brief Loads the DMP3 firmware image into ICM-20948 SRAM.
563 *
564 * Writes s_dmp3Image byte-by-byte starting at K_ICM20948_DMP_LOAD_START,
565 * then primes the SRAM read path with a dummy read transaction.
566 *
567 * @param p_data SPI device handle.
568 * @return K_SPP_OK on success, K_SPP_ERROR_NULL_POINTER or K_SPP_ERROR on failure.
569 */
571{
572 void *p_spi = p_data;
573 const spp_uint8_t *p_firmware = s_dmp3Image;
574 spp_uint16_t firmwareSize = (spp_uint16_t)sizeof(s_dmp3Image);
575 spp_uint16_t done;
576 spp_uint16_t loadAddr;
577 SPP_RetVal_t ret;
578
579 if (p_spi == NULL)
580 {
582 }
583
585 if (ret != K_SPP_OK)
586 {
587 return ret;
588 }
589
590 /* ---- Write: byte-by-byte, MEM_BANK_SEL only on change ---- */
591 spp_uint8_t lastBank = 0xFFU;
592
593 done = 0U;
594 loadAddr = K_ICM20948_DMP_LOAD_START;
595 while (done < firmwareSize)
596 {
597 spp_uint8_t bank = (spp_uint8_t)(loadAddr >> 8);
598 spp_uint8_t offset = (spp_uint8_t)(loadAddr & 0xFFU);
599
600 if (bank != lastBank)
601 {
603 if (ret != K_SPP_OK)
604 {
605 return ret;
606 }
607 lastBank = bank;
608 }
609
611 if (ret != K_SPP_OK)
612 {
613 return ret;
614 }
615 ret = SPP_SERVICES_ICM20948_writeReg(p_spi, K_ICM20948_REG_MEM_R_W, p_firmware[done]);
616 if (ret != K_SPP_OK)
617 {
618 return ret;
619 }
620
621 done++;
622 loadAddr = (spp_uint16_t)(loadAddr + 1U);
623 }
624
625 /* Prime the SRAM read path — the DMP memory controller needs one read transaction
626 * after the write burst before verify reads return correct values. */
627 {
628 spp_uint8_t primeVal = 0U;
632 }
633
634 return K_SPP_OK;
635}
636
637/* ----------------------------------------------------------------
638 * Driver — DMP initialisation
639 * ---------------------------------------------------------------- */
640
641/**
642 * @brief Executes the full DMP initialisation sequence.
643 *
644 * Resets the device, loads DMP firmware, configures gyroscope/accelerometer
645 * full-scale ranges, compass mount matrix, sample-rate dividers and output
646 * data rates, then starts the magnetometer and enables the DMP FIFO.
647 *
648 * @param p_data SPI device handle.
649 * @return K_SPP_OK on success, K_SPP_ERROR on any step failure.
650 */
652{
653 void *p_spi = p_data;
654 SPP_RetVal_t ret;
655 spp_uint8_t whoAmIValue;
656 spp_uint8_t pllRaw;
657 spp_int8_t pllTrim;
658
659 static const ICM20948_DmpMatrixEntry_t s_cpassMatrix[] = {
660 {K_ICM20948_DMP_CPASS_MTX_00, 0x09999999U}, {K_ICM20948_DMP_CPASS_MTX_01, 0x00000000U},
661 {K_ICM20948_DMP_CPASS_MTX_02, 0x00000000U}, {K_ICM20948_DMP_CPASS_MTX_10, 0x00000000U},
662 {K_ICM20948_DMP_CPASS_MTX_11, 0xF6666667U}, {K_ICM20948_DMP_CPASS_MTX_12, 0x00000000U},
663 {K_ICM20948_DMP_CPASS_MTX_20, 0x00000000U}, {K_ICM20948_DMP_CPASS_MTX_21, 0x00000000U},
664 {K_ICM20948_DMP_CPASS_MTX_22, 0xF6666667U}};
665
666 static const ICM20948_DmpMatrixEntry_t s_b2sMatrix[] = {
667 {K_ICM20948_DMP_B2S_MTX_00, 0x40000000U}, {K_ICM20948_DMP_B2S_MTX_01, 0x00000000U},
668 {K_ICM20948_DMP_B2S_MTX_02, 0x00000000U}, {K_ICM20948_DMP_B2S_MTX_10, 0x00000000U},
669 {K_ICM20948_DMP_B2S_MTX_11, 0x40000000U}, {K_ICM20948_DMP_B2S_MTX_12, 0x00000000U},
670 {K_ICM20948_DMP_B2S_MTX_20, 0x00000000U}, {K_ICM20948_DMP_B2S_MTX_21, 0x00000000U},
671 {K_ICM20948_DMP_B2S_MTX_22, 0x40000000U}};
672
673 if (p_spi == NULL)
674 {
676 }
677
679 if (ret != K_SPP_OK)
680 {
681 return ret;
682 }
683
684 ret = SPP_SERVICES_ICM20948_readReg(p_spi, K_ICM20948_REG_WHO_AM_I, &whoAmIValue);
685 if (ret != K_SPP_OK)
686 {
687 return ret;
688 }
689
690 printf("ICM20948 WHO_AM_I=0x%02X\n", whoAmIValue);
691
692#ifdef SPP_DEBUG_PRINT
693 SPP_LOGI(K_ICM20948_LOG_TAG, "WHO_AM_I = 0x%02X (expect 0xEA)", whoAmIValue);
694#endif
695 if (whoAmIValue != K_ICM20948_WHO_AM_I_VALUE)
696 {
697 return K_SPP_ERROR;
698 }
699
700 {
701 ICM20948_RegPwrMgmt1_t pwrMgmt1Reg = {.value = 0U};
702 ICM20948_RegUserCtrl_t userCtrlReg = {.value = 0U};
703 ICM20948_RegPwrMgmt2_t pwrMgmt2Reg = {.value = 0U};
704 ICM20948_RegLpConf_t lpConfReg = {.value = 0U};
705
706 /* Software reset — chip returns to POR state. */
707 pwrMgmt1Reg.bits.deviceReset = 1U;
709 if (ret != K_SPP_OK)
710 {
711 return ret;
712 }
714
715 /* Wake up, auto clock, LP_EN=0 — required before DMP SRAM is writable. */
716 pwrMgmt1Reg.value = 0U;
717 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
719 if (ret != K_SPP_OK)
720 {
721 return ret;
722 }
723
724 userCtrlReg.bits.i2cIfDis = 1U;
726 if (ret != K_SPP_OK)
727 {
728 return ret;
729 }
730
731 pwrMgmt2Reg.bits.disableAccelX = 1U;
732 pwrMgmt2Reg.bits.disableAccelY = 1U;
733 pwrMgmt2Reg.bits.disableAccelZ = 1U;
734 pwrMgmt2Reg.bits.disableGyroZ = 1U;
736 if (ret != K_SPP_OK)
737 {
738 return ret;
739 }
740
741 lpConfReg.bits.i2cMstCyc = 1U;
743 if (ret != K_SPP_OK)
744 {
745 return ret;
746 }
747
749 if (ret != K_SPP_OK)
750 {
751 return ret;
752 }
753 }
754
755 SPP_LOGI(K_ICM20948_LOG_TAG, "Loading DMP firmware...");
756 ret = SPP_SERVICES_ICM20948_loadDmp(p_data);
757 if (ret != K_SPP_OK)
758 {
759 SPP_LOGE(K_ICM20948_LOG_TAG, "loadDmp failed ret=%d", (int)ret);
760 return ret;
761 }
762 SPP_LOGI(K_ICM20948_LOG_TAG, "DMP loaded OK");
763
765 if (ret != K_SPP_OK)
766 {
767 return ret;
768 }
769
771 if (ret != K_SPP_OK)
772 {
773 return ret;
774 }
775
777 if (ret != K_SPP_OK)
778 {
779 return ret;
780 }
781
783 if (ret != K_SPP_OK)
784 {
785 return ret;
786 }
787
788 ret = ICM20948_dmpWriteOutputConfig(p_data, 0x0000U, 0x0000U);
789 if (ret != K_SPP_OK)
790 {
791 return ret;
792 }
793
795 if (ret != K_SPP_OK)
796 {
797 return ret;
798 }
799
801 if (ret != K_SPP_OK)
802 {
803 return ret;
804 }
805
806 {
807 ICM20948_RegIntEnable_t intEnableReg = {.value = 0U};
808 ICM20948_RegIntEnable2_t intEnable2Reg = {.value = 0U};
809 ICM20948_RegSingleFifoPrioritySel_t fifoPriorityReg = {.value = 0U};
810 ICM20948_RegHwFixDisable_t hwFixDisableReg = {.value = 0U};
811
812 intEnableReg.bits.rawData0Rdy = 1U;
814 if (ret != K_SPP_OK)
815 {
816 return ret;
817 }
818
819 intEnable2Reg.bits.fifoOverflowEn0 = 1U;
821 if (ret != K_SPP_OK)
822 {
823 return ret;
824 }
825
826 fifoPriorityReg.value = 0xE4U;
828 if (ret != K_SPP_OK)
829 {
830 return ret;
831 }
832
833 hwFixDisableReg.value = 0x48U;
835 if (ret != K_SPP_OK)
836 {
837 return ret;
838 }
839 }
840
842 if (ret != K_SPP_OK)
843 {
844 return ret;
845 }
846
848 if (ret != K_SPP_OK)
849 {
850 return ret;
851 }
852
854 if (ret != K_SPP_OK)
855 {
856 return ret;
857 }
858
860 if (ret != K_SPP_OK)
861 {
862 return ret;
863 }
864
866 if (ret != K_SPP_OK)
867 {
868 return ret;
869 }
870
872 if (ret != K_SPP_OK)
873 {
874 return ret;
875 }
876
878 if (ret != K_SPP_OK)
879 {
880 return ret;
881 }
882
883 {
884 ICM20948_RegFifoCfg_t fifoCfgReg = {.value = 0U};
885 ICM20948_RegFifoEn1_t fifoEn1Reg = {.value = 0U};
886 ICM20948_RegFifoEn2_t fifoEn2Reg = {.value = 0U};
887
889 if (ret != K_SPP_OK)
890 {
891 return ret;
892 }
893
895 if (ret != K_SPP_OK)
896 {
897 return ret;
898 }
899
901 if (ret != K_SPP_OK)
902 {
903 return ret;
904 }
905
907 if (ret != K_SPP_OK)
908 {
909 return ret;
910 }
911 }
912
913 {
914 ICM20948_RegPwrMgmt1_t pwrMgmt1Reg = {.value = 0U};
915 ICM20948_RegPwrMgmt2_t pwrMgmt2Reg = {.value = 0U};
916
917 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
918 pwrMgmt1Reg.bits.lpEn = 1U;
920 if (ret != K_SPP_OK)
921 {
922 return ret;
923 }
924
925 pwrMgmt2Reg.value = 0x7FU;
927 if (ret != K_SPP_OK)
928 {
929 return ret;
930 }
931
932 pwrMgmt1Reg.bits.sleep = 1U;
934 if (ret != K_SPP_OK)
935 {
936 return ret;
937 }
938
940
941 pwrMgmt1Reg.value = 0U;
942 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
943 pwrMgmt1Reg.bits.lpEn = 1U;
945 if (ret != K_SPP_OK)
946 {
947 return ret;
948 }
949
951
952 pwrMgmt1Reg.value = 0U;
953 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
955 if (ret != K_SPP_OK)
956 {
957 return ret;
958 }
959
961 }
962
963 /* AK09916 magnetometer: soft-reset */
965 if (ret != K_SPP_OK)
966 {
967 return ret;
968 }
969
970 SPP_LOGI(K_ICM20948_LOG_TAG, "Phase: mag soft-reset");
972 if (ret != K_SPP_OK)
973 {
974 return ret;
975 }
976
978 if (ret != K_SPP_OK)
979 {
980 return ret;
981 }
982
983 ret = SPP_SERVICES_ICM20948_writeReg(p_spi, 0x0DU, 0x00U);
984 if (ret != K_SPP_OK)
985 {
986 return ret;
987 }
988
989 ret = SPP_SERVICES_ICM20948_writeReg(p_spi, 0x11U, 0x00U);
990 if (ret != K_SPP_OK)
991 {
992 return ret;
993 }
994
996 if (ret != K_SPP_OK)
997 {
998 return ret;
999 }
1000
1002 if (ret != K_SPP_OK)
1003 {
1004 return ret;
1005 }
1006
1008 if (ret != K_SPP_OK)
1009 {
1010 return ret;
1011 }
1012
1014 if (ret != K_SPP_OK)
1015 {
1016 return ret;
1017 }
1018
1020 if (ret != K_SPP_OK)
1021 {
1022 return ret;
1023 }
1024
1026 if (ret != K_SPP_OK)
1027 {
1028 return ret;
1029 }
1030
1032 if (ret != K_SPP_OK)
1033 {
1034 return ret;
1035 }
1036
1037 {
1038 ICM20948_RegUserCtrl_t userCtrlReg = {.value = 0U};
1039
1040 userCtrlReg.bits.i2cIfDis = 1U;
1041 userCtrlReg.bits.i2cMstEn = 1U;
1043 if (ret != K_SPP_OK)
1044 {
1045 return ret;
1046 }
1047
1049
1050 userCtrlReg.value = 0U;
1051 userCtrlReg.bits.i2cIfDis = 1U;
1053 if (ret != K_SPP_OK)
1054 {
1055 return ret;
1056 }
1057 }
1058
1060 if (ret != K_SPP_OK)
1061 {
1062 return ret;
1063 }
1064
1066 if (ret != K_SPP_OK)
1067 {
1068 return ret;
1069 }
1070
1072 if (ret != K_SPP_OK)
1073 {
1074 return ret;
1075 }
1076
1078 if (ret != K_SPP_OK)
1079 {
1080 return ret;
1081 }
1082
1083 {
1084 ICM20948_RegUserCtrl_t userCtrlReg = {.value = 0U};
1085
1086 userCtrlReg.bits.i2cIfDis = 1U;
1088 if (ret != K_SPP_OK)
1089 {
1090 return ret;
1091 }
1092 }
1093
1094 SPP_LOGI(K_ICM20948_LOG_TAG, "Phase: cpass+b2s matrix");
1095 for (spp_uint8_t i = 0U; i < (sizeof(s_cpassMatrix) / sizeof(s_cpassMatrix[0])); i++)
1096 {
1097 ret = SPP_SERVICES_ICM20948_dmpWrite32(p_data, s_cpassMatrix[i].addr, s_cpassMatrix[i].val);
1098 if (ret != K_SPP_OK)
1099 {
1100 return ret;
1101 }
1102
1104 if (ret != K_SPP_OK)
1105 {
1106 return ret;
1107 }
1108 }
1109
1111
1112 for (spp_uint8_t i = 0U; i < (sizeof(s_b2sMatrix) / sizeof(s_b2sMatrix[0])); i++)
1113 {
1114 ret = SPP_SERVICES_ICM20948_dmpWrite32(p_data, s_b2sMatrix[i].addr, s_b2sMatrix[i].val);
1115 if (ret != K_SPP_OK)
1116 {
1117 return ret;
1118 }
1119
1121 if (ret != K_SPP_OK)
1122 {
1123 return ret;
1124 }
1125 }
1126
1128
1130 if (ret != K_SPP_OK)
1131 {
1132 return ret;
1133 }
1134
1135 {
1136 ICM20948_RegAccelConfig_t accelConfigReg = {.value = 0U};
1137 ICM20948_RegAccelConfig2_t accelConfig2Reg = {.value = 0U};
1138
1139 accelConfigReg.bits.accelFsSel = K_ICM20948_ACCEL_FS_4G;
1141 if (ret != K_SPP_OK)
1142 {
1143 return ret;
1144 }
1145
1147 if (ret != K_SPP_OK)
1148 {
1149 return ret;
1150 }
1151 }
1152
1154 if (ret != K_SPP_OK)
1155 {
1156 return ret;
1157 }
1158
1160 if (ret != K_SPP_OK)
1161 {
1162 return ret;
1163 }
1164
1166 if (ret != K_SPP_OK)
1167 {
1168 return ret;
1169 }
1170
1172 if (ret != K_SPP_OK)
1173 {
1174 return ret;
1175 }
1176
1178 if (ret != K_SPP_OK)
1179 {
1180 return ret;
1181 }
1182
1184 if (ret != K_SPP_OK)
1185 {
1186 return ret;
1187 }
1188
1189 {
1190 ICM20948_RegGyroConfig_t gyroConfigReg = {.value = 0U};
1191
1192 gyroConfigReg.bits.gyroFchoice = 1U;
1193 gyroConfigReg.bits.gyroFsSel = K_ICM20948_GYRO_FS_2000DPS;
1195 if (ret != K_SPP_OK)
1196 {
1197 return ret;
1198 }
1199
1200 ret = SPP_SERVICES_ICM20948_writeReg(p_spi, 0x02U, 0x00U);
1201 if (ret != K_SPP_OK)
1202 {
1203 return ret;
1204 }
1205 }
1206
1208 if (ret != K_SPP_OK)
1209 {
1210 return ret;
1211 }
1212
1214 if (ret != K_SPP_OK)
1215 {
1216 return ret;
1217 }
1218
1220 if (ret != K_SPP_OK)
1221 {
1222 return ret;
1223 }
1224
1226 if (ret != K_SPP_OK)
1227 {
1228 return ret;
1229 }
1230
1232 if (ret != K_SPP_OK)
1233 {
1234 return ret;
1235 }
1236
1238 if (ret != K_SPP_OK)
1239 {
1240 return ret;
1241 }
1242
1243 pllTrim = (spp_int8_t)pllRaw;
1244
1246 if (ret != K_SPP_OK)
1247 {
1248 return ret;
1249 }
1250
1252 if (ret != K_SPP_OK)
1253 {
1254 return ret;
1255 }
1256
1259 if (ret != K_SPP_OK)
1260 {
1261 return ret;
1262 }
1263
1265 if (ret != K_SPP_OK)
1266 {
1267 return ret;
1268 }
1269
1270 {
1271 ICM20948_RegLpConf_t lpConfReg = {.value = 0U};
1272 ICM20948_RegPwrMgmt1_t pwrMgmt1Reg = {.value = 0U};
1273
1274 lpConfReg.bits.i2cMstCyc = 1U;
1276 if (ret != K_SPP_OK)
1277 {
1278 return ret;
1279 }
1280
1281 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
1283 if (ret != K_SPP_OK)
1284 {
1285 return ret;
1286 }
1287 }
1288
1289 SPP_LOGI(K_ICM20948_LOG_TAG, "Phase: 3-seq DMP enable loop");
1290 for (spp_uint8_t seq = 0U; seq < 3U; seq++)
1291 {
1292 ICM20948_RegUserCtrl_t userCtrlReg = {.value = 0U};
1293 ICM20948_RegPwrMgmt2_t pwrMgmt2Reg = {.value = 0U};
1294 ICM20948_RegPwrMgmt1_t pwrMgmt1Reg = {.value = 0U};
1295
1296 if (seq == 0U)
1297 {
1298 userCtrlReg.bits.i2cIfDis = 1U;
1299 }
1300 else
1301 {
1302 /* 0xD0 = DMP_EN + FIFO_EN + I2C_IF_DIS; I2C master not yet enabled here. */
1303 userCtrlReg.bits.i2cIfDis = 1U;
1304 userCtrlReg.bits.fifoEn = 1U;
1305 userCtrlReg.bits.dmpEn = 1U;
1306 }
1307
1309 if (ret != K_SPP_OK)
1310 {
1311 return ret;
1312 }
1313
1314 pwrMgmt2Reg.value = 0x47U;
1316 if (ret != K_SPP_OK)
1317 {
1318 return ret;
1319 }
1320
1321 /* Second write: same 0xD0 value. */
1322 userCtrlReg.value = 0U;
1323 userCtrlReg.bits.i2cIfDis = 1U;
1324 userCtrlReg.bits.fifoEn = 1U;
1325 userCtrlReg.bits.dmpEn = 1U;
1327 if (ret != K_SPP_OK)
1328 {
1329 return ret;
1330 }
1331
1332 ret = ICM20948_dmpWriteOutputConfig(p_data, 0x0000U, 0x0000U);
1333 if (ret != K_SPP_OK)
1334 {
1335 return ret;
1336 }
1337
1338 if (seq < 2U)
1339 {
1340 pwrMgmt2Reg.value = 0x7FU;
1342 if (ret != K_SPP_OK)
1343 {
1344 return ret;
1345 }
1346
1347 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
1348 pwrMgmt1Reg.bits.sleep = 1U;
1350 if (ret != K_SPP_OK)
1351 {
1352 return ret;
1353 }
1354
1356
1357 pwrMgmt1Reg.value = 0U;
1358 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
1360 if (ret != K_SPP_OK)
1361 {
1362 return ret;
1363 }
1364
1366 if (ret != K_SPP_OK)
1367 {
1368 return ret;
1369 }
1370
1372 if (ret != K_SPP_OK)
1373 {
1374 return ret;
1375 }
1376
1378 if (ret != K_SPP_OK)
1379 {
1380 return ret;
1381 }
1382 }
1383 }
1384
1385 SPP_LOGI(K_ICM20948_LOG_TAG, "Phase: final output config + mag start");
1386 ret = ICM20948_dmpWriteOutputConfig(p_data, 0xE400U, 0x03C0U);
1387 if (ret != K_SPP_OK)
1388 {
1389 return ret;
1390 }
1391
1393 if (ret != K_SPP_OK)
1394 {
1395 return ret;
1396 }
1397
1399 if (ret != K_SPP_OK)
1400 {
1401 return ret;
1402 }
1403
1405 if (ret != K_SPP_OK)
1406 {
1407 return ret;
1408 }
1409
1411 if (ret != K_SPP_OK)
1412 {
1413 return ret;
1414 }
1415
1417 if (ret != K_SPP_OK)
1418 {
1419 return ret;
1420 }
1421
1423 if (ret != K_SPP_OK)
1424 {
1425 return ret;
1426 }
1427
1429 if (ret != K_SPP_OK)
1430 {
1431 return ret;
1432 }
1433
1435 if (ret != K_SPP_OK)
1436 {
1437 return ret;
1438 }
1439
1441 if (ret != K_SPP_OK)
1442 {
1443 return ret;
1444 }
1445
1447 if (ret != K_SPP_OK)
1448 {
1449 return ret;
1450 }
1451
1453 if (ret != K_SPP_OK)
1454 {
1455 return ret;
1456 }
1457
1459 if (ret != K_SPP_OK)
1460 {
1461 return ret;
1462 }
1463
1465 if (ret != K_SPP_OK)
1466 {
1467 return ret;
1468 }
1469
1471 if (ret != K_SPP_OK)
1472 {
1473 return ret;
1474 }
1475
1477 if (ret != K_SPP_OK)
1478 {
1479 return ret;
1480 }
1481
1484 if (ret != K_SPP_OK)
1485 {
1486 return ret;
1487 }
1488
1489 {
1490 ICM20948_RegPwrMgmt2_t pwrMgmt2Reg = {.value = 0U};
1491
1492 pwrMgmt2Reg.value = 0x40U;
1494 if (ret != K_SPP_OK)
1495 {
1496 return ret;
1497 }
1498 }
1499
1501 if (ret != K_SPP_OK)
1502 {
1503 return ret;
1504 }
1505
1507 if (ret != K_SPP_OK)
1508 {
1509 return ret;
1510 }
1511
1513 if (ret != K_SPP_OK)
1514 {
1515 return ret;
1516 }
1517
1519 if (ret != K_SPP_OK)
1520 {
1521 return ret;
1522 }
1523
1525 if (ret != K_SPP_OK)
1526 {
1527 return ret;
1528 }
1529
1531 if (ret != K_SPP_OK)
1532 {
1533 return ret;
1534 }
1535
1537 if (ret != K_SPP_OK)
1538 {
1539 return ret;
1540 }
1541
1543 if (ret != K_SPP_OK)
1544 {
1545 return ret;
1546 }
1547
1549 if (ret != K_SPP_OK)
1550 {
1551 return ret;
1552 }
1553
1555 if (ret != K_SPP_OK)
1556 {
1557 return ret;
1558 }
1559
1561 if (ret != K_SPP_OK)
1562 {
1563 return ret;
1564 }
1565
1566 {
1567 ICM20948_RegUserCtrl_t userCtrlReg = {.value = 0U};
1568
1569 userCtrlReg.bits.i2cIfDis = 1U;
1570 userCtrlReg.bits.i2cMstEn = 1U;
1571 userCtrlReg.bits.fifoEn = 1U;
1572 userCtrlReg.bits.dmpEn = 1U;
1574 if (ret != K_SPP_OK)
1575 {
1576 return ret;
1577 }
1578
1579 userCtrlReg.bits.dmpRst = 1U;
1581 if (ret != K_SPP_OK)
1582 {
1583 return ret;
1584 }
1585 }
1586
1588
1590 if (ret != K_SPP_OK)
1591 {
1592 return ret;
1593 }
1594
1595 {
1596 ICM20948_RegPwrMgmt1_t pwrMgmt1Reg = {.value = 0U};
1597
1598 pwrMgmt1Reg.bits.clkSel = K_ICM20948_CLK_AUTO;
1599 pwrMgmt1Reg.bits.lpEn = 1U;
1601 if (ret != K_SPP_OK)
1602 {
1603 return ret;
1604 }
1605 }
1606
1607 ret = ICM20948_dmpWriteOutputConfig(p_data, 0xE400U, 0x03C0U);
1608 if (ret != K_SPP_OK)
1609 {
1610 return ret;
1611 }
1612
1614 if (ret != K_SPP_OK)
1615 {
1616 return ret;
1617 }
1618
1620 if (ret != K_SPP_OK)
1621 {
1622 return ret;
1623 }
1624
1625 return K_SPP_OK;
1626}
1627
1628/* ----------------------------------------------------------------
1629 * Driver — FIFO and sensor data
1630 * ---------------------------------------------------------------- */
1631
1632/**
1633 * @brief Reads and parses DMP FIFO packets, updating the sensor data fields.
1634 *
1635 * Checks INT_STATUS for a raw-data-ready flag, reads the FIFO count, and
1636 * processes each complete DMP packet. Resets the FIFO if the count exceeds
1637 * K_ICM20948_FIFO_RESET_THRESHOLD.
1638 *
1639 * @param p_ctx Pointer to the ICM20948 driver context.
1640 */
1642{
1643 void *p_spi = p_ctx->p_spi;
1644 spp_uint8_t txRxData[3] = {0U};
1645 SPP_RetVal_t ret;
1646
1647 if (p_spi == NULL)
1648 {
1649 return;
1650 }
1651
1652 txRxData[0] = K_READ_OP | K_ICM20948_REG_INT_STATUS;
1653 txRxData[1] = K_WRITE_OP;
1654 txRxData[2] = K_WRITE_OP;
1655
1656 ret = SPP_HAL_SPI_transmit(p_spi, txRxData, 3U);
1657 if (ret != K_SPP_OK)
1658 {
1659 return;
1660 }
1661
1662 {
1663 spp_uint8_t intStatus = txRxData[1];
1664
1666 txRxData[1] = K_WRITE_OP;
1667 txRxData[2] = K_WRITE_OP;
1668
1669 ret = SPP_HAL_SPI_transmit(p_spi, txRxData, 3U);
1670 if (ret != K_SPP_OK)
1671 {
1672 return;
1673 }
1674
1675 if ((intStatus & 0x02U) != 0U)
1676 {
1677 txRxData[0] = K_READ_OP | K_ICM20948_REG_FIFO_COUNTH;
1678 txRxData[1] = K_WRITE_OP;
1679 txRxData[2] = K_WRITE_OP;
1680
1681 ret = SPP_HAL_SPI_transmit(p_spi, txRxData, 3U);
1682 if (ret != K_SPP_OK)
1683 {
1684 return;
1685 }
1686
1687 {
1688 spp_uint16_t fifoCount = ((spp_uint16_t)txRxData[1] << 8) | txRxData[2];
1689
1690 {
1691 static spp_bool_t s_logged = false;
1692 if (!s_logged)
1693 {
1694 SPP_LOGI(K_ICM20948_LOG_TAG, "FIFO first count=%u pktsz=%u", (unsigned)fifoCount,
1696 s_logged = true;
1697 }
1698 }
1699
1700 if (fifoCount > K_ICM20948_FIFO_RESET_THRESHOLD)
1701 {
1703 return;
1704 }
1705
1706 {
1707 spp_uint16_t numPackets = fifoCount / K_ICM20948_DMP_PACKET_SIZE_BYTES;
1708
1709 for (spp_uint16_t i = 0U; i < numPackets; i++)
1710 {
1712
1713 ret = readFifoBurst(p_spi, fifoBuffer, K_ICM20948_DMP_PACKET_SIZE_BYTES);
1714 if (ret != K_SPP_OK)
1715 {
1716 return;
1717 }
1718
1719 if (i == 0U)
1720 {
1721 static spp_bool_t s_hdrLogged = false;
1722 if (!s_hdrLogged)
1723 {
1724 SPP_LOGI(K_ICM20948_LOG_TAG, "DMP hdr=%02X%02X accel=%02X%02X quat=%02X%02X",
1725 fifoBuffer[0], fifoBuffer[1], fifoBuffer[2], fifoBuffer[3], fifoBuffer[8],
1726 fifoBuffer[9]);
1727 s_hdrLogged = true;
1728 }
1729 }
1730
1731 {
1732 /* [0:1]=header1, [2:7]=accel, [8:13]=gyro_raw, [14:19]=gyro_bias(discard),
1733 * [20:25]=compass, [26:37]=quat9(Q1,Q2,Q3), [38:39]=heading_acc, [40:41]=footer */
1734
1735 spp_int16_t accelX = (spp_int16_t)(((spp_uint16_t)fifoBuffer[2] << 8) | fifoBuffer[3]);
1736 spp_int16_t accelY = (spp_int16_t)(((spp_uint16_t)fifoBuffer[4] << 8) | fifoBuffer[5]);
1737 spp_int16_t accelZ = (spp_int16_t)(((spp_uint16_t)fifoBuffer[6] << 8) | fifoBuffer[7]);
1738
1739 spp_int16_t gyroX = (spp_int16_t)(((spp_uint16_t)fifoBuffer[8] << 8) | fifoBuffer[9]);
1740 spp_int16_t gyroY = (spp_int16_t)(((spp_uint16_t)fifoBuffer[10] << 8) | fifoBuffer[11]);
1741 spp_int16_t gyroZ = (spp_int16_t)(((spp_uint16_t)fifoBuffer[12] << 8) | fifoBuffer[13]);
1742 /* [14:19] gyro bias — ignored */
1743
1744 spp_int16_t magX = (spp_int16_t)(((spp_uint16_t)fifoBuffer[20] << 8) | fifoBuffer[21]);
1745 spp_int16_t magY = (spp_int16_t)(((spp_uint16_t)fifoBuffer[22] << 8) | fifoBuffer[23]);
1746 spp_int16_t magZ = (spp_int16_t)(((spp_uint16_t)fifoBuffer[24] << 8) | fifoBuffer[25]);
1747
1748 spp_int32_t q1Raw = ((spp_int32_t)fifoBuffer[26] << 24) |
1749 ((spp_int32_t)fifoBuffer[27] << 16) |
1750 ((spp_int32_t)fifoBuffer[28] << 8) | (spp_int32_t)fifoBuffer[29];
1751 spp_int32_t q2Raw = ((spp_int32_t)fifoBuffer[30] << 24) |
1752 ((spp_int32_t)fifoBuffer[31] << 16) |
1753 ((spp_int32_t)fifoBuffer[32] << 8) | (spp_int32_t)fifoBuffer[33];
1754 spp_int32_t q3Raw = ((spp_int32_t)fifoBuffer[34] << 24) |
1755 ((spp_int32_t)fifoBuffer[35] << 16) |
1756 ((spp_int32_t)fifoBuffer[36] << 8) | (spp_int32_t)fifoBuffer[37];
1757 float accuracy = (spp_int16_t)(((spp_uint16_t)fifoBuffer[38] << 8) | fifoBuffer[39]);
1758
1759 float ax = (accelX / 8192.0f) - K_ICM20948_ACCEL_BIAS_X;
1760 float ay = (accelY / 8192.0f) - K_ICM20948_ACCEL_BIAS_Y;
1761 float az = (accelZ / 8192.0f) - K_ICM20948_ACCEL_BIAS_Z;
1762
1763 float gx = (gyroX / 32.8f) - K_ICM20948_GYRO_BIAS_X;
1764 float gy = (gyroY / 32.8f) - K_ICM20948_GYRO_BIAS_Y;
1765 float gz = (gyroZ / 32.8f) - K_ICM20948_GYRO_BIAS_Z;
1766
1767 const spp_float32_t mxRaw = magX * 0.15f;
1768 const spp_float32_t myRaw = magY * 0.15f;
1769 const spp_float32_t mzRaw = magZ * 0.15f;
1770
1771 const spp_float32_t mxCentered = mxRaw - K_ICM20948_MAG_BIAS_X;
1772 const spp_float32_t myCentered = myRaw - K_ICM20948_MAG_BIAS_Y;
1773 const spp_float32_t mzCentered = mzRaw - K_ICM20948_MAG_BIAS_Z;
1774
1775 const spp_float32_t mx = K_ICM20948_MAG_CAL_MATRIX[0][0] * mxCentered +
1776 K_ICM20948_MAG_CAL_MATRIX[0][1] * myCentered +
1777 K_ICM20948_MAG_CAL_MATRIX[0][2] * mzCentered;
1778
1779 const spp_float32_t my = K_ICM20948_MAG_CAL_MATRIX[1][0] * mxCentered +
1780 K_ICM20948_MAG_CAL_MATRIX[1][1] * myCentered +
1781 K_ICM20948_MAG_CAL_MATRIX[1][2] * mzCentered;
1782
1783 const spp_float32_t mz = K_ICM20948_MAG_CAL_MATRIX[2][0] * mxCentered +
1784 K_ICM20948_MAG_CAL_MATRIX[2][1] * myCentered +
1785 K_ICM20948_MAG_CAL_MATRIX[2][2] * mzCentered;
1786
1787 float qx = q1Raw / 1073741824.0f;
1788 float qy = q2Raw / 1073741824.0f;
1789 float qz = q3Raw / 1073741824.0f;
1790 float qwSq = 1.0f - (qx * qx) - (qy * qy) - (qz * qz);
1791 float qw = (qwSq > 0.0f) ? sqrtf(qwSq) : 0.0f;
1792
1793 p_ctx->lastData.ax = ax;
1794 p_ctx->lastData.ay = ay;
1795 p_ctx->lastData.az = az;
1796 p_ctx->lastData.gx = gx;
1797 p_ctx->lastData.gy = gy;
1798 p_ctx->lastData.gz = gz;
1799 p_ctx->lastData.mx = mx;
1800 p_ctx->lastData.my = my;
1801 p_ctx->lastData.mz = mz;
1802 p_ctx->lastData.qw = qw;
1803 p_ctx->lastData.qx = qx;
1804 p_ctx->lastData.qy = qy;
1805 p_ctx->lastData.qz = qz;
1806 p_ctx->lastData.accuracy = accuracy;
1807 p_ctx->lastData.dataReady = true;
1808 }
1809 }
1810 }
1811 }
1812 }
1813 }
1814}
SPP_Packet_t * SPP_SERVICES_DATABANK_getPacket(void)
Acquire a free packet from the pool.
Definition databank.c:53
SPP_RetVal_t SPP_SERVICES_DATABANK_returnPacket(SPP_Packet_t *p_packet)
Return a packet to the pool after processing.
Definition databank.c:64
SPP_RetVal_t SPP_SERVICES_DATABANK_packetData(SPP_Packet_t *p_packet, spp_uint16_t apid, spp_uint16_t seq, const void *p_data, spp_uint16_t dataLen)
Fill a packet with data and compute its CRC.
Definition databank.c:117
Static packet pool manager.
Finite State Machine (FSM) module for the SPP core.
SPP_RetVal_t SPP_HAL_GPIO_registerIsr(spp_uint32_t pin, void *p_isrCtx)
Register an ISR handler for a GPIO pin.
Definition gpio.c:37
SPP_RetVal_t SPP_HAL_GPIO_configInterrupt(spp_uint32_t pin, spp_uint32_t intrType, spp_uint32_t pull)
Configure a GPIO pin as an interrupt input.
Definition gpio.c:18
SPP GPIO HAL API — dispatches through the registered HAL port.
#define K_ICM20948_FIFO_RESET_THRESHOLD
Definition icm20948.c:43
#define K_ICM20948_DMP_START_ADDR_MSB
Definition icm20948.c:44
static SPP_RetVal_t readFifoBurst(void *p_spi, spp_uint8_t *p_dst, spp_uint8_t count)
Reads count bytes sequentially from the FIFO_R_W register.
Definition icm20948.c:314
#define K_READ_OP
Definition icm20948.c:35
#define K_ICM20948_WHO_AM_I_VALUE
Definition icm20948.c:41
static SPP_RetVal_t SPP_SERVICES_ICM20948_initGpio(ICM20948_Data_t *p_icm)
Initialises the ICM20948 interrupt context and registers the GPIO ISR.
Definition icm20948.c:131
static spp_int32_t SPP_SERVICES_ICM20948_calcGyroSf(spp_int8_t pll)
Computes the DMP gyroscope scale factor from the PLL trim value.
Definition icm20948.c:505
static SPP_RetVal_t SPP_SERVICES_ICM20948_lpWakeCycle(void *p_data)
Toggles PWR_MGMT_1 into low-power wake-cycle mode then back to normal.
Definition icm20948.c:474
static SPP_RetVal_t SPP_SERVICES_ICM20948_readReg(void *p_spi, spp_uint8_t reg, spp_uint8_t *p_value)
Reads a single byte from an ICM-20948 register over SPI.
Definition icm20948.c:289
static SPP_RetVal_t SPP_SERVICES_ICM20948_dmpWrite32(void *p_data, spp_uint16_t addr, spp_uint32_t value)
Writes a 32-bit big-endian value to DMP memory at addr.
Definition icm20948.c:447
#define K_ICM20948_LOG_TAG
Definition icm20948.c:46
static const spp_uint8_t s_dmp3Image[]
Definition icm20948.c:85
static SPP_RetVal_t SPP_SERVICES_ICM20948_configDmpInit(void *p_data)
Executes the full DMP initialisation sequence.
Definition icm20948.c:651
const SPP_SERVICE_ProducerContract_t * SPP_SERVICES_ICM20948_getProducerContract(void)
Returns a pointer to the ICM20948 producer contract.
Definition icm20948.c:113
static SPP_RetVal_t SPP_SERVICES_ICM20948_init(void)
Service init callback — sets hardware config, registers GPIO ISR, loads DMP firmware.
Definition icm20948.c:149
#define K_ICM20948_DMP_PACKET_SIZE_BYTES
Definition icm20948.c:42
static SPP_RetVal_t SPP_SERVICES_ICM20948_writeReg(void *p_spi, spp_uint8_t reg, spp_uint8_t value)
Writes a single byte to an ICM-20948 register over SPI.
Definition icm20948.c:271
#define K_WRITE_OP
Definition icm20948.c:36
static const spp_float32_t K_ICM20948_MAG_CAL_MATRIX[3][3]
Definition icm20948.c:99
static void SPP_SERVICES_ICM20948_checkFifoData(ICM20948_t *p_ctx)
Reads and parses DMP FIFO packets, updating the sensor data fields.
Definition icm20948.c:1641
static SPP_RetVal_t SPP_SERVICES_ICM20948_loadDmp(void *p_data)
Loads the DMP3 firmware image into ICM-20948 SRAM.
Definition icm20948.c:570
static SPP_RetVal_t SPP_SERVICES_ICM20948_dmpWrite16(void *p_data, spp_uint16_t addr, spp_uint16_t value)
Writes a 16-bit big-endian value to DMP memory at addr.
Definition icm20948.c:462
static SPP_RetVal_t SPP_SERVICES_ICM20948_dmpWriteBytes(void *p_data, spp_uint16_t addr, const spp_uint8_t *p_bytes, spp_uint8_t len)
Writes len bytes to DMP memory starting at address addr.
Definition icm20948.c:405
static ICM20948_t s_icmData
Definition icm20948.c:89
static const SPP_SERVICE_ProducerContract_t s_icm20948ProducerContract
Definition icm20948.c:91
#define K_ICM20948_DMP_DIVIDER
Definition icm20948.c:40
static SPP_RetVal_t SPP_SERVICES_ICM20948_resetFifo(void *p_spi)
Resets all five FIFO streams by toggling the FIFO_RST bits.
Definition icm20948.c:375
static SPP_RetVal_t SPP_SERVICES_ICM20948_setBank(void *p_spi, ICM20948_RegBank_t regBank)
Selects the active register bank by writing REG_BANK_SEL.
Definition icm20948.c:345
static SPP_RetVal_t SPP_SERVICES_ICM20948_acquireData(SPP_Kpid_t kpid)
Reads DMP FIFO data and publishes an SPP packet.
Definition icm20948.c:203
#define K_ICM20948_DMP_START_ADDR_LSB
Definition icm20948.c:45
static SPP_RetVal_t ICM20948_dmpWriteOutputConfig(void *p_data, spp_uint16_t outCtl1, spp_uint16_t motionEvent)
Programs the DMP output control and motion event registers.
Definition icm20948.c:526
ICM20948 IMU driver and SPP service.
#define K_ICM20948_ACCEL_BIAS_Z
Definition icm20948.h:181
#define K_ICM20948_MAG_WR_ADDR
Definition icm20948.h:154
#define K_ICM20948_DMP_DATA_INTR_CTL
Definition icm20948.h:114
#define K_ICM20948_I2C_SLV1_REG
Definition icm20948.h:101
#define K_ICM20948_INT_PIN_NUM
Definition icm20948.h:50
#define K_ICM20948_MAG_BIAS_Z
Definition icm20948.h:189
#define K_ICM20948_REG_FIFO_RST
Definition icm20948.h:72
#define K_ICM20948_REG_TIMEBASE_CORRECTION_PLL
Definition icm20948.h:81
ICM20948_RegBank_t
ICM20948 register bank selector.
Definition icm20948.h:197
@ K_ICM20948_REG_BANK_2
Definition icm20948.h:200
@ K_ICM20948_REG_BANK_1
Definition icm20948.h:199
@ K_ICM20948_REG_BANK_0
Definition icm20948.h:198
@ K_ICM20948_REG_BANK_3
Definition icm20948.h:201
#define K_ICM20948_DMP_B2S_MTX_21
Definition icm20948.h:150
#define K_ICM20948_DMP_CPASS_TIME_BUFFER
Definition icm20948.h:122
#define K_ICM20948_REG_INT_ENABLE
Definition icm20948.h:63
#define K_ICM20948_I2C_SLV1_CTRL
Definition icm20948.h:102
#define K_ICM20948_REG_PWR_MGMT_1
Definition icm20948.h:61
#define K_ICM20948_MAG_BIAS_X
Definition icm20948.h:187
#define K_ICM20948_DMP_ODR_QUAT6
Definition icm20948.h:117
#define K_ICM20948_ACCEL_BIAS_X
Definition icm20948.h:179
#define K_ICM20948_REG_PWR_MGMT_2
Definition icm20948.h:62
#define K_ICM20948_REG_WHO_AM_I
Definition icm20948.h:58
#define K_ICM20948_REG_FIFO_EN_2
Definition icm20948.h:71
#define K_ICM20948_DMP_FIFO_WATERMARK
Definition icm20948.h:125
#define K_ICM20948_DMP_B2S_MTX_01
Definition icm20948.h:144
#define K_ICM20948_REG_ACCEL_SMPLRT_DIV_2
Definition icm20948.h:87
#define K_ICM20948_DMP_GYRO_SF
Definition icm20948.h:123
#define K_ICM20948_DMP_ACC_SCALE2
Definition icm20948.h:129
#define K_ICM20948_TASK_TIMEOUT_MS
Definition icm20948.h:37
#define K_ICM20948_INT_INTR_TYPE
Definition icm20948.h:51
#define K_ICM20948_MAG_CTRL_2
Definition icm20948.h:156
#define K_ICM20948_REG_DMP_ADDR_LSB
Definition icm20948.h:91
#define K_ICM20948_REG_DMP_ADDR_MSB
Definition icm20948.h:90
#define K_ICM20948_I2C_SLV1_DO
Definition icm20948.h:103
#define K_ICM20948_I2C_SLV1_ADDR
Definition icm20948.h:100
#define K_ICM20948_DMP_B2S_MTX_00
Definition icm20948.h:143
#define K_ICM20948_GYRO_BIAS_Y
Definition icm20948.h:184
#define K_ICM20948_REG_ACCEL_SMPLRT_DIV_1
Definition icm20948.h:86
#define K_ICM20948_GYRO_BIAS_X
Definition icm20948.h:183
#define K_ICM20948_DMP_B2S_MTX_11
Definition icm20948.h:147
#define K_ICM20948_INT_PULL
Definition icm20948.h:52
#define K_ICM20948_DMP_ODR_CPASS
Definition icm20948.h:120
#define K_ICM20948_DMP_B2S_MTX_22
Definition icm20948.h:151
#define K_ICM20948_REG_HW_FIX_DISABLE
Definition icm20948.h:77
#define K_ICM20948_REG_SLV0_REG
Definition icm20948.h:97
#define K_ICM20948_DMP_DATA_OUT_CTL1
Definition icm20948.h:112
#define K_ICM20948_REG_LP_CONF
Definition icm20948.h:60
#define K_ICM20948_DMP_CPASS_MTX_00
Definition icm20948.h:130
#define K_ICM20948_DMP_B2S_MTX_02
Definition icm20948.h:145
#define K_ICM20948_DMP_DATA_OUT_CTL2
Definition icm20948.h:113
#define K_ICM20948_DMP_ACCEL_ONLY_GAIN
Definition icm20948.h:128
#define K_ICM20948_REG_BANK_SEL
Definition icm20948.h:55
#define K_ICM20948_DMP_BAC_RATE
Definition icm20948.h:126
@ K_ICM20948_ACCEL_FS_4G
Definition icm20948.h:462
#define K_ICM20948_REG_GYRO_CONFIG
Definition icm20948.h:85
#define K_ICM20948_DMP_ODR_QUAT9
Definition icm20948.h:118
#define K_ICM20948_DMP_ODR_ACCEL
Definition icm20948.h:121
#define K_ICM20948_REG_FIFO_R_W
Definition icm20948.h:76
#define K_ICM20948_DMP_CPASS_MTX_20
Definition icm20948.h:136
#define K_ICM20948_I2C_MST_ODR_CONFIG
Definition icm20948.h:94
#define K_ICM20948_REG_USER_CTRL
Definition icm20948.h:59
@ K_ICM20948_GYRO_FS_2000DPS
Definition icm20948.h:442
#define K_ICM20948_DMP_CPASS_MTX_01
Definition icm20948.h:131
#define K_ICM20948_REG_GYRO_SMPLRT_DIV
Definition icm20948.h:84
#define K_ICM20948_DMP_CPASS_MTX_12
Definition icm20948.h:135
#define K_ICM20948_DMP_MOTION_EVENT_CTL
Definition icm20948.h:115
#define K_ICM20948_DMP_ACCEL_A_VAR
Definition icm20948.h:141
#define K_ICM20948_DMP_ACC_SCALE
Definition icm20948.h:124
#define K_ICM20948_DMP_LOAD_START
Definition icm20948.h:109
#define K_ICM20948_DMP_ACCEL_CAL_INIT
Definition icm20948.h:142
#define K_ICM20948_REG_I2C_CTRL
Definition icm20948.h:95
#define K_ICM20948_DMP_B2S_RATE
Definition icm20948.h:127
#define K_ICM20948_GYRO_BIAS_Z
Definition icm20948.h:185
#define K_ICM20948_DMP_CPASS_MTX_11
Definition icm20948.h:134
#define K_ICM20948_REG_SLV0_CTRL
Definition icm20948.h:98
#define K_ICM20948_MAG_RD_ADDR
Definition icm20948.h:155
#define K_ICM20948_DMP_B2S_MTX_20
Definition icm20948.h:149
#define K_ICM20948_REG_FIFO_CFG
Definition icm20948.h:78
#define K_ICM20948_REG_MEM_START_ADDR
Definition icm20948.h:106
#define K_ICM20948_REG_SLV0_ADDR
Definition icm20948.h:96
#define K_ICM20948_DMP_CPASS_MTX_10
Definition icm20948.h:133
#define K_ICM20948_DMP_B2S_MTX_10
Definition icm20948.h:146
#define K_ICM20948_DMP_CPASS_MTX_21
Definition icm20948.h:137
#define K_ICM20948_DMP_ODR_GYRO
Definition icm20948.h:119
#define K_ICM20948_REG_ACCEL_CONFIG_2
Definition icm20948.h:89
#define K_ICM20948_REG_INT_ENABLE_2
Definition icm20948.h:65
#define K_ICM20948_SPI_HOST_USED
Definition icm20948.h:40
#define K_ICM20948_DMP_CPASS_MTX_22
Definition icm20948.h:138
#define K_ICM20948_REG_INT_STATUS
Definition icm20948.h:67
@ K_ICM20948_CLK_AUTO
Definition icm20948.h:251
#define K_ICM20948_REG_FIFO_EN_1
Definition icm20948.h:70
#define K_ICM20948_REG_MEM_R_W
Definition icm20948.h:107
#define K_ICM20948_REG_ACCEL_CONFIG
Definition icm20948.h:88
#define K_ICM20948_DMP_ACCEL_ALPHA_VAR
Definition icm20948.h:140
#define K_ICM20948_MAG_BIAS_Y
Definition icm20948.h:188
#define K_ICM20948_DMP_B2S_MTX_12
Definition icm20948.h:148
#define K_ICM20948_REG_MEM_BANK_SEL
Definition icm20948.h:108
#define K_ICM20948_DMP_GYRO_FULLSCALE
Definition icm20948.h:139
#define K_ICM20948_DMP_DATA_RDY_STATUS
Definition icm20948.h:116
#define K_ICM20948_REG_DMP_INT_STATUS
Definition icm20948.h:66
#define K_ICM20948_REG_FIFO_COUNTH
Definition icm20948.h:74
#define K_ICM20948_REG_SINGLE_FIFO_PRIORITY_SEL
Definition icm20948.h:68
#define K_ICM20948_ACCEL_BIAS_Y
Definition icm20948.h:180
#define K_ICM20948_DMP_CPASS_MTX_02
Definition icm20948.h:132
SPP logging service.
#define SPP_LOGI(tag, fmt,...)
Definition log.h:69
#define SPP_LOGE(tag, fmt,...)
Definition log.h:67
SPP_Packet_t
Definition packet.h:79
SPP_RetVal_t SPP_SERVICES_PUBSUB_publish(SPP_Packet_t *p_pkt)
Copies a packet into the internal buffer and returns it to the databank.
Definition pubsub.c:129
Publish-subscribe packet router.
Standard return-value type used across all SPP modules.
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition returnTypes.h:24
@ K_SPP_OK
Definition returnTypes.h:25
@ K_SPP_ERROR
Definition returnTypes.h:26
@ K_SPP_ERROR_NULL_POINTER
Definition returnTypes.h:30
SPP module descriptor and registry API.
void * SPP_HAL_SPI_getHandle(spp_uint8_t deviceIdx)
Obtain the SPI device handle for the given device index.
Definition spi.c:39
SPP_RetVal_t SPP_HAL_SPI_deviceInit(void *p_handle)
Initialise a specific SPI device.
Definition spi.c:49
SPP_RetVal_t SPP_HAL_SPI_transmit(void *p_handle, spp_uint8_t *p_data, spp_uint8_t length)
Performs register-based SPI transactions.
Definition spi.c:79
SPP SPI HAL API — dispatches through the registered HAL port.
ICM20948 interrupt and GPIO context.
Definition icm20948.h:565
spp_uint32_t intPin
Definition icm20948.h:568
spp_uint32_t intPull
Definition icm20948.h:570
volatile spp_bool_t drdyFlag
Definition icm20948.h:566
spp_uint32_t intIntrType
Definition icm20948.h:569
SPP_GpioIsrCtx_t isr_ctx
Definition icm20948.h:567
spp_bool_t dataReady
Definition icm20948.h:558
ICM20948 sensor instance.
Definition icm20948.h:580
spp_uint32_t intIntrType
Definition icm20948.h:584
spp_uint32_t intPull
Definition icm20948.h:585
ICM20948_SensorData_t lastData
Definition icm20948.h:590
spp_uint16_t seq
Definition icm20948.h:591
spp_uint8_t spiDevIdx
Definition icm20948.h:582
ICM20948_Data_t icmData
Definition icm20948.h:589
void * p_spi
Definition icm20948.h:588
spp_uint32_t intPin
Definition icm20948.h:583
volatile spp_bool_t * p_flag
Definition gpio.h:28
Defines the callbacks and metadata of an SPP producer.
Definition service.h:25
void SPP_HAL_TIME_delayMs(spp_uint32_t ms)
Block for the requested number of milliseconds.
Definition time.c:35
SPP HAL time API — dispatches through the registered HAL port.
Portable integer types and SPI/storage configuration types for SPP.
uint16_t spp_uint16_t
Definition types.h:29
uint32_t spp_uint32_t
Definition types.h:31
int8_t spp_int8_t
Definition types.h:28
uint8_t spp_uint8_t
Definition types.h:27
int32_t spp_int32_t
Definition types.h:32
bool spp_bool_t
Definition types.h:36
int64_t spp_int64_t
Definition types.h:34
float spp_float32_t
Definition types.h:35
int16_t spp_int16_t
Definition types.h:30
ACCEL_CONFIG_2 register bit layout.
Definition icm20948.h:482
ACCEL_CONFIG register bit layout.
Definition icm20948.h:469
struct ICM20948_RegAccelConfig_t::@105015127256256334376214066105107137252170116356 bits
REG_BANK_SEL register bit layout.
Definition icm20948.h:206
spp_uint8_t value
Definition icm20948.h:207
struct ICM20948_RegBankSel_t::@325125135051365156165251362113041170261051301056 bits
FIFO_CFG register bit layout.
Definition icm20948.h:410
spp_uint8_t value
Definition icm20948.h:411
FIFO_EN_1 register bit layout.
Definition icm20948.h:355
spp_uint8_t value
Definition icm20948.h:356
FIFO_EN_2 register bit layout.
Definition icm20948.h:369
spp_uint8_t value
Definition icm20948.h:370
FIFO_RST register bit layout.
Definition icm20948.h:384
struct ICM20948_RegFifoRst_t::@041065277126235241047134372226063255114241367201 bits
spp_uint8_t value
Definition icm20948.h:385
GYRO_CONFIG_1 register bit layout.
Definition icm20948.h:447
struct ICM20948_RegGyroConfig_t::@232176310072375053240213106010173004177117347241 bits
HW_FIX_DISABLE register bit layout.
Definition icm20948.h:421
INT_ENABLE_2 register bit layout.
Definition icm20948.h:300
struct ICM20948_RegIntEnable2_t::@250247060317310121157315035247225131256117306115 bits
INT_ENABLE register bit layout.
Definition icm20948.h:288
struct ICM20948_RegIntEnable_t::@106155203114352110244332375130240203066354261177 bits
LP_CONFIG register bit layout.
Definition icm20948.h:235
spp_uint8_t value
Definition icm20948.h:236
struct ICM20948_RegLpConf_t::@166315220372323112372004253100254373042267240270 bits
PWR_MGMT_1 register bit layout.
Definition icm20948.h:257
struct ICM20948_RegPwrMgmt1_t::@253125264070140253032213157135301362240260244352 bits
PWR_MGMT_2 register bit layout.
Definition icm20948.h:272
struct ICM20948_RegPwrMgmt2_t::@375266177224043361340021226124357137226225313223 bits
SINGLE_FIFO_PRIORITY_SEL register bit layout.
Definition icm20948.h:342
USER_CTRL register bit layout.
Definition icm20948.h:218
struct ICM20948_RegUserCtrl_t::@356065150070305204106352010301107244336331356261 bits
spp_uint16_t value
Definition kpid.h:25