From dcc1e2161b49e4d1020b4d10f3364a2cc49d8f1b Mon Sep 17 00:00:00 2001 From: Vasily Markov Date: Tue, 2 Apr 2024 12:47:04 +0300 Subject: [PATCH] uart --- .gitignore | 4 +- .vscode/launch.json | 18 +++++++ .vscode/tasks.json | 123 ++++++++++++++++++++++++++++---------------- CMakeLists.txt | 1 + app/app.c | 27 +++++----- bsp/bsp.c | 91 ++++++++++++++++++++++++++++++-- ocd_flash | 4 -- openocd_run | 1 - system_stm32f4xx.c | 2 +- 9 files changed, 204 insertions(+), 67 deletions(-) create mode 100644 .vscode/launch.json delete mode 100755 ocd_flash delete mode 100755 openocd_run diff --git a/.gitignore b/.gitignore index c795b05..ecdc425 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -build \ No newline at end of file +build +STM32F439.svd +putty_run \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..636df81 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Cortex Debug", + "cwd": "${workspaceFolder}", + "executable": "${workspaceRoot}/build/stm32.elf", + "request": "launch", + "type": "cortex-debug", + "runToEntryPoint": "main", + "servertype": "stutil", + "preLaunchTask" : "run OpenOCD", + "preRestartCommands" : [ "load", "enable breakpoint", "monitor reset" ], + "showDevDebugOutput" : "raw", + "svdFile" : "${workspaceRoot}/STM32F439.svd" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index ab2c857..bf8c5b4 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "label": "build", "type": "shell", - "command": "cmake", + "command": "cmake", "args": [ "--build", "${workspaceFolder}/build" @@ -23,10 +23,10 @@ { "label": "generate", "type": "shell", - "command": "cmake", + "command": "cmake", "args": [ "-S", - ".", + ".", "-B", "build" ], @@ -39,48 +39,85 @@ } }, { - "label": "clean", - "type": "shell", - "command": "rm", - "args": [ - "-rf", - "${workspaceFolder}/build" - ], - "options": { - "cwd": "${workspaceFolder}" - }, - "group": { - "kind": "build", - "isDefault": true - } - }, + "label": "clean", + "type": "shell", + "command": "rm", + "args": [ + "-rf", + "${workspaceFolder}/build" + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "group": { + "kind": "build", + "isDefault": true + } + }, { - "label": "flash", - "type": "shell", - "command": "openocd", - "args": [ - "-f", - "interface/stlink-v2.cfg", + "label": "flash", + "type": "shell", + "command": "openocd", + "args": [ + "-f", + "interface/stlink-v2.cfg", "-f", "target/stm32f4x.cfg", - "-c", - "init", - "-c", - "reset halt", - "-c", - "flash write_image erase ${workspaceFolder}/build/stm32.elf", - "-c", - "reset", - "-c", - "exit" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "problemMatcher": [ - "$gcc" - ] - } + "-c", + "init", + "-c", + "reset halt", + "-c", + "flash write_image erase ${workspaceFolder}/build/stm32.hex", + "-c", + "reset", + "-c", + "exit" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [ + "$gcc" + ] + }, + { + "label": "run OpenOCD", + "type": "shell", + "command": "openocd", + "args": [ + "-f", + "interface/stlink-v2.cfg", + "-f", + "target/stm32f4x.cfg" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [ + "$gcc" + ], + "isBackground": true + }, + { + "label": "gdb", + "type": "shell", + "command": "gdb", + "args": [ + "${workspaceFolder}/build/stm32.elf", + "-ex", + "target remote :3333" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [], + "dependsOn": [ + "run OpenOCD" + ] + }, ] } diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d19deb..18d7cfc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ file(GLOB_RECURSE SOURCES "startup_stm32f4xx.S" "lib/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c" "lib/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c" + "lib/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_usart.c" ) set(LINKER_SCRIPT ${PROJECT_SOURCE_DIR}/stm32_flash.ld) diff --git a/app/app.c b/app/app.c index 3a44d91..5383e48 100644 --- a/app/app.c +++ b/app/app.c @@ -1,29 +1,32 @@ #include "app.h" -const uint16_t leds = GPIO_Pin_0 | GPIO_Pin_7 | GPIO_Pin_14; -const uint16_t leds_arr[4] = {GPIO_Pin_0, GPIO_Pin_7, GPIO_Pin_14}; + void delay(uint32_t ms) { - ms *= 3360; + ms *= 16800; while(ms--) { __NOP(); } } void loop() { - static uint32_t counter = 0; - - ++counter; - - GPIO_ResetBits(GPIOB, leds); - GPIO_SetBits(GPIOB, leds_arr[counter % 3]); - - delay(250); + GPIO_ToggleBits(GPIOB, GPIO_Pin_7); + + delay(500); } void app() { - + volatile uint8_t sendData[8]; + volatile uint8_t bytesToSend = 8; + // Счетчик отправленных байт + volatile uint8_t sendDataCounter = 0; + for (uint8_t i = 0; i < 8; i++) + { + sendData[i] = i; + } + // USART_ITConfig(USART2, USART_IT_TC, ENABLE); while(1) { + USART_SendData(USART2, sendData[sendDataCounter++ % 8]); loop(); }; } diff --git a/bsp/bsp.c b/bsp/bsp.c index 4717fe1..4b53e32 100644 --- a/bsp/bsp.c +++ b/bsp/bsp.c @@ -1,17 +1,98 @@ #include "bsp.h" -const uint16_t LEDS = GPIO_Pin_0 | GPIO_Pin_7 | GPIO_Pin_14; -const uint16_t LED[4] = {GPIO_Pin_0, GPIO_Pin_7, GPIO_Pin_14}; -void board_init() { +#define PLL_M 4 +#define PLL_N 168 + +/* SYSCLK = PLL_VCO / PLL_P */ +#define PLL_P 2 + +/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */ +#define PLL_Q 7 + + + + +void SysTick_Handler(void) +{ + //GPIO_ToggleBits(GPIOB, GPIO_Pin_0); +} + +void USART2_IRQHandler() +{ + +} + +void sysTick_init(void) { + uint32_t tick = SystemCoreClock/2 - 1; + SysTick->LOAD = tick; + SysTick->VAL = tick; + SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; +} + +void systemClock_init(void) { + RCC_DeInit(); // Reset RCC configuration to default values + + RCC_HSEConfig(RCC_HSE_ON); // Enable HSE (High Speed External) clock + if (RCC_WaitForHSEStartUp() == SUCCESS) { // Wait for HSE clock to stabilize + RCC_PLLConfig(RCC_PLLSource_HSE, PLL_M, PLL_N, PLL_P, PLL_Q); // Configure PLL + RCC_PLLCmd(ENABLE); // Enable PLL + while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // Wait for PLL to stabilize + + + FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS; + RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Set PLL as system clock + while (RCC_GetSYSCLKSource() != RCC_CFGR_SWS_PLL); // Wait for PLL to become system clock + } +} + +void gpio_init() { + + const uint16_t led_pins = GPIO_Pin_0 | GPIO_Pin_7 | GPIO_Pin_14; + const uint16_t usart_pins = GPIO_Pin_5 | GPIO_Pin_6; + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); + + GPIO_InitTypeDef gpio; + GPIO_StructInit(&gpio); gpio.GPIO_Mode = GPIO_Mode_OUT; - gpio.GPIO_Pin = LEDS; + gpio.GPIO_Pin = led_pins; GPIO_Init(GPIOB, &gpio); + GPIO_ResetBits(GPIOB, led_pins); + + gpio.GPIO_Mode = GPIO_Mode_AF; + gpio.GPIO_Pin = usart_pins; + gpio.GPIO_Speed = GPIO_Speed_50MHz; + gpio.GPIO_OType = GPIO_OType_PP; + gpio.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(GPIOD, &gpio); + + GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); + GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); +} + +void usart_init() { + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + USART_InitTypeDef usart; + usart.USART_BaudRate = 115200; + usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + USART_Init(USART2, &usart); + USART_Cmd(USART2, ENABLE); + //NVIC_EnableIRQ(USART2_IRQn); +} + +void board_init() { + + SystemInit(); + //systemClock_init(); + sysTick_init(); + //__enable_irq(); + gpio_init(); + usart_init(); - GPIO_SetBits(GPIOB, LEDS); } diff --git a/ocd_flash b/ocd_flash deleted file mode 100755 index 09742ba..0000000 --- a/ocd_flash +++ /dev/null @@ -1,4 +0,0 @@ -telnet localhost 4444 -reset halt -flash write_image erase stm32.hex -reset \ No newline at end of file diff --git a/openocd_run b/openocd_run deleted file mode 100755 index 133ee47..0000000 --- a/openocd_run +++ /dev/null @@ -1 +0,0 @@ -openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg \ No newline at end of file diff --git a/system_stm32f4xx.c b/system_stm32f4xx.c index 77970b7..78eaed8 100644 --- a/system_stm32f4xx.c +++ b/system_stm32f4xx.c @@ -150,7 +150,7 @@ #define PLL_N 336 /* SYSCLK = PLL_VCO / PLL_P */ -#define PLL_P 4 +#define PLL_P 2 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */ #define PLL_Q 7