commit 221905f27c17e9d396b218dbbfb6c23831532266 Author: Jochen Friedrich Date: Sun Feb 18 20:16:33 2018 +0000 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8956788 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.hex +*.lst +*.elf +*.o +*.map diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..26adfa8 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +PROJ = avr-eb +CC = avr-gcc +OBJCOPY = avr-objcopy +CPU = atmega88 + +CFLAGS += -Os -g -mmcu=$(CPU) -Wall +CFLAGS += -Wa,-adhlns=$(<:%.c=%.lst) + +CLDFLAGS += -mmcu=$(CPU) -Wl,-Map=$(@:%.elf=%.map) + +HBWOBJS += testnode.o hbw.o + +all: testnode.hex + +testnode.o: testnode.c hbw.h hw.h testhw.h + $(CC) $(CFLAGS) -DTESTNODE -c $< -o $@ + +testnode.elf: $(HBWOBJS) + $(CC) $(CLDFLAGS) -o $@ $(HBWOBJS) + +%hex:%elf + $(OBJCOPY) -j .text -j .data -O srec $< $@ + +clean: + rm -f *.hex *.elf *.o *.lst *.map diff --git a/hbw.c b/hbw.c new file mode 100644 index 0000000..6ca9ca4 --- /dev/null +++ b/hbw.c @@ -0,0 +1,297 @@ +#include "hw.h" +#include +#include +#include +#include "hbw.h" + +static uint16_t crc16shift(uint16_t crc, uint8_t data) +{ + int stat, i; + + for (i=0; i<8; i++) { + stat = crc & 0x8000; + crc = crc << 1; + if (data & 0x80) + crc = crc | 1; + + if (stat) + crc = crc ^ 0x1002; + + data = data << 1; + } + return crc; +} + +void hbw_send(uint8_t i,uint8_t crc) +{ + if (bit_is_set(UCSR0A, UDRE0)) { + if (sesc) { + UDR0 = i & 0x7F; + sind++; + return; + } + if (crc == 1) { + scrc = crc16shift(scrc, i); + } + if (crc == 2) { + scrc = crc16shift(scrc, 0); + scrc = crc16shift(scrc, 0); + i = (scrc >> 8) & 0xFF; + } + if ((i == 0xFD) || (i == 0xFC) || (i == 0xFE)) { + UDR0 = 0xFC; + sesc = 1; + return; + } + UDR0 = i; + sind++; + } +} + +void hbw_loop(void) +{ + if (!sind) + return; + + if (sdisc) { + if (sind == 1) { + hbw_send(0x04,1); + return; + } + if (sind == 2) { + hbw_send(0x01,1); + return; + } + if (sind == 3) { + hbw_send((scrc >> 8) & 0xFF,2); + return; + } + if (sind == 4) { + hbw_send(scrc & 0xFF,0); + return; + } + if (bit_is_set(UCSR0A, TXC0)) { + PORTD &= ~(1<> 8) & 0xFF,2); + return; + } + if (sind == slen+12) { + hbw_send(scrc & 0xFF,0); + return; + } + if (bit_is_set(UCSR0A, TXC0)) { + PORTD &= ~(1< 64)) { + rind = 255; + return; + } + if ((rind < rlen + 10)) { + rmessage[rind-12] = c; + return; + } + if (rind == rlen + 10) + return; + if (rind == rlen + 11) { + rind = 255; + if (rcrc) + return; + rready = 1; + return; + } +} + + +ISR(TIMER2_COMPA_vect) +{ + cli(); + if (rlast < rwant) + rlast++; + sei(); +} diff --git a/hbw.h b/hbw.h new file mode 100644 index 0000000..563e924 --- /dev/null +++ b/hbw.h @@ -0,0 +1,33 @@ +#ifndef __HBW_H__ +#define __HBW_H__ + +#define BAUDRATE 19200L + +uint8_t my_address[4]; + +uint8_t rind; +uint16_t rcrc; +uint8_t rmessage[62]; +uint8_t raddress[4]; +uint8_t rdest[4]; +uint8_t rlen; +uint8_t resc; +uint8_t rctl; +uint8_t rwant; +volatile uint8_t rready; +volatile uint8_t rlast; + +uint8_t sind; +uint8_t sdisc; +uint8_t sctl; +uint16_t scrc; +uint8_t slen; +uint8_t sesc; +uint8_t saddress[4]; +uint8_t smessage[62]; + +void hbw_loop(void); +char sendmsg(void); +char senddsc(void); +void init_hbw(void); +#endif diff --git a/hw.h b/hw.h new file mode 100644 index 0000000..646e41f --- /dev/null +++ b/hw.h @@ -0,0 +1,42 @@ +#ifndef __HW_H__ +#define __HW_H__ +#define SenderEnable PD2 +#define ReceiverDisable PD3 + +#ifdef TESTNODE +#include "testhw.h" +#endif +#ifdef RELAISNODE +#include "relaishw.h" +#endif + +#ifndef F_CPU +#define F_CPU 16000000L +#endif + +#ifndef HBWTYPE +#define HBWTYPE 0xAB +#endif + +#ifndef HBWSERIAL0 +#define HBWSERIAL0 'C' +#define HBWSERIAL1 'O' +#define HBWSERIAL2 'L' +#define HBWSERIAL3 'A' +#define HBWSERIAL4 'B' +#define HBWSERIAL5 'D' +#define HBWSERIAL6 'E' +#define HBWSERIAL7 'V' +#define HBWSERIAL8 '0' +#define HBWSERIAL9 '0' +#endif + +#ifndef HBWMAJOR +#define HBWMAJOR 0 +#endif + +#ifndef HBWMINOR +#define HBWMINOR 0 +#endif + +#endif diff --git a/testhw.h b/testhw.h new file mode 100644 index 0000000..d561aa4 --- /dev/null +++ b/testhw.h @@ -0,0 +1,23 @@ +#ifndef __TESTHW_H__ +#define __TESTHW_H__ + +#define ConfigLED PD4 +#define ConfigButton PD5 +#define ExpLED PD6 +#define ExpButton PD7 + +#define HBWTYPE 0xB0 +#define HBWMAJOR 1 +#define HBWMINOR 2 +#define HBWSERIAL0 'C' +#define HBWSERIAL1 'O' +#define HBWSERIAL2 'L' +#define HBWSERIAL3 'A' +#define HBWSERIAL4 'B' +#define HBWSERIAL5 'T' +#define HBWSERIAL6 'S' +#define HBWSERIAL7 'T' +#define HBWSERIAL8 '0' +#define HBWSERIAL9 '1' + +#endif diff --git a/testnode.c b/testnode.c new file mode 100644 index 0000000..a552d7c --- /dev/null +++ b/testnode.c @@ -0,0 +1,103 @@ +#include "hw.h" +#include +#include +#include +#include "hbw.h" + +uint8_t expled; +uint8_t expbutton; + +uint8_t waitforack; +uint8_t waitforsend; +uint8_t acktimer; + +int main(void) +{ + uint8_t seq; + + my_address[0]=eeprom_read_byte((const uint8_t *)0); + my_address[1]=eeprom_read_byte((const uint8_t *)1); + my_address[2]=eeprom_read_byte((const uint8_t *)2); + my_address[3]=eeprom_read_byte((const uint8_t *)3); + + if ((my_address[0] == 0xFF) && + (my_address[1] == 0xFF) && + (my_address[2] == 0xFF) && + (my_address[3] == 0xFF)) + my_address[0] = 0x74; + + DDRD |= (1<> 1) & 3; + sctl = 0x18 | (seq << 5); + smessage[0] = HBWTYPE; + smessage[1] = 0; + slen = 2; + sendmsg(); + } + if ((rdest[0] == my_address[0]) && + (rdest[1] == my_address[1]) && + (rdest[2] == my_address[2]) && + (rdest[3] == my_address[3]) && + (rmessage[0] == 'n')) { + saddress[0] = raddress[0]; + saddress[1] = raddress[1]; + saddress[2] = raddress[2]; + saddress[3] = raddress[3]; + seq = (rctl >> 1) & 3; + sctl = 0x18 | (seq << 5); + smessage[0] = 'h'; + smessage[1] = 'b'; + smessage[2] = 'w'; + smessage[3] = 'b'; + smessage[4] = 'o'; + smessage[5] = 'c'; + smessage[6] = 'c'; + smessage[7] = '0'; + smessage[8] = '0'; + smessage[9] = '1'; + slen = 10; + sendmsg(); + } + if ((rdest[0] == my_address[0]) && + (rdest[1] == my_address[1]) && + (rdest[2] == my_address[2]) && + (rdest[3] == my_address[3]) && + (rmessage[0] == 'v')) { + saddress[0] = raddress[0]; + saddress[1] = raddress[1]; + saddress[2] = raddress[2]; + saddress[3] = raddress[3]; + seq = (rctl >> 1) & 3; + sctl = 0x18 | (seq << 5); + smessage[0] = 1; + smessage[1] = 2; + slen = 2; + sendmsg(); + } + rready = 0; + } + } +}