void N6100LCD::set_area(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) { uint8_t paset, caset, ramwr; if (type_ == TYPE_EPSON) { paset = PASET; caset = CASET; ramwr = RAMWR; } else { paset = PASETP; caset = CASETP; ramwr = RAMWRP; } // rows spi_cmd(paset); spi_data(y1); spi_data(y2); // columns spi_cmd(caset); spi_data(x1); spi_data(x2); // start write spi_cmd(ramwr); }
// Send a SD command, num is the actual index, NOT OR'ed with 0x40. // arg is all four bytes of the argument byte sdc_cmd(byte commandIndex, long arg) { PORTB &= ~(1<<PIN_CS); // assert chip select for the card spi_cmd(0xFF); // dummy byte commandIndex |= 0x40; // command token OR'ed with 0x40 spi_cmd(commandIndex); // send command for (int i=3; i>=0; i--) { spi_cmd(arg>>(i*8)); // send argument in little endian form (MSB first) } spi_cmd(0x95); // checksum valid for GO_IDLE_STATE, not needed thereafter, so we can hardcode this value spi_cmd(0xFF); // dummy byte gives card time to process byte res = spi_cmd(0xFF); return (res); // query return value from card }
// initialize SD card // retuns 1 if successful byte sdc_initialize(void) { // set slow clock: 1/128 base frequency (125Khz in this case) SPCR |= (1<<SPR1) | (1<<SPR0); // set slow clock: 1/128 base frequency (125Khz in this case) SPSR &= ~(1<<SPI2X); // No doubled clock frequency // wake up SD card PORTB |= (1<<PIN_CS); // deasserts card for warmup PORTB |= (1<<PIN_MOSI); // set MOSI high for(byte i=0; i<10; i++) { spi_cmd(0xFF); // send 10 times 8 pulses for a warmup (74 minimum) } // set idle mode byte retries=0; PORTB &= ~(1<<PIN_CS); // assert chip select for the card while(sdc_cmd(GO_IDLE_STATE, 0) != 0x01) { // while SD card is not in iddle state retries++; if (retries >= 0xFF) { return(NULL); // timed out! } delay(5); } // at this stage, the card is in idle mode and ready for start up retries = 0; sdc_cmd(APP_CMD, 0); // startup sequence for SD cards 55/41 while (sdc_cmd(SEND_OP_COND, 0) != 0x00) { retries++; if (retries >= 0xFF) { return(NULL); // timed out! } sdc_cmd(APP_CMD, 0); } // set fast clock, 1/4 CPU clock frequency (4Mhz in this case) SPCR &= ~((1<<SPR1) | (1<<SPR0)); // Clock Frequency: f_OSC / 4 SPSR |= (1<<SPI2X); // Doubled Clock Frequency: f_OSC / 2 return (0x01); // returned value (success) }
void N6100LCD::set_pixel(uint8_t x, uint8_t y, uint16_t color) { set_area(x, y, x, y); spi_data((color >> 4) & 0xFF); spi_data(((color & 0x0F) << 4)); spi_cmd(NOP); spi_flush(); }
// read SD card register content and store it in vBuffer void sdc_readRegister(byte sentCommand) { byte retries=0x00; byte res=sdc_cmd(sentCommand, 0); while(res != 0x00) { delay(1); retries++; if (retries >= 0xFF) return; // timed out! res=spi_cmd(0xFF); // retry } // wait for data token while (spi_cmd(0xFF) != 0xFE); // read data for (int i=0; i<16; i++) { vBuffer[i] = spi_cmd(0xFF); } // read CRC (lost results in blue sky) spi_cmd(0xFF); // LSB spi_cmd(0xFF); // MSB }
// read block on SD card and copy data in block vector // retuns 1 if successful void sdc_readBlock(long blockIndex) { byte retries = 0x00; byte res = sdc_cmd(READ_SINGLE_BLOCK, (blockIndex * blockSize)); while(res != 0x00) { delay(1); retries++; if (retries >= 0xFF) return; // timed out! res=spi_cmd(0xFF); // retry } // read data packet (includes data token, data block and CRC) // read data token while (spi_cmd(0xFF) != 0xFE); // read data block for (int i=0; i<blockSize; i++) { vBlock[i] = spi_cmd(0xFF); // read data } // read CRC (lost results in blue sky) spi_cmd(0xFF); // LSB spi_cmd(0xFF); // MSB }
/****************************************************************************** * * Function: fpga_get_details * * Description: This function the details of fpga device. * * Parameters: None * * Return Value: status (For debugging purpose only) ******************************************************************************/ FPGA_STATUS fpga_get_details(uint8_t *id) { FPGA_STATUS ret; /* Claim the SPI controller */ //ret = spi_claim(1,SPI_FPGA_OPFREQ); ret = fpga_spiclaim(SPI_FPGA_OPFREQ); if (ret) { spi_release(); return FAIL; } // spi_xfer(4,NULL,id,TRUE); /* send command to read the ID codes */ ret = spi_cmd(CMDPKT_FPGA_READID,id,sizeof(id)); if (ret) { spi_release(); return FAIL; } spi_release(); return SUCCESS; }
// write block on SD card // addr is the address in bytes (multiples of block size) void sdc_writeBlock(long blockIndex) { byte retries=0; while(sdc_cmd(WRITE_BLOCK, blockIndex * blockSize) != 0x00) { delay(1); retries++; if (retries >= 0xFF) return; // timed out! } spi_cmd(0xFF); // dummy byte (at least one) // send data packet (includes data token, data block and CRC) // data token spi_cmd(0xFE); // copy block data for (int i=0; i<blockSize; i++) { spi_cmd(vBlock[i]); } // write CRC (lost results in blue sky) spi_cmd(0xFF); // LSB spi_cmd(0xFF); // MSB // wait until write is finished while (spi_cmd(0xFF) != 0xFF) delay(1); // kind of NOP }
int N6100LCD::init(void) { if (MAP_FAILED==gpio_ && gpio_init() < 0) { DBG("gpio init failed\n"); return -1; } if (spi_ < 0 && spi_init() < 0) { DBG("spi init failed\n"); return -1; } DBG("gpio = %X\n", gpio_); DBG("spi = %d\n", spi_); INP_GPIO(rst_); OUT_GPIO(rst_); GPIO_CLR(rst_); usleep(200*1000); GPIO_SET(rst_); usleep(200*1000); if (type_ == TYPE_EPSON) { spi_cmd(DISCTL); // Display control (0xCA) spi_data(0x0C); // 12 = 1100 - CL dividing ratio [don't divide] switching period 8H (default) spi_data(0x20); // nlines/4 - 1 = 132/4 - 1 = 32 duty spi_data(0x00); // No inversely highlighted lines spi_cmd(COMSCN); // common scanning direction (0xBB) spi_data(0x01); // 1->68, 132<-69 scan direction spi_cmd(OSCON); // internal oscialltor ON (0xD1) spi_cmd(SLPOUT); // sleep out (0x94) spi_cmd(PWRCTR); // power ctrl (0x20) spi_data(0x0F); // everything on, no external reference resistors spi_cmd(DISINV); // invert display mode (0xA7) spi_cmd(DATCTL); // data control (0xBC) spi_data(0x03); // Inverse page address, reverse rotation column address, column scan-direction !!! try 0x01 spi_data(0x00); // normal RGB arrangement spi_data(0x02); // 16-bit Grayscale Type A (12-bit color) spi_cmd(VOLCTR); // electronic volume, this is the contrast/brightness (0x81) spi_data(32); // volume (contrast) setting - fine tuning, original (0-63) spi_data(3); // internal resistor ratio - coarse adjustment (0-7) spi_cmd(NOP); usleep(100 * 1000); spi_cmd(DISON); // display on (0xAF) } else if (type_ == TYPE_PHILIPS) { spi_cmd(SLEEPOUT); // Sleep Out (0x11) spi_cmd(BSTRON); // Booster voltage on (0x03) spi_cmd(DISPON); // Display on (0x29) //spi_cmd(INVON); // Inversion on (0x20) // 12-bit color pixel format: spi_cmd(COLMOD); // Color interface format (0x3A) spi_data(0x03); // 0b011 is 12-bit/pixel mode spi_cmd(MADCTL); // Memory Access Control(PHILLIPS) //spi_data(0x08); spi_data(0xC0); spi_cmd(SETCON); // Set Contrast(PHILLIPS) spi_data(0x3E); spi_cmd(NOPP); // nop(PHILLIPS) } spi_flush(); DBG("lcd init ok\n"); return 0; }