|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <zephyr/kernel.h> |
| 7 | +#include <zephyr/logging/log.h> |
| 8 | +#include <zephyr/drivers/spi.h> |
| 9 | +#include <zephyr/drivers/gpio.h> |
| 10 | + |
| 11 | +#define BUF_SIZE 32 |
| 12 | +static const struct device *spis_dev = DEVICE_DT_GET(DT_ALIAS(spis)); |
| 13 | +static const struct spi_config spis_config = {.operation = SPI_OP_MODE_SLAVE | SPI_WORD_SET(8)}; |
| 14 | + |
| 15 | +static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led), gpios); |
| 16 | + |
| 17 | +LOG_MODULE_REGISTER(spi_wakeup); |
| 18 | + |
| 19 | +int main(void) |
| 20 | +{ |
| 21 | + bool status; |
| 22 | + static char rx_buffer[BUF_SIZE]; |
| 23 | + struct spi_buf rx_spi_bufs = {.buf = rx_buffer, .len = sizeof(rx_buffer)}; |
| 24 | + struct spi_buf_set rx_spi_buf_set = {.buffers = &rx_spi_bufs, .count = 1}; |
| 25 | + |
| 26 | + LOG_INF("Hello world from %s", CONFIG_BOARD_TARGET); |
| 27 | + |
| 28 | + status = gpio_is_ready_dt(&led); |
| 29 | + __ASSERT(status, "Error: GPIO Device not ready"); |
| 30 | + |
| 31 | + status = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); |
| 32 | + __ASSERT(status == 0, "Could not configure led GPIO"); |
| 33 | + |
| 34 | + status = device_is_ready(spis_dev); |
| 35 | + __ASSERT(status, "Error: SPI device is not ready"); |
| 36 | + |
| 37 | + while (1) { |
| 38 | + memset(rx_buffer, 0x00, sizeof(rx_buffer)); |
| 39 | + LOG_INF("SPIS: waiting for SPI transfer; going to sleep..."); |
| 40 | + gpio_pin_set_dt(&led, 0); |
| 41 | + spi_read(spis_dev, &spis_config, &rx_spi_buf_set); |
| 42 | + gpio_pin_set_dt(&led, 1); |
| 43 | + LOG_INF("SPIS: woken up by %s", rx_buffer); |
| 44 | + LOG_INF("SPIS: will be active for 500ms after transfer"); |
| 45 | + k_busy_wait(500000); |
| 46 | + } |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
0 commit comments