can receive part done
This commit is contained in:
123
306-controller_interface.X/middleware/can_interface.c
Normal file
123
306-controller_interface.X/middleware/can_interface.c
Normal file
@ -0,0 +1,123 @@
|
||||
/**
|
||||
* @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, evCAinit, 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) {
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case STCA_WAIT:
|
||||
break;
|
||||
|
||||
case STCA_READ:
|
||||
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);
|
||||
}
|
101
306-controller_interface.X/middleware/can_interface.h
Normal file
101
306-controller_interface.X/middleware/can_interface.h
Normal file
@ -0,0 +1,101 @@
|
||||
/**
|
||||
* @author R<>mi Heredero
|
||||
* @version 1.0.0
|
||||
* @date August 2023
|
||||
* @file can_interface.h
|
||||
*/
|
||||
#ifndef CANINTERFACE_H
|
||||
#define CANINTERFACE_H
|
||||
|
||||
#include "../xf/xf.h"
|
||||
|
||||
typedef enum {
|
||||
STCA_INIT,
|
||||
STCA_WAIT,
|
||||
STCA_READ
|
||||
} CANINTERFACE_STATES;
|
||||
|
||||
typedef enum {
|
||||
evCAinit = 10, // TODO change this number (< 256)
|
||||
evCAnewMsg,
|
||||
evCAdone
|
||||
} CANINTERFACE_EVENTS;
|
||||
|
||||
typedef void (*CANINTERFACE_CALLBACK_FUNCTION)(void*);
|
||||
typedef void (*CANINTERFACE_CALLBACK_CAN)(uint32_t, uint32_t);
|
||||
typedef struct {
|
||||
CANINTERFACE_CALLBACK_FUNCTION f; // function
|
||||
void* p; // param(s)
|
||||
} CANINTERFACE_CALLBACK;
|
||||
|
||||
typedef struct {
|
||||
CANINTERFACE_STATES state;
|
||||
CANINTERFACE_CALLBACK wait;
|
||||
CANINTERFACE_CALLBACK read;
|
||||
CANINTERFACE_CALLBACK_CAN processCan;
|
||||
} CANINTERFACE;
|
||||
|
||||
CANINTERFACE CANINTERFACE_myself;
|
||||
|
||||
/**
|
||||
* Initialize the CANINTERFACE
|
||||
* @param me the CANINTERFACE itself
|
||||
*/
|
||||
void CANINTERFACE_init();
|
||||
|
||||
/**
|
||||
* Start the CANINTERFACE state machine
|
||||
* @param me the CANINTERFACE itself
|
||||
*/
|
||||
void CANINTERFACE_startBehaviour();
|
||||
|
||||
/**
|
||||
* Handler for receiving new can message during.
|
||||
* This function is done during interrupt
|
||||
*/
|
||||
void CANINTERFACE_newMsg();
|
||||
|
||||
/**
|
||||
* Process the event
|
||||
* @param ev the event to process
|
||||
* @return true if the event is processed
|
||||
*/
|
||||
bool CANINTERFACE_processEvent(Event* ev);
|
||||
|
||||
/*************
|
||||
* Callbacks *
|
||||
*************/
|
||||
|
||||
/**
|
||||
* Set the callback function to call when the CANINTERFACE is entering state wait
|
||||
* @param me the CANINTERFACE itself
|
||||
* @param f the function to call
|
||||
* @param p the param(s) to pass to the function
|
||||
*/
|
||||
void CANINTERFACE_onWait(CANINTERFACE_CALLBACK_FUNCTION f, void* p);
|
||||
|
||||
/**
|
||||
* Set the callback function to call when the CANINTERFACE is entering state read
|
||||
* @param me the CANINTERFACE itself
|
||||
* @param f the function to call
|
||||
* @param p the param(s) to pass to the function
|
||||
*/
|
||||
void CANINTERFACE_onRead(CANINTERFACE_CALLBACK_FUNCTION f, void* p);
|
||||
|
||||
/**
|
||||
* Set the callback function to call when the CANINTERFACE is entering state read
|
||||
* @param f the function to call
|
||||
*/
|
||||
void CANINTERFACE_onProcessCan(CANINTERFACE_CALLBACK_CAN f);
|
||||
|
||||
/************
|
||||
* EMITTERS *
|
||||
************/
|
||||
|
||||
/**
|
||||
* Emit the NewMsg event
|
||||
* @param me the CANINTERFACE itself
|
||||
* @param t time to wait in ms before triggering event
|
||||
*/void CANINTERFACE_emitNewMsg(uint16_t t);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user