fifo_5410x.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * @brief LPC5410X System FIFO chip driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include "chip.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /** SPI FIFO read FIFO statuses */
  36. #define LPC_SPIRXFIFO_STAT_SSEL0N (1 << 16) /*!< Slave select for receive on SSEL0 (active low) */
  37. #define LPC_SPIRXFIFO_STAT_SSEL1N (1 << 17) /*!< Slave select for receive on SSEL1 (active low) */
  38. #define LPC_SPIRXFIFO_STAT_SSEL2N (1 << 18) /*!< Slave select for receive on SSEL2 (active low) */
  39. #define LPC_SPIRXFIFO_STAT_SSEL3N (1 << 19) /*!< Slave select for receive on SSEL3 (active low) */
  40. #define LPC_SPIRXFIFO_STAT_SOT (1 << 20) /*!< This flag will be 1 if this is the first data after the SSELs went from deasserted to asserted */
  41. /** SPI FIFO write FIFO control */
  42. #define LPC_SPITXFIFO_CTRL_SSEL0N (1 << 16) /*!< Master assert for receive on SSEL0 (active low) */
  43. #define LPC_SPITXFIFO_CTRL_SSEL1N (1 << 17) /*!< Master assert for receive on SSEL1 (active low) */
  44. #define LPC_SPITXFIFO_CTRL_SSEL2N (1 << 18) /*!< Master assert for receive on SSEL2 (active low) */
  45. #define LPC_SPITXFIFO_CTRL_SSEL3N (1 << 19) /*!< Master assert for receive on SSEL3 (active low) */
  46. #define LPC_SPITXFIFO_CTRL_EOT (1 << 20) /*!< End of Transfer. The asserted SSEL will be deasserted at the end of a transfer */
  47. #define LPC_SPITXFIFO_CTRL_EOF (1 << 21) /*!< End of Frame. Between frames, a delay may be inserted, as defined by the FRAME_DELAY value in the DLY register */
  48. #define LPC_SPITXFIFO_CTRL_RXIGNORE (1 << 22) /*!< Receive Ignore. This allows data to be transmitted using the SPI without the need to read unneeded data from the receiver */
  49. #define LPC_SPITXFIFO_CTRL_LEN(n) ((n) << 24) /*!< Data Length. Specifies the data length from 1 to 16 bits ((n-1) encoded) */
  50. /*****************************************************************************
  51. * Public types/enumerations/variables
  52. ****************************************************************************/
  53. /*****************************************************************************
  54. * Private functions
  55. ****************************************************************************/
  56. /*****************************************************************************
  57. * Public functions
  58. ****************************************************************************/
  59. /* Initializes the system FIFO */
  60. void Chip_FIFO_Init(LPC_FIFO_T *pFIFO)
  61. {
  62. Chip_Clock_EnablePeriphClock(SYSCON_CLOCK_FIFO);
  63. Chip_SYSCON_PeriphReset(RESET_FIFO);
  64. }
  65. /* Deinitializes the system FIFO */
  66. void Chip_FIFO_Deinit(LPC_FIFO_T *pFIFO)
  67. {
  68. Chip_Clock_DisablePeriphClock(SYSCON_CLOCK_FIFO);
  69. }
  70. /* Get the FIFO space available for the USART/SPI direction */
  71. uint32_t Chip_FIFO_GetFifoSpace(LPC_FIFO_T *pFIFO, LPC_FIFO_PERIPHID_T periphId, LPC_FIFO_DIR_T dir)
  72. {
  73. uint32_t pcfg;
  74. if (periphId == FIFO_USART) {
  75. pcfg = pFIFO->common.FIFOCTLUSART;
  76. }
  77. else {
  78. pcfg = pFIFO->common.FIFOCTLSPI;
  79. }
  80. if (dir == FIFO_RX) {
  81. pcfg = pcfg >> 16;
  82. }
  83. else {
  84. pcfg = pcfg >> 24;
  85. }
  86. return pcfg & 0xFF;
  87. }
  88. /* Pause a peripheral FIFO */
  89. void Chip_FIFO_PauseFifo(LPC_FIFO_T *pFIFO, LPC_FIFO_PERIPHID_T periphId, LPC_FIFO_DIR_T dir)
  90. {
  91. if (periphId == FIFO_USART) {
  92. if (dir == FIFO_RX) {
  93. pFIFO->common.FIFOCTLUSART |= (1 << 0);
  94. }
  95. else {
  96. pFIFO->common.FIFOCTLUSART |= (1 << 8);
  97. }
  98. }
  99. else {
  100. if (dir == FIFO_RX) {
  101. pFIFO->common.FIFOCTLSPI |= (1 << 0);
  102. }
  103. else {
  104. pFIFO->common.FIFOCTLSPI |= (1 << 8);
  105. }
  106. }
  107. }
  108. /* Unpause a peripheral FIFO */
  109. void Chip_FIFO_UnpauseFifo(LPC_FIFO_T *pFIFO, LPC_FIFO_PERIPHID_T periphId, LPC_FIFO_DIR_T dir)
  110. {
  111. if (periphId == FIFO_USART) {
  112. if (dir == FIFO_RX) {
  113. pFIFO->common.FIFOCTLUSART &= ~(1 << 0);
  114. }
  115. else {
  116. pFIFO->common.FIFOCTLUSART &= ~(1 << 8);
  117. }
  118. }
  119. else {
  120. if (dir == FIFO_RX) {
  121. pFIFO->common.FIFOCTLSPI &= ~(1 << 0);
  122. }
  123. else {
  124. pFIFO->common.FIFOCTLSPI &= ~(1 << 8);
  125. }
  126. }
  127. }
  128. /* Configure a peripheral's FIFO sizes */
  129. void Chip_FIFO_ConfigFifoSize(LPC_FIFO_T *pFIFO, LPC_FIFO_PERIPHID_T periphId, LPC_FIFO_CFGSIZE_T *pSizes)
  130. {
  131. int maxP, i;
  132. uint32_t upDateMask;
  133. volatile uint32_t *updateReg, *pFifoSizes, *pFifoPause;
  134. /* Pause FIFOs */
  135. Chip_FIFO_PauseFifo(LPC_FIFO, periphId, FIFO_RX);
  136. Chip_FIFO_PauseFifo(LPC_FIFO, periphId, FIFO_TX);
  137. /* Maximum peripheral FIFOs supported */
  138. if (periphId == FIFO_USART) {
  139. maxP = LPC_FIFO_USART_MAX;
  140. updateReg = &pFIFO->common.FIFOUPDATEUSART;
  141. upDateMask = 0xF | (0xF << 16);
  142. pFifoSizes = &pFIFO->common.FIFOCFGUSART[0];
  143. pFifoPause = &pFIFO->common.FIFOCTLUSART;
  144. }
  145. else {
  146. maxP = LPC_FIFO_SPI_MAX;
  147. updateReg = &pFIFO->common.FIFOUPDATESPI;
  148. upDateMask = 0x3 | (0x3 << 16);
  149. pFifoSizes = &pFIFO->common.FIFOCFGSPI[0];
  150. pFifoPause = &pFIFO->common.FIFOCTLSPI;
  151. }
  152. /* Wait for FIFO pause */
  153. while ((*pFifoPause & ((1 << 0) | (1 << 8))) != ((1 << 0) | (1 << 8))) {}
  154. /* Update FIFO sizes */
  155. for (i = 0; i < maxP; i++) {
  156. pFifoSizes[i] = ((uint32_t) (pSizes->fifoRXSize[i]) << 0) |
  157. ((uint32_t) (pSizes->fifoTXSize[i]) << 8);
  158. }
  159. /* Update all peripheral FIFO sizes */
  160. *updateReg = upDateMask;
  161. }
  162. /* Configure the USART system FIFO */
  163. void Chip_FIFOUSART_Configure(LPC_FIFO_T *pFIFO, int usartIndex, LPC_FIFO_CFG_T *pUSARTCfg)
  164. {
  165. pFIFO->usart[usartIndex].CFG =
  166. (pUSARTCfg->noTimeoutContWrite << 4) |
  167. (pUSARTCfg->noTimeoutContEmpty << 5) |
  168. (pUSARTCfg->timeoutBase << 8) |
  169. (pUSARTCfg->timeoutValue << 12) |
  170. (pUSARTCfg->rxThreshold << 16) |
  171. (pUSARTCfg->txThreshold << 24);
  172. }
  173. /* Write data to a system FIFO (non-blocking) */
  174. int Chip_FIFOUSART_WriteTX(LPC_FIFO_T *pFIFO, int usartIndex, bool sz8, void *buff, int numData)
  175. {
  176. int datumWritten, sz16;
  177. uint8_t *p8 = (uint8_t *) buff;
  178. uint16_t *p16 = (uint16_t *) buff;
  179. /* Get configured FIFO size to determine write size, limit to buffer size */
  180. sz16 = (pFIFO->usart[usartIndex].STAT >> 24) & 0xFF;
  181. if (sz16 > numData) {
  182. sz16 = numData;
  183. }
  184. datumWritten = sz16;
  185. /* Write from buffer */
  186. while (sz16 > 0) {
  187. if (sz8) {
  188. pFIFO->usart[usartIndex].TXDAT = (uint32_t) *p8;
  189. p8++;
  190. }
  191. else {
  192. pFIFO->usart[usartIndex].TXDAT = (uint32_t) *p16;
  193. p16++;
  194. }
  195. sz16--;
  196. }
  197. return datumWritten;
  198. }
  199. /* Read data from a system FIFO (non-blocking) */
  200. int Chip_FIFOUSART_ReadRX(LPC_FIFO_T *pFIFO, int usartIndex, bool sz8, void *buff, int numData)
  201. {
  202. int datumRead, sz16;
  203. uint8_t *p8 = (uint8_t *) buff;
  204. uint16_t *p16 = (uint16_t *) buff;
  205. /* Get configured FIFO size to determine read size, limit to buffer size */
  206. sz16 = (pFIFO->usart[usartIndex].STAT >> 16) & 0xFF;
  207. if (sz16 > numData) {
  208. sz16 = numData;
  209. }
  210. datumRead = sz16;
  211. /* Read into buffer */
  212. while (sz16 > 0) {
  213. if (sz8) {
  214. *p8 = (uint8_t) (pFIFO->usart[usartIndex].RXDAT & 0xFF);
  215. p8++;
  216. }
  217. else {
  218. *p16 = (uint16_t) (pFIFO->usart[usartIndex].RXDAT & 0x1FF);
  219. p16++;
  220. }
  221. sz16--;
  222. }
  223. return datumRead;
  224. }
  225. /* Read data from a system FIFO with status (non-blocking) */
  226. int Chip_FIFOUSART_ReadRXStatus(LPC_FIFO_T *pFIFO, int usartIndex, uint16_t *buff, int numData)
  227. {
  228. int datumRead, sz16;
  229. uint16_t *p16 = (uint16_t *) buff;
  230. /* Get configured FIFO size to determine read size, limit to buffer size */
  231. sz16 = (pFIFO->usart[usartIndex].STAT >> 16) & 0xFF;
  232. if (sz16 > numData) {
  233. sz16 = numData;
  234. }
  235. datumRead = sz16;
  236. /* Read into buffer */
  237. while (sz16 > 0) {
  238. *p16 = (uint16_t) (pFIFO->usart[usartIndex].RXDATSTAT & 0xFFFF);
  239. p16++;
  240. sz16--;
  241. }
  242. return datumRead;
  243. }
  244. #if 0 /* Sorry, not yet support */
  245. /* Configure the USART system FIFO */
  246. void Chip_FIFOSPI_Configure(LPC_FIFO_T *pFIFO, int spiIndex, LPC_FIFO_CFG_T *pSPICfg)
  247. {
  248. pFIFO->spi[spiIndex].CFG =
  249. (pSPICfg->noTimeoutContWrite << 4) |
  250. (pSPICfg->noTimeoutContEmpty << 5) |
  251. (pSPICfg->timeoutBase << 6) |
  252. (pSPICfg->timeoutValue << 12) |
  253. (pSPICfg->rxThreshold << 16) |
  254. (pSPICfg->txThreshold << 24);
  255. }
  256. /* Start a data transfer (non-blocking) */
  257. void Chip_FIFOSPI_StartTransfer(LPC_FIFO_T *pFIFO, LPC_FIFO_SPICTL_T *pSetupData)
  258. {
  259. pSetupData->start = 1;
  260. Chip_FIFOSPI_Transfer(pFIFO, pSetupData);
  261. }
  262. /* Feed a SPI data transfer (non-blocking) */
  263. void Chip_FIFOSPI_Transfer(LPC_FIFO_T *pFIFO, LPC_FIFO_SPICTL_T *pSetupData)
  264. {
  265. // FIXME - not yet ready
  266. }
  267. #endif