123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- #include "ch.h"
- #include "hal.h"
- #include "malloc.h"
- #include "string.h"
- #include "st7735.h"
- #define DELAY 0x80
- #define ST7735_Select() LCD_CS_RES
- #define ST7735_Unselect() LCD_CS_SET
- #define HAL_Delay(x) chThdSleepMilliseconds(x)
- static void ST7735_Reset(void);
- // based on Adafruit ST7735 library for Arduino
- static const uint8_t
- init_cmds1[] = { // Init for 7735R, 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: Frame rate ctrl - normal mode, 3 args:
- 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
- ST7735_FRMCTR2, 3, // 4: Frame rate control - idle mode, 3 args:
- 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
- ST7735_FRMCTR3, 6, // 5: Frame rate ctrl - 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, no delay:
- 0x07, // 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, Opamp current small & Medium low
- 0x2A,
- 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, no delay
- ST7735_MADCTL, 1, // 14: Memory access control (directions), 1 arg:
- ST7735_ROTATION, // row addr/col addr, bottom to 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_INVON, 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: Gamma Adjustments (pos. polarity), 16 args, no delay:
- 0x02, 0x1c, 0x07, 0x12,
- 0x37, 0x32, 0x29, 0x2d,
- 0x29, 0x25, 0x2B, 0x39,
- 0x00, 0x01, 0x03, 0x10,
- ST7735_GMCTRN1, 16, // 2: Gamma Adjustments (neg. polarity), 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_Reset(void) {
- //HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_RESET);
- HAL_Delay(5);
- //HAL_GPIO_WritePin(ST7735_RES_GPIO_Port, ST7735_RES_Pin, GPIO_PIN_SET);
- }
- /**
- * @brief Send command
- *
- * @param cmd command for display
- */
- static void ST7735_WriteCommand(uint8_t cmd) {
- LCD_DC_CMD;
-
- // wite while spi is busy
- while (!(SPI1->SR & SPI_SR_TXE));
- // send data
- SPI1->DR = cmd;
- // wite for data to be transmitted
- while (!(SPI1->SR & SPI_SR_TXE));
- // ... and spi is busy
- while ((SPI1->SR & SPI_SR_BSY));
- }
- /**
- * @brief Send 8-bit data using DMA
- *
- * @param buff data to be sended
- * @param buff_size amount of bytes
- */
- static void ST7735_WriteData(uint8_t* buff, size_t buff_size) {
- LCD_DC_DATA;
- spiSend(&ST7735_SPI_PORT, buff_size, buff);
- }
- /**
- * @brief Send 16-bit data trough reg, with end wait.
- *
- * @param data to send to display
- */
- static void ST7735_WriteData16(uint16_t data) {
- LCD_DC_DATA;
- //spiSend(&ST7735_SPI_PORT, buff_size, buff);
-
- // set 16-bit data
- SPI1->CR1 |= SPI_CR1_DFF;
- // wite while spi is busy
- while (!(SPI1->SR & SPI_SR_TXE));
- // send data
- SPI1->DR = data;
- // wite for data too be transmitted
- while (!(SPI1->SR & SPI_SR_TXE));
- // ... and spi is unbusy
- while ((SPI1->SR & SPI_SR_BSY));
- // set 8-bit data
- SPI1->CR1 &= ~(SPI_CR1_DFF);
- }
- 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);
- }
- }
- }
- static void ST7735_SetAddressWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
- // column address set
- ST7735_WriteCommand(ST7735_CASET);
- uint8_t data[] = {0x00, x0 + ST7735_XSTART, 0x00, x1 + ST7735_XSTART};
- ST7735_WriteData(data, sizeof(data));
- // row address set
- ST7735_WriteCommand(ST7735_RASET);
- data[1] = y0 + ST7735_YSTART;
- data[3] = y1 + ST7735_YSTART;
- ST7735_WriteData(data, sizeof(data));
- // write to RAM
- ST7735_WriteCommand(ST7735_RAMWR);
- }
- void ST7735_Init(void) {
- ST7735_Select();
- ST7735_Reset();
- ST7735_ExecuteCommandList(init_cmds1);
- ST7735_ExecuteCommandList(init_cmds2);
- ST7735_ExecuteCommandList(init_cmds3);
- ST7735_Unselect();
- }
- /**
- * @brief Draw single pixel
- *
- * @param x coordinate
- * @param y coordinate
- * @param color pixel colour
- */
- 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);
- ST7735_WriteData16(color);
- ST7735_Unselect();
- }
- /**
- * @brief Draw fixed width font char.
- *
- * @param x coordinate,
- * @param y coordinate,
- * @param ch char to draw,
- * @param font fixed width font
- * @param color char color
- * @param bgcolor background color
- */
- 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);
- LCD_DC_DATA;
- SPI1->CR1 |= SPI_CR1_DFF;
- uint16_t ih = (ch - 32) * font.height;
- for(i = 0; i < font.height; i++) {
- b = font.data[ih + i];
- for(j = 0; j < font.width; j++) {
- if (b & 0x8000) {
- while (!(SPI1->SR & SPI_SR_TXE));
- SPI1->DR = color;
- } else {
- while (!(SPI1->SR & SPI_SR_TXE));
- SPI1->DR = bgcolor;
- }
- b <<= 1;
- }
- }
- while (!(SPI1->SR & SPI_SR_TXE));
- while ((SPI1->SR & SPI_SR_BSY));
- SPI1->CR1 &= ~(SPI_CR1_DFF);
- }
- void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
- ST7735_Select();
- char ch = *str;
- while (ch) {
- if (x + font.width >= ST7735_WIDTH) {
- x = 0;
- y += font.height;
- if (y + font.height >= ST7735_HEIGHT) {
- break;
- }
- if (ch == ' ') {
- // skip spaces in the beginning of the new line
- str ++;
- ch = *str;
- continue;
- }
- }
- #ifdef USE_UTF8
- if (ch >= 0xC0) {
- switch (ch) {
- case 0xD0: {
- str ++;
- ch = *str;
- if (ch >= 0x90 && ch <= 0xBF) {
- ch += 0x30;
- } else if (ch == 0x84) { // Є
- ch = 0xaa;
- } else if (ch == 0x86) { // І
- ch = 0xb2;
- } else if (ch == 0x87) { // Ї
- ch = 0xaf;
- } else if (ch == 0x81) { // Ё
- ch = 0xA8;
- } else {
- ch = '.';
- }
- break;
- }
- case 0xD1: {
- str ++;
- ch = *str;
- if (ch >= 0x7E && ch <= 0x8F) {
- ch += 0x70;
- } else if (ch == 0x94) { // є
- ch = 0xba;
- } else if (ch == 0x96) { // і
- ch = 0xb3;
- } else if (ch == 0x97) { // ї
- ch = 0xbf;
- } else if (ch == 0x91) { // ё
- ch = 0xB8;
- } else {
- ch = '.';
- }
- break;
- }
- case 0xD2: {
- str ++;
- ch = *str;
- if (ch == 0x90) { // Ґ
- ch = 0xa5;
- } else if (ch == 0x91) { // ґ
- ch = 0xb4;
- }
- break;
- }
- }
- }
- #endif /* USE_UTF8 */
- ST7735_WriteChar(x, y, ch, font, color, bgcolor);
- x += font.width;
- str ++;
- ch = *str;
- }
- ST7735_Unselect();
- }
- /**
- * @brief Draw variable width font char.
- *
- * @param x coordinate,
- * @param y coordinate,
- * @param ch char to draw,
- * @param font variable width font
- * @param color char color
- * @param bgcolor background color
- */
- static void ST7735_WriteCharV(uint16_t x, uint16_t y, char ch, FontDefV font, uint16_t color, uint16_t bgcolor) {
- uint16_t i, b, j;
- uint8_t c, w, h;
- c = ch - 32;
- w = font.width[c];
- h = font.height;
- ST7735_SetAddressWindow(x, y, w-1, h-1);
- LCD_DC_DATA;
- SPI1->CR1 |= SPI_CR1_DFF;
- uint16_t ih = c * h;
- for(i = 0; i < h; i++) {
- b = font.data[ih+i]; // ??? замінити на масив позицій
- for(j = 0; j < w; j++) {
- if (b & 0x8000) {
- while (!(SPI1->SR & SPI_SR_TXE));
- SPI1->DR = color;
- } else {
- while (!(SPI1->SR & SPI_SR_TXE));
- SPI1->DR = bgcolor;
- }
- b <<= 1;
- }
- }
- while (!(SPI1->SR & SPI_SR_TXE));
- while ((SPI1->SR & SPI_SR_BSY));
- SPI1->CR1 &= ~(SPI_CR1_DFF);
- }
- void ST7735_WriteStringV(uint16_t x, uint16_t y, const char* str, FontDefV font, uint16_t color, uint16_t bgcolor) {
- ST7735_Select();
- uint8_t w;
- while (*str) {
- w = font.width[(uint8_t)*str];
- if (x + w >= 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_WriteCharV(x, y, *str, font, color, bgcolor);
- x += w;
- str ++;
- }
- ST7735_Unselect();
- }
- void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t 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);
- for (y = h; y > 0; y--) {
- for (x = w; x > 0; x--) {
- ST7735_WriteData16(color);
- }
- }
- ST7735_Unselect();
- }
- #ifdef USE_MALLOC
- void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t 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);
- // Prepare whole line in a single buffer
- uint8_t pixel[] = {color >> 8, color & 0xFF};
- // uint8_t *line = malloc(w * sizeof(pixel));
- uint8_t *line = chHeapAlloc(NULL, w * sizeof(pixel));
- for (x = 0; x < w; ++x) {
- memcpy(line + x * sizeof(pixel), pixel, sizeof(pixel));
- }
- for (y=h; y>0; y--) {
- spiSend(&ST7735_SPI_PORT, w * sizeof(pixel), line);
- }
- free(line);
- ST7735_Unselect();
- }
- void ST7735_FillScreenFast(uint16_t color) {
- ST7735_FillRectangleFast(0, 0, ST7735_WIDTH, ST7735_HEIGHT, color);
- }
- #endif /* USE_MALLOC*/
- 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_SetGamma(GammaDef gamma) {
- ST7735_Select();
- ST7735_WriteCommand(ST7735_GAMSET);
- ST7735_WriteData((uint8_t *) &gamma, sizeof(gamma));
- ST7735_Unselect();
- }
- void ST7735_Test(void) {
- // Check border
- 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);
- }
- chThdSleepMilliseconds(3000);
- // 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);
- chThdSleepMilliseconds(5000);
- // Check colors
- ST7735_FillScreen(ST7735_BLACK);
- ST7735_WriteString(0, 0, "BLACK", Font_11x18, ST7735_WHITE, ST7735_BLACK);
- chThdSleepMilliseconds(500);
- ST7735_FillScreen(ST7735_BLUE);
- ST7735_WriteString(0, 0, "BLUE", Font_11x18, ST7735_BLACK, ST7735_BLUE);
- chThdSleepMilliseconds(500);
- ST7735_FillScreen(ST7735_RED);
- ST7735_WriteString(0, 0, "RED", Font_11x18, ST7735_BLACK, ST7735_RED);
- chThdSleepMilliseconds(500);
- ST7735_FillScreen(ST7735_GREEN);
- ST7735_WriteString(0, 0, "GREEN", Font_11x18, ST7735_BLACK, ST7735_GREEN);
- chThdSleepMilliseconds(500);
- ST7735_FillScreen(ST7735_CYAN);
- ST7735_WriteString(0, 0, "CYAN", Font_11x18, ST7735_BLACK, ST7735_CYAN);
- chThdSleepMilliseconds(500);
- ST7735_FillScreen(ST7735_MAGENTA);
- ST7735_WriteString(0, 0, "MAGENTA", Font_11x18, ST7735_BLACK, ST7735_MAGENTA);
- chThdSleepMilliseconds(500);
- ST7735_FillScreen(ST7735_YELLOW);
- ST7735_WriteString(0, 0, "YELLOW", Font_11x18, ST7735_BLACK, ST7735_YELLOW);
- chThdSleepMilliseconds(500);
- ST7735_FillScreen(ST7735_WHITE);
- ST7735_WriteString(0, 0, "WHITE", Font_11x18, ST7735_BLACK, ST7735_WHITE);
- chThdSleepMilliseconds(500);
- chThdSleepMilliseconds(15000);
- }
|