spi.c
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 /*
3  * Copyright (c) 2008 Florian Kristen, Fabian Greif
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: spi.c 6802 2008-11-12 10:25:05Z fabian $
28  */
29 // ----------------------------------------------------------------------------
30 
31 #include "mcp2515_private.h"
32 #ifdef SUPPORT_FOR_MCP2515__
33 
34 #include "spi.h"
35 
36 #ifdef SPI_PRESCALER
37  #if (SPI_PRESCALER == 2) || (SPI_PRESCALER == 8) || (SPI_PRESCALER == 32) || (SPI_PRESCALER == 64)
38  #define R_SPSR (1<<SPI2X)
39  #define SPI_PRESCALER_ (SPI_PRESCALER * 2)
40  #else
41  #define R_SPSR 0
42  #define SPI_PRESCALER_ SPI_PRESCALER
43  #endif
44 
45  #define SPI_CLOCK_RATE_BIT0 (1<<SPR0)
46  #define SPI_CLOCK_RATE_BIT1 (1<<SPR1)
47 
48  #if (SPI_PRESCALER_ == 4)
49  #define R_SPCR 0
50  #elif (SPI_PRESCALER_ == 16)
51  #define R_SPCR SPI_CLOCK_RATE_BIT0
52  #elif (SPI_PRESCALER_ == 64)
53  #define R_SPCR SPI_CLOCK_RATE_BIT1
54  #elif (SPI_PRESCALER_ == 128)
55  #define R_SPCR SPI_CLOCK_RATE_BIT1 | SPI_CLOCK_RATE_BIT0
56  #else
57  #error SPI_PRESCALER must be one of the values of 2^n with n = 1..7!
58  #endif
59 #else
60  #error SPI_PRESCALER not defined!
61 #endif
62 
63 
64 // ----------------------------------------------------------------------------
65 void mcp2515_spi_init(void)
66 {
67  #ifndef USE_SOFTWARE_SPI
68  // Aktivieren des SPI Master Interfaces
69  SPCR = (1<<SPE)|(1<<MSTR) | R_SPCR;
70  SPSR = R_SPSR;
71  #endif
72 }
73 
74 // ----------------------------------------------------------------------------
75 // Schreibt/liest ein Byte ueber den Hardware SPI Bus
76 
78 {
79  #ifdef USE_SOFTWARE_SPI
80 
81  uint8_t data_in = 0;
82 
83  RESET(P_SCK);
84  for (uint8_t i=0;i<8;i++)
85  {
86  data_in <<= 1;
87 
88  if (data & 0x80)
89  SET(P_MOSI);
90  else
91  RESET(P_MOSI);
92 
93  SET(P_SCK);
94  _delay_us(2);
95 
96  if (IS_SET(P_MISO))
97  data_in |= 1;
98 
99  data <<= 1;
100 
101  RESET(P_SCK);
102  _delay_us(2);
103  }
104 
105  return data_in;
106 
107  #else
108 
109  // put byte in send-buffer
110  SPDR = data;
111 
112  // wait until byte was send
113  while( !( SPSR & (1<<SPIF) ) )
114  ;
115 
116  return SPDR;
117 
118  #endif
119 }
120 
121 #endif // SUPPORT_FOR_MCP2515__
spi.h
mcp2515_spi_init
void mcp2515_spi_init(void)
Initialize SPI interface.
SET
@ SET
Definition: lpc_types.h:62
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.
IS_SET
#define IS_SET(x)
Definition: utils.h:234
RESET
@ RESET
Definition: lpc_types.h:62


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