st7735.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 >= 0x90 && ch <= 0xBF) {
  260. ch += 0x30;
  261. } else if (ch == 0x84) { // Є
  262. ch = 0xaa;
  263. } else if (ch == 0x86) { // І
  264. ch = 0xb2;
  265. } else if (ch == 0x87) { // Ї
  266. ch = 0xaf;
  267. } else if (ch == 0x81) { // Ё
  268. ch = 0xA8;
  269. } else {
  270. ch = '.';
  271. }
  272. break;
  273. }
  274. case 0xD1: {
  275. str ++;
  276. ch = *str;
  277. if (ch >= 0x7E && ch <= 0x8F) {
  278. ch += 0x70;
  279. } else if (ch == 0x94) { // є
  280. ch = 0xba;
  281. } else if (ch == 0x96) { // і
  282. ch = 0xb3;
  283. } else if (ch == 0x97) { // ї
  284. ch = 0xbf;
  285. } else if (ch == 0x91) { // ё
  286. ch = 0xB8;
  287. } else {
  288. ch = '.';
  289. }
  290. break;
  291. }
  292. case 0xD2: {
  293. str ++;
  294. ch = *str;
  295. if (ch == 0x90) { // Ґ
  296. ch = 0xa5;
  297. } else if (ch == 0x91) { // ґ
  298. ch = 0xb4;
  299. }
  300. break;
  301. }
  302. }
  303. }
  304. #endif /* USE_UTF8 */
  305. ST7735_WriteChar(x, y, ch, font, color, bgcolor);
  306. x += font.width;
  307. str ++;
  308. ch = *str;
  309. }
  310. ST7735_Unselect();
  311. }
  312. /**
  313. * @brief Draw variable width font char.
  314. *
  315. * @param x coordinate,
  316. * @param y coordinate,
  317. * @param ch char to draw,
  318. * @param font variable width font
  319. * @param color char color
  320. * @param bgcolor background color
  321. */
  322. static void ST7735_WriteCharV(uint16_t x, uint16_t y, char ch, FontDefV font, uint16_t color, uint16_t bgcolor) {
  323. uint16_t i, b, j;
  324. uint8_t c, w, h;
  325. c = ch - 32;
  326. w = font.width[c];
  327. h = font.height;
  328. ST7735_SetAddressWindow(x, y, w-1, h-1);
  329. LCD_DC_DATA;
  330. SPI1->CR1 |= SPI_CR1_DFF;
  331. uint16_t ih = c * h;
  332. for(i = 0; i < h; i++) {
  333. b = font.data[ih+i]; // ??? замінити на масив позицій
  334. for(j = 0; j < w; j++) {
  335. if (b & 0x8000) {
  336. while (!(SPI1->SR & SPI_SR_TXE));
  337. SPI1->DR = color;
  338. } else {
  339. while (!(SPI1->SR & SPI_SR_TXE));
  340. SPI1->DR = bgcolor;
  341. }
  342. b <<= 1;
  343. }
  344. }
  345. while (!(SPI1->SR & SPI_SR_TXE));
  346. while ((SPI1->SR & SPI_SR_BSY));
  347. SPI1->CR1 &= ~(SPI_CR1_DFF);
  348. }
  349. void ST7735_WriteStringV(uint16_t x, uint16_t y, const char* str, FontDefV font, uint16_t color, uint16_t bgcolor) {
  350. ST7735_Select();
  351. uint8_t w;
  352. while (*str) {
  353. w = font.width[(uint8_t)*str];
  354. if (x + w >= ST7735_WIDTH) {
  355. x = 0;
  356. y += font.height;
  357. if (y + font.height >= ST7735_HEIGHT) {
  358. break;
  359. }
  360. if (*str == ' ') {
  361. // skip spaces in the beginning of the new line
  362. str ++;
  363. continue;
  364. }
  365. }
  366. ST7735_WriteCharV(x, y, *str, font, color, bgcolor);
  367. x += w;
  368. str ++;
  369. }
  370. ST7735_Unselect();
  371. }
  372. void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  373. // clipping
  374. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  375. return;
  376. }
  377. if ((x + w - 1) >= ST7735_WIDTH) {
  378. w = ST7735_WIDTH - x;
  379. }
  380. if ((y + h - 1) >= ST7735_HEIGHT) {
  381. h = ST7735_HEIGHT - y;
  382. }
  383. ST7735_Select();
  384. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  385. for (y = h; y > 0; y--) {
  386. for (x = w; x > 0; x--) {
  387. ST7735_WriteData16(color);
  388. }
  389. }
  390. ST7735_Unselect();
  391. }
  392. #ifdef USE_MALLOC
  393. void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  394. // clipping
  395. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  396. return;
  397. }
  398. if ((x + w - 1) >= ST7735_WIDTH) {
  399. w = ST7735_WIDTH - x;
  400. }
  401. if ((y + h - 1) >= ST7735_HEIGHT) {
  402. h = ST7735_HEIGHT - y;
  403. }
  404. ST7735_Select();
  405. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  406. // Prepare whole line in a single buffer
  407. uint8_t pixel[] = {color >> 8, color & 0xFF};
  408. // uint8_t *line = malloc(w * sizeof(pixel));
  409. uint8_t *line = chHeapAlloc(NULL, w * sizeof(pixel));
  410. for (x = 0; x < w; ++x) {
  411. memcpy(line + x * sizeof(pixel), pixel, sizeof(pixel));
  412. }
  413. for (y=h; y>0; y--) {
  414. spiSend(&ST7735_SPI_PORT, w * sizeof(pixel), line);
  415. }
  416. free(line);
  417. ST7735_Unselect();
  418. }
  419. void ST7735_FillScreenFast(uint16_t color) {
  420. ST7735_FillRectangleFast(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  421. }
  422. #endif /* USE_MALLOC*/
  423. void ST7735_FillScreen(uint16_t color) {
  424. ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  425. }
  426. void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
  427. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  428. return;
  429. }
  430. if ((x + w - 1) >= ST7735_WIDTH) {
  431. return;
  432. }
  433. if ((y + h - 1) >= ST7735_HEIGHT) {
  434. return;
  435. }
  436. ST7735_Select();
  437. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  438. ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
  439. ST7735_Unselect();
  440. }
  441. void ST7735_InvertColors(bool invert) {
  442. ST7735_Select();
  443. ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
  444. ST7735_Unselect();
  445. }
  446. void ST7735_SetGamma(GammaDef gamma) {
  447. ST7735_Select();
  448. ST7735_WriteCommand(ST7735_GAMSET);
  449. ST7735_WriteData((uint8_t *) &gamma, sizeof(gamma));
  450. ST7735_Unselect();
  451. }
  452. void ST7735_Test(void) {
  453. // Check border
  454. ST7735_FillScreen(ST7735_BLACK);
  455. for (int x=0; x<ST7735_WIDTH; x++) {
  456. ST7735_DrawPixel(x, 0, ST7735_RED);
  457. ST7735_DrawPixel(x, ST7735_HEIGHT-1, ST7735_RED);
  458. }
  459. for(int y=0; y<ST7735_HEIGHT; y++) {
  460. ST7735_DrawPixel(0, y, ST7735_RED);
  461. ST7735_DrawPixel(ST7735_WIDTH-1, y, ST7735_RED);
  462. }
  463. chThdSleepMilliseconds(3000);
  464. // Check fonts
  465. ST7735_FillScreen(ST7735_BLACK);
  466. ST7735_WriteString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_RED, ST7735_BLACK);
  467. ST7735_WriteString(0, 3*10, "Font_11x18, green, lorem ipsum", Font_11x18, ST7735_GREEN, ST7735_BLACK);
  468. ST7735_WriteString(0, 3*10+3*18, "Font_16x26", Font_16x26, ST7735_BLUE, ST7735_BLACK);
  469. chThdSleepMilliseconds(5000);
  470. // Check colors
  471. ST7735_FillScreen(ST7735_BLACK);
  472. ST7735_WriteString(0, 0, "BLACK", Font_11x18, ST7735_WHITE, ST7735_BLACK);
  473. chThdSleepMilliseconds(500);
  474. ST7735_FillScreen(ST7735_BLUE);
  475. ST7735_WriteString(0, 0, "BLUE", Font_11x18, ST7735_BLACK, ST7735_BLUE);
  476. chThdSleepMilliseconds(500);
  477. ST7735_FillScreen(ST7735_RED);
  478. ST7735_WriteString(0, 0, "RED", Font_11x18, ST7735_BLACK, ST7735_RED);
  479. chThdSleepMilliseconds(500);
  480. ST7735_FillScreen(ST7735_GREEN);
  481. ST7735_WriteString(0, 0, "GREEN", Font_11x18, ST7735_BLACK, ST7735_GREEN);
  482. chThdSleepMilliseconds(500);
  483. ST7735_FillScreen(ST7735_CYAN);
  484. ST7735_WriteString(0, 0, "CYAN", Font_11x18, ST7735_BLACK, ST7735_CYAN);
  485. chThdSleepMilliseconds(500);
  486. ST7735_FillScreen(ST7735_MAGENTA);
  487. ST7735_WriteString(0, 0, "MAGENTA", Font_11x18, ST7735_BLACK, ST7735_MAGENTA);
  488. chThdSleepMilliseconds(500);
  489. ST7735_FillScreen(ST7735_YELLOW);
  490. ST7735_WriteString(0, 0, "YELLOW", Font_11x18, ST7735_BLACK, ST7735_YELLOW);
  491. chThdSleepMilliseconds(500);
  492. ST7735_FillScreen(ST7735_WHITE);
  493. ST7735_WriteString(0, 0, "WHITE", Font_11x18, ST7735_BLACK, ST7735_WHITE);
  494. chThdSleepMilliseconds(500);
  495. chThdSleepMilliseconds(15000);
  496. }