ili9341_spi.c 4.7 KB

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