Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of d7a_1x by
Diff: src/d7a_modem.cpp
- Revision:
- 34:1311cc53201a
- Parent:
- 33:f9a542d3efaa
- Child:
- 36:67cfe82526e6
--- a/src/d7a_modem.cpp Fri Jun 24 10:11:19 2016 +0000 +++ b/src/d7a_modem.cpp Mon Aug 22 13:47:39 2016 +0000 @@ -9,14 +9,11 @@ typedef struct { Thread* thread; - Semaphore* ressource; Queue<void, 8> ready; Queue<void, 2> boot; Queue<d7a_com_rx_msg_t, 16> pkt_queue; int8_t status; DigitalInOut* reset; - int16_t waiting_count; - uint8_t ready_count; bool booted; } d7a_modem_ctx_t; @@ -28,10 +25,7 @@ { FPRINT("\r\n"); - g_modem_ctx.ressource = new Semaphore(1); g_modem_ctx.thread = new Thread(d7a_modem_thread, NULL, osPriorityHigh, DEFAULT_STACK_SIZE); - g_modem_ctx.waiting_count = 0; - g_modem_ctx.ready_count = 0; g_modem_ctx.booted = false; if (reset_pin == NC) @@ -40,52 +34,81 @@ } else { - g_modem_ctx.reset = new DigitalInOut(reset_pin, PIN_OUTPUT, OpenDrain, 0); + g_modem_ctx.reset = new DigitalInOut(reset_pin, PIN_OUTPUT, OpenDrain, 1); } + d7a_com_start(); d7a_modem_reset(); + ASSERT(d7a_sys_ping_modem(30000), "Modem does not respond\r\n"); +} + +static bool d7a_modem_wait_boot( uint32_t millisec ) +{ + FPRINT("(%d)\r\n", millisec); + osEvent evt = g_modem_ctx.boot.get(millisec); + return (evt.status == osEventMessage)? true: false; +} + + +static bool d7a_modem_wait_ready( uint32_t millisec ) +{ + FPRINT("(%d)\r\n", millisec); + osEvent evt = g_modem_ctx.ready.get(millisec); + return (evt.status == osEventMessage)? true: false; +} + +static void d7a_modem_soft_reset(void) +{ + FPRINT("\r\n"); + IPRINT("MODEM Soft Reset.\r\n"); - // Wait for modem ready - osEvent evt = g_modem_ctx.boot.get(5000); - ASSERT(evt.status == osEventMessage, "MODEM BOOT Timeout\r\n"); - IPRINT("MODEM Boot OK.\r\n"); - ASSERT(d7a_modem_wait_ready(2000), "MODEM READY Timeout\r\n"); - IPRINT("MODEM Ready.\r\n"); - g_modem_ctx.booted = true; + // Try software reset + d7a_sys_software_reset(); + + // Clean buffer and queues + d7a_com_restart(); +} + +static void d7a_modem_hard_reset(void) +{ + FPRINT("\r\n"); + + ASSERT(g_modem_ctx.reset != NULL, "No reset PIN specified\r\n"); + + IPRINT("MODEM Hard Reset.\r\n"); + + // Use hardware reset + // Assert reset pin + *(g_modem_ctx.reset) = 0; + + // Clean buffer and queues + d7a_com_restart(); + Thread::wait(100); + + // Release reset pin + *(g_modem_ctx.reset) = 1; + } void d7a_modem_reset( void ) { - if (g_modem_ctx.reset == NULL) - { - // Try software reset - d7a_sys_software_reset(); - - // Clean reception buffer - d7a_com_flush_rx(); - } - else + FPRINT("\r\n"); + bool reset_ok = false; + g_modem_ctx.booted = false; + + d7a_modem_soft_reset(); + reset_ok = d7a_modem_wait_boot(5000); + + if (!reset_ok) { - // Use hardware reset - // Assert reset pin - *(g_modem_ctx.reset) = 0; - Thread::wait(10); - - // Clean reception buffer - d7a_com_flush_rx(); - - // Release reset pin - *(g_modem_ctx.reset) = 1; - Thread::wait(10); + d7a_modem_hard_reset(); + reset_ok = d7a_modem_wait_boot(5000); } -} - -bool d7a_modem_wait_ready( uint32_t millisec ) -{ - g_modem_ctx.waiting_count++; - FPRINT("(%d)\r\n", g_modem_ctx.waiting_count); - osEvent evt = g_modem_ctx.ready.get(millisec); - return (evt.status == osEventMessage)? true: false; + ASSERT(reset_ok, "MODEM BOOT Timeout\r\n"); + g_modem_ctx.booted = true; + + ASSERT(d7a_modem_wait_ready(5000), "MODEM READY Timeout\r\n"); + IPRINT("MODEM Ready.\r\n"); } void d7a_modem_new_pkt(d7a_com_rx_msg_t* pkt) @@ -111,7 +134,7 @@ msg.abuf = buf; msg.plen = 1; msg.alen = len; - d7a_com_send_msg(&msg); + d7a_com_post_msg(&msg); } bool d7a_modem_register_file(uint8_t fid, uint8_t retry_policy) @@ -128,8 +151,8 @@ // Check If modem already has it DPRINT("Register: %d\r\n", rparams.bf.fid); - d7a_fs_distant_stat(rparams.bf.fid, NULL); - g_modem_ctx.status = (int32_t)d7a_fs_wait_done(TO_FS); + + g_modem_ctx.status = d7a_fs_distant_stat(rparams.bf.fid, NULL); if (g_modem_ctx.status < 0) { d7a_fs_properties_t props; @@ -137,8 +160,7 @@ d7a_fs_get_properties(rparams.bf.fid, KAL_FS_PROP_ALL, &props); // create a remote Host file matching our local one props.type = HOST; - d7a_fs_distant_create(rparams.bf.fid, &props); - g_modem_ctx.status = (int32_t)d7a_fs_wait_done(TO_FS); + g_modem_ctx.status = d7a_fs_distant_create(rparams.bf.fid, &props); } if (g_modem_ctx.status == 0) @@ -152,6 +174,8 @@ ret = false; } + //Thread::wait(100); // Let time to the modem to write to the flash + DPRINT("Register done.\r\n"); //g_modem_ctx.ressource->release(); @@ -173,6 +197,7 @@ d7a_modem_msg(WM_CMD_NOTIFY_FILE,(uint8_t*)&nfp,sizeof(notify_file_param_t)); WARNING(d7a_modem_wait_ready(TO_FS), "Notification Timeout on FID:%d\r\n", fid); + //ASSERT(d7a_modem_wait_ready(TO_FS), "Notification Timeout on FID:%d\r\n", fid); //Thread::wait(100); // XXX: To avoid lost packets @@ -187,11 +212,8 @@ //g_modem_ctx.ressource->wait(); - d7a_fs_read(0, uid, 0, 8); - d7a_fs_wait_done(TO_FS); - - d7a_fs_read(2, &rev, 0, sizeof(revision_t)); - d7a_fs_wait_done(TO_FS); + D7A_FS_READ(0, uid, 0, 8); + D7A_FS_READ(2, &rev, 0, sizeof(revision_t)); PRINT("------------ D7A Modem infos ------------\r\n"); PRINT("| UID: %02X%02X%02X%02X%02X%02X%02X%02X |\r\n", uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uid[7]); @@ -222,14 +244,13 @@ uint8_t cmd = pkt->buffer[0]; if (cmd == WM_CMD_READY) { - g_modem_ctx.waiting_count--; - DPRINT("Modem ready (%d)\r\n", g_modem_ctx.waiting_count); + DPRINT("Modem ready\r\n"); g_modem_ctx.ready.put(NULL); } else if (cmd == WM_CMD_BOOT) { ASSERT(g_modem_ctx.booted == false, "Modem rebooted.\r\n"); - DPRINT("Modem booted\r\n"); + DPRINT("Modem booted CAUSE:%d NB_BOOT:%d\r\n", pkt->buffer[1], ((uint16_t)pkt->buffer[4])<<8 & pkt->buffer[3]); g_modem_ctx.boot.put(NULL); } else