WIP can sender - template done

This commit is contained in:
Rémi Heredero 2023-08-23 07:29:12 +02:00
parent d5c30dfea4
commit ff9137a026
4 changed files with 85 additions and 65 deletions

View File

@ -7,34 +7,56 @@
#include "can_sender.h" #include "can_sender.h"
void CAN_SENDER_init(CAN_SENDER* me){ void CANSENDER_init(CANSENDER* me){
me->state = STCS_INIT; me->state = STCS_INIT;
me->sendingTime = 1; me->sendingTime = 1;
me->wait.f = NULL; me->wait.f = NULL;
me->send.f = NULL; me->send.f = NULL;
} }
void CAN_SENDER_startBehaviour(CAN_SENDER* me){ void CANSENDER_startBehaviour(CANSENDER* me){
POST(me, &CAN_SENDER_processEvent, evCSinit, 0, 0); POST(me, &CANSENDER_processEvent, evCSinit, 0, 0);
} }
bool CAN_SENDER_processEvent(Event* ev) { bool CANSENDER_processEvent(Event* ev) {
bool processed = false; bool processed = false;
CAN_SENDER* me = (CAN_SENDER*)Event_getTarget(ev); CANSENDER* me = (CANSENDER*)Event_getTarget(ev);
CAN_SENDER_STATES oldState = me->state; CANSENDER_STATES oldState = me->state;
evIDT evid = Event_getId(ev); evIDT evid = Event_getId(ev);
uint64_t data = Event_getData(ev);
switch (me->state) { // onState switch (me->state) { // onState
case STCS_INIT: case STCS_INIT:
if (ev->id == evCSinit) { if (ev->id == evCSinit) {
CANSENDER.state = STCS_WAIT;
} }
break; break;
case STCS_WAIT: case STCS_WAIT:
if (ev->id == evCSsend) {
CANSENDER.state = STCS_SEND;
}
break; break;
case STCS_SEND: 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; break;
} }
@ -76,12 +98,12 @@ bool CAN_SENDER_processEvent(Event* ev) {
* Callbacks * * Callbacks *
*************/ *************/
void CAN_SENDER_onWait(CAN_SENDER* me, CAN_SENDER_CALLBACK_FUNCTION f, void* p) { void CANSENDER_onWait(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p) {
me->wait.f = f; me->wait.f = f;
me->wait.p = p; me->wait.p = p;
} }
void CAN_SENDER_onSend(CAN_SENDER* me, CAN_SENDER_CALLBACK_FUNCTION f, void* p) { void CANSENDER_onSend(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p) {
me->send.f = f; me->send.f = f;
me->send.p = p; me->send.p = p;
} }
@ -90,18 +112,20 @@ void CAN_SENDER_onSend(CAN_SENDER* me, CAN_SENDER_CALLBACK_FUNCTION f, void* p)
* EMITTERS * * EMITTERS *
************/ ************/
void CAN_SENDER_emitSend(CAN_SENDER* me, uint16_t t) { void CANSENDER_emitSend(CANSENDER* me, uint16_t t) {
POST(me, &CAN_SENDER_processEvent, evCSsend, t, 0); POST(me, &CANSENDER_processEvent, evCSsend, t, 0);
} }
void CAN_SENDER_emitDone(CAN_SENDER* me, uint16_t t) { void CANSENDER_emitDone(CANSENDER* me, uint16_t t) {
POST(me, &CAN_SENDER_processEvent, evCSdone, t, 0); POST(me, &CANSENDER_processEvent, evCSdone, t, 0);
} }
CANSENDER_sendCanMsg(CANSENDER* me, uint8_t id, uint32_t data)
/*********** /***********
* SETTERS * * SETTERS *
***********/ ***********/
void CAN_SENDER_setSendingTime(CAN_SENDER* me, uint8_t v) { void CANSENDER_setSendingTime(CANSENDER* me, uint8_t v) {
me->sendingTime = v; me->sendingTime = v;
} }

View File

@ -4,8 +4,8 @@
* @date August 2023 * @date August 2023
* @file can_sender.h * @file can_sender.h
*/ */
#ifndef CAN_SENDER_H #ifndef CANSENDER_H
#define CAN_SENDER_H #define CANSENDER_H
#include "../../xf/xf.h" #include "../../xf/xf.h"
@ -13,65 +13,65 @@ typedef enum {
STCS_INIT, STCS_INIT,
STCS_WAIT, STCS_WAIT,
STCS_SEND STCS_SEND
} CAN_SENDER_STATES; } CANSENDER_STATES;
typedef enum { typedef enum {
evCSinit = 15, // TODO change this number (< 256) evCSinit = 15, // TODO change this number (< 256)
evCSsend, evCSsend,
evCSdone evCSdone
} CAN_SENDER_EVENTS; } CANSENDER_EVENTS;
typedef void (*CAN_SENDER_CALLBACK_FUNCTION)(void*); typedef void (*CANSENDER_CALLBACK_FUNCTION)(void*);
typedef struct { typedef struct {
CAN_SENDER_CALLBACK_FUNCTION f; // function CANSENDER_CALLBACK_FUNCTION f; // function
void* p; // param(s) void* p; // param(s)
} CAN_SENDER_CALLBACK; } CANSENDER_CALLBACK;
typedef struct { typedef struct {
CAN_SENDER_STATES state; CANSENDER_STATES state;
uint8_t sendingTime; uint8_t sendingTime;
CAN_SENDER_CALLBACK wait; CANSENDER_CALLBACK wait;
CAN_SENDER_CALLBACK send; CANSENDER_CALLBACK send;
} CAN_SENDER; } CANSENDER;
/** /**
* Initialize the CAN_SENDER * Initialize the CANSENDER
* @param me the CAN_SENDER itself * @param me the CANSENDER itself
*/ */
void CAN_SENDER_init(CAN_SENDER* me); void CANSENDER_init(CANSENDER* me);
/** /**
* Start the CAN_SENDER state machine * Start the CANSENDER state machine
* @param me the CAN_SENDER itself * @param me the CANSENDER itself
*/ */
void CAN_SENDER_startBehaviour(CAN_SENDER* me); void CANSENDER_startBehaviour(CANSENDER* me);
/** /**
* Process the event * Process the event
* @param ev the event to process * @param ev the event to process
* @return true if the event is processed * @return true if the event is processed
*/ */
bool CAN_SENDER_processEvent(Event* ev); bool CANSENDER_processEvent(Event* ev);
/************* /*************
* Callbacks * * Callbacks *
*************/ *************/
/** /**
* Set the callback function to call when the CAN_SENDER is entering state wait * Set the callback function to call when the CANSENDER is entering state wait
* @param me the CAN_SENDER itself * @param me the CANSENDER itself
* @param f the function to call * @param f the function to call
* @param p the param(s) to pass to the function * @param p the param(s) to pass to the function
*/ */
void CAN_SENDER_onWait(CAN_SENDER* me, CAN_SENDER_CALLBACK_FUNCTION f, void* p); void CANSENDER_onWait(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p);
/** /**
* Set the callback function to call when the CAN_SENDER is entering state send * Set the callback function to call when the CANSENDER is entering state send
* @param me the CAN_SENDER itself * @param me the CANSENDER itself
* @param f the function to call * @param f the function to call
* @param p the param(s) to pass to the function * @param p the param(s) to pass to the function
*/ */
void CAN_SENDER_onSend(CAN_SENDER* me, CAN_SENDER_CALLBACK_FUNCTION f, void* p); void CANSENDER_onSend(CANSENDER* me, CANSENDER_CALLBACK_FUNCTION f, void* p);
/************ /************
* EMITTERS * * EMITTERS *
@ -79,17 +79,21 @@ void CAN_SENDER_onSend(CAN_SENDER* me, CAN_SENDER_CALLBACK_FUNCTION f, void* p);
/** /**
* Emit the send event * Emit the send event
* @param me the CAN_SENDER itself * @param me the CANSENDER itself
* @param t time to wait in ms before triggering event * @param t time to wait in ms before triggering event
*/ */
void CAN_SENDER_emitSend(CAN_SENDER* me, uint16_t t); void CANSENDER_emitSend(CANSENDER* me, uint16_t t);
/** /**
* Emit the done event * Emit the done event
* @param me the CAN_SENDER itself * @param me the CANSENDER itself
* @param t time to wait in ms before triggering event * @param t time to wait in ms before triggering event
*/ */
void CAN_SENDER_emitDone(CAN_SENDER* me, uint16_t t); 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 * * SETTERS *
@ -100,6 +104,9 @@ void CAN_SENDER_emitSend(CAN_SENDER* me, uint16_t t);
* @param me * @param me
* @param v * @param v
*/ */
void CAN_SENDER_setSendingTime(CAN_SENDER* me, uint8_t 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 #endif

View File

@ -319,21 +319,6 @@
<property key="coverage-enable" value=""/> <property key="coverage-enable" value=""/>
<property key="stack-guidance" value="false"/> <property key="stack-guidance" value="false"/>
</XC8-CO> </XC8-CO>
<XC8-config-global>
<property key="advanced-elf" value="true"/>
<property key="gcc-opt-driver-new" value="true"/>
<property key="gcc-opt-std" value="-std=c99"/>
<property key="gcc-output-file-format" value="dwarf-3"/>
<property key="omit-pack-options" value="false"/>
<property key="omit-pack-options-new" value="1"/>
<property key="output-file-format" value="-mcof,+elf"/>
<property key="stack-size-high" value="auto"/>
<property key="stack-size-low" value="auto"/>
<property key="stack-size-main" value="auto"/>
<property key="stack-type" value="compiled"/>
<property key="user-pack-device-support" value=""/>
<property key="wpo-lto" value="false"/>
</XC8-config-global>
</conf> </conf>
</confs> </confs>
</configurationDescriptor> </configurationDescriptor>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<diagram program="umlet" version="15.0.0"> <diagram program="umlet" version="15.1">
<zoom_level>15</zoom_level> <zoom_level>15</zoom_level>
<element> <element>
<id>UMLSpecialState</id> <id>UMLSpecialState</id>
@ -211,14 +211,18 @@ evCSdone
<coordinates> <coordinates>
<x>765</x> <x>765</x>
<y>630</y> <y>630</y>
<w>645</w> <w>705</w>
<h>150</h> <h>270</h>
</coordinates> </coordinates>
<panel_attributes>_*How to use*_ <panel_attributes>_*How to use*_
CANSENDER_send(uint32_t id, uint32_t data); *Like a static class:*
or sendCanMsg(uint32_t id, uint32_t data);
CANSENDER_send(CANSENDER *me, uint8_t id, uint32_t data);</panel_attributes>
*Or like an Object: *
CANSENDER_seSender(CANSENDER* me, uint8_t s);
CANSENDER_setRecipient(CANSENDER* me, uint8_t r);
CANSENDER_sendCanMsg(CANSENDER* me, uint8_t id, uint32_t data);</panel_attributes>
<additional_attributes/> <additional_attributes/>
</element> </element>
</diagram> </diagram>