AudioRecord and FFT/MSE comparison. Call AudioRecord_demo for control record and AudioSample for subsequent recordings.

Dependencies:   CMSIS_DSP_401 STM32L4xx_HAL_Driver

Fork of OneHopeOnePrayer by Senior Design: Sound Monitor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_nucleo.c Source File

stm32l4xx_nucleo.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_nucleo.c
00004   * @author  MCD Application Team
00005   * @version V2.0.0
00006   * @date    16-September-2015
00007   * @brief   This file provides set of firmware functions to manage:
00008   *          - LEDs and push-button available on STM32L4XX-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) 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 "stm32l4xx_nucleo.h"
00044 
00045 /** @addtogroup BSP
00046   * @{
00047   */
00048 
00049 /** @defgroup STM32L4XX_NUCLEO STM32L476RG-Nucleo
00050   * @brief This file provides set of firmware functions to manage Leds and push-button
00051   *        available on STM32L4XX-Nucleo Kit from STMicroelectronics.
00052   *        It provides also LCD, joystick and uSD functions to communicate with
00053   *        Adafruit 1.8" TFT LCD shield (reference ID 802)
00054   * @{
00055   */
00056 
00057 
00058 /** @defgroup STM32L4XX_NUCLEO_Private_Defines Private Defines
00059   * @{
00060   */
00061 
00062 /**
00063   * @brief STM32L476RG NUCLEO BSP Driver version V2.0.0
00064   */
00065 #define __STM32L4XX_NUCLEO_BSP_VERSION_MAIN   (0x02) /*!< [31:24] main version */
00066 #define __STM32L4XX_NUCLEO_BSP_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */
00067 #define __STM32L4XX_NUCLEO_BSP_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */
00068 #define __STM32L4XX_NUCLEO_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
00069 #define __STM32L4XX_NUCLEO_BSP_VERSION       ((__STM32L4XX_NUCLEO_BSP_VERSION_MAIN << 24)\
00070                                              |(__STM32L4XX_NUCLEO_BSP_VERSION_SUB1 << 16)\
00071                                              |(__STM32L4XX_NUCLEO_BSP_VERSION_SUB2 << 8 )\
00072                                              |(__STM32L4XX_NUCLEO_BSP_VERSION_RC))
00073 
00074 /**
00075   * @brief LINK SD Card
00076   */
00077 #define SD_DUMMY_BYTE            0xFF
00078 #define SD_NO_RESPONSE_EXPECTED  0x80
00079 
00080 /**
00081   * @}
00082   */
00083 
00084 
00085 /** @defgroup STM32L4XX_NUCLEO_Private_Variables Exported Variables
00086   * @{
00087   */
00088 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
00089 
00090 const uint16_t GPIO_PIN[LEDn] = {LED2_PIN};
00091 
00092 GPIO_TypeDef*  BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT};
00093 const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN};
00094 const uint16_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn};
00095 
00096 /**
00097  * @brief BUS variables
00098  */
00099 
00100 #ifdef HAL_SPI_MODULE_ENABLED
00101 uint32_t hnucleo_SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX;        /*<! Value of Timeout when SPI communication fails */
00102 static SPI_HandleTypeDef hnucleo_Spi;
00103 #endif /* HAL_SPI_MODULE_ENABLED */
00104 
00105 #ifdef HAL_ADC_MODULE_ENABLED
00106 static ADC_HandleTypeDef hnucleo_Adc;
00107 /* ADC channel configuration structure declaration */
00108 static ADC_ChannelConfTypeDef hnucleo_AdcChannelConfig;
00109 #endif /* HAL_ADC_MODULE_ENABLED */
00110 
00111 /**
00112   * @}
00113   */
00114 
00115 /** @defgroup STM32L4XX_NUCLEO_Private_Functions Private Functions
00116   * @{
00117   */
00118 #ifdef HAL_SPI_MODULE_ENABLED
00119 static void               SPIx_Init(void);
00120 static void               SPIx_Write(uint8_t Value);
00121 static void               SPIx_Error (void);
00122 static void               SPIx_MspInit(void);
00123 #endif /* HAL_SPI_MODULE_ENABLED */
00124 
00125 #ifdef HAL_ADC_MODULE_ENABLED
00126 static HAL_StatusTypeDef  ADCx_Init(void);
00127 static void               ADCx_MspInit(ADC_HandleTypeDef *hadc);
00128 #endif /* HAL_ADC_MODULE_ENABLED */
00129 
00130 #ifdef HAL_SPI_MODULE_ENABLED
00131 /* SD IO functions */
00132 void                      SD_IO_Init(void);
00133 void                      SD_IO_CSState(uint8_t state);
00134 void                      SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength);
00135 uint8_t                   SD_IO_WriteByte(uint8_t Data);
00136 
00137 /* LCD IO functions */
00138 void                      LCD_IO_Init(void);
00139 void                      LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
00140 void                      LCD_IO_WriteReg(uint8_t LCDReg);
00141 void                      LCD_Delay(uint32_t delay);
00142 #endif /* HAL_SPI_MODULE_ENABLED */
00143 /**
00144   * @}
00145   */
00146 
00147 /** @defgroup STM32L4XX_NUCLEO_Exported_Functions Exported Functions
00148   * @{
00149   */
00150 
00151 /**
00152   * @brief  This method returns the STM32L4XX NUCLEO BSP Driver revision
00153   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
00154   */
00155 uint32_t BSP_GetVersion(void)
00156 {
00157     return __STM32L4XX_NUCLEO_BSP_VERSION;
00158 }
00159 
00160 /** @defgroup STM32L4XX_NUCLEO_LED_Functions LED Functions
00161   * @{
00162   */
00163 
00164 /**
00165   * @brief  Configures LED GPIO.
00166   * @param  Led: LED to be configured.
00167   *          This parameter can be one of the following values:
00168   *            @arg  LED2
00169   * @retval None
00170   */
00171 void BSP_LED_Init(Led_TypeDef Led)
00172 {
00173     GPIO_InitTypeDef  gpioinitstruct = {0};
00174 
00175     /* Enable the GPIO_LED Clock */
00176     LEDx_GPIO_CLK_ENABLE(Led);
00177 
00178     /* Configure the GPIO_LED pin */
00179     gpioinitstruct.Pin   = GPIO_PIN[Led];
00180     gpioinitstruct.Mode  = GPIO_MODE_OUTPUT_PP;
00181     gpioinitstruct.Pull  = GPIO_NOPULL;
00182     gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH;
00183     HAL_GPIO_Init(GPIO_PORT[Led], &gpioinitstruct);
00184 }
00185 
00186 /**
00187   * @brief  Turns selected LED On.
00188   * @param  Led: Specifies the Led to be set on.
00189   *   This parameter can be one of following parameters:
00190   *            @arg  LED2
00191   * @retval None
00192   */
00193 void BSP_LED_On(Led_TypeDef Led)
00194 {
00195     HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
00196 }
00197 
00198 /**
00199   * @brief  Turns selected LED Off.
00200   * @param  Led: Specifies the Led to be set off.
00201   *   This parameter can be one of following parameters:
00202   *            @arg  LED2
00203   * @retval None
00204   */
00205 void BSP_LED_Off(Led_TypeDef Led)
00206 {
00207     HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
00208 }
00209 
00210 /**
00211   * @brief  Toggles the selected LED.
00212   * @param  Led: Specifies the Led to be toggled.
00213   *   This parameter can be one of following parameters:
00214   *            @arg  LED2
00215   * @retval None
00216   */
00217 void BSP_LED_Toggle(Led_TypeDef Led)
00218 {
00219     HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
00220 }
00221 
00222 /**
00223   * @}
00224   */
00225 
00226 /** @defgroup STM32L4XX_NUCLEO_BUTTON_Functions BUTTON Functions
00227   * @{
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_USER
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 gpioinitstruct = {0};
00244 
00245     /* Enable the BUTTON Clock */
00246     BUTTONx_GPIO_CLK_ENABLE(Button);
00247 
00248     if (ButtonMode == BUTTON_MODE_GPIO) {
00249         /* Configure Button pin as input */
00250         gpioinitstruct.Pin    = BUTTON_PIN[Button];
00251         gpioinitstruct.Mode   = GPIO_MODE_INPUT;
00252         gpioinitstruct.Pull   = GPIO_NOPULL;
00253         gpioinitstruct.Speed  = GPIO_SPEED_FREQ_HIGH;
00254 
00255         HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct);
00256     } else if(ButtonMode == BUTTON_MODE_EXTI) {
00257         /* Configure Button pin as input with External interrupt */
00258         gpioinitstruct.Pin    = BUTTON_PIN[Button];
00259         gpioinitstruct.Mode   = GPIO_MODE_IT_FALLING;
00260         gpioinitstruct.Pull   = GPIO_NOPULL;
00261         gpioinitstruct.Speed  = GPIO_SPEED_FREQ_HIGH;
00262         HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct);
00263 
00264         /* Enable and set Button EXTI Interrupt to the lowest priority */
00265         HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
00266         HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00267     }
00268 }
00269 
00270 /**
00271   * @brief  Returns the selected Button state.
00272   * @param  Button: Specifies the Button to be checked.
00273   *   This parameter should be: BUTTON_USER
00274   * @retval Button state.
00275   */
00276 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00277 {
00278     return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00279 }
00280 
00281 #ifdef HAL_ADC_MODULE_ENABLED
00282 /**
00283   * @brief  Configures joystick available on adafruit 1.8" TFT shield
00284   *         managed through ADC to detect motion.
00285   * @retval Joystickstatus (0=> success, 1=> fail)
00286   */
00287 uint8_t BSP_JOY_Init(void)
00288 {
00289     if (ADCx_Init() != HAL_OK) {
00290         return (uint8_t) HAL_ERROR;
00291     }
00292 
00293     /* Select Channel 15 to be converted */
00294     hnucleo_AdcChannelConfig.Channel       = ADC_CHANNEL_15;
00295     hnucleo_AdcChannelConfig.SamplingTime  = ADC_SAMPLETIME_24CYCLES_5;
00296     hnucleo_AdcChannelConfig.Rank          = 1;
00297     hnucleo_AdcChannelConfig.SingleDiff    = ADC_SINGLE_ENDED;
00298     hnucleo_AdcChannelConfig.OffsetNumber  = ADC_OFFSET_NONE;
00299 
00300     /* Return Joystick initialization status */
00301     return (uint8_t) HAL_ADC_ConfigChannel(&hnucleo_Adc, &hnucleo_AdcChannelConfig);
00302 }
00303 
00304 /**
00305   * @brief  Returns the Joystick key pressed.
00306   * @note   To know which Joystick key is pressed we need to detect the voltage
00307   *         level on each key output
00308   *           - None  : 3.3 V / 4095
00309   *           - SEL   : 1.055 V / 1308
00310   *           - DOWN  : 0.71 V / 88
00311   *           - LEFT  : 3.0 V / 3720
00312   *           - RIGHT : 0.595 V / 737
00313   *           - UP    : 1.65 V / 2046
00314   * @retval JOYState_TypeDef: Code of the Joystick key pressed.
00315   */
00316 JOYState_TypeDef BSP_JOY_GetState(void)
00317 {
00318     JOYState_TypeDef state = JOY_NONE;
00319     uint16_t  keyconvertedvalue = 0;
00320 
00321     /* Start the conversion process */
00322     HAL_ADC_Start(&hnucleo_Adc);
00323 
00324     /* Wait for the end of conversion */
00325     HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
00326 
00327     /* Check if the continous conversion of regular channel is finished */
00328     if(HAL_ADC_GetState(&hnucleo_Adc) & HAL_ADC_STATE_REG_EOC) {
00329         /* Get the converted value of regular channel */
00330         keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
00331     }
00332 
00333     if((keyconvertedvalue > 1980) && (keyconvertedvalue < 2120)) {
00334         state = JOY_UP;
00335     } else if((keyconvertedvalue > 630) && (keyconvertedvalue < 830)) {
00336         state = JOY_RIGHT;
00337     } else if((keyconvertedvalue > 1210) && (keyconvertedvalue < 1410)) {
00338         state = JOY_SEL;
00339     } else if((keyconvertedvalue > 20) && (keyconvertedvalue < 160)) {
00340         state = JOY_DOWN;
00341     } else if((keyconvertedvalue > 3620) && (keyconvertedvalue < 3820)) {
00342         state = JOY_LEFT;
00343     } else {
00344         state = JOY_NONE;
00345     }
00346 
00347     /* Return the code of the Joystick key pressed*/
00348     return state;
00349 }
00350 
00351 #endif /* HAL_ADC_MODULE_ENABLED */
00352 
00353 /**
00354   * @}
00355   */
00356 
00357 /**
00358   * @}
00359   */
00360 
00361 /** @addtogroup STM32L4XX_NUCLEO_Private_Functions
00362   * @{
00363   */
00364 
00365 #ifdef HAL_SPI_MODULE_ENABLED
00366 /******************************************************************************
00367                             BUS OPERATIONS
00368 *******************************************************************************/
00369 /**
00370   * @brief  Initialize SPI MSP.
00371   * @retval None
00372   */
00373 static void SPIx_MspInit(void)
00374 {
00375     GPIO_InitTypeDef  gpioinitstruct = {0};
00376 
00377     /*** Configure the GPIOs ***/
00378     /* Enable GPIO clock */
00379     NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
00380     NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
00381 
00382     /* Configure SPI SCK */
00383     gpioinitstruct.Pin        = NUCLEO_SPIx_SCK_PIN;
00384     gpioinitstruct.Mode       = GPIO_MODE_AF_PP;
00385     gpioinitstruct.Pull       = GPIO_PULLUP;
00386     gpioinitstruct.Speed      = GPIO_SPEED_FREQ_VERY_HIGH;
00387     gpioinitstruct.Alternate  = NUCLEO_SPIx_SCK_AF;
00388     HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &gpioinitstruct);
00389 
00390     /* Configure SPI MISO and MOSI */
00391     gpioinitstruct.Pin        = NUCLEO_SPIx_MOSI_PIN;
00392     gpioinitstruct.Alternate  = NUCLEO_SPIx_MISO_MOSI_AF;
00393     gpioinitstruct.Pull       = GPIO_PULLDOWN;
00394     HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &gpioinitstruct);
00395 
00396     gpioinitstruct.Pin        = NUCLEO_SPIx_MISO_PIN;
00397     HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &gpioinitstruct);
00398 
00399     /*** Configure the SPI peripheral ***/
00400     /* Enable SPI clock */
00401     NUCLEO_SPIx_CLK_ENABLE();
00402 }
00403 
00404 /**
00405   * @brief  Initialize SPI HAL.
00406   * @retval None
00407   */
00408 static void SPIx_Init(void)
00409 {
00410     if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET) {
00411         /* SPI Config */
00412         hnucleo_Spi.Instance = NUCLEO_SPIx;
00413         /* SPI baudrate is set to 8 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 32/4 = 8 MHz)
00414          to verify these constraints:
00415             - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
00416               Since the provided driver doesn't use read capability from LCD, only constraint
00417               on write baudrate is considered.
00418             - SD card SPI interface max baudrate is 25MHz for write/read
00419             - PCLK2 max frequency is 32 MHz
00420          */
00421         hnucleo_Spi.Init.BaudRatePrescaler  = SPI_BAUDRATEPRESCALER_4;
00422         hnucleo_Spi.Init.Direction          = SPI_DIRECTION_2LINES;
00423         hnucleo_Spi.Init.CLKPhase           = SPI_PHASE_2EDGE;
00424         hnucleo_Spi.Init.CLKPolarity        = SPI_POLARITY_HIGH;
00425         hnucleo_Spi.Init.CRCCalculation     = SPI_CRCCALCULATION_DISABLE;
00426         hnucleo_Spi.Init.CRCPolynomial      = 7;
00427         hnucleo_Spi.Init.CRCLength          = SPI_CRC_LENGTH_DATASIZE;
00428         hnucleo_Spi.Init.DataSize           = SPI_DATASIZE_8BIT;
00429         hnucleo_Spi.Init.FirstBit           = SPI_FIRSTBIT_MSB;
00430         hnucleo_Spi.Init.NSS                = SPI_NSS_SOFT;
00431         hnucleo_Spi.Init.NSSPMode           = SPI_NSS_PULSE_DISABLE;
00432         hnucleo_Spi.Init.TIMode             = SPI_TIMODE_DISABLE;
00433         hnucleo_Spi.Init.Mode               = SPI_MODE_MASTER;
00434 
00435         SPIx_MspInit();
00436         HAL_SPI_Init(&hnucleo_Spi);
00437     }
00438 }
00439 
00440 /**
00441   * @brief  SPI Write byte(s) to device
00442   * @param  DataIn: Pointer to data buffer to write
00443   * @param  DataOut: Pointer to data buffer for read data
00444   * @param  DataLength: number of bytes to write
00445   * @retval None
00446   */
00447 static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
00448 {
00449     HAL_StatusTypeDef status = HAL_OK;
00450     status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) DataIn, DataOut, DataLength, hnucleo_SpixTimeout);
00451 
00452     /* Check the communication status */
00453     if(status != HAL_OK) {
00454         /* Execute user timeout callback */
00455         SPIx_Error();
00456     }
00457 }
00458 
00459 /**
00460   * @brief  SPI Write a byte to device
00461   * @param  Value: value to be written
00462   * @retval None
00463   */
00464 static void SPIx_Write(uint8_t Value)
00465 {
00466     HAL_StatusTypeDef status = HAL_OK;
00467     uint8_t data;
00468 
00469     status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &Value, &data, 1, hnucleo_SpixTimeout);
00470 
00471     /* Check the communication status */
00472     if(status != HAL_OK) {
00473         /* Execute user timeout callback */
00474         SPIx_Error();
00475     }
00476 }
00477 
00478 /**
00479   * @brief  SPI error treatment function
00480   * @retval None
00481   */
00482 static void SPIx_Error (void)
00483 {
00484     /* De-initialize the SPI communication BUS */
00485     HAL_SPI_DeInit(&hnucleo_Spi);
00486 
00487     /* Re-Initiaize the SPI communication BUS */
00488     SPIx_Init();
00489 }
00490 
00491 /******************************************************************************
00492                             LINK OPERATIONS
00493 *******************************************************************************/
00494 
00495 /********************************* LINK SD ************************************/
00496 /**
00497   * @brief  Initialize the SD Card and put it into StandBy State (Ready for
00498   *         data transfer).
00499   * @retval None
00500   */
00501 void SD_IO_Init(void)
00502 {
00503     GPIO_InitTypeDef  gpioinitstruct = {0};
00504     uint8_t counter = 0;
00505 
00506     /* SD_CS_GPIO Periph clock enable */
00507     SD_CS_GPIO_CLK_ENABLE();
00508 
00509     /* Configure SD_CS_PIN pin: SD Card CS pin */
00510     gpioinitstruct.Pin    = SD_CS_PIN;
00511     gpioinitstruct.Mode   = GPIO_MODE_OUTPUT_PP;
00512     gpioinitstruct.Pull   = GPIO_PULLUP;
00513     gpioinitstruct.Speed  = GPIO_SPEED_FREQ_VERY_HIGH;
00514     HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
00515 
00516     /* Configure LCD_CS_PIN pin: LCD Card CS pin */
00517     gpioinitstruct.Pin   = LCD_CS_PIN;
00518     gpioinitstruct.Mode  = GPIO_MODE_OUTPUT_PP;
00519     gpioinitstruct.Pull  = GPIO_NOPULL;
00520     gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00521     HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
00522     LCD_CS_HIGH();
00523 
00524     /*------------Put SD in SPI mode--------------*/
00525     /* SD SPI Config */
00526     SPIx_Init();
00527 
00528     /* SD chip select high */
00529     SD_CS_HIGH();
00530 
00531     /* Send dummy byte 0xFF, 10 times with CS high */
00532     /* Rise CS and MOSI for 80 clocks cycles */
00533     for (counter = 0; counter <= 9; counter++) {
00534         /* Send dummy byte 0xFF */
00535         SD_IO_WriteByte(SD_DUMMY_BYTE);
00536     }
00537 }
00538 
00539 /**
00540   * @brief  Set SD interface Chip Select state
00541   * @param  val: 0 (low) or 1 (high) state
00542   * @retval None
00543   */
00544 void SD_IO_CSState(uint8_t val)
00545 {
00546     if(val == 1) {
00547         SD_CS_HIGH();
00548     } else {
00549         SD_CS_LOW();
00550     }
00551 }
00552 
00553 /**
00554   * @brief  Write byte(s) on the SD
00555   * @param  DataIn: Pointer to data buffer to write
00556   * @param  DataOut: Pointer to data buffer for read data
00557   * @param  DataLength: number of bytes to write
00558   * @retval None
00559   */
00560 void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength)
00561 {
00562     /* Send the byte */
00563     SPIx_WriteReadData(DataIn, DataOut, DataLength);
00564 }
00565 
00566 /**
00567   * @brief  Write a byte on the SD.
00568   * @param  Data: byte to send.
00569   * @retval Data written
00570   */
00571 uint8_t SD_IO_WriteByte(uint8_t Data)
00572 {
00573     uint8_t tmp;
00574 
00575     /* Send the byte */
00576     SPIx_WriteReadData(&Data,&tmp,1);
00577     return tmp;
00578 }
00579 
00580 /********************************* LINK LCD ***********************************/
00581 /**
00582   * @brief  Initialize the LCD
00583   * @retval None
00584   */
00585 void LCD_IO_Init(void)
00586 {
00587     GPIO_InitTypeDef  gpioinitstruct = {0};
00588 
00589     /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */
00590     LCD_CS_GPIO_CLK_ENABLE();
00591     LCD_DC_GPIO_CLK_ENABLE();
00592 
00593     /* Configure LCD_CS_PIN pin: LCD Card CS pin */
00594     gpioinitstruct.Pin    = LCD_CS_PIN;
00595     gpioinitstruct.Mode   = GPIO_MODE_OUTPUT_PP;
00596     gpioinitstruct.Pull   = GPIO_NOPULL;
00597     gpioinitstruct.Speed  = GPIO_SPEED_FREQ_VERY_HIGH;
00598     HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct);
00599 
00600     /* Configure LCD_DC_PIN pin: LCD Card DC pin */
00601     gpioinitstruct.Pin    = LCD_DC_PIN;
00602     HAL_GPIO_Init(LCD_DC_GPIO_PORT, &gpioinitstruct);
00603 
00604     /* LCD chip select high */
00605     LCD_CS_HIGH();
00606 
00607     /* LCD SPI Config */
00608     SPIx_Init();
00609 }
00610 
00611 /**
00612   * @brief  Write command to select the LCD register.
00613   * @param  LCDReg: Address of the selected register.
00614   * @retval None
00615   */
00616 void LCD_IO_WriteReg(uint8_t LCDReg)
00617 {
00618     /* Reset LCD control line CS */
00619     LCD_CS_LOW();
00620 
00621     /* Set LCD data/command line DC to Low */
00622     LCD_DC_LOW();
00623 
00624     /* Send Command */
00625     SPIx_Write(LCDReg);
00626 
00627     /* Deselect : Chip Select high */
00628     LCD_CS_HIGH();
00629 }
00630 
00631 /**
00632 * @brief  Write register value.
00633 * @param  pData Pointer on the register value
00634 * @param  Size Size of byte to transmit to the register
00635 * @retval None
00636 */
00637 void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size)
00638 {
00639     uint32_t counter = 0;
00640     __IO uint32_t data = 0;
00641 
00642     /* Reset LCD control line CS */
00643     LCD_CS_LOW();
00644 
00645     /* Set LCD data/command line DC to High */
00646     LCD_DC_HIGH();
00647 
00648     if (Size == 1) {
00649         /* Only 1 byte to be sent to LCD - general interface can be used */
00650         /* Send Data */
00651         SPIx_Write(*pData);
00652     } else {
00653         /* Several data should be sent in a raw */
00654         /* Direct SPI accesses for optimization */
00655         for (counter = Size; counter != 0; counter--) {
00656             while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE) {
00657             }
00658             /* Need to invert bytes for LCD*/
00659             *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *(pData+1);
00660 
00661             while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE) {
00662             }
00663             *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *pData;
00664             counter--;
00665             pData += 2;
00666         }
00667 
00668         /* Wait until the bus is ready before releasing Chip select */
00669         while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET) {
00670         }
00671     }
00672 
00673     /* Empty the Rx fifo */
00674     data = *(&hnucleo_Spi.Instance->DR);
00675     UNUSED(data);  /* Remove GNU warning */
00676 
00677     /* Deselect : Chip Select high */
00678     LCD_CS_HIGH();
00679 }
00680 
00681 /**
00682   * @brief  Wait for loop in ms.
00683   * @param  Delay in ms.
00684   * @retval None
00685   */
00686 void LCD_Delay(uint32_t Delay)
00687 {
00688     HAL_Delay(Delay);
00689 }
00690 
00691 #endif /* HAL_SPI_MODULE_ENABLED */
00692 
00693 #ifdef HAL_ADC_MODULE_ENABLED
00694 /******************************* LINK JOYSTICK ********************************/
00695 /**
00696   * @brief  Initialize ADC MSP.
00697   * @retval None
00698   */
00699 static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
00700 {
00701     GPIO_InitTypeDef  gpioinitstruct = {0};
00702     RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct;
00703 
00704     /*** Configure the GPIOs ***/
00705     /* Enable GPIO clock */
00706     NUCLEO_ADCx_GPIO_CLK_ENABLE();
00707 
00708     /* Configure ADC1 Channel8 as analog input */
00709     gpioinitstruct.Pin    = NUCLEO_ADCx_GPIO_PIN ;
00710     gpioinitstruct.Mode   = GPIO_MODE_ANALOG_ADC_CONTROL;
00711     gpioinitstruct.Pull   = GPIO_NOPULL;
00712     gpioinitstruct.Speed  = GPIO_SPEED_FREQ_HIGH;
00713     HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &gpioinitstruct);
00714 
00715     /*** Configure the ADC peripheral ***/
00716     /* Enable ADC clock */
00717     NUCLEO_ADCx_CLK_ENABLE();
00718 
00719     /* Configure SYSCLK as source clock for ADC */
00720     RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
00721     RCC_PeriphCLKInitStruct.AdcClockSelection    = RCC_ADCCLKSOURCE_SYSCLK;
00722     HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
00723 }
00724 
00725 /**
00726   * @brief  Initializes ADC HAL.
00727   * @retval None
00728   */
00729 static HAL_StatusTypeDef ADCx_Init(void)
00730 {
00731     if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET) {
00732         /* ADC Config */
00733         hnucleo_Adc.Instance                    = NUCLEO_ADCx;
00734         hnucleo_Adc.Init.ClockPrescaler         = ADC_CLOCK_ASYNC_DIV8; /* (must not exceed 16MHz) */
00735         hnucleo_Adc.Init.Resolution             = ADC_RESOLUTION_12B;
00736         hnucleo_Adc.Init.DataAlign              = ADC_DATAALIGN_RIGHT;
00737         hnucleo_Adc.Init.ScanConvMode           = DISABLE;
00738         hnucleo_Adc.Init.EOCSelection           = ADC_EOC_SINGLE_CONV;
00739         hnucleo_Adc.Init.LowPowerAutoWait       = ENABLE;
00740         hnucleo_Adc.Init.ContinuousConvMode     = DISABLE;
00741         hnucleo_Adc.Init.NbrOfConversion        = 1;
00742         hnucleo_Adc.Init.DiscontinuousConvMode  = DISABLE;
00743         hnucleo_Adc.Init.NbrOfDiscConversion    = 1;
00744         hnucleo_Adc.Init.ExternalTrigConv       = ADC_SOFTWARE_START;
00745         hnucleo_Adc.Init.ExternalTrigConvEdge   = ADC_EXTERNALTRIGCONVEDGE_NONE;
00746         hnucleo_Adc.Init.DMAContinuousRequests  = DISABLE;
00747         hnucleo_Adc.Init.Overrun                = ADC_OVR_DATA_PRESERVED;
00748         hnucleo_Adc.Init.OversamplingMode       = DISABLE;
00749 
00750         ADCx_MspInit(&hnucleo_Adc);
00751         if (HAL_ADC_Init(&hnucleo_Adc) != HAL_OK) {
00752             return HAL_ERROR;
00753         }
00754 
00755         if (HAL_ADCEx_Calibration_Start(&hnucleo_Adc,ADC_SINGLE_ENDED) != HAL_OK) {
00756             return HAL_ERROR;
00757         }
00758     }
00759 
00760     return HAL_OK;
00761 }
00762 
00763 #endif /* HAL_ADC_MODULE_ENABLED */
00764 
00765 /**
00766   * @}
00767   */
00768 
00769 /**
00770   * @}
00771   */
00772 
00773 /**
00774   * @}
00775   */
00776 
00777 /**
00778   * @}
00779   */
00780 
00781 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00782