1
0

finish receiver

This commit is contained in:
Rémi Heredero 2024-04-20 18:44:37 +02:00
parent 964e9247c4
commit b93c51f369
6 changed files with 270 additions and 18 deletions

View File

@ -1,5 +1,203 @@
void MacReceiver(void *argument) #include "main.h"
{ #include <cassert>
// TODO #include <cstdint>
#include <stdint.h>
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;
}
}
} }

View File

@ -17,9 +17,9 @@ void MacSender(void *argument) {
for(;;) { for(;;) {
//---------------------------------------------------------------------------- //--------------------------------------------------------------------------
// QUEUE READ // QUEUE READ
//---------------------------------------------------------------------------- //--------------------------------------------------------------------------
retCode = osMessageQueueGet( retCode = osMessageQueueGet(
queue_macS_id, queue_macS_id,
&queueMsg, &queueMsg,
@ -31,6 +31,9 @@ void MacSender(void *argument) {
switch(queueMsg.type) { switch(queueMsg.type) {
//----------------------------------------------------------------------
// TOKEN MESSAGE
//----------------------------------------------------------------------
case TOKEN: { case TOKEN: {
// Get token and save it // Get token and save it
memcpy(lastToken, msg, TOKENSIZE-2); memcpy(lastToken, msg, TOKENSIZE-2);
@ -80,13 +83,17 @@ void MacSender(void *argument) {
break; break;
} }
//----------------------------------------------------------------------
// DATABACK MESSAGE
//----------------------------------------------------------------------
case DATABACK: { case DATABACK: {
break; break;
} }
//----------------------------------------------------------------------
// NEW TOKEN MESSAGE
//----------------------------------------------------------------------
case NEW_TOKEN: { case NEW_TOKEN: {
lastToken[0] = TOKEN_TAG; lastToken[0] = TOKEN_TAG;
@ -108,27 +115,34 @@ void MacSender(void *argument) {
break; break;
} }
//----------------------------------------------------------------------
// START MESSAGE
//----------------------------------------------------------------------
case START: { case START: {
// Do nothing, don't care to receive start gTokenInterface.connected = true;
break; break;
} }
//----------------------------------------------------------------------
// STOP MESSAGE
//----------------------------------------------------------------------
case STOP: { case STOP: {
// Do nothing, don't care to receive stop gTokenInterface.connected = false;
break; break;
} }
//----------------------------------------------------------------------
// DATA MESSAGE
//----------------------------------------------------------------------
case DATA_IND: { case DATA_IND: {
break; break;
} }
//----------------------------------------------------------------------
// DEFAULT - TBD
//----------------------------------------------------------------------
default: { default: {
break; break;
} }
} }

14
main.c
View File

@ -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 /// \brief Configure the clock @ 216MHz and peripheral clocks
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////

1
main.h
View File

@ -56,6 +56,7 @@ extern osEventFlagsId_t eventFlag_id;
void CheckRetCode(uint32_t retCode,uint32_t lineNumber,char * fileName,uint8_t mode); void CheckRetCode(uint32_t retCode,uint32_t lineNumber,char * fileName,uint8_t mode);
void DebugFrame(char * stringP); void DebugFrame(char * stringP);
void DebugMacFrame(uint8_t preChar,uint8_t * stringP); void DebugMacFrame(uint8_t preChar,uint8_t * stringP);
uint8_t Checksum(uint8_t* frame);
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// structure for system usage // structure for system usage

View File

@ -145,7 +145,7 @@
<SetRegEntry> <SetRegEntry>
<Number>0</Number> <Number>0</Number>
<Key>ST-LINKIII-KEIL_SWO</Key> <Key>ST-LINKIII-KEIL_SWO</Key>
<Name>-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)</Name> <Name>-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</Name>
</SetRegEntry> </SetRegEntry>
<SetRegEntry> <SetRegEntry>
<Number>0</Number> <Number>0</Number>
@ -160,6 +160,21 @@
<WinNumber>1</WinNumber> <WinNumber>1</WinNumber>
<ItemText>sai</ItemText> <ItemText>sai</ItemText>
</Ww> </Ww>
<Ww>
<count>1</count>
<WinNumber>1</WinNumber>
<ItemText>gTokenInterface</ItemText>
</Ww>
<Ww>
<count>2</count>
<WinNumber>1</WinNumber>
<ItemText>t</ItemText>
</Ww>
<Ww>
<count>3</count>
<WinNumber>1</WinNumber>
<ItemText>lastToken</ItemText>
</Ww>
</WatchWindow1> </WatchWindow1>
<MemoryWindow1> <MemoryWindow1>
<Mm> <Mm>
@ -178,12 +193,12 @@
</Mm> </Mm>
</MemoryWindow3> </MemoryWindow3>
<ScvdPack> <ScvdPack>
<Filename>C:\Keil_v5\ARM\CMSIS\5.9.0\CMSIS\RTOS2\RTX\RTX5.scvd</Filename> <Filename>C:\Users\remi\AppData\Local\Arm\Packs\ARM\CMSIS\5.9.0\CMSIS\RTOS2\RTX\RTX5.scvd</Filename>
<Type>ARM.CMSIS.5.9.0</Type> <Type>ARM.CMSIS.5.9.0</Type>
<SubType>1</SubType> <SubType>1</SubType>
</ScvdPack> </ScvdPack>
<ScvdPack> <ScvdPack>
<Filename>C:\Keil_v5\Keil\ARM_Compiler\1.7.2\EventRecorder.scvd</Filename> <Filename>C:\Users\remi\AppData\Local\Arm\Packs\Keil\ARM_Compiler\1.7.2\EventRecorder.scvd</Filename>
<Type>Keil.ARM_Compiler.1.7.2</Type> <Type>Keil.ARM_Compiler.1.7.2</Type>
<SubType>1</SubType> <SubType>1</SubType>
</ScvdPack> </ScvdPack>

View File

@ -10,7 +10,7 @@
<TargetName>Target 1</TargetName> <TargetName>Target 1</TargetName>
<ToolsetNumber>0x4</ToolsetNumber> <ToolsetNumber>0x4</ToolsetNumber>
<ToolsetName>ARM-ADS</ToolsetName> <ToolsetName>ARM-ADS</ToolsetName>
<pCCUsed>5060960::V5.06 update 7 (build 960)::.\ARMCC</pCCUsed> <pCCUsed>5060960::V5.06 update 7 (build 960)::C:\Program Files (x86)\ARM_Compiler_5.06u7</pCCUsed>
<uAC6>0</uAC6> <uAC6>0</uAC6>
<TargetOption> <TargetOption>
<TargetCommonOption> <TargetCommonOption>
@ -186,6 +186,7 @@
<RvdsVP>2</RvdsVP> <RvdsVP>2</RvdsVP>
<RvdsMve>0</RvdsMve> <RvdsMve>0</RvdsMve>
<RvdsCdeCp>0</RvdsCdeCp> <RvdsCdeCp>0</RvdsCdeCp>
<nBranchProt>0</nBranchProt>
<hadIRAM2>1</hadIRAM2> <hadIRAM2>1</hadIRAM2>
<hadIROM2>1</hadIROM2> <hadIROM2>1</hadIROM2>
<StupSel>8</StupSel> <StupSel>8</StupSel>
@ -864,4 +865,13 @@
</files> </files>
</RTE> </RTE>
<LayerInfo>
<Layers>
<Layer>
<LayName>tokenring_project</LayName>
<LayPrjMark>1</LayPrjMark>
</Layer>
</Layers>
</LayerInfo>
</Project> </Project>