Scan example on D7-LoRa
Dependencies: modem_ref_helper DebouncedInterrupt
main.cpp
- Committer:
- Jeej
- Date:
- 2018-06-12
- Revision:
- 1:27716ee59ca4
- Parent:
- 0:6703784ff93e
- Child:
- 2:de388004dca6
File content as of revision 1:27716ee59ca4:
// @autor: jeremie@wizzilab.com // @date: 2017-05-02 #include "DebouncedInterrupt.h" #include "modem_ref_helper.h" #include "modem_callbacks.h" #if 1 // Use D7 LoRa #define XCL_DL d7lora_dl_xcl #define XCL_UL d7lora_ul_xcl #else // Use D7 FSK #define XCL_DL d7fsk_dl_xcl #define XCL_UL d7fsk_ul_xcl #endif #if 1 // Disable Duty Cycle #define DUTY 255 #else // Use default Duty Cycle #define DUTY D7A_CTF_ENCODE(31) #endif #define MY_POLICY_IDX 0 Semaphore button_user(0); Semaphore modem_ready(0); Queue<void, 8> modem_resp; enum { MODEM_RESP_NO, MODEM_RESP_TERMINAL, MODEM_RESP_DONE, }; // This discribes the retry policy of the stack for each packet // Do not modify uncommented parameters alp_retry_policy_t my_policy = { .meta.procedure = 0, .meta.respond = true, .meta.persistant = false, .meta.bulk = false, .depth = 1, .retries = 0, // The stack will retry x times (each packet will be sent a maximum of x+1 times) .slot_time = 0 // Interval between retries in seconds }; uint8_t g_main_id; d7a_xcl_t d7fsk_dl_xcl = { .bf.s = 0, .bf.m = 1 }; d7a_xcl_t d7fsk_ul_xcl = { .bf.s = 2, .bf.m = 1 }; d7a_xcl_t d7lora_dl_xcl = { .bf.s = 8, .bf.m = 1 }; d7a_xcl_t d7lora_ul_xcl = { .bf.s = 9, .bf.m = 1 }; alp_d7a_itf_t my_itf = { .type = ALP_ITF_TYPE_D7A, .cfg.to = 0, .cfg.te = 0, .cfg.qos.bf.resp = D7A_RESP_ALL, .cfg.qos.bf.retry = MY_POLICY_IDX, .cfg.addressee.ctrl.bf.nls = D7A_NLS_AES_CCM_64, .cfg.addressee.ctrl.bf.idf = D7A_ID_NBID, .cfg.addressee.xcl = XCL_UL, .cfg.addressee.id[0] = D7A_CTF_VAL(4,0), }; void print_status(int status) { switch (status) { case ALP_ERR_NONE: //PRINT("Status: OK\n"); break; case ALP_ERR_FILE_EXIST: PRINT("Status: Already registered\n"); break; default: PRINT("Status: error %d\n", status); break; } } void print_resp(int status) { switch (status) { case ALP_ERR_NONE: //PRINT("Resp: OK\n"); break; case ALP_ERR_FILE_EXIST: PRINT("Resp: Already registered\n"); break; default: PRINT("Resp: error %d\n", status); break; } } // Callback for broadcast read void my_read_response_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)id; if (terminal) { print_status(err); modem_resp.put((void*)MODEM_RESP_TERMINAL); } else { print_resp(err); if (ALP_ERR_NONE == err) { modem_resp.put((void*)MODEM_RESP_DONE); } } } // Interrupt Service Routine on button press. void button_push_isr( void ) { button_user.release(); } void button_user_thread() { FPRINT("(id:0x%08x)\r\n", osThreadGetId()); osEvent evt; uint32_t resp; d7a_sp_res_t istat; fw_version_t fw_ver; uint8_t nb = 0; uint8_t my_user_id = modem_get_id(my_read_response_callback); uint8_t scan_xcl[] = { XCL_DL.byte, XCL_UL.byte }; memset(&istat, 0, sizeof(d7a_sp_res_t)); memset(&fw_ver, 0, sizeof(fw_version_t)); while (true) { // Wait for button press PRINT("Press button to scan...\r\n"); button_user.wait(); for (uint8_t i = 0; i < sizeof(scan_xcl); i++) { nb = 0; my_itf.cfg.addressee.xcl.byte = scan_xcl[i]; PRINT("Scanning XCL 0x%02X...\n", my_itf.cfg.addressee.xcl.byte); modem_remote_read_file((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, D7A_FID_FIRMWARE_VERSION, (void*)&fw_ver, 12, sizeof(fw_version_t), my_user_id); do { evt = modem_resp.get(); resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_NO; if (MODEM_RESP_DONE == resp) { nb++; PRINT("%d: XCL:%02X ", nb, istat.addressee.xcl.byte); PRINT_DATA("UID:", "%02X", istat.addressee.id, 8, " "); PRINT("snr:%d rxlev:%d lb:%d ", istat.snr, istat.rxlev, istat.lb); PRINT("v%d.%d.%d\n", fw_ver.major, fw_ver.minor, fw_ver.patch); // Reset variables memset(&istat, 0, sizeof(d7a_sp_res_t)); memset(&fw_ver, 0, sizeof(fw_version_t)); } } while (MODEM_RESP_TERMINAL != resp); } PRINT("Done.\n"); } } modem_callbacks_t callbacks = { .read = my_read, .write = my_write, .read_fprop = my_read_fprop, .flush = my_flush, .remove = my_delete, .udata = my_udata, .lqual = my_lqual, .ldown = my_ldown, .reset = my_reset, .boot = my_boot }; // Callback for g_main_id User void my_main_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)id; if (terminal) { print_status(err); modem_ready.release(); } else { print_resp(err); } } /*** Main function ------------------------------------------------------------- ***/ int main() { // Start & initialize #ifdef DEBUG_LED DBG_OPEN(DEBUG_LED); #else DBG_OPEN(NC); #endif PRINT("\n" "-----------------------------------------\n" "-------------- Demo D7A LoRa ------------\n" "-----------------------------------------\n"); FPRINT("(id:0x%08x)\r\n", osThreadGetId()); modem_helper_open(&callbacks); g_main_id = modem_get_id(my_main_callback); // Set custom retry policy // XXX Won't work the first time as we need to reboot the modem for the changes to be applied // Check current retry policy alp_retry_policy_t old; modem_read_file(WM_FID_ALP_CFG, &old, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), g_main_id); modem_ready.wait(); if (memcmp(&old, &my_policy, sizeof(alp_retry_policy_t))) { // Update retry policy modem_write_file(WM_FID_ALP_CFG, &my_policy, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), g_main_id); modem_ready.wait(); modem_flush_file(WM_FID_ALP_CFG, g_main_id); modem_ready.wait(); // Reboot PRINT("Rebooting to apply new retry policy\n"); my_reset(); } // Put modem to listen to downlink access class modem_write_file(D7A_FID_DLL_CFG, &XCL_DL, offsetof(d7a_dll_cfg_t, xcl), sizeof(d7a_xcl_t), g_main_id); modem_ready.wait(); // Configure interfaces to use uplink access class modem_write_file(FID_D7A_ITF0, &XCL_UL, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), g_main_id); modem_ready.wait(); modem_write_file(FID_D7A_ITF1, &XCL_UL, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), g_main_id); modem_ready.wait(); modem_write_file(FID_D7A_ITF2, &XCL_UL, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), g_main_id); modem_ready.wait(); modem_write_file(FID_D7A_ITF3, &XCL_UL, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), g_main_id); modem_ready.wait(); // Configure duty d7a_ctf_t duty = { .byte = DUTY }; for (int i = 0; i < 8; i++) { modem_write_file(D7A_FID_ACCESS_PROFILE_0 + XCL_DL.bf.s, &duty, offsetof(d7a_access_profile_t, sb[0].duty) + (i * sizeof(d7a_subband_t)), sizeof(d7a_ctf_t), g_main_id); modem_ready.wait(); } for (int i = 0; i < 8; i++) { modem_write_file(D7A_FID_ACCESS_PROFILE_0 + XCL_UL.bf.s, &duty, offsetof(d7a_access_profile_t, sb[0].duty) + (i * sizeof(d7a_subband_t)), sizeof(d7a_ctf_t), g_main_id); modem_ready.wait(); } PRINT("Start D7A Stack\n"); modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS, true, g_main_id); modem_ready.wait(); #ifdef DEBUG_BUTTON DebouncedInterrupt user_interrupt(DEBUG_BUTTON); user_interrupt.attach(button_push_isr, IRQ_FALL, 500, true); Thread but_th(osPriorityNormal, 2048, NULL); osStatus status = but_th.start(button_user_thread); ASSERT(status == osOK, "Failed to start but thread (err: %d)\r\n", status); #else #error You need a button to use this APP as is #endif #ifdef DEBUG_LED DigitalOut my_led(DEBUG_LED); #endif // Set main task to lowest priority osThreadSetPriority(osThreadGetId(), osPriorityIdle); while(true) { Thread::wait(500); #ifdef DEBUG_LED my_led = !my_led; #endif } }