2 #ifndef libpropeller_pwm32_h__
3 #define libpropeller_pwm32_h__
11 #define INLINE__ static inline
12 #define Yield__() __asm__ volatile( "" ::: "memory" )
13 #define PostEffect__(X, Y) __extension__({ int tmp__ = (X); (X) = (Y); tmp__; })
15 #define INLINE__ static
17 #define PostEffect__(X, Y) (tmp__ = (X), (X) = (Y), tmp__)
53 cog = cognew((
int) (&(*(
int *) dat())), (
int) (&command)) + 1;
72 void Duty(
int pin,
int duty_cycle,
int period) {
74 if (duty_cycle == 0) {
78 if (duty_cycle == 100) {
82 if ((duty_cycle != 0) && (duty_cycle != 100)) {
83 int Baseperiod = ((period * 1000) /
Resolution);
84 int Ton = ((duty_cycle * Baseperiod) / 100);
85 int Toff = (Baseperiod - Ton);
92 if ((Ton == 0) && (Toff == 0)) {
95 if ((Ton != 0) || (Toff != 0)) {
96 InternalPWM(pin, Ton, Toff);
111 void Servo(
int pin,
int pulse_width) {
113 if (pulse_width != 0) {
115 Toff = (((20000 - pulse_width) * 1000) /
Resolution);
116 InternalPWM(pin, Ton, Toff);
129 void PWM(
int pin,
int on_time,
int off_time) {
135 static const int Updatetontoff = 1;
136 static const int Io_state = 2;
137 static const int Dutyoverride = 3;
138 static const int Syncphase = 4;
139 static const int Duty_default = 0;
140 static const int Duty_100 = 1;
141 static const int Duty_0 = 2;
142 static const int Disablepin = 0;
143 static const int Enablepin = 1;
148 volatile int command, argument_0, argument_1, argument_2;
150 void Statemode(
int Pin,
int State) {
154 while (!(command == 0)) {
159 void Dutymode(
int Pin,
int Mode) {
162 command = Dutyoverride;
163 while (!(command == 0)) {
172 void Phasesync(
int Pin1,
int Pin2,
int Phase) {
178 while (!(command == 0)) {
190 void InternalPWM(
int Pin,
int Ontime,
int Offtime) {
191 if ((Ontime == 0) && (Offtime == 0)) {
202 if ((Ontime == 0) && (Offtime == 0)) {
205 if ((Ontime != 0) && (Offtime != 0)) {
207 argument_1 = (Ontime - 1);
208 argument_2 = (Offtime - 1);
209 command = Updatetontoff;
210 while (!(command == 0)) {
218 static volatile uint8_t * dat() {
219 static volatile uint8_t data[] = {
220 0xf0, 0xe1, 0xbd, 0xa0, 0xf0, 0xd8, 0xbd, 0xa0, 0x04, 0xe0, 0xfd, 0x80, 0xf0, 0xda, 0xbd, 0xa0,
221 0x04, 0xe0, 0xfd, 0x80, 0xf0, 0xdc, 0xbd, 0xa0, 0x04, 0xe0, 0xfd, 0x80, 0xf0, 0xde, 0xbd, 0xa0,
222 0x01, 0x28, 0xfe, 0x85, 0xf4, 0xe8, 0xb2, 0x6e, 0x34, 0x29, 0xa2, 0xa0, 0x54, 0x29, 0x92, 0xa0,
223 0x01, 0x2a, 0xfe, 0x85, 0xf5, 0xea, 0xb2, 0x6e, 0x35, 0x2b, 0xa2, 0xa0, 0x55, 0x2b, 0x92, 0xa0,
224 0x01, 0x2c, 0xfe, 0x85, 0xf6, 0xec, 0xb2, 0x6e, 0x36, 0x2d, 0xa2, 0xa0, 0x56, 0x2d, 0x92, 0xa0,
225 0x01, 0x2e, 0xfe, 0x85, 0xf7, 0xee, 0xb2, 0x6e, 0x37, 0x2f, 0xa2, 0xa0, 0x57, 0x2f, 0x92, 0xa0,
226 0x01, 0x30, 0xfe, 0x85, 0xf8, 0xf0, 0xb2, 0x6e, 0x38, 0x31, 0xa2, 0xa0, 0x58, 0x31, 0x92, 0xa0,
227 0x01, 0x32, 0xfe, 0x85, 0xf9, 0xf2, 0xb2, 0x6e, 0x39, 0x33, 0xa2, 0xa0, 0x59, 0x33, 0x92, 0xa0,
228 0x01, 0x34, 0xfe, 0x85, 0xfa, 0xf4, 0xb2, 0x6e, 0x3a, 0x35, 0xa2, 0xa0, 0x5a, 0x35, 0x92, 0xa0,
229 0x01, 0x36, 0xfe, 0x85, 0xfb, 0xf6, 0xb2, 0x6e, 0x3b, 0x37, 0xa2, 0xa0, 0x5b, 0x37, 0x92, 0xa0,
230 0x01, 0x38, 0xfe, 0x85, 0xfc, 0xf8, 0xb2, 0x6e, 0x3c, 0x39, 0xa2,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xa0,
242 0x01, 0x50, 0xfe, 0x85, 0x08, 0x11, 0xb3, 0x6e, 0x48, 0x51, 0xa2, 0xa0, 0x68, 0x51, 0x92, 0xa0,
243 0x01, 0x52, 0xfe, 0x85, 0x09, 0x13, 0xb3, 0x6e, 0x49, 0x53, 0xa2, 0xa0, 0x69, 0x53, 0x92, 0xa0,
244 0x01, 0x54, 0xfe, 0x85, 0x0a, 0x15, 0xb3, 0x6e, 0x4a, 0x55, 0xa2, 0xa0, 0x6a, 0x55, 0x92, 0xa0,
245 0x01, 0x56, 0xfe, 0x85, 0x0b, 0x17, 0xb3, 0x6e, 0x4b, 0x57, 0xa2, 0xa0, 0x6b, 0x57, 0x92, 0xa0,
246 0x01, 0x58, 0xfe, 0x85, 0x0c, 0x19, 0xb3, 0x6e, 0x4c, 0x59, 0xa2, 0xa0, 0x6c, 0x59, 0x92, 0xa0,
247 0x01, 0x5a, 0xfe, 0x85, 0x0d, 0x1b, 0xb3, 0x6e, 0x4d, 0x5b, 0xa2, 0xa0, 0x6d, 0x5b, 0x92, 0xa0,
248 0x01, 0x5c, 0xfe, 0x85, 0x0e, 0x1d, 0xb3, 0x6e, 0x4e, 0x5d, 0xa2, 0xa0, 0x6e, 0x5d, 0x92, 0xa0,
249 0x01, 0x5e, 0xfe, 0x85, 0x0f, 0x1f, 0xb3, 0x6e, 0x4f, 0x5f, 0xa2, 0xa0, 0x6f, 0x5f, 0x92, 0xa0,
250 0x01, 0x60, 0xfe, 0x85, 0x10, 0x21, 0xb3, 0x6e, 0x50, 0x61, 0xa2, 0xa0, 0x70, 0x61, 0x92, 0xa0,
251 0x01, 0x62, 0xfe, 0x85, 0x11, 0x23, 0xb3, 0x6e, 0x51, 0x63, 0xa2, 0xa0, 0x71, 0x63, 0x92, 0xa0,
252 0x01, 0x64, 0xfe, 0x85, 0x12, 0x25, 0xb3, 0x6e, 0x52, 0x65, 0xa2, 0xa0, 0x72, 0x65, 0x92, 0xa0,
253 0x01, 0x66, 0xfe, 0x85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 0x08, 0xeb, 0xe0, 0xbd, 0x80,
276 0xee, 0xd6, 0xbd, 0x08, 0x14, 0xd7, 0xfd, 0x80, 0xeb, 0xc8, 0xbd, 0x54, 0x00, 0x00, 0x00, 0x00,
277 0xf0, 0x00, 0xbc, 0xa0, 0xec, 0xd4, 0x3d, 0x08, 0x08, 0x00, 0x7c, 0x5c, 0xd1, 0x00, 0x00, 0x00,
278 0xc9, 0x00, 0x00, 0x00, 0xdd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
280 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
281 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
282 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
283 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
284 0x00, 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
285 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00,
286 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x80, 0x00,
287 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08,
288 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x80,
289 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
290 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
295 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
296 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
297 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
298 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
299 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
300 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
305 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
312 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
313 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
316 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
317 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
320 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
336 #endif // libpropeller_pwm32_h__