at90can_get_message.c
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 /*
3  * Copyright (c) 2007 Fabian Greif, Roboterclub Aachen e.V.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $Id: at90can_get_message.c 7224 2009-01-25 20:19:32Z fabian $
28  */
29 // ----------------------------------------------------------------------------
30 
31 #include "at90can_private.h"
32 #ifdef SUPPORT_FOR_AT90CAN__
33 
34 bool at90can_copy_mob_to_message(can_t *msg)
35 {
36  // read status
37  uint8_t cancdmob = CANCDMOB;
38 
39  // read length
40  msg->length = cancdmob & 0x0f;
41 
42  #if SUPPORT_EXTENDED_CANID
43 
44  if (cancdmob & (1 << IDE))
45  {
46  // extended identifier
47  uint32_t tmp;
48  uint8_t *ptr = (uint8_t *) &tmp;
49 
50  *ptr = CANIDT4;
51  *(ptr + 1) = CANIDT3;
52  *(ptr + 2) = CANIDT2;
53  *(ptr + 3) = CANIDT1;
54 
55  msg->id = tmp >> 3;
56 
57  /* equivalent to:
58  msg->id = (uint8_t) CANIDT4 >> 3;
59  msg->id |= (uint32_t) CANIDT3 << 5;
60  msg->id |= (uint32_t) CANIDT2 << 13;
61  msg->id |= (uint32_t) CANIDT1 << 21;
62  */
63 
64  msg->flags.extended = 1;
65  }
66  else
67  {
68  // standard identifier
69  uint16_t id;
70 
71  id = (uint8_t) CANIDT2 >> 5;
72  id |= (uint16_t) CANIDT1 << 3;
73 
74  msg->id = (uint32_t) id;
75  msg->flags.extended = 0;
76  }
77 
78  #else
79 
80  if (cancdmob & (1 << IDE))
81  {
82  return false;
83  }
84  else
85  {
86  // standard identifier
87  msg->id = (uint8_t) CANIDT2 >> 5;
88  msg->id |= (uint16_t) CANIDT1 << 3;
89  }
90 
91  #endif
92 
93  if (CANIDT4 & (1 << RTRTAG)) {
94  msg->flags.rtr = 1;
95  }
96  else {
97  msg->flags.rtr = 0;
98 
99  // read data
100  uint8_t *p = msg->data;
101  for (uint8_t i = 0;i < msg->length;i++) {
102  *p++ = CANMSG;
103  }
104  }
105 
106  #if SUPPORT_TIMESTAMPS
107  msg->timestamp = CANSTM;
108  #endif
109 
110  return true;
111 }
112 
113 // ----------------------------------------------------------------------------
114 
115 uint8_t at90can_get_message(can_t *msg)
116 {
117  bool found = false;
118  uint8_t mob;
119 
120  // check if there is any waiting message
121  if (!at90can_check_message())
122  return 0;
123 
124  // find the MOb with the received message
125  for (mob = 0; mob < 15; mob++)
126  {
127  CANPAGE = mob << 4;
128 
129  if (CANSTMOB & (1<<RXOK))
130  {
131  found = true;
132 
133  // clear flags
134  CANSTMOB &= 0;
135  break;
136  }
137  }
138 
139  if (!found) {
140  return 0; // should never happen
141  }
142 
143  found = at90can_copy_mob_to_message( msg );
144 
145  #if CAN_RX_BUFFER_SIZE == 0
146  // mark message as processed
148  _messages_waiting--;
150  #endif
151 
152  // re-enable interrupts
153  _enable_mob_interrupt( mob );
154 
155  // clear flags
156  CANCDMOB = (1 << CONMOB1) | (CANCDMOB & (1 << IDE));
157 
158  if (found) {
159  return (mob + 1);
160  }
161  else {
162  // only if SUPPORT_EXTENDED_CANID=0 and a extended message was received
163  return 0;
164  }
165 }
166 
167 #endif // SUPPORT_FOR_AT90CAN__
ENTER_CRITICAL_SECTION
#define ENTER_CRITICAL_SECTION
Definition: utils.h:126
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::rtr
int rtr
Remote-Transmit-Request-Frame?
Definition: can.h:182
can_t
Datenstruktur zum Aufnehmen von CAN Nachrichten.
Definition: can.h:177
can_t::data
uint8_t data[8]
Die Daten der CAN Nachricht.
Definition: can.h:193
uavcan::uint16_t
std::uint16_t uint16_t
Definition: std.hpp:25
IDE
#define IDE
Definition: mcp2515_defs.h:296
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
at90can_private.h
can_t::extended
int extended
extended ID?
Definition: can.h:183
can_t::flags
struct can_t::@136 flags
LEAVE_CRITICAL_SECTION
#define LEAVE_CRITICAL_SECTION
Definition: utils.h:127
can_t::id
uint32_t id
ID der Nachricht (11 oder 29 Bit)
Definition: can.h:180


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