spi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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.h"
  47. /**
  48. * \brief Determines if the SPI module is currently synchronizing to the bus.
  49. *
  50. * This function will check if the underlying hardware peripheral module is
  51. * currently synchronizing across multiple clock domains to the hardware bus.
  52. * This function can be used to delay further operations on the module until it
  53. * is ready.
  54. *
  55. * \param[in] module SPI hardware module
  56. *
  57. * \return Synchronization status of the underlying hardware module
  58. * \retval true Module synchronization is ongoing
  59. * \retval false Module synchronization is not ongoing
  60. *
  61. */
  62. static bool _spi_is_active(Spi *const spi_module)
  63. {
  64. Assert(spi_module);
  65. return spi_module->SPI_BUS_STATUS.bit.SPI_ACTIVE;
  66. }
  67. /**
  68. * \internal Enable SPI clock.
  69. *
  70. * This function will enable SPI clock.
  71. *
  72. * \param[in] module Pointer to the software instance struct
  73. */
  74. static void _spi_clock_enable(struct spi_module *const module)
  75. {
  76. Assert(module);
  77. Spi *const spi_module = (module->hw);
  78. if (spi_module == (void *)SPI0) {
  79. system_clock_peripheral_enable(PERIPHERAL_SPI0_SCK_CLK);
  80. system_clock_peripheral_enable(PERIPHERAL_SPI0_SCK_PHASE);
  81. system_clock_peripheral_enable(PERIPHERAL_SPI0_IF);
  82. system_clock_peripheral_enable(PERIPHERAL_SPI0_CORE);
  83. } else if (spi_module == (void *)SPI1) {
  84. system_clock_peripheral_enable(PERIPHERAL_SPI1_SCK_CLK);
  85. system_clock_peripheral_enable(PERIPHERAL_SPI1_SCK_PHASE);
  86. system_clock_peripheral_enable(PERIPHERAL_SPI1_IF);
  87. system_clock_peripheral_enable(PERIPHERAL_SPI1_CORE);
  88. }
  89. }
  90. /**
  91. * \internal Disable SPI clock.
  92. *
  93. * This function will disable SPI clock.
  94. *
  95. * \param[in] module Pointer to the software instance struct
  96. */
  97. static void _spi_clock_disable(struct spi_module *const module)
  98. {
  99. Assert(module);
  100. Spi *const spi_module = (module->hw);
  101. if (spi_module == (void *)SPI0) {
  102. system_clock_peripheral_disable(PERIPHERAL_SPI0_SCK_CLK);
  103. system_clock_peripheral_disable(PERIPHERAL_SPI0_SCK_PHASE);
  104. system_clock_peripheral_disable(PERIPHERAL_SPI0_IF);
  105. system_clock_peripheral_disable(PERIPHERAL_SPI0_CORE);
  106. } else if (spi_module == (void *)SPI1) {
  107. system_clock_peripheral_disable(PERIPHERAL_SPI1_SCK_CLK);
  108. system_clock_peripheral_disable(PERIPHERAL_SPI1_SCK_PHASE);
  109. system_clock_peripheral_disable(PERIPHERAL_SPI1_IF);
  110. system_clock_peripheral_disable(PERIPHERAL_SPI1_CORE);
  111. }
  112. }
  113. /**
  114. * \internal Writes an SPI configuration to the hardware module.
  115. *
  116. * This function will write out a given configuration to the hardware module.
  117. * Can only be done when the module is disabled.
  118. *
  119. * \param[in] module Pointer to the software instance struct
  120. * \param[in] config Pointer to the configuration struct
  121. *
  122. * \return The status of the configuration
  123. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  124. * \retval STATUS_OK If the configuration was written
  125. */
  126. static enum status_code _spi_set_config(
  127. struct spi_module *const module,
  128. const struct spi_config *const config)
  129. {
  130. Assert(module);
  131. Assert(config);
  132. Spi *const spi_module = (module->hw);
  133. module->mode = config->mode;
  134. #if CONF_SPI_MASTER_ENABLE == true
  135. /* Find baud value and write it */
  136. if (config->mode == SPI_MODE_MASTER) {
  137. spi_module->SPI_CLK_DIVIDER.reg = config->clock_divider;
  138. }
  139. #endif
  140. /* Set data order */
  141. if (config->data_order == SPI_DATA_ORDER_LSB) {
  142. spi_module->SPI_CONFIGURATION.bit.LSB_FIRST_ENABLE = 0x1;
  143. } else {
  144. spi_module->SPI_CONFIGURATION.bit.LSB_FIRST_ENABLE = 0x0;
  145. }
  146. /* Set clock polarity and clock phase */
  147. switch(config->transfer_mode)
  148. {
  149. case SPI_TRANSFER_MODE_0:
  150. spi_module->SPI_CONFIGURATION.bit.SCK_PHASE = 0x0;
  151. spi_module->SPI_CONFIGURATION.bit.SCK_POLARITY = 0x0;
  152. break;
  153. case SPI_TRANSFER_MODE_1:
  154. spi_module->SPI_CONFIGURATION.bit.SCK_PHASE = 0x1;
  155. spi_module->SPI_CONFIGURATION.bit.SCK_POLARITY = 0x0;
  156. break;
  157. case SPI_TRANSFER_MODE_2:
  158. spi_module->SPI_CONFIGURATION.bit.SCK_PHASE = 0x0;
  159. spi_module->SPI_CONFIGURATION.bit.SCK_POLARITY = 0x1;
  160. break;
  161. case SPI_TRANSFER_MODE_3:
  162. spi_module->SPI_CONFIGURATION.bit.SCK_PHASE = 0x1;
  163. spi_module->SPI_CONFIGURATION.bit.SCK_POLARITY = 0x1;
  164. break;
  165. default:
  166. break;
  167. }
  168. return STATUS_OK;
  169. }
  170. /**
  171. * \brief Checks if the SPI in master mode has shifted out last data, or if the
  172. * master has ended the transfer in slave mode.
  173. *
  174. * This function will check if the SPI master module has shifted out last data,
  175. * or if the slave select pin has been drawn high by the master for the SPI
  176. * slave module.
  177. *
  178. * \param[in] module Pointer to the software instance struct
  179. *
  180. * \return Indication of whether any writes are ongoing
  181. * \retval true If the SPI master module has shifted out data, or slave select
  182. * has been drawn high for SPI slave
  183. * \retval false If the SPI master module has not shifted out data
  184. */
  185. static inline bool _spi_is_write_complete(
  186. Spi *const spi_module)
  187. {
  188. Assert(spi_module);
  189. /* Check interrupt flag */
  190. return (spi_module->TRANSMIT_STATUS.bit.TX_FIFO_EMPTY);
  191. }
  192. /**
  193. * \brief Checks if the SPI module is ready to write data
  194. *
  195. * This function will check if the SPI module is ready to write data.
  196. *
  197. * \param[in] module Pointer to the software instance struct
  198. *
  199. * \return Indication of whether the module is ready to read data or not
  200. * \retval true If the SPI module is ready to write data
  201. * \retval false If the SPI module is not ready to write data
  202. */
  203. static inline bool _spi_is_ready_to_write(
  204. Spi *const spi_module)
  205. {
  206. Assert(spi_module);
  207. /* Check interrupt flag */
  208. return (spi_module->TRANSMIT_STATUS.bit.TX_FIFO_NOT_FULL);
  209. }
  210. /**
  211. * \brief Checks if the SPI module is ready to read data
  212. *
  213. * This function will check if the SPI module is ready to read data.
  214. *
  215. * \param[in] module Pointer to the software instance struct
  216. *
  217. * \return Indication of whether the module is ready to read data or not
  218. * \retval true If the SPI module is ready to read data
  219. * \retval false If the SPI module is not ready to read data
  220. */
  221. static inline bool _spi_is_ready_to_read(
  222. Spi *const spi_module)
  223. {
  224. Assert(spi_module);
  225. /* Check interrupt flag */
  226. return (spi_module->RECEIVE_STATUS.bit.RX_FIFO_NOT_EMPTY);
  227. }
  228. /**
  229. * \brief Initializes an SPI peripheral slave device configuration structure to default values
  230. *
  231. * This function will initialize a given SPI slave device configuration
  232. * structure to a set of known default values. This function should be called
  233. * on any new instance of the configuration structures before being modified by
  234. * the user application.
  235. *
  236. * The default configuration is as follows:
  237. * \li Slave Select on GPIO pin 12
  238. * \li Addressing not enabled
  239. *
  240. * \param[out] config Configuration structure to initialize to default values
  241. */
  242. void spi_slave_inst_get_config_defaults(
  243. struct spi_slave_inst_config *const config)
  244. {
  245. Assert(config);
  246. config->ss_pin = PIN_LP_GPIO_12;
  247. config->address_enabled = false;
  248. config->address = 0;
  249. }
  250. /**
  251. * \brief Initializes an SPI configuration structure to default values
  252. *
  253. * This function will initialize a given SPI configuration structure to a set
  254. * of known default values. This function should be called on any new
  255. * instance of the configuration structures before being modified by the
  256. * user application.
  257. *
  258. * The default configuration is as follows:
  259. * \li Master mode enabled
  260. * \li MSB of the data is transmitted first
  261. * \li Transfer mode 0
  262. * \li MUX Setting 0
  263. * \li Character size 8 bit
  264. * \li Not enabled in sleep mode
  265. * \li Receiver enabled
  266. * \li Baudrate 50000
  267. * \li Default pinmux settings for all pads
  268. * \li Clock source 0 (26MHz)
  269. * \li Clock divider (Formula: baud_rate = ((clock input freq/clock_divider+1)/2))
  270. * (For Example: if clock source is CLOCK_INPUT_0 then
  271. * ((26000000/(129+1))/2) = 100000 bps)
  272. *
  273. * \param[in,out] config Configuration structure to initialize to default values
  274. */
  275. void spi_get_config_defaults(
  276. struct spi_config *const config)
  277. {
  278. Assert(config);
  279. config->mode = SPI_MODE_MASTER;
  280. config->data_order = SPI_DATA_ORDER_MSB;
  281. config->transfer_mode = SPI_TRANSFER_MODE_0;
  282. config->clock_source = SPI_CLK_INPUT_0;
  283. config->clock_divider = 129;
  284. config->pin_number_pad[0] = PIN_LP_GPIO_10;
  285. config->pin_number_pad[1] = PIN_LP_GPIO_11;
  286. config->pin_number_pad[2] = PIN_LP_GPIO_12;
  287. config->pin_number_pad[3] = PIN_LP_GPIO_13;
  288. config->pinmux_sel_pad[0] = MUX_LP_GPIO_10_SPI0_SCK;
  289. config->pinmux_sel_pad[1] = MUX_LP_GPIO_11_SPI0_MOSI;
  290. config->pinmux_sel_pad[2] = MUX_LP_GPIO_12_SPI0_SSN;
  291. config->pinmux_sel_pad[3] = MUX_LP_GPIO_13_SPI0_MISO;
  292. };
  293. /**
  294. * \brief Attaches an SPI peripheral slave
  295. *
  296. * This function will initialize the software SPI peripheral slave, based on
  297. * the values of the config struct. The slave can then be selected and
  298. * optionally addressed by the \ref spi_select_slave function.
  299. *
  300. * \param[out] slave Pointer to the software slave instance struct
  301. * \param[in] config Pointer to the config struct
  302. *
  303. */
  304. void spi_attach_slave(
  305. struct spi_slave_inst *const slave,
  306. struct spi_slave_inst_config *const config)
  307. {
  308. Assert(slave);
  309. Assert(config);
  310. slave->ss_pin = config->ss_pin;
  311. slave->address_enabled = config->address_enabled;
  312. slave->address = config->address;
  313. struct gpio_config config_gpio;
  314. gpio_get_config_defaults(&config_gpio);
  315. config_gpio.direction = GPIO_PIN_DIR_OUTPUT;
  316. gpio_pin_set_config(slave->ss_pin, &config_gpio);
  317. gpio_pin_set_output_level(slave->ss_pin, true);
  318. }
  319. /**
  320. * \brief Resets the SPI module
  321. *
  322. * This function will reset the SPI module to its power on default values and
  323. * disable it.
  324. *
  325. * \param[in,out] module Pointer to the software instance struct
  326. */
  327. void spi_reset(struct spi_module *const module)
  328. {
  329. /* Sanity check arguments */
  330. Spi *const spi_module = (module->hw);
  331. /* Disable the module */
  332. spi_disable(module);
  333. /* Software reset the module */
  334. if(spi_module == (void *)SPI0) {
  335. system_peripheral_reset(PERIPHERAL_SPI0_CORE);
  336. system_peripheral_reset(PERIPHERAL_SPI0_IF);
  337. } else if (spi_module == (void *)SPI1) {
  338. system_peripheral_reset(PERIPHERAL_SPI1_CORE);
  339. system_peripheral_reset(PERIPHERAL_SPI1_IF);
  340. }
  341. }
  342. /**
  343. * \brief Initializes the SPI module
  344. *
  345. * This function will initialize the SPI module, based on the values
  346. * of the config struct.
  347. *
  348. * \param[out] module Pointer to the software instance struct
  349. * \param[in] hw Pointer to hardware instance
  350. * \param[in] config Pointer to the config struct
  351. *
  352. * \return Status of the initialization
  353. * \retval STATUS_OK Module initiated correctly
  354. * \retval STATUS_ERR_DENIED If module is enabled
  355. * \retval STATUS_BUSY If module is busy resetting
  356. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  357. */
  358. enum status_code spi_init(
  359. struct spi_module *const module,
  360. Spi *const hw,
  361. const struct spi_config *const config)
  362. {
  363. /* Sanity check arguments */
  364. Assert(module);
  365. Assert(hw);
  366. Assert(config);
  367. uint8_t idx;
  368. /* Initialize device instance */
  369. module->hw = hw;
  370. Spi *const spi_module = (module->hw);
  371. /* Check if module is enabled. */
  372. if (spi_module->SPI_MODULE_ENABLE.reg & SPI_MODULE_ENABLE_MASK) {
  373. spi_module->SPI_MODULE_ENABLE.reg = (0x0ul << SPI_MODULE_ENABLE_ENABLE_Pos);
  374. }
  375. spi_reset(module);
  376. _spi_clock_enable(module);
  377. #if SPI_CALLBACK_MODE == true
  378. if (module->hw == SPI0) {
  379. _spi_instances[0] = module;
  380. system_register_isr(RAM_ISR_TABLE_SPIRX0_INDEX, (uint32_t)spi_rx0_isr_handler);
  381. system_register_isr(RAM_ISR_TABLE_SPITX0_INDEX, (uint32_t)spi_tx0_isr_handler);
  382. } else if (module->hw == SPI1) {
  383. _spi_instances[1] = module;
  384. system_register_isr(RAM_ISR_TABLE_SPIRX1_INDEX, (uint32_t)spi_rx1_isr_handler);
  385. system_register_isr(RAM_ISR_TABLE_SPITX1_INDEX, (uint32_t)spi_tx1_isr_handler);
  386. }
  387. #endif
  388. //Program the pinmux.
  389. struct gpio_config config_gpio;
  390. gpio_get_config_defaults(&config_gpio);
  391. /* Set the pinmux for this spi module. */
  392. for(idx = 0; idx < 4; idx++) {
  393. if (config->pin_number_pad[idx] != PINMUX_UNUSED) {
  394. if (config->mode == SPI_MODE_MASTER) {
  395. config_gpio.direction = GPIO_PIN_DIR_OUTPUT;
  396. } else if (config->mode == SPI_MODE_SLAVE) {
  397. config_gpio.direction = GPIO_PIN_DIR_INPUT;
  398. }
  399. gpio_pin_set_config(config->pin_number_pad[idx], &config_gpio);
  400. gpio_pinmux_cofiguration(config->pin_number_pad[idx], \
  401. (uint16_t)(config->pinmux_sel_pad[idx]));
  402. }
  403. }
  404. /* Set up the input clock for the module */
  405. spi_module->CLOCK_SOURCE_SELECT.reg = config->clock_source;
  406. # if CONF_SPI_MASTER_ENABLE == true
  407. if (config->mode == SPI_MODE_MASTER) {
  408. /* Set the mode in SPI master mode */
  409. spi_module->SPI_MASTER_MODE.reg = SPI_MODE_MASTER;
  410. }
  411. # endif
  412. # if CONF_SPI_SLAVE_ENABLE == true
  413. if (config->mode == SPI_MODE_SLAVE) {
  414. /* Set the mode in SPI slave mode */
  415. spi_module->SPI_MASTER_MODE.reg = SPI_MODE_SLAVE;
  416. }
  417. # endif
  418. #if SPI_CALLBACK_MODE == true
  419. /* Temporary variables */
  420. uint8_t i;
  421. /* Initialize parameters */
  422. for (i = 0; i < SPI_CALLBACK_N; i++) {
  423. module->callback[i] = NULL;
  424. }
  425. module->tx_buffer_ptr = NULL;
  426. module->rx_buffer_ptr = NULL;
  427. module->remaining_tx_buffer_length = 0x0000;
  428. module->remaining_rx_buffer_length = 0x0000;
  429. module->registered_callback = 0x00;
  430. module->enabled_callback = 0x00;
  431. module->status = STATUS_OK;
  432. module->dir = SPI_DIRECTION_IDLE;
  433. module->locked = 0;
  434. #endif
  435. /* Write configuration to module and return status code */
  436. return _spi_set_config(module, config);
  437. }
  438. /**
  439. * \name Enable/Disable
  440. * @{
  441. */
  442. /**
  443. * \brief Enables the SPI module
  444. *
  445. * This function will enable the SPI module.
  446. *
  447. * \param[in,out] module Pointer to the software instance struct
  448. */
  449. void spi_enable(struct spi_module *const module)
  450. {
  451. Spi *const spi_module = (module->hw);
  452. #if SPI_CALLBACK_MODE == true
  453. if(spi_module == SPI0) {
  454. NVIC_EnableIRQ(SPI0_RX_IRQn);
  455. NVIC_EnableIRQ(SPI0_TX_IRQn);
  456. } else if(spi_module == SPI1) {
  457. NVIC_EnableIRQ(SPI1_RX_IRQn);
  458. NVIC_EnableIRQ(SPI1_TX_IRQn);
  459. }
  460. #endif
  461. /* Enable SPI */
  462. spi_module->SPI_MODULE_ENABLE.reg = SPI_MODULE_ENABLE_ENABLE;
  463. }
  464. /**
  465. * \brief Disables the SPI module
  466. *
  467. * This function will disable the SPI module.
  468. *
  469. * \param[in,out] module Pointer to the software instance struct
  470. */
  471. void spi_disable(struct spi_module *const module)
  472. {
  473. Spi *const spi_module = (module->hw);
  474. # if SPI_CALLBACK_MODE == true
  475. if(spi_module == SPI0) {
  476. NVIC_DisableIRQ(SPI0_RX_IRQn);
  477. NVIC_DisableIRQ(SPI0_TX_IRQn);
  478. } else if(spi_module == SPI1) {
  479. NVIC_DisableIRQ(SPI1_RX_IRQn);
  480. NVIC_DisableIRQ(SPI1_TX_IRQn);
  481. }
  482. # endif
  483. /* Disable SPI */
  484. spi_module->SPI_MODULE_ENABLE.reg = (0x0ul << SPI_MODULE_ENABLE_ENABLE_Pos);
  485. _spi_clock_disable(module);
  486. }
  487. /**
  488. * \brief Attempt to get lock on driver instance
  489. *
  490. * This function checks the instance's lock, which indicates whether or not it
  491. * is currently in use, and sets the lock if it was not already set.
  492. *
  493. * The purpose of this is to enable exclusive access to driver instances, so
  494. * that, e.g., transactions by different services will not interfere with each
  495. * other.
  496. *
  497. * \param[in,out] module Pointer to the driver instance to lock
  498. *
  499. * \retval STATUS_OK If the module was locked
  500. * \retval STATUS_BUSY If the module was already locked
  501. */
  502. enum status_code spi_lock(struct spi_module *const module)
  503. {
  504. enum status_code status;
  505. if (module->locked) {
  506. status = STATUS_BUSY;
  507. } else {
  508. module->locked = true;
  509. status = STATUS_OK;
  510. }
  511. return status;
  512. }
  513. /**
  514. * \brief Unlock driver instance
  515. *
  516. * This function clears the instance lock, indicating that it is available for
  517. * use.
  518. *
  519. * \param[in,out] module Pointer to the driver instance to lock.
  520. *
  521. * \retval STATUS_OK If the module was locked
  522. * \retval STATUS_BUSY If the module was already locked
  523. */
  524. void spi_unlock(struct spi_module *const module)
  525. {
  526. module->locked = false;
  527. }
  528. /**
  529. * \brief Transfers a single SPI character
  530. *
  531. * This function will send a single SPI character via SPI and ignore any data
  532. * shifted in by the connected device. To both send and receive data, use the
  533. * \ref spi_transceive_wait function or use the \ref spi_read function after
  534. * writing a character.
  535. *
  536. * Note that this function does not handle the SS (Slave Select)
  537. * pin(s) in master mode; this must be handled from the user application.
  538. *
  539. * \note In slave mode, the data will not be transferred before a master
  540. * initiates a transaction.
  541. *
  542. * \param[in] module Pointer to the software instance struct
  543. * \param[in] tx_data Data to transmit
  544. *
  545. * \return Status of the procedure
  546. * \retval STATUS_OK If the data was written
  547. * \retval STATUS_BUSY If the last write was not completed
  548. */
  549. enum status_code spi_write(struct spi_module *module, uint8_t tx_data)
  550. {
  551. /* Sanity check arguments */
  552. Assert(module);
  553. Assert(module->hw);
  554. Spi *const spi_module = (module->hw);
  555. /* Check if the data register has been copied to the shift register */
  556. if (!_spi_is_ready_to_write(spi_module)) {
  557. /* Data register has not been copied to the shift register, return */
  558. return STATUS_BUSY;
  559. }
  560. /* Write the character to the DATA register */
  561. spi_module->TRANSMIT_DATA.reg = tx_data & SPI_TRANSMIT_DATA_MASK;
  562. return STATUS_OK;
  563. }
  564. /**
  565. * \brief Reads last received SPI character
  566. *
  567. * This function will return the last SPI character shifted into the receive
  568. * register by the \ref spi_write function
  569. *
  570. * \note Receiver must be enabled in the configuration
  571. *
  572. * \param[in] module Pointer to the software instance struct
  573. * \param[out] rx_data Pointer to store the received data
  574. *
  575. * \returns Status of the read operation.
  576. * \retval STATUS_OK If data was read
  577. * \retval STATUS_ERR_IO If no data is available
  578. * \retval STATUS_ERR_OVERFLOW If the data is overflown
  579. */
  580. enum status_code spi_read(
  581. struct spi_module *const module,
  582. uint8_t *rx_data)
  583. {
  584. /* Sanity check arguments */
  585. Assert(module);
  586. Assert(module->hw);
  587. Spi *const spi_module = (module->hw);
  588. /* Check if data is ready to be read */
  589. if (!_spi_is_ready_to_read(spi_module)) {
  590. /* No data has been received, return */
  591. return STATUS_ERR_IO;
  592. }
  593. /* Return value */
  594. enum status_code retval = STATUS_OK;
  595. /* Check if data is overflown */
  596. if (spi_module->RECEIVE_STATUS.bit.FIFO_OVERRUN) {
  597. retval = STATUS_ERR_OVERFLOW;
  598. }
  599. /* Read the character from the DATA register */
  600. *rx_data = ((uint8_t)spi_module->RECEIVE_DATA.reg & SPI_RECEIVE_DATA_MASK);
  601. return retval;
  602. }
  603. /**
  604. * \brief Transceive requested amount of data to and from the SPI.
  605. *
  606. * This function will return after sending and receiving requested amount of data
  607. *
  608. * \note Receiver must be enabled in the configuration
  609. * \note The \ref spi_select_slave function should be called before calling
  610. * this function.
  611. *
  612. * \param[in] module Pointer to the software instance struct
  613. * \param[in] tx_data Pointer containing the data to be transmitted
  614. * \param[in] length Length of data to be read
  615. * \param[out] rx_data Pointer to store the received data
  616. *
  617. * \returns Status of the read operation.
  618. * \retval STATUS_OK If data was read
  619. * \retval STATUS_ERR_IO If no data is available
  620. * \retval STATUS_ERR_OVERFLOW If the data is overflown
  621. */
  622. enum status_code spi_transceive_buffer_wait(
  623. struct spi_module *const module,
  624. uint8_t *tx_data,
  625. uint8_t *rx_data,
  626. uint16_t length)
  627. {
  628. Spi *spi_module = module->hw;
  629. uint8_t dummy = 0;
  630. uint8_t skip_mosi = 0;
  631. uint8_t skip_miso = 0;
  632. uint8_t status;
  633. uint16_t transfer_len = 0;
  634. if(spi_module == 0) {
  635. return STATUS_ERR_NOT_INITIALIZED;
  636. }
  637. if(!tx_data) {
  638. tx_data = &dummy;
  639. *tx_data = module->tx_dummy_byte;
  640. skip_mosi = 1;
  641. } else if(!rx_data) {
  642. rx_data = &dummy;
  643. skip_miso = 1;
  644. } else if(length == 0) {
  645. return STATUS_ERR_INVALID_ARG;
  646. }
  647. /* Check for Idle */
  648. do {
  649. status = _spi_is_active(spi_module);
  650. }while(status);
  651. /* Clear all status registers */
  652. spi_module->RECEIVE_STATUS.reg;
  653. spi_module->TRANSMIT_STATUS.reg;
  654. /* Start transfer */
  655. while(transfer_len < length) {
  656. /* Write data to MOSI */
  657. while(!_spi_is_ready_to_write(spi_module));
  658. spi_module->TRANSMIT_DATA.reg = *tx_data;
  659. /* Read data shifted from MISO */
  660. while(!_spi_is_ready_to_read(spi_module));
  661. *rx_data = spi_module->RECEIVE_DATA.reg;
  662. transfer_len++;
  663. if (!skip_mosi) {
  664. tx_data++;
  665. }
  666. if (!skip_miso) {
  667. rx_data++;
  668. }
  669. }
  670. /* check TXFIFO is empty */
  671. do {
  672. status = _spi_is_write_complete(spi_module);
  673. }while(!status);
  674. return STATUS_OK;
  675. }
  676. /**
  677. * \brief Transceive single byte of data to and from the SPI.
  678. *
  679. * This function will return after single byte of data transceived.
  680. *
  681. * \note Receiver must be enabled in the configuration
  682. * \note The \ref spi_select_slave function should be called before calling
  683. * this function.
  684. *
  685. * \param[in] module Pointer to the software instance struct
  686. * \param[in] tx_data Pointer containing the data to be transmitted
  687. * \param[out] rx_data Pointer to store the received data
  688. *
  689. * \returns Status of the read operation.
  690. * \retval STATUS_OK If data was read
  691. * \retval STATUS_ERR_IO If no data is available
  692. * \retval STATUS_ERR_OVERFLOW If the data is overflown
  693. */
  694. enum status_code spi_transceive_wait(
  695. struct spi_module *const module,
  696. uint8_t *tx_data,
  697. uint8_t *rx_data)
  698. {
  699. return spi_transceive_buffer_wait(module, tx_data, rx_data, 1);
  700. }
  701. /**
  702. * \brief Reads requested amount of data from the SPI.
  703. *
  704. * This function will return after reading requested amount of data
  705. *
  706. * \note Receiver must be enabled in the configuration
  707. * \note The \ref spi_select_slave function should be called before calling
  708. * this function.
  709. *
  710. * \param[in] module Pointer to the software instance struct
  711. * \param[in] length Length of data to be read
  712. * \param[in] dummy Dummy byte to be sent on bus when reading data
  713. * \param[out] rx_data Pointer to store the received data
  714. *
  715. * \returns Status of the read operation.
  716. * \retval STATUS_OK If data was read
  717. * \retval STATUS_ERR_IO If no data is available
  718. * \retval STATUS_ERR_OVERFLOW If the data is overflown
  719. */
  720. enum status_code spi_read_buffer_wait(
  721. struct spi_module *const module,
  722. uint8_t *rx_data,
  723. uint16_t length,
  724. uint8_t dummy)
  725. {
  726. module->tx_dummy_byte = dummy;
  727. return spi_transceive_buffer_wait(module, NULL, rx_data, length);
  728. }
  729. /**
  730. * \brief Writes requested amount of data to the SPI.
  731. *
  732. * This function will return after writing requested amount of data
  733. *
  734. * \note The \ref spi_select_slave function should be called before calling
  735. * this function.
  736. *
  737. * \param[in] module Pointer to the software instance struct
  738. * \param[in] length length of data to be read
  739. * \param[out] tx_data Pointer to buffer to be transmitted
  740. *
  741. * \returns Status of the read operation.
  742. * \retval STATUS_OK If data was read
  743. * \retval STATUS_ERR_IO If no data is available
  744. * \retval STATUS_ERR_OVERFLOW If the data is overflown
  745. */
  746. enum status_code spi_write_buffer_wait(
  747. struct spi_module *const module,
  748. uint8_t *tx_data,
  749. uint16_t length)
  750. {
  751. return spi_transceive_buffer_wait(module, tx_data, NULL, length);
  752. }
  753. /**
  754. * \brief Asserting/Deasserting the slave select for the corresponding slave.
  755. *
  756. * This function will assert or deassert the SS of the requested slave device.
  757. *
  758. * \param[in] module Pointer to the software instance struct
  759. * \param[in] slave Pointer containing slave instance
  760. * \param[in] select Bool to select the salve or deselect
  761. *
  762. * \returns Status of the slave select operation.
  763. *
  764. * \retval STATUS_OK If SS pin is a valid one and selected/deselected
  765. * \retval STATUS_ERR_INVALID_ARG Invalid SS pin
  766. */
  767. enum status_code spi_select_slave(
  768. struct spi_module *const module,
  769. struct spi_slave_inst *const slave,
  770. bool select)
  771. {
  772. uint8_t gpio_num = slave->ss_pin;
  773. if(select) {
  774. /* ASSERT Slave select pin */
  775. gpio_pin_set_output_level(gpio_num, false);
  776. } else {
  777. /* DEASSERT Slave select pin */
  778. gpio_pin_set_output_level(gpio_num, true);
  779. }
  780. return STATUS_OK;
  781. }