non functional spi

This commit is contained in:
Ben Kyd
2023-06-04 20:03:53 +01:00
parent f51302bc0f
commit cd7c124e24
5 changed files with 1796 additions and 1837 deletions

View File

@@ -6,6 +6,12 @@ endif()
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2")
project(passr)
target_sources(app PRIVATE src/main.c)

View File

@@ -6,7 +6,8 @@ CMAKE_ZEPHYR_PATH=../zephyrproject/zephyr
CMAKE_ZEPHYR_COMMAND=west build
CMAKE_ZEPHYR_FLAGS=-b $(TARGET)
all: compile flash
all: compile flash listen
cf: compile flash
setup:
west init
@@ -19,9 +20,11 @@ flash: compile
cp build/zephyr/zephyr.hex ./zephyr.hex
echo "Copied zephyr.hex to project root"
nrfjprog -f nrf52 --program zephyr.hex --sectorerase --verify --reset
listen: flash
minicom -D /dev/ttyACM0 -b 115200
listen:
com:
minicom -D /dev/ttyACM0 -b 115200
clean:

View File

@@ -15,49 +15,20 @@
low-power-enable;
};
};
spi_slave_default: spi_slave_default {
group1 {
psels = <NRF_PSEL(SPIS_SCK, 1, 1)>,
<NRF_PSEL(SPIS_MOSI, 1, 2)>,
<NRF_PSEL(SPIS_MISO, 1, 3)>,
<NRF_PSEL(SPIS_CSN, 1, 4)>;
};
};
spi_slave_sleep: spi_slave_sleep {
group1 {
psels = <NRF_PSEL(SPIS_SCK, 1, 1)>,
<NRF_PSEL(SPIS_MOSI, 1, 2)>,
<NRF_PSEL(SPIS_MISO, 1, 3)>,
<NRF_PSEL(SPIS_CSN, 1, 4)>;
low-power-enable;
};
};
};
my_spi_master: &spi3 {
spi_master: &spi0 {
compatible = "nordic,nrf-spim";
status = "okay";
pinctrl-0 = <&spi_master_default>;
pinctrl-1 = <&spi_master_sleep>;
pinctrl-names = "default", "sleep";
cs-gpios = <&gpio0 28 GPIO_ACTIVE_LOW>;
reg_my_spi_master: spi-dev-a@0 {
reg_spi_master: spi-dev-a@0 {
reg = <0>;
};
};
my_spi_slave: &spi1 {
compatible = "nordic,nrf-spis";
status = "okay";
pinctrl-0 = <&spi_slave_default>;
pinctrl-1 = <&spi_slave_sleep>;
pinctrl-names = "default", "sleep";
def-char = <0x00>;
};
// By default uart1 will occupy P1.01 and P1.02. In order to make these pins available, disable uart1
&uart1 {
status="disabled";
};

View File

@@ -5,27 +5,35 @@
#include <zephyr/drivers/spi.h>
#include <zephyr/sys/printk.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 SPI_MASTER DT_NODELABEL(spi_master)
#define SPI_SLAVE DT_NODELABEL(spi_slave)
#define MY_SPI_MASTER DT_NODELABEL(my_spi_master)
#define MY_SPI_SLAVE DT_NODELABEL(my_spi_slave)
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
// SPI master functionality
const struct device *spi_dev;
static struct k_poll_signal spi_done_sig = K_POLL_SIGNAL_INITIALIZER(spi_done_sig);
struct spi_cs_control spim_cs = {
.gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_my_spi_master)),
const struct spi_cs_control spim_cs = {
.gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(reg_spi_master)),
.delay = 0,
};
static const struct spi_config spi_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_MODE_CPOL | SPI_MODE_CPHA,
.frequency = 10000,
.slave = 0,
.cs = {{(const struct device*)&spim_cs}},
};
static void spi_init(void)
{
spi_dev = DEVICE_DT_GET(MY_SPI_MASTER);
printk("SPI master init\n");
spi_dev = DEVICE_DT_GET(SPI_MASTER);
if(!device_is_ready(spi_dev)) {
printk("SPI master device not ready!\n");
}
@@ -34,13 +42,6 @@ static void spi_init(void)
}
}
static const struct spi_config spi_cfg = {
.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
SPI_MODE_CPOL | SPI_MODE_CPHA,
.frequency = 4000000,
.slave = 0,
.cs = &spim_cs,
};
static int spi_write_test_msg(void)
{
@@ -57,14 +58,14 @@ static int spi_write_test_msg(void)
.count = 1
};
struct spi_buf rx_buf = {
/* 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++;
@@ -74,7 +75,7 @@ static int spi_write_test_msg(void)
k_poll_signal_reset(&spi_done_sig);
// Start transaction
int error = spi_transceive_async(spi_dev, &spi_cfg, &tx, &rx, &spi_done_sig);
int error = spi_transceive(spi_dev, &spi_cfg, &tx, NULL);
if(error != 0){
printk("SPI transceive error: %i\n", error);
return error;
@@ -89,82 +90,6 @@ static int spi_write_test_msg(void)
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_async(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);
void main(void)
{
int ret;
@@ -180,11 +105,7 @@ void main(void)
spi_init();
spi_slave_init();
printk("SPI master/slave example started\n");
spi_slave_write_test_msg();
printk("SPI master started\n");
while (1) {
spi_write_test_msg();
@@ -194,14 +115,5 @@ void main(void)
return;
}
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();
}
}
printk("SPI master/slave example finished\n");
}

3461
zephyr.hex

File diff suppressed because it is too large Load Diff