mcp2515_send_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_send_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 #include <util/delay.h>
35 
36 // ----------------------------------------------------------------------------
37 uint8_t mcp2515_send_message(const can_t *msg)
38 {
39  // Status des MCP2515 auslesen
40  uint8_t status = mcp2515_read_status(SPI_READ_STATUS);
41 
42  /* Statusbyte:
43  *
44  * Bit Funktion
45  * 2 TXB0CNTRL.TXREQ
46  * 4 TXB1CNTRL.TXREQ
47  * 6 TXB2CNTRL.TXREQ
48  */
49  uint8_t address;
50  if (_bit_is_clear(status, 2)) {
51  address = 0x00;
52  }
53  else if (_bit_is_clear(status, 4)) {
54  address = 0x02;
55  }
56  else if (_bit_is_clear(status, 6)) {
57  address = 0x04;
58  }
59  else {
60  // Alle Puffer sind belegt,
61  // Nachricht kann nicht verschickt werden
62  return 0;
63  }
64 
66  spi_putc(SPI_WRITE_TX | address);
67  #if SUPPORT_EXTENDED_CANID
68  mcp2515_write_id(&msg->id, msg->flags.extended);
69  #else
70  mcp2515_write_id(&msg->id);
71  #endif
72  uint8_t length = msg->length & 0x0f;
73 
74  // Ist die Nachricht ein "Remote Transmit Request" ?
75  if (msg->flags.rtr)
76  {
77  // Ein RTR hat zwar eine Laenge,
78  // enthaelt aber keine Daten
79 
80  // Nachrichten Laenge + RTR einstellen
81  spi_putc((1<<RTR) | length);
82  }
83  else
84  {
85  // Nachrichten Laenge einstellen
86  spi_putc(length);
87 
88  // Daten
89  for (uint8_t i=0;i<length;i++) {
90  spi_putc(msg->data[i]);
91  }
92  }
93  SET(MCP2515_CS);
94 
95  _delay_us(1);
96 
97  // CAN Nachricht verschicken
98  // die letzten drei Bit im RTS Kommando geben an welcher
99  // Puffer gesendet werden soll.
100  RESET(MCP2515_CS);
101  address = (address == 0) ? 1 : address;
102  spi_putc(SPI_RTS | address);
103  SET(MCP2515_CS);
104 
106 
107  return address;
108 }
109 
110 #endif // SUPPORT_FOR_MCP2515__
CAN_INDICATE_TX_TRAFFIC_FUNCTION
#define CAN_INDICATE_TX_TRAFFIC_FUNCTION
Definition: can_private.h:133
can_t::length
uint8_t length
Anzahl der Datenbytes.
Definition: can.h:192
SPI_RTS
#define SPI_RTS
Definition: mcp2515_defs.h:41
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
SET
@ SET
Definition: lpc_types.h:62
SPI_WRITE_TX
#define SPI_WRITE_TX
Definition: mcp2515_defs.h:40
can_t::data
uint8_t data[8]
Die Daten der CAN Nachricht.
Definition: can.h:193
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
_bit_is_clear
#define _bit_is_clear(pin, bit)
Definition: utils.h:274
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
RESET
@ RESET
Definition: lpc_types.h:62
SPI_READ_STATUS
#define SPI_READ_STATUS
Definition: mcp2515_defs.h:42
MCP2515_CS
#define MCP2515_CS
Definition: src/config.h:48


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