Moderators: adafruit_support_bill, adafruit
FOR MEGA ARDUINOS edit the SD/utility/Sd2Card.h file after installing and uncomment the line that says #define MEGA_SOFT_SPI 1 to allow the Mega to use the same pinout for SD cards as the Classic Arduinos
// Graphics library by ladyada/adafruit with init code from Rossum
// banned license
#include <WProgram.h>
// comment or uncomment the next line for special pinout!
#define USE_ADAFRUIT_SHIELD_PINOUT
// Graphics library by ladyada/adafruit with init code from Rossum
// banned license
#include <WProgram.h>
// comment or uncomment the next line for special pinout!
#define USE_ADAFRUIT_SHIELD_PINOUT
// register names from Peter Barrett's Microtouch code
#define TFTLCD_START_OSC 0x00
#define TFTLCD_DRIV_OUT_CTRL 0x01
#define TFTLCD_DRIV_WAV_CTRL 0x02
#define TFTLCD_ENTRY_MOD 0x03
#define TFTLCD_RESIZE_CTRL 0x04
#define TFTLCD_DISP_CTRL1 0x07
#define TFTLCD_DISP_CTRL2 0x08
#define TFTLCD_DISP_CTRL3 0x09
#define TFTLCD_DISP_CTRL4 0x0A
#define TFTLCD_RGB_DISP_IF_CTRL1 0x0C
#define TFTLCD_FRM_MARKER_POS 0x0D
#define TFTLCD_RGB_DISP_IF_CTRL2 0x0F
#define TFTLCD_POW_CTRL1 0x10
#define TFTLCD_POW_CTRL2 0x11
#define TFTLCD_POW_CTRL3 0x12
#define TFTLCD_POW_CTRL4 0x13
#define TFTLCD_GRAM_HOR_AD 0x20
#define TFTLCD_GRAM_VER_AD 0x21
#define TFTLCD_RW_GRAM 0x22
#define TFTLCD_POW_CTRL7 0x29
#define TFTLCD_FRM_RATE_COL_CTRL 0x2B
#define TFTLCD_GAMMA_CTRL1 0x30
#define TFTLCD_GAMMA_CTRL2 0x31
#define TFTLCD_GAMMA_CTRL3 0x32
#define TFTLCD_GAMMA_CTRL4 0x35
#define TFTLCD_GAMMA_CTRL5 0x36
#define TFTLCD_GAMMA_CTRL6 0x37
#define TFTLCD_GAMMA_CTRL7 0x38
#define TFTLCD_GAMMA_CTRL8 0x39
#define TFTLCD_GAMMA_CTRL9 0x3C
#define TFTLCD_GAMMA_CTRL10 0x3D
#define TFTLCD_HOR_START_AD 0x50
#define TFTLCD_HOR_END_AD 0x51
#define TFTLCD_VER_START_AD 0x52
#define TFTLCD_VER_END_AD 0x53
#define TFTLCD_GATE_SCAN_CTRL1 0x60
#define TFTLCD_GATE_SCAN_CTRL2 0x61
#define TFTLCD_GATE_SCAN_CTRL3 0x6A
#define TFTLCD_PART_IMG1_DISP_POS 0x80
#define TFTLCD_PART_IMG1_START_AD 0x81
#define TFTLCD_PART_IMG1_END_AD 0x82
#define TFTLCD_PART_IMG2_DISP_POS 0x83
#define TFTLCD_PART_IMG2_START_AD 0x84
#define TFTLCD_PART_IMG2_END_AD 0x85
#define TFTLCD_PANEL_IF_CTRL1 0x90
#define TFTLCD_PANEL_IF_CTRL2 0x92
#define TFTLCD_PANEL_IF_CTRL3 0x93
#define TFTLCD_PANEL_IF_CTRL4 0x95
#define TFTLCD_PANEL_IF_CTRL5 0x97
#define TFTLCD_PANEL_IF_CTRL6 0x98
#define TFTLCD_DELAYCMD 0xFF
#define swap(a, b) { int16_t t = a; a = b; b = t; }
class TFTLCD : public Print {
public:
TFTLCD(uint8_t cs, uint8_t cd, uint8_t wr, uint8_t rd, uint8_t reset);
uint16_t Color565(uint8_t r, uint8_t g, uint8_t b);
// drawing primitives!
void drawPixel(uint16_t x, uint16_t y, uint16_t color);
void fillScreen(uint16_t color);
void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
void drawFastLine(uint16_t x0, uint16_t y0, uint16_t l, uint16_t color, uint8_t flag);
void drawVerticalLine(uint16_t x0, uint16_t y0, uint16_t length, uint16_t color);
void drawHorizontalLine(uint16_t x0, uint16_t y0, uint16_t length, uint16_t color);
void drawTriangle(uint16_t x0, uint16_t y0,
uint16_t x1, uint16_t y1,
uint16_t x2, uint16_t y2, uint16_t color);
void fillTriangle(int32_t x0, int32_t y0,
int32_t x1, int32_t y1,
int32_t x2, int32_t y2,
uint16_t color);
void drawRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color);
void fillRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color);
void drawRoundRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t radius, uint16_t color);
void fillRoundRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t radius, uint16_t color);
void drawCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);
void fillCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);
void setCursor(uint16_t x, uint16_t y);
void setTextColor(uint16_t c);
void setTextSize(uint8_t s);
virtual void write(uint8_t);
void drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint8_t s = 1);
void drawString(uint16_t x, uint16_t y, char *c, uint16_t color, uint8_t s = 1);
// commands
void initDisplay(void);
void goHome(void);
void goTo(int x, int y);
void reset(void);
void setRotation(uint8_t x);
uint8_t getRotation();
/* low level */
void writeData(uint16_t d);
void writeCommand(uint16_t c);
uint16_t readData(void);
uint16_t readRegister(uint16_t addr);
void writeRegister(uint16_t addr, uint16_t data);
uint16_t width();
uint16_t height();
static const uint16_t TFTWIDTH = 240;
static const uint16_t TFTHEIGHT = 320;
void writeData_unsafe(uint16_t d);
void setWriteDir(void);
void setReadDir(void);
void write8(uint8_t d);
private:
void drawCircleHelper(uint16_t x0, uint16_t y0, uint16_t r, uint8_t corner, uint16_t color);
void fillCircleHelper(uint16_t x0, uint16_t y0, uint16_t r, uint8_t corner, uint16_t delta, uint16_t color);
uint8_t read8(void);
uint8_t _cs, _cd, _reset, _wr, _rd;
uint8_t csport, cdport, wrport, rdport;
uint8_t cspin, cdpin, wrpin, rdpin;
uint16_t _width, _height;
uint8_t textsize;
uint16_t cursor_x, cursor_y;
uint16_t textcolor;
uint8_t rotation;
};
/* Arduino Sd2Card Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino Sd2Card Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or banned FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino Sd2Card Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef Sd2Card_h
#define Sd2Card_h
/**
* \file
* Sd2Card class
*/
#include "Sd2PinMap.h"
#include "SdInfo.h"
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
uint8_t const SPI_FULL_SPEED = 0;
/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
uint8_t const SPI_HALF_SPEED = 1;
/** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
uint8_t const SPI_QUARTER_SPEED = 2;
/**
* Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
* Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
*
* MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
* on Mega Arduinos. Software SPI works well with GPS Shield V1.1
* but many SD cards will fail with GPS Shield V1.0.
*/
#define MEGA_SOFT_SPI 1
//------------------------------------------------------------------------------
#if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
#define SOFTWARE_SPI
#endif // MEGA_SOFT_SPI
//------------------------------------------------------------------------------
// SPI pin definitions
//
#ifndef SOFTWARE_SPI
// hardware pin defs
/**
* SD Chip Select pin
*
* Warning if this pin is redefined the hardware SS will pin will be enabled
* as an output by init(). An avr processor will not function as an SPI
* master unless SS is set to output mode.
*/
/** The default chip select pin for the SD card is SS. */
uint8_t const SD_CHIP_SELECT_PIN = SS_PIN;
// The following three pins must not be redefined for hardware SPI.
/** SPI Master Out Slave In pin */
uint8_t const SPI_MOSI_PIN = MOSI_PIN;
/** SPI Master In Slave Out pin */
uint8_t const SPI_MISO_PIN = MISO_PIN;
/** SPI Clock pin */
uint8_t const SPI_SCK_PIN = SCK_PIN;
/** optimize loops for hardware SPI */
#define OPTIMIZE_HARDWARE_SPI
#else // SOFTWARE_SPI
// define software SPI pins so Mega can use unmodified GPS Shield
/** SPI chip select pin */
uint8_t const SD_CHIP_SELECT_PIN = 10;
/** SPI Master Out Slave In pin */
uint8_t const SPI_MOSI_PIN = 11;
/** SPI Master In Slave Out pin */
uint8_t const SPI_MISO_PIN = 12;
/** SPI Clock pin */
uint8_t const SPI_SCK_PIN = 13;
#endif // SOFTWARE_SPI
//------------------------------------------------------------------------------
/** Protect block zero from write if nonzero */
#define SD_PROTECT_BLOCK_ZERO 1
/** init timeout ms */
uint16_t const SD_INIT_TIMEOUT = 2000;
/** erase timeout ms */
uint16_t const SD_ERASE_TIMEOUT = 10000;
/** read timeout ms */
uint16_t const SD_READ_TIMEOUT = 300;
/** write time out ms */
uint16_t const SD_WRITE_TIMEOUT = 600;
//------------------------------------------------------------------------------
// SD card errors
/** timeout error for command CMD0 */
uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
/** CMD8 was not accepted - not a valid SD card*/
uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
/** card returned an error response for CMD17 (read block) */
uint8_t const SD_CARD_ERROR_CMD17 = 0X3;
/** card returned an error response for CMD24 (write block) */
uint8_t const SD_CARD_ERROR_CMD24 = 0X4;
/** WRITE_MULTIPLE_BLOCKS command failed */
uint8_t const SD_CARD_ERROR_CMD25 = 0X05;
/** card returned an error response for CMD58 (read OCR) */
uint8_t const SD_CARD_ERROR_CMD58 = 0X06;
/** SET_WR_BLK_ERASE_COUNT failed */
uint8_t const SD_CARD_ERROR_ACMD23 = 0X07;
/** card's ACMD41 initialization process timeout */
uint8_t const SD_CARD_ERROR_ACMD41 = 0X08;
/** card returned a bad CSR version field */
uint8_t const SD_CARD_ERROR_BAD_CSD = 0X09;
/** erase block group command failed */
uint8_t const SD_CARD_ERROR_ERASE = 0X0A;
/** card not capable of single block erase */
uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0X0B;
/** Erase sequence timed out */
uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0X0C;
/** card returned an error token instead of read data */
uint8_t const SD_CARD_ERROR_READ = 0X0D;
/** read CID or CSD failed */
uint8_t const SD_CARD_ERROR_READ_REG = 0X0E;
/** timeout while waiting for start of read data */
uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X0F;
/** card did not accept STOP_TRAN_TOKEN */
uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X10;
/** card returned an error token as a response to a write operation */
uint8_t const SD_CARD_ERROR_WRITE = 0X11;
/** attempt to write protected block zero */
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X12;
/** card did not go ready for a multiple block write */
uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X13;
/** card returned an error to a CMD13 status check after a write */
uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
/** timeout occurred during write programming */
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
/** incorrect rate selected */
uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
//------------------------------------------------------------------------------
// card types
/** Standard capacity V1 SD card */
uint8_t const SD_CARD_TYPE_SD1 = 1;
/** Standard capacity V2 SD card */
uint8_t const SD_CARD_TYPE_SD2 = 2;
/** High Capacity SD card */
uint8_t const SD_CARD_TYPE_SDHC = 3;
//------------------------------------------------------------------------------
/**
* \class Sd2Card
* \brief Raw access to SD and SDHC flash memory cards.
*/
class Sd2Card {
public:
/** Construct an instance of Sd2Card. */
Sd2Card(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {}
uint32_t cardSize(void);
uint8_t erase(uint32_t firstBlock, uint32_t lastBlock);
uint8_t eraseSingleBlockEnable(void);
/**
* \return error code for last error. See Sd2Card.h for a list of error codes.
*/
uint8_t errorCode(void) const {return errorCode_;}
/** \return error data for last error. */
uint8_t errorData(void) const {return status_;}
/**
* Initialize an SD flash memory card with default clock rate and chip
* select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
*/
uint8_t init(void) {
return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN);
}
/**
* Initialize an SD flash memory card with the selected SPI clock rate
* and the default SD chip select pin.
* See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
*/
uint8_t init(uint8_t sckRateID) {
return init(sckRateID, SD_CHIP_SELECT_PIN);
}
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
void partialBlockRead(uint8_t value);
/** Returns the current value, true or false, for partial block read. */
uint8_t partialBlockRead(void) const {return partialBlockRead_;}
uint8_t readBlock(uint32_t block, uint8_t* dst);
uint8_t readData(uint32_t block,
uint16_t offset, uint16_t count, uint8_t* dst);
/**
* Read a cards CID register. The CID contains card identification
* information such as Manufacturer ID, Product name, Product serial
* number and Manufacturing date. */
uint8_t readCID(cid_t* cid) {
return readRegister(CMD10, cid);
}
/**
* Read a cards CSD register. The CSD contains Card-Specific Data that
* provides information regarding access to the card's contents. */
uint8_t readCSD(csd_t* csd) {
return readRegister(CMD9, csd);
}
void readEnd(void);
uint8_t setSckRate(uint8_t sckRateID);
/** Return the card type: SD V1, SD V2 or SDHC */
uint8_t type(void) const {return type_;}
uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src);
uint8_t writeData(const uint8_t* src);
uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount);
uint8_t writeStop(void);
private:
uint32_t block_;
uint8_t chipSelectPin_;
uint8_t errorCode_;
uint8_t inBlock_;
uint16_t offset_;
uint8_t partialBlockRead_;
uint8_t status_;
uint8_t type_;
// private functions
uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
cardCommand(CMD55, 0);
return cardCommand(cmd, arg);
}
uint8_t cardCommand(uint8_t cmd, uint32_t arg);
void error(uint8_t code) {errorCode_ = code;}
uint8_t readRegister(uint8_t cmd, void* buf);
uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
void chipSelectHigh(void);
void chipSelectLow(void);
void type(uint8_t value) {type_ = value;}
uint8_t waitNotBusy(uint16_t timeoutMillis);
uint8_t writeData(uint8_t token, const uint8_t* src);
uint8_t waitStartBlock(void);
};
#endif // Sd2Card_h

Return to Glowy things (LCD, LED, TFT, EL) purchased at Adafruit
Users browsing this forum: No registered users and 7 guests