From 6d27f1755b782340d1c55c3f8f01a193514a3607 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 18 Dec 2015 23:06:46 +0100 Subject: o Splitting out init from main. --- CMakeLists.txt | 2 +- init.s | 68 -------------------------------------- init_high.cpp | 27 +++++++++++++++ init_low.s | 69 +++++++++++++++++++++++++++++++++++++++ test1.cpp | 101 +++++++++++---------------------------------------------- 5 files changed, 116 insertions(+), 151 deletions(-) delete mode 100644 init.s create mode 100644 init_high.cpp create mode 100644 init_low.s diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a8ef4d..5bee982 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ project(teensy-playground C CXX ASM) #include(cmake/Teensy.cmake) -add_executable(test1.elf test1.cpp init.s include/stm32f10x_conf.h +add_executable(test1.elf test1.cpp init_low.s init_high.cpp include/stm32f10x_conf.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 diff --git a/init.s b/init.s deleted file mode 100644 index 122a6ad..0000000 --- a/init.s +++ /dev/null @@ -1,68 +0,0 @@ -/* -https://github.com/dwelch67/stm32_samples -http://stackoverflow.com/questions/9565921/cortex-m3-initialisation -*/ - -.syntax unified -.cpu cortex-m3 -.thumb - -.section .isr_vectors - -.global vectors -vectors: -stacktop: .word 0x20001000 -.word _Reset_Handler -.word NMI_Handler -.word HardFault_Handler -.word MemManage_Handler -.word BusFault_Handler -.word UsageFault_Handler -.word hang2 -.word hang2 -.word hang2 -.word hang2 -.word hang2 -.word hang2 -.word hang2 -.word hang2 -.word hang2 - -/* VERY significant */ -.section .text - -.thumb_func -.global _Reset_Handler -_Reset_Handler: - bl main - b hang2 - -.thumb_func -hang2: - b . - -.thumb_func -NMI_Handler: - b hang2 - -.thumb_func -HardFault_Handler: - tst lr, #4 - ite eq - mrseq r0, msp - mrsne r0, psp - b HardFault_Handler_C - -.thumb_func -MemManage_Handler: - b hang2 - -.thumb_func -BusFault_Handler: - b hang2 - -.thumb_func -UsageFault_Handler: - b hang2 - -.end diff --git a/init_high.cpp b/init_high.cpp new file mode 100644 index 0000000..235a649 --- /dev/null +++ b/init_high.cpp @@ -0,0 +1,27 @@ +#include + +extern uint32_t _copy_data_load, _copy_data_store, _copy_data_store_end; +extern uint32_t _bss_start, _bss_end; + +extern int main(); +extern "C" int init_high(); + +int init_high() { + // Copy data from flash to ram + uint32_t *src = &_copy_data_load; + uint32_t *dest = &_copy_data_store; + uint32_t *end = &_copy_data_store_end; + + while (dest <= end) { + *dest++ = *src++; + } + + // Clear the BSS segment + dest = &_bss_start; + end = &_bss_end; + while (dest <= end) { + *dest++ = 0; + } + + return main(); +} diff --git a/init_low.s b/init_low.s new file mode 100644 index 0000000..bd7f329 --- /dev/null +++ b/init_low.s @@ -0,0 +1,69 @@ +/* +https://github.com/dwelch67/stm32_samples +http://stackoverflow.com/questions/9565921/cortex-m3-initialisation +*/ + +.syntax unified +.cpu cortex-m3 +.thumb + +.section .isr_vectors + +.global vectors +vectors: +stacktop: .word 0x20001000 +.word _Reset_Handler +.word NMI_Handler +.word HardFault_Handler +.word MemManage_Handler +.word BusFault_Handler +.word UsageFault_Handler +.word halt +.word halt +.word halt +.word halt +.word halt +.word halt +.word halt +.word halt +.word halt + +/* VERY significant */ +.section .text + +.thumb_func +.global _Reset_Handler +_Reset_Handler: + bl init_high + b halt + +.thumb_func +.global halt +halt: + b . + +.thumb_func +NMI_Handler: + b halt + +.thumb_func +HardFault_Handler: + tst lr, #4 + ite eq + mrseq r0, msp + mrsne r0, psp + b HardFault_Handler_C + +.thumb_func +MemManage_Handler: + b halt + +.thumb_func +BusFault_Handler: + b halt + +.thumb_func +UsageFault_Handler: + b halt + +.end diff --git a/test1.cpp b/test1.cpp index 89e859f..57149c3 100644 --- a/test1.cpp +++ b/test1.cpp @@ -5,14 +5,14 @@ #include #include +int init_high(); + +extern "C" void halt(); + #include "tmp/printf/printf.h" #include "stm32f10x_conf.h" -extern "C" -//__attribute__((naked)) -int main(void); - extern "C" __attribute__((naked)) void HardFault_Handler_C(uint32_t *hardfault_args); @@ -46,8 +46,7 @@ void HardFault_Handler_C(uint32_t *hardfault_args) { hardfault_data->pc = hardfault_args[6]; hardfault_data->psr = hardfault_args[7]; - do { - } while (1); + halt(); } void send_command(int command, void *message) { @@ -64,77 +63,21 @@ void send_command(int command, void *message) { ); } -//static -const char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"; - -extern uint32_t _copy_data_load, _copy_data_store, _copy_data_store_end; -extern uint32_t _bss_start, _bss_end; - size_t strlen(const char *s) { size_t size = 0; while (*s++ != '\0') size++; return size; } -void test_command() { - char msg[100]; - - tfp_sprintf(msg, "Hello World: c=%c\r\n", 'c'); - size_t len = strlen(msg); - - uint32_t message[] = { - 2, - (uint32_t) msg, - len - }; - send_command(0x05, &message); - - tfp_sprintf(msg, "Hello %s\r\n", "World!"); - len = strlen(msg); - - uint32_t message3[] = { - 2, - (uint32_t) msg, - len - }; - - uint32_t message2[] = { - 2, - (uint32_t) alphabet, - 28 - }; - send_command(0x05, &message2); - - send_command(0x05, &message3); - - send_command(0x05, &message2); -} +int run = 1; /* * When we get there the stack pointer is set */ int main() { - // Copy data from flash to ram - uint32_t *src = &_copy_data_load; - uint32_t *dest = &_copy_data_store; - uint32_t *end = &_copy_data_store_end; - - while (dest <= end) { - *dest++ = *src++; - } - - // Clear the BSS segment - dest = &_bss_start; - end = &_bss_end; - while (dest <= end) { - *dest++ = 0; - } - - test_command(); - SystemInit(); - SystemCoreClockUpdate(); -// RCC->APB2ENR = RCC_APB2ENR_IOPCEN; +// SystemCoreClockUpdate(); + SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_MEMFAULTPENDED_Msk | SCB_SHCSR_BUSFAULTENA_Msk; RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE); @@ -146,25 +89,19 @@ int main() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE); - if (1) { - GPIO_InitTypeDef init; - GPIO_StructInit(&init); - init.GPIO_Mode = GPIO_Mode_Out_PP; - GPIO_Init(GPIOB, &init); - - while (1) { - GPIO_SetBits(GPIOA, GPIO_Pin_All); - GPIO_SetBits(GPIOB, GPIO_Pin_All); - GPIO_SetBits(GPIOC, GPIO_Pin_All); - -// send_command(0x05, &message); + GPIO_InitTypeDef init; + GPIO_StructInit(&init); + init.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_Init(GPIOB, &init); - GPIO_ResetBits(GPIOA, GPIO_Pin_All); - GPIO_ResetBits(GPIOB, GPIO_Pin_All); - GPIO_ResetBits(GPIOC, GPIO_Pin_All); + while (run) { +// GPIO_SetBits(GPIOA, GPIO_Pin_All); + GPIO_SetBits(GPIOB, GPIO_Pin_All); +// GPIO_SetBits(GPIOC, GPIO_Pin_All); -// send_command(0x05, &message); - } +// GPIO_ResetBits(GPIOA, GPIO_Pin_All); + GPIO_ResetBits(GPIOB, GPIO_Pin_All); +// GPIO_ResetBits(GPIOC, GPIO_Pin_All); } return 0; -- cgit v1.2.3