bool jsfsInit() { #ifndef LINUX if (!fat_initialised) { #ifdef SD_CARD_ANYWHERE if (!isSdSPISetup()) { #ifdef SD_SPI const char *deviceStr = jshGetDeviceString(SD_SPI); JsVar *spi = jsvSkipNameAndUnLock(jspGetNamedVariable(deviceStr)); JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinMISO = SD_DO_PIN; inf.pinMOSI = SD_DI_PIN; inf.pinSCK = SD_CLK_PIN; jshSPISetup(SD_SPI, &inf); sdSPISetup(spi, SD_CS_PIN); jsvUnLock(spi); #else jsError("SD card must be setup with E.connectSDCard first"); return false; #endif } #endif FRESULT res; if ((res = f_mount(&jsfsFAT, "", 1/*immediate*/)) != FR_OK) { jsfsReportError("Unable to mount SD card", res); return false; } fat_initialised = true; } #endif return true; }
/*JSON{ "type" : "method", "class" : "SPI", "name" : "setup", "generate" : "jswrap_spi_setup", "params" : [ ["options","JsVar",["An optional structure containing extra information on initialising the SPI port","Please note that baud rate is set to the nearest that can be managed - which may be -+ 50%","```{sck:pin, miso:pin, mosi:pin, baud:integer=100000, mode:integer=0, order:'msb'/'lsb'='msb' }```","If sck,miso and mosi are left out, they will automatically be chosen. However if one or more is specified then the unspecified pins will not be set up.","You can find out which pins to use by looking at [your board's reference page](#boards) and searching for pins with the `SPI` marker.","The SPI ```mode``` is between 0 and 3 - see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Clock_polarity_and_phase","On STM32F1-based parts, you cannot mix AF and non-AF pins (SPI pins are usually grouped on the chip - and you can't mix pins from two groups). Espruino will not warn you about this."]] ] } Set up this SPI port as an SPI Master. */ void jswrap_spi_setup(JsVar *parent, JsVar *options) { IOEventFlags device = jsiGetDeviceFromClass(parent); JshSPIInfo inf; jsspiPopulateSPIInfo(&inf, options); if (DEVICE_IS_SPI(device)) { jshSPISetup(device, &inf); #ifdef LINUX if (jsvIsObject(options)) { jsvUnLock(jsvObjectSetChild(parent, "path", jsvObjectGetChild(options, "path", 0))); } #endif } else if (device == EV_NONE) { // software mode - at least configure pins properly if (inf.pinSCK != PIN_UNDEFINED) jshPinSetState(inf.pinSCK, JSHPINSTATE_GPIO_OUT); if (inf.pinMISO != PIN_UNDEFINED) jshPinSetState(inf.pinMISO, JSHPINSTATE_GPIO_IN); if (inf.pinMOSI != PIN_UNDEFINED) jshPinSetState(inf.pinMOSI, JSHPINSTATE_GPIO_OUT); } else return; // Set up options, so we can initialise it on startup if (options) jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME)); else jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME); }
/*JSON{ "type" : "method", "class" : "SPI", "name" : "send4bit", "generate" : "jswrap_spi_send4bit", "params" : [ ["data","JsVar","The data to send - either an integer, array, or string"], ["bit0","int32","The 4 bits to send for a 0 (MSB first)"], ["bit1","int32","The 4 bits to send for a 1 (MSB first)"], ["nss_pin","pin","An nSS pin - this will be lowered before SPI output and raised afterwards (optional). There will be a small delay between when this is lowered and when sending starts, and also between sending finishing and it being raised."] ] } Send data down SPI, using 4 bits for each 'real' bit (MSB first). This can be useful for faking one-wire style protocols Sending multiple bytes in one call to send is preferable as they can then be transmitted end to end. Using multiple calls to send() will result in significantly slower transmission speeds. */ void jswrap_spi_send4bit(JsVar *parent, JsVar *srcdata, int bit0, int bit1, Pin nss_pin) { NOT_USED(parent); IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_SPI(device)) { jsExceptionHere(JSET_ERROR, "SPI.send4bit only works on hardware SPI"); return; } jshSPISet16(device, true); // 16 bit output if (bit0==0 && bit1==0) { bit0 = 0x01; bit1 = 0x03; } bit0 = bit0 & 0x0F; bit1 = bit1 & 0x0F; if (!jshIsDeviceInitialised(device)) { JshSPIInfo inf; jshSPIInitInfo(&inf); jshSPISetup(device, &inf); } // we're just sending (no receive) jshSPISetReceive(device, false); // assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, false); // send data if (jsvIsNumeric(srcdata)) { jsspiSend4bit(device, (unsigned char)jsvGetInteger(srcdata), bit0, bit1); } else if (jsvIsIterable(srcdata)) { jshInterruptOff(); JsvIterator it; jsvIteratorNew(&it, srcdata); while (jsvIteratorHasElement(&it)) { unsigned char in = (unsigned char)jsvIteratorGetIntegerValue(&it); jsspiSend4bit(device, in, bit0, bit1); jsvIteratorNext(&it); } jsvIteratorFree(&it); jshInterruptOn(); } else { jsExceptionHere(JSET_ERROR, "Variable type %t not suited to transmit operation", srcdata); } jshSPIWait(device); // wait until SPI send finished and clear the RX buffer // de-assert NSS if (nss_pin!=PIN_UNDEFINED) jshPinOutput(nss_pin, true); jshSPISet16(device, false); // back to 8 bit }
/*JSON{ "type":"staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "description" : "Initialise the WIZnet module and return an Ethernet object", "params" : [ ], "return" : ["JsVar", "An Ethernet Object"], "return_object":"Ethernet" }*/ JsVar *jswrap_wiznet_connect() { JsVar *ethObj = jspNewObject(0, "Ethernet"); // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); // CS Configuration jshSetPinStateIsManual(ETH_CS_PIN, false); jshPinOutput(ETH_CS_PIN, 1); // de-assert CS // Wiznet reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZCHIP Initialized fail.\r\n"); return 0; } /* PHY link status check */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); return 0; } } while (tmp == PHY_LINK_OFF); JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }
void dma_setup(int bitRate) { // init SPI JshSPIInfo inf; jshSPIInitInfo(&inf); inf.baudRate = bitRate; inf.baudRateSpec = SPIB_MINIMUM; // we don't want SPI to be any slower than this inf.spiMSB = false; inf.pinMOSI = tvPinVideo; jshPinSetValue(tvPinVideo, 0); // set default video output state jshSPISetup(TVSPIDEVICE, &inf); // disable IRQs - because jsHardware enabled them SPI_I2S_ITConfig(TVSPI, SPI_I2S_IT_RXNE, DISABLE); // init DMA #ifdef STM32F4 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_TVDMA, ENABLE); #else RCC_AHBPeriphClockCmd(RCC_AHBPeriph_TVDMA, ENABLE); #endif DMA_InitTypeDef DMA_InitStructure; DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(TVSPI->DR)); DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; // DMA_PeripheralDataSize_HalfWord and 16 bit? DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; #ifdef STM32F4 DMA_InitStructure.DMA_Channel = DMA_Channel_TVSPI_TX; // needed for SPI TX DMA_InitStructure.DMA_Memory0BaseAddr = (u32)tvPixelPtr; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_MemoryBurst =DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst =DMA_PeripheralBurst_Single; #else DMA_InitStructure.DMA_MemoryBaseAddr = (u32)tvPixelPtr; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_High; #endif DMA_InitStructure.DMA_BufferSize = tvWidth>>3/*bytes*/; DMA_DeInit(DMA_TVSPI_TX); DMA_Init(DMA_TVSPI_TX, &DMA_InitStructure); SPI_I2S_DMACmd(TVSPI, SPI_I2S_DMAReq_Tx, ENABLE); }
/*JSON{ "type" : "staticmethod", "class" : "CC3000", "name" : "connect", "generate" : "jswrap_cc3000_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default). SPI should be 1,000,000 baud, and set to 'mode 1'"], ["cs", "pin", "The pin to use for Chip Select"], ["en", "pin", "The pin to use for Enable"], ["irq", "pin", "The pin to use for Interrupts"] ], "return" : ["JsVar","A WLAN Object"], "return_object" : "WLAN" } Initialise the CC3000 and return a WLAN object */ JsVar *jswrap_cc3000_connect(JsVar *spi, Pin cs, Pin en, Pin irq) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = WLAN_CLK_PIN; inf.pinMISO = WLAN_MISO_PIN; inf.pinMOSI = WLAN_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_1; // Mode 1 CPOL= 0 CPHA= 1 jshSPISetup(WLAN_SPI, &inf); spiDevice = WLAN_SPI; } if (!jshIsPinValid(cs)) cs = WLAN_CS_PIN; if (!jshIsPinValid(en)) en = WLAN_EN_PIN; if (!jshIsPinValid(irq)) irq = WLAN_IRQ_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_CC3000); net.data.device = spiDevice; net.data.pinCS = cs; net.data.pinEN = en; net.data.pinIRQ = irq; networkSet(&net); JsVar *wlanObj = jspNewObject(0, "WLAN"); cc3000_initialise(wlanObj); networkFree(&net); return wlanObj; }
/*JSON{ "type" : "method", "class" : "SPI", "name" : "setup", "generate" : "jswrap_spi_setup", "params" : [ ["options","JsVar",["An optional structure containing extra information on initialising the SPI port","Please note that baud rate is set to the nearest that can be managed - which may be -+ 50%","```{sck:pin, miso:pin, mosi:pin, baud:integer=100000, mode:integer=0, order:'msb'/'lsb'='msb' }```","If sck,miso and mosi are left out, they will automatically be chosen. However if one or more is specified then the unspecified pins will not be set up.","You can find out which pins to use by looking at [your board's reference page](#boards) and searching for pins with the `SPI` marker.","The SPI ```mode``` is between 0 and 3 - see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Clock_polarity_and_phase","On STM32F1-based parts, you cannot mix AF and non-AF pins (SPI pins are usually grouped on the chip - and you can't mix pins from two groups). Espruino will not warn you about this."]] ] } Set up this SPI port as an SPI Master. */ void jswrap_spi_setup( JsVar *parent, //!< The variable that is the class instance of this function. JsVar *options //!< The options controlling SPI. ) { // // Design: The options variable is a JS Object which contains a series of settings. These // settings are parsed by `jsspiPopulateSPIInfo` to populate a C structure of type // `JshSPIInfo`. // // The options are also hung off the class instance variable in a property symbolically called // DEVICE_OPTIONS_NAME ("_options"). // IOEventFlags device = jsiGetDeviceFromClass(parent); JshSPIInfo inf; // Debug // jsiConsolePrintf("jswrap_spi_setup called parent=%v, options=%v\n", parent, options); jsspiPopulateSPIInfo(&inf, options); if (DEVICE_IS_SPI(device)) { jshSPISetup(device, &inf); #ifdef LINUX if (jsvIsObject(options)) { jsvObjectSetChildAndUnLock(parent, "path", jsvObjectGetChild(options, "path", 0)); } #endif } else if (device == EV_NONE) { // software mode - at least configure pins properly if (inf.pinSCK != PIN_UNDEFINED) jshPinSetState(inf.pinSCK, JSHPINSTATE_GPIO_OUT); if (inf.pinMISO != PIN_UNDEFINED) jshPinSetState(inf.pinMISO, JSHPINSTATE_GPIO_IN); if (inf.pinMOSI != PIN_UNDEFINED) jshPinSetState(inf.pinMOSI, JSHPINSTATE_GPIO_OUT); } else return; // Set up options, so we can initialise it on startup if (options) jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME)); else jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME); }
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 }
bool jsfsInit() { #ifndef LINUX if (!fat_initialised) { #ifndef USE_FLASHFS #ifdef SD_CARD_ANYWHERE if (!isSdSPISetup()) { #ifdef SD_SPI const char *deviceStr = jshGetDeviceString(SD_SPI); JsVar *spi = jsvSkipNameAndUnLock(jspGetNamedVariable(deviceStr)); JshSPIInfo inf; jshSPIInitInfo(&inf); inf.baudRate = 4000000; // 4Mhz bit rate for onboard SD cards inf.pinMISO = SD_DO_PIN; inf.pinMOSI = SD_DI_PIN; inf.pinSCK = SD_CLK_PIN; jshSPISetup(SD_SPI, &inf); sdSPISetup(spi, SD_CS_PIN); jsvUnLock(spi); #else jsExceptionHere(JSET_ERROR,"SD card must be setup with E.connectSDCard first"); return false; #endif // SD_SPI } #endif // SD_CARD_ANYWHER #endif // USE_FLASHFS FRESULT res; if ((res = f_mount(&jsfsFAT, "", 1)) != FR_OK) { jsfsReportError("Unable to mount media", res); return false; } fat_initialised = true; } #endif // LINUX return true; }
/*JSON{ "type" : "staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default)"], ["cs", "pin", "The pin to use for Chip Select"] ], "return" : ["JsVar","An Ethernet Object"], "return_object" : "Ethernet" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_wiznet_connect(JsVar *spi, Pin cs) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); spiDevice = ETH_SPI; } if (!jshIsPinValid(cs)) cs = ETH_CS_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); net.data.device = spiDevice; net.data.pinCS = cs; networkSet(&net); JsVar *ethObj = jspNewObject(0, "Ethernet"); // CS Configuration jshSetPinStateIsManual(net.data.pinCS, false); jshPinOutput(net.data.pinCS, 1); // de-assert CS // Initialise WIZnet functions reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZnet Initialize failed.\r\n"); networkFree(&net); return 0; } #if _WIZCHIP_ == 5500 /* PHY link status check - W5100 doesn't have this */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); networkFree(&net); return 0; } } while (tmp == PHY_LINK_OFF); #endif networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }