my fork

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f4xx_nucleo.c Source File

stm32f4xx_nucleo.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_nucleo.c
00004   * @author  MCD Application Team
00005   * @version V1.1.0
00006   * @date    19-June-2014
00007   * @cond DOXYGEN_EXCLUDE
00008   * @brief   This file provides set of firmware functions to manage:
00009   *          + LEDs and push-button available on STM32F4XX-Nucleo Kit 
00010   *            from STMicroelectronics.
00011   *          + LCD, joystick and microSD available on Adafruit 1.8" TFT LCD 
00012   *            Expansion Board (reference ID 802).
00013   * @endcond
00014   ******************************************************************************
00015   * @attention
00016   *
00017   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00018   *
00019   * Redistribution and use in source and binary forms, with or without modification,
00020   * are permitted provided that the following conditions are met:
00021   *   1. Redistributions of source code must retain the above copyright notice,
00022   *      this list of conditions and the following disclaimer.
00023   *   2. Redistributions in binary form must reproduce the above copyright notice,
00024   *      this list of conditions and the following disclaimer in the documentation
00025   *      and/or other materials provided with the distribution.
00026   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00027   *      may be used to endorse or promote products derived from this software
00028   *      without specific prior written permission.
00029   *
00030   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00031   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00032   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00033   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00034   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00036   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00037   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00038   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00039   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00040   *
00041   ******************************************************************************
00042   */ 
00043   
00044 /* Includes ------------------------------------------------------------------*/
00045 #include "stm32f4xx_nucleo.h "
00046 
00047 /** @addtogroup BSP
00048   * @{
00049   */ 
00050 
00051 /** @addtogroup STM32F4XX_NUCLEO
00052   * @{
00053   */   
00054     
00055 /** @addtogroup STM32F4XX_NUCLEO_LOW_LEVEL 
00056   * @brief This file provides set of firmware functions to manage Leds and push-button
00057   *        available on STM32F4xx-Nucleo Kit from STMicroelectronics.
00058   * @{
00059   */ 
00060 
00061 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_TypesDefinitions
00062   * @{
00063   */ 
00064 /**
00065   * @}
00066   */ 
00067 
00068 
00069 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Defines
00070   * @{
00071   */ 
00072 
00073 /**
00074   * @brief STM32F4xx NUCLEO BSP Driver version number V1.1.0
00075   */
00076 #define __STM32F4xx_NUCLEO_BSP_VERSION_MAIN   (0x01) /*!< [31:24] main version */
00077 #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB1   (0x01) /*!< [23:16] sub1 version */
00078 #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */
00079 #define __STM32F4xx_NUCLEO_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */ 
00080 #define __STM32F4xx_NUCLEO_BSP_VERSION        ((__STM32F4xx_NUCLEO_BSP_VERSION_MAIN << 24)\
00081                                              |(__STM32F4xx_NUCLEO_BSP_VERSION_SUB1 << 16)\
00082                                              |(__STM32F4xx_NUCLEO_BSP_VERSION_SUB2 << 8 )\
00083                                              |(__STM32F4xx_NUCLEO_BSP_VERSION_RC))   
00084 
00085 /**
00086   * @brief LINK SD Card
00087   */
00088 #define SD_DUMMY_BYTE            0xFF    
00089 #define SD_NO_RESPONSE_EXPECTED  0x80
00090 
00091 /**
00092   * @}
00093   */ 
00094 
00095 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Macros
00096   * @{
00097   */ 
00098 /**
00099   * @}
00100   */ 
00101 
00102 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Variables
00103   * @{
00104   */ 
00105 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
00106 
00107 const uint16_t GPIO_PIN[LEDn] = {LED2_PIN};
00108 
00109 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT}; 
00110 const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN}; 
00111 const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
00112 
00113 /**
00114  * @brief BUS variables
00115  */
00116 
00117 uint32_t SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
00118 static SPI_HandleTypeDef hnucleo_Spi;
00119 static ADC_HandleTypeDef hnucleo_Adc;
00120 
00121 /* ADC channel configuration structure declaration */
00122 static ADC_ChannelConfTypeDef sConfig;
00123 
00124 /**
00125   * @}
00126   */ 
00127 
00128 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_FunctionPrototypes
00129   * @{
00130   */
00131 static void       SPIx_Init(void);
00132 static void       SPIx_Write(uint8_t Value);
00133 static uint32_t   SPIx_Read(void);
00134 static void       SPIx_Error(void);
00135 static void       SPIx_MspInit(SPI_HandleTypeDef *hspi);
00136 
00137 static void       ADCx_Init(void);
00138 static void       ADCx_MspInit(ADC_HandleTypeDef *hadc);
00139 
00140 /* SD IO functions */
00141 void              SD_IO_Init(void);
00142 HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response);
00143 HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response);
00144 void              SD_IO_WriteDummy(void);
00145 void              SD_IO_WriteByte(uint8_t Data);
00146 uint8_t           SD_IO_ReadByte(void);
00147 
00148 /* LCD IO functions */
00149 void              LCD_IO_Init(void);
00150 void              LCD_IO_WriteData(uint8_t Data);
00151 void              LCD_IO_WriteReg(uint8_t LCDReg);
00152 void              LCD_Delay(uint32_t delay);
00153 /**
00154   * @}
00155   */ 
00156 
00157 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Functions
00158   * @{
00159   */ 
00160 
00161 /**
00162   * @brief  This method returns the STM32F4xx NUCLEO BSP Driver revision
00163   * @param  None
00164   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
00165   */
00166 uint32_t BSP_GetVersion(void)
00167 {
00168   return __STM32F4xx_NUCLEO_BSP_VERSION;
00169 }
00170 
00171 /**
00172   * @brief  Configures LED GPIO.
00173   * @param  Led: Specifies the Led to be configured. 
00174   *   This parameter can be one of following parameters:
00175   *     @arg LED2
00176   * @retval None
00177   */
00178 void BSP_LED_Init(Led_TypeDef Led)
00179 {
00180   GPIO_InitTypeDef  GPIO_InitStruct;
00181   
00182   /* Enable the GPIO_LED Clock */
00183   LEDx_GPIO_CLK_ENABLE(Led);
00184   
00185   /* Configure the GPIO_LED pin */
00186   GPIO_InitStruct.Pin = GPIO_PIN[Led];
00187   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00188   GPIO_InitStruct.Pull = GPIO_PULLUP;
00189   GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00190   
00191   HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
00192   
00193   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 
00194 }
00195 
00196 /**
00197   * @brief  Turns selected LED On.
00198   * @param  Led: Specifies the Led to be set on. 
00199   *   This parameter can be one of following parameters:
00200   *     @arg LED2
00201   * @retval None
00202   */
00203 void BSP_LED_On(Led_TypeDef Led)
00204 {
00205   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET); 
00206 }
00207 
00208 /**
00209   * @brief  Turns selected LED Off.
00210   * @param  Led: Specifies the Led to be set off. 
00211   *   This parameter can be one of following parameters:
00212   *     @arg LED2
00213   * @retval None
00214   */
00215 void BSP_LED_Off(Led_TypeDef Led)
00216 {
00217   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 
00218 }
00219 
00220 /**
00221   * @brief  Toggles the selected LED.
00222   * @param  Led: Specifies the Led to be toggled. 
00223   *   This parameter can be one of following parameters:
00224   *     @arg LED2  
00225   * @retval None
00226   */
00227 void BSP_LED_Toggle(Led_TypeDef Led)
00228 {
00229   HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
00230 }
00231 
00232 /**
00233   * @brief  Configures Button GPIO and EXTI Line.
00234   * @param  Button: Specifies the Button to be configured.
00235   *   This parameter should be: BUTTON_KEY
00236   * @param  ButtonMode: Specifies Button mode.
00237   *   This parameter can be one of following parameters:   
00238   *     @arg BUTTON_MODE_GPIO: Button will be used as simple IO 
00239   *     @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
00240   *                            generation capability  
00241   * @retval None
00242   */
00243 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
00244 {
00245   GPIO_InitTypeDef GPIO_InitStruct;
00246   
00247   /* Enable the BUTTON Clock */
00248   BUTTONx_GPIO_CLK_ENABLE(Button);
00249   
00250   if(ButtonMode == BUTTON_MODE_GPIO)
00251   {
00252     /* Configure Button pin as input */
00253     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00254     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00255     GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00256     GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00257     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00258   }
00259   
00260   if(ButtonMode == BUTTON_MODE_EXTI)
00261   {
00262     /* Configure Button pin as input with External interrupt */
00263     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00264     GPIO_InitStruct.Pull = GPIO_NOPULL;
00265     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 
00266     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00267     
00268     /* Enable and set Button EXTI Interrupt to the lowest priority */
00269     HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
00270     HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00271   }
00272 }
00273 
00274 /**
00275   * @brief  Returns the selected Button state.
00276   * @param  Button: Specifies the Button to be checked.
00277   *   This parameter should be: BUTTON_KEY  
00278   * @retval The Button GPIO pin value.
00279   */
00280 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00281 {
00282   return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00283 }
00284 
00285 /******************************************************************************
00286                             BUS OPERATIONS
00287 *******************************************************************************/
00288 /**
00289   * @brief  Initializes SPI MSP.
00290   * @param  None
00291   * @retval None
00292   */
00293 static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
00294 {
00295   GPIO_InitTypeDef  GPIO_InitStruct;  
00296   
00297   /*** Configure the GPIOs ***/  
00298   /* Enable GPIO clock */
00299   NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
00300   NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
00301   
00302   /* Configure SPI SCK */
00303   GPIO_InitStruct.Pin = NUCLEO_SPIx_SCK_PIN;
00304   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00305   GPIO_InitStruct.Pull  = GPIO_PULLUP;
00306   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00307   GPIO_InitStruct.Alternate = NUCLEO_SPIx_SCK_AF;
00308   HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
00309 
00310   /* Configure SPI MISO and MOSI */ 
00311   GPIO_InitStruct.Pin = NUCLEO_SPIx_MOSI_PIN;
00312   GPIO_InitStruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF;
00313   GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
00314   HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
00315   
00316   GPIO_InitStruct.Pin = NUCLEO_SPIx_MISO_PIN;
00317   HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
00318 
00319   /*** Configure the SPI peripheral ***/ 
00320   /* Enable SPI clock */
00321   NUCLEO_SPIx_CLK_ENABLE();
00322 }
00323 
00324 /**
00325   * @brief  Initializes SPI HAL.
00326   * @param  None
00327   * @retval None
00328   */
00329 static void SPIx_Init(void)
00330 {
00331   if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
00332   {
00333     /* SPI Config */
00334     hnucleo_Spi.Instance = NUCLEO_SPIx;
00335       /* SPI baudrate is set to 12,5 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 100/8 = 12,5 MHz) 
00336        to verify these constraints:
00337           - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
00338             Since the provided driver doesn't use read capability from LCD, only constraint 
00339             on write baudrate is considered.
00340           - SD card SPI interface max baudrate is 25MHz for write/read
00341           - PCLK2 max frequency is 100 MHz 
00342        */ 
00343     hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
00344     hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES;
00345     hnucleo_Spi.Init.CLKPhase = SPI_PHASE_2EDGE;
00346     hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_HIGH;
00347     hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
00348     hnucleo_Spi.Init.CRCPolynomial = 7;
00349     hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
00350     hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
00351     hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
00352     hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLED;
00353     hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
00354 
00355     SPIx_MspInit(&hnucleo_Spi);
00356     HAL_SPI_Init(&hnucleo_Spi);
00357   }
00358 }
00359 
00360 /**
00361   * @brief  SPI Read 4 bytes from device.
00362   * @param  None
00363   * @retval Read data
00364 */
00365 static uint32_t SPIx_Read(void)
00366 {
00367   HAL_StatusTypeDef status = HAL_OK;
00368   uint32_t readvalue = 0;
00369   uint32_t writevalue = 0xFFFFFFFF;
00370   
00371   status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &writevalue, (uint8_t*) &readvalue, 1, SpixTimeout);
00372   
00373   /* Check the communication status */
00374   if(status != HAL_OK)
00375   {
00376     /* Execute user timeout callback */
00377     SPIx_Error();
00378   }
00379 
00380   return readvalue;
00381 }
00382 
00383 /**
00384   * @brief  SPI Write a byte to device.
00385   * @param  Value: value to be written
00386   * @retval None
00387   */
00388 static void SPIx_Write(uint8_t Value)
00389 {
00390   HAL_StatusTypeDef status = HAL_OK;
00391 
00392   status = HAL_SPI_Transmit(&hnucleo_Spi, (uint8_t*) &Value, 1, SpixTimeout);
00393     
00394   /* Check the communication status */
00395   if(status != HAL_OK)
00396   {
00397     /* Execute user timeout callback */
00398     SPIx_Error();
00399   }
00400 }
00401 
00402 /**
00403   * @brief  SPI error treatment function.
00404   * @param  None
00405   * @retval None
00406   */
00407 static void SPIx_Error (void)
00408 {
00409   /* De-initialize the SPI communication BUS */
00410   HAL_SPI_DeInit(&hnucleo_Spi);
00411   
00412   /* Re-Initiaize the SPI communication BUS */
00413   SPIx_Init();
00414 }
00415 
00416 /******************************************************************************
00417                             LINK OPERATIONS
00418 *******************************************************************************/
00419 
00420 /********************************* LINK SD ************************************/
00421 /**
00422   * @brief  Initializes the SD Card and put it into StandBy State (Ready for 
00423   *         data transfer).
00424   * @param  None
00425   * @retval None
00426   */
00427 void SD_IO_Init(void)
00428 {
00429   GPIO_InitTypeDef  GPIO_InitStruct;
00430   uint8_t counter;
00431 
00432   /* SD_CS_GPIO Periph clock enable */
00433   SD_CS_GPIO_CLK_ENABLE();
00434 
00435   /* Configure SD_CS_PIN pin: SD Card CS pin */
00436   GPIO_InitStruct.Pin = SD_CS_PIN;
00437   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00438   GPIO_InitStruct.Pull = GPIO_PULLUP;
00439   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00440   HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
00441 
00442   /*------------Put SD in SPI mode--------------*/
00443   /* SD SPI Config */
00444   SPIx_Init();
00445   
00446   /* SD chip select high */
00447   SD_CS_HIGH();
00448   
00449   /* Send dummy byte 0xFF, 10 times with CS high */
00450   /* Rise CS and MOSI for 80 clocks cycles */
00451   for (counter = 0; counter <= 9; counter++)
00452   {
00453     /* Send dummy byte 0xFF */
00454     SD_IO_WriteByte(SD_DUMMY_BYTE);
00455   }
00456 }
00457 
00458 /**
00459   * @brief  Writes a byte on the SD.
00460   * @param  Data: byte to send.
00461   * @retval None
00462   */
00463 void SD_IO_WriteByte(uint8_t Data)
00464 {
00465   /* Send the byte */
00466   SPIx_Write(Data);
00467 }
00468 
00469 /**
00470   * @brief  Reads a byte from the SD.
00471   * @param  None
00472   * @retval The received byte.
00473   */
00474 uint8_t SD_IO_ReadByte(void)
00475 {
00476   uint8_t data = 0;
00477   
00478   /* Get the received data */
00479   data = SPIx_Read();
00480 
00481   /* Return the shifted data */
00482   return data;
00483 }
00484 
00485 /**
00486   * @brief  Sends 5 bytes command to the SD card and get response.
00487   * @param  Cmd: The user expected command to send to SD card.
00488   * @param  Arg: The command argument
00489   * @param  Crc: The CRC
00490   * @param  Response: Expected response from the SD card
00491   * @retval HAL_StatusTypeDef HAL Status
00492   */
00493 HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response)
00494 {
00495   uint32_t counter = 0x00;
00496   uint8_t frame[6];
00497 
00498   /* Prepare Frame to send */
00499   frame[0] = (Cmd | 0x40);         /* Construct byte 1 */
00500   frame[1] = (uint8_t)(Arg >> 24); /* Construct byte 2 */
00501   frame[2] = (uint8_t)(Arg >> 16); /* Construct byte 3 */
00502   frame[3] = (uint8_t)(Arg >> 8);  /* Construct byte 4 */
00503   frame[4] = (uint8_t)(Arg);       /* Construct byte 5 */
00504   frame[5] = (Crc);                /* Construct byte 6 */
00505   
00506   /* SD chip select low */
00507   SD_CS_LOW();
00508     
00509   /* Send Frame */
00510   for (counter = 0; counter < 6; counter++)
00511   {
00512     SD_IO_WriteByte(frame[counter]); /* Send the Cmd bytes */
00513   }
00514 
00515   if(Response != SD_NO_RESPONSE_EXPECTED)
00516   {
00517     return SD_IO_WaitResponse(Response);
00518   }
00519   
00520   return HAL_OK;
00521 }
00522 
00523 /**
00524   * @brief  Waits response from the SD card
00525   * @param  Response: Expected response from the SD card
00526   * @retval HAL_StatusTypeDef HAL Status
00527   */
00528 HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response)
00529 {
00530   uint32_t timeout = 0xFFFF;
00531 
00532   /* Check if response is got or a timeout is happen */
00533   while ((SD_IO_ReadByte() != Response) && timeout)
00534   {
00535     timeout--;
00536   }
00537 
00538   if (timeout == 0)
00539   {
00540     /* After time out */
00541     return HAL_TIMEOUT;
00542   }
00543   else
00544   {
00545     /* Right response got */
00546     return HAL_OK;
00547   }
00548 }
00549 
00550 /**
00551   * @brief  Sends dummy byte with CS High.
00552   * @param  None
00553   * @retval None
00554   */
00555 void SD_IO_WriteDummy(void)
00556 {
00557   /* SD chip select high */
00558   SD_CS_HIGH();
00559   
00560   /* Send Dummy byte 0xFF */
00561   SD_IO_WriteByte(SD_DUMMY_BYTE);
00562 }
00563 
00564 /********************************* LINK LCD ***********************************/
00565 /**
00566   * @brief  Initializes the LCD.
00567   * @param  None
00568   * @retval None
00569   */
00570 void LCD_IO_Init(void)
00571 {
00572   GPIO_InitTypeDef  GPIO_InitStruct;
00573    
00574   /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */
00575   LCD_CS_GPIO_CLK_ENABLE();
00576   LCD_DC_GPIO_CLK_ENABLE();
00577   
00578   /* Configure LCD_CS_PIN pin: LCD Card CS pin */
00579   GPIO_InitStruct.Pin = LCD_CS_PIN;
00580   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00581   GPIO_InitStruct.Pull = GPIO_NOPULL;
00582   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00583   HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
00584 
00585   /* Configure LCD_DC_PIN pin: LCD Card DC pin */
00586   GPIO_InitStruct.Pin = LCD_DC_PIN;
00587   HAL_GPIO_Init(LCD_DC_GPIO_PORT, &GPIO_InitStruct);
00588   
00589   /* LCD chip select high */
00590   LCD_CS_HIGH();
00591   
00592   /* LCD SPI Config */
00593   SPIx_Init();
00594 }
00595 
00596 /**
00597   * @brief  Writes command to select the LCD register.
00598   * @param  LCDReg: Address of the selected register.
00599   * @retval None
00600   */
00601 void LCD_IO_WriteReg(uint8_t LCDReg)
00602 {
00603   /* Reset LCD control line CS */
00604   LCD_CS_LOW();
00605   
00606   /* Set LCD data/command line DC to Low */
00607   LCD_DC_LOW();
00608     
00609   /* Send Command */
00610   SPIx_Write(LCDReg);
00611   
00612   /* Deselect : Chip Select high */
00613   LCD_CS_HIGH();
00614 }
00615 
00616 /**
00617   * @brief  Writes data to select the LCD register.
00618   *         This function must be used after st7735_WriteReg() function
00619   * @param  Data: data to write to the selected register.
00620   * @retval None
00621   */
00622 void LCD_IO_WriteData(uint8_t Data)
00623 {
00624   /* Reset LCD control line CS */
00625   LCD_CS_LOW();
00626   
00627   /* Set LCD data/command line DC to High */
00628   LCD_DC_HIGH();
00629 
00630   /* Send Data */
00631   SPIx_Write(Data);
00632   
00633   /* Deselect : Chip Select high */
00634   LCD_CS_HIGH();
00635 }
00636 
00637 /**
00638   * @brief  Wait for loop in ms.
00639   * @param  Delay in ms.
00640   * @retval None
00641   */
00642 void LCD_Delay(uint32_t Delay)
00643 {
00644   HAL_Delay(Delay);
00645 }
00646 
00647 /******************************* LINK JOYSTICK ********************************/
00648 /**
00649   * @brief  Initializes ADC MSP.
00650   * @param  None
00651   * @retval None
00652   */
00653 static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
00654 {
00655   GPIO_InitTypeDef  GPIO_InitStruct;
00656   
00657   /*** Configure the GPIOs ***/  
00658   /* Enable GPIO clock */
00659   NUCLEO_ADCx_GPIO_CLK_ENABLE();
00660   
00661   /* Configure the selected ADC Channel as analog input */
00662   GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
00663   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
00664   GPIO_InitStruct.Pull = GPIO_NOPULL;
00665   HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &GPIO_InitStruct);
00666   
00667   /*** Configure the ADC peripheral ***/ 
00668   /* Enable ADC clock */
00669   NUCLEO_ADCx_CLK_ENABLE(); 
00670 }
00671 
00672 /**
00673   * @brief  Initializes ADC HAL.
00674   * @param  None
00675   * @retval None
00676   */
00677 static void ADCx_Init(void)
00678 {
00679   if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
00680   {
00681     /* ADC Config */
00682     hnucleo_Adc.Instance                   = NUCLEO_ADCx;
00683     hnucleo_Adc.Init.ClockPrescaler        = ADC_CLOCKPRESCALER_PCLK_DIV4; /* (must not exceed 36MHz) */
00684     hnucleo_Adc.Init.Resolution            = ADC_RESOLUTION12b;
00685     hnucleo_Adc.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
00686     hnucleo_Adc.Init.ContinuousConvMode    = DISABLE;
00687     hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE;
00688     hnucleo_Adc.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE;
00689     hnucleo_Adc.Init.EOCSelection          = EOC_SINGLE_CONV;
00690     hnucleo_Adc.Init.NbrOfConversion       = 1;
00691     hnucleo_Adc.Init.DMAContinuousRequests = DISABLE;    
00692     
00693     ADCx_MspInit(&hnucleo_Adc);
00694     HAL_ADC_Init(&hnucleo_Adc);
00695   }
00696 }
00697 
00698 /**
00699   * @brief  Configures joystick available on adafruit 1.8" TFT shield 
00700   *         managed through ADC to detect motion.
00701   * @param  None
00702   * @retval Joystickstatus (0=> success, 1=> fail) 
00703   */
00704 uint8_t BSP_JOY_Init(void)
00705 {
00706   uint8_t status = 1;
00707    
00708   ADCx_Init();
00709    
00710   /* Select the ADC Channel to be converted */
00711   sConfig.Channel = NUCLEO_ADCx_CHANNEL;
00712   sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
00713   sConfig.Rank = 1;
00714   status = HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig);
00715   
00716   /* Return Joystick initialization status */
00717   return status;
00718 }
00719 
00720 /**
00721   * @brief  Returns the Joystick key pressed.
00722   * @note   To know which Joystick key is pressed we need to detect the voltage
00723   *         level on each key output
00724   *           - None  : 3.3 V / 4095
00725   *           - SEL   : 1.055 V / 1308
00726   *           - DOWN  : 0.71 V / 88
00727   *           - LEFT  : 3.0 V / 3720 
00728   *           - RIGHT : 0.595 V / 737
00729   *           - UP    : 1.65 V / 2046
00730   * @retval JOYState_TypeDef: Code of the Joystick key pressed.
00731   */
00732 JOYState_TypeDef BSP_JOY_GetState(void)
00733 {
00734   JOYState_TypeDef state;
00735   uint16_t  keyconvertedvalue = 0;
00736   
00737   /* Start the conversion process */
00738   HAL_ADC_Start(&hnucleo_Adc);
00739   
00740   /* Wait for the end of conversion */
00741   HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
00742   
00743   /* Check if the continous conversion of regular channel is finished */
00744   if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_EOC_REG)
00745   {
00746     /* Get the converted value of regular channel */
00747     keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
00748   }
00749   
00750   if((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090))
00751   {
00752     state = JOY_UP;
00753   }
00754   else if((keyconvertedvalue > 680) && (keyconvertedvalue < 780))
00755   {
00756     state = JOY_RIGHT;
00757   }
00758   else if((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350))
00759   {
00760     state = JOY_SEL;
00761   }
00762   else if((keyconvertedvalue > 50) && (keyconvertedvalue < 130))
00763   {
00764     state = JOY_DOWN;
00765   }
00766   else if((keyconvertedvalue > 3680) && (keyconvertedvalue < 3760))
00767   {
00768     state = JOY_LEFT;
00769   }
00770   else
00771   {
00772     state = JOY_NONE;
00773   }
00774   
00775   /* Loop while a key is pressed */
00776   if(state != JOY_NONE)
00777   { 
00778     keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);  
00779   }
00780   /* Return the code of the Joystick key pressed */
00781   return state;
00782 }
00783 
00784 /**
00785   * @}
00786   */ 
00787 
00788 /**
00789   * @}
00790   */
00791 
00792 /**
00793   * @}
00794   */    
00795 
00796 /**
00797   * @}
00798   */ 
00799     
00800 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00801