long SpiFirstWrite(unsigned char *ucBuf, unsigned short usLength) { // workaround for first transaction ASSERT_CS(); // 50 microsecond delay jshDelayMicroseconds(50); // SPI writes first 4 bytes of data SpiWriteDataSynchronous(ucBuf, 4); jshDelayMicroseconds(50); SpiWriteDataSynchronous(ucBuf + 4, usLength - 4); // From this point on - operate in a regular way sSpiInformation.ulSpiState = eSPI_STATE_IDLE; DEASSERT_CS(); jshDelayMicroseconds(10000); return(0); }
/** Reset one-wire, return true if a device was present */ static bool NO_INLINE OneWireReset(Pin pin) { jshPinSetState(pin, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP); //jshInterruptOff(); jshPinSetValue(pin, 0); jshDelayMicroseconds(500); jshPinSetValue(pin, 1); jshDelayMicroseconds(80); bool hasDevice = !jshPinGetValue(pin); //jshInterruptOn(); jshDelayMicroseconds(420); return hasDevice; }
/** Write 'bits' bits, and return what was read (to read, you must send all 1s) */ static JsVarInt NO_INLINE OneWireRead(Pin pin, int bits) { jshPinSetState(pin, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP); JsVarInt result = 0; JsVarInt mask = 1; while (bits-- > 0) { jshInterruptOff(); jshPinSetValue(pin, 0); jshDelayMicroseconds(3); jshPinSetValue(pin, 1); jshDelayMicroseconds(10); // leave time to let it rise if (jshPinGetValue(pin)) result = result | mask; jshInterruptOn(); jshDelayMicroseconds(53); mask = mask << 1; } return result; }
/** * \brief Set the value of the pin to be the value supplied and then wait for * a given period and set the pin value again to be the opposite. */ void jshPinPulse(Pin pin, //!< The pin to be pulsed. bool value, //!< The value to be pulsed into the pin. JsVarFloat time //!< The period in milliseconds to hold the pin. ) { if (jshIsPinValid(pin)) { jshPinSetState(pin, JSHPINSTATE_GPIO_OUT); jshPinSetValue(pin, value); jshDelayMicroseconds(jshGetTimeFromMilliseconds(time)); jshPinSetValue(pin, !value); } else jsError("Invalid pin!"); } // End of jshPinPulse
void SpiReadDataSynchronous(unsigned char *data, unsigned short size) { int bSend = 0, bRecv = 0; while (bSend<size || bRecv<size) { int r = jshSPISend(WLAN_SPI, (bSend<size)?READ:-1); bSend++; if (bSend>0 && r>=0) data[bRecv++] = r; } jshDelayMicroseconds(10); // because of final clock pulse }
/** Write 'bits' bits, and return what was read (to read, you must send all 1s) */ static void NO_INLINE OneWireWrite(Pin pin, int bits, unsigned long long data) { jshPinSetState(pin, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP); unsigned long long mask = 1; while (bits-- > 0) { if (data & mask) { // short pulse jshInterruptOff(); jshPinSetValue(pin, 0); jshDelayMicroseconds(10); jshPinSetValue(pin, 1); jshInterruptOn(); jshDelayMicroseconds(55); } else { // long pulse jshInterruptOff(); jshPinSetValue(pin, 0); jshDelayMicroseconds(65); jshPinSetValue(pin, 1); jshInterruptOn(); jshDelayMicroseconds(5); } mask = mask << 1; } }
void SpiInit(void) { // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = WLAN_CLK_PIN; inf.pinMISO = WLAN_MISO_PIN; inf.pinMOSI = WLAN_MOSI_PIN; inf.baudRate = 100000; // FIXME - just slow for debug inf.spiMode = SPIF_SPI_MODE_1; // Mode 1 CPOL= 0 CPHA= 1 jshSPISetup(WLAN_SPI, &inf); // WLAN CS, EN and WALN IRQ Configuration jshSetPinStateIsManual(WLAN_CS_PIN, false); jshPinOutput(WLAN_CS_PIN, 1); // de-assert CS jshSetPinStateIsManual(WLAN_EN_PIN, false); jshPinOutput(WLAN_EN_PIN, 0); // disable WLAN jshSetPinStateIsManual(WLAN_IRQ_PIN, true); jshPinSetState(WLAN_IRQ_PIN, JSHPINSTATE_GPIO_IN_PULLUP); // flip into read mode with pullup // wait a little (ensure that WLAN takes effect) jshDelayMicroseconds(500*1000); // force a 500ms delay! FIXME }
void delay(int msec){ int boot = 0; for ( ;boot<msec; boot++) { jshDelayMicroseconds(1000); } }