rework CAN

This commit is contained in:
Rémi Heredero 2023-09-04 08:43:14 +02:00
commit c846a12edb
4 changed files with 159 additions and 17 deletions

View File

@ -29,7 +29,7 @@ typedef union {
uint16_t full; uint16_t full;
} BYTES_2; } BYTES_2;
void CM_processIncome(uint8_t idSender, uint8_t idMsg, uint32_t data){ void CM_processIncome(uint8_t idSender, uint8_t idMsg, bool rtr, uint32_t data){
BYTES_4 incomeData; BYTES_4 incomeData;
incomeData.full = data; incomeData.full = data;
BYTES_4 revertData; BYTES_4 revertData;
@ -59,21 +59,21 @@ void CM_processIncome(uint8_t idSender, uint8_t idMsg, uint32_t data){
if(idMsg == 0x1) { // CONTROL_SPEED_FACTOR if(idMsg == 0x1) { // CONTROL_SPEED_FACTOR
// valHH valH valL valLL // valHH valH valL valLL
KART_CST.CONTROL_SPEED_FACTOR = data; KART_CST.CONTROL_SPEED_FACTOR = revertData.full;
MEM_write_4_byte(MEMADD_CONTROL_SPEED_FACTOR, KART_CST.CONTROL_SPEED_FACTOR); MEM_write_4_byte(MEMADD_CONTROL_SPEED_FACTOR, KART_CST.CONTROL_SPEED_FACTOR);
} }
if(idMsg == 0x2) { // CONTROL_POWER_FACTOR if(idMsg == 0x2) { // CONTROL_POWER_FACTOR
// valHH valH valL valLL // valHH valH valL valLL
KART_CST.CONTROL_POWER_FACTOR = data; KART_CST.CONTROL_POWER_FACTOR = revertData.full;
MEM_write_4_byte(MEMADD_CONTROL_POWER_FACTOR, KART_CST.CONTROL_POWER_FACTOR); MEM_write_4_byte(MEMADD_CONTROL_POWER_FACTOR, KART_CST.CONTROL_POWER_FACTOR);
CAN_Send(0, 5, KART_CST.CONTROL_POWER_FACTOR); CAN_Send(0, 5, KART_CST.CONTROL_POWER_FACTOR);
} }
if(idMsg == 0x3) { // CONTROL_STEERING_FACTOR if(idMsg == 0x3) { // CONTROL_STEERING_FACTOR
// valHH valH valL valLL // valHH valH valL valLL
KART_CST.CONTROL_STEERING_FACTOR = data; KART_CST.CONTROL_STEERING_FACTOR = revertData.full;
MEM_write_4_byte(MEMADD_CONTROL_STEERING_FACTOR, KART_CST.CONTROL_STEERING_FACTOR); MEM_write_4_byte(MEMADD_CONTROL_STEERING_FACTOR, KART_CST.CONTROL_STEERING_FACTOR);
} }

View File

@ -32,7 +32,7 @@ S R M
* @param idMsg is of the message * @param idMsg is of the message
* @param data data of the message * @param data data of the message
*/ */
void CM_processIncome(uint8_t idSender, uint8_t idMsg, uint32_t data); void CM_processIncome(uint8_t idSender, uint8_t idMsg, bool rtr, uint32_t data);
/** /**
* Send alive message from controller * Send alive message from controller

View File

@ -1,5 +1,5 @@
/** /**
* @author Rémi Heredero * @author R<EFBFBD>mi Heredero
* @version 1.0.0 * @version 1.0.0
* @date August 2023 * @date August 2023
* @file can_interface.c * @file can_interface.c
@ -54,6 +54,11 @@ bool CAN_processEvent(Event* ev) {
uint64_t data = Event_getData(ev); uint64_t data = Event_getData(ev);
CAN_4_BYTES tmpData; CAN_4_BYTES tmpData;
uCAN_MSG canMsg;
uint32_t canData = (uint32_t) data;
uint8_t idMsg;
uint8_t idRecipient;
uint8_t idSender;
switch (me->state) { // onState switch (me->state) { // onState
@ -68,25 +73,40 @@ bool CAN_processEvent(Event* ev) {
// New message arrive // New message arrive
if (ev->id == evCAnewMsg) { if (ev->id == evCAnewMsg) {
if (me->receiveCan != NULL) { if (me->receiveCan != NULL) {
uint32_t canData = (uint32_t) data;
data = data>>32; data = data>>32;
tmpData.full = data; tmpData.full = data;
uint8_t idMsg = tmpData.separate.byte0; idMsg = tmpData.separate.byte0;
idMsg = idMsg >> 4; idMsg = idMsg >> 4;
idMsg = idMsg & 0xF; idMsg = idMsg & 0xF;
uint8_t idRecipient = tmpData.separate.byte1; idRecipient = tmpData.separate.byte1;
idRecipient = idRecipient & 0xF; idRecipient = idRecipient & 0xF;
uint8_t idSender = tmpData.separate.byte1; idSender = tmpData.separate.byte1;
idSender = idSender >> 4; idSender = idSender >> 4;
me->receiveCan(idSender, idMsg, canData); me->receiveCan(idSender, idMsg, false, canData);
}
}
if (ev->id == evCAnewRTR) {
if (me->receiveCan != NULL) {
data = data>>32;
tmpData.full = data;
idMsg = tmpData.separate.byte0;
idMsg = idMsg >> 4;
idMsg = idMsg & 0xF;
idRecipient = tmpData.separate.byte1;
idRecipient = idRecipient & 0xF;
idSender = tmpData.separate.byte1;
idSender = idSender >> 4;
me->receiveCan(idSender, idMsg, true, canData);
} }
} }
// Send a message // Send a message
if (ev->id == evCAsend) { if (ev->id == evCAsend4) {
uCAN_MSG canMsg;
canMsg.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B; // standard canMsg.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B; // standard
canMsg.frame.dlc = 4; // 4 bytes to send canMsg.frame.dlc = 4; // 4 bytes to send
canMsg.frame.rtr = 0; // no remote frame canMsg.frame.rtr = 0; // no remote frame
@ -101,6 +121,70 @@ bool CAN_processEvent(Event* ev) {
canMsg.frame.id = (uint32_t) data; canMsg.frame.id = (uint32_t) data;
CAN_transmit(&canMsg); CAN_transmit(&canMsg);
} }
if (ev->id == evCAsend2) {
canMsg.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B; // standard
canMsg.frame.dlc = 2; // 4 bytes to send
canMsg.frame.rtr = 0; // no remote frame
canMsg.frame.data0 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data1 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data2 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data3 = (uint8_t) data;
data = data >> 8;
canMsg.frame.id = (uint32_t) data;
CAN_transmit(&canMsg);
}
if (ev->id == evCAsend1) {
canMsg.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B; // standard
canMsg.frame.dlc = 1; // 4 bytes to send
canMsg.frame.rtr = 0; // no remote frame
canMsg.frame.data0 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data1 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data2 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data3 = (uint8_t) data;
data = data >> 8;
canMsg.frame.id = (uint32_t) data;
CAN_transmit(&canMsg);
}
if (ev->id == evCAsend0) {
canMsg.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B; // standard
canMsg.frame.dlc = 0; // 4 bytes to send
canMsg.frame.rtr = 0; // no remote frame
canMsg.frame.data0 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data1 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data2 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data3 = (uint8_t) data;
data = data >> 8;
canMsg.frame.id = (uint32_t) data;
CAN_transmit(&canMsg);
}
if (ev->id == evCAsendRTR) {
canMsg.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B; // standard
canMsg.frame.dlc = 0; // 4 bytes to send
canMsg.frame.rtr = 1; // no remote frame
canMsg.frame.data0 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data1 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data2 = (uint8_t) data;
data = data >> 8;
canMsg.frame.data3 = (uint8_t) data;
data = data >> 8;
canMsg.frame.id = (uint32_t) data;
CAN_transmit(&canMsg);
}
break; break;
} }
@ -151,15 +235,62 @@ void CAN_newMsg() {
data = data | canMsg.frame.data1; data = data | canMsg.frame.data1;
data = data<<8; data = data<<8;
data = data | canMsg.frame.data0; data = data | canMsg.frame.data0;
if(canMsg.frame.rtr) {
POST(&CAN_myself, &CAN_processEvent, evCAnewRTR, 0, data);
} else {
POST(&CAN_myself, &CAN_processEvent, evCAnewMsg, 0, data); POST(&CAN_myself, &CAN_processEvent, evCAnewMsg, 0, data);
} }
}
void CAN_Send(uint8_t idRecipient, uint8_t idMsg, uint32_t data) { void CAN_Send(uint8_t idRecipient, uint8_t idMsg, uint32_t data) {
uint64_t tmpData = CAN_myself.sender; uint64_t tmpData = CAN_myself.sender;
tmpData = (tmpData<<4) | idRecipient; tmpData = (tmpData<<4) | idRecipient;
tmpData = (tmpData<<4) | idMsg; tmpData = (tmpData<<4) | idMsg;
tmpData = (tmpData<<32) | data; tmpData = (tmpData<<32) | data;
POST(&CAN_myself, &CAN_processEvent, evCAsend, 0, tmpData); POST(&CAN_myself, &CAN_processEvent, evCAsend4, 0, tmpData);
}
void CAN_send_4_bytes(uint8_t idRecipient, uint8_t idMsg, uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3) {
uint64_t tmpData = CAN_myself.sender;
tmpData = (tmpData<<4) | idRecipient;
tmpData = (tmpData<<4) | idMsg;
tmpData = (tmpData<<8) | byte3;
tmpData = (tmpData<<8) | byte2;
tmpData = (tmpData<<8) | byte1;
tmpData = (tmpData<<8) | byte0;
POST(&CAN_myself, &CAN_processEvent, evCAsend4, 0, tmpData);
}
void CAN_send_2_bytes(uint8_t idRecipient, uint8_t idMsg, uint16_t data) {
uint64_t tmpData = CAN_myself.sender;
tmpData = (tmpData<<4) | idRecipient;
tmpData = (tmpData<<4) | idMsg;
tmpData = (tmpData<<32) | data;
POST(&CAN_myself, &CAN_processEvent, evCAsend2, 0, tmpData);
}
void CAN_send_1_byte(uint8_t idRecipient, uint8_t idMsg, uint8_t data) {
uint64_t tmpData = CAN_myself.sender;
tmpData = (tmpData<<4) | idRecipient;
tmpData = (tmpData<<4) | idMsg;
tmpData = (tmpData<<32) | data;
POST(&CAN_myself, &CAN_processEvent, evCAsend1, 0, tmpData);
}
void CAN_send_0_byte(uint8_t idRecipient, uint8_t idMsg) {
uint64_t tmpData = CAN_myself.sender;
tmpData = (tmpData<<4) | idRecipient;
tmpData = (tmpData<<4) | idMsg;
tmpData = tmpData<<32;
POST(&CAN_myself, &CAN_processEvent, evCAsend0, 0, tmpData);
}
void CAN_send_rtr(uint8_t idRecipient, uint8_t idMsg) {
uint64_t tmpData = CAN_myself.sender;
tmpData = (tmpData<<4) | idRecipient;
tmpData = (tmpData<<4) | idMsg;
tmpData = tmpData<<32;
POST(&CAN_myself, &CAN_processEvent, evCAsendRTR, 0, tmpData);
} }
/*********** /***********

View File

@ -17,10 +17,15 @@ typedef enum {
typedef enum { typedef enum {
evCAinit = 10, evCAinit = 10,
evCAnewMsg, evCAnewMsg,
evCAsend evCAnewRTR,
evCAsend4,
evCAsend2,
evCAsend1,
evCAsend0,
evCAsendRTR
} CAN_EVENTS; } CAN_EVENTS;
typedef void (*CAN_CALLBACK)(uint8_t, uint8_t, uint32_t); typedef void (*CAN_CALLBACK)(uint8_t, uint8_t, bool, uint32_t);
typedef struct { typedef struct {
CAN_STATES state; CAN_STATES state;
@ -78,6 +83,12 @@ void CAN_newMsg();
*/ */
void CAN_Send(uint8_t idRecipient, uint8_t idMsg, uint32_t data); void CAN_Send(uint8_t idRecipient, uint8_t idMsg, uint32_t data);
void CAN_send_4_bytes(uint8_t idRecipient, uint8_t idMsg, uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3);
void CAN_send_2_bytes(uint8_t idRecipient, uint8_t idMsg, uint16_t data);
void CAN_send_1_byte(uint8_t idRecipient, uint8_t idMsg, uint8_t data);
void CAN_send_0_byte(uint8_t idRecipient, uint8_t idMsg);
void CAN_send_rtr(uint8_t idRecipient, uint8_t idMsg);
/*********** /***********
* SETTERS * * SETTERS *
***********/ ***********/