finish devleop can interface not yet tested

This commit is contained in:
2023-08-23 13:45:34 +02:00
parent ff9137a026
commit 21f9cb7f62
23 changed files with 927 additions and 1144 deletions

View File

@ -1,134 +0,0 @@
/**
* @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);
}

View File

@ -1,104 +0,0 @@
/**
* @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
*/
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 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 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 t time to wait in ms before triggering event
*/
void CANINTERFACE_emitNewMsg(uint16_t t);
/**
* Emit the Done event
* @param t time to wait in ms before triggering event
*/
void CANINTERFACE_emitDone(uint16_t t);
#endif

View File

@ -1,131 +0,0 @@
/**
* @author R<>mi Heredero
* @version 1.0.0
* @date August 2023
* @file can_sender.c
*/
#include "can_sender.h"
void CANSENDER_init(CANSENDER* me){
me->state = STCS_INIT;
me->sendingTime = 1;
me->wait.f = NULL;
me->send.f = NULL;
}
void CANSENDER_startBehaviour(CANSENDER* me){
POST(me, &CANSENDER_processEvent, evCSinit, 0, 0);
}
bool CANSENDER_processEvent(Event* ev) {
bool processed = false;
CANSENDER* me = (CANSENDER*)Event_getTarget(ev);
CANSENDER_STATES oldState = me->state;
evIDT evid = Event_getId(ev);
uint64_t data = Event_getData(ev);
switch (me->state) { // onState
case STCS_INIT:
if (ev->id == evCSinit) {
CANSENDER.state = STCS_WAIT;
}
break;
case STCS_WAIT:
if (ev->id == evCSsend) {
CANSENDER.state = STCS_SEND;
}
break;
case STCS_SEND:
if (ev->id == evCSdone) {
CANSENDER.state = STCS_WAIT;
}
uCAN_MSG canMsg;
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);
CANSENDER_emitDone(me, 0);
break;
}
if(oldState != me->state){
switch (oldState) { // onExit
case STCS_INIT:
break;
case STCS_WAIT:
break;
case STCS_SEND:
break;
}
switch (me->state) { // onEntry
case STCS_INIT:
break;
case STCS_WAIT:
if (me->wait.f != NULL) {
me->wait.f(me->wait.p);
}
break;
case STCS_SEND:
if (me->send.f != NULL) {
me->send.f(me->send.p);
}
break;
}
processed = true;
}
return processed;
}
/*************
* Callbacks *
*************/
void CANSENDER_onWait(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p) {
me->wait.f = f;
me->wait.p = p;
}
void CANSENDER_onSend(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p) {
me->send.f = f;
me->send.p = p;
}
/************
* EMITTERS *
************/
void CANSENDER_emitSend(CANSENDER* me, uint16_t t) {
POST(me, &CANSENDER_processEvent, evCSsend, t, 0);
}
void CANSENDER_emitDone(CANSENDER* me, uint16_t t) {
POST(me, &CANSENDER_processEvent, evCSdone, t, 0);
}
CANSENDER_sendCanMsg(CANSENDER* me, uint8_t id, uint32_t data)
/***********
* SETTERS *
***********/
void CANSENDER_setSendingTime(CANSENDER* me, uint8_t v) {
me->sendingTime = v;
}

View File

@ -1,112 +0,0 @@
/**
* @author R<>mi Heredero
* @version 1.0.0
* @date August 2023
* @file can_sender.h
*/
#ifndef CANSENDER_H
#define CANSENDER_H
#include "../../xf/xf.h"
typedef enum {
STCS_INIT,
STCS_WAIT,
STCS_SEND
} CANSENDER_STATES;
typedef enum {
evCSinit = 15, // TODO change this number (< 256)
evCSsend,
evCSdone
} CANSENDER_EVENTS;
typedef void (*CANSENDER_CALLBACK_FUNCTION)(void*);
typedef struct {
CANSENDER_CALLBACK_FUNCTION f; // function
void* p; // param(s)
} CANSENDER_CALLBACK;
typedef struct {
CANSENDER_STATES state;
uint8_t sendingTime;
CANSENDER_CALLBACK wait;
CANSENDER_CALLBACK send;
} CANSENDER;
/**
* Initialize the CANSENDER
* @param me the CANSENDER itself
*/
void CANSENDER_init(CANSENDER* me);
/**
* Start the CANSENDER state machine
* @param me the CANSENDER itself
*/
void CANSENDER_startBehaviour(CANSENDER* me);
/**
* Process the event
* @param ev the event to process
* @return true if the event is processed
*/
bool CANSENDER_processEvent(Event* ev);
/*************
* Callbacks *
*************/
/**
* Set the callback function to call when the CANSENDER is entering state wait
* @param me the CANSENDER itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void CANSENDER_onWait(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p);
/**
* Set the callback function to call when the CANSENDER is entering state send
* @param me the CANSENDER itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void CANSENDER_onSend(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p);
/************
* EMITTERS *
************/
/**
* Emit the send event
* @param me the CANSENDER itself
* @param t time to wait in ms before triggering event
*/
void CANSENDER_emitSend(CANSENDER* me, uint16_t t);
/**
* Emit the done event
* @param me the CANSENDER itself
* @param t time to wait in ms before triggering event
*/
void CANSENDER_emitDone(CANSENDER* me, uint16_t t);
void CANSENDER_sendCanMsg(CANSENDER* me, uint8_t id, uint32_t data);
void sendCanMsg(uint32_t id, uint32_t data);
/***********
* SETTERS *
***********/
/**
*
* @param me
* @param v
*/
void CANSENDER_setSendingTime(CANSENDER* me, uint8_t v);
void CANSENDER_seSender(CANSENDER* me, uint8_t s);
void CANSENDER_setRecipient(CANSENDER* me, uint8_t r);
#endif

View File

@ -0,0 +1,127 @@
/**
* @author R<>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
canMsg.frame.dlc = 8; // 8 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);
}

View File

@ -0,0 +1,99 @@
/**
* @author R<>mi Heredero
* @version 1.0.0
* @date August 2023
* @file can_interface.h
*/
#ifndef CAN_H
#define CAN_H
#include "../xf/xf.h"
typedef enum {
STCA_INIT,
STCA_PROCESS
} CAN_STATES;
typedef enum {
evCAinit = 10, // TODO change this number (< 256)
evCAnewMsg,
evCAsend
} CAN_EVENTS;
typedef void (*CAN_CALLBACK)(uint8_t, uint8_t, uint32_t);
typedef struct {
CAN_STATES state;
uint8_t sender;
CAN_CALLBACK receiveCan;
} CAN;
CAN CAN_myself;
/**
* Initialize the CAN
* @param me the CAN itself
*/
void CAN_init();
/**
* Start the CAN state machine
*/
void CAN_startBehaviour();
/**
* Process the event
* @param ev the event to process
* @return true if the event is processed
*/
bool CAN_processEvent(Event* ev);
/*************
* Callbacks *
*************/
/**
* Set the callback function to call when the CAN is entering state read
* @param f the function to call
*/
void CAN_onReceiveCan(CAN_CALLBACK f);
/************
* EMITTERS *
************/
/**
* Handler for receiving new can message during.
* This function is done during interrupt
*/
void CAN_newMsg();
/**
* Put a new can message on the queue
* @param idRecipient id for the recipient
* @param idMsg id for the message
* @param data 4 bytes of data to send
*/
void CAN_Send(uint8_t idRecipient, uint8_t idMsg, uint32_t data);
/***********
* SETTERS *
***********/
/**
* Set the sender of this firmware
* @param idSender id of the sender
* 1 CONTROL
* 2 JOYSTICK
* 3 DISPLAY
* 4 DRIVE
* 5 STEERING
* 6 SUPPLY
* 7 UNDEFINED YET
* 0 BROADCAST/DEBUG
*/
void CAN_setSender(uint8_t idSender);
#endif