This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
SummerSchool2-Controller/306-controller_interface.X/middleware/can_interface.c

136 lines
3.4 KiB
C
Raw Normal View History

/**
* @author R<EFBFBD>mi Heredero
* @version 1.0.0
* @date August 2023
* @file can_interface.c
*/
#include "can_interface.h"
void CAN_init(){
CAN_myself.receiveCan = NULL;
CAN_myself.sender = 0;
}
void CAN_startBehaviour(){
POST(&CAN_myself, &CAN_processEvent, evCAinit, 0, 0);
}
bool CAN_processEvent(Event* ev) {
bool processed = false;
CAN* me = (CAN*)Event_getTarget(ev);
CAN_STATES oldState = me->state;
evIDT evid = Event_getId(ev);
uint64_t data = Event_getData(ev);
switch (me->state) { // onState
case STCA_INIT:
if (ev->id == evCAinit) {
me->state = STCA_PROCESS;
}
break;
case STCA_PROCESS:
// New message arrive
if (ev->id == evCAnewMsg) {
if (me->receiveCan != NULL) {
uint32_t canData = (uint32_t) data;
data = data>>32;
uint8_t idMsg = (uint8_t) data;
data = data>>4;
uint8_t idRecipient = (uint8_t) data;
data = data>>4;
uint8_t idSender = (uint8_t) data;
me->receiveCan(idSender, idMsg, canData);
}
}
// Send a message
if (ev->id == evCAsend) {
uCAN_MSG canMsg;
canMsg.frame.idType = 0; // I don't understand what is it
2023-08-24 13:26:29 +00:00
canMsg.frame.dlc = 4; // 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;
}
if(oldState != me->state){
switch (oldState) { // onExit
case STCA_INIT:
break;
case STCA_PROCESS:
break;
}
switch (me->state) { // onEntry
case STCA_INIT:
break;
case STCA_PROCESS:
break;
}
processed = true;
}
return processed;
}
/*************
* Callbacks *
*************/
void CAN_onReceiveCan(CAN_CALLBACK f) {
CAN_myself.receiveCan = f;
}
/************
* EMITTERS *
************/
void CAN_newMsg() {
uint64_t data;
uCAN_MSG canMsg;
CAN_receive(&canMsg);
data = canMsg.frame.id;
data = data<<32;
data = canMsg.frame.data0;
data = data<<8;
data = canMsg.frame.data1;
data = data<<8;
data = canMsg.frame.data2;
data = data<<8;
data = canMsg.frame.data3;
POST(&CAN_myself, &CAN_processEvent, evCAnewMsg, 0, data);
}
void CAN_Send(uint8_t idRecipient, uint8_t idMsg, uint32_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, evCAsend, 0, tmpData);
}
2023-08-24 15:28:30 +00:00
/***********
* SETTERS *
***********/
void CAN_setSender(uint8_t idSender) {
CAN_myself.sender = idSender;
}