aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/pwm.c
blob: 670dad835918ffe5c9e5c6efe3e758e50e91c5f3 (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Simple PWM controller
 *
 * Copyright (c) 2018-2020 Michael Buesch <m@bues.ch>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "battery.h"
#include "compat.h"
#include "debug.h"
#include "eeprom.h"
#include "main.h"
#include "pwm.h"
#include "pwm_conf.h"
#include "util.h"


/* Physical PWM limits */
#define PWM_MIN			0u
#define PWM_MAX			0xFFu

/* Logical PWM limits */
#define PWM_NEGLIM		(PWM_MIN + 0u)
#define PWM_POSLIM		((uint8_t)((PWM_MAX * CONF_PWMLIM) / 100u))

/* High resolution setpoint threshold */
#define PWM_HIGHRES_SP_THRES	0x9FFu
#define PWM_HIGHRES_SP_HYST	0x200u

/* PWM timer modes for pwm_sp_set() */
#define PWM_UNKNOWN_MODE	0u
#define PWM_IRQ_MODE		1u /* Interrupt mode */
#define PWM_HW_MODE		2u /* Hardware-PWM mode */

/* Output ports. */
#if NR_PWM == 3u
# define PWM_TIM0_PORT		PORTD
# define PWM_TIM0_PORTBIT	PD6
# define PWM_TIM1_PORT		PORTB
# define PWM_TIM1_PORTBIT	PB1
# define PWM_TIM2_PORT		PORTB
# define PWM_TIM2_PORTBIT	PB3
#elif NR_PWM == 1u
# define PWM_TIM0_PORT		PORTB
# define PWM_TIM0_PORTBIT	PB0
# define PWM_TIM1_PORT		PWM_TIM0_PORT		/* dummy */
# define PWM_TIM1_PORTBIT	PWM_TIM0_PORTBIT	/* dummy */
# define PWM_TIM2_PORT		PWM_TIM0_PORT		/* dummy */
# define PWM_TIM2_PORTBIT	PWM_TIM0_PORTBIT	/* dummy */
#else
# error
#endif

#if CONF_PWMINVERT
# define ASM_PWM0_OUT_HIGH	"cbi %[_OUT0_PORT], %[_OUT0_BIT] \n"
# define ASM_PWM0_OUT_LOW	"sbi %[_OUT0_PORT], %[_OUT0_BIT] \n"
# define ASM_PWM1_OUT_HIGH	"cbi %[_OUT1_PORT], %[_OUT1_BIT] \n"
# define ASM_PWM1_OUT_LOW	"sbi %[_OUT1_PORT], %[_OUT1_BIT] \n"
# define ASM_PWM2_OUT_HIGH	"cbi %[_OUT2_PORT], %[_OUT2_BIT] \n"
# define ASM_PWM2_OUT_LOW	"sbi %[_OUT2_PORT], %[_OUT2_BIT] \n"
#else
# define ASM_PWM0_OUT_HIGH	"sbi %[_OUT0_PORT], %[_OUT0_BIT] \n"
# define ASM_PWM0_OUT_LOW	"cbi %[_OUT0_PORT], %[_OUT0_BIT] \n"
# define ASM_PWM1_OUT_HIGH	"sbi %[_OUT1_PORT], %[_OUT1_BIT] \n"
# define ASM_PWM1_OUT_LOW	"cbi %[_OUT1_PORT], %[_OUT1_BIT] \n"
# define ASM_PWM2_OUT_HIGH	"sbi %[_OUT2_PORT], %[_OUT2_BIT] \n"
# define ASM_PWM2_OUT_LOW	"cbi %[_OUT2_PORT], %[_OUT2_BIT] \n"
#endif

#define ASM_INPUTS						\
	[_OUT0_PORT]	"I" (_SFR_IO_ADDR(PWM_TIM0_PORT)),	\
	[_OUT0_BIT]	"M" (PWM_TIM0_PORTBIT),			\
	[_OUT1_PORT]	"I" (_SFR_IO_ADDR(PWM_TIM1_PORT)),	\
	[_OUT1_BIT]	"M" (PWM_TIM1_PORTBIT),			\
	[_OUT2_PORT]	"I" (_SFR_IO_ADDR(PWM_TIM2_PORT)),	\
	[_OUT2_BIT]	"M" (PWM_TIM2_PORTBIT)


static struct {
	uint8_t active_mode[NR_PWM];
	uint16_t active_setpoint[NR_PWM];
	uint8_t irq_count;
} pwm;


/* In high resolution mode TIM0_OVF_vect triggers with a frequency of:
 *   F_CPU / (256    *    256)
 *            ^prescaler  ^8-bit-overflow
 * Thus 65536 CPU cycles are one PWM cycle (duty low + duty high).
 * The duty cycle setpoint range is 65536.
 * Therefore the conversion from duty cycle setpoint to CPU cycles is:
 *   1 to 1
 */
/* Setpoint to cycle multiplicator */
static inline uint32_t pwm_sp_to_cpucyc_mul(uint8_t index)
{
	struct eeprom_data *eedata;
	uint32_t mul;

	mul = lim_u16(CONF_PWMLOWSPFACT_MUL);

	eedata = eeprom_get_data();
	if (eedata)
		mul = lim_u16(mul + eedata->pwmcorr_mul[index]);

	return mul;
}
/* Setpoint to cycle divisor */
static inline uint32_t pwm_sp_to_cpucyc_div(uint8_t index)
{
	struct eeprom_data *eedata;
	uint32_t div;

	div = lim_u16(CONF_PWMLOWSPFACT_DIV);

	eedata = eeprom_get_data();
	if (eedata)
		div = lim_u16(div + eedata->pwmcorr_div[index]);

	return div;
}

/* Set a hardware pin to the specified state. */
static inline void pwm_hw_pin_set(uint8_t index, bool set)
{
	switch (index) {
#if NR_PWM >= 1
	case 0u:
		if (set)
			PWM_TIM0_PORT |= (1u << PWM_TIM0_PORTBIT);
		else
			PWM_TIM0_PORT &= (uint8_t)~(1u << PWM_TIM0_PORTBIT);
		break;
#endif
#if NR_PWM >= 2
	case 1u:
		if (set)
			PWM_TIM1_PORT |= (1u << PWM_TIM1_PORTBIT);
		else
			PWM_TIM1_PORT &= (uint8_t)~(1u << PWM_TIM1_PORTBIT);
		break;
#endif
#if NR_PWM >= 3
	case 2u:
		if (set)
			PWM_TIM2_PORT |= (1u << PWM_TIM2_PORTBIT);
		else
			PWM_TIM2_PORT &= (uint8_t)~(1u << PWM_TIM2_PORTBIT);
		break;
#endif
	}
}

/* Set the hardware duty cycle. */
static inline void pwm_hw_set_duty(uint8_t index, uint8_t pwm_duty)
{
	switch (index) {
#if NR_PWM >= 1
	case 0u:
		OCR0A = pwm_duty;
		break;
#endif
#if NR_PWM >= 2
	case 1u:
		OCR1A = pwm_duty;
		break;
#endif
#if NR_PWM >= 3
	case 2u:
		OCR2A = pwm_duty;
		break;
#endif
	}
}

/* Reset the timer count register. */
static inline void pwm_hw_count_reset(uint8_t index)
{
	switch (index) {
#if NR_PWM >= 1
	case 0u:
		TCNT0 = CONF_PWMINVERT ? 0u : 0xFFu;
		break;
#endif
#if NR_PWM >= 2
	case 1u:
		TCNT1 = CONF_PWMINVERT ? 0u : 0xFFu;
		break;
#endif
#if NR_PWM >= 3
	case 2u:
		TCNT2 = CONF_PWMINVERT ? 0u : 0xFFu;
		break;
#endif
	}
}

/* Clear the IRQ flag hardware register bit. */
static inline void pwm_hw_clear_irq_flag(uint8_t index)
{
	switch (index) {
#if NR_PWM >= 1
	case 0u:
		TIFR0 = (1u << TOV0);
		break;
#endif
#if NR_PWM >= 2
	case 1u:
		TIFR1 = (1u << TOV1);
		break;
#endif
#if NR_PWM >= 3
	case 2u:
		TIFR2 = (1u << TOV2);
		break;
#endif
	}
}

/* Enable/disable the timer interrupt. */
static inline void pwm_hw_enable_irq(uint8_t index, bool enable)
{
	if (enable) {
		switch (index) {
#if NR_PWM >= 1
		case 0u:
			TIMSK0 |= (1u << TOIE0);
			break;
#endif
#if NR_PWM >= 2
		case 1u:
			TIMSK1 |= (1u << TOIE1);
			break;
#endif
#if NR_PWM >= 3
		case 2u:
			TIMSK2 |= (1u << TOIE2);
			break;
#endif
		}
	} else {
		switch (index) {
#if NR_PWM >= 1
		case 0u:
			TIMSK0 &= (uint8_t)~(1u << TOIE0);
			break;
#endif
#if NR_PWM >= 2
		case 1u:
			TIMSK1 &= (uint8_t)~(1u << TOIE1);
			break;
#endif
#if NR_PWM >= 3
		case 2u:
			TIMSK2 &= (uint8_t)~(1u << TOIE2);
			break;
#endif
		}
	}

	pwm_hw_clear_irq_flag(index);
}

/* Stop the timer hardware. */
static inline void pwm_hw_stop_timer(uint8_t index)
{
	switch (index) {
#if NR_PWM >= 1
	case 0u:
		TCCR0B = 0u;
		break;
#endif
#if NR_PWM >= 2
	case 1u:
		TCCR1B = 0u;
		break;
#endif
#if NR_PWM >= 3
	case 2u:
		TCCR2B = 0u;
		break;
#endif
	}
}

/* Set hardware driver mode. */
static inline void pwm_hw_set_hardware_driven(uint8_t index, bool hw_driven)
{
	switch (index) {
#if NR_PWM >= 1
	case 0u:
		if (hw_driven) {
			TCCR0A = (1u << COM0A1) | (1u << COM0A0) |
				 (0u << COM0B1) | (0u << COM0B0) |
				 (1u << WGM01) | (1u << WGM00);
		} else {
			TCCR0A = (0u << COM0A1) | (0u << COM0A0) |
				 (0u << COM0B1) | (0u << COM0B0) |
				 (1u << WGM01) | (1u << WGM00);
		}
		break;
#endif
#if NR_PWM >= 2
	case 1u:
		if (hw_driven) {
			TCCR1A = (1u << COM1A1) | (1u << COM1A0) |
				 (0u << COM1B1) | (0u << COM1B0) |
				 (0u << WGM11) | (1u << WGM10);
		} else {
			TCCR1A = (0u << COM1A1) | (0u << COM1A0) |
				 (0u << COM1B1) | (0u << COM1B0) |
				 (0u << WGM11) | (1u << WGM10);
		}
		break;
#endif
#if NR_PWM >= 3
	case 2u:
		if (hw_driven) {
			TCCR2A = (1u << COM2A1) | (1u << COM2A0) |
				 (0u << COM2B1) | (0u << COM2B0) |
				 (1u << WGM21) | (1u << WGM20);
		} else {
			TCCR2A = (0u << COM2A1) | (0u << COM2A0) |
				 (0u << COM2B1) | (0u << COM2B0) |
				 (1u << WGM21) | (1u << WGM20);
		}
		break;
#endif
	}
}

/* Set IRQ mode or HW mode. */
static inline void pwm_hw_set_operation_mode(uint8_t index, uint8_t mode)
{
	/* Reset the timer counter. */
	pwm_hw_count_reset(index);

	if (mode == PWM_IRQ_MODE) {
		switch (index) {
#if NR_PWM >= 1
		case 0u:
			/* Slow clock (PS=256) */
			TCCR0B = (0u << FOC0A) | (0u << FOC0B) |
				 (0u << WGM02) |
				 (1u << CS02) | (0u << CS01) | (0u << CS00);
			break;
#endif
#if NR_PWM >= 2
		case 1u:
			/* Slow clock (PS=256) */
			TCCR1C = (0u << FOC1A) | (0u << FOC1B);
			TCCR1B = (0u << ICNC1) | (0u << ICES1) |
				 (0u << WGM13) | (1u << WGM12) |
				 (1u << CS12) | (0u << CS11) | (0u << CS10);
			break;
#endif
#if NR_PWM >= 3
		case 2u:
			/* Slow clock (PS=256) */
			TCCR2B = (0u << FOC2A) | (0u << FOC2B) |
				 (0u << WGM22) |
				 (1u << CS22) | (1u << CS21) | (0u << CS20);
			break;
#endif
		}
	} else {
		switch (index) {
#if NR_PWM >= 1
		case 0u:
			/* Fast clock (PS=1) */
			TCCR0B = (0u << FOC0A) | (0u << FOC0B) |
				 (0u << WGM02) |
				 (0u << CS02) | (0u << CS01) | (1u << CS00);
			break;
#endif
#if NR_PWM >= 2
		case 1u:
			/* Fast clock (PS=1) */
			TCCR1C = (0u << FOC1A) | (0u << FOC1B);
			TCCR1B = (0u << ICNC1) | (0u << ICES1) |
				 (0u << WGM13) | (1u << WGM12) |
				 (0u << CS12) | (0u << CS11) | (1u << CS10);
			break;
#endif
#if NR_PWM >= 3
		case 2u:
			/* Fast clock (PS=1) */
			TCCR2B = (0u << FOC2A) | (0u << FOC2B) |
				 (0u << WGM22) |
				 (0u << CS22) | (0u << CS21) | (1u << CS20);
			break;
#endif
		}
	}

	/* Enable or disable the OVF interrupt. */
	pwm_hw_enable_irq(index, (mode == PWM_IRQ_MODE));
}

/* Set the PWM output port state. */
static inline void port_out_set(uint8_t index, bool high)
{
	if (high ^ CONF_PWMINVERT)
		pwm_hw_pin_set(index, true);
	else
		pwm_hw_pin_set(index, false);
}

/* Shutdown the PWM and all outputs. */
static void pwm_turn_off_all(void)
{
	uint8_t i;

	for (i = 0u; i < NR_PWM; i++) {
		/* Stop timer. */
		pwm_hw_stop_timer(i);
		pwm_hw_enable_irq(i, false);
		pwm_hw_set_hardware_driven(i, false);

		/* Set output to idle state. */
		port_out_set(i, false);

		memory_barrier();
		pwm.active_mode[i] = PWM_UNKNOWN_MODE;
		pwm.active_setpoint[i] = 0u;
	}
}

/* Get the interrupt count. */
uint8_t pwm_get_irq_count(void)
{
	uint8_t count;

	memory_barrier();
	count = pwm.irq_count;
	memory_barrier();

	return count;
}

/* Set the PWM setpoint.
 * Must be called with interrupts disabled. */
void pwm_sp_set(IF_MULTIPWM(uint8_t index,) uint16_t setpoint)
{
	uint32_t pwm_duty_range;
	uint8_t pwm_duty;
	uint8_t mode;
	IF_SINGLEPWM(uint8_t index = 0u);

	if (battery_voltage_is_critical()) {

		/* The battery is not Ok. Turn off all outputs. */
		pwm_turn_off_all();

	} else {
		/* Determine the mode.
		 *
		 * HW_MODE:
		 * Normal PWM duty cycle.
		 * Use high frequency low resolution PWM.
		 * Disable interrupt mode.
		 *
		 * IRQ_MODE:
		 * Small PWM duty cycles are handled with a much
		 * much higher resolution, but with much lower frequency
		 * in the PWM timer interrupt.
		 */
		mode = pwm.active_mode[index];
		if ((setpoint > (PWM_HIGHRES_SP_THRES + PWM_HIGHRES_SP_HYST)) ||
		    (setpoint == 0u)) {
			mode = PWM_HW_MODE;
		} else if ((setpoint <= (PWM_HIGHRES_SP_THRES - PWM_HIGHRES_SP_HYST)) ||
			   (mode == PWM_UNKNOWN_MODE)) {
			mode = PWM_IRQ_MODE;
		}

		/* Calculate PWM duty cycle value from the setpoint value */
		pwm_duty_range = PWM_POSLIM - PWM_NEGLIM;
		pwm_duty = (uint8_t)(((uint32_t)setpoint * pwm_duty_range) / 0xFFFFu);
		pwm_duty += PWM_NEGLIM;

		/* Invert the PWM, if required. */
		if (!CONF_PWMINVERT)
			pwm_duty = (uint8_t)(PWM_MAX - pwm_duty);

		/* Store the setpoint for use by TIM0_OVF interrupt. */
		pwm.active_setpoint[index] = setpoint;
		memory_barrier();

		if (mode != pwm.active_mode[index]) {
			/* Mode changed. Disable the timer before reconfiguring. */
			pwm_hw_stop_timer(index);
		}

		if (mode == PWM_IRQ_MODE || pwm_duty == PWM_MIN) {
			/* In interrupt mode or if the duty cycle is zero,
			 * do not drive the output pin by hardware. */
			pwm_hw_set_hardware_driven(index, false);
		} else {
			/* Drive the output pin by hardware. */
			pwm_hw_set_hardware_driven(index, true);
		}

		if (mode == PWM_HW_MODE) {
			/* Set the duty cycle in hardware. */
			if (pwm_duty == PWM_MIN) {
				/* Zero duty cycle. Set HW pin directly. */
				pwm_hw_pin_set(index, true);
			} else {
				/* Non-zero duty cycle. Use timer to drive pin. */
				pwm_hw_set_duty(index, pwm_duty);
			}
		}

		if (mode != pwm.active_mode[index]) {
			pwm.active_mode[index] = mode;

			/* Set the clock prescaler (fast or slow). */
			pwm_hw_set_operation_mode(index, mode);
		}
	}
}

/* Get the active PWM setpoint. */
uint16_t pwm_sp_get(IF_MULTIPWM(uint8_t index)
		    IF_SINGLEPWM(void))
{
	uint16_t setpoint;
	uint8_t irq_state;
	IF_SINGLEPWM(uint8_t index = 0u);

	irq_state = irq_disable_save();
	setpoint = pwm.active_setpoint[index];
	irq_restore(irq_state);

	return setpoint;
}

/* Initialize the PWM timer. */
void pwm_init(bool enable)
{
	uint8_t i;

	for (i = 0u; i < NR_PWM; i++) {
		/* Reset mode. */
		pwm.active_mode[i] = PWM_UNKNOWN_MODE;
	}

	/* Initialize output. */
	if (enable) {
		for (i = 0u; i < NR_PWM; i++)
			pwm_sp_set(IF_MULTIPWM(i,) 0u);
	} else
		pwm_turn_off_all();
}

#if NR_PWM >= 1
# include "pwm0_isr.c"
#endif
#if NR_PWM >= 2
# include "pwm1_isr.c"
#endif
#if NR_PWM >= 3
# include "pwm2_isr.c"
#endif
bues.ch cgit interface