STSW-STLKT01: Drivers/BSP/SensorTile/SensorTile.c Source File

STSW-STLKT01

STSW-STLKT01
SensorTile.c
Go to the documentation of this file.
1 
38 /* Includes ------------------------------------------------------------------*/
39 #include "SensorTile.h"
40 
41 
67 //#define SYNCHRO_WAIT(NB) for(int i=0; i<NB; i++){__asm("dsb\n");}
68 //#define SYNCHRO_SPI_DELAY (1)
69 
73 #define __SensorTile_BSP_VERSION_MAIN (0x01)
74 #define __SensorTile_BSP_VERSION_SUB1 (0x00)
75 #define __SensorTile_BSP_VERSION_SUB2 (0x00)
76 #define __SensorTile_BSP_VERSION_RC (0x00)
77 #define __SensorTile_BSP_VERSION ((__SensorTile_BSP_VERSION_MAIN << 24)\
78 |(__SensorTile_BSP_VERSION_SUB1 << 16)\
79  |(__SensorTile_BSP_VERSION_SUB2 << 8 )\
80  |(__SensorTile_BSP_VERSION_RC))
81 
82 
90 static uint32_t I2C_SENSORTILE_Timeout = SENSORTILE_I2C_ONBOARD_SENSORS_TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */
91 static I2C_HandleTypeDef I2C_SENSORTILE_Handle;
92 static SPI_HandleTypeDef SPI_Sensor_Handle;
93 
94  SPI_HandleTypeDef SPI_SD_Handle;
95  DMA_HandleTypeDef hdma_tx;
96 
97 
98 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED1_GPIO_PORT, LEDSWD_GPIO_PORT};
99 
100 const uint32_t GPIO_PIN[LEDn] = {LED1_PIN, LEDSWD_PIN};
101 
102 uint32_t SpixTimeout = SENSORTILE_SD_SPI_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
103 
112 static void I2C_SENSORTILE_MspInit( void );
113 static void I2C_SENSORTILE_Error( uint8_t Addr );
114 static uint8_t I2C_SENSORTILE_ReadData( uint8_t Addr, uint8_t Reg, uint8_t* pBuffer, uint16_t Size );
115 static uint8_t I2C_SENSORTILE_WriteData( uint8_t Addr, uint8_t Reg, uint8_t* pBuffer, uint16_t Size );
116 static uint8_t I2C_SENSORTILE_Init( void );
117 
118 static void SD_IO_SPI_Init(void);/*high speed*/
119 
120 static void SD_IO_SPI_Init_LS(void); /*low speed*/
121 
122 static void SD_IO_SPI_Write(uint8_t Value);
123 static uint32_t SD_IO_SPI_Read(void);
124 static void SD_IO_SPI_Error (void);
125 static void SD_IO_SPI_MspInit(SPI_HandleTypeDef *hspi);
126 
127 void SPI_Read(SPI_HandleTypeDef* xSpiHandle, uint8_t *val);
128 void SPI_Read_nBytes(SPI_HandleTypeDef* xSpiHandle, uint8_t *val, uint16_t nBytesToRead );
129 void SPI_Write(SPI_HandleTypeDef* xSpiHandle, uint8_t val);
130 
131 /* Link functions for SD Card peripheral over SPI */
132 void SD_IO_Init(void);
133 void SD_IO_Init_LS(void);/*low speed*/
134 
135 HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response);
136 uint8_t SD_IO_WriteCmd_wResp(uint8_t Cmd, uint32_t Arg, uint8_t Crc);
137 HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response);
138 void SD_IO_WriteDummy(void);
139 void SD_IO_WriteByte(uint8_t Data);
140 uint8_t SD_IO_ReadByte(void);
141 void SD_IO_WriteDMA(uint8_t *pData, uint16_t Size);
142 
159 uint32_t BSP_GetVersion(void)
160 {
161  return __SensorTile_BSP_VERSION;
162 }
163 
164 
172 void BSP_LED_Init(Led_TypeDef Led)
173 {
174  GPIO_InitTypeDef GPIO_InitStruct;
175 
176  /* Enable VddIO2 for GPIOG */
177  __HAL_RCC_PWR_CLK_ENABLE();
178  HAL_PWREx_EnableVddIO2();
179 
180  /* Enable the GPIO_LED clock */
181  LEDx_GPIO_CLK_ENABLE(Led);
182 
183  /* Configure the GPIO_LED pin */
184  GPIO_InitStruct.Pin = GPIO_PIN[Led];
185  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
186  GPIO_InitStruct.Pull = GPIO_NOPULL;
187  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
188 
189  HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
190 }
191 
192 
204 void BSP_LED_DeInit(Led_TypeDef Led)
205 {
206 
207 }
208 
219 void BSP_LED_On(Led_TypeDef Led)
220 {
221  if(Led == LED1)
222  {
223  HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
224  }
225  else if (Led == LEDSWD)
226  {
227  HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
228  }
229 }
230 
241 void BSP_LED_Off(Led_TypeDef Led)
242 {
243  if(Led == LED1)
244  {
245  HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
246  }
247  else if (Led == LEDSWD)
248  {
249  HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
250  }
251 }
252 
263 void BSP_LED_Toggle(Led_TypeDef Led)
264 {
265  HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
266 }
267 
274 DrvStatusTypeDef Sensor_IO_I2C_Init( void )
275 {
276  if ( I2C_SENSORTILE_Init() )
277  {
278  return COMPONENT_ERROR;
279  }
280  else
281  {
282  return COMPONENT_OK;
283  }
284 }
285 
286 
293 DrvStatusTypeDef Sensor_IO_SPI_Init( void )
294 {
295  GPIO_InitTypeDef GPIO_InitStruct;
296 
297  if(HAL_SPI_GetState( &SPI_Sensor_Handle) == HAL_SPI_STATE_RESET )
298  {
299  SENSORTILE_SENSORS_SPI_CLK_ENABLE();
300  SENSORTILE_SENSORS_SPI_GPIO_CLK_ENABLE();
301 
302  GPIO_InitStruct.Pin = SENSORTILE_SENSORS_SPI_MOSI_Pin;
303  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
304  GPIO_InitStruct.Pull = GPIO_PULLUP;
305  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
306  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
307  HAL_GPIO_Init(SENSORTILE_SENSORS_SPI_Port, &GPIO_InitStruct);
308 
309  GPIO_InitStruct.Pin = SENSORTILE_SENSORS_SPI_SCK_Pin;
310  GPIO_InitStruct.Pull = GPIO_PULLUP;
311  HAL_GPIO_Init(SENSORTILE_SENSORS_SPI_Port, &GPIO_InitStruct);
312 
313  SPI_Sensor_Handle.Instance = SENSORTILE_SENSORS_SPI;
314  SPI_Sensor_Handle.Init.Mode = SPI_MODE_MASTER;
315  SPI_Sensor_Handle.Init.Direction = SPI_DIRECTION_1LINE;
316  SPI_Sensor_Handle.Init.DataSize = SPI_DATASIZE_8BIT;
317  SPI_Sensor_Handle.Init.CLKPolarity = SPI_POLARITY_HIGH;
318  SPI_Sensor_Handle.Init.CLKPhase = SPI_PHASE_2EDGE;
319  SPI_Sensor_Handle.Init.NSS = SPI_NSS_SOFT;
320  SPI_Sensor_Handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; // 5 MHz
321 // SPI_Sensor_Handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; // 2.5MHz
322  SPI_Sensor_Handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
323  SPI_Sensor_Handle.Init.TIMode = SPI_TIMODE_DISABLED;
324  SPI_Sensor_Handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
325  SPI_Sensor_Handle.Init.CRCPolynomial = 7;
326  SPI_Sensor_Handle.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
327  SPI_Sensor_Handle.Init.NSSPMode = SPI_NSS_PULSE_DISABLED;
328  HAL_SPI_Init(&SPI_Sensor_Handle);
329 
330  SPI_1LINE_TX(&SPI_Sensor_Handle);
331  __HAL_SPI_ENABLE(&SPI_Sensor_Handle);
332  }
333  return COMPONENT_OK;
334 }
335 
336 uint8_t Sensor_IO_SPI_CS_Init_All(void)
337 {
338  GPIO_InitTypeDef GPIO_InitStruct;
339 
340  /* Set all the pins before init to avoid glitch */
341  HAL_GPIO_WritePin(SENSORTILE_LSM6DSM_SPI_CS_Port, SENSORTILE_LSM6DSM_SPI_CS_Pin, GPIO_PIN_SET);
342  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_X_SPI_CS_Port, SENSORTILE_LSM303AGR_X_SPI_CS_Pin, GPIO_PIN_SET);
343  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_M_SPI_CS_Port, SENSORTILE_LSM303AGR_M_SPI_CS_Pin, GPIO_PIN_SET);
344  HAL_GPIO_WritePin(SENSORTILE_LPS22HB_SPI_CS_Port, SENSORTILE_LPS22HB_SPI_CS_Pin, GPIO_PIN_SET);
345 
346  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
347  GPIO_InitStruct.Pull = GPIO_NOPULL;
348  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
349 
350  SENSORTILE_LSM6DSM_SPI_CS_GPIO_CLK_ENABLE();
351  GPIO_InitStruct.Pin = SENSORTILE_LSM6DSM_SPI_CS_Pin;
352  HAL_GPIO_Init(SENSORTILE_LSM6DSM_SPI_CS_Port, &GPIO_InitStruct);
353  HAL_GPIO_WritePin(SENSORTILE_LSM6DSM_SPI_CS_Port, SENSORTILE_LSM6DSM_SPI_CS_Pin, GPIO_PIN_SET);
354 
355  SENSORTILE_LSM303AGR_X_SPI_CS_GPIO_CLK_ENABLE();
356  GPIO_InitStruct.Pin = SENSORTILE_LSM303AGR_X_SPI_CS_Pin;
357  HAL_GPIO_Init(SENSORTILE_LSM303AGR_X_SPI_CS_Port, &GPIO_InitStruct);
358  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_X_SPI_CS_Port, SENSORTILE_LSM303AGR_X_SPI_CS_Pin, GPIO_PIN_SET);
359 
360  SENSORTILE_LSM303AGR_M_SPI_CS_GPIO_CLK_ENABLE();
361  GPIO_InitStruct.Pin = SENSORTILE_LSM303AGR_M_SPI_CS_Pin;
362  HAL_GPIO_Init(SENSORTILE_LSM303AGR_M_SPI_CS_Port, &GPIO_InitStruct);
363  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_M_SPI_CS_Port, SENSORTILE_LSM303AGR_M_SPI_CS_Pin, GPIO_PIN_SET);
364 
365  SENSORTILE_LPS22HB_SPI_CS_GPIO_CLK_ENABLE();
366  GPIO_InitStruct.Pin = SENSORTILE_LPS22HB_SPI_CS_Pin;
367  HAL_GPIO_Init(SENSORTILE_LPS22HB_SPI_CS_Port, &GPIO_InitStruct);
368  HAL_GPIO_WritePin(SENSORTILE_LPS22HB_SPI_CS_Port, SENSORTILE_LPS22HB_SPI_CS_Pin, GPIO_PIN_SET);
369 
370  return COMPONENT_OK;
371 }
372 
373 uint8_t Sensor_IO_SPI_CS_Init(void *handle)
374 {
375  GPIO_InitTypeDef GPIO_InitStruct;
376  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
377 
378  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
379  GPIO_InitStruct.Pull = GPIO_NOPULL;
380  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
381 
382  switch(ctx->spiDevice)
383  {
384  case LSM6DSM:
385  SENSORTILE_LSM6DSM_SPI_CS_GPIO_CLK_ENABLE();
386  GPIO_InitStruct.Pin = SENSORTILE_LSM6DSM_SPI_CS_Pin;
387  /* Set the pin before init to avoid glitch */
388  HAL_GPIO_WritePin(SENSORTILE_LSM6DSM_SPI_CS_Port, SENSORTILE_LSM6DSM_SPI_CS_Pin, GPIO_PIN_SET);
389  HAL_GPIO_Init(SENSORTILE_LSM6DSM_SPI_CS_Port, &GPIO_InitStruct);
390  break;
391  case LSM303AGR_X:
392  SENSORTILE_LSM303AGR_X_SPI_CS_GPIO_CLK_ENABLE();
393  GPIO_InitStruct.Pin = SENSORTILE_LSM303AGR_X_SPI_CS_Pin;
394  /* Set the pin before init to avoid glitch */
395  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_X_SPI_CS_Port, SENSORTILE_LSM303AGR_X_SPI_CS_Pin, GPIO_PIN_SET);
396  HAL_GPIO_Init(SENSORTILE_LSM303AGR_X_SPI_CS_Port, &GPIO_InitStruct);
397  break;
398  case LSM303AGR_M:
399  SENSORTILE_LSM303AGR_M_SPI_CS_GPIO_CLK_ENABLE();
400  GPIO_InitStruct.Pin = SENSORTILE_LSM303AGR_M_SPI_CS_Pin;
401  /* Set the pin before init to avoid glitch */
402  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_M_SPI_CS_Port, SENSORTILE_LSM303AGR_M_SPI_CS_Pin, GPIO_PIN_SET);
403  HAL_GPIO_Init(SENSORTILE_LSM303AGR_M_SPI_CS_Port, &GPIO_InitStruct);
404  break;
405  case LPS22HB:
406  SENSORTILE_LPS22HB_SPI_CS_GPIO_CLK_ENABLE();
407  GPIO_InitStruct.Pin = SENSORTILE_LPS22HB_SPI_CS_Pin;
408  /* Set the pin before init to avoid glitch */
409  HAL_GPIO_WritePin(SENSORTILE_LPS22HB_SPI_CS_Port, SENSORTILE_LPS22HB_SPI_CS_Pin, GPIO_PIN_SET);
410  HAL_GPIO_Init(SENSORTILE_LPS22HB_SPI_CS_Port, &GPIO_InitStruct);
411  break;
412  default:
413  return COMPONENT_NOT_IMPLEMENTED;
414  }
415  return COMPONENT_OK;
416 }
417 
427 uint8_t Sensor_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
428 {
429 
430  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
431 
432  if(ctx->ifType == 0)
433  {
434 
435  if ( nBytesToWrite > 1 )
436  {
437  if (ctx->who_am_i == HTS221_WHO_AM_I_VAL)
438  {
439  WriteAddr |= 0x80; /* Enable I2C multi-bytes Write */
440  }
441  }
442  return Sensor_IO_I2C_Write( handle, WriteAddr, pBuffer, nBytesToWrite );
443  }
444 
445  if(ctx->ifType == 1)
446  {
447  if ( nBytesToWrite > 1 )
448  {
449  switch(ctx->who_am_i)
450  {
451  case LSM303AGR_ACC_WHO_AM_I: WriteAddr |= 0x40; break;
452  case LSM303AGR_MAG_WHO_AM_I: break;
453  default:;
454  }
455  }
456  return Sensor_IO_SPI_Write( handle, WriteAddr, pBuffer, nBytesToWrite );
457  }
458 
459  return COMPONENT_ERROR;
460 }
461 
462 
472 uint8_t Sensor_IO_I2C_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
473 {
474  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
475 
476 
477  return I2C_SENSORTILE_WriteData( ctx->address, WriteAddr, pBuffer, nBytesToWrite );
478 }
479 
480 
490 uint8_t Sensor_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
491 {
492 
493  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
494 
495  if(ctx->ifType == 0)
496  {
497 
498  if ( nBytesToRead > 1 )
499  {
500  if (ctx->who_am_i == HTS221_WHO_AM_I_VAL)
501  {
502  ReadAddr |= 0x80; /* Enable I2C multi-bytes Write */
503  }
504  }
505  return Sensor_IO_I2C_Read( handle, ReadAddr, pBuffer, nBytesToRead );
506  }
507 
508  if(ctx->ifType == 1 )
509  {
510  if ( nBytesToRead > 1 ) {
511  switch(ctx->who_am_i)
512  {
513  case LSM303AGR_ACC_WHO_AM_I: ReadAddr |= 0x40; break;
514  case LSM303AGR_MAG_WHO_AM_I: break;
515  default:;
516  }
517  }
518  return Sensor_IO_SPI_Read( handle, ReadAddr, pBuffer, nBytesToRead );
519  }
520 
521  return COMPONENT_ERROR;
522 }
523 
524 
534 uint8_t Sensor_IO_I2C_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
535 {
536 
537  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
538 
539 
540  return I2C_SENSORTILE_ReadData( ctx->address, ReadAddr, pBuffer, nBytesToRead );
541 }
542 
543 
553 uint8_t Sensor_IO_SPI_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
554 {
555  uint8_t i;
556 
557 // Select the correct device
558  Sensor_IO_SPI_CS_Enable(handle);
559 
560  SPI_Write(&SPI_Sensor_Handle, WriteAddr);
561 
562  for(i=0;i<nBytesToWrite;i++)
563  {
564  SPI_Write(&SPI_Sensor_Handle, pBuffer[i]);
565  }
566 // Deselect the device
567  Sensor_IO_SPI_CS_Disable(handle);
568 
569  return COMPONENT_OK;
570 }
571 
581 uint8_t Sensor_IO_SPI_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
582 {
583  /* Select the correct device */
584  Sensor_IO_SPI_CS_Enable(handle);
585 
586  /* Write Reg Address */
587  SPI_Write(&SPI_Sensor_Handle, ReadAddr | 0x80);
588 
589  /* Disable the SPI and change the data line to input */
590  __HAL_SPI_DISABLE(&SPI_Sensor_Handle);
591  SPI_1LINE_RX(&SPI_Sensor_Handle);
592 
593  /* Check if we need to read one byte or more */
594  if(nBytesToRead > 1U)
595  {
596  SPI_Read_nBytes(&SPI_Sensor_Handle, pBuffer, nBytesToRead);
597  }
598  else
599  {
600  SPI_Read(&SPI_Sensor_Handle, pBuffer);
601  }
602 
603  /* Deselect the device */
604  Sensor_IO_SPI_CS_Disable(handle);
605 
606  /* Change the data line to output and enable the SPI */
607  SPI_1LINE_TX(&SPI_Sensor_Handle);
608  __HAL_SPI_ENABLE(&SPI_Sensor_Handle);
609 
610  return COMPONENT_OK;
611 }
612 
613 
614 uint8_t Sensor_IO_SPI_CS_Enable(void *handle)
615 {
616  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
617 
618  switch(ctx->spiDevice)
619  {
620  case LSM6DSM:
621  HAL_GPIO_WritePin(SENSORTILE_LSM6DSM_SPI_CS_Port, SENSORTILE_LSM6DSM_SPI_CS_Pin, GPIO_PIN_RESET);
622  break;
623  case LSM303AGR_X:
624  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_X_SPI_CS_Port, SENSORTILE_LSM303AGR_X_SPI_CS_Pin, GPIO_PIN_RESET);
625  break;
626  case LSM303AGR_M:
627  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_M_SPI_CS_Port, SENSORTILE_LSM303AGR_M_SPI_CS_Pin, GPIO_PIN_RESET);
628  break;
629  case LPS22HB:
630  HAL_GPIO_WritePin(SENSORTILE_LPS22HB_SPI_CS_Port, SENSORTILE_LPS22HB_SPI_CS_Pin, GPIO_PIN_RESET);
631  break;
632  }
633  return COMPONENT_OK;
634 }
635 
636 uint8_t Sensor_IO_SPI_CS_Disable(void *handle)
637 {
638  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
639 
640  switch(ctx->spiDevice)
641  {
642  case LSM6DSM:
643  HAL_GPIO_WritePin(SENSORTILE_LSM6DSM_SPI_CS_Port, SENSORTILE_LSM6DSM_SPI_CS_Pin, GPIO_PIN_SET);
644  break;
645  case LSM303AGR_X:
646  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_X_SPI_CS_Port, SENSORTILE_LSM303AGR_X_SPI_CS_Pin, GPIO_PIN_SET);
647  break;
648  case LSM303AGR_M:
649  HAL_GPIO_WritePin(SENSORTILE_LSM303AGR_M_SPI_CS_Port, SENSORTILE_LSM303AGR_M_SPI_CS_Pin, GPIO_PIN_SET);
650  break;
651  case LPS22HB:
652  HAL_GPIO_WritePin(SENSORTILE_LPS22HB_SPI_CS_Port, SENSORTILE_LPS22HB_SPI_CS_Pin, GPIO_PIN_SET);
653  break;
654  }
655  return COMPONENT_OK;
656 }
657 
664 DrvStatusTypeDef LSM6DSM_Sensor_IO_ITConfig( void )
665 {
666 
667  /* At the moment this feature is only implemented for LSM6DSM */
668  GPIO_InitTypeDef GPIO_InitStructureInt2;
669 
670  /* Enable INT2 GPIO clock */
671  LSM6DSM_INT2_GPIO_CLK_ENABLE();
672 
673  /* Configure GPIO PINs to detect Interrupts */
674  GPIO_InitStructureInt2.Pin = LSM6DSM_INT2_PIN;
675  GPIO_InitStructureInt2.Mode = GPIO_MODE_IT_RISING;
676  GPIO_InitStructureInt2.Speed = GPIO_SPEED_FAST;
677  GPIO_InitStructureInt2.Pull = GPIO_NOPULL;
678  HAL_GPIO_Init(LSM6DSM_INT2_GPIO_PORT, &GPIO_InitStructureInt2);
679 
680  /* Enable and set EXTI Interrupt priority */
681  HAL_NVIC_SetPriority(LSM6DSM_INT2_EXTI_IRQn, 0x08, 0x00);
682  HAL_NVIC_EnableIRQ(LSM6DSM_INT2_EXTI_IRQn);
683 
684  return COMPONENT_OK;
685 }
686 
687 //#if defined(__ICCARM__)
688 //#pragma optimize=none
689 //#endif
690 
697 void SPI_Read(SPI_HandleTypeDef* xSpiHandle, uint8_t *val)
698 {
699  /* In master RX mode the clock is automaticaly generated on the SPI enable.
700  So to guarantee the clock generation for only one data, the clock must be
701  disabled after the first bit and before the latest bit */
702  /* Interrupts should be disabled during this operation */
703 
704  __disable_irq();
705 
706  __HAL_SPI_ENABLE(xSpiHandle);
707  __asm("dsb\n");
708  __asm("dsb\n");
709  __HAL_SPI_DISABLE(xSpiHandle);
710 
711  __enable_irq();
712 
713  while ((xSpiHandle->Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE);
714  /* read the received data */
715  *val = *(__IO uint8_t *) &xSpiHandle->Instance->DR;
716  while ((xSpiHandle->Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY);
717 }
718 
726 void SPI_Read_nBytes(SPI_HandleTypeDef* xSpiHandle, uint8_t *val, uint16_t nBytesToRead)
727 {
728  /* Interrupts should be disabled during this operation */
729  __disable_irq();
730  __HAL_SPI_ENABLE(xSpiHandle);
731 
732  /* Transfer loop */
733  while (nBytesToRead > 1U)
734  {
735  /* Check the RXNE flag */
736  if (xSpiHandle->Instance->SR & SPI_FLAG_RXNE)
737  {
738  /* read the received data */
739  *val = *(__IO uint8_t *) &xSpiHandle->Instance->DR;
740  val += sizeof(uint8_t);
741  nBytesToRead--;
742  }
743  }
744  /* In master RX mode the clock is automaticaly generated on the SPI enable.
745  So to guarantee the clock generation for only one data, the clock must be
746  disabled after the first bit and before the latest bit of the last Byte received */
747  /* __DSB instruction are inserted to garantee that clock is Disabled in the right timeframe */
748 
749  __DSB();
750  __DSB();
751  __HAL_SPI_DISABLE(xSpiHandle);
752 
753  __enable_irq();
754 
755  while ((xSpiHandle->Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE);
756  /* read the received data */
757  *val = *(__IO uint8_t *) &xSpiHandle->Instance->DR;
758  while ((xSpiHandle->Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY);
759 }
760 
767 void SPI_Write(SPI_HandleTypeDef* xSpiHandle, uint8_t val)
768 {
769  /* check TXE flag */
770  while ((xSpiHandle->Instance->SR & SPI_FLAG_TXE) != SPI_FLAG_TXE);
771 
772  /* Write the data */
773  *((__IO uint8_t*) &xSpiHandle->Instance->DR) = val;
774 
775  /* Wait BSY flag */
776  while ((xSpiHandle->Instance->SR & SPI_FLAG_FTLVL) != SPI_FTLVL_EMPTY);
777  while ((xSpiHandle->Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY);
778 }
779 
780 
781 /******************************* I2C Routines *********************************/
782 
789 static uint8_t I2C_SENSORTILE_Init( void )
790 {
791  if(HAL_I2C_GetState( &I2C_SENSORTILE_Handle) == HAL_I2C_STATE_RESET )
792  {
793  /* I2C_SENSORTILE peripheral configuration */
794  I2C_SENSORTILE_Handle.Init.Timing = I2C_SENSORTILE_TIMING_400KHZ;
795 // I2C_SENSORTILE_Handle.Init.Timing = I2C_SENSORTILE_TIMING_100KHZ;
796  I2C_SENSORTILE_Handle.Init.OwnAddress1 = 0x33;
797  I2C_SENSORTILE_Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
798  I2C_SENSORTILE_Handle.Instance = SENSORTILE_I2C_ONBOARD_SENSORS;
799 
800  /* Init the I2C */
802  HAL_I2C_Init( &I2C_SENSORTILE_Handle );
803  }
804 
805  if( HAL_I2C_GetState( &I2C_SENSORTILE_Handle) == HAL_I2C_STATE_READY )
806  {
807  return 0;
808  }
809  else
810  {
811  return 1;
812  }
813 }
814 
815 
816 
826 static uint8_t I2C_SENSORTILE_WriteData( uint8_t Addr, uint8_t Reg, uint8_t* pBuffer, uint16_t Size )
827 {
828 
829  HAL_StatusTypeDef status = HAL_OK;
830 
831  status = HAL_I2C_Mem_Write( &I2C_SENSORTILE_Handle, Addr, ( uint16_t )Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Size, I2C_SENSORTILE_Timeout );
832 
833  /* Check the communication status */
834  if( status != HAL_OK )
835  {
836 
837  /* Execute user timeout callback */
838  I2C_SENSORTILE_Error( Addr );
839  return 1;
840  }
841  else
842  {
843  return 0;
844  }
845 }
846 
847 
857 static uint8_t I2C_SENSORTILE_ReadData( uint8_t Addr, uint8_t Reg, uint8_t* pBuffer, uint16_t Size )
858 {
859  HAL_StatusTypeDef status = HAL_OK;
860 
861  status = HAL_I2C_Mem_Read( &I2C_SENSORTILE_Handle, Addr, ( uint16_t )Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Size, I2C_SENSORTILE_Timeout );
862 
863  /* Check the communication status */
864  if( status != HAL_OK )
865  {
866  /* Execute user timeout callback */
867  I2C_SENSORTILE_Error( Addr );
868  return 1;
869  }
870  else
871  {
872  return 0;
873  }
874 }
875 
876 
882 static void I2C_SENSORTILE_Error( uint8_t Addr )
883 {
884  /* De-initialize the I2C comunication bus */
885  HAL_I2C_DeInit( &I2C_SENSORTILE_Handle );
886 
887  /* Re-Initiaize the I2C comunication bus */
889 }
890 
891 
897 static void I2C_SENSORTILE_MspInit( void )
898 {
899  GPIO_InitTypeDef GPIO_InitStruct;
900 
901  RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct;
902 
903  /*##-1- Configure the I2C clock source. The clock is derived from the SYSCLK #*/
904  RCC_PeriphCLKInitStruct.PeriphClockSelection = SENSORTILE_I2C_ONBOARD_SENSORS_RCC_PERIPHCLK;
905  RCC_PeriphCLKInitStruct.I2c3ClockSelection = SENSORTILE_I2C_ONBOARD_SENSORS_I2CCLKSOURCE;
906  HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
907 
908  /* Enable I2C GPIO clocks */
909  SENSORTILE_I2C_ONBOARD_SENSORS_SCL_SDA_GPIO_CLK_ENABLE();
910 
911  /* I2C_SENSORTILE SCL and SDA pins configuration -------------------------------------*/
912  GPIO_InitStruct.Pin = SENSORTILE_I2C_ONBOARD_SENSORS_SCL_PIN | SENSORTILE_I2C_ONBOARD_SENSORS_SDA_PIN;
913  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
914  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
915  GPIO_InitStruct.Pull = GPIO_PULLUP;
916  GPIO_InitStruct.Alternate = SENSORTILE_I2C_ONBOARD_SENSORS_SCL_SDA_AF;
917 
918  HAL_GPIO_Init( SENSORTILE_I2C_ONBOARD_SENSORS_SCL_SDA_GPIO_PORT, &GPIO_InitStruct );
919 
920  /* Enable the I2C_SENSORTILE peripheral clock */
921  SENSORTILE_I2C_ONBOARD_SENSORS_CLK_ENABLE();
922 
923  /* Force the I2C peripheral clock reset */
924  SENSORTILE_I2C_ONBOARD_SENSORS_FORCE_RESET();
925 
926  /* Release the I2C peripheral clock reset */
927  SENSORTILE_I2C_ONBOARD_SENSORS_RELEASE_RESET();
928 
929  /* Enable and set I2C_SENSORTILE Interrupt to the highest priority */
930  HAL_NVIC_SetPriority(SENSORTILE_I2C_ONBOARD_SENSORS_EV_IRQn, 0, 0);
931  HAL_NVIC_EnableIRQ(SENSORTILE_I2C_ONBOARD_SENSORS_EV_IRQn);
932 
933  /* Enable and set I2C_SENSORTILE Interrupt to the highest priority */
934  HAL_NVIC_SetPriority(SENSORTILE_I2C_ONBOARD_SENSORS_ER_IRQn, 0, 0);
935  HAL_NVIC_EnableIRQ(SENSORTILE_I2C_ONBOARD_SENSORS_ER_IRQn);
936 }
937 
938 
939 /******************************* SPI Routines**********************************/
945 static void SD_IO_SPI_MspInit(SPI_HandleTypeDef *hspi)
946 {
947  GPIO_InitTypeDef GPIO_InitStructure;
948 
949  /* Enable VddIO2 for GPIOG */
950  __HAL_RCC_PWR_CLK_ENABLE();
951  HAL_PWREx_EnableVddIO2();
952 
953  /* Enable SPI clock */
954  SENSORTILE_SD_SPI_CLK_ENABLE();
955 
956  /* Enable DMA clock */
957  __HAL_RCC_DMA2_CLK_ENABLE();
958 
959 
960  /*** Configure the GPIOs ***/
961  /* Enable GPIO clock */
962  SENSORTILE_SD_SPI_SCK_GPIO_CLK_ENABLE();
963  SENSORTILE_SD_SPI_MISO_MOSI_GPIO_CLK_ENABLE();
964 
965  /* configure SPI SCK */
966  GPIO_InitStructure.Pin = SENSORTILE_SD_SPI_SCK_PIN;
967  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
968  GPIO_InitStructure.Pull = GPIO_NOPULL;
969  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
970  GPIO_InitStructure.Alternate = SENSORTILE_SD_SPI_SCK_AF;
971  HAL_GPIO_Init(SENSORTILE_SD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
972 
973  /* configure SPI MISO and MOSI */
974  GPIO_InitStructure.Pin = (SENSORTILE_SD_SPI_MOSI_PIN);
975  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
976  GPIO_InitStructure.Pull = GPIO_NOPULL;
977  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
978  GPIO_InitStructure.Alternate = SENSORTILE_SD_SPI_MISO_MOSI_AF;
979  HAL_GPIO_Init(SENSORTILE_SD_SPI_MISO_MOSI_GPIO_PORT, &GPIO_InitStructure);
980 
981  GPIO_InitStructure.Pin = (SENSORTILE_SD_SPI_MISO_PIN );
982  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
983  GPIO_InitStructure.Pull = GPIO_PULLUP;
984  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
985  GPIO_InitStructure.Alternate = SENSORTILE_SD_SPI_MISO_MOSI_AF;
986  HAL_GPIO_Init(SENSORTILE_SD_SPI_MISO_MOSI_GPIO_PORT, &GPIO_InitStructure);
987 
988  /*** Configure the SPI peripheral ***/
989 
990  /*##-3- Configure the DMA ##################################################*/
991  /* Configure the DMA handler for Transmission process */
992  hdma_tx.Instance = DMA2_Channel2;
993  hdma_tx.Init.Request = DMA_REQUEST_3;
994  hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
995  hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
996  hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
997  hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
998  hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
999  hdma_tx.Init.Mode = DMA_NORMAL;
1000  hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
1001 
1002  HAL_DMA_Init(&hdma_tx);
1003 
1004  /* Associate the initialized DMA handle to the the SPI handle */
1005  __HAL_LINKDMA(hspi, hdmatx, hdma_tx);
1006 
1007  /*##-4- Configure the NVIC for DMA #########################################*/
1008  /* NVIC configuration for DMA transfer complete interrupt (SPI1_TX) */
1009  HAL_NVIC_SetPriority(DMA2_Channel2_IRQn, 2, 1);
1010  HAL_NVIC_EnableIRQ(DMA2_Channel2_IRQn);
1011 
1012 // /*##-5- Configure the NVIC for SPI #########################################*/
1013 // HAL_NVIC_SetPriority(SPI3_IRQn, 0, 2);
1014 // HAL_NVIC_EnableIRQ(SPI3_IRQn);
1015 
1016 }
1017 
1023 static void SD_IO_SPI_Init_LS(void)
1024 {
1025  if(HAL_SPI_GetState(&SPI_SD_Handle) == HAL_SPI_STATE_RESET)
1026  {
1027  SPI_SD_Handle.Instance = SENSORTILE_SD_SPI;
1028  SPI_SD_Handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; /* SPI baudrate is PCLK2/SPI_BaudRatePrescaler */
1029  SPI_SD_Handle.Init.Direction = SPI_DIRECTION_2LINES;
1030  SPI_SD_Handle.Init.CLKPhase = SPI_PHASE_1EDGE;//SPI_PHASE_2EDGE;SPI_PHASE_2EDGE;
1031  SPI_SD_Handle.Init.CLKPolarity = SPI_POLARITY_LOW;//SPI_POLARITY_HIGH;SPI_POLARITY_HIGH;
1032  SPI_SD_Handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
1033  SPI_SD_Handle.Init.CRCPolynomial = 7;
1034  SPI_SD_Handle.Init.DataSize = SPI_DATASIZE_8BIT;
1035  SPI_SD_Handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
1036  SPI_SD_Handle.Init.NSS = SPI_NSS_SOFT;
1037  SPI_SD_Handle.Init.TIMode = SPI_TIMODE_DISABLED;
1038  SPI_SD_Handle.Init.Mode = SPI_MODE_MASTER;
1039 
1040  SD_IO_SPI_MspInit(&SPI_SD_Handle);
1041  HAL_SPI_Init(&SPI_SD_Handle);
1042  }
1043 }
1049 static void SD_IO_SPI_Init(void)
1050 {
1051 
1052  HAL_SPI_DeInit(&SPI_SD_Handle);
1053 
1054  HAL_Delay(1);
1055 
1056  if(HAL_SPI_GetState(&SPI_SD_Handle) == HAL_SPI_STATE_RESET)
1057  {
1058  SPI_SD_Handle.Instance = SENSORTILE_SD_SPI;
1059  SPI_SD_Handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; /* SPI baudrate is PCLK2/SPI_BaudRatePrescaler */
1060  SPI_SD_Handle.Init.Direction = SPI_DIRECTION_2LINES;
1061  SPI_SD_Handle.Init.CLKPhase = SPI_PHASE_1EDGE;//SPI_PHASE_2EDGE;
1062  SPI_SD_Handle.Init.CLKPolarity = SPI_POLARITY_LOW;//SPI_POLARITY_HIGH;
1063  SPI_SD_Handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
1064  SPI_SD_Handle.Init.CRCPolynomial = 7;
1065  SPI_SD_Handle.Init.DataSize = SPI_DATASIZE_8BIT;
1066  SPI_SD_Handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
1067  SPI_SD_Handle.Init.NSS = SPI_NSS_SOFT;
1068  SPI_SD_Handle.Init.TIMode = SPI_TIMODE_DISABLED;
1069  SPI_SD_Handle.Init.Mode = SPI_MODE_MASTER;
1070 
1071  SD_IO_SPI_MspInit(&SPI_SD_Handle);
1072  HAL_SPI_Init(&SPI_SD_Handle);
1073  }
1074 }
1075 
1081 static uint32_t SD_IO_SPI_Read(void)
1082 {
1083  HAL_StatusTypeDef status = HAL_OK;
1084  uint32_t readvalue = 0;
1085  uint32_t writevalue = 0xFFFFFFFF;
1086 
1087  status = HAL_SPI_TransmitReceive(&SPI_SD_Handle, (uint8_t*) &writevalue, (uint8_t*) &readvalue, 1, SpixTimeout);
1088 
1089  /* Check the communication status */
1090  if(status != HAL_OK)
1091  {
1092  /* Execute user timeout callback */
1093  SD_IO_SPI_Error();
1094  }
1095 
1096  return readvalue;
1097 }
1098 
1104 static void SD_IO_SPI_Write(uint8_t Value)
1105 {
1106  HAL_StatusTypeDef status = HAL_OK;
1107 
1108  status = HAL_SPI_Transmit(&SPI_SD_Handle, (uint8_t*) &Value, 1, SpixTimeout);
1109 
1110  /* Check the communication status */
1111  if(status != HAL_OK)
1112  {
1113  /* Execute user timeout callback */
1114  SD_IO_SPI_Error();
1115  }
1116 }
1117 
1123 static void SD_IO_SPI_Error (void)
1124 {
1125  /* De-initialize the SPI communication BUS */
1126  HAL_SPI_DeInit(&SPI_SD_Handle);
1127 
1128  /* Re- Initiaize the SPI communication BUS */
1129  SD_IO_SPI_Init();
1130 }
1131 
1132 /******************************** LINK SD Card ********************************/
1133 
1140 void SD_IO_Init(void)
1141 {
1142  uint8_t counter;
1143 
1144  /* SD SPI Config */
1145  SD_IO_CS_Init();
1146 
1147  /* SD SPI Config */
1148  SD_IO_SPI_Init();
1149 
1150  SENSORTILE_SD_CS_HIGH();
1151 
1152  /* Send dummy byte 0xFF, 10 times with CS high */
1153  /* Rise CS and MOSI for 80 clocks cycles */
1154  for (counter = 0; counter <= 9; counter++)
1155  {
1156  /* Send dummy byte 0xFF */
1157  SD_IO_WriteByte(SENSORTILE_SD_DUMMY_BYTE);
1158  }
1159 }
1160 
1168 {
1169  uint8_t counter;
1170 
1171  /* SD SPI Config */
1172  SD_IO_CS_Init();
1173 
1174  /* SD SPI Config */
1176 
1177  SENSORTILE_SD_CS_HIGH();
1178 
1179  /* Send dummy byte 0xFF, 10 times with CS high */
1180  /* Rise CS and MOSI for 80 clocks cycles */
1181  for (counter = 0; counter <= 9; counter++)
1182  {
1183  /* Send dummy byte 0xFF */
1184  SD_IO_WriteByte(SENSORTILE_SD_DUMMY_BYTE);
1185  }
1186 }
1187 
1188 void SD_IO_CS_Init(void)
1189 {
1190  GPIO_InitTypeDef GPIO_InitStructure;
1191 
1192  /* SD_CS_GPIO and SD_DETECT_GPIO Periph clock enable */
1193  SENSORTILE_SD_CS_GPIO_CLK_ENABLE();
1194 
1195  /* Configure SD_CS_PIN pin: SD Card CS pin */
1196  GPIO_InitStructure.Pin = SENSORTILE_SD_CS_PIN;
1197  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
1198  GPIO_InitStructure.Pull = GPIO_PULLUP;
1199  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
1200  HAL_GPIO_Init(SENSORTILE_SD_CS_GPIO_PORT, &GPIO_InitStructure);
1201 
1202  /* SD chip select high */
1203 // SENSORTILE_SD_CS_HIGH();
1204 }
1205 
1206 void SD_IO_CS_DeInit(void)
1207 {
1208  GPIO_InitTypeDef GPIO_InitStructure;
1209 
1210  /* Configure SD_CS_PIN pin: SD Card CS pin */
1211  GPIO_InitStructure.Pin = SENSORTILE_SD_CS_PIN;
1212  GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
1213  GPIO_InitStructure.Pull = GPIO_NOPULL;
1214  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
1215  HAL_GPIO_Init(SENSORTILE_SD_CS_GPIO_PORT, &GPIO_InitStructure);
1216 }
1217 
1218 
1224 void SD_IO_WriteByte(uint8_t Data)
1225 {
1226  /* Send the byte */
1227  SD_IO_SPI_Write(Data);
1228 
1229 }
1230 
1231 
1237 void SD_IO_WriteDMA(uint8_t *pData, uint16_t Size)
1238 {
1239 
1240  if(HAL_SPI_Transmit_DMA(&SPI_SD_Handle, (uint8_t*)pData, Size) != HAL_OK)
1241  {
1242  /* Transfer error in transmission process */
1243  while(1);
1244  }
1245 
1246 }
1247 
1253 uint8_t SD_IO_ReadByte(void)
1254 {
1255  uint8_t data = 0;
1256 
1257  /* Get the received data */
1258  data = SD_IO_SPI_Read();
1259 
1260  /* Return the shifted data */
1261  return data;
1262 }
1263 
1264 
1273 uint8_t SD_IO_WriteCmd_wResp(uint8_t Cmd, uint32_t Arg, uint8_t Crc)
1274 {
1275  uint32_t n = 0x00, resp;
1276  uint8_t frame[6];
1277 
1278  /* Prepare Frame to send */
1279  frame[0] = (Cmd | 0x40); /* Construct byte 1 */
1280  frame[1] = (uint8_t)(Arg >> 24); /* Construct byte 2 */
1281  frame[2] = (uint8_t)(Arg >> 16); /* Construct byte 3 */
1282  frame[3] = (uint8_t)(Arg >> 8); /* Construct byte 4 */
1283  frame[4] = (uint8_t)(Arg); /* Construct byte 5 */
1284  frame[5] = (Crc); /* Construct CRC: byte 6 */
1285 
1286  /* SD chip select low */
1287  SENSORTILE_SD_CS_LOW();
1288 
1289  /* Send Frame */
1290  for (n = 0; n < 6; n++)
1291  {
1292  SD_IO_WriteByte(frame[n]); /* Send the Cmd bytes */
1293  }
1294 
1295  n = 10; /* Wait for response (10 bytes max) */
1296  do {
1297  resp = SD_IO_ReadByte();
1298  } while ((resp & 0x80) && --n);
1299 
1300  return resp; /* Return received response */
1301 }
1302 
1303 
1312 HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response)
1313 {
1314  uint32_t counter = 0x00;
1315  uint8_t frame[6];
1316 
1317  /* Prepare Frame to send */
1318  frame[0] = (Cmd | 0x40); /* Construct byte 1 */
1319  frame[1] = (uint8_t)(Arg >> 24); /* Construct byte 2 */
1320  frame[2] = (uint8_t)(Arg >> 16); /* Construct byte 3 */
1321  frame[3] = (uint8_t)(Arg >> 8); /* Construct byte 4 */
1322  frame[4] = (uint8_t)(Arg); /* Construct byte 5 */
1323  frame[5] = (Crc); /* Construct CRC: byte 6 */
1324 
1325  /* SD chip select low */
1326  SENSORTILE_SD_CS_LOW();
1327 
1328  /* Send Frame */
1329  for (counter = 0; counter < 6; counter++)
1330  {
1331  SD_IO_WriteByte(frame[counter]); /* Send the Cmd bytes */
1332  }
1333 
1334  if(Response != SENSORTILE_SD_NO_RESPONSE_EXPECTED)
1335  {
1336  return SD_IO_WaitResponse(Response);
1337  }
1338 
1339  return HAL_OK;
1340 }
1341 
1347 HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response)
1348 {
1349  uint32_t timeout = 0xFF0;//0x400;//
1350 
1351  /* Check if response is got or a timeout is happen */
1352  while ((SD_IO_ReadByte() != Response) && timeout)
1353  {
1354  timeout--;
1355  }
1356 
1357  if (timeout == 0)
1358  {
1359  /* After time out */
1360  return HAL_TIMEOUT;
1361  }
1362  else
1363  {
1364  /* Right response got */
1365  return HAL_OK;
1366  }
1367 }
1368 
1375 {
1376  /* SD chip select high */
1377  SENSORTILE_SD_CS_HIGH();
1378 
1379  /* Send Dummy byte 0xFF */
1380  SD_IO_WriteByte(SENSORTILE_SD_DUMMY_BYTE);
1381 }
1382 
1383 
1384 
1385 
1402 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
static void I2C_SENSORTILE_Error(uint8_t Addr)
Manages error callback by re-initializing I2C.
Definition: SensorTile.c:882
uint8_t SD_IO_ReadByte(void)
Reads a byte from the SD.
Definition: SensorTile.c:1253
static void SD_IO_SPI_Error(void)
SPI error treatment function.
Definition: SensorTile.c:1123
void BSP_LED_Off(Led_TypeDef Led)
Turns selected LED Off.
Definition: SensorTile.c:241
void SD_IO_WriteByte(uint8_t Data)
Writes a byte on the SD.
Definition: SensorTile.c:1224
uint32_t BSP_GetVersion(void)
This method returns the STM32446E EVAL BSP Driver revision.
Definition: SensorTile.c:159
static uint32_t SD_IO_SPI_Read(void)
SPI Read 4 bytes from device.
Definition: SensorTile.c:1081
void BSP_LED_Toggle(Led_TypeDef Led)
Toggles the selected LED.
Definition: SensorTile.c:263
#define SENSORTILE_SD_CS_PIN
SD Control Interface pins.
Definition: SensorTile.h:200
void SPI_Read(SPI_HandleTypeDef *xSpiHandle, uint8_t *val)
This function reads a single byte on SPI 3-wire.
Definition: SensorTile.c:697
uint8_t Sensor_IO_Write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
Writes a buffer to the sensor.
Definition: SensorTile.c:427
uint8_t Sensor_IO_Read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
Reads from the sensor to a buffer.
Definition: SensorTile.c:490
DrvStatusTypeDef Sensor_IO_SPI_Init(void)
Configures sensor SPI interface.
Definition: SensorTile.c:293
static void SD_IO_SPI_Init_LS(void)
Initializes SPI HAL. Low baundrate for initializazion phase.
Definition: SensorTile.c:1023
static void SD_IO_SPI_MspInit(SPI_HandleTypeDef *hspi)
Initializes SPI MSP.
Definition: SensorTile.c:945
uint8_t Sensor_IO_I2C_Write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
Writes a buffer to the sensor by I2C.
Definition: SensorTile.c:472
uint8_t SD_IO_WriteCmd_wResp(uint8_t Cmd, uint32_t Arg, uint8_t Crc)
Sends 5 bytes command to the SD card and get response.
Definition: SensorTile.c:1273
HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response)
Sends 5 bytes command to the SD card and get response.
Definition: SensorTile.c:1312
This file contains definitions for SensorTile.c file.
void BSP_LED_DeInit(Led_TypeDef Led)
DeInit LEDs.
Definition: SensorTile.c:204
void BSP_LED_Init(Led_TypeDef Led)
Configures LEDs.
Definition: SensorTile.c:172
uint8_t Sensor_IO_I2C_Read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
Reads from the sensor to a buffer by I2C.
Definition: SensorTile.c:534
static uint8_t I2C_SENSORTILE_Init(void)
Configures I2C interface.
Definition: SensorTile.c:789
void SD_IO_WriteDummy(void)
Sends dummy byte with CS High.
Definition: SensorTile.c:1374
void SPI_Write(SPI_HandleTypeDef *xSpiHandle, uint8_t val)
This function writes a single byte on SPI 3-wire.
Definition: SensorTile.c:767
static void I2C_SENSORTILE_MspInit(void)
I2C MSP Initialization.
Definition: SensorTile.c:897
void SPI_Read_nBytes(SPI_HandleTypeDef *xSpiHandle, uint8_t *val, uint16_t nBytesToRead)
This function reads multiple bytes on SPI 3-wire.
Definition: SensorTile.c:726
static void SD_IO_SPI_Write(uint8_t Value)
SPI Write a byte to device.
Definition: SensorTile.c:1104
DrvStatusTypeDef LSM6DSM_Sensor_IO_ITConfig(void)
Configures sensor interrupts interface for LSM6DSM sensor.
Definition: SensorTile.c:664
DrvStatusTypeDef Sensor_IO_I2C_Init(void)
Configures sensor SPI interface.
Definition: SensorTile.c:274
void SD_IO_Init(void)
Initializes the SD Card and put it into StandBy State (Ready for data transfer).
Definition: SensorTile.c:1140
uint8_t Sensor_IO_SPI_Write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
Writes a buffer to the sensor.
Definition: SensorTile.c:553
void BSP_LED_On(Led_TypeDef Led)
Turns selected LED On.
Definition: SensorTile.c:219
uint8_t Sensor_IO_SPI_Read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
Reads a from the sensor to buffer.
Definition: SensorTile.c:581
static uint8_t I2C_SENSORTILE_ReadData(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Size)
Read a register of the device through BUS.
Definition: SensorTile.c:857
void SD_IO_Init_LS(void)
Initializes the SD Card and put it into StandBy State (Ready for data transfer). Low baundrate...
Definition: SensorTile.c:1167
HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response)
Waits response from the SD card.
Definition: SensorTile.c:1347
static uint8_t I2C_SENSORTILE_WriteData(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Size)
Write data to the register of the device through BUS.
Definition: SensorTile.c:826
static void SD_IO_SPI_Init(void)
Initializes SPI HAL. High baundrate.
Definition: SensorTile.c:1049
void SD_IO_WriteDMA(uint8_t *pData, uint16_t Size)
Writes a block by DMA on the SD.
Definition: SensorTile.c:1237
Generated by   doxygen 1.8.13