123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include "ch.h"
- #include "hal.h"
- 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(8) | CAN_BTR_BRP(6)
- };
- static THD_WORKING_AREA(can_rx_wa, 256);
- static THD_FUNCTION(can_rx, p) {
- event_listener_t el;
- CANRxFrame rxmsg;
- (void)p;
- chRegSetThreadName("receiver");
- chEvtRegister(&CAND1.rxfull_event, &el, 0);
- while (true) {
- if (chEvtWaitAnyTimeout(ALL_EVENTS, TIME_MS2I(100)) == 0)
- continue;
- while (canReceive(&CAND1, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE) == MSG_OK) {
-
- palTogglePad(IOPORT3, GPIOC_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 (true) {
- canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100));
- chThdSleepMilliseconds(500);
- }
- }
- int main(void) {
-
- halInit();
- chSysInit();
-
- canStart(&CAND1, &cancfg);
-
- chThdCreateStatic(can_rx_wa, sizeof(can_rx_wa), NORMALPRIO + 7, can_rx, NULL);
- chThdCreateStatic(can_tx_wa, sizeof(can_tx_wa), NORMALPRIO + 7, can_tx, NULL);
-
- while (true) {
- chThdSleepMilliseconds(500);
- }
- return 0;
- }
|