diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2020-11-21 21:57:14 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2020-11-21 21:57:14 -0700 |
commit | 822c68c74ebc3ac3694f87d516f1e91f2ce1d0fe (patch) | |
tree | 709b480c0d0283501c2376713f6b495c32292520 | |
parent | b073c19f9ec330423fa07c66d1c0604883044f6b (diff) | |
download | stm32l4-822c68c74ebc3ac3694f87d516f1e91f2ce1d0fe.tar.gz stm32l4-822c68c74ebc3ac3694f87d516f1e91f2ce1d0fe.tar.bz2 stm32l4-822c68c74ebc3ac3694f87d516f1e91f2ce1d0fe.zip |
Fix mem.c to use the address of DATA_SEGMENT_START instead of the value
-rw-r--r-- | 02-usart/include/arch/x86_64/arch.h | 4 | ||||
-rw-r--r-- | 02-usart/include/mem.h | 5 | ||||
-rw-r--r-- | 02-usart/src/core/clock.c | 1 | ||||
-rw-r--r-- | 02-usart/src/core/usart.c | 10 | ||||
-rw-r--r-- | 02-usart/src/main.c | 15 | ||||
-rw-r--r-- | 02-usart/src/mem.c | 11 | ||||
-rw-r--r-- | 02-usart/src/stdlibrepl.c | 13 | ||||
-rw-r--r-- | 02-usart/tests/test_memory.c | 10 |
8 files changed, 53 insertions, 16 deletions
diff --git a/02-usart/include/arch/x86_64/arch.h b/02-usart/include/arch/x86_64/arch.h index accc449..e87559b 100644 --- a/02-usart/include/arch/x86_64/arch.h +++ b/02-usart/include/arch/x86_64/arch.h @@ -27,7 +27,7 @@ // Pretend there's a data segement at the start of SRAM1 for more accurate // testing. #define GHOST_DATA_SEGMENT_SIZE 1234 -#define DATA_SEGMENT_START SRAM1_BASE -#define DATA_SEGMENT_STOP (SRAM1_BASE + GHOST_DATA_SEGMENT_SIZE) +#define DATA_SEGMENT_START (*((uint8_t*)SRAM1_BASE)) +#define DATA_SEGMENT_STOP (*(((uint8_t*)SRAM1_BASE) + GHOST_DATA_SEGMENT_SIZE)) #endif /* ARCH_H_ */ diff --git a/02-usart/include/mem.h b/02-usart/include/mem.h index f376e7d..d150744 100644 --- a/02-usart/include/mem.h +++ b/02-usart/include/mem.h @@ -4,8 +4,11 @@ #include "arch.h" #include <stddef.h> +#define DATA_SEGMENT_STOP_ADDR ((uint8_t*) &DATA_SEGMENT_STOP) +#define DATA_SEGMENT_START_ADDR ((uint8_t*) &DATA_SEGMENT_START) + #define MAX_HEAP_SIZE \ - ((16384 - (DATA_SEGMENT_STOP - DATA_SEGMENT_START)) / 4 * 4) + ((16384 - (DATA_SEGMENT_STOP_ADDR - DATA_SEGMENT_START_ADDR)) / 4 * 4) /* allocates memory on the head, which is stored in sram2 */ void* halloc(size_t n); diff --git a/02-usart/src/core/clock.c b/02-usart/src/core/clock.c index d779140..f32cb4e 100644 --- a/02-usart/src/core/clock.c +++ b/02-usart/src/core/clock.c @@ -103,4 +103,5 @@ int set_system_clock_src(system_clock_src_t src) { uint8_t value = RCC.cfg_r & ~0x03; RCC.cfg_r = value | src; + return 0; } diff --git a/02-usart/src/core/usart.c b/02-usart/src/core/usart.c index dc15e57..19d982e 100644 --- a/02-usart/src/core/usart.c +++ b/02-usart/src/core/usart.c @@ -5,12 +5,12 @@ void set_usart1_clock_src(__IO rcc_t* rcc, usart_clk_src_t usart_clk_src) { - rcc->ccip_r = rcc->ccip_r & (~0x03) | usart_clk_src; + rcc->ccip_r = (rcc->ccip_r & (~0x03)) | usart_clk_src; } void set_usart2_clock_src(__IO rcc_t* rcc, usart_clk_src_t usart_clk_src) { - rcc->ccip_r = rcc->ccip_r & ~(0x03 << 2) | (usart_clk_src << 2); + rcc->ccip_r = (rcc->ccip_r & ~(0x03 << 2)) | (usart_clk_src << 2); } void set_usart2_clock_enabled(__IO rcc_t* rcc, bool enable) @@ -41,8 +41,6 @@ void usart_set_parity(__IO usart_t* usart, usart_parity_t parity) void usart_set_enabled(__IO usart_t* usart, usart_enable_t enabled) { - uint32_t c_r1 = usart->c_r1; - if (!enabled) { regset(usart->c_r1, usart_ue, 0); } else { @@ -104,6 +102,7 @@ void usart_printf(__IO usart_t* usart, const char* fmt, ...) va_list l; union { void* ptr; + char* str; int i; } b; char buf[128]; @@ -130,6 +129,9 @@ void usart_printf(__IO usart_t* usart, const char* fmt, ...) decimalify(b.i, buf); usart_transmit_str_sync(usart, buf); break; + case 's': + b.str = va_arg(l, char*); + usart_transmit_str_sync(usart, b.str); } ++ fmt; } else { diff --git a/02-usart/src/main.c b/02-usart/src/main.c index d26dce4..b050d3e 100644 --- a/02-usart/src/main.c +++ b/02-usart/src/main.c @@ -81,9 +81,18 @@ int main() for (;;); } - const char* thing = "HELLO DMA!\r\n"; - regset(USART2.ic_r, usart_tccf, 1); - dma_mem2p_initiate_transfer(dma_chan, thing, strlen(thing)); + // const char* thing = "Good Thing This Works!"; + + char* str = halloc(128); + strcpy(str, "Hello, Heap!"); + + usart_printf(&USART2, "DATA_SEGMENT_START %p\n", &DATA_SEGMENT_START); + usart_printf(&USART2, "DATA_SEGMENT_STOP: %p\n", &DATA_SEGMENT_STOP); + usart_printf(&USART2, "str at: %p\n", str); + usart_printf(&USART2, "str: %s\n", str); + // usart_printf(&USART2, "%s\n", thing); + // regset(USART2.ic_r, usart_tccf, 1); + // dma_mem2p_initiate_transfer(dma_chan, thing, strlen(thing)); __IO gpio_port_t* port_b = enable_gpio(GPIO_PORT_B); gpio_output_pin_t pin3 = set_gpio_pin_output(port_b, PIN_3); diff --git a/02-usart/src/mem.c b/02-usart/src/mem.c index 0cb9ee4..02d59de 100644 --- a/02-usart/src/mem.c +++ b/02-usart/src/mem.c @@ -62,7 +62,7 @@ halloc_node_t* halloc_start; void* halloc(size_t size) { if (!halloc_start) { - halloc_start = (halloc_node_t*) DATA_SEGMENT_STOP; + halloc_start = (halloc_node_t*) DATA_SEGMENT_STOP_ADDR; memset(halloc_start, 0, sizeof(halloc_node_t)); halloc_start->size = (MAX_HEAP_SIZE / 4) - 1; } @@ -161,20 +161,19 @@ int debug_halloc_assert_consistency(char* error, size_t len) { halloc_node_t* cur = halloc_node_at_off(0); size_t total_size = 0; - size_t offset = 0; size_t loop_check = 0; while(1) { total_size += cur->size + 1; halloc_node_t* next = halloc_node_next(cur); - if (next == DATA_SEGMENT_STOP + MAX_HEAP_SIZE) { + if ((uint8_t*) next == ((uint8_t*)&DATA_SEGMENT_STOP) + MAX_HEAP_SIZE) { break; - } else if (next > (void*)(DATA_SEGMENT_STOP + MAX_HEAP_SIZE)){ + } else if ((uint8_t*) next > (uint8_t*)DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE){ snprintf( error, len, "Next node points is out of bounds. %p vs max of %p\n", next, - (void*)(DATA_SEGMENT_STOP + MAX_HEAP_SIZE)); + (void*)(DATA_SEGMENT_STOP_ADDR + MAX_HEAP_SIZE)); return 1; } cur = next; @@ -182,7 +181,7 @@ int debug_halloc_assert_consistency(char* error, size_t len) if (total_size * 4 != MAX_HEAP_SIZE) { snprintf( - error, len, "Total recorded size is inconsistent. %d vs %d\n", + error, len, "Total recorded size is inconsistent. %lu vs %lu\n", total_size * 4, MAX_HEAP_SIZE); return 1; } diff --git a/02-usart/src/stdlibrepl.c b/02-usart/src/stdlibrepl.c new file mode 100644 index 0000000..2d9d839 --- /dev/null +++ b/02-usart/src/stdlibrepl.c @@ -0,0 +1,13 @@ +/* + * Replacement for common stdlib functions that don't exist + * on the ARM bare-metal compilation environment. + */ + +#include <stddef.h> + +size_t strlen(char* ch) +{ + size_t ret = 0; + while(*(ch ++) != 0) ++ ret; + return ret; +} diff --git a/02-usart/tests/test_memory.c b/02-usart/tests/test_memory.c index e68977a..1b88ad1 100644 --- a/02-usart/tests/test_memory.c +++ b/02-usart/tests/test_memory.c @@ -72,6 +72,8 @@ TEST(memory, halloc) ASSERT_CHAIN(test4, test5); wipeout_halloc(); + + return 0; } struct UNEVEN_STRUCT { @@ -102,6 +104,8 @@ TEST(memory, uneven_halloc) ASSERT_EQ(V(test1) + 12, test2); wipeout_halloc(); + + return 0; } TEST(memory, halloc_free) @@ -149,6 +153,8 @@ TEST(memory, halloc_free) ASSERT_EQ((*halloc_start >> 1) * 4, MAX_HEAP_SIZE - 4); wipeout_halloc(); + + return 0; } TEST(memory, halloc_free_alloc2) @@ -224,6 +230,8 @@ TEST(memory, consistency_stress) ASSERT_TRUE(false); } ASSERT_EQ((*halloc_start >> 1) * 4, MAX_HEAP_SIZE - 4); + + return 0; } TEST(memory, halloc_free_alloc) @@ -248,4 +256,6 @@ TEST(memory, halloc_free_alloc) // Test 2 was large enough to accomodate 3 smaller structs. ASSERT_EQ(V(test7), V(test2)); ASSERT_EQ(V(test8), V(test2) + sizeof(*test7) + 4); + + return 0; } |