Working Thread (the wireless kind) example with DHCP Ethernet enabled. At this point, router is not a correct description. I do not know if Thread will handle routing automatically or I will need to do something to enable internet connectivity to a Thread client device. That is the goal, give a Thread client a routable connection to the internet.

Dependencies:   EthernetInterface fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Committer:
jmulvain
Date:
Thu Nov 03 14:07:59 2016 +0000
Revision:
29:241a264ebe8f
Parent:
27:1eb29717bfd9
Added ethernet library and acquire address.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:01fb291427ce 1 #include "mbed.h"
cotigac 18:b02fc0e53df8 2 #include "rtos.h"
sam_grove 2:3e7685cfb2a7 3
FSL\B36402 23:6f13fea3cace 4 #include "Phy.h"
cotigac 19:71b793021c78 5 #include "SMAC_Interface.h"
cotigac 19:71b793021c78 6 #include "SMAC_Config.h"
andreikovacs 27:1eb29717bfd9 7 #include "MemManager.h"
andreikovacs 27:1eb29717bfd9 8 #include "circular_buffer.h"
cotigac 19:71b793021c78 9
jmulvain 29:241a264ebe8f 10 //includes for project
jmulvain 29:241a264ebe8f 11 #include "EthernetInterface.h"
jmulvain 29:241a264ebe8f 12 //end new includes
jmulvain 29:241a264ebe8f 13 char * const cu8FreescaleLogo[]= {
jmulvain 29:241a264ebe8f 14 "\f\r\n",
jmulvain 29:241a264ebe8f 15 "\n\r\n\r\n\r #\n",
jmulvain 29:241a264ebe8f 16 "\r ###\n",
jmulvain 29:241a264ebe8f 17 "\r ### *\n",
jmulvain 29:241a264ebe8f 18 "\r # ***\n",
jmulvain 29:241a264ebe8f 19 "\r *** #\n",
jmulvain 29:241a264ebe8f 20 "\r * ###\n",
jmulvain 29:241a264ebe8f 21 "\r ###\n",
jmulvain 29:241a264ebe8f 22 "\r * #\n",
jmulvain 29:241a264ebe8f 23 "\r ***\n",
jmulvain 29:241a264ebe8f 24 "\r *** #\n",
jmulvain 29:241a264ebe8f 25 "\r # * ###\n",
jmulvain 29:241a264ebe8f 26 "\r ### ###\n",
jmulvain 29:241a264ebe8f 27 "\r ### * # F R E E S C A L E\n",
jmulvain 29:241a264ebe8f 28 "\r # ***\n",
jmulvain 29:241a264ebe8f 29 "\r *** S E M I C O N D U C T O R\n",
jmulvain 29:241a264ebe8f 30 "\r # *\n",
jmulvain 29:241a264ebe8f 31 "\r ### 2 0 1 5\n",
jmulvain 29:241a264ebe8f 32 "\r ###\n",
jmulvain 29:241a264ebe8f 33 "\r # Wireless Uart Demo\r\n\n",
jmulvain 29:241a264ebe8f 34 NULL
andreikovacs 27:1eb29717bfd9 35 };
andreikovacs 26:56ca40dcfae1 36
FSL\B36402 23:6f13fea3cace 37 #define gMcps_Cnf_EVENT_c (1<<1)
FSL\B36402 23:6f13fea3cace 38 #define gMcps_Ind_EVENT_c (1<<2)
FSL\B36402 23:6f13fea3cace 39 #define gMlme_EdCnf_EVENT_c (1<<3)
FSL\B36402 23:6f13fea3cace 40 #define gMlme_CcaCnf_EVENT_c (1<<4)
FSL\B36402 23:6f13fea3cace 41 #define gMlme_TimeoutInd_EVENT_c (1<<5)
FSL\B36402 23:6f13fea3cace 42 #define gWUSelf_EVENT_c (1<<6)
FSL\B36402 23:6f13fea3cace 43
andreikovacs 27:1eb29717bfd9 44 #define gDefaultBaudRate_UART_c 115200UL
andreikovacs 27:1eb29717bfd9 45
andreikovacs 27:1eb29717bfd9 46 Serial uart(USBTX, USBRX);
andreikovacs 27:1eb29717bfd9 47 CircularBuffer uartBuf;
andreikovacs 27:1eb29717bfd9 48
cotigac 19:71b793021c78 49 #ifdef VERBOSE
cotigac 19:71b793021c78 50 static bool_t bCCAFailed;
cotigac 19:71b793021c78 51 static bool_t bACKFailed;
cotigac 19:71b793021c78 52 #endif
FSL\B36402 23:6f13fea3cace 53 uint32_t gTaskEventFlags;
jmulvain 29:241a264ebe8f 54 static uint8_t gau8TxDataBuffer[gMaxSmacSDULength_c + sizeof(rxPacket_t)];
sam_grove 25:f40bc034cd8b 55 txPacket_t *gAppTxPacket;
sam_grove 25:f40bc034cd8b 56 rxPacket_t *gAppRxPacket;
cotigac 19:71b793021c78 57 static txContextConfig_t txConfigContext;
cotigac 19:71b793021c78 58
andreikovacs 27:1eb29717bfd9 59
andreikovacs 27:1eb29717bfd9 60 void PrintMenu(char * const pu8Menu[])
andreikovacs 27:1eb29717bfd9 61 {
jmulvain 29:241a264ebe8f 62 uint8_t u8Index = 0;
jmulvain 29:241a264ebe8f 63 while(pu8Menu[u8Index]) {
jmulvain 29:241a264ebe8f 64 uart.printf(pu8Menu[u8Index]);
jmulvain 29:241a264ebe8f 65 u8Index++;
jmulvain 29:241a264ebe8f 66 }
andreikovacs 27:1eb29717bfd9 67 }
andreikovacs 27:1eb29717bfd9 68
andreikovacs 27:1eb29717bfd9 69
cotigac 19:71b793021c78 70 void InitProject(void);
FSL\B36402 23:6f13fea3cace 71 void InitApp(void);
FSL\B36402 23:6f13fea3cace 72
cotigac 19:71b793021c78 73 extern smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance);
cotigac 19:71b793021c78 74 extern smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance);
cotigac 19:71b793021c78 75
andreikovacs 27:1eb29717bfd9 76 DigitalOut led1(LED_GREEN);
cotigac 18:b02fc0e53df8 77 InterruptIn sw2(SW2);
cotigac 18:b02fc0e53df8 78 uint32_t button_pressed;
cotigac 18:b02fc0e53df8 79 Thread *thread2;
FSL\B36402 23:6f13fea3cace 80 Thread *eventsThread;
sam_grove 2:3e7685cfb2a7 81
andreikovacs 27:1eb29717bfd9 82 void uartSetBaudRate(uint32_t b)
andreikovacs 27:1eb29717bfd9 83 {
andreikovacs 27:1eb29717bfd9 84 uart.baud(b);
andreikovacs 27:1eb29717bfd9 85 }
andreikovacs 27:1eb29717bfd9 86
cotigac 18:b02fc0e53df8 87 void sw2_press(void)
cotigac 18:b02fc0e53df8 88 {
cotigac 18:b02fc0e53df8 89 thread2->signal_set(0x1);
cotigac 18:b02fc0e53df8 90 }
sam_grove 2:3e7685cfb2a7 91
cotigac 18:b02fc0e53df8 92 void led_thread(void const *argument)
cotigac 18:b02fc0e53df8 93 {
cotigac 18:b02fc0e53df8 94 while (true) {
cotigac 18:b02fc0e53df8 95 led1 = !led1;
andreikovacs 27:1eb29717bfd9 96 Thread::wait(200);
sam_grove 2:3e7685cfb2a7 97 }
sam_grove 2:3e7685cfb2a7 98 }
sam_grove 2:3e7685cfb2a7 99
cotigac 18:b02fc0e53df8 100 void button_thread(void const *argument)
sam_grove 2:3e7685cfb2a7 101 {
cotigac 18:b02fc0e53df8 102 while (true) {
cotigac 18:b02fc0e53df8 103 Thread::signal_wait(0x1);
cotigac 18:b02fc0e53df8 104 button_pressed++;
cotigac 18:b02fc0e53df8 105 }
sam_grove 2:3e7685cfb2a7 106 }
sam_grove 2:3e7685cfb2a7 107
FSL\B36402 23:6f13fea3cace 108 void events_thread(void const *argument)
FSL\B36402 23:6f13fea3cace 109 {
jmulvain 29:241a264ebe8f 110 uint8_t rcvd = 0, c = 0;
FSL\B36402 23:6f13fea3cace 111
jmulvain 29:241a264ebe8f 112 while (true) {
FSL\B36402 23:6f13fea3cace 113 Thread::signal_wait(0x1);
jmulvain 29:241a264ebe8f 114 if(gMcps_Cnf_EVENT_c == (gTaskEventFlags & gMcps_Cnf_EVENT_c)) {
FSL\B36402 23:6f13fea3cace 115 //get back in RX
jmulvain 29:241a264ebe8f 116 MLMERXEnableRequest(gAppRxPacket, 0);
andreikovacs 27:1eb29717bfd9 117 //uart.printf("McpsDataCnf: Packet sent\r\n");
FSL\B36402 23:6f13fea3cace 118
FSL\B36402 23:6f13fea3cace 119 }
jmulvain 29:241a264ebe8f 120
jmulvain 29:241a264ebe8f 121 if(gMcps_Ind_EVENT_c == (gTaskEventFlags & gMcps_Ind_EVENT_c)) {
FSL\B36402 23:6f13fea3cace 122 rcvd = gAppRxPacket->smacPdu.smacPdu[0];
FSL\B36402 23:6f13fea3cace 123
FSL\B36402 23:6f13fea3cace 124 //get back in RX
FSL\B36402 23:6f13fea3cace 125 //gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t));
FSL\B36402 23:6f13fea3cace 126 //gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c;
andreikovacs 27:1eb29717bfd9 127 uart.printf("%c", rcvd);
FSL\B36402 23:6f13fea3cace 128 MLMERXEnableRequest(gAppRxPacket, 0);
FSL\B36402 23:6f13fea3cace 129
jmulvain 29:241a264ebe8f 130
FSL\B36402 23:6f13fea3cace 131 }
jmulvain 29:241a264ebe8f 132
jmulvain 29:241a264ebe8f 133 if(gMlme_TimeoutInd_EVENT_c == (gTaskEventFlags & gMlme_TimeoutInd_EVENT_c)) {
andreikovacs 27:1eb29717bfd9 134 uart.printf("MlmeTimeoutInd: \r\n");
FSL\B36402 23:6f13fea3cace 135 }
jmulvain 29:241a264ebe8f 136
jmulvain 29:241a264ebe8f 137 if(gMlme_EdCnf_EVENT_c == (gTaskEventFlags & gMlme_EdCnf_EVENT_c)) {
andreikovacs 27:1eb29717bfd9 138 uart.printf("EdCnf: \r\n");
FSL\B36402 23:6f13fea3cace 139 }
jmulvain 29:241a264ebe8f 140
jmulvain 29:241a264ebe8f 141 if(gMlme_CcaCnf_EVENT_c == (gTaskEventFlags & gMlme_CcaCnf_EVENT_c)) {
andreikovacs 27:1eb29717bfd9 142 uart.printf("CcaCnf: \r\n");
FSL\B36402 23:6f13fea3cace 143 }
jmulvain 29:241a264ebe8f 144
jmulvain 29:241a264ebe8f 145 if(gWUSelf_EVENT_c == (gTaskEventFlags & gWUSelf_EVENT_c)) {
jmulvain 29:241a264ebe8f 146 if (buffer_Ok_c == uartBuf.getFromBuffer(&c)) {
andreikovacs 27:1eb29717bfd9 147 gAppTxPacket->smacPdu.smacPdu[0] = c;
andreikovacs 27:1eb29717bfd9 148 gAppTxPacket->u8DataLength = 1;
andreikovacs 27:1eb29717bfd9 149 (void)MLMERXDisableRequest();
andreikovacs 27:1eb29717bfd9 150 (void)MCPSDataRequest(gAppTxPacket);
andreikovacs 27:1eb29717bfd9 151 }
FSL\B36402 23:6f13fea3cace 152 }
jmulvain 29:241a264ebe8f 153
FSL\B36402 23:6f13fea3cace 154 gTaskEventFlags = 0;
FSL\B36402 23:6f13fea3cace 155 }
FSL\B36402 23:6f13fea3cace 156 }
FSL\B36402 23:6f13fea3cace 157
sam_grove 2:3e7685cfb2a7 158 int main()
sam_grove 2:3e7685cfb2a7 159 {
jmulvain 29:241a264ebe8f 160 // Intialize Ethernet connection
jmulvain 29:241a264ebe8f 161 EthernetInterface eth;
jmulvain 29:241a264ebe8f 162 wait(1);
jmulvain 29:241a264ebe8f 163 //eth.init("169.254.151.121", "255.255.0.0", "169.254.151.122");
jmulvain 29:241a264ebe8f 164 eth.init();
jmulvain 29:241a264ebe8f 165 eth.connect();
jmulvain 29:241a264ebe8f 166
jmulvain 29:241a264ebe8f 167
jmulvain 29:241a264ebe8f 168
andreikovacs 26:56ca40dcfae1 169 MEM_Init();
cotigac 18:b02fc0e53df8 170 Thread thread(led_thread);
cotigac 18:b02fc0e53df8 171 thread2 = new Thread(button_thread);
FSL\B36402 23:6f13fea3cace 172 eventsThread = new Thread(events_thread);
FSL\B36402 23:6f13fea3cace 173 Phy_Init();
cotigac 19:71b793021c78 174 InitSmac();
jmulvain 29:241a264ebe8f 175
andreikovacs 27:1eb29717bfd9 176 uartSetBaudRate(gDefaultBaudRate_UART_c);
jmulvain 29:241a264ebe8f 177
FSL\B36402 23:6f13fea3cace 178 //Tell SMAC who to call when it needs to pass a message to the application thread.
FSL\B36402 23:6f13fea3cace 179 Smac_RegisterSapHandlers((SMAC_APP_MCPS_SapHandler_t)smacToAppMcpsSap,(SMAC_APP_MLME_SapHandler_t)smacToAppMlmeSap,0);
FSL\B36402 23:6f13fea3cace 180
FSL\B36402 23:6f13fea3cace 181 InitApp();
jmulvain 29:241a264ebe8f 182
jmulvain 29:241a264ebe8f 183 PrintMenu(cu8FreescaleLogo);//splash screen
jmulvain 29:241a264ebe8f 184 //show ip address which will indicate success or fail
jmulvain 29:241a264ebe8f 185 printf("Success. Connected!. Device IP Address is %s\r\n", eth.getIPAddress());
jmulvain 29:241a264ebe8f 186
cotigac 18:b02fc0e53df8 187 button_pressed = 0;
cotigac 18:b02fc0e53df8 188 sw2.fall(&sw2_press);
jmulvain 29:241a264ebe8f 189 while (true)
andreikovacs 27:1eb29717bfd9 190 {
andreikovacs 27:1eb29717bfd9 191 if(uart.readable())
jmulvain 29:241a264ebe8f 192 {
andreikovacs 27:1eb29717bfd9 193 (void)uartBuf.addToBuffer(uart.getc());
andreikovacs 27:1eb29717bfd9 194 }
andreikovacs 27:1eb29717bfd9 195 if ( uartBuf.getCount() )
andreikovacs 27:1eb29717bfd9 196 {
andreikovacs 27:1eb29717bfd9 197 gTaskEventFlags |= gWUSelf_EVENT_c;
andreikovacs 27:1eb29717bfd9 198 eventsThread->signal_set(0x1);
jmulvain 29:241a264ebe8f 199 }
andreikovacs 27:1eb29717bfd9 200 Thread::yield();
sam_grove 2:3e7685cfb2a7 201 }
sam_grove 2:3e7685cfb2a7 202 }
cotigac 19:71b793021c78 203
FSL\B36402 23:6f13fea3cace 204 void InitApp()
FSL\B36402 23:6f13fea3cace 205 {
jmulvain 29:241a264ebe8f 206 gAppTxPacket = (txPacket_t*)gau8TxDataBuffer; //Map TX packet to buffer
jmulvain 29:241a264ebe8f 207 gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t));
jmulvain 29:241a264ebe8f 208
jmulvain 29:241a264ebe8f 209 InitProject();
jmulvain 29:241a264ebe8f 210
jmulvain 29:241a264ebe8f 211 SMACFillHeader(&(gAppTxPacket->smacHeader), gDefaultAddress_c);
jmulvain 29:241a264ebe8f 212
jmulvain 29:241a264ebe8f 213 (void)MLMEPAOutputAdjust(gDefaultOutputPower_c);
jmulvain 29:241a264ebe8f 214 (void)MLMESetChannelRequest(gDefaultChannelNumber_c);
jmulvain 29:241a264ebe8f 215 (void)MLMEConfigureTxContext(&txConfigContext);
jmulvain 29:241a264ebe8f 216 //AppDelayTmr = TMR_AllocateTimer();
jmulvain 29:241a264ebe8f 217 gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c;
jmulvain 29:241a264ebe8f 218 (void)MLMERXEnableRequest(gAppRxPacket, 0);
FSL\B36402 23:6f13fea3cace 219 }
FSL\B36402 23:6f13fea3cace 220
FSL\B36402 23:6f13fea3cace 221 /* (Management) Sap handler for managing timeout indication and ED confirm
FSL\B36402 23:6f13fea3cace 222 This is running in INTERRUPT context, so need to send messages to one of the task */
cotigac 19:71b793021c78 223 smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance)
cotigac 19:71b793021c78 224 {
jmulvain 29:241a264ebe8f 225 switch(pMsg->msgType) {
jmulvain 29:241a264ebe8f 226 case gMlmeEdCnf_c:
jmulvain 29:241a264ebe8f 227 gTaskEventFlags |= gMlme_EdCnf_EVENT_c;
jmulvain 29:241a264ebe8f 228 break;
jmulvain 29:241a264ebe8f 229 case gMlmeCcaCnf_c:
jmulvain 29:241a264ebe8f 230 gTaskEventFlags |= gMlme_CcaCnf_EVENT_c;
jmulvain 29:241a264ebe8f 231 break;
jmulvain 29:241a264ebe8f 232 case gMlmeTimeoutInd_c:
jmulvain 29:241a264ebe8f 233 gTaskEventFlags |= gMlme_TimeoutInd_EVENT_c;
jmulvain 29:241a264ebe8f 234 break;
jmulvain 29:241a264ebe8f 235 default:
jmulvain 29:241a264ebe8f 236 break;
jmulvain 29:241a264ebe8f 237 }
jmulvain 29:241a264ebe8f 238 eventsThread->signal_set(0x1);
jmulvain 29:241a264ebe8f 239 MEM_BufferFree(pMsg);
jmulvain 29:241a264ebe8f 240 return gErrorNoError_c;
cotigac 19:71b793021c78 241 }
cotigac 19:71b793021c78 242
FSL\B36402 23:6f13fea3cace 243 /* (Data) Sap handler for managing data confirm and data indication
FSL\B36402 23:6f13fea3cace 244 This is running in INTERRUPT context, so need to send messages to one of the task */
cotigac 19:71b793021c78 245 smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance)
jmulvain 29:241a264ebe8f 246 {
jmulvain 29:241a264ebe8f 247 switch(pMsg->msgType) {
FSL\B36402 23:6f13fea3cace 248 case gMcpsDataInd_c:
jmulvain 29:241a264ebe8f 249 if(pMsg->msgData.dataInd.pRxPacket->rxStatus == rxSuccessStatus_c) {
FSL\B36402 23:6f13fea3cace 250 gTaskEventFlags |= gMcps_Ind_EVENT_c;
FSL\B36402 23:6f13fea3cace 251 }
FSL\B36402 23:6f13fea3cace 252 break;
FSL\B36402 23:6f13fea3cace 253
FSL\B36402 23:6f13fea3cace 254 case gMcpsDataCnf_c:
FSL\B36402 23:6f13fea3cace 255 #ifdef VERBOSE
jmulvain 29:241a264ebe8f 256 if(pMsg->msgData.dataCnf.status == gErrorChannelBusy_c) {
FSL\B36402 23:6f13fea3cace 257 bCCAFailed = TRUE;
FSL\B36402 23:6f13fea3cace 258 }
FSL\B36402 23:6f13fea3cace 259
jmulvain 29:241a264ebe8f 260 if(pMsg->msgData.dataCnf.status == gErrorNoAck_c) {
FSL\B36402 23:6f13fea3cace 261 bACKFailed = TRUE;
FSL\B36402 23:6f13fea3cace 262 }
cotigac 19:71b793021c78 263 #endif
FSL\B36402 23:6f13fea3cace 264
FSL\B36402 23:6f13fea3cace 265 gTaskEventFlags |= gMcps_Cnf_EVENT_c;
FSL\B36402 23:6f13fea3cace 266 break;
FSL\B36402 23:6f13fea3cace 267
FSL\B36402 23:6f13fea3cace 268 default:
FSL\B36402 23:6f13fea3cace 269 break;
FSL\B36402 23:6f13fea3cace 270 }
FSL\B36402 23:6f13fea3cace 271 eventsThread->signal_set(0x1);
FSL\B36402 23:6f13fea3cace 272 MEM_BufferFree(pMsg);
FSL\B36402 23:6f13fea3cace 273
FSL\B36402 23:6f13fea3cace 274 return gErrorNoError_c;
cotigac 19:71b793021c78 275 }
cotigac 19:71b793021c78 276
cotigac 19:71b793021c78 277 void InitProject(void)
jmulvain 29:241a264ebe8f 278 {
jmulvain 29:241a264ebe8f 279 /*Global Data init*/
cotigac 19:71b793021c78 280 #ifdef VERBOSE
jmulvain 29:241a264ebe8f 281 bACKFailed = FALSE;
jmulvain 29:241a264ebe8f 282 bCCAFailed = FALSE;
cotigac 19:71b793021c78 283 #endif
FSL\B36402 23:6f13fea3cace 284
jmulvain 29:241a264ebe8f 285 gTaskEventFlags = 0;
FSL\B36402 23:6f13fea3cace 286
jmulvain 29:241a264ebe8f 287 txConfigContext.autoAck = FALSE;
jmulvain 29:241a264ebe8f 288 txConfigContext.ccaBeforeTx = FALSE;
jmulvain 29:241a264ebe8f 289 txConfigContext.retryCountAckFail = 0;
jmulvain 29:241a264ebe8f 290 txConfigContext.retryCountCCAFail = 0;
FSL\B36402 23:6f13fea3cace 291 }