stm32h747i_discovery_bus.c
Go to the documentation of this file.
1 
22 /* Includes ------------------------------------------------------------------*/
25 
42 #ifndef I2C_VALID_TIMING_NBR
43  #define I2C_VALID_TIMING_NBR 128U
44 #endif
45 #define I2C_SPEED_FREQ_STANDARD 0U /* 100 kHz */
46 #define I2C_SPEED_FREQ_FAST 1U /* 400 kHz */
47 #define I2C_SPEED_FREQ_FAST_PLUS 2U /* 1 MHz */
48 #define I2C_ANALOG_FILTER_DELAY_MIN 50U /* ns */
49 #define I2C_ANALOG_FILTER_DELAY_MAX 260U /* ns */
50 #define I2C_USE_ANALOG_FILTER 1U
51 #define I2C_DIGITAL_FILTER_COEF 0U
52 #define I2C_PRESC_MAX 16U
53 #define I2C_SCLDEL_MAX 16U
54 #define I2C_SDADEL_MAX 16U
55 #define I2C_SCLH_MAX 256U
56 #define I2C_SCLL_MAX 256U
57 #define SEC2NSEC 1000000000UL
58 
65 typedef struct
66 {
67  uint32_t freq; /* Frequency in Hz */
68  uint32_t freq_min; /* Minimum frequency in Hz */
69  uint32_t freq_max; /* Maximum frequency in Hz */
70  uint32_t hddat_min; /* Minimum data hold time in ns */
71  uint32_t vddat_max; /* Maximum data valid time in ns */
72  uint32_t sudat_min; /* Minimum data setup time in ns */
73  uint32_t lscl_min; /* Minimum low period of the SCL clock in ns */
74  uint32_t hscl_min; /* Minimum high period of SCL clock in ns */
75  uint32_t trise; /* Rise time in ns */
76  uint32_t tfall; /* Fall time in ns */
77  uint32_t dnf; /* Digital noise filter coefficient */
78 } I2C_Charac_t;
79 
80 typedef struct
81 {
82  uint32_t presc; /* Timing prescaler */
83  uint32_t tscldel; /* SCL delay */
84  uint32_t tsdadel; /* SDA delay */
85  uint32_t sclh; /* SCL high period */
86  uint32_t scll; /* SCL low period */
95 static const I2C_Charac_t I2C_Charac[] =
96 {
98  {
99  .freq = 100000,
100  .freq_min = 80000,
101  .freq_max = 120000,
102  .hddat_min = 0,
103  .vddat_max = 3450,
104  .sudat_min = 250,
105  .lscl_min = 4700,
106  .hscl_min = 4000,
107  .trise = 640,
108  .tfall = 20,
110  },
112  {
113  .freq = 400000,
114  .freq_min = 320000,
115  .freq_max = 480000,
116  .hddat_min = 0,
117  .vddat_max = 900,
118  .sudat_min = 100,
119  .lscl_min = 1300,
120  .hscl_min = 600,
121  .trise = 250,
122  .tfall = 100,
124  },
126  {
127  .freq = 1000000,
128  .freq_min = 800000,
129  .freq_max = 1200000,
130  .hddat_min = 0,
131  .vddat_max = 450,
132  .sudat_min = 50,
133  .lscl_min = 500,
134  .hscl_min = 260,
135  .trise = 60,
136  .tfall = 100,
138  },
139 };
147 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
148 static uint32_t IsI2c4MspCbValid = 0;
149 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
150 static uint32_t I2c4InitCounter = 0;
152 static uint32_t I2c_valid_timing_nbr = 0;
153 #if defined(BSP_USE_CMSIS_OS)
154 static osSemaphoreId BspI2cSemaphore = 0;
155 #endif
156 
171 static void I2C4_MspInit(I2C_HandleTypeDef *phi2c);
172 static void I2C4_MspDeInit(I2C_HandleTypeDef *phi2c);
173 static int32_t I2C4_WriteReg(uint16_t DevAddr, uint16_t MemAddSize, uint16_t Reg, uint8_t *pData, uint16_t Length);
174 static int32_t I2C4_ReadReg(uint16_t DevAddr, uint16_t MemAddSize, uint16_t Reg, uint8_t *pData, uint16_t Length);
175 static uint32_t I2C_GetTiming(uint32_t clock_src_freq, uint32_t i2c_freq);
176 static uint32_t I2C_Compute_SCLL_SCLH(uint32_t clock_src_freq, uint32_t I2C_speed);
177 static void I2C_Compute_PRESC_SCLDEL_SDADEL(uint32_t clock_src_freq, uint32_t I2C_speed);
190 int32_t BSP_I2C4_Init(void)
191 {
192  int32_t ret = BSP_ERROR_NONE;
193 
195 
196  if (I2c4InitCounter == 0U)
197  {
198  I2c4InitCounter++;
199 
201  {
202 #if defined(BSP_USE_CMSIS_OS)
203  if(BspI2cSemaphore == NULL)
204  {
205  /* Create semaphore to prevent multiple I2C access */
206  osSemaphoreDef(BSP_I2C_SEM);
207  BspI2cSemaphore = osSemaphoreCreate(osSemaphore(BSP_I2C_SEM), 1);
208  }
209 #endif
210 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 0)
211  /* Init the I2C4 Msp */
213 #else
214  if (IsI2c4MspCbValid == 0U)
215  {
216  if (BSP_I2C4_RegisterDefaultMspCallbacks() != BSP_ERROR_NONE)
217  {
218  ret = BSP_ERROR_MSP_FAILURE;
219  }
220  }
221  if (ret == BSP_ERROR_NONE)
222  {
223 #endif
225  {
226  ret = BSP_ERROR_BUS_FAILURE;
227  }
228 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
229  }
230 #endif
231  }
232  }
233 
234  return ret;
235 }
236 
241 int32_t BSP_I2C4_DeInit(void)
242 {
243  int32_t ret = BSP_ERROR_NONE;
244 
245  I2c4InitCounter--;
246  if (I2c4InitCounter == 0U)
247  {
248 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 0)
250 #endif /* (USE_HAL_I2C_REGISTER_CALLBACKS == 0) */
251 
252  /* Init the I2C */
254  {
255  ret = BSP_ERROR_BUS_FAILURE;
256  }
257  }
258 
259  return ret;
260 }
261 
268 __weak HAL_StatusTypeDef MX_I2C4_Init(I2C_HandleTypeDef *hI2c, uint32_t timing)
269 {
270  HAL_StatusTypeDef status = HAL_OK;
271 
272  hI2c->Init.Timing = timing;
273  hI2c->Init.OwnAddress1 = 0;
276  hI2c->Init.OwnAddress2 = 0;
280 
281  if (HAL_I2C_Init(hI2c) != HAL_OK)
282  {
283  status = HAL_ERROR;
284  }
285  else
286  {
287  uint32_t analog_filter;
288 
289  analog_filter = I2C_ANALOGFILTER_ENABLE;
290  if (HAL_I2CEx_ConfigAnalogFilter(hI2c, analog_filter) != HAL_OK)
291  {
292  status = HAL_ERROR;
293  }
294  else
295  {
297  {
298  status = HAL_ERROR;
299  }
300  }
301  }
302 
303  return status;
304 }
305 
314 int32_t BSP_I2C4_WriteReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
315 {
316  int32_t ret;
317 #if defined(BSP_USE_CMSIS_OS)
318  /* Get semaphore to prevent multiple I2C access */
319  osSemaphoreWait(BspI2cSemaphore, osWaitForever);
320 #endif
321  if(I2C4_WriteReg(DevAddr, Reg, I2C_MEMADD_SIZE_8BIT, pData, Length) == 0)
322  {
323  ret = BSP_ERROR_NONE;
324  }
325  else
326  {
328  {
330  }
331  else
332  {
334  }
335  }
336 #if defined(BSP_USE_CMSIS_OS)
337  /* Release semaphore to prevent multiple I2C access */
338  osSemaphoreRelease(BspI2cSemaphore);
339 #endif
340  return ret;
341 }
342 
351 int32_t BSP_I2C4_ReadReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
352 {
353  int32_t ret;
354 #if defined(BSP_USE_CMSIS_OS)
355  /* Get semaphore to prevent multiple I2C access */
356  osSemaphoreWait(BspI2cSemaphore, osWaitForever);
357 #endif
358  if(I2C4_ReadReg(DevAddr, Reg, I2C_MEMADD_SIZE_8BIT, pData, Length) == 0)
359  {
360  ret = BSP_ERROR_NONE;
361  }
362  else
363  {
365  {
367  }
368  else
369  {
371  }
372  }
373 #if defined(BSP_USE_CMSIS_OS)
374  /* Release semaphore to prevent multiple I2C access */
375  osSemaphoreRelease(BspI2cSemaphore);
376 #endif
377  return ret;
378 }
379 
388 int32_t BSP_I2C4_WriteReg16(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
389 {
390  int32_t ret;
391  #if defined(BSP_USE_CMSIS_OS)
392  /* Get semaphore to prevent multiple I2C access */
393  osSemaphoreWait(BspI2cSemaphore, osWaitForever);
394 #endif
395  if(I2C4_WriteReg(DevAddr, Reg, I2C_MEMADD_SIZE_16BIT, pData, Length) == 0)
396  {
397  ret = BSP_ERROR_NONE;
398  }
399  else
400  {
402  {
404  }
405  else
406  {
408  }
409  }
410 #if defined(BSP_USE_CMSIS_OS)
411  /* Release semaphore to prevent multiple I2C access */
412  osSemaphoreRelease(BspI2cSemaphore);
413 #endif
414  return ret;
415 }
416 
425 int32_t BSP_I2C4_ReadReg16(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
426 {
427  int32_t ret;
428 #if defined(BSP_USE_CMSIS_OS)
429  /* Get semaphore to prevent multiple I2C access */
430  osSemaphoreWait(BspI2cSemaphore, osWaitForever);
431 #endif
432  if(I2C4_ReadReg(DevAddr, Reg, I2C_MEMADD_SIZE_16BIT, pData, Length) == 0)
433  {
434  ret = BSP_ERROR_NONE;
435  }
436  else
437  {
439  {
441  }
442  else
443  {
445  }
446  }
447 #if defined(BSP_USE_CMSIS_OS)
448  /* Release semaphore to prevent multiple I2C access */
449  osSemaphoreRelease(BspI2cSemaphore);
450 #endif
451  return ret;
452 }
453 
461 int32_t BSP_I2C4_IsReady(uint16_t DevAddr, uint32_t Trials)
462 {
463  int32_t ret = BSP_ERROR_NONE;
464 #if defined(BSP_USE_CMSIS_OS)
465  /* Get semaphore to prevent multiple I2C access */
466  osSemaphoreWait(BspI2cSemaphore, osWaitForever);
467 #endif
468  if(HAL_I2C_IsDeviceReady(&hbus_i2c4, DevAddr, Trials, 1000) != HAL_OK)
469  {
470  ret = BSP_ERROR_BUSY;
471  }
472 #if defined(BSP_USE_CMSIS_OS)
473  /* Release semaphore to prevent multiple I2C access */
474  osSemaphoreRelease(BspI2cSemaphore);
475 #endif
476  return ret;
477 }
478 
483 int32_t BSP_GetTick(void)
484 {
485  return (int32_t)HAL_GetTick();
486 }
487 
488 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
489 
493 int32_t BSP_I2C4_RegisterDefaultMspCallbacks (void)
494 {
495  int32_t ret = BSP_ERROR_NONE;
496 #if defined(BSP_USE_CMSIS_OS)
497  /* Get semaphore to prevent multiple I2C access */
498  osSemaphoreWait(BspI2cSemaphore, osWaitForever);
499 #endif
501 
502  /* Register default MspInit/MspDeInit Callback */
503  if(HAL_I2C_RegisterCallback(&hbus_i2c4, HAL_I2C_MSPINIT_CB_ID, I2C4_MspInit) != HAL_OK)
504  {
506  }
507  else if(HAL_I2C_RegisterCallback(&hbus_i2c4, HAL_I2C_MSPDEINIT_CB_ID, I2C4_MspDeInit) != HAL_OK)
508  {
510  }
511  else
512  {
513  IsI2c4MspCbValid = 1U;
514  }
515 #if defined(BSP_USE_CMSIS_OS)
516  /* Release semaphore to prevent multiple I2C access */
517  osSemaphoreRelease(BspI2cSemaphore);
518 #endif
519  /* BSP status */
520  return ret;
521 }
522 
528 int32_t BSP_I2C4_RegisterMspCallbacks (BSP_I2C_Cb_t *Callback)
529 {
530  int32_t ret = BSP_ERROR_NONE;
531 #if defined(BSP_USE_CMSIS_OS)
532  /* Get semaphore to prevent multiple I2C access */
533  osSemaphoreWait(BspI2cSemaphore, osWaitForever);
534 #endif
536 
537  /* Register MspInit/MspDeInit Callbacks */
538  if(HAL_I2C_RegisterCallback(&hbus_i2c4, HAL_I2C_MSPINIT_CB_ID, Callback->pMspI2cInitCb) != HAL_OK)
539  {
541  }
542  else if(HAL_I2C_RegisterCallback(&hbus_i2c4, HAL_I2C_MSPDEINIT_CB_ID, Callback->pMspI2cDeInitCb) != HAL_OK)
543  {
545  }
546  else
547  {
548  IsI2c4MspCbValid = 1U;
549  }
550 #if defined(BSP_USE_CMSIS_OS)
551  /* Release semaphore to prevent multiple I2C access */
552  osSemaphoreRelease(BspI2cSemaphore);
553 #endif
554  /* BSP status */
555  return ret;
556 }
557 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
558 
572 static uint32_t I2C_GetTiming(uint32_t clock_src_freq, uint32_t i2c_freq)
573 {
574  uint32_t ret = 0;
575  uint32_t speed;
576  uint32_t idx;
577 
578  if((clock_src_freq != 0U) && (i2c_freq != 0U))
579  {
580  for ( speed = 0 ; speed <= (uint32_t)I2C_SPEED_FREQ_FAST_PLUS ; speed++)
581  {
582  if ((i2c_freq >= I2C_Charac[speed].freq_min) &&
583  (i2c_freq <= I2C_Charac[speed].freq_max))
584  {
585  I2C_Compute_PRESC_SCLDEL_SDADEL(clock_src_freq, speed);
586  idx = I2C_Compute_SCLL_SCLH(clock_src_freq, speed);
587 
588  if (idx < I2C_VALID_TIMING_NBR)
589  {
590  ret = ((I2c_valid_timing[idx].presc & 0x0FU) << 28) |\
591  ((I2c_valid_timing[idx].tscldel & 0x0FU) << 20) |\
592  ((I2c_valid_timing[idx].tsdadel & 0x0FU) << 16) |\
593  ((I2c_valid_timing[idx].sclh & 0xFFU) << 8) |\
594  ((I2c_valid_timing[idx].scll & 0xFFU) << 0);
595  }
596  break;
597  }
598  }
599  }
600 
601  return ret;
602 }
603 
610 static void I2C_Compute_PRESC_SCLDEL_SDADEL(uint32_t clock_src_freq, uint32_t I2C_speed)
611 {
612  uint32_t prev_presc = I2C_PRESC_MAX;
613  uint32_t ti2cclk;
614  int32_t tsdadel_min, tsdadel_max;
615  int32_t tscldel_min;
616  uint32_t presc, scldel, sdadel;
617  uint32_t tafdel_min, tafdel_max;
618 
619  ti2cclk = (SEC2NSEC + (clock_src_freq / 2U))/ clock_src_freq;
620 
621  tafdel_min = I2C_ANALOG_FILTER_DELAY_MIN;
622  tafdel_max = I2C_ANALOG_FILTER_DELAY_MAX;
623 
624  /* tDNF = DNF x tI2CCLK
625  tPRESC = (PRESC+1) x tI2CCLK
626  SDADEL >= {tf +tHD;DAT(min) - tAF(min) - tDNF - [3 x tI2CCLK]} / {tPRESC}
627  SDADEL <= {tVD;DAT(max) - tr - tAF(max) - tDNF- [4 x tI2CCLK]} / {tPRESC} */
628 
629  tsdadel_min = (int32_t)I2C_Charac[I2C_speed].tfall + (int32_t)I2C_Charac[I2C_speed].hddat_min -
630  (int32_t)tafdel_min - (int32_t)(((int32_t)I2C_Charac[I2C_speed].dnf + 3) * (int32_t)ti2cclk);
631 
632  tsdadel_max = (int32_t)I2C_Charac[I2C_speed].vddat_max - (int32_t)I2C_Charac[I2C_speed].trise -
633  (int32_t)tafdel_max - (int32_t)(((int32_t)I2C_Charac[I2C_speed].dnf + 4) * (int32_t)ti2cclk);
634 
635 
636  /* {[tr+ tSU;DAT(min)] / [tPRESC]} - 1 <= SCLDEL */
637  tscldel_min = (int32_t)I2C_Charac[I2C_speed].trise + (int32_t)I2C_Charac[I2C_speed].sudat_min;
638 
639  if (tsdadel_min <= 0)
640  {
641  tsdadel_min = 0;
642  }
643 
644  if (tsdadel_max <= 0)
645  {
646  tsdadel_max = 0;
647  }
648 
649  for (presc = 0; presc < I2C_PRESC_MAX; presc++)
650  {
651  for (scldel = 0; scldel < I2C_SCLDEL_MAX; scldel++)
652  {
653  /* TSCLDEL = (SCLDEL+1) * (PRESC+1) * TI2CCLK */
654  uint32_t tscldel = (scldel + 1U) * (presc + 1U) * ti2cclk;
655 
656  if (tscldel >= (uint32_t)tscldel_min)
657  {
658  for (sdadel = 0; sdadel < I2C_SDADEL_MAX; sdadel++)
659  {
660  /* TSDADEL = SDADEL * (PRESC+1) * TI2CCLK */
661  uint32_t tsdadel = (sdadel * (presc + 1U)) * ti2cclk;
662 
663  if ((tsdadel >= (uint32_t)tsdadel_min) && (tsdadel <= (uint32_t)tsdadel_max))
664  {
665  if(presc != prev_presc)
666  {
670  prev_presc = presc;
672 
674  {
675  return;
676  }
677  }
678  }
679  }
680  }
681  }
682  }
683 }
684 
691 static uint32_t I2C_Compute_SCLL_SCLH (uint32_t clock_src_freq, uint32_t I2C_speed)
692 {
693  uint32_t ret = 0xFFFFFFFFU;
694  uint32_t ti2cclk;
695  uint32_t ti2cspeed;
696  uint32_t prev_error;
697  uint32_t dnf_delay;
698  uint32_t clk_min, clk_max;
699  uint32_t scll, sclh;
700  uint32_t tafdel_min;
701 
702  ti2cclk = (SEC2NSEC + (clock_src_freq / 2U))/ clock_src_freq;
703  ti2cspeed = (SEC2NSEC + (I2C_Charac[I2C_speed].freq / 2U))/ I2C_Charac[I2C_speed].freq;
704 
705  tafdel_min = I2C_ANALOG_FILTER_DELAY_MIN;
706 
707  /* tDNF = DNF x tI2CCLK */
708  dnf_delay = I2C_Charac[I2C_speed].dnf * ti2cclk;
709 
710  clk_max = SEC2NSEC / I2C_Charac[I2C_speed].freq_min;
711  clk_min = SEC2NSEC / I2C_Charac[I2C_speed].freq_max;
712 
713  prev_error = ti2cspeed;
714 
715  for (uint32_t count = 0; count < I2c_valid_timing_nbr; count++)
716  {
717  /* tPRESC = (PRESC+1) x tI2CCLK*/
718  uint32_t tpresc = (I2c_valid_timing[count].presc + 1U) * ti2cclk;
719 
720  for (scll = 0; scll < I2C_SCLL_MAX; scll++)
721  {
722  /* tLOW(min) <= tAF(min) + tDNF + 2 x tI2CCLK + [(SCLL+1) x tPRESC ] */
723  uint32_t tscl_l = tafdel_min + dnf_delay + (2U * ti2cclk) + ((scll + 1U) * tpresc);
724 
725 
726  /* The I2CCLK period tI2CCLK must respect the following conditions:
727  tI2CCLK < (tLOW - tfilters) / 4 and tI2CCLK < tHIGH */
728  if ((tscl_l > I2C_Charac[I2C_speed].lscl_min) && (ti2cclk < ((tscl_l - tafdel_min - dnf_delay) / 4U)))
729  {
730  for (sclh = 0; sclh < I2C_SCLH_MAX; sclh++)
731  {
732  /* tHIGH(min) <= tAF(min) + tDNF + 2 x tI2CCLK + [(SCLH+1) x tPRESC] */
733  uint32_t tscl_h = tafdel_min + dnf_delay + (2U * ti2cclk) + ((sclh + 1U) * tpresc);
734 
735  /* tSCL = tf + tLOW + tr + tHIGH */
736  uint32_t tscl = tscl_l + tscl_h + I2C_Charac[I2C_speed].trise + I2C_Charac[I2C_speed].tfall;
737 
738  if ((tscl >= clk_min) && (tscl <= clk_max) && (tscl_h >= I2C_Charac[I2C_speed].hscl_min) && (ti2cclk < tscl_h))
739  {
740  int32_t error = (int32_t)tscl - (int32_t)ti2cspeed;
741 
742  if (error < 0)
743  {
744  error = -error;
745  }
746 
747  /* look for the timings with the lowest clock error */
748  if ((uint32_t)error < prev_error)
749  {
750  prev_error = (uint32_t)error;
751  I2c_valid_timing[count].scll = scll;
752  I2c_valid_timing[count].sclh = sclh;
753  ret = count;
754  }
755  }
756  }
757  }
758  }
759  }
760 
761  return ret;
762 }
763 
769 static void I2C4_MspInit(I2C_HandleTypeDef *phi2c)
770 {
771  GPIO_InitTypeDef gpio_init_structure;
772 
773  /* Prevent unused argument(s) compilation warning */
774  UNUSED(phi2c);
775 
776  /*** Configure the GPIOs ***/
777  /* Enable SCL GPIO clock */
779  /* Enable SDA GPIO clock */
781 
782  /* Configure I2C Tx as alternate function */
783  gpio_init_structure.Pin = BUS_I2C4_SCL_PIN;
784  gpio_init_structure.Mode = GPIO_MODE_AF_OD;
785  gpio_init_structure.Pull = GPIO_NOPULL;
786  gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
787  gpio_init_structure.Alternate = BUS_I2C4_SCL_AF;
788  HAL_GPIO_Init(BUS_I2C4_SCL_GPIO_PORT, &gpio_init_structure);
789 
790  /* Configure I2C Rx as alternate function */
791  gpio_init_structure.Pin = BUS_I2C4_SDA_PIN;
792  gpio_init_structure.Mode = GPIO_MODE_AF_OD;
793  gpio_init_structure.Pull = GPIO_NOPULL;
794  gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
795  gpio_init_structure.Alternate = BUS_I2C4_SDA_AF;
796  HAL_GPIO_Init(BUS_I2C4_SDA_GPIO_PORT, &gpio_init_structure);
797 
798  /*** Configure the I2C peripheral ***/
799  /* Enable I2C clock */
801 
802  /* Force the I2C peripheral clock reset */
804 
805  /* Release the I2C peripheral clock reset */
807 }
808 
815 {
816  GPIO_InitTypeDef gpio_init_structure;
817 
818  /* Prevent unused argument(s) compilation warning */
819  UNUSED(phi2c);
820 
821  /* Configure I2C Tx, Rx as alternate function */
822  gpio_init_structure.Pin = BUS_I2C4_SCL_PIN;
823  HAL_GPIO_DeInit(BUS_I2C4_SCL_GPIO_PORT, gpio_init_structure.Pin );
824  gpio_init_structure.Pin = BUS_I2C4_SDA_PIN;
825  HAL_GPIO_DeInit(BUS_I2C4_SDA_GPIO_PORT, gpio_init_structure.Pin);
826 
827  /* Disable I2C clock */
829 }
830 
840 static int32_t I2C4_WriteReg(uint16_t DevAddr, uint16_t Reg, uint16_t MemAddSize, uint8_t *pData, uint16_t Length)
841 {
842  if(HAL_I2C_Mem_Write(&hbus_i2c4, DevAddr, Reg, MemAddSize, pData, Length, 1000) == HAL_OK)
843  {
844  return BSP_ERROR_NONE;
845  }
846 
847  return BSP_ERROR_BUS_FAILURE;
848 }
849 
859 static int32_t I2C4_ReadReg(uint16_t DevAddr, uint16_t Reg, uint16_t MemAddSize, uint8_t *pData, uint16_t Length)
860 {
861  if (HAL_I2C_Mem_Read(&hbus_i2c4, DevAddr, Reg, MemAddSize, pData, Length, 1000) == HAL_OK)
862  {
863  return BSP_ERROR_NONE;
864  }
865 
866  return BSP_ERROR_BUS_FAILURE;
867 }
868 
885 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
I2C_Charac_t::lscl_min
uint32_t lscl_min
Definition: stm32h747i_discovery_bus.c:73
HAL_I2C_Init
HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
I2C_Compute_SCLL_SCLH
static uint32_t I2C_Compute_SCLL_SCLH(uint32_t clock_src_freq, uint32_t I2C_speed)
Calculate SCLL and SCLH and find best configuration.
Definition: stm32h747i_discovery_bus.c:691
I2C_OA2_NOMASK
#define I2C_OA2_NOMASK
Definition: stm32f7xx_hal_i2c.h:319
I2C_SDADEL_MAX
#define I2C_SDADEL_MAX
Definition: stm32h747i_discovery_bus.c:54
HAL_StatusTypeDef
HAL_StatusTypeDef
HAL Status structures definition
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:40
BUS_I2C4_RELEASE_RESET
#define BUS_I2C4_RELEASE_RESET()
Definition: stm32h747i_discovery_bus.h:74
HAL_I2C_ERROR_AF
#define HAL_I2C_ERROR_AF
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:167
BUS_I2C4_CLK_DISABLE
#define BUS_I2C4_CLK_DISABLE()
Definition: stm32h747i_discovery_bus.h:67
HAL_I2C_GetState
HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
I2C_InitTypeDef::OwnAddress1
uint32_t OwnAddress1
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:56
I2C_Timings_t::tscldel
uint32_t tscldel
Definition: stm32h747i_discovery_bus.c:83
I2C_InitTypeDef::AddressingMode
uint32_t AddressingMode
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:59
I2C4_MspInit
static void I2C4_MspInit(I2C_HandleTypeDef *phi2c)
Initializes I2C MSP.
Definition: stm32h747i_discovery_bus.c:769
I2C_Timings_t::scll
uint32_t scll
Definition: stm32h747i_discovery_bus.c:86
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
GPIO_SPEED_FREQ_HIGH
#define GPIO_SPEED_FREQ_HIGH
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:144
GPIO_InitTypeDef
GPIO Init structure definition
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:47
I2C_Compute_PRESC_SCLDEL_SDADEL
static void I2C_Compute_PRESC_SCLDEL_SDADEL(uint32_t clock_src_freq, uint32_t I2C_speed)
Compute PRESC, SCLDEL and SDADEL.
Definition: stm32h747i_discovery_bus.c:610
I2C_Charac_t::trise
uint32_t trise
Definition: stm32h747i_discovery_bus.c:75
I2C_SCLL_MAX
#define I2C_SCLL_MAX
Definition: stm32h747i_discovery_bus.c:56
I2C_InitTypeDef::OwnAddress2
uint32_t OwnAddress2
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:65
I2C_SCLDEL_MAX
#define I2C_SCLDEL_MAX
Definition: stm32h747i_discovery_bus.c:53
I2c_valid_timing_nbr
static uint32_t I2c_valid_timing_nbr
Definition: stm32h747i_discovery_bus.c:152
I2C_DIGITAL_FILTER_COEF
#define I2C_DIGITAL_FILTER_COEF
Definition: stm32h747i_discovery_bus.c:51
GPIO_InitTypeDef::Alternate
uint32_t Alternate
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:61
stm32h747i_discovery_bus.h
This file is the header of stm32h747i_discovery_bus.c file.
BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE
#define BSP_ERROR_BUS_ACKNOWLEDGE_FAILURE
Definition: stm32h747i_discovery_errno.h:55
I2C_ADDRESSINGMODE_7BIT
#define I2C_ADDRESSINGMODE_7BIT
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:299
I2C_GetTiming
static uint32_t I2C_GetTiming(uint32_t clock_src_freq, uint32_t i2c_freq)
Compute I2C timing according current I2C clock source and required I2C clock.
Definition: stm32h747i_discovery_bus.c:572
I2C_InitTypeDef::DualAddressMode
uint32_t DualAddressMode
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:62
I2C4_ReadReg
static int32_t I2C4_ReadReg(uint16_t DevAddr, uint16_t MemAddSize, uint16_t Reg, uint8_t *pData, uint16_t Length)
Read a register of the device through BUS.
Definition: stm32h747i_discovery_bus.c:859
HAL_RCC_GetPCLK2Freq
uint32_t HAL_RCC_GetPCLK2Freq(void)
BSP_ERROR_NONE
#define BSP_ERROR_NONE
Definition: stm32h747i_discovery_errno.h:30
I2C_Timings_t::presc
uint32_t presc
Definition: stm32h747i_discovery_bus.c:82
HAL_I2C_STATE_RESET
@ HAL_I2C_STATE_RESET
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:109
HAL_ERROR
@ HAL_ERROR
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:43
BSP_ERROR_BUSY
#define BSP_ERROR_BUSY
Definition: stm32h747i_discovery_errno.h:33
BSP_I2C4_ReadReg
int32_t BSP_I2C4_ReadReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
Read a 8bit register of the device through BUS.
Definition: stm32h747i_discovery_bus.c:351
hbus_i2c4
I2C_HandleTypeDef hbus_i2c4
Definition: stm32h747i_discovery_bus.c:163
HAL_GetTick
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c:323
BUS_I2C4_SDA_PIN
#define BUS_I2C4_SDA_PIN
Definition: stm32h747i_discovery_bus.h:78
HAL_I2CEx_ConfigDigitalFilter
HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
HAL_I2C_Mem_Write
HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
I2C_InitTypeDef::NoStretchMode
uint32_t NoStretchMode
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:71
BSP_GetTick
int32_t BSP_GetTick(void)
Delay function.
Definition: stm32h747i_discovery_bus.c:483
I2C4_WriteReg
static int32_t I2C4_WriteReg(uint16_t DevAddr, uint16_t MemAddSize, uint16_t Reg, uint8_t *pData, uint16_t Length)
Write a value in a register of the device through BUS.
Definition: stm32h747i_discovery_bus.c:840
I2C4_MspDeInit
static void I2C4_MspDeInit(I2C_HandleTypeDef *phi2c)
DeInitializes I2C MSP.
Definition: stm32h747i_discovery_bus.c:814
HAL_OK
@ HAL_OK
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:42
I2C_Timings_t::tsdadel
uint32_t tsdadel
Definition: stm32h747i_discovery_bus.c:84
I2C_Charac_t::vddat_max
uint32_t vddat_max
Definition: stm32h747i_discovery_bus.c:71
BUS_I2C4
#define BUS_I2C4
Definition: stm32h747i_discovery_bus.h:65
I2C_DUALADDRESS_DISABLE
#define I2C_DUALADDRESS_DISABLE
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:308
HAL_GPIO_Init
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
I2C_Charac
static const I2C_Charac_t I2C_Charac[]
Definition: stm32h747i_discovery_bus.c:95
UNUSED
#define UNUSED(x)
Definition: porcupine/demo/c/dr_libs/old/dr.h:92
I2C_Charac_t::hscl_min
uint32_t hscl_min
Definition: stm32h747i_discovery_bus.c:74
GPIO_InitTypeDef::Mode
uint32_t Mode
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:52
I2C_ANALOG_FILTER_DELAY_MAX
#define I2C_ANALOG_FILTER_DELAY_MAX
Definition: stm32h747i_discovery_bus.c:49
I2C_GENERALCALL_DISABLE
#define I2C_GENERALCALL_DISABLE
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:317
BUS_I2C4_SCL_PIN
#define BUS_I2C4_SCL_PIN
Definition: stm32h747i_discovery_bus.h:77
BSP_I2C4_ReadReg16
int32_t BSP_I2C4_ReadReg16(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
Read a 16bit register of the device through BUS.
Definition: stm32h747i_discovery_bus.c:425
I2C_MEMADD_SIZE_16BIT
#define I2C_MEMADD_SIZE_16BIT
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:336
BSP_ERROR_BUS_FAILURE
#define BSP_ERROR_BUS_FAILURE
Definition: stm32h747i_discovery_errno.h:38
I2C_Timings_t::sclh
uint32_t sclh
Definition: stm32h747i_discovery_bus.c:85
stm32h747i_discovery_errno.h
GPIO_InitTypeDef::Pull
uint32_t Pull
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:55
GPIO_NOPULL
#define GPIO_NOPULL
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:154
MX_I2C4_Init
__weak HAL_StatusTypeDef MX_I2C4_Init(I2C_HandleTypeDef *hI2c, uint32_t timing)
MX I2C4 initialization.
Definition: stm32h747i_discovery_bus.c:268
BUS_I2C4_SDA_GPIO_PORT
#define BUS_I2C4_SDA_GPIO_PORT
Definition: stm32h747i_discovery_bus.h:80
I2C_SCLH_MAX
#define I2C_SCLH_MAX
Definition: stm32h747i_discovery_bus.c:55
BUS_I2C4_CLK_ENABLE
#define BUS_I2C4_CLK_ENABLE()
Definition: stm32h747i_discovery_bus.h:66
I2c4InitCounter
static uint32_t I2c4InitCounter
Definition: stm32h747i_discovery_bus.c:150
HAL_GPIO_DeInit
void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
count
size_t count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_common/ma_test_common.c:31
I2C_InitTypeDef::Timing
uint32_t Timing
Definition: stm32f7xx_hal_i2c.h:50
GPIO_InitTypeDef::Speed
uint32_t Speed
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:58
__HAL_I2C_RESET_HANDLE_STATE
#define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__)
Reset I2C handle state.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:427
I2C_MEMADD_SIZE_8BIT
#define I2C_MEMADD_SIZE_8BIT
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:335
HAL_I2C_IsDeviceReady
HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
I2C_Charac_t::freq_max
uint32_t freq_max
Definition: stm32h747i_discovery_bus.c:69
I2C_InitTypeDef::GeneralCallMode
uint32_t GeneralCallMode
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:68
I2C_Charac_t::freq
uint32_t freq
Definition: stm32h747i_discovery_bus.c:67
I2C_SPEED_FREQ_STANDARD
#define I2C_SPEED_FREQ_STANDARD
Definition: stm32h747i_discovery_bus.c:45
BSP_I2C4_WriteReg16
int32_t BSP_I2C4_WriteReg16(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
Write a 16bit value in a register of the device through BUS.
Definition: stm32h747i_discovery_bus.c:388
I2C_PRESC_MAX
#define I2C_PRESC_MAX
Definition: stm32h747i_discovery_bus.c:52
I2C_Charac_t::tfall
uint32_t tfall
Definition: stm32h747i_discovery_bus.c:76
I2C_HandleTypeDef::Init
I2C_InitTypeDef Init
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:193
I2C_InitTypeDef::OwnAddress2Masks
uint32_t OwnAddress2Masks
Definition: stm32f7xx_hal_i2c.h:66
BUS_I2C4_SCL_GPIO_CLK_ENABLE
#define BUS_I2C4_SCL_GPIO_CLK_ENABLE()
Definition: stm32h747i_discovery_bus.h:68
GPIO_MODE_AF_OD
#define GPIO_MODE_AF_OD
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:123
BUS_I2C4_FORCE_RESET
#define BUS_I2C4_FORCE_RESET()
Definition: stm32h747i_discovery_bus.h:73
BSP_I2C4_DeInit
int32_t BSP_I2C4_DeInit(void)
DeInitializes I2C HAL.
Definition: stm32h747i_discovery_bus.c:241
I2C_Charac_t::sudat_min
uint32_t sudat_min
Definition: stm32h747i_discovery_bus.c:72
I2c_valid_timing
static I2C_Timings_t I2c_valid_timing[I2C_VALID_TIMING_NBR]
Definition: stm32h747i_discovery_bus.c:151
BUS_I2C4_SCL_AF
#define BUS_I2C4_SCL_AF
Definition: stm32h747i_discovery_bus.h:81
BSP_I2C4_WriteReg
int32_t BSP_I2C4_WriteReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
Write a 8bit value in a register of the device through BUS.
Definition: stm32h747i_discovery_bus.c:314
BSP_I2C4_IsReady
int32_t BSP_I2C4_IsReady(uint16_t DevAddr, uint32_t Trials)
Checks if target device is ready for communication.
Definition: stm32h747i_discovery_bus.c:461
SEC2NSEC
#define SEC2NSEC
Definition: stm32h747i_discovery_bus.c:57
BSP_I2C4_Init
int32_t BSP_I2C4_Init(void)
Initializes I2C HAL.
Definition: stm32h747i_discovery_bus.c:190
I2C_ANALOG_FILTER_DELAY_MIN
#define I2C_ANALOG_FILTER_DELAY_MIN
Definition: stm32h747i_discovery_bus.c:48
I2C_HandleTypeDef
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:188
I2C_Charac_t::freq_min
uint32_t freq_min
Definition: stm32h747i_discovery_bus.c:68
HAL_I2CEx_ConfigAnalogFilter
HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
I2C_ANALOGFILTER_ENABLE
#define I2C_ANALOGFILTER_ENABLE
Definition: stm32f7xx_hal_i2c_ex.h:49
I2C_Timings_t
Definition: stm32h747i_discovery_bus.c:80
HAL_I2C_GetError
uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
error
static int error(vorb *f, enum STBVorbisError e)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/stb_vorbis.c:896
BSP_ERROR_MSP_FAILURE
#define BSP_ERROR_MSP_FAILURE
Definition: stm32h747i_discovery_errno.h:40
HAL_I2C_Mem_Read
HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
I2C_SPEED_FREQ_FAST_PLUS
#define I2C_SPEED_FREQ_FAST_PLUS
Definition: stm32h747i_discovery_bus.c:47
BUS_I2C4_FREQUENCY
#define BUS_I2C4_FREQUENCY
Definition: stm32h747i_discovery_bus.h:85
I2C_NOSTRETCH_DISABLE
#define I2C_NOSTRETCH_DISABLE
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:326
I2C_SPEED_FREQ_FAST
#define I2C_SPEED_FREQ_FAST
Definition: stm32h747i_discovery_bus.c:46
I2C_HandleTypeDef::Instance
I2C_TypeDef * Instance
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h:191
Length
Length
BUS_I2C4_SCL_GPIO_PORT
#define BUS_I2C4_SCL_GPIO_PORT
Definition: stm32h747i_discovery_bus.h:79
I2C_Charac_t
Definition: stm32h747i_discovery_bus.c:65
BSP_ERROR_PERIPH_FAILURE
#define BSP_ERROR_PERIPH_FAILURE
Definition: stm32h747i_discovery_errno.h:34
I2C_VALID_TIMING_NBR
#define I2C_VALID_TIMING_NBR
Definition: stm32h747i_discovery_bus.c:43
BUS_I2C4_SDA_GPIO_CLK_ENABLE
#define BUS_I2C4_SDA_GPIO_CLK_ENABLE()
Definition: stm32h747i_discovery_bus.h:70
HAL_I2C_DeInit
HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
I2C_Charac_t::dnf
uint32_t dnf
Definition: stm32h747i_discovery_bus.c:77
BUS_I2C4_SDA_AF
#define BUS_I2C4_SDA_AF
Definition: stm32h747i_discovery_bus.h:82
GPIO_InitTypeDef::Pin
uint32_t Pin
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:49
I2C_Charac_t::hddat_min
uint32_t hddat_min
Definition: stm32h747i_discovery_bus.c:70


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:54