Skip to content

Commit

Permalink
Fix basic examples for new init
Browse files Browse the repository at this point in the history
  • Loading branch information
Thalhammer committed Nov 24, 2019
1 parent d3b30a5 commit b2da994
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 25 deletions.
25 changes: 25 additions & 0 deletions api/include/util/init_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

/*
Control init behaviour:
By default _libc_app_init will
- Check if gpio3 is pulled low and cancel boot if so
- Init the debug uart to 115200 baud
- Wait 10 seconds to give time to reflash on error
You can define a couple of symbols which, if present, will configure these features.
In order to configure them, place the following makros somewhere in the global scope of your program once.
Placeing the twice will result in a linker error.
*/

#ifdef __cplusplus
/* Disable auto init of the debug uart */
#define INIT_DISABLE_DEBUG_UART() extern "C" { int _init_debug_uart = 0; }
/* Disable 10 second delay */
#define INIT_DISABLE_SAFETY_DELAY() extern "C" { int _init_safety_delay = 0; }
#else
/* Disable auto init of the debug uart */
#define INIT_DISABLE_DEBUG_UART() int _init_debug_uart = 0;
/* Disable 10 second delay */
#define INIT_DISABLE_SAFETY_DELAY() int _init_safety_delay = 0;
#endif
15 changes: 11 additions & 4 deletions api/src/util/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ extern void (*__init_array_end []) (void);
extern void (*__fini_array_start []) (void);
extern void (*__fini_array_end []) (void);

int __attribute__((weak)) _init_debug_uart = 1;
int __attribute__((weak)) _init_safety_delay = 1;

static int call_preinit() {
TRACE("calling preinit array\r\n");
size_t count = __preinit_array_end - __preinit_array_start;
Expand Down Expand Up @@ -51,10 +54,14 @@ extern void __cxa_finalize(void*);

int _libc_app_init(void) {
if(boot_cfg() != TX_SUCCESS) return TX_SUCCESS;
if(debug_init() != TX_SUCCESS) return TX_SUCCESS;
TRACE("waiting some time\r\n");
qapi_Timer_Sleep(10, QAPI_TIMER_UNIT_SEC, true);
TRACE("init\r\n");
if(_init_debug_uart) {
if(debug_init() != TX_SUCCESS) return TX_SUCCESS;
}
if(_init_safety_delay) {
TRACE("waiting some time\r\n");
qapi_Timer_Sleep(10, QAPI_TIMER_UNIT_SEC, true);
TRACE("init\r\n");
}

int res = call_preinit();
if(res != TX_SUCCESS) return res;
Expand Down
5 changes: 4 additions & 1 deletion build_scripts/default.mk
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ $(OUTNAME).bin: $(OUTNAME).elf
@echo Generating $@
@$(OBJCOPY) -O binary --only-section=ER_RO --only-section=ER_RW $^ $@

$(OUTNAME).elf: $(OBJ) $(SELF_DIR)/linker.lds
$(OUTNAME).elf: $(OBJ) $(SELF_DIR)/linker.lds $(SELF_DIR)/../api/bin/lib.a
@mkdir -p $(OUTPUT_PATH)
@echo Linking $@
@$(LINK) -o $@ -e _txm_module_thread_shell_entry -T $(SELF_DIR)/linker.lds --entry=__txm_module_preamble -Map=$(OUTNAME).map --start-group $(OBJ) $(SELF_DIR)/../api/bin/lib.a $(GCC_TOOLCHAIN)/../lib/gcc/arm-none-eabi/7.3.1/libgcc.a --end-group -gc-sections -z defs --print-memory-usage
Expand All @@ -93,4 +93,7 @@ $(OUTNAME).elf: $(OBJ) $(SELF_DIR)/linker.lds
@echo Building $<
@$(CC) -E $(FLAGS) $(CFLAGS) $(INC_PATHS) $< > $@

$(SELF_DIR)/../api/bin/lib.a:
@+make -C $(SELF_DIR)/../api/

-include $(OBJ:%=$(DEP_DIR)/%.d)
5 changes: 5 additions & 0 deletions examples/00-helloworld/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
#include "qapi_timer.h"
#include "qapi_uart.h"

#include "util/init_config.h"

#include "tx_api.h"

INIT_DISABLE_DEBUG_UART();
INIT_DISABLE_SAFETY_DELAY();

char send_buf[256];

int dam_app_start(void)
Expand Down
4 changes: 4 additions & 0 deletions examples/01-uart/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
#include "qapi/qapi_types.h"
#include "qapi/qapi.h"
#include "qapi/qapi_status.h"
#include "util/init_config.h"
#include "qapi_timer.h"
#include "uart.h"

#include "tx_api.h"

INIT_DISABLE_DEBUG_UART();
INIT_DISABLE_SAFETY_DELAY();

int dam_app_start(void)
{
const char* msg = "Hello from DAM :) \r\n";
Expand Down
6 changes: 4 additions & 2 deletions examples/03-timer/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "qapi/qapi_status.h"
#include "qapi_timer.h"
#include "util/uart.h"
#include "util/boot_cfg.h"
#include "util/init_config.h"

#include "tx_api.h"

Expand All @@ -19,9 +19,11 @@ void timer_test_cb(uint32_t udata) {

uint8_t uart_buf[256];

INIT_DISABLE_DEBUG_UART();
INIT_DISABLE_SAFETY_DELAY();

int dam_app_start(void)
{
if(boot_cfg() != 0) return TX_SUCCESS;
i=0;
uart_init_config_t uartcfg;
uart_default_cfg(&uartcfg);
Expand Down
4 changes: 4 additions & 0 deletions examples/04-net/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "qapi/qapi_timer.h"
#include "util/uart.h"
#include "util/boot_cfg.h"
#include "util/init_config.h"

#include "tx_api.h"

Expand Down Expand Up @@ -163,6 +164,9 @@ static void reconnect_cb(uint32_t udata) {
}
}

INIT_DISABLE_DEBUG_UART();
INIT_DISABLE_SAFETY_DELAY();

int dam_app_start(void)
{
if(boot_cfg() != 0) return TX_SUCCESS;
Expand Down
21 changes: 5 additions & 16 deletions examples/05-netmgr/src/main.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#include <stdlib.h>

#include "qapi/qapi_types.h"
#include "qapi/qapi.h"
#include "qapi/qapi_status.h"
#include "util/debug.h"
#include "txm_module.h"
#include "util/trace.h"
#include "util/netmgr.h"
#include "txm_module.h"
#include "util/boot_cfg.h"
#include "qapi/qapi_tlmm.h"
#include "qapi/qapi_timer.h"

#define TRACE_TAG "main"

Expand All @@ -25,18 +16,13 @@ void constate_changed(netmgr_constate_t s, void* a) {

int dam_app_start(void)
{
if(boot_cfg() != 0) return TX_SUCCESS;
if(debug_init() != 0) return TX_SUCCESS;
debug_printf("\033[2J\033[1;1H"); // Clear console

TRACE("waiting some time\r\n");
qapi_Timer_Sleep(10, QAPI_TIMER_UNIT_SEC, true);
TRACE("starting network\r\n");
if(netmgr_init() != TX_SUCCESS) {
TRACE("failed to init network manager\r\n");
return TX_SUCCESS;
}
netmgr_set_autoreconnect(false);
netmgr_set_autoreconnect(0);
netmgr_add_constate_cb(constate_changed, NULL);
TRACE("connecting to network\r\n");
if(netmgr_connect(APN, USER, PASS) != 0) {
Expand All @@ -46,5 +32,8 @@ int dam_app_start(void)

TRACE("init done\r\n");

// Wait to prevent libc_exit
while(1){ tx_thread_sleep(1000); }

return TX_SUCCESS;
}
2 changes: 0 additions & 2 deletions examples/06-mqtt/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
#include "qapi/qapi_ns_utils.h"
#include "qapi/qapi_dnsc.h"
#include "qapi/qapi_mqtt.h"
#include "util/debug.h"
#include "util/trace.h"
#include "util/netmgr.h"
#include "util/htons.h"
#include "util/boot_cfg.h"
#include "stdio.h"
#include "txm_module.h"

Expand Down

0 comments on commit b2da994

Please sign in to comment.