st7735.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. /**
  237. * @brief
  238. *
  239. * @param x from ST7735_XSTART to ST7735_WIDTH
  240. * @param y from ST7735_YSTART to ST7735_HEIGHT
  241. * @param str string to output
  242. * @param font
  243. * @param color
  244. * @param bgcolor
  245. */
  246. void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
  247. ST7735_Select();
  248. char ch = *str;
  249. while (ch) {
  250. if (x + font.width >= ST7735_WIDTH) {
  251. x = 0;
  252. y += font.height;
  253. if (y + font.height >= ST7735_HEIGHT) {
  254. break;
  255. }
  256. if (ch == ' ') {
  257. // skip spaces in the beginning of the new line
  258. str ++;
  259. ch = *str;
  260. continue;
  261. }
  262. }
  263. #ifdef USE_UTF8
  264. if (ch >= 0xC0) {
  265. switch (ch) {
  266. case 0xD0: {
  267. str ++;
  268. ch = *str;
  269. if (ch >= 0x90 && ch <= 0xBF) {
  270. ch += 0x30;
  271. } else if (ch == 0x84) { // Є
  272. ch = 0xaa;
  273. } else if (ch == 0x86) { // І
  274. ch = 0xb2;
  275. } else if (ch == 0x87) { // Ї
  276. ch = 0xaf;
  277. } else if (ch == 0x81) { // Ё
  278. ch = 0xA8;
  279. } else {
  280. ch = '.';
  281. }
  282. break;
  283. }
  284. case 0xD1: {
  285. str ++;
  286. ch = *str;
  287. if (ch >= 0x7E && ch <= 0x8F) {
  288. ch += 0x70;
  289. } else if (ch == 0x94) { // є
  290. ch = 0xba;
  291. } else if (ch == 0x96) { // і
  292. ch = 0xb3;
  293. } else if (ch == 0x97) { // ї
  294. ch = 0xbf;
  295. } else if (ch == 0x91) { // ё
  296. ch = 0xB8;
  297. } else {
  298. ch = '.';
  299. }
  300. break;
  301. }
  302. case 0xD2: {
  303. str ++;
  304. ch = *str;
  305. if (ch == 0x90) { // Ґ
  306. ch = 0xa5;
  307. } else if (ch == 0x91) { // ґ
  308. ch = 0xb4;
  309. }
  310. break;
  311. }
  312. case 0xC2: {
  313. str ++;
  314. ch = *str;
  315. if (ch == 0xB0) { // °
  316. ch = 0xB0;
  317. }
  318. break;
  319. }
  320. }
  321. }
  322. #endif /* USE_UTF8 */
  323. ST7735_WriteChar(x, y, ch, font, color, bgcolor);
  324. x += font.width;
  325. str ++;
  326. ch = *str;
  327. }
  328. ST7735_Unselect();
  329. }
  330. /**
  331. * @brief Draw line or filled rectangle
  332. *
  333. * @param x from ST7735_XSTART to ST7735_WIDTH
  334. * @param y from ST7735_YSTART to ST7735_HEIGHT
  335. * @param w from 1 to ST7735_WIDTH
  336. * @param h from 1 to ST7735_HEIGHT
  337. * @param color 16-bit RGB565
  338. */
  339. void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  340. // clipping
  341. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  342. return;
  343. }
  344. if ((x + w - 1) >= ST7735_WIDTH) {
  345. w = ST7735_WIDTH - x;
  346. }
  347. if ((y + h - 1) >= ST7735_HEIGHT) {
  348. h = ST7735_HEIGHT - y;
  349. }
  350. ST7735_Select();
  351. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  352. for (y = h; y > 0; y--) {
  353. for (x = w; x > 0; x--) {
  354. ST7735_WriteData16(color);
  355. }
  356. }
  357. ST7735_Unselect();
  358. }
  359. void ST7735_FillScreen(uint16_t color) {
  360. ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  361. }
  362. void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
  363. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  364. return;
  365. }
  366. if ((x + w - 1) >= ST7735_WIDTH) {
  367. return;
  368. }
  369. if ((y + h - 1) >= ST7735_HEIGHT) {
  370. return;
  371. }
  372. ST7735_Select();
  373. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  374. ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
  375. ST7735_Unselect();
  376. }
  377. void ST7735_InvertColors(bool invert) {
  378. ST7735_Select();
  379. ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
  380. ST7735_Unselect();
  381. }
  382. void ST7735_SetGamma(GammaDef gamma) {
  383. ST7735_Select();
  384. ST7735_WriteCommand(ST7735_GAMSET);
  385. ST7735_WriteData((uint8_t *) &gamma, sizeof(gamma));
  386. ST7735_Unselect();
  387. }