example: gpio: add input usage

Signed-off-by: liangkangnan <liangkangnan@163.com>
pull/1/head
liangkangnan 2020-06-14 10:45:55 +08:00
parent 5c70814fc6
commit 6b01facc48
3 changed files with 11 additions and 3 deletions

View File

@ -1 +1 @@
通过一个GPIO来控制板上的LED以1Hz频率闪烁 两个GPIO一个输入一个输出。输出那个GPIO的输出电平等于输入那个GPIO的输入电平

View File

@ -6,8 +6,15 @@
int main() int main()
{ {
GPIO_REG(GPIO_CTRL) |= 0x1; // gpio0输出模式
GPIO_REG(GPIO_CTRL) |= 0x1 << 3; // gpio1输入模式
while (1) { while (1) {
GPIO_REG(GPIO_DATA) ^= 0x1; // 如果GPIO1输入高
busy_wait(500 * 1000); // delay 500ms if (GPIO_REG(GPIO_DATA) & 0x2)
GPIO_REG(GPIO_DATA) |= 0x1; // GPIO0输出高
// 如果GPIO1输入低
else
GPIO_REG(GPIO_DATA) &= ~0x1; // GPIO0输出低
} }
} }

View File

@ -2,6 +2,7 @@
#define _GPIO_H_ #define _GPIO_H_
#define GPIO_BASE (0x40000000) #define GPIO_BASE (0x40000000)
#define GPIO_CTRL (GPIO_BASE + (0x00))
#define GPIO_DATA (GPIO_BASE + (0x04)) #define GPIO_DATA (GPIO_BASE + (0x04))
#define GPIO_REG(addr) (*((volatile uint32_t *)addr)) #define GPIO_REG(addr) (*((volatile uint32_t *)addr))