st7735.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* vim: set ai et ts=4 sw=4: */
  2. #include "stm32f4xx_hal.h"
  3. #include "st7735.h"
  4. #define DELAY 0x80
  5. // based on Adafruit ST7735 library for Arduino
  6. static const uint8_t
  7. init_cmds1[] = { // Init for 7735R, part 1 (red or green tab)
  8. 15, // 15 commands in list:
  9. ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
  10. 150, // 150 ms delay
  11. ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
  12. 255, // 500 ms delay
  13. ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
  14. 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
  15. ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
  16. 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
  17. ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
  18. 0x01, 0x2C, 0x2D, // Dot inversion mode
  19. 0x01, 0x2C, 0x2D, // Line inversion mode
  20. ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
  21. 0x07, // No inversion
  22. ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
  23. 0xA2,
  24. 0x02, // -4.6V
  25. 0x84, // AUTO mode
  26. ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
  27. 0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
  28. ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
  29. 0x0A, // Opamp current small
  30. 0x00, // Boost frequency
  31. ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
  32. 0x8A, // BCLK/2, Opamp current small & Medium low
  33. 0x2A,
  34. ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
  35. 0x8A, 0xEE,
  36. ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
  37. 0x0E,
  38. ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
  39. ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
  40. ST7735_ROTATION, // row addr/col addr, bottom to top refresh
  41. ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
  42. 0x05 }, // 16-bit color
  43. #if (defined(ST7735_IS_128X128) || defined(ST7735_IS_160X128))
  44. init_cmds2[] = { // Init for 7735R, part 2 (1.44" display)
  45. 2, // 2 commands in list:
  46. ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
  47. 0x00, 0x00, // XSTART = 0
  48. 0x00, 0x7F, // XEND = 127
  49. ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
  50. 0x00, 0x00, // XSTART = 0
  51. 0x00, 0x7F }, // XEND = 127
  52. #endif // ST7735_IS_128X128
  53. #ifdef ST7735_IS_160X80
  54. init_cmds2[] = { // Init for 7735S, part 2 (160x80 display)
  55. 3, // 3 commands in list:
  56. ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
  57. 0x00, 0x00, // XSTART = 0
  58. 0x00, 0x4F, // XEND = 79
  59. ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
  60. 0x00, 0x00, // XSTART = 0
  61. 0x00, 0x9F , // XEND = 159
  62. ST7735_INVON, 0 }, // 3: Invert colors
  63. #endif
  64. init_cmds3[] = { // Init for 7735R, part 3 (red or green tab)
  65. 4, // 4 commands in list:
  66. ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
  67. 0x02, 0x1c, 0x07, 0x12,
  68. 0x37, 0x32, 0x29, 0x2d,
  69. 0x29, 0x25, 0x2B, 0x39,
  70. 0x00, 0x01, 0x03, 0x10,
  71. ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
  72. 0x03, 0x1d, 0x07, 0x06,
  73. 0x2E, 0x2C, 0x29, 0x2D,
  74. 0x2E, 0x2E, 0x37, 0x3F,
  75. 0x00, 0x00, 0x02, 0x10,
  76. ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
  77. 10, // 10 ms delay
  78. ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
  79. 100 }; // 100 ms delay
  80. static void ST7735_Select() {
  81. HAL_GPIO_WritePin(ST7735_CS_GPIO_Port, ST7735_CS_Pin, GPIO_PIN_RESET);
  82. }
  83. void ST7735_Unselect() {
  84. HAL_GPIO_WritePin(ST7735_CS_GPIO_Port, ST7735_CS_Pin, GPIO_PIN_SET);
  85. }
  86. static void ST7735_Reset() {
  87. HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_RESET);
  88. HAL_Delay(5);
  89. HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
  90. }
  91. static void ST7735_WriteCommand(uint8_t cmd) {
  92. HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_RESET);
  93. HAL_SPI_Transmit(&ST7735_SPI_PORT, &cmd, sizeof(cmd), HAL_MAX_DELAY);
  94. }
  95. static void ST7735_WriteData(uint8_t* buff, size_t buff_size) {
  96. HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET);
  97. HAL_SPI_Transmit(&ST7735_SPI_PORT, buff, buff_size, HAL_MAX_DELAY);
  98. }
  99. static void ST7735_ExecuteCommandList(const uint8_t *addr) {
  100. uint8_t numCommands, numArgs;
  101. uint16_t ms;
  102. numCommands = *addr++;
  103. while(numCommands--) {
  104. uint8_t cmd = *addr++;
  105. ST7735_WriteCommand(cmd);
  106. numArgs = *addr++;
  107. // If high bit set, delay follows args
  108. ms = numArgs & DELAY;
  109. numArgs &= ~DELAY;
  110. if(numArgs) {
  111. ST7735_WriteData((uint8_t*)addr, numArgs);
  112. addr += numArgs;
  113. }
  114. if(ms) {
  115. ms = *addr++;
  116. if(ms == 255) ms = 500;
  117. HAL_Delay(ms);
  118. }
  119. }
  120. }
  121. static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
  122. // column address set
  123. ST7735_WriteCommand(ST7735_CASET);
  124. uint8_t data[] = { 0x00, x0 + ST7735_XSTART, 0x00, x1 + ST7735_XSTART };
  125. ST7735_WriteData(data, sizeof(data));
  126. // row address set
  127. ST7735_WriteCommand(ST7735_RASET);
  128. data[1] = y0 + ST7735_YSTART;
  129. data[3] = y1 + ST7735_YSTART;
  130. ST7735_WriteData(data, sizeof(data));
  131. // write to RAM
  132. ST7735_WriteCommand(ST7735_RAMWR);
  133. }
  134. void ST7735_Init() {
  135. ST7735_Select();
  136. ST7735_Reset();
  137. ST7735_ExecuteCommandList(init_cmds1);
  138. ST7735_ExecuteCommandList(init_cmds2);
  139. ST7735_ExecuteCommandList(init_cmds3);
  140. ST7735_Unselect();
  141. }
  142. void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color) {
  143. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT))
  144. return;
  145. ST7735_Select();
  146. ST7735_SetAddressWindow(x, y, x+1, y+1);
  147. uint8_t data[] = { color >> 8, color & 0xFF };
  148. ST7735_WriteData(data, sizeof(data));
  149. ST7735_Unselect();
  150. }
  151. static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) {
  152. uint32_t i, b, j;
  153. ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
  154. for(i = 0; i < font.height; i++) {
  155. b = font.data[(ch - 32) * font.height + i];
  156. for(j = 0; j < font.width; j++) {
  157. if((b << j) & 0x8000) {
  158. uint8_t data[] = { color >> 8, color & 0xFF };
  159. ST7735_WriteData(data, sizeof(data));
  160. } else {
  161. uint8_t data[] = { bgcolor >> 8, bgcolor & 0xFF };
  162. ST7735_WriteData(data, sizeof(data));
  163. }
  164. }
  165. }
  166. }
  167. /*
  168. Simpler (and probably slower) implementation:
  169. static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color) {
  170. uint32_t i, b, j;
  171. for(i = 0; i < font.height; i++) {
  172. b = font.data[(ch - 32) * font.height + i];
  173. for(j = 0; j < font.width; j++) {
  174. if((b << j) & 0x8000) {
  175. ST7735_DrawPixel(x + j, y + i, color);
  176. }
  177. }
  178. }
  179. }
  180. */
  181. void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
  182. ST7735_Select();
  183. while(*str) {
  184. if(x + font.width >= ST7735_WIDTH) {
  185. x = 0;
  186. y += font.height;
  187. if(y + font.height >= ST7735_HEIGHT) {
  188. break;
  189. }
  190. if(*str == ' ') {
  191. // skip spaces in the beginning of the new line
  192. str++;
  193. continue;
  194. }
  195. }
  196. ST7735_WriteChar(x, y, *str, font, color, bgcolor);
  197. x += font.width;
  198. str++;
  199. }
  200. ST7735_Unselect();
  201. }
  202. void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
  203. // clipping
  204. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
  205. if((x + w - 1) >= ST7735_WIDTH) w = ST7735_WIDTH - x;
  206. if((y + h - 1) >= ST7735_HEIGHT) h = ST7735_HEIGHT - y;
  207. ST7735_Select();
  208. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  209. uint8_t data[] = { color >> 8, color & 0xFF };
  210. HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET);
  211. for(y = h; y > 0; y--) {
  212. for(x = w; x > 0; x--) {
  213. HAL_SPI_Transmit(&ST7735_SPI_PORT, data, sizeof(data), HAL_MAX_DELAY);
  214. }
  215. }
  216. ST7735_Unselect();
  217. }
  218. void ST7735_FillScreen(uint16_t color) {
  219. ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
  220. }
  221. void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) {
  222. if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return;
  223. if((x + w - 1) >= ST7735_WIDTH) return;
  224. if((y + h - 1) >= ST7735_HEIGHT) return;
  225. ST7735_Select();
  226. ST7735_SetAddressWindow(x, y, x+w-1, y+h-1);
  227. ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h);
  228. ST7735_Unselect();
  229. }
  230. void ST7735_InvertColors(bool invert) {
  231. ST7735_Select();
  232. ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF);
  233. ST7735_Unselect();
  234. }