1*150812a8SEvalZero /*
2*150812a8SEvalZero * Copyright (c) 2013 - 2018, Nordic Semiconductor ASA
3*150812a8SEvalZero * All rights reserved.
4*150812a8SEvalZero *
5*150812a8SEvalZero * Redistribution and use in source and binary forms, with or without
6*150812a8SEvalZero * modification, are permitted provided that the following conditions are met:
7*150812a8SEvalZero *
8*150812a8SEvalZero * 1. Redistributions of source code must retain the above copyright notice, this
9*150812a8SEvalZero * list of conditions and the following disclaimer.
10*150812a8SEvalZero *
11*150812a8SEvalZero * 2. Redistributions in binary form must reproduce the above copyright
12*150812a8SEvalZero * notice, this list of conditions and the following disclaimer in the
13*150812a8SEvalZero * documentation and/or other materials provided with the distribution.
14*150812a8SEvalZero *
15*150812a8SEvalZero * 3. Neither the name of the copyright holder nor the names of its
16*150812a8SEvalZero * contributors may be used to endorse or promote products derived from this
17*150812a8SEvalZero * software without specific prior written permission.
18*150812a8SEvalZero *
19*150812a8SEvalZero * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20*150812a8SEvalZero * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*150812a8SEvalZero * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*150812a8SEvalZero * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23*150812a8SEvalZero * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*150812a8SEvalZero * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*150812a8SEvalZero * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*150812a8SEvalZero * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*150812a8SEvalZero * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*150812a8SEvalZero * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*150812a8SEvalZero * POSSIBILITY OF SUCH DAMAGE.
30*150812a8SEvalZero */
31*150812a8SEvalZero
32*150812a8SEvalZero #include <nrfx.h>
33*150812a8SEvalZero
34*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS_ENABLED)
35*150812a8SEvalZero
36*150812a8SEvalZero #if !(NRFX_CHECK(NRFX_SPIS0_ENABLED) || \
37*150812a8SEvalZero NRFX_CHECK(NRFX_SPIS1_ENABLED) || \
38*150812a8SEvalZero NRFX_CHECK(NRFX_SPIS2_ENABLED) || \
39*150812a8SEvalZero NRFX_CHECK(NRFX_SPIS3_ENABLED))
40*150812a8SEvalZero #error "No enabled SPIS instances. Check <nrfx_config.h>."
41*150812a8SEvalZero #endif
42*150812a8SEvalZero
43*150812a8SEvalZero #include <nrfx_spis.h>
44*150812a8SEvalZero #include "prs/nrfx_prs.h"
45*150812a8SEvalZero
46*150812a8SEvalZero #define NRFX_LOG_MODULE SPIS
47*150812a8SEvalZero #include <nrfx_log.h>
48*150812a8SEvalZero
49*150812a8SEvalZero #define EVT_TO_STR(event) \
50*150812a8SEvalZero (event == NRF_SPIS_EVENT_ACQUIRED ? "NRF_SPIS_EVENT_ACQUIRED" : \
51*150812a8SEvalZero (event == NRF_SPIS_EVENT_END ? "NRF_SPIS_EVENT_END" : \
52*150812a8SEvalZero "UNKNOWN ERROR"))
53*150812a8SEvalZero
54*150812a8SEvalZero #define SPISX_LENGTH_VALIDATE(peripheral, drv_inst_idx, rx_len, tx_len) \
55*150812a8SEvalZero (((drv_inst_idx) == NRFX_CONCAT_3(NRFX_, peripheral, _INST_IDX)) && \
56*150812a8SEvalZero NRFX_EASYDMA_LENGTH_VALIDATE(peripheral, rx_len, tx_len))
57*150812a8SEvalZero
58*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS0_ENABLED)
59*150812a8SEvalZero #define SPIS0_LENGTH_VALIDATE(...) SPISX_LENGTH_VALIDATE(SPIS0, __VA_ARGS__)
60*150812a8SEvalZero #else
61*150812a8SEvalZero #define SPIS0_LENGTH_VALIDATE(...) 0
62*150812a8SEvalZero #endif
63*150812a8SEvalZero
64*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS1_ENABLED)
65*150812a8SEvalZero #define SPIS1_LENGTH_VALIDATE(...) SPISX_LENGTH_VALIDATE(SPIS1, __VA_ARGS__)
66*150812a8SEvalZero #else
67*150812a8SEvalZero #define SPIS1_LENGTH_VALIDATE(...) 0
68*150812a8SEvalZero #endif
69*150812a8SEvalZero
70*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS2_ENABLED)
71*150812a8SEvalZero #define SPIS2_LENGTH_VALIDATE(...) SPISX_LENGTH_VALIDATE(SPIS2, __VA_ARGS__)
72*150812a8SEvalZero #else
73*150812a8SEvalZero #define SPIS2_LENGTH_VALIDATE(...) 0
74*150812a8SEvalZero #endif
75*150812a8SEvalZero
76*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS3_ENABLED)
77*150812a8SEvalZero #define SPIS3_LENGTH_VALIDATE(...) SPISX_LENGTH_VALIDATE(SPIS3, __VA_ARGS__)
78*150812a8SEvalZero #else
79*150812a8SEvalZero #define SPIS3_LENGTH_VALIDATE(...) 0
80*150812a8SEvalZero #endif
81*150812a8SEvalZero
82*150812a8SEvalZero #define SPIS_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len) \
83*150812a8SEvalZero (SPIS0_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len) || \
84*150812a8SEvalZero SPIS1_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len) || \
85*150812a8SEvalZero SPIS2_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len) || \
86*150812a8SEvalZero SPIS3_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len))
87*150812a8SEvalZero
88*150812a8SEvalZero
89*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
90*150812a8SEvalZero #include <nrfx_gpiote.h>
91*150812a8SEvalZero #define USE_DMA_ISSUE_WORKAROUND
92*150812a8SEvalZero // This handler is called by the GPIOTE driver when a falling edge is detected
93*150812a8SEvalZero // on the CSN line. There is no need to do anything here. The handling of the
94*150812a8SEvalZero // interrupt itself provides a protection for DMA transfers.
csn_event_handler(nrfx_gpiote_pin_t pin,nrf_gpiote_polarity_t action)95*150812a8SEvalZero static void csn_event_handler(nrfx_gpiote_pin_t pin,
96*150812a8SEvalZero nrf_gpiote_polarity_t action)
97*150812a8SEvalZero {
98*150812a8SEvalZero }
99*150812a8SEvalZero #endif
100*150812a8SEvalZero
101*150812a8SEvalZero
102*150812a8SEvalZero /**@brief States of the SPI transaction state machine. */
103*150812a8SEvalZero typedef enum
104*150812a8SEvalZero {
105*150812a8SEvalZero SPIS_STATE_INIT, /**< Initialization state. In this state the module waits for a call to @ref spi_slave_buffers_set. */
106*150812a8SEvalZero SPIS_BUFFER_RESOURCE_REQUESTED, /**< State where the configuration of the memory buffers, which are to be used in SPI transaction, has started. */
107*150812a8SEvalZero SPIS_BUFFER_RESOURCE_CONFIGURED, /**< State where the configuration of the memory buffers, which are to be used in SPI transaction, has completed. */
108*150812a8SEvalZero SPIS_XFER_COMPLETED /**< State where SPI transaction has been completed. */
109*150812a8SEvalZero } nrfx_spis_state_t;
110*150812a8SEvalZero
111*150812a8SEvalZero /**@brief SPIS control block - driver instance local data. */
112*150812a8SEvalZero typedef struct
113*150812a8SEvalZero {
114*150812a8SEvalZero volatile uint32_t tx_buffer_size; //!< SPI slave TX buffer size in bytes.
115*150812a8SEvalZero volatile uint32_t rx_buffer_size; //!< SPI slave RX buffer size in bytes.
116*150812a8SEvalZero nrfx_spis_event_handler_t handler; //!< SPI event handler.
117*150812a8SEvalZero volatile const uint8_t * tx_buffer; //!< SPI slave TX buffer.
118*150812a8SEvalZero volatile uint8_t * rx_buffer; //!< SPI slave RX buffer.
119*150812a8SEvalZero nrfx_drv_state_t state; //!< driver initialization state.
120*150812a8SEvalZero volatile nrfx_spis_state_t spi_state; //!< SPI slave state.
121*150812a8SEvalZero void * p_context; //!< Context set on initialization.
122*150812a8SEvalZero } spis_cb_t;
123*150812a8SEvalZero
124*150812a8SEvalZero static spis_cb_t m_cb[NRFX_SPIS_ENABLED_COUNT];
125*150812a8SEvalZero
nrfx_spis_init(nrfx_spis_t const * const p_instance,nrfx_spis_config_t const * p_config,nrfx_spis_event_handler_t event_handler,void * p_context)126*150812a8SEvalZero nrfx_err_t nrfx_spis_init(nrfx_spis_t const * const p_instance,
127*150812a8SEvalZero nrfx_spis_config_t const * p_config,
128*150812a8SEvalZero nrfx_spis_event_handler_t event_handler,
129*150812a8SEvalZero void * p_context)
130*150812a8SEvalZero {
131*150812a8SEvalZero NRFX_ASSERT(p_config);
132*150812a8SEvalZero NRFX_ASSERT(event_handler);
133*150812a8SEvalZero spis_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
134*150812a8SEvalZero nrfx_err_t err_code;
135*150812a8SEvalZero
136*150812a8SEvalZero NRF_SPIS_Type * p_spis = p_instance->p_reg;
137*150812a8SEvalZero
138*150812a8SEvalZero if (p_cb->state != NRFX_DRV_STATE_UNINITIALIZED)
139*150812a8SEvalZero {
140*150812a8SEvalZero err_code = NRFX_ERROR_INVALID_STATE;
141*150812a8SEvalZero NRFX_LOG_WARNING("Function: %s, error code: %s.",
142*150812a8SEvalZero __func__,
143*150812a8SEvalZero NRFX_LOG_ERROR_STRING_GET(err_code));
144*150812a8SEvalZero return err_code;
145*150812a8SEvalZero }
146*150812a8SEvalZero
147*150812a8SEvalZero if ((uint32_t)p_config->mode > (uint32_t)NRF_SPIS_MODE_3)
148*150812a8SEvalZero {
149*150812a8SEvalZero err_code = NRFX_ERROR_INVALID_PARAM;
150*150812a8SEvalZero NRFX_LOG_WARNING("Function: %s, error code: %s.",
151*150812a8SEvalZero __func__,
152*150812a8SEvalZero NRFX_LOG_ERROR_STRING_GET(err_code));
153*150812a8SEvalZero return err_code;
154*150812a8SEvalZero }
155*150812a8SEvalZero #if NRFX_CHECK(NRFX_PRS_ENABLED)
156*150812a8SEvalZero static nrfx_irq_handler_t const irq_handlers[NRFX_SPIS_ENABLED_COUNT] = {
157*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS0_ENABLED)
158*150812a8SEvalZero nrfx_spis_0_irq_handler,
159*150812a8SEvalZero #endif
160*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS1_ENABLED)
161*150812a8SEvalZero nrfx_spis_1_irq_handler,
162*150812a8SEvalZero #endif
163*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS2_ENABLED)
164*150812a8SEvalZero nrfx_spis_2_irq_handler,
165*150812a8SEvalZero #endif
166*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS3_ENABLED)
167*150812a8SEvalZero nrfx_spis_3_irq_handler,
168*150812a8SEvalZero #endif
169*150812a8SEvalZero };
170*150812a8SEvalZero if (nrfx_prs_acquire(p_spis,
171*150812a8SEvalZero irq_handlers[p_instance->drv_inst_idx]) != NRFX_SUCCESS)
172*150812a8SEvalZero {
173*150812a8SEvalZero err_code = NRFX_ERROR_BUSY;
174*150812a8SEvalZero NRFX_LOG_WARNING("Function: %s, error code: %s.",
175*150812a8SEvalZero __func__,
176*150812a8SEvalZero NRFX_LOG_ERROR_STRING_GET(err_code));
177*150812a8SEvalZero return err_code;
178*150812a8SEvalZero }
179*150812a8SEvalZero #endif // NRFX_CHECK(NRFX_PRS_ENABLED)
180*150812a8SEvalZero
181*150812a8SEvalZero // Configure the SPI pins for input.
182*150812a8SEvalZero uint32_t mosi_pin;
183*150812a8SEvalZero uint32_t miso_pin;
184*150812a8SEvalZero
185*150812a8SEvalZero if (p_config->miso_pin != NRFX_SPIS_PIN_NOT_USED)
186*150812a8SEvalZero {
187*150812a8SEvalZero nrf_gpio_cfg(p_config->miso_pin,
188*150812a8SEvalZero NRF_GPIO_PIN_DIR_INPUT,
189*150812a8SEvalZero NRF_GPIO_PIN_INPUT_CONNECT,
190*150812a8SEvalZero NRF_GPIO_PIN_NOPULL,
191*150812a8SEvalZero p_config->miso_drive,
192*150812a8SEvalZero NRF_GPIO_PIN_NOSENSE);
193*150812a8SEvalZero miso_pin = p_config->miso_pin;
194*150812a8SEvalZero }
195*150812a8SEvalZero else
196*150812a8SEvalZero {
197*150812a8SEvalZero miso_pin = NRF_SPIS_PIN_NOT_CONNECTED;
198*150812a8SEvalZero }
199*150812a8SEvalZero
200*150812a8SEvalZero if (p_config->mosi_pin != NRFX_SPIS_PIN_NOT_USED)
201*150812a8SEvalZero {
202*150812a8SEvalZero nrf_gpio_cfg(p_config->mosi_pin,
203*150812a8SEvalZero NRF_GPIO_PIN_DIR_INPUT,
204*150812a8SEvalZero NRF_GPIO_PIN_INPUT_CONNECT,
205*150812a8SEvalZero NRF_GPIO_PIN_NOPULL,
206*150812a8SEvalZero NRF_GPIO_PIN_S0S1,
207*150812a8SEvalZero NRF_GPIO_PIN_NOSENSE);
208*150812a8SEvalZero mosi_pin = p_config->mosi_pin;
209*150812a8SEvalZero }
210*150812a8SEvalZero else
211*150812a8SEvalZero {
212*150812a8SEvalZero mosi_pin = NRF_SPIS_PIN_NOT_CONNECTED;
213*150812a8SEvalZero }
214*150812a8SEvalZero
215*150812a8SEvalZero nrf_gpio_cfg(p_config->csn_pin,
216*150812a8SEvalZero NRF_GPIO_PIN_DIR_INPUT,
217*150812a8SEvalZero NRF_GPIO_PIN_INPUT_CONNECT,
218*150812a8SEvalZero p_config->csn_pullup,
219*150812a8SEvalZero NRF_GPIO_PIN_S0S1,
220*150812a8SEvalZero NRF_GPIO_PIN_NOSENSE);
221*150812a8SEvalZero
222*150812a8SEvalZero nrf_gpio_cfg(p_config->sck_pin,
223*150812a8SEvalZero NRF_GPIO_PIN_DIR_INPUT,
224*150812a8SEvalZero NRF_GPIO_PIN_INPUT_CONNECT,
225*150812a8SEvalZero NRF_GPIO_PIN_NOPULL,
226*150812a8SEvalZero NRF_GPIO_PIN_S0S1,
227*150812a8SEvalZero NRF_GPIO_PIN_NOSENSE);
228*150812a8SEvalZero
229*150812a8SEvalZero nrf_spis_pins_set(p_spis, p_config->sck_pin, mosi_pin, miso_pin, p_config->csn_pin);
230*150812a8SEvalZero
231*150812a8SEvalZero nrf_spis_rx_buffer_set(p_spis, NULL, 0);
232*150812a8SEvalZero nrf_spis_tx_buffer_set(p_spis, NULL, 0);
233*150812a8SEvalZero
234*150812a8SEvalZero // Configure SPI mode.
235*150812a8SEvalZero nrf_spis_configure(p_spis, p_config->mode, p_config->bit_order);
236*150812a8SEvalZero
237*150812a8SEvalZero // Configure DEF and ORC characters.
238*150812a8SEvalZero nrf_spis_def_set(p_spis, p_config->def);
239*150812a8SEvalZero nrf_spis_orc_set(p_spis, p_config->orc);
240*150812a8SEvalZero
241*150812a8SEvalZero // Clear possible pending events.
242*150812a8SEvalZero nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_END);
243*150812a8SEvalZero nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_ACQUIRED);
244*150812a8SEvalZero
245*150812a8SEvalZero // Enable END_ACQUIRE shortcut.
246*150812a8SEvalZero nrf_spis_shorts_enable(p_spis, NRF_SPIS_SHORT_END_ACQUIRE);
247*150812a8SEvalZero
248*150812a8SEvalZero p_cb->spi_state = SPIS_STATE_INIT;
249*150812a8SEvalZero p_cb->handler = event_handler;
250*150812a8SEvalZero p_cb->p_context = p_context;
251*150812a8SEvalZero
252*150812a8SEvalZero #if defined(USE_DMA_ISSUE_WORKAROUND)
253*150812a8SEvalZero // Configure a GPIOTE channel to generate interrupts on each falling edge
254*150812a8SEvalZero // on the CSN line. Handling of these interrupts will make the CPU active,
255*150812a8SEvalZero // and thus will protect the DMA transfers started by SPIS right after it
256*150812a8SEvalZero // is selected for communication.
257*150812a8SEvalZero // [the GPIOTE driver may be already initialized at this point (by this
258*150812a8SEvalZero // driver when another SPIS instance is used, or by an application code),
259*150812a8SEvalZero // so just ignore the returned value]
260*150812a8SEvalZero (void)nrfx_gpiote_init();
261*150812a8SEvalZero static nrfx_gpiote_in_config_t const csn_gpiote_config =
262*150812a8SEvalZero NRFX_GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
263*150812a8SEvalZero nrfx_err_t gpiote_err_code = nrfx_gpiote_in_init(p_config->csn_pin,
264*150812a8SEvalZero &csn_gpiote_config, csn_event_handler);
265*150812a8SEvalZero if (gpiote_err_code != NRFX_SUCCESS)
266*150812a8SEvalZero {
267*150812a8SEvalZero err_code = NRFX_ERROR_INTERNAL;
268*150812a8SEvalZero NRFX_LOG_INFO("Function: %s, error code: %s.",
269*150812a8SEvalZero __func__,
270*150812a8SEvalZero NRFX_LOG_ERROR_STRING_GET(err_code));
271*150812a8SEvalZero return err_code;
272*150812a8SEvalZero }
273*150812a8SEvalZero nrfx_gpiote_in_event_enable(p_config->csn_pin, true);
274*150812a8SEvalZero #endif
275*150812a8SEvalZero
276*150812a8SEvalZero // Enable IRQ.
277*150812a8SEvalZero nrf_spis_int_enable(p_spis, NRF_SPIS_INT_ACQUIRED_MASK |
278*150812a8SEvalZero NRF_SPIS_INT_END_MASK);
279*150812a8SEvalZero NRFX_IRQ_PRIORITY_SET(nrfx_get_irq_number(p_instance->p_reg),
280*150812a8SEvalZero p_config->irq_priority);
281*150812a8SEvalZero NRFX_IRQ_ENABLE(nrfx_get_irq_number(p_instance->p_reg));
282*150812a8SEvalZero
283*150812a8SEvalZero p_cb->state = NRFX_DRV_STATE_INITIALIZED;
284*150812a8SEvalZero
285*150812a8SEvalZero // Enable SPI slave device.
286*150812a8SEvalZero nrf_spis_enable(p_spis);
287*150812a8SEvalZero
288*150812a8SEvalZero NRFX_LOG_INFO("Initialized.");
289*150812a8SEvalZero return NRFX_SUCCESS;
290*150812a8SEvalZero }
291*150812a8SEvalZero
292*150812a8SEvalZero
nrfx_spis_uninit(nrfx_spis_t const * const p_instance)293*150812a8SEvalZero void nrfx_spis_uninit(nrfx_spis_t const * const p_instance)
294*150812a8SEvalZero {
295*150812a8SEvalZero spis_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
296*150812a8SEvalZero NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
297*150812a8SEvalZero
298*150812a8SEvalZero NRF_SPIS_Type * p_spis = p_instance->p_reg;
299*150812a8SEvalZero
300*150812a8SEvalZero #define DISABLE_ALL 0xFFFFFFFF
301*150812a8SEvalZero nrf_spis_disable(p_spis);
302*150812a8SEvalZero NRFX_IRQ_DISABLE(nrfx_get_irq_number(p_instance->p_reg));
303*150812a8SEvalZero nrf_spis_int_disable(p_spis, DISABLE_ALL);
304*150812a8SEvalZero #undef DISABLE_ALL
305*150812a8SEvalZero
306*150812a8SEvalZero #if NRFX_CHECK(NRFX_PRS_ENABLED)
307*150812a8SEvalZero nrfx_prs_release(p_spis);
308*150812a8SEvalZero #endif
309*150812a8SEvalZero
310*150812a8SEvalZero p_cb->state = NRFX_DRV_STATE_UNINITIALIZED;
311*150812a8SEvalZero NRFX_LOG_INFO("Uninitialized.");
312*150812a8SEvalZero }
313*150812a8SEvalZero
314*150812a8SEvalZero
315*150812a8SEvalZero /**@brief Function for executing the state entry action. */
spis_state_entry_action_execute(NRF_SPIS_Type * p_spis,spis_cb_t * p_cb)316*150812a8SEvalZero static void spis_state_entry_action_execute(NRF_SPIS_Type * p_spis,
317*150812a8SEvalZero spis_cb_t * p_cb)
318*150812a8SEvalZero {
319*150812a8SEvalZero nrfx_spis_evt_t event;
320*150812a8SEvalZero
321*150812a8SEvalZero switch (p_cb->spi_state)
322*150812a8SEvalZero {
323*150812a8SEvalZero case SPIS_BUFFER_RESOURCE_REQUESTED:
324*150812a8SEvalZero nrf_spis_task_trigger(p_spis, NRF_SPIS_TASK_ACQUIRE);
325*150812a8SEvalZero break;
326*150812a8SEvalZero
327*150812a8SEvalZero case SPIS_BUFFER_RESOURCE_CONFIGURED:
328*150812a8SEvalZero event.evt_type = NRFX_SPIS_BUFFERS_SET_DONE;
329*150812a8SEvalZero event.rx_amount = 0;
330*150812a8SEvalZero event.tx_amount = 0;
331*150812a8SEvalZero
332*150812a8SEvalZero NRFX_ASSERT(p_cb->handler != NULL);
333*150812a8SEvalZero p_cb->handler(&event, p_cb->p_context);
334*150812a8SEvalZero break;
335*150812a8SEvalZero
336*150812a8SEvalZero case SPIS_XFER_COMPLETED:
337*150812a8SEvalZero event.evt_type = NRFX_SPIS_XFER_DONE;
338*150812a8SEvalZero event.rx_amount = nrf_spis_rx_amount_get(p_spis);
339*150812a8SEvalZero event.tx_amount = nrf_spis_tx_amount_get(p_spis);
340*150812a8SEvalZero NRFX_LOG_INFO("Transfer rx_len:%d.", event.rx_amount);
341*150812a8SEvalZero NRFX_LOG_DEBUG("Rx data:");
342*150812a8SEvalZero NRFX_LOG_HEXDUMP_DEBUG((uint8_t const *)p_cb->rx_buffer,
343*150812a8SEvalZero event.rx_amount * sizeof(p_cb->rx_buffer[0]));
344*150812a8SEvalZero NRFX_ASSERT(p_cb->handler != NULL);
345*150812a8SEvalZero p_cb->handler(&event, p_cb->p_context);
346*150812a8SEvalZero break;
347*150812a8SEvalZero
348*150812a8SEvalZero default:
349*150812a8SEvalZero // No implementation required.
350*150812a8SEvalZero break;
351*150812a8SEvalZero }
352*150812a8SEvalZero }
353*150812a8SEvalZero
354*150812a8SEvalZero /**@brief Function for changing the state of the SPI state machine.
355*150812a8SEvalZero *
356*150812a8SEvalZero * @param[in] p_spis SPIS instance register.
357*150812a8SEvalZero * @param[in] p_cb SPIS instance control block.
358*150812a8SEvalZero * @param[in] new_state State where the state machine transits to.
359*150812a8SEvalZero */
spis_state_change(NRF_SPIS_Type * p_spis,spis_cb_t * p_cb,nrfx_spis_state_t new_state)360*150812a8SEvalZero static void spis_state_change(NRF_SPIS_Type * p_spis,
361*150812a8SEvalZero spis_cb_t * p_cb,
362*150812a8SEvalZero nrfx_spis_state_t new_state)
363*150812a8SEvalZero {
364*150812a8SEvalZero p_cb->spi_state = new_state;
365*150812a8SEvalZero spis_state_entry_action_execute(p_spis, p_cb);
366*150812a8SEvalZero }
367*150812a8SEvalZero
nrfx_spis_buffers_set(nrfx_spis_t const * const p_instance,uint8_t const * p_tx_buffer,size_t tx_buffer_length,uint8_t * p_rx_buffer,size_t rx_buffer_length)368*150812a8SEvalZero nrfx_err_t nrfx_spis_buffers_set(nrfx_spis_t const * const p_instance,
369*150812a8SEvalZero uint8_t const * p_tx_buffer,
370*150812a8SEvalZero size_t tx_buffer_length,
371*150812a8SEvalZero uint8_t * p_rx_buffer,
372*150812a8SEvalZero size_t rx_buffer_length)
373*150812a8SEvalZero {
374*150812a8SEvalZero NRFX_ASSERT(p_tx_buffer != NULL || tx_buffer_length == 0);
375*150812a8SEvalZero NRFX_ASSERT(p_rx_buffer != NULL || rx_buffer_length == 0);
376*150812a8SEvalZero
377*150812a8SEvalZero spis_cb_t * p_cb = &m_cb[p_instance->drv_inst_idx];
378*150812a8SEvalZero nrfx_err_t err_code;
379*150812a8SEvalZero
380*150812a8SEvalZero if (!SPIS_LENGTH_VALIDATE(p_instance->drv_inst_idx,
381*150812a8SEvalZero rx_buffer_length,
382*150812a8SEvalZero tx_buffer_length))
383*150812a8SEvalZero {
384*150812a8SEvalZero return NRFX_ERROR_INVALID_LENGTH;
385*150812a8SEvalZero }
386*150812a8SEvalZero
387*150812a8SEvalZero // EasyDMA requires that transfer buffers are placed in Data RAM region;
388*150812a8SEvalZero // signal error if they are not.
389*150812a8SEvalZero if ((p_tx_buffer != NULL && !nrfx_is_in_ram(p_tx_buffer)) ||
390*150812a8SEvalZero (p_rx_buffer != NULL && !nrfx_is_in_ram(p_rx_buffer)))
391*150812a8SEvalZero {
392*150812a8SEvalZero err_code = NRFX_ERROR_INVALID_ADDR;
393*150812a8SEvalZero NRFX_LOG_WARNING("Function: %s, error code: %s.",
394*150812a8SEvalZero __func__,
395*150812a8SEvalZero NRFX_LOG_ERROR_STRING_GET(err_code));
396*150812a8SEvalZero return err_code;
397*150812a8SEvalZero }
398*150812a8SEvalZero
399*150812a8SEvalZero switch (p_cb->spi_state)
400*150812a8SEvalZero {
401*150812a8SEvalZero case SPIS_STATE_INIT:
402*150812a8SEvalZero case SPIS_XFER_COMPLETED:
403*150812a8SEvalZero case SPIS_BUFFER_RESOURCE_CONFIGURED:
404*150812a8SEvalZero p_cb->tx_buffer = p_tx_buffer;
405*150812a8SEvalZero p_cb->rx_buffer = p_rx_buffer;
406*150812a8SEvalZero p_cb->tx_buffer_size = tx_buffer_length;
407*150812a8SEvalZero p_cb->rx_buffer_size = rx_buffer_length;
408*150812a8SEvalZero err_code = NRFX_SUCCESS;
409*150812a8SEvalZero
410*150812a8SEvalZero spis_state_change(p_instance->p_reg, p_cb, SPIS_BUFFER_RESOURCE_REQUESTED);
411*150812a8SEvalZero break;
412*150812a8SEvalZero
413*150812a8SEvalZero case SPIS_BUFFER_RESOURCE_REQUESTED:
414*150812a8SEvalZero err_code = NRFX_ERROR_INVALID_STATE;
415*150812a8SEvalZero break;
416*150812a8SEvalZero
417*150812a8SEvalZero default:
418*150812a8SEvalZero // @note: execution of this code path would imply internal error in the design.
419*150812a8SEvalZero err_code = NRFX_ERROR_INTERNAL;
420*150812a8SEvalZero break;
421*150812a8SEvalZero }
422*150812a8SEvalZero
423*150812a8SEvalZero NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
424*150812a8SEvalZero return err_code;
425*150812a8SEvalZero }
426*150812a8SEvalZero
spis_irq_handler(NRF_SPIS_Type * p_spis,spis_cb_t * p_cb)427*150812a8SEvalZero static void spis_irq_handler(NRF_SPIS_Type * p_spis, spis_cb_t * p_cb)
428*150812a8SEvalZero {
429*150812a8SEvalZero // @note: as multiple events can be pending for processing, the correct event processing order
430*150812a8SEvalZero // is as follows:
431*150812a8SEvalZero // - SPI semaphore acquired event.
432*150812a8SEvalZero // - SPI transaction complete event.
433*150812a8SEvalZero
434*150812a8SEvalZero // Check for SPI semaphore acquired event.
435*150812a8SEvalZero if (nrf_spis_event_check(p_spis, NRF_SPIS_EVENT_ACQUIRED))
436*150812a8SEvalZero {
437*150812a8SEvalZero nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_ACQUIRED);
438*150812a8SEvalZero NRFX_LOG_DEBUG("SPIS: Event: %s.", EVT_TO_STR(NRF_SPIS_EVENT_ACQUIRED));
439*150812a8SEvalZero
440*150812a8SEvalZero switch (p_cb->spi_state)
441*150812a8SEvalZero {
442*150812a8SEvalZero case SPIS_BUFFER_RESOURCE_REQUESTED:
443*150812a8SEvalZero nrf_spis_tx_buffer_set(p_spis, (uint8_t *)p_cb->tx_buffer, p_cb->tx_buffer_size);
444*150812a8SEvalZero nrf_spis_rx_buffer_set(p_spis, (uint8_t *)p_cb->rx_buffer, p_cb->rx_buffer_size);
445*150812a8SEvalZero
446*150812a8SEvalZero nrf_spis_task_trigger(p_spis, NRF_SPIS_TASK_RELEASE);
447*150812a8SEvalZero
448*150812a8SEvalZero spis_state_change(p_spis, p_cb, SPIS_BUFFER_RESOURCE_CONFIGURED);
449*150812a8SEvalZero break;
450*150812a8SEvalZero
451*150812a8SEvalZero default:
452*150812a8SEvalZero // No implementation required.
453*150812a8SEvalZero break;
454*150812a8SEvalZero }
455*150812a8SEvalZero }
456*150812a8SEvalZero
457*150812a8SEvalZero // Check for SPI transaction complete event.
458*150812a8SEvalZero if (nrf_spis_event_check(p_spis, NRF_SPIS_EVENT_END))
459*150812a8SEvalZero {
460*150812a8SEvalZero nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_END);
461*150812a8SEvalZero NRFX_LOG_DEBUG("SPIS: Event: %s.", EVT_TO_STR(NRF_SPIS_EVENT_END));
462*150812a8SEvalZero
463*150812a8SEvalZero switch (p_cb->spi_state)
464*150812a8SEvalZero {
465*150812a8SEvalZero case SPIS_BUFFER_RESOURCE_CONFIGURED:
466*150812a8SEvalZero spis_state_change(p_spis, p_cb, SPIS_XFER_COMPLETED);
467*150812a8SEvalZero break;
468*150812a8SEvalZero
469*150812a8SEvalZero default:
470*150812a8SEvalZero // No implementation required.
471*150812a8SEvalZero break;
472*150812a8SEvalZero }
473*150812a8SEvalZero }
474*150812a8SEvalZero }
475*150812a8SEvalZero
476*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS0_ENABLED)
nrfx_spis_0_irq_handler(void)477*150812a8SEvalZero void nrfx_spis_0_irq_handler(void)
478*150812a8SEvalZero {
479*150812a8SEvalZero spis_irq_handler(NRF_SPIS0, &m_cb[NRFX_SPIS0_INST_IDX]);
480*150812a8SEvalZero }
481*150812a8SEvalZero #endif
482*150812a8SEvalZero
483*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS1_ENABLED)
nrfx_spis_1_irq_handler(void)484*150812a8SEvalZero void nrfx_spis_1_irq_handler(void)
485*150812a8SEvalZero {
486*150812a8SEvalZero spis_irq_handler(NRF_SPIS1, &m_cb[NRFX_SPIS1_INST_IDX]);
487*150812a8SEvalZero }
488*150812a8SEvalZero #endif
489*150812a8SEvalZero
490*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS2_ENABLED)
nrfx_spis_2_irq_handler(void)491*150812a8SEvalZero void nrfx_spis_2_irq_handler(void)
492*150812a8SEvalZero {
493*150812a8SEvalZero spis_irq_handler(NRF_SPIS2, &m_cb[NRFX_SPIS2_INST_IDX]);
494*150812a8SEvalZero }
495*150812a8SEvalZero #endif
496*150812a8SEvalZero
497*150812a8SEvalZero #if NRFX_CHECK(NRFX_SPIS3_ENABLED)
nrfx_spis_3_irq_handler(void)498*150812a8SEvalZero void nrfx_spis_3_irq_handler(void)
499*150812a8SEvalZero {
500*150812a8SEvalZero spis_irq_handler(NRF_SPIS3, &m_cb[NRFX_SPIS3_INST_IDX]);
501*150812a8SEvalZero }
502*150812a8SEvalZero #endif
503*150812a8SEvalZero
504*150812a8SEvalZero #endif // NRFX_CHECK(NRFX_SPIS_ENABLED)
505