spi_callback.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /**
  2. * \file
  3. *
  4. * \brief Serial Peripheral Interface Driver for SAMB11
  5. *
  6. * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include "spi_callback.h"
  47. struct spi_module *_spi_instances[SPI_INST_NUM];
  48. /**
  49. * \internal
  50. *
  51. * Dummy byte to send when reading in master mode.
  52. */
  53. static uint16_t dummy_write;
  54. static bool flag_direction_both[SPI_INST_NUM];
  55. /**
  56. * \internal
  57. * Writes a character from the TX buffer to the Data register.
  58. *
  59. * \param[in,out] module Pointer to SPI software instance struct
  60. */
  61. static void _spi_write(
  62. struct spi_module *const module)
  63. {
  64. /* Pointer to the hardware module instance */
  65. Spi *const spi_hw = module->hw;
  66. /* Write value will be at least 8-bits long */
  67. uint16_t data_to_send = *(module->tx_buffer_ptr);
  68. /* Increment 8-bit pointer */
  69. (module->tx_buffer_ptr)++;
  70. /* Write the data to send*/
  71. spi_hw->TRANSMIT_DATA.reg = data_to_send & SPI_TRANSMIT_DATA_MASK;
  72. /* Decrement remaining buffer length */
  73. (module->remaining_tx_buffer_length)--;
  74. }
  75. /**
  76. * \internal
  77. * Reads a character from the Data register to the RX buffer.
  78. *
  79. * \param[in,out] module Pointer to SPI software instance struct
  80. */
  81. static void _spi_read(
  82. struct spi_module *const module)
  83. {
  84. /* Pointer to the hardware module instance */
  85. Spi *const spi_hw = module->hw;
  86. uint16_t received_data = (spi_hw->RECEIVE_DATA.reg & SPI_RECEIVE_DATA_MASK);
  87. /* Read value will be at least 8-bits long */
  88. *(module->rx_buffer_ptr) = received_data;
  89. /* Increment 8-bit pointer */
  90. module->rx_buffer_ptr += 1;
  91. /* Decrement length of the remaining buffer */
  92. module->remaining_rx_buffer_length--;
  93. }
  94. #if CONF_SPI_MASTER_ENABLE == true
  95. /**
  96. * \internal
  97. * Writes a dummy character to the Data register.
  98. *
  99. * \param[in,out] module Pointer to SPI software instance struct
  100. */
  101. static void _spi_write_dummy(
  102. struct spi_module *const module)
  103. {
  104. /* Pointer to the hardware module instance */
  105. Spi *const spi_hw = module->hw;
  106. /* Write dummy byte */
  107. spi_hw->TRANSMIT_DATA.reg = dummy_write;
  108. /* Decrement remaining dummy buffer length */
  109. module->remaining_dummy_buffer_length--;
  110. }
  111. #endif
  112. /**
  113. * \internal
  114. * Writes a dummy character from the to the Data register.
  115. *
  116. * \param[in,out] module Pointer to SPI software instance struct
  117. */
  118. static void _spi_read_dummy(
  119. struct spi_module *const module)
  120. {
  121. /* Pointer to the hardware module instance */
  122. Spi *const spi_hw = module->hw;
  123. uint16_t flush = 0;
  124. /* Read dummy byte */
  125. flush = spi_hw->RECEIVE_DATA.reg;
  126. UNUSED(flush);
  127. /* Decrement remaining dummy buffer length */
  128. module->remaining_dummy_buffer_length--;
  129. }
  130. void spi_rx0_isr_handler(void)
  131. {
  132. struct spi_module *module = _spi_instances[0];
  133. /* get interrupt flags and mask out enabled callbacks */
  134. uint32_t flags = module->hw->RECEIVE_STATUS.reg;
  135. flags &= module->hw->RX_INTERRUPT_MASK.reg;
  136. if (flags & SPI_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY) {
  137. if (module->hw->RECEIVE_STATUS.reg & SPI_RECEIVE_STATUS_FIFO_OVERRUN) {
  138. if (module->dir != SPI_DIRECTION_WRITE) {
  139. /* Store the error code */
  140. module->status = STATUS_ERR_OVERFLOW;
  141. /* End transaction */
  142. module->dir = SPI_DIRECTION_IDLE;
  143. module->hw->RX_INTERRUPT_MASK.reg &=
  144. ~(SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK |
  145. SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  146. /* Run callback if registered and enabled */
  147. if ((module->enabled_callback & (1 << SPI_CALLBACK_ERROR)) &&
  148. (module->registered_callback & (1 << SPI_CALLBACK_ERROR))) {
  149. module->status = STATUS_ERR_OVERFLOW;
  150. module->hw->RX_INTERRUPT_MASK.reg &=
  151. ~(SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK);
  152. (module->callback[SPI_CALLBACK_ERROR])(module);
  153. }
  154. }
  155. /* Flush */
  156. uint16_t flush = module->hw->RECEIVE_DATA.reg;
  157. UNUSED(flush);
  158. } else {
  159. if (module->dir == SPI_DIRECTION_WRITE) {
  160. /* Flush receive buffer when writing */
  161. _spi_read_dummy(module);
  162. if (module->remaining_dummy_buffer_length == 0) {
  163. module->hw->RX_INTERRUPT_MASK.reg &=
  164. ~SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK;
  165. module->status = STATUS_OK;
  166. module->dir = SPI_DIRECTION_IDLE;
  167. ///* Run callback if registered and enabled */
  168. //if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_TRANSMITTED)) &&
  169. //(module->registered_callback & (1 << SPI_CALLBACK_BUFFER_TRANSMITTED))) {
  170. //(module->callback[SPI_CALLBACK_BUFFER_TRANSMITTED])(module);
  171. //}
  172. }
  173. } else {
  174. _spi_read(module);
  175. if (module->remaining_rx_buffer_length == 0) {
  176. if(module->dir == SPI_DIRECTION_READ) {
  177. if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_RECEIVED)) &&
  178. (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_RECEIVED))) {
  179. module->status = STATUS_OK;
  180. module->hw->RX_INTERRUPT_MASK.reg &=
  181. ~(SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  182. (module->callback[SPI_CALLBACK_BUFFER_RECEIVED])(module);
  183. }
  184. } else if (module->dir == SPI_DIRECTION_BOTH) {
  185. if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED)) &&
  186. (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED))) {
  187. module->hw->RX_INTERRUPT_MASK.reg &=
  188. ~(SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  189. if (flag_direction_both[0]) {
  190. module->status = STATUS_OK;
  191. flag_direction_both[0] = false;
  192. (module->callback[SPI_CALLBACK_BUFFER_TRANSCEIVED])(module);
  193. } else {
  194. flag_direction_both[0] = true;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. void spi_tx0_isr_handler(void)
  204. {
  205. struct spi_module *module = _spi_instances[0];
  206. /* get interrupt flags and mask out enabled callbacks */
  207. uint32_t flags = module->hw->TRANSMIT_STATUS.reg;
  208. flags &= module->hw->TX_INTERRUPT_MASK.reg;
  209. if (flags & SPI_TRANSMIT_STATUS_TX_FIFO_NOT_FULL_1) {
  210. # if CONF_SPI_MASTER_ENABLE == true
  211. if ((module->mode == SPI_MODE_MASTER) &&
  212. (module->dir == SPI_DIRECTION_READ)) {
  213. /* Send dummy byte when reading in master mode */
  214. _spi_write_dummy(module);
  215. if (module->remaining_dummy_buffer_length == 0) {
  216. /* Disable the Data Register Empty Interrupt */
  217. module->hw->TX_INTERRUPT_MASK.reg &=
  218. ~SPI_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  219. }
  220. }
  221. # endif
  222. if (0
  223. # if CONF_SPI_MASTER_ENABLE == true
  224. || ((module->mode == SPI_MODE_MASTER) &&
  225. (module->dir != SPI_DIRECTION_READ))
  226. # endif
  227. # if CONF_SPI_SLAVE_ENABLE == true
  228. || ((module->mode == SPI_MODE_SLAVE) &&
  229. (module->dir != SPI_DIRECTION_READ))
  230. # endif
  231. ) {
  232. _spi_write(module);
  233. if (module->remaining_tx_buffer_length == 0) {
  234. module->hw->TX_INTERRUPT_MASK.reg &=
  235. ~SPI_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  236. module->hw->TX_INTERRUPT_MASK.reg |=
  237. SPI_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  238. }
  239. }
  240. }
  241. if (flags & SPI_TRANSMIT_STATUS_TX_FIFO_EMPTY) {
  242. if (module->dir == SPI_DIRECTION_WRITE) {
  243. if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_TRANSMITTED)) &&
  244. (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_TRANSMITTED))) {
  245. module->status = STATUS_OK;
  246. /* Disable interrupt */
  247. module->hw->TX_INTERRUPT_MASK.reg &=
  248. ~SPI_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  249. (module->callback[SPI_CALLBACK_BUFFER_TRANSMITTED])(module);
  250. }
  251. } else if (module->dir == SPI_DIRECTION_BOTH) {
  252. if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED)) &&
  253. (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED))) {
  254. /* Disable interrupt */
  255. module->hw->TX_INTERRUPT_MASK.reg &=
  256. ~SPI_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  257. if (flag_direction_both[0]) {
  258. module->status = STATUS_OK;
  259. flag_direction_both[0] = false;
  260. (module->callback[SPI_CALLBACK_BUFFER_TRANSCEIVED])(module);
  261. } else {
  262. flag_direction_both[0] = true;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. void spi_rx1_isr_handler(void)
  269. {
  270. struct spi_module *module = _spi_instances[1];
  271. /* get interrupt flags and mask out enabled callbacks */
  272. uint32_t flags = module->hw->RECEIVE_STATUS.reg;
  273. flags &= module->hw->RX_INTERRUPT_MASK.reg;
  274. if (flags & SPI_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY) {
  275. if (module->hw->RECEIVE_STATUS.reg & SPI_RECEIVE_STATUS_FIFO_OVERRUN) {
  276. if (module->dir != SPI_DIRECTION_WRITE) {
  277. /* Store the error code */
  278. module->status = STATUS_ERR_OVERFLOW;
  279. /* End transaction */
  280. module->dir = SPI_DIRECTION_IDLE;
  281. module->hw->RX_INTERRUPT_MASK.reg &=
  282. ~(SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK |
  283. SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  284. /* Run callback if registered and enabled */
  285. if ((module->enabled_callback & (1 << SPI_CALLBACK_ERROR)) &&
  286. (module->registered_callback & (1 << SPI_CALLBACK_ERROR))) {
  287. module->status = STATUS_ERR_OVERFLOW;
  288. module->hw->RX_INTERRUPT_MASK.reg &=
  289. ~(SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK);
  290. (module->callback[SPI_CALLBACK_ERROR])(module);
  291. }
  292. }
  293. /* Flush */
  294. uint16_t flush = module->hw->RECEIVE_DATA.reg;
  295. UNUSED(flush);
  296. } else {
  297. if (module->dir == SPI_DIRECTION_WRITE) {
  298. /* Flush receive buffer when writing */
  299. _spi_read_dummy(module);
  300. if (module->remaining_dummy_buffer_length == 0) {
  301. module->hw->RX_INTERRUPT_MASK.reg &=
  302. ~SPI_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK;
  303. module->status = STATUS_OK;
  304. module->dir = SPI_DIRECTION_IDLE;
  305. }
  306. } else {
  307. _spi_read(module);
  308. if (module->remaining_rx_buffer_length == 0) {
  309. if(module->dir == SPI_DIRECTION_READ) {
  310. if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_RECEIVED)) &&
  311. (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_RECEIVED))) {
  312. module->status = STATUS_OK;
  313. module->hw->RX_INTERRUPT_MASK.reg &=
  314. ~(SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  315. (module->callback[SPI_CALLBACK_BUFFER_RECEIVED])(module);
  316. }
  317. } else if (module->dir == SPI_DIRECTION_BOTH) {
  318. if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED)) &&
  319. (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED))) {
  320. module->hw->RX_INTERRUPT_MASK.reg &=
  321. ~(SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  322. if (flag_direction_both[1]) {
  323. module->status = STATUS_OK;
  324. flag_direction_both[1] = false;
  325. (module->callback[SPI_CALLBACK_BUFFER_TRANSCEIVED])(module);
  326. } else {
  327. flag_direction_both[1] = true;
  328. }
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }
  336. void spi_tx1_isr_handler(void)
  337. {
  338. struct spi_module *module = _spi_instances[1];
  339. /* get interrupt flags and mask out enabled callbacks */
  340. uint32_t flags = module->hw->TRANSMIT_STATUS.reg;
  341. flags &= module->hw->TX_INTERRUPT_MASK.reg;
  342. if (flags & SPI_TRANSMIT_STATUS_TX_FIFO_NOT_FULL_1) {
  343. # if CONF_SPI_MASTER_ENABLE == true
  344. if ((module->mode == SPI_MODE_MASTER) &&
  345. (module->dir == SPI_DIRECTION_READ)) {
  346. /* Send dummy byte when reading in master mode */
  347. _spi_write_dummy(module);
  348. if (module->remaining_dummy_buffer_length == 0) {
  349. /* Disable the Data Register Empty Interrupt */
  350. module->hw->TX_INTERRUPT_MASK.reg &=
  351. ~SPI_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  352. }
  353. }
  354. # endif
  355. if (0
  356. # if CONF_SPI_MASTER_ENABLE == true
  357. || ((module->mode == SPI_MODE_MASTER) &&
  358. (module->dir != SPI_DIRECTION_READ))
  359. # endif
  360. # if CONF_SPI_SLAVE_ENABLE == true
  361. || ((module->mode == SPI_MODE_SLAVE) &&
  362. (module->dir != SPI_DIRECTION_READ))
  363. # endif
  364. ) {
  365. _spi_write(module);
  366. if (module->remaining_tx_buffer_length == 0) {
  367. module->hw->TX_INTERRUPT_MASK.reg &=
  368. ~SPI_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  369. module->hw->TX_INTERRUPT_MASK.reg |=
  370. SPI_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  371. }
  372. }
  373. }
  374. if (flags & SPI_TRANSMIT_STATUS_TX_FIFO_EMPTY) {
  375. if (module->dir == SPI_DIRECTION_WRITE) {
  376. if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_TRANSMITTED)) &&
  377. (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_TRANSMITTED))) {
  378. module->status = STATUS_OK;
  379. /* Disable interrupt */
  380. module->hw->TX_INTERRUPT_MASK.reg &=
  381. ~SPI_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  382. (module->callback[SPI_CALLBACK_BUFFER_TRANSMITTED])(module);
  383. }
  384. } else if (module->dir == SPI_DIRECTION_BOTH) {
  385. if ((module->enabled_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED)) &&
  386. (module->registered_callback & (1 << SPI_CALLBACK_BUFFER_TRANSCEIVED))) {
  387. /* Disable interrupt */
  388. module->hw->TX_INTERRUPT_MASK.reg &=
  389. ~SPI_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  390. if (flag_direction_both[1]) {
  391. module->status = STATUS_OK;
  392. flag_direction_both[1] = false;
  393. (module->callback[SPI_CALLBACK_BUFFER_TRANSCEIVED])(module);
  394. } else {
  395. flag_direction_both[1] = true;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. /**
  402. * \brief Registers a SPI callback function
  403. *
  404. * Registers a callback function which is implemented by the user.
  405. *
  406. * \note The callback must be enabled by \ref spi_enable_callback, in order
  407. * for the interrupt handler to call it when the conditions for the
  408. * callback type are met.
  409. *
  410. * \param[in] module Pointer to SPI software instance struct
  411. * \param[in] callback_func Pointer to callback function
  412. * \param[in] callback_type Callback type given by an enum
  413. *
  414. */
  415. void spi_register_callback(
  416. struct spi_module *const module,
  417. spi_callback_t callback_func,
  418. enum spi_callback callback_type)
  419. {
  420. /* Sanity check arguments */
  421. Assert(module);
  422. Assert(callback_func);
  423. /* Register callback function */
  424. module->callback[callback_type] = callback_func;
  425. /* Set the bit corresponding to the callback_type */
  426. module->registered_callback |= (1 << callback_type);
  427. }
  428. /**
  429. * \brief Unregisters a SPI callback function
  430. *
  431. * Unregisters a callback function which is implemented by the user.
  432. *
  433. * \param[in] module Pointer to SPI software instance struct
  434. * \param[in] callback_type Callback type given by an enum
  435. *
  436. */
  437. void spi_unregister_callback(
  438. struct spi_module *const module,
  439. enum spi_callback callback_type)
  440. {
  441. /* Sanity check arguments */
  442. Assert(module);
  443. /* Unregister callback function */
  444. module->callback[callback_type] = NULL;
  445. /* Clear the bit corresponding to the callback_type */
  446. module->registered_callback &= ~(1 << callback_type);
  447. }
  448. /**
  449. * \brief Enables callback
  450. *
  451. * Enables the callback function registered by the \ref spi_register_callback.
  452. * The callback function will be called from the interrupt handler when the
  453. * conditions for the callback type are met.
  454. *
  455. * \param[in] module Pointer to SPI software instance struct
  456. * \param[in] callback_type Callback type given by an enum
  457. */
  458. void spi_enable_callback(struct spi_module *const module,
  459. enum spi_callback callback_type)
  460. {
  461. /* Sanity check arguments */
  462. Assert(module);
  463. /* Enable callback */
  464. module->enabled_callback |= (1 << callback_type);
  465. }
  466. /**
  467. * \brief Disables callback
  468. *
  469. * Disables the callback function registered by the \ref spi_register_callback.
  470. * The callback function will not be called from the interrupt handler.
  471. *
  472. * \param[in] module Pointer to SPI software instance struct
  473. * \param[in] callback_type Callback type given by an enum
  474. */
  475. void spi_disable_callback(struct spi_module *const module,
  476. enum spi_callback callback_type)
  477. {
  478. /* Sanity check arguments */
  479. Assert(module);
  480. /* Enable callback */
  481. module->enabled_callback &= ~(1 << callback_type);
  482. }
  483. /**
  484. * \internal
  485. * Starts write of a buffer with a given length
  486. *
  487. * \param[in] module Pointer to SPI software instance struct
  488. * \param[in] tx_data Pointer to data to be transmitted
  489. * \param[in] length Length of data buffer
  490. *
  491. */
  492. static void _spi_write_buffer(
  493. struct spi_module *const module,
  494. uint8_t *tx_data,
  495. uint16_t length)
  496. {
  497. Assert(module);
  498. Assert(tx_data);
  499. /* Write parameters to the device instance */
  500. module->remaining_tx_buffer_length = length;
  501. module->remaining_dummy_buffer_length = length;
  502. module->tx_buffer_ptr = tx_data;
  503. module->status = STATUS_BUSY;
  504. module->dir = SPI_DIRECTION_WRITE;
  505. /* Get a pointer to the hardware module instance */
  506. Spi *const hw = module->hw;
  507. hw->TX_INTERRUPT_MASK.reg = SPI_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  508. }
  509. /**
  510. * \internal
  511. * Setup SPI to read a buffer with a given length
  512. *
  513. * \param[in] module Pointer to SPI software instance struct
  514. * \param[in] rx_data Pointer to data to be received
  515. * \param[in] length Length of data buffer
  516. *
  517. */
  518. static void _spi_read_buffer(
  519. struct spi_module *const module,
  520. uint8_t *rx_data,
  521. uint16_t length)
  522. {
  523. Assert(module);
  524. Assert(rx_data);
  525. /* Set length for the buffer and the pointer, and let
  526. * the interrupt handler do the rest */
  527. module->remaining_rx_buffer_length = length;
  528. module->remaining_dummy_buffer_length = length;
  529. module->rx_buffer_ptr = rx_data;
  530. module->status = STATUS_BUSY;
  531. module->dir = SPI_DIRECTION_READ;
  532. /* Get a pointer to the hardware module instance */
  533. Spi *const hw = module->hw;
  534. hw->RX_INTERRUPT_MASK.reg = SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK;
  535. #if CONF_SPI_MASTER_ENABLE == true
  536. hw->TX_INTERRUPT_MASK.reg = SPI_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  537. #endif
  538. }
  539. /**
  540. * \internal
  541. * Starts transceive of buffers with a given length
  542. *
  543. * \param[in] module Pointer to SPI software instance struct
  544. * \param[in] rx_data Pointer to data to be received
  545. * \param[in] tx_data Pointer to data to be transmitted
  546. * \param[in] length Length of data buffer
  547. *
  548. */
  549. static void _spi_transceive_buffer(
  550. struct spi_module *const module,
  551. uint8_t *tx_data,
  552. uint8_t *rx_data,
  553. uint16_t length)
  554. {
  555. Assert(module);
  556. Assert(tx_data);
  557. /* Write parameters to the device instance */
  558. module->remaining_tx_buffer_length = length;
  559. module->remaining_rx_buffer_length = length;
  560. module->rx_buffer_ptr = rx_data;
  561. module->tx_buffer_ptr = tx_data;
  562. module->status = STATUS_BUSY;
  563. module->dir = SPI_DIRECTION_BOTH;
  564. if (module->hw == SPI0) {
  565. flag_direction_both[0] = false;
  566. } else if (module->hw == SPI1) {
  567. flag_direction_both[1] = false;
  568. }
  569. /* Get a pointer to the hardware module instance */
  570. Spi *const hw = module->hw;
  571. /* Enable the Data Register Empty and RX Complete Interrupt */
  572. hw->TX_INTERRUPT_MASK.reg = SPI_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  573. hw->RX_INTERRUPT_MASK.reg = SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK;
  574. }
  575. /**
  576. * \brief Asynchronous buffer write
  577. *
  578. * Sets up the driver to write to the SPI from a given buffer. If registered
  579. * and enabled, a callback function will be called when the write is finished.
  580. *
  581. * \param[in] module Pointer to SPI software instance struct
  582. * \param[out] tx_data Pointer to data buffer to receive
  583. * \param[in] length Data buffer length
  584. *
  585. * \returns Status of the write request operation.
  586. * \retval STATUS_OK If the operation completed successfully
  587. * \retval STATUS_ERR_BUSY If the SPI was already busy with a write
  588. * operation
  589. * \retval STATUS_ERR_INVALID_ARG If requested write length was zero
  590. */
  591. enum status_code spi_write_buffer_job(
  592. struct spi_module *const module,
  593. uint8_t *tx_data,
  594. uint16_t length)
  595. {
  596. Assert(module);
  597. Assert(tx_data);
  598. if (length == 0) {
  599. return STATUS_ERR_INVALID_ARG;
  600. }
  601. /* Check if the SPI is busy transmitting or slave waiting for TXC*/
  602. if (module->status == STATUS_BUSY) {
  603. return STATUS_BUSY;
  604. }
  605. /* Issue internal write */
  606. _spi_write_buffer(module, tx_data, length);
  607. return STATUS_OK;
  608. }
  609. /**
  610. * \brief Asynchronous buffer read
  611. *
  612. * Sets up the driver to read from the SPI to a given buffer. If registered
  613. * and enabled, a callback function will be called when the read is finished.
  614. *
  615. * \note If address matching is enabled for the slave, the first character
  616. * received and placed in the RX buffer will be the address.
  617. *
  618. * \param[in] module Pointer to SPI software instance struct
  619. * \param[out] rx_data Pointer to data buffer to receive
  620. * \param[in] length Data buffer length
  621. * \param[in] dummy Dummy character to send when reading in master mode
  622. *
  623. * \returns Status of the operation.
  624. * \retval STATUS_OK If the operation completed successfully
  625. * \retval STATUS_ERR_BUSY If the SPI was already busy with a read
  626. * operation
  627. * \retval STATUS_ERR_DENIED If the receiver is not enabled
  628. * \retval STATUS_ERR_INVALID_ARG If requested read length was zero
  629. */
  630. enum status_code spi_read_buffer_job(
  631. struct spi_module *const module,
  632. uint8_t *rx_data,
  633. uint16_t length,
  634. uint16_t dummy)
  635. {
  636. /* Sanity check arguments */
  637. Assert(module);
  638. Assert(rx_data);
  639. if (length == 0) {
  640. return STATUS_ERR_INVALID_ARG;
  641. }
  642. /* Check if the SPI is busy transmitting or slave waiting for TXC*/
  643. if (module->status == STATUS_BUSY) {
  644. return STATUS_BUSY;
  645. }
  646. dummy_write = dummy;
  647. /* Issue internal read */
  648. _spi_read_buffer(module, rx_data, length);
  649. return STATUS_OK;
  650. }
  651. /**
  652. * \brief Asynchronous buffer write and read
  653. *
  654. * Sets up the driver to write and read to and from given buffers. If registered
  655. * and enabled, a callback function will be called when the transfer is finished.
  656. *
  657. * \note If address matching is enabled for the slave, the first character
  658. * received and placed in the RX buffer will be the address.
  659. *
  660. * \param[in] module Pointer to SPI software instance struct
  661. * \param[in] tx_data Pointer to data buffer to send
  662. * \param[out] rx_data Pointer to data buffer to receive
  663. * \param[in] length Data buffer length
  664. *
  665. * \returns Status of the operation.
  666. * \retval STATUS_OK If the operation completed successfully
  667. * \retval STATUS_ERR_BUSY If the SPI was already busy with a read
  668. * operation
  669. * \retval STATUS_ERR_DENIED If the receiver is not enabled
  670. * \retval STATUS_ERR_INVALID_ARG If requested read length was zero
  671. */
  672. enum status_code spi_transceive_buffer_job(
  673. struct spi_module *const module,
  674. uint8_t *tx_data,
  675. uint8_t *rx_data,
  676. uint16_t length)
  677. {
  678. /* Sanity check arguments */
  679. Assert(module);
  680. Assert(rx_data);
  681. if (length == 0) {
  682. return STATUS_ERR_INVALID_ARG;
  683. }
  684. /* Check if the SPI is busy transmitting or slave waiting for TXC*/
  685. if (module->status == STATUS_BUSY) {
  686. return STATUS_BUSY;
  687. }
  688. /* Issue internal transceive */
  689. _spi_transceive_buffer(module, tx_data, rx_data, length);
  690. return STATUS_OK;
  691. }