Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Datasheet says the controller only needs nanoseconds between rise/falls.
- #define TFT_US_DELAY 1
- void beginDisplay(ILI9341_TFT* self) {
- GPIO_ResetBits(self->pins.p8.chip_sel.bank,
- self->pins.p8.chip_sel.pin);
- GPIO_ResetBits(self->pins.p8.dat_cmd.bank,
- self->pins.p8.dat_cmd.pin);
- GPIO_SetBits(self->pins.p8.wr_en.bank,
- self->pins.p8.wr_en.pin);
- GPIO_SetBits(self->pins.p8.rd_en.bank,
- self->pins.p8.rd_en.pin);
- GPIO_SetBits(self->pins.p8.reset.bank,
- self->pins.p8.reset.pin);
- delay_ms(120);
- GPIO_ResetBits(self->pins.p8.reset.bank,
- self->pins.p8.reset.pin);
- delay_ms(120);
- GPIO_SetBits(self->pins.p8.reset.bank,
- self->pins.p8.reset.pin);
- delay_ms(120);
- // Exit sleep.
- TFT_writeCommand(self, 0x11);
- delay_ms(120);
- // Test 'read ID/version' commands.
- TFT_writeCommand(self, 0xDA);
- uint8_t temp = TFT_read8(self);
- temp = TFT_read8(self);
- TFT_writeCommand(self, 0xDB);
- temp = TFT_read8(self);
- temp = TFT_read8(self);
- TFT_writeCommand(self, 0xDC);
- temp = TFT_read8(self);
- temp = TFT_read8(self);
- // (Undocumented-but-required commands according to
- // Adafruit and other examples)
- unholyMagic(self);
- // Startup commands.
- TFT_writeCommand(self, I9341_PWCTR1);
- TFT_writeData(self, 0x23);
- TFT_writeCommand(self, I9341_PWCTR2);
- TFT_writeData(self, 0x10);
- TFT_writeCommand(self, I9341_VCMCTR1);
- TFT_writeData(self, 0x3e);
- TFT_writeData(self, 0x28);
- TFT_writeCommand(self, I9341_VCMCTR2);
- TFT_writeData(self, 0x86);
- TFT_writeCommand(self, I9341_MADCTL);
- TFT_writeData(self, 0x48);
- TFT_writeCommand(self, I9341_PIXFMT);
- TFT_writeData(self, 0x55);
- TFT_writeCommand(self, I9341_FRMCTR1);
- TFT_writeData(self, 0x00);
- TFT_writeData(self, 0x18);
- TFT_writeCommand(self, I9341_DFUNCTR);
- TFT_writeData(self, 0x08);
- TFT_writeData(self, 0x82);
- TFT_writeData(self, 0x27);
- TFT_writeCommand(self, 0xF2);
- TFT_writeData(self, 0x00);
- TFT_writeCommand(self, I9341_GAMMASET);
- TFT_writeData(self, 0x01);
- TFT_writeCommand(self, I9341_GMCTRP1);
- TFT_writeData(self, 0x0F);
- TFT_writeData(self, 0x31);
- TFT_writeData(self, 0x2B);
- TFT_writeData(self, 0x0C);
- TFT_writeData(self, 0x0E);
- TFT_writeData(self, 0x08);
- TFT_writeData(self, 0x4E);
- TFT_writeData(self, 0xF1);
- TFT_writeData(self, 0x37);
- TFT_writeData(self, 0x07);
- TFT_writeData(self, 0x10);
- TFT_writeData(self, 0x03);
- TFT_writeData(self, 0x0E);
- TFT_writeData(self, 0x09);
- TFT_writeData(self, 0x00);
- TFT_writeCommand(self, I9341_GMCTRN1);
- TFT_writeData(self, 0x00);
- TFT_writeData(self, 0x0E);
- TFT_writeData(self, 0x14);
- TFT_writeData(self, 0x03);
- TFT_writeData(self, 0x11);
- TFT_writeData(self, 0x07);
- TFT_writeData(self, 0x31);
- TFT_writeData(self, 0xC1);
- TFT_writeData(self, 0x48);
- TFT_writeData(self, 0x08);
- TFT_writeData(self, 0x0F);
- TFT_writeData(self, 0x0C);
- TFT_writeData(self, 0x31);
- TFT_writeData(self, 0x36);
- TFT_writeData(self, 0x0F);
- TFT_writeCommand(self, I9341_INVOFF);
- delay_ms(120);
- TFT_writeCommand(self, I9341_SLPOUT);
- delay_ms(120);
- TFT_writeCommand(self, I9341_DISPON);
- delay_ms(120);
- // Blank screen to a color; draw from 0,0 to 239,319.
- uint16_t color = 0x01FF10;
- TFT_writeCommand(self, I9341_CASET);
- TFT_writeData(self, (0 >> 8));
- TFT_writeData(self, (0 & 0xFF));
- TFT_writeData(self, (239 >> 8));
- TFT_writeData(self, (239 & 0xFF));
- TFT_writeCommand(self, I9341_PASET);
- TFT_writeData(self, (0 >> 8));
- TFT_writeData(self, (0 & 0xFF));
- TFT_writeData(self, (319 >> 8));
- TFT_writeData(self, (319 & 0xFF));
- TFT_writeCommand(self, I9341_RAMWR);
- for (x = 0; x < 240; ++x) {
- for (y = 0; y < 320; ++y) {
- TFT_writeData(self, color >> 8);
- TFT_writeData(self, color & 0xFF);
- }
- }
- }
- void TFT_writeEnable(ILI9341_TFT* self) {
- /* Set data pins to push-pull 2MHz output. */
- }
- void TFT_readEnable(ILI9341_TFT* self) {
- /* Set data pins to floating input. */
- }
- uint8_t TFT_read8(ILI9341_TFT* self) {
- // Pull the D/C pin high (data mode.)
- GPIO_SetBits(self->pins.p8.dat_cmd.bank,
- self->pins.p8.dat_cmd.pin);
- // Set BRR of 'rd_en' pin (pull low)
- // Indicates 'active' read state.
- GPIO_ResetBits(self->pins.p8.rd_en.bank,
- self->pins.p8.rd_en.pin);
- delay_us(TFT_US_DELAY);
- GPIO_SetBits(self->pins.p8.rd_en.bank,
- self->pins.p8.rd_en.pin);
- TFT_readEnable(self);
- // Data pins aren't necessarily on the same bank, so:
- uint8_t rd_dat = 0x0;
- int8_t t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat0.bank,
- rd_dat |= (t_bit << 0);
- t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat1.bank,
- self->pins.p8.dat1.pin);
- rd_dat |= (t_bit << 1);
- t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat2.bank,
- self->pins.p8.dat2.pin);
- rd_dat |= (t_bit << 2);
- t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat3.bank,
- self->pins.p8.dat3.pin);
- rd_dat |= (t_bit << 3);
- t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat4.bank,
- self->pins.p8.dat4.pin);
- rd_dat |= (t_bit << 4);
- t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat5.bank,
- self->pins.p8.dat5.pin);
- rd_dat |= (t_bit << 5);
- t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat6.bank,
- self->pins.p8.dat6.pin);
- rd_dat |= (t_bit << 6);
- t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat7.bank,
- self->pins.p8.dat7.pin);
- rd_dat |= (t_bit << 7);
- delay_us(TFT_US_DELAY);
- return rd_dat;
- }
- void TFT_write8(ILI9341_TFT* self, uint8_t dat) {
- TFT_writeEnable(self);
- // Write the given values to data pins.
- // Again, to keep things modular this could be much faster.
- if (dat & (1 << 0)) {
- GPIO_SetBits(self->pins.p8.dat0.bank,
- self->pins.p8.dat0.pin);
- } else {
- GPIO_ResetBits(self->pins.p8.dat0.bank,
- self->pins.p8.dat0.pin);
- } if (dat & (1 << 1)) {
- GPIO_SetBits(self->pins.p8.dat1.bank,
- self->pins.p8.dat1.pin);
- } else {
- GPIO_ResetBits(self->pins.p8.dat1.bank,
- self->pins.p8.dat1.pin);
- } if (dat & (1 << 2)) {
- GPIO_SetBits(self->pins.p8.dat2.bank,
- self->pins.p8.dat2.pin);
- } else {
- GPIO_ResetBits(self->pins.p8.dat2.bank,
- self->pins.p8.dat2.pin);
- } if (dat & (1 << 3)) {
- GPIO_SetBits(self->pins.p8.dat3.bank,
- self->pins.p8.dat3.pin);
- } else {
- GPIO_ResetBits(self->pins.p8.dat3.bank,
- self->pins.p8.dat3.pin);
- } if (dat & (1 << 4)) {
- GPIO_SetBits(self->pins.p8.dat4.bank,
- self->pins.p8.dat4.pin);
- } else {
- GPIO_ResetBits(self->pins.p8.dat4.bank,
- self->pins.p8.dat4.pin);
- } if (dat & (1 << 5)) {
- GPIO_SetBits(self->pins.p8.dat5.bank,
- self->pins.p8.dat5.pin);
- } else {
- GPIO_ResetBits(self->pins.p8.dat5.bank,
- self->pins.p8.dat5.pin);
- } if (dat & (1 << 6)) {
- GPIO_SetBits(self->pins.p8.dat6.bank,
- self->pins.p8.dat6.pin);
- } else {
- GPIO_ResetBits(self->pins.p8.dat6.bank,
- self->pins.p8.dat6.pin);
- } if (dat & (1 << 7)) {
- GPIO_SetBits(self->pins.p8.dat7.bank,
- self->pins.p8.dat7.pin);
- } else {
- GPIO_ResetBits(self->pins.p8.dat7.bank,
- self->pins.p8.dat7.pin);
- }
- // Cycle 'wr_en' between active/idle (pull low, then high.)
- GPIO_ResetBits(self->pins.p8.wr_en.bank,
- self->pins.p8.wr_en.pin);
- delay_us(TFT_US_DELAY);
- GPIO_SetBits(self->pins.p8.wr_en.bank,
- }
- void TFT_writeCommand(ILI9341_TFT* self, uint8_t comm) {
- // Pull the D/C pin low (command mode.)
- GPIO_ResetBits(self->pins.p8.dat_cmd.bank,
- self->pins.p8.dat_cmd.pin);
- // Write the 8-bit command.
- TFT_write8(self, comm);
- }
- void TFT_writeData(ILI9341_TFT* self, uint8_t dat) {
- // Pull the D/C pin high (data mode.)
- GPIO_SetBits(self->pins.p8.dat_cmd.bank,
- self->pins.p8.dat_cmd.pin);
- // Write the 8 bits of data.
- TFT_write8(self, dat);
- }
- // These commands aren't documented; I found them in Adafruit's
- // library, and a terse explanation involving 'magic numbers'
- // on their forum. So...I call a spade a spade.
- void unholyMagic(ILI9341_TFT* self) {
- TFT_writeCommand(self, 0xEF);
- TFT_writeData(self, 0x03);
- TFT_writeData(self, 0x80);
- TFT_writeData(self, 0x02);
- TFT_writeCommand(self, 0xCF);
- TFT_writeData(self, 0x00);
- TFT_writeData(self, 0xC1);
- TFT_writeData(self, 0x30);
- TFT_writeCommand(self, 0xED);
- TFT_writeData(self, 0x64);
- TFT_writeData(self, 0x03);
- TFT_writeData(self, 0x12);
- TFT_writeData(self, 0x81);
- TFT_writeCommand(self, 0xE8);
- TFT_writeData(self, 0x85);
- TFT_writeData(self, 0x00);
- TFT_writeData(self, 0x78);
- TFT_writeCommand(self, 0xCB);
- TFT_writeData(self, 0x39);
- TFT_writeData(self, 0x2C);
- TFT_writeData(self, 0x00);
- TFT_writeData(self, 0x34);
- TFT_writeData(self, 0x02);
- TFT_writeCommand(self, 0xF7);
- TFT_writeData(self, 0x20);
- TFT_writeCommand(self, 0xEA);
- TFT_writeData(self, 0x00);
- TFT_writeData(self, 0x00);
- }
- /* (Delay methods) */
- // Pick one
- void initSysTick_100us(void) {
- TickScale = 10000;
- while (SysTick_Config(SystemCoreClock / TickScale) != 0) {}
- }
- void initSysTick_10us(void) {
- TickScale = 100000;
- while (SysTick_Config(SystemCoreClock / TickScale) != 0) {}
- }
- void initSysTick_us(void) {
- TickScale = 1000000;
- while (SysTick_Config(SystemCoreClock / TickScale) != 0) {}
- }
- // Timed delays.
- void delay_ms(__IO uint32_t delay_m) {
- TimingDelay = delay_m * TickScale / 1000;
- while (TimingDelay != 0);
- }
- void delay_us(__IO uint32_t delay_u) {
- TimingDelay = delay_u * TickScale / 1000000;
- while (TimingDelay != 0);
- }
- // (Called in the SysTick)
- void TimingDelay_Decrement(void) {
- if (TimingDelay != 0) { TimingDelay--; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement