-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.c
246 lines (214 loc) · 6.11 KB
/
utils.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "utils.h"
//void rtc_setup(void) {
// // Based on http://www.stm32.eu/node/97 and AN2821
//
// // Enable PWR and BKP clocks
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
// // Enable access to BKP
// PWR_BackupAccessCmd(ENABLE);
// // BKP de-init
// BKP_DeInit();
// // Start editing RTC configuration
// RTC_EnterConfigMode();
// // Enable LSE
// RCC_LSEConfig(RCC_LSE_ON); // RCC_LSICmd(ENABLE);
// // Wait for LSE
// while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} // while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {}
// // RTCCLK = LSE = 32.768 kHz
// RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
// // Turn on RTC clock
// RCC_RTCCLKCmd(ENABLE);
// // Wait for synchronization
// RTC_WaitForSynchro();
// // Wait until operation finish
// RTC_WaitForLastTask();
// // Enable RTC second
// RTC_ITConfig(RTC_IT_SEC, ENABLE);
// RTC_WaitForLastTask();
// // Set Prescaler to 32768 ticks
// // RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1)
// RTC_SetPrescaler(32767);
// RTC_WaitForLastTask();
// // Exit configuration editing mode
// RTC_ExitConfigMode();
// // Set RTC counter (time) to 12:00:00
// RTC_SetCounter(12 * 60 * 60);
//
// // Disable default Tamper Pin
// BKP_TamperPinCmd(DISABLE);
//
// // Enable calibration data on Tamper Pin
//// BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
//
// // Enable seconds indicator on Tamper Pin
// BKP_RTCOutputConfig(BKP_RTCOutputSource_Second);
//
// // Enable RTC interrupts
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
//
// // Interrupt every 1 second
// RTC_ITConfig(RTC_IT_SEC, ENABLE);
// RTC_WaitForLastTask();
//}
void led_init(void) {
// Enable GPIO clock
if (LED_PORT == GPIOA) {
rcc_periph_clock_enable(RCC_GPIOA);
} else if (LED_PORT == GPIOB) {
rcc_periph_clock_enable(RCC_GPIOB);
} else if (LED_PORT == GPIOC) {
rcc_periph_clock_enable(RCC_GPIOC);
}
#ifdef STM32F1
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
LED_PIN);
#elif STM32F0
gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN);
#endif
gpio_clear(LED_PORT, LED_PIN);
}
void led_toggle() {
// Toggles LED state on dev board
gpio_toggle(LED_PORT, LED_PIN);
}
void led_set(bool new_state) {
if (new_state) {
gpio_set(LED_PORT, LED_PIN);
} else {
gpio_clear(LED_PORT, LED_PIN);
}
}
void hacf(void) {
led_set(0);
while (1) {
delay_ms(30);
led_toggle();
delay_ms(300);
led_toggle();
}
}
// Saturated add functions for 8 / 16 / 32 unsigned integers
inline uint8_t sadd8(uint8_t a, uint8_t b) {
return (a > 0xFF - b) ? 0xFF : a + b;
}
inline uint16_t sadd16(uint16_t a, uint16_t b) {
return (a > 0xFFFF - b) ? 0xFFFF : a + b;
}
inline uint32_t sadd32(uint32_t a, uint32_t b) {
return (a > 0xFFFFFFFF - b) ? 0xFFFFFFFF : a + b;
}
inline uint8_t check_bit(uint32_t variable, uint8_t pos) {
return (uint8_t)((variable >> pos) & 1u);
}
inline uint32_t toggle_bit(uint32_t variable, uint8_t pos) {
return variable ^ (1u << pos);
}
volatile uint32_t system_millis;
uint8_t system_precision;
void systick_setup(uint8_t precision) {
system_precision = precision;
// clock rate / 100 to get 10 ms interrupt rate
systick_set_reload(
(uint32_t)(rcc_ahb_frequency / fast_int_pow(10, (1 + precision))));
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_counter_enable();
systick_interrupt_enable();
}
// Due to some technical reasons, this function is rounded to 10 ms
void delay_ms(uint32_t time) {
uint32_t wake = system_millis + time;
while (wake > system_millis)
;
}
inline uint32_t get_system_millis(void) { return system_millis; }
// For quick calculation of power when base and exponent are integers
// https://stackoverflow.com/a/101613
int32_t fast_int_pow(int32_t base, uint32_t exponent) {
int32_t result = 1;
while (1) {
if (exponent & 1) {
result *= base;
}
exponent >>= 1;
if (!exponent) {
break;
}
base *= base;
}
return result;
}
void sys_tick_handler(void) {
system_millis += fast_int_pow(10, 2u - system_precision);
}
void setup_delay_timer(uint32_t timer) {
enum rcc_periph_clken rcc_tim;
switch (timer) {
case TIM1:
rcc_tim = RCC_TIM1;
break;
case TIM2:
rcc_tim = RCC_TIM2;
break;
case TIM3:
rcc_tim = RCC_TIM3;
break;
default:
hacf();
return;
}
rcc_periph_clock_enable(rcc_tim);
// microsecond counter
timer_set_prescaler(timer, (rcc_ahb_frequency / 2) / 1e6 - 1);
timer_set_period(timer, 0xffff);
timer_one_shot_mode(timer);
}
void delay_us(uint32_t timer, uint16_t delay) {
TIM_ARR(timer) = ssub(delay - 1, 3) + 1;
TIM_EGR(timer) = TIM_EGR_UG;
TIM_CR1(timer) |= TIM_CR1_CEN;
// timer_enable_counter;
while (TIM_CR1(timer) & TIM_CR1_CEN)
;
}
void trace_init(void) {
// enable the use DWT
if (!dwt_enable_cycle_counter()) {
hacf();
}
}
// Initialize structure used for debouncing
error_t debounce_init(debounce *config, const pin *_pin, uint16_t threshold) {
if (!config || !_pin) {
return E_NULL_PTR;
}
config->cnt = 0;
config->pin = *_pin;
config->state = 0;
config->known_state = 0;
config->threshold = threshold;
return E_SUCCESS;
}
// When this function is called, value from pin is read and, if threshold is
// reached, state of structure is updated.
error_t debounce_get_state(debounce *config) {
if (!config) {
return E_NULL_PTR;
}
bool new_state = gpio_get(config->pin.port, config->pin.gpio);
if (new_state != config->state) {
config->cnt++;
if (config->cnt > config->threshold) {
config->state = new_state;
config->cnt = 0;
}
} else {
config->cnt = 0;
}
return E_SUCCESS;
}