2015-06-05 01:46:25 +00:00
|
|
|
/** esp/iomux.h
|
|
|
|
*
|
|
|
|
* Configuration of iomux registers.
|
|
|
|
*
|
|
|
|
* Part of esp-open-rtos
|
|
|
|
* Copyright (C) 2015 Superhouse Automation Pty Ltd
|
|
|
|
* BSD Licensed as described in the file LICENSE
|
|
|
|
*/
|
|
|
|
#ifndef _ESP_IOMUX_H
|
|
|
|
#define _ESP_IOMUX_H
|
2015-08-19 00:38:31 +00:00
|
|
|
#include "esp/types.h"
|
|
|
|
#include "esp/iomux_regs.h"
|
2015-06-05 01:46:25 +00:00
|
|
|
|
2015-10-05 07:33:32 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-06-05 01:46:25 +00:00
|
|
|
/**
|
|
|
|
* Convert a GPIO pin number to an iomux register index.
|
|
|
|
*
|
|
|
|
* This function should evaluate to a constant if the gpio_number is
|
|
|
|
* known at compile time, or return the result from a lookup table if not.
|
|
|
|
*
|
|
|
|
*/
|
2015-11-28 07:01:03 +00:00
|
|
|
uint8_t IRAM gpio_to_iomux(const uint8_t gpio_number);
|
2015-06-05 01:46:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an iomux register index to a GPIO pin number.
|
|
|
|
*
|
|
|
|
* This function should evaluate to a constant if the iomux_num is
|
|
|
|
* known at compile time, or return the result from a lookup table if not.
|
|
|
|
*
|
|
|
|
*/
|
2015-11-28 07:01:03 +00:00
|
|
|
uint8_t IRAM iomux_to_gpio(const uint8_t iomux_num);
|
2015-06-05 01:46:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directly get the IOMUX register for a particular gpio number
|
|
|
|
*
|
2015-08-19 00:38:31 +00:00
|
|
|
* ie *gpio_iomux_reg(3) is equivalent to IOMUX_GPIO3
|
2015-06-05 01:46:25 +00:00
|
|
|
*/
|
|
|
|
inline static esp_reg_t gpio_iomux_reg(const uint8_t gpio_number)
|
|
|
|
{
|
2015-08-19 00:38:31 +00:00
|
|
|
return &(IOMUX.PIN[gpio_to_iomux(gpio_number)]);
|
2015-06-05 01:46:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 07:10:35 +00:00
|
|
|
/**
|
|
|
|
* Set IOMUX function.
|
|
|
|
*
|
|
|
|
* @param iomux_num Index of IOMUX register. Can be converted from GPIO number
|
|
|
|
* with gpio_to_iomux.
|
|
|
|
* @param iomux_func GPIO function definition IOMUX_GPIOn_FUNC_*
|
|
|
|
*/
|
|
|
|
inline static void iomux_set_function(uint8_t iomux_num, uint32_t iomux_func)
|
2016-02-22 02:34:11 +00:00
|
|
|
{
|
|
|
|
uint32_t prev = IOMUX.PIN[iomux_num] & ~IOMUX_PIN_FUNC_MASK;
|
2016-08-16 07:10:35 +00:00
|
|
|
IOMUX.PIN[iomux_num] = iomux_func | prev;
|
2016-02-22 02:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline static void iomux_set_direction_flags(uint8_t iomux_num, uint32_t dir_flags)
|
|
|
|
{
|
|
|
|
uint32_t mask = IOMUX_PIN_OUTPUT_ENABLE | IOMUX_PIN_OUTPUT_ENABLE_SLEEP;
|
|
|
|
uint32_t prev = IOMUX.PIN[iomux_num] & ~mask;
|
|
|
|
IOMUX.PIN[iomux_num] = dir_flags | prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static void iomux_set_pullup_flags(uint8_t iomux_num, uint32_t pullup_flags)
|
|
|
|
{
|
|
|
|
uint32_t mask = IOMUX_PIN_PULLUP | IOMUX_PIN_PULLDOWN | IOMUX_PIN_PULLUP_SLEEP | IOMUX_PIN_PULLDOWN_SLEEP;
|
|
|
|
uint32_t prev = IOMUX.PIN[iomux_num] & ~mask;
|
|
|
|
IOMUX.PIN[iomux_num] = pullup_flags | prev;
|
|
|
|
}
|
|
|
|
|
2015-06-05 01:46:25 +00:00
|
|
|
/**
|
|
|
|
* Set a pin to the GPIO function.
|
|
|
|
*
|
|
|
|
* This allows you to set pins to GPIO without knowing in advance the
|
|
|
|
* exact register masks to use.
|
|
|
|
*
|
2016-02-22 02:34:11 +00:00
|
|
|
* Sets the function and direction, but leaves the pullup configuration the
|
|
|
|
* same as before.
|
2015-06-05 01:46:25 +00:00
|
|
|
*/
|
2016-02-22 02:34:11 +00:00
|
|
|
inline static void iomux_set_gpio_function(uint8_t gpio_number, bool output_enable)
|
2015-06-05 01:46:25 +00:00
|
|
|
{
|
2016-02-22 02:34:11 +00:00
|
|
|
const uint8_t iomux_num = gpio_to_iomux(gpio_number);
|
2016-08-15 10:58:10 +00:00
|
|
|
const uint32_t func = iomux_num > 11 ? IOMUX_FUNC(0) : IOMUX_FUNC(3);
|
2016-02-22 02:34:11 +00:00
|
|
|
iomux_set_function(iomux_num, func);
|
|
|
|
iomux_set_direction_flags(iomux_num, output_enable ? IOMUX_PIN_OUTPUT_ENABLE : 0);
|
2015-06-05 01:46:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:33:32 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-05 01:46:25 +00:00
|
|
|
#endif
|