bool SDFS::begin(uint8_t ssPin, SPIClass &spi, uint32_t frequency, const char * mountpoint) { if(_pdrv != 0xFF) { return true; } spi.begin(); _pdrv = sdcard_init(ssPin, &spi, frequency); if(_pdrv == 0xFF) { return false; } if(!sdcard_mount(_pdrv, mountpoint)){ sdcard_uninit(_pdrv); _pdrv = 0xFF; return false; } _impl->mountpoint(mountpoint); return true; }
/** Initialize an SD flash memory card. \param[in] sckRateID SPI clock rate selector. See setSckRate(). \param[in] chipSelectPin SD chip select pin number. \return The value one, true, is returned for success and the value zero, false, is returned for failure. The reason for failure can be determined by calling errorCode() and errorData(). */ uint8_t Sd2Card::init(uint8_t chipSelectPin, uint8_t sckRateID, int8_t SPI_Port, int8_t cardDetectionPin, int8_t level) { // Serial.println("> Sd2Card::init"); errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0; chipSelectPin_ = chipSelectPin; SPI_Port_ = SPI_Port; cardDetectionPin_ = cardDetectionPin; level_ = level; // 16-bit init start time allows over a minute uint16_t t0 = (uint16_t)millis(); uint32_t arg; // set pin modes pinMode(chipSelectPin_, OUTPUT); chipSelectHigh(); if (cardDetectionPin_ >= 0) { pinMode(cardDetectionPin_, INPUT_PULLUP); } //#ifndef USE_SPI_LIB // pinMode(SPI_MISO_PIN, INPUT); // pinMode(SPI_MOSI_PIN, OUTPUT); // pinMode(SPI_SCK_PIN, OUTPUT); //#endif //#ifndef SOFTWARE_SPI //#ifndef USE_SPI_LIB // // SS must be in output mode even it is not chip select // pinMode(SS_PIN, OUTPUT); // digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin // // Enable SPI, Master, clock rate f_osc/128 // SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0); // // clear double speed // SPSR &= ~(1 << SPI2X); //#else // USE_SPI_LIB /// @todo Add SPI port selection for LM4F and TM4C SPI_for_SD.begin(); #if defined(__LM4F120H5QR__) || defined(__TM4C1230C3PM__) || defined(__TM4C123GH6PM__) || defined(__TM4C129XNCZAD__) || defined(__TM4C1294NCPDT__) // LM4F and TM4C specific if (SPI_Port >= 0) { SPI_for_SD.setModule(SPI_Port); } #endif #ifdef SPI_CLOCK_DIV128 // Serial.println("> SPI_Port 128"); SPI_for_SD.setClockDivider(SPI_CLOCK_DIV128); #else // Serial.println("> SPI_Port 255"); SPI_for_SD.setClockDivider(255); #endif //#endif // USE_SPI_LIB //#endif // SOFTWARE_SPI // Hardware card detection if (cardDetectionPin_ >= 0) { // debugln("hardware card detection %i should be %i", digitalRead(cardDetectionPin_), level); if (digitalRead(cardDetectionPin_) != level_) { // Serial.println("*** hardware failure"); error(SD_CARD_ERROR_CMD0); // I don't like goto but this is how it is implemented goto fail; } } // Software card detection // must supply min of 74 clock cycles with CS high. for (uint8_t i = 0; i < 10; i++) { spiSend(0xff); } chipSelectLow(); // Serial.print("software card detection "); // command to go idle in SPI mode while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) { if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) { error(SD_CARD_ERROR_CMD0); // Serial.println("*** software failure"); goto fail; } } // check SD version if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) { type(SD_CARD_TYPE_SD1); } else { // only need last byte of r7 response for (uint8_t i = 0; i < 4; i++) { status_ = spiRec(); } if (status_ != 0xaa) { error(SD_CARD_ERROR_CMD8); goto fail; } type(SD_CARD_TYPE_SD2); } // initialize card and send host supports SDHC if SD2 arg = type() == SD_CARD_TYPE_SD2 ? 0x40000000 : 0; while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { // check for timeout if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) { error(SD_CARD_ERROR_ACMD41); goto fail; } } // if SD2 read OCR register to check for SDHC card if (type() == SD_CARD_TYPE_SD2) { if (cardCommand(CMD58, 0)) { error(SD_CARD_ERROR_CMD58); goto fail; } if ((spiRec() & 0xc0) == 0xc0) { type(SD_CARD_TYPE_SDHC); } // discard rest of ocr - contains allowed voltage range for (uint8_t i = 0; i < 3; i++) { spiRec(); } } chipSelectHigh(); #ifndef SOFTWARE_SPI return setSckRate(sckRateID); #else // SOFTWARE_SPI return true; #endif // SOFTWARE_SPI fail: chipSelectHigh(); return false; }
void Enc28J60Network::init(uint8_t* macaddr) { MemoryPool::init(); // 1 byte in between RX_STOP_INIT and pool to allow prepending of controlbyte // initialize I/O // ss as output: pinMode(ENC28J60_CONTROL_CS, OUTPUT); CSPASSIVE; // ss=0 // #ifdef ENC28J60DEBUG Serial.println("ENC28J60::initialize / before initSPI()"); #endif SPI.begin(); SPI.setBitOrder(MSBFIRST); // SPI.setDataMode(SPI_MODE0); // SPI.setClockDivider(SPI_CLOCK_DIV16); #ifdef ENC28J60DEBUG Serial.println("ENC28J60::initialize / after initSPI()"); Serial.print("ENC28J60::initialize / csPin = "); Serial.println(SPI.nssPin()); Serial.print("ENC28J60::initialize / miso = "); Serial.println(SPI.misoPin()); Serial.print("ENC28J60::initialize / mosi = "); Serial.println(SPI.mosiPin()); Serial.print("ENC28J60::initialize / sck = "); Serial.println(SPI.sckPin()); #endif selectPin = ENC28J60_CONTROL_CS; pinMode(selectPin, OUTPUT); digitalWrite(selectPin, HIGH); // perform system reset writeOp(ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET); delay(2); // errata B7/2 delay(50); // check CLKRDY bit to see if reset is complete // The CLKRDY does not work. See Rev. B4 Silicon Errata point. Just wait. //while(!(readReg(ESTAT) & ESTAT_CLKRDY)); // do bank 0 stuff // initialize receive buffer // 16-bit transfers, must write low byte first // set receive buffer start address #ifdef ENC28J60DEBUG Serial.println("ENC28J60::initialize / before readOp(ENC28J60_READ_CTRL_REG, ESTAT)"); #endif while (!readOp(ENC28J60_READ_CTRL_REG, ESTAT) & ESTAT_CLKRDY) ; #ifdef ENC28J60DEBUG Serial.println("ENC28J60::initialize / after readOp(ENC28J60_READ_CTRL_REG, ESTAT)"); #endif nextPacketPtr = RXSTART_INIT; // Rx start writeRegPair(ERXSTL, RXSTART_INIT); // set receive pointer address writeRegPair(ERXRDPTL, RXSTART_INIT); // RX end writeRegPair(ERXNDL, RXSTOP_INIT); // TX start //-------------writeRegPair(ETXSTL, TXSTART_INIT); // TX end //-------------writeRegPair(ETXNDL, TXSTOP_INIT); // do bank 1 stuff, packet filter: // For broadcast packets we allow only ARP packtets // All other packets should be unicast only for our mac (MAADR) // // The pattern to match on is therefore // Type ETH.DST // ARP BROADCAST // 06 08 -- ff ff ff ff ff ff -> ip checksum for theses bytes=f7f9 // in binary these poitions are:11 0000 0011 1111 // This is hex 303F->EPMM0=0x3f,EPMM1=0x30 //TODO define specific pattern to receive dhcp-broadcast packages instead of setting ERFCON_BCEN! // enableBroadcast(); // change to add ERXFCON_BCEN recommended by epam writeReg(ERXFCON, ERXFCON_UCEN|ERXFCON_CRCEN|ERXFCON_PMEN|ERXFCON_BCEN); writeRegPair(EPMM0, 0x303f); writeRegPair(EPMCSL, 0xf7f9); // // // do bank 2 stuff // enable MAC receive // and bring MAC out of reset (writes 0x00 to MACON2) writeRegPair(MACON1, MACON1_MARXEN|MACON1_TXPAUS|MACON1_RXPAUS); //----------------writeRegPair(MACON2, 0x00); // enable automatic padding to 60bytes and CRC operations writeOp(ENC28J60_BIT_FIELD_SET, MACON3, MACON3_PADCFG0|MACON3_TXCRCEN|MACON3_FRMLNEN); // set inter-frame gap (non-back-to-back) writeRegPair(MAIPGL, 0x0C12); // set inter-frame gap (back-to-back) writeReg(MABBIPG, 0x12); // Set the maximum packet size which the controller will accept // Do not send packets longer than MAX_FRAMELEN: writeRegPair(MAMXFLL, MAX_FRAMELEN); // do bank 3 stuff // write MAC address // NOTE: MAC address in ENC28J60 is byte-backward writeReg(MAADR5, macaddr[0]); writeReg(MAADR4, macaddr[1]); writeReg(MAADR3, macaddr[2]); writeReg(MAADR2, macaddr[3]); writeReg(MAADR1, macaddr[4]); writeReg(MAADR0, macaddr[5]); // no loopback of transmitted frames phyWrite(PHCON2, PHCON2_HDLDIS); // switch to bank 0 setBank(ECON1); // enable interrutps writeOp(ENC28J60_BIT_FIELD_SET, EIE, EIE_INTIE|EIE_PKTIE); // enable packet reception writeOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN); //Configure leds phyWrite(PHLCON,0x476); byte rev = readReg(EREVID); // microchip forgot to step the number on the silcon when they // released the revision B7. 6 is now rev B7. We still have // to see what they do when they release B8. At the moment // there is no B8 out yet if (rev > 5) ++rev; #ifdef ENC28J60DEBUG Serial.print("ENC28J60::initialize returns "); Serial.println(rev); #endif // return rev; }