Scan example on D7-LoRa
Dependencies: modem_ref_helper DebouncedInterrupt
Revision 8:afc331e53b7d, committed 2021-02-19
- Comitter:
- Jeej
- Date:
- Fri Feb 19 14:25:06 2021 +0000
- Parent:
- 7:7d9414d33a63
- Commit message:
- New driver for 5.6.x
Changed in this revision
--- a/main.cpp Thu May 28 09:12:20 2020 +0000 +++ b/main.cpp Fri Feb 19 14:25:06 2021 +0000 @@ -2,7 +2,7 @@ // @date: 2017-05-02 #include "DebouncedInterrupt.h" -#include "modem_ref_helper.h" +#include "modem_d7a.h" #include "modem_callbacks.h" #if 1 @@ -24,14 +24,6 @@ #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 = { .m = 0x1, .s = 0 } }; d7a_xcl_t d7fsk_ul_xcl = { .bf = { .m = 0x1, .s = 2 } }; @@ -51,29 +43,6 @@ .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 ) { @@ -85,17 +54,16 @@ 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); + alp_payload_t* alp; + alp_payload_t* alp_rsp; + d7a_sp_res_t istat; + int err; 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)); + modem_d7a_enable_itf(); while (true) { @@ -110,34 +78,76 @@ 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); + alp = NULL; + alp = alp_payload_f_rd_data(alp, D7A_FID_FIRMWARE_VERSION, 12, sizeof(fw_version_t), false); - do + err = modem_remote_raw_alp((void*)&my_itf, alp, &alp_rsp, (uint32_t)60000); + + if (err < ALP_ERR_NONE) + { + PRINT("Timeout.\n"); + } + else { - evt = modem_resp.get(); - resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_NO; + err = alp_payload_get_err(alp_rsp); + PRINT("Err %d: ", err); + modem_print_error(my_itf.type, err); + } + + do { + nb++; - if (MODEM_RESP_DONE == resp) + alp = alp_payload_extract(&alp_rsp, ALP_OPCODE_RSP_ISTATUS); + + if (alp) { - nb++; + alp_parsed_chunk_t r; + u8* p = alp->d; + + alp_parse_chunk(&p, &r); + memcpy(&istat, r.data, r.meta.itf.length); + 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); + + alp_payload_free(alp); + } + else + { + break; + } + + alp = alp_payload_extract(&alp_rsp, ALP_OPCODE_RSP_F_DATA); + + if (alp) + { + alp_parsed_chunk_t r; + u8* p = alp->d; + + alp_parse_chunk(&p, &r); + memcpy(&fw_ver, r.data, r.meta.f_data.length); + 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)); + alp_payload_free(alp); + } + else + { + break; } - - } while (MODEM_RESP_TERMINAL != resp); + + FLUSH(); + } while (1); + + alp_payload_free(alp_rsp); } PRINT("Done.\n"); } } -modem_callbacks_t callbacks = { +modem_ref_callbacks_t callbacks = { .read = my_read, .write = my_write, .read_fprop = my_read_fprop, @@ -150,22 +160,6 @@ .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() { @@ -180,45 +174,27 @@ "------------- 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); + modem_open(&callbacks); // 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(D7A_FID_DLL_CFG, &XCL_DL, offsetof(d7a_dll_cfg_t, xcl), sizeof(d7a_xcl_t)); - 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 interface to use uplink access class + modem_write_file(IFID_REPORT, &XCL_UL, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t)); + +#if 0 // 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(); + 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)); } 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(); + 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)); } - - 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); +#endif #ifdef DEBUG_BUTTON DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
--- a/modem_callbacks.cpp Thu May 28 09:12:20 2020 +0000 +++ b/modem_callbacks.cpp Fri Feb 19 14:25:06 2021 +0000 @@ -1,87 +1,37 @@ -#include "modem_ref_helper.h" +#include "modem_d7a.h" // ============================================================}}} // Callbacks to MODEM's ALP requests // ============================================================{{{ -void my_read(u8 fid, u32 offset, u32 length, int id) +void my_read(u8 action, u8 fid, u32 offset, u32 length, int id) { - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); -} - -void my_write(u8 fid, void *data, u32 offset, u32 length, int id) -{ - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); } -void my_read_fprop(u8 fid, int id) -{ - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); -} - -void my_flush(u8 fid, int id) +void my_write(u8 action, u8 fid, void *data, u32 offset, u32 length, int id) { - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); -} - -void my_delete(u8 fid, int id) -{ - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); } -void my_udata(void *data, u32 length) +void my_read_fprop(u8 action, u8 fid, int id) +{ + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); +} + +void my_flush(u8 action, u8 fid, int id) +{ + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); +} + +void my_delete(u8 action, u8 fid, int id) +{ + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); +} + +void my_udata(alp_payload_t* alp) { - uint8_t* p = (uint8_t*)data; - int32_t rem = length; - alp_parsed_chunk_t r; - d7a_sp_res_t* istat; - - do { - uint32_t parsed = alp_parse_chunk(&p, &r); - if (!parsed) - { - // Discard the payload in case of parsing error. - PRINT("Parsing error!\r\n"); - break; - } - rem -= parsed; - - switch (r.type) - { - // Interface status - case ALP_OPCODE_RSP_ISTATUS: - // D7A Interface - if (ALP_ITF_TYPE_D7A == r.meta.itf.type) - { - union { - u8 b[8]; - u32 w[2]; - } uid; - - // ISTATUS can come either alone or together with ALP_OPCODE_RSP_F_DATA - // but there should be only one per payload, moreover it will come first - istat = (d7a_sp_res_t*)r.data; - memcpy(uid.b,istat->addressee.id,8); - - PRINT("Got accessed by UID:%08X%08X SNR: %3ddB RXLEV: -%-3ddBm LB: %3ddB\n", - HAL_U32_BYTE_SWAP(uid.w[0]), HAL_U32_BYTE_SWAP(uid.w[1]), - istat->snr, istat->rxlev, istat->lb); - } - else - { - PRINT("Got accessed by unknown Interface 0x%02X\n", r.meta.itf.type); - } - break; - // Data return - case ALP_OPCODE_RSP_F_DATA: - // RSP_F_DATA can come either alone or together with ISTATUS - PRINT("Got UNS File[%3d]@%d %d Bytes\n", r.meta.f_data.fid, r.meta.f_data.offset, r.meta.f_data.length); - break; - default: - PRINT("Untreated OPCODE %d\n", r.type); - break; - } - } while (rem > 0); + alp_payload_print(alp); } void my_lqual(u8 ifid, int per) @@ -107,4 +57,21 @@ // Modem re-booted, restart APP my_reset(); +} + +void my_busy(u8 busy) +{ + if (busy) + { + PRINT("Modem Busy\r\n"); + + /* Stop report, do not use modem */ + /* Wait for modem reboot or modem not busy */ + } + else + { + PRINT("Modem not Busy\r\n"); + + /* Resume reports */ + } } \ No newline at end of file
--- a/modem_callbacks.h Thu May 28 09:12:20 2020 +0000 +++ b/modem_callbacks.h Fri Feb 19 14:25:06 2021 +0000 @@ -1,11 +1,11 @@ #include "hal_types.h" -void my_read(u8 fid, u32 offset, u32 length, int id); -void my_write(u8 fid, void *data, u32 offset, u32 length, int id); -void my_read_fprop(u8 fid, int id); -void my_flush(u8 fid, int id); -void my_delete(u8 fid, int id); -void my_udata(void *data, u32 length); +void my_read(u8 action, u8 fid, u32 offset, u32 length, int id); +void my_write(u8 action, u8 fid, void *data, u32 offset, u32 length, int id); +void my_read_fprop(u8 action, u8 fid, int id); +void my_flush(u8 action, u8 fid, int id); +void my_delete(u8 action, u8 fid, int id); +void my_udata(alp_payload_t* alp); void my_lqual(u8 ifid, int per); void my_ldown(u8 ifid); void my_reset(void);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modem_d7a.lib Fri Feb 19 14:25:06 2021 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#15bf99fcfed9
--- a/modem_ref_helper.lib Thu May 28 09:12:20 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#d624707636f9