Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: fsl_phy_mcr20a fsl_smac mbed-rtos mbed
Fork of mcr20_wireless_uart by
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 #include "Phy.h" 00005 #include "SMAC_Interface.h" 00006 #include "SMAC_Config.h" 00007 #include "MemManager.h" 00008 #include "circular_buffer.h" 00009 00010 char * const cu8FreescaleLogo[]={ 00011 "\f\r\n", 00012 "\n\r\n\r\n\r #\n", 00013 "\r ###\n", 00014 "\r ### *\n", 00015 "\r # ***\n", 00016 "\r *** #\n", 00017 "\r * ###\n", 00018 "\r ###\n", 00019 "\r * #\n", 00020 "\r ***\n", 00021 "\r *** #\n", 00022 "\r # * ###\n", 00023 "\r ### ###\n", 00024 "\r ### * # F R E E S C A L E\n", 00025 "\r # ***\n", 00026 "\r *** S E M I C O N D U C T O R\n", 00027 "\r # *\n", 00028 "\r ### 2 0 1 5\n", 00029 "\r ###\n", 00030 "\r # Wireless Uart Demo\r\n\n", 00031 NULL 00032 }; 00033 00034 char * const IUPUI_Logo[]={ " n\n\n", 00035 " \r ", 00036 "\r ", 00037 "\r ELECTRICAL AND COMPUTER ENGINEERING", 00038 "\r\r IUPUI", 00039 "\r\r ", 00040 00041 "\n", 00042 NULL 00043 }; 00044 #define gMcps_Cnf_EVENT_c (1<<1) 00045 #define gMcps_Ind_EVENT_c (1<<2) 00046 #define gMlme_EdCnf_EVENT_c (1<<3) 00047 #define gMlme_CcaCnf_EVENT_c (1<<4) 00048 #define gMlme_TimeoutInd_EVENT_c (1<<5) 00049 #define gWUSelf_EVENT_c (1<<6) 00050 00051 #define gDefaultBaudRate_UART_c 115200UL 00052 00053 Serial uart(USBTX, USBRX); 00054 CircularBuffer uartBuf; 00055 00056 #ifdef VERBOSE 00057 static bool_t bCCAFailed; 00058 static bool_t bACKFailed; 00059 #endif 00060 uint32_t gTaskEventFlags; 00061 static uint8_t gau8TxDataBuffer[gMaxSmacSDULength_c + sizeof(rxPacket_t)]; 00062 txPacket_t *gAppTxPacket; 00063 rxPacket_t *gAppRxPacket; 00064 static txContextConfig_t txConfigContext; 00065 00066 00067 void PrintMenu(char * const pu8Menu[]) 00068 { 00069 uint8_t u8Index = 0; 00070 while(pu8Menu[u8Index]){ 00071 uart.printf(pu8Menu[u8Index]); 00072 u8Index++; 00073 } 00074 } 00075 00076 00077 void InitProject(void); 00078 void InitApp(void); 00079 00080 extern smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance); 00081 extern smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance); 00082 00083 DigitalOut led1(LED_GREEN); 00084 InterruptIn sw2(SW2); 00085 uint32_t button_pressed; 00086 Thread *thread2; 00087 Thread *eventsThread; 00088 00089 void uartSetBaudRate(uint32_t b) 00090 { 00091 uart.baud(b); 00092 } 00093 00094 void sw2_press(void) 00095 { 00096 thread2->signal_set(0x1); 00097 } 00098 00099 void led_thread(void const *argument) 00100 { 00101 while (true) { 00102 led1 = !led1; 00103 Thread::wait(200); 00104 } 00105 } 00106 00107 void button_thread(void const *argument) 00108 { 00109 while (true) { 00110 Thread::signal_wait(0x1); 00111 button_pressed++; 00112 } 00113 } 00114 00115 void events_thread(void const *argument) 00116 { 00117 uint8_t rcvd = 0, c = 0; 00118 00119 while (true) 00120 { 00121 Thread::signal_wait(0x1); 00122 if(gMcps_Cnf_EVENT_c == (gTaskEventFlags & gMcps_Cnf_EVENT_c)) 00123 { 00124 //get back in RX 00125 MLMERXEnableRequest(gAppRxPacket, 0); 00126 //uart.printf("McpsDataCnf: Packet sent\r\n"); 00127 00128 } 00129 00130 if(gMcps_Ind_EVENT_c == (gTaskEventFlags & gMcps_Ind_EVENT_c)) 00131 { 00132 rcvd = gAppRxPacket->smacPdu.smacPdu[0]; 00133 00134 //get back in RX 00135 //gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t)); 00136 //gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c; 00137 uart.printf("%c", rcvd); 00138 MLMERXEnableRequest(gAppRxPacket, 0); 00139 00140 00141 } 00142 00143 if(gMlme_TimeoutInd_EVENT_c == (gTaskEventFlags & gMlme_TimeoutInd_EVENT_c)) 00144 { 00145 uart.printf("MlmeTimeoutInd: \r\n"); 00146 } 00147 00148 if(gMlme_EdCnf_EVENT_c == (gTaskEventFlags & gMlme_EdCnf_EVENT_c)) 00149 { 00150 uart.printf("EdCnf: \r\n"); 00151 } 00152 00153 if(gMlme_CcaCnf_EVENT_c == (gTaskEventFlags & gMlme_CcaCnf_EVENT_c)) 00154 { 00155 uart.printf("CcaCnf: \r\n"); 00156 } 00157 00158 if(gWUSelf_EVENT_c == (gTaskEventFlags & gWUSelf_EVENT_c)) 00159 { 00160 if (buffer_Ok_c == uartBuf.getFromBuffer(&c)) 00161 { 00162 gAppTxPacket->smacPdu.smacPdu[0] = c; 00163 gAppTxPacket->u8DataLength = 1; 00164 (void)MLMERXDisableRequest(); 00165 (void)MCPSDataRequest(gAppTxPacket); 00166 } 00167 } 00168 00169 gTaskEventFlags = 0; 00170 } 00171 } 00172 00173 int main() 00174 { 00175 MEM_Init(); 00176 Thread thread(led_thread); 00177 thread2 = new Thread(button_thread); 00178 eventsThread = new Thread(events_thread); 00179 Phy_Init(); 00180 InitSmac(); 00181 00182 uartSetBaudRate(gDefaultBaudRate_UART_c); 00183 00184 //Tell SMAC who to call when it needs to pass a message to the application thread. 00185 Smac_RegisterSapHandlers((SMAC_APP_MCPS_SapHandler_t)smacToAppMcpsSap,(SMAC_APP_MLME_SapHandler_t)smacToAppMlmeSap,0); 00186 00187 InitApp(); 00188 00189 PrintMenu(cu8FreescaleLogo); 00190 PrintMenu(IUPUI_Logo); 00191 00192 button_pressed = 0; 00193 sw2.fall(&sw2_press); 00194 while (true) 00195 { 00196 if(uart.readable()) 00197 { 00198 (void)uartBuf.addToBuffer(uart.getc()); 00199 } 00200 if ( uartBuf.getCount() ) 00201 { 00202 gTaskEventFlags |= gWUSelf_EVENT_c; 00203 eventsThread->signal_set(0x1); 00204 } 00205 Thread::yield(); 00206 } 00207 } 00208 00209 void InitApp() 00210 { 00211 gAppTxPacket = (txPacket_t*)gau8TxDataBuffer; //Map TX packet to buffer 00212 gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t)); 00213 00214 InitProject(); 00215 00216 SMACFillHeader(&(gAppTxPacket->smacHeader), gDefaultAddress_c); 00217 00218 (void)MLMEPAOutputAdjust(gDefaultOutputPower_c); 00219 (void)MLMESetChannelRequest(gDefaultChannelNumber_c); 00220 (void)MLMEConfigureTxContext(&txConfigContext); 00221 //AppDelayTmr = TMR_AllocateTimer(); 00222 gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c; 00223 (void)MLMERXEnableRequest(gAppRxPacket, 0); 00224 } 00225 00226 /* (Management) Sap handler for managing timeout indication and ED confirm 00227 This is running in INTERRUPT context, so need to send messages to one of the task */ 00228 smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance) 00229 { 00230 switch(pMsg->msgType) 00231 { 00232 case gMlmeEdCnf_c: 00233 gTaskEventFlags |= gMlme_EdCnf_EVENT_c; 00234 break; 00235 case gMlmeCcaCnf_c: 00236 gTaskEventFlags |= gMlme_CcaCnf_EVENT_c; 00237 break; 00238 case gMlmeTimeoutInd_c: 00239 gTaskEventFlags |= gMlme_TimeoutInd_EVENT_c; 00240 break; 00241 default: 00242 break; 00243 } 00244 eventsThread->signal_set(0x1); 00245 MEM_BufferFree(pMsg); 00246 return gErrorNoError_c; 00247 } 00248 00249 /* (Data) Sap handler for managing data confirm and data indication 00250 This is running in INTERRUPT context, so need to send messages to one of the task */ 00251 smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance) 00252 { 00253 switch(pMsg->msgType) 00254 { 00255 case gMcpsDataInd_c: 00256 if(pMsg->msgData.dataInd.pRxPacket->rxStatus == rxSuccessStatus_c) 00257 { 00258 gTaskEventFlags |= gMcps_Ind_EVENT_c; 00259 } 00260 break; 00261 00262 case gMcpsDataCnf_c: 00263 #ifdef VERBOSE 00264 if(pMsg->msgData.dataCnf.status == gErrorChannelBusy_c) 00265 { 00266 bCCAFailed = TRUE; 00267 } 00268 00269 if(pMsg->msgData.dataCnf.status == gErrorNoAck_c) 00270 { 00271 bACKFailed = TRUE; 00272 } 00273 #endif 00274 00275 gTaskEventFlags |= gMcps_Cnf_EVENT_c; 00276 break; 00277 00278 default: 00279 break; 00280 } 00281 eventsThread->signal_set(0x1); 00282 MEM_BufferFree(pMsg); 00283 00284 return gErrorNoError_c; 00285 } 00286 00287 void InitProject(void) 00288 { 00289 /*Global Data init*/ 00290 #ifdef VERBOSE 00291 bACKFailed = FALSE; 00292 bCCAFailed = FALSE; 00293 #endif 00294 00295 gTaskEventFlags = 0; 00296 00297 txConfigContext.autoAck = FALSE; 00298 txConfigContext.ccaBeforeTx = FALSE; 00299 txConfigContext.retryCountAckFail = 0; 00300 txConfigContext.retryCountCCAFail = 0; 00301 }
Generated on Sat Jul 16 2022 23:59:26 by
 1.7.2
 1.7.2 
    