Skip to content

Commit 1123768

Browse files
agnerskarlp
authored andcommitted
vf6xx: add GPIO example
This example muxes two pins as GPIO and enables one GPIO (logical high). The other GPIO is then read in the main loop. One can use a jumper to bridge the two GPIOs to see that also reading succeeds.
1 parent 0b9e7d0 commit 1123768

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
##
2+
## This file is part of the libopencm3 project.
3+
##
4+
## Copyright (C) 2009 Uwe Hermann <[email protected]>
5+
## Copyright (C) 2014 Stefan Agner <[email protected]>
6+
##
7+
## This library is free software: you can redistribute it and/or modify
8+
## it under the terms of the GNU Lesser General Public License as published by
9+
## the Free Software Foundation, either version 3 of the License, or
10+
## (at your option) any later version.
11+
##
12+
## This library is distributed in the hope that it will be useful,
13+
## but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
## GNU Lesser General Public License for more details.
16+
##
17+
## You should have received a copy of the GNU Lesser General Public License
18+
## along with this library. If not, see <http://www.gnu.org/licenses/>.
19+
##
20+
21+
BINARY = gpio
22+
23+
LDSCRIPT = ../colibri-vf61.ld
24+
25+
include ../Makefile.include
26+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This file is part of the libopencm3 project.
3+
*
4+
* Copyright (C) 2009 Uwe Hermann <[email protected]>
5+
* Copyright (C) 2014 Stefan Agner <[email protected]>
6+
*
7+
* This library is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#include <stdio.h>
22+
#include <errno.h>
23+
#include <libopencm3/cm3/common.h>
24+
#include <libopencm3/vf6xx/ccm.h>
25+
#include <libopencm3/vf6xx/uart.h>
26+
#include <libopencm3/vf6xx/gpio.h>
27+
#include <libopencm3/vf6xx/iomuxc.h>
28+
29+
30+
static const uint32_t gpio_out = IOMUXC_PAD(MUX_MODE_ALT0, SPEED_LOW, \
31+
DSE_150OHM, PUS_PU_100KOHM, IOMUXC_OBE | IOMUXC_IBE);
32+
33+
static const uint32_t gpio_in = IOMUXC_PAD(MUX_MODE_ALT0, SPEED_LOW, \
34+
DSE_150OHM, PUS_PU_100KOHM, IOMUXC_IBE);
35+
36+
int _write(int file, char *ptr, int len);
37+
void sys_tick_handler(void);
38+
39+
int _write(int file, char *ptr, int len)
40+
{
41+
int i;
42+
43+
if (file == 1) {
44+
for (i = 0; i < len; i++)
45+
uart_send_blocking(UART2, ptr[i]);
46+
return i;
47+
}
48+
49+
errno = EIO;
50+
return -1;
51+
}
52+
53+
static void uart_init()
54+
{
55+
ccm_clock_gate_enable(CG9_UART2);
56+
uart_enable(UART2);
57+
uart_set_baudrate(UART2, 115200);
58+
}
59+
60+
int main(void)
61+
{
62+
int old_state = -1;
63+
int new_state = 0;
64+
65+
66+
/* Init Clock Control and UART */
67+
ccm_calculate_clocks();
68+
uart_init();
69+
70+
/* Iris Carrier Board, X16, Pin 15 */
71+
iomuxc_mux(PTC3, gpio_out);
72+
73+
/* Iris Carrier Board, X16, Pin 16 */
74+
iomuxc_mux(PTC2, gpio_in);
75+
76+
gpio_set(PTC3);
77+
printf("GPIO %d is %s\r\n", PTC3, gpio_get(PTC3) ? "high" : "low");
78+
79+
/*
80+
* Read input GPIO in a loop and print changes
81+
* A jumper between PTC3<=> PTC2 can be used to set PTC2 to high
82+
*/
83+
while (1) {
84+
new_state = gpio_get(PTC2);
85+
if (old_state != new_state) {
86+
printf("GPIO %d is %s\r\n", PTC2,
87+
new_state ? "high" : "low");
88+
old_state = new_state;
89+
}
90+
}
91+
92+
93+
return 0;
94+
}
95+

0 commit comments

Comments
 (0)