-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
209 lines (171 loc) · 4.77 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/spi.h>
/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000
/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
#define MY_SPI_MASTER DT_NODELABEL(my_spi_master)
#define MY_SPI_MASTER_CS_DT_SPEC SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_my_spi_master))
#define MY_SPI_SLAVE DT_NODELABEL(my_spi_slave)
// SPI master functionality
const struct device *spi_dev;
static struct k_poll_signal spi_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_done_sig);
static void spi_init(void)
{
spi_dev = DEVICE_DT_GET(MY_SPI_MASTER);
if(!device_is_ready(spi_dev)) {
printk("SPI master device not ready!\n");
}
struct gpio_dt_spec spim_cs_gpio = MY_SPI_MASTER_CS_DT_SPEC;
if(!device_is_ready(spim_cs_gpio.port)){
printk("SPI master chip select device not ready!\n");
}
}
static struct spi_config spi_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_MODE_CPOL | SPI_MODE_CPHA,
.frequency = 4000000,
.slave = 0,
.cs = {.gpio = MY_SPI_MASTER_CS_DT_SPEC, .delay = 0},
};
static int spi_write_test_msg(void)
{
static uint8_t counter = 0;
static uint8_t tx_buffer[2];
static uint8_t rx_buffer[2];
const struct spi_buf tx_buf = {
.buf = tx_buffer,
.len = sizeof(tx_buffer)
};
const struct spi_buf_set tx = {
.buffers = &tx_buf,
.count = 1
};
struct spi_buf rx_buf = {
.buf = rx_buffer,
.len = sizeof(rx_buffer),
};
const struct spi_buf_set rx = {
.buffers = &rx_buf,
.count = 1
};
// Update the TX buffer with a rolling counter
tx_buffer[0] = counter++;
printk("SPI TX: 0x%.2x, 0x%.2x\n", tx_buffer[0], tx_buffer[1]);
// Reset signal
k_poll_signal_reset(&spi_done_sig);
// Start transaction
int error = spi_transceive_signal(spi_dev, &spi_cfg, &tx, &rx, &spi_done_sig);
if(error != 0){
printk("SPI transceive error: %i\n", error);
return error;
}
// Wait for the done signal to be raised and log the rx buffer
int spi_signaled, spi_result;
do{
k_poll_signal_check(&spi_done_sig, &spi_signaled, &spi_result);
} while(spi_signaled == 0);
printk("SPI RX: 0x%.2x, 0x%.2x\n", rx_buffer[0], rx_buffer[1]);
return 0;
}
// SPI slave functionality
const struct device *spi_slave_dev;
static struct k_poll_signal spi_slave_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_slave_done_sig);
static const struct spi_config spi_slave_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_OP_MODE_SLAVE,
.frequency = 4000000,
.slave = 0,
};
static void spi_slave_init(void)
{
spi_slave_dev = DEVICE_DT_GET(MY_SPI_SLAVE);
if(!device_is_ready(spi_dev)) {
printk("SPI slave device not ready!\n");
}
}
static uint8_t slave_tx_buffer[2];
static uint8_t slave_rx_buffer[2];
static int spi_slave_write_test_msg(void)
{
static uint8_t counter = 0;
const struct spi_buf s_tx_buf = {
.buf = slave_tx_buffer,
.len = sizeof(slave_tx_buffer)
};
const struct spi_buf_set s_tx = {
.buffers = &s_tx_buf,
.count = 1
};
struct spi_buf s_rx_buf = {
.buf = slave_rx_buffer,
.len = sizeof(slave_rx_buffer),
};
const struct spi_buf_set s_rx = {
.buffers = &s_rx_buf,
.count = 1
};
// Update the TX buffer with a rolling counter
slave_tx_buffer[1] = counter++;
printk("SPI SLAVE TX: 0x%.2x, 0x%.2x\n", slave_tx_buffer[0], slave_tx_buffer[1]);
// Reset signal
k_poll_signal_reset(&spi_slave_done_sig);
// Start transaction
int error = spi_transceive_signal(spi_slave_dev, &spi_slave_cfg, &s_tx, &s_rx, &spi_slave_done_sig);
if(error != 0){
printk("SPI slave transceive error: %i\n", error);
return error;
}
return 0;
}
static int spi_slave_check_for_message(void)
{
int signaled, result;
k_poll_signal_check(&spi_slave_done_sig, &signaled, &result);
if(signaled != 0){
return 0;
}
else return -1;
}
/*
* A build error on this line means your board is unsupported.
* See the sample documentation for information on how to fix this.
*/
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
int main(void)
{
int ret;
if (!device_is_ready(led.port)) {
return 0;
}
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
if (ret < 0) {
return 0;
}
spi_init();
spi_slave_init();
printk("SPI master/slave example started\n");
spi_slave_write_test_msg();
while (1) {
spi_write_test_msg();
ret = gpio_pin_toggle_dt(&led);
if (ret < 0) {
return 0;
}
k_msleep(SLEEP_TIME_MS);
if(spi_slave_check_for_message() == 0){
// Print the last received data
printk("SPI SLAVE RX: 0x%.2x, 0x%.2x\n", slave_rx_buffer[0], slave_rx_buffer[1]);
// Prepare the next SPI slave transaction
spi_slave_write_test_msg();
}
}
return 0;
}