wireless_uart

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by samuel belete

Committer:
andreikovacs
Date:
Tue Jun 23 11:49:16 2015 +0000
Revision:
26:56ca40dcfae1
Parent:
25:f40bc034cd8b
Child:
27:1eb29717bfd9
Added Freescale Connectivity Framework memory allocator

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