Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stm32f4xx_nucleo.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f4xx_nucleo.c 00004 * @author MCD Application Team 00005 * @version V1.1.0 00006 * @date 19-June-2014 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) 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 "stm32f4xx_nucleo.h" 00044 00045 /** @addtogroup BSP 00046 * @{ 00047 */ 00048 00049 /** @addtogroup STM32F4XX_NUCLEO 00050 * @{ 00051 */ 00052 00053 /** @addtogroup 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 00060 * @{ 00061 */ 00062 /** 00063 * @} 00064 */ 00065 00066 00067 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Defines 00068 * @{ 00069 */ 00070 00071 /** 00072 * @brief STM32F4xx NUCLEO BSP Driver version number V1.1.0 00073 */ 00074 #define __STM32F4xx_NUCLEO_BSP_VERSION_MAIN (0x01) /*!< [31:24] main version */ 00075 #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */ 00076 #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB2 (0x00) /*!< [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 00094 * @{ 00095 */ 00096 /** 00097 * @} 00098 */ 00099 00100 /** @defgroup 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 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 STM32F4XX_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 STM32F4XX_NUCLEO_LOW_LEVEL_Private_Functions 00156 * @{ 00157 */ 00158 00159 /** 00160 * @brief This method returns the STM32F4xx 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 __STM32F4xx_NUCLEO_BSP_VERSION; 00167 } 00168 00169 /** 00170 * @brief Configures LED GPIO. 00171 * @param Led: Specifies the Led to be configured. 00172 * This parameter can be one of following parameters: 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 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 00192 } 00193 00194 /** 00195 * @brief Turns selected LED On. 00196 * @param Led: Specifies the Led to be set on. 00197 * This parameter can be one of following parameters: 00198 * @arg LED2 00199 * @retval None 00200 */ 00201 void BSP_LED_On(Led_TypeDef Led) 00202 { 00203 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET); 00204 } 00205 00206 /** 00207 * @brief Turns selected LED Off. 00208 * @param Led: Specifies the Led to be set off. 00209 * This parameter can be one of following parameters: 00210 * @arg LED2 00211 * @retval None 00212 */ 00213 void BSP_LED_Off(Led_TypeDef Led) 00214 { 00215 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET); 00216 } 00217 00218 /** 00219 * @brief Toggles the selected LED. 00220 * @param Led: Specifies the Led to be toggled. 00221 * This parameter can be one of following parameters: 00222 * @arg LED2 00223 * @retval None 00224 */ 00225 void BSP_LED_Toggle(Led_TypeDef Led) 00226 { 00227 HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]); 00228 } 00229 00230 /** 00231 * @brief Configures Button GPIO and EXTI Line. 00232 * @param Button: Specifies the Button to be configured. 00233 * This parameter should be: BUTTON_KEY 00234 * @param ButtonMode: Specifies Button mode. 00235 * This parameter can be one of following parameters: 00236 * @arg BUTTON_MODE_GPIO: Button will be used as simple IO 00237 * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt 00238 * generation capability 00239 * @retval None 00240 */ 00241 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode) 00242 { 00243 GPIO_InitTypeDef GPIO_InitStruct; 00244 00245 /* Enable the BUTTON Clock */ 00246 BUTTONx_GPIO_CLK_ENABLE(Button); 00247 00248 if(ButtonMode == BUTTON_MODE_GPIO) 00249 { 00250 /* Configure Button pin as input */ 00251 GPIO_InitStruct.Pin = BUTTON_PIN[Button]; 00252 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 00253 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 00254 GPIO_InitStruct.Speed = GPIO_SPEED_FAST; 00255 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct); 00256 } 00257 00258 if(ButtonMode == 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.Mode = GPIO_MODE_IT_RISING; 00264 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct); 00265 00266 /* Enable and set Button EXTI Interrupt to the lowest priority */ 00267 HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00); 00268 HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button])); 00269 } 00270 } 00271 00272 /** 00273 * @brief Returns the selected Button state. 00274 * @param Button: Specifies the Button to be checked. 00275 * This parameter should be: BUTTON_KEY 00276 * @retval The Button GPIO pin value. 00277 */ 00278 uint32_t BSP_PB_GetState(Button_TypeDef Button) 00279 { 00280 return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]); 00281 } 00282 00283 /****************************************************************************** 00284 BUS OPERATIONS 00285 *******************************************************************************/ 00286 /** 00287 * @brief Initializes SPI MSP. 00288 * @param None 00289 * @retval None 00290 */ 00291 static void SPIx_MspInit(SPI_HandleTypeDef *hspi) 00292 { 00293 GPIO_InitTypeDef GPIO_InitStruct; 00294 00295 /*** Configure the GPIOs ***/ 00296 /* Enable GPIO clock */ 00297 NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE(); 00298 NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE(); 00299 00300 /* Configure SPI SCK */ 00301 GPIO_InitStruct.Pin = NUCLEO_SPIx_SCK_PIN; 00302 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 00303 GPIO_InitStruct.Pull = GPIO_PULLUP; 00304 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; 00305 GPIO_InitStruct.Alternate = NUCLEO_SPIx_SCK_AF; 00306 HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct); 00307 00308 /* Configure SPI MISO and MOSI */ 00309 GPIO_InitStruct.Pin = NUCLEO_SPIx_MOSI_PIN; 00310 GPIO_InitStruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF; 00311 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 00312 HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct); 00313 00314 GPIO_InitStruct.Pin = NUCLEO_SPIx_MISO_PIN; 00315 HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct); 00316 00317 /*** Configure the SPI peripheral ***/ 00318 /* Enable SPI clock */ 00319 NUCLEO_SPIx_CLK_ENABLE(); 00320 } 00321 00322 /** 00323 * @brief Initializes SPI HAL. 00324 * @param None 00325 * @retval None 00326 */ 00327 static void SPIx_Init(void) 00328 { 00329 if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET) 00330 { 00331 /* SPI Config */ 00332 hnucleo_Spi.Instance = NUCLEO_SPIx; 00333 /* SPI baudrate is set to 12,5 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 100/8 = 12,5 MHz) 00334 to verify these constraints: 00335 - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read 00336 Since the provided driver doesn't use read capability from LCD, only constraint 00337 on write baudrate is considered. 00338 - SD card SPI interface max baudrate is 25MHz for write/read 00339 - PCLK2 max frequency is 100 MHz 00340 */ 00341 hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; 00342 hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES; 00343 hnucleo_Spi.Init.CLKPhase = SPI_PHASE_2EDGE; 00344 hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_HIGH; 00345 hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; 00346 hnucleo_Spi.Init.CRCPolynomial = 7; 00347 hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT; 00348 hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB; 00349 hnucleo_Spi.Init.NSS = SPI_NSS_SOFT; 00350 hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLED; 00351 hnucleo_Spi.Init.Mode = SPI_MODE_MASTER; 00352 00353 SPIx_MspInit(&hnucleo_Spi); 00354 HAL_SPI_Init(&hnucleo_Spi); 00355 } 00356 } 00357 00358 /** 00359 * @brief SPI Read 4 bytes from device. 00360 * @param None 00361 * @retval Read data 00362 */ 00363 static uint32_t SPIx_Read(void) 00364 { 00365 HAL_StatusTypeDef status = HAL_OK; 00366 uint32_t readvalue = 0; 00367 uint32_t writevalue = 0xFFFFFFFF; 00368 00369 status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &writevalue, (uint8_t*) &readvalue, 1, SpixTimeout); 00370 00371 /* Check the communication status */ 00372 if(status != HAL_OK) 00373 { 00374 /* Execute user timeout callback */ 00375 SPIx_Error(); 00376 } 00377 00378 return readvalue; 00379 } 00380 00381 /** 00382 * @brief SPI Write a byte to device. 00383 * @param Value: value to be written 00384 * @retval None 00385 */ 00386 static void SPIx_Write(uint8_t Value) 00387 { 00388 HAL_StatusTypeDef status = HAL_OK; 00389 00390 status = HAL_SPI_Transmit(&hnucleo_Spi, (uint8_t*) &Value, 1, SpixTimeout); 00391 00392 /* Check the communication status */ 00393 if(status != HAL_OK) 00394 { 00395 /* Execute user timeout callback */ 00396 SPIx_Error(); 00397 } 00398 } 00399 00400 /** 00401 * @brief SPI error treatment function. 00402 * @param None 00403 * @retval None 00404 */ 00405 static void SPIx_Error (void) 00406 { 00407 /* De-initialize the SPI communication BUS */ 00408 HAL_SPI_DeInit(&hnucleo_Spi); 00409 00410 /* Re-Initiaize the SPI communication BUS */ 00411 SPIx_Init(); 00412 } 00413 00414 /****************************************************************************** 00415 LINK OPERATIONS 00416 *******************************************************************************/ 00417 00418 /********************************* LINK SD ************************************/ 00419 /** 00420 * @brief Initializes the SD Card and put it into StandBy State (Ready for 00421 * data transfer). 00422 * @param None 00423 * @retval None 00424 */ 00425 void SD_IO_Init(void) 00426 { 00427 GPIO_InitTypeDef GPIO_InitStruct; 00428 uint8_t counter; 00429 00430 /* SD_CS_GPIO Periph clock enable */ 00431 SD_CS_GPIO_CLK_ENABLE(); 00432 00433 /* Configure SD_CS_PIN pin: SD Card CS pin */ 00434 GPIO_InitStruct.Pin = SD_CS_PIN; 00435 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 00436 GPIO_InitStruct.Pull = GPIO_PULLUP; 00437 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; 00438 HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct); 00439 00440 /*------------Put SD in SPI mode--------------*/ 00441 /* SD SPI Config */ 00442 SPIx_Init(); 00443 00444 /* SD chip select high */ 00445 SD_CS_HIGH(); 00446 00447 /* Send dummy byte 0xFF, 10 times with CS high */ 00448 /* Rise CS and MOSI for 80 clocks cycles */ 00449 for (counter = 0; counter <= 9; counter++) 00450 { 00451 /* Send dummy byte 0xFF */ 00452 SD_IO_WriteByte(SD_DUMMY_BYTE); 00453 } 00454 } 00455 00456 /** 00457 * @brief Writes a byte on the SD. 00458 * @param Data: byte to send. 00459 * @retval None 00460 */ 00461 void SD_IO_WriteByte(uint8_t Data) 00462 { 00463 /* Send the byte */ 00464 SPIx_Write(Data); 00465 } 00466 00467 /** 00468 * @brief Reads a byte from the SD. 00469 * @param None 00470 * @retval The received byte. 00471 */ 00472 uint8_t SD_IO_ReadByte(void) 00473 { 00474 uint8_t data = 0; 00475 00476 /* Get the received data */ 00477 data = SPIx_Read(); 00478 00479 /* Return the shifted data */ 00480 return data; 00481 } 00482 00483 /** 00484 * @brief Sends 5 bytes command to the SD card and get response. 00485 * @param Cmd: The user expected command to send to SD card. 00486 * @param Arg: The command argument 00487 * @param Crc: The CRC 00488 * @param Response: Expected response from the SD card 00489 * @retval HAL_StatusTypeDef HAL Status 00490 */ 00491 HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response) 00492 { 00493 uint32_t counter = 0x00; 00494 uint8_t frame[6]; 00495 00496 /* Prepare Frame to send */ 00497 frame[0] = (Cmd | 0x40); /* Construct byte 1 */ 00498 frame[1] = (uint8_t)(Arg >> 24); /* Construct byte 2 */ 00499 frame[2] = (uint8_t)(Arg >> 16); /* Construct byte 3 */ 00500 frame[3] = (uint8_t)(Arg >> 8); /* Construct byte 4 */ 00501 frame[4] = (uint8_t)(Arg); /* Construct byte 5 */ 00502 frame[5] = (Crc); /* Construct byte 6 */ 00503 00504 /* SD chip select low */ 00505 SD_CS_LOW(); 00506 00507 /* Send Frame */ 00508 for (counter = 0; counter < 6; counter++) 00509 { 00510 SD_IO_WriteByte(frame[counter]); /* Send the Cmd bytes */ 00511 } 00512 00513 if(Response != SD_NO_RESPONSE_EXPECTED) 00514 { 00515 return SD_IO_WaitResponse(Response); 00516 } 00517 00518 return HAL_OK; 00519 } 00520 00521 /** 00522 * @brief Waits response from the SD card 00523 * @param Response: Expected response from the SD card 00524 * @retval HAL_StatusTypeDef HAL Status 00525 */ 00526 HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response) 00527 { 00528 uint32_t timeout = 0xFFFF; 00529 00530 /* Check if response is got or a timeout is happen */ 00531 while ((SD_IO_ReadByte() != Response) && timeout) 00532 { 00533 timeout--; 00534 } 00535 00536 if (timeout == 0) 00537 { 00538 /* After time out */ 00539 return HAL_TIMEOUT; 00540 } 00541 else 00542 { 00543 /* Right response got */ 00544 return HAL_OK; 00545 } 00546 } 00547 00548 /** 00549 * @brief Sends dummy byte with CS High. 00550 * @param None 00551 * @retval None 00552 */ 00553 void SD_IO_WriteDummy(void) 00554 { 00555 /* SD chip select high */ 00556 SD_CS_HIGH(); 00557 00558 /* Send Dummy byte 0xFF */ 00559 SD_IO_WriteByte(SD_DUMMY_BYTE); 00560 } 00561 00562 /********************************* LINK LCD ***********************************/ 00563 /** 00564 * @brief Initializes the LCD. 00565 * @param None 00566 * @retval None 00567 */ 00568 void LCD_IO_Init(void) 00569 { 00570 GPIO_InitTypeDef GPIO_InitStruct; 00571 00572 /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */ 00573 LCD_CS_GPIO_CLK_ENABLE(); 00574 LCD_DC_GPIO_CLK_ENABLE(); 00575 00576 /* Configure LCD_CS_PIN pin: LCD Card CS pin */ 00577 GPIO_InitStruct.Pin = LCD_CS_PIN; 00578 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 00579 GPIO_InitStruct.Pull = GPIO_NOPULL; 00580 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; 00581 HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct); 00582 00583 /* Configure LCD_DC_PIN pin: LCD Card DC pin */ 00584 GPIO_InitStruct.Pin = LCD_DC_PIN; 00585 HAL_GPIO_Init(LCD_DC_GPIO_PORT, &GPIO_InitStruct); 00586 00587 /* LCD chip select high */ 00588 LCD_CS_HIGH(); 00589 00590 /* LCD SPI Config */ 00591 SPIx_Init(); 00592 } 00593 00594 /** 00595 * @brief Writes command to select the LCD register. 00596 * @param LCDReg: Address of the selected register. 00597 * @retval None 00598 */ 00599 void LCD_IO_WriteReg(uint8_t LCDReg) 00600 { 00601 /* Reset LCD control line CS */ 00602 LCD_CS_LOW(); 00603 00604 /* Set LCD data/command line DC to Low */ 00605 LCD_DC_LOW(); 00606 00607 /* Send Command */ 00608 SPIx_Write(LCDReg); 00609 00610 /* Deselect : Chip Select high */ 00611 LCD_CS_HIGH(); 00612 } 00613 00614 /** 00615 * @brief Writes data to select the LCD register. 00616 * This function must be used after st7735_WriteReg() function 00617 * @param Data: data to write to the selected register. 00618 * @retval None 00619 */ 00620 void LCD_IO_WriteData(uint8_t Data) 00621 { 00622 /* Reset LCD control line CS */ 00623 LCD_CS_LOW(); 00624 00625 /* Set LCD data/command line DC to High */ 00626 LCD_DC_HIGH(); 00627 00628 /* Send Data */ 00629 SPIx_Write(Data); 00630 00631 /* Deselect : Chip Select high */ 00632 LCD_CS_HIGH(); 00633 } 00634 00635 /** 00636 * @brief Wait for loop in ms. 00637 * @param Delay in ms. 00638 * @retval None 00639 */ 00640 void LCD_Delay(uint32_t Delay) 00641 { 00642 HAL_Delay(Delay); 00643 } 00644 00645 /******************************* LINK JOYSTICK ********************************/ 00646 /** 00647 * @brief Initializes ADC MSP. 00648 * @param None 00649 * @retval None 00650 */ 00651 static void ADCx_MspInit(ADC_HandleTypeDef *hadc) 00652 { 00653 GPIO_InitTypeDef GPIO_InitStruct; 00654 00655 /*** Configure the GPIOs ***/ 00656 /* Enable GPIO clock */ 00657 NUCLEO_ADCx_GPIO_CLK_ENABLE(); 00658 00659 /* Configure the selected ADC Channel as analog input */ 00660 GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ; 00661 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 00662 GPIO_InitStruct.Pull = GPIO_NOPULL; 00663 HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &GPIO_InitStruct); 00664 00665 /*** Configure the ADC peripheral ***/ 00666 /* Enable ADC clock */ 00667 NUCLEO_ADCx_CLK_ENABLE(); 00668 } 00669 00670 /** 00671 * @brief Initializes ADC HAL. 00672 * @param None 00673 * @retval None 00674 */ 00675 static void ADCx_Init(void) 00676 { 00677 if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET) 00678 { 00679 /* ADC Config */ 00680 hnucleo_Adc.Instance = NUCLEO_ADCx; 00681 hnucleo_Adc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4; /* (must not exceed 36MHz) */ 00682 hnucleo_Adc.Init.Resolution = ADC_RESOLUTION12b; 00683 hnucleo_Adc.Init.DataAlign = ADC_DATAALIGN_RIGHT; 00684 hnucleo_Adc.Init.ContinuousConvMode = DISABLE; 00685 hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE; 00686 hnucleo_Adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; 00687 hnucleo_Adc.Init.EOCSelection = EOC_SINGLE_CONV; 00688 hnucleo_Adc.Init.NbrOfConversion = 1; 00689 hnucleo_Adc.Init.DMAContinuousRequests = DISABLE; 00690 00691 ADCx_MspInit(&hnucleo_Adc); 00692 HAL_ADC_Init(&hnucleo_Adc); 00693 } 00694 } 00695 00696 /** 00697 * @brief Configures joystick available on adafruit 1.8" TFT shield 00698 * managed through ADC to detect motion. 00699 * @param None 00700 * @retval Joystickstatus (0=> success, 1=> fail) 00701 */ 00702 uint8_t BSP_JOY_Init(void) 00703 { 00704 uint8_t status = 1; 00705 00706 ADCx_Init(); 00707 00708 /* Select the ADC Channel to be converted */ 00709 sConfig.Channel = NUCLEO_ADCx_CHANNEL; 00710 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; 00711 sConfig.Rank = 1; 00712 status = HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig); 00713 00714 /* Return Joystick initialization status */ 00715 return status; 00716 } 00717 00718 /** 00719 * @brief Returns the Joystick key pressed. 00720 * @note To know which Joystick key is pressed we need to detect the voltage 00721 * level on each key output 00722 * - None : 3.3 V / 4095 00723 * - SEL : 1.055 V / 1308 00724 * - DOWN : 0.71 V / 88 00725 * - LEFT : 3.0 V / 3720 00726 * - RIGHT : 0.595 V / 737 00727 * - UP : 1.65 V / 2046 00728 * @retval JOYState_TypeDef: Code of the Joystick key pressed. 00729 */ 00730 JOYState_TypeDef BSP_JOY_GetState(void) 00731 { 00732 JOYState_TypeDef state; 00733 uint16_t keyconvertedvalue = 0; 00734 00735 /* Start the conversion process */ 00736 HAL_ADC_Start(&hnucleo_Adc); 00737 00738 /* Wait for the end of conversion */ 00739 HAL_ADC_PollForConversion(&hnucleo_Adc, 10); 00740 00741 /* Check if the continous conversion of regular channel is finished */ 00742 if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_EOC_REG) 00743 { 00744 /* Get the converted value of regular channel */ 00745 keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc); 00746 } 00747 00748 if((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090)) 00749 { 00750 state = JOY_UP; 00751 } 00752 else if((keyconvertedvalue > 680) && (keyconvertedvalue < 780)) 00753 { 00754 state = JOY_RIGHT; 00755 } 00756 else if((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350)) 00757 { 00758 state = JOY_SEL; 00759 } 00760 else if((keyconvertedvalue > 50) && (keyconvertedvalue < 130)) 00761 { 00762 state = JOY_DOWN; 00763 } 00764 else if((keyconvertedvalue > 3680) && (keyconvertedvalue < 3760)) 00765 { 00766 state = JOY_LEFT; 00767 } 00768 else 00769 { 00770 state = JOY_NONE; 00771 } 00772 00773 /* Loop while a key is pressed */ 00774 if(state != JOY_NONE) 00775 { 00776 keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc); 00777 } 00778 /* Return the code of the Joystick key pressed */ 00779 return state; 00780 } 00781 00782 /** 00783 * @} 00784 */ 00785 00786 /** 00787 * @} 00788 */ 00789 00790 /** 00791 * @} 00792 */ 00793 00794 /** 00795 * @} 00796 */ 00797 00798 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 20:10:41 by
