st7735.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /**
  2. * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(ST7735)
  42. #include "nrf_lcd.h"
  43. #include "nrf_drv_spi.h"
  44. #include "nrf_delay.h"
  45. #include "nrf_gpio.h"
  46. #include "boards.h"
  47. // Set of commands described in ST7735 data sheet.
  48. #define ST7735_NOP 0x00
  49. #define ST7735_SWRESET 0x01
  50. #define ST7735_RDDID 0x04
  51. #define ST7735_RDDST 0x09
  52. #define ST7735_SLPIN 0x10
  53. #define ST7735_SLPOUT 0x11
  54. #define ST7735_PTLON 0x12
  55. #define ST7735_NORON 0x13
  56. #define ST7735_INVOFF 0x20
  57. #define ST7735_INVON 0x21
  58. #define ST7735_DISPOFF 0x28
  59. #define ST7735_DISPON 0x29
  60. #define ST7735_CASET 0x2A
  61. #define ST7735_RASET 0x2B
  62. #define ST7735_RAMWR 0x2C
  63. #define ST7735_RAMRD 0x2E
  64. #define ST7735_PTLAR 0x30
  65. #define ST7735_COLMOD 0x3A
  66. #define ST7735_MADCTL 0x36
  67. #define ST7735_FRMCTR1 0xB1
  68. #define ST7735_FRMCTR2 0xB2
  69. #define ST7735_FRMCTR3 0xB3
  70. #define ST7735_INVCTR 0xB4
  71. #define ST7735_DISSET5 0xB6
  72. #define ST7735_PWCTR1 0xC0
  73. #define ST7735_PWCTR2 0xC1
  74. #define ST7735_PWCTR3 0xC2
  75. #define ST7735_PWCTR4 0xC3
  76. #define ST7735_PWCTR5 0xC4
  77. #define ST7735_VMCTR1 0xC5
  78. #define ST7735_RDID1 0xDA
  79. #define ST7735_RDID2 0xDB
  80. #define ST7735_RDID3 0xDC
  81. #define ST7735_RDID4 0xDD
  82. #define ST7735_PWCTR6 0xFC
  83. #define ST7735_GMCTRP1 0xE0
  84. #define ST7735_GMCTRN1 0xE1
  85. #define ST7735_MADCTL_MY 0x80
  86. #define ST7735_MADCTL_MX 0x40
  87. #define ST7735_MADCTL_MV 0x20
  88. #define ST7735_MADCTL_ML 0x10
  89. #define ST7735_MADCTL_RGB 0x00
  90. #define ST7735_MADCTL_BGR 0x08
  91. #define ST7735_MADCTL_MH 0x04
  92. /* @} */
  93. #define RGB2BGR(x) (x << 11) | (x & 0x07E0) | (x >> 11)
  94. static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(ST7735_SPI_INSTANCE); /**< SPI instance. */
  95. /**
  96. * @brief Structure holding ST7735 controller basic parameters.
  97. */
  98. typedef struct
  99. {
  100. uint8_t tab_color; /**< Color of tab attached to the used screen. */
  101. }st7735_t;
  102. /**
  103. * @brief Enumerator with TFT tab colors.
  104. */
  105. typedef enum{
  106. INITR_GREENTAB = 0, /**< Green tab. */
  107. INITR_REDTAB, /**< Red tab. */
  108. INITR_BLACKTAB, /**< Black tab. */
  109. INITR_144GREENTAB /**< Green tab, 1.44" display. */
  110. }st7735_tab_t;
  111. static st7735_t m_st7735;
  112. static inline void spi_write(const void * data, size_t size)
  113. {
  114. APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, data, size, NULL, 0));
  115. }
  116. static inline void write_command(uint8_t c)
  117. {
  118. nrf_gpio_pin_clear(ST7735_DC_PIN);
  119. spi_write(&c, sizeof(c));
  120. }
  121. static inline void write_data(uint8_t c)
  122. {
  123. nrf_gpio_pin_set(ST7735_DC_PIN);
  124. spi_write(&c, sizeof(c));
  125. }
  126. static void set_addr_window(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
  127. {
  128. ASSERT(x0 <= x1);
  129. ASSERT(y0 <= y1);
  130. write_command(ST7735_CASET);
  131. write_data(0x00); // For a 128x160 display, it is always 0.
  132. write_data(x0);
  133. write_data(0x00); // For a 128x160 display, it is always 0.
  134. write_data(x1);
  135. write_command(ST7735_RASET);
  136. write_data(0x00); // For a 128x160 display, it is always 0.
  137. write_data(y0);
  138. write_data(0x00); // For a 128x160 display, it is always 0.
  139. write_data(y1);
  140. write_command(ST7735_RAMWR);
  141. }
  142. static void command_list(void)
  143. {
  144. write_command(ST7735_SWRESET);
  145. nrf_delay_ms(150);
  146. write_command(ST7735_SLPOUT);
  147. nrf_delay_ms(500);
  148. write_command(ST7735_FRMCTR1);
  149. write_data(0x01);
  150. write_data(0x2C);
  151. write_data(0x2D);
  152. write_command(ST7735_FRMCTR2);
  153. write_data(0x01);
  154. write_data(0x2C);
  155. write_data(0x2D);
  156. write_command(ST7735_FRMCTR3);
  157. write_data(0x01);
  158. write_data(0x2C);
  159. write_data(0x2D);
  160. write_data(0x01);
  161. write_data(0x2C);
  162. write_data(0x2D);
  163. write_command(ST7735_INVCTR);
  164. write_data(0x07);
  165. write_command(ST7735_PWCTR1);
  166. write_data(0xA2);
  167. write_data(0x02);
  168. write_data(0x84);
  169. write_command(ST7735_PWCTR2);
  170. write_data(0xC5);
  171. write_command(ST7735_PWCTR3);
  172. write_data(0x0A);
  173. write_data(0x00);
  174. write_command(ST7735_PWCTR3);
  175. write_data(0x8A);
  176. write_data(0x2A);
  177. write_command(ST7735_PWCTR5);
  178. write_data(0x8A);
  179. write_data(0xEE);
  180. write_data(0x0E);
  181. write_command(ST7735_INVOFF);
  182. write_command(ST7735_MADCTL);
  183. write_data(0xC8);
  184. write_command(ST7735_COLMOD);
  185. write_data(0x05);
  186. if (m_st7735.tab_color == INITR_GREENTAB)
  187. {
  188. write_command(ST7735_CASET);
  189. write_data(0x00);
  190. write_data(0x02);
  191. write_data(0x00);
  192. write_data(0x81);
  193. write_command(ST7735_RASET);
  194. write_data(0x00);
  195. write_data(0x01);
  196. write_data(0x00);
  197. write_data(0xA0);
  198. }
  199. else if (m_st7735.tab_color == INITR_144GREENTAB)
  200. {
  201. write_command(ST7735_CASET);
  202. write_data(0x00);
  203. write_data(0x00);
  204. write_data(0x00);
  205. write_data(0x7F);
  206. write_command(ST7735_RASET);
  207. write_data(0x00);
  208. write_data(0x00);
  209. write_data(0x00);
  210. write_data(0x7F);
  211. }
  212. else if (m_st7735.tab_color == INITR_REDTAB)
  213. {
  214. write_command(ST7735_CASET);
  215. write_data(0x00);
  216. write_data(0x00);
  217. write_data(0x00);
  218. write_data(0x7F);
  219. write_command(ST7735_RASET);
  220. write_data(0x00);
  221. write_data(0x00);
  222. write_data(0x00);
  223. write_data(0x9F);
  224. }
  225. write_command(ST7735_GMCTRP1);
  226. write_data(0x02);
  227. write_data(0x1c);
  228. write_data(0x07);
  229. write_data(0x12);
  230. write_data(0x37);
  231. write_data(0x32);
  232. write_data(0x29);
  233. write_data(0x2d);
  234. write_data(0x29);
  235. write_data(0x25);
  236. write_data(0x2b);
  237. write_data(0x39);
  238. write_data(0x00);
  239. write_data(0x01);
  240. write_data(0x03);
  241. write_data(0x10);
  242. write_command(ST7735_GMCTRN1);
  243. write_data(0x03);
  244. write_data(0x1d);
  245. write_data(0x07);
  246. write_data(0x06);
  247. write_data(0x2e);
  248. write_data(0x2c);
  249. write_data(0x29);
  250. write_data(0x2d);
  251. write_data(0x2e);
  252. write_data(0x2e);
  253. write_data(0x37);
  254. write_data(0x3f);
  255. write_data(0x00);
  256. write_data(0x00);
  257. write_data(0x02);
  258. write_data(0x10);
  259. write_command(ST7735_NORON);
  260. nrf_delay_ms(10);
  261. write_command(ST7735_DISPON);
  262. nrf_delay_ms(100);
  263. if (m_st7735.tab_color == INITR_BLACKTAB)
  264. {
  265. write_command(ST7735_MADCTL);
  266. write_data(0xC0);
  267. }
  268. }
  269. static ret_code_t hardware_init(void)
  270. {
  271. ret_code_t err_code;
  272. nrf_gpio_cfg_output(ST7735_DC_PIN);
  273. nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  274. spi_config.sck_pin = ST7735_SCK_PIN;
  275. spi_config.miso_pin = ST7735_MISO_PIN;
  276. spi_config.mosi_pin = ST7735_MOSI_PIN;
  277. spi_config.ss_pin = ST7735_SS_PIN;
  278. err_code = nrf_drv_spi_init(&spi, &spi_config, NULL, NULL);
  279. return err_code;
  280. }
  281. static ret_code_t st7735_init(void)
  282. {
  283. ret_code_t err_code;
  284. m_st7735.tab_color = ST7735_TAB_COLOR;
  285. err_code = hardware_init();
  286. if (err_code != NRF_SUCCESS)
  287. {
  288. return err_code;
  289. }
  290. command_list();
  291. return err_code;
  292. }
  293. static void st7735_uninit(void)
  294. {
  295. nrf_drv_spi_uninit(&spi);
  296. }
  297. static void st7735_pixel_draw(uint16_t x, uint16_t y, uint32_t color)
  298. {
  299. set_addr_window(x, y, x, y);
  300. color = RGB2BGR(color);
  301. const uint8_t data[2] = {color >> 8, color};
  302. nrf_gpio_pin_set(ST7735_DC_PIN);
  303. spi_write(data, sizeof(data));
  304. nrf_gpio_pin_clear(ST7735_DC_PIN);
  305. }
  306. static void st7735_rect_draw(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t color)
  307. {
  308. set_addr_window(x, y, x + width - 1, y + height - 1);
  309. color = RGB2BGR(color);
  310. const uint8_t data[2] = {color >> 8, color};
  311. nrf_gpio_pin_set(ST7735_DC_PIN);
  312. // Duff's device algorithm for optimizing loop.
  313. uint32_t i = (height * width + 7) / 8;
  314. /*lint -save -e525 -e616 -e646 */
  315. switch ((height * width) % 8) {
  316. case 0:
  317. do {
  318. spi_write(data, sizeof(data));
  319. case 7:
  320. spi_write(data, sizeof(data));
  321. case 6:
  322. spi_write(data, sizeof(data));
  323. case 5:
  324. spi_write(data, sizeof(data));
  325. case 4:
  326. spi_write(data, sizeof(data));
  327. case 3:
  328. spi_write(data, sizeof(data));
  329. case 2:
  330. spi_write(data, sizeof(data));
  331. case 1:
  332. spi_write(data, sizeof(data));
  333. } while (--i > 0);
  334. default:
  335. break;
  336. }
  337. /*lint -restore */
  338. nrf_gpio_pin_clear(ST7735_DC_PIN);
  339. }
  340. static void st7735_dummy_display(void)
  341. {
  342. /* No implementation needed. */
  343. }
  344. static void st7735_rotation_set(nrf_lcd_rotation_t rotation)
  345. {
  346. write_command(ST7735_MADCTL);
  347. switch (rotation) {
  348. case NRF_LCD_ROTATE_0:
  349. if (m_st7735.tab_color == INITR_BLACKTAB)
  350. {
  351. write_data(ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_RGB);
  352. }
  353. else
  354. {
  355. write_data(ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_BGR);
  356. }
  357. break;
  358. case NRF_LCD_ROTATE_90:
  359. if (m_st7735.tab_color == INITR_BLACKTAB)
  360. {
  361. write_data(ST7735_MADCTL_MY | ST7735_MADCTL_MV | ST7735_MADCTL_RGB);
  362. }
  363. else
  364. {
  365. write_data(ST7735_MADCTL_MY | ST7735_MADCTL_MV | ST7735_MADCTL_BGR);
  366. }
  367. break;
  368. case NRF_LCD_ROTATE_180:
  369. if (m_st7735.tab_color == INITR_BLACKTAB)
  370. {
  371. write_data(ST7735_MADCTL_RGB);
  372. }
  373. else
  374. {
  375. write_data(ST7735_MADCTL_BGR);
  376. }
  377. break;
  378. case NRF_LCD_ROTATE_270:
  379. if (m_st7735.tab_color == INITR_BLACKTAB)
  380. {
  381. write_data(ST7735_MADCTL_MX | ST7735_MADCTL_MV | ST7735_MADCTL_RGB);
  382. }
  383. else
  384. {
  385. write_data(ST7735_MADCTL_MX | ST7735_MADCTL_MV | ST7735_MADCTL_BGR);
  386. }
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. static void st7735_display_invert(bool invert)
  393. {
  394. write_command(invert ? ST7735_INVON : ST7735_INVOFF);
  395. }
  396. static lcd_cb_t st7735_cb = {
  397. .height = ST7735_HEIGHT,
  398. .width = ST7735_WIDTH
  399. };
  400. const nrf_lcd_t nrf_lcd_st7735 = {
  401. .lcd_init = st7735_init,
  402. .lcd_uninit = st7735_uninit,
  403. .lcd_pixel_draw = st7735_pixel_draw,
  404. .lcd_rect_draw = st7735_rect_draw,
  405. .lcd_display = st7735_dummy_display,
  406. .lcd_rotation_set = st7735_rotation_set,
  407. .lcd_display_invert = st7735_display_invert,
  408. .p_lcd_cb = &st7735_cb
  409. };
  410. #endif // NRF_MODULE_ENABLED(ST7735)