A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.

Dependencies:   mbed

Committer:
fahadmirza
Date:
Thu Jan 24 23:45:12 2019 +0000
Revision:
39:cb0e5a76ab15
Parent:
5:53302861bfea
Doxygen style header

Who changed what in which revision?

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