test sending sensor results over lora radio. Accelerometer and temp/pressure.

Dependencies:   SX127x

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers x_nucleo_iks01a2.c Source File

x_nucleo_iks01a2.c

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    x_nucleo_iks01a2.c
00004  * @author  MEMS Application Team
00005  * @brief   This file provides X_NUCLEO_IKS01A2 MEMS shield board specific functions
00006  ******************************************************************************
00007  * @attention
00008  *
00009  * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
00010  *
00011  * Redistribution and use in source and binary forms, with or without modification,
00012  * are permitted provided that the following conditions are met:
00013  *   1. Redistributions of source code must retain the above copyright notice,
00014  *      this list of conditions and the following disclaimer.
00015  *   2. Redistributions in binary form must reproduce the above copyright notice,
00016  *      this list of conditions and the following disclaimer in the documentation
00017  *      and/or other materials provided with the distribution.
00018  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00019  *      may be used to endorse or promote products derived from this software
00020  *      without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00030  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032  *
00033  ******************************************************************************
00034  */
00035 
00036 /* Includes ------------------------------------------------------------------*/
00037 
00038 #include "x_nucleo_iks01a2.h"
00039 
00040 
00041 /** @addtogroup BSP BSP
00042  * @{
00043  */
00044 
00045 /** @addtogroup X_NUCLEO_IKS01A2 X_NUCLEO_IKS01A2
00046  * @{
00047  */
00048 
00049 /** @addtogroup X_NUCLEO_IKS01A2_IO IO
00050  * @{
00051  */
00052 
00053 /** @addtogroup X_NUCLEO_IKS01A2_IO_Private_Variables Private variables
00054  * @{
00055  */
00056 
00057 static uint32_t I2C_EXPBD_Timeout =
00058   NUCLEO_I2C_EXPBD_TIMEOUT_MAX;    /*<! Value of Timeout when I2C communication fails */
00059 static I2C_HandleTypeDef I2C_EXPBD_Handle;
00060 
00061 /**
00062  * @}
00063  */
00064 
00065 /* Link function for sensor peripheral */
00066 uint8_t Sensor_IO_Write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);
00067 uint8_t Sensor_IO_Read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);
00068 
00069 static void I2C_EXPBD_MspInit(void);
00070 static void I2C_EXPBD_Error(uint8_t Addr);
00071 static uint8_t I2C_EXPBD_ReadData(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Size);
00072 static uint8_t I2C_EXPBD_WriteData(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Size);
00073 static uint8_t I2C_EXPBD_Init(void);
00074 
00075 /** @addtogroup X_NUCLEO_IKS01A2_IO_Public_Functions Public functions
00076  * @{
00077  */
00078 
00079 /**
00080  * @brief  Configures sensor I2C interface.
00081  * @param  None
00082  * @retval COMPONENT_OK in case of success
00083  * @retval COMPONENT_ERROR in case of failure
00084  */
00085 DrvStatusTypeDef Sensor_IO_Init(void)
00086 {
00087   if (I2C_EXPBD_Init())
00088   {
00089     return COMPONENT_ERROR;
00090   }
00091   else
00092   {
00093     return COMPONENT_OK;
00094   }
00095 }
00096 
00097 /**
00098  * @brief  Configures sensor interrupts interface for sensor in DIL24 socket.
00099  * @param  None
00100  * @retval COMPONENT_OK in case of success
00101  * @retval COMPONENT_ERROR in case of failure
00102  */
00103 DrvStatusTypeDef DIL24_Sensor_IO_ITConfig(void)
00104 {
00105   GPIO_InitTypeDef GPIO_InitStructureInt1;
00106   GPIO_InitTypeDef GPIO_InitStructureInt2;
00107 
00108   /* Enable INT1 GPIO clock */
00109   M_INT1_O_GPIO_CLK_ENABLE();
00110 
00111   /* Configure GPIO PINs to detect Interrupts */
00112   GPIO_InitStructureInt1.Pin = M_INT1_O_PIN;
00113   GPIO_InitStructureInt1.Mode = GPIO_MODE_IT_RISING;
00114   GPIO_InitStructureInt1.Speed = GPIO_SPEED_FREQ_HIGH;
00115   GPIO_InitStructureInt1.Pull  = GPIO_NOPULL;
00116   HAL_GPIO_Init(M_INT1_O_GPIO_PORT, &GPIO_InitStructureInt1);
00117 
00118   /* Enable and set EXTI Interrupt priority */
00119   HAL_NVIC_SetPriority(M_INT1_O_EXTI_IRQn, 0x00, 0x00);
00120   HAL_NVIC_EnableIRQ(M_INT1_O_EXTI_IRQn);
00121 
00122   /* Enable INT2 GPIO clock */
00123   M_INT2_O_GPIO_CLK_ENABLE();
00124 
00125   /* Configure GPIO PINs to detect Interrupts */
00126   GPIO_InitStructureInt2.Pin = M_INT2_O_PIN;
00127   GPIO_InitStructureInt2.Mode = GPIO_MODE_IT_RISING;
00128   GPIO_InitStructureInt2.Speed = GPIO_SPEED_FREQ_HIGH;
00129   GPIO_InitStructureInt2.Pull  = GPIO_NOPULL;
00130   HAL_GPIO_Init(M_INT2_O_GPIO_PORT, &GPIO_InitStructureInt2);
00131 
00132   /* Enable and set EXTI Interrupt priority */
00133   HAL_NVIC_SetPriority(M_INT2_O_EXTI_IRQn, 0x00, 0x00);
00134   HAL_NVIC_EnableIRQ(M_INT2_O_EXTI_IRQn);
00135 
00136   return COMPONENT_OK;
00137 }
00138 
00139 /**
00140  * @}
00141  */
00142 
00143 /** @addtogroup X_NUCLEO_IKS01A2_IO_Private_Functions Private functions
00144  * @{
00145  */
00146 
00147 /******************************* Link functions *******************************/
00148 
00149 /**
00150  * @brief  Writes a buffer to the sensor
00151  * @param  handle instance handle
00152  * @param  WriteAddr specifies the internal sensor address register to be written to
00153  * @param  pBuffer pointer to data buffer
00154  * @param  nBytesToWrite number of bytes to be written
00155  * @retval 0 in case of success
00156  * @retval 1 in case of failure
00157  */
00158 uint8_t Sensor_IO_Write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
00159 {
00160   uint8_t ret = 0;
00161   DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
00162 
00163   switch (ctx->who_am_i)
00164   {
00165     case IKS01A2_LSM303AGR_ACC_LIS2DH12_WHO_AM_I:
00166     case IKS01A2_LSM303AGR_MAG_LIS2MDL_IIS2MDC_ISM303DAC_MAG_WHO_AM_I:
00167     case IKS01A2_ISM303DAC_ACC_WHO_AM_I:
00168     case IKS01A2_HTS221_WHO_AM_I:
00169     case IKS01A2_LPS22HB_LPS33HW_WHO_AM_I:
00170     case IKS01A2_H3LIS331DL_AIS328DQ_AIS3624DQ_WHO_AM_I:
00171     case IKS01A2_A3G4250D_WHO_AM_I:
00172       if (nBytesToWrite > 1)
00173       {
00174         WriteAddr |= 0x80;  /* Enable I2C multi-bytes Write */
00175       }
00176       /* call I2C_EXPBD Write data bus function */
00177       if (I2C_EXPBD_WriteData(ctx->address, WriteAddr, pBuffer, nBytesToWrite))
00178       {
00179         ret = 1;
00180       }
00181       break;
00182 
00183     case IKS01A2_LSM6DSL_ISM330DLC_WHO_AM_I:
00184     case IKS01A2_LIS2DW12_IIS2DLPC_WHO_AM_I:
00185     case IKS01A2_LPS22HH_WHO_AM_I:
00186     case IKS01A2_LSM6DSO_WHO_AM_I:
00187     case IKS01A2_LSM6DSR_WHO_AM_I:
00188     default:
00189       /* call I2C_EXPBD Write data bus function */
00190       if (I2C_EXPBD_WriteData(ctx->address, WriteAddr, pBuffer, nBytesToWrite))
00191       {
00192         ret = 1;
00193       }
00194       break;
00195   }
00196 
00197   return ret;
00198 }
00199 
00200 
00201 
00202 /**
00203  * @brief  Reads a from the sensor to buffer
00204  * @param  handle instance handle
00205  * @param  ReadAddr specifies the internal sensor address register to be read from
00206  * @param  pBuffer pointer to data buffer
00207  * @param  nBytesToRead number of bytes to be read
00208  * @retval 0 in case of success
00209  * @retval 1 in case of failure
00210  */
00211 uint8_t Sensor_IO_Read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead)
00212 {
00213   uint8_t ret = 0;
00214   DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
00215 
00216   switch (ctx->who_am_i)
00217   {
00218     case IKS01A2_LSM303AGR_ACC_LIS2DH12_WHO_AM_I:
00219     case IKS01A2_LSM303AGR_MAG_LIS2MDL_IIS2MDC_ISM303DAC_MAG_WHO_AM_I:
00220     case IKS01A2_ISM303DAC_ACC_WHO_AM_I:
00221     case IKS01A2_HTS221_WHO_AM_I:
00222     case IKS01A2_LPS22HB_LPS33HW_WHO_AM_I:
00223     case IKS01A2_H3LIS331DL_AIS328DQ_AIS3624DQ_WHO_AM_I:
00224     case IKS01A2_A3G4250D_WHO_AM_I:
00225       if (nBytesToRead > 1)
00226       {
00227         ReadAddr |= 0x80;  /* Enable I2C multi-bytes Read */
00228       }
00229       /* call I2C_EXPBD Read data bus function */
00230       if (I2C_EXPBD_ReadData(ctx->address, ReadAddr, pBuffer, nBytesToRead))
00231       {
00232         ret = 1;
00233       }
00234       break;
00235 
00236     case IKS01A2_LSM6DSL_ISM330DLC_WHO_AM_I:
00237     case IKS01A2_LIS2DW12_IIS2DLPC_WHO_AM_I:
00238     case IKS01A2_LPS22HH_WHO_AM_I:
00239     case IKS01A2_LSM6DSO_WHO_AM_I:
00240     case IKS01A2_LSM6DSR_WHO_AM_I:
00241     default:
00242       /* call I2C_EXPBD Read data bus function */
00243       if (I2C_EXPBD_ReadData(ctx->address, ReadAddr, pBuffer, nBytesToRead))
00244       {
00245         ret = 1;
00246       }
00247       break;
00248   }
00249 
00250   return ret;
00251 }
00252 
00253 
00254 
00255 /******************************* I2C Routines *********************************/
00256 
00257 /**
00258  * @brief  Configures I2C interface.
00259  * @param  None
00260  * @retval 0 in case of success
00261  * @retval 1 in case of failure
00262  */
00263 static uint8_t I2C_EXPBD_Init(void)
00264 {
00265   if (HAL_I2C_GetState(&I2C_EXPBD_Handle) == HAL_I2C_STATE_RESET)
00266   {
00267 
00268     /* I2C_EXPBD peripheral configuration */
00269 
00270 #if ((defined (USE_STM32F4XX_NUCLEO)) || (defined (USE_STM32L1XX_NUCLEO)))
00271     I2C_EXPBD_Handle.Init.ClockSpeed = NUCLEO_I2C_EXPBD_SPEED;
00272     I2C_EXPBD_Handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
00273 #endif
00274 
00275 #if (defined (TARGET_STM32L0) || defined (USE_STM32L4XX_NUCLEO))
00276     I2C_EXPBD_Handle.Init.Timing = NUCLEO_I2C_EXPBD_TIMING_400KHZ;    /* 400KHz */
00277 #endif
00278 
00279     I2C_EXPBD_Handle.Init.OwnAddress1    = 0x33;
00280     I2C_EXPBD_Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
00281     I2C_EXPBD_Handle.Instance            = NUCLEO_I2C_EXPBD;
00282 
00283     /* Init the I2C */
00284     I2C_EXPBD_MspInit();
00285     HAL_I2C_Init(&I2C_EXPBD_Handle);
00286   }
00287 
00288   if (HAL_I2C_GetState(&I2C_EXPBD_Handle) == HAL_I2C_STATE_READY)
00289   {
00290     return 0;
00291   }
00292   else
00293   {
00294     return 1;
00295   }
00296 }
00297 
00298 
00299 
00300 /**
00301  * @brief  Write data to the register of the device through BUS
00302  * @param  Addr Device address on BUS
00303  * @param  Reg The target register address to be written
00304  * @param  pBuffer The data to be written
00305  * @param  Size Number of bytes to be written
00306  * @retval 0 in case of success
00307  * @retval 1 in case of failure
00308  */
00309 static uint8_t I2C_EXPBD_WriteData(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Size)
00310 {
00311 
00312   HAL_StatusTypeDef status = HAL_OK;
00313 
00314   status = HAL_I2C_Mem_Write(&I2C_EXPBD_Handle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Size,
00315                              I2C_EXPBD_Timeout);
00316 
00317   /* Check the communication status */
00318   if (status != HAL_OK)
00319   {
00320 
00321     /* Execute user timeout callback */
00322     I2C_EXPBD_Error(Addr);
00323     return 1;
00324   }
00325   else
00326   {
00327     return 0;
00328   }
00329 }
00330 
00331 
00332 
00333 /**
00334  * @brief  Read a register of the device through BUS
00335  * @param  Addr Device address on BUS
00336  * @param  Reg The target register address to read
00337  * @param  pBuffer The data to be read
00338  * @param  Size Number of bytes to be read
00339  * @retval 0 in case of success
00340  * @retval 1 in case of failure
00341  */
00342 static uint8_t I2C_EXPBD_ReadData(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Size)
00343 {
00344 
00345   HAL_StatusTypeDef status = HAL_OK;
00346 
00347   status = HAL_I2C_Mem_Read(&I2C_EXPBD_Handle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Size,
00348                             I2C_EXPBD_Timeout);
00349 
00350   /* Check the communication status */
00351   if (status != HAL_OK)
00352   {
00353     /* Execute user timeout callback */
00354     I2C_EXPBD_Error(Addr);
00355     return 1;
00356   }
00357   else
00358   {
00359     return 0;
00360   }
00361 }
00362 
00363 
00364 
00365 /**
00366  * @brief  Manages error callback by re-initializing I2C
00367  * @param  Addr I2C Address
00368  * @retval None
00369  */
00370 static void I2C_EXPBD_Error(uint8_t Addr)
00371 {
00372 
00373   /* De-initialize the I2C comunication bus */
00374   HAL_I2C_DeInit(&I2C_EXPBD_Handle);
00375 
00376   /* Re-Initiaize the I2C comunication bus */
00377   I2C_EXPBD_Init();
00378 }
00379 
00380 
00381 /**
00382  * @brief I2C MSP Initialization
00383  * @param None
00384  * @retval None
00385  */
00386 static void I2C_EXPBD_MspInit(void)
00387 {
00388   GPIO_InitTypeDef  GPIO_InitStruct;
00389 
00390   /* Enable I2C GPIO clocks */
00391   NUCLEO_I2C_EXPBD_SCL_SDA_GPIO_CLK_ENABLE();
00392 
00393   /* I2C_EXPBD SCL and SDA pins configuration -------------------------------------*/
00394   GPIO_InitStruct.Pin        = NUCLEO_I2C_EXPBD_SCL_PIN | NUCLEO_I2C_EXPBD_SDA_PIN;
00395   GPIO_InitStruct.Mode       = GPIO_MODE_AF_OD;
00396   GPIO_InitStruct.Speed      = GPIO_SPEED_FREQ_HIGH;
00397   GPIO_InitStruct.Pull       = GPIO_NOPULL;
00398   GPIO_InitStruct.Alternate  = NUCLEO_I2C_EXPBD_SCL_SDA_AF;
00399 
00400   HAL_GPIO_Init(NUCLEO_I2C_EXPBD_SCL_SDA_GPIO_PORT, &GPIO_InitStruct);
00401 
00402   /* Enable the I2C_EXPBD peripheral clock */
00403   NUCLEO_I2C_EXPBD_CLK_ENABLE();
00404 
00405   /* Force the I2C peripheral clock reset */
00406   NUCLEO_I2C_EXPBD_FORCE_RESET();
00407 
00408   /* Release the I2C peripheral clock reset */
00409   NUCLEO_I2C_EXPBD_RELEASE_RESET();
00410 
00411   /* Enable and set I2C_EXPBD Interrupt to the highest priority */
00412   HAL_NVIC_SetPriority(NUCLEO_I2C_EXPBD_EV_IRQn, 0, 0);
00413   HAL_NVIC_EnableIRQ(NUCLEO_I2C_EXPBD_EV_IRQn);
00414 
00415 #if ((defined (USE_STM32F4XX_NUCLEO)) || (defined (USE_STM32L1XX_NUCLEO)) || (defined (USE_STM32L4XX_NUCLEO)))
00416   /* Enable and set I2C_EXPBD Interrupt to the highest priority */
00417   HAL_NVIC_SetPriority(NUCLEO_I2C_EXPBD_ER_IRQn, 0, 0);
00418   HAL_NVIC_EnableIRQ(NUCLEO_I2C_EXPBD_ER_IRQn);
00419 #endif
00420 
00421 }
00422 #if 0
00423 #endif /* if 0 */
00424 
00425 /**
00426  * @}
00427  */
00428 
00429 /**
00430  * @}
00431  */
00432 
00433 /**
00434  * @}
00435  */
00436 
00437 /**
00438  * @}
00439  */
00440 
00441 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/