Skip to content

Commit

Permalink
Set UART_DEBUG and UART_RX defines in makefile instead of main source…
Browse files Browse the repository at this point in the history
… code file. Remove ifdefs for UART debugging and let the compiler remove unused calls when defines are not set to simplify source code.
  • Loading branch information
breaker27 committed Dec 31, 2013
1 parent f589104 commit b080f33
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 47 deletions.
11 changes: 10 additions & 1 deletion firmware/makefile_common/Makefile.rules
Expand Up @@ -26,7 +26,7 @@ LST = $(CSRC:%.c=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
ALL_CFLAGS = -g$(DEBUG)
ALL_CFLAGS += -DF_CPU=$(F_CPU)UL
ALL_CFLAGS += -DUART_BAUD_RATE=$(UART_BAUD_RATE)UL
ALL_CFLAGS += -DUART_RX=$(UART_RX)UL

ALL_CFLAGS += -O$(OPT)
ALL_CFLAGS += -mmcu=$(MCU) -I.
ALL_CFLAGS += $(CFLAGS)
Expand All @@ -38,6 +38,15 @@ ALL_ASFLAGS += -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs
ALL_LDFLAGS = $(LDFLAGS)
ALL_LDFLAGS += -Wl,-Map=$(OBJDIR)/$(TARGET).map,--cref

# add UART defines used to compile some object files
ifeq ($(UART_DEBUG),1)
ALL_CFLAGS += -DUART_DEBUG

ifeq ($(UART_RX),1)
ALL_CFLAGS += -DUART_RX
endif
endif

# set (differently named) CPU type for avrdude
ifeq ($(MCU),atmega328)
AVRDUDEMCU = m328p
Expand Down
2 changes: 2 additions & 0 deletions firmware/shc_dimmer/Makefile
Expand Up @@ -20,6 +20,8 @@
MCU = atmega328
F_CPU = 8000000
UART_BAUD_RATE = 4800
#UART_DEBUG = 1
#UART_RX = 1

# Target file name (without extension).
TARGET = shc_dimmer
Expand Down
2 changes: 2 additions & 0 deletions firmware/shc_powerswitch/Makefile
Expand Up @@ -20,6 +20,8 @@
MCU = atmega168
F_CPU = 8000000
UART_BAUD_RATE = 4800
#UART_DEBUG = 1
#UART_RX = 1

# Target file name (without extension).
TARGET = shc_powerswitch
Expand Down
2 changes: 2 additions & 0 deletions firmware/shc_tempsensor/Makefile
Expand Up @@ -20,6 +20,8 @@
MCU = atmega168
F_CPU = 1000000
UART_BAUD_RATE = 4800
#UART_DEBUG = 1
#UART_RX = 1

# Target file name (without extension).
TARGET = shc_tempsensor
Expand Down
16 changes: 2 additions & 14 deletions firmware/shc_tempsensor/shc_tempsensor.c
Expand Up @@ -30,9 +30,6 @@
#include "../src_common/msggrp_generic.h"
#include "../src_common/msggrp_tempsensor.h"

// switch on debugging by UART
// #define UART_DEBUG

#include "sht11.h"

#include "aes256.h"
Expand Down Expand Up @@ -71,17 +68,13 @@ void printbytearray(uint8_t * b, uint8_t len)

void rfm12_sendbuf(void)
{
#ifdef UART_DEBUG
UART_PUTS("Before encryption: ");
printbytearray(bufx, __PACKETSIZEBYTES);
#endif

uint8_t aes_byte_count = aes256_encrypt_cbc(bufx, __PACKETSIZEBYTES);

#ifdef UART_DEBUG
UART_PUTS("After encryption: ");
printbytearray(bufx, aes_byte_count);
#endif

rfm12_tx(aes_byte_count, 0, (uint8_t *) bufx);
}
Expand Down Expand Up @@ -122,15 +115,13 @@ int main ( void )

osccal_init();

#ifdef UART_DEBUG
uart_init(false);
uart_init();
UART_PUTS ("\r\n");
UART_PUTS ("smarthomatic Tempsensor V1.0 (c) 2013 Uwe Freese, www.smarthomatic.org\r\n");
UART_PUTF ("Device ID: %u\r\n", device_id);
UART_PUTF ("Packet counter: %lu\r\n", packetcounter);
UART_PUTF ("Temperature Sensor Type: %u\r\n", temperature_sensor_type);
UART_PUTF ("Brightness Sensor Type: %u\r\n", brightness_sensor_type);
#endif

// init AES key
eeprom_read_block(aes_key, (uint8_t *)EEPROM_AESKEY_BYTE, 32);
Expand Down Expand Up @@ -211,14 +202,12 @@ int main ( void )

bat_p_val = bat_percentage(vbat);

#ifdef UART_DEBUG
UART_PUTF("CRC32 is %lx (added as first 4 bytes)\r\n", getBuf32(0));
UART_PUTF("Battery: %u%%, Temperature: ", bat_p_val);
printSigned(temp);
UART_PUTS(" deg.C, Humidity: ");
printSigned(hum);
UART_PUTS("%\r\n");
#endif

rfm12_sendbuf();
rfm12_tick(); // send packet, and then WAIT SOME TIME BEFORE GOING TO SLEEP (otherwise packet would not be sent)
Expand All @@ -243,9 +232,8 @@ int main ( void )
msg_generic_batterystatus_set_percentage(bat_p_val);
pkg_header_calc_crc32();

#ifdef UART_DEBUG
UART_PUTF("Battery: %u%%\r\n", bat_p_val);
#endif

rfm12_sendbuf();
rfm12_tick(); // send packet, and then WAIT SOME TIME BEFORE GOING TO SLEEP (otherwise packet would not be sent)

Expand Down
51 changes: 26 additions & 25 deletions firmware/src_common/uart.c
Expand Up @@ -32,7 +32,7 @@ uint8_t bytes_pos = 0;
// This buffer is used for sending strings over UART using UART_PUT... functions.
char uartbuf[65];

#ifdef UART_RX
#ifdef UART_RX
// All received bytes from UART are stored in this buffer by the interrupt routine. This is a ringbuffer.
#define RXBUF_LENGTH 50
char rxbuf[RXBUF_LENGTH];
Expand Down Expand Up @@ -232,7 +232,9 @@ void process_rxbuf(void)
}
#endif

void uart_init(bool enable_RX) {
void uart_init(void)
{
#ifdef UART_DEBUG
PORTD |= 0x01; // Pullup an RXD an

UCSR0B |= (1 << TXEN0); // UART TX einschalten
Expand All @@ -242,44 +244,43 @@ void uart_init(bool enable_RX) {
UBRR0H = (uint8_t)((UBRR_VAL) >> 8);
UBRR0L = (uint8_t)((UBRR_VAL) & 0xFF);

// UF: Baudraten-Berechnung stimmt irgendwie nicht, obwohl F_CPU auf 4 MHz angepasst wurde. Daher manuell eingetragene Werte nehmen.
// 1 MHz
//UBRR0L = 0x03;
//UBRR0H = 0x00;

// 4 MHz
//UBRR0L = 0x0c;
//UBRR0H = 0x00;

// 12 MHz
//UBRR0L = 0x26;
//UBRR0H = 0x00;
#ifdef UART_RX
// activate rx IRQ
UCSR0B |= (1 << RXCIE0);
#endif // UART_RX

if (enable_RX)
{
// activate rx IRQ
UCSR0B |= (1 << RXCIE0);
}
#endif // UART_DEBUG
}

void uart_putc(char c) {
// UF: UDRE0 statt UDRE, UDR0 statt UDR
#ifdef UART_DEBUG
void uart_putc(char c)
{
while (!(UCSR0A & (1<<UDRE0))); /* warten bis Senden moeglich */
UDR0 = c; /* schreibt das Zeichen x auf die Schnittstelle */
}
#endif // UART_DEBUG

void uart_putstr(char *str) {
while(*str) {
void uart_putstr(char *str)
{
#ifdef UART_DEBUG
while (*str)
{
uart_putc(*str++);
}
#endif // UART_DEBUG
}

void uart_putstr_P(PGM_P str) {
void uart_putstr_P(PGM_P str)
{
#ifdef UART_DEBUG
char tmp;
while((tmp = pgm_read_byte(str))) {

while ((tmp = pgm_read_byte(str)))
{
uart_putc(tmp);
str++;
}
#endif // UART_DEBUG
}

uint16_t hex_to_uint8(uint8_t * buf, uint8_t offset)
Expand Down
22 changes: 15 additions & 7 deletions firmware/src_common/uart.h
Expand Up @@ -37,21 +37,29 @@
#error Systematic UART baud rate is greater than 1% and therefore too high!
#endif

#define UART_PUTS(X) uart_putstr_P(PSTR((X)));
#define UART_PUTF(X, A) {sprintf_P(uartbuf, PSTR((X)), (A)); uart_putstr(uartbuf);}
#define UART_PUTF2(X, A, B) {sprintf_P(uartbuf, PSTR((X)), (A), (B)); uart_putstr(uartbuf);}
#define UART_PUTF3(X, A, B, C) {sprintf_P(uartbuf, PSTR((X)), (A), (B), (C)); uart_putstr(uartbuf);}
#define UART_PUTF4(X, A, B, C, D) {sprintf_P(uartbuf, PSTR((X)), (A), (B), (C), (D)); uart_putstr(uartbuf);}
#ifdef UART_DEBUG
#define UART_PUTS(X) uart_putstr_P(PSTR((X)));
#define UART_PUTF(X, A) {sprintf_P(uartbuf, PSTR((X)), (A)); uart_putstr(uartbuf);}
#define UART_PUTF2(X, A, B) {sprintf_P(uartbuf, PSTR((X)), (A), (B)); uart_putstr(uartbuf);}
#define UART_PUTF3(X, A, B, C) {sprintf_P(uartbuf, PSTR((X)), (A), (B), (C)); uart_putstr(uartbuf);}
#define UART_PUTF4(X, A, B, C, D) {sprintf_P(uartbuf, PSTR((X)), (A), (B), (C), (D)); uart_putstr(uartbuf);}
#else
#define UART_PUTS(X) /* noop */
#define UART_PUTF(X, A) /* noop */
#define UART_PUTF2(X, A, B) /* noop */
#define UART_PUTF3(X, A, B, C) /* noop */
#define UART_PUTF4(X, A, B, C, D) /* noop */
#endif

extern char uartbuf[];
extern char sendbuf[];
extern bool send_data_avail;
extern uint8_t uart_timeout;

void uart_init(bool enable_RX);
void uart_putc(char c);
void uart_init(void);
void uart_putstr(char * str);
void uart_putstr_P(PGM_P str);

uint16_t hex_to_uint8(uint8_t * buf, uint8_t offset);

#ifdef UART_RX
Expand Down
2 changes: 2 additions & 0 deletions firmware/src_common/util.c
Expand Up @@ -287,6 +287,7 @@ void check_eeprom_compatibility(uint8_t expectedDeviceType)
// print an info over UART about the OSCCAL adjustment that was made
void osccal_info(void)
{
#ifdef UART_DEBUG
uint8_t mode = eeprom_read_UIntValue8(EEPROM_OSCCALMODE_BYTE, EEPROM_OSCCALMODE_BIT,
EEPROM_OSCCALMODE_LENGTH_BITS, EEPROM_OSCCALMODE_MINVAL, EEPROM_OSCCALMODE_MAXVAL);

Expand All @@ -295,6 +296,7 @@ void osccal_info(void)
int16_t adjustment = (int16_t)mode - 128;
UART_PUTF("The CPU speed was adjusted by +%d/1000 as set in OSCCAL_MODE byte.\r\n", adjustment);
}
#endif // UART_DEBUG
}

// Initialize the OSCCAL register, used to adjust the internal clock.
Expand Down

0 comments on commit b080f33

Please sign in to comment.