Skip to content

Commit bf02700

Browse files
committed
2 parents f7a2be4 + c747e25 commit bf02700

File tree

4 files changed

+75
-24
lines changed

4 files changed

+75
-24
lines changed

libraries/mbed/api/CAN.h

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ class CAN {
150150
*/
151151
void monitor(bool silent);
152152

153+
enum Mode {
154+
Reset = 0,
155+
Normal,
156+
Silent,
157+
LocalTest,
158+
GlobalTest,
159+
SilentTest
160+
};
161+
162+
/** Change CAN operation to the specified mode
163+
*
164+
* @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
165+
*
166+
* @returns
167+
* 0 if mode change failed or unsupported,
168+
* 1 if mode change was successful
169+
*/
170+
int mode(Mode mode);
171+
153172
/** Returns number of read errors to detect read overflow errors.
154173
*/
155174
unsigned char rderror();
@@ -158,33 +177,45 @@ class CAN {
158177
*/
159178
unsigned char tderror();
160179

180+
enum IrqType {
181+
RxIrq = 0,
182+
TxIrq,
183+
EwIrq,
184+
DoIrq,
185+
WuIrq,
186+
EpIrq,
187+
AlIrq,
188+
BeIrq,
189+
IdIrq
190+
};
191+
161192
/** Attach a function to call whenever a CAN frame received interrupt is
162193
* generated.
163194
*
164195
* @param fptr A pointer to a void function, or 0 to set as none
165-
* @param event Which can interrupt to attach the member function to (CAN::IRQ_RX, CAN::IRQ_TX, CAN::IRQ_ERROR, CAN::IRQ_OVERRUN, CAN::IRQ_WAKEUP, CAN::IRQ_PASSIVE, CAN::IRQ_ARB, CAN::IRQ_BUS, CAN::IRQ_READY) - Note that not every event is supported by all hardware
196+
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
166197
*/
167-
void attach(void (*fptr)(void), can_irq_event event=IRQ_RX);
198+
void attach(void (*fptr)(void), IrqType type=RxIrq);
168199

169200
/** Attach a member function to call whenever a CAN frame received interrupt
170201
* is generated.
171202
*
172203
* @param tptr pointer to the object to call the member function on
173204
* @param mptr pointer to the member function to be called
174-
* @param event Which can interrupt to attach the member function to (CAN::IRQ_RX, CAN::IRQ_TX, CAN::IRQ_ERROR, CAN::IRQ_OVERRUN, CAN::IRQ_WAKEUP, CAN::IRQ_PASSIVE, CAN::IRQ_ARB, CAN::IRQ_BUS, CAN::IRQ_READY) - Note that not every event is supported by all hardware
205+
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
175206
*/
176207
template<typename T>
177-
void attach(T* tptr, void (T::*mptr)(void), can_irq_event event=IRQ_RX) {
208+
void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
178209
if((mptr != NULL) && (tptr != NULL)) {
179-
_irq[event].attach(tptr, mptr);
180-
can_irq_set(&_can, event, 1);
210+
_irq[type].attach(tptr, mptr);
211+
can_irq_set(&_can, (CanIrqType)type, 1);
181212
}
182213
else {
183-
can_irq_set(&_can, event, 0);
214+
can_irq_set(&_can, (CanIrqType)type, 0);
184215
}
185216
}
186217

187-
static void _irq_handler(uint32_t id, can_irq_event event);
218+
static void _irq_handler(uint32_t id, CanIrqType type);
188219

189220
protected:
190221
can_t _can;

libraries/mbed/common/CAN.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,23 @@ void CAN::monitor(bool silent) {
5959
can_monitor(&_can, (silent) ? 1 : 0);
6060
}
6161

62-
void CAN::attach(void (*fptr)(void), can_irq_event event) {
63-
if (fptr) {
64-
_irq[event].attach(fptr);
65-
can_irq_set(&_can, event, 1);
66-
} else {
67-
can_irq_set(&_can, event, 0);
68-
}
62+
int CAN::mode(Mode mode) {
63+
return can_mode(&_can, (CanMode)mode);
6964
}
7065

71-
void CAN::_irq_handler(uint32_t id, can_irq_event event) {
72-
CAN *handler = (CAN*)id;
73-
handler->_irq[event].call();
74-
}
66+
void CAN::attach(void (*fptr)(void), IrqType type) {
67+
if (fptr) {
68+
_irq[(CanIrqType)type].attach(fptr);
69+
can_irq_set(&_can, (CanIrqType)type, 1);
70+
} else {
71+
can_irq_set(&_can, (CanIrqType)type, 0);
72+
}
73+
}
74+
75+
void CAN::_irq_handler(uint32_t id, CanIrqType type) {
76+
CAN *handler = (CAN*)id;
77+
handler->_irq[type].call();
78+
}
7579

7680
} // namespace mbed
7781

libraries/mbed/hal/can_api.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,19 @@ typedef enum {
3838
IRQ_ARB,
3939
IRQ_BUS,
4040
IRQ_READY
41-
} can_irq_event;
41+
} CanIrqType;
4242

43-
typedef void (*can_irq_handler)(uint32_t id, can_irq_event event);
43+
44+
typedef enum {
45+
MODE_RESET,
46+
MODE_NORMAL,
47+
MODE_SILENT,
48+
MODE_TEST_GLOBAL,
49+
MODE_TEST_LOCAL,
50+
MODE_TEST_SILENT
51+
} CanMode;
52+
53+
typedef void (*can_irq_handler)(uint32_t id, CanIrqType type);
4454

4555
typedef struct can_s can_t;
4656

@@ -50,10 +60,11 @@ int can_frequency(can_t *obj, int hz);
5060

5161
void can_irq_init (can_t *obj, can_irq_handler handler, uint32_t id);
5262
void can_irq_free (can_t *obj);
53-
void can_irq_set (can_t *obj, can_irq_event irq, uint32_t enable);
63+
void can_irq_set (can_t *obj, CanIrqType irq, uint32_t enable);
5464

5565
int can_write (can_t *obj, CAN_Message, int cc);
5666
int can_read (can_t *obj, CAN_Message *msg);
67+
int can_mode (can_t *obj, CanMode mode);
5768
void can_reset (can_t *obj);
5869
unsigned char can_rderror (can_t *obj);
5970
unsigned char can_tderror (can_t *obj);

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/can_api.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ static inline void can_enable(can_t *obj) {
7878
}
7979
}
8080

81+
int can_mode(can_t *obj, CanMode mode)
82+
{
83+
return 0; // not implemented
84+
}
85+
8186
static inline void can_irq(uint32_t icr, uint32_t index) {
8287
uint32_t i;
8388

@@ -133,10 +138,10 @@ void can_irq_free(can_t *obj) {
133138
}
134139

135140
// Clear or set a irq
136-
void can_irq_set(can_t *obj, can_irq_event event, uint32_t enable) {
141+
void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable) {
137142
uint32_t ier;
138143

139-
switch (event) {
144+
switch (type) {
140145
case IRQ_RX: ier = (1 << 0); break;
141146
case IRQ_TX: ier = (1 << 1); break;
142147
case IRQ_ERROR: ier = (1 << 2); break;

0 commit comments

Comments
 (0)