From: Jan "Yenya" Kasprzak Date: Thu, 6 Dec 2012 23:10:20 +0000 (+0100) Subject: pwm.c: make it usable from non-atomic blocks X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=commitdiff_plain;h=8650676310d5304199ffe174b45a9a396c9610a7 pwm.c: make it usable from non-atomic blocks --- diff --git a/firmware/Makefile b/firmware/Makefile index 7be101d..1b0113e 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -10,7 +10,7 @@ MCU=attiny861a AVRDUDE_MCU=attiny861 AVRDUDE_PROGRAMMER=usbasp -CFLAGS=-Wall -Os -mmcu=$(MCU) -DUSE_LOGGING=1 -DF_CPU=1000000UL +CFLAGS=-Wall -Os -mmcu=$(MCU) -DUSE_LOGGING=1 -DF_CPU=1000000UL -std=gnu99 LDFLAGS= AVRDUDE_FLAGS= -p$(AVRDUDE_MCU) -c $(AVRDUDE_PROGRAMMER) diff --git a/firmware/pwm.c b/firmware/pwm.c index 8c95619..56fb088 100644 --- a/firmware/pwm.c +++ b/firmware/pwm.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "lights.h" @@ -67,12 +68,14 @@ void susp_pwm() void pwm_off(unsigned char n) { - pwm[n] = 0; - - switch (n) { - case 0: DDRB &= ~_BV(PB1); break; - case 1: DDRB &= ~_BV(PB3); break; - case 2: DDRB &= ~_BV(PB5); break; + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + pwm[n] = 0; + + switch (n) { + case 0: DDRB &= ~_BV(PB1); break; + case 1: DDRB &= ~_BV(PB3); break; + case 2: DDRB &= ~_BV(PB5); break; + } } } @@ -91,17 +94,14 @@ static void pwm_update_hw(unsigned char n) case 0: TC1H = hi; OCR1A = lo; - DDRB |= _BV(PB1); break; case 1: TC1H = hi; OCR1B = lo; - DDRB |= _BV(PB3); break; case 2: TC1H = hi; OCR1D = lo; - DDRB |= _BV(PB5); break; } } @@ -111,8 +111,17 @@ void pwm_set(unsigned char n, uint16_t stride) if (((stride + (1 << PWM_STEP_SHIFT)) >> PWM_STEP_SHIFT) >= PWM_MAX) stride = PWM_MAX << PWM_STEP_SHIFT; - pwm[n] = stride; - pwm_update_hw(n); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + pwm[n] = stride; + + pwm_update_hw(n); + + switch(n) { + case 0: DDRB |= _BV(PB1); break; + case 1: DDRB |= _BV(PB3); break; + case 2: DDRB |= _BV(PB5); break; + } + } } void pwm_timer()