From 62766425fe3a552440228c78f13a2c1dd62e228b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 18 Dec 2015 21:57:14 +0100 Subject: o Functional and tested printf. --- README.md | 3 +++ test1.cpp | 71 ++++++++++++++++++++++++++++++++++++----------------- tmp/printf/printf.c | 10 ++++---- tmp/printf/printf.h | 15 +++++++---- 4 files changed, 67 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 5a3315d..22e36a7 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ * http://www.banggood.com/ARM-Cortex-M3-STM32F103C8T6-STM32-Minimum-System-Development-Board-p-920184.html * http://www.lctech-inc.com/Hardware/Detail.aspx?id=0172e854-77b0-43d5-b300-68e570c914fd +* http://stackoverflow.com/questions/32422075/arm-bare-metal-binary-linking +* https://github.com/ckormanyos/real-time-cpp/blob/master/ref_app/target/micros/stm32f100/make/stm32f100.ld +* http://www.bravegnu.org/gnu-eprog/c-startup.html * http://www.st.com/web/en/catalog/tools/PF257890 ** STSW-STM32054 diff --git a/test1.cpp b/test1.cpp index e9ede35..89e859f 100644 --- a/test1.cpp +++ b/test1.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "tmp/printf/printf.h" @@ -50,6 +51,12 @@ void HardFault_Handler_C(uint32_t *hardfault_args) { } void send_command(int command, void *message) { + bool active = (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == CoreDebug_DHCSR_C_DEBUGEN_Msk; + + if (!active) { + return; + } + __asm volatile ( "mov r0, %[cmd];" "mov r1, %[msg];" @@ -58,17 +65,51 @@ void send_command(int command, void *message) { } //static -const char *msg_a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"; - -//uint32_t message3[] = { -// 2, -// (uint32_t) "YOYO\r\n", -// 6 -//}; +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); +} + /* * When we get there the stack pointer is set */ @@ -89,21 +130,7 @@ int main() { *dest++ = 0; } - uint32_t message[] = { - 2, - (uint32_t) "Hello World! again\r\n", - 20 - }; - send_command(0x05, &message); - - uint32_t message2[] = { - 2, - (uint32_t) msg_a, - 28 - }; - send_command(0x05, &message2); - - send_command(0x05, &message); + test_command(); SystemInit(); SystemCoreClockUpdate(); diff --git a/tmp/printf/printf.c b/tmp/printf/printf.c index 3950a5f..f10f61f 100755 --- a/tmp/printf/printf.c +++ b/tmp/printf/printf.c @@ -105,9 +105,9 @@ static int a2d(char ch) else return -1; } -static char a2i(char ch, char** src,int base,int* nump) +static char a2i(char ch, const char** src,int base,int* nump) { - char* p= *src; + const char* p= *src; int num=0; int digit; while ((digit=a2d(ch))>=0) { @@ -133,7 +133,7 @@ static void putchw(void* putp,putcf putf,int n, char z, char* bf) putf(putp,ch); } -void tfp_format(void* putp,putcf putf,char *fmt, va_list va) +void tfp_format(void* putp,putcf putf,const char *fmt, va_list va) { char bf[12]; @@ -218,7 +218,7 @@ void init_printf(void* putp,void (*putf) (void*,char)) stdout_putp=putp; } -void tfp_printf(char *fmt, ...) +void tfp_printf(const char *fmt, ...) { va_list va; va_start(va,fmt); @@ -233,7 +233,7 @@ static void putcp(void* p,char c) -void tfp_sprintf(char* s,char *fmt, ...) +void tfp_sprintf(char* s,const char *fmt, ...) { va_list va; va_start(va,fmt); diff --git a/tmp/printf/printf.h b/tmp/printf/printf.h index 9723b0f..f697ea6 100755 --- a/tmp/printf/printf.h +++ b/tmp/printf/printf.h @@ -108,17 +108,22 @@ regs Kusti, 23.10.2004 #include +#ifdef __cplusplus +extern "C" { +#endif + void init_printf(void* putp,void (*putf) (void*,char)); -void tfp_printf(char *fmt, ...); -void tfp_sprintf(char* s,char *fmt, ...); +void tfp_printf(const char *fmt, ...); +void tfp_sprintf(char* s,const char *fmt, ...); -void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va); +void tfp_format(void* putp,void (*putf) (void*,char),const char *fmt, va_list va); #define printf tfp_printf #define sprintf tfp_sprintf +#ifdef __cplusplus +}; #endif - - +#endif -- cgit v1.2.3