STM32303C_EVAL BSP User Manual: stm32303c_eval.c Source File

STM32303C EVAL BSP Drivers

stm32303c_eval.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32303c_eval.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of firmware functions to manage LEDs, 
00006   *          push-button and COM port available on STM32303C-EVAL evaluation
00007   *          board (MB1019) RevC from STMicroelectronics.
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00012   *
00013   * Redistribution and use in source and binary forms, with or without modification,
00014   * are permitted provided that the following conditions are met:
00015   *   1. Redistributions 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 its contributors
00021   *      may be used to endorse or promote products derived from this software
00022   *      without specific prior written permission.
00023   *
00024   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034   *
00035   ******************************************************************************
00036   */
00037   
00038 /* Includes ------------------------------------------------------------------*/
00039 #include "stm32303c_eval.h"
00040 
00041 /** @addtogroup BSP
00042   * @{
00043   */ 
00044 
00045 /** @addtogroup STM32303C_EVAL
00046  * @brief This file provides firmware functions to manage Leds, push-buttons,  
00047  *        COM ports, SD card and temperature sensor available on 
00048  *        STM32303C-EVAL evaluation board from STMicroelectronics.
00049   * @{
00050   */   
00051     
00052 /** @addtogroup STM32303C_EVAL_Common
00053   * @{
00054   */ 
00055 
00056 /** @addtogroup STM32303C_EVAL_Private_Constants
00057   * @{
00058   */ 
00059 /* LINK LCD */
00060 #define START_BYTE         0x70
00061 #define SET_INDEX          0x00
00062 #define READ_STATUS        0x01
00063 #define LCD_WRITE_REG      0x02
00064 #define LCD_READ_REG       0x03
00065 
00066 /* LINK SD Card */
00067 #define SD_DUMMY_BYTE            0xFF
00068 #define SD_NO_RESPONSE_EXPECTED  0x80
00069 
00070 /* LINK EEPROM SPI */
00071 #define EEPROM_CMD_WREN         0x06  /*!< Write enable instruction */
00072 #define EEPROM_CMD_WRDI         0x04  /*!< Write disable instruction */
00073 #define EEPROM_CMD_RDSR         0x05  /*!< Read Status Register instruction  */
00074 #define EEPROM_CMD_WRSR         0x01  /*!< Write Status Register instruction */
00075 #define EEPROM_CMD_WRITE        0x02  /*!< Write to Memory instruction */
00076 #define EEPROM_CMD_READ         0x03  /*!< Read from Memory instruction */
00077 
00078 #define EEPROM_WIP_FLAG         0x01  /*!< Write In Progress (WIP) flag */
00079 
00080 /**
00081  * @brief STM32303C EVAL BSP Driver version number V2.1.2
00082    */
00083 #define __STM32303C_EVAL_BSP_VERSION_MAIN       (0x02) /*!< [31:24] main version */
00084 #define __STM32303C_EVAL_BSP_VERSION_SUB1       (0x01) /*!< [23:16] sub1 version */
00085 #define __STM32303C_EVAL_BSP_VERSION_SUB2       (0x02) /*!< [15:8]  sub2 version */
00086 #define __STM32303C_EVAL_BSP_VERSION_RC         (0x00) /*!< [7:0]  release candidate */
00087 #define __STM32303C_EVAL_BSP_VERSION            ((__STM32303C_EVAL_BSP_VERSION_MAIN << 24)\
00088                                                 |(__STM32303C_EVAL_BSP_VERSION_SUB1 << 16)\
00089                                                 |(__STM32303C_EVAL_BSP_VERSION_SUB2 << 8 )\
00090                                                 |(__STM32303C_EVAL_BSP_VERSION_RC))
00091 /**
00092   * @}
00093   */ 
00094 
00095 /** @addtogroup STM32303C_EVAL_Private_Variables
00096   * @{
00097   */ 
00098 /**
00099  * @brief LED variables
00100  */
00101 GPIO_TypeDef* LED_PORT[LEDn] = {LED1_GPIO_PORT,
00102                                 LED2_GPIO_PORT,
00103                                 LED3_GPIO_PORT,
00104                                 LED4_GPIO_PORT};
00105 
00106 const uint16_t LED_PIN[LEDn] = {LED1_PIN,
00107                                 LED2_PIN,
00108                                 LED3_PIN,
00109                                 LED4_PIN};
00110 
00111 /**
00112  * @brief BUTTON variables
00113  */
00114 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT,
00115                                       SEL_JOY_GPIO_PORT,
00116                                       LEFT_JOY_GPIO_PORT,
00117                                       RIGHT_JOY_GPIO_PORT,
00118                                       DOWN_JOY_GPIO_PORT,
00119                                       UP_JOY_GPIO_PORT}; 
00120 
00121 const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN,
00122                                       SEL_JOY_PIN,
00123                                       LEFT_JOY_PIN,
00124                                       RIGHT_JOY_PIN,
00125                                       DOWN_JOY_PIN,
00126                                       UP_JOY_PIN};
00127 
00128 const uint16_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn,
00129                                        SEL_JOY_EXTI_IRQn,
00130                                        LEFT_JOY_EXTI_IRQn,
00131                                        RIGHT_JOY_EXTI_IRQn,
00132                                        DOWN_JOY_EXTI_IRQn,
00133                                        UP_JOY_EXTI_IRQn};
00134 
00135 /**
00136  * @brief JOYSTICK variables
00137  */
00138 GPIO_TypeDef* JOY_PORT[JOYn] = {SEL_JOY_GPIO_PORT,
00139                                 LEFT_JOY_GPIO_PORT, 
00140                                 RIGHT_JOY_GPIO_PORT, 
00141                                 DOWN_JOY_GPIO_PORT, 
00142                                 UP_JOY_GPIO_PORT}; 
00143 
00144 const uint16_t JOY_PIN[JOYn] = {SEL_JOY_PIN, 
00145                                 LEFT_JOY_PIN, 
00146                                 RIGHT_JOY_PIN, 
00147                                 DOWN_JOY_PIN, 
00148                                 UP_JOY_PIN}; 
00149 
00150 const uint8_t JOY_IRQn[JOYn] = {SEL_JOY_EXTI_IRQn,
00151                                 LEFT_JOY_EXTI_IRQn, 
00152                                 RIGHT_JOY_EXTI_IRQn, 
00153                                 DOWN_JOY_EXTI_IRQn, 
00154                                 UP_JOY_EXTI_IRQn};
00155 
00156 /**
00157  * @brief COM variables
00158  */
00159 #if defined(HAL_UART_MODULE_ENABLED)
00160 USART_TypeDef* COM_USART[COMn]   = {EVAL_COM1}; 
00161 
00162 GPIO_TypeDef*  COM_TX_PORT[COMn] = {EVAL_COM1_TX_GPIO_PORT};
00163  
00164 GPIO_TypeDef*  COM_RX_PORT[COMn] = {EVAL_COM1_RX_GPIO_PORT};
00165 
00166 const uint16_t COM_TX_PIN[COMn]  = {EVAL_COM1_TX_PIN};
00167 
00168 const uint16_t COM_RX_PIN[COMn]  = {EVAL_COM1_RX_PIN};
00169  
00170 const uint16_t COM_TX_AF[COMn]   = {EVAL_COM1_TX_AF};
00171 
00172 const uint16_t COM_RX_AF[COMn]   = {EVAL_COM1_RX_AF};
00173 #endif /* HAL_UART_MODULE_ENABLED) */
00174 
00175 /**
00176  * @brief BUS variables
00177  */
00178 #if defined(HAL_I2C_MODULE_ENABLED)
00179 uint32_t I2cxTimeout = EVAL_I2Cx_TIMEOUT_MAX;   /*<! Value of Timeout when I2C communication fails */
00180 I2C_HandleTypeDef heval_I2c;
00181 #endif /* HAL_I2C_MODULE_ENABLED */
00182 
00183 #if defined(HAL_SPI_MODULE_ENABLED)
00184 uint32_t SpixTimeout = EVAL_SPIx_TIMEOUT_MAX;        /*<! Value of Timeout when SPI communication fails */
00185 static SPI_HandleTypeDef heval_Spi;
00186 #endif /* HAL_SPI_MODULE_ENABLED */
00187 
00188 /**
00189   * @}
00190   */ 
00191 
00192 /** @defgroup STM32303C_EVAL_BUS Bus Operation functions
00193   * @{
00194   */
00195 #if defined(HAL_I2C_MODULE_ENABLED)
00196 /* I2Cx bus function */
00197 static void               I2Cx_Init(void);
00198 static void               I2Cx_WriteData(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t Value);
00199 static HAL_StatusTypeDef  I2Cx_WriteBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
00200 static uint8_t            I2Cx_ReadData(uint16_t Addr, uint8_t Reg, uint16_t RegSize);
00201 static HAL_StatusTypeDef  I2Cx_ReadBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
00202 static HAL_StatusTypeDef  I2Cx_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
00203 static void               I2Cx_Error (void);
00204 static void               I2Cx_MspInit(I2C_HandleTypeDef *hi2c);  
00205 
00206 /* Link function for EEPROM peripheral over I2C */
00207 void                      EEPROM_I2C_IO_Init(void);
00208 HAL_StatusTypeDef         EEPROM_I2C_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
00209 HAL_StatusTypeDef         EEPROM_I2C_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
00210 HAL_StatusTypeDef         EEPROM_I2C_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
00211 
00212 /* Link function for Temperature Sensor peripheral */
00213 void                      TSENSOR_IO_Init(void);
00214 void                      TSENSOR_IO_Write(uint16_t DevAddress, uint8_t* pBuffer, uint8_t WriteAddr, uint16_t Length);
00215 void                      TSENSOR_IO_Read(uint16_t DevAddress, uint8_t* pBuffer, uint8_t ReadAddr, uint16_t Length);
00216 uint16_t                  TSENSOR_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
00217 
00218 /* Link function for Audio Codec peripheral */
00219 void                      AUDIO_IO_Init(void);
00220 void                      AUDIO_IO_DeInit(void);
00221 void                      AUDIO_IO_Write(uint16_t DevAddress, uint8_t Reg, uint8_t Value);
00222 uint8_t                   AUDIO_IO_Read(uint16_t DevAddress, uint8_t Reg);
00223 void                      AUDIO_IO_Delay(uint32_t delay);
00224 #endif /* HAL_I2C_MODULE_ENABLED */
00225 
00226 #if defined(HAL_SPI_MODULE_ENABLED)
00227 /* SPIx bus function */
00228 static void               SPIx_Init(void);
00229 static void               SPIx_Write(uint8_t Value);
00230 static uint32_t           SPIx_Read(void);
00231 static void               SPIx_Error (void);
00232 static void               SPIx_MspInit(SPI_HandleTypeDef *hspi);
00233     
00234 /* Link function for LCD peripheral over SPI */
00235 void                      LCD_IO_Init(void);
00236 void                      LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
00237 void                      LCD_IO_WriteReg(uint8_t Reg);
00238 uint16_t                  LCD_IO_ReadData(uint16_t Reg);
00239 void                      LCD_Delay (uint32_t delay);
00240 
00241 /* Link function for EEPROM peripheral over SPI */
00242 void                      EEPROM_SPI_IO_Init(void);
00243 void                      EEPROM_SPI_IO_WriteByte(uint8_t Data);
00244 uint8_t                   EEPROM_SPI_IO_ReadByte(void);
00245 HAL_StatusTypeDef         EEPROM_SPI_IO_WriteData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
00246 HAL_StatusTypeDef         EEPROM_SPI_IO_ReadData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
00247 HAL_StatusTypeDef         EEPROM_SPI_IO_WaitEepromStandbyState(void);
00248 
00249 /* Link functions for SD Card peripheral over SPI */
00250 void                      SD_IO_Init(void);
00251 HAL_StatusTypeDef         SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response);
00252 HAL_StatusTypeDef         SD_IO_WaitResponse(uint8_t Response);
00253 void                      SD_IO_WriteDummy(void);
00254 void                      SD_IO_WriteByte(uint8_t Data);
00255 uint8_t                   SD_IO_ReadByte(void);
00256 #endif /* HAL_SPI_MODULE_ENABLED */
00257 
00258 /**
00259   * @}
00260   */ 
00261 
00262 /** @addtogroup STM32303C_EVAL_Exported_Functions
00263   * @{
00264   */ 
00265 
00266 /**
00267   * @brief  This method returns the STM32303C EVAL BSP Driver revision
00268   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
00269   */
00270 uint32_t BSP_GetVersion(void)
00271 {
00272   return __STM32303C_EVAL_BSP_VERSION;
00273 }
00274 
00275 /**
00276   * @brief  Configures LED GPIO.
00277   * @param  Led Specifies the Led to be configured. 
00278   *   This parameter can be one of following parameters:
00279   *     @arg LED1
00280   *     @arg LED2
00281   *     @arg LED3
00282   *     @arg LED4
00283   * @retval None
00284   */
00285 void BSP_LED_Init(Led_TypeDef Led)
00286 {
00287   GPIO_InitTypeDef  GPIO_InitStruct;
00288   
00289   /* Enable the GPIO_LED clock */
00290   LEDx_GPIO_CLK_ENABLE(Led);
00291 
00292   /* Configure the GPIO_LED pin */
00293   GPIO_InitStruct.Pin = LED_PIN[Led];
00294   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00295   GPIO_InitStruct.Pull = GPIO_PULLUP;
00296   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
00297   
00298   HAL_GPIO_Init(LED_PORT[Led], &GPIO_InitStruct);
00299   
00300   HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET); 
00301 }
00302 
00303 /**
00304   * @brief  Turns selected LED On.
00305   * @param  Led Specifies the Led to be set on. 
00306   *   This parameter can be one of following parameters:
00307   *     @arg LED1
00308   *     @arg LED2
00309   *     @arg LED3
00310   *     @arg LED4  
00311   * @retval None
00312   */
00313 void BSP_LED_On(Led_TypeDef Led)
00314 {
00315   HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
00316 }
00317 
00318 /**
00319   * @brief  Turns selected LED Off.
00320   * @param  Led Specifies the Led to be set off. 
00321   *   This parameter can be one of following parameters:
00322   *     @arg LED1
00323   *     @arg LED2
00324   *     @arg LED3
00325   *     @arg LED4 
00326   * @retval None
00327   */
00328 void BSP_LED_Off(Led_TypeDef Led)
00329 {
00330   HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
00331 }
00332 
00333 /**
00334   * @brief  Toggles the selected LED.
00335   * @param  Led Specifies the Led to be toggled. 
00336   *   This parameter can be one of following parameters:
00337   *     @arg LED1
00338   *     @arg LED2
00339   *     @arg LED3
00340   *     @arg LED4  
00341   * @retval None
00342   */
00343 void BSP_LED_Toggle(Led_TypeDef Led)
00344 {
00345   HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]);
00346 }
00347 
00348 /**
00349   * @brief  Configures push button GPIO and EXTI Line.
00350   * @param  Button Button to be configured.
00351   *   This parameter can be one of the following values: 
00352   *     @arg BUTTON_KEY   : Key Push Button 
00353   * @param  Button_Mode Button mode requested.
00354   *   This parameter can be one of the following values:   
00355   *     @arg BUTTON_MODE_GPIO: Button will be used as simple IO 
00356   *     @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line
00357   *                            with interrupt generation capability
00358   * @retval None
00359   */
00360 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode)
00361 {
00362   GPIO_InitTypeDef GPIO_InitStruct;
00363 
00364   /* Enable the corresponding Push Button clock */
00365   BUTTONx_GPIO_CLK_ENABLE(Button);
00366 
00367   /* Configure Push Button pin as input */
00368   GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00369   GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00370   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
00371 
00372   if (Button_Mode == BUTTON_MODE_GPIO)
00373   {
00374     /* Configure Button pin as input */
00375     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00376     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00377   }
00378 
00379   if (Button_Mode == BUTTON_MODE_EXTI)
00380   {
00381     if(Button == BUTTON_KEY)
00382     {
00383       /* Configure Key Push Button pin as input with External interrupt, falling edge */
00384       GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
00385     }
00386     else
00387     {
00388       /* Configure Joystick Push Button pin as input with External interrupt, rising edge */
00389       GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
00390     }
00391     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00392 
00393     /* Enable and set Button EXTI Interrupt to the lowest priority */
00394     HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
00395     HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00396   }
00397 }
00398 
00399 /**
00400   * @brief  Returns the selected button state.
00401   * @param  Button Button to be checked.
00402   *   This parameter can be one of the following values:
00403   *     @arg BUTTON_KEY: Key Push Button 
00404   * @retval The Button GPIO pin value
00405   */
00406 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00407 {
00408   return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00409 }
00410 
00411 /**
00412   * @brief  Configures all button of the joystick in GPIO or EXTI modes.
00413   * @param  Joy_Mode Joystick mode.
00414   *    This parameter can be one of the following values:
00415   *      @arg  JOY_MODE_GPIO: Joystick pins will be used as simple IOs
00416   *      @arg  JOY_MODE_EXTI: Joystick pins will be connected to EXTI line 
00417   *                                 with interrupt generation capability  
00418   * @retval HAL_OK: if all initializations are OK. Other value if error.
00419   */
00420 uint8_t BSP_JOY_Init(JOYMode_TypeDef Joy_Mode)
00421 {
00422   JOYState_TypeDef joykey;
00423   GPIO_InitTypeDef GPIO_InitStruct;
00424 
00425   /* Initialized the Joystick. */
00426   for(joykey = JOY_SEL; joykey < (JOY_SEL+JOYn) ; joykey++)
00427   {
00428     /* Enable the JOY clock */
00429     JOYx_GPIO_CLK_ENABLE(joykey);
00430 
00431     GPIO_InitStruct.Pin = JOY_PIN[joykey];
00432     GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00433     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
00434 
00435     if (Joy_Mode == JOY_MODE_GPIO)
00436     {
00437       /* Configure Joy pin as input */
00438       GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00439       HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct);
00440     }
00441 
00442     if (Joy_Mode == JOY_MODE_EXTI)
00443     {
00444       /* Configure Joy pin as input with External interrupt */
00445       GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
00446       HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct);
00447 
00448       /* Enable and set Joy EXTI Interrupt to the lowest priority */
00449       HAL_NVIC_SetPriority((IRQn_Type)(JOY_IRQn[joykey]), 0x0F, 0x00);
00450       HAL_NVIC_EnableIRQ((IRQn_Type)(JOY_IRQn[joykey]));
00451     }
00452   }
00453   
00454   return HAL_OK;
00455 }
00456 
00457 /**
00458   * @brief  Returns the current joystick status.
00459   * @retval Code of the joystick key pressed
00460   *          This code can be one of the following values:
00461   *            @arg  JOY_NONE
00462   *            @arg  JOY_SEL
00463   *            @arg  JOY_DOWN
00464   *            @arg  JOY_LEFT
00465   *            @arg  JOY_RIGHT
00466   *            @arg  JOY_UP
00467   *            @arg  JOY_NONE
00468   */
00469 JOYState_TypeDef BSP_JOY_GetState(void)
00470 {
00471   JOYState_TypeDef joykey;
00472         
00473   for(joykey = JOY_SEL; joykey < (JOY_SEL+JOYn) ; joykey++)
00474   {
00475     if(HAL_GPIO_ReadPin(JOY_PORT[joykey], JOY_PIN[joykey]) == GPIO_PIN_SET)
00476     {
00477       /* Return Code Joystick key pressed */
00478       return joykey;
00479     }
00480   }
00481   
00482   /* No Joystick key pressed */
00483   return JOY_NONE;
00484 }
00485 
00486 #if defined(HAL_UART_MODULE_ENABLED)
00487 /**
00488   * @brief  Configures COM port.
00489   * @param  COM Specifies the COM port to be configured.
00490   *   This parameter can be one of following parameters:    
00491   *     @arg COM1  
00492   * @param  huart pointer to a UART_HandleTypeDef structure that
00493   *   contains the configuration information for the specified UART peripheral.
00494   * @retval None
00495   */
00496 void BSP_COM_Init(COM_TypeDef COM, UART_HandleTypeDef* huart)
00497 {
00498   GPIO_InitTypeDef GPIO_InitStruct;
00499 
00500   /* Enable GPIO clock */
00501   COMx_TX_GPIO_CLK_ENABLE(COM);
00502   COMx_RX_GPIO_CLK_ENABLE(COM);
00503 
00504   /* Enable USART clock */
00505   COMx_CLK_ENABLE(COM);
00506 
00507   /* Configure USART Tx as alternate function push-pull */
00508   GPIO_InitStruct.Pin = COM_TX_PIN[COM];
00509   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00510   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
00511   GPIO_InitStruct.Pull = GPIO_PULLUP;
00512   GPIO_InitStruct.Alternate = COM_TX_AF[COM];
00513   HAL_GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStruct);
00514     
00515   /* Configure USART Rx as alternate function push-pull */
00516   GPIO_InitStruct.Pin = COM_RX_PIN[COM];
00517   GPIO_InitStruct.Alternate = COM_RX_AF[COM];
00518   HAL_GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStruct);
00519 
00520   /* USART configuration */
00521   huart->Instance = COM_USART[COM];
00522   HAL_UART_Init(huart);
00523 }
00524 #endif /* HAL_UART_MODULE_ENABLED) */
00525 /**
00526   * @}
00527   */ 
00528 
00529 /** @addtogroup STM32303C_EVAL_BUS
00530   * @{
00531   */ 
00532 /*******************************************************************************
00533                             BUS OPERATIONS
00534 *******************************************************************************/
00535 #if defined(HAL_I2C_MODULE_ENABLED)
00536 /******************************* I2C Routines**********************************/
00537 
00538 /**
00539   * @brief Initializes I2C MSP
00540   * @param hi2c I2C handle
00541   * @retval None
00542   */
00543 static void I2Cx_MspInit(I2C_HandleTypeDef *hi2c)
00544 {
00545   GPIO_InitTypeDef  GPIO_InitStruct;  
00546   RCC_PeriphCLKInitTypeDef  RCC_PeriphCLKInitStruct;
00547 
00548   if (hi2c->Instance == EVAL_I2Cx)
00549   {
00550     /*##-1- Configure the Eval I2C clock source. The clock is derived from the SYSCLK #*/
00551     RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2;
00552     RCC_PeriphCLKInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_SYSCLK;
00553     HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
00554 
00555     /*##-2- Configure the GPIOs ################################################*/  
00556 
00557     /* Enable GPIO clock */
00558     EVAL_I2Cx_SDA_GPIO_CLK_ENABLE();
00559     EVAL_I2Cx_SCL_GPIO_CLK_ENABLE();
00560       
00561     /* Configure I2C Tx as alternate function  */
00562     GPIO_InitStruct.Pin       = EVAL_I2Cx_SCL_PIN;
00563     GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
00564     GPIO_InitStruct.Pull      = GPIO_NOPULL;
00565     GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;
00566     GPIO_InitStruct.Alternate = EVAL_I2Cx_SCL_SDA_AF;
00567     HAL_GPIO_Init(EVAL_I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
00568       
00569     /* Configure I2C Rx as alternate function  */
00570     GPIO_InitStruct.Pin = EVAL_I2Cx_SDA_PIN;
00571     HAL_GPIO_Init(EVAL_I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
00572     
00573     
00574     /*##-3- Configure the Eval I2Cx peripheral #######################################*/ 
00575     /* Enable Eval_I2Cx clock */
00576     EVAL_I2Cx_CLK_ENABLE();
00577     
00578     /* Force the I2C Periheral Clock Reset */  
00579     EVAL_I2Cx_FORCE_RESET();
00580       
00581     /* Release the I2C Periheral Clock Reset */  
00582     EVAL_I2Cx_RELEASE_RESET(); 
00583     
00584     /* Enable and set Eval I2Cx Interrupt to the lowest priority */
00585     HAL_NVIC_SetPriority(EVAL_I2Cx_EV_IRQn, 0x0F, 0);
00586     HAL_NVIC_EnableIRQ(EVAL_I2Cx_EV_IRQn);
00587     
00588     /* Enable and set Eval I2Cx Interrupt to the lowest priority */
00589     HAL_NVIC_SetPriority(EVAL_I2Cx_ER_IRQn, 0x0F, 0);
00590     HAL_NVIC_EnableIRQ(EVAL_I2Cx_ER_IRQn);  
00591   }
00592 }
00593 
00594 /**
00595   * @brief Eval I2Cx Bus initialization
00596   * @retval None
00597   */
00598 static void I2Cx_Init(void)
00599 {
00600   if(HAL_I2C_GetState(&heval_I2c) == HAL_I2C_STATE_RESET)
00601   {
00602     heval_I2c.Instance              = EVAL_I2Cx;
00603     heval_I2c.Init.Timing           = EVAL_I2Cx_TIMING;
00604     heval_I2c.Init.OwnAddress1      = 0;
00605     heval_I2c.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
00606     heval_I2c.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLE;
00607     heval_I2c.Init.OwnAddress2      = 0;
00608     heval_I2c.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLE;
00609     heval_I2c.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;  
00610 
00611     /* Init the I2C */
00612     I2Cx_MspInit(&heval_I2c);
00613     HAL_I2C_Init(&heval_I2c);
00614   }
00615 }
00616 
00617 /**
00618   * @brief  Write a value in a register of the device through BUS.
00619   * @param  Addr Device address on BUS Bus.  
00620   * @param  Reg The target register address to write
00621   * @param  RegSize The target register size (can be 8BIT or 16BIT)
00622   * @param  Value The target register value to be written 
00623   * @retval None 
00624   */
00625 static void I2Cx_WriteData(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t Value)
00626   {
00627   HAL_StatusTypeDef status = HAL_OK;
00628   
00629   status = HAL_I2C_Mem_Write(&heval_I2c, Addr, (uint16_t)Reg, RegSize, &Value, 1, I2cxTimeout); 
00630   
00631   /* Check the communication status */
00632   if(status != HAL_OK)
00633   {
00634     /* Re-Initiaize the BUS */
00635     I2Cx_Error();
00636   }        
00637 }
00638 
00639 /**
00640   * @brief  Write a value in a register of the device through BUS.
00641   * @param  Addr Device address on BUS Bus.  
00642   * @param  Reg The target register address to write
00643   * @param  RegSize The target register size (can be 8BIT or 16BIT)
00644   * @param  pBuffer The target register value to be written 
00645   * @param  Length buffer size to be written
00646   * @retval None
00647   */
00648 static HAL_StatusTypeDef I2Cx_WriteBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length)
00649 {
00650   HAL_StatusTypeDef status = HAL_OK;
00651   
00652   status = HAL_I2C_Mem_Write(&heval_I2c, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2cxTimeout); 
00653 
00654 /* Check the communication status */
00655   if(status != HAL_OK)
00656   {
00657     /* Re-Initiaize the BUS */
00658     I2Cx_Error();
00659   }        
00660   return status;
00661 }
00662 
00663 /**
00664   * @brief  Read a register of the device through BUS
00665   * @param  Addr Device address on BUS
00666   * @param  Reg The target register address to read
00667   * @param  RegSize The target register size (can be 8BIT or 16BIT)
00668   * @retval read register value
00669   */
00670 static uint8_t I2Cx_ReadData(uint16_t Addr, uint8_t Reg, uint16_t RegSize)
00671 {
00672   HAL_StatusTypeDef status = HAL_OK;
00673   uint8_t value = 0;
00674   
00675   status = HAL_I2C_Mem_Read(&heval_I2c, Addr, Reg, RegSize, &value, 1, I2cxTimeout);
00676  
00677   /* Check the communication status */
00678   if(status != HAL_OK)
00679   {
00680     /* Re-Initiaize the BUS */
00681     I2Cx_Error();
00682   
00683   }
00684   return value;
00685 }
00686 
00687 /**
00688   * @brief  Reads multiple data on the BUS.
00689   * @param  Addr I2C Address
00690   * @param  Reg Reg Address 
00691   * @param  RegSize The target register size (can be 8BIT or 16BIT)
00692   * @param  pBuffer pointer to read data buffer
00693   * @param  Length length of the data
00694   * @retval 0 if no problems to read multiple data
00695   */
00696 static HAL_StatusTypeDef I2Cx_ReadBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length)
00697 {
00698   HAL_StatusTypeDef status = HAL_OK;
00699 
00700   status = HAL_I2C_Mem_Read(&heval_I2c, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2cxTimeout);
00701   
00702 /* Check the communication status */
00703   if(status != HAL_OK)
00704   {
00705     /* Re-Initiaize the BUS */
00706     I2Cx_Error();
00707   }        
00708   return status;
00709 }
00710 
00711 /**
00712 * @brief  Checks if target device is ready for communication. 
00713 * @note   This function is used with Memory devices
00714 * @param  DevAddress Target device address
00715 * @param  Trials Number of trials
00716 * @retval HAL status
00717 */
00718 static HAL_StatusTypeDef I2Cx_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
00719 { 
00720   return (HAL_I2C_IsDeviceReady(&heval_I2c, DevAddress, Trials, I2cxTimeout));
00721 }
00722 
00723 
00724 /**
00725   * @brief Eval I2Cx error treatment function
00726   * @retval None
00727   */
00728 static void I2Cx_Error (void)
00729 {
00730   /* De-initialize the I2C communication BUS */
00731   HAL_I2C_DeInit(&heval_I2c);
00732   
00733   /* Re- Initiaize the I2C communication BUS */
00734   I2Cx_Init();
00735 }
00736 
00737 #endif /*HAL_I2C_MODULE_ENABLED*/
00738 
00739 #if defined(HAL_SPI_MODULE_ENABLED)
00740 /******************************* SPI Routines **********************************/
00741 /**
00742   * @brief Initializes SPI MSP.
00743   * @param hspi SPI handle
00744   * @retval None
00745   */
00746 static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
00747 {
00748   GPIO_InitTypeDef  GPIO_InitStruct;  
00749   
00750   /*** Configure the GPIOs ***/  
00751   /* Enable GPIO clock */
00752   EVAL_SPIx_SCK_GPIO_CLK_ENABLE();
00753   EVAL_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
00754   
00755   /* configure SPI SCK */
00756   GPIO_InitStruct.Pin = EVAL_SPIx_SCK_PIN;
00757   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00758   GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
00759   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
00760   GPIO_InitStruct.Alternate = EVAL_SPIx_SCK_AF;
00761   HAL_GPIO_Init(EVAL_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
00762 
00763   /* configure SPI MISO and MOSI */
00764   GPIO_InitStruct.Pin = (EVAL_SPIx_MISO_PIN | EVAL_SPIx_MOSI_PIN);
00765   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00766   GPIO_InitStruct.Pull  = GPIO_NOPULL;
00767   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
00768   GPIO_InitStruct.Alternate = EVAL_SPIx_MISO_MOSI_AF;
00769   HAL_GPIO_Init(EVAL_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
00770 
00771   /*** Configure the SPI peripheral ***/ 
00772   /* Enable SPI clock */
00773   EVAL_SPIx_CLK_ENABLE();
00774 }
00775 
00776 /**
00777   * @brief  Initializes SPI HAL.
00778   * @retval None
00779   */
00780 static void SPIx_Init(void)
00781 {
00782   if(HAL_SPI_GetState(&heval_Spi) == HAL_SPI_STATE_RESET)
00783   {
00784     /* SPI Config */
00785     heval_Spi.Instance = EVAL_SPIx;
00786     /* SPI baudrate is set to 18 MHz (PCLK2/SPI_BaudRatePrescaler = 36/2 = 18 MHz) */
00787     heval_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
00788     heval_Spi.Init.Direction = SPI_DIRECTION_2LINES;
00789     heval_Spi.Init.CLKPhase = SPI_PHASE_1EDGE;
00790     heval_Spi.Init.CLKPolarity = SPI_POLARITY_LOW;
00791     heval_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
00792     heval_Spi.Init.CRCPolynomial = 7;
00793     heval_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
00794     heval_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
00795     heval_Spi.Init.NSS = SPI_NSS_SOFT;
00796     heval_Spi.Init.TIMode = SPI_TIMODE_DISABLE;
00797     heval_Spi.Init.Mode = SPI_MODE_MASTER;
00798     
00799     SPIx_MspInit(&heval_Spi);
00800     HAL_SPI_Init(&heval_Spi);
00801   }
00802 }
00803 
00804 /**
00805   * @brief SPI Read 4 bytes from device
00806   * @retval Read data
00807 */
00808 static uint32_t SPIx_Read(void)
00809 {
00810   HAL_StatusTypeDef status = HAL_OK;
00811   uint32_t readvalue = 0;
00812   uint32_t writevalue = 0xFFFFFFFF;
00813   
00814   status = HAL_SPI_TransmitReceive(&heval_Spi, (uint8_t*) &writevalue, (uint8_t*) &readvalue, 1, SpixTimeout);
00815 
00816   /* Check the communication status */
00817   if(status != HAL_OK)
00818   {
00819     /* Execute user timeout callback */
00820     SPIx_Error();
00821   }
00822 
00823   return readvalue;
00824 }
00825 
00826 /**
00827   * @brief SPI Write a byte to device
00828   * @param Value value to be written
00829   * @retval None
00830   */
00831 static void SPIx_Write(uint8_t Value)
00832 {
00833   HAL_StatusTypeDef status = HAL_OK;
00834 
00835   status = HAL_SPI_Transmit(&heval_Spi, (uint8_t*) &Value, 1, SpixTimeout);
00836 
00837   /* Check the communication status */
00838   if(status != HAL_OK)
00839   {
00840     /* Execute user timeout callback */
00841     SPIx_Error();
00842   }
00843 }
00844 
00845 /**
00846   * @brief SPI error treatment function
00847   * @retval None
00848   */
00849 static void SPIx_Error (void)
00850 {
00851   /* De-initialize the SPI communication BUS */
00852   HAL_SPI_DeInit(&heval_Spi);
00853   
00854   /* Re- Initiaize the SPI communication BUS */
00855   SPIx_Init();
00856 }
00857 /**
00858   * @}
00859   */ 
00860 
00861 /** @defgroup STM32303C_EVAL_LINK_OPERATIONS Link Operation functions
00862   * @{
00863   */
00864 
00865 /********************************* LINK LCD ***********************************/
00866 
00867 /**
00868   * @brief  Configures the LCD_SPI interface.
00869   * @retval None
00870   */
00871 void LCD_IO_Init(void)
00872 {
00873   GPIO_InitTypeDef GPIO_InitStruct;
00874 
00875   /* Configure the LCD Control pins ------------------------------------------*/
00876   LCD_NCS_GPIO_CLK_ENABLE();
00877     
00878   /* Configure NCS in Output Push-Pull mode */
00879   GPIO_InitStruct.Pin     = LCD_NCS_PIN;
00880   GPIO_InitStruct.Mode    = GPIO_MODE_OUTPUT_PP;
00881   GPIO_InitStruct.Pull    = GPIO_NOPULL;
00882   GPIO_InitStruct.Speed   = GPIO_SPEED_FREQ_HIGH;
00883   HAL_GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStruct);
00884     
00885   /* Set or Reset the control line */
00886   LCD_CS_LOW();
00887   LCD_CS_HIGH();
00888 
00889   SPIx_Init();
00890 }
00891 
00892 /**
00893   * @brief  Write register value.
00894   * @param  pData Pointer on the register value
00895   * @param  Size Size of byte to transmit to the register
00896   * @retval None
00897   */
00898 void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size)
00899 {
00900   uint32_t counter = 0;
00901 
00902   /* Reset LCD control line(/CS) and Send data */  
00903   LCD_CS_LOW();
00904 
00905   /* Send Start Byte */
00906   SPIx_Write(START_BYTE | LCD_WRITE_REG);
00907   
00908   for (counter = Size; counter != 0; counter--)
00909   {
00910     /* Need to invert bytes for LCD*/
00911     SPIx_Write(*(pData+1));
00912     SPIx_Write(*pData);
00913     counter--;
00914     pData += 2;
00915   }
00916   
00917   /* Reset LCD control line(/CS) and Send data */  
00918   LCD_CS_HIGH();
00919 }
00920 
00921 /**
00922   * @brief  register address.
00923   * @param  Reg
00924   * @retval None
00925   */
00926 void LCD_IO_WriteReg(uint8_t Reg) 
00927 {
00928   /* Reset LCD control line(/CS) and Send command */
00929   LCD_CS_LOW();
00930   
00931   /* Send Start Byte */
00932   SPIx_Write(START_BYTE | SET_INDEX);
00933   
00934   /* Write 16-bit Reg Index (High Byte is 0) */
00935   SPIx_Write(0x00);
00936   SPIx_Write(Reg);
00937   
00938   /* Deselect : Chip Select high */
00939   LCD_CS_HIGH();
00940 }
00941 
00942 /**
00943   * @brief  Read register value.
00944   * @param  Reg
00945   * @retval None
00946   */
00947 uint16_t LCD_IO_ReadData(uint16_t Reg) 
00948 {
00949   uint32_t readvalue = 0;
00950 
00951   /* Change BaudRate Prescaler 8 for Read */
00952   /* Mean SPI baudrate is set to 72/8 = 9 MHz */
00953   heval_Spi.Instance->CR1 &= 0xFFC7;
00954   heval_Spi.Instance->CR1 |= SPI_BAUDRATEPRESCALER_8;
00955   
00956   /* Send Reg value to Read */
00957   LCD_IO_WriteReg(Reg);
00958 
00959   /* Reset LCD control line(/CS) and Send command */
00960   LCD_CS_LOW();
00961   
00962   /* Send Start Byte */
00963   SPIx_Write(START_BYTE | LCD_READ_REG);
00964 
00965   /* Read Upper Byte */
00966   SPIx_Write(0xFF);
00967   readvalue = SPIx_Read();
00968   readvalue = readvalue << 8;
00969   readvalue |= SPIx_Read();
00970   
00971   /* Recover Baud Rate initial value */
00972   heval_Spi.Instance->CR1 &= 0xFFC7;
00973   heval_Spi.Instance->CR1 |= heval_Spi.Init.BaudRatePrescaler;
00974 
00975   HAL_Delay(10);
00976 
00977   /* Deselect : Chip Select high */
00978   LCD_CS_HIGH();
00979   return readvalue;
00980 }
00981 
00982 /**
00983   * @brief  Wait for loop in ms.
00984   * @param  Delay in ms.
00985   * @retval None
00986   */
00987 void LCD_Delay (uint32_t Delay)
00988 {
00989   HAL_Delay(Delay);
00990 }
00991 
00992 /******************************** LINK SD Card ********************************/
00993 
00994 /**
00995   * @brief  Initializes the SD Card and put it into StandBy State (Ready for 
00996   *         data transfer).
00997   * @retval None
00998   */
00999 void SD_IO_Init(void)
01000 {
01001   GPIO_InitTypeDef  GPIO_InitStruct;
01002   uint8_t counter;
01003 
01004   /* SD_CS_GPIO and SD_DETECT_GPIO Periph clock enable */
01005   SD_CS_GPIO_CLK_ENABLE();
01006   SD_DETECT_GPIO_CLK_ENABLE();
01007 
01008   /* Configure SD_CS_PIN pin: SD Card CS pin */
01009   GPIO_InitStruct.Pin = SD_CS_PIN;
01010   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
01011   GPIO_InitStruct.Pull = GPIO_PULLUP;
01012   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
01013   HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
01014 
01015   /* Configure SD_DETECT_PIN pin: SD Card detect pin */
01016   GPIO_InitStruct.Pin = SD_DETECT_PIN;
01017   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
01018   GPIO_InitStruct.Pull = GPIO_PULLUP;
01019   HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStruct);
01020 
01021   /* Enable and set SD EXTI Interrupt to the lowest priority */
01022   HAL_NVIC_SetPriority(SD_DETECT_EXTI_IRQn, 0x0F, 0);
01023   HAL_NVIC_EnableIRQ(SD_DETECT_EXTI_IRQn);
01024 
01025   /*------------Put SD in SPI mode--------------*/
01026   /* SD SPI Config */
01027   SPIx_Init();
01028 
01029   /* SD chip select high */
01030   SD_CS_HIGH();
01031   
01032   /* Send dummy byte 0xFF, 10 times with CS high */
01033   /* Rise CS and MOSI for 80 clocks cycles */
01034   for (counter = 0; counter <= 9; counter++)
01035   {
01036     /* Send dummy byte 0xFF */
01037     SD_IO_WriteByte(SD_DUMMY_BYTE);
01038   }
01039 }
01040 
01041 /**
01042   * @brief  Write a byte on the SD.
01043   * @param  Data byte to send.
01044   * @retval None
01045   */
01046 void SD_IO_WriteByte(uint8_t Data)
01047 {
01048   /* Send the byte */
01049   SPIx_Write(Data);
01050 }
01051 
01052 /**
01053   * @brief  Read a byte from the SD.
01054   * @retval The received byte.
01055   */
01056 uint8_t SD_IO_ReadByte(void)
01057 {
01058   uint8_t data = 0;
01059   
01060   /* Get the received data */
01061   data = SPIx_Read();
01062 
01063   /* Return the shifted data */
01064   return data;
01065 }
01066 
01067 /**
01068   * @brief  Send 5 bytes command to the SD card and get response
01069   * @param  Cmd The user expected command to send to SD card.
01070   * @param  Arg The command argument.
01071   * @param  Crc The CRC.
01072   * @param  Response Expected response from the SD card
01073   * @retval  HAL_StatusTypeDef HAL Status
01074   */
01075 HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response)
01076 {
01077   uint32_t counter = 0x00;
01078   uint8_t frame[6];
01079 
01080   /* Prepare Frame to send */
01081   frame[0] = (Cmd | 0x40); /* Construct byte 1 */
01082   frame[1] = (uint8_t)(Arg >> 24); /* Construct byte 2 */
01083   frame[2] = (uint8_t)(Arg >> 16); /* Construct byte 3 */
01084   frame[3] = (uint8_t)(Arg >> 8); /* Construct byte 4 */
01085   frame[4] = (uint8_t)(Arg); /* Construct byte 5 */
01086   frame[5] = (Crc); /* Construct CRC: byte 6 */
01087   
01088   /* SD chip select low */
01089   SD_CS_LOW();
01090     
01091   /* Send Frame */
01092   for (counter = 0; counter < 6; counter++)
01093   {
01094     SD_IO_WriteByte(frame[counter]); /* Send the Cmd bytes */
01095   }
01096 
01097   if(Response != SD_NO_RESPONSE_EXPECTED)
01098   {
01099     return SD_IO_WaitResponse(Response);
01100   }
01101 
01102   return HAL_OK;
01103 }
01104 
01105 /**
01106   * @brief  Wait response from the SD card
01107   * @param  Response Expected response from the SD card
01108   * @retval  HAL_StatusTypeDef HAL Status
01109   */
01110 HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response)
01111 {
01112   uint32_t timeout = 0xFFF;
01113 
01114   /* Check if response is got or a timeout is happen */
01115   while ((SD_IO_ReadByte() != Response) && timeout)
01116   {
01117     timeout--;
01118   }
01119 
01120   if (timeout == 0)
01121   {
01122     /* After time out */
01123     return HAL_TIMEOUT;
01124   }
01125   else
01126   {
01127     /* Right response got */
01128     return HAL_OK;
01129   }
01130 }
01131 
01132 /**
01133   * @brief  Send dummy byte with CS High
01134   * @retval None
01135   */
01136 void SD_IO_WriteDummy(void)
01137 {
01138   /* SD chip select high */
01139   SD_CS_HIGH();
01140     
01141   /* Send Dummy byte 0xFF */
01142   SD_IO_WriteByte(SD_DUMMY_BYTE);
01143 }
01144 
01145 /******************************** LINK EEPROM SPI ********************************/
01146 
01147 /**
01148   * @brief  Initializes the EEPROM SPI and put it into StandBy State (Ready for 
01149   *         data transfer).
01150   * @retval None
01151   */
01152 void EEPROM_SPI_IO_Init(void)
01153 {
01154   GPIO_InitTypeDef  GPIO_InitStruct;
01155 
01156   /* EEPROM_CS_GPIO Periph clock enable */
01157   EEPROM_CS_GPIO_CLK_ENABLE();
01158 
01159   /* Configure EEPROM_CS_PIN pin: EEPROM SPI CS pin */
01160   GPIO_InitStruct.Pin = EEPROM_CS_PIN;
01161   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
01162   GPIO_InitStruct.Pull = GPIO_PULLUP;
01163   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
01164   HAL_GPIO_Init(EEPROM_CS_GPIO_PORT, &GPIO_InitStruct);
01165 
01166   /*------------Put EEPROM in SPI mode--------------*/
01167   /* EEPROM SPI Config */
01168   SPIx_Init();
01169   
01170   /* EEPROM chip select high */
01171   EEPROM_CS_HIGH();
01172 }
01173 
01174 /**
01175   * @brief  Write a byte on the EEPROM.
01176   * @param  Data byte to send.
01177   * @retval None
01178   */
01179 void EEPROM_SPI_IO_WriteByte(uint8_t Data)
01180 {
01181   /* Send the byte */
01182   SPIx_Write(Data);
01183 }
01184 
01185 /**
01186   * @brief  Read a byte from the EEPROM.
01187   * @retval uint8_t (The received byte).
01188   */
01189 uint8_t EEPROM_SPI_IO_ReadByte(void)
01190 {
01191   uint8_t data = 0;
01192   
01193   /* Get the received data */
01194   data = SPIx_Read();
01195 
01196   /* Return the shifted data */
01197   return data;
01198 }
01199 
01200 /**
01201   * @brief  Write data to SPI EEPROM driver
01202   * @param  MemAddress Internal memory address
01203   * @param  pBuffer Pointer to data buffer
01204   * @param  BufferSize Amount of data to be read
01205   * @retval HAL_StatusTypeDef HAL Status
01206   */
01207 HAL_StatusTypeDef EEPROM_SPI_IO_WriteData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
01208 {
01209   /*!< Enable the write access to the EEPROM */
01210   /*!< Select the EEPROM: Chip Select low */
01211   EEPROM_CS_LOW();
01212 
01213   /*!< Send "Write Enable" instruction */
01214   SPIx_Write(EEPROM_CMD_WREN);
01215 
01216   /*!< Deselect the EEPROM: Chip Select high */
01217   EEPROM_CS_HIGH();
01218   
01219   /*!< Select the EEPROM: Chip Select low */
01220   EEPROM_CS_LOW();
01221 
01222   /*!< Send "Write to Memory " instruction */
01223   /* Send the byte */
01224   SPIx_Write(EEPROM_CMD_WRITE);
01225   
01226   /*!< Send MemAddress high nibble address byte to write to */
01227   SPIx_Write((MemAddress & 0xFF0000) >> 16);
01228   
01229   /*!< Send MemAddress medium nibble address byte to write to */
01230   SPIx_Write((MemAddress & 0xFF00) >> 8);
01231   
01232   /*!< Send MemAddress low nibble address byte to write to */
01233   SPIx_Write(MemAddress & 0xFF);
01234   
01235   /*!< while there is data to be written on the EEPROM */
01236   while ((BufferSize)--)
01237   {
01238     /*!< Send the current byte */
01239     SPIx_Write(*pBuffer);
01240     /*!< Point on the next byte to be written */
01241     pBuffer++;
01242   }
01243   
01244   /*!< Deselect the EEPROM: Chip Select high */
01245   EEPROM_CS_HIGH();
01246   
01247   /*!< Wait the end of EEPROM writing */
01248   EEPROM_SPI_IO_WaitEepromStandbyState();
01249   
01250   /*!< Disable the write access to the EEROM */
01251   EEPROM_CS_LOW();
01252 
01253   /*!< Send "Write Disable" instruction */
01254   SPIx_Write(EEPROM_CMD_WRDI);
01255 
01256   /*!< Deselect the EEPROM: Chip Select high */
01257   EEPROM_CS_HIGH();
01258 
01259   return HAL_OK;
01260 }
01261 
01262 /**
01263   * @brief  Read data from SPI EEPROM driver
01264   * @param  MemAddress Internal memory address
01265   * @param  pBuffer Pointer to data buffer
01266   * @param  BufferSize Amount of data to be read
01267   * @retval HAL_StatusTypeDef HAL Status
01268   */
01269 HAL_StatusTypeDef EEPROM_SPI_IO_ReadData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
01270 {
01271   /*!< Select the EEPROM: Chip Select low */
01272   EEPROM_CS_LOW();
01273  
01274   /*!< Send "Write to Memory " instruction */
01275   SPIx_Write(EEPROM_CMD_READ);
01276   
01277   /*!< Send MemAddress high nibble address byte to write to */
01278   SPIx_Write((MemAddress & 0xFF0000) >> 16);
01279   
01280   /*!< Send WriteAddr medium nibble address byte to write to */
01281   SPIx_Write((MemAddress & 0xFF00) >> 8);
01282   
01283   /*!< Send WriteAddr low nibble address byte to write to */
01284   SPIx_Write(MemAddress & 0xFF);
01285   
01286   while ((BufferSize)--) /*!< while there is data to be read */
01287   {
01288     /*!< Read a byte from the EEPROM */
01289     *pBuffer = SPIx_Read();
01290     /*!< Point to the next location where the byte read will be saved */
01291     pBuffer++;
01292   }
01293   
01294   /*!< Deselect the EEPROM: Chip Select high */
01295   EEPROM_CS_HIGH();
01296 
01297   return HAL_OK;
01298 }
01299 
01300 /**
01301   * @brief  Wait response from the SPI EEPROM
01302   * @retval HAL_StatusTypeDef HAL Status
01303   */
01304 HAL_StatusTypeDef EEPROM_SPI_IO_WaitEepromStandbyState(void)
01305 {
01306   uint32_t timeout = 0xFFFF;
01307   uint32_t eepromstatus;
01308   
01309   /*!< Select the EEPROM: Chip Select low */
01310   EEPROM_CS_LOW();
01311   
01312   /*!< Send "Read Status Register" instruction */
01313   SPIx_Write(EEPROM_CMD_RDSR);
01314 
01315   /*!< Loop as long as the memory is busy with a write cycle */
01316   do
01317   {
01318     /*!< Send a dummy byte to generate the clock needed by the EEPROM
01319     and put the value of the status register in EEPROM Status variable */
01320     eepromstatus = SPIx_Read();
01321     timeout --;
01322   }
01323   while (((eepromstatus & EEPROM_WIP_FLAG) == SET) && timeout); /* Write in progress */
01324 
01325   /*!< Deselect the EEPROM: Chip Select high */
01326   EEPROM_CS_HIGH();
01327 
01328   if ((eepromstatus & EEPROM_WIP_FLAG) != SET)
01329   {
01330     /* Right response got */
01331     return HAL_OK;
01332   }
01333   else
01334   {
01335     /* After time out */
01336     return HAL_TIMEOUT;
01337   }
01338 }
01339 #endif /* HAL_SPI_MODULE_ENABLED */
01340 
01341 #if defined(HAL_I2C_MODULE_ENABLED)
01342 /********************************* LINK I2C EEPROM *****************************/
01343 /**
01344   * @brief  Initializes peripherals used by the I2C EEPROM driver.
01345   * @retval None
01346   */
01347 void EEPROM_I2C_IO_Init(void)
01348 {
01349   I2Cx_Init();
01350 }
01351 
01352 /**
01353   * @brief  Write data to I2C EEPROM driver
01354   * @param  DevAddress Target device address
01355   * @param  MemAddress Internal memory address
01356   * @param  pBuffer Pointer to data buffer
01357   * @param  BufferSize Amount of data to be sent
01358   * @retval HAL status
01359   */
01360 HAL_StatusTypeDef EEPROM_I2C_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
01361 {
01362   return (I2Cx_WriteBuffer(DevAddress, MemAddress, I2C_MEMADD_SIZE_16BIT, pBuffer, BufferSize));
01363 }
01364 
01365 /**
01366   * @brief  Read data from I2C EEPROM driver
01367   * @param  DevAddress Target device address
01368   * @param  MemAddress Internal memory address
01369   * @param  pBuffer Pointer to data buffer
01370   * @param  BufferSize Amount of data to be read
01371   * @retval HAL status
01372   */
01373 HAL_StatusTypeDef EEPROM_I2C_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
01374 {
01375   return (I2Cx_ReadBuffer(DevAddress, MemAddress, I2C_MEMADD_SIZE_16BIT, pBuffer, BufferSize));
01376 }
01377 
01378 /**
01379 * @brief  Checks if target device is ready for communication. 
01380 * @note   This function is used with Memory devices
01381 * @param  DevAddress Target device address
01382 * @param  Trials Number of trials
01383 * @retval HAL status
01384 */
01385 HAL_StatusTypeDef EEPROM_I2C_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
01386 { 
01387   return (I2Cx_IsDeviceReady(DevAddress, Trials));
01388 }
01389 
01390 /********************************* LINK I2C TEMPERATURE SENSOR *****************************/
01391 /**
01392   * @brief  Initializes peripherals used by the I2C Temperature Sensor driver.
01393   * @retval None
01394   */
01395 void TSENSOR_IO_Init(void)
01396 {
01397   I2Cx_Init();
01398 }
01399 
01400 /**
01401   * @brief  Writes one byte to the TSENSOR.
01402   * @param  DevAddress Target device address
01403   * @param  pBuffer Pointer to data buffer
01404   * @param  WriteAddr TSENSOR's internal address to write to.
01405   * @param  Length Number of data to write
01406   * @retval None
01407   */
01408 void TSENSOR_IO_Write(uint16_t DevAddress, uint8_t* pBuffer, uint8_t WriteAddr, uint16_t Length)
01409 {
01410   I2Cx_WriteBuffer(DevAddress, WriteAddr, I2C_MEMADD_SIZE_8BIT, pBuffer, Length);
01411 }
01412 
01413 /**
01414   * @brief  Reads one byte from the TSENSOR.
01415   * @param  DevAddress Target device address
01416   * @param  pBuffer pointer to the buffer that receives the data read from the TSENSOR.
01417   * @param  ReadAddr TSENSOR's internal address to read from.
01418   * @param  Length Number of data to read
01419   * @retval None
01420   */
01421 void TSENSOR_IO_Read(uint16_t DevAddress, uint8_t* pBuffer, uint8_t ReadAddr, uint16_t Length)
01422 {
01423   I2Cx_ReadBuffer(DevAddress, ReadAddr, I2C_MEMADD_SIZE_8BIT, pBuffer, Length);
01424 }
01425 
01426 /**
01427 * @brief  Checks if Temperature Sensor is ready for communication. 
01428   * @param  DevAddress Target device address
01429   * @param  Trials Number of trials
01430 * @retval HAL status
01431 */
01432 uint16_t TSENSOR_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
01433 { 
01434   return (I2Cx_IsDeviceReady(DevAddress, Trials));
01435 }
01436 
01437 
01438 /********************************* LINK AUDIO CODEC ***********************************/
01439 /**
01440   * @brief  Initializes peripherals used by the Audio Codec driver.
01441   * @retval None
01442   */
01443 void AUDIO_IO_Init(void) 
01444 {
01445   I2Cx_Init();
01446 }
01447 
01448 /**
01449   * @brief  DeInitializes Audio low level.
01450   * @note   This function is intentionally kept empty, user should define it.
01451   */
01452 void AUDIO_IO_DeInit(void)
01453 {
01454 
01455 }
01456 
01457 /**
01458   * @brief  Writes a single data on the Audio Codec.
01459   * @param  DevAddress Target device address
01460   * @param  Reg Target Register address 
01461   * @param  Value Data to be written
01462   * @retval None
01463   */
01464 void AUDIO_IO_Write(uint16_t DevAddress, uint8_t Reg, uint8_t Value)
01465 {
01466   I2Cx_WriteData(DevAddress, Reg, I2C_MEMADD_SIZE_8BIT, Value);
01467 }
01468 
01469 /**
01470   * @brief  Reads a single data from the Audio Codec.
01471   * @param  DevAddress Target device address
01472   * @param  Reg Target Register address 
01473   * @retval Data to be read
01474   */
01475 uint8_t AUDIO_IO_Read(uint16_t DevAddress, uint8_t Reg)
01476 {
01477   uint8_t value;
01478   
01479   value = I2Cx_ReadData(DevAddress, Reg, I2C_MEMADD_SIZE_8BIT);
01480   
01481   return value;
01482 }
01483 
01484 /**
01485   * @brief  Wait for loop in ms.
01486   * @param  Delay in ms.
01487   * @retval None
01488   */
01489 void AUDIO_IO_Delay(uint32_t Delay)
01490 {
01491   HAL_Delay(Delay);
01492 }
01493 
01494 #endif /* HAL_I2C_MODULE_ENABLED */
01495 
01496 /**
01497   * @}
01498   */ 
01499 
01500 /**
01501   * @}
01502   */ 
01503 
01504 /**
01505   * @}
01506   */
01507 
01508 /**
01509   * @}
01510   */
01511     
01512 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed May 31 2017 10:00:44 for STM32303C_EVAL BSP User Manual by   doxygen 1.7.6.1