Thread UART

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by samuel belete

Committer:
sam_grove
Date:
Tue Apr 28 16:37:24 2015 -0500
Revision:
25:f40bc034cd8b
Parent:
24:088286081619
Child:
26:56ca40dcfae1
Add mem pool management. Dont use malloc with ARM std lib in IRQ context.
Tx running stable. Rx crashing instantly. Not allocating queue before
being needed.

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"
cotigac 19:71b793021c78 7
FSL\B36402 23:6f13fea3cace 8 #define gMcps_Cnf_EVENT_c (1<<1)
FSL\B36402 23:6f13fea3cace 9 #define gMcps_Ind_EVENT_c (1<<2)
FSL\B36402 23:6f13fea3cace 10 #define gMlme_EdCnf_EVENT_c (1<<3)
FSL\B36402 23:6f13fea3cace 11 #define gMlme_CcaCnf_EVENT_c (1<<4)
FSL\B36402 23:6f13fea3cace 12 #define gMlme_TimeoutInd_EVENT_c (1<<5)
FSL\B36402 23:6f13fea3cace 13 #define gWUSelf_EVENT_c (1<<6)
FSL\B36402 23:6f13fea3cace 14
cotigac 19:71b793021c78 15 #ifdef VERBOSE
cotigac 19:71b793021c78 16 static bool_t bCCAFailed;
cotigac 19:71b793021c78 17 static bool_t bACKFailed;
cotigac 19:71b793021c78 18 #endif
FSL\B36402 23:6f13fea3cace 19 uint32_t gTaskEventFlags;
cotigac 19:71b793021c78 20 static uint8_t gau8TxDataBuffer[gMaxSmacSDULength_c + sizeof(rxPacket_t)];
sam_grove 25:f40bc034cd8b 21 txPacket_t *gAppTxPacket;
sam_grove 25:f40bc034cd8b 22 rxPacket_t *gAppRxPacket;
cotigac 19:71b793021c78 23 static txContextConfig_t txConfigContext;
cotigac 19:71b793021c78 24
cotigac 19:71b793021c78 25 void InitProject(void);
FSL\B36402 23:6f13fea3cace 26 void InitApp(void);
FSL\B36402 23:6f13fea3cace 27
cotigac 19:71b793021c78 28 extern smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance);
cotigac 19:71b793021c78 29 extern smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance);
cotigac 19:71b793021c78 30
cotigac 18:b02fc0e53df8 31 DigitalOut led1(LED1);
cotigac 18:b02fc0e53df8 32 InterruptIn sw2(SW2);
cotigac 18:b02fc0e53df8 33 uint32_t button_pressed;
cotigac 18:b02fc0e53df8 34 Thread *thread2;
FSL\B36402 23:6f13fea3cace 35 Thread *eventsThread;
sam_grove 2:3e7685cfb2a7 36
cotigac 18:b02fc0e53df8 37 void sw2_press(void)
cotigac 18:b02fc0e53df8 38 {
cotigac 18:b02fc0e53df8 39 thread2->signal_set(0x1);
cotigac 18:b02fc0e53df8 40 }
sam_grove 2:3e7685cfb2a7 41
cotigac 18:b02fc0e53df8 42 void led_thread(void const *argument)
cotigac 18:b02fc0e53df8 43 {
cotigac 18:b02fc0e53df8 44 while (true) {
cotigac 18:b02fc0e53df8 45 led1 = !led1;
cotigac 18:b02fc0e53df8 46 Thread::wait(1000);
sam_grove 2:3e7685cfb2a7 47 }
sam_grove 2:3e7685cfb2a7 48 }
sam_grove 2:3e7685cfb2a7 49
cotigac 18:b02fc0e53df8 50 void button_thread(void const *argument)
sam_grove 2:3e7685cfb2a7 51 {
cotigac 18:b02fc0e53df8 52 while (true) {
cotigac 18:b02fc0e53df8 53 Thread::signal_wait(0x1);
cotigac 18:b02fc0e53df8 54 button_pressed++;
cotigac 18:b02fc0e53df8 55 }
sam_grove 2:3e7685cfb2a7 56 }
sam_grove 2:3e7685cfb2a7 57
FSL\B36402 23:6f13fea3cace 58 void events_thread(void const *argument)
FSL\B36402 23:6f13fea3cace 59 {
FSL\B36402 23:6f13fea3cace 60 uint8_t rcvd = 0;
FSL\B36402 23:6f13fea3cace 61
FSL\B36402 23:6f13fea3cace 62 while (true)
FSL\B36402 23:6f13fea3cace 63 {
FSL\B36402 23:6f13fea3cace 64 Thread::signal_wait(0x1);
FSL\B36402 23:6f13fea3cace 65
FSL\B36402 23:6f13fea3cace 66 if(gMcps_Cnf_EVENT_c == (gTaskEventFlags & gMcps_Cnf_EVENT_c))
FSL\B36402 23:6f13fea3cace 67 {
FSL\B36402 23:6f13fea3cace 68 //get back in RX
FSL\B36402 23:6f13fea3cace 69 MLMERXEnableRequest(gAppRxPacket, 0);
FSL\B36402 23:6f13fea3cace 70
FSL\B36402 23:6f13fea3cace 71 //printf("McpsDataCnf: Packet sent\r\n");
FSL\B36402 24:088286081619 72 //fflush(stdout);
FSL\B36402 23:6f13fea3cace 73 }
FSL\B36402 23:6f13fea3cace 74
FSL\B36402 23:6f13fea3cace 75 if(gMcps_Ind_EVENT_c == (gTaskEventFlags & gMcps_Ind_EVENT_c))
FSL\B36402 23:6f13fea3cace 76 {
FSL\B36402 23:6f13fea3cace 77 rcvd = gAppRxPacket->smacPdu.smacPdu[0];
FSL\B36402 23:6f13fea3cace 78
FSL\B36402 23:6f13fea3cace 79 //get back in RX
FSL\B36402 23:6f13fea3cace 80 //gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t));
FSL\B36402 23:6f13fea3cace 81 //gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c;
FSL\B36402 23:6f13fea3cace 82 MLMERXEnableRequest(gAppRxPacket, 0);
FSL\B36402 23:6f13fea3cace 83
FSL\B36402 23:6f13fea3cace 84 printf("McpsDataInd: Received %d\r\n", rcvd);
FSL\B36402 24:088286081619 85 fflush(stdout);
FSL\B36402 23:6f13fea3cace 86 }
FSL\B36402 23:6f13fea3cace 87
FSL\B36402 23:6f13fea3cace 88 if(gMlme_TimeoutInd_EVENT_c == (gTaskEventFlags & gMlme_TimeoutInd_EVENT_c))
FSL\B36402 23:6f13fea3cace 89 {
FSL\B36402 24:088286081619 90 printf("MlmeTimeoutInd: \r\n");
FSL\B36402 24:088286081619 91 fflush(stdout);
FSL\B36402 23:6f13fea3cace 92 }
FSL\B36402 23:6f13fea3cace 93
FSL\B36402 23:6f13fea3cace 94 if(gMlme_EdCnf_EVENT_c == (gTaskEventFlags & gMlme_EdCnf_EVENT_c))
FSL\B36402 23:6f13fea3cace 95 {
FSL\B36402 24:088286081619 96 printf("EdCnf: \r\n");
FSL\B36402 24:088286081619 97 fflush(stdout);
FSL\B36402 23:6f13fea3cace 98 }
FSL\B36402 23:6f13fea3cace 99
FSL\B36402 23:6f13fea3cace 100 if(gMlme_CcaCnf_EVENT_c == (gTaskEventFlags & gMlme_CcaCnf_EVENT_c))
FSL\B36402 23:6f13fea3cace 101 {
FSL\B36402 24:088286081619 102 printf("CcaCnf: \r\n");
FSL\B36402 24:088286081619 103 fflush(stdout);
FSL\B36402 23:6f13fea3cace 104 }
FSL\B36402 23:6f13fea3cace 105
FSL\B36402 23:6f13fea3cace 106 if(gWUSelf_EVENT_c == (gTaskEventFlags & gWUSelf_EVENT_c))
FSL\B36402 23:6f13fea3cace 107 {
FSL\B36402 23:6f13fea3cace 108
FSL\B36402 23:6f13fea3cace 109 }
FSL\B36402 23:6f13fea3cace 110
FSL\B36402 23:6f13fea3cace 111 gTaskEventFlags = 0;
FSL\B36402 23:6f13fea3cace 112 }
FSL\B36402 23:6f13fea3cace 113 }
FSL\B36402 23:6f13fea3cace 114
sam_grove 2:3e7685cfb2a7 115 int main()
sam_grove 2:3e7685cfb2a7 116 {
sam_grove 25:f40bc034cd8b 117 InitMem(extmem, sizeof(extmem));
cotigac 18:b02fc0e53df8 118 Thread thread(led_thread);
cotigac 18:b02fc0e53df8 119 thread2 = new Thread(button_thread);
FSL\B36402 23:6f13fea3cace 120 eventsThread = new Thread(events_thread);
FSL\B36402 23:6f13fea3cace 121
FSL\B36402 23:6f13fea3cace 122 Phy_Init();
cotigac 19:71b793021c78 123 InitSmac();
cotigac 19:71b793021c78 124
FSL\B36402 23:6f13fea3cace 125 //Tell SMAC who to call when it needs to pass a message to the application thread.
FSL\B36402 23:6f13fea3cace 126 Smac_RegisterSapHandlers((SMAC_APP_MCPS_SapHandler_t)smacToAppMcpsSap,(SMAC_APP_MLME_SapHandler_t)smacToAppMlmeSap,0);
FSL\B36402 23:6f13fea3cace 127
FSL\B36402 23:6f13fea3cace 128 InitApp();
FSL\B36402 23:6f13fea3cace 129
cotigac 18:b02fc0e53df8 130 button_pressed = 0;
cotigac 18:b02fc0e53df8 131 sw2.fall(&sw2_press);
cotigac 18:b02fc0e53df8 132 while (true) {
cotigac 18:b02fc0e53df8 133 Thread::wait(5000);
FSL\B36402 24:088286081619 134
FSL\B36402 23:6f13fea3cace 135 gAppTxPacket->smacPdu.smacPdu[0] = (uint8_t)button_pressed;
FSL\B36402 23:6f13fea3cace 136 gAppTxPacket->u8DataLength = 1;
FSL\B36402 23:6f13fea3cace 137 (void)MLMERXDisableRequest();
FSL\B36402 23:6f13fea3cace 138 (void)MCPSDataRequest(gAppTxPacket);
FSL\B36402 24:088286081619 139
cotigac 18:b02fc0e53df8 140 printf("SW2 was pressed (last 5 seconds): %d \r\n", button_pressed);
cotigac 18:b02fc0e53df8 141 fflush(stdout);
cotigac 18:b02fc0e53df8 142 button_pressed = 0;
sam_grove 2:3e7685cfb2a7 143 }
sam_grove 2:3e7685cfb2a7 144 }
cotigac 19:71b793021c78 145
FSL\B36402 23:6f13fea3cace 146 void InitApp()
FSL\B36402 23:6f13fea3cace 147 {
FSL\B36402 23:6f13fea3cace 148 gAppTxPacket = (txPacket_t*)gau8TxDataBuffer; //Map TX packet to buffer
FSL\B36402 23:6f13fea3cace 149 gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t));
FSL\B36402 23:6f13fea3cace 150
FSL\B36402 23:6f13fea3cace 151 InitProject();
FSL\B36402 23:6f13fea3cace 152
FSL\B36402 23:6f13fea3cace 153 SMACFillHeader(&(gAppTxPacket->smacHeader), gDefaultAddress_c);
FSL\B36402 23:6f13fea3cace 154
FSL\B36402 23:6f13fea3cace 155 (void)MLMEPAOutputAdjust(gDefaultOutputPower_c);
FSL\B36402 23:6f13fea3cace 156 (void)MLMESetChannelRequest(gDefaultChannelNumber_c);
FSL\B36402 23:6f13fea3cace 157 (void)MLMEConfigureTxContext(&txConfigContext);
FSL\B36402 23:6f13fea3cace 158 //AppDelayTmr = TMR_AllocateTimer();
FSL\B36402 23:6f13fea3cace 159 gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c;
FSL\B36402 23:6f13fea3cace 160 (void)MLMERXEnableRequest(gAppRxPacket, 0);
FSL\B36402 23:6f13fea3cace 161 }
FSL\B36402 23:6f13fea3cace 162
FSL\B36402 23:6f13fea3cace 163 /* (Management) Sap handler for managing timeout indication and ED confirm
FSL\B36402 23:6f13fea3cace 164 This is running in INTERRUPT context, so need to send messages to one of the task */
cotigac 19:71b793021c78 165 smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance)
cotigac 19:71b793021c78 166 {
cotigac 19:71b793021c78 167 switch(pMsg->msgType)
cotigac 19:71b793021c78 168 {
FSL\B36402 23:6f13fea3cace 169 case gMlmeEdCnf_c:
FSL\B36402 23:6f13fea3cace 170 gTaskEventFlags |= gMlme_EdCnf_EVENT_c;
FSL\B36402 23:6f13fea3cace 171 break;
FSL\B36402 23:6f13fea3cace 172 case gMlmeCcaCnf_c:
FSL\B36402 23:6f13fea3cace 173 gTaskEventFlags |= gMlme_CcaCnf_EVENT_c;
FSL\B36402 23:6f13fea3cace 174 break;
FSL\B36402 23:6f13fea3cace 175 case gMlmeTimeoutInd_c:
FSL\B36402 23:6f13fea3cace 176 gTaskEventFlags |= gMlme_TimeoutInd_EVENT_c;
FSL\B36402 23:6f13fea3cace 177 break;
FSL\B36402 23:6f13fea3cace 178 default:
FSL\B36402 23:6f13fea3cace 179 break;
cotigac 19:71b793021c78 180 }
FSL\B36402 23:6f13fea3cace 181 eventsThread->signal_set(0x1);
cotigac 19:71b793021c78 182 MEM_BufferFree(pMsg);
cotigac 19:71b793021c78 183 return gErrorNoError_c;
cotigac 19:71b793021c78 184 }
cotigac 19:71b793021c78 185
FSL\B36402 23:6f13fea3cace 186 /* (Data) Sap handler for managing data confirm and data indication
FSL\B36402 23:6f13fea3cace 187 This is running in INTERRUPT context, so need to send messages to one of the task */
cotigac 19:71b793021c78 188 smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance)
FSL\B36402 23:6f13fea3cace 189 {
FSL\B36402 23:6f13fea3cace 190 switch(pMsg->msgType)
cotigac 19:71b793021c78 191 {
FSL\B36402 23:6f13fea3cace 192 case gMcpsDataInd_c:
FSL\B36402 23:6f13fea3cace 193 if(pMsg->msgData.dataInd.pRxPacket->rxStatus == rxSuccessStatus_c)
FSL\B36402 23:6f13fea3cace 194 {
FSL\B36402 23:6f13fea3cace 195 gTaskEventFlags |= gMcps_Ind_EVENT_c;
FSL\B36402 23:6f13fea3cace 196 }
FSL\B36402 23:6f13fea3cace 197 break;
FSL\B36402 23:6f13fea3cace 198
FSL\B36402 23:6f13fea3cace 199 case gMcpsDataCnf_c:
FSL\B36402 23:6f13fea3cace 200 #ifdef VERBOSE
FSL\B36402 23:6f13fea3cace 201 if(pMsg->msgData.dataCnf.status == gErrorChannelBusy_c)
FSL\B36402 23:6f13fea3cace 202 {
FSL\B36402 23:6f13fea3cace 203 bCCAFailed = TRUE;
FSL\B36402 23:6f13fea3cace 204 }
FSL\B36402 23:6f13fea3cace 205
FSL\B36402 23:6f13fea3cace 206 if(pMsg->msgData.dataCnf.status == gErrorNoAck_c)
FSL\B36402 23:6f13fea3cace 207 {
FSL\B36402 23:6f13fea3cace 208 bACKFailed = TRUE;
FSL\B36402 23:6f13fea3cace 209 }
cotigac 19:71b793021c78 210 #endif
FSL\B36402 23:6f13fea3cace 211
FSL\B36402 23:6f13fea3cace 212 gTaskEventFlags |= gMcps_Cnf_EVENT_c;
FSL\B36402 23:6f13fea3cace 213 break;
FSL\B36402 23:6f13fea3cace 214
FSL\B36402 23:6f13fea3cace 215 default:
FSL\B36402 23:6f13fea3cace 216 break;
FSL\B36402 23:6f13fea3cace 217 }
FSL\B36402 23:6f13fea3cace 218
FSL\B36402 23:6f13fea3cace 219 eventsThread->signal_set(0x1);
FSL\B36402 23:6f13fea3cace 220 MEM_BufferFree(pMsg);
FSL\B36402 23:6f13fea3cace 221
FSL\B36402 23:6f13fea3cace 222 return gErrorNoError_c;
cotigac 19:71b793021c78 223 }
cotigac 19:71b793021c78 224
cotigac 19:71b793021c78 225 void InitProject(void)
cotigac 19:71b793021c78 226 {
cotigac 19:71b793021c78 227 /*Global Data init*/
cotigac 19:71b793021c78 228 #ifdef VERBOSE
cotigac 19:71b793021c78 229 bACKFailed = FALSE;
cotigac 19:71b793021c78 230 bCCAFailed = FALSE;
cotigac 19:71b793021c78 231 #endif
FSL\B36402 23:6f13fea3cace 232
FSL\B36402 23:6f13fea3cace 233 gTaskEventFlags = 0;
FSL\B36402 23:6f13fea3cace 234
cotigac 19:71b793021c78 235 txConfigContext.autoAck = FALSE;
cotigac 19:71b793021c78 236 txConfigContext.ccaBeforeTx = FALSE;
cotigac 19:71b793021c78 237 txConfigContext.retryCountAckFail = 0;
cotigac 19:71b793021c78 238 txConfigContext.retryCountCCAFail = 0;
FSL\B36402 23:6f13fea3cace 239 }