123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #include "ch.h"
- #include "hal.h"
- struct can_instance {
- CANDriver *canp;
- ioline_t led;
- };
- static const struct can_instance can1 = {&CAND1, LINE_LED_GREEN};
- static const CANConfig cancfg = {
- CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
- CAN_BTR_LBKM | CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
- CAN_BTR_TS1(12) | CAN_BTR_BRP(9)
- };
- static THD_WORKING_AREA(can_rx1_wa, 256);
- static THD_FUNCTION(can_rx, p) {
- struct can_instance *cip = p;
- event_listener_t el;
- CANRxFrame rxmsg;
- (void)p;
- chRegSetThreadName("receiver");
- chEvtRegister(&cip->canp->rxfull_event, &el, 0);
- while(!chThdShouldTerminateX()) {
- if (chEvtWaitAnyTimeout(ALL_EVENTS, TIME_MS2I(100)) == 0)
- continue;
- while (canReceive(cip->canp, CAN_ANY_MAILBOX,
- &rxmsg, TIME_IMMEDIATE) == MSG_OK) {
-
- palToggleLine(cip->led);
- }
- }
- chEvtUnregister(&CAND1.rxfull_event, &el);
- }
- static THD_WORKING_AREA(can_tx_wa, 256);
- static THD_FUNCTION(can_tx, p) {
- CANTxFrame txmsg;
- (void)p;
- chRegSetThreadName("transmitter");
- txmsg.IDE = CAN_IDE_EXT;
- txmsg.EID = 0x01234567;
- txmsg.RTR = CAN_RTR_DATA;
- txmsg.DLC = 8;
- txmsg.data32[0] = 0x55AA55AA;
- txmsg.data32[1] = 0x00FF00FF;
- while (!chThdShouldTerminateX()) {
- canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100));
- chThdSleepMilliseconds(500);
- }
- }
- int main(void) {
-
- halInit();
- chSysInit();
-
- palSetLineMode(LINE_LED_GREEN, PAL_MODE_OUTPUT_PUSHPULL);
-
- canStart(&CAND1, &cancfg);
-
- chThdCreateStatic(can_rx1_wa, sizeof(can_rx1_wa), NORMALPRIO + 7,
- can_rx, (void *)&can1);
- chThdCreateStatic(can_tx_wa, sizeof(can_tx_wa), NORMALPRIO + 7,
- can_tx, NULL);
-
- while (true) {
- chThdSleepMilliseconds(500);
- }
- return 0;
- }
|