Skip to content

Commit 1ea8c2f

Browse files
author
Espressif Systems
committed
1.add gpio/pin_mux/spi/timer/uart register define headers;
2.add gpio driver;
1 parent af801b0 commit 1ea8c2f

File tree

14 files changed

+1326
-390
lines changed

14 files changed

+1326
-390
lines changed

examples/dirver_lib/driver/Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
#############################################################
3+
# Required variables for each makefile
4+
# Discard this section from all parent makefiles
5+
# Expected variables (with automatic defaults):
6+
# CSRCS (all "C" files in the dir)
7+
# SUBDIRS (all subdirs with a Makefile)
8+
# GEN_LIBS - list of libs to be generated ()
9+
# GEN_IMAGES - list of images to be generated ()
10+
# COMPONENTS_xxx - a list of libs/objs in the form
11+
# subdir/lib to be extracted and rolled up into
12+
# a generated lib/image xxx.a ()
13+
#
14+
ifndef PDIR
15+
GEN_LIBS = libdriver.a
16+
endif
17+
18+
19+
#############################################################
20+
# Configuration i.e. compile options etc.
21+
# Target specific stuff (defines etc.) goes in here!
22+
# Generally values applying to a tree are captured in the
23+
# makefile at its root level - these are then overridden
24+
# for a subtree within the makefile rooted therein
25+
#
26+
#DEFINES +=
27+
28+
#############################################################
29+
# Recursion Magic - Don't touch this!!
30+
#
31+
# Each subtree potentially has an include directory
32+
# corresponding to the common APIs applicable to modules
33+
# rooted at that subtree. Accordingly, the INCLUDE PATH
34+
# of a module can only contain the include directories up
35+
# its parent path, and not its siblings
36+
#
37+
# Required for each makefile to inherit from the parent
38+
#
39+
40+
INCLUDES := $(INCLUDES) -I $(PDIR)include
41+
INCLUDES += -I ./
42+
PDIR := ../$(PDIR)
43+
sinclude $(PDIR)Makefile
44+

examples/dirver_lib/driver/gpio.c

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* Copyright (C) 2014 -2016 Espressif System
3+
*
4+
*/
5+
6+
#include "espressif/esp_common.h"
7+
#include "freertos/portmacro.h"
8+
9+
#include "gpio.h"
10+
11+
void ICACHE_FLASH_ATTR
12+
gpio_config(GPIO_ConfigTypeDef *pGPIOConfig)
13+
{
14+
uint16 gpio_pin_mask = pGPIOConfig->GPIO_Pin;
15+
uint32 io_reg;
16+
uint8 io_num = 0;
17+
uint32 pin_reg;
18+
19+
if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Input) {
20+
GPIO_AS_INPUT(gpio_pin_mask);
21+
} else if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Output) {
22+
GPIO_AS_OUTPUT(gpio_pin_mask);
23+
}
24+
25+
do {
26+
if ((gpio_pin_mask >> io_num) & 0x1) {
27+
io_reg = GPIO_PIN_REG(io_num);
28+
29+
if ((0x1 << io_num) & (GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_4 | GPIO_Pin_5)) {
30+
PIN_FUNC_SELECT(io_reg, 0);
31+
} else {
32+
PIN_FUNC_SELECT(io_reg, 3);
33+
}
34+
35+
if (pGPIOConfig->GPIO_Pullup) {
36+
PIN_PULLUP_EN(io_reg);
37+
} else {
38+
PIN_PULLUP_DIS(io_reg);
39+
}
40+
41+
if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Out_OD) {
42+
portENTER_CRITICAL();
43+
44+
pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(io_num));
45+
pin_reg &= (~GPIO_PIN_DRIVER_MASK);
46+
pin_reg |= (GPIO_PAD_DRIVER_ENABLE << GPIO_PIN_DRIVER_LSB);
47+
GPIO_REG_WRITE(GPIO_PIN_ADDR(io_num), pin_reg);
48+
49+
portEXIT_CRITICAL();
50+
} else if (pGPIOConfig->GPIO_Mode == GPIO_Mode_Sigma_Delta) {
51+
portENTER_CRITICAL();
52+
53+
pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(io_num));
54+
pin_reg &= (~GPIO_PIN_SOURCE_MASK);
55+
pin_reg |= (0x1 << GPIO_PIN_SOURCE_LSB);
56+
GPIO_REG_WRITE(GPIO_PIN_ADDR(io_num), pin_reg);
57+
GPIO_REG_WRITE(GPIO_SIGMA_DELTA_ADDRESS, SIGMA_DELTA_ENABLE);
58+
59+
portEXIT_CRITICAL();
60+
}
61+
62+
gpio_pin_intr_state_set(io_num, pGPIOConfig->GPIO_IntrType);
63+
}
64+
65+
io_num++;
66+
} while (io_num < 16);
67+
}
68+
69+
70+
/*
71+
* Change GPIO pin output by setting, clearing, or disabling pins.
72+
* In general, it is expected that a bit will be set in at most one
73+
* of these masks. If a bit is clear in all masks, the output state
74+
* remains unchanged.
75+
*
76+
* There is no particular ordering guaranteed; so if the order of
77+
* writes is significant, calling code should divide a single call
78+
* into multiple calls.
79+
*/
80+
void ICACHE_FLASH_ATTR
81+
gpio_output_conf(uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask)
82+
{
83+
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, set_mask);
84+
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clear_mask);
85+
GPIO_REG_WRITE(GPIO_ENABLE_W1TS_ADDRESS, enable_mask);
86+
GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, disable_mask);
87+
}
88+
89+
/*
90+
* Sample the value of GPIO input pins and returns a bitmask.
91+
*/
92+
uint32 ICACHE_FLASH_ATTR
93+
gpio_input_get(void)
94+
{
95+
return GPIO_REG_READ(GPIO_IN_ADDRESS);
96+
}
97+
98+
/*
99+
* Register an application-specific interrupt handler for GPIO pin
100+
* interrupts. Once the interrupt handler is called, it will not
101+
* be called again until after a call to gpio_intr_ack. Any GPIO
102+
* interrupts that occur during the interim are masked.
103+
*
104+
* The application-specific handler is called with a mask of
105+
* pending GPIO interrupts. After processing pin interrupts, the
106+
* application-specific handler may wish to use gpio_intr_pending
107+
* to check for any additional pending interrupts before it returns.
108+
*/
109+
void ICACHE_FLASH_ATTR
110+
gpio_intr_handler_register(void *fn)
111+
{
112+
_xt_isr_attach(ETS_GPIO_INUM, fn);
113+
}
114+
115+
/*
116+
only highlevel and lowlevel intr can use for wakeup
117+
*/
118+
void ICACHE_FLASH_ATTR
119+
gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state)
120+
{
121+
uint32 pin_reg;
122+
123+
if ((intr_state == GPIO_PIN_INTR_LOLEVEL) || (intr_state == GPIO_PIN_INTR_HILEVEL)) {
124+
portENTER_CRITICAL();
125+
126+
pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(i));
127+
pin_reg &= (~GPIO_PIN_INT_TYPE_MASK);
128+
pin_reg |= (intr_state << GPIO_PIN_INT_TYPE_LSB);
129+
pin_reg |= GPIO_PIN_WAKEUP_ENABLE_SET(GPIO_WAKEUP_ENABLE);
130+
GPIO_REG_WRITE(GPIO_PIN_ADDR(i), pin_reg);
131+
132+
portEXIT_CRITICAL();
133+
}
134+
}
135+
136+
void ICACHE_FLASH_ATTR
137+
gpio_pin_wakeup_disable(void)
138+
{
139+
uint8 i;
140+
uint32 pin_reg;
141+
142+
for (i = 0; i < GPIO_PIN_COUNT; i++) {
143+
pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(i));
144+
145+
if (pin_reg & GPIO_PIN_WAKEUP_ENABLE_MASK) {
146+
pin_reg &= (~GPIO_PIN_INT_TYPE_MASK);
147+
pin_reg |= (GPIO_PIN_INTR_DISABLE << GPIO_PIN_INT_TYPE_LSB);
148+
pin_reg &= ~(GPIO_PIN_WAKEUP_ENABLE_SET(GPIO_WAKEUP_ENABLE));
149+
GPIO_REG_WRITE(GPIO_PIN_ADDR(i), pin_reg);
150+
}
151+
}
152+
}
153+
154+
void ICACHE_FLASH_ATTR
155+
gpio_pin_intr_state_set(uint32 i, GPIO_INT_TYPE intr_state)
156+
{
157+
uint32 pin_reg;
158+
159+
portENTER_CRITICAL();
160+
161+
pin_reg = GPIO_REG_READ(GPIO_PIN_ADDR(i));
162+
pin_reg &= (~GPIO_PIN_INT_TYPE_MASK);
163+
pin_reg |= (intr_state << GPIO_PIN_INT_TYPE_LSB);
164+
GPIO_REG_WRITE(GPIO_PIN_ADDR(i), pin_reg);
165+
166+
portEXIT_CRITICAL();
167+
}
168+
169+
void ICACHE_FLASH_ATTR
170+
gpio16_output_conf(void)
171+
{
172+
WRITE_PERI_REG(PAD_XPD_DCDC_CONF,
173+
(READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); // mux configuration for XPD_DCDC to output rtc_gpio0
174+
175+
WRITE_PERI_REG(RTC_GPIO_CONF,
176+
(READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); //mux configuration for out enable
177+
178+
WRITE_PERI_REG(RTC_GPIO_ENABLE,
179+
(READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe) | (uint32)0x1); //out enable
180+
}
181+
182+
void ICACHE_FLASH_ATTR
183+
gpio16_output_set(uint8 value)
184+
{
185+
WRITE_PERI_REG(RTC_GPIO_OUT,
186+
(READ_PERI_REG(RTC_GPIO_OUT) & (uint32)0xfffffffe) | (uint32)(value & 1));
187+
}
188+
189+
void ICACHE_FLASH_ATTR
190+
gpio16_input_conf(void)
191+
{
192+
WRITE_PERI_REG(PAD_XPD_DCDC_CONF,
193+
(READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | (uint32)0x1); // mux configuration for XPD_DCDC and rtc_gpio0 connection
194+
195+
WRITE_PERI_REG(RTC_GPIO_CONF,
196+
(READ_PERI_REG(RTC_GPIO_CONF) & (uint32)0xfffffffe) | (uint32)0x0); //mux configuration for out enable
197+
198+
WRITE_PERI_REG(RTC_GPIO_ENABLE,
199+
READ_PERI_REG(RTC_GPIO_ENABLE) & (uint32)0xfffffffe); //out disable
200+
}
201+
202+
uint8 ICACHE_FLASH_ATTR
203+
gpio16_input_get(void)
204+
{
205+
return (uint8)(READ_PERI_REG(RTC_GPIO_IN_DATA) & 1);
206+
}
207+

examples/dirver_lib/include/gpio.h

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (C) 2014 -2016 Espressif System
3+
*
4+
*/
5+
6+
#ifndef __GPIO_H__
7+
#define __GPIO_H__
8+
9+
#define GPIO_Pin_0 (BIT(0)) /* Pin 0 selected */
10+
#define GPIO_Pin_1 (BIT(1)) /* Pin 1 selected */
11+
#define GPIO_Pin_2 (BIT(2)) /* Pin 2 selected */
12+
#define GPIO_Pin_3 (BIT(3)) /* Pin 3 selected */
13+
#define GPIO_Pin_4 (BIT(4)) /* Pin 4 selected */
14+
#define GPIO_Pin_5 (BIT(5)) /* Pin 5 selected */
15+
#define GPIO_Pin_6 (BIT(6)) /* Pin 6 selected */
16+
#define GPIO_Pin_7 (BIT(7)) /* Pin 7 selected */
17+
#define GPIO_Pin_8 (BIT(8)) /* Pin 8 selected */
18+
#define GPIO_Pin_9 (BIT(9)) /* Pin 9 selected */
19+
#define GPIO_Pin_10 (BIT(10)) /* Pin 10 selected */
20+
#define GPIO_Pin_11 (BIT(11)) /* Pin 11 selected */
21+
#define GPIO_Pin_12 (BIT(12)) /* Pin 12 selected */
22+
#define GPIO_Pin_13 (BIT(13)) /* Pin 13 selected */
23+
#define GPIO_Pin_14 (BIT(14)) /* Pin 14 selected */
24+
#define GPIO_Pin_15 (BIT(15)) /* Pin 15 selected */
25+
#define GPIO_Pin_All (0xFFFF) /* All pins selected */
26+
27+
#define GPIO_PIN_REG_0 PERIPHS_IO_MUX_GPIO0_U
28+
#define GPIO_PIN_REG_1 PERIPHS_IO_MUX_U0TXD_U
29+
#define GPIO_PIN_REG_2 PERIPHS_IO_MUX_GPIO2_U
30+
#define GPIO_PIN_REG_3 PERIPHS_IO_MUX_U0RXD_U
31+
#define GPIO_PIN_REG_4 PERIPHS_IO_MUX_GPIO4_U
32+
#define GPIO_PIN_REG_5 PERIPHS_IO_MUX_GPIO5_U
33+
#define GPIO_PIN_REG_6 PERIPHS_IO_MUX_SD_CLK_U
34+
#define GPIO_PIN_REG_7 PERIPHS_IO_MUX_SD_DATA0_U
35+
#define GPIO_PIN_REG_8 PERIPHS_IO_MUX_SD_DATA1_U
36+
#define GPIO_PIN_REG_9 PERIPHS_IO_MUX_SD_DATA2_U
37+
#define GPIO_PIN_REG_10 PERIPHS_IO_MUX_SD_DATA3_U
38+
#define GPIO_PIN_REG_11 PERIPHS_IO_MUX_SD_CMD_U
39+
#define GPIO_PIN_REG_12 PERIPHS_IO_MUX_MTDI_U
40+
#define GPIO_PIN_REG_13 PERIPHS_IO_MUX_MTCK_U
41+
#define GPIO_PIN_REG_14 PERIPHS_IO_MUX_MTMS_U
42+
#define GPIO_PIN_REG_15 PERIPHS_IO_MUX_MTDO_U
43+
44+
#define GPIO_PIN_REG(i) \
45+
(i==0) ? GPIO_PIN_REG_0: \
46+
(i==1) ? GPIO_PIN_REG_1: \
47+
(i==2) ? GPIO_PIN_REG_2: \
48+
(i==3) ? GPIO_PIN_REG_3: \
49+
(i==4) ? GPIO_PIN_REG_4: \
50+
(i==5) ? GPIO_PIN_REG_5: \
51+
(i==6) ? GPIO_PIN_REG_6: \
52+
(i==7) ? GPIO_PIN_REG_7: \
53+
(i==8) ? GPIO_PIN_REG_8: \
54+
(i==9) ? GPIO_PIN_REG_9: \
55+
(i==10)? GPIO_PIN_REG_10: \
56+
(i==11)? GPIO_PIN_REG_11: \
57+
(i==12)? GPIO_PIN_REG_12: \
58+
(i==13)? GPIO_PIN_REG_13: \
59+
(i==14)? GPIO_PIN_REG_14: \
60+
GPIO_PIN_REG_15
61+
62+
#define GPIO_PIN_ADDR(i) (GPIO_PIN0_ADDRESS + i*4)
63+
64+
#define GPIO_ID_IS_PIN_REGISTER(reg_id) \
65+
((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1)))
66+
67+
#define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0)
68+
69+
typedef enum {
70+
GPIO_PIN_INTR_DISABLE = 0,
71+
GPIO_PIN_INTR_POSEDGE = 1,
72+
GPIO_PIN_INTR_NEGEDGE = 2,
73+
GPIO_PIN_INTR_ANYEGDE = 3,
74+
GPIO_PIN_INTR_LOLEVEL = 4,
75+
GPIO_PIN_INTR_HILEVEL = 5
76+
} GPIO_INT_TYPE;
77+
78+
typedef enum {
79+
GPIO_Mode_Input = 0x0,
80+
GPIO_Mode_Out_OD,
81+
GPIO_Mode_Output ,
82+
GPIO_Mode_Sigma_Delta ,
83+
} GPIOMode_TypeDef;
84+
85+
typedef enum {
86+
GPIO_PullUp_DIS = 0x0,
87+
GPIO_PullUp_EN = 0x1,
88+
} GPIO_Pullup_IF;
89+
90+
typedef struct {
91+
uint16 GPIO_Pin;
92+
GPIOMode_TypeDef GPIO_Mode;
93+
GPIO_Pullup_IF GPIO_Pullup;
94+
GPIO_INT_TYPE GPIO_IntrType;
95+
} GPIO_ConfigTypeDef;
96+
97+
#define GPIO_OUTPUT_SET(gpio_no, bit_value) \
98+
gpio_output_conf(bit_value<<gpio_no, ((~bit_value)&0x01)<<gpio_no, 1<<gpio_no, 0)
99+
100+
#define GPIO_OUTPUT(gpio_bits, bit_value) \
101+
if(bit_value) gpio_output_conf(gpio_bits, 0, gpio_bits, 0);\
102+
else gpio_output_conf(0, gpio_bits, gpio_bits, 0)
103+
104+
#define GPIO_DIS_OUTPUT(gpio_no) gpio_output_conf(0, 0, 0, 1<<gpio_no)
105+
#define GPIO_AS_INPUT(gpio_bits) gpio_output_conf(0, 0, 0, gpio_bits)
106+
#define GPIO_AS_OUTPUT(gpio_bits) gpio_output_conf(0, 0, gpio_bits, 0)
107+
#define GPIO_INPUT_GET(gpio_no) ((gpio_input_get()>>gpio_no)&BIT0)
108+
109+
void gpio16_output_conf(void);
110+
void gpio16_output_set(uint8 value);
111+
void gpio16_input_conf(void);
112+
uint8 gpio16_input_get(void);
113+
114+
void gpio_output_conf(uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask);
115+
void gpio_intr_handler_register(void *fn);
116+
void gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state);
117+
void gpio_pin_wakeup_disable();
118+
void gpio_pin_intr_state_set(uint32 i, GPIO_INT_TYPE intr_state);
119+
uint32 gpio_input_get(void);
120+
121+
#endif
File renamed without changes.

0 commit comments

Comments
 (0)