Skip to content

Commit

Permalink
Merge pull request #15077 from hazzlim/make_can_api_portable
Browse files Browse the repository at this point in the history
CAN: Use uintptr_t for can_irq_ids
  • Loading branch information
0xc0170 authored Sep 29, 2021
2 parents 963aa8b + b493a15 commit 0fac696
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 126 deletions.
2 changes: 1 addition & 1 deletion drivers/include/drivers/CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class CAN
*/
void attach(Callback<void()> func, IrqType type = IrqType::RxIrq);

static void _irq_handler(uint32_t id, CanIrqType type);
static void _irq_handler(uintptr_t context, CanIrqType type);

#if !defined(DOXYGEN_ONLY)
protected:
Expand Down
12 changes: 6 additions & 6 deletions drivers/source/CAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ CAN::CAN(PinName rd, PinName td) : _can(), _irq()
{
// No lock needed in constructor
can_init(&_can, rd, td);
can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
can_irq_init(&_can, (&CAN::_irq_handler), reinterpret_cast<uintptr_t>(this));
}

CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq()
{
// No lock needed in constructor
can_init_freq(&_can, rd, td, hz);
can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
can_irq_init(&_can, (&CAN::_irq_handler), reinterpret_cast<uintptr_t>(this));
}

CAN::CAN(const can_pinmap_t &pinmap) : _can(), _irq()
{
// No lock needed in constructor
can_init_direct(&_can, &pinmap);
can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
can_irq_init(&_can, (&CAN::_irq_handler), reinterpret_cast<uintptr_t>(this));
}

CAN::CAN(const can_pinmap_t &pinmap, int hz) : _can(), _irq()
{
// No lock needed in constructor
can_init_freq_direct(&_can, &pinmap, hz);
can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
can_irq_init(&_can, (&CAN::_irq_handler), reinterpret_cast<uintptr_t>(this));
}

CAN::~CAN()
Expand Down Expand Up @@ -153,9 +153,9 @@ void CAN::attach(Callback<void()> func, IrqType type)
unlock();
}

void CAN::_irq_handler(uint32_t id, CanIrqType type)
void CAN::_irq_handler(uintptr_t context, CanIrqType type)
{
CAN *handler = (CAN *)id;
CAN *handler = reinterpret_cast<CAN *>(context);
if (handler->_irq[type]) {
handler->_irq[type].call();
}
Expand Down
4 changes: 2 additions & 2 deletions hal/include/hal/can_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ typedef struct {
int td_function;
} can_pinmap_t;

typedef void (*can_irq_handler)(uint32_t id, CanIrqType type);
typedef void (*can_irq_handler)(uintptr_t context, CanIrqType type);

typedef struct can_s can_t;

Expand All @@ -74,7 +74,7 @@ void can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int h
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_init(can_t *obj, can_irq_handler handler, uintptr_t context);
void can_irq_free(can_t *obj);
void can_irq_set(can_t *obj, CanIrqType irq, uint32_t enable);

Expand Down
18 changes: 9 additions & 9 deletions targets/TARGET_GigaDevice/TARGET_GD32F30X/can_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* CAN1 interrupt vector number */
#define CAN1_IRQ_BASE_NUM 63

static uint32_t can_irq_ids[2] = {0};
static uintptr_t can_irq_contexts[2] = {0};
static can_irq_handler irq_callback;

/** CAN interrupt handle .
Expand Down Expand Up @@ -66,13 +66,13 @@ static void dev_can_irq_handle(uint32_t periph, int id)

/* CAN transmit complete interrupt handle */
if (flag0 || flag1 || flag2) {
irq_callback(can_irq_ids[id], IRQ_TX);
irq_callback(can_irq_contexts[id], IRQ_TX);
}

/* CAN receive complete interrupt handle */
if (CAN_INTEN_RFNEIE0 == (CAN_INTEN(periph) & CAN_INTEN_RFNEIE0)) {
if (0 != can_receive_message_length_get(periph, CAN_FIFO0)) {
irq_callback(can_irq_ids[id], IRQ_RX);
irq_callback(can_irq_contexts[id], IRQ_RX);
}
}

Expand All @@ -81,18 +81,18 @@ static void dev_can_irq_handle(uint32_t periph, int id)
/* passive error interrupt handle */
if (CAN_INTEN_PERRIE == (CAN_INTEN(periph) & CAN_INTEN_PERRIE)) {
if (SET == can_flag_get(periph, CAN_FLAG_PERR)) {
irq_callback(can_irq_ids[id], IRQ_PASSIVE);
irq_callback(can_irq_contexts[id], IRQ_PASSIVE);
}
}

/* bus-off interrupt handle */
if (CAN_INTEN_BOIE == (CAN_INTEN(periph) & CAN_INTEN_BOIE)) {
if (SET == can_flag_get(periph, CAN_FLAG_BOERR)) {
irq_callback(can_irq_ids[id], IRQ_BUS);
irq_callback(can_irq_contexts[id], IRQ_BUS);
}
}

irq_callback(can_irq_ids[id], IRQ_ERROR);
irq_callback(can_irq_contexts[id], IRQ_ERROR);
}
}

Expand Down Expand Up @@ -330,10 +330,10 @@ int can_frequency(can_t *obj, int hz)
* @param handler the interrupt callback.
* @param id the CANx index.
*/
void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
void can_irq_init(can_t *obj, can_irq_handler handler, uintptr_t context)
{
irq_callback = handler;
can_irq_ids[obj->index] = id;
can_irq_contexts[obj->index] = context;
}

/** disable the interrupt.
Expand All @@ -351,7 +351,7 @@ void can_irq_free(can_t *obj)
CAN_INTEN_PERRIE | CAN_INTEN_BOIE | CAN_INTEN_ERRIE);
}

can_irq_ids[obj->index] = 0;
can_irq_contexts[obj->index] = 0;
}

/** Set the interrupt handle.
Expand Down
18 changes: 9 additions & 9 deletions targets/TARGET_GigaDevice/TARGET_GD32F4XX/can_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/* CAN1 interrupt vector number */
#define CAN1_IRQ_BASE_NUM 63

static uint32_t can_irq_ids[2] = {0};
static uintptr_t can_irq_contexts[2] = {0};
static can_irq_handler irq_callback;

/** CAN interrupt handle .
Expand Down Expand Up @@ -66,13 +66,13 @@ static void dev_can_irq_handle(uint32_t periph, int id)

/* CAN transmit complete interrupt handle */
if (flag0 || flag1 || flag2) {
irq_callback(can_irq_ids[id], IRQ_TX);
irq_callback(can_irq_contexts[id], IRQ_TX);
}

/* CAN receive complete interrupt handle */
if (CAN_INTEN_RFNEIE0 == (CAN_INTEN(periph) & CAN_INTEN_RFNEIE0)) {
if (0 != can_receive_message_length_get(periph, CAN_FIFO0)) {
irq_callback(can_irq_ids[id], IRQ_RX);
irq_callback(can_irq_contexts[id], IRQ_RX);
}
}

Expand All @@ -81,18 +81,18 @@ static void dev_can_irq_handle(uint32_t periph, int id)
/* passive error interrupt handle */
if (CAN_INTEN_PERRIE == (CAN_INTEN(periph) & CAN_INTEN_PERRIE)) {
if (SET == can_flag_get(periph, CAN_FLAG_PERR)) {
irq_callback(can_irq_ids[id], IRQ_PASSIVE);
irq_callback(can_irq_contexts[id], IRQ_PASSIVE);
}
}

/* bus-off interrupt handle */
if (CAN_INTEN_BOIE == (CAN_INTEN(periph) & CAN_INTEN_BOIE)) {
if (SET == can_flag_get(periph, CAN_FLAG_BOERR)) {
irq_callback(can_irq_ids[id], IRQ_BUS);
irq_callback(can_irq_contexts[id], IRQ_BUS);
}
}

irq_callback(can_irq_ids[id], IRQ_ERROR);
irq_callback(can_irq_contexts[id], IRQ_ERROR);
}
}

Expand Down Expand Up @@ -331,10 +331,10 @@ int can_frequency(can_t *obj, int hz)
* @param handler the interrupt callback.
* @param id the CANx index.
*/
void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
void can_irq_init(can_t *obj, can_irq_handler handler, uintptr_t context)
{
irq_callback = handler;
can_irq_ids[obj->index] = id;
can_irq_contexts[obj->index] = context;
}

/** disable the interrupt.
Expand All @@ -352,7 +352,7 @@ void can_irq_free(can_t *obj)
CAN_INTEN_PERRIE | CAN_INTEN_BOIE | CAN_INTEN_ERRIE);
}

can_irq_ids[obj->index] = 0;
can_irq_contexts[obj->index] = 0;
}

/** Set the interrupt handle.
Expand Down
20 changes: 10 additions & 10 deletions targets/TARGET_NUVOTON/TARGET_M451/can_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define NU_CAN_DEBUG 0
#define CAN_NUM 1

static uint32_t can_irq_ids[CAN_NUM] = {0};
static uintptr_t can_irq_contexts[CAN_NUM] = {0};
static can_irq_handler can0_irq_handler;


Expand Down Expand Up @@ -125,34 +125,34 @@ static void can_irq(CANName name, int id)
/**************************/
if(can->STATUS & CAN_STATUS_RXOK_Msk) {
can->STATUS &= ~CAN_STATUS_RXOK_Msk; /* Clear Rx Ok status*/
can0_irq_handler(can_irq_ids[id], IRQ_RX);
can0_irq_handler(can_irq_contexts[id], IRQ_RX);
}

if(can->STATUS & CAN_STATUS_TXOK_Msk) {
can->STATUS &= ~CAN_STATUS_TXOK_Msk; /* Clear Tx Ok status*/
can0_irq_handler(can_irq_ids[id], IRQ_TX);
can0_irq_handler(can_irq_contexts[id], IRQ_TX);
}

/**************************/
/* Error Status interrupt */
/**************************/
if(can->STATUS & CAN_STATUS_EWARN_Msk) {
can0_irq_handler(can_irq_ids[id], IRQ_ERROR);
can0_irq_handler(can_irq_contexts[id], IRQ_ERROR);
}

if(can->STATUS & CAN_STATUS_BOFF_Msk) {
can0_irq_handler(can_irq_ids[id], IRQ_BUS);
can0_irq_handler(can_irq_contexts[id], IRQ_BUS);
}
} else if (u8IIDRstatus!=0) {

can0_irq_handler(can_irq_ids[id], IRQ_OVERRUN);
can0_irq_handler(can_irq_contexts[id], IRQ_OVERRUN);

CAN_CLR_INT_PENDING_BIT(can, ((can->IIDR) -1)); /* Clear Interrupt Pending */

} else if(can->WU_STATUS == 1) {

can->WU_STATUS = 0; /* Write '0' to clear */
can0_irq_handler(can_irq_ids[id], IRQ_WAKEUP);
can0_irq_handler(can_irq_contexts[id], IRQ_WAKEUP);
}
}

Expand All @@ -161,17 +161,17 @@ void CAN0_IRQHandler(void)
can_irq(CAN_0, 0);
}

void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
void can_irq_init(can_t *obj, can_irq_handler handler, uintptr_t context)
{
can0_irq_handler = handler;
can_irq_ids[obj->index] = id;
can_irq_contexts[obj->index] = context;
}

void can_irq_free(can_t *obj)
{
CAN_DisableInt((CAN_T *)NU_MODBASE(obj->can), (CAN_CON_IE_Msk|CAN_CON_SIE_Msk|CAN_CON_EIE_Msk));

can_irq_ids[obj->index] = 0;
can_irq_contexts[obj->index] = 0;

NVIC_DisableIRQ(CAN0_IRQn);
}
Expand Down
32 changes: 16 additions & 16 deletions targets/TARGET_NUVOTON/TARGET_M480/can_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define NU_CAN_DEBUG 0
#define CAN_NUM 2

static uint32_t can_irq_ids[CAN_NUM] = {0};
static uintptr_t can_irq_contexts[CAN_NUM] = {0};
static can_irq_handler can0_irq_handler;
static can_irq_handler can1_irq_handler;

Expand Down Expand Up @@ -140,17 +140,17 @@ static void can_irq(CANName name, int id)
if(can->STATUS & CAN_STATUS_RXOK_Msk) {
can->STATUS &= ~CAN_STATUS_RXOK_Msk; /* Clear Rx Ok status*/
if(id)
can1_irq_handler(can_irq_ids[id], IRQ_RX);
can1_irq_handler(can_irq_contexts[id], IRQ_RX);
else
can0_irq_handler(can_irq_ids[id], IRQ_RX);
can0_irq_handler(can_irq_contexts[id], IRQ_RX);
}

if(can->STATUS & CAN_STATUS_TXOK_Msk) {
can->STATUS &= ~CAN_STATUS_TXOK_Msk; /* Clear Tx Ok status*/
if(id)
can1_irq_handler(can_irq_ids[id], IRQ_TX);
can1_irq_handler(can_irq_contexts[id], IRQ_TX);
else
can0_irq_handler(can_irq_ids[id], IRQ_TX);
can0_irq_handler(can_irq_contexts[id], IRQ_TX);

}

Expand All @@ -159,33 +159,33 @@ static void can_irq(CANName name, int id)
/**************************/
if(can->STATUS & CAN_STATUS_EWARN_Msk) {
if(id)
can1_irq_handler(can_irq_ids[id], IRQ_ERROR);
can1_irq_handler(can_irq_contexts[id], IRQ_ERROR);
else
can0_irq_handler(can_irq_ids[id], IRQ_ERROR);
can0_irq_handler(can_irq_contexts[id], IRQ_ERROR);
}

if(can->STATUS & CAN_STATUS_BOFF_Msk) {
if(id)
can1_irq_handler(can_irq_ids[id], IRQ_BUS);
can1_irq_handler(can_irq_contexts[id], IRQ_BUS);
else
can0_irq_handler(can_irq_ids[id], IRQ_BUS);
can0_irq_handler(can_irq_contexts[id], IRQ_BUS);
}
} else if (u8IIDRstatus!=0) {

if(id)
can1_irq_handler(can_irq_ids[id], IRQ_OVERRUN);
can1_irq_handler(can_irq_contexts[id], IRQ_OVERRUN);
else
can0_irq_handler(can_irq_ids[id], IRQ_OVERRUN);
can0_irq_handler(can_irq_contexts[id], IRQ_OVERRUN);

CAN_CLR_INT_PENDING_BIT(can, ((can->IIDR) -1)); /* Clear Interrupt Pending */

} else if(can->WU_STATUS == 1) {

can->WU_STATUS = 0; /* Write '0' to clear */
if(id)
can1_irq_handler(can_irq_ids[id], IRQ_WAKEUP);
can1_irq_handler(can_irq_contexts[id], IRQ_WAKEUP);
else
can0_irq_handler(can_irq_ids[id], IRQ_WAKEUP);
can0_irq_handler(can_irq_contexts[id], IRQ_WAKEUP);
}
}

Expand All @@ -199,21 +199,21 @@ void CAN1_IRQHandler(void)
can_irq(CAN_1, 1);
}

void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
void can_irq_init(can_t *obj, can_irq_handler handler, uintptr_t context)
{
if(obj->index)
can1_irq_handler = handler;
else
can0_irq_handler = handler;
can_irq_ids[obj->index] = id;
can_irq_contexts[obj->index] = context;

}

void can_irq_free(can_t *obj)
{
CAN_DisableInt((CAN_T *)NU_MODBASE(obj->can), (CAN_CON_IE_Msk|CAN_CON_SIE_Msk|CAN_CON_EIE_Msk));

can_irq_ids[obj->index] = 0;
can_irq_contexts[obj->index] = 0;

if(!obj->index)
NVIC_DisableIRQ(CAN0_IRQn);
Expand Down
Loading

0 comments on commit 0fac696

Please sign in to comment.