From 4987fbabd022a5f51f5d82ff6703e1230eea0f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 30 Aug 2023 16:19:42 +0200 Subject: [PATCH 1/2] add send for 4,1 or 0 bytes --- .../middleware/can_interface.c | 101 ++++++++++++++++-- .../middleware/can_interface.h | 10 +- 2 files changed, 104 insertions(+), 7 deletions(-) diff --git a/306-controller_interface.X/middleware/can_interface.c b/306-controller_interface.X/middleware/can_interface.c index 6c893fd..a60b653 100644 --- a/306-controller_interface.X/middleware/can_interface.c +++ b/306-controller_interface.X/middleware/can_interface.c @@ -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); } /*********** diff --git a/306-controller_interface.X/middleware/can_interface.h b/306-controller_interface.X/middleware/can_interface.h index d65266b..79d7655 100644 --- a/306-controller_interface.X/middleware/can_interface.h +++ b/306-controller_interface.X/middleware/can_interface.h @@ -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 * ***********/ From 448b5e66adc77c8ad3a975b16558c3cd89ca28e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 30 Aug 2023 16:20:41 +0200 Subject: [PATCH 2/2] fix 1 byte sending --- 306-controller_interface.X/middleware/can_interface.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/306-controller_interface.X/middleware/can_interface.h b/306-controller_interface.X/middleware/can_interface.h index 79d7655..b76967b 100644 --- a/306-controller_interface.X/middleware/can_interface.h +++ b/306-controller_interface.X/middleware/can_interface.h @@ -83,7 +83,7 @@ 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_1_byte(uint8_t idRecipient, uint8_t idMsg, uint8_t data); void CAN_send_0_byte(uint8_t idRecipient, uint8_t idMsg); /***********