From 68c180f2aba4471483e1786556c2bc2a94a7d2a7 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 8 Apr 2014 11:28:54 +0200 Subject: Increase CPU and UART speed Re-enable interrupt based accel. --- accel.c | 2 +- common.h | 4 ++-- i2c.c | 15 ++++++++++++++- main.c | 12 ++++++++++-- uart.c | 15 +++++++++++++-- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/accel.c b/accel.c index 670121e..b58f7f7 100644 --- a/accel.c +++ b/accel.c @@ -67,7 +67,7 @@ bool accelProcess () { reading = false; } } else { - if (/*drdy &&*/ twr.status != TWST_WAIT) { + if (drdy && twr.status != TWST_WAIT) { /* new data available in device buffer and bus is free, we are * reading the registers inbetween out_x/y/z and ignore them */ if (!twRequest (TWM_READ, LIS302DL, LIS302DL_UNUSED1, (uint8_t *) val, 6)) { diff --git a/common.h b/common.h index ecf2c27..eba8d6e 100644 --- a/common.h +++ b/common.h @@ -1,8 +1,8 @@ #ifndef COMMON_H #define COMMON_H -/* cpu runs at 1mhz */ -#define F_CPU 1000000 +/* cpu runs at n mhz */ +#define F_CPU 8000000 #define sleepwhile(cond) \ sleep_enable (); \ diff --git a/i2c.c b/i2c.c index 57ee551..7855e88 100644 --- a/i2c.c +++ b/i2c.c @@ -3,6 +3,7 @@ #include #include "i2c.h" +#include "common.h" volatile twReq twr; @@ -42,9 +43,21 @@ static bool twWriteRaw (const uint8_t data) { } void twInit () { - /* set scl to 3.6 kHz (at 1Mhz CPU speed)*/ +#if F_CPU == 1000000 + /* set scl to 3.6 kHz */ TWBR = 2; TWSR |= 0x3; /* set prescaler to 64 */ +#elif F_CPU == 4000000 + /* set scl to 50 kHz ? */ + TWBR = 32; + TWSR |= 0x0; /* set prescaler to 0 */ +#elif F_CPU == 8000000 + /* set scl to 100 kHz */ + TWBR = 32; + TWSR |= 0x0; /* set prescaler to 0 */ +#else +#error "cpu speed not supported" +#endif twr.mode = TWM_INVALID; twr.status = TWST_ERR; diff --git a/main.c b/main.c index 372c070..9c35e99 100644 --- a/main.c +++ b/main.c @@ -28,9 +28,17 @@ static void ledShow (const unsigned char val) { static void cpuInit () { /* enter change prescaler mode */ - CLKPR = CLKPCE << 1; - /* write new prescaler = 8 (i.e. 1Mhz clock frequency) */ + CLKPR = (1 << CLKPCE); + /* write new prescaler */ +#if F_CPU == 1000000 CLKPR = 0b00000011; +#elif F_CPU == 4000000 + CLKPR = 0b00000001; +#elif F_CPU == 8000000 + CLKPR = 0b00000000; +#else +#error "cpu speed not supported" +#endif } int main () { diff --git a/uart.c b/uart.c index 4b7ca39..3528a22 100644 --- a/uart.c +++ b/uart.c @@ -2,6 +2,7 @@ #include #include "uart.h" +#include "common.h" /* blocking uart send */ @@ -23,13 +24,23 @@ static int uartPutc (char c, FILE *stream) { static FILE mystdout = FDEV_SETUP_STREAM (uartPutc, NULL, _FDEV_SETUP_WRITE); void uartInit () { - /* Set baud rate (9600, double speed, at 1mhz) */ UBRR0H = 0; +#if F_CPU == 1000000 + /* Set baud rate (9600, double speed) */ UBRR0L = 12; +#elif F_CPU == 4000000 + /* Set baud rate (9600, double speed) */ + UBRR0L = 51; +#elif F_CPU == 8000000 + /* baudrate 38.4k */ + UBRR0L = 25; +#else +#error "cpu speed not supported" +#endif /* enable double speed mode */ UCSR0A = (1 << U2X0); /* Enable receiver and transmitter */ - UCSR0B = (1<