This fork is necessary for the Starship code.

Fork of BSP_DISCO_F429ZI by ST

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