WILLY BAYOT / stm32_adafruit

Dependents:   TDEMNucleo

Committer:
willybayot
Date:
Mon Jan 05 16:07:19 2015 +0000
Revision:
3:d685bfd3ba5f
Parent:
2:1be1ed63e942
v

Who changed what in which revision?

UserRevisionLine numberNew contents of line
willybayot 0:4db361f2e6d5 1 /**
willybayot 0:4db361f2e6d5 2 ******************************************************************************
willybayot 0:4db361f2e6d5 3 * @file stm32f4xx_nucleo.c
willybayot 0:4db361f2e6d5 4 * @author MCD Application Team
willybayot 0:4db361f2e6d5 5 * @version V1.1.0
willybayot 0:4db361f2e6d5 6 * @date 19-June-2014
willybayot 0:4db361f2e6d5 7 * @brief This file provides set of firmware functions to manage:
willybayot 0:4db361f2e6d5 8 * - LEDs and push-button available on STM32F4XX-Nucleo Kit
willybayot 0:4db361f2e6d5 9 * from STMicroelectronics
willybayot 0:4db361f2e6d5 10 * - LCD, joystick and microSD available on Adafruit 1.8" TFT LCD
willybayot 0:4db361f2e6d5 11 * shield (reference ID 802)
willybayot 0:4db361f2e6d5 12 ******************************************************************************
willybayot 0:4db361f2e6d5 13 * @attention
willybayot 0:4db361f2e6d5 14 *
willybayot 0:4db361f2e6d5 15 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
willybayot 0:4db361f2e6d5 16 *
willybayot 0:4db361f2e6d5 17 * Redistribution and use in source and binary forms, with or without modification,
willybayot 0:4db361f2e6d5 18 * are permitted provided that the following conditions are met:
willybayot 0:4db361f2e6d5 19 * 1. Redistributions of source code must retain the above copyright notice,
willybayot 0:4db361f2e6d5 20 * this list of conditions and the following disclaimer.
willybayot 0:4db361f2e6d5 21 * 2. Redistributions in binary form must reproduce the above copyright notice,
willybayot 0:4db361f2e6d5 22 * this list of conditions and the following disclaimer in the documentation
willybayot 0:4db361f2e6d5 23 * and/or other materials provided with the distribution.
willybayot 0:4db361f2e6d5 24 * 3. Neither the name of STMicroelectronics nor the names of its contributors
willybayot 0:4db361f2e6d5 25 * may be used to endorse or promote products derived from this software
willybayot 0:4db361f2e6d5 26 * without specific prior written permission.
willybayot 0:4db361f2e6d5 27 *
willybayot 0:4db361f2e6d5 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
willybayot 0:4db361f2e6d5 29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
willybayot 0:4db361f2e6d5 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
willybayot 0:4db361f2e6d5 31 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
willybayot 0:4db361f2e6d5 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
willybayot 0:4db361f2e6d5 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
willybayot 0:4db361f2e6d5 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
willybayot 0:4db361f2e6d5 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
willybayot 0:4db361f2e6d5 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
willybayot 0:4db361f2e6d5 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
willybayot 0:4db361f2e6d5 38 *
willybayot 0:4db361f2e6d5 39 ******************************************************************************
willybayot 0:4db361f2e6d5 40 */
willybayot 0:4db361f2e6d5 41
willybayot 0:4db361f2e6d5 42 /* Includes ------------------------------------------------------------------*/
willybayot 0:4db361f2e6d5 43 #include "stm32f4xx_nucleo.h"
willybayot 0:4db361f2e6d5 44
willybayot 0:4db361f2e6d5 45 /** @addtogroup BSP
willybayot 0:4db361f2e6d5 46 * @{
willybayot 0:4db361f2e6d5 47 */
willybayot 0:4db361f2e6d5 48
willybayot 0:4db361f2e6d5 49 /** @addtogroup STM32F4XX_NUCLEO
willybayot 0:4db361f2e6d5 50 * @{
willybayot 0:4db361f2e6d5 51 */
willybayot 0:4db361f2e6d5 52
willybayot 0:4db361f2e6d5 53 /** @addtogroup STM32F4XX_NUCLEO_LOW_LEVEL
willybayot 0:4db361f2e6d5 54 * @brief This file provides set of firmware functions to manage Leds and push-button
willybayot 0:4db361f2e6d5 55 * available on STM32F4xx-Nucleo Kit from STMicroelectronics.
willybayot 0:4db361f2e6d5 56 * @{
willybayot 0:4db361f2e6d5 57 */
willybayot 0:4db361f2e6d5 58
willybayot 0:4db361f2e6d5 59 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_TypesDefinitions
willybayot 0:4db361f2e6d5 60 * @{
willybayot 0:4db361f2e6d5 61 */
willybayot 0:4db361f2e6d5 62 /**
willybayot 0:4db361f2e6d5 63 * @}
willybayot 0:4db361f2e6d5 64 */
willybayot 0:4db361f2e6d5 65
willybayot 0:4db361f2e6d5 66
willybayot 0:4db361f2e6d5 67 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Defines
willybayot 0:4db361f2e6d5 68 * @{
willybayot 0:4db361f2e6d5 69 */
willybayot 0:4db361f2e6d5 70
willybayot 0:4db361f2e6d5 71 /**
willybayot 0:4db361f2e6d5 72 * @brief STM32F4xx NUCLEO BSP Driver version number V1.1.0
willybayot 0:4db361f2e6d5 73 */
willybayot 0:4db361f2e6d5 74 #define __STM32F4xx_NUCLEO_BSP_VERSION_MAIN (0x01) /*!< [31:24] main version */
willybayot 0:4db361f2e6d5 75 #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
willybayot 0:4db361f2e6d5 76 #define __STM32F4xx_NUCLEO_BSP_VERSION_SUB2 (0x00) /*!< [15:8] sub2 version */
willybayot 0:4db361f2e6d5 77 #define __STM32F4xx_NUCLEO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
willybayot 0:4db361f2e6d5 78 #define __STM32F4xx_NUCLEO_BSP_VERSION ((__STM32F4xx_NUCLEO_BSP_VERSION_MAIN << 24)\
willybayot 0:4db361f2e6d5 79 |(__STM32F4xx_NUCLEO_BSP_VERSION_SUB1 << 16)\
willybayot 0:4db361f2e6d5 80 |(__STM32F4xx_NUCLEO_BSP_VERSION_SUB2 << 8 )\
willybayot 0:4db361f2e6d5 81 |(__STM32F4xx_NUCLEO_BSP_VERSION_RC))
willybayot 0:4db361f2e6d5 82
willybayot 0:4db361f2e6d5 83 /**
willybayot 0:4db361f2e6d5 84 * @brief LINK SD Card
willybayot 0:4db361f2e6d5 85 */
willybayot 0:4db361f2e6d5 86 #define SD_DUMMY_BYTE 0xFF
willybayot 0:4db361f2e6d5 87 #define SD_NO_RESPONSE_EXPECTED 0x80
willybayot 0:4db361f2e6d5 88
willybayot 0:4db361f2e6d5 89 /**
willybayot 0:4db361f2e6d5 90 * @}
willybayot 0:4db361f2e6d5 91 */
willybayot 0:4db361f2e6d5 92
willybayot 0:4db361f2e6d5 93 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Macros
willybayot 0:4db361f2e6d5 94 * @{
willybayot 0:4db361f2e6d5 95 */
willybayot 0:4db361f2e6d5 96 /**
willybayot 0:4db361f2e6d5 97 * @}
willybayot 0:4db361f2e6d5 98 */
willybayot 0:4db361f2e6d5 99
willybayot 0:4db361f2e6d5 100 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Variables
willybayot 0:4db361f2e6d5 101 * @{
willybayot 0:4db361f2e6d5 102 */
willybayot 0:4db361f2e6d5 103 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
willybayot 0:4db361f2e6d5 104
willybayot 0:4db361f2e6d5 105 const uint16_t GPIO_PIN[LEDn] = {LED2_PIN};
willybayot 0:4db361f2e6d5 106
willybayot 0:4db361f2e6d5 107 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
willybayot 0:4db361f2e6d5 108 const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
willybayot 0:4db361f2e6d5 109 const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
willybayot 0:4db361f2e6d5 110
willybayot 0:4db361f2e6d5 111 /**
willybayot 0:4db361f2e6d5 112 * @brief BUS variables
willybayot 0:4db361f2e6d5 113 */
willybayot 0:4db361f2e6d5 114
willybayot 0:4db361f2e6d5 115 uint32_t SpixTimeout = NUCLEO_SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
willybayot 0:4db361f2e6d5 116 static SPI_HandleTypeDef hnucleo_Spi;
willybayot 0:4db361f2e6d5 117 static ADC_HandleTypeDef hnucleo_Adc;
willybayot 0:4db361f2e6d5 118
willybayot 0:4db361f2e6d5 119 /* ADC channel configuration structure declaration */
willybayot 0:4db361f2e6d5 120 static ADC_ChannelConfTypeDef sConfig;
willybayot 0:4db361f2e6d5 121
willybayot 0:4db361f2e6d5 122 /**
willybayot 0:4db361f2e6d5 123 * @}
willybayot 0:4db361f2e6d5 124 */
willybayot 0:4db361f2e6d5 125
willybayot 0:4db361f2e6d5 126 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_FunctionPrototypes
willybayot 0:4db361f2e6d5 127 * @{
willybayot 0:4db361f2e6d5 128 */
willybayot 0:4db361f2e6d5 129 static void SPIx_Init(void);
willybayot 0:4db361f2e6d5 130 static void SPIx_Write(uint8_t Value);
willybayot 0:4db361f2e6d5 131 static uint32_t SPIx_Read(void);
willybayot 0:4db361f2e6d5 132 static void SPIx_Error(void);
willybayot 0:4db361f2e6d5 133 static void SPIx_MspInit(SPI_HandleTypeDef *hspi);
willybayot 0:4db361f2e6d5 134
willybayot 0:4db361f2e6d5 135 static void ADCx_Init(void);
willybayot 0:4db361f2e6d5 136 static void ADCx_MspInit(ADC_HandleTypeDef *hadc);
willybayot 0:4db361f2e6d5 137
willybayot 0:4db361f2e6d5 138 /* SD IO functions */
willybayot 0:4db361f2e6d5 139 void SD_IO_Init(void);
willybayot 0:4db361f2e6d5 140 HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response);
willybayot 0:4db361f2e6d5 141 HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response);
willybayot 0:4db361f2e6d5 142 void SD_IO_WriteDummy(void);
willybayot 0:4db361f2e6d5 143 void SD_IO_WriteByte(uint8_t Data);
willybayot 0:4db361f2e6d5 144 uint8_t SD_IO_ReadByte(void);
willybayot 0:4db361f2e6d5 145
willybayot 0:4db361f2e6d5 146 /* LCD IO functions */
willybayot 0:4db361f2e6d5 147 void LCD_IO_Init(void);
willybayot 0:4db361f2e6d5 148 void LCD_IO_WriteData(uint8_t Data);
willybayot 0:4db361f2e6d5 149 void LCD_IO_WriteReg(uint8_t LCDReg);
willybayot 0:4db361f2e6d5 150 void LCD_Delay(uint32_t delay);
willybayot 0:4db361f2e6d5 151 /**
willybayot 0:4db361f2e6d5 152 * @}
willybayot 0:4db361f2e6d5 153 */
willybayot 0:4db361f2e6d5 154
willybayot 0:4db361f2e6d5 155 /** @defgroup STM32F4XX_NUCLEO_LOW_LEVEL_Private_Functions
willybayot 0:4db361f2e6d5 156 * @{
willybayot 0:4db361f2e6d5 157 */
willybayot 0:4db361f2e6d5 158
willybayot 0:4db361f2e6d5 159 /**
willybayot 0:4db361f2e6d5 160 * @brief This method returns the STM32F4xx NUCLEO BSP Driver revision
willybayot 0:4db361f2e6d5 161 * @param None
willybayot 0:4db361f2e6d5 162 * @retval version: 0xXYZR (8bits for each decimal, R for RC)
willybayot 0:4db361f2e6d5 163 */
willybayot 0:4db361f2e6d5 164 uint32_t BSP_GetVersion(void)
willybayot 0:4db361f2e6d5 165 {
willybayot 0:4db361f2e6d5 166 return __STM32F4xx_NUCLEO_BSP_VERSION;
willybayot 0:4db361f2e6d5 167 }
willybayot 0:4db361f2e6d5 168
willybayot 0:4db361f2e6d5 169 /**
willybayot 0:4db361f2e6d5 170 * @brief Configures LED GPIO.
willybayot 0:4db361f2e6d5 171 * @param Led: Specifies the Led to be configured.
willybayot 0:4db361f2e6d5 172 * This parameter can be one of following parameters:
willybayot 0:4db361f2e6d5 173 * @arg LED2
willybayot 0:4db361f2e6d5 174 * @retval None
willybayot 0:4db361f2e6d5 175 */
willybayot 0:4db361f2e6d5 176 void BSP_LED_Init(Led_TypeDef Led)
willybayot 0:4db361f2e6d5 177 {
willybayot 0:4db361f2e6d5 178 GPIO_InitTypeDef GPIO_InitStruct;
willybayot 0:4db361f2e6d5 179
willybayot 0:4db361f2e6d5 180 /* Enable the GPIO_LED Clock */
willybayot 0:4db361f2e6d5 181 LEDx_GPIO_CLK_ENABLE(Led);
willybayot 0:4db361f2e6d5 182
willybayot 0:4db361f2e6d5 183 /* Configure the GPIO_LED pin */
willybayot 0:4db361f2e6d5 184 GPIO_InitStruct.Pin = GPIO_PIN[Led];
willybayot 0:4db361f2e6d5 185 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
willybayot 0:4db361f2e6d5 186 GPIO_InitStruct.Pull = GPIO_PULLUP;
willybayot 0:4db361f2e6d5 187 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
willybayot 0:4db361f2e6d5 188
willybayot 0:4db361f2e6d5 189 HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 190
willybayot 0:4db361f2e6d5 191 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
willybayot 0:4db361f2e6d5 192 }
willybayot 0:4db361f2e6d5 193
willybayot 0:4db361f2e6d5 194 /**
willybayot 0:4db361f2e6d5 195 * @brief Turns selected LED On.
willybayot 0:4db361f2e6d5 196 * @param Led: Specifies the Led to be set on.
willybayot 0:4db361f2e6d5 197 * This parameter can be one of following parameters:
willybayot 0:4db361f2e6d5 198 * @arg LED2
willybayot 0:4db361f2e6d5 199 * @retval None
willybayot 0:4db361f2e6d5 200 */
willybayot 0:4db361f2e6d5 201 void BSP_LED_On(Led_TypeDef Led)
willybayot 0:4db361f2e6d5 202 {
willybayot 0:4db361f2e6d5 203 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
willybayot 0:4db361f2e6d5 204 }
willybayot 0:4db361f2e6d5 205
willybayot 0:4db361f2e6d5 206 /**
willybayot 0:4db361f2e6d5 207 * @brief Turns selected LED Off.
willybayot 0:4db361f2e6d5 208 * @param Led: Specifies the Led to be set off.
willybayot 0:4db361f2e6d5 209 * This parameter can be one of following parameters:
willybayot 0:4db361f2e6d5 210 * @arg LED2
willybayot 0:4db361f2e6d5 211 * @retval None
willybayot 0:4db361f2e6d5 212 */
willybayot 0:4db361f2e6d5 213 void BSP_LED_Off(Led_TypeDef Led)
willybayot 0:4db361f2e6d5 214 {
willybayot 0:4db361f2e6d5 215 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
willybayot 0:4db361f2e6d5 216 }
willybayot 0:4db361f2e6d5 217
willybayot 0:4db361f2e6d5 218 /**
willybayot 0:4db361f2e6d5 219 * @brief Toggles the selected LED.
willybayot 0:4db361f2e6d5 220 * @param Led: Specifies the Led to be toggled.
willybayot 0:4db361f2e6d5 221 * This parameter can be one of following parameters:
willybayot 0:4db361f2e6d5 222 * @arg LED2
willybayot 0:4db361f2e6d5 223 * @retval None
willybayot 0:4db361f2e6d5 224 */
willybayot 0:4db361f2e6d5 225 void BSP_LED_Toggle(Led_TypeDef Led)
willybayot 0:4db361f2e6d5 226 {
willybayot 0:4db361f2e6d5 227 HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
willybayot 0:4db361f2e6d5 228 }
willybayot 0:4db361f2e6d5 229
willybayot 0:4db361f2e6d5 230 /**
willybayot 0:4db361f2e6d5 231 * @brief Configures Button GPIO and EXTI Line.
willybayot 0:4db361f2e6d5 232 * @param Button: Specifies the Button to be configured.
willybayot 0:4db361f2e6d5 233 * This parameter should be: BUTTON_KEY
willybayot 0:4db361f2e6d5 234 * @param ButtonMode: Specifies Button mode.
willybayot 0:4db361f2e6d5 235 * This parameter can be one of following parameters:
willybayot 0:4db361f2e6d5 236 * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
willybayot 0:4db361f2e6d5 237 * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
willybayot 0:4db361f2e6d5 238 * generation capability
willybayot 0:4db361f2e6d5 239 * @retval None
willybayot 0:4db361f2e6d5 240 */
willybayot 0:4db361f2e6d5 241 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
willybayot 0:4db361f2e6d5 242 {
willybayot 0:4db361f2e6d5 243 GPIO_InitTypeDef GPIO_InitStruct;
willybayot 0:4db361f2e6d5 244
willybayot 0:4db361f2e6d5 245 /* Enable the BUTTON Clock */
willybayot 0:4db361f2e6d5 246 BUTTONx_GPIO_CLK_ENABLE(Button);
willybayot 0:4db361f2e6d5 247
willybayot 0:4db361f2e6d5 248 if(ButtonMode == BUTTON_MODE_GPIO)
willybayot 0:4db361f2e6d5 249 {
willybayot 0:4db361f2e6d5 250 /* Configure Button pin as input */
willybayot 0:4db361f2e6d5 251 GPIO_InitStruct.Pin = BUTTON_PIN[Button];
willybayot 0:4db361f2e6d5 252 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
willybayot 0:4db361f2e6d5 253 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
willybayot 0:4db361f2e6d5 254 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
willybayot 0:4db361f2e6d5 255 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 256 }
willybayot 0:4db361f2e6d5 257
willybayot 0:4db361f2e6d5 258 if(ButtonMode == BUTTON_MODE_EXTI)
willybayot 0:4db361f2e6d5 259 {
willybayot 0:4db361f2e6d5 260 /* Configure Button pin as input with External interrupt */
willybayot 0:4db361f2e6d5 261 GPIO_InitStruct.Pin = BUTTON_PIN[Button];
willybayot 0:4db361f2e6d5 262 GPIO_InitStruct.Pull = GPIO_NOPULL;
willybayot 0:4db361f2e6d5 263 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
willybayot 0:4db361f2e6d5 264 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 265
willybayot 0:4db361f2e6d5 266 /* Enable and set Button EXTI Interrupt to the lowest priority */
willybayot 0:4db361f2e6d5 267 HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
willybayot 0:4db361f2e6d5 268 HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
willybayot 0:4db361f2e6d5 269 }
willybayot 0:4db361f2e6d5 270 }
willybayot 0:4db361f2e6d5 271
willybayot 0:4db361f2e6d5 272 /**
willybayot 0:4db361f2e6d5 273 * @brief Returns the selected Button state.
willybayot 0:4db361f2e6d5 274 * @param Button: Specifies the Button to be checked.
willybayot 0:4db361f2e6d5 275 * This parameter should be: BUTTON_KEY
willybayot 0:4db361f2e6d5 276 * @retval The Button GPIO pin value.
willybayot 0:4db361f2e6d5 277 */
willybayot 0:4db361f2e6d5 278 uint32_t BSP_PB_GetState(Button_TypeDef Button)
willybayot 0:4db361f2e6d5 279 {
willybayot 0:4db361f2e6d5 280 return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
willybayot 0:4db361f2e6d5 281 }
willybayot 0:4db361f2e6d5 282
willybayot 0:4db361f2e6d5 283 /******************************************************************************
willybayot 0:4db361f2e6d5 284 BUS OPERATIONS
willybayot 0:4db361f2e6d5 285 *******************************************************************************/
willybayot 0:4db361f2e6d5 286 /**
willybayot 0:4db361f2e6d5 287 * @brief Initializes SPI MSP.
willybayot 0:4db361f2e6d5 288 * @param None
willybayot 0:4db361f2e6d5 289 * @retval None
willybayot 0:4db361f2e6d5 290 */
willybayot 0:4db361f2e6d5 291 static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
willybayot 0:4db361f2e6d5 292 {
willybayot 0:4db361f2e6d5 293 GPIO_InitTypeDef GPIO_InitStruct;
willybayot 0:4db361f2e6d5 294
willybayot 0:4db361f2e6d5 295 /*** Configure the GPIOs ***/
willybayot 0:4db361f2e6d5 296 /* Enable GPIO clock */
willybayot 0:4db361f2e6d5 297 NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE();
willybayot 0:4db361f2e6d5 298 NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE();
willybayot 0:4db361f2e6d5 299
willybayot 0:4db361f2e6d5 300 /* Configure SPI SCK */
willybayot 0:4db361f2e6d5 301 GPIO_InitStruct.Pin = NUCLEO_SPIx_SCK_PIN;
willybayot 0:4db361f2e6d5 302 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
willybayot 0:4db361f2e6d5 303 GPIO_InitStruct.Pull = GPIO_PULLUP;
willybayot 0:4db361f2e6d5 304 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
willybayot 0:4db361f2e6d5 305 GPIO_InitStruct.Alternate = NUCLEO_SPIx_SCK_AF;
willybayot 0:4db361f2e6d5 306 HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 307
willybayot 0:4db361f2e6d5 308 /* Configure SPI MISO and MOSI */
willybayot 0:4db361f2e6d5 309 GPIO_InitStruct.Pin = NUCLEO_SPIx_MOSI_PIN;
willybayot 0:4db361f2e6d5 310 GPIO_InitStruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF;
willybayot 0:4db361f2e6d5 311 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
willybayot 0:4db361f2e6d5 312 HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 313
willybayot 0:4db361f2e6d5 314 GPIO_InitStruct.Pin = NUCLEO_SPIx_MISO_PIN;
willybayot 0:4db361f2e6d5 315 HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 316
willybayot 0:4db361f2e6d5 317 /*** Configure the SPI peripheral ***/
willybayot 0:4db361f2e6d5 318 /* Enable SPI clock */
willybayot 0:4db361f2e6d5 319 NUCLEO_SPIx_CLK_ENABLE();
willybayot 0:4db361f2e6d5 320 }
willybayot 0:4db361f2e6d5 321
willybayot 0:4db361f2e6d5 322 /**
willybayot 0:4db361f2e6d5 323 * @brief Initializes SPI HAL.
willybayot 0:4db361f2e6d5 324 * @param None
willybayot 0:4db361f2e6d5 325 * @retval None
willybayot 0:4db361f2e6d5 326 */
willybayot 0:4db361f2e6d5 327 static void SPIx_Init(void)
willybayot 0:4db361f2e6d5 328 {
willybayot 0:4db361f2e6d5 329 if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
willybayot 0:4db361f2e6d5 330 {
willybayot 0:4db361f2e6d5 331 /* SPI Config */
willybayot 0:4db361f2e6d5 332 hnucleo_Spi.Instance = NUCLEO_SPIx;
willybayot 0:4db361f2e6d5 333 /* SPI baudrate is set to 12,5 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 100/8 = 12,5 MHz)
willybayot 0:4db361f2e6d5 334 to verify these constraints:
willybayot 0:4db361f2e6d5 335 - ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
willybayot 0:4db361f2e6d5 336 Since the provided driver doesn't use read capability from LCD, only constraint
willybayot 0:4db361f2e6d5 337 on write baudrate is considered.
willybayot 0:4db361f2e6d5 338 - SD card SPI interface max baudrate is 25MHz for write/read
willybayot 0:4db361f2e6d5 339 - PCLK2 max frequency is 100 MHz
willybayot 0:4db361f2e6d5 340 */
willybayot 0:4db361f2e6d5 341 hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
willybayot 0:4db361f2e6d5 342 hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES;
willybayot 0:4db361f2e6d5 343 hnucleo_Spi.Init.CLKPhase = SPI_PHASE_2EDGE;
willybayot 0:4db361f2e6d5 344 hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_HIGH;
willybayot 0:4db361f2e6d5 345 hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
willybayot 0:4db361f2e6d5 346 hnucleo_Spi.Init.CRCPolynomial = 7;
willybayot 0:4db361f2e6d5 347 hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
willybayot 0:4db361f2e6d5 348 hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
willybayot 0:4db361f2e6d5 349 hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
willybayot 0:4db361f2e6d5 350 hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLED;
willybayot 0:4db361f2e6d5 351 hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
willybayot 0:4db361f2e6d5 352
willybayot 0:4db361f2e6d5 353 SPIx_MspInit(&hnucleo_Spi);
willybayot 0:4db361f2e6d5 354 HAL_SPI_Init(&hnucleo_Spi);
willybayot 0:4db361f2e6d5 355 }
willybayot 0:4db361f2e6d5 356 }
willybayot 0:4db361f2e6d5 357
willybayot 0:4db361f2e6d5 358 /**
willybayot 0:4db361f2e6d5 359 * @brief SPI Read 4 bytes from device.
willybayot 0:4db361f2e6d5 360 * @param None
willybayot 0:4db361f2e6d5 361 * @retval Read data
willybayot 0:4db361f2e6d5 362 */
willybayot 0:4db361f2e6d5 363 static uint32_t SPIx_Read(void)
willybayot 0:4db361f2e6d5 364 {
willybayot 0:4db361f2e6d5 365 HAL_StatusTypeDef status = HAL_OK;
willybayot 0:4db361f2e6d5 366 uint32_t readvalue = 0;
willybayot 0:4db361f2e6d5 367 uint32_t writevalue = 0xFFFFFFFF;
willybayot 0:4db361f2e6d5 368
willybayot 0:4db361f2e6d5 369 status = HAL_SPI_TransmitReceive(&hnucleo_Spi, (uint8_t*) &writevalue, (uint8_t*) &readvalue, 1, SpixTimeout);
willybayot 0:4db361f2e6d5 370
willybayot 0:4db361f2e6d5 371 /* Check the communication status */
willybayot 0:4db361f2e6d5 372 if(status != HAL_OK)
willybayot 0:4db361f2e6d5 373 {
willybayot 0:4db361f2e6d5 374 /* Execute user timeout callback */
willybayot 0:4db361f2e6d5 375 SPIx_Error();
willybayot 0:4db361f2e6d5 376 }
willybayot 0:4db361f2e6d5 377
willybayot 0:4db361f2e6d5 378 return readvalue;
willybayot 0:4db361f2e6d5 379 }
willybayot 0:4db361f2e6d5 380
willybayot 0:4db361f2e6d5 381 /**
willybayot 0:4db361f2e6d5 382 * @brief SPI Write a byte to device.
willybayot 0:4db361f2e6d5 383 * @param Value: value to be written
willybayot 0:4db361f2e6d5 384 * @retval None
willybayot 0:4db361f2e6d5 385 */
willybayot 0:4db361f2e6d5 386 static void SPIx_Write(uint8_t Value)
willybayot 0:4db361f2e6d5 387 {
willybayot 0:4db361f2e6d5 388 HAL_StatusTypeDef status = HAL_OK;
willybayot 0:4db361f2e6d5 389
willybayot 0:4db361f2e6d5 390 status = HAL_SPI_Transmit(&hnucleo_Spi, (uint8_t*) &Value, 1, SpixTimeout);
willybayot 0:4db361f2e6d5 391
willybayot 0:4db361f2e6d5 392 /* Check the communication status */
willybayot 0:4db361f2e6d5 393 if(status != HAL_OK)
willybayot 0:4db361f2e6d5 394 {
willybayot 0:4db361f2e6d5 395 /* Execute user timeout callback */
willybayot 0:4db361f2e6d5 396 SPIx_Error();
willybayot 0:4db361f2e6d5 397 }
willybayot 0:4db361f2e6d5 398 }
willybayot 0:4db361f2e6d5 399
willybayot 0:4db361f2e6d5 400 /**
willybayot 0:4db361f2e6d5 401 * @brief SPI error treatment function.
willybayot 0:4db361f2e6d5 402 * @param None
willybayot 0:4db361f2e6d5 403 * @retval None
willybayot 0:4db361f2e6d5 404 */
willybayot 0:4db361f2e6d5 405 static void SPIx_Error (void)
willybayot 0:4db361f2e6d5 406 {
willybayot 0:4db361f2e6d5 407 /* De-initialize the SPI communication BUS */
willybayot 0:4db361f2e6d5 408 HAL_SPI_DeInit(&hnucleo_Spi);
willybayot 0:4db361f2e6d5 409
willybayot 0:4db361f2e6d5 410 /* Re-Initiaize the SPI communication BUS */
willybayot 0:4db361f2e6d5 411 SPIx_Init();
willybayot 0:4db361f2e6d5 412 }
willybayot 0:4db361f2e6d5 413
willybayot 0:4db361f2e6d5 414 /******************************************************************************
willybayot 0:4db361f2e6d5 415 LINK OPERATIONS
willybayot 0:4db361f2e6d5 416 *******************************************************************************/
willybayot 0:4db361f2e6d5 417
willybayot 0:4db361f2e6d5 418 /********************************* LINK SD ************************************/
willybayot 0:4db361f2e6d5 419 /**
willybayot 0:4db361f2e6d5 420 * @brief Initializes the SD Card and put it into StandBy State (Ready for
willybayot 0:4db361f2e6d5 421 * data transfer).
willybayot 0:4db361f2e6d5 422 * @param None
willybayot 0:4db361f2e6d5 423 * @retval None
willybayot 0:4db361f2e6d5 424 */
willybayot 0:4db361f2e6d5 425 void SD_IO_Init(void)
willybayot 0:4db361f2e6d5 426 {
willybayot 0:4db361f2e6d5 427 GPIO_InitTypeDef GPIO_InitStruct;
willybayot 0:4db361f2e6d5 428 uint8_t counter;
willybayot 0:4db361f2e6d5 429
willybayot 0:4db361f2e6d5 430 /* SD_CS_GPIO Periph clock enable */
willybayot 0:4db361f2e6d5 431 SD_CS_GPIO_CLK_ENABLE();
willybayot 0:4db361f2e6d5 432
willybayot 0:4db361f2e6d5 433 /* Configure SD_CS_PIN pin: SD Card CS pin */
willybayot 0:4db361f2e6d5 434 GPIO_InitStruct.Pin = SD_CS_PIN;
willybayot 0:4db361f2e6d5 435 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
willybayot 0:4db361f2e6d5 436 GPIO_InitStruct.Pull = GPIO_PULLUP;
willybayot 0:4db361f2e6d5 437 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
willybayot 0:4db361f2e6d5 438 HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 439
willybayot 0:4db361f2e6d5 440 /*------------Put SD in SPI mode--------------*/
willybayot 0:4db361f2e6d5 441 /* SD SPI Config */
willybayot 2:1be1ed63e942 442 SPIx_Init();
willybayot 0:4db361f2e6d5 443
willybayot 0:4db361f2e6d5 444 /* SD chip select high */
willybayot 0:4db361f2e6d5 445 SD_CS_HIGH();
willybayot 0:4db361f2e6d5 446
willybayot 0:4db361f2e6d5 447 /* Send dummy byte 0xFF, 10 times with CS high */
willybayot 0:4db361f2e6d5 448 /* Rise CS and MOSI for 80 clocks cycles */
willybayot 0:4db361f2e6d5 449 for (counter = 0; counter <= 9; counter++)
willybayot 0:4db361f2e6d5 450 {
willybayot 0:4db361f2e6d5 451 /* Send dummy byte 0xFF */
willybayot 0:4db361f2e6d5 452 SD_IO_WriteByte(SD_DUMMY_BYTE);
willybayot 0:4db361f2e6d5 453 }
willybayot 0:4db361f2e6d5 454 }
willybayot 0:4db361f2e6d5 455
willybayot 0:4db361f2e6d5 456 /**
willybayot 0:4db361f2e6d5 457 * @brief Writes a byte on the SD.
willybayot 0:4db361f2e6d5 458 * @param Data: byte to send.
willybayot 0:4db361f2e6d5 459 * @retval None
willybayot 0:4db361f2e6d5 460 */
willybayot 0:4db361f2e6d5 461 void SD_IO_WriteByte(uint8_t Data)
willybayot 0:4db361f2e6d5 462 {
willybayot 0:4db361f2e6d5 463 /* Send the byte */
willybayot 0:4db361f2e6d5 464 SPIx_Write(Data);
willybayot 0:4db361f2e6d5 465 }
willybayot 0:4db361f2e6d5 466
willybayot 0:4db361f2e6d5 467 /**
willybayot 0:4db361f2e6d5 468 * @brief Reads a byte from the SD.
willybayot 0:4db361f2e6d5 469 * @param None
willybayot 0:4db361f2e6d5 470 * @retval The received byte.
willybayot 0:4db361f2e6d5 471 */
willybayot 0:4db361f2e6d5 472 uint8_t SD_IO_ReadByte(void)
willybayot 0:4db361f2e6d5 473 {
willybayot 0:4db361f2e6d5 474 uint8_t data = 0;
willybayot 0:4db361f2e6d5 475
willybayot 0:4db361f2e6d5 476 /* Get the received data */
willybayot 0:4db361f2e6d5 477 data = SPIx_Read();
willybayot 0:4db361f2e6d5 478
willybayot 0:4db361f2e6d5 479 /* Return the shifted data */
willybayot 0:4db361f2e6d5 480 return data;
willybayot 0:4db361f2e6d5 481 }
willybayot 0:4db361f2e6d5 482
willybayot 0:4db361f2e6d5 483 /**
willybayot 0:4db361f2e6d5 484 * @brief Sends 5 bytes command to the SD card and get response.
willybayot 0:4db361f2e6d5 485 * @param Cmd: The user expected command to send to SD card.
willybayot 0:4db361f2e6d5 486 * @param Arg: The command argument
willybayot 0:4db361f2e6d5 487 * @param Crc: The CRC
willybayot 0:4db361f2e6d5 488 * @param Response: Expected response from the SD card
willybayot 0:4db361f2e6d5 489 * @retval HAL_StatusTypeDef HAL Status
willybayot 0:4db361f2e6d5 490 */
willybayot 0:4db361f2e6d5 491 HAL_StatusTypeDef SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response)
willybayot 0:4db361f2e6d5 492 {
willybayot 0:4db361f2e6d5 493 uint32_t counter = 0x00;
willybayot 0:4db361f2e6d5 494 uint8_t frame[6];
willybayot 0:4db361f2e6d5 495
willybayot 0:4db361f2e6d5 496 /* Prepare Frame to send */
willybayot 0:4db361f2e6d5 497 frame[0] = (Cmd | 0x40); /* Construct byte 1 */
willybayot 0:4db361f2e6d5 498 frame[1] = (uint8_t)(Arg >> 24); /* Construct byte 2 */
willybayot 0:4db361f2e6d5 499 frame[2] = (uint8_t)(Arg >> 16); /* Construct byte 3 */
willybayot 0:4db361f2e6d5 500 frame[3] = (uint8_t)(Arg >> 8); /* Construct byte 4 */
willybayot 0:4db361f2e6d5 501 frame[4] = (uint8_t)(Arg); /* Construct byte 5 */
willybayot 0:4db361f2e6d5 502 frame[5] = (Crc); /* Construct byte 6 */
willybayot 0:4db361f2e6d5 503
willybayot 0:4db361f2e6d5 504 /* SD chip select low */
willybayot 0:4db361f2e6d5 505 SD_CS_LOW();
willybayot 0:4db361f2e6d5 506
willybayot 0:4db361f2e6d5 507 /* Send Frame */
willybayot 0:4db361f2e6d5 508 for (counter = 0; counter < 6; counter++)
willybayot 0:4db361f2e6d5 509 {
willybayot 0:4db361f2e6d5 510 SD_IO_WriteByte(frame[counter]); /* Send the Cmd bytes */
willybayot 0:4db361f2e6d5 511 }
willybayot 0:4db361f2e6d5 512
willybayot 0:4db361f2e6d5 513 if(Response != SD_NO_RESPONSE_EXPECTED)
willybayot 0:4db361f2e6d5 514 {
willybayot 0:4db361f2e6d5 515 return SD_IO_WaitResponse(Response);
willybayot 0:4db361f2e6d5 516 }
willybayot 0:4db361f2e6d5 517
willybayot 0:4db361f2e6d5 518 return HAL_OK;
willybayot 0:4db361f2e6d5 519 }
willybayot 0:4db361f2e6d5 520
willybayot 0:4db361f2e6d5 521 /**
willybayot 0:4db361f2e6d5 522 * @brief Waits response from the SD card
willybayot 0:4db361f2e6d5 523 * @param Response: Expected response from the SD card
willybayot 0:4db361f2e6d5 524 * @retval HAL_StatusTypeDef HAL Status
willybayot 0:4db361f2e6d5 525 */
willybayot 0:4db361f2e6d5 526 HAL_StatusTypeDef SD_IO_WaitResponse(uint8_t Response)
willybayot 0:4db361f2e6d5 527 {
willybayot 0:4db361f2e6d5 528 uint32_t timeout = 0xFFFF;
willybayot 0:4db361f2e6d5 529
willybayot 0:4db361f2e6d5 530 /* Check if response is got or a timeout is happen */
willybayot 0:4db361f2e6d5 531 while ((SD_IO_ReadByte() != Response) && timeout)
willybayot 0:4db361f2e6d5 532 {
willybayot 0:4db361f2e6d5 533 timeout--;
willybayot 0:4db361f2e6d5 534 }
willybayot 0:4db361f2e6d5 535
willybayot 0:4db361f2e6d5 536 if (timeout == 0)
willybayot 0:4db361f2e6d5 537 {
willybayot 0:4db361f2e6d5 538 /* After time out */
willybayot 0:4db361f2e6d5 539 return HAL_TIMEOUT;
willybayot 0:4db361f2e6d5 540 }
willybayot 0:4db361f2e6d5 541 else
willybayot 0:4db361f2e6d5 542 {
willybayot 0:4db361f2e6d5 543 /* Right response got */
willybayot 0:4db361f2e6d5 544 return HAL_OK;
willybayot 0:4db361f2e6d5 545 }
willybayot 0:4db361f2e6d5 546 }
willybayot 0:4db361f2e6d5 547
willybayot 0:4db361f2e6d5 548 /**
willybayot 0:4db361f2e6d5 549 * @brief Sends dummy byte with CS High.
willybayot 0:4db361f2e6d5 550 * @param None
willybayot 0:4db361f2e6d5 551 * @retval None
willybayot 0:4db361f2e6d5 552 */
willybayot 0:4db361f2e6d5 553 void SD_IO_WriteDummy(void)
willybayot 0:4db361f2e6d5 554 {
willybayot 0:4db361f2e6d5 555 /* SD chip select high */
willybayot 0:4db361f2e6d5 556 SD_CS_HIGH();
willybayot 0:4db361f2e6d5 557
willybayot 0:4db361f2e6d5 558 /* Send Dummy byte 0xFF */
willybayot 0:4db361f2e6d5 559 SD_IO_WriteByte(SD_DUMMY_BYTE);
willybayot 0:4db361f2e6d5 560 }
willybayot 0:4db361f2e6d5 561
willybayot 0:4db361f2e6d5 562 /********************************* LINK LCD ***********************************/
willybayot 0:4db361f2e6d5 563 /**
willybayot 0:4db361f2e6d5 564 * @brief Initializes the LCD.
willybayot 0:4db361f2e6d5 565 * @param None
willybayot 0:4db361f2e6d5 566 * @retval None
willybayot 0:4db361f2e6d5 567 */
willybayot 0:4db361f2e6d5 568 void LCD_IO_Init(void)
willybayot 0:4db361f2e6d5 569 {
willybayot 0:4db361f2e6d5 570 GPIO_InitTypeDef GPIO_InitStruct;
willybayot 0:4db361f2e6d5 571
willybayot 0:4db361f2e6d5 572 /* LCD_CS_GPIO and LCD_DC_GPIO Periph clock enable */
willybayot 0:4db361f2e6d5 573 LCD_CS_GPIO_CLK_ENABLE();
willybayot 0:4db361f2e6d5 574 LCD_DC_GPIO_CLK_ENABLE();
willybayot 0:4db361f2e6d5 575
willybayot 0:4db361f2e6d5 576 /* Configure LCD_CS_PIN pin: LCD Card CS pin */
willybayot 0:4db361f2e6d5 577 GPIO_InitStruct.Pin = LCD_CS_PIN;
willybayot 0:4db361f2e6d5 578 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
willybayot 0:4db361f2e6d5 579 GPIO_InitStruct.Pull = GPIO_NOPULL;
willybayot 0:4db361f2e6d5 580 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
willybayot 0:4db361f2e6d5 581 HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 582
willybayot 0:4db361f2e6d5 583 /* Configure LCD_DC_PIN pin: LCD Card DC pin */
willybayot 0:4db361f2e6d5 584 GPIO_InitStruct.Pin = LCD_DC_PIN;
willybayot 0:4db361f2e6d5 585 HAL_GPIO_Init(LCD_DC_GPIO_PORT, &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 586
willybayot 0:4db361f2e6d5 587 /* LCD chip select high */
willybayot 0:4db361f2e6d5 588 LCD_CS_HIGH();
willybayot 0:4db361f2e6d5 589
willybayot 0:4db361f2e6d5 590 /* LCD SPI Config */
willybayot 0:4db361f2e6d5 591 SPIx_Init();
willybayot 0:4db361f2e6d5 592 }
willybayot 0:4db361f2e6d5 593
willybayot 0:4db361f2e6d5 594 /**
willybayot 0:4db361f2e6d5 595 * @brief Writes command to select the LCD register.
willybayot 0:4db361f2e6d5 596 * @param LCDReg: Address of the selected register.
willybayot 0:4db361f2e6d5 597 * @retval None
willybayot 0:4db361f2e6d5 598 */
willybayot 0:4db361f2e6d5 599 void LCD_IO_WriteReg(uint8_t LCDReg)
willybayot 0:4db361f2e6d5 600 {
willybayot 0:4db361f2e6d5 601 /* Reset LCD control line CS */
willybayot 0:4db361f2e6d5 602 LCD_CS_LOW();
willybayot 0:4db361f2e6d5 603
willybayot 0:4db361f2e6d5 604 /* Set LCD data/command line DC to Low */
willybayot 0:4db361f2e6d5 605 LCD_DC_LOW();
willybayot 0:4db361f2e6d5 606
willybayot 0:4db361f2e6d5 607 /* Send Command */
willybayot 0:4db361f2e6d5 608 SPIx_Write(LCDReg);
willybayot 0:4db361f2e6d5 609
willybayot 0:4db361f2e6d5 610 /* Deselect : Chip Select high */
willybayot 0:4db361f2e6d5 611 LCD_CS_HIGH();
willybayot 0:4db361f2e6d5 612 }
willybayot 0:4db361f2e6d5 613
willybayot 0:4db361f2e6d5 614 /**
willybayot 0:4db361f2e6d5 615 * @brief Writes data to select the LCD register.
willybayot 0:4db361f2e6d5 616 * This function must be used after st7735_WriteReg() function
willybayot 0:4db361f2e6d5 617 * @param Data: data to write to the selected register.
willybayot 0:4db361f2e6d5 618 * @retval None
willybayot 0:4db361f2e6d5 619 */
willybayot 0:4db361f2e6d5 620 void LCD_IO_WriteData(uint8_t Data)
willybayot 0:4db361f2e6d5 621 {
willybayot 0:4db361f2e6d5 622 /* Reset LCD control line CS */
willybayot 0:4db361f2e6d5 623 LCD_CS_LOW();
willybayot 0:4db361f2e6d5 624
willybayot 0:4db361f2e6d5 625 /* Set LCD data/command line DC to High */
willybayot 0:4db361f2e6d5 626 LCD_DC_HIGH();
willybayot 0:4db361f2e6d5 627
willybayot 0:4db361f2e6d5 628 /* Send Data */
willybayot 0:4db361f2e6d5 629 SPIx_Write(Data);
willybayot 0:4db361f2e6d5 630
willybayot 0:4db361f2e6d5 631 /* Deselect : Chip Select high */
willybayot 0:4db361f2e6d5 632 LCD_CS_HIGH();
willybayot 0:4db361f2e6d5 633 }
willybayot 0:4db361f2e6d5 634
willybayot 0:4db361f2e6d5 635 /**
willybayot 0:4db361f2e6d5 636 * @brief Wait for loop in ms.
willybayot 0:4db361f2e6d5 637 * @param Delay in ms.
willybayot 0:4db361f2e6d5 638 * @retval None
willybayot 0:4db361f2e6d5 639 */
willybayot 0:4db361f2e6d5 640 void LCD_Delay(uint32_t Delay)
willybayot 0:4db361f2e6d5 641 {
willybayot 0:4db361f2e6d5 642 HAL_Delay(Delay);
willybayot 0:4db361f2e6d5 643 }
willybayot 0:4db361f2e6d5 644
willybayot 0:4db361f2e6d5 645 /******************************* LINK JOYSTICK ********************************/
willybayot 0:4db361f2e6d5 646 /**
willybayot 0:4db361f2e6d5 647 * @brief Initializes ADC MSP.
willybayot 0:4db361f2e6d5 648 * @param None
willybayot 0:4db361f2e6d5 649 * @retval None
willybayot 0:4db361f2e6d5 650 */
willybayot 0:4db361f2e6d5 651 static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
willybayot 0:4db361f2e6d5 652 {
willybayot 0:4db361f2e6d5 653 GPIO_InitTypeDef GPIO_InitStruct;
willybayot 0:4db361f2e6d5 654
willybayot 0:4db361f2e6d5 655 /*** Configure the GPIOs ***/
willybayot 0:4db361f2e6d5 656 /* Enable GPIO clock */
willybayot 0:4db361f2e6d5 657 NUCLEO_ADCx_GPIO_CLK_ENABLE();
willybayot 0:4db361f2e6d5 658
willybayot 0:4db361f2e6d5 659 /* Configure the selected ADC Channel as analog input */
willybayot 0:4db361f2e6d5 660 GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
willybayot 0:4db361f2e6d5 661 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
willybayot 0:4db361f2e6d5 662 GPIO_InitStruct.Pull = GPIO_NOPULL;
willybayot 0:4db361f2e6d5 663 HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT, &GPIO_InitStruct);
willybayot 0:4db361f2e6d5 664
willybayot 0:4db361f2e6d5 665 /*** Configure the ADC peripheral ***/
willybayot 0:4db361f2e6d5 666 /* Enable ADC clock */
willybayot 0:4db361f2e6d5 667 NUCLEO_ADCx_CLK_ENABLE();
willybayot 0:4db361f2e6d5 668 }
willybayot 0:4db361f2e6d5 669
willybayot 0:4db361f2e6d5 670 /**
willybayot 0:4db361f2e6d5 671 * @brief Initializes ADC HAL.
willybayot 0:4db361f2e6d5 672 * @param None
willybayot 0:4db361f2e6d5 673 * @retval None
willybayot 0:4db361f2e6d5 674 */
willybayot 0:4db361f2e6d5 675 static void ADCx_Init(void)
willybayot 0:4db361f2e6d5 676 {
willybayot 0:4db361f2e6d5 677 if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
willybayot 0:4db361f2e6d5 678 {
willybayot 0:4db361f2e6d5 679 /* ADC Config */
willybayot 0:4db361f2e6d5 680 hnucleo_Adc.Instance = NUCLEO_ADCx;
willybayot 0:4db361f2e6d5 681 hnucleo_Adc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4; /* (must not exceed 36MHz) */
willybayot 0:4db361f2e6d5 682 hnucleo_Adc.Init.Resolution = ADC_RESOLUTION12b;
willybayot 0:4db361f2e6d5 683 hnucleo_Adc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
willybayot 0:4db361f2e6d5 684 hnucleo_Adc.Init.ContinuousConvMode = DISABLE;
willybayot 0:4db361f2e6d5 685 hnucleo_Adc.Init.DiscontinuousConvMode = DISABLE;
willybayot 0:4db361f2e6d5 686 hnucleo_Adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
willybayot 0:4db361f2e6d5 687 hnucleo_Adc.Init.EOCSelection = EOC_SINGLE_CONV;
willybayot 0:4db361f2e6d5 688 hnucleo_Adc.Init.NbrOfConversion = 1;
willybayot 0:4db361f2e6d5 689 hnucleo_Adc.Init.DMAContinuousRequests = DISABLE;
willybayot 0:4db361f2e6d5 690
willybayot 0:4db361f2e6d5 691 ADCx_MspInit(&hnucleo_Adc);
willybayot 0:4db361f2e6d5 692 HAL_ADC_Init(&hnucleo_Adc);
willybayot 0:4db361f2e6d5 693 }
willybayot 0:4db361f2e6d5 694 }
willybayot 0:4db361f2e6d5 695
willybayot 0:4db361f2e6d5 696 /**
willybayot 0:4db361f2e6d5 697 * @brief Configures joystick available on adafruit 1.8" TFT shield
willybayot 0:4db361f2e6d5 698 * managed through ADC to detect motion.
willybayot 0:4db361f2e6d5 699 * @param None
willybayot 0:4db361f2e6d5 700 * @retval Joystickstatus (0=> success, 1=> fail)
willybayot 0:4db361f2e6d5 701 */
willybayot 0:4db361f2e6d5 702 uint8_t BSP_JOY_Init(void)
willybayot 0:4db361f2e6d5 703 {
willybayot 0:4db361f2e6d5 704 uint8_t status = 1;
willybayot 0:4db361f2e6d5 705
willybayot 0:4db361f2e6d5 706 ADCx_Init();
willybayot 0:4db361f2e6d5 707
willybayot 0:4db361f2e6d5 708 /* Select the ADC Channel to be converted */
willybayot 0:4db361f2e6d5 709 sConfig.Channel = NUCLEO_ADCx_CHANNEL;
willybayot 0:4db361f2e6d5 710 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
willybayot 0:4db361f2e6d5 711 sConfig.Rank = 1;
willybayot 0:4db361f2e6d5 712 status = HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig);
willybayot 0:4db361f2e6d5 713
willybayot 0:4db361f2e6d5 714 /* Return Joystick initialization status */
willybayot 0:4db361f2e6d5 715 return status;
willybayot 0:4db361f2e6d5 716 }
willybayot 0:4db361f2e6d5 717
willybayot 0:4db361f2e6d5 718 /**
willybayot 0:4db361f2e6d5 719 * @brief Returns the Joystick key pressed.
willybayot 0:4db361f2e6d5 720 * @note To know which Joystick key is pressed we need to detect the voltage
willybayot 0:4db361f2e6d5 721 * level on each key output
willybayot 0:4db361f2e6d5 722 * - None : 3.3 V / 4095
willybayot 0:4db361f2e6d5 723 * - SEL : 1.055 V / 1308
willybayot 0:4db361f2e6d5 724 * - DOWN : 0.71 V / 88
willybayot 0:4db361f2e6d5 725 * - LEFT : 3.0 V / 3720
willybayot 0:4db361f2e6d5 726 * - RIGHT : 0.595 V / 737
willybayot 0:4db361f2e6d5 727 * - UP : 1.65 V / 2046
willybayot 0:4db361f2e6d5 728 * @retval JOYState_TypeDef: Code of the Joystick key pressed.
willybayot 0:4db361f2e6d5 729 */
willybayot 0:4db361f2e6d5 730 JOYState_TypeDef BSP_JOY_GetState(void)
willybayot 0:4db361f2e6d5 731 {
willybayot 0:4db361f2e6d5 732 JOYState_TypeDef state;
willybayot 0:4db361f2e6d5 733 uint16_t keyconvertedvalue = 0;
willybayot 0:4db361f2e6d5 734
willybayot 0:4db361f2e6d5 735 /* Start the conversion process */
willybayot 0:4db361f2e6d5 736 HAL_ADC_Start(&hnucleo_Adc);
willybayot 0:4db361f2e6d5 737
willybayot 0:4db361f2e6d5 738 /* Wait for the end of conversion */
willybayot 0:4db361f2e6d5 739 HAL_ADC_PollForConversion(&hnucleo_Adc, 10);
willybayot 0:4db361f2e6d5 740
willybayot 0:4db361f2e6d5 741 /* Check if the continous conversion of regular channel is finished */
willybayot 0:4db361f2e6d5 742 if(HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_EOC_REG)
willybayot 0:4db361f2e6d5 743 {
willybayot 0:4db361f2e6d5 744 /* Get the converted value of regular channel */
willybayot 0:4db361f2e6d5 745 keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
willybayot 0:4db361f2e6d5 746 }
willybayot 0:4db361f2e6d5 747
willybayot 0:4db361f2e6d5 748 if((keyconvertedvalue > 2010) && (keyconvertedvalue < 2090))
willybayot 0:4db361f2e6d5 749 {
willybayot 0:4db361f2e6d5 750 state = JOY_UP;
willybayot 0:4db361f2e6d5 751 }
willybayot 0:4db361f2e6d5 752 else if((keyconvertedvalue > 680) && (keyconvertedvalue < 780))
willybayot 0:4db361f2e6d5 753 {
willybayot 0:4db361f2e6d5 754 state = JOY_RIGHT;
willybayot 0:4db361f2e6d5 755 }
willybayot 0:4db361f2e6d5 756 else if((keyconvertedvalue > 1270) && (keyconvertedvalue < 1350))
willybayot 0:4db361f2e6d5 757 {
willybayot 0:4db361f2e6d5 758 state = JOY_SEL;
willybayot 0:4db361f2e6d5 759 }
willybayot 0:4db361f2e6d5 760 else if((keyconvertedvalue > 50) && (keyconvertedvalue < 130))
willybayot 0:4db361f2e6d5 761 {
willybayot 0:4db361f2e6d5 762 state = JOY_DOWN;
willybayot 0:4db361f2e6d5 763 }
willybayot 0:4db361f2e6d5 764 else if((keyconvertedvalue > 3680) && (keyconvertedvalue < 3760))
willybayot 0:4db361f2e6d5 765 {
willybayot 0:4db361f2e6d5 766 state = JOY_LEFT;
willybayot 0:4db361f2e6d5 767 }
willybayot 0:4db361f2e6d5 768 else
willybayot 0:4db361f2e6d5 769 {
willybayot 0:4db361f2e6d5 770 state = JOY_NONE;
willybayot 0:4db361f2e6d5 771 }
willybayot 0:4db361f2e6d5 772
willybayot 0:4db361f2e6d5 773 /* Loop while a key is pressed */
willybayot 0:4db361f2e6d5 774 if(state != JOY_NONE)
willybayot 0:4db361f2e6d5 775 {
willybayot 0:4db361f2e6d5 776 keyconvertedvalue = HAL_ADC_GetValue(&hnucleo_Adc);
willybayot 0:4db361f2e6d5 777 }
willybayot 0:4db361f2e6d5 778 /* Return the code of the Joystick key pressed */
willybayot 0:4db361f2e6d5 779 return state;
willybayot 0:4db361f2e6d5 780 }
willybayot 0:4db361f2e6d5 781
willybayot 0:4db361f2e6d5 782 /**
willybayot 0:4db361f2e6d5 783 * @}
willybayot 0:4db361f2e6d5 784 */
willybayot 0:4db361f2e6d5 785
willybayot 0:4db361f2e6d5 786 /**
willybayot 0:4db361f2e6d5 787 * @}
willybayot 0:4db361f2e6d5 788 */
willybayot 0:4db361f2e6d5 789
willybayot 0:4db361f2e6d5 790 /**
willybayot 0:4db361f2e6d5 791 * @}
willybayot 0:4db361f2e6d5 792 */
willybayot 0:4db361f2e6d5 793
willybayot 0:4db361f2e6d5 794 /**
willybayot 0:4db361f2e6d5 795 * @}
willybayot 0:4db361f2e6d5 796 */
willybayot 0:4db361f2e6d5 797
willybayot 0:4db361f2e6d5 798 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/