-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled.cpp
More file actions
224 lines (177 loc) · 5.58 KB
/
Copy pathled.cpp
File metadata and controls
224 lines (177 loc) · 5.58 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdint.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rtc.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <string>
#define USART_CONSOLE USART1
extern "C"{
/**
* Use USART_CONSOLE as a console.
* This is a syscall for newlib
* @param file
* @param ptr
* @param len
* @return
*/
int _write(int file, char *ptr, int len)
{
int i;
for (i = 0; i < len; i++) {
usart_send_blocking(USART_CONSOLE, ptr[i]);
}
return i;
}
}
/* milliseconds since boot */
volatile uint32_t system_millis;
/* Called when systick fires */
void sys_tick_handler(void)
{
system_millis++;
}
/* simple sleep for delay milliseconds */
void msleep(uint32_t delay)
{
uint32_t wake = system_millis + delay;
while (wake > system_millis);
}
/* Getter function for the current time */
uint32_t mtime(void)
{
return system_millis;
}
void adc_setup(void)
{
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO0);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO1);
/* Make sure the ADC doesn't run during config. */
adc_power_off(ADC1);
/* We configure everything for one single conversion. */
adc_disable_scan_mode(ADC1);
adc_set_single_conversion_mode(ADC1);
adc_disable_external_trigger_regular(ADC1);
adc_set_right_aligned(ADC1);
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
adc_power_on(ADC1);
/* Wait for ADC starting up. */
int i;
for (i = 0; i < 800000; i++) /* Wait a bit. */
__asm__("nop");
adc_reset_calibration(ADC1);
adc_calibrate(ADC1);
}
uint16_t read_adc_naiive(uint8_t channel)
{
uint8_t channel_array[16];
channel_array[0] = channel;
adc_set_regular_sequence(ADC1, 1, channel_array);
adc_start_conversion_direct(ADC1);
while (!adc_eoc(ADC1));
uint16_t reg16 = adc_read_regular(ADC1);
return reg16;
}
/* Set STM32 to 24 MHz. */
void clock_setup(void)
{
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_ADC1);
rcc_periph_clock_enable(RCC_USART1);
rcc_clock_setup_in_hsi_out_48mhz();
/* clock rate / 48000 to get 1mS interrupt rate */
systick_set_reload(48000); //24MHz / 1000 -> 24000
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_counter_enable();
/* this done last */
systick_interrupt_enable();
}
void gpio_setup(void)
{
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL , GPIO1);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO13);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN , GPIO14);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
usart_set_baudrate(USART1, 38400);
usart_set_databits(USART1, 8);
usart_set_stopbits(USART1, USART_STOPBITS_1);
usart_set_mode(USART1, USART_MODE_TX);
usart_set_parity(USART1, USART_PARITY_NONE);
usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
/* Finally enable the USART. */
usart_enable(USART1);
}
void user_button_enable(void)
{
/*Enable GPIOA clock */
//rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
/*set GPIOA0 as input open-drain */
//gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO13);
}
void pwm_setup()
{
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_TIM1);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM1_CH1);
rcc_periph_clock_enable(RCC_TIM1);
timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP);
timer_reset(TIM1);
// Divide counter by 64 to scale it down from 48MHz clock speed to a 750000Hz rate.
timer_set_prescaler(TIM1, 64);
// Set timer period by solving:
// PWM frequency = timer clock speed / timer period
timer_set_period(TIM1, 750000/50); //50Hz
timer_enable_break_main_output(TIM1); // Must be called for advanced timers
// like this one. Unclear what this
// does or why it's necessary but the
// libopencm3 timer and STM32 docs
// mention it.
timer_enable_oc_output(TIM1, TIM_OC1);
// Set the PWM mode to 2 which means the PWM signal is low when
// the timer value is below the threshold and high above it.
timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1);
// 1ms (-90º) -> 5% de 20ms, 1.5ms (0º) -> 7.5% , 2ms (90) -> 10%
timer_set_oc_value(TIM1, TIM_OC1, (750000 * 0.1) /50);
timer_enable_preload(TIM1);
timer_enable_counter(TIM1)
}
int main( void )
{
clock_setup();
gpio_setup();
user_button_enable();
adc_setup();
std::string s;
double i = 0.025f;
bool inc = true;
while(1)
{
uint16_t w = (750000 * i)/50.0f;
timer_set_oc_value(TIM1, TIM_OC1, w );
gpio_toggle(GPIOB, GPIO1);
uint16_t adcValue = read_adc_naiive(0);
s = std::to_string(adcValue);
printf("%d\n",adcValue);
if(inc)
{
i = i + 0.001;
}else
{
i = i - 0.001;
}
if (i > 0.10)
{
inc = false;
}
if (i < 0.025)
{
inc = true;
}
msleep(1000);
}
return 0;
}