Initial FreeRTOS support.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2023-10-24 09:35:16 +08:00
parent d8f30fd48d
commit 926b50ef6f
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
16 changed files with 886 additions and 71 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "SDK"]
path = SDK
url = https://git.minori.work/Embedded_SDK/N32WB031_SDK.git
[submodule "lib/freertos"]
path = lib/freertos
url = https://github.com/FreeRTOS/FreeRTOS-Kernel.git

View File

@ -8,6 +8,9 @@ enable_language(ASM)
# Different linker scripts
set(TARGET_LDSCRIPT_FLASH "${CMAKE_SOURCE_DIR}/SDK/gcc/n32wb03x_FLASH.ld")
# ROM API Symbols
set(TARGET_SYMBOL_LIST "${CMAKE_CURRENT_SOURCE_DIR}/SDK/middlewares/Nationstech/ble_library/ns_ble_stack/symdef/symbol_g15.txt")
set(TARGET_SOURCES
"SDK/firmware/n32wb03x_std_periph_driver/src/misc.c"
"SDK/firmware/n32wb03x_std_periph_driver/src/n32wb03x_adc.c"
@ -27,9 +30,31 @@ set(TARGET_SOURCES
"SDK/firmware/n32wb03x_std_periph_driver/src/n32wb03x_tim.c"
"SDK/firmware/n32wb03x_std_periph_driver/src/n32wb03x_usart.c"
"SDK/firmware/n32wb03x_std_periph_driver/src/n32wb03x_wwdg.c"
"SDK/middlewares/Nationstech/ble_library/ns_ble_profile/dis/diss/src/diss.c"
"SDK/middlewares/Nationstech/ble_library/ns_ble_profile/dis/diss/src/diss_task.c"
"SDK/middlewares/Nationstech/ble_library/ns_ble_profile/prf.c"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/hl/src/att/lib_att.c"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/stack_common/rwip.c"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/stack_common/rwip_driver.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/ble/ns_ble.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/ble/ns_ble_task.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/ecc/ns_ecc.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/ecc/sha256.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/ecc/uECC.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/error/ns_error.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/log/ns_log_lpuart.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/log/ns_log_usart.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/scheduler/ns_scheduler.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/sec/ns_sec.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/sleep/ns_sleep.c"
"SDK/middlewares/Nationstech/ble_library/ns_library/timer/ns_timer.c"
"board/board.c"
"board/n32wb03x_it.c"
"board/system_n32wb03x.c"
"SDK/gcc/startup_n32wb03x.S"
"src/app_ble_support.c"
"src/app_ble_profiles/app_ble_diss.c"
"src/app_freertos_support.c"
"src/main.c"
)
@ -42,8 +67,44 @@ set(TARGET_C_INCLUDES
"SDK/firmware/CMSIS/core"
"SDK/firmware/CMSIS/device"
"SDK/firmware/n32wb03x_std_periph_driver/inc"
"SDK/middlewares/Nationstech/ble_library/ns_ble_profile/dis/diss/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/arch"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/arch"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ahi/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/hl/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/hl/inc"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/hl/src/gap"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/hl/src/gatt"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/hl/src/l2c"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/ll/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/ll/src"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/ll/src/llc"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/ll/src/lld"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/ble/ll/src/llm"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/em/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/hci/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/ip/sch/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/aes/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/common/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/common/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/common/src"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/dbg/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/ecc_p256/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/h4tl/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/ke/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/modules/rwip/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/rfinit/api"
"SDK/middlewares/Nationstech/ble_library/ns_ble_stack/stack_common"
"SDK/middlewares/Nationstech/ble_library/ns_library/adv"
"SDK/middlewares/Nationstech/ble_library/ns_library/ble"
"SDK/middlewares/Nationstech/ble_library/ns_library/delay"
"SDK/middlewares/Nationstech/ble_library/ns_library/log"
"SDK/middlewares/Nationstech/ble_library/ns_library/sec"
"SDK/middlewares/Nationstech/ble_library/ns_library/sleep"
"SDK/middlewares/Nationstech/ble_library/ns_library/timer"
"board"
"include"
"include/ble"
)
# Shared libraries linked with application
@ -51,6 +112,7 @@ set(TARGET_LIBS
"c"
"m"
"nosys"
"freertos_kernel"
)
# Shared library and linker script search paths
@ -59,21 +121,31 @@ set(TARGET_LIB_DIRECTORIES
# Conditional flags
# DEBUG
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG -O0 -g")
# RELEASE
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_ASM_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_ASM_FLAGS_RELEASE "-DNDEBUG -O2 -flto")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto")
# Final compiler flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--just-symbols=${TARGET_SYMBOL_LIST} -Wl,--gc-sections")
# FreeRTOS
set(FREERTOS_PORT "GCC_ARM_CM0" CACHE STRING "")
set(FREERTOS_HEAP "4" CACHE STRING "")
add_library(freertos_config INTERFACE)
target_include_directories(freertos_config SYSTEM INTERFACE include)
add_subdirectory(lib/freertos)
# Shared sources, includes and definitions
add_compile_definitions(${TARGET_C_DEFINES})
@ -87,21 +159,21 @@ link_libraries(${TARGET_LIBS})
add_executable("${CMAKE_PROJECT_NAME}_FLASH.elf" ${TARGET_SOURCES})
target_compile_definitions("${CMAKE_PROJECT_NAME}_FLASH.elf"
PRIVATE ${TARGET_C_DEFINES_XIP}
)
)
target_link_options("${CMAKE_PROJECT_NAME}_FLASH.elf"
PRIVATE "-T${TARGET_LDSCRIPT_FLASH}"
PRIVATE "-Wl,--Map=${CMAKE_PROJECT_NAME}_FLASH.map"
)
)
set_property(TARGET "${CMAKE_PROJECT_NAME}_FLASH.elf" APPEND
PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_PROJECT_NAME}_FLASH.map"
)
)
add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_FLASH.hex"
COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_FLASH.elf" "${CMAKE_PROJECT_NAME}_FLASH.hex"
DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.elf"
)
)
add_custom_target("${CMAKE_PROJECT_NAME}_FLASH_HEX" DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.hex")
if(DEFINED TARGET_TOOLCHAIN_SIZE)
if (DEFINED TARGET_TOOLCHAIN_SIZE)
add_custom_command(TARGET "${CMAKE_PROJECT_NAME}_FLASH.elf" POST_BUILD
COMMAND ${TARGET_TOOLCHAIN_SIZE} "${CMAKE_PROJECT_NAME}_FLASH.elf"
)
endif()
)
endif ()

1
SDK Submodule

@ -0,0 +1 @@
Subproject commit e061fa30b5d4dd389623fa02d17aefd25a36ee7a

57
board/board.c Normal file
View File

@ -0,0 +1,57 @@
/* Device */
#include "n32wb03x.h"
/* Board */
#include "board.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
typedef struct {
GPIO_Module *port;
uint32_t port_rcc;
uint8_t pin;
bool active_high;
} board_led_table_t;
static const board_led_table_t s_led_table[] = {
{.port = GPIOB, .port_rcc = RCC_APB2_PERIPH_GPIOB, .pin = 0, .active_high = true},
{.port = GPIOA, .port_rcc = RCC_APB2_PERIPH_GPIOA, .pin = 6, .active_high = true},
};
void board_led_init(uint8_t led) {
GPIO_InitType led_init;
GPIO_InitStruct(&led_init);
if (led >= ARRAY_SIZE(s_led_table)) {
return;
}
led_init.Pin = (1U << s_led_table[led].pin);
led_init.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
RCC_EnableAPB2PeriphClk(s_led_table[led].port_rcc, ENABLE);
GPIO_InitPeripheral(s_led_table[led].port, &led_init);
board_led_set(led, false);
}
void board_led_set(uint8_t led, bool on) {
if (led >= ARRAY_SIZE(s_led_table)) {
return;
}
Bit_OperateType val = on ? Bit_SET : Bit_RESET;
if (!s_led_table[led].active_high) {
val = on ? Bit_RESET : Bit_SET;
}
GPIO_WriteBit(s_led_table[led].port, (1U << s_led_table[led].pin), val);
}
void board_led_toggle(uint8_t led) {
if (led >= ARRAY_SIZE(s_led_table)) {
return;
}
GPIO_TogglePin(s_led_table[led].port, (1U << s_led_table[led].pin));
}

11
board/board.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef BOARD_H
#define BOARD_H
#include <stdbool.h>
#include <stdint.h>
void board_led_init(uint8_t led);
void board_led_set(uint8_t led, bool on);
void board_led_toggle(uint8_t led);
#endif // BOARD_H

View File

@ -60,20 +60,6 @@ void HardFault_Handler(void)
}
}
/**
* @brief This function handles SVCall exception.
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles PendSV_Handler exception.
*/
void PendSV_Handler(void)
{
}
/*******************************************************************************/
/* N32WB03X Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */

View File

@ -253,13 +253,10 @@ void SystemCoreClockUpdate(void)
/**
* @brief dealy cycles.
*/
__attribute__((naked)) void system_delay_cycles(uint32_t i)
{
asm volatile(
"sub r0, #1\n"
"bne system_delay_cycles\n"
"bx lr"
);
void system_delay_cycles(uint32_t i) {
for (volatile uint32_t j = i; j > 0; j--) {
j = j;
}
}
/**

108
include/FreeRTOSConfig.h Normal file
View File

@ -0,0 +1,108 @@
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
/* Ensure stdint is only used by the compiler, and not the assembler. */
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
#endif
/* General configuration */
#define configUSE_PREEMPTION 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
#define configUSE_TICKLESS_IDLE 0
#define configCPU_CLOCK_HZ (SystemCoreClock)
#define configTICK_RATE_HZ 1000
#define configMAX_PRIORITIES 8
#define configMINIMAL_STACK_SIZE 128
#define configMAX_TASK_NAME_LEN 16
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
#define configUSE_TASK_NOTIFICATIONS 1
#define configTASK_NOTIFICATION_ARRAY_ENTRIES 3
#define configUSE_MUTEXES 0
#define configUSE_RECURSIVE_MUTEXES 0
#define configUSE_COUNTING_SEMAPHORES 0
#define configUSE_ALTERNATIVE_API 0 /* Deprecated! */
#define configQUEUE_REGISTRY_SIZE 10
#define configUSE_QUEUE_SETS 0
#define configUSE_TIME_SLICING 0
#define configUSE_NEWLIB_REENTRANT 1
#define configENABLE_BACKWARD_COMPATIBILITY 0
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
#define configSTACK_DEPTH_TYPE uint16_t
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t
/* Memory allocation related definitions. */
#define configSUPPORT_STATIC_ALLOCATION 0
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configTOTAL_HEAP_SIZE (16 * 1024)
#define configAPPLICATION_ALLOCATED_HEAP 0
#define configSTACK_ALLOCATION_FROM_SEPARATE_HEAP 0
/* Hook function related definitions. */
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCHECK_FOR_STACK_OVERFLOW 2
#define configUSE_MALLOC_FAILED_HOOK 1
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0
/* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_TRACE_FACILITY 0
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
/* Co-routine related definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES 1
/* Software timer related definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY 3
#define configTIMER_QUEUE_LENGTH 10
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
/* Define to trap errors during development. */
#define configASSERT( x ) if( ( x ) == 0 ) for(;;) { }
/* Optional functions - most linkers will remove unused functions anyway. */
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_xResumeFromISR 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_xTaskGetSchedulerState 1
#define INCLUDE_xTaskGetCurrentTaskHandle 1
#define INCLUDE_uxTaskGetStackHighWaterMark 0
#define INCLUDE_xTaskGetIdleTaskHandle 0
#define INCLUDE_eTaskGetState 0
#define INCLUDE_xEventGroupSetBitFromISR 1
#define INCLUDE_xTimerPendFunctionCall 0
#define INCLUDE_xTaskAbortDelay 0
#define INCLUDE_xTaskGetHandle 0
#define INCLUDE_xTaskResumeFromISR 1
/* Port-specific settings */
#ifdef __NVIC_PRIO_BITS
/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
#define configPRIO_BITS __NVIC_PRIO_BITS
#else
#define configPRIO_BITS 2 /* 3 priority levels */
#endif
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 3
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 3
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler
/* A header file that defines trace macro can be included here. */
#endif /* FREERTOS_CONFIG_H */

View File

@ -0,0 +1,63 @@
#ifndef APP_BLE_DISS_H
#define APP_BLE_DISS_H
#include <stdint.h>
#include "rwip_config.h" // SW Configuration
/* Define ------------------------------------------------------------*/
/// Manufacturer Name Value
#define APP_DISS_MANUFACTURER_NAME ("Nations")
#define APP_DISS_MANUFACTURER_NAME_LEN (7)
/// Model Number String Value
#define APP_DISS_MODEL_NB_STR ("NS-BLE-1.0")
#define APP_DISS_MODEL_NB_STR_LEN (10)
/// Serial Number
#define APP_DISS_SERIAL_NB_STR ("1.0.0.0-LE")
#define APP_DISS_SERIAL_NB_STR_LEN (10)
/// Firmware Revision
#define APP_DISS_FIRM_REV_STR ("1.0.0")
#define APP_DISS_FIRM_REV_STR_LEN (5)
/// System ID Value - LSB -> MSB
#define APP_DISS_SYSTEM_ID ("\x12\x34\x56\xFF\xFE\x9A\xBC\xDE")
#define APP_DISS_SYSTEM_ID_LEN (8)
/// Hardware Revision String
#define APP_DISS_HARD_REV_STR ("1.0.0")
#define APP_DISS_HARD_REV_STR_LEN (5)
/// Software Revision String
#define APP_DISS_SW_REV_STR ("1.0.0")
#define APP_DISS_SW_REV_STR_LEN (5)
/// IEEE
#define APP_DISS_IEEE ("\xFF\xEE\xDD\xCC\xBB\xAA")
#define APP_DISS_IEEE_LEN (6)
/**
* PNP ID Value - LSB -> MSB
* Vendor ID Source : 0x02 (USB Implementers Forum assigned Vendor ID value)
* Vendor ID : 0x045E (Microsoft Corp)
* Product ID : 0x0040
* Product Version : 0x0300
*/
#define APP_DISS_PNP_ID ("\x02\x5E\x04\x40\x00\x00\x03")
#define APP_DISS_PNP_ID_LEN (7)
#if (BLE_APP_HID)
#define APP_DISS_FEATURES \
(DIS_MANUFACTURER_NAME_CHAR_SUP_BIT | DIS_MODEL_NB_STR_CHAR_SUP_BIT | DIS_SYSTEM_ID_CHAR_SUP_BIT | \
DIS_PNP_ID_CHAR_SUP_BIT)
#else
#define APP_DISS_FEATURES (DIS_ALL_FEAT_SUP)
#endif //(BLE_APP_HID)
void app_ble_diss_init(void);
#endif // APP_BLE_DISS_H

View File

@ -0,0 +1,111 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file app_user_config.h
* @author Nations Firmware Team
* @version v1.0.1
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef _APP_USER_CONFIG_H_
#define _APP_USER_CONFIG_H_
#include "ns_adv_data_def.h"
/* Device name */
#define CUSTOM_DEVICE_NAME "Nations RTOS!"
/* adv configer*/
#define CUSTOM_ADV_FAST_INTERVAL 160 /**< Fast advertising interval (in units of 0.625 ms. This value corresponds to 100 ms.). */
#define CUSTOM_ADV_SLOW_INTERVAL 3200 /**< Slow advertising interval (in units of 0.625 ms. This value corresponds to 2 seconds). */
#define CUSTOM_ADV_FAST_DURATION 0 /**< The advertising duration of fast advertising in units of 1 seconds. maximum is 655 seconds */
#define CUSTOM_ADV_SLOW_DURATION 180 /**< The advertising duration of slow advertising in units of 1 seconds. maximum is 655 seconds */
// Advertise data
#define CUSTOM_USER_ADVERTISE_DATA "\x00"
#define CUSTOM_USER_ADVERTISE_DATA_LEN (sizeof(CUSTOM_USER_ADVERTISE_DATA) - 1)
// Scan response data
#define CUSTOM_USER_ADV_SCAN_RESP_DATA \
"\x06"\
ADV_TYPE_MANUFACTURER_SPECIFIC_DATA\
"\x25\x00NXP"
// Scan response data length- maximum 31 bytes
#define CUSTOM_USER_ADV_SCAN_RESP_DATA_LEN (sizeof(CUSTOM_USER_ADV_SCAN_RESP_DATA) - 1)
/* connection config */
#define MIN_CONN_INTERVAL 15 /**< Minimum connection interval (15 ms) */
#define MAX_CONN_INTERVAL 30 /**< Maximum connection interval (30 ms). */
#define SLAVE_LATENCY 0 /**< Slave latency. */
#define CONN_SUP_TIMEOUT 5000 /**< Connection supervisory timeout (5000ms). */
#define FIRST_CONN_PARAMS_UPDATE_DELAY (5000) /**< Time of initiating event to update connection params (5 seconds). */
//sec config
#define SEC_PARAM_IO_CAPABILITIES GAP_IO_CAP_NO_INPUT_NO_OUTPUT /**< No I/O capabilities. (@enum gap_io_cap) */
#define SEC_PARAM_OOB 0 /**< Out Of Band data not available. */
#define SEC_PARAM_KEY_SIZE 16 /**< Minimum encryption key size. 7 to 16 */
#define SEC_PARAM_BOND 1 /**< Perform bonding. */
#define SEC_PARAM_MITM 1 /**< Man In The Middle protection not required. */
#define SEC_PARAM_LESC 0 /**< LE Secure Connections not enabled. */
#define SEC_PARAM_KEYPRESS 0 /**< Keypress notifications not enabled. */
#define SEC_PARAM_IKEY GAP_KDIST_NONE /**< Initiator Key Distribution. (@enum gap_kdist) */
#define SEC_PARAM_RKEY GAP_KDIST_ENCKEY /**< Responder Key Distribution. (@enum gap_kdist) */
#define SEC_PARAM_SEC_MODE_LEVEL GAP_NO_SEC /**< Device security requirements (minimum security level). (@enum see gap_sec_req) */
//bond conifg
#define MAX_BOND_PEER 5
#define BOND_STORE_ENABLE 0
#define BOND_DATA_BASE_ADDR 0x0103B000
/* profiles config */
#define CFG_APP_DIS 1
#define CFG_PRF_DISS 1
/* User config */
#define NS_LOG_ERROR_ENABLE 1
#define NS_LOG_WARNING_ENABLE 1
#define NS_LOG_INFO_ENABLE 1
#define NS_LOG_DEBUG_ENABLE 0
#define NS_LOG_LPUART_ENABLE 1
#define NS_TIMER_ENABLE 1
#define FIRMWARE_VERSION "1.0.0"
#define HARDWARE_VERSION "1.0.0"
#endif // _APP_USER_CONFIG_H_

110
include/ble/rwapp_config.h Normal file
View File

@ -0,0 +1,110 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file rwapp_config.h
* @author Nations Firmware Team
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef _RWAPP_CONFIG_H_
#define _RWAPP_CONFIG_H_
/**
* @addtogroup app
* @brief Application configuration definition
*
* @{
**/
#include "app_user_config.h"
#include "rwip_config.h"
/* Includes ------------------------------------------------------------------*/
/* Define ------------------------------------------------------------*/
/// Application Profile
#if defined(CFG_APP_PRF)
#define BLE_APP_PRF 1
#else // defined(CFG_APP_PRF)
#define BLE_APP_PRF 0
#endif // defined(CFG_APP_PRF)
/// Health Thermometer Application
#if defined(CFG_APP_HT)
#define BLE_APP_HT 1
#else // defined(CFG_APP_HT)
#define BLE_APP_HT 0
#endif // defined(CFG_APP_HT)
/// HID Application
#if defined(CFG_APP_HID)
#define BLE_APP_HID 1
#else // defined(CFG_APP_HID)
#define BLE_APP_HID 0
#endif // defined(CFG_APP_HID)
/// DIS Application
#if defined(CFG_APP_DIS)
#define BLE_APP_DIS 1
#else // defined(CFG_APP_DIS)
#define BLE_APP_DIS 0
#endif // defined(CFG_APP_DIS)
/// Battery Service Application
#if (CFG_APP_BATT)
#define BLE_APP_BATT 1
#else
#define BLE_APP_BATT 0
#endif //(CFG_APP_BATT)
/// Security Application
#if (defined(CFG_APP_SEC) || BLE_APP_HID)
#define BLE_APP_SEC 1
#else //(defined(CFG_APP_SEC) || BLE_APP_HID)
#define BLE_APP_SEC 0
#endif //(defined(CFG_APP_SEC) || BLE_APP_HID)
#define AM0_APP_OPTIONAL_CHARACTERISTICS 0
#if defined(CFG_APP_NS_IUS)
#define BLE_APP_NS_IUS 1
#else // defined(CFG_APP_NS_IUS)
#define BLE_APP_NS_IUS 0
#endif // defined(CFG_APP_NS_IUS)
/// @} rwapp_config
#endif /* _RWAPP_CONFIG_H_ */

1
lib/freertos Submodule

@ -0,0 +1 @@
Subproject commit 7562ebc6e13ae560de98b00ac96c2d518b0c6639

View File

@ -0,0 +1,109 @@
#include "app_ble_profiles/app_ble_diss.h"
/* Stack configuration */
#include "rwip_config.h"
/* BLE Stack */
#include "ns_ble.h"
/* Profile */
#include "diss.h"
#include "diss_task.h"
static int app_ble_diss_handler(ke_msg_id_t const msgid, struct diss_value_req_ind const *param,
ke_task_id_t const dest_id, ke_task_id_t const src_id);
static const struct ke_msg_handler app_diss_msg_handler_list[] = {
{DISS_VALUE_REQ_IND, (ke_msg_func_t)app_ble_diss_handler},
};
static const struct app_subtask_handlers app_diss_handlers = APP_HANDLERS(app_diss);
void app_ble_diss_init(void) {
struct diss_db_cfg *db_cfg;
struct gapm_profile_task_add_cmd *req = KE_MSG_ALLOC_DYN(GAPM_PROFILE_TASK_ADD_CMD, TASK_GAPM, TASK_APP,
gapm_profile_task_add_cmd, sizeof(struct diss_db_cfg));
req->operation = GAPM_PROFILE_TASK_ADD;
req->sec_lvl = PERM(SVC_AUTH, NO_AUTH);
req->prf_task_id = TASK_ID_DISS;
req->app_task = TASK_APP;
req->start_hdl = 0;
db_cfg = (struct diss_db_cfg *)req->param;
db_cfg->features = APP_DISS_FEATURES;
ke_msg_send(req);
struct prf_task_t prf;
prf.prf_task_id = TASK_ID_DISS;
prf.prf_task_handler = &app_diss_handlers;
ns_ble_prf_task_register(&prf);
struct prf_get_func_t get_func;
get_func.task_id = TASK_ID_DISS;
get_func.prf_itf_get_func = diss_prf_itf_get;
prf_get_itf_func_register(&get_func);
}
static int app_ble_diss_handler(ke_msg_id_t const msgid, struct diss_value_req_ind const *param,
ke_task_id_t const dest_id, ke_task_id_t const src_id) {
// Initialize length
uint8_t len = 0;
// Pointer to the data
uint8_t *data = NULL;
// Check requested value
switch (param->value) {
case DIS_MANUFACTURER_NAME_CHAR:
len = APP_DISS_MANUFACTURER_NAME_LEN;
data = (uint8_t *)APP_DISS_MANUFACTURER_NAME;
break;
case DIS_MODEL_NB_STR_CHAR:
len = APP_DISS_MODEL_NB_STR_LEN;
data = (uint8_t *)APP_DISS_MODEL_NB_STR;
break;
case DIS_SYSTEM_ID_CHAR:
len = APP_DISS_SYSTEM_ID_LEN;
data = (uint8_t *)APP_DISS_SYSTEM_ID;
break;
case DIS_PNP_ID_CHAR:
len = APP_DISS_PNP_ID_LEN;
data = (uint8_t *)APP_DISS_PNP_ID;
break;
case DIS_SERIAL_NB_STR_CHAR:
len = APP_DISS_SERIAL_NB_STR_LEN;
data = (uint8_t *)APP_DISS_SERIAL_NB_STR;
break;
case DIS_HARD_REV_STR_CHAR:
len = APP_DISS_HARD_REV_STR_LEN;
data = (uint8_t *)APP_DISS_HARD_REV_STR;
break;
case DIS_FIRM_REV_STR_CHAR:
len = APP_DISS_FIRM_REV_STR_LEN;
data = (uint8_t *)APP_DISS_FIRM_REV_STR;
break;
case DIS_SW_REV_STR_CHAR:
len = APP_DISS_SW_REV_STR_LEN;
data = (uint8_t *)APP_DISS_SW_REV_STR;
break;
case DIS_IEEE_CHAR:
len = APP_DISS_IEEE_LEN;
data = (uint8_t *)APP_DISS_IEEE;
break;
default:
break;
}
struct diss_value_cfm *cfm_value = KE_MSG_ALLOC_DYN(DISS_VALUE_CFM, src_id, dest_id, diss_value_cfm, len);
cfm_value->value = param->value;
cfm_value->length = len;
if (len) {
memcpy(&cfm_value->data[0], data, len);
}
ke_msg_send(cfm_value);
return (KE_MSG_CONSUMED);
}

172
src/app_ble_support.c Normal file
View File

@ -0,0 +1,172 @@
/* FreeRTOS */
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
/* BLE Stack */
#include "ns_ble.h"
#include "ns_ble_task.h"
#include "ns_sec.h"
/* Profiles */
#include "app_ble_profiles/app_ble_diss.h"
/* Board */
#include "board.h"
static TaskHandle_t s_ble_task_handle;
static void app_ble_task(void *parameters);
static void app_ble_msg_handler(struct ble_msg_t const *p_ble_msg);
static void app_user_msg_handler(ke_msg_id_t msgid, void const *p_param);
static void app_ble_gap_params_init(void);
static void app_ble_adv_init(void);
static void app_ble_sec_init(void);
static void app_ble_prf_init(void);
void app_ble_init(void) {
struct ns_stack_cfg_t app_handler = {0};
app_handler.ble_msg_handler = app_ble_msg_handler;
app_handler.user_msg_handler = app_user_msg_handler;
app_handler.lsc_cfg = BLE_LSC_LSE_32768HZ;
ns_ble_stack_init(&app_handler);
app_ble_gap_params_init();
app_ble_sec_init();
app_ble_adv_init();
app_ble_prf_init();
// start advertising
ns_ble_adv_start();
if (xTaskCreate(app_ble_task, "BLE", 1536, NULL, 5, &s_ble_task_handle) != pdPASS) {
for (;;) {
/* -- */
}
}
NVIC_SetPriority(RESERVED_IRQn, 2);
NVIC_EnableIRQ(RESERVED_IRQn);
}
void RESERVED_IRQHandler(void) {
BaseType_t hp_woken = pdFALSE;
xTaskNotifyFromISR(s_ble_task_handle, 0, eNoAction, &hp_woken);
portYIELD_FROM_ISR(hp_woken);
}
static void app_ble_task(void *parameters) {
uint32_t notification_value;
for (;;) {
rwip_schedule();
board_led_toggle(0);
xTaskNotifyWait(0, 0, &notification_value, portMAX_DELAY);
}
}
static void app_ble_msg_handler(struct ble_msg_t const *p_ble_msg) {
switch (p_ble_msg->msg_id) {
case APP_BLE_GAP_CONNECTED:
board_led_set(1, true);
break;
case APP_BLE_GAP_DISCONNECTED:
ns_ble_adv_start();
board_led_set(1, false);
break;
default:
break;
}
}
static void app_ble_adv_msg_handler(enum app_adv_mode adv_mode) {
}
static void app_user_msg_handler(ke_msg_id_t const msgid, void const *p_param) {
}
static void app_ble_gap_params_init(void) {
struct ns_gap_params_t dev_info = {0};
uint8_t *p_mac = SystemGetMacAddr();
if (p_mac != NULL) {
memcpy(dev_info.mac_addr.addr, p_mac, BD_ADDR_LEN);
} else {
memcpy(dev_info.mac_addr.addr, "\x01\x02\x03\x04\x05\x06", BD_ADDR_LEN);
}
dev_info.mac_addr_type = GAPM_STATIC_ADDR;
dev_info.appearance = 0;
dev_info.dev_role = GAP_ROLE_PERIPHERAL;
dev_info.dev_name_len = sizeof(CUSTOM_DEVICE_NAME) - 1;
memcpy(dev_info.dev_name, CUSTOM_DEVICE_NAME, dev_info.dev_name_len);
dev_info.dev_conn_param.intv_min = MSECS_TO_UNIT(MIN_CONN_INTERVAL, MSECS_UNIT_1_25_MS);
dev_info.dev_conn_param.intv_max = MSECS_TO_UNIT(MAX_CONN_INTERVAL, MSECS_UNIT_1_25_MS);
dev_info.dev_conn_param.latency = SLAVE_LATENCY;
dev_info.dev_conn_param.time_out = MSECS_TO_UNIT(CONN_SUP_TIMEOUT, MSECS_UNIT_10_MS);
dev_info.conn_param_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
ns_ble_gap_init(&dev_info);
}
static void app_ble_adv_init(void) {
struct ns_adv_params_t user_adv = {0};
// init advertising data
user_adv.adv_data_len = CUSTOM_USER_ADVERTISE_DATA_LEN;
memcpy(user_adv.adv_data, CUSTOM_USER_ADVERTISE_DATA, CUSTOM_USER_ADVERTISE_DATA_LEN);
user_adv.scan_rsp_data_len = CUSTOM_USER_ADV_SCAN_RESP_DATA_LEN;
memcpy(user_adv.scan_rsp_data, CUSTOM_USER_ADV_SCAN_RESP_DATA, CUSTOM_USER_ADV_SCAN_RESP_DATA_LEN);
user_adv.attach_appearance = false;
user_adv.attach_name = true;
user_adv.ex_adv_enable = false;
user_adv.adv_phy = PHY_1MBPS_VALUE;
// init advertising params
user_adv.directed_adv.enable = false;
user_adv.fast_adv.enable = true;
user_adv.fast_adv.duration = CUSTOM_ADV_FAST_DURATION;
user_adv.fast_adv.adv_intv = CUSTOM_ADV_FAST_INTERVAL;
user_adv.slow_adv.enable = true;
user_adv.slow_adv.duration = CUSTOM_ADV_SLOW_DURATION;
user_adv.slow_adv.adv_intv = CUSTOM_ADV_SLOW_INTERVAL;
user_adv.ble_adv_msg_handler = app_ble_adv_msg_handler;
ns_ble_adv_init(&user_adv);
}
static void app_ble_sec_init(void) {
struct ns_sec_init_t sec_init = {0};
sec_init.rand_pin_enable = false;
sec_init.pin_code = 123456;
sec_init.pairing_feat.auth =
(SEC_PARAM_BOND | (SEC_PARAM_MITM << 2) | (SEC_PARAM_LESC << 3) | (SEC_PARAM_KEYPRESS << 4));
sec_init.pairing_feat.iocap = SEC_PARAM_IO_CAPABILITIES;
sec_init.pairing_feat.key_size = SEC_PARAM_KEY_SIZE;
sec_init.pairing_feat.oob = SEC_PARAM_OOB;
sec_init.pairing_feat.ikey_dist = SEC_PARAM_IKEY;
sec_init.pairing_feat.rkey_dist = SEC_PARAM_RKEY;
sec_init.pairing_feat.sec_req = SEC_PARAM_SEC_MODE_LEVEL;
sec_init.bond_enable = BOND_STORE_ENABLE;
sec_init.bond_db_addr = BOND_DATA_BASE_ADDR;
sec_init.bond_max_peer = MAX_BOND_PEER;
sec_init.bond_sync_delay = 2000;
sec_init.ns_sec_msg_handler = NULL;
ns_sec_init(&sec_init);
}
static void app_ble_prf_init(void) {
ns_ble_add_prf_func_register(app_ble_diss_init);
}

View File

@ -0,0 +1,22 @@
#include "FreeRTOS.h"
#include "task.h"
/* SDK */
#include "n32wb03x.h"
/* BLE */
#include "ns_ble.h"
#include "ns_ble_task.h"
#include "ns_sleep.h"
void vApplicationMallocFailedHook(void) {
for (;;) {
__WFI();
}
}
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
for (;;) {
__WFI();
}
}

View File

@ -1,47 +1,39 @@
/* FreeRTOS */
#include "FreeRTOS.h"
#include "task.h"
/* Device */
#include "n32wb03x.h"
#include "n32wb03x_gpio.h"
static volatile uint32_t uwTick = 0U;
/* Board */
#include "board.h"
static void HAL_Delay(uint32_t msec);
void app_ble_init(void);
static void task_initialize(void *parameters);
int main(void) {
SysTick_Config(SystemCoreClock / 1000);
board_led_init(0);
board_led_init(1);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
app_ble_init();
GPIO_InitType led;
led.Pin = GPIO_PIN_0;
led.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
led.GPIO_Speed = GPIO_SPEED_LOW;
led.GPIO_Current = GPIO_DC_12MA;
led.GPIO_Pull = GPIO_NO_PULL;
led.GPIO_Alternate = GPIO_AF0;
GPIO_InitPeripheral(GPIOB, &led);
led.Pin = GPIO_PIN_6;
GPIO_InitPeripheral(GPIOA, &led);
GPIO_TogglePin(GPIOB, GPIO_PIN_0);
for (;;) {
GPIO_TogglePin(GPIOB, GPIO_PIN_0);
GPIO_TogglePin(GPIOA, GPIO_PIN_6);
HAL_Delay(500);
if (xTaskCreate(task_initialize, "INIT", 256, NULL, 4, NULL) != pdTRUE) {
goto dead_loop;
}
}
static void HAL_Delay(uint32_t msec) {
uint32_t t_start = uwTick;
vTaskStartScheduler();
while (uwTick - t_start < msec) {
dead_loop:
for (;;) {
/* -- */
__WFI();
}
}
void SysTick_Handler(void) {
uwTick++;
}
static void task_initialize(void *parameters) {
for (;;) {
// board_led_toggle(0);
vTaskDelay(pdMS_TO_TICKS(100));
}
}