42 #if RUUVI_NRF5_SDK15_SPI_ENABLED
53 #include "nrf_drv_spi.h"
54 #include "app_util_platform.h"
58 static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE (
60 static bool m_spi_init_done =
false;
63 ruuvi_mode, nrf_drv_spi_mode_t * nrf_mode)
68 *nrf_mode = NRF_DRV_SPI_MODE_0;
72 *nrf_mode = NRF_DRV_SPI_MODE_1;
76 *nrf_mode = NRF_DRV_SPI_MODE_2;
80 *nrf_mode = NRF_DRV_SPI_MODE_3;
89 ruuvi_freq, nrf_drv_spi_frequency_t * nrf_freq)
94 *nrf_freq = NRF_DRV_SPI_FREQ_1M;
98 *nrf_freq = NRF_DRV_SPI_FREQ_2M;
102 *nrf_freq = NRF_DRV_SPI_FREQ_4M;
106 *nrf_freq = NRF_DRV_SPI_FREQ_8M;
119 if (m_spi_init_done) {
return NRF_ERROR_INVALID_STATE; }
123 nrf_drv_spi_frequency_t frequency = NRF_DRV_SPI_FREQ_1M;
124 status |= ruuvi_to_nrf_spi_mode (config->
mode, &mode);
125 status |= ruuvi_to_nrf_spi_freq (config->
frequency, &frequency);
127 nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
128 spi_config.ss_pin = NRF_DRV_SPI_PIN_NOT_USED;
129 spi_config.miso_pin = ruuvi_to_nrf_pin_map (config->
miso);
130 spi_config.mosi_pin = ruuvi_to_nrf_pin_map (config->
mosi);
131 spi_config.sck_pin = ruuvi_to_nrf_pin_map (config->
sclk);
133 spi_config.orc = 0xFF;
134 spi_config.frequency = frequency;
135 spi_config.mode = mode;
136 spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;
138 ret_code_t err_code = NRF_SUCCESS;
139 err_code = nrf_drv_spi_init (&spi, &spi_config, NULL, NULL);
148 m_spi_init_done =
true;
154 return m_spi_init_done;
164 nrf_drv_spi_uninit (&spi);
165 m_spi_init_done =
false;
170 const size_t tx_len, uint8_t * rx,
const size_t rx_len)
175 if ( (NULL == tx && 0 != tx_len) || (NULL == rx && 0 != rx_len)) {
return RD_ERROR_NULL; }
177 ret_code_t err_code = NRF_SUCCESS;
178 err_code |= nrf_drv_spi_transfer (&spi, tx, tx_len, rx, rx_len);
#define RD_ERROR_INVALID_PARAM
Invalid Parameter.
#define RD_ERROR_NULL
Null Pointer.
uint32_t rd_status_t
bitfield for representing errors
rd_status_t ruuvi_nrf5_sdk15_to_ruuvi_error(const ret_code_t error)
convert nrf5 sdk15 error code into Ruuvi error code.
#define RD_ERROR_CHECK(error, mask)
Shorthand macro for calling the rd_error_check with current file & line.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
rd_status_t ri_gpio_configure(const ri_gpio_id_t pin, const ri_gpio_mode_t mode)
Configure a pin of a port into a mode. If there are several ports the platform driver must implement ...
rd_status_t ri_gpio_write(const ri_gpio_id_t pin, const ri_gpio_state_t state)
Write a pin of a port into given state If there are several ports the platform driver must implement ...
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
@ RI_GPIO_MODE_OUTPUT_STANDARD
Push-pull output, can be written.
@ RI_GPIO_HIGH
GPIO electrically high.
Interface for SPI operations.
rd_status_t ri_spi_uninit()
Uninitialize SPI driver.
@ RI_SPI_MODE_1
CPOL = 0, CPHA = 1.
@ RI_SPI_MODE_0
CPOL = 0, CPHA = 0.
@ RI_SPI_MODE_3
CPOL = 1, CPHA = 1.
@ RI_SPI_MODE_2
CPOL = 1, CPHA = 0.
@ RI_SPI_FREQUENCY_4M
4 Mbps
@ RI_SPI_FREQUENCY_2M
2 Mbps
@ RI_SPI_FREQUENCY_1M
1 Mbps
@ RI_SPI_FREQUENCY_8M
8 Mbps
rd_status_t ri_spi_xfer_blocking(const uint8_t *const p_tx, const size_t tx_len, uint8_t *const p_rx, const size_t rx_len)
SPI transfer function.
bool ri_spi_is_init()
check if SPI interface is already initialized.
rd_status_t ri_spi_init(const ri_spi_init_config_t *const config)
Initialize SPI driver with given settings.
#define SPI_DEFAULT_CONFIG_IRQ_PRIORITY
ri_gpio_id_t * ss_pins
array of SPI pins, can be freed after function exits
ri_gpio_id_t sclk
pin number of SCLK
size_t ss_pins_number
sizeof ss_pins
ri_gpio_id_t miso
pin number of MISO
ri_spi_mode_t mode
Mode of SPI Bus, see ri_spi_mode_t.
ri_spi_frequency_t frequency
Frequency of SPI Bus, see ri_spi_frequency_t.
ri_gpio_id_t mosi
pin number of MOSI