ili9341_spi.c 4.9 KB

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