Skip to content

Commit

Permalink
Split up of util lib into util_hw.c|h and util_generic.c|h.
Browse files Browse the repository at this point in the history
  • Loading branch information
breaker27 committed Feb 16, 2014
1 parent a7456db commit e82702e
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 191 deletions.
3 changes: 2 additions & 1 deletion firmware/shc_basestation/util.c
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.c"
#include "../src_common/util_generic.c"
#include "../src_common/util_hw.c"
#include "../src_common/e2p_access.c"
3 changes: 2 additions & 1 deletion firmware/shc_basestation/util.h
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.h"
#include "../src_common/util_generic.h"
#include "../src_common/util_hw.h"
#include "../src_common/e2p_access.h"
3 changes: 2 additions & 1 deletion firmware/shc_dimmer/util.c
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.c"
#include "../src_common/util_generic.c"
#include "../src_common/util_hw.c"
#include "../src_common/e2p_access.c"
3 changes: 2 additions & 1 deletion firmware/shc_dimmer/util.h
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.h"
#include "../src_common/util_generic.h"
#include "../src_common/util_hw.h"
#include "../src_common/e2p_access.h"
3 changes: 2 additions & 1 deletion firmware/shc_powerswitch/util.c
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.c"
#include "../src_common/util_generic.c"
#include "../src_common/util_hw.c"
#include "../src_common/e2p_access.c"
3 changes: 2 additions & 1 deletion firmware/shc_powerswitch/util.h
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.h"
#include "../src_common/util_generic.h"
#include "../src_common/util_hw.h"
#include "../src_common/e2p_access.h"
3 changes: 2 additions & 1 deletion firmware/shc_tempsensor/util.c
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.c"
#include "../src_common/util_generic.c"
#include "../src_common/util_hw.c"
#include "../src_common/e2p_access.c"
3 changes: 2 additions & 1 deletion firmware/shc_tempsensor/util.h
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.h"
#include "../src_common/util_generic.h"
#include "../src_common/util_hw.h"
#include "../src_common/e2p_access.h"
3 changes: 2 additions & 1 deletion firmware/shc_thermostat_hr25/util.c
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.c"
#include "../src_common/util_generic.c"
#include "../src_common/util_hw.c"
#include "../src_common/e2p_access.c"
3 changes: 2 additions & 1 deletion firmware/shc_thermostat_hr25/util.h
Expand Up @@ -16,5 +16,6 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#include "../src_common/util.h"
#include "../src_common/util_generic.h"
#include "../src_common/util_hw.h"
#include "../src_common/e2p_access.h"
2 changes: 1 addition & 1 deletion firmware/src_common/src_common.pnproj
@@ -1 +1 @@
<Project name="src_common"><File path="uart.c"></File><File path="uart.h"></File><File path="util.h"></File><File path="aes256.c"></File><File path="aes256.h"></File><File path="e2p_access.c"></File><File path="e2p_access.h"></File><File path="util.c"></File><File path="msggrp_tempsensor.h"></File><File path="msggrp_generic.h"></File><File path="packet_header.h"></File><File path="packet_headerext_ack.h"></File><File path="packet_headerext_ackstatus.h"></File><File path="packet_headerext_get.h"></File><File path="packet_headerext_set.h"></File><File path="packet_headerext_status.h"></File><File path="packet_headerext_setget.h"></File><File path="msggrp_powerswitch.h"></File><File path="packet_headerext_common.h"></File><File path="msggrp_dimmer.h"></File><File path="e2p_basestation.h"></File><File path="e2p_dimmer.h"></File><File path="e2p_generic.h"></File><File path="e2p_hardware.h"></File><File path="e2p_powerswitch.h"></File><File path="e2p_tempsensor.h"></File><File path="e2p_thermostat.h"></File></Project>
<Project name="src_common"><File path="uart.c"></File><File path="uart.h"></File><File path="aes256.c"></File><File path="aes256.h"></File><File path="e2p_access.c"></File><File path="e2p_access.h"></File><File path="msggrp_tempsensor.h"></File><File path="msggrp_generic.h"></File><File path="packet_header.h"></File><File path="packet_headerext_ack.h"></File><File path="packet_headerext_ackstatus.h"></File><File path="packet_headerext_get.h"></File><File path="packet_headerext_set.h"></File><File path="packet_headerext_status.h"></File><File path="packet_headerext_setget.h"></File><File path="msggrp_powerswitch.h"></File><File path="packet_headerext_common.h"></File><File path="msggrp_dimmer.h"></File><File path="e2p_basestation.h"></File><File path="e2p_dimmer.h"></File><File path="e2p_generic.h"></File><File path="e2p_hardware.h"></File><File path="e2p_powerswitch.h"></File><File path="e2p_tempsensor.h"></File><File path="e2p_thermostat.h"></File><File path="util_generic.c"></File><File path="util_generic.h"></File><File path="util_hw.c"></File><File path="util_hw.h"></File></Project>
6 changes: 5 additions & 1 deletion firmware/src_common/uart.c
Expand Up @@ -96,6 +96,8 @@ ISR(USART_RX_vect)
} // else: Buffer overflow (undetected!)
}

#endif // UART_RX

void uart_init(void)
{
#ifdef UART_DEBUG
Expand Down Expand Up @@ -177,6 +179,8 @@ void print_bytearray(uint8_t * b, uint8_t len)
#endif // UART_DEBUG
}

#ifdef UART_RX

// Process all bytes in the rxbuffer. This function should be called in the main loop.
// It can be interrupted by a UART RX interrupt, so additional bytes can be added into the ringbuffer while this function is running.
void process_rxbuf(void)
Expand Down Expand Up @@ -282,4 +286,4 @@ void process_rxbuf(void)
uart_timeout = bytes_to_read == bytes_pos ? 0 : 255;
}
}
#endif
#endif // UART_RX
176 changes: 176 additions & 0 deletions firmware/src_common/util_generic.c
@@ -0,0 +1,176 @@
/*
* This file is part of smarthomatic, http://www.smarthomatic.org.
* Copyright (c) 2013 Uwe Freese
*
* Contributors:
* CRC32: K.Moraw, www.helitron.de, 2009
*
* Development for smarthomatic by Uwe Freese started by adding a
* function to encrypt and decrypt a byte buffer directly and adding CBC mode.
*
* smarthomatic 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 3 of the License, or (at your
* option) any later version.
*
* smarthomatic 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 smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/


#include "util_generic.h"

// PRECONDITION (which is NOT checked!): min_in < max_in, min_out < max_out
uint16_t linear_interpolate16(uint16_t in, uint16_t min_in, uint16_t max_in, uint16_t min_out, uint16_t max_out)
{
if (in >= max_in)
return max_out;
if (in <= min_in)
return min_out;
// interpolate
return min_out + (in - min_in) * (max_out - min_out) / (max_in - min_in);
}

uint32_t linear_interpolate32(uint32_t in, uint32_t min_in, uint32_t max_in, uint32_t min_out, uint32_t max_out)
{
if (in >= max_in)
return max_out;
if (in <= min_in)
return min_out;
// interpolate
return min_out + (in - min_in) * (max_out - min_out) / (max_in - min_in);
}

float linear_interpolate_f(float in, float min_in, float max_in, float min_out, float max_out)
{
if (in >= max_in)
return max_out;
if (in <= min_in)
return min_out;
// interpolate
return min_out + (in - min_in) * (max_out - min_out) / (max_in - min_in);
}

// Take one characters and return the hex value it represents (0..15).
// If characters are not 0..9, a..f, A..F, character is interpreted as 0xf.
// 0 = 48, 9 = 57
// A = 65, F = 70
// a = 97, f = 102
uint8_t hex_to_byte(char c)
{
if (c <= 48) // 0
{
return 0;
}
else if (c <= 57) // 1..9
{
return c - 48;
}
else if (c <= 65) // A
{
return 10;
}
else if (c <= 70) // B..F
{
return c - 55;
}
else if (c <= 97) // a
{
return 10;
}
else if (c <= 102) // b..f
{
return c - 87;
}
else // f
{
return 15;
}
}

uint8_t hex_to_uint8(uint8_t * buf, uint8_t offset)
{
return hex_to_byte(buf[offset]) * 16 + hex_to_byte(buf[offset + 1]);
}


// Return a 32 bit value stored in 4 bytes in the buffer at the given offset.
uint32_t getBuf32(uint8_t offset)
{
return ((uint32_t)bufx[offset] << 24) | ((uint32_t)bufx[offset + 1] << 16) | ((uint32_t)bufx[offset + 2] << 8) | ((uint32_t)bufx[offset + 3] << 0);
}

// Return a 16 bit value stored in 2 bytes in the buffer at the given offset.
uint32_t getBuf16(uint8_t offset)
{
return ((uint32_t)bufx[offset] << 8) | ((uint32_t)bufx[offset + 1] << 0);
}

// write a 32 bit value to the buffer
void setBuf32(uint8_t offset, uint32_t val)
{
bufx[0 + offset] = (val >> 24) & 0xff;
bufx[1 + offset] = (val >> 16) & 0xff;
bufx[2 + offset] = (val >> 8) & 0xff;
bufx[3 + offset] = (val >> 0) & 0xff;
}

// write a 16 bit value to the buffer
void setBuf16(uint8_t offset, uint16_t val)
{
bufx[0 + offset] = (val >> 8) & 0xff;
bufx[1 + offset] = (val >> 0) & 0xff;
}

/*
Grundlagen zu diesen Funktionen wurden der Webseite:
http://www.cs.waikato.ac.nz/~312/crc.txt
entnommen (A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS)
Algorithmus entsprechend CRC32 fuer Ethernet
Startwert FFFFFFFF, LSB zuerst
Im Ergebnis kommt MSB zuerst und alle Bits sind invertiert
Das Ergebnis wurde geprueft mit dem CRC-Calculator:
http://www.zorc.breitbandkatze.de/crc.html
(Einstellung Button CRC-32 waehlen, Daten eingeben und calculate druecken)
Autor: K.Moraw, www.helitron.de, Oktober 2009
*/

unsigned long reg32; // Schieberegister

unsigned long crc32_bytecalc(unsigned char byte)
{
int i;
unsigned long polynom = 0xEDB88320; // Generatorpolynom

for (i = 0; i < 8; ++i)
{
if ((reg32&1) != (byte&1))
reg32 = (reg32>>1)^polynom;
else
reg32 >>= 1;
byte >>= 1;
}
return reg32 ^ 0xffffffff; // inverses Ergebnis, MSB zuerst
}

unsigned long crc32(unsigned char *data, int len)
{
int i;
reg32 = 0xffffffff;

for (i = 0; i < len; i++)
{
crc32_bytecalc(data[i]); // Berechne fuer jeweils 8 Bit der Nachricht
}

return reg32 ^ 0xffffffff;
}
45 changes: 15 additions & 30 deletions firmware/src_common/util.h → firmware/src_common/util_generic.h
Expand Up @@ -16,32 +16,27 @@
* with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _UTIL_H
#define _UTIL_H
// Utility functions are split up into util_*.{c|h} files.
// The util.{c|h} files in the devices directories include all of them.
// Only one lib is therefore built that contains all functions.

#ifndef _UTIL_GENERIC_H
#define _UTIL_GENERIC_H

#include <inttypes.h>
#include <stdio.h>
#include <stdbool.h>
#include <avr/pgmspace.h>

#define sbi(ADDRESS,BIT) ((ADDRESS) |= (1<<(BIT)))
#define cbi(ADDRESS,BIT) ((ADDRESS) &= ~(1<<(BIT)))

// How often should the packetcounter_base be increased and written to EEPROM?
// This should be 2^32 (which is the maximum transmitted packet counter) /
// 100.000 (which is the maximum amount of possible EEPROM write cycles) or more.
// Therefore 100 is a good value.
#define PACKET_COUNTER_WRITE_CYCLE 100

// used as buffer for sending data to SHT
uint8_t bufx[65];
unsigned int adc_data;
uint32_t packetcounter;

// ########## Linear Interpolation

uint16_t linear_interpolate16(uint16_t in, uint16_t min_in, uint16_t max_in, uint16_t min_out, uint16_t max_out);
uint32_t linear_interpolate32(uint32_t in, uint32_t min_in, uint32_t max_in, uint32_t min_out, uint32_t max_out);
float linear_interpolate_f(float in, float min_in, float max_in, float min_out, float max_out);
uint16_t bat_percentage(uint16_t vbat, uint16_t vempty);

// ########## Hex -> Dec conversion

uint8_t hex_to_byte(char c);
uint8_t hex_to_uint8(uint8_t * buf, uint8_t offset);
Expand All @@ -56,25 +51,15 @@ static inline uint32_t hex_to_uint24(uint8_t * buf, uint8_t offset)
return ((uint32_t)hex_to_uint8(buf, offset) << 16) + ((uint32_t)hex_to_uint8(buf, offset + 2) << 8) + hex_to_uint8(buf, offset + 4);
}

void adc_init(void);
void adc_on(bool on);
unsigned int read_adc(unsigned char adc_input);
// ########## read/write 16 bit / 32 bit values to the byte buffer in PC byte order

unsigned long crc32(unsigned char *data, int len);

// read/write 16 bit / 32 bit values to the byte buffer in PC byte order
uint32_t getBuf16(uint8_t offset);
uint32_t getBuf32(uint8_t offset);
void setBuf32(uint8_t offset, uint32_t val);
void setBuf16(uint8_t offset, uint16_t val);

void util_init(void);
void switch_led(bool b_on);
void led_blink(uint16_t on, uint16_t off, uint8_t times);
void check_eeprom_compatibility(uint8_t deviceType);
void osccal_info(void);
void osccal_init(void);
void inc_packetcounter(void);
void rfm12_sendbuf(void);
// ########## CRC32

unsigned long crc32(unsigned char *data, int len);

#endif /* _UTIL_H */
#endif /* _UTIL_GENERIC_H */

0 comments on commit e82702e

Please sign in to comment.