SPI Functional
This commit is contained in:
@@ -17,14 +17,14 @@
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
my_spi_master: &spi3 {
|
spi_master: &spi3 {
|
||||||
compatible = "nordic,nrf-spim";
|
compatible = "nordic,nrf-spim";
|
||||||
status = "okay";
|
status = "okay";
|
||||||
pinctrl-0 = <&spi_master_default>;
|
pinctrl-0 = <&spi_master_default>;
|
||||||
pinctrl-1 = <&spi_master_sleep>;
|
pinctrl-1 = <&spi_master_sleep>;
|
||||||
pinctrl-names = "default", "sleep";
|
pinctrl-names = "default", "sleep";
|
||||||
cs-gpios = <&gpio0 28 GPIO_ACTIVE_LOW>;
|
cs-gpios = <&gpio0 28 GPIO_ACTIVE_LOW>;
|
||||||
reg_my_spi_master: spi-dev-a@0 {
|
reg_spi_master: spi-dev-a@0 {
|
||||||
reg = <0>;
|
reg = <0>;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
84
src/main.c
84
src/main.c
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stdint.h>
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
#include <zephyr/device.h>
|
#include <zephyr/device.h>
|
||||||
#include <zephyr/devicetree.h>
|
#include <zephyr/devicetree.h>
|
||||||
@@ -9,23 +10,20 @@
|
|||||||
|
|
||||||
/* The devicetree node identifier for the "led0" alias. */
|
/* The devicetree node identifier for the "led0" alias. */
|
||||||
#define LED0_NODE DT_ALIAS(led0)
|
#define LED0_NODE DT_ALIAS(led0)
|
||||||
|
#define SPI_MASTER DT_NODELABEL(spi_master)
|
||||||
|
|
||||||
#define MY_SPI_MASTER DT_NODELABEL(my_spi_master)
|
// SPI
|
||||||
|
|
||||||
#define MY_SPI_SLAVE DT_NODELABEL(my_spi_slave)
|
|
||||||
|
|
||||||
// SPI master functionality
|
|
||||||
const struct device *spi_dev;
|
const struct device *spi_dev;
|
||||||
static struct k_poll_signal spi_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_done_sig);
|
static struct k_poll_signal spi_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_done_sig);
|
||||||
|
|
||||||
struct spi_cs_control spim_cs = {
|
struct spi_cs_control spim_cs = {
|
||||||
.gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_my_spi_master)),
|
.gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_spi_master)),
|
||||||
.delay = 0,
|
.delay = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void spi_init(void)
|
static void spi_init(void)
|
||||||
{
|
{
|
||||||
spi_dev = DEVICE_DT_GET(MY_SPI_MASTER);
|
spi_dev = DEVICE_DT_GET(SPI_MASTER);
|
||||||
if(!device_is_ready(spi_dev)) {
|
if(!device_is_ready(spi_dev)) {
|
||||||
printk("SPI master device not ready!\n");
|
printk("SPI master device not ready!\n");
|
||||||
}
|
}
|
||||||
@@ -35,58 +33,50 @@ static void spi_init(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static const struct spi_config spi_cfg = {
|
static const struct spi_config spi_cfg = {
|
||||||
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
|
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB,
|
||||||
SPI_MODE_CPHA,
|
|
||||||
.frequency = 4000000,
|
.frequency = 4000000,
|
||||||
.slave = 0,
|
.slave = 0,
|
||||||
.cs = &spim_cs,
|
.cs = &spim_cs,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int spi_write_test_msg(void)
|
static int spi_write_8(uint8_t command) {
|
||||||
{
|
|
||||||
static uint8_t counter = 0;
|
|
||||||
static uint8_t tx_buffer[2];
|
|
||||||
static uint8_t rx_buffer[2];
|
|
||||||
|
|
||||||
const struct spi_buf tx_buf = {
|
const struct spi_buf tx_buf = {
|
||||||
.buf = tx_buffer,
|
.buf = &command,
|
||||||
.len = sizeof(tx_buffer)
|
.len = sizeof(command)
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct spi_buf_set tx = {
|
const struct spi_buf_set tx = {
|
||||||
.buffers = &tx_buf,
|
.buffers = &tx_buf,
|
||||||
.count = 1
|
.count = 1
|
||||||
};
|
};
|
||||||
|
|
||||||
struct spi_buf rx_buf = {
|
k_poll_signal_reset(&spi_done_sig);
|
||||||
.buf = rx_buffer,
|
int error = spi_write(spi_dev, &spi_cfg, &tx);
|
||||||
.len = sizeof(rx_buffer),
|
if(error != 0){
|
||||||
|
printk("SPI write error: %i\n", error);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int spi_write_buf(uint8_t* buf, int len) {
|
||||||
|
const struct spi_buf tx_buf = {
|
||||||
|
.buf = buf,
|
||||||
|
.len = len
|
||||||
};
|
};
|
||||||
const struct spi_buf_set rx = {
|
|
||||||
.buffers = &rx_buf,
|
const struct spi_buf_set tx = {
|
||||||
|
.buffers = &tx_buf,
|
||||||
.count = 1
|
.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);
|
k_poll_signal_reset(&spi_done_sig);
|
||||||
// Start transaction
|
int error = spi_write(spi_dev, &spi_cfg, &tx);
|
||||||
|
|
||||||
int error = spi_transceive_async(spi_dev, &spi_cfg, &tx, &rx, &spi_done_sig);
|
|
||||||
if(error != 0){
|
if(error != 0){
|
||||||
printk("SPI transceive error: %i\n", error);
|
printk("SPI write error: %i\n", error);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A build error on this line means your board is unsupported.
|
* A build error on this line means your board is unsupported.
|
||||||
* See the sample documentation for information on how to fix this.
|
* See the sample documentation for information on how to fix this.
|
||||||
@@ -108,16 +98,24 @@ void main(void)
|
|||||||
|
|
||||||
spi_init();
|
spi_init();
|
||||||
|
|
||||||
|
k_msleep(SLEEP_TIME_MS);
|
||||||
|
|
||||||
printk("SPI master/slave example started\n");
|
spi_write_8(0x12);
|
||||||
|
|
||||||
|
k_msleep(SLEEP_TIME_MS);
|
||||||
|
|
||||||
|
spi_write_8(0x46);
|
||||||
|
spi_write_8(0xF7);
|
||||||
|
|
||||||
|
k_msleep(SLEEP_TIME_MS);
|
||||||
|
|
||||||
|
spi_write_8(0x47);
|
||||||
|
spi_write_8(0xF7);
|
||||||
|
|
||||||
|
k_msleep(SLEEP_TIME_MS);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
spi_write_test_msg();
|
gpio_pin_toggle_dt(&led);
|
||||||
ret = gpio_pin_toggle_dt(&led);
|
|
||||||
if (ret < 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
k_msleep(SLEEP_TIME_MS);
|
k_msleep(SLEEP_TIME_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
29091
zephyr.hex
29091
zephyr.hex
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user