SPI Functional

This commit is contained in:
Ben Kyd
2023-06-19 01:00:33 +01:00
parent 4a130a9e74
commit 28728f90e5
4 changed files with 94754 additions and 1946 deletions

View File

@@ -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>;
}; };
}; };

67497
spi.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -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,57 +33,49 @@ 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) {
{ const struct spi_buf tx_buf = {
static uint8_t counter = 0; .buf = &command,
static uint8_t tx_buffer[2]; .len = sizeof(command)
static uint8_t rx_buffer[2]; };
const struct spi_buf tx_buf = { const struct spi_buf_set tx = {
.buf = tx_buffer, .buffers = &tx_buf,
.len = sizeof(tx_buffer) .count = 1
}; };
const struct spi_buf_set tx = {
.buffers = &tx_buf,
.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);
const struct spi_buf_set rx = { return error;
.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_async(spi_dev, &spi_cfg, &tx, &rx, &spi_done_sig);
if(error != 0){
printk("SPI transceive error: %i\n", error);
return error;
}
return 0;
} }
// SPI slave functionality static int spi_write_buf(uint8_t* buf, int len) {
const struct device *spi_slave_dev; const struct spi_buf tx_buf = {
static struct k_poll_signal spi_slave_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_slave_done_sig); .buf = buf,
.len = len
};
const struct spi_buf_set tx = {
.buffers = &tx_buf,
.count = 1
};
k_poll_signal_reset(&spi_done_sig);
int error = spi_write(spi_dev, &spi_cfg, &tx);
if(error != 0){
printk("SPI write error: %i\n", error);
return error;
}
}
/* /*
* A build error on this line means your board is unsupported. * A build error on this line means your board is unsupported.
@@ -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

File diff suppressed because it is too large Load Diff