st7735.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #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. #endif // ST7735_IS_128X128
  60. #ifdef ST7735_IS_160X80
  61. init_cmds2[] = { // Init for 7735S, part 2 (160x80 display)
  62. 3, // 3 commands in list:
  63. ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
  64. 0x00, 0x00, // XSTART = 0
  65. 0x00, 0x4F, // XEND = 79
  66. ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
  67. 0x00, 0x00, // XSTART = 0
  68. 0x00, 0x9F , // XEND = 159
  69. ST7735_INVON, 0 }, // 3: Invert colors
  70. #endif
  71. init_cmds3[] = { // Init for 7735R, part 3 (red or green tab)
  72. 4, // 4 commands in list:
  73. ST7735_GMCTRP1, 16 , // 1: Gamma Adjustments (pos. polarity), 16 args, no delay:
  74. 0x02, 0x1c, 0x07, 0x12,
  75. 0x37, 0x32, 0x29, 0x2d,
  76. 0x29, 0x25, 0x2B, 0x39,
  77. 0x00, 0x01, 0x03, 0x10,
  78. ST7735_GMCTRN1, 16 , // 2: Gamma Adjustments (neg. polarity), 16 args, no delay:
  79. 0x03, 0x1d, 0x07, 0x06,
  80. 0x2E, 0x2C, 0x29, 0x2D,
  81. 0x2E, 0x2E, 0x37, 0x3F,
  82. 0x00, 0x00, 0x02, 0x10,
  83. ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
  84. 10, // 10 ms delay
  85. ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
  86. 100 }; // 100 ms delay
  87. static void ST7735_Reset(void) {
  88. // HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_RESET);
  89. HAL_Delay(5);
  90. // HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
  91. }
  92. static void ST7735_WriteCommand(uint8_t cmd) {
  93. LCD_DC_CMD;
  94. spiSend(&ST7735_SPI_PORT, sizeof(cmd), &cmd);
  95. }
  96. static void ST7735_WriteData(uint8_t* buff, size_t buff_size) {
  97. LCD_DC_DATA;
  98. spiSend(&ST7735_SPI_PORT, buff_size, buff);
  99. }
  100. static void ST7735_ExecuteCommandList(const uint8_t *addr) {
  101. uint8_t numCommands, numArgs;
  102. uint16_t ms;
  103. numCommands = *addr++;
  104. while(numCommands--) {
  105. uint8_t cmd = *addr++;
  106. ST7735_WriteCommand(cmd);
  107. numArgs = *addr++;
  108. // If high bit set, delay follows args
  109. ms = numArgs & DELAY;
  110. numArgs &= ~DELAY;
  111. if(numArgs) {
  112. ST7735_WriteData((uint8_t*)addr, numArgs);
  113. addr += numArgs;
  114. }
  115. if(ms) {
  116. ms = *addr++;
  117. if(ms == 255) ms = 500;
  118. HAL_Delay(ms);
  119. }
  120. }
  121. }
  122. static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
  123. // column address set
  124. ST7735_WriteCommand(ST7735_CASET);
  125. uint8_t data[] = { 0x00, x0 + ST7735_XSTART, 0x00, x1 + ST7735_XSTART };
  126. ST7735_WriteData(data, sizeof(data));
  127. // row address set
  128. ST7735_WriteCommand(ST7735_RASET);
  129. data[1] = y0 + ST7735_YSTART;
  130. data[3] = y1 + ST7735_YSTART;
  131. ST7735_WriteData(data, sizeof(data));
  132. // write to RAM
  133. ST7735_WriteCommand(ST7735_RAMWR);
  134. }
  135. void ST7735_Init() {
  136. ST7735_Select();
  137. ST7735_Reset();
  138. ST7735_ExecuteCommandList(init_cmds1);
  139. ST7735_ExecuteCommandList(init_cmds2);
  140. ST7735_ExecuteCommandList(init_cmds3);
  141. ST7735_Unselect();
  142. }
  143. void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color) {
  144. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT))
  145. return;
  146. ST7735_Select();
  147. ST7735_SetAddressWindow(x, y, x+1, y+1);
  148. uint8_t data[] = { color >> 8, color & 0xFF };
  149. ST7735_WriteData(data, sizeof(data));
  150. ST7735_Unselect();
  151. }
  152. static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) {
  153. uint32_t i, b, j;
  154. ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
  155. for(i = 0; i < font.height; i++) {
  156. b = font.data[(ch - 32) * font.height + i];
  157. for(j = 0; j < font.width; j++) {
  158. if((b << j) & 0x8000) {
  159. uint8_t data[] = { color >> 8, color & 0xFF };
  160. ST7735_WriteData(data, sizeof(data));
  161. } else {
  162. uint8_t data[] = { bgcolor >> 8, bgcolor & 0xFF };
  163. ST7735_WriteData(data, sizeof(data));
  164. }
  165. }
  166. }
  167. }
  168. /*
  169. Simpler (and probably slower) implementation:
  170. static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color) {
  171. uint32_t i, b, j;
  172. for(i = 0; i < font.height; i++) {
  173. b = font.data[(ch - 32) * font.height + i];
  174. for(j = 0; j < font.width; j++) {
  175. if((b << j) & 0x8000) {
  176. ST7735_DrawPixel(x + j, y + i, color);
  177. }
  178. }
  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)) return;
  206. if((x + w - 1) >= ST7735_WIDTH) w = ST7735_WIDTH - x;
  207. if((y + h - 1) >= ST7735_HEIGHT) h = ST7735_HEIGHT - y;
  208. ST7735_Select();
  209. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  210. uint8_t data[] = { color >> 8, color & 0xFF };
  211. LCD_DC_DATA;
  212. for(y = h; y > 0; y--) {
  213. for(x = w; x > 0; x--) {
  214. spiSend(&ST7735_SPI_PORT, sizeof(data), data);
  215. }
  216. }
  217. ST7735_Unselect();
  218. }
  219. void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  220. // clipping
  221. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
  222. if((x + w - 1) >= ST7735_WIDTH) w = ST7735_WIDTH - x;
  223. if((y + h - 1) >= ST7735_HEIGHT) h = ST7735_HEIGHT - y;
  224. ST7735_Select();
  225. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  226. // Prepare whole line in a single buffer
  227. uint8_t pixel[] = { color >> 8, color & 0xFF };
  228. uint8_t *line = malloc(w * sizeof(pixel));
  229. for(x = 0; x < w; ++x)
  230. memcpy(line + x * sizeof(pixel), pixel, sizeof(pixel));
  231. LCD_DC_DATA;
  232. for(y = h; y > 0; y--)
  233. spiSend(&ST7735_SPI_PORT, w * sizeof(pixel), line);
  234. free(line);
  235. ST7735_Unselect();
  236. }
  237. void ST7735_FillScreen(uint16_t color) {
  238. ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  239. }
  240. void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
  241. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
  242. if((x + w - 1) >= ST7735_WIDTH) return;
  243. if((y + h - 1) >= ST7735_HEIGHT) return;
  244. ST7735_Select();
  245. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  246. ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
  247. ST7735_Unselect();
  248. }
  249. void ST7735_InvertColors(bool invert) {
  250. ST7735_Select();
  251. ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
  252. ST7735_Unselect();
  253. }
  254. void ST7735_SetGamma(GammaDef gamma) {
  255. ST7735_Select();
  256. ST7735_WriteCommand(ST7735_GAMSET);
  257. ST7735_WriteData((uint8_t *) &gamma, sizeof(gamma));
  258. ST7735_Unselect();
  259. }
  260. void ST7735_Test(void) {
  261. // Check border
  262. ST7735_FillScreen(ST7735_BLACK);
  263. for(int x = 0; x < ST7735_WIDTH; x++) {
  264. ST7735_DrawPixel(x, 0, ST7735_RED);
  265. ST7735_DrawPixel(x, ST7735_HEIGHT-1, ST7735_RED);
  266. }
  267. for(int y = 0; y < ST7735_HEIGHT; y++) {
  268. ST7735_DrawPixel(0, y, ST7735_RED);
  269. ST7735_DrawPixel(ST7735_WIDTH-1, y, ST7735_RED);
  270. }
  271. chThdSleepMilliseconds(3000);
  272. // Check fonts
  273. ST7735_FillScreen(ST7735_BLACK);
  274. ST7735_WriteString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_RED, ST7735_BLACK);
  275. ST7735_WriteString(0, 3*10, "Font_11x18, green, lorem ipsum", Font_11x18, ST7735_GREEN, ST7735_BLACK);
  276. ST7735_WriteString(0, 3*10+3*18, "Font_16x26", Font_16x26, ST7735_BLUE, ST7735_BLACK);
  277. chThdSleepMilliseconds(5000);
  278. // Check colors
  279. ST7735_FillScreen(ST7735_BLACK);
  280. ST7735_WriteString(0, 0, "BLACK", Font_11x18, ST7735_WHITE, ST7735_BLACK);
  281. chThdSleepMilliseconds(500);
  282. ST7735_FillScreen(ST7735_BLUE);
  283. ST7735_WriteString(0, 0, "BLUE", Font_11x18, ST7735_BLACK, ST7735_BLUE);
  284. chThdSleepMilliseconds(500);
  285. ST7735_FillScreen(ST7735_RED);
  286. ST7735_WriteString(0, 0, "RED", Font_11x18, ST7735_BLACK, ST7735_RED);
  287. chThdSleepMilliseconds(500);
  288. ST7735_FillScreen(ST7735_GREEN);
  289. ST7735_WriteString(0, 0, "GREEN", Font_11x18, ST7735_BLACK, ST7735_GREEN);
  290. chThdSleepMilliseconds(500);
  291. ST7735_FillScreen(ST7735_CYAN);
  292. ST7735_WriteString(0, 0, "CYAN", Font_11x18, ST7735_BLACK, ST7735_CYAN);
  293. chThdSleepMilliseconds(500);
  294. ST7735_FillScreen(ST7735_MAGENTA);
  295. ST7735_WriteString(0, 0, "MAGENTA", Font_11x18, ST7735_BLACK, ST7735_MAGENTA);
  296. chThdSleepMilliseconds(500);
  297. ST7735_FillScreen(ST7735_YELLOW);
  298. ST7735_WriteString(0, 0, "YELLOW", Font_11x18, ST7735_BLACK, ST7735_YELLOW);
  299. chThdSleepMilliseconds(500);
  300. ST7735_FillScreen(ST7735_WHITE);
  301. ST7735_WriteString(0, 0, "WHITE", Font_11x18, ST7735_BLACK, ST7735_WHITE);
  302. chThdSleepMilliseconds(500);
  303. chThdSleepMilliseconds(15000);
  304. }