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/can_interface.c

135 lines
3.2 KiB
C
Raw Normal View History

2023-08-22 13:53:38 +00:00
/**
* @author R<EFBFBD>mi Heredero
* @version 1.0.0
* @date August 2023
* @file can_interface.c
*/
#include "can_interface.h"
void CANINTERFACE_init(){
CANINTERFACE_myself.wait.f = NULL;
CANINTERFACE_myself.read.f = NULL;
CANINTERFACE_myself.processCan = NULL;
}
void CANINTERFACE_startBehaviour(){
POST(&CANINTERFACE_myself, &CANINTERFACE_processEvent, evCAinit, 0, 0);
}
void CANINTERFACE_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;
2023-08-22 14:56:01 +00:00
POST(&CANINTERFACE_myself, &CANINTERFACE_processEvent, evCAnewMsg, 0, data);
2023-08-22 13:53:38 +00:00
}
bool CANINTERFACE_processEvent(Event* ev) {
bool processed = false;
CANINTERFACE* me = (CANINTERFACE*)Event_getTarget(ev);
CANINTERFACE_STATES oldState = me->state;
evIDT evid = Event_getId(ev);
uint64_t data = Event_getData(ev);
uint32_t canData = (uint32_t) data;
data = data>>8;
uint32_t canId = (uint32_t) data;
switch (me->state) { // onState
case STCA_INIT:
if (ev->id == evCAinit) {
2023-08-22 14:56:01 +00:00
me->state = STCA_WAIT;
2023-08-22 13:53:38 +00:00
}
break;
case STCA_WAIT:
2023-08-22 14:56:01 +00:00
if (ev->id == evCAnewMsg) {
me->state = STCA_READ;
CANINTERFACE_emitDone(0);
}
2023-08-22 13:53:38 +00:00
break;
case STCA_READ:
2023-08-22 14:56:01 +00:00
if (ev->id == evCAdone) {
me->state = STCA_WAIT;
}
2023-08-22 13:53:38 +00:00
break;
}
if(oldState != me->state){
switch (oldState) { // onExit
case STCA_INIT:
break;
case STCA_WAIT:
break;
case STCA_READ:
break;
}
switch (me->state) { // onEntry
case STCA_INIT:
break;
case STCA_WAIT:
if (me->wait.f != NULL) {
me->wait.f(me->wait.p);
}
break;
case STCA_READ:
if (me->read.f != NULL) {
me->read.f(me->read.p);
}
if (me->processCan != NULL) {
me->processCan(canId, canData);
}
break;
}
processed = true;
}
return processed;
}
/*************
* Callbacks *
*************/
void CANINTERFACE_onWait(CANINTERFACE_CALLBACK_FUNCTION f, void* p) {
CANINTERFACE_myself.wait.f = f;
CANINTERFACE_myself.wait.p = p;
}
void CANINTERFACE_onRead(CANINTERFACE_CALLBACK_FUNCTION f, void* p) {
CANINTERFACE_myself.read.f = f;
CANINTERFACE_myself.read.p = p;
}
void CANINTERFACE_onProcessCan(CANINTERFACE_CALLBACK_CAN f) {
CANINTERFACE_myself.processCan = f;
}
/************
* EMITTERS *
************/
void CANINTERFACE_emitNewMsg(uint16_t t) {
POST(&CANINTERFACE_myself, &CANINTERFACE_processEvent, evCAnewMsg, t, 0);
}
2023-08-22 14:56:01 +00:00
void CANINTERFACE_emitDone(uint16_t t) {
POST(&CANINTERFACE_myself, &CANINTERFACE_processEvent, evCAdone, t, 0);
}