-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
159 lines (135 loc) · 3.25 KB
/
main.c
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
149
150
151
152
153
154
155
156
157
158
159
/**
* @file main.c
* @brief ADC interrupt example for LibOpenCM3 with STM32.
* @author ZiTe ([email protected])
* @copyright MIT License
*/
#include "main.h"
int main(void)
{
rcc_setup();
adc_setup();
usart_setup();
printf("ADC Interrupt.\r\n");
/* Software start the first conversion. */
#if defined(STM32F1)
adc_start_conversion_direct(ADC1);
#else
adc_start_conversion_regular(ADC1);
#endif
/* Halt. */
while (1)
{
}
return 0;
}
static void rcc_setup(void)
{
#if defined(STM32F1)
rcc_clock_setup_in_hse_8mhz_out_72mhz();
#elif defined(STM32F4)
rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
#endif
rcc_periph_clock_enable(RCC_USART_TX_GPIO);
rcc_periph_clock_enable(RCC_USART2);
rcc_periph_clock_enable(RCC_ADC_GPIO);
rcc_periph_clock_enable(RCC_ADC1);
}
static void adc_setup(void)
{
/* Set to input analog. */
#if defined(STM32F1)
gpio_set_mode(GPIO_ADC_PORT,
GPIO_MODE_INPUT,
GPIO_CNF_INPUT_ANALOG,
GPIO_ADC_IN0_PIN);
#else
gpio_mode_setup(GPIO_ADC_PORT,
GPIO_MODE_ANALOG,
GPIO_PUPD_NONE,
GPIO_ADC_IN0_PIN);
#endif
/* Setup ADC. */
adc_power_off(ADC1);
adc_disable_scan_mode(ADC1);
adc_disable_external_trigger_regular(ADC1);
adc_set_single_conversion_mode(ADC1);
adc_set_right_aligned(ADC1);
adc_set_sample_time_on_all_channels(ADC1, ADC_SIMPLE_TIME);
/* Setup interrupt. */
adc_enable_eoc_interrupt(ADC1);
nvic_enable_irq(ADC_IRQ);
uint8_t channels[16];
channels[0] = 0;
adc_set_regular_sequence(ADC1, 1, channels);
adc_power_on(ADC1);
delay(800000); /* Wait a bit. */
#if defined(STM32F1)
adc_reset_calibration(ADC1);
adc_calibrate(ADC1);
#endif
}
static void usart_setup(void)
{
/* Set USART-Tx pin to alternate function. */
#if defined(NUCLEO_F103RB)
gpio_set_mode(GPIO_USART_TX_PORT,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
GPIO_USART_TX_PIN);
#else
gpio_mode_setup(GPIO_USART_TX_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_USART_TX_PIN);
gpio_set_af(GPIO_USART_TX_PORT, GPIO_USART_AF, GPIO_USART_TX_PIN);
#endif
/* Config USART params. */
usart_set_baudrate(USART2, USART_BAUDRATE);
usart_set_databits(USART2, 8);
usart_set_stopbits(USART2, USART_STOPBITS_1);
usart_set_parity(USART2, USART_PARITY_NONE);
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
usart_set_mode(USART2, USART_MODE_TX);
usart_enable(USART2);
}
static void delay(uint32_t value)
{
for (uint32_t i = 0; i < value; i++)
{
__asm__("nop"); /* Do nothing. */
}
}
/* For printf(). */
int _write(int file, char *ptr, int len)
{
int i;
if (file == 1)
{
for (i = 0; i < len; i++)
{
usart_send_blocking(USART2, ptr[i]);
}
return i;
}
errno = EIO;
return -1;
}
/**
* @brief ADC Interrupt service routine.
*/
#if defined(STM32F1)
void adc1_2_isr(void)
#else
void adc_isr(void)
#endif
{
/* Clear regular end of conversion flag. */
ADC_SR(ADC1) &= ~ADC_SR_EOC;
uint16_t value = adc_read_regular(ADC1);
printf("%4d\r\n", value);
delay(5000000);
/* Sart a new conversion. */
#if defined(STM32F1)
adc_start_conversion_direct(ADC1);
#else
adc_start_conversion_regular(ADC1);
#endif
}