ili9341_spi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-1-16 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(NU_PKG_USING_ILI9341_SPI)
  14. #include <rtdevice.h>
  15. #include <lcd_ili9341.h>
  16. #if !defined(NU_PKG_USING_ILI9341_SPI_CLK_FREQ)
  17. #define NU_PKG_USING_ILI9341_SPI_CLK_FREQ 48000000
  18. #endif
  19. static struct rt_spi_device ili9341_spi_device;
  20. static struct rt_spi_configuration ili9341_cfg =
  21. {
  22. .mode = RT_SPI_MODE_0 | RT_SPI_MSB,
  23. .data_width = 8,
  24. .max_hz = NU_PKG_USING_ILI9341_SPI_CLK_FREQ,
  25. };
  26. static void ili9341_change_datawidth(int data_width)
  27. {
  28. if (ili9341_cfg.data_width != data_width)
  29. {
  30. ili9341_cfg.data_width = data_width;
  31. rt_spi_configure(&ili9341_spi_device, &ili9341_cfg);
  32. }
  33. }
  34. void ili9341_send_cmd(rt_uint8_t cmd)
  35. {
  36. ili9341_change_datawidth(8);
  37. CLR_RS;
  38. rt_spi_transfer(&ili9341_spi_device, (const void *)&cmd, NULL, 1);
  39. SET_RS;
  40. }
  41. void ili9341_send_cmd_parameter(uint8_t data)
  42. {
  43. ili9341_change_datawidth(8);
  44. rt_spi_transfer(&ili9341_spi_device, (const void *)&data, NULL, 1);
  45. }
  46. static void ili9341_write_data_16bit(uint16_t data)
  47. {
  48. ili9341_change_datawidth(16);
  49. rt_spi_transfer(&ili9341_spi_device, (const void *)&data, NULL, 2);
  50. }
  51. void ili9341_send_pixel_data(rt_uint16_t pixel)
  52. {
  53. ili9341_write_data_16bit(pixel);
  54. }
  55. void ili9341_send_pixels(rt_uint16_t *pixels, int len)
  56. {
  57. ili9341_change_datawidth(16);
  58. rt_spi_transfer(&ili9341_spi_device, (const void *)pixels, NULL, len);
  59. }
  60. static rt_err_t ili9341_spi_send_then_recv(struct rt_spi_device *device,
  61. const void *send_buf,
  62. rt_size_t send_length,
  63. void *recv_buf,
  64. rt_size_t recv_length)
  65. {
  66. rt_err_t result;
  67. struct rt_spi_message message;
  68. RT_ASSERT(device != RT_NULL);
  69. RT_ASSERT(device->bus != RT_NULL);
  70. ili9341_change_datawidth(8);
  71. result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  72. if (result == RT_EOK)
  73. {
  74. if (device->bus->owner != device)
  75. {
  76. /* not the same owner as current, re-configure SPI bus */
  77. result = device->bus->ops->configure(device, &device->config);
  78. if (result == RT_EOK)
  79. {
  80. /* set SPI bus owner */
  81. device->bus->owner = device;
  82. }
  83. else
  84. {
  85. /* configure SPI bus failed */
  86. result = -RT_EIO;
  87. goto __exit;
  88. }
  89. }
  90. /* send data */
  91. message.send_buf = send_buf;
  92. message.recv_buf = RT_NULL;
  93. message.length = send_length;
  94. message.cs_take = 1;
  95. message.cs_release = 0;
  96. message.next = RT_NULL;
  97. CLR_RS;
  98. result = device->bus->ops->xfer(device, &message);
  99. SET_RS;
  100. if (result == 0)
  101. {
  102. result = -RT_EIO;
  103. goto __exit;
  104. }
  105. /* recv data */
  106. message.send_buf = RT_NULL;
  107. message.recv_buf = recv_buf;
  108. message.length = recv_length;
  109. message.cs_take = 0;
  110. message.cs_release = 1;
  111. message.next = RT_NULL;
  112. result = device->bus->ops->xfer(device, &message);
  113. if (result == 0)
  114. {
  115. result = -RT_EIO;
  116. goto __exit;
  117. }
  118. result = RT_EOK;
  119. }
  120. else
  121. {
  122. return -RT_EIO;
  123. }
  124. __exit:
  125. rt_mutex_release(&(device->bus->lock));
  126. return result;
  127. }
  128. void ili9341_set_column(uint16_t StartCol, uint16_t EndCol)
  129. {
  130. ili9341_send_cmd(0x2A);
  131. ili9341_write_data_16bit(StartCol);
  132. ili9341_write_data_16bit(EndCol);
  133. }
  134. void ili9341_set_page(uint16_t StartPage, uint16_t EndPage)
  135. {
  136. ili9341_send_cmd(0x2B);
  137. ili9341_write_data_16bit(StartPage);
  138. ili9341_write_data_16bit(EndPage);
  139. }
  140. void ili9341_lcd_get_pixel(char *color, int x, int y)
  141. {
  142. uint8_t cmd;
  143. typedef union
  144. {
  145. rt_uint32_t rgbx;
  146. struct
  147. {
  148. rt_uint8_t x;
  149. rt_uint8_t r;
  150. rt_uint8_t g;
  151. rt_uint8_t b;
  152. } S;
  153. } ili9341_pixel;
  154. ili9341_pixel bgrx;
  155. if (x >= XSIZE_PHYS || y >= YSIZE_PHYS)
  156. {
  157. *(rt_uint16_t *)color = 0;
  158. return;
  159. }
  160. ili9341_set_column(x, x);
  161. ili9341_set_page(y, y);
  162. cmd = 0x2E;
  163. ili9341_spi_send_then_recv(&ili9341_spi_device, &cmd, 1, &bgrx, 4);
  164. //rt_kprintf("%08x.\n", bgrx);
  165. // To RGB565
  166. *(rt_uint16_t *)color = ((bgrx.S.r >> 3) << 11) | ((bgrx.S.g >> 2) << 5) | (bgrx.S.b >> 3);
  167. }
  168. rt_err_t rt_hw_lcd_ili9341_spi_init(const char *spibusname, void *pvUserData)
  169. {
  170. if (rt_spi_bus_attach_device(&ili9341_spi_device, "lcd_ili9341", spibusname, pvUserData) != RT_EOK)
  171. return -RT_ERROR;
  172. if (pvUserData != RT_NULL)
  173. {
  174. // GPIO CS pin mode to output */
  175. rt_pin_mode(*((rt_base_t *)pvUserData), PIN_MODE_OUTPUT);
  176. }
  177. rt_kprintf("Preferred ili9341 spi clock frequency is %d Hz.\n", NU_PKG_USING_ILI9341_SPI_CLK_FREQ);
  178. return rt_spi_configure(&ili9341_spi_device, &ili9341_cfg);
  179. }
  180. #endif /* #if defined(NU_PKG_USING_ILI9341_SPI) */