Thread UART

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by samuel belete

Committer:
phy_sam
Date:
Thu Aug 18 16:44:14 2016 +0000
Revision:
29:104ca2835945
Parent:
27:1eb29717bfd9
FRDM-k64f   &  CR20A use  for test_conectivty_test

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