nrfx_spim.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <nrfx.h>
  41. #if NRFX_CHECK(NRFX_SPIM_ENABLED)
  42. #if !(NRFX_CHECK(NRFX_SPIM0_ENABLED) || NRFX_CHECK(NRFX_SPIM1_ENABLED) || \
  43. NRFX_CHECK(NRFX_SPIM2_ENABLED) || NRFX_CHECK(NRFX_SPIM3_ENABLED))
  44. #error "No enabled SPIM instances. Check <nrfx_config.h>."
  45. #endif
  46. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED) && !NRFX_CHECK(NRFX_SPIM3_ENABLED)
  47. #error "Extended options are available only in SPIM3 on the nRF52840 SoC."
  48. #endif
  49. #include <nrfx_spim.h>
  50. #include "nrfx_prs.h"
  51. #include <hal/nrf_gpio.h>
  52. #define NRFX_LOG_MODULE SPIM
  53. #include <nrfx_log.h>
  54. #define SPIMX_LENGTH_VALIDATE(peripheral, drv_inst_idx, rx_len, tx_len) \
  55. (((drv_inst_idx) == NRFX_CONCAT_3(NRFX_, peripheral, _INST_IDX)) && \
  56. NRFX_EASYDMA_LENGTH_VALIDATE(peripheral, rx_len, tx_len))
  57. #if NRFX_CHECK(NRFX_SPIM0_ENABLED)
  58. #define SPIM0_LENGTH_VALIDATE(...) SPIMX_LENGTH_VALIDATE(SPIM0, __VA_ARGS__)
  59. #else
  60. #define SPIM0_LENGTH_VALIDATE(...) 0
  61. #endif
  62. #if NRFX_CHECK(NRFX_SPIM1_ENABLED)
  63. #define SPIM1_LENGTH_VALIDATE(...) SPIMX_LENGTH_VALIDATE(SPIM1, __VA_ARGS__)
  64. #else
  65. #define SPIM1_LENGTH_VALIDATE(...) 0
  66. #endif
  67. #if NRFX_CHECK(NRFX_SPIM2_ENABLED)
  68. #define SPIM2_LENGTH_VALIDATE(...) SPIMX_LENGTH_VALIDATE(SPIM2, __VA_ARGS__)
  69. #else
  70. #define SPIM2_LENGTH_VALIDATE(...) 0
  71. #endif
  72. #if NRFX_CHECK(NRFX_SPIM3_ENABLED)
  73. #define SPIM3_LENGTH_VALIDATE(...) SPIMX_LENGTH_VALIDATE(SPIM3, __VA_ARGS__)
  74. #else
  75. #define SPIM3_LENGTH_VALIDATE(...) 0
  76. #endif
  77. #define SPIM_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len) \
  78. (SPIM0_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len) || \
  79. SPIM1_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len) || \
  80. SPIM2_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len) || \
  81. SPIM3_LENGTH_VALIDATE(drv_inst_idx, rx_len, tx_len))
  82. #if defined(NRF52840_XXAA) && (NRFX_CHECK(NRFX_SPIM3_ENABLED))
  83. // Enable workaround for nRF52840 anomaly 195 (SPIM3 continues to draw current after disable).
  84. #define USE_WORKAROUND_FOR_ANOMALY_195
  85. #endif
  86. // Control block - driver instance local data.
  87. typedef struct
  88. {
  89. nrfx_spim_evt_handler_t handler;
  90. void * p_context;
  91. nrfx_spim_evt_t evt; // Keep the struct that is ready for event handler. Less memcpy.
  92. nrfx_drv_state_t state;
  93. volatile bool transfer_in_progress;
  94. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED)
  95. bool use_hw_ss;
  96. #endif
  97. // [no need for 'volatile' attribute for the following members, as they
  98. // are not concurrently used in IRQ handlers and main line code]
  99. bool ss_active_high;
  100. uint8_t ss_pin;
  101. uint8_t miso_pin;
  102. uint8_t orc;
  103. #if NRFX_CHECK(NRFX_SPIM_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
  104. size_t tx_length;
  105. size_t rx_length;
  106. #endif
  107. } spim_control_block_t;
  108. static spim_control_block_t m_cb[NRFX_SPIM_ENABLED_COUNT];
  109. #if NRFX_CHECK(NRFX_SPIM3_NRF52840_ANOMALY_198_WORKAROUND_ENABLED)
  110. // Workaround for nRF52840 anomaly 198: SPIM3 transmit data might be corrupted.
  111. static uint32_t m_anomaly_198_preserved_value;
  112. static void anomaly_198_enable(uint8_t const * p_buffer, size_t buf_len)
  113. {
  114. m_anomaly_198_preserved_value = *((volatile uint32_t *)0x40000E00);
  115. if (buf_len == 0)
  116. {
  117. return;
  118. }
  119. uint32_t buffer_end_addr = ((uint32_t)p_buffer) + buf_len;
  120. uint32_t block_addr = ((uint32_t)p_buffer) & ~0x1FFF;
  121. uint32_t block_flag = (1UL << ((block_addr >> 13) & 0xFFFF));
  122. uint32_t occupied_blocks = 0;
  123. if (block_addr >= 0x20010000)
  124. {
  125. occupied_blocks = (1UL << 8);
  126. }
  127. else
  128. {
  129. do {
  130. occupied_blocks |= block_flag;
  131. block_flag <<= 1;
  132. block_addr += 0x2000;
  133. } while ((block_addr < buffer_end_addr) && (block_addr < 0x20012000));
  134. }
  135. *((volatile uint32_t *)0x40000E00) = occupied_blocks;
  136. }
  137. static void anomaly_198_disable(void)
  138. {
  139. *((volatile uint32_t *)0x40000E00) = m_anomaly_198_preserved_value;
  140. }
  141. #endif // NRFX_CHECK(NRFX_SPIM3_NRF52840_ANOMALY_198_WORKAROUND_ENABLED)
  142. nrfx_err_t nrfx_spim_init(nrfx_spim_t const * const p_instance,
  143. nrfx_spim_config_t const * p_config,
  144. nrfx_spim_evt_handler_t handler,
  145. void * p_context)
  146. {
  147. NRFX_ASSERT(p_config);
  148. spim_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
  149. nrfx_err_t err_code;
  150. if (p_cb->state != NRFX_DRV_STATE_UNINITIALIZED)
  151. {
  152. err_code = NRFX_ERROR_INVALID_STATE;
  153. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  154. __func__,
  155. NRFX_LOG_ERROR_STRING_GET(err_code));
  156. return err_code;
  157. }
  158. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED)
  159. // Currently, only SPIM3 in nRF52840 supports the extended features.
  160. // Other instances must be checked.
  161. if ((p_instance->drv_inst_idx != NRFX_SPIM3_INST_IDX) &&
  162. ((p_config->dcx_pin != NRFX_SPIM_PIN_NOT_USED) ||
  163. (p_config->frequency == NRF_SPIM_FREQ_16M) ||
  164. (p_config->frequency == NRF_SPIM_FREQ_32M) ||
  165. (p_config->use_hw_ss)))
  166. {
  167. err_code = NRFX_ERROR_NOT_SUPPORTED;
  168. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  169. __func__,
  170. NRFX_LOG_ERROR_STRING_GET(err_code));
  171. return err_code;
  172. }
  173. #endif
  174. NRF_SPIM_Type * p_spim = (NRF_SPIM_Type *)p_instance->p_reg;
  175. #if NRFX_CHECK(NRFX_PRS_ENABLED)
  176. static nrfx_irq_handler_t const irq_handlers[NRFX_SPIM_ENABLED_COUNT] = {
  177. #if NRFX_CHECK(NRFX_SPIM0_ENABLED)
  178. nrfx_spim_0_irq_handler,
  179. #endif
  180. #if NRFX_CHECK(NRFX_SPIM1_ENABLED)
  181. nrfx_spim_1_irq_handler,
  182. #endif
  183. #if NRFX_CHECK(NRFX_SPIM2_ENABLED)
  184. nrfx_spim_2_irq_handler,
  185. #endif
  186. #if NRFX_CHECK(NRFX_SPIM3_ENABLED)
  187. nrfx_spim_3_irq_handler,
  188. #endif
  189. };
  190. if (nrfx_prs_acquire(p_instance->p_reg,
  191. irq_handlers[p_instance->drv_inst_idx]) != NRFX_SUCCESS)
  192. {
  193. err_code = NRFX_ERROR_BUSY;
  194. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  195. __func__,
  196. NRFX_LOG_ERROR_STRING_GET(err_code));
  197. return err_code;
  198. }
  199. #endif // NRFX_CHECK(NRFX_PRS_ENABLED)
  200. p_cb->handler = handler;
  201. p_cb->p_context = p_context;
  202. uint32_t mosi_pin;
  203. uint32_t miso_pin;
  204. // Configure pins used by the peripheral:
  205. // - SCK - output with initial value corresponding with the SPI mode used:
  206. // 0 - for modes 0 and 1 (CPOL = 0), 1 - for modes 2 and 3 (CPOL = 1);
  207. // according to the reference manual guidelines this pin and its input
  208. // buffer must always be connected for the SPI to work.
  209. if (p_config->mode <= NRF_SPIM_MODE_1)
  210. {
  211. nrf_gpio_pin_clear(p_config->sck_pin);
  212. }
  213. else
  214. {
  215. nrf_gpio_pin_set(p_config->sck_pin);
  216. }
  217. nrf_gpio_cfg(p_config->sck_pin,
  218. NRF_GPIO_PIN_DIR_OUTPUT,
  219. NRF_GPIO_PIN_INPUT_CONNECT,
  220. NRF_GPIO_PIN_NOPULL,
  221. NRF_GPIO_PIN_S0S1,
  222. NRF_GPIO_PIN_NOSENSE);
  223. // - MOSI (optional) - output with initial value 0,
  224. if (p_config->mosi_pin != NRFX_SPIM_PIN_NOT_USED)
  225. {
  226. mosi_pin = p_config->mosi_pin;
  227. nrf_gpio_pin_clear(mosi_pin);
  228. nrf_gpio_cfg_output(mosi_pin);
  229. }
  230. else
  231. {
  232. mosi_pin = NRF_SPIM_PIN_NOT_CONNECTED;
  233. }
  234. // - MISO (optional) - input,
  235. if (p_config->miso_pin != NRFX_SPIM_PIN_NOT_USED)
  236. {
  237. miso_pin = p_config->miso_pin;
  238. nrf_gpio_cfg_input(miso_pin, (nrf_gpio_pin_pull_t)NRFX_SPIM_MISO_PULL_CFG);
  239. }
  240. else
  241. {
  242. miso_pin = NRF_SPIM_PIN_NOT_CONNECTED;
  243. }
  244. p_cb->miso_pin = p_config->miso_pin;
  245. // - Slave Select (optional) - output with initial value 1 (inactive).
  246. // 'p_cb->ss_pin' variable is used during transfers to check if SS pin should be toggled,
  247. // so this field needs to be initialized even if the pin is not used.
  248. p_cb->ss_pin = p_config->ss_pin;
  249. if (p_config->ss_pin != NRFX_SPIM_PIN_NOT_USED)
  250. {
  251. if (p_config->ss_active_high)
  252. {
  253. nrf_gpio_pin_clear(p_config->ss_pin);
  254. }
  255. else
  256. {
  257. nrf_gpio_pin_set(p_config->ss_pin);
  258. }
  259. nrf_gpio_cfg_output(p_config->ss_pin);
  260. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED)
  261. if (p_config->use_hw_ss)
  262. {
  263. p_cb->use_hw_ss = p_config->use_hw_ss;
  264. nrf_spim_csn_configure(p_spim,
  265. p_config->ss_pin,
  266. (p_config->ss_active_high == true ?
  267. NRF_SPIM_CSN_POL_HIGH : NRF_SPIM_CSN_POL_LOW),
  268. p_config->ss_duration);
  269. }
  270. #endif
  271. p_cb->ss_active_high = p_config->ss_active_high;
  272. }
  273. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED)
  274. // - DCX (optional) - output.
  275. if (p_config->dcx_pin != NRFX_SPIM_PIN_NOT_USED)
  276. {
  277. nrf_gpio_pin_set(p_config->dcx_pin);
  278. nrf_gpio_cfg_output(p_config->dcx_pin);
  279. nrf_spim_dcx_pin_set(p_spim, p_config->dcx_pin);
  280. }
  281. // Change rx delay
  282. nrf_spim_iftiming_set(p_spim, p_config->rx_delay);
  283. #endif
  284. nrf_spim_pins_set(p_spim, p_config->sck_pin, mosi_pin, miso_pin);
  285. nrf_spim_frequency_set(p_spim, p_config->frequency);
  286. nrf_spim_configure(p_spim, p_config->mode, p_config->bit_order);
  287. nrf_spim_orc_set(p_spim, p_config->orc);
  288. nrf_spim_enable(p_spim);
  289. if (p_cb->handler)
  290. {
  291. NRFX_IRQ_PRIORITY_SET(nrfx_get_irq_number(p_instance->p_reg),
  292. p_config->irq_priority);
  293. NRFX_IRQ_ENABLE(nrfx_get_irq_number(p_instance->p_reg));
  294. }
  295. p_cb->transfer_in_progress = false;
  296. p_cb->state = NRFX_DRV_STATE_INITIALIZED;
  297. err_code = NRFX_SUCCESS;
  298. NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
  299. return err_code;
  300. }
  301. void nrfx_spim_uninit(nrfx_spim_t const * const p_instance)
  302. {
  303. spim_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
  304. NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
  305. if (p_cb->handler)
  306. {
  307. NRFX_IRQ_DISABLE(nrfx_get_irq_number(p_instance->p_reg));
  308. }
  309. NRF_SPIM_Type * p_spim = (NRF_SPIM_Type *)p_instance->p_reg;
  310. if (p_cb->handler)
  311. {
  312. nrf_spim_int_disable(p_spim, NRF_SPIM_ALL_INTS_MASK);
  313. if (p_cb->transfer_in_progress)
  314. {
  315. // Ensure that SPI is not performing any transfer.
  316. nrf_spim_task_trigger(p_spim, NRF_SPIM_TASK_STOP);
  317. // while (!nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_STOPPED))
  318. // {}
  319. uint32_t timeout = 2000;
  320. while (!nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_STOPPED) && timeout > 0)
  321. {timeout--;}
  322. p_cb->transfer_in_progress = false;
  323. }
  324. }
  325. if (p_cb->miso_pin != NRFX_SPIM_PIN_NOT_USED)
  326. {
  327. nrf_gpio_cfg_default(p_cb->miso_pin);
  328. }
  329. nrf_spim_disable(p_spim);
  330. #ifdef USE_WORKAROUND_FOR_ANOMALY_195
  331. if (p_spim == NRF_SPIM3)
  332. {
  333. *(volatile uint32_t *)0x4002F004 = 1;
  334. }
  335. #endif
  336. #if NRFX_CHECK(NRFX_PRS_ENABLED)
  337. nrfx_prs_release(p_instance->p_reg);
  338. #endif
  339. p_cb->state = NRFX_DRV_STATE_UNINITIALIZED;
  340. }
  341. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED)
  342. nrfx_err_t nrfx_spim_xfer_dcx(nrfx_spim_t const * const p_instance,
  343. nrfx_spim_xfer_desc_t const * p_xfer_desc,
  344. uint32_t flags,
  345. uint8_t cmd_length)
  346. {
  347. NRFX_ASSERT(cmd_length <= NRF_SPIM_DCX_CNT_ALL_CMD);
  348. nrf_spim_dcx_cnt_set((NRF_SPIM_Type *)p_instance->p_reg, cmd_length);
  349. return nrfx_spim_xfer(p_instance, p_xfer_desc, 0);
  350. }
  351. #endif
  352. static void finish_transfer(spim_control_block_t * p_cb)
  353. {
  354. // If Slave Select signal is used, this is the time to deactivate it.
  355. if (p_cb->ss_pin != NRFX_SPIM_PIN_NOT_USED)
  356. {
  357. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED)
  358. if (!p_cb->use_hw_ss)
  359. #endif
  360. {
  361. if (p_cb->ss_active_high)
  362. {
  363. nrf_gpio_pin_clear(p_cb->ss_pin);
  364. }
  365. else
  366. {
  367. nrf_gpio_pin_set(p_cb->ss_pin);
  368. }
  369. }
  370. }
  371. // By clearing this flag before calling the handler we allow subsequent
  372. // transfers to be started directly from the handler function.
  373. p_cb->transfer_in_progress = false;
  374. p_cb->evt.type = NRFX_SPIM_EVENT_DONE;
  375. p_cb->handler(&p_cb->evt, p_cb->p_context);
  376. }
  377. __STATIC_INLINE void spim_int_enable(NRF_SPIM_Type * p_spim, bool enable)
  378. {
  379. if (!enable)
  380. {
  381. nrf_spim_int_disable(p_spim, NRF_SPIM_INT_END_MASK);
  382. }
  383. else
  384. {
  385. nrf_spim_int_enable(p_spim, NRF_SPIM_INT_END_MASK);
  386. }
  387. }
  388. __STATIC_INLINE void spim_list_enable_handle(NRF_SPIM_Type * p_spim, uint32_t flags)
  389. {
  390. if (NRFX_SPIM_FLAG_TX_POSTINC & flags)
  391. {
  392. nrf_spim_tx_list_enable(p_spim);
  393. }
  394. else
  395. {
  396. nrf_spim_tx_list_disable(p_spim);
  397. }
  398. if (NRFX_SPIM_FLAG_RX_POSTINC & flags)
  399. {
  400. nrf_spim_rx_list_enable(p_spim);
  401. }
  402. else
  403. {
  404. nrf_spim_rx_list_disable(p_spim);
  405. }
  406. }
  407. static nrfx_err_t spim_xfer(NRF_SPIM_Type * p_spim,
  408. spim_control_block_t * p_cb,
  409. nrfx_spim_xfer_desc_t const * p_xfer_desc,
  410. uint32_t flags)
  411. {
  412. nrfx_err_t err_code;
  413. // EasyDMA requires that transfer buffers are placed in Data RAM region;
  414. // signal error if they are not.
  415. if ((p_xfer_desc->p_tx_buffer != NULL && !nrfx_is_in_ram(p_xfer_desc->p_tx_buffer)) ||
  416. (p_xfer_desc->p_rx_buffer != NULL && !nrfx_is_in_ram(p_xfer_desc->p_rx_buffer)))
  417. {
  418. p_cb->transfer_in_progress = false;
  419. err_code = NRFX_ERROR_INVALID_ADDR;
  420. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  421. __func__,
  422. NRFX_LOG_ERROR_STRING_GET(err_code));
  423. return err_code;
  424. }
  425. #if NRFX_CHECK(NRFX_SPIM_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
  426. p_cb->tx_length = 0;
  427. p_cb->rx_length = 0;
  428. #endif
  429. nrf_spim_tx_buffer_set(p_spim, p_xfer_desc->p_tx_buffer, p_xfer_desc->tx_length);
  430. nrf_spim_rx_buffer_set(p_spim, p_xfer_desc->p_rx_buffer, p_xfer_desc->rx_length);
  431. #if NRFX_CHECK(NRFX_SPIM3_NRF52840_ANOMALY_198_WORKAROUND_ENABLED)
  432. if (p_spim == NRF_SPIM3)
  433. {
  434. anomaly_198_enable(p_xfer_desc->p_tx_buffer, p_xfer_desc->tx_length);
  435. }
  436. #endif
  437. nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_END);
  438. spim_list_enable_handle(p_spim, flags);
  439. if (!(flags & NRFX_SPIM_FLAG_HOLD_XFER))
  440. {
  441. nrf_spim_task_trigger(p_spim, NRF_SPIM_TASK_START);
  442. }
  443. #if NRFX_CHECK(NRFX_SPIM_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
  444. if (flags & NRFX_SPIM_FLAG_HOLD_XFER)
  445. {
  446. nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_STARTED);
  447. p_cb->tx_length = p_xfer_desc->tx_length;
  448. p_cb->rx_length = p_xfer_desc->rx_length;
  449. nrf_spim_tx_buffer_set(p_spim, p_xfer_desc->p_tx_buffer, 0);
  450. nrf_spim_rx_buffer_set(p_spim, p_xfer_desc->p_rx_buffer, 0);
  451. nrf_spim_int_enable(p_spim, NRF_SPIM_INT_STARTED_MASK);
  452. }
  453. #endif
  454. if (!p_cb->handler)
  455. {
  456. while (!nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_END)){}
  457. #if NRFX_CHECK(NRFX_SPIM3_NRF52840_ANOMALY_198_WORKAROUND_ENABLED)
  458. if (p_spim == NRF_SPIM3)
  459. {
  460. anomaly_198_disable();
  461. }
  462. #endif
  463. if (p_cb->ss_pin != NRFX_SPIM_PIN_NOT_USED)
  464. {
  465. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED)
  466. if (!p_cb->use_hw_ss)
  467. #endif
  468. {
  469. if (p_cb->ss_active_high)
  470. {
  471. nrf_gpio_pin_clear(p_cb->ss_pin);
  472. }
  473. else
  474. {
  475. nrf_gpio_pin_set(p_cb->ss_pin);
  476. }
  477. }
  478. }
  479. }
  480. else
  481. {
  482. spim_int_enable(p_spim, !(flags & NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER));
  483. }
  484. err_code = NRFX_SUCCESS;
  485. NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
  486. return err_code;
  487. }
  488. nrfx_err_t nrfx_spim_xfer(nrfx_spim_t const * const p_instance,
  489. nrfx_spim_xfer_desc_t const * p_xfer_desc,
  490. uint32_t flags)
  491. {
  492. spim_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
  493. NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
  494. NRFX_ASSERT(p_xfer_desc->p_tx_buffer != NULL || p_xfer_desc->tx_length == 0);
  495. NRFX_ASSERT(p_xfer_desc->p_rx_buffer != NULL || p_xfer_desc->rx_length == 0);
  496. NRFX_ASSERT(SPIM_LENGTH_VALIDATE(p_instance->drv_inst_idx,
  497. p_xfer_desc->rx_length,
  498. p_xfer_desc->tx_length));
  499. nrfx_err_t err_code = NRFX_SUCCESS;
  500. if (p_cb->transfer_in_progress)
  501. {
  502. err_code = NRFX_ERROR_BUSY;
  503. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  504. __func__,
  505. NRFX_LOG_ERROR_STRING_GET(err_code));
  506. return err_code;
  507. }
  508. else
  509. {
  510. if (p_cb->handler && !(flags & (NRFX_SPIM_FLAG_REPEATED_XFER |
  511. NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER)))
  512. {
  513. p_cb->transfer_in_progress = true;
  514. }
  515. }
  516. p_cb->evt.xfer_desc = *p_xfer_desc;
  517. if (p_cb->ss_pin != NRFX_SPIM_PIN_NOT_USED)
  518. {
  519. #if NRFX_CHECK(NRFX_SPIM_EXTENDED_ENABLED)
  520. if (!p_cb->use_hw_ss)
  521. #endif
  522. {
  523. if (p_cb->ss_active_high)
  524. {
  525. nrf_gpio_pin_set(p_cb->ss_pin);
  526. }
  527. else
  528. {
  529. nrf_gpio_pin_clear(p_cb->ss_pin);
  530. }
  531. }
  532. }
  533. return spim_xfer(p_instance->p_reg, p_cb, p_xfer_desc, flags);
  534. }
  535. void nrfx_spim_abort(nrfx_spim_t const * p_instance)
  536. {
  537. spim_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
  538. NRFX_ASSERT(p_cb->state != NRFX_DRV_STATE_UNINITIALIZED);
  539. nrf_spim_task_trigger(p_instance->p_reg, NRF_SPIM_TASK_STOP);
  540. while (!nrf_spim_event_check(p_instance->p_reg, NRF_SPIM_EVENT_STOPPED))
  541. {}
  542. p_cb->transfer_in_progress = false;
  543. }
  544. uint32_t nrfx_spim_start_task_get(nrfx_spim_t const * p_instance)
  545. {
  546. NRF_SPIM_Type * p_spim = (NRF_SPIM_Type *)p_instance->p_reg;
  547. return nrf_spim_task_address_get(p_spim, NRF_SPIM_TASK_START);
  548. }
  549. uint32_t nrfx_spim_end_event_get(nrfx_spim_t const * p_instance)
  550. {
  551. NRF_SPIM_Type * p_spim = (NRF_SPIM_Type *)p_instance->p_reg;
  552. return nrf_spim_event_address_get(p_spim, NRF_SPIM_EVENT_END);
  553. }
  554. static void irq_handler(NRF_SPIM_Type * p_spim, spim_control_block_t * p_cb)
  555. {
  556. #if NRFX_CHECK(NRFX_SPIM_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
  557. if ((nrf_spim_int_enable_check(p_spim, NRF_SPIM_INT_STARTED_MASK)) &&
  558. (nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_STARTED)) )
  559. {
  560. /* Handle first, zero-length, auxiliary transmission. */
  561. nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_STARTED);
  562. nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_END);
  563. NRFX_ASSERT(p_spim->TXD.MAXCNT == 0);
  564. p_spim->TXD.MAXCNT = p_cb->tx_length;
  565. NRFX_ASSERT(p_spim->RXD.MAXCNT == 0);
  566. p_spim->RXD.MAXCNT = p_cb->rx_length;
  567. /* Disable STARTED interrupt, used only in auxiliary transmission. */
  568. nrf_spim_int_disable(p_spim, NRF_SPIM_INT_STARTED_MASK);
  569. /* Start the actual, glitch-free transmission. */
  570. nrf_spim_task_trigger(p_spim, NRF_SPIM_TASK_START);
  571. return;
  572. }
  573. #endif
  574. if (nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_END))
  575. {
  576. #if NRFX_CHECK(NRFX_SPIM3_NRF52840_ANOMALY_198_WORKAROUND_ENABLED)
  577. if (p_spim == NRF_SPIM3)
  578. {
  579. anomaly_198_disable();
  580. }
  581. #endif
  582. nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_END);
  583. NRFX_ASSERT(p_cb->handler);
  584. NRFX_LOG_DEBUG("Event: NRF_SPIM_EVENT_END.");
  585. finish_transfer(p_cb);
  586. }
  587. }
  588. #if NRFX_CHECK(NRFX_SPIM0_ENABLED)
  589. void nrfx_spim_0_irq_handler(void)
  590. {
  591. irq_handler(NRF_SPIM0, &m_cb[NRFX_SPIM0_INST_IDX]);
  592. }
  593. #endif
  594. #if NRFX_CHECK(NRFX_SPIM1_ENABLED)
  595. void nrfx_spim_1_irq_handler(void)
  596. {
  597. irq_handler(NRF_SPIM1, &m_cb[NRFX_SPIM1_INST_IDX]);
  598. }
  599. #endif
  600. #if NRFX_CHECK(NRFX_SPIM2_ENABLED)
  601. void nrfx_spim_2_irq_handler(void)
  602. {
  603. irq_handler(NRF_SPIM2, &m_cb[NRFX_SPIM2_INST_IDX]);
  604. }
  605. #endif
  606. #if NRFX_CHECK(NRFX_SPIM3_ENABLED)
  607. void nrfx_spim_3_irq_handler(void)
  608. {
  609. irq_handler(NRF_SPIM3, &m_cb[NRFX_SPIM3_INST_IDX]);
  610. }
  611. #endif
  612. #endif // NRFX_CHECK(NRFX_SPIM_ENABLED)