AWS IoT demonstration using the Avnet Shield (AT&T LTE) and the FRDM-K64F target board.

Dependencies:   K64F_FATFileSystem

Fork of mbed-os-example-tls-tls-client by mbed-os-examples

Committer:
ampembeng
Date:
Thu Dec 01 18:05:38 2016 +0000
Revision:
15:6f2798e45099
Initial commit.  Demo works with both the FRDM wired Ethernet and the Avnet Shield wireless modem.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ampembeng 15:6f2798e45099 1 /*
ampembeng 15:6f2798e45099 2 Copyright (c) 2016 Fred Kellerman
ampembeng 15:6f2798e45099 3
ampembeng 15:6f2798e45099 4 Permission is hereby granted, free of charge, to any person obtaining a copy
ampembeng 15:6f2798e45099 5 of this software and associated documentation files (the "Software"), to deal
ampembeng 15:6f2798e45099 6 in the Software without restriction, including without limitation the rights
ampembeng 15:6f2798e45099 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ampembeng 15:6f2798e45099 8 copies of the Software, and to permit persons to whom the Software is
ampembeng 15:6f2798e45099 9 furnished to do so, subject to the following conditions:
ampembeng 15:6f2798e45099 10
ampembeng 15:6f2798e45099 11 The above copyright notice and this permission notice shall be included in
ampembeng 15:6f2798e45099 12 all copies or substantial portions of the Software.
ampembeng 15:6f2798e45099 13
ampembeng 15:6f2798e45099 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ampembeng 15:6f2798e45099 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ampembeng 15:6f2798e45099 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ampembeng 15:6f2798e45099 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ampembeng 15:6f2798e45099 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ampembeng 15:6f2798e45099 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ampembeng 15:6f2798e45099 20 THE SOFTWARE.
ampembeng 15:6f2798e45099 21
ampembeng 15:6f2798e45099 22 @file WncController.cpp
ampembeng 15:6f2798e45099 23 @purpose Controls WNC Cellular Modem
ampembeng 15:6f2798e45099 24 @version 1.0
ampembeng 15:6f2798e45099 25 @date July 2016
ampembeng 15:6f2798e45099 26 @author Fred Kellerman
ampembeng 15:6f2798e45099 27 */
ampembeng 15:6f2798e45099 28
ampembeng 15:6f2798e45099 29 #include "WncControllerK64F.h"
ampembeng 15:6f2798e45099 30
ampembeng 15:6f2798e45099 31 using namespace WncControllerK64F_fk;
ampembeng 15:6f2798e45099 32
ampembeng 15:6f2798e45099 33 WncControllerK64F::WncControllerK64F(struct WncGpioPinListK64F * pPins, MODSERIAL * wnc_uart, MODSERIAL * debug_uart)
ampembeng 15:6f2798e45099 34 {
ampembeng 15:6f2798e45099 35 m_logTimer.start(); // Start the log timer now!
ampembeng 15:6f2798e45099 36 m_pDbgUart = debug_uart;
ampembeng 15:6f2798e45099 37 m_pWncUart = wnc_uart;
ampembeng 15:6f2798e45099 38 m_gpioPinList = *pPins;
ampembeng 15:6f2798e45099 39 }
ampembeng 15:6f2798e45099 40
ampembeng 15:6f2798e45099 41 bool WncControllerK64F::enterWncTerminalMode(MODSERIAL * pUart, bool echoOn)
ampembeng 15:6f2798e45099 42 {
ampembeng 15:6f2798e45099 43 if (pUart == NULL)
ampembeng 15:6f2798e45099 44 return (false); // Need a uart!
ampembeng 15:6f2798e45099 45
ampembeng 15:6f2798e45099 46 string * resp;
ampembeng 15:6f2798e45099 47 AtCmdErr_e r = sendWncCmd("AT", &resp, 500);
ampembeng 15:6f2798e45099 48 if (r == WNC_AT_CMD_TIMEOUT)
ampembeng 15:6f2798e45099 49 return (false);
ampembeng 15:6f2798e45099 50
ampembeng 15:6f2798e45099 51 pUart->puts("\r\nEntering WNC Terminal Mode - press <CTRL>-Q to exit!\r\n");
ampembeng 15:6f2798e45099 52
ampembeng 15:6f2798e45099 53 while (1) {
ampembeng 15:6f2798e45099 54 if (pUart->readable()) {
ampembeng 15:6f2798e45099 55 char c = pUart->getc();
ampembeng 15:6f2798e45099 56 if (c == '\x11') {
ampembeng 15:6f2798e45099 57 pUart->puts("\r\nExiting WNC Terminal Mode!\r\n");
ampembeng 15:6f2798e45099 58 // Cleanup in case user doesn't finish command:
ampembeng 15:6f2798e45099 59 sendWncCmd("AT", &resp, 300);
ampembeng 15:6f2798e45099 60 // Above AT may fail but should get WNC back in sync
ampembeng 15:6f2798e45099 61 return (sendWncCmd("AT", &resp, 500) == WNC_AT_CMD_OK);
ampembeng 15:6f2798e45099 62 }
ampembeng 15:6f2798e45099 63 if (echoOn == true) {
ampembeng 15:6f2798e45099 64 pUart->putc(c);
ampembeng 15:6f2798e45099 65 }
ampembeng 15:6f2798e45099 66 m_pWncUart->putc(c);
ampembeng 15:6f2798e45099 67 }
ampembeng 15:6f2798e45099 68 if (m_pWncUart->readable())
ampembeng 15:6f2798e45099 69 pUart->putc(m_pWncUart->getc());
ampembeng 15:6f2798e45099 70 }
ampembeng 15:6f2798e45099 71 }
ampembeng 15:6f2798e45099 72
ampembeng 15:6f2798e45099 73
ampembeng 15:6f2798e45099 74 int WncControllerK64F::putc(char c)
ampembeng 15:6f2798e45099 75 {
ampembeng 15:6f2798e45099 76 return (m_pWncUart->putc(c));
ampembeng 15:6f2798e45099 77 }
ampembeng 15:6f2798e45099 78
ampembeng 15:6f2798e45099 79 int WncControllerK64F::puts(const char * s)
ampembeng 15:6f2798e45099 80 {
ampembeng 15:6f2798e45099 81 return (m_pWncUart->puts(s));
ampembeng 15:6f2798e45099 82 }
ampembeng 15:6f2798e45099 83
ampembeng 15:6f2798e45099 84 char WncControllerK64F::getc(void)
ampembeng 15:6f2798e45099 85 {
ampembeng 15:6f2798e45099 86 return (m_pWncUart->getc());
ampembeng 15:6f2798e45099 87 }
ampembeng 15:6f2798e45099 88
ampembeng 15:6f2798e45099 89 int WncControllerK64F::charReady(void)
ampembeng 15:6f2798e45099 90 {
ampembeng 15:6f2798e45099 91 return (m_pWncUart->readable());
ampembeng 15:6f2798e45099 92 }
ampembeng 15:6f2798e45099 93
ampembeng 15:6f2798e45099 94 int WncControllerK64F::dbgWriteChar(char b)
ampembeng 15:6f2798e45099 95 {
ampembeng 15:6f2798e45099 96 if (m_pDbgUart != NULL)
ampembeng 15:6f2798e45099 97 return (m_pDbgUart->putc(b));
ampembeng 15:6f2798e45099 98 else
ampembeng 15:6f2798e45099 99 return (0);
ampembeng 15:6f2798e45099 100 }
ampembeng 15:6f2798e45099 101
ampembeng 15:6f2798e45099 102 int WncControllerK64F::dbgWriteChars(const char * b)
ampembeng 15:6f2798e45099 103 {
ampembeng 15:6f2798e45099 104 if (m_pDbgUart != NULL)
ampembeng 15:6f2798e45099 105 return (m_pDbgUart->puts(b));
ampembeng 15:6f2798e45099 106 else
ampembeng 15:6f2798e45099 107 return (0);
ampembeng 15:6f2798e45099 108 }
ampembeng 15:6f2798e45099 109
ampembeng 15:6f2798e45099 110 bool WncControllerK64F::initWncModem(uint8_t powerUpTimeoutSecs)
ampembeng 15:6f2798e45099 111 {
ampembeng 15:6f2798e45099 112 // Hard reset the modem (doesn't go through
ampembeng 15:6f2798e45099 113 // the signal level translator)
ampembeng 15:6f2798e45099 114 *m_gpioPinList.mdm_reset = 0;
ampembeng 15:6f2798e45099 115
ampembeng 15:6f2798e45099 116 // disable signal level translator (necessary
ampembeng 15:6f2798e45099 117 // for the modem to boot properly). All signals
ampembeng 15:6f2798e45099 118 // except mdm_reset go through the level translator
ampembeng 15:6f2798e45099 119 // and have internal pull-up/down in the module. While
ampembeng 15:6f2798e45099 120 // the level translator is disabled, these pins will
ampembeng 15:6f2798e45099 121 // be in the correct state.
ampembeng 15:6f2798e45099 122 *m_gpioPinList.shield_3v3_1v8_sig_trans_ena = 0;
ampembeng 15:6f2798e45099 123
ampembeng 15:6f2798e45099 124 // While the level translator is disabled and ouptut pins
ampembeng 15:6f2798e45099 125 // are tristated, make sure the inputs are in the same state
ampembeng 15:6f2798e45099 126 // as the WNC Module pins so that when the level translator is
ampembeng 15:6f2798e45099 127 // enabled, there are no differences.
ampembeng 15:6f2798e45099 128 *m_gpioPinList.mdm_uart2_rx_boot_mode_sel = 1; // UART2_RX should be high
ampembeng 15:6f2798e45099 129 *m_gpioPinList.mdm_power_on = 0; // powr_on should be low
ampembeng 15:6f2798e45099 130 *m_gpioPinList.mdm_wakeup_in = 1; // wake-up should be high
ampembeng 15:6f2798e45099 131 *m_gpioPinList.mdm_uart1_cts = 0; // indicate that it is ok to send
ampembeng 15:6f2798e45099 132
ampembeng 15:6f2798e45099 133 // Now, wait for the WNC Module to perform its initial boot correctly
ampembeng 15:6f2798e45099 134 waitMs(1000);
ampembeng 15:6f2798e45099 135
ampembeng 15:6f2798e45099 136 // The WNC module initializes comms at 115200 8N1 so set it up
ampembeng 15:6f2798e45099 137 m_pWncUart->baud(115200);
ampembeng 15:6f2798e45099 138
ampembeng 15:6f2798e45099 139 //Now, enable the level translator, the input pins should now be the
ampembeng 15:6f2798e45099 140 //same as how the M14A module is driving them with internal pull ups/downs.
ampembeng 15:6f2798e45099 141 //When enabled, there will be no changes in these 4 pins...
ampembeng 15:6f2798e45099 142 *m_gpioPinList.shield_3v3_1v8_sig_trans_ena = 1;
ampembeng 15:6f2798e45099 143
ampembeng 15:6f2798e45099 144 bool res = waitForPowerOnModemToRespond(powerUpTimeoutSecs);
ampembeng 15:6f2798e45099 145
ampembeng 15:6f2798e45099 146 // Toggle wakeup to prevent future dropped 'A' of "AT", this was
ampembeng 15:6f2798e45099 147 // suggested by ATT.
ampembeng 15:6f2798e45099 148 if (res == true) {
ampembeng 15:6f2798e45099 149 m_pDbgUart->puts("\r\nToggling Wakeup...\r\n");
ampembeng 15:6f2798e45099 150 waitMs(20);
ampembeng 15:6f2798e45099 151 *m_gpioPinList.mdm_wakeup_in = 0;
ampembeng 15:6f2798e45099 152 waitMs(2000);
ampembeng 15:6f2798e45099 153 *m_gpioPinList.mdm_wakeup_in = 1;
ampembeng 15:6f2798e45099 154 waitMs(20);
ampembeng 15:6f2798e45099 155 m_pDbgUart->puts("Toggling complete.\r\n");
ampembeng 15:6f2798e45099 156 }
ampembeng 15:6f2798e45099 157
ampembeng 15:6f2798e45099 158 return (res);
ampembeng 15:6f2798e45099 159 }
ampembeng 15:6f2798e45099 160
ampembeng 15:6f2798e45099 161 void WncControllerK64F::waitMs(int t)
ampembeng 15:6f2798e45099 162 {
ampembeng 15:6f2798e45099 163 wait_ms(t);
ampembeng 15:6f2798e45099 164 }
ampembeng 15:6f2798e45099 165
ampembeng 15:6f2798e45099 166 void WncControllerK64F::waitUs(int t)
ampembeng 15:6f2798e45099 167 {
ampembeng 15:6f2798e45099 168 wait_ms(t);
ampembeng 15:6f2798e45099 169 }
ampembeng 15:6f2798e45099 170
ampembeng 15:6f2798e45099 171 int WncControllerK64F::getLogTimerTicks(void)
ampembeng 15:6f2798e45099 172 {
ampembeng 15:6f2798e45099 173 return (m_logTimer.read_us());
ampembeng 15:6f2798e45099 174 }
ampembeng 15:6f2798e45099 175
ampembeng 15:6f2798e45099 176 void WncControllerK64F::startTimerA(void)
ampembeng 15:6f2798e45099 177 {
ampembeng 15:6f2798e45099 178 m_timerA.start();
ampembeng 15:6f2798e45099 179 m_timerA.reset();
ampembeng 15:6f2798e45099 180 }
ampembeng 15:6f2798e45099 181
ampembeng 15:6f2798e45099 182 void WncControllerK64F::stopTimerA(void)
ampembeng 15:6f2798e45099 183 {
ampembeng 15:6f2798e45099 184 m_timerA.stop();
ampembeng 15:6f2798e45099 185 }
ampembeng 15:6f2798e45099 186
ampembeng 15:6f2798e45099 187 int WncControllerK64F::getTimerTicksA_mS(void)
ampembeng 15:6f2798e45099 188 {
ampembeng 15:6f2798e45099 189 return (m_timerA.read_ms());
ampembeng 15:6f2798e45099 190 }
ampembeng 15:6f2798e45099 191
ampembeng 15:6f2798e45099 192 void WncControllerK64F::startTimerB(void)
ampembeng 15:6f2798e45099 193 {
ampembeng 15:6f2798e45099 194 m_timerB.start();
ampembeng 15:6f2798e45099 195 m_timerB.reset();
ampembeng 15:6f2798e45099 196 }
ampembeng 15:6f2798e45099 197
ampembeng 15:6f2798e45099 198 void WncControllerK64F::stopTimerB(void)
ampembeng 15:6f2798e45099 199 {
ampembeng 15:6f2798e45099 200 m_timerB.stop();
ampembeng 15:6f2798e45099 201 }
ampembeng 15:6f2798e45099 202
ampembeng 15:6f2798e45099 203 int WncControllerK64F::getTimerTicksB_mS(void)
ampembeng 15:6f2798e45099 204 {
ampembeng 15:6f2798e45099 205 return (m_timerB.read_ms());
ampembeng 15:6f2798e45099 206 }
ampembeng 15:6f2798e45099 207