Skip to content

Commit

Permalink
Add on-power-on modem hook (before a reset was needed)
Browse files Browse the repository at this point in the history
  • Loading branch information
loryruta committed Jun 9, 2019
1 parent 6bf52e9 commit ef3a4f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
5 changes: 1 addition & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,9 @@ int main() {
uart_setup(UART_A0, UART_BAUD_RATE_9600_SMCLK_12MHZ);
uart_hub_select(1);

PMCU_log("Syncing modem...");
PMCU_log("Syncing & resetting modem...");
__pmcu_assert("modem", modem_sync());

PMCU_log("Resetting modem...");
__pmcu_assert("modem", modem_reset());

PMCU_log("Attaching GPRS service to modem...");
__pmcu_assert("modem", modem_gprs_attach());

Expand Down
36 changes: 28 additions & 8 deletions modem.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ void modem_execute(const char *cmd) {
}

PMCU_Error modem_sync() {
size_t i;
uint8_t tmp;

uart_write(UART_A0, 27); // 27 = ESC key, if was sending something exits
/* Try to exit AT+CIPSEND state if was in */
uart_write(UART_A0, 27);

for (i = MODEM_SYNC_RETRIALS; i > 0; i--) {
/* Wait until the modem is awake */
while (1) {
modem_execute("AT");
if (uart_read_until_string(UART_A0, "OK\r\n", NULL, NULL, 3) != PMCU_OK) { // delay of 3 seconds
if (uart_read_until_string(UART_A0, "OK\r\n", NULL, NULL, 3) != PMCU_OK) {
continue;
}
break;
}

return PMCU_OK;
}

PMCU_Error modem_reset() {
/* Reset modem to default state */
modem_execute("ATZ");
uart_read_until_string(UART_A0, "OK\r\n", NULL, NULL, MODEM_COMMAND_TIMEOUT);

Expand All @@ -103,6 +103,26 @@ PMCU_Error modem_reset() {
return pmcu_error;
}

/* Wait until find an available network */
while (1) {
__delay_cycles(12288000 * 3);
modem_execute("AT+CREG?");
if ((pmcu_error = modem_read(modem_buffer)) != PMCU_OK) {
continue;
}
tmp = strcmp(modem_buffer, "+CREG: 0,1") != 0 && strcmp(modem_buffer, "+CREG: 0,5") != 0;
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
continue;
}
if (tmp) {
continue;
}
break;
}

/* Wait a few seconds to settle */
__delay_cycles(12288000 * 3);

return PMCU_OK;
}

Expand Down

0 comments on commit ef3a4f5

Please sign in to comment.