st7735.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* vim: set ai et ts=4 sw=4: */
  2. #include "ch.h"
  3. #include "hal.h"
  4. #include "malloc.h"
  5. #include "string.h"
  6. #include "st7735.h"
  7. #define DELAY 0x80
  8. #define ST7735_Select() LCD_CS_RES
  9. #define ST7735_Unselect() LCD_CS_SET
  10. #define HAL_Delay(x) chThdSleepMilliseconds(x)
  11. static void ST7735_Reset(void);
  12. // based on Adafruit ST7735 library for Arduino
  13. static const uint8_t
  14. init_cmds1[] = { // Init for 7735R, part 1 (red or green tab)
  15. 15, // 15 commands in list:
  16. ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
  17. 150, // 150 ms delay
  18. ST7735_SLPOUT, DELAY, // 2: Out of sleep mode, 0 args, w/delay
  19. 255, // 500 ms delay
  20. ST7735_FRMCTR1, 3, // 3: Frame rate ctrl - normal mode, 3 args:
  21. 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
  22. ST7735_FRMCTR2, 3, // 4: Frame rate control - idle mode, 3 args:
  23. 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
  24. ST7735_FRMCTR3, 6, // 5: Frame rate ctrl - partial mode, 6 args:
  25. 0x01, 0x2C, 0x2D, // Dot inversion mode
  26. 0x01, 0x2C, 0x2D, // Line inversion mode
  27. ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg, no delay:
  28. 0x07, // No inversion
  29. ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay:
  30. 0xA2,
  31. 0x02, // -4.6V
  32. 0x84, // AUTO mode
  33. ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay:
  34. 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
  35. ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay:
  36. 0x0A, // Opamp current small
  37. 0x00, // Boost frequency
  38. ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay:
  39. 0x8A, // BCLK/2, Opamp current small & Medium low
  40. 0x2A,
  41. ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay:
  42. 0x8A, 0xEE,
  43. ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay:
  44. 0x0E,
  45. ST7735_INVOFF, 0, // 13: Don't invert display, no args, no delay
  46. ST7735_MADCTL, 1, // 14: Memory access control (directions), 1 arg:
  47. ST7735_ROTATION, // row addr/col addr, bottom to top refresh
  48. ST7735_COLMOD, 1, // 15: set color mode, 1 arg, no delay:
  49. 0x05 // 16-bit color
  50. },
  51. #if (defined(ST7735_IS_128X128) || defined(ST7735_IS_160X128))
  52. init_cmds2[] = { // Init for 7735R, part 2 (1.44" display)
  53. 2, // 2 commands in list:
  54. ST7735_CASET, 4, // 1: Column addr set, 4 args, no delay:
  55. 0x00, 0x00, // XSTART = 0
  56. 0x00, 0x7F, // XEND = 127
  57. ST7735_RASET, 4, // 2: Row addr set, 4 args, no delay:
  58. 0x00, 0x00, // XSTART = 0
  59. 0x00, 0x7F // XEND = 127
  60. },
  61. #endif // ST7735_IS_128X128
  62. #ifdef ST7735_IS_160X80
  63. init_cmds2[] = { // Init for 7735S, part 2 (160x80 display)
  64. 3, // 3 commands in list:
  65. ST7735_CASET, 4, // 1: Column addr set, 4 args, no delay:
  66. 0x00, 0x00, // XSTART = 0
  67. 0x00, 0x4F, // XEND = 79
  68. ST7735_RASET, 4, // 2: Row addr set, 4 args, no delay:
  69. 0x00, 0x00, // XSTART = 0
  70. 0x00, 0x9F, // XEND = 159
  71. ST7735_INVON, 0 // 3: Invert colors
  72. },
  73. #endif
  74. init_cmds3[] = { // Init for 7735R, part 3 (red or green tab)
  75. 4, // 4 commands in list:
  76. ST7735_GMCTRP1, 16, // 1: Gamma Adjustments (pos. polarity), 16 args, no delay:
  77. 0x02, 0x1c, 0x07, 0x12,
  78. 0x37, 0x32, 0x29, 0x2d,
  79. 0x29, 0x25, 0x2B, 0x39,
  80. 0x00, 0x01, 0x03, 0x10,
  81. ST7735_GMCTRN1, 16, // 2: Gamma Adjustments (neg. polarity), 16 args, no delay:
  82. 0x03, 0x1d, 0x07, 0x06,
  83. 0x2E, 0x2C, 0x29, 0x2D,
  84. 0x2E, 0x2E, 0x37, 0x3F,
  85. 0x00, 0x00, 0x02, 0x10,
  86. ST7735_NORON, DELAY, // 3: Normal display on, no args, w/delay
  87. 10, // 10 ms delay
  88. ST7735_DISPON, DELAY, // 4: Main screen turn on, no args w/delay
  89. 100 // 100 ms delay
  90. };
  91. static void ST7735_Reset(void) {
  92. //HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_RESET);
  93. HAL_Delay(5);
  94. //HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
  95. }
  96. static void ST7735_WriteCommand(uint8_t cmd) {
  97. LCD_DC_CMD;
  98. // !!! TODO: немає сенсу пересилати 1 байт за допомогою DMA. треба переробити
  99. spiSend(&ST7735_SPI_PORT, sizeof(cmd), &cmd);
  100. }
  101. static void ST7735_WriteData(uint8_t* buff, size_t buff_size) {
  102. LCD_DC_DATA;
  103. spiSend(&ST7735_SPI_PORT, buff_size, buff);
  104. }
  105. static void ST7735_ExecuteCommandList(const uint8_t *addr) {
  106. uint8_t numCommands, numArgs;
  107. uint16_t ms;
  108. numCommands = *addr ++;
  109. while (numCommands--) {
  110. uint8_t cmd = *addr ++;
  111. ST7735_WriteCommand(cmd);
  112. numArgs = *addr ++;
  113. // If high bit set, delay follows args
  114. ms = numArgs & DELAY;
  115. numArgs &= ~DELAY;
  116. if (numArgs) {
  117. ST7735_WriteData((uint8_t*)addr, numArgs);
  118. addr += numArgs;
  119. }
  120. if (ms) {
  121. ms = *addr ++;
  122. if (ms == 255) {
  123. ms = 500;
  124. }
  125. HAL_Delay(ms);
  126. }
  127. }
  128. }
  129. static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
  130. // column address set
  131. ST7735_WriteCommand(ST7735_CASET);
  132. uint8_t data[] = {0x00, x0 + ST7735_XSTART, 0x00, x1 + ST7735_XSTART};
  133. ST7735_WriteData(data, sizeof(data));
  134. // row address set
  135. ST7735_WriteCommand(ST7735_RASET);
  136. data[1] = y0 + ST7735_YSTART;
  137. data[3] = y1 + ST7735_YSTART;
  138. ST7735_WriteData(data, sizeof(data));
  139. // write to RAM
  140. ST7735_WriteCommand(ST7735_RAMWR);
  141. }
  142. void ST7735_Init() {
  143. ST7735_Select();
  144. ST7735_Reset();
  145. ST7735_ExecuteCommandList(init_cmds1);
  146. ST7735_ExecuteCommandList(init_cmds2);
  147. ST7735_ExecuteCommandList(init_cmds3);
  148. ST7735_Unselect();
  149. }
  150. void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color) {
  151. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  152. return;
  153. }
  154. ST7735_Select();
  155. ST7735_SetAddressWindow(x, y, x+1, y+1);
  156. uint8_t data[] = {color >> 8, color & 0xFF};
  157. // !!! TODO: тут пересилається 2 байти за допомогою DMA. треба переробити
  158. ST7735_WriteData(data, sizeof(data));
  159. ST7735_Unselect();
  160. }
  161. static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) {
  162. uint32_t i, b, j;
  163. ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
  164. for(i = 0; i < font.height; i++) {
  165. b = font.data[(ch - 32) * font.height + i];
  166. for(j = 0; j < font.width; j++) {
  167. //unnecessary transformations
  168. //if ((b << j) & 0x8000) {
  169. if (b & 0x8000) {
  170. uint8_t data[] = {color >> 8, color & 0xFF};
  171. // !!! TODO: тут пересилається 2 байти за допомогою DMA. треба переробити
  172. ST7735_WriteData(data, sizeof(data));
  173. } else {
  174. uint8_t data[] = {bgcolor >> 8, bgcolor & 0xFF};
  175. // !!! TODO: тут пересилається 2 байти за допомогою DMA. треба переробити
  176. ST7735_WriteData(data, sizeof(data));
  177. }
  178. b <<= 1;
  179. }
  180. }
  181. }
  182. void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
  183. ST7735_Select();
  184. while (*str) {
  185. if (x + font.width >= ST7735_WIDTH) {
  186. x = 0;
  187. y += font.height;
  188. if (y + font.height >= ST7735_HEIGHT) {
  189. break;
  190. }
  191. if (*str == ' ') {
  192. // skip spaces in the beginning of the new line
  193. str++;
  194. continue;
  195. }
  196. }
  197. ST7735_WriteChar(x, y, *str, font, color, bgcolor);
  198. x += font.width;
  199. str ++;
  200. }
  201. ST7735_Unselect();
  202. }
  203. void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  204. // clipping
  205. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  206. return;
  207. }
  208. if ((x + w - 1) >= ST7735_WIDTH) {
  209. w = ST7735_WIDTH - x;
  210. }
  211. if ((y + h - 1) >= ST7735_HEIGHT) {
  212. h = ST7735_HEIGHT - y;
  213. }
  214. ST7735_Select();
  215. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  216. uint8_t data[] = {color >> 8, color & 0xFF};
  217. LCD_DC_DATA;
  218. for (y = h; y > 0; y--) {
  219. for (x = w; x > 0; x--) {
  220. spiSend(&ST7735_SPI_PORT, sizeof(data), data);
  221. }
  222. }
  223. ST7735_Unselect();
  224. }
  225. void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  226. // clipping
  227. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  228. return;
  229. }
  230. if ((x + w - 1) >= ST7735_WIDTH) {
  231. w = ST7735_WIDTH - x;
  232. }
  233. if ((y + h - 1) >= ST7735_HEIGHT) {
  234. h = ST7735_HEIGHT - y;
  235. }
  236. ST7735_Select();
  237. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  238. // Prepare whole line in a single buffer
  239. uint8_t pixel[] = {color >> 8, color & 0xFF};
  240. uint8_t *line = malloc(w * sizeof(pixel));
  241. for (x = 0; x < w; ++x) {
  242. memcpy(line + x * sizeof(pixel), pixel, sizeof(pixel));
  243. }
  244. LCD_DC_DATA;
  245. for (y=h; y>0; y--) {
  246. spiSend(&ST7735_SPI_PORT, w * sizeof(pixel), line);
  247. }
  248. free(line);
  249. ST7735_Unselect();
  250. }
  251. void ST7735_FillScreen(uint16_t color) {
  252. ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  253. }
  254. void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
  255. if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
  256. return;
  257. }
  258. if ((x + w - 1) >= ST7735_WIDTH) {
  259. return;
  260. }
  261. if ((y + h - 1) >= ST7735_HEIGHT) {
  262. return;
  263. }
  264. ST7735_Select();
  265. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  266. ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
  267. ST7735_Unselect();
  268. }
  269. void ST7735_InvertColors(bool invert) {
  270. ST7735_Select();
  271. ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
  272. ST7735_Unselect();
  273. }
  274. void ST7735_SetGamma(GammaDef gamma) {
  275. ST7735_Select();
  276. ST7735_WriteCommand(ST7735_GAMSET);
  277. ST7735_WriteData((uint8_t *) &gamma, sizeof(gamma));
  278. ST7735_Unselect();
  279. }
  280. void ST7735_Test(void) {
  281. // Check border
  282. ST7735_FillScreen(ST7735_BLACK);
  283. for (int x=0; x<ST7735_WIDTH; x++) {
  284. ST7735_DrawPixel(x, 0, ST7735_RED);
  285. ST7735_DrawPixel(x, ST7735_HEIGHT-1, ST7735_RED);
  286. }
  287. for(int y=0; y<ST7735_HEIGHT; y++) {
  288. ST7735_DrawPixel(0, y, ST7735_RED);
  289. ST7735_DrawPixel(ST7735_WIDTH-1, y, ST7735_RED);
  290. }
  291. chThdSleepMilliseconds(3000);
  292. // Check fonts
  293. ST7735_FillScreen(ST7735_BLACK);
  294. ST7735_WriteString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_RED, ST7735_BLACK);
  295. ST7735_WriteString(0, 3*10, "Font_11x18, green, lorem ipsum", Font_11x18, ST7735_GREEN, ST7735_BLACK);
  296. ST7735_WriteString(0, 3*10+3*18, "Font_16x26", Font_16x26, ST7735_BLUE, ST7735_BLACK);
  297. chThdSleepMilliseconds(5000);
  298. // Check colors
  299. ST7735_FillScreen(ST7735_BLACK);
  300. ST7735_WriteString(0, 0, "BLACK", Font_11x18, ST7735_WHITE, ST7735_BLACK);
  301. chThdSleepMilliseconds(500);
  302. ST7735_FillScreen(ST7735_BLUE);
  303. ST7735_WriteString(0, 0, "BLUE", Font_11x18, ST7735_BLACK, ST7735_BLUE);
  304. chThdSleepMilliseconds(500);
  305. ST7735_FillScreen(ST7735_RED);
  306. ST7735_WriteString(0, 0, "RED", Font_11x18, ST7735_BLACK, ST7735_RED);
  307. chThdSleepMilliseconds(500);
  308. ST7735_FillScreen(ST7735_GREEN);
  309. ST7735_WriteString(0, 0, "GREEN", Font_11x18, ST7735_BLACK, ST7735_GREEN);
  310. chThdSleepMilliseconds(500);
  311. ST7735_FillScreen(ST7735_CYAN);
  312. ST7735_WriteString(0, 0, "CYAN", Font_11x18, ST7735_BLACK, ST7735_CYAN);
  313. chThdSleepMilliseconds(500);
  314. ST7735_FillScreen(ST7735_MAGENTA);
  315. ST7735_WriteString(0, 0, "MAGENTA", Font_11x18, ST7735_BLACK, ST7735_MAGENTA);
  316. chThdSleepMilliseconds(500);
  317. ST7735_FillScreen(ST7735_YELLOW);
  318. ST7735_WriteString(0, 0, "YELLOW", Font_11x18, ST7735_BLACK, ST7735_YELLOW);
  319. chThdSleepMilliseconds(500);
  320. ST7735_FillScreen(ST7735_WHITE);
  321. ST7735_WriteString(0, 0, "WHITE", Font_11x18, ST7735_BLACK, ST7735_WHITE);
  322. chThdSleepMilliseconds(500);
  323. chThdSleepMilliseconds(15000);
  324. }