Data reception demo.

Dependencies:   modem_ref_helper CRC

Revision:
0:02418479dcf7
Child:
1:55636c88cd5e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu May 11 17:48:47 2017 +0000
@@ -0,0 +1,247 @@
+// @autor: jeremie@wizzilab.com
+// @date: 2017-05-02
+
+#include "mbed.h"
+#include "rtos.h"
+#include "WizziDebug.h"
+#include "WizziCom.h"
+
+#include "hwcfg.h"
+#include "files.h"
+#include "ram_fs.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 "alp.h"
+
+WizziCom* g_modem_com;
+Semaphore modem_ready(0);
+Queue<void, 8> g_file_modified;
+
+uint8_t g_main_id;
+
+void thread_file_modified()
+{
+    uint8_t fid;
+    osEvent evt;
+    
+    while (true)
+    {
+        evt = g_file_modified.get();
+        fid = (evt.status == osEventMessage)? (uint8_t)(uint32_t)evt.value.p : NULL;
+        
+        switch (fid)
+        {
+            case FID_STRING_FILE:
+                // Print string
+                uint8_t str[SIZE_STRING_FILE];
+                ram_fs_read(FID_STRING_FILE, 0, SIZE_STRING_FILE, (uint8_t*)&str);
+                PRINT("STRING: ");
+                PRINT((char*)str);
+                PRINT("\r\n");
+                break;
+            default:
+            break;
+        }
+    }
+}
+
+// Misc
+// ============================================================{{{
+
+void my_get_alp_file_props(uint8_t fid, alp_file_header_t* hdr)
+{
+    memcpy(hdr, ram_fs_get_header(fid), sizeof(alp_file_header_t));
+}
+
+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;
+    }
+}
+
+// ============================================================}}}
+
+// 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;
+    
+    // Retrieve Flow ID from header and send packet 
+    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,
+    .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
+    DBG_OPEN(DEBUG_LED);
+    PRINT("\r\n--- Starting new run ---\r\n");
+    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
+    
+    alp_file_header_t hdr;
+    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(1000);
+    
+    // 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);
+
+    // Open driver
+    modem_open(my_serial_send, &callbacks);
+    
+    g_main_id = modem_get_id(my_main_callback);
+
+    DPRINT("Start Modem Process (id=%d)\n", g_main_id);
+    Thread::wait(1000);
+    
+    modem_read_file(0, (uint8_t*)&uid.b[0], 0, 8, g_main_id);
+    modem_ready.wait();
+    
+    modem_read_file(2, (uint8_t*)&rev, 0, sizeof(revision_t), g_main_id);
+    modem_ready.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");
+    
+    //DPRINT("UID: %08X%08X\n", HAL_U32_BYTE_SWAP(uid.w[0]), HAL_U32_BYTE_SWAP(uid.w[1]));
+
+    DPRINT("Register Files\n");
+    // HOST Revision is a local file. Uses D7AActP Notification.
+    ram_fs_new(FID_HOST_REV, (uint8_t*)&h_rev, (uint8_t*)&f_rev);
+    my_get_alp_file_props(FID_HOST_REV, &hdr);
+    modem_declare_file(FID_HOST_REV, &hdr, g_main_id);
+    modem_ready.wait();
+    
+    // Allow remote access.
+    modem_delete_file(FID_STRING_FILE, g_main_id);
+    modem_ready.wait();
+    ram_fs_new(FID_STRING_FILE, (uint8_t*)&h_string_file, (uint8_t*)&f_string_file);
+    my_get_alp_file_props(FID_STRING_FILE, &hdr);
+    modem_declare_file(FID_STRING_FILE, &hdr, g_main_id);
+    modem_ready.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.wait();
+    
+    PRINT("Start D7A Stack\n");
+    modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP , true, g_main_id);
+    modem_ready.wait();
+    
+    PRINT("Notify Modem Version\n");
+    modem_notify_file(FID_MODEM_REV, 0, SIZE_HOST_REV, g_main_id);
+    modem_ready.wait();
+    
+    PRINT("Notify FW Version\n");
+    modem_notify_file(FID_HOST_REV, 0, SIZE_HOST_REV, g_main_id);
+    modem_ready.wait();
+    
+    // Start file modified thread
+    Thread th_file_modified(osPriorityNormal, 1024, NULL);
+    osStatus status = th_file_modified.start(thread_file_modified);
+    ASSERT(status == osOK, "Failed to start thread_file_modified (err: %d)\r\n", status);
+
+#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
+    }
+}
\ No newline at end of file