Skip to content

Commit

Permalink
Modify code to use stdint.h, and functions accepting parameters!
Browse files Browse the repository at this point in the history
  • Loading branch information
laneboysrc committed Mar 4, 2014
1 parent 1b0fa11 commit af2cab7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 60 deletions.
59 changes: 27 additions & 32 deletions firmware/hk310-filter/hk310-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ static __code uint16_t __at (_CONFIG2) configword2 = _WRT_OFF & _PLLEN_OFF & _ST


extern void Init_UART(void);
extern unsigned char UART_read_byte(void);
extern void UART_send(void);
extern unsigned char tx_value;

unsigned char b;
unsigned int new_crc16;

extern uint8_t UART_read_byte(void);
extern void UART_send(uint8_t);

#define STATE_WAIT_FOR_SYNC 0
#define STATE_SYNC2 1
Expand Down Expand Up @@ -58,23 +53,22 @@ static void Init_hardware(void) {

/*****************************************************************************
Add byte 'b' to the CRC16-CCITT checksum.
Checksum is stored in the global variable new_crc16.
****************************************************************************/
void crc16_ccitt(void)
uint16_t crc16_ccitt(uint16_t crc16, uint8_t b)
{
unsigned char i;
uint8_t i;

new_crc16 = new_crc16 ^ b << 8;
crc16 = crc16 ^ b << 8;

for (i = 0; i < 8 ; i++) {
if (new_crc16 & 0x8000) {
new_crc16 = new_crc16 << 1 ^ 0x1021;
if (crc16 & 0x8000) {
crc16 = crc16 << 1 ^ 0x1021;
}
else {
new_crc16 = new_crc16 << 1;
crc16 = crc16 << 1;
}
}
return crc16;
}


Expand All @@ -90,13 +84,14 @@ void crc16_ccitt(void)
Input: b: byte to process
****************************************************************************/
unsigned char filter(void)
uint8_t filter(uint8_t b)
{
static unsigned char state = STATE_WAIT_FOR_SYNC;
static unsigned char skip;
static unsigned int new_checksum;
static char count = 0;
static int new_ch3;
static uint8_t state = STATE_WAIT_FOR_SYNC;
static uint8_t skip;
static uint16_t new_checksum;
static uint16_t new_crc16;
static uint8_t count = 0;
static uint16_t new_ch3;

switch (state) {
case STATE_WAIT_FOR_SYNC:
Expand Down Expand Up @@ -131,9 +126,8 @@ unsigned char filter(void)
state = STATE_SKIP;
skip = 11;
}
new_crc16 = 0;
new_checksum = (int)b;
crc16_ccitt();
new_crc16 = crc16_ccitt(0, b);
break;

case STATE_PAYLOAD4:
Expand All @@ -146,13 +140,13 @@ unsigned char filter(void)
skip = 10;
}
new_checksum += (int)b;
crc16_ccitt();
new_crc16 = crc16_ccitt(new_crc16, b);
break;

case STATE_PAYLOAD5:
{
char t;
char ch3;
uint8_t t;
uint8_t ch3;
t = b >> 4;
ch3 = b & 0x0f;

Expand All @@ -163,23 +157,23 @@ unsigned char filter(void)
}
state = STATE_PAYLOAD6;
new_checksum += (int)b;
crc16_ccitt();
new_crc16 = crc16_ccitt(new_crc16, b);
break;

case STATE_PAYLOAD6:
// Low byte of steering

state = STATE_PAYLOAD7;
new_checksum += (int)b;
crc16_ccitt();
new_crc16 = crc16_ccitt(new_crc16, b);
break;

case STATE_PAYLOAD7:
// Low byte of throttle

state = STATE_PAYLOAD8;
new_checksum += (int)b;
crc16_ccitt();
new_crc16 = crc16_ccitt(new_crc16, b);
break;

case STATE_PAYLOAD8:
Expand All @@ -188,7 +182,7 @@ unsigned char filter(void)

state = STATE_CRC_H;
new_checksum += (int)b;
crc16_ccitt();
new_crc16 = crc16_ccitt(new_crc16, b);
break;

case STATE_CRC_H:
Expand Down Expand Up @@ -244,9 +238,10 @@ void main(void) {
Init_UART();

while (1) {
uint8_t b;
b = UART_read_byte();
tx_value = filter();
UART_send();
b = filter(b);
UART_send(b);
}
}

Expand Down
4 changes: 2 additions & 2 deletions firmware/hk310-filter/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ all: hk310-filter
hk310-filter: hk310-filter.hex

hk310-filter.hex: hk310-filter.o uart.o
sdcc --use-non-free -mpic14 -p16f1825 -o $@ $^
sdcc --opt-code-size --use-non-free -mpic14 -p16f1825 -o $@ $^


#############################################################################
.c.o:
sdcc --use-non-free -mpic14 -p16f1825 -c $<
sdcc --opt-code-size --use-non-free -mpic14 -p16f1825 -c $<


clean :
Expand Down
51 changes: 25 additions & 26 deletions firmware/hk310-filter/uart.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include <pic16f1825.h>
#include <stdint.h>

#define SPBRG_VALUE 208L // 19200 @ 16 MHz


void UART_send(void);
void UART_send_uint(void);
void UART_send_uchar(void);
unsigned char UART_read_byte(void);
unsigned char tx_value;
unsigned int tx_uint;
void UART_send(uint8_t);
void UART_send_uint(uint16_t);
void UART_send_uchar(uint8_t);
uint8_t UART_read_byte(void);


/*****************************************************************************
Expand All @@ -34,85 +33,85 @@ void Init_UART(void) {
/*****************************************************************************
Send tx_value out via the UART
****************************************************************************/
void UART_send(void) {
// Wait for TSR register being empty, then send the character in tx_value
void UART_send(uint8_t c) {
// Wait for TSR register being empty, then send the character
while (!TRMT);
TXREG = tx_value;
TXREG = c;
}


/*****************************************************************************
Send tx_uint as decimal number with leading zeros out via the UART
****************************************************************************/
void UART_send_uint(void) {
void UART_send_uint(uint16_t tx_uint) {
uint8_t tx_value;

tx_value = 0;
while (tx_uint >= 9999) {
tx_uint -= 10000;
++tx_value;
}
tx_value += '0';
UART_send();
UART_send(tx_value);

tx_value = 0;
while (tx_uint >= 1000) {
tx_uint -= 1000;
++tx_value;
}
tx_value += '0';
UART_send();
UART_send(tx_value);

tx_value = 0;
while (tx_uint >= 100) {
tx_uint -= 100;
++tx_value;
}
tx_value += '0';
UART_send();
UART_send(tx_value);

tx_value = 0;
while (tx_uint >= 10) {
tx_uint -= 10;
++tx_value;
}
tx_value += '0';
UART_send();
UART_send(tx_value);

tx_value = tx_uint + '0';
UART_send();
UART_send(tx_value);

tx_value = '\n';
UART_send();
UART_send('\n');
}


/*****************************************************************************
Send (unsigned char)tx_uint as decimal number with leading zeros out via
the UART
****************************************************************************/
void UART_send_uchar(void) {
tx_uint = tx_uint & 0xff;
void UART_send_uchar(uint8_t tx_uint) {
uint8_t tx_value;

tx_value = 0;
while (tx_uint >= 100) {
tx_uint -= 100;
++tx_value;
}
tx_value += '0';
UART_send();
UART_send(tx_value);

tx_value = 0;
while (tx_uint >= 10) {
tx_uint -= 10;
++tx_value;
}
tx_value += '0';
UART_send();
UART_send(tx_value);

tx_value = tx_uint + '0';
UART_send();
UART_send(tx_value);

tx_value = '\n';
UART_send();
UART_send('\n');
}


Expand All @@ -136,7 +135,7 @@ void UART_send_uchar(void) {
; recommended that you ignore the error flags. Eventually an error will cause
; the receiver to hang up if you don't clear the error condition.
;*****************************************************************************/
unsigned char UART_read_byte(void)
uint8_t UART_read_byte(void)
{
do {
if (OERR) {
Expand Down

0 comments on commit af2cab7

Please sign in to comment.