void jshPinPulse(Pin pin, bool value, JsVarFloat time) { if (jshIsPinValid(pin)) { jshPinSetState(pin, JSHPINSTATE_GPIO_OUT); jshPinSetValue(pin, value); usleep(time*1000000); jshPinSetValue(pin, !value); } else jsError("Invalid pin!"); }
void LCD_init_hardware() { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); /* Enable the FSMC Clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE); /* Enable the FSMC pins for LCD control */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15 | GPIO_Pin_7 /*NE1*/ | GPIO_Pin_11/*RS*/; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOE, &GPIO_InitStructure); FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; FSMC_NORSRAMTimingInitTypeDef p; p.FSMC_AddressSetupTime = 0x02; p.FSMC_AddressHoldTime = 0x00; p.FSMC_DataSetupTime = 0x05; p.FSMC_BusTurnAroundDuration = 0x00; p.FSMC_CLKDivision = 0x00; p.FSMC_DataLatency = 0x00; p.FSMC_AccessMode = FSMC_AccessMode_B; FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR; FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); /* Enable FSMC Bank1_SRAM Bank */ FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); // Toggle LCD reset pin #ifdef LCD_RESET jshPinSetState(LCD_RESET, JSHPINSTATE_GPIO_OUT); jshPinSetValue(LCD_RESET, 0); //RESET=0 LCD_DELAY(DELAY_LONG); jshPinSetValue(LCD_RESET, 1); //RESET=1 #endif }
static void i2c_wr_bit(i2cInfo *inf, bool b) { jshPinSetValue(inf->pinSDA, b); dly(inf); jshPinSetValue(inf->pinSCL, 1); dly(inf); int timeout = I2C_TIMEOUT; while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch if (!timeout) err("Timeout (wr)"); jshPinSetValue(inf->pinSCL, 0); jshPinSetValue(inf->pinSDA, 1); // stop forcing SDA (needed?) }
/** * \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
/** 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; }
static bool i2c_rd_bit(i2cInfo *inf) { jshPinSetValue(inf->pinSDA, 1); // stop forcing SDA dly(inf); jshPinSetValue(inf->pinSCL, 1); // stop forcing SDA int timeout = I2C_TIMEOUT; while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch if (!timeout) err("Timeout (rd)"); dly(inf); bool b = jshPinGetValue(inf->pinSDA); jshPinSetValue(inf->pinSCL, 0); return b; }
static void i2c_stop(i2cInfo *inf) { jshPinSetValue(inf->pinSDA, 0); dly(inf); jshPinSetValue(inf->pinSCL, 1); int timeout = I2C_TIMEOUT; while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch if (!timeout) err("Timeout (stop)"); dly(inf); jshPinSetValue(inf->pinSDA, 1); dly(inf); if (!jshPinGetValue(inf->pinSDA)) err("Arbitration (stop)"); dly(inf); inf->started = false; }
void jshPinOutput(Pin pin, bool value) { if (jshIsPinValid(pin)) { if (!jshGetPinStateIsManual(pin)) jshPinSetState(pin, JSHPINSTATE_GPIO_OUT); jshPinSetValue(pin, value); } else jsExceptionHere(JSET_ERROR, "Invalid pin!"); }
static void i2c_start(i2cInfo *inf) { if (inf->started) { // reset jshPinSetValue(inf->pinSDA, 1); dly(inf); jshPinSetValue(inf->pinSCL, 1); int timeout = I2C_TIMEOUT; while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch if (!timeout) err("Timeout (start)"); dly(inf); } if (!jshPinGetValue(inf->pinSDA)) err("Arbitration (start)"); jshPinSetValue(inf->pinSDA, 0); dly(inf); jshPinSetValue(inf->pinSCL, 0); dly(inf); inf->started = true; }
void jswrap_onewire_write(JsVar *parent, JsVar *data, bool leavePowerOn) { Pin pin = onewire_getpin(parent); if (!jshIsPinValid(pin)) return; jsvIterateCallback(data, (void (*)(int, void *))_jswrap_onewire_write_cb, (void*)&pin); if (leavePowerOn) { // We're asked to leave power on for parasitically powered devices, to do that properly we // need to actively pull the line high. This is required, for example, for parasitically // powered DS18B20 temperature sensors. jshPinSetValue(pin, 1); jshPinSetState(pin, JSHPINSTATE_GPIO_OUT); } else { // We don't need to leave power on, so just tri-state the pin jshPinSetState(pin, JSHPINSTATE_GPIO_IN); jshPinSetValue(pin, 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; }
/** 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 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); }
void jshPinOutput(Pin pin, bool value) { if (jshIsPinValid(pin)) { jshPinSetState(pin, JSHPINSTATE_GPIO_OUT); jshPinSetValue(pin, value); } else jsError("Invalid pin!"); }
static ALWAYS_INLINE void sync_end() { jshPinSetValue(tvPinSync, 1); }
void jshPinOutput(Pin pin, bool value) { if (jshIsPinValid(pin)) jshPinSetValue(pin, value); }
static ALWAYS_INLINE void sync_start() { jshPinSetValue(tvPinSync, 0); }