Advertisement
Guest User

ILI9341 8-bit parallel code...that doesn't work

a guest
Oct 7th, 2017
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.67 KB | None | 0 0
  1. // Datasheet says the controller only needs nanoseconds between rise/falls.
  2. #define TFT_US_DELAY 1
  3.  
  4. void beginDisplay(ILI9341_TFT* self) {
  5.     GPIO_ResetBits(self->pins.p8.chip_sel.bank,
  6.                    self->pins.p8.chip_sel.pin);
  7.     GPIO_ResetBits(self->pins.p8.dat_cmd.bank,
  8.                  self->pins.p8.dat_cmd.pin);
  9.     GPIO_SetBits(self->pins.p8.wr_en.bank,
  10.                  self->pins.p8.wr_en.pin);
  11.     GPIO_SetBits(self->pins.p8.rd_en.bank,
  12.                  self->pins.p8.rd_en.pin);
  13.     GPIO_SetBits(self->pins.p8.reset.bank,
  14.                  self->pins.p8.reset.pin);
  15.     delay_ms(120);
  16.     GPIO_ResetBits(self->pins.p8.reset.bank,
  17.                    self->pins.p8.reset.pin);
  18.     delay_ms(120);
  19.     GPIO_SetBits(self->pins.p8.reset.bank,
  20.                  self->pins.p8.reset.pin);
  21.     delay_ms(120);
  22.  
  23.     // Exit sleep.
  24.     TFT_writeCommand(self, 0x11);
  25.     delay_ms(120);
  26.  
  27.     // Test 'read ID/version' commands.
  28.     TFT_writeCommand(self, 0xDA);
  29.     uint8_t temp = TFT_read8(self);
  30.     temp = TFT_read8(self);
  31.     TFT_writeCommand(self, 0xDB);
  32.     temp = TFT_read8(self);
  33.     temp = TFT_read8(self);
  34.     TFT_writeCommand(self, 0xDC);
  35.     temp = TFT_read8(self);
  36.     temp = TFT_read8(self);
  37.  
  38.     // (Undocumented-but-required commands according to
  39.     //  Adafruit and other examples)
  40.     unholyMagic(self);
  41.  
  42.     // Startup commands.
  43.     TFT_writeCommand(self, I9341_PWCTR1);
  44.     TFT_writeData(self, 0x23);
  45.     TFT_writeCommand(self, I9341_PWCTR2);
  46.     TFT_writeData(self, 0x10);
  47.     TFT_writeCommand(self, I9341_VCMCTR1);
  48.     TFT_writeData(self, 0x3e);
  49.     TFT_writeData(self, 0x28);
  50.     TFT_writeCommand(self, I9341_VCMCTR2);
  51.     TFT_writeData(self, 0x86);
  52.     TFT_writeCommand(self, I9341_MADCTL);
  53.     TFT_writeData(self, 0x48);
  54.     TFT_writeCommand(self, I9341_PIXFMT);
  55.     TFT_writeData(self, 0x55);
  56.     TFT_writeCommand(self, I9341_FRMCTR1);
  57.     TFT_writeData(self, 0x00);
  58.     TFT_writeData(self, 0x18);
  59.     TFT_writeCommand(self, I9341_DFUNCTR);
  60.     TFT_writeData(self, 0x08);
  61.     TFT_writeData(self, 0x82);
  62.     TFT_writeData(self, 0x27);
  63.     TFT_writeCommand(self, 0xF2);
  64.     TFT_writeData(self, 0x00);
  65.     TFT_writeCommand(self, I9341_GAMMASET);
  66.     TFT_writeData(self, 0x01);
  67.     TFT_writeCommand(self, I9341_GMCTRP1);
  68.     TFT_writeData(self, 0x0F);
  69.     TFT_writeData(self, 0x31);
  70.     TFT_writeData(self, 0x2B);
  71.     TFT_writeData(self, 0x0C);
  72.     TFT_writeData(self, 0x0E);
  73.     TFT_writeData(self, 0x08);
  74.     TFT_writeData(self, 0x4E);
  75.     TFT_writeData(self, 0xF1);
  76.     TFT_writeData(self, 0x37);
  77.     TFT_writeData(self, 0x07);
  78.     TFT_writeData(self, 0x10);
  79.     TFT_writeData(self, 0x03);
  80.     TFT_writeData(self, 0x0E);
  81.     TFT_writeData(self, 0x09);
  82.     TFT_writeData(self, 0x00);
  83.     TFT_writeCommand(self, I9341_GMCTRN1);
  84.     TFT_writeData(self, 0x00);
  85.     TFT_writeData(self, 0x0E);
  86.     TFT_writeData(self, 0x14);
  87.     TFT_writeData(self, 0x03);
  88.     TFT_writeData(self, 0x11);
  89.     TFT_writeData(self, 0x07);
  90.     TFT_writeData(self, 0x31);
  91.     TFT_writeData(self, 0xC1);
  92.     TFT_writeData(self, 0x48);
  93.     TFT_writeData(self, 0x08);
  94.     TFT_writeData(self, 0x0F);
  95.     TFT_writeData(self, 0x0C);
  96.     TFT_writeData(self, 0x31);
  97.     TFT_writeData(self, 0x36);
  98.     TFT_writeData(self, 0x0F);
  99.     TFT_writeCommand(self, I9341_INVOFF);
  100.     delay_ms(120);
  101.     TFT_writeCommand(self, I9341_SLPOUT);
  102.     delay_ms(120);
  103.     TFT_writeCommand(self, I9341_DISPON);
  104.     delay_ms(120);
  105.  
  106.     // Blank screen to a color; draw from 0,0 to 239,319.
  107.     uint16_t color = 0x01FF10;
  108.     TFT_writeCommand(self, I9341_CASET);
  109.     TFT_writeData(self, (0 >> 8));
  110.     TFT_writeData(self, (0 & 0xFF));
  111.     TFT_writeData(self, (239 >> 8));
  112.     TFT_writeData(self, (239 & 0xFF));
  113.     TFT_writeCommand(self, I9341_PASET);
  114.     TFT_writeData(self, (0 >> 8));
  115.     TFT_writeData(self, (0 & 0xFF));
  116.     TFT_writeData(self, (319 >> 8));
  117.     TFT_writeData(self, (319 & 0xFF));
  118.     TFT_writeCommand(self, I9341_RAMWR);
  119.     for (x = 0; x < 240; ++x) {
  120.         for (y = 0; y < 320; ++y) {
  121.             TFT_writeData(self, color >> 8);
  122.             TFT_writeData(self, color & 0xFF);
  123.         }
  124.     }
  125. }
  126.  
  127. void TFT_writeEnable(ILI9341_TFT* self) {
  128.     /* Set data pins to push-pull 2MHz output. */
  129. }
  130. void TFT_readEnable(ILI9341_TFT* self) {
  131.     /* Set data pins to floating input. */
  132. }
  133.  
  134. uint8_t TFT_read8(ILI9341_TFT* self) {
  135.     // Pull the D/C pin high (data mode.)
  136.     GPIO_SetBits(self->pins.p8.dat_cmd.bank,
  137.                  self->pins.p8.dat_cmd.pin);
  138.     // Set BRR of 'rd_en' pin (pull low)
  139.     // Indicates 'active' read state.
  140.     GPIO_ResetBits(self->pins.p8.rd_en.bank,
  141.                    self->pins.p8.rd_en.pin);
  142.     delay_us(TFT_US_DELAY);
  143.     GPIO_SetBits(self->pins.p8.rd_en.bank,
  144.                  self->pins.p8.rd_en.pin);
  145.     TFT_readEnable(self);
  146.     // Data pins aren't necessarily on the same bank, so:
  147.     uint8_t rd_dat = 0x0;
  148.     int8_t t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat0.bank,
  149.     rd_dat |= (t_bit << 0);
  150.     t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat1.bank,
  151.                                   self->pins.p8.dat1.pin);
  152.     rd_dat |= (t_bit << 1);
  153.     t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat2.bank,
  154.                                   self->pins.p8.dat2.pin);
  155.     rd_dat |= (t_bit << 2);
  156.     t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat3.bank,
  157.                                   self->pins.p8.dat3.pin);
  158.     rd_dat |= (t_bit << 3);
  159.     t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat4.bank,
  160.                                   self->pins.p8.dat4.pin);
  161.     rd_dat |= (t_bit << 4);
  162.     t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat5.bank,
  163.                                   self->pins.p8.dat5.pin);
  164.     rd_dat |= (t_bit << 5);
  165.     t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat6.bank,
  166.                                   self->pins.p8.dat6.pin);
  167.     rd_dat |= (t_bit << 6);
  168.     t_bit = GPIO_ReadInputDataBit(self->pins.p8.dat7.bank,
  169.                                   self->pins.p8.dat7.pin);
  170.     rd_dat |= (t_bit << 7);
  171.  
  172.     delay_us(TFT_US_DELAY);
  173.     return rd_dat;
  174. }
  175.  
  176. void TFT_write8(ILI9341_TFT* self, uint8_t dat) {
  177.     TFT_writeEnable(self);
  178.     // Write the given values to data pins.
  179.     // Again, to keep things modular this could be much faster.
  180.     if (dat & (1 << 0)) {
  181.         GPIO_SetBits(self->pins.p8.dat0.bank,
  182.                      self->pins.p8.dat0.pin);
  183.     } else {
  184.         GPIO_ResetBits(self->pins.p8.dat0.bank,
  185.                       self->pins.p8.dat0.pin);
  186.     } if (dat & (1 << 1)) {
  187.         GPIO_SetBits(self->pins.p8.dat1.bank,
  188.                      self->pins.p8.dat1.pin);
  189.     } else {
  190.         GPIO_ResetBits(self->pins.p8.dat1.bank,
  191.                        self->pins.p8.dat1.pin);
  192.     } if (dat & (1 << 2)) {
  193.         GPIO_SetBits(self->pins.p8.dat2.bank,
  194.                      self->pins.p8.dat2.pin);
  195.     } else {
  196.         GPIO_ResetBits(self->pins.p8.dat2.bank,
  197.                        self->pins.p8.dat2.pin);
  198.     } if (dat & (1 << 3)) {
  199.         GPIO_SetBits(self->pins.p8.dat3.bank,
  200.                      self->pins.p8.dat3.pin);
  201.     } else {
  202.         GPIO_ResetBits(self->pins.p8.dat3.bank,
  203.                        self->pins.p8.dat3.pin);
  204.     } if (dat & (1 << 4)) {
  205.         GPIO_SetBits(self->pins.p8.dat4.bank,
  206.                      self->pins.p8.dat4.pin);
  207.     } else {
  208.         GPIO_ResetBits(self->pins.p8.dat4.bank,
  209.                        self->pins.p8.dat4.pin);
  210.     } if (dat & (1 << 5)) {
  211.         GPIO_SetBits(self->pins.p8.dat5.bank,
  212.                      self->pins.p8.dat5.pin);
  213.     } else {
  214.         GPIO_ResetBits(self->pins.p8.dat5.bank,
  215.                        self->pins.p8.dat5.pin);
  216.     } if (dat & (1 << 6)) {
  217.         GPIO_SetBits(self->pins.p8.dat6.bank,
  218.                      self->pins.p8.dat6.pin);
  219.     } else {
  220.         GPIO_ResetBits(self->pins.p8.dat6.bank,
  221.                        self->pins.p8.dat6.pin);
  222.     } if (dat & (1 << 7)) {
  223.         GPIO_SetBits(self->pins.p8.dat7.bank,
  224.                      self->pins.p8.dat7.pin);
  225.     } else {
  226.         GPIO_ResetBits(self->pins.p8.dat7.bank,
  227.                        self->pins.p8.dat7.pin);
  228.     }
  229.  
  230.     // Cycle 'wr_en' between active/idle (pull low, then high.)
  231.     GPIO_ResetBits(self->pins.p8.wr_en.bank,
  232.                  self->pins.p8.wr_en.pin);
  233.     delay_us(TFT_US_DELAY);
  234.     GPIO_SetBits(self->pins.p8.wr_en.bank,
  235. }
  236.  
  237. void TFT_writeCommand(ILI9341_TFT* self, uint8_t comm) {
  238.     // Pull the D/C pin low (command mode.)
  239.     GPIO_ResetBits(self->pins.p8.dat_cmd.bank,
  240.                    self->pins.p8.dat_cmd.pin);
  241.     // Write the 8-bit command.
  242.     TFT_write8(self, comm);
  243. }
  244.  
  245. void TFT_writeData(ILI9341_TFT* self, uint8_t dat) {
  246.     // Pull the D/C pin high (data mode.)
  247.     GPIO_SetBits(self->pins.p8.dat_cmd.bank,
  248.                  self->pins.p8.dat_cmd.pin);
  249.     // Write the 8 bits of data.
  250.     TFT_write8(self, dat);
  251. }
  252.  
  253. // These commands aren't documented; I found them in Adafruit's
  254. // library, and a terse explanation involving 'magic numbers'
  255. // on their forum. So...I call a spade a spade.
  256. void unholyMagic(ILI9341_TFT* self) {
  257.     TFT_writeCommand(self, 0xEF);
  258.     TFT_writeData(self, 0x03);
  259.     TFT_writeData(self, 0x80);
  260.     TFT_writeData(self, 0x02);
  261.     TFT_writeCommand(self, 0xCF);
  262.     TFT_writeData(self, 0x00);
  263.     TFT_writeData(self, 0xC1);
  264.     TFT_writeData(self, 0x30);
  265.     TFT_writeCommand(self, 0xED);
  266.     TFT_writeData(self, 0x64);
  267.     TFT_writeData(self, 0x03);
  268.     TFT_writeData(self, 0x12);
  269.     TFT_writeData(self, 0x81);
  270.     TFT_writeCommand(self, 0xE8);
  271.     TFT_writeData(self, 0x85);
  272.     TFT_writeData(self, 0x00);
  273.     TFT_writeData(self, 0x78);
  274.     TFT_writeCommand(self, 0xCB);
  275.     TFT_writeData(self, 0x39);
  276.     TFT_writeData(self, 0x2C);
  277.     TFT_writeData(self, 0x00);
  278.     TFT_writeData(self, 0x34);
  279.     TFT_writeData(self, 0x02);
  280.     TFT_writeCommand(self, 0xF7);
  281.     TFT_writeData(self, 0x20);
  282.     TFT_writeCommand(self, 0xEA);
  283.     TFT_writeData(self, 0x00);
  284.     TFT_writeData(self, 0x00);
  285. }
  286.  
  287. /* (Delay methods) */
  288. // Pick one
  289. void initSysTick_100us(void) {
  290.     TickScale = 10000;
  291.     while (SysTick_Config(SystemCoreClock / TickScale) != 0) {}
  292. }
  293. void initSysTick_10us(void) {
  294.     TickScale = 100000;
  295.     while (SysTick_Config(SystemCoreClock / TickScale) != 0) {}
  296. }
  297. void initSysTick_us(void) {
  298.     TickScale = 1000000;
  299.     while (SysTick_Config(SystemCoreClock / TickScale) != 0) {}
  300. }
  301. // Timed delays.
  302. void delay_ms(__IO uint32_t delay_m) {
  303.     TimingDelay = delay_m * TickScale / 1000;
  304.     while (TimingDelay != 0);
  305. }
  306. void delay_us(__IO uint32_t delay_u) {
  307.     TimingDelay = delay_u * TickScale / 1000000;
  308.     while (TimingDelay != 0);
  309. }
  310. // (Called in the SysTick)
  311. void TimingDelay_Decrement(void) {
  312.     if (TimingDelay != 0) { TimingDelay--; }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement