Advertisement
abdullahkahraman

FT810.c / FT810 CMD_LOGO Problem

Aug 1st, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.39 KB | None | 0 0
  1. /*
  2.  * FT810.c
  3.  *
  4.  *  Created on: 1 Haz 2016
  5.  *      Author: Abdullah
  6.  */
  7.  
  8. #include "FT810.h"
  9. #include "board.h"
  10.  
  11. uint8_t SPI_transceive(uint8_t tx_data)
  12. {
  13.     //Clear Tx and Rx FIFO
  14.     //DSPI0->MCR |= SPI_MCR_CLR_RXF_MASK | SPI_MCR_CLR_TXF_MASK;
  15.     //tmp = DSPI0->POPR;
  16.     // Clear all flags
  17.     DSPI0->SR = kDSPI_AllStatusFlag;
  18.  
  19.     // Clock out the data
  20.     DSPI0->PUSHR = tx_data;
  21.  
  22.     // Wait for the transfer to complete
  23.     while ((DSPI0->SR & SPI_SR_TCF_MASK) == 0)
  24.         ;
  25.  
  26.     return DSPI0->POPR;
  27. }
  28.  
  29. uint8_t FT810_memory_read_8(uint32_t address)
  30. {
  31.     uint8_t buf;
  32.  
  33.     // For SPI memory read transaction, the host sends
  34.     // two zero bits, followed by the 22-bit address.
  35.     // This is followed by a dummy byte. After the
  36.     // dummy byte, the FT810 responds to each host
  37.     // byte with read data_ bytes.
  38.  
  39.     FT810_clear_CS();
  40.  
  41.     EVE_AddrForRd(address);
  42.     buf = EVE_Read8();
  43.  
  44.     FT810_set_CS();
  45.  
  46.     return buf;
  47. }
  48.  
  49. uint16_t FT810_memory_read_16(uint32_t address)
  50. {
  51.     uint16_t buf;
  52.  
  53.     // For SPI memory read transaction, the host sends
  54.     // two zero bits, followed by the 22-bit address.
  55.     // This is followed by a dummy byte. After the
  56.     // dummy byte, the FT810 responds to each host
  57.     // byte with read data_ bytes.
  58.  
  59.     FT810_clear_CS();
  60.  
  61.     EVE_AddrForRd(address);
  62.     buf = EVE_Read16();
  63.  
  64.     FT810_set_CS();
  65.  
  66.     return buf;
  67. }
  68.  
  69. uint32_t FT810_memory_read_32(uint32_t address)
  70. {
  71.     uint32_t buf;
  72.  
  73.     // For SPI memory read transaction, the host sends
  74.     // two zero bits, followed by the 22-bit address.
  75.     // This is followed by a dummy byte. After the
  76.     // dummy byte, the FT810 responds to each host
  77.     // byte with read data_ bytes.
  78.  
  79.     FT810_clear_CS();
  80.  
  81.     EVE_AddrForRd(address);
  82.     buf = EVE_Read32();
  83.  
  84.     FT810_set_CS();
  85.  
  86.     return buf;
  87. }
  88.  
  89. void FT810_memory_write_8(uint32_t address, uint8_t data_)
  90. {
  91.     // For SPI memory write transaction, the host sends
  92.     // a 1 and a 0 bit, followed by the 22-bit address.
  93.     // Then bytes to be written are sent.
  94.  
  95.     FT810_clear_CS();
  96.  
  97.     // Write Memory Address
  98.     EVE_AddrForWr(address);
  99.  
  100.     // Write the data_
  101.     EVE_Write8(data_);
  102.  
  103.     FT810_set_CS();
  104. }
  105.  
  106. void FT810_memory_write_16(uint32_t address, uint16_t data_)
  107. {
  108.     // For SPI memory write transaction, the host sends
  109.     // a 1 and a 0 bit, followed by the 22-bit address.
  110.     // Then bytes to be written are sent.
  111.  
  112.     FT810_clear_CS();
  113.  
  114.     // Write Memory Address
  115.     EVE_AddrForWr(address);
  116.  
  117.     // Write the data_
  118.     EVE_Write16(data_);
  119.  
  120.     FT810_set_CS();
  121. }
  122.  
  123. void FT810_memory_write_32(uint32_t address, uint32_t data_)
  124. {
  125.     // For SPI memory write transaction, the host sends
  126.     // a 1 and a 0 bit, followed by the 22-bit address.
  127.     // Then bytes to be written are sent.
  128.  
  129.     FT810_clear_CS();
  130.  
  131.     // Write Memory Address
  132.     EVE_AddrForWr(address);
  133.  
  134.     // Write the data_
  135.     EVE_Write32(data_);
  136.  
  137.     FT810_set_CS();
  138. }
  139.  
  140. void EVE_AddrForRd(uint32_t address)
  141. {
  142.     // To write to memory, make the MSB of the first byte set 1
  143.     // Write the memory address bits 21..to..16
  144.     SPI_transceive(((uint8_t) ((address & 0x3F0000) >> 16)));
  145.  
  146.     // Write the memory address bits 15..to..8
  147.     SPI_transceive((uint8_t) ((address & 0xFF00) >> 8));
  148.  
  149.     // Write the memory address bits 7..to..0
  150.     SPI_transceive((uint8_t) (address & 0xFF));
  151.  
  152.     // Send dummy byte before doing the read
  153.     SPI_transceive(0x00);
  154. }
  155.  
  156. void EVE_AddrForWr(uint32_t address)
  157. {
  158.     // To write to memory, make the MSB of the first byte set 1
  159.     // Write the memory address bits 21..to..16
  160.     SPI_transceive(0x80 | ((uint8_t) ((address & 0x3F0000) >> 16)));
  161.  
  162.     // Write the memory address bits 15..to..8
  163.     SPI_transceive((uint8_t) ((address & 0xFF00) >> 8));
  164.  
  165.     // Write the memory address bits 7..to..0
  166.     SPI_transceive((uint8_t) (address & 0xFF));
  167. }
  168.  
  169. void EVE_Write8(uint8_t data_)
  170. {
  171.     SPI_transceive((uint8_t) (data_ & 0xFF));
  172. }
  173.  
  174. void EVE_Write16(uint16_t data_)
  175. {
  176.     SPI_transceive((uint8_t) (data_ & 0xFF));
  177.     SPI_transceive((uint8_t) ((data_ & 0xFF00) >> 8));
  178. }
  179.  
  180. void EVE_Write32(uint32_t data_)
  181. {
  182.     SPI_transceive((uint8_t) (data_ & 0xFF));
  183.     SPI_transceive((uint8_t) ((data_ & 0xFF00) >> 8));
  184.     SPI_transceive((uint8_t) ((data_ & 0xFF0000) >> 16));
  185.     SPI_transceive((uint8_t) ((data_ & 0xFF000000) >> 24));
  186. }
  187.  
  188. uint8_t EVE_Read8(void)
  189. {
  190.     uint8_t buf;
  191.  
  192.     // Read the FIFO to the destination by sending
  193.     // a dummy byte
  194.     buf = (uint8_t) (SPI_transceive(0x00));
  195.  
  196.     return buf;
  197. }
  198.  
  199. uint16_t EVE_Read16(void)
  200. {
  201.     uint16_t buf;
  202.  
  203.     // Read the FIFO to the destination by sending
  204.     // a dummy byte
  205.     buf = (uint16_t) (SPI_transceive(0x00));
  206.     buf |= (uint16_t) (SPI_transceive(0x00)) << 8;
  207.  
  208.     return buf;
  209. }
  210.  
  211. uint32_t EVE_Read32(void)
  212. {
  213.     uint32_t buf;
  214.  
  215.     // Read the FIFO to the destination by sending
  216.     // a dummy byte
  217.     buf = (uint32_t) (SPI_transceive(0x00));
  218.     buf |= (uint32_t) (SPI_transceive(0x00)) << 8;
  219.     buf |= (uint32_t) (SPI_transceive(0x00)) << 16;
  220.     buf |= (uint32_t) (SPI_transceive(0x00)) << 24;
  221.  
  222.     return buf;
  223. }
  224.  
  225. void FT810_write_command(uint8_t command, uint8_t parameter)
  226. {
  227.     // For SPI command transaction, the host sends a 0 bit
  228.     // and 1 bit, followed by the 6-bit command code.
  229.     // This is followed by 2 bytes 00h.
  230.  
  231.     // Start by clearing the chip select pin
  232.     FT810_clear_CS();
  233.     // Send the command byte
  234.     SPI_transceive(command);
  235.     // Send the parameter
  236.     SPI_transceive(parameter);
  237.     // Add a dummy filling byte
  238.     SPI_transceive(0x00);
  239.     // Release the CS
  240.     FT810_set_CS();
  241. }
  242.  
  243. int8_t FT810_init(void)
  244. {
  245.     volatile uint8_t buffer = 0;
  246.     volatile uint32_t timeout;
  247.     // Note: Keep SPI below 11MHz here
  248.  
  249.     // Initially set CS(chip select).
  250.     FT810_set_CS();
  251.     // Clear PD pin to shut down FT810
  252.     FT810_clear_PD();
  253.     // Wait for about 20ms for settling
  254.     for (timeout = 0x3FFFF; timeout != 0; timeout--)
  255.         ;
  256.     // Set PD pin to make FT810 alive
  257.     FT810_set_PD();
  258.     // Wait for about 20ms for reset to complete
  259.     for (timeout = 0x3FFFF; timeout != 0; timeout--)
  260.         ;
  261.  
  262.     FT810_write_command(FT810_ACTIVE, 0);
  263.  
  264.     // Wait for about 500ms for settling
  265.     for (timeout = 0x1FFFFFF; timeout != 0; timeout--)
  266.         ;
  267.  
  268.     buffer = FT810_memory_read_8(FT810_REG_ID);
  269.     if (buffer != 0x7C)
  270.         return -3;
  271.  
  272.     // Wait a little
  273.     for (timeout = 0x1FFFF; timeout != 0; timeout--)
  274.         ;
  275.  
  276.     buffer = FT810_memory_read_8(FT810_REG_CPURESET);
  277.     if (buffer != 0x00)
  278.         return -4;
  279.  
  280.     // Note: Could now increase SPI clock rate up to 30MHz SPI if preferred
  281.  
  282.     // Turn OFF backlight
  283.     // FT810_memory_write_8(FT810_REG_PWM_DUTY, 0);
  284.  
  285.     // Set PCLK to zero - don't clock the LCD until later
  286.     // FT810_memory_write_8(FT810_REG_PCLK, 0);
  287.  
  288.     // Initialize Display Parameters
  289.     FT810_memory_write_16(FT810_REG_HCYCLE, SCREEN_HCYCLE);
  290.     FT810_memory_write_16(FT810_REG_HOFFSET, SCREEN_HOFFSET);
  291.     FT810_memory_write_16(FT810_REG_HSYNC0, SCREEN_HSYNC0);
  292.     FT810_memory_write_16(FT810_REG_HSYNC1, SCREEN_HSYNC1);
  293.     FT810_memory_write_16(FT810_REG_VCYCLE, SCREEN_VCYCLE);
  294.     FT810_memory_write_16(FT810_REG_VOFFSET, SCREEN_VOFFSET);
  295.     FT810_memory_write_16(FT810_REG_VSYNC0, SCREEN_VSYNC0);
  296.     FT810_memory_write_16(FT810_REG_VSYNC1, SCREEN_VSYNC1);
  297.     FT810_memory_write_8(FT810_REG_SWIZZLE, SCREEN_SWIZZLE);
  298.     FT810_memory_write_8(FT810_REG_PCLK_POL, SCREEN_PCLK_POL);
  299.     FT810_memory_write_16(FT810_REG_HSIZE, SCREEN_HSIZE);
  300.     FT810_memory_write_16(FT810_REG_VSIZE, SCREEN_VSIZE);
  301.  
  302.     // Set Disp GPIO Direction
  303.     // Bit 7 enables the LCD Display. It is set to output driving high
  304.     // Bit 1 enables the Audio Amplifier. It is set to output driving low to shut down the amplifier since audio not used here
  305.     // Bit 0 is unused but set to output driving high.
  306.     FT810_memory_write_8(FT810_REG_GPIO_DIR, 0x83);
  307.     // Wait a little
  308.     for (timeout = 0xFFFF; timeout != 0; timeout--)
  309.         ;
  310.     // Enable the display
  311.     FT810_memory_write_8(FT810_REG_GPIO, 0x81);
  312.  
  313.     // Start clocking the LCD
  314.     FT810_memory_write_8(FT810_REG_PCLK, SCREEN_PCLK);
  315.  
  316.     // Set backlight PWM frequency
  317.     FT810_memory_write_16(FT810_REG_PWM_HZ, 750);
  318.     // Turn ON backlight
  319.     FT810_memory_write_8(FT810_REG_PWM_DUTY, 125);
  320.  
  321.     // Write Initial Display List & Enable Display
  322.  
  323.     // Set Initial Color to BLACK
  324.     FT810_memory_write_32(FT810_RAM_DL + 0, CLEAR_COLOR_RGB(0, 0xFF, 0));
  325.     // Clear to the Initial Color
  326.     FT810_memory_write_32(FT810_RAM_DL + 4, CLEAR(1, 1, 1));
  327.     // End Display List
  328.     FT810_memory_write_32(FT810_RAM_DL + 8, FT810_DL_DISPLAY);
  329.  
  330.     // Make this display list active on the next frame
  331.     FT810_memory_write_32(FT810_REG_DLSWAP, FT810_DLSWAP_FRAME);
  332.  
  333.     // Wait for about 20ms for the commands to process
  334.     for (timeout = 0x3FFFF; timeout != 0; timeout--)
  335.         ;
  336.  
  337.     return 0;
  338. }
  339.  
  340.  
  341. void FT810_CMD(uint32_t data_)
  342. {
  343.     uint32_t buf_write_reg;
  344.     if (FT810_CMD_FIFO_empty() == 1)
  345.     {
  346.         // Check the actual value of the current WRITE register (pointer)
  347.         buf_write_reg = FT810_memory_read_32(FT810_REG_CMD_WRITE);
  348.         // Make offset as  the current value of the CMD_WRITE pointer in
  349.         // the FIFO, write the command to that pointer
  350.         FT810_memory_write_32(FT810_RAM_CMD + buf_write_reg, data_);
  351.         // Increment the pointer by 4 bytes
  352.         FT810_memory_write_32(FT810_REG_CMD_WRITE, buf_write_reg + 4);
  353.     }
  354. }
  355.  
  356. void EVE_CmdWrite(uint8_t command, uint8_t parameter)
  357. {
  358.     // For SPI command transaction, the host sends a 0 bit
  359.     // and 1 bit, followed by the 6-bit command code.
  360.     // This is followed by 2 bytes 00h.
  361.  
  362.     command = 0x40 | (command & 0x3F);
  363.     // Start by clearing the chip select pin
  364.     FT810_clear_CS();
  365.     // Send the command byte
  366.     SPI_transceive(command);
  367.     // Send the parameter
  368.     SPI_transceive(parameter);
  369.     // Add a dummy filling byte
  370.     SPI_transceive(0x00);
  371.     // Release the CS
  372.     FT810_set_CS();
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement