Data reception demo.

Dependencies:   modem_ref_helper CRC

Revision:
4:854f22f7962b
Parent:
3:e9ea4d325c01
Child:
5:a44b6f60bfc7
--- a/main.cpp	Wed May 17 14:38:25 2017 +0000
+++ b/main.cpp	Thu May 18 13:46:00 2017 +0000
@@ -1,34 +1,14 @@
 // @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_ref_helper.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 "files.h"
 #include "CriusOLED.h"
 
-#define MODEM_VERSION_MAJOR         4
-#define MODEM_VERSION_MINOR         8
-
 #define RESTORE_TIME    30
 #define KEEP_ALIVE      3600
 
-WizziCom* g_modem_com;
 Semaphore modem_ready(0);
 Queue<void, 8> g_file_modified;
 
@@ -59,7 +39,7 @@
         {
             // Keep alive: Report status
             modem_write_file(FID_STATUS_FILE, &status, 0, SIZE_STATUS_FILE, g_main_id);
-            //modem_ready.wait();
+            modem_ready.wait();
         }
         
         Thread::wait(1000);
@@ -100,67 +80,6 @@
     }
 }
 
-// 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,
@@ -174,124 +93,47 @@
     .boot       = my_boot
 };
 
-// Callback for g_main_id User
-void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
+void print_status(int status)
+{
+    switch (status)
+    {
+        case ALP_ERR_NONE:
+            PRINT("Status: OK\n");
+            break;
+        default:
+            PRINT("Status: error %d\n", status);
+            break;
+    }
+}
+
+static 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);
-    }
-}
-
-void modem_update_file(uint8_t fid, alp_file_header_t* header, uint8_t* data)
-{
-    alp_file_header_t remote_header;
-    
-    memset(&remote_header, 0, sizeof(alp_file_header_t));
-    
-    // Read remote header
-    modem_read_fprop(fid, &remote_header, g_main_id);
-    modem_ready.wait();
-    
-    // Add file in local file system
-    ram_fs_new(fid, (uint8_t*)header, data);
-    
-    // Update file
-    if (memcmp(&remote_header, header, sizeof(alp_file_header_t)))
-    {
-        PRINT("Updating file %d\n", fid);
-        // Delete
-        modem_delete_file(fid, g_main_id);
-        modem_ready.wait();
-        // Restore in local file system
-        ram_fs_new(fid, (uint8_t*)header, data);
-        // Re-create
-        if (data)
-        {
-            modem_declare_file(fid, header, g_main_id);
-        }
-        else
-        {
-            modem_create_file(fid, header, g_main_id);
-        }
-        modem_ready.wait();
-    }
-    else
-    {
-        PRINT("File %d up to date\n", fid);
-    }
 }
 
 /*** Main function ------------------------------------------------------------- ***/
 int main()
 {
     // Start & initialize
+#ifdef DEBUG_LED
     DBG_OPEN(DEBUG_LED);
+#else
+    DBG_OPEN(NC);
+#endif
     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);
-
-    // Open driver
-    modem_open(my_serial_send, &callbacks);
+    modem_helper_open(&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(D7A_FID_UID, (uint8_t*)&uid.b[0], 0, 8, g_main_id);
-    modem_ready.wait();
-    
-    modem_read_file(D7A_FID_FIRMWARE_VERSION, (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");
         
-    // Check version
-    if (rev.fw_version.major != MODEM_VERSION_MAJOR || rev.fw_version.minor != MODEM_VERSION_MINOR)
-    {
-        PRINT("You need a modem at version %d.%d.x to use this APP.\n"
-              "Please check updates by importing:\n"
-              "https://developer.mbed.org/teams/WizziLab/code/D7A_WM_Updater\n"
-              , MODEM_VERSION_MAJOR, MODEM_VERSION_MINOR);
-        Thread::wait(osWaitForever);
-    }
-    //DPRINT("UID: %08X%08X\n", HAL_U32_BYTE_SWAP(uid.w[0]), HAL_U32_BYTE_SWAP(uid.w[1]));
-
-    DPRINT("Register Files\n");
+    PRINT("Register Files\n");
     // HOST Revision is a local file. Uses D7AActP Notification.
     modem_update_file(FID_HOST_REV, (alp_file_header_t*)&h_rev, (uint8_t*)&f_rev);
     
@@ -339,7 +181,7 @@
     
     display_logo();
 
-#if (DEBUG_LED != NC)
+#ifdef DEBUG_LED
     DigitalOut my_led(DEBUG_LED);
 #endif
     
@@ -348,7 +190,7 @@
     while(true)
     {
         Thread::wait(500);
-#if (DEBUG_LED != NC)
+#ifdef DEBUG_LED
         my_led = !my_led;
 #endif
     }