void ioInit() { // outputs PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); // inputs w/pull up PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4); PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO4_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5); PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO5_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDI_U); }
/** * Set the state of the specific pin. * * The possible states are: * * JSHPINSTATE_UNDEFINED * JSHPINSTATE_GPIO_OUT * JSHPINSTATE_GPIO_OUT_OPENDRAIN * JSHPINSTATE_GPIO_IN * JSHPINSTATE_GPIO_IN_PULLUP * JSHPINSTATE_GPIO_IN_PULLDOWN * JSHPINSTATE_ADC_IN * JSHPINSTATE_AF_OUT * JSHPINSTATE_AF_OUT_OPENDRAIN * JSHPINSTATE_USART_IN * JSHPINSTATE_USART_OUT * JSHPINSTATE_DAC_OUT * JSHPINSTATE_I2C */ void jshPinSetState(Pin pin, //!< The pin to have its state changed. JshPinState state //!< The new desired state of the pin. ) { // Debug // os_printf("> ESP8266: jshPinSetState %d, %s\n", pin, pinStateToString(state)); assert(pin < 16); int periph = PERIPHS_IO_MUX + PERIPHS[pin]; // Disable the pin's pull-up. PIN_PULLUP_DIS(periph); //PIN_PULLDWN_DIS(periph); uint8_t primary_func = pin < 6 ? (PERIPHS_IO_MUX_U0TXD_U == pin || PERIPHS_IO_MUX_U0RXD_U == pin) ? FUNC_UART : FUNC_GPIO : 0; uint8_t select_func = pinFunction(state); PIN_FUNC_SELECT(periph, primary_func == select_func ? 0 : select_func); switch (state) { case JSHPINSTATE_GPIO_OUT: case JSHPINSTATE_GPIO_OUT_OPENDRAIN: //case JSHPINSTATE_AF_OUT: //case JSHPINSTATE_AF_OUT_OPENDRAIN: //case JSHPINSTATE_USART_OUT: //case JSHPINSTATE_DAC_OUT: gpio_output_set(0, 1 << pin, 1 << pin, 0); break; case JSHPINSTATE_GPIO_IN_PULLUP: PIN_PULLUP_EN(periph); //case JSHPINSTATE_GPIO_IN_PULLDOWN: if (JSHPINSTATE_GPIO_IN_PULLDOWN == pin) PIN_PULLDWN_EN(periph); case JSHPINSTATE_GPIO_IN: gpio_output_set(0, 0, 0, 1 << pin); break; case JSHPINSTATE_ADC_IN: case JSHPINSTATE_USART_IN: case JSHPINSTATE_I2C: PIN_PULLUP_EN(periph); break; default: break; } }
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); }
void gpio_config(GPIO_ConfigTypeDef *pGPIOConfig) { uint32 gpio_pin_mask = pGPIOConfig->GPIO_Pin; uint32 gpio_pin_mask_high = pGPIOConfig->GPIO_Pin_high; uint32 io_reg; uint8 io_num = 0; uint32 pin_reg; uint32 bit_valid; if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Input) { GPIO_AS_INPUT(gpio_pin_mask); GPIO_AS_INPUT_HIGH(gpio_pin_mask_high); } else if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Output) { GPIO_AS_OUTPUT(gpio_pin_mask); GPIO_AS_OUTPUT_HIGH(gpio_pin_mask_high); } do { bit_valid = (io_num >= 32 ? (gpio_pin_mask_high & (0x1 << (io_num - 32))) : (gpio_pin_mask & (0x1 << io_num))); if (bit_valid && (io_reg = GPIO_PIN_REG[io_num])) { if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Input) { SET_PERI_REG_MASK(io_reg, FUN_IE); } //for ESP32 function 2 of every pad is allways GPIO func PIN_FUNC_SELECT(io_reg, 2); if (pGPIOConfig->GPIO_Pullup) { PIN_PULLUP_EN(io_reg); } else { PIN_PULLUP_DIS(io_reg); } if (pGPIOConfig->GPIO_Pulldown) { PIN_PULLDWN_EN(io_reg); } else { PIN_PULLDWN_DIS(io_reg); } if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Out_OD) { portENTER_CRITICAL(); pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(io_num)); //pin_reg &= (~GPIO_GPIO_PIN0_PAD_DRIVER); pin_reg |= GPIO_GPIO_PIN0_PAD_DRIVER; GPIO_REG_WRITE(GPIO_PIN_ADDR(io_num), pin_reg); portEXIT_CRITICAL(); } gpio_pin_intr_state_set(io_num, pGPIOConfig->GPIO_IntrType); } io_num++; } while (io_num < GPIO_PIN_COUNT); }
void ICACHE_FLASH_ATTR dhgpio_pull(unsigned int pollup_mask, unsigned int nopoll_mask) { if(pollup_mask & PIN_GPIOO) PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO0_U); if(pollup_mask & PIN_GPIO1) PIN_PULLUP_EN(PERIPHS_IO_MUX_U0TXD_U); if(pollup_mask & PIN_GPIO2) PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); if(pollup_mask & PIN_GPIO3) PIN_PULLUP_EN(PERIPHS_IO_MUX_U0RXD_U); if(pollup_mask & PIN_GPIO4) PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO4_U); if(pollup_mask & PIN_GPIO5) PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO5_U); if(pollup_mask & PIN_GPIO12) PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDI_U); if(pollup_mask & PIN_GPIO13) PIN_PULLUP_EN(PERIPHS_IO_MUX_MTCK_U); if(pollup_mask & PIN_GPIO14) PIN_PULLUP_EN(PERIPHS_IO_MUX_MTMS_U); if(pollup_mask & PIN_GPIO15) PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDO_U); if(nopoll_mask & PIN_GPIOO) PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO0_U); if(nopoll_mask & PIN_GPIO1) PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); if(nopoll_mask & PIN_GPIO2) PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U); if(nopoll_mask & PIN_GPIO3) PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0RXD_U); if(nopoll_mask & PIN_GPIO4) PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO4_U); if(nopoll_mask & PIN_GPIO5) PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO5_U); if(nopoll_mask & PIN_GPIO12) PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDI_U); if(nopoll_mask & PIN_GPIO13) PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTCK_U); if(nopoll_mask & PIN_GPIO14) PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTMS_U); if(nopoll_mask & PIN_GPIO15) PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U); }
void ICACHE_FLASH_ATTR DHTInit(enum DHTType dht_type, uint32_t poll_time) { sensor_type = dht_type; PIN_FUNC_SELECT(DHT_MUX, DHT_FUNC); PIN_PULLUP_EN(DHT_MUX); os_printf("DHT setup for type %d\r\n", dht_type); os_timer_disarm(&dhtTimer); os_timer_setfn(&dhtTimer, DHTCb, NULL); os_timer_arm(&dhtTimer, poll_time, 1); }
LOCAL void ICACHE_FLASH_ATTR buttonTask(void *pvParameters) { static uint32_t messageId = 0; uint8_t loopIndex; uint8_t currentValue; uint8_t remoteRequest[50] = {0}; uint32_t remoteRequestLength; DBGINF(("Button task initiated")); vTaskDelay(100); DBGINF(("Button task running")); for (loopIndex = 0; loopIndex < CTNC_MAX_NUMBER_OF_RESOURCE; loopIndex++) { if (CTNC_RES_TYPE_BINARY_INPUT == fResource[loopIndex].type) { PIN_FUNC_SELECT(fResource[loopIndex].gpioName, fResource[loopIndex].gpioFunc); PIN_PULLUP_EN(fResource[loopIndex].gpioName); //GPIO_DIS_OUTPUT(GPIO_ID_PIN(fResource[loopIndex].gpioNumber)); gpio_output_set(0, BIT(GPIO_ID_PIN(fResource[loopIndex].gpioNumber)), 0,0); } } DBGINF(("All resource output set to current values.")); while (1) { for (loopIndex = 0; loopIndex < CTNC_MAX_NUMBER_OF_RESOURCE; loopIndex++) { fResource[loopIndex].lacheValue++; currentValue = GPIO_READ(fResource[loopIndex].gpioNumber); if ( (currentValue != fResource[loopIndex].value) || (fResource[loopIndex].lacheValue >= 5000) ) { fResource[loopIndex].value = currentValue; fResource[loopIndex].lacheValue = 0; messageId++; remoteRequestLength = snprintf( &remoteRequest[0], 50, "/%d/%s/%s/uresval/%d/%d", messageId, MESH_getNodeID(), MESH_getNodeID(), fResource[loopIndex].id, fResource[loopIndex].value); for (loopIndex = 0; loopIndex < 4; loopIndex++) { sendUDPData(remoteRequest, remoteRequestLength); vTaskDelay(50); } break; } } vTaskDelay(CTNC_BTN_TASK_SLEEP); } DBGERR(("QUITING BUTTON TASK, This is never expected")); vTaskDelete(NULL); }
void BtnInit() { // Select pin function PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); // Enable pull up R PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO0_U); // Set GPIO0 as input mode gpio_output_set(0, 0, 0, BIT0); os_timer_disarm(&BtnTimer); os_timer_setfn(&BtnTimer, BtnTimerCb, NULL); os_timer_arm(&BtnTimer, 500, 1); }
void DHTInit(enum sensor_type sensor_type, uint32_t polltime) { SENSOR = sensor_type; // Set OKA_DHT_IO_NUM to output mode for DHT22 PIN_FUNC_SELECT(OKA_DHT_IO_MUX, OKA_DHT_IO_FUNC); PIN_PULLUP_EN(OKA_DHT_IO_MUX); /* PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); */ os_printf("DHT sensor: setup for type %d, poll interval of %d\n", sensor_type, (int)polltime); static ETSTimer dhtTimer; os_timer_setfn(&dhtTimer, pollDHTCb, NULL); os_timer_arm(&dhtTimer, polltime, 1); }
/****************************************************************************** * FunctionName : peri_jdq_init * Description : JDQ initialize, mainly initialize pwm mode * Parameters : uint8 gpio_id * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR peri_jdq_init(uint8 gpio_id) { // enum JDQ_STATE jdq_state; PRINTF("I am the jdq\n"); //PRINTF("jdq_state: %d\n", jdq_state); PIN_FUNC_SELECT(tisan_get_gpio_name(gpio_id), tisan_get_gpio_general_func(gpio_id)); PIN_PULLUP_EN(tisan_get_gpio_name(gpio_id)); jdq_pin=gpio_id; }
/** * Sets the pull up and pull down registers for a pin. */ static void ICACHE_FLASH_ATTR easygpio_setupPulls(uint32_t gpio_name, bool pullUp, bool pullDown) { if (pullUp) { PIN_PULLDWN_DIS(gpio_name); PIN_PULLUP_EN(gpio_name); } else if (pullDown) { PIN_PULLUP_DIS(gpio_name); PIN_PULLDWN_EN(gpio_name); } else { PIN_PULLDWN_DIS(gpio_name); PIN_PULLUP_DIS(gpio_name); } }
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 pull up and pull down registers for a pin. * This seems to do very little for the actual pull effect * - it's always pull up for both EASYGPIO_PULLUP and EASYGPIO_PULLDOWN. * But that is something the SDK needs to fix. */ static void ICACHE_FLASH_ATTR easygpio_setupPullsByName(uint32_t gpio_name, EasyGPIO_PullStatus pullStatus) { if (EASYGPIO_PULLUP == pullStatus){ PIN_PULLDWN_DIS(gpio_name); PIN_PULLUP_EN(gpio_name); } else if (EASYGPIO_PULLDOWN == pullStatus){ PIN_PULLUP_DIS(gpio_name); PIN_PULLDWN_EN(gpio_name); } else { PIN_PULLDWN_DIS(gpio_name); PIN_PULLUP_DIS(gpio_name); } }
void ICACHE_FLASH_ATTR DHTInit(enum sensor_type sensor_type, uint32_t polltime) { SENSOR = sensor_type; // Set GPIO2 to output mode for DHT22 PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); pollDHTCb(NULL); os_printf("DHT Setup for type %d, poll interval of %d\n", sensor_type, (int)polltime); static ETSTimer dhtTimer; os_timer_setfn(&dhtTimer, pollDHTCb, NULL); os_timer_arm(&dhtTimer, polltime, 1); }
void ICACHE_FLASH_ATTR user_dht22_init(void) { //Set GPIO2 to output mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); os_timer_disarm(&some_timer); //Setup timer os_timer_setfn(&some_timer, (os_timer_func_t *) readDHT, NULL); //Arm the timer //&some_timer is the pointer //1000 is the fire time in ms //0 for once and 1 for repeating os_timer_arm(&some_timer, 2500, 1); }
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); } }
void ICACHE_FLASH_ATTR vibrate_init(uint8 gpio_id) { uint32 v_gpio_name=tisan_get_gpio_name(gpio_id); uint8 v_gpio_func=tisan_get_gpio_general_func(gpio_id); PIN_FUNC_SELECT(v_gpio_name, v_gpio_func); PIN_PULLUP_EN(v_gpio_name); gpio_output_set(0, 0, 0, GPIO_ID_PIN(gpio_id)); //set as input mode gpio_register_set(GPIO_PIN_ADDR(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 status GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(gpio_id)); //enable interrupt gpio_pin_intr_state_set(GPIO_ID_PIN(gpio_id), GPIO_PIN_INTR_NEGEDGE); peri_alarm_init(ALARM_GPIO_ID); peri_vibrate_tim_start(100); }
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; }
esp_err_t spi_set_interface(spi_host_t host, spi_interface_t *interface) { SPI_CHECK(host < SPI_NUM_MAX, "host num error", ESP_ERR_INVALID_ARG); SPI_CHECK(spi_object[host], "spi has not been initialized yet", ESP_FAIL); SPI_CHECK(interface, "parameter pointer is empty", ESP_ERR_INVALID_ARG); spi_object[host]->interface = *interface; ENTER_CRITICAL(); switch (host) { case CSPI_HOST: { // Initialize SPI IO PIN_PULLUP_EN(PERIPHS_IO_MUX_SD_CLK_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SPICLK); if (interface->mosi_en) { PIN_PULLUP_EN(PERIPHS_IO_MUX_SD_DATA1_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, FUNC_SPID_MOSI); } if (interface->miso_en) { PIN_PULLUP_EN(PERIPHS_IO_MUX_SD_DATA0_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, FUNC_SPIQ_MISO); } if (interface->cs_en) { PIN_PULLUP_EN(PERIPHS_IO_MUX_SD_CMD_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, FUNC_SPICS0); } } break; case HSPI_HOST: { // Initialize HSPI IO PIN_PULLUP_EN(PERIPHS_IO_MUX_MTMS_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_HSPI_CLK); //GPIO14 is SPI CLK pin (Clock) if (interface->mosi_en) { PIN_PULLUP_EN(PERIPHS_IO_MUX_MTCK_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_HSPID_MOSI); //GPIO13 is SPI MOSI pin (Master Data Out) } if (interface->miso_en) { PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDI_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_HSPIQ_MISO); //GPIO12 is SPI MISO pin (Master Data In) } if (interface->cs_en) { PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDO_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_HSPI_CS0); } } break; } // Set the clock polarity and phase SPI[host]->pin.ck_idle_edge = interface->cpol; if (interface->cpol == interface->cpha) { SPI[host]->user.ck_out_edge = false; } else { SPI[host]->user.ck_out_edge = true; } // Set data bit order SPI[host]->ctrl.wr_bit_order = interface->bit_tx_order; SPI[host]->ctrl.rd_bit_order = interface->bit_rx_order; // Set data byte order SPI[host]->user.wr_byte_order = interface->byte_tx_order; SPI[host]->user.rd_byte_order = interface->byte_rx_order; EXIT_CRITICAL(); return ESP_OK; }
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; }
LOCAL void ICACHE_FLASH_ATTR readDHT(void *arg) { int counter = 0; int laststate = 1; int i = 0; int j = 0; int checksum = 0; DEBUG("[DHT22] start read \r\n"); int data[100]; data[0] = data[1] = data[2] = data[3] = data[4] = 0; GPIO_OUTPUT_SET(2, 1); os_delay_us(250000); GPIO_OUTPUT_SET(2, 0); os_delay_us(2000); GPIO_OUTPUT_SET(2, 1); os_delay_us(40); GPIO_DIS_OUTPUT(2); PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); // wait for pin to drop? while (GPIO_INPUT_GET(2) == 1 && i < 100000) { os_delay_us(1); i++; } if (i == 100000) return; // read data! for (i = 0; i < MAXTIMINGS; i++) { counter = 0; while (GPIO_INPUT_GET(2) == laststate) { counter++; os_delay_us(1); if (counter == 1000) break; } laststate = GPIO_INPUT_GET(2); if (counter == 1000) break; //bits[bitidx++] = counter; if ((i > 3) && (i % 2 == 0)) { // shove each bit into the storage bytes data[j / 8] <<= 1; if (counter > BREAKTIME) data[j / 8] |= 1; j++; } } /* for (i=3; i<bitidx; i+=2) { os_printf("bit %d: %d\n", i-3, bits[i]); os_printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > BREAKTIME); } os_printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]); */ float temp_p, hum_p; if (j >= 39) { checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF; if (data[4] == checksum) { /* yay! checksum is valid */ hum_p = data[0] * 256 + data[1]; hum_p /= 10; temp_p = (data[2] & 0x7F) * 256 + data[3]; temp_p /= 10.0; if (data[2] & 0x80) temp_p *= -1; //sendReading(temp_p, hum_p); char tmp[30]; lastTemp = temp_p; lastHum = hum_p; DEBUG("[DHT22] Temp = %.1f, Hum = %.1f\r\n", temp_p, hum_p); } } DEBUG("[DHT22] end read \r\n"); }
//Main routine. Initialize stdout, the I/O and the webserver and we're done. void user_init(void) { // my stuff dBR_BOILER_SET=65; // default setting 65C boiler temperature on reboot dBR_TEMP_ROOM_SET=18; // default setting 18C room temperature on reboot dBR_HUMIDITY_SET=60; // default setting 65% humidity target on reboot dBR_MODE=2; // default BR MODE is AUTO dDEVICE_MODE=1; // This mode defines if device uses remote (MQTT) management or uses local logics - 0 for remote, 1 for local mode. DEFAULT is 0 - remote // HTTPD stdoutInit(); ioInit(); httpdInit(builtInUrls, 80); //MQTT uart_init(115200, 115200); CFG_Load(); sleepms(1000); MQTT_InitConnection(&mqttClient, sysCfg.mqtt_host, sysCfg.mqtt_port, SEC_NONSSL); //MQTT_InitConnection(&mqttClient, "192.168.11.122", 1880, 0); MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive, 1); //MQTT_InitClient(&mqttClient, "client_id", "user", "pass", 120, 1); // MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0); MQTT_OnConnected(&mqttClient, mqttConnectedCb); MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); MQTT_OnPublished(&mqttClient, mqttPublishedCb); MQTT_OnData(&mqttClient, mqttDataCb); INFO("device_ID:%s\r\n",sysCfg.device_id); INFO("MQTTHOST:%s\r\n",sysCfg.mqtt_host); //DS18B20 timers os_timer_disarm(&ds18b20_timer); os_timer_setfn(&ds18b20_timer, (os_timer_func_t *)ds18b20_cb, (void *)0); os_timer_arm(&ds18b20_timer, DELAY, 1); // DHT22 initialize DHTInit(DHT22, DELAY); os_timer_disarm(&dht22_timer); os_timer_setfn(&dht22_timer, (os_timer_func_t *)dht22_cb, (void *)0); os_timer_arm(&dht22_timer, DELAY, 1); // INPUT PIN initialize ETS_GPIO_INTR_DISABLE(); // Disable gpio interrupts ETS_GPIO_INTR_ATTACH(read_input_pin, 13); // GPIO13 interrupt handler PIN_FUNC_SELECT(PIN_GPIO13_MUX, PIN_GPIO13_FUNC); gpio_output_set(0, 0, 0, BIT13); // Set GPIO13 as input PIN_PULLUP_EN(PIN_GPIO13_MUX); // Enable pullup GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(13)); // Clear GPIO12 status gpio_pin_intr_state_set(GPIO_ID_PIN(13), GPIO_PIN_INTR_NEGEDGE); // Interrupt on NEGATIVE GPIO13 edge ETS_GPIO_INTR_ENABLE(); // Enable gpio interrupts // INFO("Input pin INITIALIZED ! ! !"); // initialize GPIO12 PIN_FUNC_SELECT(PIN_GPIO12_MUX, PIN_GPIO12_FUNC); GPIO_OUTPUT_SET(PIN_GPIO12, 0); // INFO("GPIO12 set to OFF\r\n"); // initialize GPIO5 GPIO_OUTPUT_SET(PIN_GPIO5, 0); // INFO("GPIO5 set to OFF\r\n"); // initialize GPIO14 PIN_FUNC_SELECT(PIN_GPIO14_MUX, PIN_GPIO14_FUNC); GPIO_OUTPUT_SET(PIN_GPIO14, 0); // INFO("GPIO14 set to OFF\r\n"); WIFI_Connect(sysCfg.sta_ssid, sysCfg.sta_pwd, wifiConnectCb); os_printf("\nReady\n"); }
// pinMode([pin], [direction]) void ICACHE_FLASH_ATTR at_setupPinModeCmd(uint8_t id, char *pPara) { int result = 0, err = 0, flag = 0; uint8 buffer[32] = {0}; pPara++; // skip '=' //get the first parameter (uint8_t) // digit flag = at_get_next_int_dec(&pPara, &result, &err); // flag must be true because there are more parameters if ((flag == FALSE) && (result > 0) ) { at_response_error(); return; } uint8_t pin = result; // Don't go any further if the pin is un-usable or non-existant if (!pinValid(pin)) { at_response_error(); return; } if (*pPara++ != ',') { // skip ',' at_response_error(); return; } char mode = *pPara++; if ((mode == 'i') || (mode == 'I')) { if (pin == STATUS_LED_PIN) { wifi_status_led_uninstall(); } PIN_FUNC_SELECT(pinMap[pin], pinFunction[pin]); GPIO_DIS_OUTPUT(pin); PIN_PULLUP_DIS(pinMap[pin]); os_sprintf(buffer, "%d=INPUT\r\n", pin); } else if ((mode == 'o') || (mode == 'O')) { if (pin == STATUS_LED_PIN) { wifi_status_led_uninstall(); } PIN_FUNC_SELECT(pinMap[pin], pinFunction[pin]); GPIO_OUTPUT_SET(pin, 0); os_sprintf(buffer, "%d=OUTPUT\r\n", pin); } else if ((mode == 'p') || (mode == 'P')) { if (pin == STATUS_LED_PIN) { wifi_status_led_uninstall(); } PIN_FUNC_SELECT(pinMap[pin], pinFunction[pin]); GPIO_DIS_OUTPUT(pin); PIN_PULLUP_EN(pinMap[pin]); os_sprintf(buffer, "%d=INPUT_PULLUP\r\n", pin); } else { at_response_error(); return; } at_port_print(buffer); at_response_ok(); }
void ICACHE_FLASH_ATTR peri_alarm_init(uint8 gpio_id) { PIN_FUNC_SELECT(tisan_get_gpio_name(gpio_id), tisan_get_gpio_general_func(gpio_id)); PIN_PULLUP_EN(tisan_get_gpio_name(gpio_id)); }
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"); }
void ICACHE_FLASH_ATTR supla_esp_gpio_init(void) { switch_cfgbtn_state_check = 0; switch_cfgbtn_last_state = -1; switch_cfgbtn_counter = 0; #if defined(USE_GPIO3) || defined(UART_SWAP) system_uart_swap (); #endif ETS_GPIO_INTR_DISABLE(); GPIO_PORT_INIT; #ifdef USE_GPIO16_INPUT gpio16_input_conf(); #endif #ifdef USE_GPIO16_OUTPUT gpio16_output_conf(); #endif #ifdef USE_GPIO3 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3); PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0RXD_U); #endif #ifdef BTN_PULLUP #if CFG_PORT == 0 PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO0_U); #elif CFG_PORT == 2 PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); #elif CFG_PORT == 4 PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO4_U); #elif CFG_PORT == 12 PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDI_U); #elif CFG_PORT == 13 PIN_PULLUP_EN(PERIPHS_IO_MUX_MTCK_U); #elif CFG_PORT == 14 PIN_PULLUP_EN(PERIPHS_IO_MUX_MTMS_U); #endif #endif #ifdef LED_RED_PORT #if LED_RED_PORT != 16 gpio_pin_intr_state_set(GPIO_ID_PIN(LED_RED_PORT), GPIO_PIN_INTR_DISABLE); #endif #endif #ifdef LED_GREEN_PORT gpio_pin_intr_state_set(GPIO_ID_PIN(LED_GREEN_PORT), GPIO_PIN_INTR_DISABLE); #endif #ifdef LED_BLUE_PORT gpio_pin_intr_state_set(GPIO_ID_PIN(LED_BLUE_PORT), GPIO_PIN_INTR_DISABLE); #endif #ifdef RELAY1_PORT gpio_pin_intr_state_set(GPIO_ID_PIN(RELAY1_PORT), GPIO_PIN_INTR_DISABLE); #endif #ifdef RELAY2_PORT gpio_pin_intr_state_set(GPIO_ID_PIN(RELAY2_PORT), GPIO_PIN_INTR_DISABLE); #endif #ifdef ZAM_INPUT1 supla_esp_gpio_enable_input_port(ZAM_INPUT1); #endif #ifdef ZAM_INPUT2 supla_esp_gpio_enable_input_port(ZAM_INPUT2); #endif ETS_GPIO_INTR_ENABLE(); keys.key_num = 0; single_key[0] = key_init_single(CFG_PORT, supla_esg_gpio_cfg_pressed, supla_esg_gpio_manual_pressed); keys.key_num++; #if defined(BUTTON1_PORT) && defined(RELAY1_PORT) single_key[keys.key_num] = key_init_single(BUTTON1_PORT, NULL, supla_esg_gpio_button1_pressed); keys.key_num++; #endif #if defined(BUTTON1_PORT) && defined(RELAY1_PORT) single_key[keys.key_num] = key_init_single(BUTTON2_PORT, NULL, supla_esg_gpio_button2_pressed); keys.key_num++; #endif keys.single_key = single_key; keys.handler = supla_esp_key_intr_handler; #ifdef RELAY_STATE_RESTORE struct rst_info *rtc_info = system_get_rst_info(); #ifdef RELAY1_PORT if ( rtc_info->reason == 0 ) { supla_esp_gpio_relay1_hi(supla_esp_state.Relay1); } #endif #ifdef RELAY2_PORT if ( rtc_info->reason == 0 ) { supla_esp_gpio_relay2_hi(supla_esp_state.Relay2); } #endif #elif defined(RESET_RELAY_PORT) #ifdef RELAY1_PORT supla_esp_gpio_relay1_hi(RELAY_INIT_VALUE); #endif #ifdef RELAY2_PORT supla_esp_gpio_relay2_hi(RELAY_INIT_VALUE); #endif #endif key_init(&keys); GPIO_PORT_POST_INIT; supla_esp_gpio_init_time = system_get_time(); }