ft80x_spi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /****************************************************************************
  2. * drivers/lcd/ft80x_spi.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <inttypes.h>
  25. #include <unistd.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <debug.h>
  29. #include <nuttx/kmalloc.h>
  30. #include <nuttx/lcd/ft80x.h>
  31. #include <nuttx/spi/spi.h>
  32. #include "ft80x.h"
  33. #if defined(CONFIG_LCD_FT80X) && defined(CONFIG_LCD_FT80X_SPI)
  34. /****************************************************************************
  35. * Private Functions
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Name: ft80x_select
  39. *
  40. * Description:
  41. * Select the FT80X part
  42. *
  43. ****************************************************************************/
  44. static void ft80x_select(FAR struct ft80x_dev_s *priv)
  45. {
  46. lcdinfo("Mode: %d Bits: 8 Frequency: %" PRId32 "\n",
  47. SPIDEV_MODE0, priv->frequency);
  48. DEBUGASSERT(priv != NULL);
  49. /* Lock the SPI bus */
  50. SPI_LOCK(priv->spi, true);
  51. /* Configure SPI for the FT80X */
  52. SPI_SETMODE(priv->spi, SPIDEV_MODE0);
  53. SPI_SETBITS(priv->spi, 8);
  54. SPI_HWFEATURES(priv->spi, 0);
  55. SPI_SETFREQUENCY(priv->spi, priv->frequency);
  56. /* Select SPI device */
  57. SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), true);
  58. }
  59. /****************************************************************************
  60. * Name: ft80x_deselect
  61. *
  62. * Description:
  63. * De-select the FT80X part
  64. *
  65. ****************************************************************************/
  66. static void ft80x_deselect(FAR struct ft80x_dev_s *priv)
  67. {
  68. /* Des-select the FT80x device */
  69. SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), false);
  70. /* Unlock bus */
  71. SPI_LOCK(priv->spi, false);
  72. }
  73. /****************************************************************************
  74. * Public Functions
  75. ****************************************************************************/
  76. /****************************************************************************
  77. * Name: ft80x_host_command
  78. *
  79. * Description:
  80. * Send a host command to the FT80x
  81. *
  82. * For an SPI write command write transaction, the host writes a zero bit
  83. * followed by a one bit, followed by the 5-bit command, followed by two
  84. * bytes of zero. All data is streamed with a single chip select.
  85. *
  86. * NOTE: Commands are defined in ft80x.h with bit 7 = 0 and bit 6 = 1.
  87. *
  88. ****************************************************************************/
  89. void ft80x_host_command(FAR struct ft80x_dev_s *priv, uint8_t cmd)
  90. {
  91. struct ft80x_hostwrite_s hostwrite;
  92. DEBUGASSERT(priv != NULL && (cmd == 0x00 || (cmd & 0xc0) == 0x40));
  93. /* Format the host write command */
  94. hostwrite.cmd = cmd;
  95. hostwrite.pad1 = 0;
  96. hostwrite.pad2 = 0;
  97. /* Select the FT80x */
  98. ft80x_select(priv);
  99. /* Send the host write command */
  100. SPI_SNDBLOCK(priv->spi, &hostwrite, sizeof(struct ft80x_hostwrite_s));
  101. /* De-select the FT80x */
  102. ft80x_deselect(priv);
  103. }
  104. /****************************************************************************
  105. * Name: ft80x_read_memory
  106. *
  107. * Description:
  108. * Read from FT80X memory
  109. *
  110. * For SPI memory read transaction, the host sends two zero bits, followed
  111. * by the 22-bit address. This is followed by a dummy byte. After the dummy
  112. * byte, the FT80x responds to each host byte with read data bytes.
  113. *
  114. ****************************************************************************/
  115. void ft80x_read_memory(FAR struct ft80x_dev_s *priv, uint32_t addr,
  116. FAR void *buffer, size_t buflen)
  117. {
  118. struct ft80x_spiread_s spiread;
  119. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0 &&
  120. buffer != NULL && buflen > 0);
  121. /* Format the read header */
  122. spiread.addrh = (addr >> 16) & 0x3f;
  123. spiread.addrm = (addr >> 8) & 0xff;
  124. spiread.addrl = addr & 0xff;
  125. spiread.dummy = 0xff;
  126. /* Select the FT80x */
  127. ft80x_select(priv);
  128. /* Send the read header */
  129. SPI_SNDBLOCK(priv->spi, &spiread, sizeof(struct ft80x_spiread_s));
  130. /* Then read the FT80x memory into the user provided buffer */
  131. SPI_RECVBLOCK(priv->spi, buffer, buflen);
  132. /* De-select the FT80x */
  133. ft80x_deselect(priv);
  134. }
  135. /****************************************************************************
  136. * Name: ft80x_read_byte, ft80x_read_hword, ft80x_read_word
  137. *
  138. * Description:
  139. * Read an 8-, 16-, or 32-bt bit value from FT80X memory
  140. *
  141. * For SPI memory read transaction, the host sends two zero bits, followed
  142. * by the 22-bit address. This is followed by a dummy byte. After the dummy
  143. * byte, the FT80x responds to each host byte with read data bytes.
  144. *
  145. ****************************************************************************/
  146. uint8_t ft80x_read_byte(FAR struct ft80x_dev_s *priv, uint32_t addr)
  147. {
  148. uint8_t data;
  149. ft80x_read_memory(priv, addr, (FAR void *)&data, 1);
  150. return data;
  151. }
  152. uint16_t ft80x_read_hword(FAR struct ft80x_dev_s *priv, uint32_t addr)
  153. {
  154. uint16_t data;
  155. ft80x_read_memory(priv, addr, (FAR void *)&data, 2);
  156. return data;
  157. }
  158. uint32_t ft80x_read_word(FAR struct ft80x_dev_s *priv, uint32_t addr)
  159. {
  160. uint32_t data;
  161. ft80x_read_memory(priv, addr, (FAR void *)&data, 4);
  162. return data;
  163. }
  164. /****************************************************************************
  165. * Name: ft80x_write_memory
  166. *
  167. * Description:
  168. * Write to FT80X memory
  169. *
  170. * For SPI memory write transaction, the host sends a '1' bit and '0' bit,
  171. * followed by the 22-bit address. This is followed by the write data.
  172. *
  173. ****************************************************************************/
  174. void ft80x_write_memory(FAR struct ft80x_dev_s *priv, uint32_t addr,
  175. FAR const void *buffer, size_t buflen)
  176. {
  177. struct ft80x_spiwrite_s spiwrite;
  178. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0 &&
  179. buffer != NULL && buflen > 0);
  180. /* Format the write header */
  181. spiwrite.addrh = 0x80 | ((addr >> 16) & 0x3f);
  182. spiwrite.addrm = (addr >> 8) & 0xff;
  183. spiwrite.addrl = addr & 0xff;
  184. /* Select the FT80x */
  185. ft80x_select(priv);
  186. /* Send the write header */
  187. SPI_SNDBLOCK(priv->spi, &spiwrite, sizeof(struct ft80x_spiwrite_s));
  188. /* Then write to the FT80x memory from the user provided buffer */
  189. SPI_SNDBLOCK(priv->spi, buffer, buflen);
  190. /* De-select the FT80x */
  191. ft80x_deselect(priv);
  192. }
  193. /****************************************************************************
  194. * Name: ft80x_write_byte, ft80x_write_hword, ft80x_write_word
  195. *
  196. * Description:
  197. * Write an 8-, 16-, or 32-bt bit value to FT80X memory
  198. *
  199. * For SPI memory write transaction, the host sends a '1' bit and '0' bit,
  200. * followed by the 22-bit address. This is followed by the write data.
  201. *
  202. ****************************************************************************/
  203. void ft80x_write_byte(FAR struct ft80x_dev_s *priv, uint32_t addr,
  204. uint8_t data)
  205. {
  206. struct ft80x_spiwrite8_s spiwrite;
  207. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0);
  208. /* Format the write header */
  209. spiwrite.addrh = 0x80 | ((addr >> 16) & 0x3f);
  210. spiwrite.addrm = (addr >> 8) & 0xff;
  211. spiwrite.addrl = addr & 0xff;
  212. spiwrite.data = data;
  213. /* Select the FT80x */
  214. ft80x_select(priv);
  215. /* Send the write header and 8-bit data */
  216. SPI_SNDBLOCK(priv->spi, &spiwrite, sizeof(struct ft80x_spiwrite8_s));
  217. /* De-select the FT80x */
  218. ft80x_deselect(priv);
  219. }
  220. void ft80x_write_hword(FAR struct ft80x_dev_s *priv, uint32_t addr,
  221. uint16_t data)
  222. {
  223. struct ft80x_spiwrite16_s spiwrite;
  224. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0);
  225. /* Format the write header */
  226. spiwrite.addrh = 0x80 | ((addr >> 16) & 0x3f);
  227. spiwrite.addrm = (addr >> 8) & 0xff;
  228. spiwrite.addrl = addr & 0xff;
  229. spiwrite.data[0] = data & 0xff; /* Little endian */
  230. spiwrite.data[1] = (data >> 8) & 0xff;
  231. /* Select the FT80x */
  232. ft80x_select(priv);
  233. /* Send the write header and 16-bit data */
  234. SPI_SNDBLOCK(priv->spi, &spiwrite, sizeof(struct ft80x_spiwrite16_s));
  235. /* De-select the FT80x */
  236. ft80x_deselect(priv);
  237. }
  238. void ft80x_write_word(FAR struct ft80x_dev_s *priv, uint32_t addr,
  239. uint32_t data)
  240. {
  241. struct ft80x_spiwrite32_s spiwrite;
  242. DEBUGASSERT(priv != NULL && (addr & 0xffc00000) == 0);
  243. /* Format the write header */
  244. spiwrite.addrh = 0x80 | ((addr >> 16) & 0x3f);
  245. spiwrite.addrm = (addr >> 8) & 0xff;
  246. spiwrite.addrl = addr & 0xff;
  247. spiwrite.data[0] = data & 0xff; /* Little endian */
  248. spiwrite.data[1] = (data >> 8) & 0xff;
  249. spiwrite.data[2] = (data >> 16) & 0xff;
  250. spiwrite.data[3] = (data >> 24) & 0xff;
  251. /* Select the FT80x */
  252. ft80x_select(priv);
  253. /* Send the write header and 32-bit data */
  254. SPI_SNDBLOCK(priv->spi, &spiwrite, sizeof(struct ft80x_spiwrite32_s));
  255. /* De-select the FT80x */
  256. ft80x_deselect(priv);
  257. }
  258. #endif /* CONFIG_LCD_FT80X && CONFIG_LCD_FT80X_SPI */