From e696d2f52fad0de38e6100b27a4cb573187a8d8c Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Fri, 14 Feb 2025 23:03:36 +0200 Subject: [PATCH] fix driver issues, add simple display testing code --- Core/Inc/stm32f4xx_it.h | 1 + Core/Src/main.c | 276 ++++++++++++++++++---------------------- Core/Src/stm32f4xx_it.c | 19 ++- test1.ioc | 30 +++-- 4 files changed, 158 insertions(+), 168 deletions(-) diff --git a/Core/Inc/stm32f4xx_it.h b/Core/Inc/stm32f4xx_it.h index 5c05258..8911416 100644 --- a/Core/Inc/stm32f4xx_it.h +++ b/Core/Inc/stm32f4xx_it.h @@ -55,6 +55,7 @@ void SVC_Handler(void); void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); +void EXTI0_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 58195ee..6cf90e9 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -36,55 +36,13 @@ #define DISPLAY_RW ((uint16_t) (0x1U << 10)) #define DISPLAY_ENA ((uint16_t) (0x1U << 11)) -#define UPDATE_STATUS GPIOD->ODR = ((GPIOE->ODR & DISPLAY_ENA) << 1) | ((GPIOE->ODR & DISPLAY_RS) << 6) -#define DISPLAY_CHAR_DELAY 1 // character -#define DISPLAY_COMM_DELAY 60 // command - -#define DISPLAY_INSTRUCT_BYTE(byte) \ - do { \ - GPIOE->ODR = ((((uint16_t)(byte)) << 8) & 0xF000); \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_COMM_DELAY); \ - GPIOE->BSRR = DISPLAY_ENA; \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_COMM_DELAY); \ - GPIOE->BSRR = (DISPLAY_ENA << 16); \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_COMM_DELAY); \ - \ - GPIOE->ODR = ((((uint16_t)(byte)) << 12) & 0xF000); \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_COMM_DELAY); \ - GPIOE->BSRR = DISPLAY_ENA; \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_COMM_DELAY); \ - GPIOE->BSRR = (DISPLAY_ENA << 16); \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_COMM_DELAY); \ +#define PANIC(status) \ + do { \ + GPIOD->ODR = status; \ + while (1) {} \ } while (0) -#define DISPLAY_DATA_BYTE(byte) \ - do { \ - GPIOE->ODR = (((((uint16_t)(byte)) << 8) & 0xF000) | DISPLAY_RS); \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_CHAR_DELAY); \ - GPIOE->BSRR = DISPLAY_ENA; \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_CHAR_DELAY); \ - GPIOE->BSRR = (DISPLAY_ENA << 16); \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_CHAR_DELAY); \ - \ - GPIOE->ODR = (((((uint16_t)(byte)) << 12) & 0xF000) | DISPLAY_RS); \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_CHAR_DELAY); \ - GPIOE->BSRR = DISPLAY_ENA; \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_CHAR_DELAY); \ - GPIOE->BSRR = (DISPLAY_ENA << 16); \ - UPDATE_STATUS; \ - HAL_Delay(DISPLAY_CHAR_DELAY); \ - } while (0) +#define POLL_UNTIL_READY do { while (read_status() & 0x80) {} } while (0) /* USER CODE END PD */ @@ -97,6 +55,8 @@ /* USER CODE BEGIN PV */ +int change = 1; + /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -109,6 +69,70 @@ static void MX_GPIO_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ +uint8_t read_status(void) +{ + // make sure GPIOE is in correct mode + GPIOE->MODER = 0x00504000; + + if (GPIOE->MODER != 0x00504000) + PANIC(0x4000); + + uint8_t status = 0; + + GPIOE->ODR = 0x0; + GPIOE->BSRR = DISPLAY_RW; + + GPIOE->BSRR = DISPLAY_ENA; + status |= (GPIOE->IDR & 0xF000) >> 8; + GPIOE->BSRR = (DISPLAY_ENA << 16); + + GPIOE->BSRR = DISPLAY_ENA; + status |= (GPIOE->IDR & 0xF000) >> 12; + GPIOE->BSRR = (DISPLAY_ENA << 16); + + GPIOE->ODR = 0x0; + + return status; +} + +void write_instruction_byte(uint8_t code) +{ + // make sure GPIOE is in correct mode + GPIOE->MODER = 0x55504000; + + if (GPIOE->MODER != 0x55504000) + PANIC(0x4000); + + GPIOE->ODR = (code & 0xF0) << 8; + GPIOE->BSRR = DISPLAY_ENA; + GPIOE->BSRR = (DISPLAY_ENA << 16); + + GPIOE->ODR = (code & 0x0F) << 12; + GPIOE->BSRR = DISPLAY_ENA; + GPIOE->BSRR = (DISPLAY_ENA << 16); + + POLL_UNTIL_READY; +} + +void write_data_byte(uint8_t code) +{ + // make sure GPIOE is in correct mode + GPIOE->MODER = 0x55504000; + + if (GPIOE->MODER != 0x55504000) + PANIC(0x4000); + + GPIOE->ODR = ((code & 0xF0) << 8) | (DISPLAY_RS); + GPIOE->BSRR = DISPLAY_ENA; + GPIOE->BSRR = DISPLAY_ENA << 16; + + GPIOE->ODR = ((code & 0x0F) << 12) | (DISPLAY_RS); + GPIOE->BSRR = DISPLAY_ENA; + GPIOE->BSRR = DISPLAY_ENA << 16; + + POLL_UNTIL_READY; +} + /* USER CODE END 0 */ /** @@ -141,128 +165,44 @@ int main(void) /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ - DISPLAY_INSTRUCT_BYTE(0x01); GPIOD->ODR = 0xF000; - HAL_Delay(1000); - - //HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET); - - uint8_t display_status = 0x0; - int attempts_left; - - while (1) { - GPIOE->MODER &= 0x00FFFFFFU; - attempts_left = 10; - - do { - if (GPIOE->MODER & 0xFF000000U) { - // port didn't switch mode - GPIOD->ODR = 0x4000U; - while (1) {} - } else { - GPIOD->ODR = 0x0U; - } - - GPIOE->BSRR = DISPLAY_RW | (DISPLAY_RS << 16); - GPIOE->BSRR = DISPLAY_ENA; - HAL_Delay(20); - display_status = (GPIOE->IDR & 0xF000) >> 8; - GPIOE->BSRR = (DISPLAY_ENA << 16); - HAL_Delay(20); - - - GPIOE->BSRR = DISPLAY_ENA; - HAL_Delay(20); - display_status |= (GPIOE->IDR & 0xF000) >> 12; - GPIOE->BSRR = (DISPLAY_ENA << 16); - HAL_Delay(20); - - - GPIOD->ODR = ((display_status & 0xF0) << 8); - - HAL_Delay(50); - } while (((display_status & 0x80) || ((display_status & 0x7F) != 0)) && attempts_left--); - - if (!((display_status & 0x80) || ((display_status & 0x7F) != 0))) - break; - - GPIOD->ODR = 0x0U; - GPIOE->ODR = 0x0U; - GPIOE->BSRR = DISPLAY_ENA; - HAL_Delay(20); - GPIOE->BSRR = (DISPLAY_ENA << 16); - HAL_Delay(20); - - GPIOE->BSRR = DISPLAY_ENA; - HAL_Delay(20); - GPIOE->BSRR = (DISPLAY_ENA << 16); - HAL_Delay(20); - - GPIOE->MODER |= 0x55000000U; - - GPIOE->BSRR = DISPLAY_ENA; - HAL_Delay(20); - GPIOE->BSRR = (DISPLAY_ENA << 16); - HAL_Delay(20); - - DISPLAY_INSTRUCT_BYTE(0x01); - - HAL_Delay(500); - } - - GPIOD->ODR = 0x0U; - - GPIOE->ODR = 0x0U; - - /* - GPIOE->BSRR = DISPLAY_ENA; - HAL_Delay(1); - GPIOE->BSRR = (DISPLAY_ENA << 16); - HAL_Delay(1); - - GPIOE->BSRR = DISPLAY_ENA; - HAL_Delay(1); - GPIOE->BSRR = (DISPLAY_ENA << 16); - HAL_Delay(1); - */ - - GPIOE->MODER |= 0x55000000U; - - - - // init display - /* - GPIOE->BSRR = DISPLAY_ENA; - HAL_Delay(1); - GPIOE->BSRR = (DISPLAY_ENA << 16); - HAL_Delay(1); - */ - - - // reset (supposedly) - DISPLAY_INSTRUCT_BYTE(0x01); // switch to 4-bit 2-line mode - DISPLAY_INSTRUCT_BYTE(0x28); + write_instruction_byte(0x28); - // write characters + // reset display + write_instruction_byte(0x01); + + // enable display + write_instruction_byte(0x0C); + + // move cursor to first line + write_instruction_byte(0x80); + + // write string char a[] = "Well, hi there!"; for (int i = 0; i < sizeof(a)-1; i++) { - DISPLAY_DATA_BYTE(a[i]); + write_data_byte(a[i]); } - // next line - DISPLAY_INSTRUCT_BYTE(0xC0); + /* + // move cursor to second line + write_instruction_byte(0xC0); + while (read_status() & 0x80) {} - // write more characters + // write another string char b[] = "I'm doing it!"; for (int i = 0; i < sizeof(b)-1; i++) { - DISPLAY_DATA_BYTE(b[i]); + write_data_byte(b[i]); + while (read_status() & 0x80) {} } + */ - GPIOE->ODR = 0x0U; - UPDATE_STATUS; + GPIOD->ODR = 0x0000; + + unsigned int counter = 0; + write_instruction_byte(0x04); // set address decrement after write /* USER CODE END 2 */ @@ -270,6 +210,21 @@ int main(void) /* USER CODE BEGIN WHILE */ while (1) { + counter += change; + counter &= 0xFFFF; + + unsigned int c = counter; + + write_instruction_byte(0xC4); + + for (int i = 0; i < 5; i++) { + int current_digit = c % 10; + c /= 10; + + write_data_byte('0' + (char) current_digit); + } + + HAL_Delay(70); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ @@ -330,6 +285,7 @@ static void MX_GPIO_Init(void) /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); @@ -340,6 +296,12 @@ static void MX_GPIO_Init(void) /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET); + /*Configure GPIO pin : PA0 */ + GPIO_InitStruct.Pin = GPIO_PIN_0; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + /*Configure GPIO pins : PE7 PE10 PE11 PE12 PE13 PE14 PE15 */ GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12 @@ -356,6 +318,10 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); + /* EXTI interrupt init*/ + HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI0_IRQn); + /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index 5be1641..cad61b9 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -41,7 +41,7 @@ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ - +extern int change; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -198,6 +198,23 @@ void SysTick_Handler(void) /* please refer to the startup file (startup_stm32f4xx.s). */ /******************************************************************************/ +/** + * @brief This function handles EXTI line0 interrupt. + */ +void EXTI0_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI0_IRQn 0 */ + + change *= -1; + + for (int i = 0; i < 200000; i++) {asm("nop");} + /* USER CODE END EXTI0_IRQn 0 */ + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); + /* USER CODE BEGIN EXTI0_IRQn 1 */ + + /* USER CODE END EXTI0_IRQn 1 */ +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ diff --git a/test1.ioc b/test1.ioc index 3f2374f..6a83730 100644 --- a/test1.ioc +++ b/test1.ioc @@ -12,18 +12,19 @@ Mcu.IP1=RCC Mcu.IPNb=2 Mcu.Name=STM32F407V(E-G)Tx Mcu.Package=LQFP100 -Mcu.Pin0=PE7 -Mcu.Pin1=PE10 -Mcu.Pin10=PD15 -Mcu.Pin2=PE11 -Mcu.Pin3=PE12 -Mcu.Pin4=PE13 -Mcu.Pin5=PE14 -Mcu.Pin6=PE15 -Mcu.Pin7=PD12 -Mcu.Pin8=PD13 -Mcu.Pin9=PD14 -Mcu.PinsNb=11 +Mcu.Pin0=PA0-WKUP +Mcu.Pin1=PE7 +Mcu.Pin10=PD14 +Mcu.Pin11=PD15 +Mcu.Pin2=PE10 +Mcu.Pin3=PE11 +Mcu.Pin4=PE12 +Mcu.Pin5=PE13 +Mcu.Pin6=PE14 +Mcu.Pin7=PE15 +Mcu.Pin8=PD12 +Mcu.Pin9=PD13 +Mcu.PinsNb=12 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F407VGTx @@ -31,6 +32,7 @@ MxCube.Version=6.12.1 MxDb.Version=DB.6.0.121 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.EXTI0_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false @@ -40,6 +42,8 @@ NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +PA0-WKUP.Locked=true +PA0-WKUP.Signal=GPXTI0 PD12.Locked=true PD12.Signal=GPIO_Output PD13.Locked=true @@ -120,5 +124,7 @@ RCC.VCOI2SOutputFreq_Value=192000000 RCC.VCOInputFreq_Value=1000000 RCC.VCOOutputFreq_Value=192000000 RCC.VcooutputI2S=96000000 +SH.GPXTI0.0=GPIO_EXTI0 +SH.GPXTI0.ConfNb=1 board=custom isbadioc=false