1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/9/14 - ROBIN KRENS
19 #include <sys/robsys.h>
21 #include <lib/regfunc.h>
22 #include <lib/string.h>
23 #include <lib/tinyprintf.h>
24 #include <lib/fonts/wogfont.h>
26 #include <drivers/uart.h>
27 #include <drivers/st7735s.h>
34 #define XPOS(x) (x * 6)
35 #define YPOS(y) (y * 8)
51 tftscreen.textmemptr = tftscreen.buf;
53 memset(tftscreen.buf, 0x00, 352);
55 /* Peripherial init */
56 rsetbit(RCC_APB1ENR, 14); // enable SPI2
57 rsetbit(RCC_APB2ENR, 3); // enable GPIOB
58 rsetbit(RCC_APB2ENR, 4); // enable GPIOC
60 /* The PINS used are PB12, PB13, PB15 and PC6 respectively
61 * NSS (or CS): alternative function pusp-pull
62 * NSS Output is (always high) enabled with this setting
63 * SCK Master: alternate function push-pull
64 * MOSI (or DI): alternate function push-pull
65 * D/CX (or A0): Command or data write line PC6 */
66 rwrite(GPIOB_CRH, 0xA4AA4444);
67 rwrite(GPIOC_CRL, 0x42444444);
69 /* Chip select: software enabled
70 * In case for a hardware setup, connect NSS (CS) to
75 rsetbit(SPI2_CR1, 15); // one-wire mode
76 rsetbit(SPI2_CR1, 14); // start with transfer
77 rsetbit(SPI2_CR1, 4); // FPLCK div 8
78 rsetbit(SPI2_CR1, 2); // master selection
79 rsetbit(SPI2_CR1, 6); // enable SPI
82 tft_command(TFT_SWRESET, 0);
87 /* Frame rate control */
88 tft_command(TFT_FRMCTR1, 3, 0x01, 0x2C, 0x2D);
89 tft_command(TFT_FRMCTR2, 3, 0x01, 0x2C, 0x2D);
90 tft_command(TFT_FRMCTR3, 6, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D);
93 tft_command(TFT_PWCTR1, 3, 0xA2, 0x02, 0x84);
94 tft_command(TFT_PWCTR2, 1, 0xC5);
95 tft_command(TFT_PWCTR3, 2, 0x0A, 0x00);
96 tft_command(TFT_PWCTR4, 2, 0x8A, 0x2A);
97 tft_command(TFT_PWCTR5, 2, 0x8A, 0xEE);
98 tft_command(TFT_VMCTR1, 1, 0x0E);
100 tft_command(TFT_INVOFF, 0);
101 tft_command(TFT_COLMOD, 1, 0x05); // 0x05
102 tft_command(TFT_MADCTL, 1, 0xC0); // TODO: check
104 tft_command(TFT_CASET, 4, 0x00, 0x00, 0x00, 0x7F);
105 tft_command(TFT_RASET, 4, 0x00, 0x00, 0x00, 0x9F);
108 tft_command(TFT_GMCTRP1, 16, 0x02, 0x1C, 0x07, 0x12,
109 0x37, 0x32, 0x29, 0x2D,
110 0x29, 0x25, 0x2B, 0x39,
111 0x00, 0x01, 0x03, 0x10);
112 tft_command(TFT_GMCTRN1, 16, 0x03, 0x1D, 0x07, 0x06,
113 0x2E, 0x2C, 0x29, 0x2D,
114 0x2E, 0x2E, 0x37, 0x3F,
115 0x00, 0x00, 0x02, 0x10);
117 /* Before turning on the display, fill the display
118 * so no random display data is shown */
119 tft_fill(0,0,SCRWIDTH-1,SCRHEIGHT-1,0x0000);
122 tft_command(TFT_NORON, 0);
124 tft_command(TFT_DISPON, 0);
129 /* Helper function */
130 static int txbuf_empty () {
132 while(!rchkbit(SPI2_SR, 1)) {
135 printf("Error: transmit timeout!\n");
144 /* Function to fill an area starting at (beginx, beginy)
145 * and end ending at (endx, endy */
146 int tft_fill(uint8_t beginx, uint8_t beginy, uint8_t endx,
147 uint8_t endy, uint16_t color) {
149 /* The CASET and RASET commands set the begin X/Y
150 * and end X/Y of the fill area. Both are split over
151 * two paramters/bytes. Most screen are small, so you will
152 * only need to consider the first 8 MSBs. Parameter 1 and
155 * In theory, TFT screens of 65,535 by 65,535 could be
157 tft_command(TFT_CASET, 4, 0x00, beginx, 0x00, endx);
158 tft_command(TFT_RASET, 4, 0x00, beginy, 0x00, endy);
160 /* After setting the fill area, we can send the color
161 * data. The chip autoincrements the address so we can
162 * keep writing continiously for the fill area. Note
163 * that each pixel requires two bytes to be send (16
165 uint32_t totalwrites = (endx - beginx) * (endy - beginy) * 2;
166 //printf("tw: %d, x: %d, y: %d\n", totalwrites, beginx, beginy);
167 tft_command(TFT_RAMWR, 0);
168 rsetbit(GPIOC_ODR, 6); // data = 1
169 for (int i = 0; i < totalwrites; i++) {
170 rwrite(SPI2_DR, (uint8_t) (color >> 8));
173 rwrite(SPI2_DR, (uint8_t) (color & 0xFF));
180 /* Function to individually set a pixel
181 * Refer to tft_fill, similar calls */
182 int tft_setpixel(uint8_t x, uint8_t y, uint16_t color) {
184 tft_command(TFT_CASET, 4, 0x00, x, 0x00, x+1);
185 tft_command(TFT_RASET, 4, 0x00, y, 0x00, y+1);
186 tft_command(TFT_RAMWR, 2, (uint8_t) (color >> 8), (uint8_t) (color & 0xFF));
191 int tft_puts(char * str) {
193 for (int i = 0; i < strlen(str); i++) {
194 tft_putc(0xFFFF, 0x0000, str[i]);
203 tftscreen.buf[BUFFER - 21] = '\0';
204 tftscreen.cpos -= 21;
208 /* Text scroll function
209 * Refeed the buffer */
212 /* Scroll the buffer */
213 memcpy(tftscreen.textmemptr, tftscreen.textmemptr + 21, BUFFER - 21);
214 //for (int i = 21; i >= 0; i--)
215 tftscreen.buf[BUFFER - 21] = '\0';
221 //for (int i = 0; i < 320; i++) {
222 // uart_putc(tftscreen.buf[i]);
225 tft_puts(tftscreen.buf); // CHECK: ending
230 tftscreen.cpos = BUFFER - 21;
231 // DINOSAUR tft_clrln();
236 uint8_t blanks = 21 - tftscreen.x;
238 // filler with blank spaces
239 for (int i = 0; i < blanks; i++) {
240 tft_putc(0xFFFF, 0x0000, ' ');
245 /* Low-level function to print a character to the display
246 * Should not be used directly */
247 int tft_putc(uint16_t fg, uint16_t bg, int c) {
250 int totalpixels = 35;
256 //if ((c == '\n') && (tftscreen.y == 14)) {
263 if (tftscreen.y == 14) {
269 //if (tftscreen.y < 15)
274 //// tft_putc(0xFFFF, 0x0000, 'o');
277 // // ENDFLAG = true;
281 if (tftscreen.y >= 15) {
289 tft_command(TFT_CASET, 4, 0x00, STARTX + XPOS(tftscreen.x), 0x00, (STARTX + 4) + XPOS(tftscreen.x));
290 tft_command(TFT_RASET, 4, 0x00, STARTY + YPOS(tftscreen.y), 0x00, (STARTY + 6) + YPOS(tftscreen.y));
292 tft_command(TFT_RAMWR, 0);
293 rsetbit(GPIOC_ODR, 6); // data = 1
294 for (int i = 0; i < totalpixels; i++) {
296 current = ASCII5x7[(c * 5) + column];
298 if ((current >> (7 - row)) & 0x1) {
299 rwrite(SPI2_DR, (uint8_t) (fg >> 8));
302 rwrite(SPI2_DR, (uint8_t) (fg & 0xFF));
307 rwrite(SPI2_DR, (uint8_t) (bg >> 8));
310 rwrite(SPI2_DR, (uint8_t) (bg & 0xFF));
315 /* Algoritm dependent on draw mode: top down, left right */
322 tftscreen.buf[tftscreen.cpos] = c;
326 if (tftscreen.x > 20) {
336 /* Invokes commands with a variable list of paramaters. Sending parameters
337 * requires the D/CX line to be high */
338 int tft_command(uint8_t cmd, int argsc, ...) {
342 rclrbit(GPIOC_ODR, 6); // D/CX line low
343 rwrite(SPI2_DR, cmd);
350 rsetbit(GPIOC_ODR, 6); // D/CX line high
351 for (int i = 0; i < argsc; i++) {
352 uint8_t p = (uint8_t) va_arg(ap, unsigned int);