mcp2515_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: mcp2515_get_message.c 7224 2009-01-25 20:19:32Z fabian $
28  */
29 // ----------------------------------------------------------------------------
30 
31 #include "mcp2515_private.h"
32 #ifdef SUPPORT_FOR_MCP2515__
33 
34 // ----------------------------------------------------------------------------
35 
36 uint8_t mcp2515_get_message(can_t *msg)
37 {
38  uint8_t addr;
39 
40  #ifdef RXnBF_FUNKTION
41  if (!IS_SET(MCP2515_RX0BF))
42  addr = SPI_READ_RX;
43  else if (!IS_SET(MCP2515_RX1BF))
44  addr = SPI_READ_RX | 0x04;
45  else
46  return 0;
47  #else
48  // read status
49  uint8_t status = mcp2515_read_status(SPI_RX_STATUS);
50 
51  if (_bit_is_set(status,6)) {
52  // message in buffer 0
53  addr = SPI_READ_RX;
54  }
55  else if (_bit_is_set(status,7)) {
56  // message in buffer 1
57  addr = SPI_READ_RX | 0x04;
58  }
59  else {
60  // Error: no message available
61  return 0;
62  }
63  #endif
64 
66  spi_putc(addr);
67 
68  // CAN ID auslesen und ueberpruefen
69  uint8_t tmp = mcp2515_read_id(&msg->id);
70  #if SUPPORT_EXTENDED_CANID
71  msg->flags.extended = tmp & 0x01;
72  #else
73  if (tmp & 0x01) {
74  // Nachrichten mit extended ID verwerfen
75  SET(MCP2515_CS);
76  #ifdef RXnBF_FUNKTION
77  if (!IS_SET(MCP2515_RX0BF))
78  #else
79  if (_bit_is_set(status, 6))
80  #endif
81  mcp2515_bit_modify(CANINTF, (1<<RX0IF), 0);
82  else
83  mcp2515_bit_modify(CANINTF, (1<<RX1IF), 0);
84 
85  return 0;
86  }
87  #endif
88 
89  // read DLC
90  uint8_t length = spi_putc(0xff);
91  #ifdef RXnBF_FUNKTION
92  if (!(tmp & 0x01))
93  msg->flags.rtr = (tmp & 0x02) ? 1 : 0;
94  else
95  msg->flags.rtr = (length & (1<<RTR)) ? 1 : 0;
96  #else
97  msg->flags.rtr = (_bit_is_set(status, 3)) ? 1 : 0;
98  #endif
99 
100  length &= 0x0f;
101  msg->length = length;
102  // read data
103  for (uint8_t i=0;i<length;i++) {
104  msg->data[i] = spi_putc(0xff);
105  }
106  SET(MCP2515_CS);
107 
108  // clear interrupt flag
109  #ifdef RXnBF_FUNKTION
110  if (!IS_SET(MCP2515_RX0BF))
111  #else
112  if (_bit_is_set(status, 6))
113  #endif
114  mcp2515_bit_modify(CANINTF, (1<<RX0IF), 0);
115  else
116  mcp2515_bit_modify(CANINTF, (1<<RX1IF), 0);
117 
119 
120  #ifdef RXnBF_FUNKTION
121  return 1;
122  #else
123  return (status & 0x07) + 1;
124  #endif
125 }
126 
127 #endif // SUPPORT_FOR_MCP2515__
can_t::length
uint8_t length
Anzahl der Datenbytes.
Definition: can.h:192
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
RTR
#define RTR
Bitdefinition von RXBnDLC (n = 0, 1)
Definition: mcp2515_defs.h:302
RX0IF
#define RX0IF
Definition: mcp2515_defs.h:256
SET
@ SET
Definition: lpc_types.h:62
SPI_READ_RX
#define SPI_READ_RX
Definition: mcp2515_defs.h:38
_bit_is_set
#define _bit_is_set(pin, bit)
Definition: utils.h:273
can_t::data
uint8_t data[8]
Die Daten der CAN Nachricht.
Definition: can.h:193
RX1IF
#define RX1IF
Definition: mcp2515_defs.h:255
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
SPI_RX_STATUS
#define SPI_RX_STATUS
Definition: mcp2515_defs.h:43
can_t::extended
int extended
extended ID?
Definition: can.h:183
can_t::flags
struct can_t::@136 flags
mcp2515_private.h
spi_putc
uint8_t spi_putc(uint8_t data)
Write/read one byte of the SPI interface.
can_t::id
uint32_t id
ID der Nachricht (11 oder 29 Bit)
Definition: can.h:180
IS_SET
#define IS_SET(x)
Definition: utils.h:234
CANINTF
#define CANINTF
Definition: mcp2515_defs.h:97
RESET
@ RESET
Definition: lpc_types.h:62
CAN_INDICATE_RX_TRAFFIC_FUNCTION
#define CAN_INDICATE_RX_TRAFFIC_FUNCTION
Definition: can_private.h:137
MCP2515_CS
#define MCP2515_CS
Definition: src/config.h:48


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