Andrew Lindsay / BSP_DISCO_F429ZI

Dependents:   EspSerial

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f429i_discovery.c Source File

stm32f429i_discovery.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f429i_discovery.c
00004   * @author  MCD Application Team
00005   * @version V2.1.2
00006   * @date    02-March-2015
00007   * @brief   This file provides set of firmware functions to manage Leds and
00008   *          push-button available on STM32F429I-Discovery Kit from STMicroelectronics.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013   *
00014   * Redistribution and use in source and binary forms, with or without modification,
00015   * are permitted provided that the following conditions are met:
00016   *   1. Redistributions of source code must retain the above copyright notice,
00017   *      this list of conditions and the following disclaimer.
00018   *   2. Redistributions in binary form must reproduce the above copyright notice,
00019   *      this list of conditions and the following disclaimer in the documentation
00020   *      and/or other materials provided with the distribution.
00021   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022   *      may be used to endorse or promote products derived from this software
00023   *      without specific prior written permission.
00024   *
00025   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035   *
00036   ******************************************************************************
00037   */  
00038   
00039 /* Includes ------------------------------------------------------------------*/
00040 #include "stm32f429i_discovery.h"
00041 
00042 // mbed
00043 void wait_ms(int ms);
00044 
00045 /** @addtogroup BSP
00046   * @{
00047   */ 
00048 
00049 /** @addtogroup STM32F429I_DISCOVERY
00050   * @{
00051   */
00052       
00053 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL 
00054   * @brief This file provides set of firmware functions to manage Leds and push-button
00055   *        available on STM32F429I-Discovery Kit from STMicroelectronics.
00056   * @{
00057   */ 
00058 
00059 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions
00060   * @{
00061   */ 
00062 /**
00063   * @}
00064   */ 
00065 
00066 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_Defines
00067   * @{
00068   */ 
00069   
00070   /**
00071   * @brief STM32F429I DISCO BSP Driver version number V2.1.0
00072   */
00073 #define __STM32F429I_DISCO_BSP_VERSION_MAIN   (0x02) /*!< [31:24] main version */
00074 #define __STM32F429I_DISCO_BSP_VERSION_SUB1   (0x01) /*!< [23:16] sub1 version */
00075 #define __STM32F429I_DISCO_BSP_VERSION_SUB2   (0x02) /*!< [15:8]  sub2 version */
00076 #define __STM32F429I_DISCO_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */ 
00077 #define __STM32F429I_DISCO_BSP_VERSION        ((__STM32F429I_DISCO_BSP_VERSION_MAIN << 24)\
00078                                              |(__STM32F429I_DISCO_BSP_VERSION_SUB1 << 16)\
00079                                              |(__STM32F429I_DISCO_BSP_VERSION_SUB2 << 8 )\
00080                                              |(__STM32F429I_DISCO_BSP_VERSION_RC)) 
00081 /**
00082   * @}
00083   */ 
00084 
00085 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_Macros
00086   * @{
00087   */ 
00088 /**
00089   * @}
00090   */ 
00091 
00092 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_Variables
00093   * @{
00094   */ 
00095 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED3_GPIO_PORT, 
00096                                  LED4_GPIO_PORT};
00097 
00098 const uint16_t GPIO_PIN[LEDn] = {LED3_PIN, 
00099                                  LED4_PIN};
00100 
00101 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT}; 
00102 const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN}; 
00103 const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
00104 
00105 uint32_t I2cxTimeout = I2Cx_TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */  
00106 uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */  
00107 
00108 static I2C_HandleTypeDef I2cHandle;
00109 static SPI_HandleTypeDef SpiHandle;
00110 static uint8_t Is_LCD_IO_Initialized = 0;
00111 
00112 /**
00113   * @}
00114   */ 
00115 
00116 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes
00117   * @{
00118   */ 
00119 /* I2Cx bus function */
00120 static void               I2Cx_Init(void);
00121 static void               I2Cx_ITConfig(void);
00122 static void               I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value);
00123 static void               I2Cx_WriteBuffer(uint8_t Addr, uint8_t Reg,  uint8_t *pBuffer, uint16_t Length);
00124 static uint8_t            I2Cx_ReadData(uint8_t Addr, uint8_t Reg);
00125 static uint8_t            I2Cx_ReadBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length);
00126 static void               I2Cx_Error(void);
00127 static void               I2Cx_MspInit(I2C_HandleTypeDef *hi2c);  
00128 #ifdef EE_M24LR64
00129 static HAL_StatusTypeDef  I2Cx_WriteBufferDMA(uint8_t Addr, uint16_t Reg,  uint8_t *pBuffer, uint16_t Length);
00130 static HAL_StatusTypeDef  I2Cx_ReadBufferDMA(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length);
00131 static HAL_StatusTypeDef  I2Cx_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
00132 #endif /* EE_M24LR64 */
00133 
00134 /* SPIx bus function */
00135 static void               SPIx_Init(void);
00136 static void               SPIx_Write(uint16_t Value);
00137 static uint32_t           SPIx_Read(uint8_t ReadSize);
00138 static uint8_t            SPIx_WriteRead(uint8_t Byte);
00139 static void               SPIx_Error(void);
00140 static void               SPIx_MspInit(SPI_HandleTypeDef *hspi);
00141 
00142 /* Link function for LCD peripheral */
00143 void                      LCD_IO_Init(void);
00144 void                      LCD_IO_WriteData(uint16_t RegValue);
00145 void                      LCD_IO_WriteReg(uint8_t Reg);
00146 uint32_t                  LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize);
00147 void                      LCD_Delay(uint32_t delay);
00148 
00149 /* IOExpander IO functions */
00150 void                      IOE_Init(void);
00151 void                      IOE_ITConfig(void);
00152 void                      IOE_Delay(uint32_t Delay);
00153 void                      IOE_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00154 uint8_t                   IOE_Read(uint8_t Addr, uint8_t Reg);
00155 uint16_t                  IOE_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length);
00156 void                      IOE_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length);
00157 
00158 /* Link function for GYRO peripheral */
00159 void                      GYRO_IO_Init(void);
00160 void                      GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
00161 void                      GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
00162 
00163 #ifdef EE_M24LR64
00164 /* Link function for I2C EEPROM peripheral */
00165 void                      EEPROM_IO_Init(void);
00166 HAL_StatusTypeDef         EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
00167 HAL_StatusTypeDef         EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
00168 HAL_StatusTypeDef         EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
00169 #endif /* EE_M24LR64 */
00170 
00171 /**
00172   * @}
00173   */ 
00174 
00175 /** @defgroup STM32F429I_DISCOVERY_LOW_LEVEL_Private_Functions
00176   * @{
00177   */ 
00178 
00179 /**
00180   * @brief  This method returns the STM32F429I DISCO BSP Driver revision
00181   * @param  None
00182   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
00183   */
00184 uint32_t BSP_GetVersion(void)
00185 {
00186   return __STM32F429I_DISCO_BSP_VERSION;
00187 }
00188 
00189 /**
00190   * @brief  Configures LED GPIO.
00191   * @param  Led: Specifies the Led to be configured. 
00192   *   This parameter can be one of following parameters:
00193   *     @arg LED3
00194   *     @arg LED4
00195   * @retval None
00196   */
00197 void BSP_LED_Init(Led_TypeDef Led)
00198 {
00199   GPIO_InitTypeDef  GPIO_InitStruct;
00200   
00201   /* Enable the GPIO_LED Clock */
00202   LEDx_GPIO_CLK_ENABLE(Led);
00203 
00204   /* Configure the GPIO_LED pin */
00205   GPIO_InitStruct.Pin = GPIO_PIN[Led];
00206   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00207   GPIO_InitStruct.Pull = GPIO_PULLUP;
00208   GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00209   
00210   HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
00211   
00212   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 
00213 }
00214 
00215 /**
00216   * @brief  Turns selected LED On.
00217   * @param  Led: Specifies the Led to be set on. 
00218   *   This parameter can be one of following parameters:
00219   *     @arg LED3
00220   *     @arg LED4 
00221   * @retval None
00222   */
00223 void BSP_LED_On(Led_TypeDef Led)
00224 {
00225   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET); 
00226 }
00227 
00228 /**
00229   * @brief  Turns selected LED Off.
00230   * @param  Led: Specifies the Led to be set off. 
00231   *   This parameter can be one of following parameters:
00232   *     @arg LED3
00233   *     @arg LED4
00234   * @retval None
00235   */
00236 void BSP_LED_Off(Led_TypeDef Led)
00237 {
00238   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 
00239 }
00240 
00241 /**
00242   * @brief  Toggles the selected LED.
00243   * @param  Led: Specifies the Led to be toggled. 
00244   *   This parameter can be one of following parameters:
00245   *     @arg LED3
00246   *     @arg LED4  
00247   * @retval None
00248   */
00249 void BSP_LED_Toggle(Led_TypeDef Led)
00250 {
00251   HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
00252 }
00253 
00254 /**
00255   * @brief  Configures Button GPIO and EXTI Line.
00256   * @param  Button: Specifies the Button to be configured.
00257   *   This parameter should be: BUTTON_KEY
00258   * @param  Button_Mode: Specifies Button mode.
00259   *   This parameter can be one of following parameters:   
00260   *     @arg BUTTON_MODE_GPIO: Button will be used as simple IO 
00261   *     @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
00262   *                            generation capability  
00263   * @retval None
00264   */
00265 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
00266 {
00267   GPIO_InitTypeDef GPIO_InitStruct;
00268   
00269   /* Enable the BUTTON Clock */
00270   BUTTONx_GPIO_CLK_ENABLE(Button);
00271   
00272   if (ButtonMode == BUTTON_MODE_GPIO)
00273   {
00274     /* Configure Button pin as input */
00275     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00276     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00277     GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00278     GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00279     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00280   }
00281   
00282   if (ButtonMode == BUTTON_MODE_EXTI)
00283   {
00284     /* Configure Button pin as input with External interrupt */
00285     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00286     GPIO_InitStruct.Pull = GPIO_NOPULL;
00287     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 
00288     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00289     
00290     /* Enable and set Button EXTI Interrupt to the lowest priority */
00291     HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
00292     HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00293   }
00294 }
00295 
00296 /**
00297   * @brief  Returns the selected Button state.
00298   * @param  Button: Specifies the Button to be checked.
00299   *   This parameter should be: BUTTON_KEY  
00300   * @retval The Button GPIO pin value.
00301   */
00302 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00303 {
00304   return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00305 }
00306 
00307 /*******************************************************************************
00308                             BUS OPERATIONS
00309 *******************************************************************************/
00310 
00311 /******************************* I2C Routines *********************************/
00312 
00313 /**
00314   * @brief  I2Cx MSP Initialization
00315   * @param  hi2c: I2C handle
00316   * @retval None
00317   */
00318 static void I2Cx_MspInit(I2C_HandleTypeDef *hi2c)
00319 {
00320   GPIO_InitTypeDef  GPIO_InitStruct;  
00321 #ifdef EE_M24LR64
00322   static DMA_HandleTypeDef hdma_tx;
00323   static DMA_HandleTypeDef hdma_rx;
00324   
00325   I2C_HandleTypeDef* pI2cHandle;
00326   pI2cHandle = &I2cHandle;
00327 #endif /* EE_M24LR64 */
00328 
00329   if (hi2c->Instance == DISCOVERY_I2Cx)
00330   {
00331     /* Configure the GPIOs ---------------------------------------------------*/ 
00332     /* Enable GPIO clock */
00333     DISCOVERY_I2Cx_SDA_GPIO_CLK_ENABLE();
00334     DISCOVERY_I2Cx_SCL_GPIO_CLK_ENABLE();
00335       
00336     /* Configure I2C Tx as alternate function  */
00337     GPIO_InitStruct.Pin       = DISCOVERY_I2Cx_SCL_PIN;
00338     GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
00339     GPIO_InitStruct.Pull      = GPIO_NOPULL;
00340     GPIO_InitStruct.Speed     = GPIO_SPEED_FAST;
00341     GPIO_InitStruct.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
00342     HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
00343       
00344     /* Configure I2C Rx as alternate function  */
00345     GPIO_InitStruct.Pin = DISCOVERY_I2Cx_SDA_PIN;
00346     HAL_GPIO_Init(DISCOVERY_I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
00347     
00348     
00349     /* Configure the Discovery I2Cx peripheral -------------------------------*/ 
00350     /* Enable I2C3 clock */
00351     DISCOVERY_I2Cx_CLOCK_ENABLE();
00352     
00353     /* Force the I2C Peripheral Clock Reset */  
00354     DISCOVERY_I2Cx_FORCE_RESET();
00355       
00356     /* Release the I2C Peripheral Clock Reset */  
00357     DISCOVERY_I2Cx_RELEASE_RESET(); 
00358     
00359     /* Enable and set Discovery I2Cx Interrupt to the highest priority */
00360     HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0x00, 0);
00361     HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
00362     
00363     /* Enable and set Discovery I2Cx Interrupt to the highest priority */
00364     HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0x00, 0);
00365     HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);  
00366 
00367 #ifdef EE_M24LR64
00368     /* I2C DMA TX and RX channels configuration */
00369     /* Enable the DMA clock */
00370     EEPROM_I2C_DMA_CLK_ENABLE();
00371     
00372     /* Configure the DMA stream for the EE I2C peripheral TX direction */
00373     /* Configure the DMA Stream */
00374     hdma_tx.Instance                  = EEPROM_I2C_DMA_STREAM_TX;
00375     /* Set the parameters to be configured */
00376     hdma_tx.Init.Channel              = EEPROM_I2C_DMA_CHANNEL;  
00377     hdma_tx.Init.Direction            = DMA_MEMORY_TO_PERIPH;
00378     hdma_tx.Init.PeriphInc            = DMA_PINC_DISABLE;
00379     hdma_tx.Init.MemInc               = DMA_MINC_ENABLE;
00380     hdma_tx.Init.PeriphDataAlignment  = DMA_PDATAALIGN_BYTE;
00381     hdma_tx.Init.MemDataAlignment     = DMA_MDATAALIGN_BYTE;
00382     hdma_tx.Init.Mode                 = DMA_NORMAL;
00383     hdma_tx.Init.Priority             = DMA_PRIORITY_VERY_HIGH;
00384     hdma_tx.Init.FIFOMode             = DMA_FIFOMODE_ENABLE;         
00385     hdma_tx.Init.FIFOThreshold        = DMA_FIFO_THRESHOLD_FULL;
00386     hdma_tx.Init.MemBurst             = DMA_MBURST_SINGLE;
00387     hdma_tx.Init.PeriphBurst          = DMA_PBURST_SINGLE; 
00388 
00389     /* Associate the initilalized hdma_tx handle to the the pI2cHandle handle */
00390     __HAL_LINKDMA(pI2cHandle, hdmatx, hdma_tx);
00391     
00392     /* Configure the DMA Stream */
00393     HAL_DMA_Init(&hdma_tx);
00394     
00395     /* Configure and enable I2C DMA TX Channel interrupt */
00396     HAL_NVIC_SetPriority((IRQn_Type)(EEPROM_I2C_DMA_TX_IRQn), EEPROM_I2C_DMA_PREPRIO, 0);
00397     HAL_NVIC_EnableIRQ((IRQn_Type)(EEPROM_I2C_DMA_TX_IRQn));
00398     
00399     /* Configure the DMA stream for the EE I2C peripheral TX direction */
00400     /* Configure the DMA Stream */
00401     hdma_rx.Instance                  = EEPROM_I2C_DMA_STREAM_RX;
00402     /* Set the parameters to be configured */
00403     hdma_rx.Init.Channel              = EEPROM_I2C_DMA_CHANNEL;  
00404     hdma_rx.Init.Direction            = DMA_PERIPH_TO_MEMORY;
00405     hdma_rx.Init.PeriphInc            = DMA_PINC_DISABLE;
00406     hdma_rx.Init.MemInc               = DMA_MINC_ENABLE;
00407     hdma_rx.Init.PeriphDataAlignment  = DMA_PDATAALIGN_BYTE;
00408     hdma_rx.Init.MemDataAlignment     = DMA_MDATAALIGN_BYTE;
00409     hdma_rx.Init.Mode                 = DMA_NORMAL;
00410     hdma_rx.Init.Priority             = DMA_PRIORITY_VERY_HIGH;
00411     hdma_rx.Init.FIFOMode             = DMA_FIFOMODE_ENABLE;         
00412     hdma_rx.Init.FIFOThreshold        = DMA_FIFO_THRESHOLD_FULL;
00413     hdma_rx.Init.MemBurst             = DMA_MBURST_SINGLE;
00414     hdma_rx.Init.PeriphBurst          = DMA_PBURST_SINGLE; 
00415 
00416     /* Associate the initilalized hdma_rx handle to the the pI2cHandle handle*/
00417     __HAL_LINKDMA(pI2cHandle, hdmarx, hdma_rx);
00418     
00419     /* Configure the DMA Stream */
00420     HAL_DMA_Init(&hdma_rx);
00421     
00422     /* Configure and enable I2C DMA RX Channel interrupt */
00423     HAL_NVIC_SetPriority((IRQn_Type)(EEPROM_I2C_DMA_RX_IRQn), EEPROM_I2C_DMA_PREPRIO, 0);
00424     HAL_NVIC_EnableIRQ((IRQn_Type)(EEPROM_I2C_DMA_RX_IRQn));
00425 #endif /* EE_M24LR64 */
00426   }
00427 }
00428 
00429 /**
00430   * @brief  I2Cx Bus initialization.
00431   * @param  None
00432   * @retval None
00433   */
00434 static void I2Cx_Init(void)
00435 {
00436   if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET)
00437   {
00438     I2cHandle.Instance              = DISCOVERY_I2Cx;
00439     I2cHandle.Init.ClockSpeed       = BSP_I2C_SPEED;
00440     I2cHandle.Init.DutyCycle        = I2C_DUTYCYCLE_2;
00441     I2cHandle.Init.OwnAddress1      = 0;
00442     I2cHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
00443     I2cHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
00444     I2cHandle.Init.OwnAddress2      = 0;
00445     I2cHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
00446     I2cHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;  
00447     
00448     /* Init the I2C */
00449     I2Cx_MspInit(&I2cHandle);
00450     HAL_I2C_Init(&I2cHandle);
00451   }
00452 }
00453 
00454 /**
00455   * @brief  Configures Interruption pin for I2C communication.
00456   * @param  None
00457   * @retval None
00458   */
00459 static void I2Cx_ITConfig(void)
00460 {
00461   GPIO_InitTypeDef  GPIO_InitStruct;
00462     
00463   /* Enable the GPIO EXTI Clock */
00464   STMPE811_INT_CLK_ENABLE();
00465   
00466   GPIO_InitStruct.Pin   = STMPE811_INT_PIN;
00467   GPIO_InitStruct.Pull  = GPIO_PULLUP;
00468   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
00469   GPIO_InitStruct.Mode  = GPIO_MODE_IT_FALLING;
00470   HAL_GPIO_Init(STMPE811_INT_GPIO_PORT, &GPIO_InitStruct);
00471     
00472   /* Enable and set GPIO EXTI Interrupt to the highest priority */
00473   HAL_NVIC_SetPriority((IRQn_Type)(STMPE811_INT_EXTI), 0x00, 0x00);
00474   HAL_NVIC_EnableIRQ((IRQn_Type)(STMPE811_INT_EXTI));
00475 }
00476 
00477 /**
00478   * @brief  Writes a value in a register of the device through BUS.
00479   * @param  Addr: Device address on BUS Bus.  
00480   * @param  Reg: The target register address to write
00481   * @param  Value: The target register value to be written 
00482   * @retval None
00483   */
00484 static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value)
00485   {
00486   HAL_StatusTypeDef status = HAL_OK;
00487   
00488   status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout); 
00489   
00490   /* Check the communication status */
00491   if(status != HAL_OK)
00492   {
00493     /* Re-Initialize the BUS */
00494     I2Cx_Error();
00495   }        
00496 }
00497 
00498 /**
00499   * @brief  Writes a value in a register of the device through BUS.
00500   * @param  Addr: Device address on BUS Bus.  
00501   * @param  Reg: The target register address to write
00502   * @param  pBuffer: The target register value to be written 
00503   * @param  Length: buffer size to be written
00504   * @retval None
00505   */
00506 static void I2Cx_WriteBuffer(uint8_t Addr, uint8_t Reg,  uint8_t *pBuffer, uint16_t Length)
00507   {
00508   HAL_StatusTypeDef status = HAL_OK;
00509   
00510   status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Length, I2cxTimeout); 
00511 
00512   /* Check the communication status */
00513   if(status != HAL_OK)
00514   {
00515     /* Re-Initialize the BUS */
00516     I2Cx_Error();
00517   }        
00518 }
00519 
00520 /**
00521   * @brief  Reads a register of the device through BUS.
00522   * @param  Addr: Device address on BUS Bus.  
00523   * @param  Reg: The target register address to write
00524   * @retval Data read at register address
00525   */
00526 static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg)
00527 {
00528   HAL_StatusTypeDef status = HAL_OK;
00529   uint8_t value = 0;
00530   
00531   status = HAL_I2C_Mem_Read(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &value, 1, I2cxTimeout);
00532  
00533   /* Check the communication status */
00534   if(status != HAL_OK)
00535   {
00536     /* Re-Initialize the BUS */
00537     I2Cx_Error();
00538   
00539   }
00540   return value;
00541 }
00542 
00543 /**
00544   * @brief  Reads multiple data on the BUS.
00545   * @param  Addr: I2C Address
00546   * @param  Reg: Reg Address 
00547   * @param  pBuffer: pointer to read data buffer
00548   * @param  Length: length of the data
00549   * @retval 0 if no problems to read multiple data
00550   */
00551 static uint8_t I2Cx_ReadBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
00552 {
00553   HAL_StatusTypeDef status = HAL_OK;
00554 
00555   status = HAL_I2C_Mem_Read(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Length, I2cxTimeout);
00556   
00557   /* Check the communication status */
00558   if(status == HAL_OK)
00559   {
00560     return 0;
00561   }
00562   else
00563   {
00564     /* Re-Initialize the BUS */
00565     I2Cx_Error();
00566 
00567     return 1;
00568   }
00569 }
00570 
00571 #ifdef EE_M24LR64
00572 /**
00573   * @brief  Writes a value in a register of the device through BUS in using DMA mode.
00574   * @param  Addr: Device address on BUS Bus.  
00575   * @param  Reg: The target register address to write
00576   * @param  pBuffer: The target register value to be written 
00577   * @param  Length: buffer size to be written
00578   * @retval HAL status
00579   */
00580 static HAL_StatusTypeDef I2Cx_WriteBufferDMA(uint8_t Addr, uint16_t Reg,  uint8_t *pBuffer, uint16_t Length)
00581   {
00582   HAL_StatusTypeDef status = HAL_OK;
00583   
00584   status = HAL_I2C_Mem_Write_DMA(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_16BIT, pBuffer, Length);
00585 
00586   /* Check the communication status */
00587   if(status != HAL_OK)
00588   {
00589     /* Re-Initialize the BUS */
00590     I2Cx_Error();
00591   }
00592 
00593   return status;
00594 }
00595 
00596 /**
00597   * @brief  Reads multiple data on the BUS in using DMA mode.
00598   * @param  Addr: I2C Address
00599   * @param  Reg: Reg Address 
00600   * @param  pBuffer: pointer to read data buffer
00601   * @param  Length: length of the data
00602   * @retval HAL status
00603   */
00604 static HAL_StatusTypeDef I2Cx_ReadBufferDMA(uint8_t Addr, uint16_t Reg, uint8_t *pBuffer, uint16_t Length)
00605 {
00606   HAL_StatusTypeDef status = HAL_OK;
00607 
00608   status = HAL_I2C_Mem_Read_DMA(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_16BIT, pBuffer, Length);
00609   
00610   /* Check the communication status */
00611   if(status != HAL_OK)
00612   {
00613     /* Re-Initialize the BUS */
00614     I2Cx_Error();
00615   }
00616   
00617   return status;
00618 }
00619 
00620 /**
00621 * @brief  Checks if target device is ready for communication. 
00622 * @note   This function is used with Memory devices
00623 * @param  DevAddress: Target device address
00624 * @param  Trials: Number of trials
00625 * @retval HAL status
00626 */
00627 static HAL_StatusTypeDef I2Cx_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
00628 { 
00629   return (HAL_I2C_IsDeviceReady(&I2cHandle, DevAddress, Trials, I2cxTimeout));
00630 }
00631 #endif /* EE_M24LR64 */
00632 
00633 /**
00634   * @brief  I2Cx error treatment function
00635   * @param  None
00636   * @retval None
00637   */
00638 static void I2Cx_Error(void)
00639 {
00640   /* De-initialize the SPI communication BUS */
00641   HAL_I2C_DeInit(&I2cHandle);
00642   
00643   /* Re-Initialize the SPI communication BUS */
00644   I2Cx_Init();
00645 }
00646 
00647 /******************************* SPI Routines *********************************/
00648 
00649 /**
00650   * @brief  SPIx Bus initialization
00651   * @param  None
00652   * @retval None
00653   */
00654 static void SPIx_Init(void)
00655 {
00656   if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET)
00657   {
00658     /* SPI configuration -----------------------------------------------------*/
00659     SpiHandle.Instance = DISCOVERY_SPIx;
00660     /* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz) 
00661        to verify these constraints:
00662        - ILI9341 LCD SPI interface max baudrate is 10MHz for write and 6.66MHz for read
00663        - l3gd20 SPI interface max baudrate is 10MHz for write/read
00664        - PCLK2 frequency is set to 90 MHz 
00665     */  
00666     SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
00667 
00668     /* On STM32F429I-Discovery, LCD ID cannot be read then keep a common configuration */
00669     /* for LCD and GYRO (SPI_DIRECTION_2LINES) */
00670     /* Note: To read a register a LCD, SPI_DIRECTION_1LINE should be set */
00671     SpiHandle.Init.Direction      = SPI_DIRECTION_2LINES;
00672     SpiHandle.Init.CLKPhase       = SPI_PHASE_1EDGE;
00673     SpiHandle.Init.CLKPolarity    = SPI_POLARITY_LOW;
00674     SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
00675     SpiHandle.Init.CRCPolynomial  = 7;
00676     SpiHandle.Init.DataSize       = SPI_DATASIZE_8BIT;
00677     SpiHandle.Init.FirstBit       = SPI_FIRSTBIT_MSB;
00678     SpiHandle.Init.NSS            = SPI_NSS_SOFT;
00679     SpiHandle.Init.TIMode         = SPI_TIMODE_DISABLED;
00680     SpiHandle.Init.Mode           = SPI_MODE_MASTER;
00681   
00682     SPIx_MspInit(&SpiHandle);
00683     HAL_SPI_Init(&SpiHandle);
00684   } 
00685 }
00686 
00687 /**
00688   * @brief  Reads 4 bytes from device.
00689   * @param  ReadSize: Number of bytes to read (max 4 bytes)
00690   * @retval Value read on the SPI
00691   */
00692 static uint32_t SPIx_Read(uint8_t ReadSize)
00693 {
00694   HAL_StatusTypeDef status = HAL_OK;
00695   uint32_t readvalue;
00696   
00697   status = HAL_SPI_Receive(&SpiHandle, (uint8_t*) &readvalue, ReadSize, SpixTimeout);
00698   
00699   /* Check the communication status */
00700   if(status != HAL_OK)
00701   {
00702     /* Re-Initialize the BUS */
00703     SPIx_Error();
00704   }
00705   
00706   return readvalue;
00707 }
00708 
00709 /**
00710   * @brief  Writes a byte to device.
00711   * @param  Value: value to be written
00712   * @retval None
00713   */
00714 static void SPIx_Write(uint16_t Value)
00715 {
00716   HAL_StatusTypeDef status = HAL_OK;
00717   
00718   status = HAL_SPI_Transmit(&SpiHandle, (uint8_t*) &Value, 1, SpixTimeout);
00719   
00720   /* Check the communication status */
00721   if(status != HAL_OK)
00722   {
00723     /* Re-Initialize the BUS */
00724     SPIx_Error();
00725   }
00726 }
00727 
00728 /**
00729   * @brief  Sends a Byte through the SPI interface and return the Byte received 
00730   *         from the SPI bus.
00731   * @param  Byte: Byte send.
00732   * @retval The received byte value
00733   */
00734 static uint8_t SPIx_WriteRead(uint8_t Byte)
00735 {
00736   uint8_t receivedbyte = 0;
00737   
00738   /* Send a Byte through the SPI peripheral */
00739   /* Read byte from the SPI bus */
00740   if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK)
00741   {
00742     SPIx_Error();
00743   }
00744   
00745   return receivedbyte;
00746 }
00747 
00748 /**
00749   * @brief  SPIx error treatment function.
00750   * @param  None
00751   * @retval None
00752   */
00753 static void SPIx_Error(void)
00754 {
00755   /* De-initialize the SPI communication BUS */
00756   HAL_SPI_DeInit(&SpiHandle);
00757   
00758   /* Re- Initialize the SPI communication BUS */
00759   SPIx_Init();
00760 }
00761 
00762 /**
00763   * @brief  SPI MSP Init.
00764   * @param  hspi: SPI handle
00765   * @retval None
00766   */
00767 static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
00768 {
00769   GPIO_InitTypeDef   GPIO_InitStructure;
00770 
00771   /* Enable SPIx clock */
00772   DISCOVERY_SPIx_CLK_ENABLE();
00773 
00774   /* Enable DISCOVERY_SPI GPIO clock */
00775   DISCOVERY_SPIx_GPIO_CLK_ENABLE();
00776 
00777   /* configure SPI SCK, MOSI and MISO */    
00778   GPIO_InitStructure.Pin    = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN);
00779   GPIO_InitStructure.Mode   = GPIO_MODE_AF_PP;
00780   GPIO_InitStructure.Pull   = GPIO_PULLDOWN;
00781   GPIO_InitStructure.Speed  = GPIO_SPEED_MEDIUM;
00782   GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF;
00783   HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure);      
00784 }
00785 
00786 /********************************* LINK LCD ***********************************/
00787 
00788 /**
00789   * @brief  Configures the LCD_SPI interface.
00790   * @param  None
00791   * @retval None
00792   */
00793 void LCD_IO_Init(void)
00794 {
00795   GPIO_InitTypeDef GPIO_InitStructure;
00796   
00797   if(Is_LCD_IO_Initialized == 0)
00798   {
00799     Is_LCD_IO_Initialized = 1; 
00800     
00801     /* Configure NCS in Output Push-Pull mode */
00802     LCD_WRX_GPIO_CLK_ENABLE();
00803     GPIO_InitStructure.Pin     = LCD_WRX_PIN;
00804     GPIO_InitStructure.Mode    = GPIO_MODE_OUTPUT_PP;
00805     GPIO_InitStructure.Pull    = GPIO_NOPULL;
00806     GPIO_InitStructure.Speed   = GPIO_SPEED_FAST;
00807     HAL_GPIO_Init(LCD_WRX_GPIO_PORT, &GPIO_InitStructure);
00808     
00809     LCD_RDX_GPIO_CLK_ENABLE();
00810     GPIO_InitStructure.Pin     = LCD_RDX_PIN;
00811     GPIO_InitStructure.Mode    = GPIO_MODE_OUTPUT_PP;
00812     GPIO_InitStructure.Pull    = GPIO_NOPULL;
00813     GPIO_InitStructure.Speed   = GPIO_SPEED_FAST;
00814     HAL_GPIO_Init(LCD_RDX_GPIO_PORT, &GPIO_InitStructure);
00815     
00816     /* Configure the LCD Control pins ----------------------------------------*/
00817     LCD_NCS_GPIO_CLK_ENABLE();
00818     
00819     /* Configure NCS in Output Push-Pull mode */
00820     GPIO_InitStructure.Pin     = LCD_NCS_PIN;
00821     GPIO_InitStructure.Mode    = GPIO_MODE_OUTPUT_PP;
00822     GPIO_InitStructure.Pull    = GPIO_NOPULL;
00823     GPIO_InitStructure.Speed   = GPIO_SPEED_FAST;
00824     HAL_GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
00825     
00826     /* Set or Reset the control line */
00827     LCD_CS_LOW();
00828     LCD_CS_HIGH();
00829     
00830     SPIx_Init();
00831   }
00832 }
00833 
00834 /**
00835   * @brief  Writes register value.
00836   * @param  None
00837   * @retval None
00838   */
00839 void LCD_IO_WriteData(uint16_t RegValue) 
00840 {
00841   /* Set WRX to send data */
00842   LCD_WRX_HIGH();
00843   
00844   /* Reset LCD control line(/CS) and Send data */  
00845   LCD_CS_LOW();
00846   SPIx_Write(RegValue);
00847   
00848   /* Deselect: Chip Select high */
00849   LCD_CS_HIGH();
00850 }
00851 
00852 /**
00853   * @brief  Writes register address.
00854   * @param  None
00855   * @retval None
00856   */
00857 void LCD_IO_WriteReg(uint8_t Reg) 
00858 {
00859   /* Reset WRX to send command */
00860   LCD_WRX_LOW();
00861   
00862   /* Reset LCD control line(/CS) and Send command */
00863   LCD_CS_LOW();
00864   SPIx_Write(Reg);
00865   
00866   /* Deselect: Chip Select high */
00867   LCD_CS_HIGH();
00868 }
00869 
00870 /**
00871   * @brief  Reads register value.
00872   * @param  RegValue Address of the register to read
00873   * @param  ReadSize Number of bytes to read
00874   * @retval Content of the register value
00875   */
00876 uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize) 
00877 {
00878   uint32_t readvalue = 0;
00879 
00880   /* Select: Chip Select low */
00881   LCD_CS_LOW();
00882 
00883   /* Reset WRX to send command */
00884   LCD_WRX_LOW();
00885   
00886   SPIx_Write(RegValue);
00887   
00888   readvalue = SPIx_Read(ReadSize);
00889 
00890   /* Set WRX to send data */
00891   LCD_WRX_HIGH();
00892 
00893   /* Deselect: Chip Select high */
00894   LCD_CS_HIGH();
00895   
00896   return readvalue;
00897 }
00898 
00899 /**
00900   * @brief  Wait for loop in ms.
00901   * @param  Delay in ms.
00902   * @retval None
00903   */
00904 void LCD_Delay(uint32_t Delay)
00905 {
00906   //HAL_Delay(Delay);
00907   wait_ms(Delay);
00908 }
00909 
00910 /*******************************************************************************
00911                             LINK OPERATIONS
00912 *******************************************************************************/
00913 
00914 /********************************* LINK IOE ***********************************/
00915 
00916 /**
00917   * @brief  IOE Low Level Initialization.
00918   * @param  None
00919   * @retval None
00920   */
00921 void IOE_Init(void) 
00922 {
00923   I2Cx_Init();
00924 }
00925 
00926 /**
00927   * @brief  IOE Low Level Interrupt configuration.
00928   * @param  None
00929   * @retval None
00930   */
00931 void IOE_ITConfig(void)
00932 {
00933   I2Cx_ITConfig();
00934 }
00935 
00936 /**
00937   * @brief  IOE Writes single data operation.
00938   * @param  Addr: I2C Address
00939   * @param  Reg: Reg Address 
00940   * @param  Value: Data to be written
00941   * @retval None
00942   */
00943 void IOE_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
00944 {
00945   I2Cx_WriteData(Addr, Reg, Value);
00946 }
00947 
00948 /**
00949   * @brief  IOE Reads single data.
00950   * @param  Addr: I2C Address
00951   * @param  Reg: Reg Address 
00952   * @retval The read data
00953   */
00954 uint8_t IOE_Read(uint8_t Addr, uint8_t Reg)
00955 {
00956   return I2Cx_ReadData(Addr, Reg);
00957 }
00958 
00959 /**
00960   * @brief  IOE Writes multiple data.
00961   * @param  Addr: I2C Address
00962   * @param  Reg: Reg Address 
00963   * @param  pBuffer: pointer to data buffer
00964   * @param  Length: length of the data
00965   * @retval None
00966   */
00967 void IOE_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
00968 {
00969   I2Cx_WriteBuffer(Addr, Reg, pBuffer, Length);
00970 }
00971 
00972 /**
00973   * @brief  IOE Reads multiple data.
00974   * @param  Addr: I2C Address
00975   * @param  Reg: Reg Address 
00976   * @param  pBuffer: pointer to data buffer
00977   * @param  Length: length of the data
00978   * @retval 0 if no problems to read multiple data
00979   */
00980 uint16_t IOE_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
00981 {
00982  return I2Cx_ReadBuffer(Addr, Reg, pBuffer, Length);
00983 }
00984 
00985 /**
00986   * @brief  IOE Delay.
00987   * @param  Delay in ms
00988   * @retval None
00989   */
00990 void IOE_Delay(uint32_t Delay)
00991 {
00992   //HAL_Delay(Delay);
00993   wait_ms(Delay);
00994 }
00995 
00996 /********************************* LINK GYROSCOPE *****************************/
00997 
00998 /**
00999   * @brief  Configures the Gyroscope SPI interface.
01000   * @param  None
01001   * @retval None
01002   */
01003 void GYRO_IO_Init(void)
01004 {
01005   GPIO_InitTypeDef GPIO_InitStructure;
01006   
01007   /* Configure the Gyroscope Control pins ------------------------------------*/
01008   /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */  
01009   GYRO_CS_GPIO_CLK_ENABLE();  
01010   GPIO_InitStructure.Pin = GYRO_CS_PIN;
01011   GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
01012   GPIO_InitStructure.Pull  = GPIO_NOPULL;
01013   GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
01014   HAL_GPIO_Init(GYRO_CS_GPIO_PORT, &GPIO_InitStructure);
01015   
01016   /* Deselect: Chip Select high */
01017   GYRO_CS_HIGH();
01018   
01019   /* Enable INT1, INT2 GPIO clock and Configure GPIO PINs to detect Interrupts */
01020   GYRO_INT_GPIO_CLK_ENABLE();
01021   GPIO_InitStructure.Pin = GYRO_INT1_PIN | GYRO_INT2_PIN;
01022   GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
01023   GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
01024   GPIO_InitStructure.Pull= GPIO_NOPULL;
01025   HAL_GPIO_Init(GYRO_INT_GPIO_PORT, &GPIO_InitStructure);
01026 
01027   SPIx_Init();
01028 }
01029 
01030 /**
01031   * @brief  Writes one byte to the Gyroscope.
01032   * @param  pBuffer: Pointer to the buffer containing the data to be written to the Gyroscope.
01033   * @param  WriteAdd: Gyroscope's internal address to write to.
01034   * @param  NumByteToWrite: Number of bytes to write.
01035   * @retval None
01036   */
01037 void GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite)
01038 {
01039   /* Configure the MS bit: 
01040        - When 0, the address will remain unchanged in multiple read/write commands.
01041        - When 1, the address will be auto incremented in multiple read/write commands.
01042   */
01043   if(NumByteToWrite > 0x01)
01044   {
01045     WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD;
01046   }
01047   /* Set chip select Low at the start of the transmission */
01048   GYRO_CS_LOW();
01049   
01050   /* Send the Address of the indexed register */
01051   SPIx_WriteRead(WriteAddr);
01052   
01053   /* Send the data that will be written into the device (MSB First) */
01054   while(NumByteToWrite >= 0x01)
01055   {
01056     SPIx_WriteRead(*pBuffer);
01057     NumByteToWrite--;
01058     pBuffer++;
01059   }
01060   
01061   /* Set chip select High at the end of the transmission */ 
01062   GYRO_CS_HIGH();
01063 }
01064 
01065 /**
01066   * @brief  Reads a block of data from the Gyroscope.
01067   * @param  pBuffer: Pointer to the buffer that receives the data read from the Gyroscope.
01068   * @param  ReadAddr: Gyroscope's internal address to read from.
01069   * @param  NumByteToRead: Number of bytes to read from the Gyroscope.
01070   * @retval None
01071   */
01072 void GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
01073 {  
01074   if(NumByteToRead > 0x01)
01075   {
01076     ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD);
01077   }
01078   else
01079   {
01080     ReadAddr |= (uint8_t)READWRITE_CMD;
01081   }
01082   /* Set chip select Low at the start of the transmission */
01083   GYRO_CS_LOW();
01084   
01085   /* Send the Address of the indexed register */
01086   SPIx_WriteRead(ReadAddr);
01087   
01088   /* Receive the data that will be read from the device (MSB First) */
01089   while(NumByteToRead > 0x00)
01090   {
01091     /* Send dummy byte (0x00) to generate the SPI clock to Gyroscope (Slave device) */
01092     *pBuffer = SPIx_WriteRead(DUMMY_BYTE);
01093     NumByteToRead--;
01094     pBuffer++;
01095   }
01096   
01097   /* Set chip select High at the end of the transmission */ 
01098   GYRO_CS_HIGH();
01099 }  
01100 
01101 
01102 #ifdef EE_M24LR64
01103 
01104 /******************************** LINK I2C EEPROM *****************************/
01105 
01106 /**
01107   * @brief  Initializes peripherals used by the I2C EEPROM driver.
01108   * @param  None
01109   * @retval None
01110   */
01111 void EEPROM_IO_Init(void)
01112 {
01113   I2Cx_Init();
01114 }
01115 
01116 /**
01117   * @brief  Writes data to I2C EEPROM driver in using DMA channel.
01118   * @param  DevAddress: Target device address
01119   * @param  MemAddress: Internal memory address
01120   * @param  pBuffer: Pointer to data buffer
01121   * @param  BufferSize: Amount of data to be sent
01122   * @retval HAL status
01123   */
01124 HAL_StatusTypeDef EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
01125 {
01126   return (I2Cx_WriteBufferDMA(DevAddress, MemAddress,  pBuffer, BufferSize));
01127 }
01128 
01129 /**
01130   * @brief  Reads data from I2C EEPROM driver in using DMA channel.
01131   * @param  DevAddress: Target device address
01132   * @param  MemAddress: Internal memory address
01133   * @param  pBuffer: Pointer to data buffer
01134   * @param  BufferSize: Amount of data to be read
01135   * @retval HAL status
01136   */
01137 HAL_StatusTypeDef EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
01138 {
01139   return (I2Cx_ReadBufferDMA(DevAddress, MemAddress, pBuffer, BufferSize));
01140 }
01141 
01142 /**
01143 * @brief  Checks if target device is ready for communication. 
01144 * @note   This function is used with Memory devices
01145 * @param  DevAddress: Target device address
01146 * @param  Trials: Number of trials
01147 * @retval HAL status
01148 */
01149 HAL_StatusTypeDef EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
01150 { 
01151   return (I2Cx_IsDeviceReady(DevAddress, Trials));
01152 }
01153 #endif /* EE_M24LR64 */
01154 
01155 /**
01156   * @}
01157   */ 
01158 
01159 /**
01160   * @}
01161   */ 
01162 
01163 /**
01164   * @}
01165   */   
01166 
01167 /**
01168   * @}
01169   */ 
01170       
01171 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/