Scan example on D7-LoRa
Dependencies: modem_ref_helper DebouncedInterrupt
main.cpp
- Committer:
- Jeej
- Date:
- 2019-08-20
- Revision:
- 6:10124805571b
- Parent:
- 5:8c7ce9a28169
- Child:
- 7:7d9414d33a63
File content as of revision 6:10124805571b:
// @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 0 // Disable Duty Cycle #define DUTY 255 #else // Use default Duty Cycle #define DUTY D7A_CTF_ENCODE(31) #endif Semaphore button_user(0); Semaphore modem_ready(0); Queue<void, 8> modem_resp; enum { MODEM_RESP_NO, MODEM_RESP_TERMINAL, MODEM_RESP_DONE, }; d7a_xcl_t d7fsk_dl_xcl = { .bf.s = 0, .bf.m = 0x1 }; d7a_xcl_t d7fsk_ul_xcl = { .bf.s = 2, .bf.m = 0x1 }; d7a_xcl_t d7lora_dl_xcl = { .bf.s = 8, .bf.m = 0x1 }; d7a_xcl_t d7lora_ul_xcl = { .bf.s = 9, .bf.m = 0x1 }; 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 = ALP_RPOL_ONESHOT, .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_ENCODE(4), }; // Callback for broadcast read void my_read_response_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)id; if (ALP_ERR_NONE != err) { modem_print_error(ALP_ITF_TYPE_D7A, err); } if (terminal) { modem_resp.put((void*)MODEM_RESP_TERMINAL); } else { 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 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.acquire(); 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), 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 id User void my_main_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)id; if (ALP_ERR_NONE != err) { modem_print_error(ALP_ITF_TYPE_D7A, err); } if (terminal) { modem_ready.release(); } } /*** 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); uint8_t id = modem_get_id(my_main_callback); // 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), id); modem_ready.acquire(); // Configure interfaces to use uplink access class modem_write_file(IFID_ONESHOT_HOST, &XCL_UL, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), id); modem_ready.acquire(); modem_write_file(IFID_ONESHOT_ACTP, &XCL_UL, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), id); modem_ready.acquire(); modem_write_file(IFID_REPORT, &XCL_UL, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), id); modem_ready.acquire(); // 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), id); modem_ready.acquire(); } 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), id); modem_ready.acquire(); } PRINT("Start D7A Stack\n"); modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS | ALP_D7A_ISTAT_EOP, true, id); modem_ready.acquire(); modem_free_id(id); #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(), osPriorityLow); while(true) { ThisThread::sleep_for(500); #ifdef DEBUG_LED my_led = !my_led; #endif } }