mcp2515_write_id.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_write_id.c 6568 2008-06-16 13:56:26Z fabian $
28  */
29 // ----------------------------------------------------------------------------
30 
31 #include "mcp2515_private.h"
32 #ifdef SUPPORT_FOR_MCP2515__
33 
34 // ----------------------------------------------------------------------------
35 #ifdef USE_SOFTWARE_SPI
36 
37 static uint8_t usi_interface_spi_temp;
38 
39 static void spi_start(uint8_t data) {
40  usi_interface_spi_temp = spi_putc(data);
41 }
42 
43 static uint8_t spi_wait(void) {
44  return usi_interface_spi_temp;
45 }
46 
47 #else
48 
49 static void spi_start(uint8_t data) {
50  SPDR = data;
51 }
52 
53 static uint8_t spi_wait(void) {
54  // warten bis der vorherige Werte geschrieben wurde
55  while(!(SPSR & (1<<SPIF)))
56  ;
57 
58  return SPDR;
59 }
60 
61 #endif
62 
63 // ----------------------------------------------------------------------------
64 /* Schreibt eine CAN ID in die Register des MCP2515
65  *
66  * Die Funktion setzt eine offene Verbindung zum MCP2515 vorraus
67  * und schreibt dann die CAN ID per SPI in die folgenden vier
68  * Register des MCP2515.
69  *
70  * ACHTUNG: die Funktion wurde "optimiert", damit nicht ständig unnötige
71  * 32-Bit Operationen verwendet werden :)
72  *
73  * Funktionell aequivalent zu:
74  *
75  * static void mcp2515_write_id(uint32_t *id, uint8_t extended)
76  * {
77  * if (extended) {
78  * spi_putc(*id >> 21);
79  * spi_putc(((*id >> 13) & 0xe0) | (1<<IDE) | ((*id >> 16) & 0x3));
80  * spi_putc(*id >> 8);
81  * spi_putc(*id);
82  * }
83  * else {
84  * spi_putc(*id >> 3);
85  * spi_putc(*id << 5);
86  * spi_putc(0);
87  * spi_putc(0);
88  * }
89  * }
90  */
91 
92 #if SUPPORT_EXTENDED_CANID
93 
94 void mcp2515_write_id(const uint32_t *id, uint8_t extended)
95 {
96  uint8_t tmp;
97 
98  if (extended) {
99  spi_start(*((uint16_t *) id + 1) >> 5);
100 
101  // naechsten Werte berechnen
102  tmp = (*((uint8_t *) id + 2) << 3) & 0xe0;
103  tmp |= (1 << IDE);
104  tmp |= (*((uint8_t *) id + 2)) & 0x03;
105 
106  // warten bis der vorherige Werte geschrieben wurde
107  spi_wait();
108 
109  // restliche Werte schreiben
110  spi_putc(tmp);
111  spi_putc(*((uint8_t *) id + 1));
112  spi_putc(*((uint8_t *) id));
113  }
114  else {
115  spi_start(*((uint16_t *) id) >> 3);
116 
117  // naechsten Werte berechnen
118  tmp = *((uint8_t *) id) << 5;
119  spi_wait();
120 
121  spi_putc(tmp);
122  spi_putc(0);
123  spi_putc(0);
124  }
125 }
126 
127 #else
128 
129 void mcp2515_write_id(const uint16_t *id)
130 {
131  uint8_t tmp;
132 
133  spi_start(*id >> 3);
134  tmp = *((uint8_t *) id) << 5;
135  spi_wait();
136 
137  spi_putc(tmp);
138  spi_putc(0);
139  spi_putc(0);
140 }
141 
142 #endif // USE_EXTENDED_CANID
143 
144 #endif // SUPPORT_FOR_MCP2515__
uavcan::uint32_t
std::uint32_t uint32_t
Definition: std.hpp:26
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
mcp2515_private.h
spi_putc
uint8_t spi_putc(uint8_t data)
Write/read one byte of the SPI interface.


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