-
Notifications
You must be signed in to change notification settings - Fork 17
/
spi_module.c
105 lines (89 loc) · 3.07 KB
/
spi_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "nrf.h"
#include "nrf_gpio.h"
#include "app_error.h"
#include "app_util_platform.h"
#include "nrf_delay.h"
#include "nrf_drv_spi.h"
#include "nordic_common.h"
#include "bsp.h"
#include "spi_module.h"
#define TX_RX_BUF_LENGTH 16u /**< SPI transaction buffer length. */
#if (SPI0_ENABLED == 1)
static const nrf_drv_spi_t m_spi_master = NRF_DRV_SPI_INSTANCE(0);
#elif (SPI1_ENABLED == 1)
static const nrf_drv_spi_t m_spi_master = NRF_DRV_SPI_INSTANCE(1);
#elif (SPI2_ENABLED == 1)
static const nrf_drv_spi_t m_spi_master = NRF_DRV_SPI_INSTANCE(2);
#else
#error "No SPI enabled."
#endif
// Data buffers.
static uint8_t m_tx_data[TX_RX_BUF_LENGTH] = {0}; /**< A buffer with data to transfer. */
static uint8_t m_rx_data[TX_RX_BUF_LENGTH] = {0}; /**< A buffer for incoming data. */
static volatile bool m_transfer_completed = true; /**< A flag to inform about completed transfer. */
/**@brief Function for SPI master event callback.
*
* Upon receiving an SPI transaction complete event, checks if received data are valid.
*
* @param[in] spi_master_evt SPI master driver event.
*/
static void spi_master_event_handler(nrf_drv_spi_event_t event)
{
switch (event) {
case NRF_DRV_SPI_EVENT_DONE:
// Inform application that transfer is completed.
m_transfer_completed = true;
break;
default:
// No implementation needed.
break;
}
}
void spi_init(uint32_t clk, uint32_t mosi)
{
nrf_drv_spi_config_t const config = {
.sck_pin = clk,
.mosi_pin = mosi,
.miso_pin = NRF_DRV_SPI_PIN_NOT_USED, // SPI0_CONFIG_MISO_PIN,
.ss_pin = NRF_DRV_SPI_PIN_NOT_USED, // SPI0_CONFIG_CS_PIN,
.irq_priority = APP_IRQ_PRIORITY_LOW,
.orc = 0xCC,
.frequency = NRF_DRV_SPI_FREQ_8M,
.mode = NRF_DRV_SPI_MODE_3,
.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,
};
ret_code_t err_code = nrf_drv_spi_init(&m_spi_master, &config, spi_master_event_handler);
APP_ERROR_CHECK(err_code);
}
/**@brief Functions prepares buffers and starts data transfer.
*
* @param[in] p_tx_data A pointer to a buffer TX.
* @param[in] p_rx_data A pointer to a buffer RX.
* @param[in] len A length of the data buffers.
*/
void spi_send_recv(uint8_t * const p_tx_data,
uint8_t * const p_rx_data,
const uint16_t len)
{
m_transfer_completed = false;
// Start transfer.
nrf_drv_spi_transfer(&m_spi_master,
p_tx_data, len, p_rx_data, len);
while (!m_transfer_completed);
}
uint8_t* spi_transfer(uint8_t * message, const uint16_t len)
{
memcpy((void*)m_tx_data, (void*)message, len);
spi_send_recv(m_tx_data, m_rx_data, len);
return m_rx_data;
}
#if 0
void spi_send(uint8_t * message, const uint16_t len)
{
memcpy((void*)m_tx_data, (void*)message, len);
spi_send_recv(m_tx_data, m_rx_data, 0);
}
#endif