aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32/f1/lisa-m-2/fancyblink/fancyblink.c
blob: 72994b285ebd2c81ad7cf07657cf972235013382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
 * Copyright (C) 2011 Piotr Esden-Tempski <piotr@esden.net>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/stm32/f1/gpio.h>

/* Set STM32 to 72 MHz. */
static void clock_setup(void)
{
	rcc_clock_setup_in_hse_12mhz_out_72mhz();

	/* Enable GPIOA, GPIOB, GPIOC, and AFIO clocks. */
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
}

static void gpio_setup(void)
{
	/* LED1 */
	/* Set GPIO8 (in GPIO port A) to 'output push-pull'. */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);

	/* LED2 */
	/* Set GPIO15 (in GPIO port C) to 'output push-pull'. */
	gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);

	/* JTAG_TRST */
	/* Set GPIO4 (in GPIO port B) to 'output push-pull'. */
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_PUSHPULL, GPIO4);

	AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST;

	/* ADC4 */
	/* Set GPIO5 (in GPIO port C) to 'output push-pull'. */
	gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);

	/* ADC6 */
	/* Set GPIO2 (in GPIO port C) to 'output push-pull'. */
	gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);

	/* Preconfigure the LEDs. */
	gpio_set(GPIOA, GPIO8);
	gpio_set(GPIOC, GPIO15);
	gpio_set(GPIOB, GPIO4);
	gpio_set(GPIOC, GPIO5);
	gpio_set(GPIOC, GPIO2);
}

static void led_set(int id, int on)
{
	if (on) {
		switch (id) {
			case 0:
				gpio_clear(GPIOA, GPIO8); /* LED1 On */
				break;
			case 1:
				gpio_clear(GPIOB, GPIO4); /* JTAG_TRST On */
				break;
			case 2:
				gpio_clear(GPIOC, GPIO2); /* ADC6 On */
				break;
			case 3:
				gpio_clear(GPIOC, GPIO5); /* ADC4 On */
				break;
			case 4:
				gpio_clear(GPIOC, GPIO15); /* LED2 On */
				break;
		}
	} else {
		switch (id) {
			case 0:
				gpio_set(GPIOA, GPIO8); /* LED1 On */
				break;
			case 1:
				gpio_set(GPIOB, GPIO4); /* JTAG_TRST On */
				break;
			case 2:
				gpio_set(GPIOC, GPIO2); /* ADC6 On */
				break;
			case 3:
				gpio_set(GPIOC, GPIO5); /* ADC4 On */
				break;
			case 4:
				gpio_set(GPIOC, GPIO15); /* LED2 On */
				break;
		}
	}
}

static void led_advance(void)
{
	static int state = 0;

	if (state < 5) {
		led_set(state, 1);
	} else if (state < 10) {
		led_set(state - 5, 0);
	} else if (state < 15) {
		led_set(14 - state, 1);
	} else if (state < 20) {
		led_set(19 - state, 0);
	}

	state++;
	if(state == 20) state = 0;

}

int main(void)
{
	int i;

	clock_setup();
	gpio_setup();

	/* Blink the LEDs (PC13 and PB4) on the board. */
	while (1) {
		led_advance();
		for (i = 0; i < 800000; i++)	/* Wait a bit. */
			__asm__("nop");
	}

	return 0;
}