Thread UART

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by samuel belete

Committer:
FSL\B36402
Date:
Sat Apr 25 00:50:20 2015 -0500
Revision:
23:6f13fea3cace
Parent:
19:71b793021c78
Child:
24:088286081619
First version working with IAR

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