F439_CPP_SPI_RA8875_TFT_LCD_03 1.0
STM32F439 SPI RA8875 7-inch TFT display demo built on the STM32_GFX and STM32_RA8875 libraries
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
18/* USER CODE END Header */
19/* Includes ------------------------------------------------------------------*/
20#include "main.h"
21
22/* Private includes ----------------------------------------------------------*/
23/* USER CODE BEGIN Includes */
24#include <stdio.h>
25#include "string.h"
26#include "stdbool.h"
27#include "entryPointCPP.hpp"
28
29/* USER CODE END Includes */
30
31/* Private typedef -----------------------------------------------------------*/
32/* USER CODE BEGIN PTD */
33
34/* USER CODE END PTD */
35
36/* Private define ------------------------------------------------------------*/
37/* USER CODE BEGIN PD */
38
39/* USER CODE END PD */
40
41/* Private macro -------------------------------------------------------------*/
42/* USER CODE BEGIN PM */
43
44/* USER CODE END PM */
45
46/* Private variables ---------------------------------------------------------*/
47SPI_HandleTypeDef hspi1;
48
49TIM_HandleTypeDef htim1;
50TIM_HandleTypeDef htim2;
51
52UART_HandleTypeDef huart1;
53
54/* USER CODE BEGIN PV */
55
56/* USER CODE END PV */
57
58/* Private function prototypes -----------------------------------------------*/
59void SystemClock_Config(void);
60static void MX_GPIO_Init(void);
61static void MX_SPI1_Init(void);
62static void MX_TIM1_Init(void);
63static void MX_TIM2_Init(void);
64static void MX_USART1_UART_Init(void);
65/* USER CODE BEGIN PFP */
66
67/* USER CODE END PFP */
68
69/* Private user code ---------------------------------------------------------*/
70/* USER CODE BEGIN 0 */
71
72#ifdef REDIRECT_PRINTF
73#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
74
75PUTCHAR_PROTOTYPE
76{
77 HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
78
79 return ch;
80}
81#endif
82
83/* USER CODE END 0 */
84
89int main(void)
90{
91
92 /* USER CODE BEGIN 1 */
93
94 /* USER CODE END 1 */
95
96 /* MCU Configuration--------------------------------------------------------*/
97
98 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
99 HAL_Init();
100
101 /* USER CODE BEGIN Init */
102
103 /* USER CODE END Init */
104
105 /* Configure the system clock */
106 SystemClock_Config();
107
108 /* USER CODE BEGIN SysInit */
109
110 /* USER CODE END SysInit */
111
112 /* Initialize all configured peripherals */
113 MX_GPIO_Init();
114 MX_SPI1_Init();
115 MX_TIM1_Init();
116 MX_TIM2_Init();
117 MX_USART1_UART_Init();
118 /* USER CODE BEGIN 2 */
119
120 printf("\x1b[2J\x1b[H"); // Clear the dumb terminal screen
121 printf("Starting Initialization Process\r\n");
122
123 initTest(&hspi1);
124
125 HAL_Delay(500);
126
127 /* USER CODE END 2 */
128
129 /* Infinite loop */
130 /* USER CODE BEGIN WHILE */
131 while (1)
132 {
133 printf("Start Tests\r\n");
134 testLCD(true);
135 HAL_Delay(5000);
136 testLCD(false);
137 printf("Done\r\n");
138 HAL_Delay(5000);
139 /* USER CODE END WHILE */
140
141 /* USER CODE BEGIN 3 */
142 }
143 /* USER CODE END 3 */
144}
145
150void SystemClock_Config(void)
151{
152 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
153 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
154
157 __HAL_RCC_PWR_CLK_ENABLE();
158 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
159
163 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
164 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
165 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
166 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
167 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
168 RCC_OscInitStruct.PLL.PLLM = 8;
169 RCC_OscInitStruct.PLL.PLLN = 180;
170 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
171 RCC_OscInitStruct.PLL.PLLQ = 4;
172 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
173 {
174 Error_Handler();
175 }
176
179 if (HAL_PWREx_EnableOverDrive() != HAL_OK)
180 {
181 Error_Handler();
182 }
183
186 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
187 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
188 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
189 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
190 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
191 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
192
193 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
194 {
195 Error_Handler();
196 }
197}
198
204static void MX_SPI1_Init(void)
205{
206
207 /* USER CODE BEGIN SPI1_Init 0 */
208
209 /* USER CODE END SPI1_Init 0 */
210
211 /* USER CODE BEGIN SPI1_Init 1 */
212
213 /* USER CODE END SPI1_Init 1 */
214 /* SPI1 parameter configuration*/
215 hspi1.Instance = SPI1;
216 hspi1.Init.Mode = SPI_MODE_MASTER;
217 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
218 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
219 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
220 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
221 hspi1.Init.NSS = SPI_NSS_SOFT;
222 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
223 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
224 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
225 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
226 hspi1.Init.CRCPolynomial = 10;
227 if (HAL_SPI_Init(&hspi1) != HAL_OK)
228 {
229 Error_Handler();
230 }
231 /* USER CODE BEGIN SPI1_Init 2 */
232
233 /* USER CODE END SPI1_Init 2 */
234
235}
236
242static void MX_TIM1_Init(void)
243{
244
245 /* USER CODE BEGIN TIM1_Init 0 */
246
247 /* USER CODE END TIM1_Init 0 */
248
249 TIM_ClockConfigTypeDef sClockSourceConfig = {0};
250 TIM_MasterConfigTypeDef sMasterConfig = {0};
251
252 /* USER CODE BEGIN TIM1_Init 1 */
253
254 /* USER CODE END TIM1_Init 1 */
255 htim1.Instance = TIM1;
256 htim1.Init.Prescaler = 180-1;
257 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
258 htim1.Init.Period = 65535;
259 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
260 htim1.Init.RepetitionCounter = 0;
261 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
262 if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
263 {
264 Error_Handler();
265 }
266 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
267 if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
268 {
269 Error_Handler();
270 }
271 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
272 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
273 if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
274 {
275 Error_Handler();
276 }
277 /* USER CODE BEGIN TIM1_Init 2 */
278
279 /* USER CODE END TIM1_Init 2 */
280
281}
282
288static void MX_TIM2_Init(void)
289{
290
291 /* USER CODE BEGIN TIM2_Init 0 */
292
293 /* USER CODE END TIM2_Init 0 */
294
295 TIM_ClockConfigTypeDef sClockSourceConfig = {0};
296 TIM_MasterConfigTypeDef sMasterConfig = {0};
297
298 /* USER CODE BEGIN TIM2_Init 1 */
299
300 /* USER CODE END TIM2_Init 1 */
301 htim2.Instance = TIM2;
302 htim2.Init.Prescaler = 90-1;
303 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
304 htim2.Init.Period = 4294967295;
305 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
306 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
307 if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
308 {
309 Error_Handler();
310 }
311 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
312 if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
313 {
314 Error_Handler();
315 }
316 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
317 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
318 if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
319 {
320 Error_Handler();
321 }
322 /* USER CODE BEGIN TIM2_Init 2 */
323
324 /* USER CODE END TIM2_Init 2 */
325
326}
327
333static void MX_USART1_UART_Init(void)
334{
335
336 /* USER CODE BEGIN USART1_Init 0 */
337
338 /* USER CODE END USART1_Init 0 */
339
340 /* USER CODE BEGIN USART1_Init 1 */
341
342 /* USER CODE END USART1_Init 1 */
343 huart1.Instance = USART1;
344 huart1.Init.BaudRate = 19200;
345 huart1.Init.WordLength = UART_WORDLENGTH_8B;
346 huart1.Init.StopBits = UART_STOPBITS_1;
347 huart1.Init.Parity = UART_PARITY_NONE;
348 huart1.Init.Mode = UART_MODE_TX_RX;
349 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
350 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
351 if (HAL_UART_Init(&huart1) != HAL_OK)
352 {
353 Error_Handler();
354 }
355 /* USER CODE BEGIN USART1_Init 2 */
356
357 /* USER CODE END USART1_Init 2 */
358
359}
360
366static void MX_GPIO_Init(void)
367{
368 GPIO_InitTypeDef GPIO_InitStruct = {0};
369 /* USER CODE BEGIN MX_GPIO_Init_1 */
370
371 /* USER CODE END MX_GPIO_Init_1 */
372
373 /* GPIO Ports Clock Enable */
374 __HAL_RCC_GPIOC_CLK_ENABLE();
375 __HAL_RCC_GPIOH_CLK_ENABLE();
376 __HAL_RCC_GPIOA_CLK_ENABLE();
377 __HAL_RCC_GPIOG_CLK_ENABLE();
378 __HAL_RCC_GPIOB_CLK_ENABLE();
379
380 /*Configure GPIO pin Output Level */
381 HAL_GPIO_WritePin(LCD_RESET_GPIO_Port, LCD_RESET_Pin, GPIO_PIN_RESET);
382
383 /*Configure GPIO pin Output Level */
384 HAL_GPIO_WritePin(LCD_WAIT_GPIO_Port, LCD_WAIT_Pin, GPIO_PIN_RESET);
385
386 /*Configure GPIO pin Output Level */
387 HAL_GPIO_WritePin(RA8875_CS_GPIO_Port, RA8875_CS_Pin, GPIO_PIN_RESET);
388
389 /*Configure GPIO pin : LCD_RESET_Pin */
390 GPIO_InitStruct.Pin = LCD_RESET_Pin;
391 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
392 GPIO_InitStruct.Pull = GPIO_NOPULL;
393 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
394 HAL_GPIO_Init(LCD_RESET_GPIO_Port, &GPIO_InitStruct);
395
396 /*Configure GPIO pin : LCD_WAIT_Pin */
397 GPIO_InitStruct.Pin = LCD_WAIT_Pin;
398 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
399 GPIO_InitStruct.Pull = GPIO_NOPULL;
400 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
401 HAL_GPIO_Init(LCD_WAIT_GPIO_Port, &GPIO_InitStruct);
402
403 /*Configure GPIO pin : LCD_INT_Pin */
404 GPIO_InitStruct.Pin = LCD_INT_Pin;
405 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
406 GPIO_InitStruct.Pull = GPIO_PULLUP;
407 HAL_GPIO_Init(LCD_INT_GPIO_Port, &GPIO_InitStruct);
408
409 /*Configure GPIO pin : RA8875_CS_Pin */
410 GPIO_InitStruct.Pin = RA8875_CS_Pin;
411 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
412 GPIO_InitStruct.Pull = GPIO_PULLUP;
413 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
414 HAL_GPIO_Init(RA8875_CS_GPIO_Port, &GPIO_InitStruct);
415
416 /* EXTI interrupt init*/
417 HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
418 HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
419
420 /* USER CODE BEGIN MX_GPIO_Init_2 */
421
422 /* USER CODE END MX_GPIO_Init_2 */
423}
424
425/* USER CODE BEGIN 4 */
426
427
434void _Error_Handler(const char *file, int line)
435{
436 __disable_irq();
437
438#ifdef REDIRECT_PRINTF
439 char buf[80];
440
441 snprintf(buf, sizeof(buf),
442 "Trapped in _Error_Handler(). Called from: %s, line: %d\r\n",
443 file, line);
444 printf("%s", buf);
445#endif
446
447 /* User can add an implementation to report the HAL error return state. */
448 while(1)
449 {
450 }
451}
452
453/* USER CODE END 4 */
454
455
456#ifdef USE_FULL_ASSERT
464void assert_failed(uint8_t *file, uint32_t line)
465{
466 /* USER CODE BEGIN 6 */
467 /* assert_param() can fire from inside any HAL call -- including before
468 * REDIRECT_PRINTF's UART is fully set up, or from an unusual calling
469 * context -- so this only attempts to report when REDIRECT_PRINTF is
470 * available, then halts either way. Continuing after a failed HAL
471 * parameter check is undefined behavior, not something safe to log
472 * and ignore. */
473 __disable_irq();
474
475#ifdef REDIRECT_PRINTF
476 {
477 char buf[96];
478
479 snprintf(buf, sizeof(buf),
480 "assert_param failed: file %s, line %lu\r\n",
481 (char *)file, (unsigned long)line);
482 printf("%s", buf);
483 }
484#endif
485
486 while (1)
487 {
488 }
489 /* USER CODE END 6 */
490}
491#endif /* USE_FULL_ASSERT */
C-callable entry points into the C++ RA8875 display code.
void testLCD(bool buildTest)
Runs one of the two display demonstrations.
void initTest(SPI_HandleTypeDef *halSPI)
Creates the display object and starts the RA8875 controller.
int main(void)
The application entry point.
Definition main.c:89
: Header for main.c file. This file contains the common defines of the application.