diff --git a/306-controller_interface.X/app/can_message.c b/306-controller_interface.X/app/can_message.c index 89e21df..ba37fed 100644 --- a/306-controller_interface.X/app/can_message.c +++ b/306-controller_interface.X/app/can_message.c @@ -29,7 +29,7 @@ typedef union { uint16_t full; } 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; incomeData.full = data; 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 // 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); } if(idMsg == 0x2) { // CONTROL_POWER_FACTOR // 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); CAN_Send(0, 5, KART_CST.CONTROL_POWER_FACTOR); } if(idMsg == 0x3) { // CONTROL_STEERING_FACTOR // 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); } diff --git a/306-controller_interface.X/app/can_message.h b/306-controller_interface.X/app/can_message.h index 0249c07..7a7a352 100644 --- a/306-controller_interface.X/app/can_message.h +++ b/306-controller_interface.X/app/can_message.h @@ -32,7 +32,7 @@ S R M * @param idMsg is 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 diff --git a/306-controller_interface.X/middleware/can_interface.c b/306-controller_interface.X/middleware/can_interface.c index ab667e2..44568e0 100644 --- a/306-controller_interface.X/middleware/can_interface.c +++ b/306-controller_interface.X/middleware/can_interface.c @@ -1,5 +1,5 @@ /** - * @author Rémi Heredero + * @author R�mi Heredero * @version 1.0.0 * @date August 2023 * @file can_interface.c @@ -54,6 +54,11 @@ bool CAN_processEvent(Event* ev) { uint64_t data = Event_getData(ev); 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 @@ -68,25 +73,40 @@ bool CAN_processEvent(Event* ev) { // New message arrive if (ev->id == evCAnewMsg) { if (me->receiveCan != NULL) { - uint32_t canData = (uint32_t) data; data = data>>32; tmpData.full = data; - uint8_t idMsg = tmpData.separate.byte0; + idMsg = tmpData.separate.byte0; idMsg = idMsg >> 4; idMsg = idMsg & 0xF; - uint8_t idRecipient = tmpData.separate.byte1; + idRecipient = tmpData.separate.byte1; idRecipient = idRecipient & 0xF; - uint8_t idSender = tmpData.separate.byte1; + idSender = tmpData.separate.byte1; 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 - if (ev->id == evCAsend) { - uCAN_MSG canMsg; + if (ev->id == evCAsend4) { canMsg.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B; // standard canMsg.frame.dlc = 4; // 4 bytes to send canMsg.frame.rtr = 0; // no remote frame @@ -101,6 +121,70 @@ bool CAN_processEvent(Event* ev) { canMsg.frame.id = (uint32_t) data; 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; } @@ -151,7 +235,11 @@ void CAN_newMsg() { data = data | canMsg.frame.data1; data = data<<8; data = data | canMsg.frame.data0; - POST(&CAN_myself, &CAN_processEvent, evCAnewMsg, 0, data); + if(canMsg.frame.rtr) { + POST(&CAN_myself, &CAN_processEvent, evCAnewRTR, 0, data); + } else { + POST(&CAN_myself, &CAN_processEvent, evCAnewMsg, 0, data); + } } void CAN_Send(uint8_t idRecipient, uint8_t idMsg, uint32_t data) { @@ -159,7 +247,50 @@ 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) { + 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); } /*********** diff --git a/306-controller_interface.X/middleware/can_interface.h b/306-controller_interface.X/middleware/can_interface.h index d65266b..8d65ba9 100644 --- a/306-controller_interface.X/middleware/can_interface.h +++ b/306-controller_interface.X/middleware/can_interface.h @@ -17,10 +17,15 @@ typedef enum { typedef enum { evCAinit = 10, evCAnewMsg, - evCAsend + evCAnewRTR, + evCAsend4, + evCAsend2, + evCAsend1, + evCAsend0, + evCAsendRTR } 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 { 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_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 * ***********/