st7735.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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
  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. static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) {
  204. uint32_t i, b, j;
  205. ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
  206. for(i = 0; i < font.height; i++) {
  207. b = font.data[(ch - 32) * font.height + i];
  208. for(j = 0; j < font.width; j++) {
  209. if (b & 0x8000) {
  210. ST7735_WriteData16(color);
  211. } else {
  212. ST7735_WriteData16(bgcolor);
  213. }
  214. b <<= 1;
  215. }
  216. }
  217. }
  218. void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
  219. ST7735_Select();
  220. while (*str) {
  221. if (x + font.width >= ST7735_WIDTH) {
  222. x = 0;
  223. y += font.height;
  224. if (y + font.height >= ST7735_HEIGHT) {
  225. break;
  226. }
  227. if (*str == ' ') {
  228. // skip spaces in the beginning of the new line
  229. str ++;
  230. continue;
  231. }
  232. }
  233. ST7735_WriteChar(x, y, *str, font, color, bgcolor);
  234. x += font.width;
  235. str ++;
  236. }
  237. ST7735_Unselect();
  238. }
  239. void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  240. // clipping
  241. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  242. return;
  243. }
  244. if ((x + w - 1) >= ST7735_WIDTH) {
  245. w = ST7735_WIDTH - x;
  246. }
  247. if ((y + h - 1) >= ST7735_HEIGHT) {
  248. h = ST7735_HEIGHT - y;
  249. }
  250. ST7735_Select();
  251. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  252. LCD_DC_DATA;
  253. for (y = h; y > 0; y--) {
  254. for (x = w; x > 0; x--) {
  255. ST7735_WriteData16(color);
  256. }
  257. }
  258. ST7735_Unselect();
  259. }
  260. void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  261. // clipping
  262. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  263. return;
  264. }
  265. if ((x + w - 1) >= ST7735_WIDTH) {
  266. w = ST7735_WIDTH - x;
  267. }
  268. if ((y + h - 1) >= ST7735_HEIGHT) {
  269. h = ST7735_HEIGHT - y;
  270. }
  271. ST7735_Select();
  272. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  273. // Prepare whole line in a single buffer
  274. uint8_t pixel[] = {color >> 8, color & 0xFF};
  275. uint8_t *line = malloc(w * sizeof(pixel));
  276. for (x = 0; x < w; ++x) {
  277. memcpy(line + x * sizeof(pixel), pixel, sizeof(pixel));
  278. }
  279. LCD_DC_DATA;
  280. for (y=h; y>0; y--) {
  281. spiSend(&ST7735_SPI_PORT, w * sizeof(pixel), line);
  282. }
  283. free(line);
  284. ST7735_Unselect();
  285. }
  286. void ST7735_FillScreen(uint16_t color) {
  287. ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  288. }
  289. void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
  290. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  291. return;
  292. }
  293. if ((x + w - 1) >= ST7735_WIDTH) {
  294. return;
  295. }
  296. if ((y + h - 1) >= ST7735_HEIGHT) {
  297. return;
  298. }
  299. ST7735_Select();
  300. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  301. ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
  302. ST7735_Unselect();
  303. }
  304. void ST7735_InvertColors(bool invert) {
  305. ST7735_Select();
  306. ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
  307. ST7735_Unselect();
  308. }
  309. void ST7735_SetGamma(GammaDef gamma) {
  310. ST7735_Select();
  311. ST7735_WriteCommand(ST7735_GAMSET);
  312. ST7735_WriteData((uint8_t *) &gamma, sizeof(gamma));
  313. ST7735_Unselect();
  314. }
  315. void ST7735_Test(void) {
  316. // Check border
  317. ST7735_FillScreen(ST7735_BLACK);
  318. for (int x=0; x<ST7735_WIDTH; x++) {
  319. ST7735_DrawPixel(x, 0, ST7735_RED);
  320. ST7735_DrawPixel(x, ST7735_HEIGHT-1, ST7735_RED);
  321. }
  322. for(int y=0; y<ST7735_HEIGHT; y++) {
  323. ST7735_DrawPixel(0, y, ST7735_RED);
  324. ST7735_DrawPixel(ST7735_WIDTH-1, y, ST7735_RED);
  325. }
  326. chThdSleepMilliseconds(3000);
  327. // Check fonts
  328. ST7735_FillScreen(ST7735_BLACK);
  329. ST7735_WriteString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_RED, ST7735_BLACK);
  330. ST7735_WriteString(0, 3*10, "Font_11x18, green, lorem ipsum", Font_11x18, ST7735_GREEN, ST7735_BLACK);
  331. ST7735_WriteString(0, 3*10+3*18, "Font_16x26", Font_16x26, ST7735_BLUE, ST7735_BLACK);
  332. chThdSleepMilliseconds(5000);
  333. // Check colors
  334. ST7735_FillScreen(ST7735_BLACK);
  335. ST7735_WriteString(0, 0, "BLACK", Font_11x18, ST7735_WHITE, ST7735_BLACK);
  336. chThdSleepMilliseconds(500);
  337. ST7735_FillScreen(ST7735_BLUE);
  338. ST7735_WriteString(0, 0, "BLUE", Font_11x18, ST7735_BLACK, ST7735_BLUE);
  339. chThdSleepMilliseconds(500);
  340. ST7735_FillScreen(ST7735_RED);
  341. ST7735_WriteString(0, 0, "RED", Font_11x18, ST7735_BLACK, ST7735_RED);
  342. chThdSleepMilliseconds(500);
  343. ST7735_FillScreen(ST7735_GREEN);
  344. ST7735_WriteString(0, 0, "GREEN", Font_11x18, ST7735_BLACK, ST7735_GREEN);
  345. chThdSleepMilliseconds(500);
  346. ST7735_FillScreen(ST7735_CYAN);
  347. ST7735_WriteString(0, 0, "CYAN", Font_11x18, ST7735_BLACK, ST7735_CYAN);
  348. chThdSleepMilliseconds(500);
  349. ST7735_FillScreen(ST7735_MAGENTA);
  350. ST7735_WriteString(0, 0, "MAGENTA", Font_11x18, ST7735_BLACK, ST7735_MAGENTA);
  351. chThdSleepMilliseconds(500);
  352. ST7735_FillScreen(ST7735_YELLOW);
  353. ST7735_WriteString(0, 0, "YELLOW", Font_11x18, ST7735_BLACK, ST7735_YELLOW);
  354. chThdSleepMilliseconds(500);
  355. ST7735_FillScreen(ST7735_WHITE);
  356. ST7735_WriteString(0, 0, "WHITE", Font_11x18, ST7735_BLACK, ST7735_WHITE);
  357. chThdSleepMilliseconds(500);
  358. chThdSleepMilliseconds(15000);
  359. }