From 137489b6cd7e03031b0acb5d3abab4603decde04 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 20 Dec 2015 15:10:55 +0100 Subject: o Adding serial1 for testing USART. --- CMakeLists.txt | 59 +++++++++++++++++------- debug.cpp | 44 ++++++++++++++++++ debug.h | 43 +++++++++++++++++ gdb-start | 33 ++++++++++--- serial1.cpp | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tmp/printf/printf.h | 4 +- 6 files changed, 287 insertions(+), 26 deletions(-) create mode 100644 debug.cpp create mode 100644 debug.h create mode 100644 serial1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bee982..e1d5438 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,25 @@ cmake_minimum_required(VERSION 3.2) set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/cmake/stm32.toolchain.cmake") -project(teensy-playground C CXX ASM) +project(stm32f103-playground C CXX ASM) -#include(cmake/Teensy.cmake) +function(add_extra_commands target_name) + add_custom_command(TARGET ${target_name} POST_BUILD + COMMAND arm-none-eabi-objdump -D ${target_name} > ${target_name}.asm) + add_custom_command(TARGET ${target_name} POST_BUILD + COMMAND arm-none-eabi-nm ${target_name} > ${target_name}.nm) + add_custom_command(TARGET ${target_name} POST_BUILD + COMMAND arm-none-eabi-size ${target_name} > ${target_name}.size) + add_custom_command(TARGET ${target_name} POST_BUILD + COMMAND arm-none-eabi-readelf -a ${target_name} > ${target_name}.readelf) + add_custom_command(TARGET ${target_name} POST_BUILD + COMMAND arm-none-eabi-objcopy -O ihex ${target_name} ${target_name}.hex) + add_custom_command(TARGET ${target_name} POST_BUILD + COMMAND arm-none-eabi-objcopy -O binary ${target_name} ${target_name}.bin) +endfunction() + +######################################################################################################### +# test1 add_executable(test1.elf test1.cpp init_low.s init_high.cpp include/stm32f10x_conf.h # http://www.sparetimelabs.com/tinyprintf/tinyprintf.php @@ -16,27 +32,36 @@ add_executable(test1.elf test1.cpp init_low.s init_high.cpp include/stm32f10x_co ) target_include_directories(test1.elf PUBLIC - /usr/include/newlib include tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/CoreSupport tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/inc) target_compile_definitions(test1.elf PUBLIC STM32F10X_MD USE_STDPERIPH_DRIVER) -include_directories(/usr/include/newlib) -#target_compile_options(test1 PRIVATE -nostartfiles) target_compile_options(test1.elf PUBLIC "-O0") set_target_properties(test1.elf PROPERTIES LINK_FLAGS "-nostartfiles -T${CMAKE_SOURCE_DIR}/cmake/stm32.ld") +add_extra_commands(test1.elf) + +######################################################################################################### +# serial1 + +add_executable(serial1.elf serial1.cpp init_low.s init_high.cpp include/stm32f10x_conf.h + debug.cpp debug.h + # http://www.sparetimelabs.com/tinyprintf/tinyprintf.php + tmp/printf/printf.h tmp/printf/printf.c + tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/CoreSupport/core_cm3.c + tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c + tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c + tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c + ) + +target_include_directories(serial1.elf PUBLIC + include + tmp/printf + tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/CoreSupport + tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x + tmp/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/inc) +target_compile_definitions(serial1.elf PUBLIC STM32F10X_MD USE_STDPERIPH_DRIVER) -add_custom_command(TARGET test1.elf POST_BUILD - COMMAND arm-none-eabi-objdump -D test1.elf > test1.elf.asm) -add_custom_command(TARGET test1.elf POST_BUILD - COMMAND arm-none-eabi-nm test1.elf > test1.elf.nm) -add_custom_command(TARGET test1.elf POST_BUILD - COMMAND arm-none-eabi-size test1.elf > test1.elf.size) -add_custom_command(TARGET test1.elf POST_BUILD - COMMAND arm-none-eabi-readelf -a test1.elf > test1.elf.readelf) -add_custom_command(TARGET test1.elf POST_BUILD - COMMAND arm-none-eabi-objcopy -O ihex test1.elf test1.elf.hex) -add_custom_command(TARGET test1.elf POST_BUILD - COMMAND arm-none-eabi-objcopy -O binary test1.elf test1.elf.bin) +set_target_properties(serial1.elf PROPERTIES LINK_FLAGS "-nostartfiles -T${CMAKE_SOURCE_DIR}/cmake/stm32.ld") +add_extra_commands(serial1.elf) diff --git a/debug.cpp b/debug.cpp new file mode 100644 index 0000000..d44fc79 --- /dev/null +++ b/debug.cpp @@ -0,0 +1,44 @@ +#include "debug.h" +#include "printf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static void append_char(void *v, char c) { + *(*((char **) v))++ = c; +} + +void dbg_printf(const char *fmt, ...) { + char msg[100]; + msg[0] = 0; + + va_list va; + va_start(va, fmt); + char *a = msg; + char **b = &a; + tfp_format(b, append_char, fmt, va); + va_end(va); + append_char(b, '\0'); + + send_command(SYS_WRITE0, msg); +} + +void dbg_putc(void *, char c) { + char cc = c; + send_command(SYS_WRITEC, &cc); +} + +void send_command(enum SemihostingCmd command, void *message) { + int c = command; + + __asm volatile ( + "mov r0, %[cmd];" + "mov r1, %[msg];" + "bkpt #0xAB" : : [cmd] "r"(c), [msg] "r"(message) : "r0", "r1", "memory" + ); +} + +#ifdef __cplusplus +}; +#endif diff --git a/debug.h b/debug.h new file mode 100644 index 0000000..1c262a6 --- /dev/null +++ b/debug.h @@ -0,0 +1,43 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#ifdef __cplusplus +extern "C" { +#endif + +void dbg_printf(const char *fmt, ...); + +void dbg_putc(void * junk, char); + +enum SemihostingCmd { + SYS_CLOSE = 0x02, + SYS_CLOCK = 0x10, + SYS_ELAPSED = 0x30, + SYS_ERRNO = 0x13, + SYS_FLEN = 0x0C, + SYS_GET_CMDLINE = 0x15, + SYS_HEAPINFO = 0x16, + SYS_ISERROR = 0x08, + SYS_ISTTY = 0x09, + SYS_OPEN = 0x01, + SYS_READ = 0x06, + SYS_READC = 0x07, + SYS_REMOVE = 0x0E, + SYS_RENAME = 0x0F, + SYS_SEEK = 0x0A, + SYS_SYSTEM = 0x12, + SYS_TICKFREQ = 0x31, + SYS_TIME = 0x11, + SYS_TMPNAM = 0x0D, + SYS_WRITE = 0x05, + SYS_WRITEC = 0x03, + SYS_WRITE0 = 0x04, +}; + +void send_command(enum SemihostingCmd command, void *message); + +#ifdef __cplusplus +}; +#endif + +#endif diff --git a/gdb-start b/gdb-start index c7da6c3..07bb364 100644 --- a/gdb-start +++ b/gdb-start @@ -1,15 +1,34 @@ target remote tcp:localhost:3333 define flash_test1 -monitor arm semihosting enable -monitor reset halt + shell cd build && make test1.elf + + monitor arm semihosting enable + monitor reset halt + + set confirm off + file build/test1.elf + load build/test1.elf + set confirm on + + monitor stm32f1x.cpu mwb 0x20000000 0x5a 20480 + hbreak halt +end + +define flash_serial1 + shell cd build && make serial1.elf + + monitor arm semihosting enable + monitor reset halt -set confirm off -file build/test1.elf -load build/test1.elf -set confirm on + set confirm off + file build/serial1.elf + load build/serial1.elf + set confirm on -monitor stm32f1x.cpu mwb 0x20000000 0x5a 20480 + monitor stm32f1x.cpu mwb 0x20000000 0x5a 20480 + delete breakpoint 1 + hbreak halt end monitor reset halt diff --git a/serial1.cpp b/serial1.cpp new file mode 100644 index 0000000..6bafb5a --- /dev/null +++ b/serial1.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include "printf.h" +#include "debug.h" + +int init_high(); + +extern "C" void halt(); + +#include "stm32f10x_conf.h" + +extern "C" +__attribute__((naked)) +void HardFault_Handler_C(uint32_t *hardfault_args) { + halt(); +} + +size_t strlen(const char *s) { + size_t size = 0; + while (*s++ != '\0') size++; + return size; +} + +int run = 1; + +volatile USART_TypeDef *usart1 = (volatile USART_TypeDef *) USART1_BASE; + +/* + * When we get there the stack pointer is set + */ +int main() { + SystemInit(); + + init_printf(nullptr, dbg_putc); + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO + | RCC_APB2Periph_USART1 + | RCC_APB2Periph_GPIOA + | RCC_APB2Periph_GPIOB + | RCC_APB2Periph_GPIOC, + ENABLE); + + /* ***************************************** */ + + // Debug on port B + + RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE); + RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE); + + // Make Port B's pin #5 the debug output pin + GPIO_InitTypeDef init; + GPIO_StructInit(&init); + init.GPIO_Pin = GPIO_Pin_5; + init.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_Init(GPIOB, &init); + + /* ***************************************** */ + + RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE); + RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE); + + /* + * PA9 USART1_TX + * PA10 USART1_RX + */ + + // Enable USART1 + RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); + RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); + + // Make the TX pin an output + GPIO_StructInit(&init); + init.GPIO_Pin = GPIO_Pin_9; + init.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_Init(GPIOA, &init); + + // 8 bit mode + USART1->CR1 &= ~USART_CR1_M; + USART1->CR2 &= ~USART_CR2_STOP_1; + + USART1->CR1 |= USART_CR1_UE /* Set UART Enable */ + | USART_CR1_TE; /* Set Transmission Enable */ + + // Set baud rate + int mantissa = 39; + int fraction = static_cast(16 * 0.0625); // == 1 + // 72M / (16*39.0625) = 115200 + USART1->BRR = static_cast(mantissa << 4 | fraction); + + char c = 'A'; + while (run) { + int txe = USART1->SR & USART_SR_TXE; + +// dbg_printf("1:%d?\n", x); + +// char mm[100]; +// tfp_sprintf(mm, "2:%d?\n", x); +// send_command(SYS_WRITE0, mm); + +// printf(" %u?\n", usart1->SR); +// printf(" %u?\n", 1); + + if (txe) { + GPIO_SetBits(GPIOB, GPIO_Pin_All); + GPIO_ResetBits(GPIOB, GPIO_Pin_All); + + USART1->DR = (uint16_t) c; +// USART1->DR = 0x55; + + if (c == 'Z') { + c = 'a'; + } else if (c == 'z') { + c = '0'; + } else if (c == '9') { + c = '\n'; + } else if (c == '\n') { + c = 'A'; + } else { + c++; + } + } + } + + return 0; +} + diff --git a/tmp/printf/printf.h b/tmp/printf/printf.h index f697ea6..01ea7b9 100755 --- a/tmp/printf/printf.h +++ b/tmp/printf/printf.h @@ -119,8 +119,8 @@ void tfp_sprintf(char* s,const char *fmt, ...); void tfp_format(void* putp,void (*putf) (void*,char),const char *fmt, va_list va); -#define printf tfp_printf -#define sprintf tfp_sprintf +#define printf tfp_printf +#define sprintf tfp_sprintf #ifdef __cplusplus }; -- cgit v1.2.3