From 21f9cb7f623452db80c264a36ba0824a4bcc7696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 23 Aug 2023 13:45:34 +0200 Subject: [PATCH] finish devleop can interface not yet tested --- 306-controller_interface.X/app/blcontrol.c | 25 --- 306-controller_interface.X/app/blcontrol.h | 19 -- .../app/factory/factory.c | 70 ++++++ .../app/factory/factory.h | 48 ++++ 306-controller_interface.X/{ => app}/main.c | 4 +- .../board/button/button.c | 169 ++++++++++---- .../board/button/button.h | 104 +++++++-- .../board/button/buttonsm.c | 129 ----------- .../board/button/buttonsm.h | 69 ------ 306-controller_interface.X/board/led/led.c | 85 +++---- 306-controller_interface.X/board/led/led.h | 54 +++-- 306-controller_interface.X/factory/factory.c | 62 ----- 306-controller_interface.X/factory/factory.h | 42 ---- .../mcc_generated_files/pin_manager.c | 11 +- .../mcc_generated_files/pin_manager.h | 208 +++++++++++++---- .../middleware/can/can_interface.c | 134 ----------- .../middleware/can/can_interface.h | 104 --------- .../middleware/can/can_sender.c | 131 ----------- .../middleware/can/can_sender.h | 112 --------- .../middleware/can_interface.c | 127 +++++++++++ .../middleware/can_interface.h | 99 ++++++++ .../nbproject/configurations.xml | 53 ++--- 306-controller_interface.X/ss22ep.mc3 | 212 +++++++++--------- 23 files changed, 927 insertions(+), 1144 deletions(-) delete mode 100644 306-controller_interface.X/app/blcontrol.c delete mode 100644 306-controller_interface.X/app/blcontrol.h create mode 100644 306-controller_interface.X/app/factory/factory.c create mode 100644 306-controller_interface.X/app/factory/factory.h rename 306-controller_interface.X/{ => app}/main.c (93%) delete mode 100644 306-controller_interface.X/board/button/buttonsm.c delete mode 100644 306-controller_interface.X/board/button/buttonsm.h delete mode 100644 306-controller_interface.X/factory/factory.c delete mode 100644 306-controller_interface.X/factory/factory.h delete mode 100644 306-controller_interface.X/middleware/can/can_interface.c delete mode 100644 306-controller_interface.X/middleware/can/can_interface.h delete mode 100644 306-controller_interface.X/middleware/can/can_sender.c delete mode 100644 306-controller_interface.X/middleware/can/can_sender.h create mode 100644 306-controller_interface.X/middleware/can_interface.c create mode 100644 306-controller_interface.X/middleware/can_interface.h diff --git a/306-controller_interface.X/app/blcontrol.c b/306-controller_interface.X/app/blcontrol.c deleted file mode 100644 index 60efb4f..0000000 --- a/306-controller_interface.X/app/blcontrol.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "blcontrol.h" -#include "../mcc_generated_files/mcc.h" -#include "../factory/factory.h" -//private methods - -void BLControl_init(BLControl* me) -{ - //nothing to do yet -} - -void BLControl_onButton(void * me, uint8_t buttonId, bool pressed) -{ - BLControl* realMe = (BLControl*)me; - if (buttonId == BID) - { - if (pressed) - { - LED_on(l()); - } - else - { - LED_off(l()); - } - } -} diff --git a/306-controller_interface.X/app/blcontrol.h b/306-controller_interface.X/app/blcontrol.h deleted file mode 100644 index 6b0e053..0000000 --- a/306-controller_interface.X/app/blcontrol.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef BLCONTROL_DEF -#define BLCONTROL_DEF - -#include -#include -#include "../xf/xf.h" -#include "../board/button/buttonsm.h" - -struct BLControl_ -{ - -}; - -typedef struct BLControl_ BLControl; - -void BLControl_init(BLControl* me); -void BLControl_onButton(void* me, uint8_t buttonId, bool pressed); - -#endif diff --git a/306-controller_interface.X/app/factory/factory.c b/306-controller_interface.X/app/factory/factory.c new file mode 100644 index 0000000..3b149bb --- /dev/null +++ b/306-controller_interface.X/app/factory/factory.c @@ -0,0 +1,70 @@ +#include "factory.h" + + +//the factory object containing all objects of our system +static Factory theFactory; + +//all the getters +LED* l1() { + return &theFactory.l1_; +} +LED* l2() { + return &theFactory.l2_; +} +LED* l3() { + return &theFactory.l3_; +} +LED* l4() { + return &theFactory.l4_; +} +LED* l5() { + return &theFactory.l5_; +} +LED* l6() { + return &theFactory.l6_; +} +LED* l7() { + return &theFactory.l7_; +} +LED* l8() { + return &theFactory.l8_; +} + + +//initialize all objects +void Factory_init() { + LED_init(l1(), 1); + LED_init(l2(), 2); + LED_init(l3(), 3); + LED_init(l4(), 4); + LED_init(l5(), 5); + LED_init(l6(), 6); + LED_init(l7(), 7); + LED_init(l8(), 8); + + LED_initHW(l1()); + LED_initHW(l2()); + LED_initHW(l3()); + LED_initHW(l4()); + LED_initHW(l5()); + LED_initHW(l6()); + LED_initHW(l7()); + LED_initHW(l8()); + + CAN_init(); +} + +void foo(uint8_t a, uint8_t b, uint32_t c){ + +} + +//connect objects if required +void Factory_build() { + ECAN_SetRXBnInterruptHandler(CAN_newMsg); + CAN_onReceiveCan(foo); +} + +//start all state machines +void Factory_start() { + CAN_startBehaviour(); +} diff --git a/306-controller_interface.X/app/factory/factory.h b/306-controller_interface.X/app/factory/factory.h new file mode 100644 index 0000000..737a8e3 --- /dev/null +++ b/306-controller_interface.X/app/factory/factory.h @@ -0,0 +1,48 @@ +/** + * @author Rémi Heredero + * @version. 0.0.0 + * @date August 2023 + * @file factory.h + */ + + +#ifndef FACTORY_H +#define FACTORY_H + +#include +#include + +#include "../../board/led/led.h" +#include "../../board/button/button.h" +#include "../../middleware/can_interface.h" + + +typedef struct { + LED l1_; + LED l2_; + LED l3_; + LED l4_; + LED l5_; + LED l6_; + LED l7_; + LED l8_; + +} Factory; + + +void Factory_init(); +void Factory_build(); +void Factory_start(); + +//these are global getters for our objects +LED* l1(); +LED* l2(); +LED* l3(); +LED* l4(); +LED* l5(); +LED* l6(); +LED* l7(); +LED* l8(); + + +#endif \ No newline at end of file diff --git a/306-controller_interface.X/main.c b/306-controller_interface.X/app/main.c similarity index 93% rename from 306-controller_interface.X/main.c rename to 306-controller_interface.X/app/main.c index 1c5d0ae..f8f2218 100644 --- a/306-controller_interface.X/main.c +++ b/306-controller_interface.X/app/main.c @@ -1,5 +1,5 @@ -#include "mcc_generated_files/mcc.h" -#include "xf/xf.h" +#include "../mcc_generated_files/mcc.h" +#include "../xf/xf.h" #include "factory/factory.h" /* diff --git a/306-controller_interface.X/board/button/button.c b/306-controller_interface.X/board/button/button.c index 158cf00..a227b8c 100644 --- a/306-controller_interface.X/board/button/button.c +++ b/306-controller_interface.X/board/button/button.c @@ -1,66 +1,137 @@ +/** + * @author Rémi Heredero (remi@heredero.ch) + * @version. 1.0.0 + * @date 2023-06-15 + */ #include "button.h" #include "../../mcc_generated_files/pin_manager.h" +#include "../../app/factory/factory.h" -void Button_init(Button* me, uint8_t id, bool isPullUp) -{ +/** + * @brief Initialize the button + * + * @param me The object to initialize + * @param id The id of the button + */ +void BUTTON_init(BUTTON* me, uint8_t id) { me->id = id; - me->isPullUp = isPullUp; + me->state = ST_PBINIT; + me->press.fCallBack = NULL; + me->release.fCallBack = NULL; } /** - * @brief Initialize the Driver + * @brief Initialize the hardware of the button * + * @param me The object to initialize */ -void Button_initHW(Button* me) -{ -} - -//read the state of the button -//maybe you have to adjust the -//low level calls -uint8_t Button_read(Button* me) -{ - uint8_t value = LOW; - switch (me->id) - { +void BUTTON_initHW(BUTTON* me) { + switch (me->id) { case 1: - value = IO_RA7_GetValue(); - break; + INPUT1_SetDigitalInput(); + break; case 2: - break; + INPUT2_SetDigitalInput(); + break; case 3: - break; - case 4: - break; - case 5: - break; - case 6: - break; - case 7: - break; - case 8: - break; - case 9: - break; - case 10: - break; + INPUT3_SetDigitalInput(); + break; + default: + break; } - if (me->isPullUp == true) - { - value=value==LOW?HIGH:LOW; - } - return value; - } - -//id getter -uint8_t Button_getId(Button* me) -{ - return me->id; } -//id setter -void Button_setId(Button* me, uint8_t id) -{ - me->id = id; +/** + * @brief Check if the button is pressed + * The function returns true if the button is pressed, false otherwise + * + * @param me The object to check + * @return true if the button is pressed + * @return false if the button is not pressed + */ +bool BUTTON_isPressed(BUTTON* me) { + switch (me->id) { + case 1: + return INPUT1_GetValue(); + break; + case 2: + return INPUT2_GetValue(); + break; + case 3: + return INPUT3_GetValue(); + break; + default: + return false; + break; + } +} + +void BUTTON_startBehaviour(BUTTON* me) { + POST(me, &BUTTON_processEvent, evPBInit, 0, 0); +} + +bool BUTTON_processEvent(Event * ev) { + bool processed = false; + BUTTON* me = (BUTTON*)Event_getTarget(ev); + BUTTON_STATES oldState = me->state; + evIDT evid = Event_getId(ev); + + switch(me->state){ + case ST_PBINIT: + if (evid == evPBInit) { + POST(me, &BUTTON_processEvent, evPBPoll, 0, 0); + if(BUTTON_isPressed(me)) { + me->state = ST_PBPRESSED; + } else { + me->state = ST_PBRELEASED; + } + } + break; + case ST_PBRELEASED: + if(evid == evPBPoll) { + POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0); + if(BUTTON_isPressed(me)) { + me->state = ST_PBPRESSED; + } + } + + break; + case ST_PBPRESSED: + if(evid == evPBPoll) { + POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0); + if(!BUTTON_isPressed(me)){ + me->state = ST_PBRELEASED; + } + + } + break; + } + + if(oldState != me->state) { + switch(me->state){ + case ST_PBINIT: + break; + case ST_PBRELEASED: + if(me->release.fCallBack != NULL) me->release.fCallBack(me->release.param); + break; + case ST_PBPRESSED: + if(me->press.fCallBack != NULL) me->press.fCallBack(me->press.param); + break; + } + processed = true; + } + return processed; +} + +void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack fRelease) { + me->press = fPress; + me->release = fRelease; +} + +buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param){ + buttonCallBack c; + c.fCallBack = f; + c.param = param; + return c; } \ No newline at end of file diff --git a/306-controller_interface.X/board/button/button.h b/306-controller_interface.X/board/button/button.h index de76a35..c3cfbf0 100644 --- a/306-controller_interface.X/board/button/button.h +++ b/306-controller_interface.X/board/button/button.h @@ -1,25 +1,97 @@ -#ifndef Button_ONCE -#define Button_ONCE +/** + * @author Rémi Heredero (remi@heredero.ch) + * @version. 1.0.0 + * @date 2023-06-15 + */ +#ifndef BUTTON_H +#define BUTTON_H #include #include +#include "../../xf/xf.h" -/* - * this is the declaration of the Button class +#define PB_POLL_TIME 20 // Poll time for BUTTON + +typedef enum { + ST_PBINIT, + ST_PBRELEASED, + ST_PBPRESSED +} BUTTON_STATES; + +typedef enum { + evPBInit=50, + evPBPoll +} BUTTON_EVENTS; + +// Calback function +typedef void (*fButtonCallback)(void*); +typedef struct { + fButtonCallback fCallBack; + void* param; +} buttonCallBack; + +typedef struct { + uint8_t id; // Id of the button + BUTTON_STATES state; // Actual state + buttonCallBack press; // Callback for the rising edge of the button + buttonCallBack release; // Callback for the falling edge of the button +} BUTTON; + +/** + * @brief Initialize the button + * + * @param me button itself + * @param id The id of the button */ +void BUTTON_init(BUTTON* me, uint8_t id); -struct Button_ -{ - uint8_t id; - bool isPullUp; -}; +/** + * @brief Initialize the hardware of the button + * + * @param me button itself + */ +void BUTTON_initHW(BUTTON* me); -typedef struct Button_ Button; +/** + * @brief Set both callback event functions + * + * @param me button itself + * @param fPress callback function when the button have a rising edge + * @param release callback function whent the have a falling edge + */ +void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack release); -void Button_init(Button* me, uint8_t id, bool isPullUp); -void Button_initHW(Button* me); -uint8_t Button_read(Button* me); -void Button_setId(Button* me, uint8_t id); -uint8_t Button_getId(Button* me); +/** + * @brief Check if the button is pressed + * The function returns true if the button is pressed, false otherwise + * + * @param me button itself + * @return true if the button is pressed + * @return false if the button is not pressed + */ +bool BUTTON_isPressed(BUTTON* me); -#endif +/** + * @biref Start state machine of the BUTTON + * + * @param me the button itself + */ +void BUTTON_startBehaviour(BUTTON* me); + +/** + * @brief State machine of the BUTTON + * + * @param ev event to process on the state machine + */ +bool BUTTON_processEvent(Event* ev); + +/** + * @brief Define a callback for BUTTON + * + * @param f callback function + * @param param callback parameter for the function + * @return the callback struct + */ +buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param); + +#endif /* BUTTON_H */ \ No newline at end of file diff --git a/306-controller_interface.X/board/button/buttonsm.c b/306-controller_interface.X/board/button/buttonsm.c deleted file mode 100644 index 05e7fb9..0000000 --- a/306-controller_interface.X/board/button/buttonsm.c +++ /dev/null @@ -1,129 +0,0 @@ -#include "buttonsm.h" - -/* - * this is the init method of the ButtonSM class - */ -void ButtonSM_init(ButtonSM* me, Button* button) -{ - me->state = ST_BSMINIT; - me->button = button; - - me->actualState = ST_BSMINIT; - me->observer = NULL; - me->observerCB = NULL; -} - -/* - * this is the state machine method of the ButtonSM class - */ -bool ButtonSM_processEvent(Event* ev) -{ - ButtonSM* me = (ButtonSM*)ev->target; - bool processed = false; - BSMState oldState = me->state; - - switch (me->state) - { - case ST_BSMINIT: - if (Event_getId(ev) == evBSMInit) - { - me->state = ST_BSMWAIT; - } - break; - case ST_BSMWAIT: - if (Event_getId(ev) == evBSMPollTM) - { - me->state = ST_BSMPOLL; - } - break; - case ST_BSMPOLL: - if (Event_getId(ev) == evBSMDefault) - { - if (Button_read(me->button)==HIGH) - { - me->state = ST_BSMPRESSED; - } - else - { - me->state = ST_BSMRELEASED; - } - } - break; - case ST_BSMPRESSED: - if (Event_getId(ev) == evBSMDefault) - { - me->state = ST_BSMWAIT; - } - break; - case ST_BSMRELEASED: - if (Event_getId(ev) == evBSMDefault) - { - me->state = ST_BSMWAIT; - } - break; - } - if (oldState != me->state) - { - processed = true; - switch (me->state) - { - case ST_BSMINIT: - break; - case ST_BSMWAIT: - POST(me, &ButtonSM_processEvent, evBSMPollTM,POLLTM,0); - break; - case ST_BSMPOLL: - POST(me, &ButtonSM_processEvent, evBSMDefault,0,0); - break; - case ST_BSMPRESSED: - POST(me, &ButtonSM_processEvent, evBSMDefault,0,0); - if (me->actualState != ST_BSMPRESSED) - { - if (me->observerCB != NULL) - { - me->observerCB(me->observer,Button_getId(me->button),true); - me->actualState = ST_BSMPRESSED; - } - } - break; - case ST_BSMRELEASED: - POST(me, &ButtonSM_processEvent, evBSMDefault,0,0); - if (me->actualState != ST_BSMRELEASED) - { - if (me->observerCB != NULL) - { - me->observerCB(me->observer,Button_getId(me->button),false); - me->actualState = ST_BSMRELEASED; - } - } - break; - } - } - return processed; -} - -/* - * this is the start method for the - * state machine of the ButtonSM class - */ -void ButtonSM_startBehaviour(ButtonSM* me) -{ - POST(me, &ButtonSM_processEvent, evBSMInit,0,0); - me->actualState = Button_read(me->button)==HIGH?ST_BSMPRESSED:ST_BSMRELEASED; -} - -/* - * this is the method to set the object and the - * call back method of the ButtonSM class - * this method will be called whenever the - * button changes its state - * as parameters to the callback method will be passed - * the object address, the button id and its state - * if the call back method does not belong to a class, - * then the object address must be set to NULL - */ -void ButtonSM_setObserver(ButtonSM* me, void* observer, buttonObserverCBT observerCB) -{ - me->observer = observer; - me->observerCB = observerCB; -} \ No newline at end of file diff --git a/306-controller_interface.X/board/button/buttonsm.h b/306-controller_interface.X/board/button/buttonsm.h deleted file mode 100644 index 9353774..0000000 --- a/306-controller_interface.X/board/button/buttonsm.h +++ /dev/null @@ -1,69 +0,0 @@ -#ifndef BUTTONSM_DEF -#define BUTTONSM_DEF - -#include -#include -#include "../../xf/xf.h" -#include "button.h" - -/* - * these are the events of the - * button state machine - * be sure to make the first event - * in the enumeration different from 0 - */ -typedef enum BSMEvent -{ - evBSMInit = 10, - evBSMDefault, - evBSMPollTM -} BSMEvent; - -/* - * these are the states of the - * button state machine - */ -typedef enum BSMSTate_ -{ - ST_BSMINIT, - ST_BSMWAIT, - ST_BSMPOLL, - ST_BSMPRESSED, - ST_BSMRELEASED -} BSMState; - -/* - * the associated button will be polled - * each 50 ms. do not make this time - * shorter than TICKINTERVAL - */ -#define POLLTM 50 - -/* - * this is the prototype type of the callback function - * that will be called when the associated button - * changes from released to pressed or inverse. - */ -typedef void (*buttonObserverCBT)(void*, uint8_t, bool); - -/* - * this is the declaration of the ButtonSM class - */ -struct ButtonSM_ -{ - BSMState state; - Button* button; - BSMState actualState; - - buttonObserverCBT observerCB; - void* observer; -}; - -typedef struct ButtonSM_ ButtonSM; - - -void ButtonSM_init(ButtonSM* me, Button* button); -void ButtonSM_startBehaviour(ButtonSM* me); -bool ButtonSM_processEvent(Event* ev); -void ButtonSM_setObserver(ButtonSM* me, void* observer, buttonObserverCBT observerCB); -#endif diff --git a/306-controller_interface.X/board/led/led.c b/306-controller_interface.X/board/led/led.c index cc4a4ba..c91bc4e 100644 --- a/306-controller_interface.X/board/led/led.c +++ b/306-controller_interface.X/board/led/led.c @@ -1,8 +1,13 @@ +/** + * @author Rémi Heredero (remi@heredero.ch) + * @version. 1.0.0 + * @date 2023-06-15 + */ + #include "led.h" #include "../../mcc_generated_files/pin_manager.h" -void LED_init(LED* me, uint8_t id) -{ +void LED_init(LED* me, uint8_t id) { me->id = id; } @@ -10,94 +15,68 @@ void LED_init(LED* me, uint8_t id) * @brief Initialize the Driver * */ -void LED_initHW(LED* me) -{ +void LED_initHW(LED* me) { LED_off(me); } -/* - * for the on and the off methods: - * if the output is push pull, it depends if the - * load is connect to ground or vcc. - * in this case, the load is connected to vcc, - * so on and off are inverted. Change the code as it - * is convenient for your hardware - */ - - -//switch on the led -//maybe you have to adjust your -//low level calls -void LED_on(LED* me) -{ - switch (me->id) - { +void LED_on(void* me) { + LED* l = (LED*) me; + switch (l->id) { case 1: - IO_RB0_SetLow(); + OUTPUT1_SetHigh(); break; case 2: + OUTPUT2_SetHigh(); break; case 3: + OUTPUT3_SetHigh(); break; case 4: + OUTPUT4_SetHigh(); break; case 5: + OUTPUT5_SetHigh(); break; case 6: + OUTPUT6_SetHigh(); break; case 7: + OUTPUT7_SetHigh(); break; case 8: - break; - case 9: - break; - case 10: - break; + OUTPUT8_SetHigh(); + break; } } -//switch off the led -//maybe you have to adjust your -//low level calls -void LED_off(LED* me) -{ - switch (me->id) - { +void LED_off(void* me) { + LED* l = (LED*) me; + switch (l->id) { case 1: - IO_RB0_SetHigh(); + OUTPUT1_SetLow(); break; case 2: + OUTPUT2_SetLow(); break; case 3: + OUTPUT3_SetLow(); break; case 4: + OUTPUT4_SetLow(); break; case 5: + OUTPUT5_SetLow(); break; case 6: + OUTPUT6_SetLow(); break; case 7: + OUTPUT7_SetLow(); break; case 8: - break; - case 9: - break; - case 10: + OUTPUT8_SetLow(); break; - } - + } } -void LED_setState(LED* me, uint8_t state) -{ - if (state == HIGH) - { - LED_on(me); - } - - if (state == LOW) - { - LED_off(me); - } -} \ No newline at end of file diff --git a/306-controller_interface.X/board/led/led.h b/306-controller_interface.X/board/led/led.h index 1bc5873..5255053 100644 --- a/306-controller_interface.X/board/led/led.h +++ b/306-controller_interface.X/board/led/led.h @@ -1,23 +1,43 @@ -#ifndef LED_ONCE -#define LED_ONCE +/** + * @author Rémi Heredero (remi@heredero.ch) + * @version. 1.0.0 + * @date 2023-06-15 + */ + +#ifndef LED_H +#define LED_H #include -/* - * this is the declaration of the Led class +// LED struct +typedef struct { + uint8_t id; // The id of the LED +}LED; + +/** + * Initialize the led + * @param me the led itself + * @param id the id of the led */ -struct LED_ -{ - //has a gpo - uint8_t id; -}; - -typedef struct LED_ LED; - void LED_init(LED* me, uint8_t id); -void LED_initHW(LED* me); -void LED_on(LED* me); -void LED_off(LED* me); -void LED_setState(LED* me,uint8_t state); -#endif +/** + * Initializing the led + * @param me the led itself + */ +void LED_initHW(LED* me); + +/** + * Turn On the led + * @param me the led itself + */ +void LED_on(void* me); + +/** + * Turn Off the led + * @param me the led itself + */ +void LED_off(void* me); + +#endif /* LED_H */ + diff --git a/306-controller_interface.X/factory/factory.c b/306-controller_interface.X/factory/factory.c deleted file mode 100644 index f0f5b58..0000000 --- a/306-controller_interface.X/factory/factory.c +++ /dev/null @@ -1,62 +0,0 @@ -#include "factory.h" - - -//the factory object containing all objects of our system -static Factory theFactory; - - -//all the getters -LED* l() -{ - return &theFactory.l_; -} - -Button* b() -{ - return &theFactory.b_; -} - -ButtonSM* bsm() -{ - return &theFactory.bsm_; -} - -BLControl* blc() -{ - return &theFactory.blc_; -} - - -//initialize all objects -void Factory_init() -{ - LED_init(l(),LID); - LED_initHW(l()); - Button_init(b(),BID, true); - Button_initHW(b()); - ButtonSM_init(bsm(),b()); - BLControl_init(blc()); - - CANINTERFACE_init(); - -; - -} - -void foo(uint32_t a, uint32_t b){ - -} - -//connect objects if required -void Factory_build() { - ButtonSM_setObserver(bsm(), blc(), &BLControl_onButton); - - ECAN_SetRXBnInterruptHandler(CANINTERFACE_newMsg); - //CANINTERFACE_onProcessCan(foo); -} - -//start all state machines -void Factory_start() -{ - ButtonSM_startBehaviour(bsm()); -} diff --git a/306-controller_interface.X/factory/factory.h b/306-controller_interface.X/factory/factory.h deleted file mode 100644 index fa65abc..0000000 --- a/306-controller_interface.X/factory/factory.h +++ /dev/null @@ -1,42 +0,0 @@ -/* this is the Factory class */ - - -#ifndef FACTORY_ONCE -#define FACTORY_ONCE - -#include -#include - -#include "../board/led/led.h" -#include "../board/button/button.h" -#include "../board/button/buttonsm.h" -#include "../app/blcontrol.h" -#include "../middleware/can/can_interface.h" - - -#define BID 1 -#define LID 1 - -void bObs(void*, uint8_t,bool); - -struct Factory_ -{ - LED l_; - Button b_; - ButtonSM bsm_; - BLControl blc_; -}; - -typedef struct Factory_ Factory; - -void Factory_init(); -void Factory_build(); -void Factory_start(); - -//these are global getters for our objects -LED* l(); -Button* b(); -ButtonSM* bsm(); -BLControl* blc(); - -#endif \ No newline at end of file diff --git a/306-controller_interface.X/mcc_generated_files/pin_manager.c b/306-controller_interface.X/mcc_generated_files/pin_manager.c index 87f781b..b9c898e 100644 --- a/306-controller_interface.X/mcc_generated_files/pin_manager.c +++ b/306-controller_interface.X/mcc_generated_files/pin_manager.c @@ -64,16 +64,16 @@ void PIN_MANAGER_Initialize(void) /** TRISx registers */ - TRISA = 0xFF; - TRISB = 0xFE; + TRISA = 0x00; + TRISB = 0xF6; TRISC = 0xFF; /** ANSELx registers */ ANSELC = 0xFF; - ANSELB = 0xF6; - ANSELA = 0x7F; + ANSELB = 0xEE; + ANSELA = 0x00; /** WPUx registers @@ -112,7 +112,8 @@ void PIN_MANAGER_Initialize(void) - CANRXPPS = 0x0B; //RB3->ECAN:CANRX; + CANRXPPS = 0x0C; //RB4->ECAN:CANRX; + RB3PPS = 0x33; //RB3->ECAN:CANTX0; } void PIN_MANAGER_IOC(void) diff --git a/306-controller_interface.X/mcc_generated_files/pin_manager.h b/306-controller_interface.X/mcc_generated_files/pin_manager.h index f6407c3..a4d0d24 100644 --- a/306-controller_interface.X/mcc_generated_files/pin_manager.h +++ b/306-controller_interface.X/mcc_generated_files/pin_manager.h @@ -65,45 +65,165 @@ #define PULL_UP_ENABLED 1 #define PULL_UP_DISABLED 0 -// get/set IO_RA7 aliases -#define IO_RA7_TRIS TRISAbits.TRISA7 -#define IO_RA7_LAT LATAbits.LATA7 -#define IO_RA7_PORT PORTAbits.RA7 -#define IO_RA7_WPU WPUAbits.WPUA7 -#define IO_RA7_OD ODCONAbits.ODCA7 -#define IO_RA7_ANS ANSELAbits.ANSELA7 -#define IO_RA7_SetHigh() do { LATAbits.LATA7 = 1; } while(0) -#define IO_RA7_SetLow() do { LATAbits.LATA7 = 0; } while(0) -#define IO_RA7_Toggle() do { LATAbits.LATA7 = ~LATAbits.LATA7; } while(0) -#define IO_RA7_GetValue() PORTAbits.RA7 -#define IO_RA7_SetDigitalInput() do { TRISAbits.TRISA7 = 1; } while(0) -#define IO_RA7_SetDigitalOutput() do { TRISAbits.TRISA7 = 0; } while(0) -#define IO_RA7_SetPullup() do { WPUAbits.WPUA7 = 1; } while(0) -#define IO_RA7_ResetPullup() do { WPUAbits.WPUA7 = 0; } while(0) -#define IO_RA7_SetPushPull() do { ODCONAbits.ODCA7 = 0; } while(0) -#define IO_RA7_SetOpenDrain() do { ODCONAbits.ODCA7 = 1; } while(0) -#define IO_RA7_SetAnalogMode() do { ANSELAbits.ANSELA7 = 1; } while(0) -#define IO_RA7_SetDigitalMode() do { ANSELAbits.ANSELA7 = 0; } while(0) +// get/set OUTPUT1 aliases +#define OUTPUT1_TRIS TRISAbits.TRISA0 +#define OUTPUT1_LAT LATAbits.LATA0 +#define OUTPUT1_PORT PORTAbits.RA0 +#define OUTPUT1_WPU WPUAbits.WPUA0 +#define OUTPUT1_OD ODCONAbits.ODCA0 +#define OUTPUT1_ANS ANSELAbits.ANSELA0 +#define OUTPUT1_SetHigh() do { LATAbits.LATA0 = 1; } while(0) +#define OUTPUT1_SetLow() do { LATAbits.LATA0 = 0; } while(0) +#define OUTPUT1_Toggle() do { LATAbits.LATA0 = ~LATAbits.LATA0; } while(0) +#define OUTPUT1_GetValue() PORTAbits.RA0 +#define OUTPUT1_SetDigitalInput() do { TRISAbits.TRISA0 = 1; } while(0) +#define OUTPUT1_SetDigitalOutput() do { TRISAbits.TRISA0 = 0; } while(0) +#define OUTPUT1_SetPullup() do { WPUAbits.WPUA0 = 1; } while(0) +#define OUTPUT1_ResetPullup() do { WPUAbits.WPUA0 = 0; } while(0) +#define OUTPUT1_SetPushPull() do { ODCONAbits.ODCA0 = 0; } while(0) +#define OUTPUT1_SetOpenDrain() do { ODCONAbits.ODCA0 = 1; } while(0) +#define OUTPUT1_SetAnalogMode() do { ANSELAbits.ANSELA0 = 1; } while(0) +#define OUTPUT1_SetDigitalMode() do { ANSELAbits.ANSELA0 = 0; } while(0) -// get/set IO_RB0 aliases -#define IO_RB0_TRIS TRISBbits.TRISB0 -#define IO_RB0_LAT LATBbits.LATB0 -#define IO_RB0_PORT PORTBbits.RB0 -#define IO_RB0_WPU WPUBbits.WPUB0 -#define IO_RB0_OD ODCONBbits.ODCB0 -#define IO_RB0_ANS ANSELBbits.ANSELB0 -#define IO_RB0_SetHigh() do { LATBbits.LATB0 = 1; } while(0) -#define IO_RB0_SetLow() do { LATBbits.LATB0 = 0; } while(0) -#define IO_RB0_Toggle() do { LATBbits.LATB0 = ~LATBbits.LATB0; } while(0) -#define IO_RB0_GetValue() PORTBbits.RB0 -#define IO_RB0_SetDigitalInput() do { TRISBbits.TRISB0 = 1; } while(0) -#define IO_RB0_SetDigitalOutput() do { TRISBbits.TRISB0 = 0; } while(0) -#define IO_RB0_SetPullup() do { WPUBbits.WPUB0 = 1; } while(0) -#define IO_RB0_ResetPullup() do { WPUBbits.WPUB0 = 0; } while(0) -#define IO_RB0_SetPushPull() do { ODCONBbits.ODCB0 = 0; } while(0) -#define IO_RB0_SetOpenDrain() do { ODCONBbits.ODCB0 = 1; } while(0) -#define IO_RB0_SetAnalogMode() do { ANSELBbits.ANSELB0 = 1; } while(0) -#define IO_RB0_SetDigitalMode() do { ANSELBbits.ANSELB0 = 0; } while(0) +// get/set OUTPUT2 aliases +#define OUTPUT2_TRIS TRISAbits.TRISA1 +#define OUTPUT2_LAT LATAbits.LATA1 +#define OUTPUT2_PORT PORTAbits.RA1 +#define OUTPUT2_WPU WPUAbits.WPUA1 +#define OUTPUT2_OD ODCONAbits.ODCA1 +#define OUTPUT2_ANS ANSELAbits.ANSELA1 +#define OUTPUT2_SetHigh() do { LATAbits.LATA1 = 1; } while(0) +#define OUTPUT2_SetLow() do { LATAbits.LATA1 = 0; } while(0) +#define OUTPUT2_Toggle() do { LATAbits.LATA1 = ~LATAbits.LATA1; } while(0) +#define OUTPUT2_GetValue() PORTAbits.RA1 +#define OUTPUT2_SetDigitalInput() do { TRISAbits.TRISA1 = 1; } while(0) +#define OUTPUT2_SetDigitalOutput() do { TRISAbits.TRISA1 = 0; } while(0) +#define OUTPUT2_SetPullup() do { WPUAbits.WPUA1 = 1; } while(0) +#define OUTPUT2_ResetPullup() do { WPUAbits.WPUA1 = 0; } while(0) +#define OUTPUT2_SetPushPull() do { ODCONAbits.ODCA1 = 0; } while(0) +#define OUTPUT2_SetOpenDrain() do { ODCONAbits.ODCA1 = 1; } while(0) +#define OUTPUT2_SetAnalogMode() do { ANSELAbits.ANSELA1 = 1; } while(0) +#define OUTPUT2_SetDigitalMode() do { ANSELAbits.ANSELA1 = 0; } while(0) + +// get/set OUTPUT3 aliases +#define OUTPUT3_TRIS TRISAbits.TRISA2 +#define OUTPUT3_LAT LATAbits.LATA2 +#define OUTPUT3_PORT PORTAbits.RA2 +#define OUTPUT3_WPU WPUAbits.WPUA2 +#define OUTPUT3_OD ODCONAbits.ODCA2 +#define OUTPUT3_ANS ANSELAbits.ANSELA2 +#define OUTPUT3_SetHigh() do { LATAbits.LATA2 = 1; } while(0) +#define OUTPUT3_SetLow() do { LATAbits.LATA2 = 0; } while(0) +#define OUTPUT3_Toggle() do { LATAbits.LATA2 = ~LATAbits.LATA2; } while(0) +#define OUTPUT3_GetValue() PORTAbits.RA2 +#define OUTPUT3_SetDigitalInput() do { TRISAbits.TRISA2 = 1; } while(0) +#define OUTPUT3_SetDigitalOutput() do { TRISAbits.TRISA2 = 0; } while(0) +#define OUTPUT3_SetPullup() do { WPUAbits.WPUA2 = 1; } while(0) +#define OUTPUT3_ResetPullup() do { WPUAbits.WPUA2 = 0; } while(0) +#define OUTPUT3_SetPushPull() do { ODCONAbits.ODCA2 = 0; } while(0) +#define OUTPUT3_SetOpenDrain() do { ODCONAbits.ODCA2 = 1; } while(0) +#define OUTPUT3_SetAnalogMode() do { ANSELAbits.ANSELA2 = 1; } while(0) +#define OUTPUT3_SetDigitalMode() do { ANSELAbits.ANSELA2 = 0; } while(0) + +// get/set OUTPUT4 aliases +#define OUTPUT4_TRIS TRISAbits.TRISA3 +#define OUTPUT4_LAT LATAbits.LATA3 +#define OUTPUT4_PORT PORTAbits.RA3 +#define OUTPUT4_WPU WPUAbits.WPUA3 +#define OUTPUT4_OD ODCONAbits.ODCA3 +#define OUTPUT4_ANS ANSELAbits.ANSELA3 +#define OUTPUT4_SetHigh() do { LATAbits.LATA3 = 1; } while(0) +#define OUTPUT4_SetLow() do { LATAbits.LATA3 = 0; } while(0) +#define OUTPUT4_Toggle() do { LATAbits.LATA3 = ~LATAbits.LATA3; } while(0) +#define OUTPUT4_GetValue() PORTAbits.RA3 +#define OUTPUT4_SetDigitalInput() do { TRISAbits.TRISA3 = 1; } while(0) +#define OUTPUT4_SetDigitalOutput() do { TRISAbits.TRISA3 = 0; } while(0) +#define OUTPUT4_SetPullup() do { WPUAbits.WPUA3 = 1; } while(0) +#define OUTPUT4_ResetPullup() do { WPUAbits.WPUA3 = 0; } while(0) +#define OUTPUT4_SetPushPull() do { ODCONAbits.ODCA3 = 0; } while(0) +#define OUTPUT4_SetOpenDrain() do { ODCONAbits.ODCA3 = 1; } while(0) +#define OUTPUT4_SetAnalogMode() do { ANSELAbits.ANSELA3 = 1; } while(0) +#define OUTPUT4_SetDigitalMode() do { ANSELAbits.ANSELA3 = 0; } while(0) + +// get/set OUTPUT5 aliases +#define OUTPUT5_TRIS TRISAbits.TRISA4 +#define OUTPUT5_LAT LATAbits.LATA4 +#define OUTPUT5_PORT PORTAbits.RA4 +#define OUTPUT5_WPU WPUAbits.WPUA4 +#define OUTPUT5_OD ODCONAbits.ODCA4 +#define OUTPUT5_ANS ANSELAbits.ANSELA4 +#define OUTPUT5_SetHigh() do { LATAbits.LATA4 = 1; } while(0) +#define OUTPUT5_SetLow() do { LATAbits.LATA4 = 0; } while(0) +#define OUTPUT5_Toggle() do { LATAbits.LATA4 = ~LATAbits.LATA4; } while(0) +#define OUTPUT5_GetValue() PORTAbits.RA4 +#define OUTPUT5_SetDigitalInput() do { TRISAbits.TRISA4 = 1; } while(0) +#define OUTPUT5_SetDigitalOutput() do { TRISAbits.TRISA4 = 0; } while(0) +#define OUTPUT5_SetPullup() do { WPUAbits.WPUA4 = 1; } while(0) +#define OUTPUT5_ResetPullup() do { WPUAbits.WPUA4 = 0; } while(0) +#define OUTPUT5_SetPushPull() do { ODCONAbits.ODCA4 = 0; } while(0) +#define OUTPUT5_SetOpenDrain() do { ODCONAbits.ODCA4 = 1; } while(0) +#define OUTPUT5_SetAnalogMode() do { ANSELAbits.ANSELA4 = 1; } while(0) +#define OUTPUT5_SetDigitalMode() do { ANSELAbits.ANSELA4 = 0; } while(0) + +// get/set OUTPUT6 aliases +#define OUTPUT6_TRIS TRISAbits.TRISA5 +#define OUTPUT6_LAT LATAbits.LATA5 +#define OUTPUT6_PORT PORTAbits.RA5 +#define OUTPUT6_WPU WPUAbits.WPUA5 +#define OUTPUT6_OD ODCONAbits.ODCA5 +#define OUTPUT6_ANS ANSELAbits.ANSELA5 +#define OUTPUT6_SetHigh() do { LATAbits.LATA5 = 1; } while(0) +#define OUTPUT6_SetLow() do { LATAbits.LATA5 = 0; } while(0) +#define OUTPUT6_Toggle() do { LATAbits.LATA5 = ~LATAbits.LATA5; } while(0) +#define OUTPUT6_GetValue() PORTAbits.RA5 +#define OUTPUT6_SetDigitalInput() do { TRISAbits.TRISA5 = 1; } while(0) +#define OUTPUT6_SetDigitalOutput() do { TRISAbits.TRISA5 = 0; } while(0) +#define OUTPUT6_SetPullup() do { WPUAbits.WPUA5 = 1; } while(0) +#define OUTPUT6_ResetPullup() do { WPUAbits.WPUA5 = 0; } while(0) +#define OUTPUT6_SetPushPull() do { ODCONAbits.ODCA5 = 0; } while(0) +#define OUTPUT6_SetOpenDrain() do { ODCONAbits.ODCA5 = 1; } while(0) +#define OUTPUT6_SetAnalogMode() do { ANSELAbits.ANSELA5 = 1; } while(0) +#define OUTPUT6_SetDigitalMode() do { ANSELAbits.ANSELA5 = 0; } while(0) + +// get/set OUTPUT7 aliases +#define OUTPUT7_TRIS TRISAbits.TRISA6 +#define OUTPUT7_LAT LATAbits.LATA6 +#define OUTPUT7_PORT PORTAbits.RA6 +#define OUTPUT7_WPU WPUAbits.WPUA6 +#define OUTPUT7_OD ODCONAbits.ODCA6 +#define OUTPUT7_ANS ANSELAbits.ANSELA6 +#define OUTPUT7_SetHigh() do { LATAbits.LATA6 = 1; } while(0) +#define OUTPUT7_SetLow() do { LATAbits.LATA6 = 0; } while(0) +#define OUTPUT7_Toggle() do { LATAbits.LATA6 = ~LATAbits.LATA6; } while(0) +#define OUTPUT7_GetValue() PORTAbits.RA6 +#define OUTPUT7_SetDigitalInput() do { TRISAbits.TRISA6 = 1; } while(0) +#define OUTPUT7_SetDigitalOutput() do { TRISAbits.TRISA6 = 0; } while(0) +#define OUTPUT7_SetPullup() do { WPUAbits.WPUA6 = 1; } while(0) +#define OUTPUT7_ResetPullup() do { WPUAbits.WPUA6 = 0; } while(0) +#define OUTPUT7_SetPushPull() do { ODCONAbits.ODCA6 = 0; } while(0) +#define OUTPUT7_SetOpenDrain() do { ODCONAbits.ODCA6 = 1; } while(0) +#define OUTPUT7_SetAnalogMode() do { ANSELAbits.ANSELA6 = 1; } while(0) +#define OUTPUT7_SetDigitalMode() do { ANSELAbits.ANSELA6 = 0; } while(0) + +// get/set OUTPUT8 aliases +#define OUTPUT8_TRIS TRISAbits.TRISA7 +#define OUTPUT8_LAT LATAbits.LATA7 +#define OUTPUT8_PORT PORTAbits.RA7 +#define OUTPUT8_WPU WPUAbits.WPUA7 +#define OUTPUT8_OD ODCONAbits.ODCA7 +#define OUTPUT8_ANS ANSELAbits.ANSELA7 +#define OUTPUT8_SetHigh() do { LATAbits.LATA7 = 1; } while(0) +#define OUTPUT8_SetLow() do { LATAbits.LATA7 = 0; } while(0) +#define OUTPUT8_Toggle() do { LATAbits.LATA7 = ~LATAbits.LATA7; } while(0) +#define OUTPUT8_GetValue() PORTAbits.RA7 +#define OUTPUT8_SetDigitalInput() do { TRISAbits.TRISA7 = 1; } while(0) +#define OUTPUT8_SetDigitalOutput() do { TRISAbits.TRISA7 = 0; } while(0) +#define OUTPUT8_SetPullup() do { WPUAbits.WPUA7 = 1; } while(0) +#define OUTPUT8_ResetPullup() do { WPUAbits.WPUA7 = 0; } while(0) +#define OUTPUT8_SetPushPull() do { ODCONAbits.ODCA7 = 0; } while(0) +#define OUTPUT8_SetOpenDrain() do { ODCONAbits.ODCA7 = 1; } while(0) +#define OUTPUT8_SetAnalogMode() do { ANSELAbits.ANSELA7 = 1; } while(0) +#define OUTPUT8_SetDigitalMode() do { ANSELAbits.ANSELA7 = 0; } while(0) // get/set RB3 procedures #define RB3_SetHigh() do { LATBbits.LATB3 = 1; } while(0) @@ -117,6 +237,18 @@ #define RB3_SetAnalogMode() do { ANSELBbits.ANSELB3 = 1; } while(0) #define RB3_SetDigitalMode() do { ANSELBbits.ANSELB3 = 0; } while(0) +// get/set RB4 procedures +#define RB4_SetHigh() do { LATBbits.LATB4 = 1; } while(0) +#define RB4_SetLow() do { LATBbits.LATB4 = 0; } while(0) +#define RB4_Toggle() do { LATBbits.LATB4 = ~LATBbits.LATB4; } while(0) +#define RB4_GetValue() PORTBbits.RB4 +#define RB4_SetDigitalInput() do { TRISBbits.TRISB4 = 1; } while(0) +#define RB4_SetDigitalOutput() do { TRISBbits.TRISB4 = 0; } while(0) +#define RB4_SetPullup() do { WPUBbits.WPUB4 = 1; } while(0) +#define RB4_ResetPullup() do { WPUBbits.WPUB4 = 0; } while(0) +#define RB4_SetAnalogMode() do { ANSELBbits.ANSELB4 = 1; } while(0) +#define RB4_SetDigitalMode() do { ANSELBbits.ANSELB4 = 0; } while(0) + /** @Param none diff --git a/306-controller_interface.X/middleware/can/can_interface.c b/306-controller_interface.X/middleware/can/can_interface.c deleted file mode 100644 index 4ac0fce..0000000 --- a/306-controller_interface.X/middleware/can/can_interface.c +++ /dev/null @@ -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); -} diff --git a/306-controller_interface.X/middleware/can/can_interface.h b/306-controller_interface.X/middleware/can/can_interface.h deleted file mode 100644 index 5508af3..0000000 --- a/306-controller_interface.X/middleware/can/can_interface.h +++ /dev/null @@ -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 diff --git a/306-controller_interface.X/middleware/can/can_sender.c b/306-controller_interface.X/middleware/can/can_sender.c deleted file mode 100644 index 71163be..0000000 --- a/306-controller_interface.X/middleware/can/can_sender.c +++ /dev/null @@ -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; -} diff --git a/306-controller_interface.X/middleware/can/can_sender.h b/306-controller_interface.X/middleware/can/can_sender.h deleted file mode 100644 index 3703e49..0000000 --- a/306-controller_interface.X/middleware/can/can_sender.h +++ /dev/null @@ -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 diff --git a/306-controller_interface.X/middleware/can_interface.c b/306-controller_interface.X/middleware/can_interface.c new file mode 100644 index 0000000..2656dc9 --- /dev/null +++ b/306-controller_interface.X/middleware/can_interface.c @@ -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); +} diff --git a/306-controller_interface.X/middleware/can_interface.h b/306-controller_interface.X/middleware/can_interface.h new file mode 100644 index 0000000..d08b1cb --- /dev/null +++ b/306-controller_interface.X/middleware/can_interface.h @@ -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 diff --git a/306-controller_interface.X/nbproject/configurations.xml b/306-controller_interface.X/nbproject/configurations.xml index cc66702..9db35d3 100644 --- a/306-controller_interface.X/nbproject/configurations.xml +++ b/306-controller_interface.X/nbproject/configurations.xml @@ -5,19 +5,10 @@ displayName="Header Files" projectFiles="true"> - app/blcontrol.h + app/factory/factory.h - - board/button/buttonsm.h - board/button/button.h - - - board/led/led.h - - - - factory/factory.h + board/led/led.h mcc_generated_files/memory.h - - middleware/can/can_interface.h - middleware/can/can_sender.h - + middleware/can_interface.h xf/event.h @@ -50,19 +38,11 @@ displayName="Source Files" projectFiles="true"> - app/blcontrol.c + app/factory/factory.c + app/main.c - - board/button/button.c - board/button/buttonsm.c - - - board/led/led.c - - - - factory/factory.c + board/led/led.c mcc_generated_files/memory.c - - middleware/can/can_interface.c - middleware/can/can_sender.c - + middleware/can_interface.c xf/event.c xf/xf.c - main.c + + + + + + + + + + + + + + + diff --git a/306-controller_interface.X/ss22ep.mc3 b/306-controller_interface.X/ss22ep.mc3 index 9f4cf09..33d9ca8 100644 --- a/306-controller_interface.X/ss22ep.mc3 +++ b/306-controller_interface.X/ss22ep.mc3 @@ -11676,35 +11676,35 @@ - + OUTPUT1 - + OUTPUT2 - + OUTPUT3 - + OUTPUT4 - + OUTPUT5 - + OUTPUT6 - + OUTPUT7 - + OUTPUT8 @@ -11780,35 +11780,35 @@ - disabled + output - disabled + output - disabled + output - disabled + output - disabled + output - disabled + output - disabled + output - disabled + output @@ -11884,35 +11884,35 @@ - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled @@ -11980,35 +11980,35 @@ - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled - disabled + enabled @@ -14352,11 +14352,11 @@ - 255 + 0 - 246 + 238 @@ -14364,7 +14364,7 @@ - 11 + 12 @@ -14588,11 +14588,11 @@ - 0 + 51 - 51 + 0 @@ -14720,11 +14720,11 @@ - 255 + 0 - 238 + 246 @@ -14768,35 +14768,35 @@ - analog + digital - analog + digital - analog + digital - analog + digital - analog + digital - analog + digital - analog + digital - analog + digital @@ -14812,11 +14812,11 @@ - digital + analog - analog + digital @@ -14864,7 +14864,7 @@ - 11 + 12 @@ -15608,11 +15608,11 @@ - 0 + 51 - 51 + 0 @@ -15824,35 +15824,35 @@ - input + output - input + output - input + output - input + output - input + output - input + output - input + output - input + output @@ -15868,11 +15868,11 @@ - input + output - output + input @@ -17163,7 +17163,27 @@ Window delay time 87.5% - + + + mcc_generated_files\mcc.h + a2db7e36e878f686c2bf0c2ef586ef1c6570fa2f27119b4be7b52af6403091a4 + + + mcc_generated_files\device_config.h + 89c6172ff575ce515b93f2fbc85dcedc2978e58a8e0e1fbdc52e42511ae3bc05 + + + mcc_generated_files\interrupt_manager.h + 9c2f1ae45f2ac887bb3e8b3763e1a394a6a22ffe4e9ae1c20c336fe6f12da1aa + + + mcc_generated_files\memory.c + 17fb4759c4719b77287f6c4be48edfbcf117b5b8398b771c434f23aceac256e0 + + + mcc_generated_files\tmr0.h + 6661ab783aae9f11e952805f9bca14209ec06551939552123056eefd5524fff8 + main.c cae37ae3b36cf22e97e106633433f5c00a66dd5d38ec353eb67fbbb0d88bde4d @@ -17172,57 +17192,37 @@ mcc_generated_files\device_config.c 39a6d1181ef5eab59c7dde2c52a9ea889465d4da43262200f3322abc45e77739 - - mcc_generated_files\device_config.h - 89c6172ff575ce515b93f2fbc85dcedc2978e58a8e0e1fbdc52e42511ae3bc05 - - - mcc_generated_files\ecan.c - ea62f50d319e1e537d7632774728ad6a779f442e896d043dbdea8066d028a6c6 - - - mcc_generated_files\ecan.h - aa9a50aae81bab76b876ea6123777af7d6a6d0a58fe953be27e8b88776395b2e - - - mcc_generated_files\interrupt_manager.c - df04edcd2c7d85ef90a8dbe4e46f1b1c9487b872153f4f2321249a4ce0d9635f - - - mcc_generated_files\interrupt_manager.h - 9c2f1ae45f2ac887bb3e8b3763e1a394a6a22ffe4e9ae1c20c336fe6f12da1aa - - - mcc_generated_files\mcc.c - cc9ed44843b509879e6a3f676b561ecde91e1df88d855cf7eca77e1afc8920ca - - - mcc_generated_files\mcc.h - a2db7e36e878f686c2bf0c2ef586ef1c6570fa2f27119b4be7b52af6403091a4 - - - mcc_generated_files\memory.c - 17fb4759c4719b77287f6c4be48edfbcf117b5b8398b771c434f23aceac256e0 - - - mcc_generated_files\memory.h - fbbca4e9d7ce92ddcc637d82b694a1f5cbefa75710a8a18bb1dc9ab5161f0924 - - - mcc_generated_files\pin_manager.c - 30d27d3c212d3420d65a30996dd68e9c3ba572d0d2bf50924ad025e552b9dcbf - mcc_generated_files\pin_manager.h - 76c9c709283a0d30fc6bf8d663b1394044aa806dbd1282fad68c1ebb0a221d36 + 611a409602fd8fba29be052e06a3ce86ad0a3b723b5e0f4c1a998854de7f9a7b mcc_generated_files\tmr0.c e0b4d075e819024ae77ea60a2c01182fdca45b783980cb50358d0a614736339d - mcc_generated_files\tmr0.h - 6661ab783aae9f11e952805f9bca14209ec06551939552123056eefd5524fff8 + mcc_generated_files\pin_manager.c + 04b16a3d3fcbbb333ee6fb545a405b76aba47ef3935be548bf2b8165c43c5654 + + + mcc_generated_files\ecan.c + ea62f50d319e1e537d7632774728ad6a779f442e896d043dbdea8066d028a6c6 + + + mcc_generated_files\mcc.c + cc9ed44843b509879e6a3f676b561ecde91e1df88d855cf7eca77e1afc8920ca + + + mcc_generated_files\interrupt_manager.c + df04edcd2c7d85ef90a8dbe4e46f1b1c9487b872153f4f2321249a4ce0d9635f + + + mcc_generated_files\ecan.h + aa9a50aae81bab76b876ea6123777af7d6a6d0a58fe953be27e8b88776395b2e + + + mcc_generated_files\memory.h + fbbca4e9d7ce92ddcc637d82b694a1f5cbefa75710a8a18bb1dc9ab5161f0924 \ No newline at end of file