test
Fork of CANnucleo by
Revision 21:bcd8161f8f6c, committed 2016-05-19
- Comitter:
- hudakz
- Date:
- Thu May 19 17:16:59 2016 +0000
- Parent:
- 19:249e3f065956
- Commit message:
- Added support for attaching member functions as interrupt handlers.
Changed in this revision
diff -r 249e3f065956 -r bcd8161f8f6c CAN.cpp --- a/CAN.cpp Sat Mar 19 21:16:10 2016 +0000 +++ b/CAN.cpp Thu May 19 17:16:59 2016 +0000 @@ -31,10 +31,9 @@ * @retval */ CAN::CAN(PinName rxPin, PinName txPin, FunctionalState abom /* = ENABLE */) : - _can(), _irq() { - can_init(&_can, rxPin, txPin, abom); - can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); + can_init(rxPin, txPin, abom); + can_irq_init((uint32_t)this, (&CAN::_irq_handler)); } /** @@ -44,8 +43,8 @@ * @retval */ CAN::~CAN(void) { - can_irq_free(&_can); - can_free(&_can); + can_irq_free(); + can_free(); } /** @@ -55,7 +54,7 @@ * @retval */ int CAN::frequency(int f) { - return can_frequency(&_can, f); + return can_frequency(f); } /** @@ -65,7 +64,7 @@ * @retval */ int CAN::write(CANMessage msg) { - return can_write(&_can, msg, 0); + return can_write(msg, 0); } /** @@ -75,7 +74,7 @@ * @retval */ int CAN::read(CANMessage& msg, int handle) { - return can_read(&_can, &msg, handle); + return can_read(&msg, handle); } /** @@ -85,7 +84,7 @@ * @retval */ void CAN::reset(void) { - can_reset(&_can); + can_reset(); } /** @@ -95,7 +94,7 @@ * @retval */ unsigned char CAN::rderror(void) { - return can_rderror(&_can); + return can_rderror(); } /** @@ -105,7 +104,7 @@ * @retval */ unsigned char CAN::tderror(void) { - return can_tderror(&_can); + return can_tderror(); } /** @@ -115,7 +114,7 @@ * @retval */ void CAN::monitor(bool silent) { - can_monitor(&_can, (silent) ? 1 : 0); + can_monitor((silent) ? 1 : 0); } /** @@ -125,7 +124,7 @@ * @retval */ int CAN::mode(Mode mode) { - return can_mode(&_can, (CanMode) mode); + return can_mode((CanMode) mode); } /** @@ -182,14 +181,14 @@ * | | | * STID[10:3] STID[2:0] IDE * - * Keep in mind that filter #0 was already set up in the constructor to receive all CAN messages by default. + * Recall that filter #0 has been set up in the constructor to receive all CAN messages by default. * So we have to reconfigure it. If we were set up filter #1 here then filter #0 would receive all the messages * and no message would reach filter #1! * - * To set up filter #0 we call: + * To reconfigure (set up) filter #0 we call: * can.filter(0x0207 << 21, 0xFFE00004, CANAny, 0); * - * Only these bits (set to 1) of filter id are compared with the corresponding + * Only these bits of 'Filter id' (set to 1 here in 'Filter mask') are compared with the corresponding * bits of received message (the others are disregarded) * | * --------------------------------- @@ -209,20 +208,20 @@ * ||||| |||||||| ||||| || * ----------------------- * | - * These bits (set to 0 in filter mask) are disregarded (masked). + * These bits (set to 0 in 'Filter mask') are disregarded (masked). * They can have arbitrary values. * * NOTE: For the meaning of individual bits see the mapping of 32-bits explained above. * * @param format: This parameter must be CANAny * @param handle: Selects the filter. This parameter must be a number between 0 and 13. - * @param retval: 0 - successful - * 1 - error - * 2 - busy - * 3 - time out + * @retval 0 - successful + * 1 - error + * 2 - busy + * 3 - time out */ int CAN::filter(unsigned int id, unsigned int mask, CANFormat format /* = CANAny */, int handle /* = 0 */) { - return can_filter(&_can, id, mask, format, handle); + return can_filter(id, mask, format, handle); } /** @@ -234,10 +233,8 @@ */ void CAN::attach(void (*fptr) (void), IrqType type) { HAL_NVIC_DisableIRQ(CAN_IRQ); - if(fptr) { - can_irq_set(fptr); - } - can_irq_init(&_can, &CAN::_irq_handler, (uint32_t) this); + if(fptr) + _irq[(CanIrqType)type].attach(fptr); HAL_NVIC_EnableIRQ(CAN_IRQ); } @@ -259,3 +256,5 @@ + +
diff -r 249e3f065956 -r bcd8161f8f6c CAN.h --- a/CAN.h Sat Mar 19 21:16:10 2016 +0000 +++ b/CAN.h Thu May 19 17:16:59 2016 +0000 @@ -250,7 +250,7 @@ * @param format format to filter on (Default CANAny) * @param handle message filter handle (Optional) * - * @returns 0 - successful + * @retval 0 - successful * 1 - error * 2 - busy * 3 - time out @@ -294,19 +294,15 @@ */ template<typename T> void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { - if((mptr != NULL) && (tptr != NULL)) { + HAL_NVIC_DisableIRQ(CAN_IRQ); + if((tptr != NULL) && (mptr != NULL)) _irq[type].attach(tptr, mptr); -// can_irq_set(&_can, (CanIrqType)type, 1); - } -// else { -// can_irq_set(&_can, (CanIrqType)type, 0); -// } + HAL_NVIC_EnableIRQ(CAN_IRQ); } static void _irq_handler(uint32_t id, CanIrqType type); protected: - can_t _can; FunctionPointer _irq[9]; };
diff -r 249e3f065956 -r bcd8161f8f6c can_api.c --- a/can_api.c Sat Mar 19 21:16:10 2016 +0000 +++ b/can_api.c Thu May 19 17:16:59 2016 +0000 @@ -29,8 +29,11 @@ #include "can_helper.h" #include "pinmap.h" -extern void (*rxCompleteCallback) (void); -extern CAN_HandleTypeDef _canHandle; +extern void (*rxCompleteCallback)(void); +extern CAN_HandleTypeDef _canHandle; + +static uint32_t irq_id = 0; +static can_irq_handler irq_handler = 0; /** * @brief @@ -38,9 +41,9 @@ * @param * @retval */ -void can_init(can_t* obj, PinName rd, PinName td, FunctionalState abom) { - initCAN(obj, rd, td, abom); - can_filter(obj, 0, 0, CANAny, 0); +void can_init(PinName rd, PinName td, FunctionalState abom) { + initCAN(rd, td, abom); + can_filter(0, 0, CANAny, 0); } /** @@ -49,7 +52,7 @@ * @param * @retval */ -void can_free(can_t* obj) { +void can_free(void) { HAL_CAN_MspDeInit(&_canHandle); } @@ -59,7 +62,7 @@ * @param * @retval */ -int can_frequency(can_t* obj, int hz) { +int can_frequency(int hz) { HAL_NVIC_DisableIRQ(CAN_IRQ); #if defined(TARGET_NUCLEO_F072RB) || \ @@ -224,7 +227,21 @@ * @param * @retval */ -void can_irq_init(can_t* obj, can_irq_handler handler, uint32_t id) { +void can_callback(void) { + irq_handler(irq_id, IRQ_RX); +} + +/** + * @brief + * @note + * @param + * @retval + */ +void can_irq_init(uint32_t id, can_irq_handler handler) { + irq_id = id; + irq_handler = handler; + rxCompleteCallback = can_callback; + if(HAL_CAN_Receive_IT(&_canHandle, CAN_FIFO0) != HAL_OK) { #ifdef DEBUG printf("CAN reception initialization error\r\n"); @@ -238,9 +255,9 @@ * @param * @retval */ -void can_irq_free(can_t* obj) { +void can_irq_free(void) { rxCompleteCallback = 0; -} +} /** * @brief @@ -248,17 +265,7 @@ * @param * @retval */ -void can_irq_set(void (*fptr) (void)) { - rxCompleteCallback = fptr; -} - -/** - * @brief - * @note - * @param - * @retval - */ -int can_write(can_t* obj, CAN_Message msg, int cc) { +int can_write(CAN_Message msg, int cc) { int i = 0; if(msg.format == CANStandard) { @@ -293,7 +300,7 @@ * @param * @retval */ -int can_read(can_t* obj, CAN_Message* msg, int handle) { +int can_read(CAN_Message* msg, int handle) { int i; msg->id = _canHandle.pRxMsg->IDE == CAN_ID_STD ? _canHandle.pRxMsg->StdId : _canHandle.pRxMsg->ExtId; msg->type = _canHandle.pRxMsg->RTR == CAN_RTR_DATA ? CANData : CANRemote; @@ -311,7 +318,7 @@ * @param * @retval */ -int can_mode(can_t* obj, CanMode mode) { +int can_mode(CanMode mode) { switch(mode) { case MODE_RESET: return HAL_ERROR; @@ -346,7 +353,7 @@ * @param * @retval */ -int can_filter(can_t* obj, uint32_t id, uint32_t mask, CANFormat format /*=CANAny*/, int32_t handle /*=0*/ ) { +int can_filter(uint32_t id, uint32_t mask, CANFormat format /*=CANAny*/, int32_t handle /*=0*/ ) { CAN_FilterConfTypeDef sFilterConfig; sFilterConfig.FilterNumber = handle; // Specifies the filter number (must be a number between 0 and 13 at 32-bit filter scale) @@ -368,7 +375,7 @@ * @param * @retval */ -void can_reset(can_t* obj) { +void can_reset(void) { __HAL_CAN_RESET_HANDLE_STATE(&_canHandle); } @@ -378,7 +385,7 @@ * @param * @retval */ -unsigned char can_rderror(can_t* obj) { +unsigned char can_rderror(void) { return HAL_CAN_GetError(&_canHandle); } @@ -388,7 +395,7 @@ * @param * @retval */ -unsigned char can_tderror(can_t* obj) { +unsigned char can_tderror(void) { return HAL_CAN_GetError(&_canHandle); } @@ -398,7 +405,7 @@ * @param * @retval */ -void can_monitor(can_t* obj, int silent) { +void can_monitor(int silent) { // not implemented } @@ -407,3 +414,4 @@ +
diff -r 249e3f065956 -r bcd8161f8f6c can_api.h --- a/can_api.h Sat Mar 19 21:16:10 2016 +0000 +++ b/can_api.h Thu May 19 17:16:59 2016 +0000 @@ -71,31 +71,20 @@ typedef void (*can_irq_handler)(uint32_t id, CanIrqType type); -struct can_s { - CAN_TypeDef *dev; - int index; -}; - -typedef struct can_s can_t; - -void can_init (can_t *obj, PinName rd, PinName td, FunctionalState abom); -void can_free (can_t *obj); -int can_frequency(can_t *obj, int hz); - -void can_irq_init (can_t *obj, can_irq_handler handler, uint32_t id); -void can_irq_free (can_t *obj); -//void can_irq_set (can_t *obj, CanIrqType irq, uint32_t enable); -void can_irq_set(void (*fptr)(void)); - - -int can_write (can_t *obj, CAN_Message, int cc); -int can_read (can_t *obj, CAN_Message *msg, int handle); -int can_mode (can_t *obj, CanMode mode); -int can_filter (can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle); -void can_reset (can_t *obj); -unsigned char can_rderror (can_t *obj); -unsigned char can_tderror (can_t *obj); -void can_monitor (can_t *obj, int silent); +void can_init (PinName rd, PinName td, FunctionalState abom); +void can_free (void); +int can_frequency(int hz); +void can_irq_init (uint32_t id, can_irq_handler handler); +void can_irq_free (void); +int can_write (CAN_Message, int cc); +int can_read (CAN_Message *msg, int handle); +int can_mode (CanMode mode); +int can_filter (uint32_t id, uint32_t mask, CANFormat format, int32_t handle); +void can_reset (void); +unsigned char can_rderror (void); +unsigned char can_tderror (void); +void can_monitor (int silent); +void can_callback (void); #ifdef __cplusplus };
diff -r 249e3f065956 -r bcd8161f8f6c stm32f0xx_hal_msp.c --- a/stm32f0xx_hal_msp.c Sat Mar 19 21:16:10 2016 +0000 +++ b/stm32f0xx_hal_msp.c Thu May 19 17:16:59 2016 +0000 @@ -50,7 +50,7 @@ PinName _rxPin; PinName _txPin; -void (*rxCompleteCallback) (void); +void (*rxCompleteCallback)(void); /** * @brief CAN initialization. @@ -60,7 +60,7 @@ * @param abom: Automatic recovery from bus-off state * @retval None */ -void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom) { +void initCAN(PinName rxPin, PinName txPin, FunctionalState abom) { _rxPin = rxPin; _txPin = txPin; @@ -250,3 +250,4 @@ __HAL_CAN_ENABLE_IT(_canHandle, CAN_IT_FMP0); } #endif +
diff -r 249e3f065956 -r bcd8161f8f6c stm32f0xx_hal_msp.h --- a/stm32f0xx_hal_msp.h Sat Mar 19 21:16:10 2016 +0000 +++ b/stm32f0xx_hal_msp.h Thu May 19 17:16:59 2016 +0000 @@ -40,8 +40,6 @@ #include "pinmap.h" -typedef struct can_s can_t; - #ifdef __cplusplus extern "C" { @@ -55,7 +53,7 @@ * @param abom: Automatic recovery from bus-off state * @retval None */ -void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom); +void initCAN( PinName rxPin, PinName txPin, FunctionalState abom); /** * @brief CAN MSP Initialization @@ -94,4 +92,4 @@ #endif -#endif \ No newline at end of file +#endif
diff -r 249e3f065956 -r bcd8161f8f6c stm32f1xx_hal_msp.c --- a/stm32f1xx_hal_msp.c Sat Mar 19 21:16:10 2016 +0000 +++ b/stm32f1xx_hal_msp.c Thu May 19 17:16:59 2016 +0000 @@ -49,7 +49,7 @@ PinName _rxPin; PinName _txPin; -void (*rxCompleteCallback) (void); +void (*rxCompleteCallback)(void); /** * @brief CAN initialization. @@ -59,7 +59,7 @@ * @param abom: Automatic recovery from bus-off state * @retval None */ -void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom) { +void initCAN(PinName rxPin, PinName txPin, FunctionalState abom) { _rxPin = rxPin; _txPin = txPin; @@ -251,3 +251,4 @@ #endif +
diff -r 249e3f065956 -r bcd8161f8f6c stm32f1xx_hal_msp.h --- a/stm32f1xx_hal_msp.h Sat Mar 19 21:16:10 2016 +0000 +++ b/stm32f1xx_hal_msp.h Thu May 19 17:16:59 2016 +0000 @@ -40,8 +40,6 @@ #include "pinmap.h" -typedef struct can_s can_t; - #ifdef __cplusplus extern "C" { @@ -55,7 +53,7 @@ * @param abom: Automatic recovery from bus-off state * @retval None */ -void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom); +void initCAN(PinName rxPin, PinName txPin, FunctionalState abom); /** * @brief CAN MSP Initialization @@ -95,3 +93,5 @@ #endif #endif + +
diff -r 249e3f065956 -r bcd8161f8f6c stm32f3xx_hal_msp.c --- a/stm32f3xx_hal_msp.c Sat Mar 19 21:16:10 2016 +0000 +++ b/stm32f3xx_hal_msp.c Thu May 19 17:16:59 2016 +0000 @@ -53,7 +53,7 @@ PinName _rxPin; PinName _txPin; -void (*rxCompleteCallback) (void); +void (*rxCompleteCallback)(void); /** * @brief CAN initialization. @@ -63,7 +63,7 @@ * @param abom: Automatic recovery from bus-off state * @retval None */ -void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom) { +void initCAN(PinName rxPin, PinName txPin, FunctionalState abom) { _rxPin = rxPin; _txPin = txPin; @@ -258,3 +258,5 @@ __HAL_CAN_ENABLE_IT(_canHandle, CAN_IT_FMP0); } #endif + +
diff -r 249e3f065956 -r bcd8161f8f6c stm32f3xx_hal_msp.h --- a/stm32f3xx_hal_msp.h Sat Mar 19 21:16:10 2016 +0000 +++ b/stm32f3xx_hal_msp.h Thu May 19 17:16:59 2016 +0000 @@ -40,8 +40,6 @@ #include "pinmap.h" -typedef struct can_s can_t; - #ifdef __cplusplus extern "C" { @@ -55,7 +53,7 @@ * @param abom: Automatic recovery from bus-off state * @retval None */ -void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom); +void initCAN(PinName rxPin, PinName txPin, FunctionalState abom); /** * @brief CAN MSP Initialization @@ -94,4 +92,4 @@ #endif -#endif \ No newline at end of file +#endif
diff -r 249e3f065956 -r bcd8161f8f6c stm32f4xx_hal_msp.c --- a/stm32f4xx_hal_msp.c Sat Mar 19 21:16:10 2016 +0000 +++ b/stm32f4xx_hal_msp.c Thu May 19 17:16:59 2016 +0000 @@ -49,7 +49,7 @@ PinName _rxPin; PinName _txPin; -void (*rxCompleteCallback) (void); +void (*rxCompleteCallback)(void); /** * @brief CAN initialization. @@ -59,7 +59,7 @@ * @param abom: Automatic recovery from bus-off state * @retval None */ -void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom) { +void initCAN(PinName rxPin, PinName txPin, FunctionalState abom) { _rxPin = rxPin; _txPin = txPin; @@ -250,3 +250,5 @@ } #endif + +
diff -r 249e3f065956 -r bcd8161f8f6c stm32f4xx_hal_msp.h --- a/stm32f4xx_hal_msp.h Sat Mar 19 21:16:10 2016 +0000 +++ b/stm32f4xx_hal_msp.h Thu May 19 17:16:59 2016 +0000 @@ -40,8 +40,6 @@ #include "pinmap.h" -typedef struct can_s can_t; - #ifdef __cplusplus extern "C" { @@ -55,7 +53,7 @@ * @param abom: Automatic recovery from bus-off state * @retval None */ -void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom); +void initCAN(PinName rxPin, PinName txPin, FunctionalState abom); /** * @brief CAN MSP Initialization