Skip to content

Commit

Permalink
Move hex_to_uint... functions from uart to util lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
breaker27 committed Feb 16, 2014
1 parent 8bd984c commit a7456db
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
47 changes: 2 additions & 45 deletions firmware/src_common/uart.c
Expand Up @@ -25,7 +25,6 @@
#include "uart.h"
#include "util.h"


// This buffer is used for sending strings over UART using UART_PUT... functions.
char uartbuf[65];

Expand All @@ -50,48 +49,6 @@ uint8_t bytes_pos = 0;
char sendbuf[52];
bool send_data_avail = false;

// Take two characters and return the hex value they represent.
// If characters are not 0..9, a..f, A..F, character is interpreted as 0 or f.
// 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]);
}

void process_cmd(void)
{
uart_putstr("Processing command: ");
Expand All @@ -102,7 +59,7 @@ void process_cmd(void)
{
if (enable_write_eeprom)
{
uint16_t adr = hex_to_byte((uint8_t)cmdbuf[1]) * 16 + hex_to_byte((uint8_t)cmdbuf[2]);
uint16_t adr = hex_to_uint8((uint8_t *)cmdbuf, 1);
uint8_t val = hex_to_uint8((uint8_t *)cmdbuf, 3);
UART_PUTF2("Writing data 0x%x to EEPROM pos 0x%x.\r\n", val, adr);
eeprom_write_byte((uint8_t *)adr, val);
Expand All @@ -114,7 +71,7 @@ void process_cmd(void)
}
else if ((cmdbuf[0] == 'r') && (strlen(cmdbuf) == 3))
{
uint16_t adr = hex_to_byte((uint8_t)cmdbuf[1]) * 16 + hex_to_byte((uint8_t)cmdbuf[2]);
uint16_t adr = hex_to_uint8((uint8_t *)cmdbuf, 1);
uint8_t val = eeprom_read_byte((uint8_t *)adr);
UART_PUTF2("EEPROM value at position 0x%x is 0x%x.\r\n", adr, val);
}
Expand Down
12 changes: 0 additions & 12 deletions firmware/src_common/uart.h
Expand Up @@ -63,18 +63,6 @@ void uart_init(void);
void uart_putstr(char * str);
void uart_putstr_P(PGM_P str);

uint8_t hex_to_uint8(uint8_t * buf, uint8_t offset);

static inline uint16_t hex_to_uint16(uint8_t * buf, uint8_t offset)
{
return ((uint16_t)hex_to_uint8(buf, offset) << 8) + hex_to_uint8(buf, offset + 2);
}

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 print_signed(int16_t i);
void print_bytearray(uint8_t * b, uint8_t len);

Expand Down
42 changes: 42 additions & 0 deletions firmware/src_common/util.c
Expand Up @@ -166,6 +166,48 @@ unsigned int read_adc(unsigned char adc_input)
return adc_data;
}

// 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]);
}

/*
Grundlagen zu diesen Funktionen wurden der Webseite:
http://www.cs.waikato.ac.nz/~312/crc.txt
Expand Down
13 changes: 13 additions & 0 deletions firmware/src_common/util.h
Expand Up @@ -43,6 +43,19 @@ 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);
uint16_t bat_percentage(uint16_t vbat, uint16_t vempty);

uint8_t hex_to_byte(char c);
uint8_t hex_to_uint8(uint8_t * buf, uint8_t offset);

static inline uint16_t hex_to_uint16(uint8_t * buf, uint8_t offset)
{
return ((uint16_t)hex_to_uint8(buf, offset) << 8) + hex_to_uint8(buf, offset + 2);
}

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);
Expand Down

0 comments on commit a7456db

Please sign in to comment.