diff --git a/cmake-nRF5x/CMake_nRF5x.cmake b/cmake-nRF5x/CMake_nRF5x.cmake index 3e8e96aa..ec4b47db 100755 --- a/cmake-nRF5x/CMake_nRF5x.cmake +++ b/cmake-nRF5x/CMake_nRF5x.cmake @@ -70,22 +70,23 @@ macro(nRF5x_setup) endif() set(CPU_FLAGS "-mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16") add_definitions(-DNRF52 -DNRF52832 -DNRF52832_XXAA -DNRF52_PAN_74 -DNRF52_PAN_64 -DNRF52_PAN_12 -DNRF52_PAN_58 -DNRF52_PAN_54 -DNRF52_PAN_31 -DNRF52_PAN_51 -DNRF52_PAN_36 -DNRF52_PAN_15 -DNRF52_PAN_20 -DNRF52_PAN_55 -DBOARD_PCA10040) - add_definitions(-DSOFTDEVICE_PRESENT -DS132 -DSWI_DISABLE0 -DBLE_STACK_SUPPORT_REQD -DNRF_SD_BLE_API_VERSION=6) +# add_definitions(-DSOFTDEVICE_PRESENT -DS132 -DSWI_DISABLE0 -DBLE_STACK_SUPPORT_REQD -DNRF_SD_BLE_API_VERSION=6) add_definitions(-DFREERTOS) add_definitions(-DDEBUG_NRF_USER) - add_definitions(-D__STARTUP_CLEAR_BSS) - add_definitions(-D__HEAP_SIZE=8192) - add_definitions(-D__STACK_SIZE=2048) +# add_definitions(-D__STARTUP_CLEAR_BSS) +# add_definitions(-D__HEAP_SIZE=8192) +# add_definitions(-D__STACK_SIZE=2048) include_directories( - "${NRF5_SDK_PATH}/components/softdevice/s132/headers" - "${NRF5_SDK_PATH}/components/softdevice/s132/headers/nrf52" +# "${NRF5_SDK_PATH}/components/softdevice/s132/headers" +# "${NRF5_SDK_PATH}/components/softdevice/s132/headers/nrf52" + "${NRF5_SDK_PATH}/components/drivers_nrf/nrf_soc_nosd" ) list(APPEND SDK_SOURCE_FILES "${NRF5_SDK_PATH}/modules/nrfx/mdk/system_nrf52.c" "${NRF5_SDK_PATH}/modules/nrfx/mdk/gcc_startup_nrf52.S" ) - set(SOFTDEVICE_PATH "${NRF5_SDK_PATH}/components/softdevice/s132/hex/s132_nrf52_6.1.1_softdevice.hex") +# set(SOFTDEVICE_PATH "${NRF5_SDK_PATH}/components/softdevice/s132/hex/s132_nrf52_6.1.1_softdevice.hex") endif () set(COMMON_FLAGS "-MP -MD -mthumb -mabi=aapcs -Wall -g3 -ffunction-sections -fdata-sections -fno-strict-aliasing -fno-builtin --short-enums ${CPU_FLAGS} -Wreturn-type -Werror=return-type") @@ -254,69 +255,69 @@ macro(nRF5x_setup) # LCD/GFX include_directories( "${NRF5_SDK_PATH}/external/thedotfactory_fonts" - "${NRF5_SDK_PATH}/components/ble/ble_db_discovery" +# "${NRF5_SDK_PATH}/components/ble/ble_db_discovery" ) list(APPEND SDK_SOURCE_FILES - "${NRF5_SDK_PATH}/components/ble/ble_db_discovery/ble_db_discovery.c" - "${NRF5_SDK_PATH}/components/ble/ble_services/ble_cts_c/ble_cts_c.c" - "${NRF5_SDK_PATH}/components/ble/ble_services/ble_ans_c/ble_ans_c.c" +# "${NRF5_SDK_PATH}/components/ble/ble_db_discovery/ble_db_discovery.c" +# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_cts_c/ble_cts_c.c" +# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_ans_c/ble_ans_c.c" # "${NRF5_SDK_PATH}/external/thedotfactory_fonts/orkney24pts.c" ) #BLE S132 - include_directories( - "${NRF5_SDK_PATH}/components/ble/common" - "${NRF5_SDK_PATH}/components/ble/ble_advertising" - "${NRF5_SDK_PATH}/components/ble/ble_services/ble_bas" - "${NRF5_SDK_PATH}/components/ble/ble_services/ble_hrs" - "${NRF5_SDK_PATH}/components/ble/ble_services/ble_dis" - "${NRF5_SDK_PATH}/components/ble/nrf_ble_gatt" - "${NRF5_SDK_PATH}/components/libraries/sensorsim" - "${NRF5_SDK_PATH}/components/ble/peer_manager" - "${NRF5_SDK_PATH}/components/ble/nrf_ble_qwr" - ) +# include_directories( +# "${NRF5_SDK_PATH}/components/ble/common" +# "${NRF5_SDK_PATH}/components/ble/ble_advertising" +# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_bas" +# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_hrs" +# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_dis" +# "${NRF5_SDK_PATH}/components/ble/nrf_ble_gatt" +# "${NRF5_SDK_PATH}/components/libraries/sensorsim" +# "${NRF5_SDK_PATH}/components/ble/peer_manager" +# "${NRF5_SDK_PATH}/components/ble/nrf_ble_qwr" +# ) LIST(APPEND SDK_SOURCE_FILES - "${NRF5_SDK_PATH}//components/ble/common/ble_srv_common.c" - "${NRF5_SDK_PATH}/components/ble/ble_advertising/ble_advertising.c" - "${NRF5_SDK_PATH}/components/ble/common/ble_advdata.c" - "${NRF5_SDK_PATH}/components/ble/ble_services/ble_bas/ble_bas.c" - "${NRF5_SDK_PATH}/components/ble/ble_services/ble_hrs/ble_hrs.c" - "${NRF5_SDK_PATH}/components/ble/ble_services/ble_dis/ble_dis.c" - "${NRF5_SDK_PATH}/components/ble/nrf_ble_gatt/nrf_ble_gatt.c" - "${NRF5_SDK_PATH}/components/libraries/sensorsim/sensorsim.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager.c" - "${NRF5_SDK_PATH}/components/ble/nrf_ble_qwr/nrf_ble_qwr.c" - "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/auth_status_tracker.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/gatt_cache_manager.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/gatts_cache_manager.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/id_manager.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_data_storage.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_database.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_id.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager_handler.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/pm_buffer.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/security_dispatcher.c" - "${NRF5_SDK_PATH}/components/ble/peer_manager/security_manager.c" - "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" - "${NRF5_SDK_PATH}/components/ble/common/ble_conn_params.c" - "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" - "${NRF5_SDK_PATH}/components/libraries/atomic_flags/nrf_atflags.c" - "${NRF5_SDK_PATH}/components/libraries/fds/fds.c" - "${NRF5_SDK_PATH}/components/libraries/fstorage/nrf_fstorage.c" - "${NRF5_SDK_PATH}/components/libraries/fstorage/nrf_fstorage_sd.c" - "${NRF5_SDK_PATH}/components/libraries/atomic_fifo/nrf_atfifo.c" - "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh.c" - "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_ble.c" - "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_freertos.c" - "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_soc.c" - "${NRF5_SDK_PATH}/components/libraries/experimental_section_vars/nrf_section_iter.c" - "${NRF5_SDK_PATH}/components/libraries/bsp/bsp_btn_ble.c" - "${NRF5_SDK_PATH}/components/libraries/hardfault/hardfault_implementation.c" - "${NRF5_SDK_PATH}/components/libraries/hardfault/nrf52/handler/hardfault_handler_gcc.c" +# "${NRF5_SDK_PATH}//components/ble/common/ble_srv_common.c" +# "${NRF5_SDK_PATH}/components/ble/ble_advertising/ble_advertising.c" +# "${NRF5_SDK_PATH}/components/ble/common/ble_advdata.c" +# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_bas/ble_bas.c" +# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_hrs/ble_hrs.c" +# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_dis/ble_dis.c" +# "${NRF5_SDK_PATH}/components/ble/nrf_ble_gatt/nrf_ble_gatt.c" +# "${NRF5_SDK_PATH}/components/libraries/sensorsim/sensorsim.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager.c" +# "${NRF5_SDK_PATH}/components/ble/nrf_ble_qwr/nrf_ble_qwr.c" +# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/auth_status_tracker.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/gatt_cache_manager.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/gatts_cache_manager.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/id_manager.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_data_storage.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_database.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_id.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager_handler.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/pm_buffer.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/security_dispatcher.c" +# "${NRF5_SDK_PATH}/components/ble/peer_manager/security_manager.c" +# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" +# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_params.c" +# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" +# "${NRF5_SDK_PATH}/components/libraries/atomic_flags/nrf_atflags.c" +# "${NRF5_SDK_PATH}/components/libraries/fds/fds.c" +# "${NRF5_SDK_PATH}/components/libraries/fstorage/nrf_fstorage.c" +# "${NRF5_SDK_PATH}/components/libraries/fstorage/nrf_fstorage_sd.c" +# "${NRF5_SDK_PATH}/components/libraries/atomic_fifo/nrf_atfifo.c" +# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh.c" +# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_ble.c" +# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_freertos.c" +# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_soc.c" +# "${NRF5_SDK_PATH}/components/libraries/experimental_section_vars/nrf_section_iter.c" +# "${NRF5_SDK_PATH}/components/libraries/bsp/bsp_btn_ble.c" +# "${NRF5_SDK_PATH}/components/libraries/hardfault/hardfault_implementation.c" +# "${NRF5_SDK_PATH}/components/libraries/hardfault/nrf52/handler/hardfault_handler_gcc.c" ) LIST(APPEND SDK_SOURCE_FILES diff --git a/gcc_nrf52.ld b/gcc_nrf52.ld index 992c6c2e..b849ea2c 100644 --- a/gcc_nrf52.ld +++ b/gcc_nrf52.ld @@ -5,8 +5,8 @@ GROUP(-lgcc -lc -lnosys) MEMORY { - FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0x5a000 - RAM (rwx) : ORIGIN = 0x200057b8, LENGTH = 0xa848 + FLASH (rx) : ORIGIN = 0x00000, LENGTH = 0x80000 + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000 } SECTIONS diff --git a/src/BLE/BleManager.c b/src/BLE/BleManager.c index 8caf120a..d8f0bd85 100644 --- a/src/BLE/BleManager.c +++ b/src/BLE/BleManager.c @@ -1,3 +1,5 @@ +#if 0 + #include #include #include @@ -778,3 +780,4 @@ void ble_manager_delete_bonds() { err_code = pm_peers_delete(); APP_ERROR_CHECK(err_code); } +#endif \ No newline at end of file diff --git a/src/BLE/BleManager.h b/src/BLE/BleManager.h index da5f8e3f..6222fbb5 100644 --- a/src/BLE/BleManager.h +++ b/src/BLE/BleManager.h @@ -1,5 +1,5 @@ #pragma once -#include +//#include #ifdef __cplusplus extern "C" { @@ -39,7 +39,7 @@ void ble_manager_start_advertising(void *p_erase_bonds); void ble_manager_init_peer_manager(); // TODO use signals from RTOS to notify new time -void ble_manager_set_new_time_callback(void (*OnNewTime)(current_time_char_t* currentTime)); +//void ble_manager_set_new_time_callback(void (*OnNewTime)(current_time_char_t* currentTime)); void ble_manager_set_ble_disconnection_callback(void (*OnBleDisconnection)()); void ble_manager_set_ble_connection_callback(void (*OnBleConnection)()); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e92e3998..59448be8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -23,11 +23,100 @@ nRF5x_addAppGpiote() add_definitions(-DCONFIG_GPIO_AS_PINRESET) add_definitions(-DDEBUG) - +add_definitions(-DNIMBLE_CFG_CONTROLLER) +add_definitions(-DOS_CPUTIME_FREQ) include_directories(.) include_directories(libs/) +set(TINYCRYPT_SRC + libs/mynewt-nimble/ext/tinycrypt/src/aes_encrypt.c + libs/mynewt-nimble/ext/tinycrypt/src/utils.c + ) + +set(NIMBLE_SRC + libs/mynewt-nimble/porting/npl/freertos/src/nimble_port_freertos.c + libs/mynewt-nimble/porting/npl/freertos/src/npl_os_freertos.c + + + + libs/mynewt-nimble/nimble/host/src/ble_hs.c + libs/mynewt-nimble/nimble/host/src/ble_hs_hci_evt.c + libs/mynewt-nimble/nimble/host/src/ble_l2cap_sig_cmd.c + libs/mynewt-nimble/nimble/host/src/ble_l2cap_sig.c + libs/mynewt-nimble/nimble/host/src/ble_l2cap.c + libs/mynewt-nimble/nimble/host/src/ble_hs_mbuf.c + libs/mynewt-nimble/nimble/host/src/ble_sm.c + libs/mynewt-nimble/nimble/host/src/ble_gap.c + libs/mynewt-nimble/nimble/host/src/ble_gatts.c + libs/mynewt-nimble/nimble/host/src/ble_gattc.c + libs/mynewt-nimble/nimble/host/src/ble_hs_conn.c + libs/mynewt-nimble/nimble/host/src/ble_att_svr.c + libs/mynewt-nimble/nimble/host/src/ble_store.c + libs/mynewt-nimble/nimble/host/src/ble_hs_pvcy.c + libs/mynewt-nimble/nimble/host/src/ble_hs_hci.c + libs/mynewt-nimble/nimble/host/src/ble_hs_log.c + libs/mynewt-nimble/nimble/host/src/ble_hs_hci_util.c + libs/mynewt-nimble/nimble/host/src/ble_hs_hci_cmd.c + libs/mynewt-nimble/nimble/host/src/ble_hs_cfg.c + libs/mynewt-nimble/nimble/host/src/ble_uuid.c + libs/mynewt-nimble/nimble/host/src/ble_hs_id.c + libs/mynewt-nimble/nimble/host/src/ble_hs_misc.c + libs/mynewt-nimble/nimble/host/src/ble_att.c + libs/mynewt-nimble/nimble/host/src/ble_att_clt.c + libs/mynewt-nimble/nimble/host/src/ble_att_svr.c + libs/mynewt-nimble/nimble/host/src/ble_att_cmd.c + libs/mynewt-nimble/nimble/host/src/ble_hs_atomic.c + libs/mynewt-nimble/nimble/host/src/ble_hs_adv.c + libs/mynewt-nimble/nimble/host/src/ble_hs_flow.c + libs/mynewt-nimble/nimble/host/src/ble_sm.c + libs/mynewt-nimble/nimble/host/src/ble_sm_cmd.c + libs/mynewt-nimble/nimble/host/src/ble_sm_lgcy.c + libs/mynewt-nimble/nimble/host/src/ble_sm_alg.c + libs/mynewt-nimble/nimble/host/src/ble_hs_mqueue.c + libs/mynewt-nimble/nimble/host/src/ble_hs_stop.c + libs/mynewt-nimble/nimble/host/src/ble_hs_startup.c + libs/mynewt-nimble/nimble/host/store/ram/src/ble_store_ram.c + + libs/mynewt-nimble/nimble/transport/ram/src/ble_hci_ram.c + + + + libs/mynewt-nimble/nimble/controller/src/ble_ll.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_rand.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_conn.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_ctrl.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_hci.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_conn_hci.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_utils.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_scan.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_whitelist.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_adv.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_sched.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_supp_cmd.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_hci_ev.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_rfmgmt.c + + + + libs/mynewt-nimble/porting/nimble/src/os_cputime.c + libs/mynewt-nimble/porting/nimble/src/os_cputime_pwr2.c + libs/mynewt-nimble/porting/nimble/src/os_mbuf.c + libs/mynewt-nimble/porting/nimble/src/os_mempool.c + libs/mynewt-nimble/porting/nimble/src/hal_timer.c + libs/mynewt-nimble/porting/nimble/src/mem.c + libs/mynewt-nimble/porting/nimble/src/endian.c + libs/mynewt-nimble/porting/nimble/src/os_msys_init.c + + libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_hw.c + libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c + + libs/mynewt-nimble/nimble/host/services/gap/src/ble_svc_gap.c + libs/mynewt-nimble/nimble/host/services/gatt/src/ble_svc_gatt.c + + libs/mynewt-nimble/nimble/host/util/src/addr.c + ) + set(LVGL_SRC libs/lv_conf.h libs/lvgl/lvgl.h @@ -230,6 +319,8 @@ list(APPEND SOURCE_FILES FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c FreeRTOS/port_cmsis.c + ${TINYCRYPT_SRC} + ${NIMBLE_SRC} ${LVGL_SRC} ${IMAGE_FILES} @@ -288,6 +379,18 @@ set(INCLUDE_FILES include_directories( FreeRTOS/ libs/date/includes + libs/mynewt-nimble/porting/npl/freertos/include + libs/mynewt-nimble/nimble/include + libs/mynewt-nimble/porting/nimble/include + libs/mynewt-nimble/nimble/host/include + libs/mynewt-nimble/nimble/controller/include + libs/mynewt-nimble/nimble/transport/ram/include + libs/mynewt-nimble/nimble/drivers/nrf52/include + libs/mynewt-nimble/ext/tinycrypt/include + libs/mynewt-nimble/nimble/host/services/gap/include + libs/mynewt-nimble/nimble/host/services/gatt/include + libs/mynewt-nimble/nimble/host/util/include + libs/mynewt-nimble/nimble/host/store/ram/include ) link_directories( diff --git a/src/FreeRTOSConfig.h b/src/FreeRTOSConfig.h index 609c3f2b..557e5edf 100644 --- a/src/FreeRTOSConfig.h +++ b/src/FreeRTOSConfig.h @@ -63,7 +63,7 @@ #define configTICK_RATE_HZ 1024 #define configMAX_PRIORITIES ( 3 ) #define configMINIMAL_STACK_SIZE ( 120 ) -#define configTOTAL_HEAP_SIZE ( 1024*10 ) +#define configTOTAL_HEAP_SIZE ( 1024*20 ) #define configMAX_TASK_NAME_LEN ( 4 ) #define configUSE_16_BIT_TICKS 0 #define configIDLE_SHOULD_YIELD 1 diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index e65abb61..59f0a92f 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -4,9 +4,14 @@ #include #include #include -#include #include +#include +#include +#include #include "SystemTask.h" +#include +#include +#include #include "../main.h" using namespace Pinetime::System; @@ -32,14 +37,87 @@ void SystemTask::Process(void *instance) { app->Work(); } +static int _gap_event_cb(struct ble_gap_event *event, void *arg) +{ + return 0; +} + +static int +adv_event(struct ble_gap_event *event, void *arg) +{ + switch (event->type) { + case BLE_GAP_EVENT_ADV_COMPLETE: + return 0; + case BLE_GAP_EVENT_CONNECT: + return 0; + case BLE_GAP_EVENT_DISCONNECT: + return 0; + default: + return 0; + } +} + void SystemTask::Work() { - watchdog.Setup(7); - watchdog.Start(); +// watchdog.Setup(7); +// watchdog.Start(); NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason())); APP_GPIOTE_INIT(2); - bool erase_bonds=true; - ble_manager_init_peer_manager(); - nrf_sdh_freertos_init(ble_manager_start_advertising, &erase_bonds); +// bool erase_bonds=true; +// ble_manager_init_peer_manager(); +// nrf_sdh_freertos_init(ble_manager_start_advertising, &erase_bonds); +/* BLE */ + while (!ble_hs_synced()) {} + + int res; + res = ble_hs_util_ensure_addr(0); + assert(res == 0); + uint8_t addrType; + res = ble_hs_id_infer_auto(0, &addrType); + assert(res == 0); + + res = ble_svc_gap_device_name_set("Pinetime-JF"); + assert(res == 0); + + + /* set adv parameters */ + struct ble_gap_adv_params adv_params; + struct ble_hs_adv_fields fields; + /* advertising payload is split into advertising data and advertising + response, because all data cannot fit into single packet; name of device + is sent as response to scan request */ + struct ble_hs_adv_fields rsp_fields; + + /* fill all fields and parameters with zeros */ + memset(&adv_params, 0, sizeof(adv_params)); + memset(&fields, 0, sizeof(fields)); + memset(&rsp_fields, 0, sizeof(rsp_fields)); + + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + + fields.flags = BLE_HS_ADV_F_DISC_GEN | + BLE_HS_ADV_F_BREDR_UNSUP; +// fields.uuids128 = BLE_UUID128(BLE_UUID128_DECLARE( +// 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, +// 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff)); + fields.num_uuids128 = 0; + fields.uuids128_is_complete = 0;; + fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; + + rsp_fields.name = (uint8_t *)"Pinetime-JF"; + rsp_fields.name_len = strlen("Pinetime-JF"); + rsp_fields.name_is_complete = 1; + + res = ble_gap_adv_set_fields(&fields); + assert(res == 0); + + res = ble_gap_adv_rsp_set_fields(&rsp_fields); + + res = ble_gap_adv_start(addrType, NULL, 36000, + &adv_params, adv_event, NULL); + assert(res == 0); + +/* /BLE*/ spi.Init(); lcd.Init(); diff --git a/src/drivers/Watchdog.cpp b/src/drivers/Watchdog.cpp index 55b6de73..850fd2f1 100644 --- a/src/drivers/Watchdog.cpp +++ b/src/drivers/Watchdog.cpp @@ -33,16 +33,16 @@ void Watchdog::Kick() { Watchdog::ResetReasons Watchdog::ActualResetReason() const { uint32_t resetReason; - sd_power_reset_reason_get(&resetReason); - sd_power_reset_reason_clr(0xFFFFFFFF); - if(resetReason & 0x01u) return ResetReasons::ResetPin; - if((resetReason >> 1u) & 0x01u) return ResetReasons::Watchdog; - if((resetReason >> 2u) & 0x01u) return ResetReasons::SoftReset; - if((resetReason >> 3u) & 0x01u) return ResetReasons::CpuLockup; - if((resetReason >> 16u) & 0x01u) return ResetReasons::SystemOff; - if((resetReason >> 17u) & 0x01u) return ResetReasons::LpComp; - if((resetReason >> 18u) & 0x01u) return ResetReasons::DebugInterface; - if((resetReason >> 19u) & 0x01u) return ResetReasons::NFC; +// sd_power_reset_reason_get(&resetReason); +// sd_power_reset_reason_clr(0xFFFFFFFF); +// if(resetReason & 0x01u) return ResetReasons::ResetPin; +// if((resetReason >> 1u) & 0x01u) return ResetReasons::Watchdog; +// if((resetReason >> 2u) & 0x01u) return ResetReasons::SoftReset; +// if((resetReason >> 3u) & 0x01u) return ResetReasons::CpuLockup; +// if((resetReason >> 16u) & 0x01u) return ResetReasons::SystemOff; +// if((resetReason >> 17u) & 0x01u) return ResetReasons::LpComp; +// if((resetReason >> 18u) & 0x01u) return ResetReasons::DebugInterface; +// if((resetReason >> 19u) & 0x01u) return ResetReasons::NFC; return ResetReasons::HardReset; } diff --git a/src/main.cpp b/src/main.cpp index 106d19eb..f4c5c60a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +//#include #include #include "BLE/BleManager.h" #include "Components/Battery/BatteryController.h" @@ -17,6 +17,18 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #if NRF_LOG_ENABLED #include "Logging/NrfLogger.h" @@ -93,20 +105,20 @@ void OnNewNotification(const char* message, uint8_t size) { systemTask->PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); } -void OnNewTime(current_time_char_t* currentTime) { - auto dayOfWeek = currentTime->exact_time_256.day_date_time.day_of_week; - auto year = currentTime->exact_time_256.day_date_time.date_time.year; - auto month = currentTime->exact_time_256.day_date_time.date_time.month; - auto day = currentTime->exact_time_256.day_date_time.date_time.day; - auto hour = currentTime->exact_time_256.day_date_time.date_time.hours; - auto minute = currentTime->exact_time_256.day_date_time.date_time.minutes; - auto second = currentTime->exact_time_256.day_date_time.date_time.seconds; - - dateTimeController.SetTime(year, month, day, - dayOfWeek, hour, minute, second, nrf_rtc_counter_get(portNRF_RTC_REG)); - - systemTask->PushMessage(Pinetime::System::SystemTask::Messages::OnNewTime); -} +//void OnNewTime(current_time_char_t* currentTime) { +// auto dayOfWeek = currentTime->exact_time_256.day_date_time.day_of_week; +// auto year = currentTime->exact_time_256.day_date_time.date_time.year; +// auto month = currentTime->exact_time_256.day_date_time.date_time.month; +// auto day = currentTime->exact_time_256.day_date_time.date_time.day; +// auto hour = currentTime->exact_time_256.day_date_time.date_time.hours; +// auto minute = currentTime->exact_time_256.day_date_time.date_time.minutes; +// auto second = currentTime->exact_time_256.day_date_time.date_time.seconds; +// +// dateTimeController.SetTime(year, month, day, +// dayOfWeek, hour, minute, second, nrf_rtc_counter_get(portNRF_RTC_REG)); +// +// systemTask->PushMessage(Pinetime::System::SystemTask::Messages::OnNewTime); +//} void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) { if(((NRF_SPIM0->INTENSET & (1<<6)) != 0) && NRF_SPIM0->EVENTS_END == 1) { @@ -124,6 +136,100 @@ void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) { } } +static void (*radio_isr_addr)(void) ; +static void (*rng_isr_addr)(void) ; +static void (*rtc0_isr_addr)(void) ; + + +/* Some interrupt handlers required for NimBLE radio driver */ +extern "C" { +void RADIO_IRQHandler(void) { + ((void (*)(void)) radio_isr_addr)(); +} + +void RNG_IRQHandler(void) { + ((void (*)(void)) rng_isr_addr)(); +} + +void RTC0_IRQHandler(void) { + ((void (*)(void)) rtc0_isr_addr)(); +} + +void WDT_IRQHandler(void) { + nrf_wdt_event_clear(NRF_WDT_EVENT_TIMEOUT); +} + +void npl_freertos_hw_set_isr(int irqn, void (*addr)(void)) { + switch (irqn) { + case RADIO_IRQn: + radio_isr_addr = addr; + break; + case RNG_IRQn: + rng_isr_addr = addr; + break; + case RTC0_IRQn: + rtc0_isr_addr = addr; + break; + } +} + +uint32_t +npl_freertos_hw_enter_critical(void) { + uint32_t ctx = __get_PRIMASK(); + __disable_irq(); + return (ctx & 0x01); +} + +void npl_freertos_hw_exit_critical(uint32_t ctx) { + if (!ctx) { + __enable_irq(); + } +} + + +static struct ble_npl_eventq g_eventq_dflt; + +struct ble_npl_eventq * +nimble_port_get_dflt_eventq(void) { + return &g_eventq_dflt; +} + +void nimble_port_run(void) { + struct ble_npl_event *ev; + + while (1) { + ev = ble_npl_eventq_get(&g_eventq_dflt, BLE_NPL_TIME_FOREVER); + ble_npl_event_run(ev); + } +} + +void BleHost(void *) { + nimble_port_run(); +} + +void nimble_port_init(void) { + void os_msys_init(void); + void ble_store_ram_init(void); + ble_npl_eventq_init(&g_eventq_dflt); + os_msys_init(); + ble_hs_init(); + ble_store_ram_init(); + + hal_timer_init(5, NULL); + os_cputime_init(32768); + ble_ll_init(); + ble_hci_ram_init(); + nimble_port_freertos_init(BleHost); + + +} + +void nimble_port_ll_task_func(void *args) { +// extern void ble_ll_task(void *arg); + ble_ll_task(args); +} +} + int main(void) { logger.Init(); @@ -135,11 +241,15 @@ int main(void) { dateTimeController, notificationManager)); systemTask->Start(); - ble_manager_init(); - ble_manager_set_new_time_callback(OnNewTime); - ble_manager_set_ble_connection_callback(OnBleConnection); - ble_manager_set_ble_disconnection_callback(OnBleDisconnection); - ble_manager_set_new_notification_callback(OnNewNotification); + nimble_port_init(); + ble_svc_gap_init(); + ble_svc_gatt_init(); + +// ble_manager_init(); +// ble_manager_set_new_time_callback(OnNewTime); +// ble_manager_set_ble_connection_callback(OnBleConnection); +// ble_manager_set_ble_disconnection_callback(OnBleDisconnection); +// ble_manager_set_new_notification_callback(OnNewNotification); vTaskStartScheduler(); diff --git a/src/sdk_config.h b/src/sdk_config.h index a63eb6fb..244b21bd 100644 --- a/src/sdk_config.h +++ b/src/sdk_config.h @@ -8460,15 +8460,15 @@ // NRF_LOG_ENABLED - nrf_log - Logger //========================================================== #ifndef NRF_LOG_ENABLED -#define NRF_LOG_ENABLED 0 +#define NRF_LOG_ENABLED 1 #endif #ifndef NRF_LOG_BACKEND_RTT_ENABLED -#define NRF_LOG_BACKEND_RTT_ENABLED 0 +#define NRF_LOG_BACKEND_RTT_ENABLED 1 #endif #ifndef NRF_LOG_BACKEND_SERIAL_USES_RTT -#define NRF_LOG_BACKEND_SERIAL_USES_RTT 0 +#define NRF_LOG_BACKEND_SERIAL_USES_RTT 1 #endif // Log message pool - Configuration of log message pool