// Perform the onewire reset function. We will wait up to 250uS for // the bus to come high, if it doesn't then it is broken or shorted // and we return a 0; // Returns 1 if a device asserted a presence pulse, 0 otherwise. uint32 ICACHE_FLASH_ATTR onewire_reset() { uint32 result; uint8 retries = 125; // Disable output on the pin. GPIO_DIS_OUTPUT(ONEWIRE_PIN); // Wait for the bus to get high (which it should because of the pull-up resistor). do { if (--retries == 0) { return 0; } os_delay_us(2); } while (!GPIO_INPUT_GET(ONEWIRE_PIN)); // Transmit the reset pulse by pulling the bus low for at least 480 us. GPIO_OUTPUT_SET(ONEWIRE_PIN, 0); os_delay_us(500); // Release the bus, and wait, then check for a presence pulse, which should start 15-60 us after the reset, and will last 60-240 us. // So 65us after the reset the bus must be high. GPIO_DIS_OUTPUT(ONEWIRE_PIN); os_delay_us(65); result = !GPIO_INPUT_GET(ONEWIRE_PIN); // After sending the reset pulse, the master (we) must wait at least another 480 us. os_delay_us(490); return result; }
void ICACHE_FLASH_ATTR gpio_init() { // Configure switch (relays) INFO("Configure Switch 1 %d\n", SWITCH01_GPIO ); PIN_FUNC_SELECT(SWITCH01_GPIO_MUX, SWITCH01_GPIO_FUNC); set_switch(SWITCH01_GPIO, 0); //GPIO_OUTPUT_SET(SWITCH01_GPIO, 0); INFO("Configure Switch 2 %d\n", SWITCH02_GPIO ); PIN_FUNC_SELECT(SWITCH02_GPIO_MUX, SWITCH02_GPIO_FUNC); set_switch(SWITCH02_GPIO, 0); //GPIO_OUTPUT_SET(SWITCH02_GPIO, 0); INFO("Configure Switch 3 %d\n", SWITCH03_GPIO ); PIN_FUNC_SELECT(SWITCH03_GPIO_MUX, SWITCH03_GPIO_FUNC); set_switch(SWITCH03_GPIO, 0); //GPIO_OUTPUT_SET(SWITCH03_GPIO, 0); //Configure Toggle switches INFO("Configure Toggle 1 %d\n", TOGGLE01_GPIO ); ETS_GPIO_INTR_DISABLE(); // Disable gpio interrupts ETS_GPIO_INTR_ATTACH(toggle_changed, 0); // GPIO interrupt handler PIN_FUNC_SELECT(TOGGLE01_GPIO_MUX, TOGGLE01_GPIO_FUNC); // Set function GPIO_DIS_OUTPUT(TOGGLE01_GPIO); // Set as input gpio_pin_intr_state_set(GPIO_ID_PIN(TOGGLE01_GPIO), GPIO_PIN_INTR_ANYEDGE); // Interrupt on any edge //ETS_GPIO_INTR_ENABLE(); // Enable gpio interrupts INFO("Configure Toggle 2 %d\n", TOGGLE02_GPIO ); //ETS_GPIO_INTR_DISABLE(); // Disable gpio interrupts //ETS_GPIO_INTR_ATTACH(toggle_changed); // GPIO interrupt handler PIN_FUNC_SELECT(TOGGLE02_GPIO_MUX, TOGGLE02_GPIO_FUNC); // Set function GPIO_DIS_OUTPUT(TOGGLE02_GPIO); // Set as input gpio_pin_intr_state_set(GPIO_ID_PIN(TOGGLE02_GPIO), GPIO_PIN_INTR_ANYEDGE); // Interrupt on any edge //ETS_GPIO_INTR_ENABLE(); // Enable gpio interrupts INFO("Configure Toggle 3 %d\n", TOGGLE03_GPIO ); //ETS_GPIO_INTR_DISABLE(); // Disable gpio interrupts //ETS_GPIO_INTR_ATTACH(toggle_changed); // GPIO interrupt handler PIN_FUNC_SELECT(TOGGLE03_GPIO_MUX, TOGGLE03_GPIO_FUNC); // Set function GPIO_DIS_OUTPUT(TOGGLE03_GPIO); // Set as input gpio_pin_intr_state_set(GPIO_ID_PIN(TOGGLE03_GPIO), GPIO_PIN_INTR_ANYEDGE); // Interrupt on any edge ETS_GPIO_INTR_ENABLE(); // Enable gpio interrupts // Configure push button // INFO("Confgiure push button %d\n", BUTTON_GPIO ); // ETS_GPIO_INTR_DISABLE(); // Disable gpio interrupts // ETS_GPIO_INTR_ATTACH(button_press, BUTTON_GPIO); // GPIO0 interrupt handler // PIN_FUNC_SELECT(BUTTON_GPIO_MUX, BUTTON_GPIO_FUNC); // Set function // GPIO_DIS_OUTPUT(BUTTON_GPIO); // Set as input // gpio_pin_intr_state_set(GPIO_ID_PIN(BUTTON_GPIO), 2); // Interrupt on negative edge // ETS_GPIO_INTR_ENABLE(); // Enable gpio interrupts }
// // Write a bit. Port and bit is used to cut lookup time and provide // more certain timing. // void ICACHE_FLASH_ATTR onewire_write_bit(uint32 v) { GPIO_OUTPUT_SET(ONEWIRE_PIN, 0); if (v) { os_delay_us(10); GPIO_DIS_OUTPUT(ONEWIRE_PIN); // Drive output high using the pull-up os_delay_us(55); } else { os_delay_us(65); GPIO_DIS_OUTPUT(ONEWIRE_PIN); // Drive output high using the pull-up os_delay_us(5); } }
void supla_ds18b20_reset(void) { uint8_t retries = 125; GPIO_DIS_OUTPUT( supla_w1_pin ); do { if (--retries == 0) return; os_delay_us(2); } while ( !GPIO_INPUT_GET( supla_w1_pin )); GPIO_OUTPUT_SET( supla_w1_pin, 0 ); os_delay_us(480); GPIO_DIS_OUTPUT( supla_w1_pin ); os_delay_us(480); }
/** * called first at OS init */ void ICACHE_FLASH_ATTR valveDriverInit() { // configure GPIO outputs PIN_FUNC_SELECT(GENERATOR_GPIO_MUX, GENERATOR_GPIO_FUNC); PIN_FUNC_SELECT(OPEN_VALVE_GPIO_MUX, OPEN_VALVE_GPIO_FUNC); PIN_FUNC_SELECT(CLOSE_VALVE_GPIO_MUX, CLOSE_VALVE_GPIO_FUNC); PIN_FUNC_SELECT(CAPACITOR_GPIO_MUX, CAPACITOR_GPIO_FUNC); // configure passive output state GPIO_OUTPUT_SET(GENERATOR_GPIO, 0); GPIO_DIS_OUTPUT(OPEN_VALVE_GPIO); GPIO_OUTPUT_SET(CLOSE_VALVE_GPIO, 0); GPIO_DIS_OUTPUT(CAPACITOR_GPIO); }
// initialization void IRrecv::enableIRIn() { // initialize state machine variables irparams.rcvstate = STATE_IDLE; irparams.rawlen = 0; // set pin modes //PIN_FUNC_SELECT(IR_IN_MUX, IR_IN_FUNC); GPIO_DIS_OUTPUT(irparams.recvpin); // Initialize timer os_timer_disarm(&timer); os_timer_setfn(&timer, (os_timer_func_t *)read_timeout, &timer); // ESP Attach Interrupt ETS_GPIO_INTR_DISABLE(); ETS_GPIO_INTR_ATTACH(gpio_intr, NULL); gpio_pin_intr_state_set(GPIO_ID_PIN(irparams.recvpin), GPIO_PIN_INTR_ANYEDGE); ETS_GPIO_INTR_ENABLE(); //ETS_INTR_UNLOCK(); //attachInterrupt(irparams.recvpin, readIR, CHANGE); //irReadTimer.initializeUs(USECPERTICK, readIR).start(); //os_timer_arm_us(&irReadTimer, USECPERTICK, 1); //ets_timer_arm_new(&irReadTimer, USECPERTICK, 1, 0); }
/** * Sets the 'gpio_pin' pin as a GPIO and sets the pull-up and * pull-down registers for that pin. * 'pullStatus' has no effect on output pins or GPIO16 */ bool ICACHE_FLASH_ATTR easygpio_pinMode(uint8_t gpio_pin, EasyGPIO_PullStatus pullStatus, EasyGPIO_PinMode pinMode) { uint32_t gpio_name; uint8_t gpio_func; if (16==gpio_pin) { // ignoring pull status on GPIO16 for now if (EASYGPIO_OUTPUT == pinMode) { gpio16_output_conf(); } else { gpio16_input_conf(); } return true; } else if (!easygpio_getGPIONameFunc(gpio_pin, &gpio_name, &gpio_func) ) { return false; } PIN_FUNC_SELECT(gpio_name, gpio_func); easygpio_setupPullsByName(gpio_name, pullStatus); if (EASYGPIO_OUTPUT != pinMode) { GPIO_DIS_OUTPUT(GPIO_ID_PIN(gpio_pin)); } else { // must enable the pin or else the WRITE_PERI_REG won't work gpio_output_set(0, 0, BIT(GPIO_ID_PIN(gpio_pin)),0); } return true; }
/** * open valve */ LOCAL void ICACHE_FLASH_ATTR valveOpen(SleeperStateT* sleeperState) { // discharge capacitor as much as possible while powering up generator (this may also close valve if still open) GPIO_OUTPUT_SET(CLOSE_VALVE_GPIO, 1); // start generator GPIO_OUTPUT_SET(GENERATOR_GPIO, 1); os_delay_us(50000); // 50 ms -> us // stop discharging capacitor GPIO_OUTPUT_SET(CLOSE_VALVE_GPIO, 0); os_delay_us(20); // 20 us // open latching valve by charing capacitor GPIO_OUTPUT_SET(OPEN_VALVE_GPIO, 0); os_delay_us(VALVE_OPEN_PULSE_DURATION); // (250 ms) -> us // disable power to valve and disable generator GPIO_DIS_OUTPUT(OPEN_VALVE_GPIO); GPIO_OUTPUT_SET(GENERATOR_GPIO, 0); // update state if (!sleeperState->rtcMem.valveOpen) { sleeperState->rtcMem.valveOpen = true; sleeperState->rtcMem.totalOpenCount++; } sleeperState->rtcMem.valveOpenTime = now; ets_uart_printf("valveOpen\r\n"); }
/** * close valve */ LOCAL void ICACHE_FLASH_ATTR valveClose(SleeperStateT* sleeperState) { // start generator GPIO_OUTPUT_SET(GENERATOR_GPIO, 1); os_delay_us(1000); // 1 ms -> us // recharge capacitor while bypassing valve GPIO_OUTPUT_SET(CAPACITOR_GPIO, 0); os_delay_us(50000); // 50 ms -> us // stop capacitor charging and disable generator GPIO_DIS_OUTPUT(CAPACITOR_GPIO); os_delay_us(20); // 20 us GPIO_OUTPUT_SET(GENERATOR_GPIO, 0); // close latching valve by discharging capacitor GPIO_OUTPUT_SET(CLOSE_VALVE_GPIO, 1); os_delay_us(62500); // 62.5 ms -> us // continue discharging capacitor until os shutdown // update state sleeperState->rtcMem.valveOpen = false; if (now > sleeperState->rtcMem.valveOpenTime) { sleeperState->rtcMem.totalOpenDuration += (now - sleeperState->rtcMem.valveOpenTime)/1000; } ets_uart_printf("valveClose\r\n"); }
/** * Sets the 'gpio_pin' pin as a GPIO and sets the interrupt to trigger on that pin. * The 'interruptArg' is the function argument that will be sent to your interruptHandler */ bool ICACHE_FLASH_ATTR easygpio_attachInterrupt(uint8_t gpio_pin, EasyGPIO_PullStatus pullStatus, void (*interruptHandler)(void *arg), void *interruptArg) { uint32_t gpio_name; uint8_t gpio_func; if (gpio_pin == 16) { os_printf("easygpio_setupInterrupt Error: GPIO16 does not have interrupts\n"); return false; } if (!easygpio_getGPIONameFunc(gpio_pin, &gpio_name, &gpio_func) ) { return false; } ETS_GPIO_INTR_ATTACH(interruptHandler, interruptArg); ETS_GPIO_INTR_DISABLE(); PIN_FUNC_SELECT(gpio_name, gpio_func); easygpio_setupPullsByName(gpio_name, pullStatus); // disable output GPIO_DIS_OUTPUT(gpio_pin); gpio_register_set(GPIO_PIN_ADDR(gpio_pin), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE) | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE)); //clear gpio14 status GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(gpio_pin)); ETS_GPIO_INTR_ENABLE(); return true; }
void ICACHE_FLASH_ATTR onewire_init() { // Set the configured pin as gpio pin. PIN_FUNC_SELECT(ONEWIRE_MUX, ONEWIRE_FUNC); // Enable pull-up on the configured pin. PIN_PULLUP_EN(ONEWIRE_MUX); // Set configured pin as an input GPIO_DIS_OUTPUT(ONEWIRE_PIN); }
/** * Uniform way of turning an output GPIO pin into input mode. Handles GPIO 0-16. * The pin must be initiated with easygpio_pinMode() so that the pin mux is setup as a gpio in the first place. * This function does the same thing as GPIO_DIS_OUTPUT, but works on GPIO16 too. */ void easygpio_outputDisable(uint8_t gpio_pin) { if (16==gpio_pin) { WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & 0xfffffffeUL); //out disable } else { GPIO_DIS_OUTPUT(GPIO_ID_PIN(gpio_pin)); } }
void ICACHE_FLASH_ATTR user_init() { uart_init(BIT_RATE_115200); PIN_FUNC_SELECT(PIN0_MUX, PIN0_FUNC); GPIO_DIS_OUTPUT(PIN0); PIN_FUNC_SELECT(PIN1_MUX, PIN1_FUNC); GPIO_DIS_OUTPUT(PIN1); static ETSTimer timer; os_timer_disarm(&timer); os_timer_setfn(&timer, (os_timer_func_t *)on_timeout, &timer); os_timer_arm(&timer, 250, 1); hap_setConnectedCb(onMqttConnected); hap_setDisconnectedCb(onMqttDisconnected); hap_init(); }
void supla_ds18b20_write( uint8_t v, int power ) { uint8_t bitMask; for (bitMask = 0x01; bitMask; bitMask <<= 1) { supla_ds18b20_write_bit( (bitMask & v)?1:0); } if ( !power) { GPIO_DIS_OUTPUT( supla_w1_pin ); GPIO_OUTPUT_SET( supla_w1_pin, 0 ); } }
static void NO_INTR_CODE set_gpio_interrupt(uint8 pin) { ETS_GPIO_INTR_DISABLE(); PIN_FUNC_SELECT(pin_mux[pin], pin_func[pin]); GPIO_DIS_OUTPUT(pin_num[pin]); gpio_register_set(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE) | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE)); ETS_GPIO_INTR_ENABLE(); }
// // Read a bit. Port and bit is used to cut lookup time and provide // more certain timing. // uint32 ICACHE_FLASH_ATTR onewire_read_bit(void) { uint32 r; GPIO_OUTPUT_SET(ONEWIRE_PIN, 0); os_delay_us(3); GPIO_DIS_OUTPUT(ONEWIRE_PIN); os_delay_us(10); r = GPIO_INPUT_GET(ONEWIRE_PIN); os_delay_us(53); return r; }
int supla_ds18b20_read_bit(void) { int r; GPIO_OUTPUT_SET( supla_w1_pin, 0 ); os_delay_us(3); GPIO_DIS_OUTPUT( supla_w1_pin ); os_delay_us(10); r = GPIO_INPUT_GET( supla_w1_pin ); os_delay_us(53); return r; }
int platform_gpio_mode( unsigned pin, unsigned mode, unsigned pull ) { NODE_DBG("Function platform_gpio_mode() is called. pin_mux:%d, func:%d\n", pin_mux[pin], pin_func[pin]); if (pin >= NUM_GPIO) return -1; if(pin == 0){ if(mode==PLATFORM_GPIO_INPUT) gpio16_input_conf(); else gpio16_output_conf(); return 1; } #ifdef LUA_USE_MODULES_PWM platform_pwm_close(pin); // closed from pwm module, if it is used in pwm #endif if (pull == PLATFORM_GPIO_PULLUP) { PIN_PULLUP_EN(pin_mux[pin]); } else { PIN_PULLUP_DIS(pin_mux[pin]); } switch(mode){ case PLATFORM_GPIO_INPUT: GPIO_DIS_OUTPUT(pin_num[pin]); set_gpio_no_interrupt(pin, TRUE); break; case PLATFORM_GPIO_OUTPUT: set_gpio_no_interrupt(pin, TRUE); GPIO_REG_WRITE(GPIO_ENABLE_W1TS_ADDRESS, BIT(pin_num[pin])); break; case PLATFORM_GPIO_OPENDRAIN: set_gpio_no_interrupt(pin, FALSE); GPIO_REG_WRITE(GPIO_ENABLE_W1TS_ADDRESS, BIT(pin_num[pin])); break; #ifdef GPIO_INTERRUPT_ENABLE case PLATFORM_GPIO_INT: set_gpio_interrupt(pin); break; #endif default: break; } return 1; }
/** * Sets the 'gpio_pin' pin as an input GPIO and sets the pull up and * pull down registers for that pin. */ bool ICACHE_FLASH_ATTR easygpio_setupAsInput(uint8_t gpio_pin, bool pullUp, bool pullDown) { uint32_t gpio_name; uint8_t gpio_func; if (!easygpio_getGpioNameFunc(gpio_pin, &gpio_name, &gpio_func) ) { return false; } PIN_FUNC_SELECT(gpio_name, gpio_func); easygpio_setupPulls(gpio_name, pullUp, pullDown); GPIO_DIS_OUTPUT(gpio_pin); return true; }
/** * Sets the 'gpio_pin' pin as an input GPIO and sets the pull up and * pull down registers for that pin. */ bool ICACHE_FLASH_ATTR easygpio_pinMode(uint8_t gpio_pin, EasyGPIO_PullStatus pullStatus, EasyGPIO_PinMode pinMode) { uint32_t gpio_name; uint8_t gpio_func; if (!easygpio_getGPIONameFunc(gpio_pin, &gpio_name, &gpio_func) ) { return false; } PIN_FUNC_SELECT(gpio_name, gpio_func); easygpio_setupPullsByName(gpio_name, pullStatus); if (EASYGPIO_OUTPUT != pinMode) { GPIO_DIS_OUTPUT(gpio_pin); } return true; }
static int do_gpio(int argc, const char* argv[]) { char *tmp = argv[2]; int gpio = skip_atoi(&tmp); if (strcmp(argv[1], "in") == 0) { GPIO_DIS_OUTPUT(gpio); console_printf("GP%d==%d\n", gpio, GPIO_INPUT_GET(gpio)); } else if (strcmp(argv[1], "out") == 0) { if (argc < 4) return; tmp = argv[3]; int v = skip_atoi(&tmp); GPIO_OUTPUT_SET(gpio, v); } }
void gpio_configure(port_t *port) { /* set GPIO function */ if (port->port_no >= GPIO12_NO && port->port_no <= GPIO15_NO) { PIN_FUNC_SELECT(ports_get_gpio_mux(port->port_no), 3 /* function number 3 is the GPIO */); } if (IS_OUTPUT(port)) { if (IS_PULL_UP(port)) { PIN_PULLUP_EN(ports_get_gpio_mux(port->port_no)); } else { PIN_PULLUP_DIS(ports_get_gpio_mux(port->port_no)); } } else { GPIO_DIS_OUTPUT(port->port_no); } }
/****************************************************************************** * FunctionName : user_plug_set_status * Description : set plug's status, 0x00 or 0x01 * Parameters : uint8 - status * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_plug_set_status(bool status) { // if (status != plug_param.status) { // if (status > 1) { // os_printf("error status input!\n"); // return; // } // // plug_param.status = status; // PLUG_STATUS_OUTPUT(PLUG_RELAY_LED_IO_NUM, status); // } if (status != plug_param.status) { if (status > 1) { os_printf("error status input!\n"); return; } plug_param.status = status; //PLUG_STATUS_OUTPUT(PLUG_RELAY_LED_IO_NUM, status); ETS_GPIO_INTR_DISABLE(); PIN_FUNC_SELECT(pin_mux[pin], pin_func[pin]); GPIO_DIS_OUTPUT(pin_num[pin]); gpio_register_set(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE) | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE)); ETS_GPIO_INTR_ENABLE(); //disable global interrupt ETS_GPIO_INTR_DISABLE(); //clear interrupt status GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(pin_num[pin])); pin_int_type[pin] = type; //enable interrupt gpio_pin_intr_state_set(GPIO_ID_PIN(pin_num[pin]), type); //enable global interrupt ETS_GPIO_INTR_ENABLE(); ETS_GPIO_INTR_ATTACH(func, arg) } }
//Init function void ICACHE_FLASH_ATTR user_init() { struct rst_info *reset_info; PRINTF("wake\r\n"); GPIO_DIS_OUTPUT(GPIO_SWITCH); reset_info = system_get_rst_info(); PRINTF("rst reason: %d\r\n", reset_info->reason); if(reset_info->reason == REASON_DEEP_SLEEP_AWAKE) { state = STATE_LOAD_PERSIST; } else { state = STATE_INIT_PERSIST; } os_timer_disarm(&timer); os_timer_setfn(&timer, (os_timer_func_t *)timer_fn, NULL); os_timer_arm(&timer, MIN_LOOP_TIMER_MS, false); /* // read high-frequency sensor // compare sensor w/ persistent value // if different: // connect to ap // notify server // on success change persistent value // deep sleep state = STATE_HF_READ; //Start os task system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); system_os_post(user_procTaskPrio, 0, 0 ); */ }
/** * Sets the 'gpio_pin' pin as a GPIO and sets the interrupt to trigger on that pin */ bool ICACHE_FLASH_ATTR easygpio_setupInterrupt(uint8_t gpio_pin, bool pullUp, bool pullDown, void (*interruptHandler)(void)) { uint32_t gpio_name; uint8_t gpio_func; if (gpio_pin == 6 || gpio_pin == 7 || gpio_pin == 8 || gpio_pin == 11 || gpio_pin >= 17) { os_printf("easygpio_setupInterrupt Error: There is no GPIO%d, check your code\n", gpio_pin); return false; } if (gpio_pin == 16) { os_printf("easygpio_setupInterrupt Error: GPIO16 does not have interrupts\n"); return false; } if (!easygpio_getGpioNameFunc(gpio_pin, &gpio_name, &gpio_func) ) { return false; } ETS_GPIO_INTR_ATTACH(interruptHandler, NULL); ETS_GPIO_INTR_DISABLE(); PIN_FUNC_SELECT(gpio_name, gpio_func); easygpio_setupPulls(gpio_name, pullUp, pullDown); // disable output GPIO_DIS_OUTPUT(gpio_pin); gpio_register_set(GPIO_PIN_ADDR(gpio_pin), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE) | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE)); //clear gpio14 status GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(gpio_pin)); ETS_GPIO_INTR_ENABLE(); return true; }
static void ICACHE_FLASH_ATTR pollDHTCb(void * arg){ int counter = 0; int laststate = 1; int i = 0; int bits_in = 0; // int bitidx = 0; // int bits[250]; int data[100]; data[0] = data[1] = data[2] = data[3] = data[4] = 0; // Wake up device, 250ms of high GPIO_OUTPUT_SET(DHT_PIN, 1); delay_ms(500); // Hold low for 20ms GPIO_OUTPUT_SET(DHT_PIN, 0); delay_ms(20); // High for 40ms // GPIO_OUTPUT_SET(2, 1); GPIO_DIS_OUTPUT(DHT_PIN); os_delay_us(40); // Set pin to input with pullup // PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); // os_printf("Waiting for gpio2 to drop \n"); // wait for pin to drop? while (GPIO_INPUT_GET(DHT_PIN) == 1 && i < DHT_MAXCOUNT) { if (i >= DHT_MAXCOUNT) { goto fail; } i++; } // os_printf("Reading DHT\n"); // read data! for (i = 0; i < MAXTIMINGS; i++) { // Count high time (in approx us) counter = 0; while (GPIO_INPUT_GET(DHT_PIN) == laststate) { counter++; os_delay_us(1); if (counter == 1000) break; } laststate = GPIO_INPUT_GET(DHT_PIN); if (counter == 1000) break; // store data after 3 reads if ((i > 3) && (i % 2 == 0)) { // shove each bit into the storage bytes data[bits_in / 8] <<= 1; if (counter > BREAKTIME) { //os_printf("1"); data[bits_in / 8] |= 1; } else { //os_printf("0"); } bits_in++; } } if (bits_in < 40) { os_printf("Got too few bits: %d should be at least 40", bits_in); goto fail; } int checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF; //os_printf("DHT: %02x %02x %02x %02x [%02x] CS: %02x\n", data[0], data[1],data[2],data[3],data[4],checksum); if (data[4] != checksum) { os_printf("Checksum was incorrect after %d bits. Expected %d but got %d", bits_in, data[4], checksum); goto fail; } reading.temperature = scale_temperature(data); reading.humidity = scale_humidity(data); //os_printf("Temp = %d*C, Hum = %d%%\n", (int)(reading.temperature * 100), (int)(reading.humidity * 100)); reading.success = 1; return; fail: os_printf("Failed to get DHT reading, dying\n"); reading.success = 0; }
int ICACHE_FLASH_ATTR set_gpio_mode(unsigned pin, unsigned mode, unsigned pull) { if (pin >= GPIO_PIN_NUM) return -1; if(pin == 0) { if(mode == GPIO_INPUT) gpio16_input_conf(); else gpio16_output_conf(); return 1; } switch(pull) { case GPIO_PULLUP: // PIN_PULLDWN_DIS(pin_mux[pin]); PIN_PULLUP_EN(pin_mux[pin]); break; case GPIO_PULLDOWN: PIN_PULLUP_DIS(pin_mux[pin]); // PIN_PULLDWN_EN(pin_mux[pin]); break; case GPIO_FLOAT: PIN_PULLUP_DIS(pin_mux[pin]); // PIN_PULLDWN_DIS(pin_mux[pin]); break; default: PIN_PULLUP_DIS(pin_mux[pin]); // PIN_PULLDWN_DIS(pin_mux[pin]); break; } switch(mode) { case GPIO_INPUT: GPIO_DIS_OUTPUT(pin_num[pin]); break; case GPIO_OUTPUT: ETS_GPIO_INTR_DISABLE(); #ifdef GPIO_INTERRUPT_ENABLE pin_int_type[pin] = GPIO_PIN_INTR_DISABLE; #endif PIN_FUNC_SELECT(pin_mux[pin], pin_func[pin]); //disable interrupt gpio_pin_intr_state_set(GPIO_ID_PIN(pin_num[pin]), GPIO_PIN_INTR_DISABLE); //clear interrupt status GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(pin_num[pin])); GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin]))) & (~ GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE))); //disable open drain; ETS_GPIO_INTR_ENABLE(); break; #ifdef GPIO_INTERRUPT_ENABLE case GPIO_INT: ETS_GPIO_INTR_DISABLE(); PIN_FUNC_SELECT(pin_mux[pin], pin_func[pin]); GPIO_DIS_OUTPUT(pin_num[pin]); gpio_register_set(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE) | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE)); ETS_GPIO_INTR_ENABLE(); break; #endif default: break; } return 1; }
int platform_gpio_mode( unsigned pin, unsigned mode, unsigned pull ) { // NODE_DBG("Function platform_gpio_mode() is called. pin_mux:%d, func:%d\n",pin_mux[pin],pin_func[pin]); if (pin >= NUM_GPIO) return -1; if(pin == 0){ if(mode==PLATFORM_GPIO_INPUT) gpio16_input_conf(); else gpio16_output_conf(); return 1; } platform_pwm_close(pin); // closed from pwm module, if it is used in pwm switch(pull){ case PLATFORM_GPIO_PULLUP: PIN_PULLDWN_DIS(pin_mux[pin]); PIN_PULLUP_EN(pin_mux[pin]); break; case PLATFORM_GPIO_PULLDOWN: PIN_PULLUP_DIS(pin_mux[pin]); PIN_PULLDWN_EN(pin_mux[pin]); break; case PLATFORM_GPIO_FLOAT: PIN_PULLUP_DIS(pin_mux[pin]); PIN_PULLDWN_DIS(pin_mux[pin]); break; default: PIN_PULLUP_DIS(pin_mux[pin]); PIN_PULLDWN_DIS(pin_mux[pin]); break; } switch(mode){ case PLATFORM_GPIO_INPUT: #if defined( LUA_USE_MODULES_GPIO ) && defined( GPIO_INTERRUPT_ENABLE ) lua_gpio_unref(pin); // unref the lua ref call back. #endif GPIO_DIS_OUTPUT(pin_num[pin]); case PLATFORM_GPIO_OUTPUT: ETS_GPIO_INTR_DISABLE(); #ifdef GPIO_INTERRUPT_ENABLE pin_int_type[pin] = GPIO_PIN_INTR_DISABLE; #endif PIN_FUNC_SELECT(pin_mux[pin], pin_func[pin]); //disable interrupt gpio_pin_intr_state_set(GPIO_ID_PIN(pin_num[pin]), GPIO_PIN_INTR_DISABLE); //clear interrupt status GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(pin_num[pin])); GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin]))) & (~ GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE))); //disable open drain; ETS_GPIO_INTR_ENABLE(); break; #ifdef GPIO_INTERRUPT_ENABLE case PLATFORM_GPIO_INT: ETS_GPIO_INTR_DISABLE(); PIN_FUNC_SELECT(pin_mux[pin], pin_func[pin]); GPIO_DIS_OUTPUT(pin_num[pin]); gpio_register_set(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE) | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE)); ETS_GPIO_INTR_ENABLE(); break; #endif default: break; } return 1; }
void Softuart_Init(Softuart *s, uint16_t baudrate) { //disable rs485 s->is_rs485 = 0; if(! _Softuart_Instances_Count) { os_printf("SOFTUART initialize gpio\r\n"); //Initilaize gpio subsystem gpio_init(); } //set bit time s->bit_time = (1000000 / baudrate); os_printf("SOFTUART bit_time is %d\r\n",s->bit_time); //init tx pin if(!s->pin_tx.gpio_mux_name) { os_printf("SOFTUART ERROR: Set tx pin (%d)\r\n",s->pin_tx.gpio_mux_name); } else { //enable pin as gpio PIN_FUNC_SELECT(s->pin_tx.gpio_mux_name, s->pin_tx.gpio_func); //set pullup (UART idle is VDD) PIN_PULLUP_EN(s->pin_tx.gpio_mux_name); //set high for tx idle GPIO_OUTPUT_SET(GPIO_ID_PIN(s->pin_tx.gpio_id), 1); os_delay_us(100000); os_printf("SOFTUART TX INIT DONE\r\n"); } //init rx pin if(!s->pin_rx.gpio_mux_name) { os_printf("SOFTUART ERROR: Set rx pin (%d)\r\n",s->pin_rx.gpio_mux_name); } else { //enable pin as gpio PIN_FUNC_SELECT(s->pin_rx.gpio_mux_name, s->pin_rx.gpio_func); //set pullup (UART idle is VDD) PIN_PULLUP_EN(s->pin_rx.gpio_mux_name); //set to input -> disable output GPIO_DIS_OUTPUT(GPIO_ID_PIN(s->pin_rx.gpio_id)); //set interrupt related things //disable interrupts by GPIO ETS_GPIO_INTR_DISABLE(); //attach interrupt handler and a pointer that will be passed around each time ETS_GPIO_INTR_ATTACH(Softuart_Intr_Handler, s); //not sure what this does... (quote from example): // void gpio_register_set(uint32 reg_id, uint32 value); // // From include file // Set the specified GPIO register to the specified value. // This is a very general and powerful interface that is not // expected to be used during normal operation. It is intended // mainly for debug, or for unusual requirements. // // All people repeat this mantra but I don't know what it means // gpio_register_set(GPIO_PIN_ADDR(s->pin_rx.gpio_id), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE) | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE)); //clear interrupt handler status, basically writing a low to the output GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(s->pin_rx.gpio_id)); //enable interrupt for pin on any edge (rise and fall) //@TODO: should work with ANYEDGE (=3), but complie error gpio_pin_intr_state_set(GPIO_ID_PIN(s->pin_rx.gpio_id), 3); //globally enable GPIO interrupts ETS_GPIO_INTR_ENABLE(); os_printf("SOFTUART RX INIT DONE\r\n"); } //add instance to array of instances _Softuart_GPIO_Instances[s->pin_rx.gpio_id] = s; _Softuart_Instances_Count++; os_printf("SOFTUART INIT DONE\r\n"); }
bool DHTRead(DHT_Sensor *sensor, DHT_Sensor_Data* output) { int counter = 0; int laststate = 1; int i = 0; int j = 0; int checksum = 0; int data[100]; data[0] = data[1] = data[2] = data[3] = data[4] = 0; uint8_t pin = GPIO_ID_PIN(sensor->port); // Wake up device, 250ms of high GPIO_OUTPUT_SET(pin, 1); sleepms(250); // Hold low for 20ms GPIO_OUTPUT_SET(pin, 0); sleepms(20); // High for 40ns GPIO_OUTPUT_SET(pin, 1); os_delay_us(40); // Set DHT_PIN pin as an input GPIO_DIS_OUTPUT(pin); // wait for pin to drop? while (GPIO_INPUT_GET(pin) == 1 && i < DHT_MAXCOUNT) { os_delay_us(1); i++; } if(i == DHT_MAXCOUNT) { DHT_DEBUG("DHT: Failed to get reading from GPIO%d, dying\r\n", pin); return false; } // read data for (i = 0; i < DHT_MAXTIMINGS; i++) { // Count high time (in approx us) counter = 0; while (GPIO_INPUT_GET(pin) == laststate) { counter++; os_delay_us(1); if (counter == 1000) break; } laststate = GPIO_INPUT_GET(pin); if (counter == 1000) break; // store data after 3 reads if ((i>3) && (i%2 == 0)) { // shove each bit into the storage bytes data[j/8] <<= 1; if (counter > DHT_BREAKTIME) data[j/8] |= 1; j++; } } if (j >= 39) { checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF; DHT_DEBUG("DHT%s: %02x %02x %02x %02x [%02x] CS: %02x (GPIO%d)\r\n", sensor->type==DHT11?"11":"22", data[0], data[1], data[2], data[3], data[4], checksum, pin); if (data[4] == checksum) { // checksum is valid output->temperature = scale_temperature(sensor->type, data); output->humidity = scale_humidity(sensor->type, data); //DHT_DEBUG("DHT: Temperature = %d *C, Humidity = %d %%\r\n", (int)(reading.temperature * 100), (int)(reading.humidity * 100)); DHT_DEBUG("DHT: Temperature*100 = %d *C, Humidity*100 = %d %% (GPIO%d)\n", (int) (output->temperature * 100), (int) (output->humidity * 100), pin); } else { //DHT_DEBUG("Checksum was incorrect after %d bits. Expected %d but got %d\r\n", j, data[4], checksum); DHT_DEBUG("DHT: Checksum was incorrect after %d bits. Expected %d but got %d (GPIO%d)\r\n", j, data[4], checksum, pin); return false; } } else { //DHT_DEBUG("Got too few bits: %d should be at least 40\r\n", j); DHT_DEBUG("DHT: Got too few bits: %d should be at least 40 (GPIO%d)\r\n", j, pin); return false; } return true; }