83 lines
2.9 KiB
C

/**
* @file main.c
* @brief Entry: clock update, USART banner, LED init, create tasks, start scheduler.
*/
#include "n32wb03x.h"
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "bsp_usart.h"
#include "bsp_led.h"
#include "app_tasks.h"
#include "app_cli.h"
#include "app_ble.h"
#include "app_version.h"
/* APP1 link base: the vector table lives at the start of the APP1 bank.
* Cortex-M0 has no SCB->VTOR; this chip remaps vectors via PWR->VTOR_REG
* (bit31 = enable, [30:0] = vector base). The SDK SystemInit() sets it to
* 0x81000000 (bootloader region at 0x01000000), so before the BLE stack is
* initialized the APP points VTOR at its own table (needed by USART1 RX IRQ).
*
* IMPORTANT: ns_ble_stack_init() clears VTOR to 0 ON PURPOSE: from then on
* all exceptions are dispatched through the chip-ROM trampolines + RAM relay
* (ns_ble_stack_vtor_init() copies our __Vectors to 0x200000e8/0x200009c0 and
* patches BLE_FIFO/BLE_SLP/EXTI4_12 to ROM/RAM handlers). Do NOT restore VTOR
* after app_ble_init() — with VTOR = flash base the BLE FIFO interrupt would
* land in the weak Default_Handler and hang the CPU. */
#define APP_VTOR_VALUE (0x80000000u | 0x01008000u)
/**
* @brief Main program.
*/
int main(void)
{
/* Power-on delay ~2s, same as the SDK BLE demos (rdtss / heart_rate):
* lets the analog blocks and the LSI 32kHz settle before the BLE stack
* init (and keeps SWD reachable for a while after reset). */
for (volatile uint32_t i = 0; i < 32000000u; i++)
{
__nop();
}
/* Update SystemCoreClock (configured to 64MHz HSI by SystemInit) */
SystemCoreClockUpdate();
/* Remap the vector table to this APP before any interrupt is enabled */
PWR->VTOR_REG = APP_VTOR_VALUE;
/* USART1 init, print startup banner before the scheduler runs */
bsp_usart_init();
printf("\n========================================\n");
printf(" N32WB031 FreeRTOS + UART + LED demo\n");
printf(" Firmware version %s\n", APP_FW_VERSION);
printf(" SystemCoreClock = %lu Hz\n", (unsigned long)SystemCoreClock);
printf(" BLE enabled, advertising as \"%s\"\n", "CAIIC-MCM");
printf("========================================\n");
/* LED1(PB0) / LED2(PA6) init */
bsp_led_init();
/* BLE stack init + start advertising + create the BLE schedule task.
* NOTE: ns_ble_stack_init() clears PWR->VTOR_REG so that interrupts are
* dispatched via the ROM trampoline + RAM relay (initialized from our
* __Vectors). VTOR must stay 0 from here on - do not restore it. */
app_ble_init();
/* Create log queue and tasks (LedTask, LedTask2, PrintTask) */
AppTasks_CreateAll();
/* Create the CLI task (command parsing/execution/response) */
AppCli_Init();
/* Start the FreeRTOS scheduler (port configures SysTick itself) */
vTaskStartScheduler();
/* We should never get here as control is now taken by the scheduler */
for (;;)
{
}
}