uint8_t sdmmc_readSector(uint32_t pSectorNum, char* pOutput) { SET_CS(); // Send command 17 (READ_SINGLE_BLOCK) if (sdmmc_writeCommand(SDMMC_READ_SINGLE_BLOCK, SECTOR_TO_BYTE(pSectorNum), SDMMC_DEFAULT_CRC) != 0) { CLEAR_CS(); return FALSE; } uint8_t response = 0; uint8_t retry = 0; // Wait for start-byte while(response != 0xFE) { response = spi_readByte(); if (retry++ == 0xFF) { CLEAR_CS(); return FALSE; } } // Read the block for (uint16_t i = 0; i < fBlockLength; i++) { pOutput[i] = spi_readByte(); } // Ignore the CRC checksum spi_readByte(); spi_readByte(); CLEAR_CS(); return TRUE; }
void writeData(uint8_t c) { SET_DC(); CLR_CS(); spiWrite(c); SET_CS(); }
void writeCommand(uint8_t c) { CLR_DC(); CLR_CS(); spiWrite(c); SET_CS(); }
void drawPixel(int16_t x, int16_t y, uint16_t color) { if(x < 0 || x >= _width || y < 0 || y >= _height) return; setAddrWindow(x,y,x+1,y+1); SET_DC(); CLR_CS(); spiWrite(color >> 8); spiWrite(color); SET_CS(); }
uint8_t sdmmc_changeBlockLength(uint16_t pLength) { if (pLength == 0) { pLength = SDMMC_SECTOR_SIZE; } SET_CS(); if (sdmmc_writeCommand(SDMMC_SET_BLOCKLEN, pLength, SDMMC_DEFAULT_CRC) != 0) { CLEAR_CS(); return FALSE; } CLEAR_CS(); fBlockLength = pLength; return TRUE; }
uint8_t sdmmc_writeCommand(uint8_t pCommand, uint32_t pArgument, uint8_t pCrc) { // The command is composed out of 6 Byte: // Byte 1: 0b01xxxxxx where xx = pCommand // Byte 2-5: pArgument // Byte 6: 0byyyyyyy1 where yy = CRC7 (ignored when card is in SPI mode) // Make sure that bits 7 and 6 of pCommand are 0 and 1 pCommand &= ~(1 << 7); pCommand |= (1 << 6); // Calculate CRC7 (// TODO //, currently precalculated values) // Bit 0 of the checksum byte has to be set pCrc |= 1; // Send some dummy clock signals CLEAR_CS(); spi_writeByte(0xFF); SET_CS(); // Send the data spi_writeByte(pCommand); spi_writeByte((uint8_t)((pArgument & 0xFF000000) >> 24)); spi_writeByte((uint8_t)((pArgument & 0x00FF0000) >> 16)); spi_writeByte((uint8_t)((pArgument & 0x0000FF00) >> 8)); spi_writeByte((uint8_t)(pArgument & 0x000000FF)); spi_writeByte(pCrc); // Get a response uint8_t result = 0xFF; uint8_t retry = 0; while(result == 0xFF) { result = spi_readByte(); if (retry++ == 0xFF) { result = 0; break; } } return result; }
uint8_t sdmmc_writeSector(uint32_t pSectorNum, char* pInput) { SET_CS(); // Send command 24 (WRITE_BLOCK) if (sdmmc_writeCommand(SDMMC_WRITE_BLOCK, SECTOR_TO_BYTE(pSectorNum), SDMMC_DEFAULT_CRC) != 0) { CLEAR_CS(); return FALSE; } // Send some dummy clocks for(uint8_t i = 0; i < 100; i++) { spi_readByte(); } // Send start-byte spi_writeByte(0xFE); // Send data for(uint16_t i = 0; i < fBlockLength; i++) { spi_writeByte(pInput[i]); } // Send dummy CRC checksum (won't be checked in SPI mode) spi_writeByte(0xFF); spi_writeByte(0xFF); // Get response if ((spi_readByte() & 0x1F) != 0x05) { CLEAR_CS(); return FALSE; } // Wait until memory card is ready for new commands while (spi_readByte() != 0xFF) { // burn energy } CLEAR_CS(); return TRUE; }
void initLcd(void) { CLR_RST(); SET_CS(); SET_DC(); delay_ms(5); SET_RST(); delay_ms(5); CLR_RST(); delay_ms(20); SET_RST(); delay_ms(150); _width = ILI9341_TFTWIDTH; _height = ILI9341_TFTHEIGHT; writeCommand(0xEF); writeData(0x03); writeData(0x80); writeData(0x02); writeCommand(0xCF); writeData(0x00); writeData(0XC1); writeData(0X30); writeCommand(0xED); writeData(0x64); writeData(0x03); writeData(0X12); writeData(0X81); writeCommand(0xE8); writeData(0x85); writeData(0x00); writeData(0x78); writeCommand(0xCB); writeData(0x39); writeData(0x2C); writeData(0x00); writeData(0x34); writeData(0x02); writeCommand(0xF7); writeData(0x20); writeCommand(0xEA); writeData(0x00); writeData(0x00); writeCommand(ILI9341_PWCTR1); //Power control writeData(0x23); //VRH[5:0] writeCommand(ILI9341_PWCTR2); //Power control writeData(0x10); //SAP[2:0];BT[3:0] writeCommand(ILI9341_VMCTR1); //VCM control writeData(0x3e); writeData(0x28); writeCommand(ILI9341_VMCTR2); //VCM control2 writeData(0x86); //-- writeCommand(ILI9341_MADCTL); // Memory Access Control writeData(ILI9341_MADCTL_MX | ILI9341_MADCTL_BGR); writeCommand(ILI9341_PIXFMT); writeData(0x55); writeCommand(ILI9341_FRMCTR1); writeData(0x00); writeData(0x18); writeCommand(ILI9341_DFUNCTR); // Display Function Control writeData(0x08); writeData(0x82); writeData(0x27); writeCommand(0xF2); // 3Gamma Function Disable writeData(0x00); writeCommand(ILI9341_GAMMASET); //Gamma curve selected writeData(0x01); writeCommand(ILI9341_GMCTRP1); //Set Gamma writeData(0x0F); writeData(0x31); writeData(0x2B); writeData(0x0C); writeData(0x0E); writeData(0x08); writeData(0x4E); writeData(0xF1); writeData(0x37); writeData(0x07); writeData(0x10); writeData(0x03); writeData(0x0E); writeData(0x09); writeData(0x00); writeCommand(ILI9341_GMCTRN1); //Set Gamma writeData(0x00); writeData(0x0E); writeData(0x14); writeData(0x03); writeData(0x11); writeData(0x07); writeData(0x31); writeData(0xC1); writeData(0x48); writeData(0x08); writeData(0x0F); writeData(0x0C); writeData(0x31); writeData(0x36); writeData(0x0F); writeCommand(ILI9341_SLPOUT); //Exit Sleep delay_ms(120); writeCommand(ILI9341_DISPON); //Display on }