Farnell-Element14 Bologna IOT Team / BSP_B-L475E-IOT01

Dependencies:   VL53L0X

Fork of BSP_B-L475E-IOT01 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l475e_iot01.c Source File

stm32l475e_iot01.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l475e_iot01.c
00004   * @author  MCD Application Team
00005   * @brief   STM32L475E-IOT01 board support package
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
00010   * All rights reserved.</center></h2>
00011   *
00012   * Redistribution and use in source and binary forms, with or without
00013   * modification, are permitted, provided that the following conditions are met:
00014   *
00015   * 1. Redistribution of source code must retain the above copyright notice,
00016   *    this list of conditions and the following disclaimer.
00017   * 2. Redistributions in binary form must reproduce the above copyright notice,
00018   *    this list of conditions and the following disclaimer in the documentation
00019   *    and/or other materials provided with the distribution.
00020   * 3. Neither the name of STMicroelectronics nor the names of other
00021   *    contributors to this software may be used to endorse or promote products
00022   *    derived from this software without specific written permission.
00023   * 4. This software, including modifications and/or derivative works of this
00024   *    software, must execute solely and exclusively on microcontroller or
00025   *    microprocessor devices manufactured by or for STMicroelectronics.
00026   * 5. Redistribution and use of this software other than as permitted under
00027   *    this license is void and will automatically terminate your rights under
00028   *    this license.
00029   *
00030   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
00031   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
00032   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
00033   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
00034   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
00035   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00036   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00037   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00038   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00039   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00040   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00041   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00042   *
00043   ******************************************************************************
00044   */
00045 
00046 /* Includes ------------------------------------------------------------------*/
00047 #include "stm32l475e_iot01.h"
00048 
00049 /** @defgroup BSP BSP
00050   * @{
00051   */ 
00052 
00053 /** @defgroup STM32L475E_IOT01 STM32L475E_IOT01
00054   * @{
00055   */
00056 
00057 /** @defgroup STM32L475E_IOT01_LOW_LEVEL LOW LEVEL
00058   * @{
00059   */
00060 
00061 /** @defgroup STM32L475E_IOT01_LOW_LEVEL_Private_Defines LOW LEVEL Private Def
00062   * @{
00063   */
00064 /**
00065  * @brief STM32L475E IOT01 BSP Driver version number
00066    */
00067 #define __STM32L475E_IOT01_BSP_VERSION_MAIN   (0x01) /*!< [31:24] main version */
00068 #define __STM32L475E_IOT01_BSP_VERSION_SUB1   (0x01) /*!< [23:16] sub1 version */
00069 #define __STM32L475E_IOT01_BSP_VERSION_SUB2   (0x01) /*!< [15:8]  sub2 version */
00070 #define __STM32L475E_IOT01_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
00071 #define __STM32L475E_IOT01_BSP_VERSION        ((__STM32L475E_IOT01_BSP_VERSION_MAIN << 24)\
00072                                                  |(__STM32L475E_IOT01_BSP_VERSION_SUB1 << 16)\
00073                                                  |(__STM32L475E_IOT01_BSP_VERSION_SUB2 << 8 )\
00074                                                  |(__STM32L475E_IOT01_BSP_VERSION_RC))
00075 /**
00076   * @}
00077   */
00078 
00079 /** @defgroup STM32L475E_IOT01_LOW_LEVEL_Private_Variables LOW LEVEL Variables 
00080   * @{
00081   */
00082 
00083 const uint32_t GPIO_PIN[LEDn] = {LED2_PIN};
00084 
00085 
00086 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
00087 
00088 
00089 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT};
00090 
00091 const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN};
00092 
00093 const uint16_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn};
00094 
00095 USART_TypeDef* COM_USART[COMn] = {DISCOVERY_COM1};
00096 
00097 GPIO_TypeDef* COM_TX_PORT[COMn] = {DISCOVERY_COM1_TX_GPIO_PORT};
00098 
00099 GPIO_TypeDef* COM_RX_PORT[COMn] = {DISCOVERY_COM1_RX_GPIO_PORT};
00100 
00101 const uint16_t COM_TX_PIN[COMn] = {DISCOVERY_COM1_TX_PIN};
00102 
00103 const uint16_t COM_RX_PIN[COMn] = {DISCOVERY_COM1_RX_PIN};
00104 
00105 const uint16_t COM_TX_AF[COMn] = {DISCOVERY_COM1_TX_AF};
00106 
00107 const uint16_t COM_RX_AF[COMn] = {DISCOVERY_COM1_RX_AF};
00108 
00109 I2C_HandleTypeDef hI2cHandler;
00110 UART_HandleTypeDef hDiscoUart;
00111 
00112 /**
00113   * @}
00114   */
00115 /** @defgroup STM32L475E_IOT01_LOW_LEVEL_Private_FunctionPrototypes LOW LEVEL Private Function Prototypes
00116   * @{
00117   */
00118 static void     I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler);
00119 static void     I2Cx_MspDeInit(I2C_HandleTypeDef *i2c_handler);
00120 static void     I2Cx_Init(I2C_HandleTypeDef *i2c_handler);
00121 static void     I2Cx_DeInit(I2C_HandleTypeDef *i2c_handler);
00122 static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
00123 static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
00124 static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials);
00125 static void              I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr);
00126 
00127 /* Sensors IO functions */
00128 void     SENSOR_IO_Init(void);
00129 void     SENSOR_IO_DeInit(void);
00130 void     SENSOR_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00131 uint8_t  SENSOR_IO_Read(uint8_t Addr, uint8_t Reg);
00132 uint16_t SENSOR_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00133 void     SENSOR_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00134 HAL_StatusTypeDef SENSOR_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
00135 void     SENSOR_IO_Delay(uint32_t Delay);
00136 
00137 void     NFC_IO_Init(uint8_t GpoIrqEnable);
00138 void     NFC_IO_DeInit(void);
00139 uint16_t NFC_IO_ReadMultiple (uint8_t Addr, uint8_t *pBuffer, uint16_t Length );
00140 uint16_t NFC_IO_WriteMultiple (uint8_t Addr, uint8_t *pBuffer, uint16_t Length);
00141 uint16_t NFC_IO_IsDeviceReady (uint8_t Addr, uint32_t Trials);
00142 void     NFC_IO_ReadState(uint8_t * pPinState);
00143 void     NFC_IO_RfDisable(uint8_t PinState);
00144 void     NFC_IO_Delay(uint32_t Delay);
00145 
00146 /**
00147   * @}
00148   */
00149 
00150 /** @defgroup STM32L475E_IOT01_LOW_LEVEL_Private_Functions LOW LEVEL Private Functions
00151   * @{
00152   */ 
00153 
00154 /**
00155   * @brief  This method returns the STM32L475E IOT01 BSP Driver revision
00156   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
00157   */
00158 uint32_t BSP_GetVersion(void)
00159 {
00160   return __STM32L475E_IOT01_BSP_VERSION;
00161 }
00162 
00163 /**
00164   * @brief  Configures LEDs.
00165   * @param  Led: LED to be configured. 
00166   *          This parameter can be one of the following values:
00167   *            @arg  LED2
00168   */
00169 void BSP_LED_Init(Led_TypeDef Led)
00170 {
00171   GPIO_InitTypeDef  gpio_init_structure;
00172   
00173   LEDx_GPIO_CLK_ENABLE(Led);
00174   /* Configure the GPIO_LED pin */
00175   gpio_init_structure.Pin   = GPIO_PIN[Led];
00176   gpio_init_structure.Mode  = GPIO_MODE_OUTPUT_PP;
00177   gpio_init_structure.Pull  = GPIO_NOPULL;
00178   gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
00179   
00180   HAL_GPIO_Init(GPIO_PORT[Led], &gpio_init_structure);
00181 }
00182 
00183 /**
00184   * @brief  DeInit LEDs.
00185   * @param  Led: LED to be configured. 
00186   *          This parameter can be one of the following values:
00187   *            @arg  LED2
00188   */
00189 void BSP_LED_DeInit(Led_TypeDef Led)
00190 {
00191   GPIO_InitTypeDef  gpio_init_structure;
00192   
00193   /* DeInit the GPIO_LED pin */
00194   gpio_init_structure.Pin = GPIO_PIN[Led];
00195   
00196   /* Turn off LED */
00197   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
00198   HAL_GPIO_DeInit(GPIO_PORT[Led], gpio_init_structure.Pin);
00199 }
00200 
00201 /**
00202   * @brief  Turns selected LED On.
00203   * @param  Led: LED to be set on 
00204   *          This parameter can be one of the following values:
00205   *            @arg  LED2
00206   */
00207 void BSP_LED_On(Led_TypeDef Led)
00208 {
00209   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
00210 }
00211 
00212 /**
00213   * @brief  Turns selected LED Off. 
00214   * @param  Led: LED to be set off
00215   *          This parameter can be one of the following values:
00216   *            @arg  LED2
00217   */
00218 void BSP_LED_Off(Led_TypeDef Led)
00219 {
00220   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
00221 }
00222 
00223 /**
00224   * @brief  Toggles the selected LED.
00225   * @param  Led: LED to be toggled
00226   *          This parameter can be one of the following values:
00227   *            @arg  LED2
00228   */
00229 void BSP_LED_Toggle(Led_TypeDef Led)
00230 {
00231   HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
00232 }
00233 
00234 /**
00235   * @brief  Configures button GPIO and EXTI Line.
00236   * @param  Button: Button to be configured
00237   *          This parameter can be one of the following values:
00238   *            @arg  BUTTON_WAKEUP: Wakeup Push Button
00239   * @param  ButtonMode: Button mode
00240   *          This parameter can be one of the following values:
00241   *            @arg  BUTTON_MODE_GPIO: Button will be used as simple IO
00242   *            @arg  BUTTON_MODE_EXTI: Button will be connected to EXTI line 
00243   *                                    with interrupt generation capability  
00244   */
00245 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
00246 {
00247   GPIO_InitTypeDef gpio_init_structure;
00248   
00249   /* Enable the BUTTON clock */
00250   USER_BUTTON_GPIO_CLK_ENABLE();
00251   
00252   if(ButtonMode == BUTTON_MODE_GPIO)
00253   {
00254     /* Configure Button pin as input */
00255     gpio_init_structure.Pin = BUTTON_PIN[Button];
00256     gpio_init_structure.Mode = GPIO_MODE_INPUT;
00257     gpio_init_structure.Pull = GPIO_PULLUP;
00258     gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
00259     HAL_GPIO_Init(BUTTON_PORT[Button], &gpio_init_structure);
00260   }
00261   
00262   if(ButtonMode == BUTTON_MODE_EXTI)
00263   {
00264     /* Configure Button pin as input with External interrupt */
00265     gpio_init_structure.Pin = BUTTON_PIN[Button];
00266     gpio_init_structure.Pull = GPIO_PULLUP;
00267     gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00268     
00269     gpio_init_structure.Mode = GPIO_MODE_IT_RISING;
00270     
00271     HAL_GPIO_Init(BUTTON_PORT[Button], &gpio_init_structure);
00272     
00273     /* Enable and set Button EXTI Interrupt to the lowest priority */
00274     HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
00275     HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00276   }
00277 }
00278 
00279 /**
00280   * @brief  Push Button DeInit.
00281   * @param  Button: Button to be configured
00282   *          This parameter can be one of the following values:
00283   *            @arg  BUTTON_WAKEUP: Wakeup Push Button 
00284   * @note PB DeInit does not disable the GPIO clock
00285   */
00286 void BSP_PB_DeInit(Button_TypeDef Button)
00287 {
00288   GPIO_InitTypeDef gpio_init_structure;
00289 
00290   gpio_init_structure.Pin = BUTTON_PIN[Button];
00291   HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00292   HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin);
00293 }
00294 
00295 
00296 /**
00297   * @brief  Returns the selected button state.
00298   * @param  Button: Button to be checked
00299   *          This parameter can be one of the following values:
00300   *            @arg  BUTTON_WAKEUP: Wakeup Push Button 
00301   * @retval The Button GPIO pin value (GPIO_PIN_RESET = button pressed)
00302   */
00303 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00304 {
00305   return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00306 }
00307 
00308 /**
00309   * @brief  Configures COM port.
00310   * @param  COM: COM port to be configured.
00311   *          This parameter can be one of the following values:
00312   *            @arg  COM1 
00313   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains the
00314   *                configuration information for the specified USART peripheral.
00315   */
00316 void BSP_COM_Init(COM_TypeDef COM, UART_HandleTypeDef *huart)
00317 {
00318   GPIO_InitTypeDef gpio_init_structure;
00319 
00320   /* Enable GPIO clock */
00321   DISCOVERY_COMx_TX_GPIO_CLK_ENABLE(COM);
00322   DISCOVERY_COMx_RX_GPIO_CLK_ENABLE(COM);
00323 
00324   /* Enable USART clock */
00325   DISCOVERY_COMx_CLK_ENABLE(COM);
00326 
00327   /* Configure USART Tx as alternate function */
00328   gpio_init_structure.Pin = COM_TX_PIN[COM];
00329   gpio_init_structure.Mode = GPIO_MODE_AF_PP;
00330   gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
00331   gpio_init_structure.Pull = GPIO_NOPULL;
00332   gpio_init_structure.Alternate = COM_TX_AF[COM];
00333   HAL_GPIO_Init(COM_TX_PORT[COM], &gpio_init_structure);
00334 
00335   /* Configure USART Rx as alternate function */
00336   gpio_init_structure.Pin = COM_RX_PIN[COM];
00337   gpio_init_structure.Mode = GPIO_MODE_AF_PP;
00338   gpio_init_structure.Alternate = COM_RX_AF[COM];
00339   HAL_GPIO_Init(COM_RX_PORT[COM], &gpio_init_structure);
00340 
00341   /* USART configuration */
00342   huart->Instance = COM_USART[COM];
00343   HAL_UART_Init(huart);
00344 }
00345 
00346 /**
00347   * @brief  DeInit COM port.
00348   * @param  COM: COM port to be configured.
00349   *          This parameter can be one of the following values:
00350   *            @arg  COM1 
00351   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains the
00352   *                configuration information for the specified USART peripheral.
00353   */
00354 void BSP_COM_DeInit(COM_TypeDef COM, UART_HandleTypeDef *huart)
00355 {
00356   /* USART configuration */
00357   huart->Instance = COM_USART[COM];
00358   HAL_UART_DeInit(huart);
00359 
00360   /* Enable USART clock */
00361   DISCOVERY_COMx_CLK_DISABLE(COM);
00362 
00363   /* DeInit GPIO pins can be done in the application 
00364      (by surcharging this __weak function) */
00365 
00366   /* GPIO pins clock, FMC clock and DMA clock can be shut down in the application 
00367      by surcharging this __weak function */ 
00368 }
00369 
00370 
00371 /*******************************************************************************
00372                             BUS OPERATIONS
00373 *******************************************************************************/
00374 
00375 /******************************* I2C Routines *********************************/
00376 /**
00377   * @brief  Initializes I2C MSP.
00378   * @param  i2c_handler : I2C handler
00379   * @retval None
00380   */
00381 static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler)
00382 {
00383   GPIO_InitTypeDef  gpio_init_structure;
00384 
00385   /*** Configure the GPIOs ***/
00386   /* Enable GPIO clock */
00387   DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
00388 
00389   /* Configure I2C Tx, Rx as alternate function */
00390   gpio_init_structure.Pin = DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN;
00391   gpio_init_structure.Mode = GPIO_MODE_AF_OD;
00392   gpio_init_structure.Pull = GPIO_PULLUP;
00393   gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00394   gpio_init_structure.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
00395   HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
00396 
00397   HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
00398 
00399   /*** Configure the I2C peripheral ***/
00400   /* Enable I2C clock */
00401   DISCOVERY_I2Cx_CLK_ENABLE();
00402 
00403   /* Force the I2C peripheral clock reset */
00404   DISCOVERY_I2Cx_FORCE_RESET();
00405 
00406   /* Release the I2C peripheral clock reset */
00407   DISCOVERY_I2Cx_RELEASE_RESET();
00408 
00409   /* Enable and set I2Cx Interrupt to a lower priority */
00410   HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0x0F, 0);
00411   HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
00412 
00413   /* Enable and set I2Cx Interrupt to a lower priority */
00414   HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0x0F, 0);
00415   HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);
00416 }
00417 
00418 /**
00419   * @brief  DeInitializes I2C MSP.
00420   * @param  i2c_handler : I2C handler
00421   * @retval None
00422   */
00423 static void I2Cx_MspDeInit(I2C_HandleTypeDef *i2c_handler)
00424 {
00425   GPIO_InitTypeDef  gpio_init_structure;
00426   
00427   /* Configure I2C Tx, Rx as alternate function */
00428   gpio_init_structure.Pin = DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN;
00429   HAL_GPIO_DeInit(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, gpio_init_structure.Pin);
00430   /* Disable GPIO clock */
00431   DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_DISABLE();
00432   
00433   /* Disable I2C clock */
00434   DISCOVERY_I2Cx_CLK_DISABLE();
00435 }
00436 
00437 /**
00438   * @brief  Initializes I2C HAL.
00439   * @param  i2c_handler : I2C handler
00440   * @retval None
00441   */
00442 static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler)
00443 {
00444   /* I2C configuration */
00445   i2c_handler->Instance              = DISCOVERY_I2Cx;
00446   i2c_handler->Init.Timing           = DISCOVERY_I2Cx_TIMING;
00447   i2c_handler->Init.OwnAddress1      = 0;
00448   i2c_handler->Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
00449   i2c_handler->Init.DualAddressMode  = I2C_DUALADDRESS_DISABLE;
00450   i2c_handler->Init.OwnAddress2      = 0;
00451   i2c_handler->Init.GeneralCallMode  = I2C_GENERALCALL_DISABLE;
00452   i2c_handler->Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;
00453 
00454   /* Init the I2C */
00455   I2Cx_MspInit(i2c_handler);
00456   HAL_I2C_Init(i2c_handler);
00457   
00458   /**Configure Analogue filter */
00459   HAL_I2CEx_ConfigAnalogFilter(i2c_handler, I2C_ANALOGFILTER_ENABLE);  
00460 }
00461 
00462 /**
00463   * @brief  DeInitializes I2C HAL.
00464   * @param  i2c_handler : I2C handler
00465   * @retval None
00466   */
00467 static void I2Cx_DeInit(I2C_HandleTypeDef *i2c_handler)
00468 {  /* DeInit the I2C */
00469   I2Cx_MspDeInit(i2c_handler);
00470   HAL_I2C_DeInit(i2c_handler); 
00471 }
00472 
00473 /**
00474   * @brief  Reads multiple data.
00475   * @param  i2c_handler : I2C handler
00476   * @param  Addr: I2C address
00477   * @param  Reg: Reg address
00478   * @param  MemAddress: memory address
00479   * @param  Buffer: Pointer to data buffer
00480   * @param  Length: Length of the data
00481   * @retval HAL status
00482   */
00483 static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddress, uint8_t *Buffer, uint16_t Length)
00484 {
00485   HAL_StatusTypeDef status = HAL_OK;
00486 
00487   status = HAL_I2C_Mem_Read(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
00488 
00489   /* Check the communication status */
00490   if(status != HAL_OK)
00491   {
00492     /* I2C error occured */
00493     I2Cx_Error(i2c_handler, Addr);
00494   }
00495   return status;
00496 }
00497 
00498 
00499 /**
00500   * @brief  Writes a value in a register of the device through BUS in using DMA mode.
00501   * @param  i2c_handler : I2C handler
00502   * @param  Addr: Device address on BUS Bus.
00503   * @param  Reg: The target register address to write
00504   * @param  MemAddress: memory address
00505   * @param  Buffer: The target register value to be written
00506   * @param  Length: buffer size to be written
00507   * @retval HAL status
00508   */
00509 static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddress, uint8_t *Buffer, uint16_t Length)
00510 {
00511   HAL_StatusTypeDef status = HAL_OK;
00512 
00513   status = HAL_I2C_Mem_Write(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
00514 
00515   /* Check the communication status */
00516   if(status != HAL_OK)
00517   {
00518     /* Re-Initiaize the I2C Bus */
00519     I2Cx_Error(i2c_handler, Addr);
00520   }
00521   return status;
00522 }
00523 
00524 /**
00525   * @brief  Checks if target device is ready for communication. 
00526   * @note   This function is used with Memory devices
00527   * @param  i2c_handler : I2C handler
00528   * @param  DevAddress: Target device address
00529   * @param  Trials: Number of trials
00530   * @retval HAL status
00531   */
00532 static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials)
00533 { 
00534   return (HAL_I2C_IsDeviceReady(i2c_handler, DevAddress, Trials, 1000));
00535 }
00536 
00537 /**
00538   * @brief  Manages error callback by re-initializing I2C.
00539   * @param  i2c_handler : I2C handler
00540   * @param  Addr: I2C Address
00541   * @retval None
00542   */
00543 static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr)
00544 {
00545   /* De-initialize the I2C communication bus */
00546   HAL_I2C_DeInit(i2c_handler);
00547   
00548   /* Re-Initialize the I2C communication bus */
00549   I2Cx_Init(i2c_handler);
00550 }
00551 
00552 /**
00553   * @}
00554   */
00555 
00556 /*******************************************************************************
00557                             LINK OPERATIONS
00558 *******************************************************************************/
00559 /******************************** LINK Sensors ********************************/
00560 
00561 /**
00562   * @brief  Initializes Sensors low level.
00563   * @retval None
00564   */
00565 void SENSOR_IO_Init(void)
00566 {
00567   I2Cx_Init(&hI2cHandler);
00568 }
00569 
00570 /**
00571   * @brief  DeInitializes Sensors low level.
00572   * @retval None
00573   */
00574 void SENSOR_IO_DeInit(void)
00575 {
00576   I2Cx_DeInit(&hI2cHandler);
00577 }
00578 
00579 /**
00580   * @brief  Writes a single data.
00581   * @param  Addr: I2C address
00582   * @param  Reg: Reg address
00583   * @param  Value: Data to be written
00584   * @retval None
00585   */
00586 void SENSOR_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
00587 {
00588   I2Cx_WriteMultiple(&hI2cHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
00589 }
00590 
00591 /**
00592   * @brief  Reads a single data.
00593   * @param  Addr: I2C address
00594   * @param  Reg: Reg address
00595   * @retval Data to be read
00596   */
00597 uint8_t SENSOR_IO_Read(uint8_t Addr, uint8_t Reg)
00598 {
00599   uint8_t read_value = 0;
00600 
00601   I2Cx_ReadMultiple(&hI2cHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&read_value, 1);
00602 
00603   return read_value;
00604 }
00605 
00606 /**
00607   * @brief  Reads multiple data with I2C communication
00608   *         channel from TouchScreen.
00609   * @param  Addr: I2C address
00610   * @param  Reg: Register address
00611   * @param  Buffer: Pointer to data buffer
00612   * @param  Length: Length of the data
00613   * @retval HAL status
00614   */
00615 uint16_t SENSOR_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
00616 {
00617  return I2Cx_ReadMultiple(&hI2cHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
00618 }
00619 
00620 /**
00621   * @brief  Writes multiple data with I2C communication
00622   *         channel from MCU to TouchScreen.
00623   * @param  Addr: I2C address
00624   * @param  Reg: Register address
00625   * @param  Buffer: Pointer to data buffer
00626   * @param  Length: Length of the data
00627   * @retval None
00628   */
00629 void SENSOR_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
00630 {
00631   I2Cx_WriteMultiple(&hI2cHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
00632 }
00633 
00634 /**
00635   * @brief  Checks if target device is ready for communication. 
00636   * @note   This function is used with Memory devices
00637   * @param  DevAddress: Target device address
00638   * @param  Trials: Number of trials
00639   * @retval HAL status
00640   */
00641 HAL_StatusTypeDef SENSOR_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
00642 { 
00643   return (I2Cx_IsDeviceReady(&hI2cHandler, DevAddress, Trials));
00644 }
00645 
00646 /**
00647   * @brief  Delay function used in TouchScreen low level driver.
00648   * @param  Delay: Delay in ms
00649   * @retval None
00650   */
00651 void SENSOR_IO_Delay(uint32_t Delay)
00652 {
00653   HAL_Delay(Delay);
00654 }
00655 
00656 /******************************** LINK NFC ********************************/
00657 
00658 /**
00659   * @brief  Initializes Sensors low level.
00660   * @param  GpoIrqEnable: 0x0 is disable, otherwise enabled  
00661   * @retval None
00662   */
00663 void NFC_IO_Init(uint8_t GpoIrqEnable)
00664 {
00665   GPIO_InitTypeDef GPIO_InitStruct;
00666 
00667   /* I2C init */
00668   I2Cx_Init(&hI2cHandler);
00669   
00670   /* GPIO Ports Clock Enable */
00671   NFC_GPIO_CLK_ENABLE();
00672   
00673   /* Configure GPIO pins for GPO (PE4) */
00674   if(GpoIrqEnable == 0)
00675   {
00676     GPIO_InitStruct.Pin = NFC_GPIO_GPO_PIN;
00677     GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 
00678     GPIO_InitStruct.Pull = GPIO_PULLUP;
00679     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00680     HAL_GPIO_Init(NFC_GPIO_GPO_PIN_PORT, &GPIO_InitStruct);
00681   }
00682   else
00683   {
00684     GPIO_InitStruct.Pin = NFC_GPIO_GPO_PIN;
00685     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
00686     GPIO_InitStruct.Pull = GPIO_PULLUP;
00687     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00688     HAL_GPIO_Init(NFC_GPIO_GPO_PIN_PORT, &GPIO_InitStruct);
00689     /* Enable and set EXTI4_IRQn Interrupt to the lowest priority */
00690     HAL_NVIC_SetPriority(EXTI4_IRQn, 3, 0);
00691     HAL_NVIC_EnableIRQ(EXTI4_IRQn);    
00692   }
00693   
00694   /* Configure GPIO pins for DISABLE (PE2)*/
00695   GPIO_InitStruct.Pin = NFC_GPIO_RFDISABLE_PIN;
00696   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00697   GPIO_InitStruct.Pull = GPIO_NOPULL;
00698   HAL_GPIO_Init(NFC_GPIO_RFDISABLE_PIN_PORT, &GPIO_InitStruct);
00699 }
00700 
00701 /**
00702   * @brief  DeInitializes Sensors low level.
00703   * @retval None
00704   */
00705 void NFC_IO_DeInit(void)
00706 {
00707   I2Cx_DeInit(&hI2cHandler);
00708 }
00709 
00710 /**
00711   * @brief  This functions reads a response of the M24SR device
00712   * @param  Addr: M24SR I2C address   (do we really need to add?)
00713   * @param  pBuffer : Pointer on the buffer to retrieve M24SR response
00714   * @param  Length: Length of the data
00715   * @retval Status: Success or Timeout
00716   */
00717 uint16_t NFC_IO_ReadMultiple (uint8_t Addr, uint8_t *pBuffer, uint16_t Length )
00718 {
00719   uint16_t status ;
00720     
00721     /* Before calling this function M24SR must be ready: check to detect potential issues */
00722   status = NFC_IO_IsDeviceReady(Addr, NFC_I2C_TRIALS);
00723   if (status != NFC_I2C_STATUS_SUCCESS)
00724   {
00725     return NFC_I2C_ERROR_TIMEOUT;
00726   } 
00727     
00728   if( HAL_I2C_Master_Receive(&hI2cHandler, Addr, (uint8_t*)pBuffer, Length, NFC_I2C_TIMEOUT_STD) != HAL_OK)
00729   {
00730     return NFC_I2C_ERROR_TIMEOUT;
00731   }
00732     
00733   return NFC_I2C_STATUS_SUCCESS;    
00734 }
00735 
00736 /**
00737   * @brief  This functions sends the command buffer 
00738   * @param  Addr: M24SR I2C address   (do we really need to add?)
00739   * @param  pBuffer : pointer to the buffer to send to the M24SR
00740   * @param  Length: Length of the data
00741   * @retval Status: Success or Timeout
00742   */
00743 uint16_t  NFC_IO_WriteMultiple (uint8_t Addr, uint8_t *pBuffer, uint16_t Length) 
00744 {
00745   uint16_t status ;
00746     
00747   /* Before calling this function M24SR must be ready: check to detect potential issues */
00748   status = NFC_IO_IsDeviceReady(Addr, NFC_I2C_TRIALS);
00749   if (status != NFC_I2C_STATUS_SUCCESS)
00750   {
00751     return NFC_I2C_ERROR_TIMEOUT;
00752   } 
00753     
00754   if( HAL_I2C_Master_Transmit(&hI2cHandler, Addr, (uint8_t*)pBuffer, Length, NFC_I2C_TIMEOUT_STD) != HAL_OK)    
00755   {
00756     return NFC_I2C_ERROR_TIMEOUT;
00757   }
00758 
00759   return NFC_I2C_STATUS_SUCCESS;
00760 }
00761 
00762 /**
00763   * @brief  Checks if target device is ready for communication.
00764   * @param  Addr: M24SR I2C address   (do we really need to add?)
00765   * @param  Trials: Number of trials (currently not present in M24sr)
00766   * @retval Status: Success or Timeout
00767   */
00768 uint16_t   NFC_IO_IsDeviceReady (uint8_t Addr, uint32_t Trials)
00769 {
00770     HAL_StatusTypeDef status;
00771     uint32_t tickstart = 0;
00772     uint32_t currenttick = 0;
00773     
00774     /* Get tick */
00775     tickstart = HAL_GetTick();
00776     
00777     /* Wait until M24SR is ready or timeout occurs */
00778     do
00779     {
00780         status = HAL_I2C_IsDeviceReady(&hI2cHandler, Addr, Trials, NFC_I2C_TIMEOUT_STD);
00781         currenttick = HAL_GetTick();
00782     } while( ( (currenttick - tickstart) < NFC_I2C_TIMEOUT_MAX) && (status != HAL_OK) );
00783     
00784     if (status != HAL_OK)
00785     {
00786         return NFC_I2C_ERROR_TIMEOUT;
00787     } 
00788 
00789     return NFC_I2C_STATUS_SUCCESS;
00790 }
00791 
00792 /**
00793   * @brief  This function read the state of the M24SR GPO
00794   * @param  none
00795   * @retval GPIO_PinState : state of the M24SR GPO
00796   */
00797 void NFC_IO_ReadState(uint8_t * pPinState)
00798 {
00799   *pPinState = (uint8_t)HAL_GPIO_ReadPin(NFC_GPIO_GPO_PIN_PORT,NFC_GPIO_GPO_PIN);
00800 }
00801 
00802 /**
00803   * @brief  This function set the state of the M24SR RF disable pin
00804   * @param  PinState: put RF disable pin of M24SR in PinState (1 or 0)
00805   */
00806 void NFC_IO_RfDisable(uint8_t PinState)
00807 {
00808   HAL_GPIO_WritePin(NFC_GPIO_RFDISABLE_PIN_PORT,NFC_GPIO_RFDISABLE_PIN,(GPIO_PinState)PinState);
00809 }
00810 
00811 /**
00812   * @brief  This function wait the time given in param (in milisecond)
00813   * @param  Delay: Delay in ms
00814   * @retval None
00815   */
00816 void NFC_IO_Delay(uint32_t Delay)
00817 {
00818   HAL_Delay(Delay);
00819 }
00820 
00821 /**
00822   * @}
00823   */
00824 
00825 /**
00826   * @}
00827   */
00828 
00829 /**
00830   * @}
00831   */
00832 
00833 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/