/* vim: set ai et ts=4 sw=4: */ #include "stm32f1xx_hal.h" #include "st7735.h" #include "../spi/ad0x0_spi1.h" #define DELAY 0x80 uint16_t ad0x0_charbuf[16*26];// // based on Adafruit ST7735 library for Arduino static const uint8_t Rcmd1[] = { // 7735R init, part 1 (red or green tab) 15, // 15 commands in list: ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay 150, // 150 ms delay ST7735_SLPOUT, DELAY, // 2: Out of sleep mode, 0 args, w/delay 255, // 500 ms delay ST7735_FRMCTR1, 3, // 3: Framerate ctrl - normal mode, 3 arg: 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) ST7735_FRMCTR2, 3, // 4: Framerate ctrl - idle mode, 3 args: 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) ST7735_FRMCTR3, 6, // 5: Framerate - partial mode, 6 args: 0x01, 0x2C, 0x2D, // Dot inversion mode 0x01, 0x2C, 0x2D, // Line inversion mode ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg: 0x0, // No inversion ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay: 0xA2, 0x02, // -4.6V 0x84, // AUTO mode ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay: 0xC5, // VGH25=2.4C VGSEL=-10 VGH=3 * AVDD ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay: 0x0A, // Opamp current small 0x00, // Boost frequency ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay: 0x8A, // BCLK/2, 0x2A, // opamp current small & medium low ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay: 0x8A, 0xEE, ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay: 0x0E, ST7735_INVOFF, 0, // 13: Don't invert display, no args ST7735_MADCTL, 1, // 14: Mem access ctl (directions), 1 arg: 0x68, // row/col addr, bottom-top refresh ST7735_COLMOD, 1, // 15: set color mode, 1 arg, no delay: 0x05 }, // 16-bit color #if (defined(ST7735_IS_128X128) || defined(ST7735_IS_160X128)) init_cmds2[] = { // Init for 7735R, part 2 (1.44" display) 2, // 2 commands in list: ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay: 0x00, 0x00, // XSTART = 0 0x00, 0x7F, // XEND = 127 ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay: 0x00, 0x00, // XSTART = 0 0x00, 0x7F }, // XEND = 127 #endif // ST7735_IS_128X128 #ifdef ST7735_IS_160X80 init_cmds2[] = { // Init for 7735S, part 2 (160x80 display) 3, // 3 commands in list: ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay: 0x00, 0x00, // XSTART = 0 0x00, 0x4F, // XEND = 79 ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay: 0x00, 0x00, // XSTART = 0 0x00, 0x9F , // XEND = 159 ST7735_INVOFF, 0 }, // 3: Invert colors #endif init_cmds3[] = { // Init for 7735R, part 3 (red or green tab) 4, // 4 commands in list: ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay: 0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10, ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay: 0x03, 0x1d, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D, 0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x10, ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay 10, // 10 ms delay ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay 100 }; // 100 ms delay static void ST7735_WriteCommand(uint8_t cmd) { //HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_RESET); //HAL_SPI_Transmit(&ST7735_SPI_PORT, &cmd, sizeof(cmd), HAL_MAX_DELAY); ad0x0_spi1_push(ADQS_ST7735_SET_DC,0); ad0x0_spi1_push(ADQS_TX,cmd); } static void ST7735_WriteData(uint8_t* buff, size_t buff_size) { //HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET); //HAL_SPI_Transmit(&ST7735_SPI_PORT, buff, buff_size, HAL_MAX_DELAY); ad0x0_spi1_push(ADQS_ST7735_SET_DC,1); //ad0x0_spi1_push_tx_pbuf(buff); ad0x0_spi1_push_tx_pbuf_dma_ext((uint8_t*)buff,NULL, buff_size); } static void ST7735_WriteByte(uint8_t _byte) { ad0x0_spi1_push(ADQS_ST7735_SET_DC,1); ad0x0_spi1_push(ADQS_TX,_byte); } static void ST7735_ExecuteCommandList(const uint8_t *addr) { uint8_t numCommands, numArgs; uint16_t ms; numCommands = *addr++; while(numCommands--) { uint8_t cmd = *addr++; ST7735_WriteCommand(cmd); numArgs = *addr++; // If high bit set, delay follows args ms = numArgs & DELAY; numArgs &= ~DELAY; if(numArgs) { ST7735_WriteData((uint8_t*)addr, numArgs); addr += numArgs; } if(ms) { ms = *addr++; if(ms == 255) ms = 500; //HAL_Delay(ms); ad0x0_spi1_push(ADQS_DELAY_MS,ms); } } ad0x0_spi1_wait(); } static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) { // column address set ad0x0_spi1_wait(); ST7735_WriteCommand(ST7735_CASET);//2a uint8_t data1[] = { 0x00, x0 + ST7735_XSTART, 0x00, x1 + ST7735_XSTART }; ST7735_WriteData(data1, sizeof(data1)); // row address set ST7735_WriteCommand(ST7735_RASET);//2b uint8_t data2[] = { 0x00, y0 + ST7735_YSTART, 0x00, y1 + ST7735_YSTART }; ST7735_WriteData(data2, sizeof(data2)); // write to RAM ST7735_WriteCommand(ST7735_RAMWR);//2c ad0x0_spi1_wait(); } void ST7735_Init() { //cs=0;res=0;delay(5ms);res=1;cs=1; ad0x0_spi1_push(ADQS_ST7735_SET_CS,0); ad0x0_spi1_push(ADQS_ST7735_SET_RESET,0); ad0x0_spi1_push(ADQS_DELAY_MS,5); ad0x0_spi1_push(ADQS_ST7735_SET_RESET,1); ad0x0_spi1_push(ADQS_ST7735_SET_CS,1); // ST7735_Reset(); ST7735_ExecuteCommandList(Rcmd1); ST7735_ExecuteCommandList(init_cmds2); ST7735_WriteCommand(ST7735_MADCTL); //0x36 ST7735_WriteByte(ST7735_ROTATION); ST7735_ExecuteCommandList(init_cmds3); //ST7735_WriteCommand(ST7735_INVOFF);//0x20 //ST7735_Unselect(); } void ST7735_Init2() { //cs=0;res=0;delay(5ms);res=1;cs=1; ad0x0_spi1_push(ADQS_ST7735_SET_CS,0); ad0x0_spi1_push(ADQS_ST7735_SET_RESET,0); ad0x0_spi1_push(ADQS_DELAY_MS,30); ad0x0_spi1_push(ADQS_ST7735_SET_RESET,1); ad0x0_spi1_push(ADQS_DELAY_MS,15); ad0x0_spi1_push(ADQS_ST7735_SET_CS,1); ST7735_WriteCommand(ST7735_COLMOD);//0x3A ST7735_WriteByte(0x5); ad0x0_spi1_push(ADQS_DELAY_MS,12); // Send_CMD(0x01); ST7735_WriteCommand(0x1); ad0x0_spi1_push(ADQS_DELAY_MS,12); // _delay_ms(12); ST7735_WriteCommand(ST7735_SLPOUT);//0x11 ad0x0_spi1_push(ADQS_DELAY_MS,30); //Send_CMD(0x36); //Send_DAT(0xA0); ST7735_WriteCommand(ST7735_MADCTL); //0x36 ST7735_WriteByte(ST7735_ROTATION); ST7735_WriteCommand(ST7735_INVCTR); //0xB4 ST7735_WriteByte(0x0); //ST7735_WriteCommand(ST7735_INVOFF);//0x20 //Send_CMD(0x29); ST7735_WriteCommand(0x29); } void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color) { if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return; // ST7735_Select(); ST7735_SetAddressWindow(x, y, x+1, y+1); uint8_t data[] = { color >> 8, color & 0xFF }; ST7735_WriteData(data, sizeof(data)); // ST7735_Unselect(); } /*static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) { uint32_t i, b, j; ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1); for(i = 0; i < font.height; i++) { b = font.data[(ch - 32) * font.height + i]; for(j = 0; j < font.width; j++) { if((b << j) & 0x8000) { uint8_t data[] = { color >> 8, color & 0xFF }; ST7735_WriteData(data, sizeof(data)); } else { uint8_t data[] = { bgcolor >> 8, bgcolor & 0xFF }; ST7735_WriteData(data, sizeof(data)); } } } }*/ void ST7735_WriteChar2(uint16_t x, uint16_t y, char ch, FontDef *font, uint16_t color, uint16_t bgcolor) { uint32_t i, b, j; AD0X0_LE_SWAP(color); AD0X0_LE_SWAP(bgcolor); ST7735_SetAddressWindow(x, y, x+font->width-1, y+font->height-1); //почему я ее засунул сюда? потому что буфер символа еще будет передаваться //после выхода из функции... при рендере нового мы его будем перезаписывать.. по этому ждем нахрен( //сука 20кб у стм32ф103с8т6 это блядство чистое... ну че б не сделать 128кб?! тогда бы экран в память влезал!! ad0x0_spi1_wait(); uint16_t *p,*ptxspi; p=ad0x0_charbuf; ad0x0_spi1_push(ADQS_ST7735_SET_DC,1);//!!!!!!!!!!!!!!! сука бля 2 часа просидел!!!!! тупейшая идея с DC for(i = 0; i < font->height; i++) { b = font->data[(ch - 32) * font->height + i]; //ad0x0_spi1_push_ext_setbuf((uint8_t*)p,NULL, font->width<<1);//текущая строка символа ptxspi=p; for(j = 0; j < font->width; j++) { if((b << j) & 0x8000) { //uint8_t data[] = { color >> 8, color & 0xFF }; //ST7735_WriteData(data, sizeof(data)); *p++=(uint16_t)color; } else { //uint8_t data[] = { bgcolor >> 8, bgcolor & 0xFF }; //ST7735_WriteData(data, sizeof(data)); *p++=(uint16_t)bgcolor; //*p++=(uint16_t)0x0000; } } ad0x0_spi1_push_tx_pbuf_dma_ext((uint8_t*)ptxspi,NULL,font->width<<1); } ad0x0_spi1_wait(); } /* Simpler (and probably slower) implementation: static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color) { uint32_t i, b, j; for(i = 0; i < font.height; i++) { b = font.data[(ch - 32) * font.height + i]; for(j = 0; j < font.width; j++) { if((b << j) & 0x8000) { ST7735_DrawPixel(x + j, y + i, color); } } } } */ void ST7735_WriteString(uint16_t x, uint16_t y, unsigned char* str, FontDef *font, uint16_t color, uint16_t bgcolor) { //ST7735_Select(); while(*str) { if(x + font->width >= ST7735_WIDTH) { x = 0; y += font->height; if(y + font->height >= ST7735_HEIGHT) { break; } if(*str == ' ') { // skip spaces in the beginning of the new line str++; continue; } } // ST7735_WriteChar(x, y, *str, font, color, bgcolor); ST7735_WriteChar2(x, y, *str, font, color, bgcolor); x += font->width; str++; } ad0x0_spi1_wait(); //ST7735_Unselect(); } void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) { AD0X0_LE_SWAP(color); // clipping if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return; if((x + w - 1) >= ST7735_WIDTH) w = ST7735_WIDTH - x; if((y + h - 1) >= ST7735_HEIGHT) h = ST7735_HEIGHT - y; //ST7735_Select(); ST7735_SetAddressWindow(x, y, x+w-1, y+h-1); // uint8_t data[] = { color >> 8, color & 0xFF }; //HAL_GPIO_WritePin(ST7735_DC_GPIO_Port, ST7735_DC_Pin, GPIO_PIN_SET); uint16_t ad0x0_d[160],*p=ad0x0_d; for(x = w; x > 0; x--) *p++=color; ad0x0_spi1_push(ADQS_ST7735_SET_DC,1); //ad0x0_spi1_push_tx_pbuf((uint8_t*)(ad0x0_d)); for(y = h; y > 0; y--) { ad0x0_spi1_push_tx_pbuf_ext((uint8_t*)(ad0x0_d),NULL, w); //буфер у очереди останется на той позиции где тормознулся, по этому не переприсваиваем //ad0x0_spi1_push_ext_setbuf((uint8_t*)(ad0x0_d),NULL, w); ad0x0_spi1_push(ADQS_TX_PBUF_DMA,0); /*for(x = w; x > 0; x--) { //HAL_SPI_Transmit(&ST7735_SPI_PORT, data, sizeof(data), HAL_MAX_DELAY); ad0x0_spi1_push_tx_pbuf(data); ad0x0_spi1_push(ADQS_TX_PBUF,sizeof(data)); }*/ } //while(!ad0x0_spi1_is_queue_empty()){}//!!!!!!!!!!ubrat' etot pizdets ad0x0_spi1_wait(); //ST7735_Unselect(); } void ST7735_FillScreen(uint16_t color) { ST7735_FillRectangle(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color); } void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t* data) { if((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return; if((x + w - 1) >= ST7735_WIDTH) return; if((y + h - 1) >= ST7735_HEIGHT) return; //ST7735_Select(); ST7735_SetAddressWindow(x, y, x+w-1, y+h-1); ST7735_WriteData((uint8_t*)data, sizeof(uint16_t)*w*h); //ST7735_Unselect(); } void ST7735_InvertColors(bool invert) { //ST7735_Select(); ST7735_WriteCommand(invert ? ST7735_INVON : ST7735_INVOFF); //ST7735_Unselect(); } void ST7735_Test(void){ ad0x0_err(); /*ST7735_FillScreen(ST7735_BLACK); for(int x = 0; x < ST7735_WIDTH; x++) { ST7735_DrawPixel(x, 0, ST7735_RED); ST7735_DrawPixel(x, ST7735_HEIGHT-1, ST7735_RED); } for(int y = 0; y < ST7735_HEIGHT; y++) { ST7735_DrawPixel(0, y, ST7735_RED); ST7735_DrawPixel(ST7735_WIDTH-1, y, ST7735_RED); } HAL_Delay(1000); // Check fonts ST7735_FillScreen(ST7735_BLACK); ST7735_WriteString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_RED, ST7735_BLACK); ST7735_WriteString(0, 3*10, "Font_11x18, green, lorem ipsum", &Font_11x18, ST7735_GREEN, ST7735_BLACK); //ST7735_WriteString(0, 3*10+3*18, "Font_16x26", &Font_16x26, ST7735_BLUE, ST7735_BLACK); HAL_Delay(1000); // Check colors ST7735_FillScreen(ST7735_BLACK); ST7735_WriteString(0, 0, "BLACK", &Font_11x18, ST7735_WHITE, ST7735_BLACK); ST7735_WriteString(0, 30, "тест русского ШРИФТА. Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500); ST7735_FillScreen(ST7735_RED); ST7735_WriteString(0, 0, "RED", &Font_11x18, 0x0000, ST7735_RED); ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500); ST7735_FillScreen(ST7735_GREEN); ST7735_WriteString(0, 0, "GREEN", &Font_11x18, ST7735_BLACK, ST7735_GREEN); ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500); ST7735_FillScreen(ST7735_BLUE); ST7735_WriteString(0, 0, "BLUE", &Font_11x18, ST7735_BLACK, ST7735_BLUE); ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500); ST7735_FillScreen(ST7735_CYAN); ST7735_WriteString(0, 0, "CYAN", &Font_11x18, ST7735_BLACK, ST7735_CYAN); ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500); ST7735_FillScreen(ST7735_MAGENTA); ST7735_WriteString(0, 0, "MAGENTA", &Font_11x18, ST7735_BLACK, ST7735_MAGENTA); ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500); ST7735_FillScreen(ST7735_YELLOW); ST7735_WriteString(0, 0, "YELLOW", &Font_11x18, ST7735_BLACK, ST7735_YELLOW); ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500); ST7735_FillScreen(ST7735_WHITE); ST7735_WriteString(0, 0, "WHITE", &Font_11x18, ST7735_BLACK, ST7735_WHITE); ST7735_WriteString(0, 30, "Font_7x10, red on black, lorem ipsum dolor sit amet", &Font_7x10, ST7735_WHITE, ST7735_BLACK); HAL_Delay(500);*/ } static unsigned char * i16_2s(int16_t value, unsigned char *buffer) { memset(buffer,' ',8); buffer += 7; char m=(value<0)?'-':' '; if(value<0)value=-value; // 11 áàéò äîñòàòî÷íî äëÿ äåñÿòè÷íîãî ïðåäñòàâëåíèÿ 32-õ áàéòíîãî ÷èñëà // è çàâåðøàþùåãî íóëÿ *--buffer = 0; do { *--buffer = value % 10 + '0'; value /= 10; } while (value != 0); *--buffer=m; return buffer; } static unsigned char * i32_2s(int32_t value, unsigned char *buffer) { memset(buffer,' ',8); buffer += 7; char m=(value<0)?'-':' '; if(value<0)value=-value; // 11 áàéò äîñòàòî÷íî äëÿ äåñÿòè÷íîãî ïðåäñòàâëåíèÿ 32-õ áàéòíîãî ÷èñëà // è çàâåðøàþùåãî íóëÿ *--buffer = 0; do { *--buffer = value % 10 + '0'; value /= 10; } while (value != 0); *--buffer=m; return buffer; }