void zg_init() { uint8_t clr; spiBegin(SS_PB2); spiSetClockDivider(SPI_CLOCK_DIV2); clr = SPSR; clr = SPDR; intr_occured = 0; intr_valid = 0; lastRssi = 0; zg_drv_state = DRV_STATE_INIT; zg_conn_status = 0; tx_ready = 0; rx_ready = 0; cnf_pending = 0; zg_buf = uip_buf; zg_buf_len = UIP_BUFSIZE; zg_chip_reset(); zg_interrupt2_reg(); zg_interrupt_reg(0xff, 0); zg_interrupt_reg(0x80|0x40, 1); ssid_len = (uint8_t)strlen((char *)ssid); security_passphrase_len = (uint8_t)strlen_P((PGM_P)security_passphrase); /* Create the semaphore used by the ISR to indicate that a Rx frame is ready for processing. */ vSemaphoreCreateBinary( xZGIntrSemaphore ); }
uint8_t u8g_com_HAL_DUE_shared_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch(msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: u8g_SetPILevel_DUE_hw_spi(u8g, U8G_PI_CS, 1); u8g_SetPILevel_DUE_hw_spi(u8g, U8G_PI_A0, 1); u8g_SetPIOutput_DUE_hw_spi(u8g, U8G_PI_CS); u8g_SetPIOutput_DUE_hw_spi(u8g, U8G_PI_A0); u8g_Delay(5); spiBegin(); #ifndef SPI_SPEED #define SPI_SPEED SPI_FULL_SPEED // use same SPI speed as SD card #endif spiInit(2); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g_SetPILevel_DUE_hw_spi(u8g, U8G_PI_A0, arg_val); break; case U8G_COM_MSG_CHIP_SELECT: u8g_SetPILevel_DUE_hw_spi(u8g, U8G_PI_CS, (arg_val ? 0 : 1)); break; case U8G_COM_MSG_RESET: break; case U8G_COM_MSG_WRITE_BYTE: spiSend((uint8_t)arg_val); break; case U8G_COM_MSG_WRITE_SEQ: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { spiSend(*ptr++); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: { uint8_t *ptr = (uint8_t*) arg_ptr; while (arg_val > 0) { spiSend(*ptr++); arg_val--; } } break; } return 1; }
/* Initialize MMC interface */ static void initSpi(void) { spiBegin(); /* Enable SPI function */ pinMode(USD_CS_PIN, OUTPUT); csHigh(); // FIXME: DELAY of 10ms? }
static bool sdCmd4(uint8_t cmd, uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t arg3, bool crc, uint16_t timeout, SDRx *resp) { uint8_t cmdByte = 0b01000000 | (cmd & 0b00111111), r1Byte; #warning sd code needs to have shift reg parts removed #if 0 spiBegin(SREG_SS_SD, SPIMODE_SD, SPIENDIAN_SD, sdCtx.spiDiv); #endif /* command byte */ spiByte(cmdByte); /* arguments */ spiByte(arg3); spiByte(arg2); spiByte(arg1); spiByte(arg0); if (crc) { uint8_t acc = sdCRC7(0, cmdByte, 8); acc = sdCRC7(acc, arg3, 8); acc = sdCRC7(acc, arg2, 8); acc = sdCRC7(acc, arg1, 8); acc = sdCRC7(acc, arg0, 8); acc = sdCRC7(acc, 0, 7); uint8_t crcByte = (acc << 1) | 1; if (cmd == 0) assert(crcByte == 0x95); spiByte(crcByte); } else spiByte(1); /* wait for the MS bit to be zero */ uint16_t timeStart = msec; while ((r1Byte = spiByte(0xff)) & _BV(7)) { if (msec - timeStart >= timeout) { spiEnd(); return true; } } /* load responses based on the command type */ switch (cmd) { case SDCMD0: // SDCMD_GO_IDLE_STATE resp->r1.resp = r1Byte; break; case SDCMD8: // SDCMD_SEND_IF_CMD resp->r7.resp = r1Byte; resp->r7.data[0] = spiByte(0xff); resp->r7.data[1] = spiByte(0xff); resp->r7.data[2] = spiByte(0xff); resp->r7.data[3] = spiByte(0xff); break; case SDCMD58: // SDCMD_READ_OCR resp->r3.resp = r1Byte; resp->r3.ocr[0] = spiByte(0xff); resp->r3.ocr[1] = spiByte(0xff); resp->r3.ocr[2] = spiByte(0xff); resp->r3.ocr[3] = spiByte(0xff); break; } spiEnd(); return false; }
/** * 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 sckRateID, uint8_t chipSelectPin) { errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0; chipSelectPin_ = chipSelectPin; // 16-bit init start time allows over a minute uint16_t t0 = (uint16_t)millis(); uint32_t arg; pinMode(chipSelectPin_, OUTPUT); digitalWrite(chipSelectPin_, HIGH); #ifdef USE_TEENSY3_SPI spiBegin(); spiInit(6); #else // set pin modes pinMode(chipSelectPin_, OUTPUT); chipSelectHigh(); pinMode(SPI_MISO_PIN, INPUT); pinMode(SPI_MOSI_PIN, OUTPUT); pinMode(SPI_SCK_PIN, OUTPUT); // 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); #ifdef SPI_HAS_TRANSACTION settings = SPISettings(250000, MSBFIRST, SPI_MODE0); #endif #endif // not USE_TEENSY3_SPI // must supply min of 74 clock cycles with CS high. #ifdef SPI_HAS_TRANSACTION SPI.beginTransaction(settings); #endif for (uint8_t i = 0; i < 10; i++) spiSend(0XFF); #ifdef SPI_HAS_TRANSACTION SPI.endTransaction(); #endif chipSelectLow(); // 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); 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(); return setSckRate(sckRateID); fail: chipSelectHigh(); return false; }
//void main(int argc, char ** argv) { void TaskKermit(void *pvParameters) // Operate the Kermit Server { (void) pvParameters; int status, rx_len, i, x; char c; UCHAR *inbuf; short r_slot; parity = P_PARITY; /* Set this to desired parity */ status = X_OK; /* Initial kermit status */ xargc = argc; xargv = argv; xname = argv[0]; while (--xargc > 0) { /* Loop through command-line words */ xargv++; if (**xargv == '-') { /* Have dash */ c = *(*xargv+1); /* Get the option letter */ x = doarg(c); /* Go handle the option */ if (x < 0) doexit(FAILURE); } else { /* No dash where expected */ fatal("Malformed command-line option: '",*xargv,"'"); } } if (!action) /* Nothing to do, give usage message */ usage(); #ifdef DEBUG debug(DB_LOG,"SIMULATED ERROR RATE:",0,errorrate); if (errorrate) srand(seed); /* Init random error generator */ #endif /* DEBUG */ /* THE REAL STUFF IS FROM HERE DOWN */ xSerialPrintf_P(PSTR("\r\nFree Heap Size: %u"),xPortGetMinimumEverFreeHeapSize() ); // needs heap_1.c, heap_2.c or heap_4.c xSerialPrintf_P(PSTR("\r\nKermit HighWater: %u\r\n"), uxTaskGetStackHighWaterMark(NULL)); spiBegin(SDCard); // initialise the SPI bus for Kermit Server SD Card use. spiSelect (SDCard); if (!devopen("dummy")) /* Open the communication device */ doexit(FAILURE); if (!devsettings("dummy")) /* Perform any needed settings */ doexit(FAILURE); if (db) /* Open debug log if requested */ debug(DB_OPN,"debug.log",0,0); debug(DB_MSG,"Initializing...",0,0); /* Fill in parameters for this run */ k.xfermode = xmode; /* Text/binary automatic/manual */ k.remote = remote; /* Remote vs local */ k.binary = ftype; /* 0 = text, 1 = binary */ k.parity = parity; /* Communications parity */ k.bct = (check == 5) ? 3 : check; /* Block check type */ k.ikeep = keep; /* Keep incompletely received files */ k.filelist = cmlist; /* List of files to send (if any) */ k.cancel = 0; /* Not cancelled yet */ /* Fill in the i/o pointers */ k.zinbuf = i_buf; /* File input buffer */ k.zinlen = IBUFLEN; /* File input buffer length */ k.zincnt = 0; /* File input buffer position */ k.obuf = o_buf; /* File output buffer */ k.obuflen = OBUFLEN; /* File output buffer length */ k.obufpos = 0; /* File output buffer position */ /* Fill in function pointers */ k.rxd = readpkt; /* for reading packets */ k.txd = tx_data; /* for sending packets */ k.ixd = inchk; /* for checking connection */ k.openf = openfile; /* for opening files */ k.finfo = fileinfo; /* for getting file info */ k.readf = readfile; /* for reading files */ k.writef = writefile; /* for writing to output file */ k.closef = closefile; /* for closing files */ #ifdef DEBUG k.dbf = db ? dodebug : 0; /* for debugging */ #else k.dbf = 0; #endif /* DEBUG */ /* Force Type 3 Block Check (16-bit CRC) on all packets, or not */ k.bctf = (check == 5) ? 1 : 0; /* Initialize Kermit protocol */ status = kermit(K_INIT, &k, 0, 0, "", &r); #ifdef DEBUG debug(DB_LOG,"init status:",0,status); debug(DB_LOG,"version:",k.version,0); #endif /* DEBUG */ if (status == X_ERROR) doexit(FAILURE); if (action == A_SEND) status = kermit(K_SEND, &k, 0, 0, "", &r); /* Now we read a packet ourselves and call Kermit with it. Normally, Kermit would read its own packets, but in the embedded context, the device must be free to do other things while waiting for a packet to arrive. So the real control program might dispatch to other types of tasks, of which Kermit is only one. But in order to read a packet into Kermit's internal buffer, we have to ask for a buffer address and slot number. To interrupt a transfer in progress, set k.cancel to I_FILE to interrupt only the current file, or to I_GROUP to cancel the current file and all remaining files. To cancel the whole operation in such a way that the both Kermits return an error status, call Kermit with K_ERROR. */ while (status != X_DONE) { /* Here we block waiting for a packet to come in (unless readpkt times out). Another possibility would be to call inchk() to see if any bytes are waiting to be read, and if not, go do something else for a while, then come back here and check again. */ inbuf = getrslot(&k,&r_slot); /* Allocate a window slot */ rx_len = k.rxd(&k,inbuf,P_PKTLEN); /* Try to read a packet */ debug(DB_PKT,"main packet",&(k.ipktbuf[0][r_slot]),rx_len); /* For simplicity, kermit() ACKs the packet immediately after verifying it was received correctly. If, afterwards, the control program fails to handle the data correctly (e.g. can't open file, can't write data, can't close file), then it tells Kermit to send an Error packet next time through the loop. */ if (rx_len < 1) { /* No data was read */ freerslot(&k,r_slot); /* So free the window slot */ if (rx_len < 0) /* If there was a fatal error */ doexit(FAILURE); /* give up */ /* This would be another place to dispatch to another task */ /* while waiting for a Kermit packet to show up. */ } /* Handle the input */ switch (status = kermit(K_RUN, &k, r_slot, rx_len, "", &r)) { case X_OK: #ifdef DEBUG /* This shows how, after each packet, you get the protocol state, file name, date, size, and bytes transferred so far. These can be used in a file-transfer progress display, log, etc. */ debug(DB_LOG,"NAME",r.filename ? (char *)r.filename : "(NULL)",0); debug(DB_LOG,"DATE",r.filedate ? (char *)r.filedate : "(NULL)",0); debug(DB_LOG,"SIZE",0,r.filesize); debug(DB_LOG,"STATE",0,r.status); debug(DB_LOG,"SOFAR",0,r.sofar); #endif /* DEBUG */ /* Maybe do other brief tasks here... */ continue; /* Keep looping */ case X_DONE: break; /* Finished */ case X_ERROR: doexit(FAILURE); /* Failed */ break; } } doexit(SUCCESS); }