/*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 }
/** 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; } }
/** 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; }
/** Kick a device into action (if required). For instance we may need to set up interrupts */ void jshUSARTKick(IOEventFlags device) { if (device == EV_BLUETOOTH) { /* For bluetooth, start transmit after one character. The BLE_EVT_TX_COMPLETE event will get triggered and will auto-reload whatever needs sending. */ bool jswrap_nrf_transmit_string(); jswrap_nrf_transmit_string(); } if (device == EV_SERIAL1 && !uartIsSending) { jshInterruptOff(); int ch = jshGetCharToTransmit(EV_SERIAL1); if (ch >= 0) { // put data - this will kick off the USART while (app_uart_put((uint8_t)ch) != NRF_SUCCESS); uartIsSending = true; } jshInterruptOn(); } }
void TVTIMER_IRQHandler() { jshInterruptOff(); TIM_ClearITPendingBit(TVTIMER, TIM_IT_Update); tvTimerFunc(); jshInterruptOn(); }