-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal.h
219 lines (194 loc) · 4.13 KB
/
hal.h
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
#ifndef HAL_H
#define HAL_H
#ifdef TARGET_HUGS
#include "hal_hugs.h"
#endif
#ifdef TARGET_ARDUINO
#include "hal_arduino.h"
#endif
#include <util/atomic.h>
void hal_pwm_timer_setup();
//
//
// the below must be included here so the compiler can inline
//
//
inline void hal_x_set_ocr(uint16_t duty)
{
HAL_PWM_X_MATCH = duty;
}
inline void hal_y_set_ocr(uint16_t duty)
{
HAL_PWM_Y_MATCH = duty;
}
inline void hal_dead_time()
{
__asm("add r0, 1");
__asm("add r0, 1");
__asm("add r0, 1");
__asm("add r0, 1");
__asm("add r0, 1");
}
inline void hal_set_pin(volatile uint8_t* port, uint8_t mask)
{
// this will compile to an atomic (sbi) if port and mask are constant
*port |= mask;
}
inline void hal_clear_pin(volatile uint8_t* port, uint8_t mask)
{
// this one too (cbi)
*port &= ~mask;
}
inline void hal_toggle_pin(volatile uint8_t* port, uint8_t mask)
{
// sorry, no atomic xor for io registers
*port ^= mask;
}
inline void hal_toggle_pin_atomic(volatile uint8_t* port, uint8_t mask)
{
// RMW is not interrupt-safe
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
hal_toggle_pin(port, mask);
}
}
inline void hal_pwm_x_enable()
{
HAL_TIMSK1 |= HAL_PWM_X_EN;
}
inline void hal_pwm_x_disable()
{
HAL_TIMSK1 &= ~HAL_PWM_X_EN;
}
inline void hal_pwm_y_enable()
{
HAL_TIMSK1 |= HAL_PWM_Y_EN;
}
inline void hal_pwm_y_disable()
{
HAL_TIMSK1 &= ~HAL_PWM_Y_EN;
}
inline void hal_a_low()
{
#ifdef HAL_INVERTp
hal_set_pin(&HAL_Ap_PORT, HAL_Ap_PIN);
#else
hal_clear_pin(&HAL_Ap_PORT, HAL_Ap_PIN);
#endif
hal_dead_time();
#ifdef HAL_INVERTn
hal_clear_pin(&HAL_An_PORT, HAL_An_PIN);
#else
hal_set_pin(&HAL_An_PORT, HAL_An_PIN);
#endif
}
inline void hal_a_high()
{
#ifdef HAL_INVERTn
hal_set_pin(&HAL_An_PORT, HAL_An_PIN);
#else
hal_clear_pin(&HAL_An_PORT, HAL_An_PIN);
#endif
hal_dead_time();
#ifdef HAL_INVERTp
hal_clear_pin(&HAL_Ap_PORT, HAL_Ap_PIN);
#else
hal_set_pin(&HAL_Ap_PORT, HAL_Ap_PIN);
#endif
}
inline void hal_a_tristate()
{
#ifdef HAL_INVERTn
hal_set_pin(&HAL_An_PORT, HAL_An_PIN);
#else
hal_clear_pin(&HAL_An_PORT, HAL_An_PIN);
#endif
#ifdef HAL_INVERTp
hal_set_pin(&HAL_Ap_PORT, HAL_Ap_PIN);
#else
hal_clear_pin(&HAL_Ap_PORT, HAL_Ap_PIN);
#endif
}
inline void hal_b_low()
{
#ifdef HAL_INVERTp
hal_set_pin(&HAL_Bp_PORT, HAL_Bp_PIN);
#else
hal_clear_pin(&HAL_Bp_PORT, HAL_Bp_PIN);
#endif
hal_dead_time();
#ifdef HAL_INVERTn
hal_clear_pin(&HAL_Bn_PORT, HAL_Bn_PIN);
#else
hal_set_pin(&HAL_Bn_PORT, HAL_Bn_PIN);
#endif
}
inline void hal_b_high()
{
#ifdef HAL_INVERTn
hal_set_pin(&HAL_Bn_PORT, HAL_Bn_PIN);
#else
hal_clear_pin(&HAL_Bn_PORT, HAL_Bn_PIN);
#endif
hal_dead_time();
#ifdef HAL_INVERTp
hal_clear_pin(&HAL_Bp_PORT, HAL_Bp_PIN);
#else
hal_set_pin(&HAL_Bp_PORT, HAL_Bp_PIN);
#endif
}
inline void hal_b_tristate()
{
#ifdef HAL_INVERTn
hal_set_pin(&HAL_Bn_PORT, HAL_Bn_PIN);
#else
hal_clear_pin(&HAL_Bn_PORT, HAL_Bn_PIN);
#endif
#ifdef HAL_INVERTp
hal_set_pin(&HAL_Bp_PORT, HAL_Bp_PIN);
#else
hal_clear_pin(&HAL_Bp_PORT, HAL_Bp_PIN);
#endif
}
inline void hal_c_low()
{
#ifdef HAL_INVERTp
hal_set_pin(&HAL_Cp_PORT, HAL_Cp_PIN);
#else
hal_clear_pin(&HAL_Cp_PORT, HAL_Cp_PIN);
#endif
hal_dead_time();
#ifdef HAL_INVERTn
hal_clear_pin(&HAL_Cn_PORT, HAL_Cn_PIN);
#else
hal_set_pin(&HAL_Cn_PORT, HAL_Cn_PIN);
#endif
}
inline void hal_c_high()
{
#ifdef HAL_INVERTn
hal_set_pin(&HAL_Cn_PORT, HAL_Cn_PIN);
#else
hal_clear_pin(&HAL_Cn_PORT, HAL_Cn_PIN);
#endif
hal_dead_time();
#ifdef HAL_INVERTp
hal_clear_pin(&HAL_Cp_PORT, HAL_Cp_PIN);
#else
hal_set_pin(&HAL_Cp_PORT, HAL_Cp_PIN);
#endif
}
inline void hal_c_tristate()
{
#ifdef HAL_INVERTn
hal_set_pin(&HAL_Cn_PORT, HAL_Cn_PIN);
#else
hal_clear_pin(&HAL_Cn_PORT, HAL_Cn_PIN);
#endif
#ifdef HAL_INVERTp
hal_set_pin(&HAL_Cp_PORT, HAL_Cp_PIN);
#else
hal_clear_pin(&HAL_Cp_PORT, HAL_Cp_PIN);
#endif
}
#endif