123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /*
- * Copyright (c) 2016 UAVCAN Team
- *
- * Distributed under the MIT License, available in the file LICENSE.
- *
- */
- #define HAS_CAN_CONFIG_H
- #include <canard_avr.h>
- #include <string.h>
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <avr/pgmspace.h>
- #include <can.h>
- int canardAVRInit(uint32_t bitrate)
- {
- can_bitrate_t br;
- switch (bitrate)
- {
- case 10000:
- {
- br = BITRATE_10_KBPS;
- break;
- }
- case 20000:
- {
- br = BITRATE_20_KBPS;
- break;
- }
- case 50000:
- {
- br = BITRATE_50_KBPS;
- break;
- }
- case 100000:
- {
- br = BITRATE_100_KBPS;
- break;
- }
- case 125000:
- {
- br = BITRATE_125_KBPS;
- break;
- }
- case 250000:
- {
- br = BITRATE_250_KBPS;
- break;
- }
- case 500000:
- {
- br = BITRATE_500_KBPS;
- break;
- }
- case 1000000:
- {
- br = BITRATE_1_MBPS;
- break;
- }
- default:
- {
- return -1;
- }
- }
- can_init(br);
- can_set_mode(NORMAL_MODE);
- // create a new filter for receiving all messages
- can_filter_t filter = {
- .id = 0,
- .mask = 0,
- .flags = {
- .rtr = 0,
- .extended = 0
- }
- };
- can_set_filter(0, &filter);
- // enable interrupts
- sei();
- return 0;
- }
- int canardAVRClose(void)
- {
- return 0;
- }
- int canardAVRTransmit(const CanardCANFrame* frame)
- {
- const int poll_result = can_check_free_buffer();
- if (poll_result <= 0)
- {
- return 0;
- }
- can_t transmit_msg;
- memset(&transmit_msg, 0, sizeof(transmit_msg));
- transmit_msg.id = frame->id & CANARD_CAN_EXT_ID_MASK;
- transmit_msg.length = frame->data_len;
- transmit_msg.flags.extended = (frame->id & CANARD_CAN_FRAME_EFF) != 0;
- memcpy(transmit_msg.data, frame->data, frame->data_len);
- const uint8_t res = can_send_message(&transmit_msg);
- if (res <= 0)
- {
- return -1;
- }
- return 1;
- }
- int canardAVRReceive(CanardCANFrame* out_frame)
- {
- const int poll_result = can_check_message();
- if (poll_result <= 0)
- {
- return 0;
- }
- can_t receive_msg;
- const uint8_t res = can_get_message(&receive_msg);
- if (res <= 0)
- {
- return -1;
- }
- out_frame->id = receive_msg.id;
- out_frame->data_len = receive_msg.length;
- memcpy(out_frame->data, receive_msg.data, receive_msg.length);
- if (receive_msg.flags.extended != 0)
- {
- out_frame->id |= CANARD_CAN_FRAME_EFF;
- }
- return 1;
- }
- int canardAVRConfigureAcceptanceFilters(uint8_t node_id)
- {
- static const uint32_t DefaultFilterMsgMask = 0x80;
- static const uint32_t DefaultFilterMsgID = 0x0;
- static const uint32_t DefaultFilterSrvMask = 0x7F80;
- uint8_t res = 1;
- // create a new filter for receiving messages
- can_filter_t filter_Msg = {
- .id = DefaultFilterMsgID,
- .mask = DefaultFilterMsgMask,
- .flags = {
- .rtr = 0,
- .extended = 3
- }
- };
- // create a new filter for receiving services
- can_filter_t filter_Srv = {
- .id = ((uint32_t)node_id << 8) | 0x80,
- .mask = DefaultFilterSrvMask,
- .flags = {
- .rtr = 0,
- .extended = 3
- }
- };
- // setup 2 MOb's to receive, 12 MOb's are used as send buffer
- if (!can_set_filter(0, &filter_Msg))
- {
- res = -1;
- }
- if (!can_set_filter(1, &filter_Srv))
- {
- res = -1;
- }
- return res;
- }
|