135 lines
3.2 KiB
C
135 lines
3.2 KiB
C
/**
|
|
* @author Ré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;
|
|
POST(&CANINTERFACE_myself, &CANINTERFACE_processEvent, evCAnewMsg, 0, data);
|
|
}
|
|
|
|
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) {
|
|
me->state = STCA_WAIT;
|
|
}
|
|
break;
|
|
|
|
case STCA_WAIT:
|
|
if (ev->id == evCAnewMsg) {
|
|
me->state = STCA_READ;
|
|
CANINTERFACE_emitDone(0);
|
|
}
|
|
break;
|
|
|
|
case STCA_READ:
|
|
if (ev->id == evCAdone) {
|
|
me->state = STCA_WAIT;
|
|
}
|
|
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);
|
|
}
|
|
|
|
void CANINTERFACE_emitDone(uint16_t t) {
|
|
POST(&CANINTERFACE_myself, &CANINTERFACE_processEvent, evCAdone, t, 0);
|
|
}
|