diff --git a/Makefile b/Makefile index 735e5c2..280969c 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,8 @@ specs := --specs=nosys.specs --specs=nano.specs ifeq ($(debug),1) debug_flags := -O0 -g -ggdb else - debug_flags := -Os -DNDEBUG + debug_flags := -Os -DNDEBUG -fno-strength-reduce -fomit-frame-pointer + endif cmsis_startup := @@ -40,8 +41,8 @@ inc_flags += -I$(cmsis_dir)/inc \ -I$(top_dir)/src COMM_CFLAGS += -mthumb -fno-common -Wall -ffunction-sections -fdata-sections \ - -ffreestanding -fno-builtin -mcpu=cortex-m4 - + -ffreestanding -fno-builtin -mcpu=cortex-m4 -march=armv7e-m \ + -fno-strict-aliasing -Wshadow -pipe TARGET ?= audio -include targets.mk @@ -51,6 +52,9 @@ iap_script ?= $(top_dir)/src/iap.ld FP_FLAGS ?= -mfloat-abi=hard -mfpu=fpv4-sp-d16 -D__FPU_USED=1 -D__FPU_PRESENT=1 +ARCHCCVERSION = ${shell $(CC) -v 2>&1 | sed -n '/^gcc version/p' | sed -e 's/^gcc version \([0-9\.]\)/\1/g' -e 's/[-\ ].*//g' -e '1q'} +ARCHCCMAJOR = ${shell echo $(ARCHCCVERSION) | cut -d'.' -f1} + TARGET_CFLAGS += $(FP_FLAGS) TARGET_CFLAGS += $(COMM_CFLAGS) TARGET_CFLAGS += -DUSE_STDPERIPH_DRIVER @@ -224,7 +228,7 @@ endif st-flash: $(BINARY).hex st-flash --format ihex write $< -dbg: $(BINARY).elf +debug: $(BINARY).elf $(GDB) $^ -x $(top_dir)/gdb/init.gdb clean: diff --git a/cmsis/src/startup_stm32f40xx.S b/cmsis/src/startup_stm32f40xx.S index f7891bc..a8d9829 100644 --- a/cmsis/src/startup_stm32f40xx.S +++ b/cmsis/src/startup_stm32f40xx.S @@ -66,6 +66,12 @@ defined in linker script */ * @retval : None */ + .section .text.lowlevel_init + .weak lowlevel_init + .type lowlevel_init, %function +lowlevel_init: + bx lr + .section .text.Reset_Handler .weak Reset_Handler .type Reset_Handler, %function @@ -101,6 +107,10 @@ LoopFillZerobss: /* Call the clock system intitialization function.*/ bl SystemInit + +/* preinit before main */ + bl lowlevel_init + /* Call the application's entry point.*/ bl main bx lr diff --git a/cmsis/src/startup_stm32f429_439xx.S b/cmsis/src/startup_stm32f429_439xx.S index 3ee5d3b..ae68836 100644 --- a/cmsis/src/startup_stm32f429_439xx.S +++ b/cmsis/src/startup_stm32f429_439xx.S @@ -66,6 +66,12 @@ defined in linker script */ * @retval : None */ + .section .text.lowlevel_init + .weak lowlevel_init + .type lowlevel_init, %function +lowlevel_init: + bx lr + .section .text.Reset_Handler .weak Reset_Handler .type Reset_Handler, %function @@ -102,7 +108,11 @@ LoopFillZerobss: /* Call the clock system intitialization function.*/ bl SystemInit /* Call static constructors */ - bl __libc_init_array + bl __libc_init_array + +/* preinit before main */ + bl lowlevel_init + /* Call the application's entry point.*/ bl main bx lr @@ -120,6 +130,7 @@ Default_Handler: Infinite_Loop: b Infinite_Loop .size Default_Handler, .-Default_Handler + /****************************************************************************** * * The minimal vector table for a Cortex M3. Note that the proper constructs diff --git a/gdb/stm32-lock.cfg b/gdb/stm32-lock.cfg new file mode 100644 index 0000000..4bfe1c5 --- /dev/null +++ b/gdb/stm32-lock.cfg @@ -0,0 +1,6 @@ +init +reset halt +stm32f2x lock 0 +reset halt +exit + diff --git a/gdb/stm32-unlock.cfg b/gdb/stm32-unlock.cfg new file mode 100644 index 0000000..72e9e63 --- /dev/null +++ b/gdb/stm32-unlock.cfg @@ -0,0 +1,5 @@ +init +reset halt +stm32f2x unlock 0 +reset halt +exit diff --git a/lib/a3k.c b/lib/a3k.c index 9c60216..ad7cfc9 100644 --- a/lib/a3k.c +++ b/lib/a3k.c @@ -12,12 +12,12 @@ #define RATE_PINS (6) struct fifo_tag { - char *pnew; /* points to newest fifo element */ - char *pold; /* points to oldest fifo element */ - short nelem; // number of bytes currently in fifo - short blen; - char *bend; - char *b; + char *pnew; /* points to newest fifo element */ + char *pold; /* points to oldest fifo element */ + short nelem;/* number of bytes currently in fifo */ + short blen; + char *bend; + char *b; }; typedef struct fifo_tag FIFO; @@ -34,8 +34,6 @@ struct a3k_tag { uint32_t ns; uint32_t es; - uint32_t dropped; - FIFO rx; uint8_t _buf[FORMAT3KBLEN]; }; @@ -728,7 +726,7 @@ int a3k_get_rawpacket(a3k_t tag, uint8_t *buf, int size) FIFO *f = &tag->rx; __fifo_getc(f); /* drop 0x61 */ - c = __fifo_getc(f); /*len (MSB) */ + c = __fifo_getc(f); /* len (MSB) */ len = (c << 8) | (__fifo_getc(f) & 0xff); /* len (LSB) */ __fifo_getc(f); /* drop type */ c = __fifo_getc(f); /* drop CHAND field */ @@ -836,10 +834,9 @@ start: old = f->b; } if (parity == 0) - return 1; //we have a full packet with good parity check + return 1; /* we have a full packet with good parity check */ else { a3k_discardpacket(tag); - ++tag->dropped; return 0; } } else @@ -880,8 +877,6 @@ int a3k_setup(a3k_t tag, int baudrate) pins_set(tag, 0x2c, 0, 1, 0); //ec, ns, es #endif - ticks_init(1); - if (packet_reset_a3k(tag)) { printf("reset a3k failed.\r\n"); err = -1; @@ -913,7 +908,6 @@ int a3k_setup(a3k_t tag, int baudrate) } tail: - ticks_close(); return err; } diff --git a/lib/tick.c b/lib/tick.c index b504105..f86c648 100644 --- a/lib/tick.c +++ b/lib/tick.c @@ -5,7 +5,7 @@ static unsigned int __irq_per_ms = TICKS_PER_MS; -volatile uint64_t __current_mills = 0; +volatile uint64_t __current_mills; static void (*__ticker)(int ms) = 0; @@ -41,6 +41,7 @@ void ticks_init(int ms) if (ms > 0) { __irq_per_ms = ms; } + __current_mills = 0; /* Set systick reload value to generate 1ms interrupt */ SysTick_Config(__irq_per_ms * SystemCoreClock / 1000); } diff --git a/src/main.c b/src/main.c index c9235e1..a259258 100644 --- a/src/main.c +++ b/src/main.c @@ -19,7 +19,7 @@ #define Y2_OUT (PJ2) #define Y2_IN (PJ3) -#define PKT_LEN (64) +#define PKT_LEN (48) #define PKT_NLEMS (128) /* must power of 2 */ #define RS422_PKT_HDR (0x61) @@ -43,10 +43,10 @@ struct tag_fifo { char *pnew; /* points to newest fifo element */ char *pold; /* points to oldest fifo element */ - short nelem; /* number of bytes currently in fifo */ - short blen; - char *bend; - char *b; + short nelem;/* number of bytes currently in fifo */ + short blen; + char *bend; + char *b; }; struct ringbuf { @@ -412,16 +412,14 @@ static void __on_20ms(int ms) static int __on_40ms(struct timer *tm, void *user) { to_send = (BIT0 | BIT1); - // gpio_toggle(LED_D6); +#ifdef STM32F40_41xxx + gpio_toggle(PC13); +#endif return 0; } int main() { - NVIC_SetVectorTable(NVIC_VectTab_FLASH, (APP_ADDRESS - NVIC_VectTab_FLASH)); - NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); - __enable_irq(); - /* * recovery from bad image. */ @@ -435,7 +433,7 @@ int main() /* debug console */ serial_setup(&serial0, CONSOLE_BAUDRATE, 8, SERIAL_STOPBITS_1); - printf("build: %s %s\r\n", __DATE__, __TIME__); + printf("build: %s (%s)\r\n", __DATE__, __TIME__); /* RS422 */ serial_setup(RS422_0, RS422_BAUDRATE, 8, SERIAL_STOPBITS_1); @@ -444,6 +442,10 @@ int main() /* eeprom */ eeprom_init(); +#ifdef STM32F40_41xxx + gpio_init(PC13, GPIO_OUTPUT); +#endif + #ifdef STM32F429_439xx gpio_init(Y1_OUT, GPIO_OUTPUT); gpio_init(Y1_IN, GPIO_INPUT); @@ -462,12 +464,14 @@ int main() init_frames(&tx[1], NULL, 0); printf("setup vocode 0\r\n"); + ticks_init(1); iwdg_reset(); a3k_setup(&a3k0, -1); printf("setup vocode 1\r\n"); iwdg_reset(); a3k_setup(&a3k1, -1); + ticks_close(); ticks_set_callback(__on_20ms); ticks_init(20); diff --git a/src/retarget.c b/src/retarget.c index 839ae64..a52332c 100644 --- a/src/retarget.c +++ b/src/retarget.c @@ -18,19 +18,21 @@ int console_read(void *ptr, int len) int console_write(const void *_ptr, int len) { -#if 0 - const uint8_t *ptr = _ptr, *ptr_save = _ptr; - int n; + const uint8_t *ptr = _ptr; + int n = len, x = len; while (len > 0) { n = serial_send_buffered(&serial0, ptr, len); len -= n; ptr += n; } - return ptr - ptr_save; -#else - serial_send_blocking(&serial0, _ptr, len); - return (len); -#endif + return (x); +} + +void lowlevel_init(void) +{ + NVIC_SetVectorTable(NVIC_VectTab_FLASH, (APP_ADDRESS - NVIC_VectTab_FLASH)); + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); + __enable_irq(); } static int do_mw(cmd_tbl_t s, int argc, char *argv[])