From b93c51f369ca2b24b25e7412b91627c0204f9873 Mon Sep 17 00:00:00 2001 From: Klagarge Date: Sat, 20 Apr 2024 18:44:37 +0200 Subject: [PATCH] finish receiver --- mac_receiver.c | 204 +++++++++++++++++++++++++++++++++++++- mac_sender.c | 36 +++++-- main.c | 14 +++ main.h | 1 + tokenring_project.uvoptx | 21 +++- tokenring_project.uvprojx | 12 ++- 6 files changed, 270 insertions(+), 18 deletions(-) diff --git a/mac_receiver.c b/mac_receiver.c index a554070..6b7ffde 100644 --- a/mac_receiver.c +++ b/mac_receiver.c @@ -1,5 +1,203 @@ -void MacReceiver(void *argument) -{ - // TODO +#include "main.h" +#include +#include +#include + + +typedef union { + struct { + uint8_t sapi: 3; // MSB + uint8_t addr: 4; + uint8_t nothing: 1; // LSB + }; + uint8_t raw; +} Adresse; + +typedef union { + struct { + uint8_t ack: 1; // MSB + uint8_t read: 1; + uint8_t checksum: 6; // LSB + }; + uint8_t raw; +} Status; + +void send_DATA_IND(Adresse source, Adresse destination, uint8_t* dataFramePtr) { + struct queueMsg_t queueMsg; // queue message + osStatus_t retCode; // return error code + char* strPtr; + + queueMsg.type = DATA_IND; + queueMsg.addr = source.addr; + queueMsg.sapi = source.sapi; + + strPtr = osMemoryPoolAlloc(memPool, osWaitForever); + + for(uint8_t i = 0; i < dataFramePtr[2]; i++) { + strPtr[i] = (char)dataFramePtr[3+i]; + } + strPtr[dataFramePtr[2]] = '\0'; // null-terminate string + queueMsg.anyPtr = strPtr; + + switch (destination.sapi) { + case TIME_SAPI: + retCode = osMessageQueuePut( + queue_timeR_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + break; + case CHAT_SAPI: + retCode = osMessageQueuePut( + queue_chatR_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + break; + default: + assert(false); + } +} + +void send_DATABACK(Adresse source, Adresse destination, uint8_t* dataFramePtr) { + struct queueMsg_t queueMsg; // queue message + osStatus_t retCode; // return error code + + queueMsg.type = DATABACK; + queueMsg.anyPtr = dataFramePtr; + queueMsg.addr = source.addr; + queueMsg.sapi = source.sapi; + + + retCode = osMessageQueuePut( + queue_macS_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + +} + + +void MacReceiver(void *argument) { + struct queueMsg_t queueMsg; // queue message + Adresse src; + Adresse dst; + uint8_t length; + Status status; + uint8_t* msg; + osStatus_t retCode; // return error code + + for(;;) { + //-------------------------------------------------------------------------- + // QUEUE READ + //-------------------------------------------------------------------------- + { + retCode = osMessageQueueGet( + queue_macR_id, + &queueMsg, + NULL, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + + msg = queueMsg.anyPtr; + } + switch (queueMsg.type) { + + //---------------------------------------------------------------------- + // MESSAGE FROM PHY + //---------------------------------------------------------------------- + case FROM_PHY: + if(msg[0] == TOKEN_TAG) { + //-------------------------------------------------------------- + // TOKEN + //-------------------------------------------------------------- + queueMsg.type = TOKEN; + retCode = osMessageQueuePut( + queue_macS_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + + } else { + //-------------------------------------------------------------- + // MESSAGE + //-------------------------------------------------------------- + src.raw = msg[0]; + dst.raw = msg[1]; + length = msg[2]; + status.raw = msg[3+length]; + + //-------------------------------------------------------------- + // MESSAGE FOR ME (or broadcast) + //-------------------------------------------------------------- + if( dst.addr == gTokenInterface.myAddress || + dst.addr == BROADCAST_ADDRESS ) { + + if((Checksum(msg) & 0x3F) == status.checksum) { + status.ack = 1; + + if(dst.sapi == CHAT_SAPI && gTokenInterface.connected || + dst.sapi == TIME_SAPI && gTokenInterface.broadcastTime) { + // Send to Time or Chat ---------------------------- + send_DATA_IND(src, dst, queueMsg.anyPtr); + status.read = 1; + } else { + status.read = 0; + } + msg[3+length] = status.raw; + + if(src.addr == gTokenInterface.myAddress) { // For me, from me + // Send DATABACK ----------------------------------- + send_DATABACK(src, dst, queueMsg.anyPtr); + } else { + // Send to PHY ------------------------------------- + queueMsg.type = TO_PHY; + retCode = osMessageQueuePut( + queue_phyS_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + } + + } else { // for me but bad checksum + status.ack = 0; + status.read = 0; + msg[3+length] = status.raw; + send_DATABACK(src, dst, queueMsg.anyPtr); + } + + //-------------------------------------------------------------- + // MESSAGE FOR SOMEONE ELSE + //-------------------------------------------------------------- + } else if(src.addr == gTokenInterface.myAddress) { + // MESSAGE FROM ME ----------------------------------------- + send_DATABACK(src, dst, queueMsg.anyPtr); + } else { + // MESSAGE FROM SOMEONE ELSE ------------------------------- + queueMsg.type = TO_PHY; + retCode = osMessageQueuePut( + queue_phyS_id, + &queueMsg, + osPriorityNormal, + osWaitForever); + CheckRetCode(retCode, __LINE__, __FILE__, CONTINUE); + } + + } + break; + + //---------------------------------------------------------------------- + // DEFAULT - TBD + //---------------------------------------------------------------------- + default: + break; + } + } + } diff --git a/mac_sender.c b/mac_sender.c index 6c1376f..4f669d4 100644 --- a/mac_sender.c +++ b/mac_sender.c @@ -17,9 +17,9 @@ void MacSender(void *argument) { for(;;) { - //---------------------------------------------------------------------------- + //-------------------------------------------------------------------------- // QUEUE READ - //---------------------------------------------------------------------------- + //-------------------------------------------------------------------------- retCode = osMessageQueueGet( queue_macS_id, &queueMsg, @@ -31,6 +31,9 @@ void MacSender(void *argument) { switch(queueMsg.type) { + //---------------------------------------------------------------------- + // TOKEN MESSAGE + //---------------------------------------------------------------------- case TOKEN: { // Get token and save it memcpy(lastToken, msg, TOKENSIZE-2); @@ -80,13 +83,17 @@ void MacSender(void *argument) { break; } - + //---------------------------------------------------------------------- + // DATABACK MESSAGE + //---------------------------------------------------------------------- case DATABACK: { break; } - + //---------------------------------------------------------------------- + // NEW TOKEN MESSAGE + //---------------------------------------------------------------------- case NEW_TOKEN: { lastToken[0] = TOKEN_TAG; @@ -108,27 +115,34 @@ void MacSender(void *argument) { break; } - + //---------------------------------------------------------------------- + // START MESSAGE + //---------------------------------------------------------------------- case START: { - // Do nothing, don't care to receive start + gTokenInterface.connected = true; break; } - + //---------------------------------------------------------------------- + // STOP MESSAGE + //---------------------------------------------------------------------- case STOP: { - // Do nothing, don't care to receive stop + gTokenInterface.connected = false; break; } - + //---------------------------------------------------------------------- + // DATA MESSAGE + //---------------------------------------------------------------------- case DATA_IND: { break; } - + //---------------------------------------------------------------------- + // DEFAULT - TBD + //---------------------------------------------------------------------- default: { - break; } } diff --git a/main.c b/main.c index d2c65a5..fc7af14 100644 --- a/main.c +++ b/main.c @@ -286,6 +286,20 @@ void CheckRetCode(uint32_t retCode,uint32_t lineNumber,char * fileName,uint8_t m } } +////////////////////////////////////////////////////////////////////////////////// +/// \brief Calculate the checksum of a frame +/// \param frame pointer to the frame to calculate the checksum +/// \return the checksum +////////////////////////////////////////////////////////////////////////////////// +uint8_t Checksum(uint8_t * frame) { + uint8_t checksum = 0; + uint8_t length = frame[2]; + for (uint8_t i = 0; i < length+3; i++) { + checksum = checksum + frame[i]; + } + return checksum; +} + ////////////////////////////////////////////////////////////////////////////////// /// \brief Configure the clock @ 216MHz and peripheral clocks ////////////////////////////////////////////////////////////////////////////////// diff --git a/main.h b/main.h index 0d4eaec..d2711e3 100644 --- a/main.h +++ b/main.h @@ -56,6 +56,7 @@ extern osEventFlagsId_t eventFlag_id; void CheckRetCode(uint32_t retCode,uint32_t lineNumber,char * fileName,uint8_t mode); void DebugFrame(char * stringP); void DebugMacFrame(uint8_t preChar,uint8_t * stringP); +uint8_t Checksum(uint8_t* frame); //-------------------------------------------------------------------------------- // structure for system usage diff --git a/tokenring_project.uvoptx b/tokenring_project.uvoptx index fe1731d..81e7b16 100644 --- a/tokenring_project.uvoptx +++ b/tokenring_project.uvoptx @@ -145,7 +145,7 @@ 0 ST-LINKIII-KEIL_SWO - -U0671FF485057775187193017 -O8398 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P2 -N00("ARM CoreSight SW-DP (ARM Core") -D00(5BA02477) -L00(0) -TO131075 -TC216000000 -TT216000000 -TP21 -TDS806B -TDT0 -TDC1F -TIE1 -TIP1 -FO7 -FD20010000 -FC1000 -FN1 -FF0STM32F7x_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F746NGHx$CMSIS\Flash\STM32F7x_1024.FLM) + -U066DFF485153826687131237 -O8398 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P2 -N00("ARM CoreSight SW-DP (ARM Core") -D00(5BA02477) -L00(0) -TO131075 -TC216000000 -TT216000000 -TP21 -TDS806B -TDT0 -TDC1F -TIE1 -TIP1 -FO7 -FD20010000 -FC1000 -FN1 -FF0STM32F7x_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F746NGHx$CMSIS\Flash\STM32F7x_1024.FLM) -WA0 -WE0 -WVCE4 -WS2710 -WM0 -WP2 0 @@ -160,6 +160,21 @@ 1 sai + + 1 + 1 + gTokenInterface + + + 2 + 1 + t + + + 3 + 1 + lastToken + @@ -178,12 +193,12 @@ - C:\Keil_v5\ARM\CMSIS\5.9.0\CMSIS\RTOS2\RTX\RTX5.scvd + C:\Users\remi\AppData\Local\Arm\Packs\ARM\CMSIS\5.9.0\CMSIS\RTOS2\RTX\RTX5.scvd ARM.CMSIS.5.9.0 1 - C:\Keil_v5\Keil\ARM_Compiler\1.7.2\EventRecorder.scvd + C:\Users\remi\AppData\Local\Arm\Packs\Keil\ARM_Compiler\1.7.2\EventRecorder.scvd Keil.ARM_Compiler.1.7.2 1 diff --git a/tokenring_project.uvprojx b/tokenring_project.uvprojx index d06db64..6099233 100644 --- a/tokenring_project.uvprojx +++ b/tokenring_project.uvprojx @@ -10,7 +10,7 @@ Target 1 0x4 ARM-ADS - 5060960::V5.06 update 7 (build 960)::.\ARMCC + 5060960::V5.06 update 7 (build 960)::C:\Program Files (x86)\ARM_Compiler_5.06u7 0 @@ -186,6 +186,7 @@ 2 0 0 + 0 1 1 8 @@ -864,4 +865,13 @@ + + + + tokenring_project + 1 + + + +