Thread UART
Dependencies: fsl_phy_mcr20a fsl_smac mbed-rtos mbed
Fork of mcr20_wireless_uart by
main.cpp@19:71b793021c78, 2015-04-04 (annotated)
- Committer:
- cotigac
- Date:
- Sat Apr 04 22:14:14 2015 +0000
- Revision:
- 19:71b793021c78
- Parent:
- 18:b02fc0e53df8
- Child:
- 23:6f13fea3cace
Added SMAC code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:01fb291427ce | 1 | #include "mbed.h" |
cotigac | 18:b02fc0e53df8 | 2 | #include "rtos.h" |
sam_grove | 2:3e7685cfb2a7 | 3 | |
cotigac | 19:71b793021c78 | 4 | #include "SMAC_Interface.h" |
cotigac | 19:71b793021c78 | 5 | #include "SMAC_Config.h" |
cotigac | 19:71b793021c78 | 6 | |
cotigac | 19:71b793021c78 | 7 | static uint8_t mAppSer; |
cotigac | 19:71b793021c78 | 8 | static uint8_t timePassed; |
cotigac | 19:71b793021c78 | 9 | #ifdef VERBOSE |
cotigac | 19:71b793021c78 | 10 | static bool_t bCCAFailed; |
cotigac | 19:71b793021c78 | 11 | static bool_t bACKFailed; |
cotigac | 19:71b793021c78 | 12 | #endif |
cotigac | 19:71b793021c78 | 13 | static bool_t bTxDone; |
cotigac | 19:71b793021c78 | 14 | static bool_t bRxDone; |
cotigac | 19:71b793021c78 | 15 | static bool_t evKeyPressed; |
cotigac | 19:71b793021c78 | 16 | static bool_t bTxInProgress; |
cotigac | 19:71b793021c78 | 17 | static uint8_t gau8TxDataBuffer[gMaxSmacSDULength_c + sizeof(rxPacket_t)]; |
cotigac | 19:71b793021c78 | 18 | static txPacket_t *gAppTxPacket; |
cotigac | 19:71b793021c78 | 19 | static rxPacket_t *gAppRxPacket; |
cotigac | 19:71b793021c78 | 20 | static txContextConfig_t txConfigContext; |
cotigac | 19:71b793021c78 | 21 | |
cotigac | 19:71b793021c78 | 22 | void InitProject(void); |
cotigac | 19:71b793021c78 | 23 | extern smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance); |
cotigac | 19:71b793021c78 | 24 | extern smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance); |
cotigac | 19:71b793021c78 | 25 | |
cotigac | 18:b02fc0e53df8 | 26 | DigitalOut led1(LED1); |
cotigac | 18:b02fc0e53df8 | 27 | InterruptIn sw2(SW2); |
cotigac | 18:b02fc0e53df8 | 28 | uint32_t button_pressed; |
cotigac | 18:b02fc0e53df8 | 29 | Thread *thread2; |
sam_grove | 2:3e7685cfb2a7 | 30 | |
cotigac | 18:b02fc0e53df8 | 31 | void sw2_press(void) |
cotigac | 18:b02fc0e53df8 | 32 | { |
cotigac | 18:b02fc0e53df8 | 33 | thread2->signal_set(0x1); |
cotigac | 18:b02fc0e53df8 | 34 | } |
sam_grove | 2:3e7685cfb2a7 | 35 | |
cotigac | 18:b02fc0e53df8 | 36 | void led_thread(void const *argument) |
cotigac | 18:b02fc0e53df8 | 37 | { |
cotigac | 18:b02fc0e53df8 | 38 | while (true) { |
cotigac | 18:b02fc0e53df8 | 39 | led1 = !led1; |
cotigac | 18:b02fc0e53df8 | 40 | Thread::wait(1000); |
sam_grove | 2:3e7685cfb2a7 | 41 | } |
sam_grove | 2:3e7685cfb2a7 | 42 | } |
sam_grove | 2:3e7685cfb2a7 | 43 | |
cotigac | 18:b02fc0e53df8 | 44 | void button_thread(void const *argument) |
sam_grove | 2:3e7685cfb2a7 | 45 | { |
cotigac | 18:b02fc0e53df8 | 46 | while (true) { |
cotigac | 18:b02fc0e53df8 | 47 | Thread::signal_wait(0x1); |
cotigac | 18:b02fc0e53df8 | 48 | button_pressed++; |
cotigac | 18:b02fc0e53df8 | 49 | } |
sam_grove | 2:3e7685cfb2a7 | 50 | } |
sam_grove | 2:3e7685cfb2a7 | 51 | |
sam_grove | 2:3e7685cfb2a7 | 52 | int main() |
sam_grove | 2:3e7685cfb2a7 | 53 | { |
cotigac | 18:b02fc0e53df8 | 54 | Thread thread(led_thread); |
cotigac | 18:b02fc0e53df8 | 55 | thread2 = new Thread(button_thread); |
cotigac | 19:71b793021c78 | 56 | |
cotigac | 19:71b793021c78 | 57 | InitSmac(); |
cotigac | 19:71b793021c78 | 58 | Smac_RegisterSapHandlers((SMAC_APP_MCPS_SapHandler_t)smacToAppMcpsSap,(SMAC_APP_MLME_SapHandler_t)smacToAppMlmeSap,0); |
cotigac | 19:71b793021c78 | 59 | |
cotigac | 19:71b793021c78 | 60 | InitProject(); |
sam_grove | 2:3e7685cfb2a7 | 61 | |
cotigac | 19:71b793021c78 | 62 | gAppTxPacket = (txPacket_t*)gau8TxDataBuffer; //Map TX packet to buffer |
cotigac | 19:71b793021c78 | 63 | gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t)); |
cotigac | 19:71b793021c78 | 64 | |
cotigac | 19:71b793021c78 | 65 | SMACFillHeader(&(gAppTxPacket->smacHeader), gDefaultAddress_c); |
cotigac | 19:71b793021c78 | 66 | |
cotigac | 19:71b793021c78 | 67 | (void)MLMEPAOutputAdjust(gDefaultOutputPower_c); |
cotigac | 19:71b793021c78 | 68 | (void)MLMESetChannelRequest(gDefaultChannelNumber_c); |
cotigac | 19:71b793021c78 | 69 | (void)MLMEConfigureTxContext(&txConfigContext); |
cotigac | 19:71b793021c78 | 70 | gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c; |
cotigac | 19:71b793021c78 | 71 | (void)MLMERXEnableRequest(gAppRxPacket, 0); |
cotigac | 19:71b793021c78 | 72 | |
cotigac | 18:b02fc0e53df8 | 73 | button_pressed = 0; |
cotigac | 18:b02fc0e53df8 | 74 | sw2.fall(&sw2_press); |
cotigac | 18:b02fc0e53df8 | 75 | while (true) { |
cotigac | 18:b02fc0e53df8 | 76 | Thread::wait(5000); |
cotigac | 18:b02fc0e53df8 | 77 | printf("SW2 was pressed (last 5 seconds): %d \r\n", button_pressed); |
cotigac | 18:b02fc0e53df8 | 78 | fflush(stdout); |
cotigac | 18:b02fc0e53df8 | 79 | button_pressed = 0; |
sam_grove | 2:3e7685cfb2a7 | 80 | } |
sam_grove | 2:3e7685cfb2a7 | 81 | } |
cotigac | 19:71b793021c78 | 82 | |
cotigac | 19:71b793021c78 | 83 | smacErrors_t smacToAppMlmeSap(smacToAppMlmeMessage_t* pMsg, instanceId_t instance) |
cotigac | 19:71b793021c78 | 84 | { |
cotigac | 19:71b793021c78 | 85 | switch(pMsg->msgType) |
cotigac | 19:71b793021c78 | 86 | { |
cotigac | 19:71b793021c78 | 87 | case gMlmeEdCnf_c: |
cotigac | 19:71b793021c78 | 88 | printf("EdCnf: \r\n"); |
cotigac | 19:71b793021c78 | 89 | //(void)OSA_EventSet(&gTaskEvent, gMlme_EdCnf_EVENT_c); |
cotigac | 19:71b793021c78 | 90 | break; |
cotigac | 19:71b793021c78 | 91 | case gMlmeCcaCnf_c: |
cotigac | 19:71b793021c78 | 92 | printf("CcaCnf: \r\n"); |
cotigac | 19:71b793021c78 | 93 | //(void)OSA_EventSet(&gTaskEvent, gMlme_CcaCnf_EVENT_c); |
cotigac | 19:71b793021c78 | 94 | break; |
cotigac | 19:71b793021c78 | 95 | case gMlmeTimeoutInd_c: |
cotigac | 19:71b793021c78 | 96 | printf("MlmeTimeoutInd: \r\n"); |
cotigac | 19:71b793021c78 | 97 | //(void)OSA_EventSet(&gTaskEvent, gMlme_TimeoutInd_EVENT_c); |
cotigac | 19:71b793021c78 | 98 | break; |
cotigac | 19:71b793021c78 | 99 | default: |
cotigac | 19:71b793021c78 | 100 | break; |
cotigac | 19:71b793021c78 | 101 | } |
cotigac | 19:71b793021c78 | 102 | MEM_BufferFree(pMsg); |
cotigac | 19:71b793021c78 | 103 | return gErrorNoError_c; |
cotigac | 19:71b793021c78 | 104 | } |
cotigac | 19:71b793021c78 | 105 | |
cotigac | 19:71b793021c78 | 106 | //(Data) Sap handler for managing data confirm and data indication |
cotigac | 19:71b793021c78 | 107 | smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance) |
cotigac | 19:71b793021c78 | 108 | { |
cotigac | 19:71b793021c78 | 109 | switch(pMsg->msgType) |
cotigac | 19:71b793021c78 | 110 | { |
cotigac | 19:71b793021c78 | 111 | case gMcpsDataInd_c: |
cotigac | 19:71b793021c78 | 112 | if(pMsg->msgData.dataInd.pRxPacket->rxStatus == rxSuccessStatus_c) |
cotigac | 19:71b793021c78 | 113 | { |
cotigac | 19:71b793021c78 | 114 | printf("McpsDataInd: \r\n"); |
cotigac | 19:71b793021c78 | 115 | //(void)OSA_EventSet(&gTaskEvent, gMcps_Ind_EVENT_c); |
cotigac | 19:71b793021c78 | 116 | } |
cotigac | 19:71b793021c78 | 117 | break; |
cotigac | 19:71b793021c78 | 118 | case gMcpsDataCnf_c: |
cotigac | 19:71b793021c78 | 119 | #ifdef VERBOSE |
cotigac | 19:71b793021c78 | 120 | if(pMsg->msgData.dataCnf.status == gErrorChannelBusy_c) |
cotigac | 19:71b793021c78 | 121 | { |
cotigac | 19:71b793021c78 | 122 | bCCAFailed = TRUE; |
cotigac | 19:71b793021c78 | 123 | } |
cotigac | 19:71b793021c78 | 124 | if(pMsg->msgData.dataCnf.status == gErrorNoAck_c) |
cotigac | 19:71b793021c78 | 125 | { |
cotigac | 19:71b793021c78 | 126 | bACKFailed = TRUE; |
cotigac | 19:71b793021c78 | 127 | } |
cotigac | 19:71b793021c78 | 128 | #endif |
cotigac | 19:71b793021c78 | 129 | printf("McpsDataCnf: \r\n"); |
cotigac | 19:71b793021c78 | 130 | //(void)OSA_EventSet(&gTaskEvent, gMcps_Cnf_EVENT_c); |
cotigac | 19:71b793021c78 | 131 | break; |
cotigac | 19:71b793021c78 | 132 | default: |
cotigac | 19:71b793021c78 | 133 | break; |
cotigac | 19:71b793021c78 | 134 | } |
cotigac | 19:71b793021c78 | 135 | MEM_BufferFree(pMsg); |
cotigac | 19:71b793021c78 | 136 | return gErrorNoError_c; |
cotigac | 19:71b793021c78 | 137 | } |
cotigac | 19:71b793021c78 | 138 | |
cotigac | 19:71b793021c78 | 139 | void InitProject(void) |
cotigac | 19:71b793021c78 | 140 | { |
cotigac | 19:71b793021c78 | 141 | /*Global Data init*/ |
cotigac | 19:71b793021c78 | 142 | #ifdef VERBOSE |
cotigac | 19:71b793021c78 | 143 | bACKFailed = FALSE; |
cotigac | 19:71b793021c78 | 144 | bCCAFailed = FALSE; |
cotigac | 19:71b793021c78 | 145 | #endif |
cotigac | 19:71b793021c78 | 146 | bTxInProgress = FALSE; |
cotigac | 19:71b793021c78 | 147 | bTxDone = FALSE; |
cotigac | 19:71b793021c78 | 148 | evKeyPressed = FALSE; |
cotigac | 19:71b793021c78 | 149 | txConfigContext.autoAck = FALSE; |
cotigac | 19:71b793021c78 | 150 | txConfigContext.ccaBeforeTx = FALSE; |
cotigac | 19:71b793021c78 | 151 | txConfigContext.retryCountAckFail = 0; |
cotigac | 19:71b793021c78 | 152 | txConfigContext.retryCountCCAFail = 0; |
cotigac | 19:71b793021c78 | 153 | } |