Ping pong app demo.

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
0:fa3fd69f8148
Child:
1:4629ccf8315d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 17 13:58:04 2017 +0000
@@ -0,0 +1,340 @@
+// @autor: jeremie@wizzilab.com
+// @date: 2017-05-02
+
+#include "mbed.h"
+#include "rtos.h"
+#include "DebouncedInterrupt.h"
+#include "WizziDebug.h"
+#include "WizziCom.h"
+
+#include "hwcfg.h"
+#include "modem_callbacks.h"
+
+#include "revision.h"
+#include "alp_spec.h"
+#include "alp_helpers.h"
+#include "modem_ref.h"
+#include "kal_fs.h"
+#include "d7a_1x.h"
+#include "d7a_1x_fs.h"
+#include "alp.h"
+#include "hal_types.h"
+
+
+WizziCom* g_modem_com;
+Semaphore button_user(0);
+Semaphore modem_ready[MAX_USER_NB];
+
+enum {
+    MODEM_RESP_NO,
+    MODEM_RESP_TERMINAL,
+    MODEM_RESP_DONE,
+};
+
+uint8_t g_main_id;
+uint8_t g_void_id;
+
+typedef struct {
+    Thread* thread;
+    uint32_t count;
+    d7a_addressee_t addressee;
+} ping_t;
+
+TYPEDEF_STRUCT_PACKED {
+    uint8_t type;
+    d7a_sp_cfg_t cfg;
+} alp_d7a_itf_t;
+
+#define WM_FID_ALP_CFG          50
+#define MY_POLICY_IDX           0
+#define FID_PING_PONG           128
+#define PING_DELAY              500
+#define PING_COUNTER_SIZE       sizeof(uint32_t)
+#define MAX_PING_NB             MAX_USER_NB-2
+
+Queue<ping_t, MAX_PING_NB> ping_queue;
+Queue<Thread, MAX_PING_NB> thread_queue;
+
+
+d7a_xcl_t ping_pong_xcl = { .bf.s = 13, .bf.m = 1 };
+
+alp_retry_policy_t my_policy = {
+    .meta.procedure     = 0,
+    .meta.respond       = true,
+    .meta.persistant    = false,
+    .meta.bulk          = false,
+    .depth              = 1,
+    .retries            = 0,
+    .slot_time          = 0
+};
+
+#define D7A_CTF_VAL(mant,exp)   ((uint8_t)(mant|(exp<<5)))
+#define ALP_ITF_TYPE_D7A        0xD7
+alp_d7a_itf_t my_itf = {
+    .type                           = ALP_ITF_TYPE_D7A,
+    .cfg.to                         = 0,
+    .cfg.te                         = 0,
+    .cfg.qos.bf.resp                = D7A_RESP_NO,
+    .cfg.qos.bf.retry               = MY_POLICY_IDX,
+    .cfg.qos.bf.record              = 0,
+    .cfg.qos.bf.stop_on_err         = 0,
+    .cfg.addressee.ctrl.bf.nls      = D7A_NLS_AES_CCM_64,
+    .cfg.addressee.ctrl.bf.idf      = D7A_ID_NOID,
+    .cfg.addressee.xcl              = ping_pong_xcl
+};
+
+
+#define MY_D7_ITF_SIZE(_itf) (1+my_alp_itf_d7a_cfg_size(&(_itf)->cfg))
+int my_alp_itf_d7a_cfg_size(d7a_sp_cfg_t* cfg)
+{
+    int size = sizeof(d7a_sp_cfg_t) - sizeof(d7a_addressee_t);
+    size += D7A_ADDR_LEN(cfg->addressee.ctrl);
+    return size;
+}
+
+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 User
+void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
+{
+    (void)id;
+    
+    if (terminal)
+    {    
+        print_status(err);
+        modem_ready[id].release();
+    }
+    else
+    {
+        print_resp(err);
+    }
+}
+
+void my_void_callback(uint8_t terminal, int8_t err, uint8_t id)
+{
+    (void)terminal;
+    (void)id;
+    print_status(err);
+}
+
+
+// Interrupt Service Routine on button press.
+void button_push_isr( void )
+{
+    button_user.release();
+}
+
+#if (DEBUG_LED != NC)
+DigitalOut my_led(DEBUG_LED);
+#endif
+
+void my_udata(u8 fid,void *data,u32 offset,u32 length, u8 i_type, u8 i_length, u8* i_data)
+{
+    (void)data;
+    (void)i_length;
+
+    if (i_type == ALP_ITF_TYPE_D7A && fid == FID_PING_PONG && offset == 0 && length == PING_COUNTER_SIZE)
+    {
+        d7a_sp_res_t* istat = (d7a_sp_res_t*)i_data;
+        uint32_t* count = (uint32_t*)data;
+        
+        PRINT("Got PING %d", *count);
+        PRINT_DATA(" from ", "%02X", istat->addressee.id, 8, "");
+        PRINT(" (rxlev:%d lb:%d)\n", istat->rxlev, istat->lb);
+        
+        #if (DEBUG_LED != NC)
+        my_led = 1;
+        #endif
+    
+        Thread::wait(PING_DELAY);
+        
+        #if (DEBUG_LED != NC)
+            my_led = 0;
+        #endif
+        
+        alp_d7a_itf_t resp_itf = {
+            .type                           = ALP_ITF_TYPE_D7A,
+            .cfg.to                         = 0,
+            .cfg.te                         = 0,
+            .cfg.qos.bf.resp                = D7A_RESP_NO,
+            .cfg.qos.bf.retry               = MY_POLICY_IDX,
+            .cfg.qos.bf.record              = 0,
+            .cfg.qos.bf.stop_on_err         = 0,
+            .cfg.addressee                  = istat->addressee,
+        };
+        
+        (*count)++;
+        
+        // Send ping
+        PRINT("Send PING %d", *count);
+        PRINT_DATA(" to ", "%02X", resp_itf.cfg.addressee.id, 8, "\n");
+        
+        modem_send_file_content((uint8_t*)&resp_itf, MY_D7_ITF_SIZE(&resp_itf), istat, FID_PING_PONG, count, 0, PING_COUNTER_SIZE, g_void_id);
+    }
+}
+
+void button_user_thread()
+{
+    d7a_sp_res_t istat;
+    uint32_t ping = 0;
+        
+    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
+        
+    while (true)
+    {
+        // Wait for button press
+        button_user.wait();
+
+        // Initiate ping
+        PRINT("Initiate PING\n");
+        modem_send_file_content((uint8_t*)&my_itf, MY_D7_ITF_SIZE(&my_itf), (void*)&istat, FID_PING_PONG, &ping, 0, PING_COUNTER_SIZE, g_main_id);
+        modem_ready[g_main_id].wait();
+    }
+}
+
+
+// Serial adapters to WizziLab's own architecture
+// ============================================================{{{
+
+void my_serial_input(WizziCom* com, WizziComPacket_t* pkt)
+{
+    modem_input(wizzicom_type_to_flow(pkt->type), pkt->data, pkt->length);
+    FREE(pkt);
+}
+
+int my_serial_send(uint8_t* data1, uint8_t size1, uint8_t* data2, uint8_t size2)
+{
+    (void)size1;
+    
+    g_modem_com->send((WizziComPacketType)wizzicom_flow_to_type(data1[4]), size2, data2);
+
+    return (size1 + size2);
+}
+
+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
+};
+
+/*** Main function ------------------------------------------------------------- ***/
+int main()
+{
+    // Start & initialize
+    DBG_OPEN(DEBUG_LED);
+    PRINT("\r\n--- Starting new run ---\r\n");
+    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
+    
+    static union {
+        uint8_t      b[8];
+        uint32_t     w[2];
+    } uid;
+    revision_t rev;
+            
+    // Hardware reset
+    DigitalOut reset_low(MODEM_PIN_RESET, 0);
+    Thread::wait(100);
+        
+    // Release reset
+    DigitalIn reset_release(MODEM_PIN_RESET);
+    Thread::wait(2000);
+    
+    // Open modem Com port
+    g_modem_com = new WizziCom(MODEM_PIN_TX, MODEM_PIN_RX, MODEM_PIN_IRQ_OUT, MODEM_PIN_IRQ_IN);
+    
+    // Redirect All Port traffic to my_serial_input
+    g_modem_com->attach(my_serial_input, WizziComPacketOther);
+
+    modem_open(my_serial_send, &callbacks);
+    
+    g_main_id = modem_get_id(my_main_callback);
+    g_void_id = modem_get_id(my_void_callback);
+
+    PRINT("Start Modem Process (id=%d)\n", g_main_id);
+    Thread::wait(1000);
+    
+    modem_read_file(D7A_FID_UID, uid.b, 0, 8, g_main_id);
+    modem_ready[g_main_id].wait();
+    
+    modem_read_file(D7A_FID_FIRMWARE_VERSION, &rev, 0, sizeof(revision_t), g_main_id);
+    modem_ready[g_main_id].wait();
+    
+    PRINT("------------ D7A Modem infos ------------\r\n");
+    PRINT_DATA(" - UID:              ", "%02X", uid.b, 8, "\r\n");
+    PRINT(" - Manufacturer ID:  %08X\r\n", rev.manufacturer_id);
+    PRINT(" - Device ID:        %08X\r\n", rev.device_id);
+    PRINT(" - Hardware version: %08X\r\n", rev.hw_version);
+    PRINT(" - Firmware version: v%d.%d.%d\r\n", rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
+    PRINT(" - File system CRC:  0x%08x\r\n", rev.fs_crc);
+    PRINT("-----------------------------------------\r\n");
+    
+    // Put modem to listen to this access class
+    modem_write_file(D7A_FID_DLL_CFG, &ping_pong_xcl, 0, sizeof(d7a_xcl_t), g_main_id);
+    modem_ready[g_main_id].wait();
+    
+    // Set custom 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[g_main_id].wait();
+    
+    // Configure URC: LQUAL on report file notification every 10 reports
+    PRINT("Setup URCs\n");
+    modem_enable_urc(ALP_URC_TYPE_LQUAL, IFID_REPORT, 10, true, g_main_id);
+    modem_ready[g_main_id].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[g_main_id].wait();
+    
+#ifdef DEBUG_BUTTON
+    DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
+    user_interrupt.attach(button_push_isr, IRQ_FALL, 500, true);
+    
+    Thread but_th(osPriorityNormal, 1024, NULL);
+    osStatus status = but_th.start(button_user_thread);
+    ASSERT(status == osOK, "Failed to start but thread (err: %d)\r\n", status);
+#endif
+    
+    // Set main task to lowest priority
+    osThreadSetPriority(osThreadGetId(), osPriorityIdle);
+    while(true)
+    {
+        Thread::wait(500);
+    }
+}
\ No newline at end of file