Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * FT810.c
- *
- * Created on: 1 Haz 2016
- * Author: Abdullah
- */
- #include "FT810.h"
- #include "board.h"
- uint8_t SPI_transceive(uint8_t tx_data)
- {
- //Clear Tx and Rx FIFO
- //DSPI0->MCR |= SPI_MCR_CLR_RXF_MASK | SPI_MCR_CLR_TXF_MASK;
- //tmp = DSPI0->POPR;
- // Clear all flags
- DSPI0->SR = kDSPI_AllStatusFlag;
- // Clock out the data
- DSPI0->PUSHR = tx_data;
- // Wait for the transfer to complete
- while ((DSPI0->SR & SPI_SR_TCF_MASK) == 0)
- ;
- return DSPI0->POPR;
- }
- uint8_t FT810_memory_read_8(uint32_t address)
- {
- uint8_t buf;
- // For SPI memory read transaction, the host sends
- // two zero bits, followed by the 22-bit address.
- // This is followed by a dummy byte. After the
- // dummy byte, the FT810 responds to each host
- // byte with read data_ bytes.
- FT810_clear_CS();
- EVE_AddrForRd(address);
- buf = EVE_Read8();
- FT810_set_CS();
- return buf;
- }
- uint16_t FT810_memory_read_16(uint32_t address)
- {
- uint16_t buf;
- // For SPI memory read transaction, the host sends
- // two zero bits, followed by the 22-bit address.
- // This is followed by a dummy byte. After the
- // dummy byte, the FT810 responds to each host
- // byte with read data_ bytes.
- FT810_clear_CS();
- EVE_AddrForRd(address);
- buf = EVE_Read16();
- FT810_set_CS();
- return buf;
- }
- uint32_t FT810_memory_read_32(uint32_t address)
- {
- uint32_t buf;
- // For SPI memory read transaction, the host sends
- // two zero bits, followed by the 22-bit address.
- // This is followed by a dummy byte. After the
- // dummy byte, the FT810 responds to each host
- // byte with read data_ bytes.
- FT810_clear_CS();
- EVE_AddrForRd(address);
- buf = EVE_Read32();
- FT810_set_CS();
- return buf;
- }
- void FT810_memory_write_8(uint32_t address, uint8_t data_)
- {
- // For SPI memory write transaction, the host sends
- // a 1 and a 0 bit, followed by the 22-bit address.
- // Then bytes to be written are sent.
- FT810_clear_CS();
- // Write Memory Address
- EVE_AddrForWr(address);
- // Write the data_
- EVE_Write8(data_);
- FT810_set_CS();
- }
- void FT810_memory_write_16(uint32_t address, uint16_t data_)
- {
- // For SPI memory write transaction, the host sends
- // a 1 and a 0 bit, followed by the 22-bit address.
- // Then bytes to be written are sent.
- FT810_clear_CS();
- // Write Memory Address
- EVE_AddrForWr(address);
- // Write the data_
- EVE_Write16(data_);
- FT810_set_CS();
- }
- void FT810_memory_write_32(uint32_t address, uint32_t data_)
- {
- // For SPI memory write transaction, the host sends
- // a 1 and a 0 bit, followed by the 22-bit address.
- // Then bytes to be written are sent.
- FT810_clear_CS();
- // Write Memory Address
- EVE_AddrForWr(address);
- // Write the data_
- EVE_Write32(data_);
- FT810_set_CS();
- }
- void EVE_AddrForRd(uint32_t address)
- {
- // To write to memory, make the MSB of the first byte set 1
- // Write the memory address bits 21..to..16
- SPI_transceive(((uint8_t) ((address & 0x3F0000) >> 16)));
- // Write the memory address bits 15..to..8
- SPI_transceive((uint8_t) ((address & 0xFF00) >> 8));
- // Write the memory address bits 7..to..0
- SPI_transceive((uint8_t) (address & 0xFF));
- // Send dummy byte before doing the read
- SPI_transceive(0x00);
- }
- void EVE_AddrForWr(uint32_t address)
- {
- // To write to memory, make the MSB of the first byte set 1
- // Write the memory address bits 21..to..16
- SPI_transceive(0x80 | ((uint8_t) ((address & 0x3F0000) >> 16)));
- // Write the memory address bits 15..to..8
- SPI_transceive((uint8_t) ((address & 0xFF00) >> 8));
- // Write the memory address bits 7..to..0
- SPI_transceive((uint8_t) (address & 0xFF));
- }
- void EVE_Write8(uint8_t data_)
- {
- SPI_transceive((uint8_t) (data_ & 0xFF));
- }
- void EVE_Write16(uint16_t data_)
- {
- SPI_transceive((uint8_t) (data_ & 0xFF));
- SPI_transceive((uint8_t) ((data_ & 0xFF00) >> 8));
- }
- void EVE_Write32(uint32_t data_)
- {
- SPI_transceive((uint8_t) (data_ & 0xFF));
- SPI_transceive((uint8_t) ((data_ & 0xFF00) >> 8));
- SPI_transceive((uint8_t) ((data_ & 0xFF0000) >> 16));
- SPI_transceive((uint8_t) ((data_ & 0xFF000000) >> 24));
- }
- uint8_t EVE_Read8(void)
- {
- uint8_t buf;
- // Read the FIFO to the destination by sending
- // a dummy byte
- buf = (uint8_t) (SPI_transceive(0x00));
- return buf;
- }
- uint16_t EVE_Read16(void)
- {
- uint16_t buf;
- // Read the FIFO to the destination by sending
- // a dummy byte
- buf = (uint16_t) (SPI_transceive(0x00));
- buf |= (uint16_t) (SPI_transceive(0x00)) << 8;
- return buf;
- }
- uint32_t EVE_Read32(void)
- {
- uint32_t buf;
- // Read the FIFO to the destination by sending
- // a dummy byte
- buf = (uint32_t) (SPI_transceive(0x00));
- buf |= (uint32_t) (SPI_transceive(0x00)) << 8;
- buf |= (uint32_t) (SPI_transceive(0x00)) << 16;
- buf |= (uint32_t) (SPI_transceive(0x00)) << 24;
- return buf;
- }
- void FT810_write_command(uint8_t command, uint8_t parameter)
- {
- // For SPI command transaction, the host sends a 0 bit
- // and 1 bit, followed by the 6-bit command code.
- // This is followed by 2 bytes 00h.
- // Start by clearing the chip select pin
- FT810_clear_CS();
- // Send the command byte
- SPI_transceive(command);
- // Send the parameter
- SPI_transceive(parameter);
- // Add a dummy filling byte
- SPI_transceive(0x00);
- // Release the CS
- FT810_set_CS();
- }
- int8_t FT810_init(void)
- {
- volatile uint8_t buffer = 0;
- volatile uint32_t timeout;
- // Note: Keep SPI below 11MHz here
- // Initially set CS(chip select).
- FT810_set_CS();
- // Clear PD pin to shut down FT810
- FT810_clear_PD();
- // Wait for about 20ms for settling
- for (timeout = 0x3FFFF; timeout != 0; timeout--)
- ;
- // Set PD pin to make FT810 alive
- FT810_set_PD();
- // Wait for about 20ms for reset to complete
- for (timeout = 0x3FFFF; timeout != 0; timeout--)
- ;
- FT810_write_command(FT810_ACTIVE, 0);
- // Wait for about 500ms for settling
- for (timeout = 0x1FFFFFF; timeout != 0; timeout--)
- ;
- buffer = FT810_memory_read_8(FT810_REG_ID);
- if (buffer != 0x7C)
- return -3;
- // Wait a little
- for (timeout = 0x1FFFF; timeout != 0; timeout--)
- ;
- buffer = FT810_memory_read_8(FT810_REG_CPURESET);
- if (buffer != 0x00)
- return -4;
- // Note: Could now increase SPI clock rate up to 30MHz SPI if preferred
- // Turn OFF backlight
- // FT810_memory_write_8(FT810_REG_PWM_DUTY, 0);
- // Set PCLK to zero - don't clock the LCD until later
- // FT810_memory_write_8(FT810_REG_PCLK, 0);
- // Initialize Display Parameters
- FT810_memory_write_16(FT810_REG_HCYCLE, SCREEN_HCYCLE);
- FT810_memory_write_16(FT810_REG_HOFFSET, SCREEN_HOFFSET);
- FT810_memory_write_16(FT810_REG_HSYNC0, SCREEN_HSYNC0);
- FT810_memory_write_16(FT810_REG_HSYNC1, SCREEN_HSYNC1);
- FT810_memory_write_16(FT810_REG_VCYCLE, SCREEN_VCYCLE);
- FT810_memory_write_16(FT810_REG_VOFFSET, SCREEN_VOFFSET);
- FT810_memory_write_16(FT810_REG_VSYNC0, SCREEN_VSYNC0);
- FT810_memory_write_16(FT810_REG_VSYNC1, SCREEN_VSYNC1);
- FT810_memory_write_8(FT810_REG_SWIZZLE, SCREEN_SWIZZLE);
- FT810_memory_write_8(FT810_REG_PCLK_POL, SCREEN_PCLK_POL);
- FT810_memory_write_16(FT810_REG_HSIZE, SCREEN_HSIZE);
- FT810_memory_write_16(FT810_REG_VSIZE, SCREEN_VSIZE);
- // Set Disp GPIO Direction
- // Bit 7 enables the LCD Display. It is set to output driving high
- // Bit 1 enables the Audio Amplifier. It is set to output driving low to shut down the amplifier since audio not used here
- // Bit 0 is unused but set to output driving high.
- FT810_memory_write_8(FT810_REG_GPIO_DIR, 0x83);
- // Wait a little
- for (timeout = 0xFFFF; timeout != 0; timeout--)
- ;
- // Enable the display
- FT810_memory_write_8(FT810_REG_GPIO, 0x81);
- // Start clocking the LCD
- FT810_memory_write_8(FT810_REG_PCLK, SCREEN_PCLK);
- // Set backlight PWM frequency
- FT810_memory_write_16(FT810_REG_PWM_HZ, 750);
- // Turn ON backlight
- FT810_memory_write_8(FT810_REG_PWM_DUTY, 125);
- // Write Initial Display List & Enable Display
- // Set Initial Color to BLACK
- FT810_memory_write_32(FT810_RAM_DL + 0, CLEAR_COLOR_RGB(0, 0xFF, 0));
- // Clear to the Initial Color
- FT810_memory_write_32(FT810_RAM_DL + 4, CLEAR(1, 1, 1));
- // End Display List
- FT810_memory_write_32(FT810_RAM_DL + 8, FT810_DL_DISPLAY);
- // Make this display list active on the next frame
- FT810_memory_write_32(FT810_REG_DLSWAP, FT810_DLSWAP_FRAME);
- // Wait for about 20ms for the commands to process
- for (timeout = 0x3FFFF; timeout != 0; timeout--)
- ;
- return 0;
- }
- void FT810_CMD(uint32_t data_)
- {
- uint32_t buf_write_reg;
- if (FT810_CMD_FIFO_empty() == 1)
- {
- // Check the actual value of the current WRITE register (pointer)
- buf_write_reg = FT810_memory_read_32(FT810_REG_CMD_WRITE);
- // Make offset as the current value of the CMD_WRITE pointer in
- // the FIFO, write the command to that pointer
- FT810_memory_write_32(FT810_RAM_CMD + buf_write_reg, data_);
- // Increment the pointer by 4 bytes
- FT810_memory_write_32(FT810_REG_CMD_WRITE, buf_write_reg + 4);
- }
- }
- void EVE_CmdWrite(uint8_t command, uint8_t parameter)
- {
- // For SPI command transaction, the host sends a 0 bit
- // and 1 bit, followed by the 6-bit command code.
- // This is followed by 2 bytes 00h.
- command = 0x40 | (command & 0x3F);
- // Start by clearing the chip select pin
- FT810_clear_CS();
- // Send the command byte
- SPI_transceive(command);
- // Send the parameter
- SPI_transceive(parameter);
- // Add a dummy filling byte
- SPI_transceive(0x00);
- // Release the CS
- FT810_set_CS();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement