diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2015-02-10 12:33:58 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2015-02-10 12:33:58 +0100 |
commit | 92ec11b505a5b68c33e6158dbc68cea1d23cbd35 (patch) | |
tree | 737a0ac5a0574388d515ae15923e104c5b0e10f2 | |
parent | 708e9eb417ddf41ec345f2be70c4a32721608e4c (diff) | |
download | hourglass-92ec11b505a5b68c33e6158dbc68cea1d23cbd35.tar.gz hourglass-92ec11b505a5b68c33e6158dbc68cea1d23cbd35.tar.bz2 hourglass-92ec11b505a5b68c33e6158dbc68cea1d23cbd35.zip |
Reduce CPU frequency
Also reduces PWM frequency. The speakers resonance frequency is 2kHz, with 1MHz
clock speed and prescaler 256 we get pretty close.
-rw-r--r-- | accel.c | 28 | ||||
-rw-r--r-- | common.h | 2 | ||||
-rw-r--r-- | gyro.c | 17 | ||||
-rw-r--r-- | i2c.c | 19 | ||||
-rw-r--r-- | pwm.c | 4 | ||||
-rw-r--r-- | timer.c | 28 |
6 files changed, 53 insertions, 45 deletions
@@ -163,22 +163,20 @@ bool accelProcess () { if (reading && shouldWakeup (WAKE_I2C)) { disableWakeup (WAKE_I2C); reading = false; - if (twr.status == TWST_OK) { - accelProcessHorizon (); - - /* calculate normalized z (i.e. without earth gravity component) */ - if (horizonSign == HORIZON_NEG) { - zvalnormal = zval - (-ACCEL_1G); - } else if (horizonSign == HORIZON_POS) { - zvalnormal = zval - ACCEL_1G; - } - - accelProcessShake (); - /* new data transfered */ - return true; - } else if (twr.status == TWST_ERR) { - assert (0); + /* the bus might be in use again already */ + //assert (twr.status == TWST_OK); + accelProcessHorizon (); + + /* calculate normalized z (i.e. without earth gravity component) */ + if (horizonSign == HORIZON_NEG) { + zvalnormal = zval - (-ACCEL_1G); + } else if (horizonSign == HORIZON_POS) { + zvalnormal = zval - ACCEL_1G; } + + accelProcessShake (); + /* new data transfered */ + return true; } else { if (shouldWakeup (WAKE_ACCEL) && twr.status == TWST_OK) { /* new data available in device buffer and bus is free */ @@ -2,7 +2,7 @@ #define COMMON_H /* cpu runs at n mhz */ -#define F_CPU 8000000 +#define F_CPU 1000000 #define sleepwhile(cond) \ sleep_enable (); \ @@ -136,16 +136,15 @@ bool gyroProcess () { if (shouldWakeup (WAKE_I2C)) { disableWakeup (WAKE_I2C); state = IDLE; - assert (twr.status != TWST_ERR); - if (twr.status == TWST_OK) { - /* new data transfered, process it */ - /* poor man's noise filter */ - if (abs (zval) > 64) { - zaccum += zval; - } - gyroProcessTicks (); - return true; + /* the bus might be in use again already */ + //assert (twr.status == TWST_OK); + /* new data transfered, process it */ + /* poor man's noise filter */ + if (abs (zval) > 64) { + zaccum += zval; } + gyroProcessTicks (); + return true; } break; @@ -47,17 +47,20 @@ static bool twWriteRaw (const uint8_t data) { void twInit () { #if F_CPU == 1000000 - /* set scl to 3.6 kHz */ + /* set scl to 50 kHz */ TWBR = 2; - TWSR |= 0x3; /* set prescaler to 64 */ + /* prescaler 1 */ + TWSR &= ~((1 << TWPS1) | (1 << TWPS0)); #elif F_CPU == 4000000 /* set scl to 50 kHz ? */ TWBR = 32; - TWSR |= 0x0; /* set prescaler to 0 */ + /* prescaler 1 */ + TWSR &= ~((1 << TWPS1) | (1 << TWPS0)); #elif F_CPU == 8000000 /* set scl to 100 kHz */ TWBR = 32; - TWSR |= 0x0; /* set prescaler to 0 */ + /* prescaler 1 */ + TWSR &= ~((1 << TWPS1) | (1 << TWPS0)); #else #error "cpu speed not supported" #endif @@ -135,6 +138,8 @@ static void twIntWrite () { if (TW_STATUS == TW_MT_DATA_ACK) { twStopRaw (); twr.status = TWST_OK; + enableWakeup (WAKE_I2C); + ++twr.step; } else { twr.status = TWST_ERR; } @@ -232,6 +237,8 @@ static void twIntRead () { if (status == TW_MR_DATA_NACK) { twStopRaw (); twr.status = TWST_OK; + enableWakeup (WAKE_I2C); + ++twr.step; } else { twr.status = TWST_ERR; twr.error = status; @@ -258,8 +265,6 @@ ISR(TWI_vect) { assert (0 && "nope\n"); break; } - if (twr.status == TWST_ERR || twr.status == TWST_OK) { - enableWakeup (WAKE_I2C); - } + assert (twr.status != TWST_ERR); } @@ -79,8 +79,8 @@ void pwmStart () { TIMSK0 = (1 << OCIE0A); /* compare value */ OCR0A = 1; - /* io clock with prescaler 64; ctc (part 2) */ - TCCR0B = (1 << CS02) | (0 << CS01) | (1 << CS00); + /* io clock with prescaler 256; ctc (part 2) */ + TCCR0B = (1 << CS02) | (0 << CS01) | (0 << CS00); } void pwmStop () { @@ -5,10 +5,22 @@ #include "timer.h" -/* useconds per clock tick */ +/* prescaler is 1024 */ +#define PRESCALER ((1 << CS12) | (1 << CS10)) + +#if F_CPU == 1000000 +/* with divider 1024 each tick is 1024us */ +#define US_PER_TICK 1024 +#define MAX_US ((uint32_t) 67107840) +#elif F_CPU == 8000000 +/* with divider 1024 each clock tick is 128us with a rounding error of 64us + * per second/4ms per minute, since it’s a 16 bit timer we support up to + * 2**16-1 ticks, which is 8388480us (8s) */ #define US_PER_TICK 128 -/* max useconds with max OCRA1 */ #define MAX_US ((uint32_t) 8388480) +#else +#error "cpu speed not supported" +#endif /* max/current timer hits */ static volatile uint8_t maxhits, hits; @@ -66,10 +78,6 @@ void timerStart (const uint32_t t, const bool once) { /* enable compare match interrupt */ TIMSK1 = (1 << OCIE1A); /* set compare value */ -#if F_CPU == 8000000 - /* with divider 1024 each clock tick is 128us with a rounding error of 64us - * per second/4ms per minute, since it’s a 16 bit timer we support up to - * 2**16-1 ticks, which is 8388480us (8s) */ if (t >= MAX_US) { maxhits = t/MAX_US+1; count = UINT16_MAX; @@ -82,11 +90,9 @@ void timerStart (const uint32_t t, const bool once) { lastcount = tdiv; OCR1A = tdiv; } -#else -#error "cpu speed not supported" -#endif - /* io clock with 1024 prescaler; ctc (part 2) */ - TCCR1B = (1 << CS12) | (1 << CS10) | (1 << WGM12); + + /* prescaler and ctc (part 2) */ + TCCR1B = PRESCALER | (1 << WGM12); } void timerStop () { |