Skip to content

Commit 067e446

Browse files
committed
Add CAN->filter to API and add placeholders to hal's
Add filter function and modify CAN->read definition to allow reading specific messages.
1 parent 00c641c commit 067e446

File tree

7 files changed

+56
-21
lines changed

7 files changed

+56
-21
lines changed

libraries/mbed/api/CAN.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,13 @@ class CAN {
131131
/** Read a CANMessage from the bus.
132132
*
133133
* @param msg A CANMessage to read to.
134+
* @param handle message filter handle (0 for any message)
134135
*
135136
* @returns
136137
* 0 if no message arrived,
137138
* 1 if message arrived
138139
*/
139-
int read(CANMessage &msg);
140+
int read(CANMessage &msg, int handle = 0);
140141

141142
/** Reset CAN interface.
142143
*
@@ -169,6 +170,19 @@ class CAN {
169170
*/
170171
int mode(Mode mode);
171172

173+
/** Filter out incomming messages
174+
*
175+
* @param id the id to filter on
176+
* @param mask the mask applied to the id
177+
* @param format format to filter on (Default CANAny)
178+
* @param handle message filter handle (Optional)
179+
*
180+
* @returns
181+
* 0 if filter change failed or unsupported,
182+
* new filter handle if successful
183+
*/
184+
int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
185+
172186
/** Returns number of read errors to detect read overflow errors.
173187
*/
174188
unsigned char rderror();

libraries/mbed/api/can_helper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ extern "C" {
2424

2525
enum CANFormat {
2626
CANStandard = 0,
27-
CANExtended = 1
27+
CANExtended = 1,
28+
CANAny = 2
2829
};
2930
typedef enum CANFormat CANFormat;
3031

libraries/mbed/common/CAN.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ int CAN::write(CANMessage msg) {
3939
return can_write(&_can, msg, 0);
4040
}
4141

42-
int CAN::read(CANMessage &msg) {
43-
return can_read(&_can, &msg);
42+
int CAN::read(CANMessage &msg, int handle) {
43+
return can_read(&_can, &msg, handle);
4444
}
4545

4646
void CAN::reset() {
@@ -63,19 +63,23 @@ int CAN::mode(Mode mode) {
6363
return can_mode(&_can, (CanMode)mode);
6464
}
6565

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-
}
66+
int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle) {
67+
return can_filter(&_can, id, mask, format, handle);
68+
}
7469

75-
void CAN::_irq_handler(uint32_t id, CanIrqType type) {
76-
CAN *handler = (CAN*)id;
77-
handler->_irq[type].call();
70+
void CAN::attach(void (*fptr)(void), IrqType type) {
71+
if (fptr) {
72+
_irq[(CanIrqType)type].attach(fptr);
73+
can_irq_set(&_can, (CanIrqType)type, 1);
74+
} else {
75+
can_irq_set(&_can, (CanIrqType)type, 0);
7876
}
77+
}
78+
79+
void CAN::_irq_handler(uint32_t id, CanIrqType type) {
80+
CAN *handler = (CAN*)id;
81+
handler->_irq[type].call();
82+
}
7983

8084
} // namespace mbed
8185

libraries/mbed/hal/can_api.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ void can_irq_free (can_t *obj);
6363
void can_irq_set (can_t *obj, CanIrqType irq, uint32_t enable);
6464

6565
int can_write (can_t *obj, CAN_Message, int cc);
66-
int can_read (can_t *obj, CAN_Message *msg);
66+
int can_read (can_t *obj, CAN_Message *msg, int handle);
6767
int can_mode (can_t *obj, CanMode mode);
68+
int can_filter (can_t *obj, unsigned int id, unsigned int mask, CANFormat format, int handle);
6869
void can_reset (can_t *obj);
6970
unsigned char can_rderror (can_t *obj);
7071
unsigned char can_tderror (can_t *obj);

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

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

81-
int can_mode(can_t *obj, CanMode mode)
82-
{
81+
int can_mode(can_t *obj, CanMode mode) {
82+
return 0; // not implemented
83+
}
84+
85+
int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
8386
return 0; // not implemented
8487
}
8588

@@ -355,7 +358,7 @@ int can_write(can_t *obj, CAN_Message msg, int cc) {
355358
return 0;
356359
}
357360

358-
int can_read(can_t *obj, CAN_Message *msg) {
361+
int can_read(can_t *obj, CAN_Message *msg, int handle) {
359362
CANMsg x;
360363
unsigned int *i = (unsigned int *)&x;
361364

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ static inline void can_enable(can_t *obj) {
7272
}
7373
}
7474

75+
int can_mode(can_t *obj, CanMode mode) {
76+
return 0; // not implemented
77+
}
78+
79+
int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
80+
return 0; // not implemented
81+
}
82+
7583
static int can_pclk(can_t *obj) {
7684
int value = 0;
7785
switch ((int)obj->dev) {
@@ -247,7 +255,7 @@ int can_write(can_t *obj, CAN_Message msg, int cc) {
247255
return 0;
248256
}
249257

250-
int can_read(can_t *obj, CAN_Message *msg) {
258+
int can_read(can_t *obj, CAN_Message *msg, int handle) {
251259
CANMsg x;
252260
unsigned int *i = (unsigned int *)&x;
253261

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ int can_mode(can_t *obj, CanMode mode)
8383
return 0; // not implemented
8484
}
8585

86+
int can_filter(can_t *obj, uint32_t id, uint32_t mask, CANFormat format, int32_t handle) {
87+
return 0; // not implemented
88+
}
89+
8690
static inline void can_irq(uint32_t icr, uint32_t index) {
8791
uint32_t i;
8892

@@ -340,7 +344,7 @@ int can_write(can_t *obj, CAN_Message msg, int cc) {
340344
return 0;
341345
}
342346

343-
int can_read(can_t *obj, CAN_Message *msg) {
347+
int can_read(can_t *obj, CAN_Message *msg, int handle) {
344348
CANMsg x;
345349
unsigned int *i = (unsigned int *)&x;
346350

0 commit comments

Comments
 (0)