/**
 * 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);
    }
}
/**
 * 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);
  }
}
Exemple #3
0
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 Softuart_EnableRs485(Softuart *s, uint8_t gpio_id)
{
	os_printf("SOFTUART RS485 init\r\n");

	//enable rs485
	s->is_rs485 = 1;

	//set pin in instance
	s->pin_rs485_tx_enable = gpio_id;

	//enable pin as gpio
	PIN_FUNC_SELECT(softuart_reg[gpio_id].gpio_mux_name,softuart_reg[gpio_id].gpio_func); 

	PIN_PULLUP_DIS(softuart_reg[gpio_id].gpio_mux_name);
	PIN_PULLDWN_DIS(softuart_reg[gpio_id].gpio_mux_name);

	//set low for tx idle (so other bus participants can send)
	GPIO_OUTPUT_SET(GPIO_ID_PIN(gpio_id), 0);
	
	os_printf("SOFTUART RS485 init done\r\n");
}
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 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);
		//disable pulldown
		PIN_PULLDWN_DIS(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");
	}


	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);
		//disable pulldown
		PIN_PULLDWN_DIS(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");
	}

	//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");
}