Thread UART

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by samuel belete

Committer:
andreikovacs
Date:
Mon Jun 29 05:50:47 2015 +0000
Revision:
27:1eb29717bfd9
Parent:
26:56ca40dcfae1
Child:
29:104ca2835945
Changed app to behave like the initial Wireless UART demo

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