canard_avr.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 UAVCAN Team
3  *
4  * Distributed under the MIT License, available in the file LICENSE.
5  *
6  */
7 
8 #define HAS_CAN_CONFIG_H
9 
10 #include <canard_avr.h>
11 #include <string.h>
12 
13 #include <avr/io.h>
14 #include <avr/interrupt.h>
15 #include <avr/pgmspace.h>
16 #include <can.h>
17 
18 int canardAVRInit(uint32_t bitrate)
19 {
20  can_bitrate_t br;
21  switch (bitrate)
22  {
23  case 10000:
24  {
25  br = BITRATE_10_KBPS;
26  break;
27  }
28  case 20000:
29  {
30  br = BITRATE_20_KBPS;
31  break;
32  }
33  case 50000:
34  {
35  br = BITRATE_50_KBPS;
36  break;
37  }
38  case 100000:
39  {
40  br = BITRATE_100_KBPS;
41  break;
42  }
43  case 125000:
44  {
45  br = BITRATE_125_KBPS;
46  break;
47  }
48  case 250000:
49  {
50  br = BITRATE_250_KBPS;
51  break;
52  }
53  case 500000:
54  {
55  br = BITRATE_500_KBPS;
56  break;
57  }
58  case 1000000:
59  {
60  br = BITRATE_1_MBPS;
61  break;
62  }
63  default:
64  {
65  return -1;
66  }
67  }
68 
69  can_init(br);
71 
72  // create a new filter for receiving all messages
73  can_filter_t filter = {
74  .id = 0,
75  .mask = 0,
76  .flags = {
77  .rtr = 0,
78  .extended = 0
79  }
80  };
81 
82  can_set_filter(0, &filter);
83 
84  // enable interrupts
85  sei();
86 
87  return 0;
88 }
89 
90 int canardAVRClose(void)
91 {
92  return 0;
93 }
94 
95 int canardAVRTransmit(const CanardCANFrame* frame)
96 {
97  const int poll_result = can_check_free_buffer();
98  if (poll_result <= 0)
99  {
100  return 0;
101  }
102 
103  can_t transmit_msg;
104  memset(&transmit_msg, 0, sizeof(transmit_msg));
105  transmit_msg.id = frame->id & CANARD_CAN_EXT_ID_MASK;
106  transmit_msg.length = frame->data_len;
107  transmit_msg.flags.extended = (frame->id & CANARD_CAN_FRAME_EFF) != 0;
108  memcpy(transmit_msg.data, frame->data, frame->data_len);
109 
110  const uint8_t res = can_send_message(&transmit_msg);
111  if (res <= 0)
112  {
113  return -1;
114  }
115 
116  return 1;
117 }
118 
119 int canardAVRReceive(CanardCANFrame* out_frame)
120 {
121  const int poll_result = can_check_message();
122  if (poll_result <= 0)
123  {
124  return 0;
125  }
126 
127  can_t receive_msg;
128  const uint8_t res = can_get_message(&receive_msg);
129  if (res <= 0)
130  {
131  return -1;
132  }
133 
134  out_frame->id = receive_msg.id;
135  out_frame->data_len = receive_msg.length;
136  memcpy(out_frame->data, receive_msg.data, receive_msg.length);
137 
138  if (receive_msg.flags.extended != 0)
139  {
140  out_frame->id |= CANARD_CAN_FRAME_EFF;
141  }
142 
143  return 1;
144 }
145 
147 {
148  static const uint32_t DefaultFilterMsgMask = 0x80;
149  static const uint32_t DefaultFilterMsgID = 0x0;
150  static const uint32_t DefaultFilterSrvMask = 0x7F80;
151  uint8_t res = 1;
152 
153  // create a new filter for receiving messages
154  can_filter_t filter_Msg = {
155  .id = DefaultFilterMsgID,
156  .mask = DefaultFilterMsgMask,
157  .flags = {
158  .rtr = 0,
159  .extended = 3
160  }
161  };
162 
163  // create a new filter for receiving services
164  can_filter_t filter_Srv = {
165  .id = ((uint32_t)node_id << 8) | 0x80,
166  .mask = DefaultFilterSrvMask,
167  .flags = {
168  .rtr = 0,
169  .extended = 3
170  }
171  };
172 
173  // setup 2 MOb's to receive, 12 MOb's are used as send buffer
174  if (!can_set_filter(0, &filter_Msg))
175  {
176  res = -1;
177  }
178 
179  if (!can_set_filter(1, &filter_Srv))
180  {
181  res = -1;
182  }
183 
184  return res;
185 }
can_check_free_buffer
bool can_check_free_buffer(void)
Ueberprueft ob ein Puffer zum Versenden einer Nachricht frei ist.
can_send_message
uint8_t can_send_message(const can_t *msg)
Verschickt eine Nachricht über den CAN Bus.
BITRATE_50_KBPS
@ BITRATE_50_KBPS
Definition: can.h:86
can_t::length
uint8_t length
Anzahl der Datenbytes.
Definition: can.h:192
uavcan::uint32_t
std::uint32_t uint32_t
Definition: std.hpp:26
can_t
Datenstruktur zum Aufnehmen von CAN Nachrichten.
Definition: can.h:177
canardAVRClose
int canardAVRClose(void)
Deinitialize CAN interface on AVR microcontroller.
Definition: canard_avr.c:90
can_bitrate_t
can_bitrate_t
Bitraten fuer den CAN-Bus.
Definition: can.h:83
can_filter_t::id
uint32_t id
ID der Nachricht (11 oder 29 Bit)
Definition: can.h:236
canardAVRConfigureAcceptanceFilters
int canardAVRConfigureAcceptanceFilters(uint8_t node_id)
Set hardware acceptance filters for specific node ID.
Definition: canard_avr.c:146
uavcan::CanFrame::id
uint32_t id
CAN ID with flags (above)
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:34
BITRATE_500_KBPS
@ BITRATE_500_KBPS
Definition: can.h:90
can_filter_t
Datenstruktur zur Aufnahme von CAN-Filtern.
Definition: can.h:233
NORMAL_MODE
@ NORMAL_MODE
normaler Modus, CAN Controller ist aktiv
Definition: can.h:270
can_t::data
uint8_t data[8]
Die Daten der CAN Nachricht.
Definition: can.h:193
can_set_mode
void can_set_mode(can_mode_t mode)
Setzt den Operations-Modus.
can_get_message
uint8_t can_get_message(can_t *msg)
Liest eine Nachricht aus den Empfangspuffern des CAN Controllers.
BITRATE_100_KBPS
@ BITRATE_100_KBPS
Definition: can.h:87
canardAVRTransmit
int canardAVRTransmit(const CanardCANFrame *frame)
Transmits a CanardCANFrame to the CAN device.
Definition: canard_avr.c:95
canardAVRInit
int canardAVRInit(uint32_t bitrate)
Initialize CAN interface on AVR microcontroller.
Definition: canard_avr.c:18
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
BITRATE_250_KBPS
@ BITRATE_250_KBPS
Definition: can.h:89
BITRATE_125_KBPS
@ BITRATE_125_KBPS
Definition: can.h:88
can_t::extended
int extended
extended ID?
Definition: can.h:183
can_set_filter
bool can_set_filter(uint8_t number, const can_filter_t *filter)
Setzen eines Filters.
uavcan::CanFrame::data
uint8_t data[MaxDataLen]
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:35
can_check_message
bool can_check_message(void)
Ueberpruefen ob neue CAN Nachrichten vorhanden sind.
frame
uavcan::CanFrame frame
Definition: can.cpp:78
BITRATE_1_MBPS
@ BITRATE_1_MBPS
Definition: can.h:91
can_t::flags
struct can_t::@136 flags
BITRATE_10_KBPS
@ BITRATE_10_KBPS
Definition: can.h:84
can_init
bool can_init(can_bitrate_t bitrate)
Initialisierung des CAN Interfaces.
can_t::id
uint32_t id
ID der Nachricht (11 oder 29 Bit)
Definition: can.h:180
BITRATE_20_KBPS
@ BITRATE_20_KBPS
Definition: can.h:85
canard_avr.h
canardAVRReceive
int canardAVRReceive(CanardCANFrame *out_frame)
Receives a CanardCANFrame from the CAN device.
Definition: canard_avr.c:119


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02