diff --git a/STM32F746xG.ld b/STM32F746xG.ld index fe57a39..db176e0 100644 --- a/STM32F746xG.ld +++ b/STM32F746xG.ld @@ -214,7 +214,7 @@ SECTIONS { *(.stack*) } > RAM - + ExtFlashSection : { *(ExtFlashSection ExtFlashSection.*) diff --git a/flash_data.h b/flash_data.h index 05cddd7..c3c3a43 100644 --- a/flash_data.h +++ b/flash_data.h @@ -48,8 +48,8 @@ #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x08018000) /* Base @ of Sector 3, 32 Kbytes */ #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08020000) /* Base @ of Sector 4, 128 Kbytes */ #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08040000) /* Base @ of Sector 5, 256 Kbytes */ -#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08080000) /* Base @ of Sector 6, 256 Kbytes */ -#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x080C0000) /* Base @ of Sector 7, 256 Kbytes */ +//#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08080000) /* Base @ of Sector 6, 256 Kbytes */ +//#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x080C0000) /* Base @ of Sector 7, 256 Kbytes */ #endif #endif diff --git a/ili9341.cpp b/ili9341.cpp new file mode 100644 index 0000000..b3d6dc5 --- /dev/null +++ b/ili9341.cpp @@ -0,0 +1,205 @@ +#include "ili9341.h" + +#include "stm32f7xx_ll_spi.h" + +ILI9341::ILI9341(PinName MOSI, PinName MISO, PinName SCK, PinName DC, PinName RST) + : dispPort(MOSI, MISO, SCK) + , rst(RST) + , dc(DC) +{ + // dispPort = new SPI(MOSI, MISO, SCK); + dispPort.frequency(27000000); + dispPort.format(8, 3); + SPIx = spi_get_peripheral_name(MOSI, MISO, SCK); + + rst = 1; + + osDelay(10); + rst = 0; + osDelay(10); + rst = 1; + osDelay(10); + + LCD_WR_REG(0x01); //software reset + osDelay(50); + LCD_WR_REG(0x28); + // display off + //--------------------------------------------------------- + // magic? + LCD_WR_REG(0xcf); + LCD_WR_Data(0x00); + LCD_WR_Data(0x83); + LCD_WR_Data(0x30); + + LCD_WR_REG(0xed); + LCD_WR_Data(0x64); + LCD_WR_Data(0x03); + LCD_WR_Data(0x12); + LCD_WR_Data(0x81); + LCD_WR_REG(0xe8); + LCD_WR_Data(0x85); + LCD_WR_Data(0x01); + LCD_WR_Data(0x79); + LCD_WR_REG(0xcb); + LCD_WR_Data(0x39); + LCD_WR_Data(0x2c); + LCD_WR_Data(0x00); + LCD_WR_Data(0x34); + LCD_WR_Data(0x02); + LCD_WR_REG(0xf7); + LCD_WR_Data(0x20); + LCD_WR_REG(0xea); + LCD_WR_Data(0x00); + LCD_WR_Data(0x00); + //------------power control------------------------------ + LCD_WR_REG(0xc0); //power control + LCD_WR_Data(0x26); + LCD_WR_REG(0xc1); //power control + LCD_WR_Data(0x11); + //--------------VCOM + LCD_WR_REG(0xc5); //vcom control + LCD_WR_Data(0x35); //35 + LCD_WR_Data(0x3e); //3E + LCD_WR_REG(0xc7); //vcom control + LCD_WR_Data(0xbe); // 0x94 + //------------memory access control------------------------ + LCD_WR_REG(0x36); + // memory access control + LCD_WR_Data(0xE8); //Поворот экрана на 0 градусов + // LCD_WR_Data(0x28); //Поворот экрана на 180 градусов + LCD_WR_REG(0x3a); // pixel format set + LCD_WR_Data(0x55); //16bit /pixel + //----------------- frame rate------------------------------ + LCD_WR_REG(0xb1); + // frame rate + LCD_WR_Data(0x00); + LCD_WR_Data(0x1B); //70 + //----------------Gamma--------------------------------- + LCD_WR_REG(0xf2); // 3Gamma Function Disable + LCD_WR_Data(0x08); + LCD_WR_REG(0x26); + LCD_WR_Data(0x01); // gamma set 4 gamma curve 01/02/04/08 + + LCD_WR_REG(0xE0); //positive gamma correction + LCD_WR_Data(0x1f); + LCD_WR_Data(0x1a); + LCD_WR_Data(0x18); + LCD_WR_Data(0x0a); + LCD_WR_Data(0x0f); + LCD_WR_Data(0x06); + LCD_WR_Data(0x45); + LCD_WR_Data(0x87); + LCD_WR_Data(0x32); + LCD_WR_Data(0x0a); + LCD_WR_Data(0x07); + LCD_WR_Data(0x02); + LCD_WR_Data(0x07); + LCD_WR_Data(0x05); + LCD_WR_Data(0x00); + LCD_WR_REG(0xE1); //negamma correction + LCD_WR_Data(0x00); + LCD_WR_Data(0x25); + LCD_WR_Data(0x27); + LCD_WR_Data(0x05); + LCD_WR_Data(0x10); + LCD_WR_Data(0x09); + LCD_WR_Data(0x3a); + LCD_WR_Data(0x78); + LCD_WR_Data(0x4d); + LCD_WR_Data(0x05); + LCD_WR_Data(0x18); + LCD_WR_Data(0x0d); + LCD_WR_Data(0x38); + LCD_WR_Data(0x3a); + LCD_WR_Data(0x1f); + //--------------ddram --------------------- + LCD_WR_REG(0x2a); + // column set + // size = 239 + LCD_WR_Data(0x00); + LCD_WR_Data(0x00); + LCD_WR_Data(0x00); + LCD_WR_Data(0xEF); + LCD_WR_REG(0x2b); + // page address set + // size = 319 + LCD_WR_Data(0x00); + LCD_WR_Data(0x00); + LCD_WR_Data(0x01); + LCD_WR_Data(0x3F); + // LCD_WR_REG(0x34); + //LCD_WR_REG(0x35); + // tearing effect off + // tearing effect on + // LCD_WR_REG(0xb4); // display inversion + // LCD_WR_Data(0x00); + LCD_WR_REG(0xb7); //entry mode set + LCD_WR_Data(0x07); + //-----------------display--------------------- + LCD_WR_REG(0xb6); + // display function control + LCD_WR_Data(0x0a); + LCD_WR_Data(0x82); + LCD_WR_Data(0x27); + LCD_WR_Data(0x00); + LCD_WR_REG(0x11); //sleep out + osDelay(100); + LCD_WR_REG(0x29); // display on +} + +void ILI9341::LCD_WR_Data(uint8_t Data) +{ + dc = 1; + dispPort.write(Data); +} + +void ILI9341::LCD_WR_REG(uint8_t Reg) +{ + dc = 0; + dispPort.write(Reg); +} + +void ILI9341::LCD_setaddress(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) //set coordinate for print or other function +{ + LCD_WR_REG(0x2A); + LCD_WR_Data(x1 >> 8); + LCD_WR_Data(x1); + LCD_WR_Data(x2 >> 8); + LCD_WR_Data(x2); + + LCD_WR_REG(0x2B); + LCD_WR_Data(y1 >> 8); + LCD_WR_Data(y1); + LCD_WR_Data(y2 >> 8); + LCD_WR_Data(y2); + + LCD_WR_REG(0x2C); //memory write +} + +void ILI9341::transmitData(const uint8_t* pixels, uint16_t x, uint16_t y, uint16_t w, uint16_t h) +{ + uint32_t n_bytes = w * h; + // uint8_t lo = 0, hi = 0; + + LCD_setaddress(x, y, x + w - 1, y + h - 1); + + // while (LL_SPI_IsActiveFlag_BSY(SPI1)) { + // } + dc = 1; + + // LL_SPI_SetDataWidth((SPI_TypeDef*)SPIx, LL_SPI_DATAWIDTH_16BIT); + dispPort.format(16, 3); + while (n_bytes) { + uint8_t lo = *pixels++; + uint8_t hi = *pixels++; + // dispPort->write(hi << 8 | lo); + // dispPort->write(lo); + while (!LL_SPI_IsActiveFlag_TXE(SPI1)) { + } + LL_SPI_TransmitData16((SPI_TypeDef*)SPIx, hi << 8 | lo); + + n_bytes--; + } + // LL_SPI_SetDataWidth((SPI_TypeDef*)SPIx, LL_SPI_DATAWIDTH_8BIT); + dispPort.format(8, 3); +} diff --git a/ili9341.h b/ili9341.h new file mode 100644 index 0000000..1e5a941 --- /dev/null +++ b/ili9341.h @@ -0,0 +1,22 @@ +#ifndef ILI9341_H +#define ILI9341_H + +#include "mbed.h" + +class ILI9341 { +public: + ILI9341(PinName MOSI, PinName MISO, PinName SCK, PinName DC, PinName RST); + void transmitData(const uint8_t* pixels, uint16_t x, uint16_t y, uint16_t w, uint16_t h); + +private: + SPI dispPort; + SPIName SPIx; + void LCD_WR_Data(uint8_t Data); + void LCD_WR_REG(uint8_t Reg); + void LCD_setaddress(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); + + DigitalOut rst; //(PB_1); + DigitalOut dc; //(PB_0); +}; + +#endif // ILI9341_H diff --git a/tsc2046.cpp b/tsc2046.cpp new file mode 100644 index 0000000..273b504 --- /dev/null +++ b/tsc2046.cpp @@ -0,0 +1,173 @@ +#include "tsc2046.h" + +#define CMD_X 0xD1 +#define CMD_Y 0x91 +#define CMD_ENABLE_IRQ 0x80 + +#define RESCALE_FACTOR 1000000 +#define EE_MEM_ADDR 400 + +const constexpr auto dispWidth = 320; +const constexpr auto dispHeight = 240; + +const constexpr auto xd1 = (50 * dispWidth) / 100; +const constexpr auto xd2 = (90 * dispWidth) / 100; +const constexpr auto xd3 = (10 * dispWidth) / 100; + +const constexpr auto yd1 = (10 * dispHeight) / 100; +const constexpr auto yd2 = (50 * dispHeight) / 100; +const constexpr auto yd3 = (90 * dispHeight) / 100; + +TSC2046::TSC2046(EEPROM* mem, PinName MOSI, PinName MISO, PinName SCK, PinName CS, PinName INT) +{ + tpPort = new SPI(MOSI, MISO, SCK); + tpCS = new DigitalOut(CS); + tpInt = new DigitalIn(INT, PullUp); + + eeprom = mem; + eeprom->read(EE_MEM_ADDR, (uint8_t*)&touchCoeff, sizeof(touchCoeff)); + if (touchCoeff.touchIsCalibrated > 2) + touchCoeff.touchIsCalibrated = false; + + readValue(CMD_ENABLE_IRQ); // Enable IRQ +} + +void TSC2046::GetRawXY(uint16_t* X, uint16_t* Y) +{ + readValue(CMD_X); // Dummy read - disable PenIRQ + *X = readValue(CMD_X); // Read X-Value + + readValue(CMD_Y); // Dummy read - disable PenIRQ + *Y = readValue(CMD_Y); // Read Y-Value + + readValue(CMD_ENABLE_IRQ); // Enable IRQ +} +void TSC2046::GetXY(int32_t* X, int32_t* Y) +{ + uint16_t x, y; + if (DetectTouch()) { + GetRawXY(&x, &y); + *X = (touchCoeff.cali_A * x + touchCoeff.cali_B * y + touchCoeff.cali_C) / RESCALE_FACTOR; + *Y = (touchCoeff.cali_D * x + touchCoeff.cali_E * y + touchCoeff.cali_F) / RESCALE_FACTOR; + } +} + +uint16_t TSC2046::readValue(uint16_t port) +{ + static char txbuf[3] = { 0 }; + static char rxbuf[3] = { 0 }; + + txbuf[0] = port; + tpCS->write(0); + tpPort->write(txbuf, 3, rxbuf, 3); + tpCS->write(1); + + return ((uint16_t)rxbuf[1] << 5) | (rxbuf[2] >> 3); +} + +bool TSC2046::DetectTouch() +{ + return !tpInt->read(); +} + +void TSC2046::coeffCalc() +{ + + double temp1, temp2; + double cal_A = 0.0, cal_B = 0.0, cal_C = 0.0, cal_D = 0.0, cal_E = 0.0, cal_F = 0.0; + + //A + temp1 = ((double)xd1 * ((double)yt2 - (double)yt3)) + ((double)xd2 * ((double)yt3 - (double)yt1)) + ((double)xd3 * ((double)yt1 - (double)yt2)); + temp2 = ((double)xt1 * ((double)yt2 - (double)yt3)) + ((double)xt2 * ((double)yt3 - (double)yt1)) + ((double)xt3 * ((double)yt1 - (double)yt2)); + cal_A = temp1 / temp2; + touchCoeff.cali_A = (int32_t)((double)cal_A * RESCALE_FACTOR); + + //B + temp1 = (cal_A * ((double)xt3 - (double)xt2)) + (double)xd2 - (double)xd3; + temp2 = (double)yt2 - (double)yt3; + cal_B = temp1 / temp2; + touchCoeff.cali_B = (int32_t)((double)cal_B * RESCALE_FACTOR); + + //C + cal_C = (double)xd3 - (cal_A * (double)xt3) - (cal_B * (double)yt3); + touchCoeff.cali_C = (int32_t)(cal_C * RESCALE_FACTOR); + + //D + temp1 = ((double)yd1 * ((double)yt2 - (double)yt3)) + ((double)yd2 * ((double)yt3 - (double)yt1)) + ((double)yd3 * ((double)yt1 - (double)yt2)); + temp2 = ((double)xt1 * ((double)yt2 - (double)yt3)) + ((double)xt2 * ((double)yt3 - (double)yt1)) + ((double)xt3 * ((double)yt1 - (double)yt2)); + cal_D = (double)temp1 / (double)temp2; + touchCoeff.cali_D = (int32_t)(cal_D * RESCALE_FACTOR); + + //E + temp1 = (cal_D * ((double)xt3 - (double)xt2)) + (double)yd2 - (double)yd3; + temp2 = (double)yt2 - (double)yt3; + cal_E = (double)temp1 / (double)temp2; + touchCoeff.cali_E = (int32_t)(cal_E * RESCALE_FACTOR); + + //F + cal_F = (double)yd3 - cal_D * (double)xt3 - cal_E * (double)yt3; + touchCoeff.cali_F = (int32_t)(cal_F * RESCALE_FACTOR); +} + +uint16_t TSC2046::getXd1(void) +{ + return xd1; +} + +uint16_t TSC2046::getXd2(void) +{ + return xd2; +} +uint16_t TSC2046::getXd3(void) +{ + return xd3; +} +uint16_t TSC2046::getYd1(void) +{ + return yd1; +} + +uint16_t TSC2046::getYd2(void) +{ + return yd2; +} +uint16_t TSC2046::getYd3(void) +{ + return yd3; +} + +void TSC2046::setXt1(uint32_t xt1) +{ + this->xt1 = xt1; +} +void TSC2046::setXt2(uint32_t xt2) +{ + this->xt2 = xt2; +} +void TSC2046::setXt3(uint32_t xt3) +{ + this->xt3 = xt3; +} +void TSC2046::setYt1(uint32_t yt1) +{ + this->yt1 = yt1; +} +void TSC2046::setYt2(uint32_t yt2) +{ + this->yt2 = yt2; +} +void TSC2046::setYt3(uint32_t yt3) +{ + this->yt3 = yt3; +} + +bool TSC2046::getCalibrated() +{ + return touchCoeff.touchIsCalibrated; +} + +void TSC2046::setCalibrated() +{ + touchCoeff.touchIsCalibrated = true; + eeprom->write(EE_MEM_ADDR, (uint8_t*)&touchCoeff, sizeof(touchCoeff)); +} diff --git a/tsc2046.h b/tsc2046.h new file mode 100644 index 0000000..78f4e7b --- /dev/null +++ b/tsc2046.h @@ -0,0 +1,57 @@ +#include "eeprom.h" +#include "mbed.h" + +class TSC2046 { +public: + TSC2046(EEPROM* mem, PinName MOSI, PinName MISO, PinName SCK, PinName CS, PinName INT); + + void GetXY(int32_t* X, int32_t* Y); + + uint16_t getXd1(void); + uint16_t getXd2(void); + uint16_t getXd3(void); + uint16_t getYd1(void); + uint16_t getYd2(void); + uint16_t getYd3(void); + + void setXt1(uint32_t xt1); + void setXt2(uint32_t xt2); + void setXt3(uint32_t xt3); + void setYt1(uint32_t yt1); + void setYt2(uint32_t yt2); + void setYt3(uint32_t yt3); + void setCalibrated(); + bool getCalibrated(); + + bool DetectTouch(); + + void coeffCalc(); + + void GetRawXY(uint16_t* X, uint16_t* Y); + +private: + struct + { + volatile int32_t cali_A; + volatile int32_t cali_B; + volatile int32_t cali_C; + volatile int32_t cali_D; + volatile int32_t cali_E; + volatile int32_t cali_F; + volatile uint8_t touchIsCalibrated; + uint8_t tmp; + } touchCoeff; + + uint32_t xt1; + uint32_t xt2; + uint32_t xt3; + uint32_t yt1; + uint32_t yt2; + uint32_t yt3; + + SPI* tpPort; + DigitalOut* tpCS; + DigitalIn* tpInt; + EEPROM* eeprom; + uint16_t readValue(uint16_t port); +};