TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
SPI

Enumerations

enum  spi_slave { SSB2 , SSB3 , SSE2 }
 Identifiers for available SPI slave devices. More...

Functions

int spi_init (void)
 Initialize SPI in master mode.
int spi_ready (void)
 Check if the SPI subsystem is initialized.
int spi_send_n (enum spi_slave *slave, unsigned char *data, int length)
 Send a sequence of bytes to a slave without reading response.
int spi_send (enum spi_slave *slave, unsigned char data)
 Send a single byte to a slave.
int spi_recieve (enum spi_slave *slave, unsigned char *out)
 Receive one byte from a slave.
int spi_recieve_n (enum spi_slave *slave, unsigned char *out, int length)
 Receive several bytes from a slave.
int spi_duplex (enum spi_slave *slave, unsigned char *data, unsigned char *out, int length)
 Full-duplex SPI transfer.
int spi_set_slave_select (enum spi_slave *slave, unsigned char state)
 Set the slave-select line for the given device.
int spi_push (enum spi_slave *slave, unsigned char data, unsigned char *out)
 Push one byte to a slave and optionally read the response.

Detailed Description

Enumeration Type Documentation

◆ spi_slave

enum spi_slave

#include <spi.h>

Identifiers for available SPI slave devices.

Enumerator
SSB2 

Slave on PB2

SSB3 

Slave on PB3

SSE2 

Slave on PE2

Definition at line 15 of file spi.h.

15 {
16 SSB2,
17 SSB3,
18 SSE2
19};
@ SSB2
Definition spi.h:16
@ SSB3
Definition spi.h:17
@ SSE2
Definition spi.h:18

Function Documentation

◆ spi_duplex()

int spi_duplex ( enum spi_slave * slave,
unsigned char * data,
unsigned char * out,
int length )

#include <spi.h>

Full-duplex SPI transfer.

Sends length bytes and reads a response simultaneously.

Parameters
[in]slavePointer to slave device.
[in]dataTransmit buffer, or TRASHCAN for dummy writes.
[out]outReceive buffer, or TRASHCAN to discard input.
[in]lengthNumber of bytes to exchange.
Returns
0 on success.

Definition at line 131 of file spi.c.

132 {
133 spi_set_slave_select(slave, 0);
134
135 for (int i = 0; i < length; i++) {
136 unsigned char tx = 0xFF;
137 if (data != TRASHCAN) {
138 tx = data[i];
139 }
140
141 SPDR = tx;
142 while (!(SPSR & (1 << SPIF))) {
143 }
144
145 if (out != TRASHCAN) {
146 out[i] = SPDR;
147 }
148 }
149
150 spi_set_slave_select(slave, 1);
151 return 0;
152}
int spi_set_slave_select(enum spi_slave *slave, unsigned char state)
Set the slave-select line for the given device.
Definition spi.c:43
#define TRASHCAN
Definition utils.h:19

Referenced by MCP2515_bit_modify(), MCP2515_read(), MCP2515_read_status(), spi_recieve(), spi_recieve_n(), spi_send(), and spi_send_n().

◆ spi_init()

int spi_init ( void )

#include <spi.h>

Initialize SPI in master mode.

Configures MOSI, SCK, and SS pins, sets clock prescaler, and ensures all slave-select lines default to inactive.

Returns
0 on success, non-zero on failure.

Definition at line 9 of file spi.c.

9 {
10 int status = 0;
11
12 DDRB |= (1 << PB5) | (1 << PB7) |
13 (1 << PB4); // Sets the required PORT B pins to SPI mode
14
15 SPCR |= (1 << SPE) |
16 (1 << MSTR) // Enables the SPI-bus and sets it to master mode, MSB
17 | (1 << SPR0); // first, no interrupt, and a clock freq of f_osc/4
18
19 /*
20 // Define PB2, PB4 and PE2 as slave select pins
21 */
22 DDRB |= (1 << PB2) | (1 << PB3) | (1 << PB4);
23 DDRE |= (1 << PE2);
24
25 /*
26 // Ensure initial state off
27 */
28 status = spi_set_slave_select((enum spi_slave *)SSB2, 1);
29 status = spi_set_slave_select((enum spi_slave *)SSB3, 1);
30 status = spi_set_slave_select((enum spi_slave *)SSE2, 1);
31
32 if (status) {
33 return status;
34 }
35
36 _ready = 1;
37
38 return status;
39}
spi_slave
Identifiers for available SPI slave devices.
Definition spi.h:15
static unsigned char _ready
Definition spi.c:7

Referenced by io_avr_init(), io_oled_init(), and MCP2515_init().

◆ spi_push()

int spi_push ( enum spi_slave * slave,
unsigned char data,
unsigned char * out )

#include <spi.h>

Push one byte to a slave and optionally read the response.

Parameters
[in]slavePointer to target slave.
[in]dataByte to send.
[out]outPointer to received byte (or TRASHCAN to ignore).
Returns
0 on success.

Definition at line 154 of file spi.c.

154 {
155
156 spi_set_slave_select(slave, 0);
157 SPDR = data;
158 while (!(SPSR & (1 << SPIF))) {
159 }
160
161 // Only write if pointer is valid
162 if (out != TRASHCAN) {
163 *out = SPDR;
164 }
165 return 0;
166}

Referenced by io_avr_buttons_read().

◆ spi_ready()

int spi_ready ( void )

#include <spi.h>

Check if the SPI subsystem is initialized.

Returns
0 if ready, non-zero otherwise.

Definition at line 41 of file spi.c.

41{ return !_ready; }

Referenced by io_avr_init(), io_oled_init(), and MCP2515_init().

◆ spi_recieve()

int spi_recieve ( enum spi_slave * slave,
unsigned char * out )

#include <spi.h>

Receive one byte from a slave.

Transmits dummy data and stores the received byte.

Parameters
[in]slavePointer to target slave.
[out]outPointer to received byte.
Returns
0 on success.

Definition at line 91 of file spi.c.

91 {
92 spi_duplex(slave, TRASHCAN, out, 1);
93 return 0;
94}
int spi_duplex(enum spi_slave *slave, unsigned char *data, unsigned char *out, int length)
Full-duplex SPI transfer.
Definition spi.c:131

◆ spi_recieve_n()

int spi_recieve_n ( enum spi_slave * slave,
unsigned char * out,
int length )

#include <spi.h>

Receive several bytes from a slave.

Parameters
[in]slavePointer to target slave.
[out]outBuffer to store received data.
[in]lengthNumber of bytes to read.
Returns
0 on success.

Definition at line 96 of file spi.c.

96 {
97 spi_duplex(slave, TRASHCAN, out, length);
98 return 0;
99}

Referenced by io_avr_buttons_read().

◆ spi_send()

int spi_send ( enum spi_slave * slave,
unsigned char data )

#include <spi.h>

Send a single byte to a slave.

Parameters
[in]slavePointer to target slave.
[in]dataByte to transmit.
Returns
0 on success.

Definition at line 86 of file spi.c.

86 {
87 spi_duplex(slave, &data, TRASHCAN, 1);
88 return 0;
89}

Referenced by _io_oled_cmd(), _io_oled_write(), MCP2515_request_to_send(), and MCP2515_reset().

◆ spi_send_n()

int spi_send_n ( enum spi_slave * slave,
unsigned char * data,
int length )

#include <spi.h>

Send a sequence of bytes to a slave without reading response.

Parameters
[in]slavePointer to the target slave device.
[in]dataBuffer of bytes to transmit.
[in]lengthNumber of bytes to send.
Returns
0 on success.

Definition at line 81 of file spi.c.

81 {
82 spi_duplex(slave, data, TRASHCAN, length);
83 return 0;
84}

Referenced by io_avr_led_set(), MCP2515_init(), MCP2515_write(), and MCP2515_write_n().

◆ spi_set_slave_select()

int spi_set_slave_select ( enum spi_slave * slave,
unsigned char state )

#include <spi.h>

Set the slave-select line for the given device.

Parameters
[in]slavePointer to slave identifier.
[in]state0 = select (active), 1 = deselect.
Returns
0 on success, -1 if slave is invalid.

Definition at line 43 of file spi.c.

43 {
44 switch (*slave) {
45 case SSB2:
46 if (!state) {
47 PORTB |= (1 << PB3);
48 PORTE |= (1 << PE2);
49
50 PORTB &= ~(1 << PB2);
51 } else {
52 PORTB |= (1 << PB2);
53 }
54 break;
55 case SSE2:
56 if (!state) {
57 PORTB |= (1 << PB2);
58 PORTB |= (1 << PB3);
59
60 PORTE &= ~(1 << PE2);
61 } else {
62 PORTE |= (1 << PE2);
63 }
64 break;
65 case SSB3:
66 if (!state) {
67 PORTB |= (1 << PB2);
68 PORTE |= (1 << PE2);
69
70 PORTB &= ~(1 << PB3);
71 } else {
72 PORTB |= (1 << PB3);
73 }
74 break;
75 default:
76 return -1;
77 }
78 return 0;
79}

Referenced by io_avr_buttons_read(), spi_duplex(), spi_init(), and spi_push().