add send for 4,1 or 0 bytes

This commit is contained in:
Rémi Heredero 2023-08-30 16:19:42 +02:00
parent 7550d1907b
commit 4987fbabd0
2 changed files with 104 additions and 7 deletions

View File

@ -34,9 +34,7 @@ typedef union {
uint8_t byte2;
uint8_t byte3;
} separate;
struct {
uint32_t bytes;
} full;
uint32_t full;
} CAN_4_BYTES;
void CAN_init(){
@ -73,7 +71,7 @@ bool CAN_processEvent(Event* ev) {
data = data>>32;
CAN_4_BYTES tmpData;
tmpData.full.bytes = data;
tmpData.full = data;
uint8_t idMsg = tmpData.separate.byte0;
idMsg = idMsg >> 4;
idMsg = idMsg & 0xF;
@ -87,7 +85,7 @@ bool CAN_processEvent(Event* ev) {
}
// 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.dlc = 4; // 4 bytes to send
@ -103,6 +101,57 @@ bool CAN_processEvent(Event* ev) {
canMsg.frame.id = (uint32_t) data;
CAN_transmit(&canMsg);
}
if (ev->id == evCAsend2) {
uCAN_MSG canMsg;
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) {
uCAN_MSG canMsg;
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) {
uCAN_MSG canMsg;
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);
}
break;
}
@ -161,7 +210,47 @@ void CAN_Send(uint8_t idRecipient, uint8_t idMsg, uint32_t data) {
tmpData = (tmpData<<4) | idRecipient;
tmpData = (tmpData<<4) | idMsg;
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) {
CAN_MESSAGE canMsg;
canMsg.separate.sender = CAN_myself.sender;
canMsg.separate.recipient = idRecipient;
canMsg.separate.message = idMsg;
canMsg.separate.data0 = byte0;
canMsg.separate.data1 = byte1;
canMsg.separate.data2 = byte2;
canMsg.separate.data0 = byte3;
POST(&CAN_myself, &CAN_processEvent, evCAsend4, 0, canMsg);
}
void CAN_send_16_bytes(uint8_t idRecipient, uint8_t idMsg, uint16_t data) {
// BYTES_4
// TODO finish
CAN_MESSAGE canMsg;
canMsg.separate.sender = CAN_myself.sender;
canMsg.separate.recipient = idRecipient;
canMsg.separate.message = idMsg;
canMsg.separate.data0 = data;
POST(&CAN_myself, &CAN_processEvent, evCAsend1, 0, canMsg);
}
void CAN_send_1_byte(uint8_t idRecipient, uint8_t idMsg, uint8_t data) {
CAN_MESSAGE canMsg;
canMsg.separate.sender = CAN_myself.sender;
canMsg.separate.recipient = idRecipient;
canMsg.separate.message = idMsg;
canMsg.separate.data0 = data;
POST(&CAN_myself, &CAN_processEvent, evCAsend1, 0, canMsg);
}
void CAN_send_0_byte(uint8_t idRecipient, uint8_t idMsg) {
CAN_MESSAGE canMsg;
canMsg.separate.sender = CAN_myself.sender;
canMsg.separate.recipient = idRecipient;
canMsg.separate.message = idMsg;
POST(&CAN_myself, &CAN_processEvent, evCAsend0, 0, canMsg);
}
/***********

View File

@ -17,7 +17,10 @@ typedef enum {
typedef enum {
evCAinit = 10,
evCAnewMsg,
evCAsend
evCAsend4,
evCAsend2,
evCAsend1,
evCAsend0
} CAN_EVENTS;
typedef void (*CAN_CALLBACK)(uint8_t, uint8_t, uint32_t);
@ -78,6 +81,11 @@ void CAN_newMsg();
*/
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 byte0);
void CAN_send_0_byte(uint8_t idRecipient, uint8_t idMsg);
/***********
* SETTERS *
***********/