Skip to content

Commit

Permalink
add e2p access functions (not used in firmware currently) with workin…
Browse files Browse the repository at this point in the history
…g unit test
  • Loading branch information
breaker27 committed Oct 13, 2013
1 parent 3ec07cf commit 187446d
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 11 deletions.
2 changes: 1 addition & 1 deletion firmware/shc.ppg
@@ -1 +1 @@
<Workspace name="shc"><Project path="shc_basestation\shc_basestation.pnproj"></Project><Project path="shc_tempsensor\shc_tempsensor.pnproj"></Project><Project path="shc_powerswitch\shc_powerswitch.pnproj"></Project><Project path="shc_dimmer\shc_dimmer.pnproj"></Project><Project path="rfm12\rfm12.pnproj"></Project><Project path="src_common\src_common.pnproj"></Project><Project path="makefile_common\makefile_common.pnproj"></Project></Workspace>
<Workspace name="shc"><Project path="shc_basestation\shc_basestation.pnproj"></Project><Project path="shc_tempsensor\shc_tempsensor.pnproj"></Project><Project path="shc_powerswitch\shc_powerswitch.pnproj"></Project><Project path="shc_dimmer\shc_dimmer.pnproj"></Project><Project path="rfm12\rfm12.pnproj"></Project><Project path="src_common\src_common.pnproj"></Project><Project path="makefile_common\makefile_common.pnproj"></Project><Project path="unittest\unittest.pnproj"></Project></Workspace>
2 changes: 1 addition & 1 deletion firmware/shc_basestation/shc_basestation.pnproj
@@ -1 +1 @@
<Project name="shc_basestation"><File path="Makefile"></File><File path="shc_basestation.c"></File><File path="rfm12_config.h"></File><File path="request_buffer.c"></File><File path="request_buffer.h"></File><File path="fuses.c"></File><File path="e2p_layout_base_station.h"></File></Project>
<Project name="shc_basestation"><File path="Makefile"></File><File path="shc_basestation.c"></File><File path="rfm12_config.h"></File><File path="request_buffer.c"></File><File path="request_buffer.h"></File><File path="fuses.c"></File><File path="e2p_layout.h"></File></Project>
2 changes: 1 addition & 1 deletion firmware/shc_dimmer/shc_dimmer.pnproj
@@ -1 +1 @@
<Project name="shc_dimmer"><File path="shc_dimmer.c"></File><File path="Makefile"></File><File path="rfm12_config.h"></File><File path="fuses.c"></File><File path="e2p_layout_dimmer.h"></File></Project>
<Project name="shc_dimmer"><File path="shc_dimmer.c"></File><File path="Makefile"></File><File path="rfm12_config.h"></File><File path="fuses.c"></File><File path="e2p_layout.h"></File></Project>
2 changes: 1 addition & 1 deletion firmware/shc_tempsensor/shc_tempsensor.pnproj
@@ -1 +1 @@
<Project name="shc_tempsensor"><File path="Makefile"></File><File path="shc_tempsensor.c"></File><File path="rfm12_config.h"></File><File path="sht11.c"></File><File path="sht11.h"></File><File path="config.h"></File><File path="fuses.c"></File><File path="e2p_layout_temperature_sensor.h"></File></Project>
<Project name="shc_tempsensor"><File path="Makefile"></File><File path="shc_tempsensor.c"></File><File path="rfm12_config.h"></File><File path="sht11.c"></File><File path="sht11.h"></File><File path="config.h"></File><File path="fuses.c"></File><File path="e2p_layout.h"></File></Project>
87 changes: 87 additions & 0 deletions firmware/src_common/e2p_access.c
@@ -0,0 +1,87 @@
#include "e2p_access.h"

#ifdef UNITTEST

const uint8_t e2p[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 255, 254, 253, 252, 251, 250, 249, 248 };

uint8_t eeprom_read_byte (const uint8_t *__p)
{
uint32_t p = (uint32_t)__p;
return e2p[p];
}

void signal_error_state(void)
{
//exit(1);
}

#endif

// Read UIntValue from EEPROM and limit it to the given boundaries.
// Go into error mode if bit length is not as assumed.
uint32_t __eeprom_read_UIntValue32(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval, uint16_t max_bits_for_type)
{
if (length_bits > max_bits_for_type)
{
signal_error_state();
}

uint32_t val = eeprom_read_byte((uint8_t*)(byte + 0)) << (24 + bit);

if (length_bits + bit > 8)
{
val += eeprom_read_byte((uint8_t*)(byte + 1)) << (16 + bit);
}

if (length_bits + bit > 16)
{
val += eeprom_read_byte((uint8_t*)(byte + 2)) << (8 + bit);
}

if (length_bits + bit > 24)
{
val += eeprom_read_byte((uint8_t*)(byte + 3)) << (0 + bit);
}

if (length_bits + bit > 32)
{
val += eeprom_read_byte((uint8_t*)(byte + 4)) >> (8 - bit);
}

val = val >> (32 - length_bits); // move wanted bits to the lowest position

if (length_bits < 32)
{
val = val & ((1 << length_bits) - 1); // filter out only the wanted bits
}

if (val < minval)
{
val = minval;
}

if (val > maxval)
{
val = maxval;
}

return val;
}

// Wrapper function that casts to needed type and tells the internal function the max bit size of the type.
uint8_t eeprom_read_UIntValue8(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval)
{
return (uint8_t)__eeprom_read_UIntValue32(byte, bit, length_bits, minval, maxval, 8);
}

// Wrapper function that casts to needed type and tells the internal function the max bit size of the type.
uint16_t eeprom_read_UIntValue16(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval)
{
return (uint16_t)__eeprom_read_UIntValue32(byte, bit, length_bits, minval, maxval, 16);
}

// Wrapper function that casts to needed type and tells the internal function the max bit size of the type.
uint32_t eeprom_read_UIntValue32(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval)
{
return __eeprom_read_UIntValue32(byte, bit, length_bits, minval, maxval, 32);
}
12 changes: 12 additions & 0 deletions firmware/src_common/e2p_access.h
@@ -0,0 +1,12 @@
#include <stdint.h>

#ifdef UNITTEST

uint8_t eeprom_read_byte (const uint8_t *__p);
void signal_error_state(void);

#endif

uint8_t eeprom_read_UIntValue8 (uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval);
uint16_t eeprom_read_UIntValue16(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval);
uint32_t eeprom_read_UIntValue32(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval);
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.c"></File><File path="util.h"></File><File path="aes256.c"></File><File path="aes256.h"></File></Project>
<Project name="src_common"><File path="uart.c"></File><File path="uart.h"></File><File path="util.c"></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></Project>
3 changes: 0 additions & 3 deletions firmware/src_common/util.h
Expand Up @@ -38,9 +38,6 @@ uint32_t linear_interpolate32(uint32_t in, uint32_t min_in, uint32_t max_in, uin
float linear_interpolate_f(float in, float min_in, float max_in, float min_out, float max_out);
int bat_percentage(int vbat);

uint8_t eeprom_read_uint8(unsigned int addr_offset);
unsigned int eeprom_read_int(unsigned int addr_offset);

void adc_init(void);
void adc_on(bool on);
unsigned int read_adc(unsigned char adc_input);
Expand Down
4 changes: 3 additions & 1 deletion firmware/unittest/Makefile
Expand Up @@ -20,6 +20,7 @@
OBJDIR = build
BINDIR = bin


# Compiler command
CC = gcc

Expand All @@ -28,6 +29,7 @@ LD = gcc

# Flags to pass to the compiler - add "-g" to include debug information
CFLAGS = -Wall
CFLAGS += -DUNITTEST=1 # tell some header files that we are compiling for the unittest (and AVR functions are not available)

# Flags to pass to the linker
LDFLAGS =
Expand All @@ -36,7 +38,7 @@ LDFLAGS =
RM = rm

# List your source files here
CSRC = main.c
CSRC = main.c ../src_common/e2p_access.c

# List your object files here
OBJ = $(CSRC:%.c=$(OBJDIR)/%.o)
Expand Down
86 changes: 84 additions & 2 deletions firmware/unittest/main.c
@@ -1,8 +1,90 @@
/*
* This file is part of smarthomatic, http://www.smarthomatic.org.
* Copyright (c) 2013 Uwe Freese
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../src_common/e2p_access.h"

uint8_t res = 0;

void compare(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval, uint32_t assumed_value, uint32_t val)
{
printf("Test eeprom_read_UIntValueXX(%d, %d, %d, %u, %u)\n", byte, bit, length_bits, minval, maxval);
printf(" Assumed value: %d, returned value: %u", assumed_value, val);

if (val != assumed_value)
{
res = 1;
printf(" --> NOK\n");
}
else
{
printf(" --> OK\n");
}
}

// Test one of the e2p read functions.
void test_eeprom_read_UIntValue8(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval, uint32_t assumed_value)
{
uint32_t x = eeprom_read_UIntValue8(byte, bit, length_bits, minval, maxval);
compare(byte, bit, length_bits, minval, maxval, assumed_value, x);
}

void test_eeprom_read_UIntValue16(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval, uint32_t assumed_value)
{
uint32_t x = eeprom_read_UIntValue16(byte, bit, length_bits, minval, maxval);
compare(byte, bit, length_bits, minval, maxval, assumed_value, x);
}

void test_eeprom_read_UIntValue32(uint16_t byte, uint8_t bit, uint16_t length_bits, uint32_t minval, uint32_t maxval, uint32_t assumed_value)
{
uint32_t x = eeprom_read_UIntValue32(byte, bit, length_bits, minval, maxval);
compare(byte, bit, length_bits, minval, maxval, assumed_value, x);
}

int main(int argc , char** argv){
printf("Hello World\n");
return EXIT_SUCCESS;
printf("smarthomatic unit test\n");

// 8 Bit tests
test_eeprom_read_UIntValue8(5, 0, 8, 0, 255, 5); // normal read
test_eeprom_read_UIntValue8(5, 0, 8, 7, 255, 7); // min test
test_eeprom_read_UIntValue8(5, 0, 8, 0, 3, 3); // max test
test_eeprom_read_UIntValue8(5, 1, 7, 0, 255, 5); // offset test within a byte
test_eeprom_read_UIntValue8(5, 2, 8, 0, 255, 5 << 2); // offset test over byte boundary
test_eeprom_read_UIntValue8(5, 7, 1, 0, 255, 1); // bit masking test

// 16 Bit tests
test_eeprom_read_UIntValue16(5, 0, 16, 0, 65535, 1286); // normal read
test_eeprom_read_UIntValue16(5, 0, 16, 1400, 65535, 1400); // min test
test_eeprom_read_UIntValue16(5, 0, 16, 0, 3, 3); // max test
test_eeprom_read_UIntValue16(5, 1, 15, 0, 65535, 1286); // offset test within a byte
test_eeprom_read_UIntValue16(5, 2, 16, 0, 65535, 1286 << 2); // offset test over byte boundary
test_eeprom_read_UIntValue16(5, 7, 1, 0, 65535, 1); // bit masking test

// 32 Bit tests
test_eeprom_read_UIntValue32(5, 0, 32, 0, 4294967295U, 84281096U); // normal read
test_eeprom_read_UIntValue32(5, 0, 32, 90000000U, 4294967295U, 90000000U); // min test
test_eeprom_read_UIntValue32(5, 0, 32, 0, 3, 3); // max test
test_eeprom_read_UIntValue32(5, 1, 31, 0, 4294967295U, 84281096U); // offset test within a byte
test_eeprom_read_UIntValue32(5, 2, 32, 0, 4294967295U, 84281096U << 2); // offset test over byte boundary
test_eeprom_read_UIntValue32(5, 7, 1, 0, 4294967295U, 1); // bit masking test

return res;
}
1 change: 1 addition & 0 deletions firmware/unittest/unittest.pnproj
@@ -0,0 +1 @@
<Project name="unittest"><File path="main.c"></File><File path="Makefile"></File></Project>

0 comments on commit 187446d

Please sign in to comment.