spi.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Serial Peripheral Interface Driver
  5. *
  6. * Copyright (C) 2012-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 Resets the SPI module
  49. *
  50. * This function will reset the SPI module to its power on default values and
  51. * disable it.
  52. *
  53. * \param[in,out] module Pointer to the software instance struct
  54. */
  55. void spi_reset(
  56. struct spi_module *const module)
  57. {
  58. /* Sanity check arguments */
  59. Assert(module);
  60. Assert(module->hw);
  61. SercomSpi *const spi_module = &(module->hw->SPI);
  62. /* Disable the module */
  63. spi_disable(module);
  64. while (spi_is_syncing(module)) {
  65. /* Wait until the synchronization is complete */
  66. }
  67. /* Software reset the module */
  68. spi_module->CTRLA.reg |= SERCOM_SPI_CTRLA_SWRST;
  69. }
  70. /**
  71. * \brief Set the baudrate of the SPI module
  72. *
  73. * This function will set the baudrate of the SPI module.
  74. *
  75. * \param[in] module Pointer to the software instance struct
  76. * \param[in] baudrate The baudrate wanted
  77. *
  78. * \return The status of the configuration.
  79. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  80. * \retval STATUS_OK If the configuration was written
  81. */
  82. enum status_code spi_set_baudrate(
  83. struct spi_module *const module,
  84. uint32_t baudrate)
  85. {
  86. /* Sanity check arguments */
  87. Assert(module);
  88. Assert(baudrate);
  89. Assert(module->hw);
  90. /* Value to write to BAUD register */
  91. uint16_t baud = 0;
  92. SercomSpi *const spi_module = &(module->hw->SPI);
  93. /* Disable the module */
  94. spi_disable(module);
  95. while (spi_is_syncing(module)) {
  96. /* Wait until the synchronization is complete */
  97. }
  98. /* Find frequency of the internal SERCOMi_GCLK_ID_CORE */
  99. uint32_t sercom_index = _sercom_get_sercom_inst_index(module->hw);
  100. uint32_t gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
  101. uint32_t internal_clock = system_gclk_chan_get_hz(gclk_index);
  102. /* Get baud value, based on baudrate and the internal clock frequency */
  103. enum status_code error_code = _sercom_get_sync_baud_val(
  104. baudrate, internal_clock, &baud);
  105. if (error_code != STATUS_OK) {
  106. /* Baud rate calculation error, return status code */
  107. return STATUS_ERR_INVALID_ARG;
  108. }
  109. spi_module->BAUD.reg = (uint8_t)baud;
  110. while (spi_is_syncing(module)) {
  111. /* Wait until the synchronization is complete */
  112. }
  113. /* Enable the module */
  114. spi_enable(module);
  115. while (spi_is_syncing(module)) {
  116. /* Wait until the synchronization is complete */
  117. }
  118. return STATUS_OK;
  119. }
  120. # if CONF_SPI_SLAVE_ENABLE == true
  121. /**
  122. * \internal Clears the Transmit Complete interrupt flag.
  123. *
  124. * \param[in] module Pointer to the software instance struct
  125. */
  126. static void _spi_clear_tx_complete_flag(
  127. struct spi_module *const module)
  128. {
  129. /* Sanity check arguments */
  130. Assert(module);
  131. Assert(module->hw);
  132. SercomSpi *const spi_module = &(module->hw->SPI);
  133. /* Clear interrupt flag */
  134. spi_module->INTFLAG.reg = SPI_INTERRUPT_FLAG_TX_COMPLETE;
  135. }
  136. # endif
  137. /**
  138. * \internal Writes an SPI SERCOM configuration to the hardware module.
  139. *
  140. * This function will write out a given configuration to the hardware module.
  141. * Can only be done when the module is disabled.
  142. *
  143. * \param[in] module Pointer to the software instance struct
  144. * \param[in] config Pointer to the configuration struct
  145. *
  146. * \return The status of the configuration.
  147. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  148. * \retval STATUS_OK If the configuration was written
  149. */
  150. static enum status_code _spi_set_config(
  151. struct spi_module *const module,
  152. const struct spi_config *const config)
  153. {
  154. /* Sanity check arguments */
  155. Assert(module);
  156. Assert(config);
  157. Assert(module->hw);
  158. SercomSpi *const spi_module = &(module->hw->SPI);
  159. Sercom *const hw = module->hw;
  160. struct system_pinmux_config pin_conf;
  161. system_pinmux_get_config_defaults(&pin_conf);
  162. pin_conf.direction = SYSTEM_PINMUX_PIN_DIR_INPUT;
  163. if(config->mode == SPI_MODE_SLAVE) {
  164. pin_conf.input_pull = SYSTEM_PINMUX_PIN_PULL_NONE;
  165. }
  166. uint32_t pad_pinmuxes[] = {
  167. config->pinmux_pad0, config->pinmux_pad1,
  168. config->pinmux_pad2, config->pinmux_pad3
  169. };
  170. /* Configure the SERCOM pins according to the user configuration */
  171. for (uint8_t pad = 0; pad < 4; pad++) {
  172. uint32_t current_pinmux = pad_pinmuxes[pad];
  173. if (current_pinmux == PINMUX_DEFAULT) {
  174. current_pinmux = _sercom_get_default_pad(hw, pad);
  175. }
  176. if (current_pinmux != PINMUX_UNUSED) {
  177. pin_conf.mux_position = current_pinmux & 0xFFFF;
  178. system_pinmux_pin_set_config(current_pinmux >> 16, &pin_conf);
  179. }
  180. }
  181. module->mode = config->mode;
  182. module->character_size = config->character_size;
  183. module->receiver_enabled = config->receiver_enable;
  184. # ifdef FEATURE_SPI_HARDWARE_SLAVE_SELECT
  185. module->master_slave_select_enable = config->master_slave_select_enable;
  186. # endif
  187. # if CONF_SPI_MASTER_ENABLE == true
  188. /* Value to write to BAUD register */
  189. uint16_t baud = 0;
  190. # endif
  191. /* Value to write to CTRLA register */
  192. uint32_t ctrla = 0;
  193. /* Value to write to CTRLB register */
  194. uint32_t ctrlb = 0;
  195. # if CONF_SPI_MASTER_ENABLE == true
  196. /* Find baud value and write it */
  197. if (config->mode == SPI_MODE_MASTER) {
  198. /* Find frequency of the internal SERCOMi_GCLK_ID_CORE */
  199. uint32_t sercom_index = _sercom_get_sercom_inst_index(module->hw);
  200. uint32_t gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
  201. uint32_t internal_clock = system_gclk_chan_get_hz(gclk_index);
  202. /* Get baud value, based on baudrate and the internal clock frequency */
  203. enum status_code error_code = _sercom_get_sync_baud_val(
  204. config->mode_specific.master.baudrate,
  205. internal_clock, &baud);
  206. if (error_code != STATUS_OK) {
  207. /* Baud rate calculation error, return status code */
  208. return STATUS_ERR_INVALID_ARG;
  209. }
  210. spi_module->BAUD.reg = (uint8_t)baud;
  211. }
  212. # endif
  213. # if CONF_SPI_SLAVE_ENABLE == true
  214. if (config->mode == SPI_MODE_SLAVE) {
  215. /* Set frame format */
  216. ctrla = config->mode_specific.slave.frame_format;
  217. /* Set address mode */
  218. ctrlb = config->mode_specific.slave.address_mode;
  219. /* Set address and address mask*/
  220. spi_module->ADDR.reg |=
  221. (config->mode_specific.slave.address << SERCOM_SPI_ADDR_ADDR_Pos) |
  222. (config->mode_specific.slave.address_mask << SERCOM_SPI_ADDR_ADDRMASK_Pos);
  223. if (config->mode_specific.slave.preload_enable) {
  224. /* Enable pre-loading of shift register */
  225. ctrlb |= SERCOM_SPI_CTRLB_PLOADEN;
  226. }
  227. }
  228. # endif
  229. /* Set data order */
  230. ctrla |= config->data_order;
  231. /* Set clock polarity and clock phase */
  232. ctrla |= config->transfer_mode;
  233. /* Set MUX setting */
  234. ctrla |= config->mux_setting;
  235. /* Set SPI character size */
  236. ctrlb |= config->character_size;
  237. /* Set whether module should run in standby. */
  238. if (config->run_in_standby || system_is_debugger_present()) {
  239. ctrla |= SERCOM_SPI_CTRLA_RUNSTDBY;
  240. }
  241. if (config->receiver_enable) {
  242. /* Enable receiver */
  243. ctrlb |= SERCOM_SPI_CTRLB_RXEN;
  244. }
  245. # ifdef FEATURE_SPI_SLAVE_SELECT_LOW_DETECT
  246. if (config->select_slave_low_detect_enable) {
  247. /* Enable Slave Select Low Detect */
  248. ctrlb |= SERCOM_SPI_CTRLB_SSDE;
  249. }
  250. # endif
  251. # ifdef FEATURE_SPI_HARDWARE_SLAVE_SELECT
  252. if (config->master_slave_select_enable) {
  253. /* Enable Master Slave Select */
  254. ctrlb |= SERCOM_SPI_CTRLB_MSSEN;
  255. }
  256. # endif
  257. /* Write CTRLA register */
  258. spi_module->CTRLA.reg |= ctrla;
  259. /* Write CTRLB register */
  260. spi_module->CTRLB.reg |= ctrlb;
  261. return STATUS_OK;
  262. }
  263. #if SPI_CALLBACK_MODE == false
  264. /**
  265. * \internal Checks an SPI config against current set config
  266. *
  267. * This function will check that the config does not alter the
  268. * configuration of the module. If the new config changes any
  269. * setting, the initialization will be discarded.
  270. *
  271. * \param[in] module Pointer to the software instance struct
  272. * \param[in] config Pointer to the configuration struct
  273. *
  274. * \return The status of the configuration.
  275. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  276. * \retval STATUS_ERR_DENIED If configuration was different from previous
  277. * \retval STATUS_OK If the configuration was written
  278. */
  279. static enum status_code _spi_check_config(
  280. struct spi_module *const module,
  281. const struct spi_config *const config)
  282. {
  283. /* Sanity check arguments */
  284. Assert(module);
  285. Assert(config);
  286. Assert(module->hw);
  287. SercomSpi *const spi_module = &(module->hw->SPI);
  288. Sercom *const hw = module->hw;
  289. uint32_t pad_pinmuxes[] = {
  290. config->pinmux_pad0, config->pinmux_pad1,
  291. config->pinmux_pad2, config->pinmux_pad3
  292. };
  293. /* Compare the current SERCOM pins against the user configuration */
  294. for (uint8_t pad = 0; pad < 4; pad++) {
  295. uint32_t current_pinmux = pad_pinmuxes[pad];
  296. if (current_pinmux == PINMUX_DEFAULT) {
  297. current_pinmux = _sercom_get_default_pad(hw, pad);
  298. }
  299. if (current_pinmux == PINMUX_UNUSED) {
  300. continue;
  301. }
  302. if ((current_pinmux & 0xFFFF) !=
  303. system_pinmux_pin_get_mux_position(current_pinmux >> 16)) {
  304. module->hw = NULL;
  305. return STATUS_ERR_DENIED;
  306. }
  307. }
  308. # if CONF_SPI_MASTER_ENABLE == true
  309. /* Value to read BAUD register */
  310. uint16_t baud;
  311. uint32_t external_clock = system_gclk_chan_get_hz(SERCOM_GCLK_ID);
  312. # endif
  313. /* Value to read CTRLA, CTRLB and ADDR register */
  314. uint32_t ctrla = 0;
  315. uint32_t ctrlb = 0;
  316. # if CONF_SPI_SLAVE_ENABLE == true
  317. uint32_t addr = 0;
  318. # endif
  319. # if CONF_SPI_MASTER_ENABLE == true
  320. /* Find baud value and compare it */
  321. if (config->mode == SPI_MODE_MASTER) {
  322. enum status_code error_code = _sercom_get_sync_baud_val(
  323. config->mode_specific.master.baudrate,
  324. external_clock, &baud);
  325. if (error_code != STATUS_OK) {
  326. /* Baud rate calculation error, return status code */
  327. return STATUS_ERR_INVALID_ARG;
  328. }
  329. if (spi_module->BAUD.reg != (uint8_t)baud) {
  330. return STATUS_ERR_DENIED;
  331. }
  332. ctrla |= SERCOM_SPI_CTRLA_MODE(0x3);
  333. }
  334. # endif
  335. # if CONF_SPI_SLAVE_ENABLE == true
  336. if (config->mode == SPI_MODE_SLAVE) {
  337. /* Set frame format */
  338. ctrla |= config->mode_specific.slave.frame_format;
  339. /* Set address mode */
  340. ctrlb |= config->mode_specific.slave.address_mode;
  341. /* Set address and address mask*/
  342. addr |= (config->mode_specific.slave.address << SERCOM_SPI_ADDR_ADDR_Pos) |
  343. (config->mode_specific.slave.address_mask << SERCOM_SPI_ADDR_ADDRMASK_Pos);
  344. if (spi_module->CTRLA.reg != addr) {
  345. return STATUS_ERR_DENIED;
  346. }
  347. if (config->mode_specific.slave.preload_enable) {
  348. /* Enable pre-loading of shift register */
  349. ctrlb |= SERCOM_SPI_CTRLB_PLOADEN;
  350. }
  351. ctrla |= SERCOM_SPI_CTRLA_MODE(0x2);
  352. }
  353. # endif
  354. /* Set data order */
  355. ctrla |= config->data_order;
  356. /* Set clock polarity and clock phase */
  357. ctrla |= config->transfer_mode;
  358. /* Set MUX setting */
  359. ctrla |= config->mux_setting;
  360. /* Set SPI character size */
  361. ctrlb |= config->character_size;
  362. if (config->run_in_standby) {
  363. /* Enable in sleep mode */
  364. ctrla |= SERCOM_SPI_CTRLA_RUNSTDBY;
  365. }
  366. if (config->receiver_enable) {
  367. /* Enable receiver */
  368. ctrlb |= SERCOM_SPI_CTRLB_RXEN;
  369. }
  370. # ifdef FEATURE_SPI_SLAVE_SELECT_LOW_DETECT
  371. if (config->select_slave_low_detect_enable) {
  372. /* Enable Slave Select Low Detect */
  373. ctrlb |= SERCOM_SPI_CTRLB_SSDE;
  374. }
  375. # endif
  376. # ifdef FEATURE_SPI_HARDWARE_SLAVE_SELECT
  377. if (config->master_slave_select_enable) {
  378. /* Enable Master Slave Select */
  379. ctrlb |= SERCOM_SPI_CTRLB_MSSEN;
  380. }
  381. # endif
  382. ctrla |= SERCOM_SPI_CTRLA_ENABLE;
  383. /* Check that same config is set */
  384. if (spi_module->CTRLA.reg == ctrla &&
  385. spi_module->CTRLB.reg == ctrlb) {
  386. module->mode = config->mode;
  387. module->character_size = config->character_size;
  388. return STATUS_OK;
  389. }
  390. /* Not same config, wipe module pointer and return */
  391. module->hw = NULL;
  392. return STATUS_ERR_DENIED;
  393. }
  394. #endif
  395. /**
  396. * \brief Initializes the SERCOM SPI module
  397. *
  398. * This function will initialize the SERCOM SPI module, based on the values
  399. * of the config struct.
  400. *
  401. * \param[out] module Pointer to the software instance struct
  402. * \param[in] hw Pointer to hardware instance
  403. * \param[in] config Pointer to the config struct
  404. *
  405. * \return Status of the initialization.
  406. * \retval STATUS_OK Module initiated correctly
  407. * \retval STATUS_ERR_DENIED If module is enabled
  408. * \retval STATUS_BUSY If module is busy resetting
  409. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  410. */
  411. enum status_code spi_init(
  412. struct spi_module *const module,
  413. Sercom *const hw,
  414. const struct spi_config *const config)
  415. {
  416. /* Sanity check arguments */
  417. Assert(module);
  418. Assert(hw);
  419. Assert(config);
  420. /* Initialize device instance */
  421. module->hw = hw;
  422. SercomSpi *const spi_module = &(module->hw->SPI);
  423. /* Check if module is enabled. */
  424. if (spi_module->CTRLA.reg & SERCOM_SPI_CTRLA_ENABLE) {
  425. # if SPI_CALLBACK_MODE == false
  426. /* Check if config is valid */
  427. return _spi_check_config(module, config);
  428. # else
  429. return STATUS_ERR_DENIED;
  430. # endif
  431. }
  432. /* Check if reset is in progress. */
  433. if (spi_module->CTRLA.reg & SERCOM_SPI_CTRLA_SWRST){
  434. return STATUS_BUSY;
  435. }
  436. uint32_t sercom_index = _sercom_get_sercom_inst_index(module->hw);
  437. uint32_t pm_index, gclk_index;
  438. #if (SAML21) || (SAMR30)
  439. if (sercom_index == 5) {
  440. # ifdef ID_SERCOM5
  441. pm_index = MCLK_APBDMASK_SERCOM5_Pos;
  442. gclk_index = SERCOM5_GCLK_ID_CORE;
  443. # else
  444. return STATUS_ERR_INVALID_ARG;
  445. # endif
  446. } else {
  447. pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
  448. gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
  449. }
  450. #elif (SAMC21)
  451. if (sercom_index == 5) {
  452. # ifdef ID_SERCOM5
  453. pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
  454. gclk_index = SERCOM5_GCLK_ID_CORE;
  455. # else
  456. return STATUS_ERR_INVALID_ARG;
  457. # endif
  458. } else {
  459. pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
  460. gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
  461. }
  462. #elif (SAMC20) || (SAML22)
  463. pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
  464. gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
  465. #else
  466. pm_index = sercom_index + PM_APBCMASK_SERCOM0_Pos;
  467. gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
  468. #endif
  469. /* Turn on module in PM */
  470. #if (SAML21) || (SAMR30)
  471. if (sercom_index == 5) {
  472. # ifdef ID_SERCOM5
  473. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBD, 1 << pm_index);
  474. # else
  475. return STATUS_ERR_INVALID_ARG;
  476. # endif
  477. } else {
  478. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, 1 << pm_index);
  479. }
  480. #else
  481. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, 1 << pm_index);
  482. #endif
  483. /* Set up the GCLK for the module */
  484. struct system_gclk_chan_config gclk_chan_conf;
  485. system_gclk_chan_get_config_defaults(&gclk_chan_conf);
  486. gclk_chan_conf.source_generator = config->generator_source;
  487. system_gclk_chan_set_config(gclk_index, &gclk_chan_conf);
  488. system_gclk_chan_enable(gclk_index);
  489. sercom_set_gclk_generator(config->generator_source, false);
  490. # if CONF_SPI_MASTER_ENABLE == true
  491. if (config->mode == SPI_MODE_MASTER) {
  492. /* Set the SERCOM in SPI master mode */
  493. spi_module->CTRLA.reg |= SERCOM_SPI_CTRLA_MODE(0x3);
  494. }
  495. # endif
  496. # if CONF_SPI_SLAVE_ENABLE == true
  497. if (config->mode == SPI_MODE_SLAVE) {
  498. /* Set the SERCOM in SPI slave mode */
  499. spi_module->CTRLA.reg |= SERCOM_SPI_CTRLA_MODE(0x2);
  500. }
  501. # endif
  502. #if SPI_CALLBACK_MODE == true
  503. /* Temporary variables */
  504. uint8_t i;
  505. uint8_t instance_index;
  506. /* Initialize parameters */
  507. for (i = 0; i < SPI_CALLBACK_N; i++) {
  508. module->callback[i] = NULL;
  509. }
  510. module->tx_buffer_ptr = NULL;
  511. module->rx_buffer_ptr = NULL;
  512. module->remaining_tx_buffer_length = 0x0000;
  513. module->remaining_rx_buffer_length = 0x0000;
  514. module->registered_callback = 0x00;
  515. module->enabled_callback = 0x00;
  516. module->status = STATUS_OK;
  517. module->dir = SPI_DIRECTION_IDLE;
  518. module->locked = false;
  519. /*
  520. * Set interrupt handler and register SPI software module struct in
  521. * look-up table
  522. */
  523. instance_index = _sercom_get_sercom_inst_index(module->hw);
  524. _sercom_set_handler(instance_index, _spi_interrupt_handler);
  525. _sercom_instances[instance_index] = module;
  526. #endif
  527. /* Write configuration to module and return status code */
  528. return _spi_set_config(module, config);
  529. }
  530. /**
  531. * \brief Reads buffer of \c length SPI characters
  532. *
  533. * This function will read a buffer of data from an SPI peripheral by sending
  534. * dummy SPI character if in master mode, or by waiting for data in slave mode.
  535. *
  536. * \note If address matching is enabled for the slave, the first character
  537. * received and placed in the buffer will be the address.
  538. *
  539. * \param[in] module Pointer to the software instance struct
  540. * \param[out] rx_data Data buffer for received data
  541. * \param[in] length Length of data to receive
  542. * \param[in] dummy 8- or 9-bit dummy byte to shift out in master mode
  543. *
  544. * \return Status of the read operation.
  545. * \retval STATUS_OK If the read was completed
  546. * \retval STATUS_ABORTED If transaction was ended by master before
  547. * the entire buffer was transferred
  548. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  549. * \retval STATUS_ERR_TIMEOUT If the operation was not completed within the
  550. * timeout in slave mode
  551. * \retval STATUS_ERR_DENIED If the receiver is not enabled
  552. * \retval STATUS_ERR_OVERFLOW If the data is overflown
  553. */
  554. enum status_code spi_read_buffer_wait(
  555. struct spi_module *const module,
  556. uint8_t *rx_data,
  557. uint16_t length,
  558. uint16_t dummy)
  559. {
  560. /* Sanity check arguments */
  561. Assert(module);
  562. Assert(module->hw);
  563. # if SPI_CALLBACK_MODE == true
  564. if (module->status == STATUS_BUSY) {
  565. /* Check if the SPI module is busy with a job */
  566. return STATUS_BUSY;
  567. }
  568. # endif
  569. /* Sanity check arguments */
  570. if (length == 0) {
  571. return STATUS_ERR_INVALID_ARG;
  572. }
  573. if (!(module->receiver_enabled)) {
  574. return STATUS_ERR_DENIED;
  575. }
  576. # if CONF_SPI_SLAVE_ENABLE == true
  577. if ((module->mode == SPI_MODE_SLAVE) && (spi_is_write_complete(module))) {
  578. /* Clear TX complete flag */
  579. _spi_clear_tx_complete_flag(module);
  580. }
  581. # endif
  582. uint16_t rx_pos = 0;
  583. while (length--) {
  584. # if CONF_SPI_MASTER_ENABLE == true
  585. if (module->mode == SPI_MODE_MASTER) {
  586. /* Wait until the module is ready to write a character */
  587. while (!spi_is_ready_to_write(module)) {
  588. }
  589. /* Send dummy SPI character to read in master mode */
  590. spi_write(module, dummy);
  591. }
  592. # endif
  593. # if CONF_SPI_SLAVE_ENABLE == true
  594. /* Start timeout period for slave */
  595. if (module->mode == SPI_MODE_SLAVE) {
  596. for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
  597. if (spi_is_ready_to_read(module)) {
  598. break;
  599. }
  600. }
  601. /* Check if master has ended the transaction */
  602. if (spi_is_write_complete(module)) {
  603. _spi_clear_tx_complete_flag(module);
  604. return STATUS_ABORTED;
  605. }
  606. if (!spi_is_ready_to_read(module)) {
  607. /* Not ready to read data within timeout period */
  608. return STATUS_ERR_TIMEOUT;
  609. }
  610. }
  611. # endif
  612. /* Wait until the module is ready to read a character */
  613. while (!spi_is_ready_to_read(module)) {
  614. }
  615. uint16_t received_data = 0;
  616. enum status_code retval = spi_read(module, &received_data);
  617. if (retval != STATUS_OK) {
  618. /* Overflow, abort */
  619. return retval;
  620. }
  621. /* Read value will be at least 8-bits long */
  622. rx_data[rx_pos++] = received_data;
  623. /* If 9-bit data, write next received byte to the buffer */
  624. if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
  625. rx_data[rx_pos++] = (received_data >> 8);
  626. }
  627. }
  628. return STATUS_OK;
  629. }
  630. /**
  631. * \brief Sends and reads a single SPI character
  632. *
  633. * This function will transfer a single SPI character via SPI and return the
  634. * SPI character that is shifted into the shift register.
  635. *
  636. * In master mode the SPI character will be sent immediately and the received
  637. * SPI character will be read as soon as the shifting of the data is
  638. * complete.
  639. *
  640. * In slave mode this function will place the data to be sent into the transmit
  641. * buffer. It will then block until an SPI master has shifted a complete
  642. * SPI character, and the received data is available.
  643. *
  644. * \note The data to be sent might not be sent before the next transfer, as
  645. * loading of the shift register is dependent on SCK.
  646. * \note If address matching is enabled for the slave, the first character
  647. * received and placed in the buffer will be the address.
  648. *
  649. * \param[in] module Pointer to the software instance struct
  650. * \param[in] tx_data SPI character to transmit
  651. * \param[out] rx_data Pointer to store the received SPI character
  652. *
  653. * \return Status of the operation.
  654. * \retval STATUS_OK If the operation was completed
  655. * \retval STATUS_ERR_TIMEOUT If the operation was not completed within the
  656. * timeout in slave mode
  657. * \retval STATUS_ERR_DENIED If the receiver is not enabled
  658. * \retval STATUS_ERR_OVERFLOW If the incoming data is overflown
  659. */
  660. enum status_code spi_transceive_wait(
  661. struct spi_module *const module,
  662. uint16_t tx_data,
  663. uint16_t *rx_data)
  664. {
  665. /* Sanity check arguments */
  666. Assert(module);
  667. if (!(module->receiver_enabled)) {
  668. return STATUS_ERR_DENIED;
  669. }
  670. # if SPI_CALLBACK_MODE == true
  671. if (module->status == STATUS_BUSY) {
  672. /* Check if the SPI module is busy with a job */
  673. return STATUS_BUSY;
  674. }
  675. # endif
  676. # if CONF_SPI_SLAVE_ENABLE == true
  677. uint16_t j;
  678. # endif
  679. enum status_code retval = STATUS_OK;
  680. # if CONF_SPI_SLAVE_ENABLE == true
  681. /* Start timeout period for slave */
  682. if (module->mode == SPI_MODE_SLAVE) {
  683. for (j = 0; j <= SPI_TIMEOUT; j++) {
  684. if (spi_is_ready_to_write(module)) {
  685. break;
  686. } else if (j == SPI_TIMEOUT) {
  687. /* Not ready to write data within timeout period */
  688. return STATUS_ERR_TIMEOUT;
  689. }
  690. }
  691. }
  692. # endif
  693. /* Wait until the module is ready to write the character */
  694. while (!spi_is_ready_to_write(module)) {
  695. }
  696. /* Write data */
  697. spi_write(module, tx_data);
  698. # if CONF_SPI_SLAVE_ENABLE == true
  699. /* Start timeout period for slave */
  700. if (module->mode == SPI_MODE_SLAVE) {
  701. for (j = 0; j <= SPI_TIMEOUT; j++) {
  702. if (spi_is_ready_to_read(module)) {
  703. break;
  704. } else if (j == SPI_TIMEOUT) {
  705. /* Not ready to read data within timeout period */
  706. return STATUS_ERR_TIMEOUT;
  707. }
  708. }
  709. }
  710. # endif
  711. /* Wait until the module is ready to read the character */
  712. while (!spi_is_ready_to_read(module)) {
  713. }
  714. /* Read data */
  715. retval = spi_read(module, rx_data);
  716. return retval;
  717. }
  718. /**
  719. * \brief Selects slave device
  720. *
  721. * This function will drive the slave select pin of the selected device low or
  722. * high depending on the select Boolean.
  723. * If slave address recognition is enabled, the address will be sent to the
  724. * slave when selecting it.
  725. *
  726. * \param[in] module Pointer to the software module struct
  727. * \param[in] slave Pointer to the attached slave
  728. * \param[in] select Boolean stating if the slave should be selected or
  729. * deselected
  730. *
  731. * \return Status of the operation.
  732. * \retval STATUS_OK If the slave device was selected
  733. * \retval STATUS_ERR_UNSUPPORTED_DEV If the SPI module is operating in slave
  734. * mode
  735. * \retval STATUS_BUSY If the SPI module is not ready to write
  736. * the slave address
  737. */
  738. enum status_code spi_select_slave(
  739. struct spi_module *const module,
  740. struct spi_slave_inst *const slave,
  741. const bool select)
  742. {
  743. /* Sanity check arguments */
  744. Assert(module);
  745. Assert(module->hw);
  746. Assert(slave);
  747. /* Check that the SPI module is operating in master mode */
  748. if (module->mode != SPI_MODE_MASTER) {
  749. return STATUS_ERR_UNSUPPORTED_DEV;
  750. }
  751. # ifdef FEATURE_SPI_HARDWARE_SLAVE_SELECT
  752. if(!(module->master_slave_select_enable))
  753. # endif
  754. {
  755. if (select) {
  756. /* Check if address recognition is enabled */
  757. if (slave->address_enabled) {
  758. /* Check if the module is ready to write the address */
  759. if (!spi_is_ready_to_write(module)) {
  760. /* Not ready, do not select slave and return */
  761. port_pin_set_output_level(slave->ss_pin, true);
  762. return STATUS_BUSY;
  763. }
  764. /* Drive Slave Select low */
  765. port_pin_set_output_level(slave->ss_pin, false);
  766. /* Write address to slave */
  767. spi_write(module, slave->address);
  768. if (!(module->receiver_enabled)) {
  769. /* Flush contents of shift register shifted back from slave */
  770. while (!spi_is_ready_to_read(module)) {
  771. }
  772. uint16_t flush = 0;
  773. spi_read(module, &flush);
  774. }
  775. } else {
  776. /* Drive Slave Select low */
  777. port_pin_set_output_level(slave->ss_pin, false);
  778. }
  779. } else {
  780. /* Drive Slave Select high */
  781. port_pin_set_output_level(slave->ss_pin, true);
  782. }
  783. }
  784. return STATUS_OK;
  785. }
  786. /**
  787. * \brief Sends a buffer of \c length SPI characters
  788. *
  789. * This function will send a buffer of SPI characters via the SPI
  790. * and discard any data that is received. To both send and receive a buffer of
  791. * data, use the \ref spi_transceive_buffer_wait function.
  792. *
  793. * Note that this function does not handle the _SS (slave select) pin(s) in
  794. * master mode; this must be handled by the user application.
  795. *
  796. * \param[in] module Pointer to the software instance struct
  797. * \param[in] tx_data Pointer to the buffer to transmit
  798. * \param[in] length Number of SPI characters to transfer
  799. *
  800. * \return Status of the write operation.
  801. * \retval STATUS_OK If the write was completed
  802. * \retval STATUS_ABORTED If transaction was ended by master before
  803. * entire buffer was transferred
  804. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  805. * \retval STATUS_ERR_TIMEOUT If the operation was not completed within the
  806. * timeout in slave mode
  807. */
  808. enum status_code spi_write_buffer_wait(
  809. struct spi_module *const module,
  810. const uint8_t *tx_data,
  811. uint16_t length)
  812. {
  813. /* Sanity check arguments */
  814. Assert(module);
  815. # if SPI_CALLBACK_MODE == true
  816. if (module->status == STATUS_BUSY) {
  817. /* Check if the SPI module is busy with a job */
  818. return STATUS_BUSY;
  819. }
  820. # endif
  821. if (length == 0) {
  822. return STATUS_ERR_INVALID_ARG;
  823. }
  824. # if CONF_SPI_SLAVE_ENABLE == true
  825. if ((module->mode == SPI_MODE_SLAVE) && (spi_is_write_complete(module))) {
  826. /* Clear TX complete flag */
  827. _spi_clear_tx_complete_flag(module);
  828. }
  829. # endif
  830. uint16_t tx_pos = 0;
  831. uint16_t flush_length = length;
  832. /* Write block */
  833. while (length--) {
  834. # if CONF_SPI_SLAVE_ENABLE == true
  835. /* Start timeout period for slave */
  836. if (module->mode == SPI_MODE_SLAVE) {
  837. for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
  838. if (spi_is_ready_to_write(module)) {
  839. break;
  840. }
  841. }
  842. /* Check if master has ended the transaction */
  843. if (spi_is_write_complete(module)) {
  844. _spi_clear_tx_complete_flag(module);
  845. return STATUS_ABORTED;
  846. }
  847. if (!spi_is_ready_to_write(module)) {
  848. /* Not ready to write data within timeout period */
  849. return STATUS_ERR_TIMEOUT;
  850. }
  851. }
  852. # endif
  853. /* Wait until the module is ready to write a character */
  854. while (!spi_is_ready_to_write(module)) {
  855. }
  856. /* Write value will be at least 8-bits long */
  857. uint16_t data_to_send = tx_data[tx_pos++];
  858. /* If 9-bit data, get next byte to send from the buffer */
  859. if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
  860. data_to_send |= (tx_data[tx_pos++] << 8);
  861. }
  862. /* Write the data to send */
  863. spi_write(module, data_to_send);
  864. if (module->receiver_enabled) {
  865. # if CONF_SPI_SLAVE_ENABLE == true
  866. /* Start timeout period for slave */
  867. if (module->mode == SPI_MODE_SLAVE) {
  868. for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
  869. if (length && spi_is_ready_to_write(module)) {
  870. data_to_send = tx_data[tx_pos++];
  871. /* If 9-bit data, get next byte to send from the buffer */
  872. if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
  873. data_to_send |= (tx_data[tx_pos++] << 8);
  874. }
  875. /* Write the data to send */
  876. spi_write(module, data_to_send);
  877. length--;
  878. }
  879. if (spi_is_ready_to_read(module)) {
  880. break;
  881. }
  882. }
  883. /* Check if master has ended the transaction */
  884. if (spi_is_write_complete(module)) {
  885. _spi_clear_tx_complete_flag(module);
  886. return STATUS_ABORTED;
  887. }
  888. if (!spi_is_ready_to_read(module)) {
  889. /* Not ready to read data within timeout period */
  890. return STATUS_ERR_TIMEOUT;
  891. }
  892. }
  893. # endif
  894. while (!spi_is_ready_to_read(module)) {
  895. }
  896. /* Flush read buffer */
  897. uint16_t flush;
  898. spi_read(module, &flush);
  899. flush_length--;
  900. }
  901. }
  902. # if CONF_SPI_MASTER_ENABLE == true
  903. if (module->mode == SPI_MODE_MASTER) {
  904. /* Wait for last byte to be transferred */
  905. while (!spi_is_write_complete(module)) {
  906. }
  907. }
  908. # endif
  909. # if CONF_SPI_SLAVE_ENABLE == true
  910. if (module->mode == SPI_MODE_SLAVE) {
  911. if (module->receiver_enabled) {
  912. while (flush_length) {
  913. /* Start timeout period for slave */
  914. for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
  915. if (spi_is_ready_to_read(module)) {
  916. break;
  917. }
  918. }
  919. if (!spi_is_ready_to_read(module)) {
  920. /* Not ready to read data within timeout period */
  921. return STATUS_ERR_TIMEOUT;
  922. }
  923. /* Flush read buffer */
  924. uint16_t flush;
  925. spi_read(module, &flush);
  926. flush_length--;
  927. }
  928. }
  929. }
  930. # endif
  931. return STATUS_OK;
  932. }
  933. /**
  934. * \brief Sends and receives a buffer of \c length SPI characters
  935. *
  936. * This function will send and receive a buffer of data via the SPI.
  937. *
  938. * In master mode the SPI characters will be sent immediately and the
  939. * received SPI character will be read as soon as the shifting of the SPI
  940. * character is complete.
  941. *
  942. * In slave mode this function will place the data to be sent into the transmit
  943. * buffer. It will then block until an SPI master has shifted the complete
  944. * buffer and the received data is available.
  945. *
  946. * \param[in] module Pointer to the software instance struct
  947. * \param[in] tx_data Pointer to the buffer to transmit
  948. * \param[out] rx_data Pointer to the buffer where received data will be stored
  949. * \param[in] length Number of SPI characters to transfer
  950. *
  951. * \return Status of the operation.
  952. * \retval STATUS_OK If the operation was completed
  953. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
  954. * \retval STATUS_ERR_TIMEOUT If the operation was not completed within the
  955. * timeout in slave mode
  956. * \retval STATUS_ERR_DENIED If the receiver is not enabled
  957. * \retval STATUS_ERR_OVERFLOW If the data is overflown
  958. */
  959. enum status_code spi_transceive_buffer_wait(
  960. struct spi_module *const module,
  961. uint8_t *tx_data,
  962. uint8_t *rx_data,
  963. uint16_t length)
  964. {
  965. /* Sanity check arguments */
  966. Assert(module);
  967. # if SPI_CALLBACK_MODE == true
  968. if (module->status == STATUS_BUSY) {
  969. /* Check if the SPI module is busy with a job */
  970. return STATUS_BUSY;
  971. }
  972. # endif
  973. /* Sanity check arguments */
  974. if (length == 0) {
  975. return STATUS_ERR_INVALID_ARG;
  976. }
  977. if (!(module->receiver_enabled)) {
  978. return STATUS_ERR_DENIED;
  979. }
  980. # if CONF_SPI_SLAVE_ENABLE == true
  981. if ((module->mode == SPI_MODE_SLAVE) && (spi_is_write_complete(module))) {
  982. /* Clear TX complete flag */
  983. _spi_clear_tx_complete_flag(module);
  984. }
  985. # endif
  986. uint16_t tx_pos = 0;
  987. uint16_t rx_pos = 0;
  988. uint16_t rx_length = length;
  989. /* Send and receive buffer */
  990. while (length--) {
  991. # if CONF_SPI_SLAVE_ENABLE == true
  992. /* Start timeout period for slave */
  993. if (module->mode == SPI_MODE_SLAVE) {
  994. for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
  995. if (spi_is_ready_to_write(module)) {
  996. break;
  997. }
  998. }
  999. /* Check if master has ended the transaction */
  1000. if (spi_is_write_complete(module)) {
  1001. _spi_clear_tx_complete_flag(module);
  1002. return STATUS_ABORTED;
  1003. }
  1004. if (!spi_is_ready_to_write(module)) {
  1005. /* Not ready to write data within timeout period */
  1006. return STATUS_ERR_TIMEOUT;
  1007. }
  1008. }
  1009. # endif
  1010. /* Wait until the module is ready to write a character */
  1011. while (!spi_is_ready_to_write(module)) {
  1012. }
  1013. /* Write value will be at least 8-bits long */
  1014. uint16_t data_to_send = tx_data[tx_pos++];
  1015. /* If 9-bit data, get next byte to send from the buffer */
  1016. if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
  1017. data_to_send |= (tx_data[tx_pos++] << 8);
  1018. }
  1019. /* Write the data to send */
  1020. spi_write(module, data_to_send);
  1021. # if CONF_SPI_SLAVE_ENABLE == true
  1022. /* Start timeout period for slave */
  1023. if (module->mode == SPI_MODE_SLAVE) {
  1024. for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
  1025. if (spi_is_ready_to_write(module)) {
  1026. data_to_send = tx_data[tx_pos++];
  1027. /* If 9-bit data, get next byte to send from the buffer */
  1028. if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
  1029. data_to_send |= (tx_data[tx_pos++] << 8);
  1030. }
  1031. /* Write the data to send */
  1032. spi_write(module, data_to_send);
  1033. length--;
  1034. }
  1035. if (spi_is_ready_to_read(module)) {
  1036. break;
  1037. }
  1038. }
  1039. /* Check if master has ended the transaction */
  1040. if (spi_is_write_complete(module)) {
  1041. _spi_clear_tx_complete_flag(module);
  1042. return STATUS_ABORTED;
  1043. }
  1044. if (!spi_is_ready_to_read(module)) {
  1045. /* Not ready to read data within timeout period */
  1046. return STATUS_ERR_TIMEOUT;
  1047. }
  1048. }
  1049. # endif
  1050. /* Wait until the module is ready to read a character */
  1051. while (!spi_is_ready_to_read(module)) {
  1052. }
  1053. enum status_code retval;
  1054. uint16_t received_data = 0;
  1055. rx_length--;
  1056. retval = spi_read(module, &received_data);
  1057. if (retval != STATUS_OK) {
  1058. /* Overflow, abort */
  1059. return retval;
  1060. }
  1061. /* Read value will be at least 8-bits long */
  1062. rx_data[rx_pos++] = received_data;
  1063. /* If 9-bit data, write next received byte to the buffer */
  1064. if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
  1065. rx_data[rx_pos++] = (received_data >> 8);
  1066. }
  1067. }
  1068. # if CONF_SPI_MASTER_ENABLE == true
  1069. if (module->mode == SPI_MODE_MASTER) {
  1070. /* Wait for last byte to be transferred */
  1071. while (!spi_is_write_complete(module)) {
  1072. }
  1073. }
  1074. # endif
  1075. # if CONF_SPI_SLAVE_ENABLE == true
  1076. if (module->mode == SPI_MODE_SLAVE) {
  1077. while (rx_length) {
  1078. /* Start timeout period for slave */
  1079. for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
  1080. if (spi_is_ready_to_read(module)) {
  1081. break;
  1082. }
  1083. }
  1084. if (!spi_is_ready_to_read(module)) {
  1085. /* Not ready to read data within timeout period */
  1086. return STATUS_ERR_TIMEOUT;
  1087. }
  1088. enum status_code retval;
  1089. uint16_t received_data = 0;
  1090. rx_length--;
  1091. retval = spi_read(module, &received_data);
  1092. if (retval != STATUS_OK) {
  1093. /* Overflow, abort */
  1094. return retval;
  1095. }
  1096. /* Read value will be at least 8-bits long */
  1097. rx_data[rx_pos++] = received_data;
  1098. /* If 9-bit data, write next received byte to the buffer */
  1099. if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
  1100. rx_data[rx_pos++] = (received_data >> 8);
  1101. }
  1102. }
  1103. }
  1104. # endif
  1105. return STATUS_OK;
  1106. }