diff --git a/epicardium/main.c b/epicardium/main.c index 75bb9c79537a82de067d2618d1f3071348d2b032..e20c1ba093475092e3793811a5f26fe9528c5fa4 100644 --- a/epicardium/main.c +++ b/epicardium/main.c @@ -21,6 +21,9 @@ #include "task.h" TaskHandle_t dispatcher_task_id; +TaskHandle_t ble_task_id; + +void vBleTask(void*pvParameters); /* * API dispatcher task. This task will sleep until an API call is issued and @@ -96,6 +99,18 @@ int main(void) abort(); } + if (xTaskCreate( + vBleTask, + (const char*)"BLE", + configMINIMAL_STACK_SIZE, + NULL, + tskIDLE_PRIORITY + 3, + &ble_task_id + ) != pdPASS) { + printf("Failed to create BLE task!\n"); + abort(); + } + LOG_INFO("startup", "Initializing dispatcher ..."); api_dispatcher_init(); diff --git a/epicardium/meson.build b/epicardium/meson.build index a6ba27ce781fce91b639529351cc8b07620dbeb5..e2a33829f72e9ffc30f05a048f1fd54c17340899 100644 --- a/epicardium/meson.build +++ b/epicardium/meson.build @@ -72,7 +72,7 @@ elf = executable( 'main.c', 'support.c', module_sources, - dependencies: [libcard10, max32665_startup_core0, maxusb, libff13], + dependencies: [libcard10, max32665_startup_core0, maxusb, libff13, btle], link_with: [api_dispatcher_lib, freertos], link_whole: [max32665_startup_core0_lib, board_card10_lib], include_directories: [freertos_includes], diff --git a/epicardium/modules/ble.c b/epicardium/modules/ble.c new file mode 100644 index 0000000000000000000000000000000000000000..230b51a97781118b54db32d78f9a3236b5a8d52b --- /dev/null +++ b/epicardium/modules/ble.c @@ -0,0 +1,134 @@ +#include "wsf_types.h" +#include "wsf_os.h" +#include "wsf_buf.h" +#include "wsf_timer.h" +#include "wsf_trace.h" +#include "app_ui.h" +#include "ll_api.h" +#include "sch_api.h" +#include "fit/fit_api.h" +#include "mxc_config.h" +#include "gcr_regs.h" +#include "mcr_regs.h" +#include "hci_core.h" + +#include <stdio.h> +#include <string.h> + +/* Number of WSF buffer pools */ +#define WSF_BUF_POOLS 6 + +/*! Free memory for pool buffers (use word elements for word alignment). */ +static uint32_t mainBufMem[3584/sizeof(uint32_t)+96]; + +/*! Default pool descriptor. */ +static wsfBufPoolDesc_t mainPoolDesc[WSF_BUF_POOLS] = +{ + { 16, 8 }, + { 32, 4 }, + { 64, 4 }, + { 128, 4 }, + { 256, 4 }, + { 384, 4 } +}; + + +static void PlatformInit(void) +{ + /* Change the pullup on the RST pin to 25K */ + /* TODO: Is this really needed? */ + MXC_MCR->ctrl = 0x202; + + /* Set VREGO_D to 1.3V */ + *((volatile uint32_t*)0x40004410) = 0x50; + + /* Set TX LDO to 1.1V and enable LDO. Set RX LDO to 0.9V and enable LDO */ + MXC_GCR->btleldocn = 0xD9; // TX 1.1V RX 0.9V + + /* Power up the 32MHz XO */ + MXC_GCR->clkcn |= MXC_F_GCR_CLKCN_X32M_EN; + + /* Enable peripheral clocks */ + /* TODO: Is this really needed? */ + MXC_GCR->perckcn0 &= ~(MXC_F_GCR_PERCKCN0_GPIO0D | MXC_F_GCR_PERCKCN0_GPIO1D); // Clear GPIO0 and GPIO1 Disable + MXC_GCR->perckcn1 &= ~(MXC_F_GCR_PERCKCN1_BTLED | MXC_F_GCR_PERCKCN1_TRNGD ); // Clear BTLE and ICACHE0 disable +} + +static void myTrace(const char *pStr, va_list args) +{ + extern uint8_t wsfCsNesting; + + if (wsfCsNesting == 0) + { + vprintf(pStr, args); + printf("\r\n"); + } +} + +static void WsfInit(void) +{ + WsfTimerInit(); + WsfBufInit(sizeof(mainBufMem), (uint8_t*)mainBufMem, WSF_BUF_POOLS, mainPoolDesc); + WsfTraceRegister(myTrace); +} + +void MacInit(void) +{ + wsfHandlerId_t handlerId; + + /* Initialize link layer. */ + BbInit(); + handlerId = WsfOsSetNextHandler(SchHandler); + SchInit(handlerId); + LlAdvSlaveInit(); + LlConnSlaveInit(); + handlerId = WsfOsSetNextHandler(LlHandler); + LlHandlerInit(handlerId); +} + + +/* TODO: WTF? */ +/* + * In two-chip solutions, setting the address must wait until the HCI interface is initialized. + * This handler can also catch other Application events, but none are currently implemented. + * See ble-profiles/sources/apps/app/common/app_ui.c for further details. + * + */ +void SetAddress(uint8_t event) +{ + uint8_t bdAddr[6] = {0x02, 0x02, 0x44, 0x8B, 0x05, 0x00}; + + switch (event) { + case APP_UI_RESET_CMPL: + printf("Setting address -- MAC %02X:%02X:%02X:%02X:%02X:%02X\n", bdAddr[5], bdAddr[4], bdAddr[3], bdAddr[2], bdAddr[1], bdAddr[0]); + LlSetBdAddr((uint8_t*)&bdAddr); + LlGetBdAddr(hciCoreCb.bdAddr); + break; + default: + break; + } +} + + + +static void ble_init(void) +{ + PlatformInit(); + WsfInit(); + MacInit(); + //StackInitFit(); + //FitStart(); + + /* Register a handler for Application events */ + AppUiActionRegister(SetAddress); +} + + +void vBleTask(void*pvParameters) +{ + ble_init(); + while (1){ + // TODO: this need some timing and sleeping + wsfOsDispatcher(); + } +} diff --git a/epicardium/modules/meson.build b/epicardium/modules/meson.build index 68c1b9d909170d69157fe844fe2100f119b00423..47a52e0e2aa45490807c911c8fa681266f95052c 100644 --- a/epicardium/modules/meson.build +++ b/epicardium/modules/meson.build @@ -8,4 +8,5 @@ module_sources = files( 'stream.c', 'vibra.c', 'light_sensor.c', + 'ble.c' ) diff --git a/lib/sdk/NDALibraries/BTLE/meson.build b/lib/sdk/NDALibraries/BTLE/meson.build new file mode 100644 index 0000000000000000000000000000000000000000..0a4125e61468d505b558d04018996ffcde0e0a13 --- /dev/null +++ b/lib/sdk/NDALibraries/BTLE/meson.build @@ -0,0 +1,233 @@ +includes = include_directories( + 'wsf/common/include', + 'wsf/common/include/util', + 'projects/ble-apps/ota-boot', + 'ble-profiles/sources/apps/dats', + 'ble-profiles/sources/apps/uribeacon', + 'ble-profiles/sources/apps', + #'ble-profiles/sources/apps/tag', + #'ble-profiles/sources/apps/medc', + #'ble-profiles/sources/apps/meds', + #'ble-profiles/sources/apps/hidapp', + #'ble-profiles/sources/apps/watch', + #'ble-profiles/sources/apps/datc', + #'ble-profiles/sources/apps/gluc', + #'ble-profiles/sources/apps/fit', + #'ble-profiles/sources/apps/cycling', + #'ble-profiles/sources/apps/sensor', + 'ble-profiles/sources/apps/app', + 'ble-profiles/sources/profiles', + 'ble-profiles/sources/profiles/include', + #'ble-profiles/sources/profiles/htps', + #'ble-profiles/sources/profiles/hid', + #'ble-profiles/sources/profiles/wspc', + #'ble-profiles/sources/profiles/hrpc', + #'ble-profiles/sources/profiles/uribeacon', + #'ble-profiles/sources/profiles/gap', + #'ble-profiles/sources/profiles/glps', + #'ble-profiles/sources/profiles/fmpl', + #'ble-profiles/sources/profiles/anpc', + #'ble-profiles/sources/profiles/wsps', + #'ble-profiles/sources/profiles/wdxc', + #'ble-profiles/sources/profiles/htpc', + #'ble-profiles/sources/profiles/hrps', + #'ble-profiles/sources/profiles/plxpc', + #'ble-profiles/sources/profiles/blpc', + #'ble-profiles/sources/profiles/cscp', + #'ble-profiles/sources/profiles/cpp', + #'ble-profiles/sources/profiles/glpc', + #'ble-profiles/sources/profiles/gatt', + #'ble-profiles/sources/profiles/sensor', + #'ble-profiles/sources/profiles/rscp', + #'ble-profiles/sources/profiles/blps', + #'ble-profiles/sources/profiles/plxps', + #'ble-profiles/sources/profiles/dis', + #'ble-profiles/sources/profiles/tipc', + #'ble-profiles/sources/profiles/bas', + #'ble-profiles/sources/profiles/udsc', + #'ble-profiles/sources/profiles/scpps', + #'ble-profiles/sources/profiles/include', + #'ble-profiles/sources/profiles/wpc', + #'ble-profiles/sources/profiles/paspc', + #'ble-profiles/sources/profiles/wdxs', + 'ble-profiles/sources/services', + 'ble-profiles/include/app', + 'platform/common/include', + 'controller/sources/mac/cfg', + 'controller/sources/mac/include', + 'ble-host/sources/stack/hci', + 'ble-host/sources/stack/cfg', + 'ble-host/sources/hci/exactle', + 'ble-host/include' +) + +sources = files( + 'wsf/baremetal/sources/wsf_msg.c', + 'wsf/baremetal/sources/wsf_efs.c', + 'wsf/baremetal/sources/wsf_queue.c', + 'wsf/baremetal/sources/wsf_timer.c', + 'wsf/baremetal/sources/wsf_cs.c', + 'wsf/baremetal/sources/wsf_buf.c', + 'wsf/baremetal/sources/wsf_os.c', + 'wsf/baremetal/sources/wsf_trace.c', + 'wsf/baremetal/sources/wsf_assert.c', + 'wsf/common/sources/util/crc32.c', + 'wsf/common/sources/util/bstream.c', + 'wsf/common/sources/util/terminal.c', + 'wsf/common/sources/util/calc128.c', + 'wsf/common/sources/util/bda.c', + 'wsf/common/sources/util/wstr.c', + 'wsf/common/sources/util/print.c', + #'projects/ble-apps/dats/stack_dats.c', + #'projects/ble-apps/dats/main.c', + #'projects/ble-apps/uribeacon/stack_uribeacon.c', + #'projects/ble-apps/uribeacon/main.c', + #'projects/ble-apps/tag/stack_tag.c', + #'projects/ble-apps/tag/main.c', + #'projects/ble-apps/medc/stack_medc.c', + #'projects/ble-apps/medc/main.c', + #'projects/ble-apps/meds/stack_meds.c', + #'projects/ble-apps/meds/main.c', + #'projects/ble-apps/hidapp/stack_hidapp.c', + #'projects/ble-apps/hidapp/main.c', + #'projects/ble-apps/watch/stack_watch.c', + #'projects/ble-apps/watch/main.c', + #'projects/ble-apps/datc/stack_datc.c', + #'projects/ble-apps/datc/main.c', + #'projects/ble-apps/fit/stack_fit.c', + #'projects/ble-apps/fit/main.c', + #'projects/ble-apps/cycling/stack_cycling.c', + #'projects/ble-apps/cycling/main.c', + #'projects/ble-apps/ota-boot/main.c', + 'ble-profiles/sources/apps/dats/dats_main.c', + 'ble-profiles/sources/apps/uribeacon/uribeacon_main.c', + 'ble-profiles/sources/apps/tag/tag_main.c', + 'ble-profiles/sources/apps/tag/tag_main_wdxs.c', + 'ble-profiles/sources/apps/medc/medc_htp.c', + 'ble-profiles/sources/apps/medc/medc_main.c', + 'ble-profiles/sources/apps/medc/medc_wsp.c', + 'ble-profiles/sources/apps/medc/medc_glp.c', + 'ble-profiles/sources/apps/medc/medc_hrp.c', + 'ble-profiles/sources/apps/medc/medc_plx.c', + 'ble-profiles/sources/apps/medc/medc_blp.c', + 'ble-profiles/sources/apps/meds/meds_wsp.c', + 'ble-profiles/sources/apps/meds/meds_main.c', + 'ble-profiles/sources/apps/meds/meds_blp.c', + 'ble-profiles/sources/apps/meds/meds_htp.c', + 'ble-profiles/sources/apps/meds/meds_plx.c', + 'ble-profiles/sources/apps/meds/meds_glp.c', + 'ble-profiles/sources/apps/hidapp/hidapp_main.c', + 'ble-profiles/sources/apps/watch/watch_main.c', + # TODO: This file includes a file which is only in mbed os: + # mbed-os/features/FEATURE_BLE/targets/TARGET_CORDIO/stack/ble-host/sources/stack/dm/dm_priv.h + #'ble-profiles/sources/apps/datc/datc_main.c', + 'ble-profiles/sources/apps/gluc/gluc_main.c', + 'ble-profiles/sources/apps/fit/fit_main.c', + 'ble-profiles/sources/apps/cycling/cycling_main.c', + 'ble-profiles/sources/apps/sensor/sensor_main.c', + 'ble-profiles/sources/apps/app/app_disc.c', + 'ble-profiles/sources/apps/app/app_slave.c', + 'ble-profiles/sources/apps/app/app_master.c', + 'ble-profiles/sources/apps/app/app_master_ae.c', + 'ble-profiles/sources/apps/app/app_slave_leg.c', + 'ble-profiles/sources/apps/app/app_slave_ae.c', + 'ble-profiles/sources/apps/app/app_main.c', + 'ble-profiles/sources/apps/app/app_server.c', + 'ble-profiles/sources/apps/app/app_master_leg.c', + 'ble-profiles/sources/apps/app/common/app_db.c', + 'ble-profiles/sources/apps/app/common/app_ui.c', + 'ble-profiles/sources/apps/app/common/app_hw.c', + 'ble-profiles/sources/apps/app/app_terminal.c', + 'ble-profiles/sources/apps/wdxs/wdxs_oad.c', + 'ble-profiles/sources/profiles/htps/htps_main.c', + 'ble-profiles/sources/profiles/hid/hid_main.c', + 'ble-profiles/sources/profiles/wspc/wspc_main.c', + 'ble-profiles/sources/profiles/hrpc/hrpc_main.c', + 'ble-profiles/sources/profiles/uribeacon/uricfg_main.c', + 'ble-profiles/sources/profiles/gap/gap_main.c', + 'ble-profiles/sources/profiles/glps/glps_db.c', + 'ble-profiles/sources/profiles/glps/glps_main.c', + 'ble-profiles/sources/profiles/fmpl/fmpl_main.c', + 'ble-profiles/sources/profiles/anpc/anpc_main.c', + 'ble-profiles/sources/profiles/wsps/wsps_main.c', + 'ble-profiles/sources/profiles/wdxc/wdxc_ft.c', + 'ble-profiles/sources/profiles/wdxc/wdxc_main.c', + 'ble-profiles/sources/profiles/wdxc/wdxc_stream.c', + 'ble-profiles/sources/profiles/htpc/htpc_main.c', + 'ble-profiles/sources/profiles/hrps/hrps_main.c', + 'ble-profiles/sources/profiles/plxpc/plxpc_main.c', + 'ble-profiles/sources/profiles/blpc/blpc_main.c', + 'ble-profiles/sources/profiles/cscp/cscps_main.c', + 'ble-profiles/sources/profiles/cpp/cpps_main.c', + 'ble-profiles/sources/profiles/glpc/glpc_main.c', + 'ble-profiles/sources/profiles/gatt/gatt_main.c', + 'ble-profiles/sources/profiles/sensor/gyro_main.c', + 'ble-profiles/sources/profiles/sensor/temp_main.c', + 'ble-profiles/sources/profiles/rscp/rscps_main.c', + 'ble-profiles/sources/profiles/blps/blps_main.c', + 'ble-profiles/sources/profiles/plxps/plxps_db.c', + 'ble-profiles/sources/profiles/plxps/plxps_main.c', + 'ble-profiles/sources/profiles/dis/dis_main.c', + 'ble-profiles/sources/profiles/tipc/tipc_main.c', + 'ble-profiles/sources/profiles/bas/bas_main.c', + 'ble-profiles/sources/profiles/udsc/udsc_main.c', + 'ble-profiles/sources/profiles/scpps/scpps_main.c', + 'ble-profiles/sources/profiles/wpc/wpc_main.c', + 'ble-profiles/sources/profiles/paspc/paspc_main.c', + 'ble-profiles/sources/profiles/wdxs/wdxs_stream.c', + 'ble-profiles/sources/profiles/wdxs/wdxs_au.c', + 'ble-profiles/sources/profiles/wdxs/wdxs_phy.c', + 'ble-profiles/sources/profiles/wdxs/wdxs_ft.c', + 'ble-profiles/sources/profiles/wdxs/wdxs_dc.c', + 'ble-profiles/sources/profiles/wdxs/wdxs_main.c', + 'ble-profiles/sources/services/svc_gls.c', + 'ble-profiles/sources/services/svc_cps.c', + 'ble-profiles/sources/services/svc_time.c', + 'ble-profiles/sources/services/svc_wp.c', + 'ble-profiles/sources/services/svc_alert.c', + 'ble-profiles/sources/services/svc_rscs.c', + 'ble-profiles/sources/services/svc_hts.c', + 'ble-profiles/sources/services/svc_temp.c', + 'ble-profiles/sources/services/svc_core.c', + 'ble-profiles/sources/services/svc_hid.c', + 'ble-profiles/sources/services/svc_scpss.c', + 'ble-profiles/sources/services/svc_hrs.c', + 'ble-profiles/sources/services/svc_plxs.c', + 'ble-profiles/sources/services/svc_cscs.c', + 'ble-profiles/sources/services/svc_ipss.c', + 'ble-profiles/sources/services/svc_wdxs.c', + 'ble-profiles/sources/services/svc_uricfg.c', + 'ble-profiles/sources/services/svc_dis.c', + 'ble-profiles/sources/services/svc_px.c', + 'ble-profiles/sources/services/svc_batt.c', + 'ble-profiles/sources/services/svc_wss.c', + 'ble-profiles/sources/services/svc_gyro.c', + 'ble-profiles/sources/services/svc_bps.c', + 'platform/max32665/sources/ll_math.c', + 'ble-host/sources/stack/hci/hci_main.c', + 'ble-host/sources/hci/exactle/hci_vs.c', + 'ble-host/sources/hci/exactle/hci_cmd_enc.c', + 'ble-host/sources/hci/exactle/hci_evt.c', + 'ble-host/sources/hci/exactle/hci_cmd_ae.c', + 'ble-host/sources/hci/exactle/hci_cmd.c', + 'ble-host/sources/hci/exactle/hci_tr.c', + 'ble-host/sources/hci/exactle/hci_cmd_phy.c', + 'ble-host/sources/hci/exactle/hci_cmd_master_ae.c', + 'ble-host/sources/hci/exactle/hci_core_ps.c', + 'ble-host/sources/hci/exactle/hci_cmd_master.c', + 'ble-host/sources/hci/exactle/hci_vs_ae.c', +) + +lib = static_library( + 'btle', + sources, + include_directories: includes, + dependencies: periphdriver, + c_args: '-w', +) + +btle = declare_dependency( + include_directories: includes, + link_with: lib, + dependencies: periphdriver, +) diff --git a/lib/sdk/meson.build b/lib/sdk/meson.build index e71031e577d7942f506dd1deae8124dec3dcb15c..6643962755664d296214fdaa255a76193d25c461 100644 --- a/lib/sdk/meson.build +++ b/lib/sdk/meson.build @@ -6,3 +6,5 @@ subdir('./Libraries/Boards/card10/') subdir('./Libraries/FreeRTOS/') subdir('./Libraries/MAXUSB/') + +subdir('./NDALibraries/BTLE/')