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