STM32F4xx_Nucleo_144 BSP User Manual: stm32f4xx_nucleo_144.c Source File

STM32F4xx Nucleo 144 BSP Drivers

stm32f4xx_nucleo_144.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_nucleo_144.c
00004   * @author  MCD Application Team
00005   * @version V1.0.1
00006   * @date    13-January-2016
00007   * @brief   This file provides set of firmware functions to manage:
00008   *          - LEDs and push-button available on STM32F4XX-Nucleo-144 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) 2015 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 "stm32f4xx_nucleo_144.h"
00044 
00045 
00046 /** @defgroup BSP BSP
00047   * @{
00048   */ 
00049 
00050 /** @defgroup STM32F4XX_NUCLEO_144 STM32F4XX NUCLEO 144
00051   * @{
00052   */   
00053     
00054 /** @defgroup STM32F4XX_NUCLEO_144_LOW_LEVEL STM32F4XX NUCLEO 144 LOW LEVEL
00055   * @brief This file provides set of firmware functions to manage Leds and push-button
00056   *        available on STM32F4xx-Nucleo Kit from STMicroelectronics.
00057   * @{
00058   */ 
00059 
00060 /** @defgroup STM32F4XX_NUCLEO_144_LOW_LEVEL_Private_TypesDefinitions STM32F4XX NUCLEO 144 LOW LEVEL Private TypesDefinitions
00061   * @{
00062   */ 
00063 /**
00064   * @}
00065   */ 
00066 
00067 
00068 /** @defgroup STM32F4XX_NUCLEO_144_LOW_LEVEL_Private_Defines STM32F4XX NUCLEO 144 LOW LEVEL Private Defines
00069   * @{
00070   */ 
00071 
00072 /**
00073   * @brief STM32F4xx NUCLEO BSP Driver version number V1.0.1
00074   */
00075 #define __STM32F4xx_NUCLEO_BSP_VERSION_MAIN   (0x01) /*!< [31:24] main version */
00076 #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */
00077 #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB2   (0x01) /*!< [15:8]  sub2 version */
00078 #define __STM32F4xx_NUCLEO_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */ 
00079 #define __STM32F4xx_NUCLEO_BSP_VERSION        ((__STM32F4xx_NUCLEO_BSP_VERSION_MAIN << 24)\
00080                                              |(__STM32F4xx_NUCLEO_BSP_VERSION_SUB1 << 16)\
00081                                              |(__STM32F4xx_NUCLEO_BSP_VERSION_SUB2 << 8 )\
00082                                              |(__STM32F4xx_NUCLEO_BSP_VERSION_RC))   
00083 
00084 /**
00085   * @brief LINK SD Card
00086   */
00087 #define SD_DUMMY_BYTE            0xFF    
00088 #define SD_NO_RESPONSE_EXPECTED  0x80
00089 
00090 /**
00091   * @}
00092   */ 
00093 
00094 /** @defgroup STM32F4XX_NUCLEO_144_LOW_LEVEL_Private_Macros STM32F4XX NUCLEO 144 LOW LEVEL Private Macros
00095   * @{
00096   */ 
00097 /**
00098   * @}
00099   */ 
00100 
00101 /** @defgroup STM32F4XX_NUCLEO_144_LOW_LEVEL_Private_Variables STM32F4XX NUCLEO 144 LOW LEVEL Private Variables
00102   * @{
00103   */ 
00104 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED1_GPIO_PORT, LED2_GPIO_PORT, LED3_GPIO_PORT};
00105 
00106 const uint16_t GPIO_PIN[LEDn] = {LED1_PIN, LED2_PIN, LED3_PIN};
00107 
00108 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT}; 
00109 const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN}; 
00110 const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn};
00111 
00112 /**
00113  * @brief BUS variables
00114  */
00115 
00116 #ifdef ADAFRUIT_TFT_JOY_SD_ID802
00117 #ifdef HAL_SPI_MODULE_ENABLED
00118 uint32_t SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
00119 static SPI_HandleTypeDef hnucleo_Spi; 
00120 #endif /* HAL_SPI_MODULE_ENABLED */
00121 
00122 #ifdef HAL_ADC_MODULE_ENABLED
00123 static ADC_HandleTypeDef hnucleo_Adc;
00124 /* ADC channel configuration structure declaration */
00125 static ADC_ChannelConfTypeDef sConfig;
00126 #endif /* HAL_ADC_MODULE_ENABLED */
00127 #endif /* ADAFRUIT_TFT_JOY_SD_ID802 */
00128 
00129 /**
00130   * @}
00131   */ 
00132 
00133 /** @defgroup STM32F4XX_NUCLEO_144_LOW_LEVEL_Private_FunctionPrototypes STM32F4XX NUCLEO 144 LOW LEVEL Private FunctionPrototypes
00134   * @{
00135   */
00136 #ifdef ADAFRUIT_TFT_JOY_SD_ID802
00137 
00138 #ifdef HAL_SPI_MODULE_ENABLED
00139 static void SPIx_Init(void);
00140 static void SPIx_Write(uint8_t Value);
00141 static void SPIx_Error(void);
00142 static void SPIx_MspInit(SPI_HandleTypeDef *hspi);
00143 
00144 /* SD IO functions */
00145 void SD_IO_Init(void);
00146 void SD_IO_CSState(uint8_t state);
00147 void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength);
00148 uint8_t SD_IO_WriteByte(uint8_t Data);
00149 
00150 /* LCD IO functions */
00151 void LCD_IO_Init(void);
00152 void LCD_IO_WriteData(uint8_t Data);
00153 void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
00154 void LCD_IO_WriteReg(uint8_t LCDReg);
00155 void LCD_Delay(uint32_t delay);
00156 #endif /* HAL_SPI_MODULE_ENABLED */
00157 
00158 #ifdef HAL_ADC_MODULE_ENABLED
00159 static void ADCx_Init(void);
00160 static void ADCx_DeInit(void);
00161 static void ADCx_MspInit(ADC_HandleTypeDef *hadc);
00162 static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc);
00163 #endif /* HAL_ADC_MODULE_ENABLED */
00164 
00165 #endif /* ADAFRUIT_TFT_JOY_SD_ID802 */
00166 
00167 /**
00168   * @}
00169   */ 
00170 
00171 /** @defgroup STM32F4XX_NUCLEO_144_LOW_LEVEL_Private_Functions STM32F4XX NUCLEO 144 LOW LEVEL Private Functions
00172   * @{
00173   */ 
00174 
00175 /**
00176   * @brief  This method returns the STM32F4xx NUCLEO BSP Driver revision
00177   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
00178   */
00179 uint32_t BSP_GetVersion(void)
00180 {
00181   return __STM32F4xx_NUCLEO_BSP_VERSION;
00182 }
00183 
00184 /**
00185   * @brief  Configures LED GPIO.
00186   * @param  Led: Specifies the Led to be configured. 
00187   *   This parameter can be one of following parameters:
00188   *     @arg  LED1
00189   *     @arg  LED2
00190   *     @arg  LED3
00191   */
00192 void BSP_LED_Init(Led_TypeDef Led)
00193 {
00194   GPIO_InitTypeDef  GPIO_InitStruct;
00195   
00196   /* Enable the GPIO_LED Clock */
00197   LEDx_GPIO_CLK_ENABLE(Led);
00198   
00199   /* Configure the GPIO_LED pin */
00200   GPIO_InitStruct.Pin = GPIO_PIN[Led];
00201   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00202   GPIO_InitStruct.Pull = GPIO_NOPULL;
00203   GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00204   
00205   HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
00206   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 
00207 }
00208 
00209 /**
00210   * @brief  DeInit LEDs.
00211   * @param  Led: LED to be de-init. 
00212   *   This parameter can be one of the following values:
00213   *     @arg  LED1
00214   *     @arg  LED2
00215   *     @arg  LED3
00216   * @note Led DeInit does not disable the GPIO clock nor disable the Mfx 
00217   */
00218 void BSP_LED_DeInit(Led_TypeDef Led)
00219 {
00220   GPIO_InitTypeDef  gpio_init_structure;
00221 
00222   /* Turn off LED */
00223   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
00224   /* DeInit the GPIO_LED pin */
00225   gpio_init_structure.Pin = GPIO_PIN[Led];
00226   HAL_GPIO_DeInit(GPIO_PORT[Led], gpio_init_structure.Pin);
00227 }
00228 
00229 /**
00230   * @brief  Turns selected LED On.
00231   * @param  Led: Specifies the Led to be set on. 
00232   *   This parameter can be one of following parameters:
00233   *     @arg LED2
00234   */
00235 void BSP_LED_On(Led_TypeDef Led)
00236 {
00237   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET); 
00238 }
00239 
00240 /**
00241   * @brief  Turns selected LED Off.
00242   * @param  Led: Specifies the Led to be set off. 
00243   *   This parameter can be one of following parameters:
00244   *     @arg  LED1
00245   *     @arg  LED2
00246   *     @arg  LED3
00247   */
00248 void BSP_LED_Off(Led_TypeDef Led)
00249 {
00250   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 
00251 }
00252 
00253 /**
00254   * @brief  Toggles the selected LED.
00255   * @param  Led: Specifies the Led to be toggled. 
00256   *   This parameter can be one of following parameters:
00257   *     @arg  LED1
00258   *     @arg  LED2
00259   *     @arg  LED3 
00260   */
00261 void BSP_LED_Toggle(Led_TypeDef Led)
00262 {
00263   HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
00264 }
00265 
00266 /**
00267   * @brief  Configures Button GPIO and EXTI Line.
00268   * @param  Button: Specifies the Button to be configured.
00269   *   This parameter should be: BUTTON_USER
00270   * @param  ButtonMode: Specifies Button mode.
00271   *   This parameter can be one of following parameters:   
00272   *     @arg BUTTON_MODE_GPIO: Button will be used as simple IO 
00273   *     @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
00274   *                            generation capability  
00275   */
00276 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
00277 {
00278   GPIO_InitTypeDef GPIO_InitStruct;
00279   
00280   /* Enable the BUTTON Clock */
00281   BUTTONx_GPIO_CLK_ENABLE(Button);
00282   
00283   if(ButtonMode == BUTTON_MODE_GPIO)
00284   {
00285     /* Configure Button pin as input */
00286     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00287     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00288     GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00289     GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
00290     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00291   }
00292   
00293   if(ButtonMode == BUTTON_MODE_EXTI)
00294   {
00295     /* Configure Button pin as input with External interrupt */
00296     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00297     GPIO_InitStruct.Pull = GPIO_NOPULL;
00298     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; 
00299     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00300     
00301     /* Enable and set Button EXTI Interrupt to the lowest priority */
00302     HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
00303     HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00304   }
00305 }
00306 
00307 /**
00308   * @brief  Push Button DeInit.
00309   * @param  Button: Button to be configured
00310   *   This parameter should be: BUTTON_USER
00311   * @note PB DeInit does not disable the GPIO clock
00312   */
00313 void BSP_PB_DeInit(Button_TypeDef Button)
00314 {
00315   GPIO_InitTypeDef gpio_init_structure;
00316 
00317   gpio_init_structure.Pin = BUTTON_PIN[Button];
00318   HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00319   HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin);
00320 }
00321 
00322 /**
00323   * @brief  Returns the selected Button state.
00324   * @param  Button: Specifies the Button to be checked.
00325   *   This parameter should be: BUTTON_USER  
00326   * @retval The Button GPIO pin value.
00327   */
00328 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00329 {
00330   return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00331 }
00332 
00333 /******************************************************************************
00334                             BUS OPERATIONS
00335 *******************************************************************************/
00336 #ifdef ADAFRUIT_TFT_JOY_SD_ID802
00337 
00338 /******************************* SPI ********************************/
00339 #ifdef HAL_SPI_MODULE_ENABLED
00340 
00341 /**
00342   * @brief  Initializes SPI MSP.
00343   */
00344 static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
00345 {
00346   GPIO_InitTypeDef  GPIO_InitStruct;  
00347   
00348   /*** Configure the GPIOs ***/  
00349   /* Enable GPIO clock */
00350   NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
00351   NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
00352   
00353   /* Configure SPI SCK */
00354   GPIO_InitStruct.Pin = NUCLEO_SPIx_SCK_PIN;
00355   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00356   GPIO_InitStruct.Pull  = GPIO_PULLUP;
00357   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00358   GPIO_InitStruct.Alternate = NUCLEO_SPIx_SCK_AF;
00359   HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
00360 
00361   /* Configure SPI MISO and MOSI */ 
00362   GPIO_InitStruct.Pin = NUCLEO_SPIx_MOSI_PIN;
00363   GPIO_InitStruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF;
00364   GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
00365   HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
00366   
00367   GPIO_InitStruct.Pin = NUCLEO_SPIx_MISO_PIN;
00368   GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
00369   HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
00370 
00371   /*** Configure the SPI peripheral ***/ 
00372   /* Enable SPI clock */
00373   NUCLEO_SPIx_CLK_ENABLE();
00374 }
00375 
00376 /**
00377   * @brief  Initializes SPI HAL.
00378   */
00379 static void SPIx_Init(void)
00380 {
00381   if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
00382   {
00383     /* SPI Config */
00384     hnucleo_Spi.Instance = NUCLEO_SPIx;
00385       /* SPI baudrate is set to 12,5 MHz maximum (APB2/SPI_BaudRatePrescaler = 90/8 = 11,25 MHz) 
00386        to verify these constraints:
00387           - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
00388             Since the provided driver doesn't use read capability from LCD, only constraint 
00389             on write baudrate is considered.
00390           - SD card SPI interface max baudrate is 25MHz for write/read
00391           - PCLK2 max frequency is 90 MHz 
00392        */ 
00393     hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
00394     hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES;
00395     hnucleo_Spi.Init.CLKPhase = SPI_PHASE_2EDGE;
00396     hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_HIGH;
00397     hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
00398     hnucleo_Spi.Init.CRCPolynomial = 7;
00399     hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
00400     hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
00401     hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
00402     hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLED;
00403     hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
00404 
00405     SPIx_MspInit(&hnucleo_Spi);
00406     HAL_SPI_Init(&hnucleo_Spi);
00407   }
00408 }
00409 
00410 /**
00411   * @brief  SPI Write a byte to device
00412   * @param  DataIn: value to be written
00413   * @param  DataOut: value to read
00414   * @param  DataLegnth: length of data
00415   */
00416 static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLegnth)
00417 {
00418   HAL_StatusTypeDef status = HAL_OK;
00419 
00420   status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) DataIn, DataOut, DataLegnth, SpixTimeout);
00421     
00422   /* Check the communication status */
00423   if(status != HAL_OK)
00424   {
00425     /* Execute user timeout callback */
00426     SPIx_Error();
00427   }
00428 }
00429 
00430 /**
00431   * @brief  SPI Write a byte to device.
00432   * @param  Value: value to be written
00433   */
00434 static void SPIx_Write(uint8_t Value)
00435 {
00436   HAL_StatusTypeDef status = HAL_OK;
00437   uint8_t data;
00438 
00439   status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &Value, &data, 1, SpixTimeout);
00440   
00441   /* Check the communication status */
00442   if(status != HAL_OK)
00443   {
00444     /* Execute user timeout callback */
00445     SPIx_Error();
00446   }
00447 }
00448 
00449 /**
00450   * @brief  SPI error treatment function
00451   */
00452 static void SPIx_Error (void)
00453 {
00454   /* De-initialize the SPI communication BUS */
00455   HAL_SPI_DeInit(&hnucleo_Spi);
00456   
00457   /* Re-Initiaize the SPI communication BUS */
00458   SPIx_Init();
00459 }
00460 
00461 /******************************************************************************
00462                             LINK OPERATIONS
00463 *******************************************************************************/
00464 
00465 /********************************* LINK SD ************************************/
00466 /**
00467   * @brief  Initializes the SD Card and put it into StandBy State (Ready for 
00468   *         data transfer).
00469   */
00470 void SD_IO_Init(void)
00471 {
00472   GPIO_InitTypeDef  GPIO_InitStruct;
00473   uint8_t counter;
00474 
00475   /* SD_CS_GPIO Periph clock enable */
00476   SD_CS_GPIO_CLK_ENABLE();
00477 
00478   /* Configure SD_CS_PIN pin: SD Card CS pin */
00479   GPIO_InitStruct.Pin = SD_CS_PIN;
00480   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00481   GPIO_InitStruct.Pull = GPIO_PULLUP;
00482   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00483   HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
00484 
00485 
00486   /*  LCD chip select line perturbs SD also when the LCD is not used */
00487   /*  this is a workaround to avoid sporadic failures during r/w operations */ 
00488   LCD_CS_GPIO_CLK_ENABLE();
00489   GPIO_InitStruct.Pin = LCD_CS_PIN;
00490   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00491   GPIO_InitStruct.Pull = GPIO_NOPULL;
00492   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00493   HAL_GPIO_Init(LCD_CS_GPIO_PORT, &GPIO_InitStruct);
00494   LCD_CS_HIGH();
00495 
00496   /*------------Put SD in SPI mode--------------*/
00497   /* SD SPI Config */
00498   SPIx_Init();
00499   
00500   /* SD chip select high */
00501   SD_CS_HIGH();
00502   
00503   /* Send dummy byte 0xFF, 10 times with CS high */
00504   /* Rise CS and MOSI for 80 clocks cycles */
00505   for (counter = 0; counter <= 9; counter++)
00506   {
00507     /* Send dummy byte 0xFF */
00508     SD_IO_WriteByte(SD_DUMMY_BYTE);
00509   }
00510 }
00511 
00512 /**
00513   * @brief  Set the SD_CS pin.
00514   * @param  val: pin value.
00515   */
00516 void SD_IO_CSState(uint8_t val)
00517 {
00518   if(val == 1) 
00519   {
00520     SD_CS_HIGH();
00521   }
00522   else
00523   {
00524     SD_CS_LOW();
00525   }
00526 }
00527 
00528 /**
00529   * @brief  Write a byte on the SD.
00530   * @param  DataIn: byte to send.
00531   * @param  DataOut: byte to read
00532   * @param  DataLength: length of data
00533   */
00534 void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
00535 {
00536   /* Send the byte */
00537   SPIx_WriteReadData(DataIn, DataOut, DataLength);
00538 }
00539 
00540 /**
00541   * @brief  Writes a byte on the SD.
00542   * @param  Data: byte to send.
00543   */
00544 uint8_t SD_IO_WriteByte(uint8_t Data)
00545 {
00546   uint8_t tmp;
00547   /* Send the byte */
00548   SPIx_WriteReadData(&Data,&tmp,1);
00549   return tmp;
00550 }
00551 
00552 /********************************* LINK LCD ***********************************/
00553 /**
00554   * @brief  Initializes the LCD
00555   */
00556 void LCD_IO_Init(void)
00557 {
00558   GPIO_InitTypeDef  GPIO_InitStruct;
00559    
00560   /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */
00561   LCD_CS_GPIO_CLK_ENABLE();
00562   LCD_DC_GPIO_CLK_ENABLE();
00563   
00564   /* Configure LCD_CS_PIN pin: LCD Card CS pin */
00565   GPIO_InitStruct.Pin = LCD_CS_PIN;
00566   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00567   GPIO_InitStruct.Pull = GPIO_NOPULL;
00568   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00569   HAL_GPIO_Init(LCD_CS_GPIO_PORT, &GPIO_InitStruct);
00570 
00571   /* Configure LCD_DC_PIN pin: LCD Card DC pin */
00572   GPIO_InitStruct.Pin = LCD_DC_PIN;
00573   HAL_GPIO_Init(LCD_DC_GPIO_PORT, &GPIO_InitStruct);
00574   
00575   /* LCD chip select high */
00576   LCD_CS_HIGH();
00577   
00578   /* LCD SPI Config */
00579   SPIx_Init();
00580 }
00581 
00582 /**
00583   * @brief  Writes command to select the LCD register.
00584   * @param  LCDReg: Address of the selected register.
00585   */
00586 void LCD_IO_WriteReg(uint8_t LCDReg)
00587 {
00588   /* Reset LCD control line CS */
00589   LCD_CS_LOW();
00590   
00591   /* Set LCD data/command line DC to Low */
00592   LCD_DC_LOW();
00593     
00594   /* Send Command */
00595   SPIx_Write(LCDReg);
00596   
00597   /* Deselect : Chip Select high */
00598   LCD_CS_HIGH();
00599 }
00600 
00601 /**
00602   * @brief  Writes data to select the LCD register.
00603   *         This function must be used after st7735_WriteReg() function
00604   * @param  Data: data to write to the selected register.
00605   */
00606 void LCD_IO_WriteData(uint8_t Data)
00607 {
00608   /* Reset LCD control line CS */
00609   LCD_CS_LOW();
00610   
00611   /* Set LCD data/command line DC to High */
00612   LCD_DC_HIGH();
00613 
00614   /* Send Data */
00615   SPIx_Write(Data);
00616   
00617   /* Deselect : Chip Select high */
00618   LCD_CS_HIGH();
00619 }
00620 
00621 /**
00622   * @brief  Write register value.
00623   * @param  pData Pointer on the register value
00624   * @param  Size Size of byte to transmit to the register
00625   */
00626 void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size)
00627 {
00628   uint32_t counter = 0;
00629   __IO uint32_t data = 0;
00630   
00631   /* Reset LCD control line CS */
00632   LCD_CS_LOW();
00633   
00634   /* Set LCD data/command line DC to High */
00635   LCD_DC_HIGH();
00636 
00637   if (Size == 1)
00638   {
00639     /* Only 1 byte to be sent to LCD - general interface can be used */
00640     /* Send Data */
00641     SPIx_Write(*pData);
00642   }
00643   else
00644   {
00645     /* Several data should be sent in a raw */
00646     /* Direct SPI accesses for optimization */
00647     for (counter = Size; counter != 0; counter--)
00648     {
00649       while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
00650       {
00651       }  
00652       /* Need to invert bytes for LCD*/
00653       *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *(pData+1);
00654       
00655       while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE)
00656       {
00657       }  
00658       *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *pData;
00659       counter--;
00660       pData += 2;
00661     }
00662     
00663     /* Wait until the bus is ready before releasing Chip select */ 
00664     while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET)
00665     {
00666     }  
00667   } 
00668 
00669   /* Empty the Rx fifo */
00670   data = *(&hnucleo_Spi.Instance->DR);
00671   UNUSED(data);  /* Remove GNU warning */
00672 
00673   /* Deselect : Chip Select high */
00674   LCD_CS_HIGH();
00675 }
00676 
00677 /**
00678   * @brief  Wait for loop in ms.
00679   * @param  Delay in ms.
00680   */
00681 void LCD_Delay(uint32_t Delay)
00682 {
00683   HAL_Delay(Delay);
00684 }
00685 #endif /* HAL_SPI_MODULE_ENABLED */
00686 
00687 /******************************* ADC driver ********************************/
00688 #ifdef HAL_ADC_MODULE_ENABLED
00689 
00690 /**
00691   * @brief  Initializes ADC MSP.
00692   */
00693 static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
00694 {
00695   GPIO_InitTypeDef  GPIO_InitStruct;
00696   
00697   /*** Configure the GPIOs ***/  
00698   /* Enable GPIO clock */
00699   NUCLEO_ADCx_GPIO_CLK_ENABLE();
00700   
00701   /* Configure the selected ADC Channel as analog input */
00702   GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
00703   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
00704   GPIO_InitStruct.Pull = GPIO_NOPULL;
00705   HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &GPIO_InitStruct);
00706 
00707   /*** Configure the ADC peripheral ***/ 
00708   /* Enable ADC clock */
00709   NUCLEO_ADCx_CLK_ENABLE(); 
00710 }
00711 
00712 /**
00713   * @brief  DeInitializes ADC MSP.
00714   * @note ADC DeInit does not disable the GPIO clock
00715   */
00716 static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc)
00717 {
00718   GPIO_InitTypeDef  GPIO_InitStruct;
00719 
00720   /*** DeInit the ADC peripheral ***/ 
00721   /* Disable ADC clock */
00722   NUCLEO_ADCx_CLK_DISABLE(); 
00723 
00724   /* Configure the selected ADC Channel as analog input */
00725   GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
00726   HAL_GPIO_DeInit(NUCLEO_ADCx_GPIO_PORT, GPIO_InitStruct.Pin);
00727 
00728   /* Disable GPIO clock has to be done by the application*/
00729   /* NUCLEO_ADCx_GPIO_CLK_DISABLE(); */
00730 }
00731 
00732 /**
00733   * @brief  Initializes ADC HAL.
00734   */
00735 static void ADCx_Init(void)
00736 {
00737   if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
00738   {
00739     /* ADC Config */
00740     hnucleo_Adc.Instance                   = NUCLEO_ADCx;
00741     hnucleo_Adc.Init.ClockPrescaler        = ADC_CLOCKPRESCALER_PCLK_DIV4; /* (must not exceed 36MHz) */
00742     hnucleo_Adc.Init.Resolution            = ADC_RESOLUTION12b;
00743     hnucleo_Adc.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
00744     hnucleo_Adc.Init.ContinuousConvMode    = DISABLE;
00745     hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE;
00746     hnucleo_Adc.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE;
00747     hnucleo_Adc.Init.EOCSelection          = EOC_SINGLE_CONV;
00748     hnucleo_Adc.Init.NbrOfConversion       = 1;
00749     hnucleo_Adc.Init.DMAContinuousRequests = DISABLE;    
00750     
00751     ADCx_MspInit(&hnucleo_Adc);
00752     HAL_ADC_Init(&hnucleo_Adc);
00753   }
00754 }
00755 
00756 /**
00757   * @brief  Initializes ADC HAL.
00758   */
00759 static void ADCx_DeInit(void)
00760 {
00761   hnucleo_Adc.Instance   = NUCLEO_ADCx;
00762     
00763   HAL_ADC_DeInit(&hnucleo_Adc);
00764   ADCx_MspDeInit(&hnucleo_Adc);
00765 }
00766 
00767 /******************************* LINK JOYSTICK ********************************/
00768 
00769 /**
00770   * @brief  Configures joystick available on adafruit 1.8" TFT shield 
00771   *         managed through ADC to detect motion.
00772   * @retval Joystickstatus (0=> success, 1=> fail) 
00773   */
00774 uint8_t BSP_JOY_Init(void)
00775 {
00776   uint8_t status = HAL_ERROR;
00777    
00778   ADCx_Init();
00779    
00780   /* Select the ADC Channel to be converted */
00781   sConfig.Channel      = NUCLEO_ADCx_CHANNEL;
00782   sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
00783   sConfig.Rank         = 1;
00784   status = HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig);
00785   
00786   /* Return Joystick initialization status */
00787   return status;
00788 }
00789 
00790 /**
00791   * @brief  DeInit joystick GPIOs.
00792   * @note   JOY DeInit does not disable the Mfx, just set the Mfx pins in Off mode
00793   */
00794 void BSP_JOY_DeInit(void)
00795 {
00796   ADCx_DeInit();
00797 }
00798 
00799 /**
00800   * @brief  Returns the Joystick key pressed.
00801   * @note   To know which Joystick key is pressed we need to detect the voltage
00802   *         level on each key output
00803   *           - None  : 3.3 V / 4095
00804   *           - SEL   : 1.055 V / 1308
00805   *           - DOWN  : 0.71 V / 88
00806   *           - LEFT  : 3.0 V / 3720 
00807   *           - RIGHT : 0.595 V / 737
00808   *           - UP    : 1.65 V / 2046
00809   * @retval JOYState_TypeDef: Code of the Joystick key pressed.
00810   */
00811 JOYState_TypeDef BSP_JOY_GetState(void)
00812 {
00813   JOYState_TypeDef state;
00814   uint16_t  keyconvertedvalue = 0;
00815   
00816   /* Start the conversion process */
00817   HAL_ADC_Start(&hnucleo_Adc);
00818   
00819   /* Wait for the end of conversion */
00820   HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
00821   
00822   /* Check if the continuous conversion of regular channel is finished */
00823   if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_EOC_REG)
00824   {
00825     /* Get the converted value of regular channel */
00826     keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
00827   }
00828   
00829   if((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090))
00830   {
00831     state = JOY_UP;
00832   }
00833   else if((keyconvertedvalue > 680) && (keyconvertedvalue < 780))
00834   {
00835     state = JOY_RIGHT;
00836   }
00837   else if((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350))
00838   {
00839     state = JOY_SEL;
00840   }
00841   else if((keyconvertedvalue > 50) && (keyconvertedvalue < 130))
00842   {
00843     state = JOY_DOWN;
00844   }
00845   else if((keyconvertedvalue > 3680) && (keyconvertedvalue < 3760))
00846   {
00847     state = JOY_LEFT;
00848   }
00849   else
00850   {
00851     state = JOY_NONE;
00852   }
00853   
00854   /* Loop while a key is pressed */
00855   if(state != JOY_NONE)
00856   { 
00857     keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);  
00858   }
00859   /* Return the code of the Joystick key pressed */
00860   return state;
00861 }
00862 #endif /* HAL_ADC_MODULE_ENABLED */
00863 
00864 #endif /* ADAFRUIT_TFT_JOY_SD_ID802 */
00865 
00866 
00867 /**
00868   * @}
00869   */ 
00870 
00871 /**
00872   * @}
00873   */
00874 
00875 /**
00876   * @}
00877   */    
00878 
00879 /**
00880   * @}
00881   */ 
00882     
00883 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Jan 13 2016 13:58:20 for STM32F4xx_Nucleo_144 BSP User Manual by   doxygen 1.7.6.1