st7735.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "ch.h"
  2. #include "hal.h"
  3. #include "malloc.h"
  4. #include "string.h"
  5. #include "st7735.h"
  6. #define DELAY 0x80
  7. #define ST7735_Select() LCD_CS_RES
  8. #define ST7735_Unselect() LCD_CS_SET
  9. #define HAL_Delay(x) chThdSleepMilliseconds(x)
  10. static void ST7735_Reset(void);
  11. // based on Adafruit ST7735 library for Arduino
  12. static const uint8_t
  13. init_cmds1[] = { // Init for 7735R, part 1 (red or green tab)
  14. 15, // 15 commands in list:
  15. ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
  16. 150, // 150 ms delay
  17. ST7735_SLPOUT, DELAY, // 2: Out of sleep mode, 0 args, w/delay
  18. 255, // 500 ms delay
  19. ST7735_FRMCTR1, 3, // 3: Frame rate ctrl - normal mode, 3 args:
  20. 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
  21. ST7735_FRMCTR2, 3, // 4: Frame rate control - idle mode, 3 args:
  22. 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
  23. ST7735_FRMCTR3, 6, // 5: Frame rate ctrl - partial mode, 6 args:
  24. 0x01, 0x2C, 0x2D, // Dot inversion mode
  25. 0x01, 0x2C, 0x2D, // Line inversion mode
  26. ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg, no delay:
  27. 0x07, // No inversion
  28. ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay:
  29. 0xA2,
  30. 0x02, // -4.6V
  31. 0x84, // AUTO mode
  32. ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay:
  33. 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
  34. ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay:
  35. 0x0A, // Opamp current small
  36. 0x00, // Boost frequency
  37. ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay:
  38. 0x8A, // BCLK/2, Opamp current small & Medium low
  39. 0x2A,
  40. ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay:
  41. 0x8A, 0xEE,
  42. ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay:
  43. 0x0E,
  44. ST7735_INVOFF, 0, // 13: Don't invert display, no args, no delay
  45. ST7735_MADCTL, 1, // 14: Memory access control (directions), 1 arg:
  46. ST7735_ROTATION, // row addr/col addr, bottom to top refresh
  47. ST7735_COLMOD, 1, // 15: set color mode, 1 arg, no delay:
  48. 0x05 // 16-bit color
  49. },
  50. #if (defined(ST7735_IS_128X128) || defined(ST7735_IS_160X128))
  51. init_cmds2[] = { // Init for 7735R, part 2 (1.44" display)
  52. 2, // 2 commands in list:
  53. ST7735_CASET, 4, // 1: Column addr set, 4 args, no delay:
  54. 0x00, 0x00, // XSTART = 0
  55. 0x00, 0x7F, // XEND = 127
  56. ST7735_RASET, 4, // 2: Row addr set, 4 args, no delay:
  57. 0x00, 0x00, // XSTART = 0
  58. 0x00, 0x7F // XEND = 127
  59. },
  60. #endif // ST7735_IS_128X128
  61. #ifdef ST7735_IS_160X80
  62. init_cmds2[] = { // Init for 7735S, part 2 (160x80 display)
  63. 3, // 3 commands in list:
  64. ST7735_CASET, 4, // 1: Column addr set, 4 args, no delay:
  65. 0x00, 0x00, // XSTART = 0
  66. 0x00, 0x4F, // XEND = 79
  67. ST7735_RASET, 4, // 2: Row addr set, 4 args, no delay:
  68. 0x00, 0x00, // XSTART = 0
  69. 0x00, 0x9F, // XEND = 159
  70. ST7735_INVON, 0 // 3: Invert colors
  71. },
  72. #endif
  73. init_cmds3[] = { // Init for 7735R, part 3 (red or green tab)
  74. 4, // 4 commands in list:
  75. ST7735_GMCTRP1, 16, // 1: Gamma Adjustments (pos. polarity), 16 args, no delay:
  76. 0x02, 0x1c, 0x07, 0x12,
  77. 0x37, 0x32, 0x29, 0x2d,
  78. 0x29, 0x25, 0x2B, 0x39,
  79. 0x00, 0x01, 0x03, 0x10,
  80. ST7735_GMCTRN1, 16, // 2: Gamma Adjustments (neg. polarity), 16 args, no delay:
  81. 0x03, 0x1d, 0x07, 0x06,
  82. 0x2E, 0x2C, 0x29, 0x2D,
  83. 0x2E, 0x2E, 0x37, 0x3F,
  84. 0x00, 0x00, 0x02, 0x10,
  85. ST7735_NORON, DELAY, // 3: Normal display on, no args, w/delay
  86. 10, // 10 ms delay
  87. ST7735_DISPON, DELAY, // 4: Main screen turn on, no args w/delay
  88. 100 // 100 ms delay
  89. };
  90. static void ST7735_Reset(void) {
  91. //HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_RESET);
  92. HAL_Delay(5);
  93. //HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
  94. }
  95. /**
  96. * @brief Send command
  97. *
  98. * @param cmd command for display
  99. */
  100. static void ST7735_WriteCommand(uint8_t cmd) {
  101. LCD_DC_CMD;
  102. // wite while spi is busy
  103. while (!(SPI1->SR & SPI_SR_TXE));
  104. // send data
  105. SPI1->DR = cmd;
  106. // wite for data to be transmitted
  107. while (!(SPI1->SR & SPI_SR_TXE));
  108. // ... and spi is busy
  109. while ((SPI1->SR & SPI_SR_BSY));
  110. }
  111. /**
  112. * @brief Send 8-bit data using DMA
  113. *
  114. * @param buff data to be sended
  115. * @param buff_size amount of bytes
  116. */
  117. static void ST7735_WriteData(uint8_t* buff, size_t buff_size) {
  118. LCD_DC_DATA;
  119. spiSend(&ST7735_SPI_PORT, buff_size, buff);
  120. }
  121. /**
  122. * @brief Send 16-bit data trough reg, with end wait.
  123. *
  124. * @param data to send to display
  125. */
  126. static void ST7735_WriteData16(uint16_t data) {
  127. LCD_DC_DATA;
  128. //spiSend(&ST7735_SPI_PORT, buff_size, buff);
  129. // set 16-bit data
  130. SPI1->CR1 |= SPI_CR1_DFF;
  131. // wite while spi is busy
  132. while (!(SPI1->SR & SPI_SR_TXE));
  133. // send data
  134. SPI1->DR = data;
  135. // wite for data too be transmitted
  136. while (!(SPI1->SR & SPI_SR_TXE));
  137. // ... and spi is unbusy
  138. while ((SPI1->SR & SPI_SR_BSY));
  139. // set 8-bit data
  140. SPI1->CR1 &= ~(SPI_CR1_DFF);
  141. }
  142. static void ST7735_ExecuteCommandList(const uint8_t *addr) {
  143. uint8_t numCommands, numArgs;
  144. uint16_t ms;
  145. numCommands = *addr ++;
  146. while (numCommands--) {
  147. uint8_t cmd = *addr ++;
  148. ST7735_WriteCommand(cmd);
  149. numArgs = *addr ++;
  150. // If high bit set, delay follows args
  151. ms = numArgs & DELAY;
  152. numArgs &= ~DELAY;
  153. if (numArgs) {
  154. ST7735_WriteData((uint8_t*)addr, numArgs);
  155. addr += numArgs;
  156. }
  157. if (ms) {
  158. ms = *addr ++;
  159. if (ms == 255) {
  160. ms = 500;
  161. }
  162. HAL_Delay(ms);
  163. }
  164. }
  165. }
  166. static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
  167. // column address set
  168. ST7735_WriteCommand(ST7735_CASET);
  169. uint8_t data[] = {0x00, x0 + ST7735_XSTART, 0x00, x1 + ST7735_XSTART};
  170. ST7735_WriteData(data, sizeof(data));
  171. // row address set
  172. ST7735_WriteCommand(ST7735_RASET);
  173. data[1] = y0 + ST7735_YSTART;
  174. data[3] = y1 + ST7735_YSTART;
  175. ST7735_WriteData(data, sizeof(data));
  176. // write to RAM
  177. ST7735_WriteCommand(ST7735_RAMWR);
  178. }
  179. void ST7735_Init(void) {
  180. ST7735_Select();
  181. ST7735_Reset();
  182. ST7735_ExecuteCommandList(init_cmds1);
  183. ST7735_ExecuteCommandList(init_cmds2);
  184. ST7735_ExecuteCommandList(init_cmds3);
  185. ST7735_Unselect();
  186. }
  187. /**
  188. * @brief Draw single pixel
  189. *
  190. * @param x coordinate
  191. * @param y coordinate
  192. * @param color pixel colour
  193. */
  194. void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color) {
  195. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  196. return;
  197. }
  198. ST7735_Select();
  199. ST7735_SetAddressWindow(x, y, x+1, y+1);
  200. ST7735_WriteData16(color);
  201. ST7735_Unselect();
  202. }
  203. /**
  204. * @brief Draw fixed width font char.
  205. *
  206. * @param x coordinate,
  207. * @param y coordinate,
  208. * @param ch char to draw,
  209. * @param font fixed width font
  210. * @param color char color
  211. * @param bgcolor background color
  212. */
  213. static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) {
  214. uint32_t i, b, j;
  215. ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
  216. LCD_DC_DATA;
  217. SPI1->CR1 |= SPI_CR1_DFF;
  218. uint16_t ih = (ch - 32) * font.height;
  219. for(i = 0; i < font.height; i++) {
  220. b = font.data[ih + i];
  221. for(j = 0; j < font.width; j++) {
  222. if (b & 0x8000) {
  223. while (!(SPI1->SR & SPI_SR_TXE));
  224. SPI1->DR = color;
  225. } else {
  226. while (!(SPI1->SR & SPI_SR_TXE));
  227. SPI1->DR = bgcolor;
  228. }
  229. b <<= 1;
  230. }
  231. }
  232. while (!(SPI1->SR & SPI_SR_TXE));
  233. while ((SPI1->SR & SPI_SR_BSY));
  234. SPI1->CR1 &= ~(SPI_CR1_DFF);
  235. }
  236. void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
  237. ST7735_Select();
  238. char ch = *str;
  239. while (ch) {
  240. if (x + font.width >= ST7735_WIDTH) {
  241. x = 0;
  242. y += font.height;
  243. if (y + font.height >= ST7735_HEIGHT) {
  244. break;
  245. }
  246. if (ch == ' ') {
  247. // skip spaces in the beginning of the new line
  248. str ++;
  249. ch = *str;
  250. continue;
  251. }
  252. }
  253. #ifdef USE_UTF8
  254. if (ch >= 0xC0) {
  255. switch (ch) {
  256. case 0xD0: {
  257. str ++;
  258. ch = *str;
  259. if (ch == 0x81) { ch = 0xA8; break; }
  260. if (ch >= 0x90 && ch <= 0xBF) ch += 0x2F;
  261. break;
  262. }
  263. case 0xD1: {
  264. str ++;
  265. ch = *str;
  266. if (ch == 0x91) { ch = 0xB8; break; }
  267. if (ch >= 0x7E && ch <= 0x8F) ch += 0x6F;
  268. break;
  269. }
  270. }
  271. }
  272. #endif /* USE_UTF8 */
  273. ST7735_WriteChar(x, y, ch, font, color, bgcolor);
  274. x += font.width;
  275. str ++;
  276. ch = *str;
  277. }
  278. ST7735_Unselect();
  279. }
  280. /**
  281. * @brief Draw variable width font char.
  282. *
  283. * @param x coordinate,
  284. * @param y coordinate,
  285. * @param ch char to draw,
  286. * @param font variable width font
  287. * @param color char color
  288. * @param bgcolor background color
  289. */
  290. static void ST7735_WriteCharV(uint16_t x, uint16_t y, char ch, FontDefV font, uint16_t color, uint16_t bgcolor) {
  291. uint16_t i, b, j;
  292. uint8_t c, w, h;
  293. c = ch - 32;
  294. w = font.width[c];
  295. h = font.height;
  296. ST7735_SetAddressWindow(x, y, w-1, h-1);
  297. LCD_DC_DATA;
  298. SPI1->CR1 |= SPI_CR1_DFF;
  299. uint16_t ih = c * h;
  300. for(i = 0; i < h; i++) {
  301. b = font.data[ih+i]; // ??? замінити на масив позицій
  302. for(j = 0; j < w; j++) {
  303. if (b & 0x8000) {
  304. while (!(SPI1->SR & SPI_SR_TXE));
  305. SPI1->DR = color;
  306. } else {
  307. while (!(SPI1->SR & SPI_SR_TXE));
  308. SPI1->DR = bgcolor;
  309. }
  310. b <<= 1;
  311. }
  312. }
  313. while (!(SPI1->SR & SPI_SR_TXE));
  314. while ((SPI1->SR & SPI_SR_BSY));
  315. SPI1->CR1 &= ~(SPI_CR1_DFF);
  316. }
  317. void ST7735_WriteStringV(uint16_t x, uint16_t y, const char* str, FontDefV font, uint16_t color, uint16_t bgcolor) {
  318. ST7735_Select();
  319. uint8_t w;
  320. while (*str) {
  321. w = font.width[(uint8_t)*str];
  322. if (x + w >= ST7735_WIDTH) {
  323. x = 0;
  324. y += font.height;
  325. if (y + font.height >= ST7735_HEIGHT) {
  326. break;
  327. }
  328. if (*str == ' ') {
  329. // skip spaces in the beginning of the new line
  330. str ++;
  331. continue;
  332. }
  333. }
  334. ST7735_WriteCharV(x, y, *str, font, color, bgcolor);
  335. x += w;
  336. str ++;
  337. }
  338. ST7735_Unselect();
  339. }
  340. void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  341. // clipping
  342. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  343. return;
  344. }
  345. if ((x + w - 1) >= ST7735_WIDTH) {
  346. w = ST7735_WIDTH - x;
  347. }
  348. if ((y + h - 1) >= ST7735_HEIGHT) {
  349. h = ST7735_HEIGHT - y;
  350. }
  351. ST7735_Select();
  352. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  353. for (y = h; y > 0; y--) {
  354. for (x = w; x > 0; x--) {
  355. ST7735_WriteData16(color);
  356. }
  357. }
  358. ST7735_Unselect();
  359. }
  360. #ifdef USE_MALLOC
  361. void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  362. // clipping
  363. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  364. return;
  365. }
  366. if ((x + w - 1) >= ST7735_WIDTH) {
  367. w = ST7735_WIDTH - x;
  368. }
  369. if ((y + h - 1) >= ST7735_HEIGHT) {
  370. h = ST7735_HEIGHT - y;
  371. }
  372. ST7735_Select();
  373. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  374. // Prepare whole line in a single buffer
  375. uint8_t pixel[] = {color >> 8, color & 0xFF};
  376. // uint8_t *line = malloc(w * sizeof(pixel));
  377. uint8_t *line = chHeapAlloc(NULL, w * sizeof(pixel));
  378. for (x = 0; x < w; ++x) {
  379. memcpy(line + x * sizeof(pixel), pixel, sizeof(pixel));
  380. }
  381. for (y=h; y>0; y--) {
  382. spiSend(&ST7735_SPI_PORT, w * sizeof(pixel), line);
  383. }
  384. free(line);
  385. ST7735_Unselect();
  386. }
  387. void ST7735_FillScreenFast(uint16_t color) {
  388. ST7735_FillRectangleFast(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  389. }
  390. #endif /* USE_MALLOC*/
  391. void ST7735_FillScreen(uint16_t color) {
  392. ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  393. }
  394. void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
  395. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  396. return;
  397. }
  398. if ((x + w - 1) >= ST7735_WIDTH) {
  399. return;
  400. }
  401. if ((y + h - 1) >= ST7735_HEIGHT) {
  402. return;
  403. }
  404. ST7735_Select();
  405. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  406. ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
  407. ST7735_Unselect();
  408. }
  409. void ST7735_InvertColors(bool invert) {
  410. ST7735_Select();
  411. ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
  412. ST7735_Unselect();
  413. }
  414. void ST7735_SetGamma(GammaDef gamma) {
  415. ST7735_Select();
  416. ST7735_WriteCommand(ST7735_GAMSET);
  417. ST7735_WriteData((uint8_t *) &gamma, sizeof(gamma));
  418. ST7735_Unselect();
  419. }
  420. void ST7735_Test(void) {
  421. // Check border
  422. ST7735_FillScreen(ST7735_BLACK);
  423. for (int x=0; x<ST7735_WIDTH; x++) {
  424. ST7735_DrawPixel(x, 0, ST7735_RED);
  425. ST7735_DrawPixel(x, ST7735_HEIGHT-1, ST7735_RED);
  426. }
  427. for(int y=0; y<ST7735_HEIGHT; y++) {
  428. ST7735_DrawPixel(0, y, ST7735_RED);
  429. ST7735_DrawPixel(ST7735_WIDTH-1, y, ST7735_RED);
  430. }
  431. chThdSleepMilliseconds(3000);
  432. // Check fonts
  433. ST7735_FillScreen(ST7735_BLACK);
  434. ST7735_WriteString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_RED, ST7735_BLACK);
  435. ST7735_WriteString(0, 3*10, "Font_11x18, green, lorem ipsum", Font_11x18, ST7735_GREEN, ST7735_BLACK);
  436. ST7735_WriteString(0, 3*10+3*18, "Font_16x26", Font_16x26, ST7735_BLUE, ST7735_BLACK);
  437. chThdSleepMilliseconds(5000);
  438. // Check colors
  439. ST7735_FillScreen(ST7735_BLACK);
  440. ST7735_WriteString(0, 0, "BLACK", Font_11x18, ST7735_WHITE, ST7735_BLACK);
  441. chThdSleepMilliseconds(500);
  442. ST7735_FillScreen(ST7735_BLUE);
  443. ST7735_WriteString(0, 0, "BLUE", Font_11x18, ST7735_BLACK, ST7735_BLUE);
  444. chThdSleepMilliseconds(500);
  445. ST7735_FillScreen(ST7735_RED);
  446. ST7735_WriteString(0, 0, "RED", Font_11x18, ST7735_BLACK, ST7735_RED);
  447. chThdSleepMilliseconds(500);
  448. ST7735_FillScreen(ST7735_GREEN);
  449. ST7735_WriteString(0, 0, "GREEN", Font_11x18, ST7735_BLACK, ST7735_GREEN);
  450. chThdSleepMilliseconds(500);
  451. ST7735_FillScreen(ST7735_CYAN);
  452. ST7735_WriteString(0, 0, "CYAN", Font_11x18, ST7735_BLACK, ST7735_CYAN);
  453. chThdSleepMilliseconds(500);
  454. ST7735_FillScreen(ST7735_MAGENTA);
  455. ST7735_WriteString(0, 0, "MAGENTA", Font_11x18, ST7735_BLACK, ST7735_MAGENTA);
  456. chThdSleepMilliseconds(500);
  457. ST7735_FillScreen(ST7735_YELLOW);
  458. ST7735_WriteString(0, 0, "YELLOW", Font_11x18, ST7735_BLACK, ST7735_YELLOW);
  459. chThdSleepMilliseconds(500);
  460. ST7735_FillScreen(ST7735_WHITE);
  461. ST7735_WriteString(0, 0, "WHITE", Font_11x18, ST7735_BLACK, ST7735_WHITE);
  462. chThdSleepMilliseconds(500);
  463. chThdSleepMilliseconds(15000);
  464. }