ft80x_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /****************************************************************************
  2. * drivers/lcd/ft80x_spi.c
  3. *
  4. * Copyright (C) 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #include <debug.h>
  42. #include <nuttx/kmalloc.h>
  43. #include <nuttx/lcd/ft80x.h>
  44. #include <nuttx/spi/spi.h>
  45. #include "ft80x.h"
  46. #if defined(CONFIG_LCD_FT80X) && defined(CONFIG_LCD_FT80X_SPI)
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: ft80x_select
  52. *
  53. * Description:
  54. * Select the FT80X part
  55. *
  56. ****************************************************************************/
  57. static void ft80x_select(FAR struct ft80x_dev_s *priv)
  58. {
  59. lcdinfo("Mode: %d Bits: 8 Frequency: %d\n", SPIDEV_MODE0, priv->frequency);
  60. DEBUGASSERT(priv != NULL);
  61. /* Lock the SPI bus */
  62. (void)SPI_LOCK(priv->spi, true);
  63. /* Configure SPI for the FT80X */
  64. SPI_SETMODE(priv->spi, SPIDEV_MODE0);
  65. SPI_SETBITS(priv->spi, 8);
  66. (void)SPI_HWFEATURES(priv->spi, 0);
  67. (void)SPI_SETFREQUENCY(priv->spi, priv->frequency);
  68. /* Select SPI device */
  69. SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), true);
  70. }
  71. /****************************************************************************
  72. * Name: ft80x_deselect
  73. *
  74. * Description:
  75. * De-select the FT80X part
  76. *
  77. ****************************************************************************/
  78. static void ft80x_deselect(FAR struct ft80x_dev_s *priv)
  79. {
  80. /* Des-select the FT80x device */
  81. SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), false);
  82. /* Unlock bus */
  83. (void)SPI_LOCK(priv->spi, false);
  84. }
  85. /****************************************************************************
  86. * Public Functions
  87. ****************************************************************************/
  88. /****************************************************************************
  89. * Name: ft80x_host_command
  90. *
  91. * Description:
  92. * Send a host command to the FT80x
  93. *
  94. * For an SPI write command write transaction, the host writes a zero bit
  95. * followed by a one bit, followed by the 5-bit command, followed by two
  96. * bytes of zero. All data is streamed with a single chip select.
  97. *
  98. * NOTE: Commands are defined in ft80x.h with bit 7 = 0 and bit 6 = 1.
  99. *
  100. ****************************************************************************/
  101. void ft80x_host_command(FAR struct ft80x_dev_s *priv, uint8_t cmd)
  102. {
  103. struct ft80x_hostwrite_s hostwrite;
  104. DEBUGASSERT(priv != NULL && (cmd == 0x00 || (cmd & 0xc0) == 0x40));
  105. /* Format the host write command */
  106. hostwrite.cmd = cmd;
  107. hostwrite.pad1 = 0;
  108. hostwrite.pad2 = 0;
  109. /* Select the FT80x */
  110. ft80x_select(priv);
  111. /* Send the host write command */
  112. SPI_SNDBLOCK(priv->spi, &hostwrite, sizeof(struct ft80x_hostwrite_s));
  113. /* De-select the FT80x */
  114. ft80x_deselect(priv);
  115. }
  116. /****************************************************************************
  117. * Name: ft80x_read_memory
  118. *
  119. * Description:
  120. * Read from FT80X memory
  121. *
  122. * For SPI memory read transaction, the host sends two zero bits, followed
  123. * by the 22-bit address. This is followed by a dummy byte. After the dummy
  124. * byte, the FT80x responds to each host byte with read data bytes.
  125. *
  126. ****************************************************************************/
  127. void ft80x_read_memory(FAR struct ft80x_dev_s *priv, uint32_t addr,
  128. FAR void *buffer, size_t buflen)
  129. {
  130. struct ft80x_spiread_s spiread;
  131. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0 &&
  132. buffer != NULL && buflen > 0);
  133. /* Format the read header */
  134. spiread.addrh = (addr >> 16) & 0x3f;
  135. spiread.addrm = (addr >> 8) & 0xff;
  136. spiread.addrl = addr & 0xff;
  137. spiread.dummy = 0xff;
  138. /* Select the FT80x */
  139. ft80x_select(priv);
  140. /* Send the read header */
  141. SPI_SNDBLOCK(priv->spi, &spiread, sizeof(struct ft80x_spiread_s));
  142. /* Then read the FT80x memory into the user provided buffer */
  143. SPI_RECVBLOCK(priv->spi, buffer, buflen);
  144. /* De-select the FT80x */
  145. ft80x_deselect(priv);
  146. }
  147. /****************************************************************************
  148. * Name: ft80x_read_byte, ft80x_read_hword, ft80x_read_word
  149. *
  150. * Description:
  151. * Read an 8-, 16-, or 32-bt bit value from FT80X memory
  152. *
  153. * For SPI memory read transaction, the host sends two zero bits, followed
  154. * by the 22-bit address. This is followed by a dummy byte. After the dummy
  155. * byte, the FT80x responds to each host byte with read data bytes.
  156. *
  157. ****************************************************************************/
  158. uint8_t ft80x_read_byte(FAR struct ft80x_dev_s *priv, uint32_t addr)
  159. {
  160. uint8_t data;
  161. ft80x_read_memory(priv, addr, (FAR void *)&data, 1);
  162. return data;
  163. }
  164. uint16_t ft80x_read_hword(FAR struct ft80x_dev_s *priv, uint32_t addr)
  165. {
  166. uint16_t data;
  167. ft80x_read_memory(priv, addr, (FAR void *)&data, 2);
  168. return data;
  169. }
  170. uint32_t ft80x_read_word(FAR struct ft80x_dev_s *priv, uint32_t addr)
  171. {
  172. uint32_t data;
  173. ft80x_read_memory(priv, addr, (FAR void *)&data, 4);
  174. return data;
  175. }
  176. /****************************************************************************
  177. * Name: ft80x_write_memory
  178. *
  179. * Description:
  180. * Write to FT80X memory
  181. *
  182. * For SPI memory write transaction, the host sends a '1' bit and '0' bit,
  183. * followed by the 22-bit address. This is followed by the write data.
  184. *
  185. ****************************************************************************/
  186. void ft80x_write_memory(FAR struct ft80x_dev_s *priv, uint32_t addr,
  187. FAR const void *buffer, size_t buflen)
  188. {
  189. struct ft80x_spiwrite_s spiwrite;
  190. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0 &&
  191. buffer != NULL && buflen > 0);
  192. /* Format the write header */
  193. spiwrite.addrh = 0x80 | ((addr >> 16) & 0x3f);
  194. spiwrite.addrm = (addr >> 8) & 0xff;
  195. spiwrite.addrl = addr & 0xff;
  196. /* Select the FT80x */
  197. ft80x_select(priv);
  198. /* Send the write header */
  199. SPI_SNDBLOCK(priv->spi, &spiwrite, sizeof(struct ft80x_spiwrite_s));
  200. /* Then write to the FT80x memory from the user provided buffer */
  201. SPI_SNDBLOCK(priv->spi, buffer, buflen);
  202. /* De-select the FT80x */
  203. ft80x_deselect(priv);
  204. }
  205. /****************************************************************************
  206. * Name: ft80x_write_byte, ft80x_write_hword, ft80x_write_word
  207. *
  208. * Description:
  209. * Write an 8-, 16-, or 32-bt bit value to FT80X memory
  210. *
  211. * For SPI memory write transaction, the host sends a '1' bit and '0' bit,
  212. * followed by the 22-bit address. This is followed by the write data.
  213. *
  214. ****************************************************************************/
  215. void ft80x_write_byte(FAR struct ft80x_dev_s *priv, uint32_t addr,
  216. uint8_t data)
  217. {
  218. struct ft80x_spiwrite8_s spiwrite;
  219. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0);
  220. /* Format the write header */
  221. spiwrite.addrh = 0x80 | ((addr >> 16) & 0x3f);
  222. spiwrite.addrm = (addr >> 8) & 0xff;
  223. spiwrite.addrl = addr & 0xff;
  224. spiwrite.data = data;
  225. /* Select the FT80x */
  226. ft80x_select(priv);
  227. /* Send the write header and 8-bit data */
  228. SPI_SNDBLOCK(priv->spi, &spiwrite, sizeof(struct ft80x_spiwrite8_s));
  229. /* De-select the FT80x */
  230. ft80x_deselect(priv);
  231. }
  232. void ft80x_write_hword(FAR struct ft80x_dev_s *priv, uint32_t addr,
  233. uint16_t data)
  234. {
  235. struct ft80x_spiwrite16_s spiwrite;
  236. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0);
  237. /* Format the write header */
  238. spiwrite.addrh = 0x80 | ((addr >> 16) & 0x3f);
  239. spiwrite.addrm = (addr >> 8) & 0xff;
  240. spiwrite.addrl = addr & 0xff;
  241. spiwrite.data[0] = data & 0xff; /* Little endian */
  242. spiwrite.data[1] = (data >> 8) & 0xff;
  243. /* Select the FT80x */
  244. ft80x_select(priv);
  245. /* Send the write header and 16-bit data */
  246. SPI_SNDBLOCK(priv->spi, &spiwrite, sizeof(struct ft80x_spiwrite16_s));
  247. /* De-select the FT80x */
  248. ft80x_deselect(priv);
  249. }
  250. void ft80x_write_word(FAR struct ft80x_dev_s *priv, uint32_t addr,
  251. uint32_t data)
  252. {
  253. struct ft80x_spiwrite32_s spiwrite;
  254. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0);
  255. /* Format the write header */
  256. spiwrite.addrh = 0x80 | ((addr >> 16) & 0x3f);
  257. spiwrite.addrm = (addr >> 8) & 0xff;
  258. spiwrite.addrl = addr & 0xff;
  259. spiwrite.data[0] = data & 0xff; /* Little endian */
  260. spiwrite.data[1] = (data >> 8) & 0xff;
  261. spiwrite.data[2] = (data >> 16) & 0xff;
  262. spiwrite.data[3] = (data >> 24) & 0xff;
  263. /* Select the FT80x */
  264. ft80x_select(priv);
  265. /* Send the write header and 32-bit data */
  266. SPI_SNDBLOCK(priv->spi, &spiwrite, sizeof(struct ft80x_spiwrite32_s));
  267. /* De-select the FT80x */
  268. ft80x_deselect(priv);
  269. }
  270. #endif /* CONFIG_LCD_FT80X && CONFIG_LCD_FT80X_SPI */