Hem Dutt Dabral / BLE_BlueNRG_fork

Fork of BLE_BlueNRG by Mridupawan Das

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l0xx_nucleo.c Source File

stm32l0xx_nucleo.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l0xx_nucleo.c
00004   * @author  MCD Application Team
00005   * @version V1.0.0
00006   * @date    22-April-2014
00007   * @brief   This file provides set of firmware functions to manage:
00008   *          - LEDs and push-button available on STM32L0XX-Nucleo Kit 
00009   *            from STMicroelectronics
00010   *          - LCD, joystick and microSD available on Adafruit 1.8" TFT LCD 
00011   *            shield (reference ID 802)
00012   ******************************************************************************
00013   * @attention
00014   *
00015   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00016   *
00017   * Redistribution and use in source and binary forms, with or without modification,
00018   * are permitted provided that the following conditions are met:
00019   *   1. Redistributions of source code must retain the above copyright notice,
00020   *      this list of conditions and the following disclaimer.
00021   *   2. Redistributions in binary form must reproduce the above copyright notice,
00022   *      this list of conditions and the following disclaimer in the documentation
00023   *      and/or other materials provided with the distribution.
00024   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00025   *      may be used to endorse or promote products derived from this software
00026   *      without specific prior written permission.
00027   *
00028   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00029   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00031   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00032   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00033   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00034   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00035   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00036   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00037   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038   *
00039   ******************************************************************************
00040   */ 
00041   
00042 /* Includes ------------------------------------------------------------------*/
00043 #include "stm32l0xx_nucleo.h"
00044     
00045 /** @addtogroup BSP
00046   * @{
00047   */ 
00048 
00049 /** @addtogroup STM32L0XX_NUCLEO
00050   * @{
00051   */   
00052     
00053 /** @addtogroup STM32L0XX_NUCLEO_LOW_LEVEL 
00054   * @brief This file provides set of firmware functions to manage Leds and push-button
00055   *        available on STM32L0XX-Nucleo Kit from STMicroelectronics.
00056   * @{
00057   */ 
00058 
00059 /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_TypesDefinitions 
00060   * @{
00061   */ 
00062 /**
00063   * @}
00064   */ 
00065 
00066 
00067 /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Defines 
00068   * @{
00069   */ 
00070 
00071 /**
00072   * @brief STM32L0XX NUCLEO BSP Driver version number V1.0.0
00073   */
00074 #define __STM32L0XX_NUCLEO_BSP_VERSION_MAIN   (0x01) /*!< [31:24] main version */
00075 #define __STM32L0XX_NUCLEO_BSP_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */
00076 #define __STM32L0XX_NUCLEO_BSP_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */
00077 #define __STM32L0XX_NUCLEO_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */ 
00078 #define __STM32L0XX_NUCLEO_BSP_VERSION         ((__STM32L0XX_NUCLEO_BSP_VERSION_MAIN << 24)\
00079                                              |(__STM32L0XX_NUCLEO_BSP_VERSION_SUB1 << 16)\
00080                                              |(__STM32L0XX_NUCLEO_BSP_VERSION_SUB2 << 8 )\
00081                                              |(__STM32L0XX_NUCLEO_BSP_VERSION_RC))
00082 
00083 /**
00084   * @brief LINK SD Card
00085   */
00086 #define SD_DUMMY_BYTE            0xFF    
00087 #define SD_NO_RESPONSE_EXPECTED  0x80
00088 
00089 /**
00090   * @}
00091   */ 
00092 
00093 /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Macros
00094   * @{
00095   */ 
00096 /**
00097   * @}
00098   */ 
00099 
00100 /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Variables
00101   * @{
00102   */ 
00103 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
00104 
00105 const uint16_t GPIO_PIN[LEDn] = {LED2_PIN};
00106                                 
00107 GPIO_TypeDef*  BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
00108 const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
00109 const uint16_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
00110 
00111 /**
00112  * @brief BUS variables
00113  */
00114 
00115 uint32_t SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
00116 static SPI_HandleTypeDef hnucleo_Spi;
00117 static ADC_HandleTypeDef hnucleo_Adc;
00118 
00119 /* ADC channel configuration structure declaration */
00120 static ADC_ChannelConfTypeDef sConfig;
00121 
00122 /**
00123   * @}
00124   */ 
00125 
00126 /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_FunctionPrototypes
00127   * @{
00128   */ 
00129 static void               SPIx_Init(void);
00130 static void               SPIx_Write(uint8_t Value);
00131 static uint32_t           SPIx_Read(void);
00132 static void               SPIx_Error (void);
00133 static void               SPIx_MspInit(SPI_HandleTypeDef *hspi);
00134 
00135 static void               ADCx_Init(void);
00136 static void               ADCx_MspInit(ADC_HandleTypeDef *hadc);
00137 
00138 /* SD IO functions */
00139 void                      SD_IO_Init(void);
00140 HAL_StatusTypeDef         SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response);
00141 HAL_StatusTypeDef         SD_IO_WaitResponse(uint8_t Response);
00142 void                      SD_IO_WriteDummy(void);
00143 void                      SD_IO_WriteByte(uint8_t Data);
00144 uint8_t                   SD_IO_ReadByte(void);
00145 
00146 /* LCD IO functions */
00147 void                      LCD_IO_Init(void);
00148 void                      LCD_IO_WriteData(uint8_t Data);
00149 void                      LCD_IO_WriteReg(uint8_t LCDReg);
00150 void                      LCD_Delay(uint32_t delay);
00151 /**
00152   * @}
00153   */ 
00154 
00155 /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Functions
00156   * @{
00157   */ 
00158 
00159 /**
00160   * @brief  This method returns the STM32L0XX NUCLEO BSP Driver revision
00161   * @param  None
00162   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
00163   */
00164 uint32_t BSP_GetVersion(void)
00165 {
00166   return __STM32L0XX_NUCLEO_BSP_VERSION;
00167 }
00168 
00169 /**
00170   * @brief  Configures LED GPIO.
00171   * @param  Led: LED to be configured. 
00172   *          This parameter can be one of the following values:
00173   *            @arg  LED2
00174   * @retval None
00175   */
00176 void BSP_LED_Init(Led_TypeDef Led)
00177 {
00178   GPIO_InitTypeDef  GPIO_InitStruct;
00179   
00180   /* Enable the GPIO_LED Clock */
00181   LEDx_GPIO_CLK_ENABLE(Led);
00182 
00183   /* Configure the GPIO_LED pin */
00184   GPIO_InitStruct.Pin = GPIO_PIN[Led];
00185   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00186   GPIO_InitStruct.Pull = GPIO_PULLUP;
00187   GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00188   
00189   HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
00190 }
00191 
00192 /**
00193   * @brief  Turns selected LED On.
00194   * @param  Led: Specifies the Led to be set on. 
00195   *   This parameter can be one of following parameters:
00196   *            @arg  LED2
00197   * @retval None
00198   */
00199 void BSP_LED_On(Led_TypeDef Led)
00200 {
00201   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET); 
00202 }
00203 
00204 /**
00205   * @brief  Turns selected LED Off. 
00206   * @param  Led: Specifies the Led to be set off. 
00207   *   This parameter can be one of following parameters:
00208   *            @arg  LED2
00209   * @retval None
00210   */
00211 void BSP_LED_Off(Led_TypeDef Led)
00212 {
00213   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 
00214 }
00215 
00216 /**
00217   * @brief  Toggles the selected LED.
00218   * @param  Led: Specifies the Led to be toggled. 
00219   *   This parameter can be one of following parameters:
00220   *            @arg  LED2
00221   * @retval None
00222   */
00223 void BSP_LED_Toggle(Led_TypeDef Led)
00224 {
00225   HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
00226 }
00227 
00228 /**
00229   * @brief  Configures Button GPIO and EXTI Line.
00230   * @param  Button: Specifies the Button to be configured.
00231   *   This parameter should be: BUTTON_KEY
00232   * @param  ButtonMode: Specifies Button mode.
00233   *   This parameter can be one of following parameters:   
00234   *     @arg  BUTTON_MODE_GPIO: Button will be used as simple IO
00235   *     @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
00236   *                            generation capability  
00237   * @retval None
00238   */
00239 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode)
00240 {
00241   GPIO_InitTypeDef GPIO_InitStruct;
00242   
00243   /* Enable the BUTTON Clock */
00244   BUTTONx_GPIO_CLK_ENABLE(Button);
00245   __SYSCFG_CLK_ENABLE();
00246   
00247   if(Button_Mode == BUTTON_MODE_GPIO)
00248   {
00249     /* Configure Button pin as input */
00250     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00251     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00252     GPIO_InitStruct.Pull = GPIO_NOPULL;
00253     GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00254     
00255     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00256   }
00257   
00258   if(Button_Mode == BUTTON_MODE_EXTI)
00259   {
00260     /* Configure Button pin as input with External interrupt */
00261     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00262     GPIO_InitStruct.Pull = GPIO_NOPULL;
00263     GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00264     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
00265     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00266     
00267     /* Enable and set Button EXTI Interrupt to the lowest priority */
00268     NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x03);
00269     HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00270   }
00271 }
00272 
00273 /**
00274   * @brief  Returns the selected Button state.
00275   * @param  Button: Specifies the Button to be checked.
00276   *   This parameter should be: BUTTON_KEY  
00277   * @retval The Button GPIO pin value.
00278   */
00279 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00280 {
00281   return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00282 }
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 8 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 32/4 = 8 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 32 MHz 
00342        */ 
00343     hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
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 ADC1 Channel8 as analog input */
00662   GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
00663   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
00664   HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &GPIO_InitStruct);
00665 
00666   /*** Configure the ADC peripheral ***/ 
00667   /* Enable ADC clock */
00668   NUCLEO_ADCx_CLK_ENABLE(); 
00669 }
00670 
00671 /**
00672   * @brief  Initializes ADC HAL.
00673   * @param  None
00674   * @retval None
00675   */
00676 static void ADCx_Init(void)
00677 {
00678   if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
00679   {
00680     /* ADC Config */
00681     hnucleo_Adc.Instance = NUCLEO_ADCx;
00682     hnucleo_Adc.Init.OversamplingMode      = DISABLE;
00683     hnucleo_Adc.Init.ClockPrescaler        = ADC_CLOCKPRESCALER_PCLK_DIV2; /* (must not exceed 16MHz) */
00684     hnucleo_Adc.Init.LowPowerAutoOff       = DISABLE;
00685     hnucleo_Adc.Init.LowPowerFrequencyMode = ENABLE;
00686     hnucleo_Adc.Init.LowPowerAutoWait      = ENABLE;
00687     hnucleo_Adc.Init.Resolution            = ADC_RESOLUTION12b;
00688     hnucleo_Adc.Init.SamplingTime          = ADC_SAMPLETIME_1CYCLE_5;
00689     hnucleo_Adc.Init.ScanDirection         = ADC_SCAN_DIRECTION_UPWARD;
00690     hnucleo_Adc.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
00691     hnucleo_Adc.Init.ContinuousConvMode    = DISABLE;
00692     hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE;
00693     hnucleo_Adc.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIG_EDGE_NONE;
00694     hnucleo_Adc.Init.EOCSelection          = EOC_SINGLE_CONV;
00695     hnucleo_Adc.Init.DMAContinuousRequests = DISABLE;    
00696     
00697     ADCx_MspInit(&hnucleo_Adc);
00698     HAL_ADC_Init(&hnucleo_Adc);
00699   }
00700 }
00701 
00702 /**
00703   * @brief  Configures joystick available on adafruit 1.8" TFT shield 
00704   *         managed through ADC to detect motion.
00705   * @param  None
00706   * @retval Joystickstatus (0=> success, 1=> fail) 
00707   */
00708 uint8_t BSP_JOY_Init(void)
00709 {
00710   uint8_t status = 1;
00711    
00712   ADCx_Init();
00713    
00714   /* Start ADC calibration */
00715   HAL_ADCEx_Calibration_Start(&hnucleo_Adc, ADC_SINGLE_ENDED);
00716   
00717   /* Select Channel 0 to be converted */
00718   sConfig.Channel = ADC_CHANNEL_8;    
00719   status = HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig);
00720   
00721   /* Return Joystick initialization status */
00722   return status;
00723 }
00724 
00725 /**
00726   * @brief  Returns the Joystick key pressed.
00727   * @note   To know which Joystick key is pressed we need to detect the voltage
00728   *         level on each key output
00729   *           - None  : 3.3 V / 4095
00730   *           - SEL   : 1.055 V / 1308
00731   *           - DOWN  : 0.71 V / 88
00732   *           - LEFT  : 3.0 V / 3720 
00733   *           - RIGHT : 0.595 V / 737
00734   *           - UP    : 1.65 V / 2046
00735   * @retval JOYState_TypeDef: Code of the Joystick key pressed.
00736   */
00737 JOYState_TypeDef BSP_JOY_GetState(void)
00738 {
00739   JOYState_TypeDef state;
00740   uint16_t  KeyConvertedValue = 0;
00741   
00742  /* Start the conversion process */
00743   HAL_ADC_Start(&hnucleo_Adc);
00744   
00745   /* Wait for the end of conversion */
00746   HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
00747   
00748   /* Check if the continous conversion of regular channel is finished */
00749   if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_EOC)
00750   {
00751     /* Get the converted value of regular channel */
00752     KeyConvertedValue = HAL_ADC_GetValue(&hnucleo_Adc);
00753   }
00754  
00755   if((KeyConvertedValue > 2010) && (KeyConvertedValue < 2090))
00756   {
00757     state = JOY_UP;
00758   }
00759   else if((KeyConvertedValue > 680) && (KeyConvertedValue < 780))
00760   {
00761     state = JOY_RIGHT;
00762   }
00763   else if((KeyConvertedValue > 1270) && (KeyConvertedValue < 1350))
00764   {
00765     state = JOY_SEL;
00766   }
00767   else if((KeyConvertedValue > 50) && (KeyConvertedValue < 130))
00768   {
00769     state = JOY_DOWN;
00770   }
00771   else if((KeyConvertedValue > 3680) && (KeyConvertedValue < 3760))
00772   {
00773     state = JOY_LEFT;
00774   }
00775   else
00776   {
00777     state = JOY_NONE;
00778   }
00779   
00780   /* Loop while a key is pressed */
00781   if(state != JOY_NONE)
00782   { 
00783     KeyConvertedValue = HAL_ADC_GetValue(&hnucleo_Adc);  
00784   }
00785   /* Return the code of the Joystick key pressed */
00786   return state;
00787 }
00788 
00789 /**
00790   * @}
00791   */ 
00792 
00793 /**
00794   * @}
00795   */
00796 
00797 /**
00798   * @}
00799   */    
00800 
00801 /**
00802   * @}
00803   */ 
00804     
00805 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/