Kashish Haresh Shah
/
HXCClientShield
HXC Client Shield Repository.
Driver/stm32l0xx_nucleo.c@3:5e1a54378107, 2021-04-19 (annotated)
- Committer:
- kashish_mbed
- Date:
- Mon Apr 19 17:43:09 2021 +0000
- Revision:
- 3:5e1a54378107
- Parent:
- 0:bacc6e701fb4
Successful Build file with CmdProcess Functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kashish_mbed | 0:bacc6e701fb4 | 1 | /** |
kashish_mbed | 0:bacc6e701fb4 | 2 | ****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 3 | * @file stm32l0xx_nucleo.c |
kashish_mbed | 0:bacc6e701fb4 | 4 | * @author MCD Application Team |
kashish_mbed | 0:bacc6e701fb4 | 5 | * @version V2.1.0 |
kashish_mbed | 0:bacc6e701fb4 | 6 | * @date 25-November-2016 |
kashish_mbed | 0:bacc6e701fb4 | 7 | * @brief This file provides set of firmware functions to manage: |
kashish_mbed | 0:bacc6e701fb4 | 8 | * - LEDs and push-button available on STM32L0XX-Nucleo Kit |
kashish_mbed | 0:bacc6e701fb4 | 9 | * from STMicroelectronics |
kashish_mbed | 0:bacc6e701fb4 | 10 | * - LCD, joystick and microSD available on Adafruit 1.8" TFT LCD |
kashish_mbed | 0:bacc6e701fb4 | 11 | * shield (reference ID 802) |
kashish_mbed | 0:bacc6e701fb4 | 12 | ****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 13 | * @attention |
kashish_mbed | 0:bacc6e701fb4 | 14 | * |
kashish_mbed | 0:bacc6e701fb4 | 15 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
kashish_mbed | 0:bacc6e701fb4 | 16 | * |
kashish_mbed | 0:bacc6e701fb4 | 17 | * Redistribution and use in source and binary forms, with or without modification, |
kashish_mbed | 0:bacc6e701fb4 | 18 | * are permitted provided that the following conditions are met: |
kashish_mbed | 0:bacc6e701fb4 | 19 | * 1. Redistributions of source code must retain the above copyright notice, |
kashish_mbed | 0:bacc6e701fb4 | 20 | * this list of conditions and the following disclaimer. |
kashish_mbed | 0:bacc6e701fb4 | 21 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
kashish_mbed | 0:bacc6e701fb4 | 22 | * this list of conditions and the following disclaimer in the documentation |
kashish_mbed | 0:bacc6e701fb4 | 23 | * and/or other materials provided with the distribution. |
kashish_mbed | 0:bacc6e701fb4 | 24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
kashish_mbed | 0:bacc6e701fb4 | 25 | * may be used to endorse or promote products derived from this software |
kashish_mbed | 0:bacc6e701fb4 | 26 | * without specific prior written permission. |
kashish_mbed | 0:bacc6e701fb4 | 27 | * |
kashish_mbed | 0:bacc6e701fb4 | 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
kashish_mbed | 0:bacc6e701fb4 | 29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
kashish_mbed | 0:bacc6e701fb4 | 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
kashish_mbed | 0:bacc6e701fb4 | 31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
kashish_mbed | 0:bacc6e701fb4 | 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
kashish_mbed | 0:bacc6e701fb4 | 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
kashish_mbed | 0:bacc6e701fb4 | 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
kashish_mbed | 0:bacc6e701fb4 | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
kashish_mbed | 0:bacc6e701fb4 | 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
kashish_mbed | 0:bacc6e701fb4 | 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kashish_mbed | 0:bacc6e701fb4 | 38 | * |
kashish_mbed | 0:bacc6e701fb4 | 39 | ****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 40 | */ |
kashish_mbed | 0:bacc6e701fb4 | 41 | |
kashish_mbed | 0:bacc6e701fb4 | 42 | /* Includes ------------------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 43 | #include "stm32l0xx_nucleo.h" |
kashish_mbed | 0:bacc6e701fb4 | 44 | |
kashish_mbed | 0:bacc6e701fb4 | 45 | /** @addtogroup BSP |
kashish_mbed | 0:bacc6e701fb4 | 46 | * @{ |
kashish_mbed | 0:bacc6e701fb4 | 47 | */ |
kashish_mbed | 0:bacc6e701fb4 | 48 | |
kashish_mbed | 0:bacc6e701fb4 | 49 | /** @addtogroup STM32L0XX_NUCLEO |
kashish_mbed | 0:bacc6e701fb4 | 50 | * @{ |
kashish_mbed | 0:bacc6e701fb4 | 51 | */ |
kashish_mbed | 0:bacc6e701fb4 | 52 | |
kashish_mbed | 0:bacc6e701fb4 | 53 | /** @addtogroup STM32L0XX_NUCLEO_LOW_LEVEL |
kashish_mbed | 0:bacc6e701fb4 | 54 | * @brief This file provides set of firmware functions to manage Leds and push-button |
kashish_mbed | 0:bacc6e701fb4 | 55 | * available on STM32L0XX-Nucleo Kit from STMicroelectronics. |
kashish_mbed | 0:bacc6e701fb4 | 56 | * @{ |
kashish_mbed | 0:bacc6e701fb4 | 57 | */ |
kashish_mbed | 0:bacc6e701fb4 | 58 | |
kashish_mbed | 0:bacc6e701fb4 | 59 | /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_TypesDefinitions Private Types Definitions |
kashish_mbed | 0:bacc6e701fb4 | 60 | * @{ |
kashish_mbed | 0:bacc6e701fb4 | 61 | */ |
kashish_mbed | 0:bacc6e701fb4 | 62 | /** |
kashish_mbed | 0:bacc6e701fb4 | 63 | * @} |
kashish_mbed | 0:bacc6e701fb4 | 64 | */ |
kashish_mbed | 0:bacc6e701fb4 | 65 | |
kashish_mbed | 0:bacc6e701fb4 | 66 | |
kashish_mbed | 0:bacc6e701fb4 | 67 | /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Defines Private Defines |
kashish_mbed | 0:bacc6e701fb4 | 68 | * @{ |
kashish_mbed | 0:bacc6e701fb4 | 69 | */ |
kashish_mbed | 0:bacc6e701fb4 | 70 | |
kashish_mbed | 0:bacc6e701fb4 | 71 | /** |
kashish_mbed | 0:bacc6e701fb4 | 72 | * @brief STM32L0XX NUCLEO BSP Driver version number V2.1.0 |
kashish_mbed | 0:bacc6e701fb4 | 73 | */ |
kashish_mbed | 0:bacc6e701fb4 | 74 | #define __STM32L0XX_NUCLEO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */ |
kashish_mbed | 0:bacc6e701fb4 | 75 | #define __STM32L0XX_NUCLEO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */ |
kashish_mbed | 0:bacc6e701fb4 | 76 | #define __STM32L0XX_NUCLEO_BSP_VERSION_SUB2 (0x00) /*!< [15:8] sub2 version */ |
kashish_mbed | 0:bacc6e701fb4 | 77 | #define __STM32L0XX_NUCLEO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */ |
kashish_mbed | 0:bacc6e701fb4 | 78 | #define __STM32L0XX_NUCLEO_BSP_VERSION ((__STM32L0XX_NUCLEO_BSP_VERSION_MAIN << 24)\ |
kashish_mbed | 0:bacc6e701fb4 | 79 | |(__STM32L0XX_NUCLEO_BSP_VERSION_SUB1 << 16)\ |
kashish_mbed | 0:bacc6e701fb4 | 80 | |(__STM32L0XX_NUCLEO_BSP_VERSION_SUB2 << 8 )\ |
kashish_mbed | 0:bacc6e701fb4 | 81 | |(__STM32L0XX_NUCLEO_BSP_VERSION_RC)) |
kashish_mbed | 0:bacc6e701fb4 | 82 | |
kashish_mbed | 0:bacc6e701fb4 | 83 | /** |
kashish_mbed | 0:bacc6e701fb4 | 84 | * @brief LINK SD Card |
kashish_mbed | 0:bacc6e701fb4 | 85 | */ |
kashish_mbed | 0:bacc6e701fb4 | 86 | #define SD_DUMMY_BYTE 0xFF |
kashish_mbed | 0:bacc6e701fb4 | 87 | #define SD_NO_RESPONSE_EXPECTED 0x80 |
kashish_mbed | 0:bacc6e701fb4 | 88 | |
kashish_mbed | 0:bacc6e701fb4 | 89 | /** |
kashish_mbed | 0:bacc6e701fb4 | 90 | * @} |
kashish_mbed | 0:bacc6e701fb4 | 91 | */ |
kashish_mbed | 0:bacc6e701fb4 | 92 | |
kashish_mbed | 0:bacc6e701fb4 | 93 | |
kashish_mbed | 0:bacc6e701fb4 | 94 | /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Variables Private Variables |
kashish_mbed | 0:bacc6e701fb4 | 95 | * @{ |
kashish_mbed | 0:bacc6e701fb4 | 96 | */ |
kashish_mbed | 0:bacc6e701fb4 | 97 | GPIO_TypeDef* LED_PORT[LEDn] = {LED2_GPIO_PORT}; |
kashish_mbed | 0:bacc6e701fb4 | 98 | |
kashish_mbed | 0:bacc6e701fb4 | 99 | const uint16_t LED_PIN[LEDn] = {LED2_PIN}; |
kashish_mbed | 0:bacc6e701fb4 | 100 | |
kashish_mbed | 0:bacc6e701fb4 | 101 | GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT }; |
kashish_mbed | 0:bacc6e701fb4 | 102 | const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN }; |
kashish_mbed | 0:bacc6e701fb4 | 103 | const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn }; |
kashish_mbed | 0:bacc6e701fb4 | 104 | |
kashish_mbed | 0:bacc6e701fb4 | 105 | /** |
kashish_mbed | 0:bacc6e701fb4 | 106 | * @brief BUS variables |
kashish_mbed | 0:bacc6e701fb4 | 107 | */ |
kashish_mbed | 0:bacc6e701fb4 | 108 | |
kashish_mbed | 0:bacc6e701fb4 | 109 | #ifdef HAL_SPI_MODULE_ENABLED |
kashish_mbed | 0:bacc6e701fb4 | 110 | uint32_t SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */ |
kashish_mbed | 0:bacc6e701fb4 | 111 | static SPI_HandleTypeDef hnucleo_Spi; |
kashish_mbed | 0:bacc6e701fb4 | 112 | #endif /* HAL_SPI_MODULE_ENABLED */ |
kashish_mbed | 0:bacc6e701fb4 | 113 | |
kashish_mbed | 0:bacc6e701fb4 | 114 | #ifdef HAL_ADC_MODULE_ENABLED |
kashish_mbed | 0:bacc6e701fb4 | 115 | static ADC_HandleTypeDef hnucleo_Adc; |
kashish_mbed | 0:bacc6e701fb4 | 116 | /* ADC channel configuration structure declaration */ |
kashish_mbed | 0:bacc6e701fb4 | 117 | static ADC_ChannelConfTypeDef sConfig; |
kashish_mbed | 0:bacc6e701fb4 | 118 | #endif /* HAL_ADC_MODULE_ENABLED */ |
kashish_mbed | 0:bacc6e701fb4 | 119 | |
kashish_mbed | 0:bacc6e701fb4 | 120 | /** |
kashish_mbed | 0:bacc6e701fb4 | 121 | * @} |
kashish_mbed | 0:bacc6e701fb4 | 122 | */ |
kashish_mbed | 0:bacc6e701fb4 | 123 | |
kashish_mbed | 0:bacc6e701fb4 | 124 | /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_FunctionPrototypes Private Function Prototypes |
kashish_mbed | 0:bacc6e701fb4 | 125 | * @{ |
kashish_mbed | 0:bacc6e701fb4 | 126 | */ |
kashish_mbed | 0:bacc6e701fb4 | 127 | #ifdef HAL_SPI_MODULE_ENABLED |
kashish_mbed | 0:bacc6e701fb4 | 128 | static void SPIx_Init(void); |
kashish_mbed | 0:bacc6e701fb4 | 129 | static void SPIx_Write(uint8_t Value); |
kashish_mbed | 0:bacc6e701fb4 | 130 | static void SPIx_WriteData(uint8_t *DataIn, uint16_t DataLength); |
kashish_mbed | 0:bacc6e701fb4 | 131 | static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLegnth); |
kashish_mbed | 0:bacc6e701fb4 | 132 | static void SPIx_Error (void); |
kashish_mbed | 0:bacc6e701fb4 | 133 | static void SPIx_MspInit(void); |
kashish_mbed | 0:bacc6e701fb4 | 134 | |
kashish_mbed | 0:bacc6e701fb4 | 135 | /* SD IO functions */ |
kashish_mbed | 0:bacc6e701fb4 | 136 | void SD_IO_Init(void); |
kashish_mbed | 0:bacc6e701fb4 | 137 | void SD_IO_CSState(uint8_t state); |
kashish_mbed | 0:bacc6e701fb4 | 138 | void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength); |
kashish_mbed | 0:bacc6e701fb4 | 139 | void SD_IO_ReadData(uint8_t *DataOut, uint16_t DataLength); |
kashish_mbed | 0:bacc6e701fb4 | 140 | void SD_IO_WriteData(const uint8_t *Data, uint16_t DataLength); |
kashish_mbed | 0:bacc6e701fb4 | 141 | uint8_t SD_IO_WriteByte(uint8_t Data); |
kashish_mbed | 0:bacc6e701fb4 | 142 | uint8_t SD_IO_ReadByte(void); |
kashish_mbed | 0:bacc6e701fb4 | 143 | |
kashish_mbed | 0:bacc6e701fb4 | 144 | /* LCD IO functions */ |
kashish_mbed | 0:bacc6e701fb4 | 145 | void LCD_IO_Init(void); |
kashish_mbed | 0:bacc6e701fb4 | 146 | void LCD_IO_WriteData(uint8_t Data); |
kashish_mbed | 0:bacc6e701fb4 | 147 | void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size); |
kashish_mbed | 0:bacc6e701fb4 | 148 | void LCD_IO_WriteReg(uint8_t LCDReg); |
kashish_mbed | 0:bacc6e701fb4 | 149 | void LCD_Delay(uint32_t delay); |
kashish_mbed | 0:bacc6e701fb4 | 150 | #endif /* HAL_SPI_MODULE_ENABLED */ |
kashish_mbed | 0:bacc6e701fb4 | 151 | |
kashish_mbed | 0:bacc6e701fb4 | 152 | #ifdef HAL_ADC_MODULE_ENABLED |
kashish_mbed | 0:bacc6e701fb4 | 153 | static HAL_StatusTypeDef ADCx_Init(void); |
kashish_mbed | 0:bacc6e701fb4 | 154 | static void ADCx_DeInit(void); |
kashish_mbed | 0:bacc6e701fb4 | 155 | static void ADCx_MspInit(ADC_HandleTypeDef *hadc); |
kashish_mbed | 0:bacc6e701fb4 | 156 | static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc); |
kashish_mbed | 0:bacc6e701fb4 | 157 | #endif /* HAL_ADC_MODULE_ENABLED */ |
kashish_mbed | 0:bacc6e701fb4 | 158 | /** |
kashish_mbed | 0:bacc6e701fb4 | 159 | * @} |
kashish_mbed | 0:bacc6e701fb4 | 160 | */ |
kashish_mbed | 0:bacc6e701fb4 | 161 | |
kashish_mbed | 0:bacc6e701fb4 | 162 | /** @defgroup STM32L0XX_NUCLEO_LOW_LEVEL_Private_Functions Private Functions |
kashish_mbed | 0:bacc6e701fb4 | 163 | * @{ |
kashish_mbed | 0:bacc6e701fb4 | 164 | */ |
kashish_mbed | 0:bacc6e701fb4 | 165 | |
kashish_mbed | 0:bacc6e701fb4 | 166 | /** |
kashish_mbed | 0:bacc6e701fb4 | 167 | * @brief This method returns the STM32L0XX NUCLEO BSP Driver revision |
kashish_mbed | 0:bacc6e701fb4 | 168 | * @retval version : 0xXYZR (8bits for each decimal, R for RC) |
kashish_mbed | 0:bacc6e701fb4 | 169 | */ |
kashish_mbed | 0:bacc6e701fb4 | 170 | uint32_t BSP_GetVersion(void) |
kashish_mbed | 0:bacc6e701fb4 | 171 | { |
kashish_mbed | 0:bacc6e701fb4 | 172 | return __STM32L0XX_NUCLEO_BSP_VERSION; |
kashish_mbed | 0:bacc6e701fb4 | 173 | } |
kashish_mbed | 0:bacc6e701fb4 | 174 | |
kashish_mbed | 0:bacc6e701fb4 | 175 | /** |
kashish_mbed | 0:bacc6e701fb4 | 176 | * @brief Configures LED GPIO. |
kashish_mbed | 0:bacc6e701fb4 | 177 | * @param Led: Led to be configured. |
kashish_mbed | 0:bacc6e701fb4 | 178 | * This parameter can be one of the following values: |
kashish_mbed | 0:bacc6e701fb4 | 179 | * @arg LED2 |
kashish_mbed | 0:bacc6e701fb4 | 180 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 181 | */ |
kashish_mbed | 0:bacc6e701fb4 | 182 | void BSP_LED_Init(Led_TypeDef Led) |
kashish_mbed | 0:bacc6e701fb4 | 183 | { |
kashish_mbed | 0:bacc6e701fb4 | 184 | GPIO_InitTypeDef gpioinitstruct; |
kashish_mbed | 0:bacc6e701fb4 | 185 | |
kashish_mbed | 0:bacc6e701fb4 | 186 | /* Enable the GPIO_LED Clock */ |
kashish_mbed | 0:bacc6e701fb4 | 187 | LEDx_GPIO_CLK_ENABLE(Led); |
kashish_mbed | 0:bacc6e701fb4 | 188 | |
kashish_mbed | 0:bacc6e701fb4 | 189 | /* Configure the GPIO_LED pin */ |
kashish_mbed | 0:bacc6e701fb4 | 190 | gpioinitstruct.Pin = LED_PIN[Led]; |
kashish_mbed | 0:bacc6e701fb4 | 191 | gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP; |
kashish_mbed | 0:bacc6e701fb4 | 192 | gpioinitstruct.Pull = GPIO_NOPULL; |
kashish_mbed | 0:bacc6e701fb4 | 193 | gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
kashish_mbed | 0:bacc6e701fb4 | 194 | |
kashish_mbed | 0:bacc6e701fb4 | 195 | HAL_GPIO_Init(LED_PORT[Led], &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 196 | |
kashish_mbed | 0:bacc6e701fb4 | 197 | /* Reset PIN to switch off the LED */ |
kashish_mbed | 0:bacc6e701fb4 | 198 | HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET); |
kashish_mbed | 0:bacc6e701fb4 | 199 | } |
kashish_mbed | 0:bacc6e701fb4 | 200 | |
kashish_mbed | 0:bacc6e701fb4 | 201 | /** |
kashish_mbed | 0:bacc6e701fb4 | 202 | * @brief DeInit LEDs. |
kashish_mbed | 0:bacc6e701fb4 | 203 | * @param Led: LED to be de-init. |
kashish_mbed | 0:bacc6e701fb4 | 204 | * This parameter can be one of the following values: |
kashish_mbed | 0:bacc6e701fb4 | 205 | * @arg LED2 |
kashish_mbed | 0:bacc6e701fb4 | 206 | * @note Led DeInit does not disable the GPIO clock nor disable the Mfx |
kashish_mbed | 0:bacc6e701fb4 | 207 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 208 | */ |
kashish_mbed | 0:bacc6e701fb4 | 209 | void BSP_LED_DeInit(Led_TypeDef Led) |
kashish_mbed | 0:bacc6e701fb4 | 210 | { |
kashish_mbed | 0:bacc6e701fb4 | 211 | GPIO_InitTypeDef gpio_init_structure; |
kashish_mbed | 0:bacc6e701fb4 | 212 | |
kashish_mbed | 0:bacc6e701fb4 | 213 | /* Turn off LED */ |
kashish_mbed | 0:bacc6e701fb4 | 214 | HAL_GPIO_WritePin(LED_PORT[Led],LED_PIN[Led], GPIO_PIN_RESET); |
kashish_mbed | 0:bacc6e701fb4 | 215 | /* DeInit the GPIO_LED pin */ |
kashish_mbed | 0:bacc6e701fb4 | 216 | gpio_init_structure.Pin = LED_PIN[Led]; |
kashish_mbed | 0:bacc6e701fb4 | 217 | HAL_GPIO_DeInit(LED_PORT[Led], gpio_init_structure.Pin); |
kashish_mbed | 0:bacc6e701fb4 | 218 | } |
kashish_mbed | 0:bacc6e701fb4 | 219 | |
kashish_mbed | 0:bacc6e701fb4 | 220 | /** |
kashish_mbed | 0:bacc6e701fb4 | 221 | * @brief Turns selected LED On. |
kashish_mbed | 0:bacc6e701fb4 | 222 | * @param Led: Specifies the Led to be set on. |
kashish_mbed | 0:bacc6e701fb4 | 223 | * This parameter can be one of following parameters: |
kashish_mbed | 0:bacc6e701fb4 | 224 | * @arg LED2 |
kashish_mbed | 0:bacc6e701fb4 | 225 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 226 | */ |
kashish_mbed | 0:bacc6e701fb4 | 227 | void BSP_LED_On(Led_TypeDef Led) |
kashish_mbed | 0:bacc6e701fb4 | 228 | { |
kashish_mbed | 0:bacc6e701fb4 | 229 | HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET); |
kashish_mbed | 0:bacc6e701fb4 | 230 | } |
kashish_mbed | 0:bacc6e701fb4 | 231 | |
kashish_mbed | 0:bacc6e701fb4 | 232 | /** |
kashish_mbed | 0:bacc6e701fb4 | 233 | * @brief Turns selected LED Off. |
kashish_mbed | 0:bacc6e701fb4 | 234 | * @param Led: Specifies the Led to be set off. |
kashish_mbed | 0:bacc6e701fb4 | 235 | * This parameter can be one of following parameters: |
kashish_mbed | 0:bacc6e701fb4 | 236 | * @arg LED2 |
kashish_mbed | 0:bacc6e701fb4 | 237 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 238 | */ |
kashish_mbed | 0:bacc6e701fb4 | 239 | void BSP_LED_Off(Led_TypeDef Led) |
kashish_mbed | 0:bacc6e701fb4 | 240 | { |
kashish_mbed | 0:bacc6e701fb4 | 241 | HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET); |
kashish_mbed | 0:bacc6e701fb4 | 242 | } |
kashish_mbed | 0:bacc6e701fb4 | 243 | |
kashish_mbed | 0:bacc6e701fb4 | 244 | /** |
kashish_mbed | 0:bacc6e701fb4 | 245 | * @brief Toggles the selected LED. |
kashish_mbed | 0:bacc6e701fb4 | 246 | * @param Led: Specifies the Led to be toggled. |
kashish_mbed | 0:bacc6e701fb4 | 247 | * This parameter can be one of following parameters: |
kashish_mbed | 0:bacc6e701fb4 | 248 | * @arg LED2 |
kashish_mbed | 0:bacc6e701fb4 | 249 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 250 | */ |
kashish_mbed | 0:bacc6e701fb4 | 251 | void BSP_LED_Toggle(Led_TypeDef Led) |
kashish_mbed | 0:bacc6e701fb4 | 252 | { |
kashish_mbed | 0:bacc6e701fb4 | 253 | HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]); |
kashish_mbed | 0:bacc6e701fb4 | 254 | } |
kashish_mbed | 0:bacc6e701fb4 | 255 | |
kashish_mbed | 0:bacc6e701fb4 | 256 | /** |
kashish_mbed | 0:bacc6e701fb4 | 257 | * @brief Configures Button GPIO and EXTI Line. |
kashish_mbed | 0:bacc6e701fb4 | 258 | * @param Button: Specifies the Button to be configured. |
kashish_mbed | 0:bacc6e701fb4 | 259 | * This parameter should be: BUTTON_USER |
kashish_mbed | 0:bacc6e701fb4 | 260 | * @param ButtonMode: Specifies Button mode. |
kashish_mbed | 0:bacc6e701fb4 | 261 | * This parameter can be one of following parameters: |
kashish_mbed | 0:bacc6e701fb4 | 262 | * @arg BUTTON_MODE_GPIO: Button will be used as simple IO |
kashish_mbed | 0:bacc6e701fb4 | 263 | * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt |
kashish_mbed | 0:bacc6e701fb4 | 264 | * generation capability |
kashish_mbed | 0:bacc6e701fb4 | 265 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 266 | */ |
kashish_mbed | 0:bacc6e701fb4 | 267 | void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode) |
kashish_mbed | 0:bacc6e701fb4 | 268 | { |
kashish_mbed | 0:bacc6e701fb4 | 269 | GPIO_InitTypeDef gpioinitstruct; |
kashish_mbed | 0:bacc6e701fb4 | 270 | |
kashish_mbed | 0:bacc6e701fb4 | 271 | /* Enable the BUTTON Clock */ |
kashish_mbed | 0:bacc6e701fb4 | 272 | BUTTONx_GPIO_CLK_ENABLE(Button); |
kashish_mbed | 0:bacc6e701fb4 | 273 | |
kashish_mbed | 0:bacc6e701fb4 | 274 | gpioinitstruct.Pin = BUTTON_PIN[Button]; |
kashish_mbed | 0:bacc6e701fb4 | 275 | gpioinitstruct.Pull = GPIO_NOPULL; |
kashish_mbed | 0:bacc6e701fb4 | 276 | gpioinitstruct.Speed = GPIO_SPEED_FREQ_MEDIUM; |
kashish_mbed | 0:bacc6e701fb4 | 277 | |
kashish_mbed | 0:bacc6e701fb4 | 278 | if(ButtonMode == BUTTON_MODE_GPIO) |
kashish_mbed | 0:bacc6e701fb4 | 279 | { |
kashish_mbed | 0:bacc6e701fb4 | 280 | /* Configure Button pin as input */ |
kashish_mbed | 0:bacc6e701fb4 | 281 | gpioinitstruct.Mode = GPIO_MODE_INPUT; |
kashish_mbed | 0:bacc6e701fb4 | 282 | |
kashish_mbed | 0:bacc6e701fb4 | 283 | HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 284 | } |
kashish_mbed | 0:bacc6e701fb4 | 285 | |
kashish_mbed | 0:bacc6e701fb4 | 286 | if(ButtonMode == BUTTON_MODE_EXTI) |
kashish_mbed | 0:bacc6e701fb4 | 287 | { |
kashish_mbed | 0:bacc6e701fb4 | 288 | /* Configure Button pin as input with External interrupt */ |
kashish_mbed | 0:bacc6e701fb4 | 289 | gpioinitstruct.Mode = GPIO_MODE_IT_FALLING; |
kashish_mbed | 0:bacc6e701fb4 | 290 | HAL_GPIO_Init(BUTTON_PORT[Button], &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 291 | |
kashish_mbed | 0:bacc6e701fb4 | 292 | /* Enable and set Button EXTI Interrupt to the lowest priority */ |
kashish_mbed | 0:bacc6e701fb4 | 293 | HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0); |
kashish_mbed | 0:bacc6e701fb4 | 294 | HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button])); |
kashish_mbed | 0:bacc6e701fb4 | 295 | } |
kashish_mbed | 0:bacc6e701fb4 | 296 | } |
kashish_mbed | 0:bacc6e701fb4 | 297 | |
kashish_mbed | 0:bacc6e701fb4 | 298 | /** |
kashish_mbed | 0:bacc6e701fb4 | 299 | * @brief Push Button DeInit. |
kashish_mbed | 0:bacc6e701fb4 | 300 | * @param Button: Button to be configured |
kashish_mbed | 0:bacc6e701fb4 | 301 | * This parameter should be: BUTTON_USER |
kashish_mbed | 0:bacc6e701fb4 | 302 | * @note PB DeInit does not disable the GPIO clock |
kashish_mbed | 0:bacc6e701fb4 | 303 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 304 | */ |
kashish_mbed | 0:bacc6e701fb4 | 305 | void BSP_PB_DeInit(Button_TypeDef Button) |
kashish_mbed | 0:bacc6e701fb4 | 306 | { |
kashish_mbed | 0:bacc6e701fb4 | 307 | GPIO_InitTypeDef gpio_init_structure; |
kashish_mbed | 0:bacc6e701fb4 | 308 | |
kashish_mbed | 0:bacc6e701fb4 | 309 | gpio_init_structure.Pin = BUTTON_PIN[Button]; |
kashish_mbed | 0:bacc6e701fb4 | 310 | HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button])); |
kashish_mbed | 0:bacc6e701fb4 | 311 | HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin); |
kashish_mbed | 0:bacc6e701fb4 | 312 | } |
kashish_mbed | 0:bacc6e701fb4 | 313 | |
kashish_mbed | 0:bacc6e701fb4 | 314 | /** |
kashish_mbed | 0:bacc6e701fb4 | 315 | * @brief Returns the selected Button state. |
kashish_mbed | 0:bacc6e701fb4 | 316 | * @param Button: Specifies the Button to be checked. |
kashish_mbed | 0:bacc6e701fb4 | 317 | * This parameter should be: BUTTON_USER |
kashish_mbed | 0:bacc6e701fb4 | 318 | * @retval Button state. |
kashish_mbed | 0:bacc6e701fb4 | 319 | */ |
kashish_mbed | 0:bacc6e701fb4 | 320 | uint32_t BSP_PB_GetState(Button_TypeDef Button) |
kashish_mbed | 0:bacc6e701fb4 | 321 | { |
kashish_mbed | 0:bacc6e701fb4 | 322 | return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]); |
kashish_mbed | 0:bacc6e701fb4 | 323 | } |
kashish_mbed | 0:bacc6e701fb4 | 324 | |
kashish_mbed | 0:bacc6e701fb4 | 325 | |
kashish_mbed | 0:bacc6e701fb4 | 326 | #ifdef HAL_SPI_MODULE_ENABLED |
kashish_mbed | 0:bacc6e701fb4 | 327 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 328 | BUS OPERATIONS |
kashish_mbed | 0:bacc6e701fb4 | 329 | *******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 330 | /** |
kashish_mbed | 0:bacc6e701fb4 | 331 | * @brief Initialize SPI MSP. |
kashish_mbed | 0:bacc6e701fb4 | 332 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 333 | */ |
kashish_mbed | 0:bacc6e701fb4 | 334 | static void SPIx_MspInit(void) |
kashish_mbed | 0:bacc6e701fb4 | 335 | { |
kashish_mbed | 0:bacc6e701fb4 | 336 | GPIO_InitTypeDef gpioinitstruct = {0}; |
kashish_mbed | 0:bacc6e701fb4 | 337 | |
kashish_mbed | 0:bacc6e701fb4 | 338 | /*** Configure the GPIOs ***/ |
kashish_mbed | 0:bacc6e701fb4 | 339 | /* Enable GPIO clock */ |
kashish_mbed | 0:bacc6e701fb4 | 340 | NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 341 | NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 342 | |
kashish_mbed | 0:bacc6e701fb4 | 343 | /* Configure SPI SCK */ |
kashish_mbed | 0:bacc6e701fb4 | 344 | gpioinitstruct.Pin = NUCLEO_SPIx_SCK_PIN; |
kashish_mbed | 0:bacc6e701fb4 | 345 | gpioinitstruct.Mode = GPIO_MODE_AF_PP; |
kashish_mbed | 0:bacc6e701fb4 | 346 | gpioinitstruct.Pull = GPIO_PULLUP; |
kashish_mbed | 0:bacc6e701fb4 | 347 | gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
kashish_mbed | 0:bacc6e701fb4 | 348 | gpioinitstruct.Alternate = NUCLEO_SPIx_SCK_AF; |
kashish_mbed | 0:bacc6e701fb4 | 349 | HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 350 | |
kashish_mbed | 0:bacc6e701fb4 | 351 | /* Configure SPI MISO and MOSI */ |
kashish_mbed | 0:bacc6e701fb4 | 352 | gpioinitstruct.Pin = NUCLEO_SPIx_MOSI_PIN; |
kashish_mbed | 0:bacc6e701fb4 | 353 | gpioinitstruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF; |
kashish_mbed | 0:bacc6e701fb4 | 354 | gpioinitstruct.Pull = GPIO_PULLDOWN; |
kashish_mbed | 0:bacc6e701fb4 | 355 | HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 356 | |
kashish_mbed | 0:bacc6e701fb4 | 357 | gpioinitstruct.Pin = NUCLEO_SPIx_MISO_PIN; |
kashish_mbed | 0:bacc6e701fb4 | 358 | HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 359 | |
kashish_mbed | 0:bacc6e701fb4 | 360 | /*** Configure the SPI peripheral ***/ |
kashish_mbed | 0:bacc6e701fb4 | 361 | /* Enable SPI clock */ |
kashish_mbed | 0:bacc6e701fb4 | 362 | NUCLEO_SPIx_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 363 | } |
kashish_mbed | 0:bacc6e701fb4 | 364 | |
kashish_mbed | 0:bacc6e701fb4 | 365 | /** |
kashish_mbed | 0:bacc6e701fb4 | 366 | * @brief Initialize SPI HAL. |
kashish_mbed | 0:bacc6e701fb4 | 367 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 368 | */ |
kashish_mbed | 0:bacc6e701fb4 | 369 | static void SPIx_Init(void) |
kashish_mbed | 0:bacc6e701fb4 | 370 | { |
kashish_mbed | 0:bacc6e701fb4 | 371 | if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET) |
kashish_mbed | 0:bacc6e701fb4 | 372 | { |
kashish_mbed | 0:bacc6e701fb4 | 373 | /* SPI Config */ |
kashish_mbed | 0:bacc6e701fb4 | 374 | hnucleo_Spi.Instance = NUCLEO_SPIx; |
kashish_mbed | 0:bacc6e701fb4 | 375 | /* SPI baudrate is set to 8 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 32/4 = 8 MHz) |
kashish_mbed | 0:bacc6e701fb4 | 376 | to verify these constraints: |
kashish_mbed | 0:bacc6e701fb4 | 377 | - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read |
kashish_mbed | 0:bacc6e701fb4 | 378 | Since the provided driver doesn't use read capability from LCD, only constraint |
kashish_mbed | 0:bacc6e701fb4 | 379 | on write baudrate is considered. |
kashish_mbed | 0:bacc6e701fb4 | 380 | - SD card SPI interface max baudrate is 25MHz for write/read |
kashish_mbed | 0:bacc6e701fb4 | 381 | - PCLK2 max frequency is 32 MHz |
kashish_mbed | 0:bacc6e701fb4 | 382 | */ |
kashish_mbed | 0:bacc6e701fb4 | 383 | hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; |
kashish_mbed | 0:bacc6e701fb4 | 384 | hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES; |
kashish_mbed | 0:bacc6e701fb4 | 385 | hnucleo_Spi.Init.CLKPhase = SPI_PHASE_1EDGE; |
kashish_mbed | 0:bacc6e701fb4 | 386 | hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_LOW; |
kashish_mbed | 0:bacc6e701fb4 | 387 | hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
kashish_mbed | 0:bacc6e701fb4 | 388 | hnucleo_Spi.Init.CRCPolynomial = 7; |
kashish_mbed | 0:bacc6e701fb4 | 389 | hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT; |
kashish_mbed | 0:bacc6e701fb4 | 390 | hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB; |
kashish_mbed | 0:bacc6e701fb4 | 391 | hnucleo_Spi.Init.NSS = SPI_NSS_SOFT; |
kashish_mbed | 0:bacc6e701fb4 | 392 | hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLE; |
kashish_mbed | 0:bacc6e701fb4 | 393 | hnucleo_Spi.Init.Mode = SPI_MODE_MASTER; |
kashish_mbed | 0:bacc6e701fb4 | 394 | |
kashish_mbed | 0:bacc6e701fb4 | 395 | SPIx_MspInit(); |
kashish_mbed | 0:bacc6e701fb4 | 396 | HAL_SPI_Init(&hnucleo_Spi); |
kashish_mbed | 0:bacc6e701fb4 | 397 | } |
kashish_mbed | 0:bacc6e701fb4 | 398 | } |
kashish_mbed | 0:bacc6e701fb4 | 399 | |
kashish_mbed | 0:bacc6e701fb4 | 400 | /** |
kashish_mbed | 0:bacc6e701fb4 | 401 | * @brief SPI Write a byte to device |
kashish_mbed | 0:bacc6e701fb4 | 402 | * @param DataIn: value to be written |
kashish_mbed | 0:bacc6e701fb4 | 403 | * @param DataOut: read value |
kashish_mbed | 0:bacc6e701fb4 | 404 | * @param DataLength: number of bytes to write |
kashish_mbed | 0:bacc6e701fb4 | 405 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 406 | */ |
kashish_mbed | 0:bacc6e701fb4 | 407 | static void SPIx_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength) |
kashish_mbed | 0:bacc6e701fb4 | 408 | { |
kashish_mbed | 0:bacc6e701fb4 | 409 | HAL_StatusTypeDef status = HAL_OK; |
kashish_mbed | 0:bacc6e701fb4 | 410 | |
kashish_mbed | 0:bacc6e701fb4 | 411 | status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) DataIn, DataOut, DataLength, SpixTimeout); |
kashish_mbed | 0:bacc6e701fb4 | 412 | |
kashish_mbed | 0:bacc6e701fb4 | 413 | /* Check the communication status */ |
kashish_mbed | 0:bacc6e701fb4 | 414 | if(status != HAL_OK) |
kashish_mbed | 0:bacc6e701fb4 | 415 | { |
kashish_mbed | 0:bacc6e701fb4 | 416 | /* Execute user timeout callback */ |
kashish_mbed | 0:bacc6e701fb4 | 417 | SPIx_Error(); |
kashish_mbed | 0:bacc6e701fb4 | 418 | } |
kashish_mbed | 0:bacc6e701fb4 | 419 | } |
kashish_mbed | 0:bacc6e701fb4 | 420 | |
kashish_mbed | 0:bacc6e701fb4 | 421 | /** |
kashish_mbed | 0:bacc6e701fb4 | 422 | * @brief SPI Write an amount of data to device |
kashish_mbed | 0:bacc6e701fb4 | 423 | * @param DataIn: value to be written |
kashish_mbed | 0:bacc6e701fb4 | 424 | * @param DataLength: number of bytes to write |
kashish_mbed | 0:bacc6e701fb4 | 425 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 426 | */ |
kashish_mbed | 0:bacc6e701fb4 | 427 | static void SPIx_WriteData(uint8_t *DataIn, uint16_t DataLength) |
kashish_mbed | 0:bacc6e701fb4 | 428 | { |
kashish_mbed | 0:bacc6e701fb4 | 429 | HAL_StatusTypeDef status = HAL_OK; |
kashish_mbed | 0:bacc6e701fb4 | 430 | |
kashish_mbed | 0:bacc6e701fb4 | 431 | status = HAL_SPI_Transmit(&hnucleo_Spi, DataIn, DataLength, SpixTimeout); |
kashish_mbed | 0:bacc6e701fb4 | 432 | |
kashish_mbed | 0:bacc6e701fb4 | 433 | /* Check the communication status */ |
kashish_mbed | 0:bacc6e701fb4 | 434 | if(status != HAL_OK) |
kashish_mbed | 0:bacc6e701fb4 | 435 | { |
kashish_mbed | 0:bacc6e701fb4 | 436 | /* Execute user timeout callback */ |
kashish_mbed | 0:bacc6e701fb4 | 437 | SPIx_Error(); |
kashish_mbed | 0:bacc6e701fb4 | 438 | } |
kashish_mbed | 0:bacc6e701fb4 | 439 | } |
kashish_mbed | 0:bacc6e701fb4 | 440 | |
kashish_mbed | 0:bacc6e701fb4 | 441 | /** |
kashish_mbed | 0:bacc6e701fb4 | 442 | * @brief SPI Write a byte to device |
kashish_mbed | 0:bacc6e701fb4 | 443 | * @param Value: value to be written |
kashish_mbed | 0:bacc6e701fb4 | 444 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 445 | */ |
kashish_mbed | 0:bacc6e701fb4 | 446 | static void SPIx_Write(uint8_t Value) |
kashish_mbed | 0:bacc6e701fb4 | 447 | { |
kashish_mbed | 0:bacc6e701fb4 | 448 | HAL_StatusTypeDef status = HAL_OK; |
kashish_mbed | 0:bacc6e701fb4 | 449 | uint8_t data; |
kashish_mbed | 0:bacc6e701fb4 | 450 | |
kashish_mbed | 0:bacc6e701fb4 | 451 | status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &Value, &data, 1, SpixTimeout); |
kashish_mbed | 0:bacc6e701fb4 | 452 | |
kashish_mbed | 0:bacc6e701fb4 | 453 | /* Check the communication status */ |
kashish_mbed | 0:bacc6e701fb4 | 454 | if(status != HAL_OK) |
kashish_mbed | 0:bacc6e701fb4 | 455 | { |
kashish_mbed | 0:bacc6e701fb4 | 456 | /* Execute user timeout callback */ |
kashish_mbed | 0:bacc6e701fb4 | 457 | SPIx_Error(); |
kashish_mbed | 0:bacc6e701fb4 | 458 | } |
kashish_mbed | 0:bacc6e701fb4 | 459 | } |
kashish_mbed | 0:bacc6e701fb4 | 460 | |
kashish_mbed | 0:bacc6e701fb4 | 461 | /** |
kashish_mbed | 0:bacc6e701fb4 | 462 | * @brief SPI error treatment function |
kashish_mbed | 0:bacc6e701fb4 | 463 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 464 | */ |
kashish_mbed | 0:bacc6e701fb4 | 465 | static void SPIx_Error (void) |
kashish_mbed | 0:bacc6e701fb4 | 466 | { |
kashish_mbed | 0:bacc6e701fb4 | 467 | /* De-initialize the SPI communication BUS */ |
kashish_mbed | 0:bacc6e701fb4 | 468 | HAL_SPI_DeInit(&hnucleo_Spi); |
kashish_mbed | 0:bacc6e701fb4 | 469 | |
kashish_mbed | 0:bacc6e701fb4 | 470 | /* Re-Initiaize the SPI communication BUS */ |
kashish_mbed | 0:bacc6e701fb4 | 471 | SPIx_Init(); |
kashish_mbed | 0:bacc6e701fb4 | 472 | } |
kashish_mbed | 0:bacc6e701fb4 | 473 | |
kashish_mbed | 0:bacc6e701fb4 | 474 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 475 | LINK OPERATIONS |
kashish_mbed | 0:bacc6e701fb4 | 476 | *******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 477 | |
kashish_mbed | 0:bacc6e701fb4 | 478 | /********************************* LINK SD ************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 479 | /** |
kashish_mbed | 0:bacc6e701fb4 | 480 | * @brief Initialize the SD Card and put it into StandBy State (Ready for |
kashish_mbed | 0:bacc6e701fb4 | 481 | * data transfer). |
kashish_mbed | 0:bacc6e701fb4 | 482 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 483 | */ |
kashish_mbed | 0:bacc6e701fb4 | 484 | void SD_IO_Init(void) |
kashish_mbed | 0:bacc6e701fb4 | 485 | { |
kashish_mbed | 0:bacc6e701fb4 | 486 | GPIO_InitTypeDef gpioinitstruct = {0}; |
kashish_mbed | 0:bacc6e701fb4 | 487 | uint8_t counter = 0; |
kashish_mbed | 0:bacc6e701fb4 | 488 | |
kashish_mbed | 0:bacc6e701fb4 | 489 | /* SD_CS_GPIO Periph clock enable */ |
kashish_mbed | 0:bacc6e701fb4 | 490 | SD_CS_GPIO_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 491 | |
kashish_mbed | 0:bacc6e701fb4 | 492 | /* Configure SD_CS_PIN pin: SD Card CS pin */ |
kashish_mbed | 0:bacc6e701fb4 | 493 | gpioinitstruct.Pin = SD_CS_PIN; |
kashish_mbed | 0:bacc6e701fb4 | 494 | gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP; |
kashish_mbed | 0:bacc6e701fb4 | 495 | gpioinitstruct.Pull = GPIO_PULLUP; |
kashish_mbed | 0:bacc6e701fb4 | 496 | gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH; |
kashish_mbed | 0:bacc6e701fb4 | 497 | HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 498 | |
kashish_mbed | 0:bacc6e701fb4 | 499 | /* Configure LCD_CS_PIN pin: LCD Card CS pin */ |
kashish_mbed | 0:bacc6e701fb4 | 500 | gpioinitstruct.Pin = LCD_CS_PIN; |
kashish_mbed | 0:bacc6e701fb4 | 501 | gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP; |
kashish_mbed | 0:bacc6e701fb4 | 502 | gpioinitstruct.Pull = GPIO_NOPULL; |
kashish_mbed | 0:bacc6e701fb4 | 503 | gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH; |
kashish_mbed | 0:bacc6e701fb4 | 504 | HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 505 | LCD_CS_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 506 | /*------------Put SD in SPI mode--------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 507 | /* SD SPI Config */ |
kashish_mbed | 0:bacc6e701fb4 | 508 | SPIx_Init(); |
kashish_mbed | 0:bacc6e701fb4 | 509 | |
kashish_mbed | 0:bacc6e701fb4 | 510 | /* SD chip select high */ |
kashish_mbed | 0:bacc6e701fb4 | 511 | SD_CS_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 512 | |
kashish_mbed | 0:bacc6e701fb4 | 513 | /* Send dummy byte 0xFF, 10 times with CS high */ |
kashish_mbed | 0:bacc6e701fb4 | 514 | /* Rise CS and MOSI for 80 clocks cycles */ |
kashish_mbed | 0:bacc6e701fb4 | 515 | for (counter = 0; counter <= 9; counter++) |
kashish_mbed | 0:bacc6e701fb4 | 516 | { |
kashish_mbed | 0:bacc6e701fb4 | 517 | /* Send dummy byte 0xFF */ |
kashish_mbed | 0:bacc6e701fb4 | 518 | SD_IO_WriteByte(SD_DUMMY_BYTE); |
kashish_mbed | 0:bacc6e701fb4 | 519 | } |
kashish_mbed | 0:bacc6e701fb4 | 520 | } |
kashish_mbed | 0:bacc6e701fb4 | 521 | |
kashish_mbed | 0:bacc6e701fb4 | 522 | /** |
kashish_mbed | 0:bacc6e701fb4 | 523 | * @brief Set the SD_CS pin. |
kashish_mbed | 0:bacc6e701fb4 | 524 | * @param val: pin value. |
kashish_mbed | 0:bacc6e701fb4 | 525 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 526 | */ |
kashish_mbed | 0:bacc6e701fb4 | 527 | void SD_IO_CSState(uint8_t val) |
kashish_mbed | 0:bacc6e701fb4 | 528 | { |
kashish_mbed | 0:bacc6e701fb4 | 529 | if(val == 1) |
kashish_mbed | 0:bacc6e701fb4 | 530 | { |
kashish_mbed | 0:bacc6e701fb4 | 531 | SD_CS_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 532 | } |
kashish_mbed | 0:bacc6e701fb4 | 533 | else |
kashish_mbed | 0:bacc6e701fb4 | 534 | { |
kashish_mbed | 0:bacc6e701fb4 | 535 | SD_CS_LOW(); |
kashish_mbed | 0:bacc6e701fb4 | 536 | } |
kashish_mbed | 0:bacc6e701fb4 | 537 | } |
kashish_mbed | 0:bacc6e701fb4 | 538 | |
kashish_mbed | 0:bacc6e701fb4 | 539 | /** |
kashish_mbed | 0:bacc6e701fb4 | 540 | * @brief Write byte(s) on the SD |
kashish_mbed | 0:bacc6e701fb4 | 541 | * @param DataIn: Pointer to data buffer to write |
kashish_mbed | 0:bacc6e701fb4 | 542 | * @param DataOut: Pointer to data buffer for read data |
kashish_mbed | 0:bacc6e701fb4 | 543 | * @param DataLength: number of bytes to write |
kashish_mbed | 0:bacc6e701fb4 | 544 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 545 | */ |
kashish_mbed | 0:bacc6e701fb4 | 546 | void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength) |
kashish_mbed | 0:bacc6e701fb4 | 547 | { |
kashish_mbed | 0:bacc6e701fb4 | 548 | /* Send the byte */ |
kashish_mbed | 0:bacc6e701fb4 | 549 | SPIx_WriteReadData(DataIn, DataOut, DataLength); |
kashish_mbed | 0:bacc6e701fb4 | 550 | } |
kashish_mbed | 0:bacc6e701fb4 | 551 | |
kashish_mbed | 0:bacc6e701fb4 | 552 | /** |
kashish_mbed | 0:bacc6e701fb4 | 553 | * @brief Write a byte on the SD. |
kashish_mbed | 0:bacc6e701fb4 | 554 | * @param Data: byte to send. |
kashish_mbed | 0:bacc6e701fb4 | 555 | * @retval Data written |
kashish_mbed | 0:bacc6e701fb4 | 556 | */ |
kashish_mbed | 0:bacc6e701fb4 | 557 | uint8_t SD_IO_WriteByte(uint8_t Data) |
kashish_mbed | 0:bacc6e701fb4 | 558 | { |
kashish_mbed | 0:bacc6e701fb4 | 559 | uint8_t tmp; |
kashish_mbed | 0:bacc6e701fb4 | 560 | |
kashish_mbed | 0:bacc6e701fb4 | 561 | /* Send the byte */ |
kashish_mbed | 0:bacc6e701fb4 | 562 | SPIx_WriteReadData(&Data,&tmp,1); |
kashish_mbed | 0:bacc6e701fb4 | 563 | return tmp; |
kashish_mbed | 0:bacc6e701fb4 | 564 | } |
kashish_mbed | 0:bacc6e701fb4 | 565 | |
kashish_mbed | 0:bacc6e701fb4 | 566 | /** |
kashish_mbed | 0:bacc6e701fb4 | 567 | * @brief Write an amount of data on the SD. |
kashish_mbed | 0:bacc6e701fb4 | 568 | * @param DataOut: byte to send. |
kashish_mbed | 0:bacc6e701fb4 | 569 | * @param DataLength: number of bytes to write |
kashish_mbed | 0:bacc6e701fb4 | 570 | * @retval none |
kashish_mbed | 0:bacc6e701fb4 | 571 | */ |
kashish_mbed | 0:bacc6e701fb4 | 572 | void SD_IO_ReadData(uint8_t *DataOut, uint16_t DataLength) |
kashish_mbed | 0:bacc6e701fb4 | 573 | { |
kashish_mbed | 0:bacc6e701fb4 | 574 | /* Send the byte */ |
kashish_mbed | 0:bacc6e701fb4 | 575 | SD_IO_WriteReadData(DataOut, DataOut, DataLength); |
kashish_mbed | 0:bacc6e701fb4 | 576 | } |
kashish_mbed | 0:bacc6e701fb4 | 577 | |
kashish_mbed | 0:bacc6e701fb4 | 578 | /** |
kashish_mbed | 0:bacc6e701fb4 | 579 | * @brief Write an amount of data on the SD. |
kashish_mbed | 0:bacc6e701fb4 | 580 | * @param Data: byte to send. |
kashish_mbed | 0:bacc6e701fb4 | 581 | * @param DataLength: number of bytes to write |
kashish_mbed | 0:bacc6e701fb4 | 582 | * @retval none |
kashish_mbed | 0:bacc6e701fb4 | 583 | */ |
kashish_mbed | 0:bacc6e701fb4 | 584 | void SD_IO_WriteData(const uint8_t *Data, uint16_t DataLength) |
kashish_mbed | 0:bacc6e701fb4 | 585 | { |
kashish_mbed | 0:bacc6e701fb4 | 586 | /* Send the byte */ |
kashish_mbed | 0:bacc6e701fb4 | 587 | SPIx_WriteData((uint8_t *)Data, DataLength); |
kashish_mbed | 0:bacc6e701fb4 | 588 | } |
kashish_mbed | 0:bacc6e701fb4 | 589 | |
kashish_mbed | 0:bacc6e701fb4 | 590 | /********************************* LINK LCD ***********************************/ |
kashish_mbed | 0:bacc6e701fb4 | 591 | /** |
kashish_mbed | 0:bacc6e701fb4 | 592 | * @brief Initialize the LCD |
kashish_mbed | 0:bacc6e701fb4 | 593 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 594 | */ |
kashish_mbed | 0:bacc6e701fb4 | 595 | void LCD_IO_Init(void) |
kashish_mbed | 0:bacc6e701fb4 | 596 | { |
kashish_mbed | 0:bacc6e701fb4 | 597 | GPIO_InitTypeDef gpioinitstruct = {0}; |
kashish_mbed | 0:bacc6e701fb4 | 598 | |
kashish_mbed | 0:bacc6e701fb4 | 599 | /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */ |
kashish_mbed | 0:bacc6e701fb4 | 600 | LCD_CS_GPIO_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 601 | LCD_DC_GPIO_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 602 | |
kashish_mbed | 0:bacc6e701fb4 | 603 | /* Configure LCD_CS_PIN pin: LCD Card CS pin */ |
kashish_mbed | 0:bacc6e701fb4 | 604 | gpioinitstruct.Pin = LCD_CS_PIN; |
kashish_mbed | 0:bacc6e701fb4 | 605 | gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP; |
kashish_mbed | 0:bacc6e701fb4 | 606 | gpioinitstruct.Pull = GPIO_NOPULL; |
kashish_mbed | 0:bacc6e701fb4 | 607 | gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
kashish_mbed | 0:bacc6e701fb4 | 608 | HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 609 | |
kashish_mbed | 0:bacc6e701fb4 | 610 | /* Configure LCD_DC_PIN pin: LCD Card DC pin */ |
kashish_mbed | 0:bacc6e701fb4 | 611 | gpioinitstruct.Pin = LCD_DC_PIN; |
kashish_mbed | 0:bacc6e701fb4 | 612 | HAL_GPIO_Init(LCD_DC_GPIO_PORT, &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 613 | |
kashish_mbed | 0:bacc6e701fb4 | 614 | /* LCD chip select high */ |
kashish_mbed | 0:bacc6e701fb4 | 615 | LCD_CS_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 616 | |
kashish_mbed | 0:bacc6e701fb4 | 617 | /* LCD SPI Config */ |
kashish_mbed | 0:bacc6e701fb4 | 618 | SPIx_Init(); |
kashish_mbed | 0:bacc6e701fb4 | 619 | } |
kashish_mbed | 0:bacc6e701fb4 | 620 | |
kashish_mbed | 0:bacc6e701fb4 | 621 | /** |
kashish_mbed | 0:bacc6e701fb4 | 622 | * @brief Write command to select the LCD register. |
kashish_mbed | 0:bacc6e701fb4 | 623 | * @param LCDReg: Address of the selected register. |
kashish_mbed | 0:bacc6e701fb4 | 624 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 625 | */ |
kashish_mbed | 0:bacc6e701fb4 | 626 | void LCD_IO_WriteReg(uint8_t LCDReg) |
kashish_mbed | 0:bacc6e701fb4 | 627 | { |
kashish_mbed | 0:bacc6e701fb4 | 628 | /* Reset LCD control line CS */ |
kashish_mbed | 0:bacc6e701fb4 | 629 | LCD_CS_LOW(); |
kashish_mbed | 0:bacc6e701fb4 | 630 | |
kashish_mbed | 0:bacc6e701fb4 | 631 | /* Set LCD data/command line DC to Low */ |
kashish_mbed | 0:bacc6e701fb4 | 632 | LCD_DC_LOW(); |
kashish_mbed | 0:bacc6e701fb4 | 633 | |
kashish_mbed | 0:bacc6e701fb4 | 634 | /* Send Command */ |
kashish_mbed | 0:bacc6e701fb4 | 635 | SPIx_Write(LCDReg); |
kashish_mbed | 0:bacc6e701fb4 | 636 | |
kashish_mbed | 0:bacc6e701fb4 | 637 | /* Deselect : Chip Select high */ |
kashish_mbed | 0:bacc6e701fb4 | 638 | LCD_CS_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 639 | } |
kashish_mbed | 0:bacc6e701fb4 | 640 | |
kashish_mbed | 0:bacc6e701fb4 | 641 | /** |
kashish_mbed | 0:bacc6e701fb4 | 642 | * @brief Writes data to select the LCD register. |
kashish_mbed | 0:bacc6e701fb4 | 643 | * This function must be used after st7735_WriteReg() function |
kashish_mbed | 0:bacc6e701fb4 | 644 | * @param Data: data to write to the selected register. |
kashish_mbed | 0:bacc6e701fb4 | 645 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 646 | */ |
kashish_mbed | 0:bacc6e701fb4 | 647 | void LCD_IO_WriteData(uint8_t Data) |
kashish_mbed | 0:bacc6e701fb4 | 648 | { |
kashish_mbed | 0:bacc6e701fb4 | 649 | /* Reset LCD control line CS */ |
kashish_mbed | 0:bacc6e701fb4 | 650 | LCD_CS_LOW(); |
kashish_mbed | 0:bacc6e701fb4 | 651 | |
kashish_mbed | 0:bacc6e701fb4 | 652 | /* Set LCD data/command line DC to High */ |
kashish_mbed | 0:bacc6e701fb4 | 653 | LCD_DC_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 654 | |
kashish_mbed | 0:bacc6e701fb4 | 655 | /* Send Data */ |
kashish_mbed | 0:bacc6e701fb4 | 656 | SPIx_Write(Data); |
kashish_mbed | 0:bacc6e701fb4 | 657 | |
kashish_mbed | 0:bacc6e701fb4 | 658 | /* Deselect : Chip Select high */ |
kashish_mbed | 0:bacc6e701fb4 | 659 | LCD_CS_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 660 | } |
kashish_mbed | 0:bacc6e701fb4 | 661 | |
kashish_mbed | 0:bacc6e701fb4 | 662 | /** |
kashish_mbed | 0:bacc6e701fb4 | 663 | * @brief Write register value. |
kashish_mbed | 0:bacc6e701fb4 | 664 | * @param pData Pointer on the register value |
kashish_mbed | 0:bacc6e701fb4 | 665 | * @param Size Size of byte to transmit to the register |
kashish_mbed | 0:bacc6e701fb4 | 666 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 667 | */ |
kashish_mbed | 0:bacc6e701fb4 | 668 | void LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size) |
kashish_mbed | 0:bacc6e701fb4 | 669 | { |
kashish_mbed | 0:bacc6e701fb4 | 670 | uint32_t counter = 0; |
kashish_mbed | 0:bacc6e701fb4 | 671 | __IO uint32_t data = 0; |
kashish_mbed | 0:bacc6e701fb4 | 672 | |
kashish_mbed | 0:bacc6e701fb4 | 673 | /* Reset LCD control line CS */ |
kashish_mbed | 0:bacc6e701fb4 | 674 | LCD_CS_LOW(); |
kashish_mbed | 0:bacc6e701fb4 | 675 | |
kashish_mbed | 0:bacc6e701fb4 | 676 | /* Set LCD data/command line DC to High */ |
kashish_mbed | 0:bacc6e701fb4 | 677 | LCD_DC_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 678 | |
kashish_mbed | 0:bacc6e701fb4 | 679 | if (Size == 1) |
kashish_mbed | 0:bacc6e701fb4 | 680 | { |
kashish_mbed | 0:bacc6e701fb4 | 681 | /* Only 1 byte to be sent to LCD - general interface can be used */ |
kashish_mbed | 0:bacc6e701fb4 | 682 | /* Send Data */ |
kashish_mbed | 0:bacc6e701fb4 | 683 | SPIx_Write(*pData); |
kashish_mbed | 0:bacc6e701fb4 | 684 | } |
kashish_mbed | 0:bacc6e701fb4 | 685 | else |
kashish_mbed | 0:bacc6e701fb4 | 686 | { |
kashish_mbed | 0:bacc6e701fb4 | 687 | /* Several data should be sent in a raw */ |
kashish_mbed | 0:bacc6e701fb4 | 688 | /* Direct SPI accesses for optimization */ |
kashish_mbed | 0:bacc6e701fb4 | 689 | for (counter = Size; counter != 0; counter--) |
kashish_mbed | 0:bacc6e701fb4 | 690 | { |
kashish_mbed | 0:bacc6e701fb4 | 691 | while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE) |
kashish_mbed | 0:bacc6e701fb4 | 692 | { |
kashish_mbed | 0:bacc6e701fb4 | 693 | } |
kashish_mbed | 0:bacc6e701fb4 | 694 | /* Need to invert bytes for LCD*/ |
kashish_mbed | 0:bacc6e701fb4 | 695 | *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *(pData+1); |
kashish_mbed | 0:bacc6e701fb4 | 696 | |
kashish_mbed | 0:bacc6e701fb4 | 697 | while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE) |
kashish_mbed | 0:bacc6e701fb4 | 698 | { |
kashish_mbed | 0:bacc6e701fb4 | 699 | } |
kashish_mbed | 0:bacc6e701fb4 | 700 | *((__IO uint8_t*)&hnucleo_Spi.Instance->DR) = *pData; |
kashish_mbed | 0:bacc6e701fb4 | 701 | counter--; |
kashish_mbed | 0:bacc6e701fb4 | 702 | pData += 2; |
kashish_mbed | 0:bacc6e701fb4 | 703 | } |
kashish_mbed | 0:bacc6e701fb4 | 704 | |
kashish_mbed | 0:bacc6e701fb4 | 705 | /* Wait until the bus is ready before releasing Chip select */ |
kashish_mbed | 0:bacc6e701fb4 | 706 | while(((hnucleo_Spi.Instance->SR) & SPI_FLAG_BSY) != RESET) |
kashish_mbed | 0:bacc6e701fb4 | 707 | { |
kashish_mbed | 0:bacc6e701fb4 | 708 | } |
kashish_mbed | 0:bacc6e701fb4 | 709 | } |
kashish_mbed | 0:bacc6e701fb4 | 710 | |
kashish_mbed | 0:bacc6e701fb4 | 711 | /* Empty the Rx fifo */ |
kashish_mbed | 0:bacc6e701fb4 | 712 | data = *(&hnucleo_Spi.Instance->DR); |
kashish_mbed | 0:bacc6e701fb4 | 713 | UNUSED(data); /* Remove GNU warning */ |
kashish_mbed | 0:bacc6e701fb4 | 714 | |
kashish_mbed | 0:bacc6e701fb4 | 715 | /* Deselect : Chip Select high */ |
kashish_mbed | 0:bacc6e701fb4 | 716 | LCD_CS_HIGH(); |
kashish_mbed | 0:bacc6e701fb4 | 717 | } |
kashish_mbed | 0:bacc6e701fb4 | 718 | |
kashish_mbed | 0:bacc6e701fb4 | 719 | /** |
kashish_mbed | 0:bacc6e701fb4 | 720 | * @brief Wait for loop in ms. |
kashish_mbed | 0:bacc6e701fb4 | 721 | * @param Delay in ms. |
kashish_mbed | 0:bacc6e701fb4 | 722 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 723 | */ |
kashish_mbed | 0:bacc6e701fb4 | 724 | void LCD_Delay(uint32_t Delay) |
kashish_mbed | 0:bacc6e701fb4 | 725 | { |
kashish_mbed | 0:bacc6e701fb4 | 726 | HAL_Delay(Delay); |
kashish_mbed | 0:bacc6e701fb4 | 727 | } |
kashish_mbed | 0:bacc6e701fb4 | 728 | #endif /* HAL_SPI_MODULE_ENABLED */ |
kashish_mbed | 0:bacc6e701fb4 | 729 | |
kashish_mbed | 0:bacc6e701fb4 | 730 | /******************************* LINK JOYSTICK ********************************/ |
kashish_mbed | 0:bacc6e701fb4 | 731 | #ifdef HAL_ADC_MODULE_ENABLED |
kashish_mbed | 0:bacc6e701fb4 | 732 | /** |
kashish_mbed | 0:bacc6e701fb4 | 733 | * @brief Initialize ADC MSP. |
kashish_mbed | 0:bacc6e701fb4 | 734 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 735 | */ |
kashish_mbed | 0:bacc6e701fb4 | 736 | static void ADCx_MspInit(ADC_HandleTypeDef *hadc) |
kashish_mbed | 0:bacc6e701fb4 | 737 | { |
kashish_mbed | 0:bacc6e701fb4 | 738 | GPIO_InitTypeDef gpioinitstruct = {0}; |
kashish_mbed | 0:bacc6e701fb4 | 739 | |
kashish_mbed | 0:bacc6e701fb4 | 740 | /*** Configure the GPIOs ***/ |
kashish_mbed | 0:bacc6e701fb4 | 741 | /* Enable GPIO clock */ |
kashish_mbed | 0:bacc6e701fb4 | 742 | NUCLEO_ADCx_GPIO_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 743 | |
kashish_mbed | 0:bacc6e701fb4 | 744 | /* Configure ADC1 Channel8 as analog input */ |
kashish_mbed | 0:bacc6e701fb4 | 745 | gpioinitstruct.Pin = NUCLEO_ADCx_GPIO_PIN ; |
kashish_mbed | 0:bacc6e701fb4 | 746 | gpioinitstruct.Mode = GPIO_MODE_ANALOG; |
kashish_mbed | 0:bacc6e701fb4 | 747 | gpioinitstruct.Pull = GPIO_NOPULL; |
kashish_mbed | 0:bacc6e701fb4 | 748 | HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &gpioinitstruct); |
kashish_mbed | 0:bacc6e701fb4 | 749 | |
kashish_mbed | 0:bacc6e701fb4 | 750 | /*** Configure the ADC peripheral ***/ |
kashish_mbed | 0:bacc6e701fb4 | 751 | /* Enable ADC clock */ |
kashish_mbed | 0:bacc6e701fb4 | 752 | NUCLEO_ADCx_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 753 | } |
kashish_mbed | 0:bacc6e701fb4 | 754 | |
kashish_mbed | 0:bacc6e701fb4 | 755 | /** |
kashish_mbed | 0:bacc6e701fb4 | 756 | * @brief DeInitializes ADC MSP. |
kashish_mbed | 0:bacc6e701fb4 | 757 | * @param hadc: ADC peripheral |
kashish_mbed | 0:bacc6e701fb4 | 758 | * @note ADC DeInit does not disable the GPIO clock |
kashish_mbed | 0:bacc6e701fb4 | 759 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 760 | */ |
kashish_mbed | 0:bacc6e701fb4 | 761 | static void ADCx_MspDeInit(ADC_HandleTypeDef *hadc) |
kashish_mbed | 0:bacc6e701fb4 | 762 | { |
kashish_mbed | 0:bacc6e701fb4 | 763 | GPIO_InitTypeDef gpioinitstruct; |
kashish_mbed | 0:bacc6e701fb4 | 764 | |
kashish_mbed | 0:bacc6e701fb4 | 765 | /*** DeInit the ADC peripheral ***/ |
kashish_mbed | 0:bacc6e701fb4 | 766 | /* Disable ADC clock */ |
kashish_mbed | 0:bacc6e701fb4 | 767 | NUCLEO_ADCx_CLK_DISABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 768 | |
kashish_mbed | 0:bacc6e701fb4 | 769 | /* Configure the selected ADC Channel as analog input */ |
kashish_mbed | 0:bacc6e701fb4 | 770 | gpioinitstruct.Pin = NUCLEO_ADCx_GPIO_PIN ; |
kashish_mbed | 0:bacc6e701fb4 | 771 | HAL_GPIO_DeInit(NUCLEO_ADCx_GPIO_PORT, gpioinitstruct.Pin); |
kashish_mbed | 0:bacc6e701fb4 | 772 | |
kashish_mbed | 0:bacc6e701fb4 | 773 | /* Disable GPIO clock has to be done by the application*/ |
kashish_mbed | 0:bacc6e701fb4 | 774 | /* NUCLEO_ADCx_GPIO_CLK_DISABLE(); */ |
kashish_mbed | 0:bacc6e701fb4 | 775 | } |
kashish_mbed | 0:bacc6e701fb4 | 776 | |
kashish_mbed | 0:bacc6e701fb4 | 777 | /** |
kashish_mbed | 0:bacc6e701fb4 | 778 | * @brief Initializes ADC HAL. |
kashish_mbed | 0:bacc6e701fb4 | 779 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 780 | */ |
kashish_mbed | 0:bacc6e701fb4 | 781 | static HAL_StatusTypeDef ADCx_Init(void) |
kashish_mbed | 0:bacc6e701fb4 | 782 | { |
kashish_mbed | 0:bacc6e701fb4 | 783 | /* Set ADC instance */ |
kashish_mbed | 0:bacc6e701fb4 | 784 | hnucleo_Adc.Instance = NUCLEO_ADCx; |
kashish_mbed | 0:bacc6e701fb4 | 785 | |
kashish_mbed | 0:bacc6e701fb4 | 786 | if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET) |
kashish_mbed | 0:bacc6e701fb4 | 787 | { |
kashish_mbed | 0:bacc6e701fb4 | 788 | /* ADC Config */ |
kashish_mbed | 0:bacc6e701fb4 | 789 | hnucleo_Adc.Instance = NUCLEO_ADCx; |
kashish_mbed | 0:bacc6e701fb4 | 790 | hnucleo_Adc.Init.OversamplingMode = DISABLE; |
kashish_mbed | 0:bacc6e701fb4 | 791 | hnucleo_Adc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; /* (must not exceed 16MHz) */ |
kashish_mbed | 0:bacc6e701fb4 | 792 | hnucleo_Adc.Init.LowPowerAutoPowerOff = DISABLE; |
kashish_mbed | 0:bacc6e701fb4 | 793 | hnucleo_Adc.Init.LowPowerFrequencyMode = ENABLE; |
kashish_mbed | 0:bacc6e701fb4 | 794 | hnucleo_Adc.Init.LowPowerAutoWait = ENABLE; |
kashish_mbed | 0:bacc6e701fb4 | 795 | hnucleo_Adc.Init.Resolution = ADC_RESOLUTION_12B; |
kashish_mbed | 0:bacc6e701fb4 | 796 | hnucleo_Adc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; |
kashish_mbed | 0:bacc6e701fb4 | 797 | hnucleo_Adc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; |
kashish_mbed | 0:bacc6e701fb4 | 798 | hnucleo_Adc.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
kashish_mbed | 0:bacc6e701fb4 | 799 | hnucleo_Adc.Init.ContinuousConvMode = DISABLE; |
kashish_mbed | 0:bacc6e701fb4 | 800 | hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE; |
kashish_mbed | 0:bacc6e701fb4 | 801 | hnucleo_Adc.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Trig of conversion start done manually by software, without external event */ |
kashish_mbed | 0:bacc6e701fb4 | 802 | hnucleo_Adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because trig by software start */ |
kashish_mbed | 0:bacc6e701fb4 | 803 | hnucleo_Adc.Init.EOCSelection = ADC_EOC_SEQ_CONV; |
kashish_mbed | 0:bacc6e701fb4 | 804 | hnucleo_Adc.Init.DMAContinuousRequests = DISABLE; |
kashish_mbed | 0:bacc6e701fb4 | 805 | |
kashish_mbed | 0:bacc6e701fb4 | 806 | /* Initialize MSP related to ADC */ |
kashish_mbed | 0:bacc6e701fb4 | 807 | ADCx_MspInit(&hnucleo_Adc); |
kashish_mbed | 0:bacc6e701fb4 | 808 | |
kashish_mbed | 0:bacc6e701fb4 | 809 | /* Initialize ADC */ |
kashish_mbed | 0:bacc6e701fb4 | 810 | if (HAL_ADC_Init(&hnucleo_Adc) != HAL_OK) |
kashish_mbed | 0:bacc6e701fb4 | 811 | { |
kashish_mbed | 0:bacc6e701fb4 | 812 | return HAL_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 813 | } |
kashish_mbed | 0:bacc6e701fb4 | 814 | |
kashish_mbed | 0:bacc6e701fb4 | 815 | if (HAL_ADCEx_Calibration_Start(&hnucleo_Adc,ADC_SINGLE_ENDED) != HAL_OK) |
kashish_mbed | 0:bacc6e701fb4 | 816 | { |
kashish_mbed | 0:bacc6e701fb4 | 817 | return HAL_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 818 | } |
kashish_mbed | 0:bacc6e701fb4 | 819 | } |
kashish_mbed | 0:bacc6e701fb4 | 820 | |
kashish_mbed | 0:bacc6e701fb4 | 821 | return HAL_OK; |
kashish_mbed | 0:bacc6e701fb4 | 822 | } |
kashish_mbed | 0:bacc6e701fb4 | 823 | |
kashish_mbed | 0:bacc6e701fb4 | 824 | /** |
kashish_mbed | 0:bacc6e701fb4 | 825 | * @brief Initializes ADC HAL. |
kashish_mbed | 0:bacc6e701fb4 | 826 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 827 | */ |
kashish_mbed | 0:bacc6e701fb4 | 828 | static void ADCx_DeInit(void) |
kashish_mbed | 0:bacc6e701fb4 | 829 | { |
kashish_mbed | 0:bacc6e701fb4 | 830 | hnucleo_Adc.Instance = NUCLEO_ADCx; |
kashish_mbed | 0:bacc6e701fb4 | 831 | |
kashish_mbed | 0:bacc6e701fb4 | 832 | HAL_ADC_DeInit(&hnucleo_Adc); |
kashish_mbed | 0:bacc6e701fb4 | 833 | ADCx_MspDeInit(&hnucleo_Adc); |
kashish_mbed | 0:bacc6e701fb4 | 834 | } |
kashish_mbed | 0:bacc6e701fb4 | 835 | |
kashish_mbed | 0:bacc6e701fb4 | 836 | /******************************* LINK JOYSTICK ********************************/ |
kashish_mbed | 0:bacc6e701fb4 | 837 | |
kashish_mbed | 0:bacc6e701fb4 | 838 | /** |
kashish_mbed | 0:bacc6e701fb4 | 839 | * @brief Configures joystick available on adafruit 1.8" TFT shield |
kashish_mbed | 0:bacc6e701fb4 | 840 | * managed through ADC to detect motion. |
kashish_mbed | 0:bacc6e701fb4 | 841 | * @retval Joystickstatus (0=> success, 1=> fail) |
kashish_mbed | 0:bacc6e701fb4 | 842 | */ |
kashish_mbed | 0:bacc6e701fb4 | 843 | uint8_t BSP_JOY_Init(void) |
kashish_mbed | 0:bacc6e701fb4 | 844 | { |
kashish_mbed | 0:bacc6e701fb4 | 845 | if (ADCx_Init() != HAL_OK) |
kashish_mbed | 0:bacc6e701fb4 | 846 | { |
kashish_mbed | 0:bacc6e701fb4 | 847 | return (uint8_t) HAL_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 848 | } |
kashish_mbed | 0:bacc6e701fb4 | 849 | |
kashish_mbed | 0:bacc6e701fb4 | 850 | /* Select Channel 8 to be converted */ |
kashish_mbed | 0:bacc6e701fb4 | 851 | sConfig.Channel = ADC_CHANNEL_8; |
kashish_mbed | 0:bacc6e701fb4 | 852 | sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; |
kashish_mbed | 0:bacc6e701fb4 | 853 | |
kashish_mbed | 0:bacc6e701fb4 | 854 | /* Return Joystick initialization status */ |
kashish_mbed | 0:bacc6e701fb4 | 855 | return (uint8_t) HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig); |
kashish_mbed | 0:bacc6e701fb4 | 856 | } |
kashish_mbed | 0:bacc6e701fb4 | 857 | |
kashish_mbed | 0:bacc6e701fb4 | 858 | /** |
kashish_mbed | 0:bacc6e701fb4 | 859 | * @brief DeInit joystick GPIOs. |
kashish_mbed | 0:bacc6e701fb4 | 860 | * @note JOY DeInit does not disable the Mfx, just set the Mfx pins in Off mode |
kashish_mbed | 0:bacc6e701fb4 | 861 | * @retval None. |
kashish_mbed | 0:bacc6e701fb4 | 862 | */ |
kashish_mbed | 0:bacc6e701fb4 | 863 | void BSP_JOY_DeInit(void) |
kashish_mbed | 0:bacc6e701fb4 | 864 | { |
kashish_mbed | 0:bacc6e701fb4 | 865 | ADCx_DeInit(); |
kashish_mbed | 0:bacc6e701fb4 | 866 | } |
kashish_mbed | 0:bacc6e701fb4 | 867 | |
kashish_mbed | 0:bacc6e701fb4 | 868 | /** |
kashish_mbed | 0:bacc6e701fb4 | 869 | * @brief Returns the Joystick key pressed. |
kashish_mbed | 0:bacc6e701fb4 | 870 | * @note To know which Joystick key is pressed we need to detect the voltage |
kashish_mbed | 0:bacc6e701fb4 | 871 | * level on each key output |
kashish_mbed | 0:bacc6e701fb4 | 872 | * - None : 3.3 V / 4095 |
kashish_mbed | 0:bacc6e701fb4 | 873 | * - SEL : 1.055 V / 1308 |
kashish_mbed | 0:bacc6e701fb4 | 874 | * - DOWN : 0.71 V / 88 |
kashish_mbed | 0:bacc6e701fb4 | 875 | * - LEFT : 3.0 V / 3720 |
kashish_mbed | 0:bacc6e701fb4 | 876 | * - RIGHT : 0.595 V / 737 |
kashish_mbed | 0:bacc6e701fb4 | 877 | * - UP : 1.65 V / 2046 |
kashish_mbed | 0:bacc6e701fb4 | 878 | * @retval JOYState_TypeDef: Code of the Joystick key pressed. |
kashish_mbed | 0:bacc6e701fb4 | 879 | */ |
kashish_mbed | 0:bacc6e701fb4 | 880 | JOYState_TypeDef BSP_JOY_GetState(void) |
kashish_mbed | 0:bacc6e701fb4 | 881 | { |
kashish_mbed | 0:bacc6e701fb4 | 882 | JOYState_TypeDef state = JOY_NONE; |
kashish_mbed | 0:bacc6e701fb4 | 883 | uint16_t keyconvertedvalue = 0; |
kashish_mbed | 0:bacc6e701fb4 | 884 | |
kashish_mbed | 0:bacc6e701fb4 | 885 | /* Start the conversion process */ |
kashish_mbed | 0:bacc6e701fb4 | 886 | HAL_ADC_Start(&hnucleo_Adc); |
kashish_mbed | 0:bacc6e701fb4 | 887 | |
kashish_mbed | 0:bacc6e701fb4 | 888 | /* Wait for the end of conversion */ |
kashish_mbed | 0:bacc6e701fb4 | 889 | if (HAL_ADC_PollForConversion(&hnucleo_Adc, 10) != HAL_TIMEOUT) |
kashish_mbed | 0:bacc6e701fb4 | 890 | { |
kashish_mbed | 0:bacc6e701fb4 | 891 | /* Get the converted value of regular channel */ |
kashish_mbed | 0:bacc6e701fb4 | 892 | keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc); |
kashish_mbed | 0:bacc6e701fb4 | 893 | } |
kashish_mbed | 0:bacc6e701fb4 | 894 | |
kashish_mbed | 0:bacc6e701fb4 | 895 | if((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090)) |
kashish_mbed | 0:bacc6e701fb4 | 896 | { |
kashish_mbed | 0:bacc6e701fb4 | 897 | state = JOY_UP; |
kashish_mbed | 0:bacc6e701fb4 | 898 | } |
kashish_mbed | 0:bacc6e701fb4 | 899 | else if((keyconvertedvalue > 680) && (keyconvertedvalue < 780)) |
kashish_mbed | 0:bacc6e701fb4 | 900 | { |
kashish_mbed | 0:bacc6e701fb4 | 901 | state = JOY_RIGHT; |
kashish_mbed | 0:bacc6e701fb4 | 902 | } |
kashish_mbed | 0:bacc6e701fb4 | 903 | else if((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350)) |
kashish_mbed | 0:bacc6e701fb4 | 904 | { |
kashish_mbed | 0:bacc6e701fb4 | 905 | state = JOY_SEL; |
kashish_mbed | 0:bacc6e701fb4 | 906 | } |
kashish_mbed | 0:bacc6e701fb4 | 907 | else if((keyconvertedvalue > 50) && (keyconvertedvalue < 130)) |
kashish_mbed | 0:bacc6e701fb4 | 908 | { |
kashish_mbed | 0:bacc6e701fb4 | 909 | state = JOY_DOWN; |
kashish_mbed | 0:bacc6e701fb4 | 910 | } |
kashish_mbed | 0:bacc6e701fb4 | 911 | else if((keyconvertedvalue > 3570) && (keyconvertedvalue < 3800)) |
kashish_mbed | 0:bacc6e701fb4 | 912 | { |
kashish_mbed | 0:bacc6e701fb4 | 913 | state = JOY_LEFT; |
kashish_mbed | 0:bacc6e701fb4 | 914 | } |
kashish_mbed | 0:bacc6e701fb4 | 915 | else |
kashish_mbed | 0:bacc6e701fb4 | 916 | { |
kashish_mbed | 0:bacc6e701fb4 | 917 | state = JOY_NONE; |
kashish_mbed | 0:bacc6e701fb4 | 918 | } |
kashish_mbed | 0:bacc6e701fb4 | 919 | |
kashish_mbed | 0:bacc6e701fb4 | 920 | /* Return the code of the Joystick key pressed */ |
kashish_mbed | 0:bacc6e701fb4 | 921 | return state; |
kashish_mbed | 0:bacc6e701fb4 | 922 | } |
kashish_mbed | 0:bacc6e701fb4 | 923 | #endif /* HAL_ADC_MODULE_ENABLED */ |
kashish_mbed | 0:bacc6e701fb4 | 924 | |
kashish_mbed | 0:bacc6e701fb4 | 925 | /** |
kashish_mbed | 0:bacc6e701fb4 | 926 | * @} |
kashish_mbed | 0:bacc6e701fb4 | 927 | */ |
kashish_mbed | 0:bacc6e701fb4 | 928 | |
kashish_mbed | 0:bacc6e701fb4 | 929 | /** |
kashish_mbed | 0:bacc6e701fb4 | 930 | * @} |
kashish_mbed | 0:bacc6e701fb4 | 931 | */ |
kashish_mbed | 0:bacc6e701fb4 | 932 | |
kashish_mbed | 0:bacc6e701fb4 | 933 | /** |
kashish_mbed | 0:bacc6e701fb4 | 934 | * @} |
kashish_mbed | 0:bacc6e701fb4 | 935 | */ |
kashish_mbed | 0:bacc6e701fb4 | 936 | |
kashish_mbed | 0:bacc6e701fb4 | 937 | /** |
kashish_mbed | 0:bacc6e701fb4 | 938 | * @} |
kashish_mbed | 0:bacc6e701fb4 | 939 | */ |
kashish_mbed | 0:bacc6e701fb4 | 940 | |
kashish_mbed | 0:bacc6e701fb4 | 941 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
kashish_mbed | 0:bacc6e701fb4 | 942 |