|
|
|
@ -6,12 +6,12 @@
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "hbw.h"
|
|
|
|
|
|
|
|
|
|
uint32_t my_address;
|
|
|
|
|
uint8_t my_address[4];
|
|
|
|
|
|
|
|
|
|
uint8_t rind;
|
|
|
|
|
uint16_t rcrc;
|
|
|
|
|
uint32_t raddress;
|
|
|
|
|
uint32_t rdest;
|
|
|
|
|
uint8_t raddress[4];
|
|
|
|
|
uint8_t rdest[4];
|
|
|
|
|
uint8_t resc;
|
|
|
|
|
uint8_t rctl;
|
|
|
|
|
volatile uint8_t rready;
|
|
|
|
@ -23,23 +23,16 @@ uint8_t sdisc;
|
|
|
|
|
uint8_t sctl;
|
|
|
|
|
uint16_t scrc;
|
|
|
|
|
uint8_t sesc;
|
|
|
|
|
uint32_t saddress;
|
|
|
|
|
uint8_t saddress[4];
|
|
|
|
|
uint8_t slen;
|
|
|
|
|
uint8_t smessage[62];
|
|
|
|
|
|
|
|
|
|
uint16_t sdelay;
|
|
|
|
|
|
|
|
|
|
uint8_t waitack;
|
|
|
|
|
uint32_t aaddress;
|
|
|
|
|
|
|
|
|
|
uint8_t asent;
|
|
|
|
|
uint8_t waitack;
|
|
|
|
|
uint8_t gotack;
|
|
|
|
|
|
|
|
|
|
uint8_t configled;
|
|
|
|
|
uint8_t configbutton;
|
|
|
|
|
uint8_t configstate;
|
|
|
|
|
uint16_t configtimer;
|
|
|
|
|
|
|
|
|
|
static uint16_t crc16shift(uint16_t crc, uint8_t data)
|
|
|
|
|
{
|
|
|
|
|
int stat, i;
|
|
|
|
@ -60,14 +53,16 @@ static uint16_t crc16shift(uint16_t crc, uint8_t data)
|
|
|
|
|
|
|
|
|
|
static void readaddr(void)
|
|
|
|
|
{
|
|
|
|
|
// my_address=eeprom_read_dword((const uint32_t *)EESIZE-4);
|
|
|
|
|
((uint8_t*)&my_address)[0]=eeprom_read_byte((const uint8_t *)EESIZE-4);
|
|
|
|
|
((uint8_t*)&my_address)[1]=eeprom_read_byte((const uint8_t *)EESIZE-3);
|
|
|
|
|
((uint8_t*)&my_address)[2]=eeprom_read_byte((const uint8_t *)EESIZE-2);
|
|
|
|
|
((uint8_t*)&my_address)[3]=eeprom_read_byte((const uint8_t *)EESIZE-1);
|
|
|
|
|
|
|
|
|
|
if (my_address == 0xFFFFFFFF)
|
|
|
|
|
((uint8_t*)&my_address)[0] = 0x42;
|
|
|
|
|
my_address[0]=eeprom_read_byte((const uint8_t *)EESIZE-4);
|
|
|
|
|
my_address[1]=eeprom_read_byte((const uint8_t *)EESIZE-3);
|
|
|
|
|
my_address[2]=eeprom_read_byte((const uint8_t *)EESIZE-2);
|
|
|
|
|
my_address[3]=eeprom_read_byte((const uint8_t *)EESIZE-1);
|
|
|
|
|
|
|
|
|
|
if ((my_address[0] == 0xFF) &&
|
|
|
|
|
(my_address[1] == 0xFF) &&
|
|
|
|
|
(my_address[2] == 0xFF) &&
|
|
|
|
|
(my_address[3] == 0xFF))
|
|
|
|
|
my_address[0] = 0x42;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void send(uint8_t i,uint8_t crc)
|
|
|
|
@ -129,7 +124,7 @@ void rloop(void)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sind < 5) {
|
|
|
|
|
send(((uint8_t*)&saddress)[sind-1],1);
|
|
|
|
|
send(saddress[sind-1],1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (sind == 5) {
|
|
|
|
@ -137,7 +132,7 @@ void rloop(void)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (sind < 10) {
|
|
|
|
|
send(((uint8_t*)&my_address)[sind-6],1);
|
|
|
|
|
send(my_address[sind-6],1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (sind == 10) {
|
|
|
|
@ -213,7 +208,10 @@ static void announce(void)
|
|
|
|
|
{
|
|
|
|
|
sctl = 0xF8;
|
|
|
|
|
|
|
|
|
|
saddress = 0xFFFFFFFF;
|
|
|
|
|
saddress[0] = 0xFF;
|
|
|
|
|
saddress[1] = 0xFF;
|
|
|
|
|
saddress[2] = 0xFF;
|
|
|
|
|
saddress[3] = 0xFF;
|
|
|
|
|
|
|
|
|
|
slen = 16;
|
|
|
|
|
|
|
|
|
@ -246,16 +244,6 @@ void hbw_init(void)
|
|
|
|
|
PORTD &= ~(1<<SenderEnable);
|
|
|
|
|
PORTD &= ~(1<<ReceiverDisable);
|
|
|
|
|
|
|
|
|
|
#ifdef ConfigButton
|
|
|
|
|
DDRD &= ~(1<<ConfigButton);
|
|
|
|
|
PORTD |= (1<<ConfigButton);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef ConfigLED
|
|
|
|
|
DDRD |= (1<<ConfigLED);
|
|
|
|
|
PORTD &= ~(1<<ConfigLED);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
UBRR0L = ((F_CPU/(BAUDRATE*16))-1) & 0xFF;
|
|
|
|
|
UBRR0H = 0;
|
|
|
|
|
UCSR0B = (1 << TXEN0) | (1 << RXEN0) | (1 << RXCIE0);
|
|
|
|
@ -272,14 +260,9 @@ void hbw_init(void)
|
|
|
|
|
rready = 0;
|
|
|
|
|
asent = 0;
|
|
|
|
|
hbw_timer = 0;
|
|
|
|
|
gotack = 0;
|
|
|
|
|
waitack = 0;
|
|
|
|
|
configled = 0;
|
|
|
|
|
configbutton = 0;
|
|
|
|
|
configstate = 0;
|
|
|
|
|
|
|
|
|
|
readaddr();
|
|
|
|
|
srand(my_address);
|
|
|
|
|
srand(my_address[3]);
|
|
|
|
|
sdelay = 1000 + (rand() & 0x1F);
|
|
|
|
|
sei();
|
|
|
|
|
}
|
|
|
|
@ -289,23 +272,30 @@ static void process(void)
|
|
|
|
|
uint8_t seq,i;
|
|
|
|
|
uint16_t addr;
|
|
|
|
|
|
|
|
|
|
if ((rdest == my_address) &&
|
|
|
|
|
(raddress == aaddress) &&
|
|
|
|
|
waitack) {
|
|
|
|
|
waitack = 0;
|
|
|
|
|
gotack = 1;
|
|
|
|
|
if ((rdest[0] == my_address[0]) &&
|
|
|
|
|
(rdest[1] == my_address[1]) &&
|
|
|
|
|
(rdest[2] == my_address[2]) &&
|
|
|
|
|
(rdest[3] == my_address[3]) &&
|
|
|
|
|
((rctl & 0x13) == 0x11)) {
|
|
|
|
|
if (waitack) {
|
|
|
|
|
waitack = 0;
|
|
|
|
|
gotack = 1;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ((rdest == my_address) &&
|
|
|
|
|
if ((rdest[0] == my_address[0]) &&
|
|
|
|
|
(rdest[1] == my_address[1]) &&
|
|
|
|
|
(rdest[2] == my_address[2]) &&
|
|
|
|
|
(rdest[3] == my_address[3]) &&
|
|
|
|
|
((rctl & 0x11) == 0x10)) {
|
|
|
|
|
saddress = raddress;
|
|
|
|
|
saddress[0] = raddress[0];
|
|
|
|
|
saddress[1] = raddress[1];
|
|
|
|
|
saddress[2] = raddress[2];
|
|
|
|
|
saddress[3] = raddress[3];
|
|
|
|
|
seq = (rctl >> 1) & 3;
|
|
|
|
|
if(!rlen)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
switch (rmessage[0]) {
|
|
|
|
|
case '@':
|
|
|
|
|
if (rlen != 6)
|
|
|
|
|
return;
|
|
|
|
|
if (rmessage[1] == 'a') {
|
|
|
|
|
eeprom_write_byte((uint8_t *)EESIZE-4, rmessage[2]);
|
|
|
|
|
eeprom_write_byte((uint8_t *)EESIZE-3, rmessage[3]);
|
|
|
|
@ -342,8 +332,6 @@ static void process(void)
|
|
|
|
|
sendmsg();
|
|
|
|
|
break;
|
|
|
|
|
case 'v':
|
|
|
|
|
if (rlen != 2)
|
|
|
|
|
return;
|
|
|
|
|
smessage[0] = HBWMAJOR;
|
|
|
|
|
smessage[1] = HBWMINOR;
|
|
|
|
|
slen = 2;
|
|
|
|
@ -352,10 +340,6 @@ static void process(void)
|
|
|
|
|
sendmsg();
|
|
|
|
|
break;
|
|
|
|
|
case 'R':
|
|
|
|
|
if (rlen != 4)
|
|
|
|
|
return;
|
|
|
|
|
if (rmessage[3] > 62)
|
|
|
|
|
return;
|
|
|
|
|
addr = (rmessage[1] >> 8) | rmessage[2];
|
|
|
|
|
for (i = 0; i < rmessage[3]; i++)
|
|
|
|
|
smessage[i] = eeprom_read_byte((const uint8_t *)addr+i);
|
|
|
|
@ -365,10 +349,6 @@ static void process(void)
|
|
|
|
|
sendmsg();
|
|
|
|
|
break;
|
|
|
|
|
case 'W':
|
|
|
|
|
if (rlen <= 4)
|
|
|
|
|
return;
|
|
|
|
|
if (rlen != rmessage[3] + 4)
|
|
|
|
|
return;
|
|
|
|
|
addr = (rmessage[1] >> 8) | rmessage[2];
|
|
|
|
|
for (i = 0; i < rmessage[3]; i++)
|
|
|
|
|
eeprom_write_byte((uint8_t *)addr+i, rmessage[4+i]);
|
|
|
|
@ -378,12 +358,8 @@ static void process(void)
|
|
|
|
|
break;
|
|
|
|
|
case 'x':
|
|
|
|
|
case 's':
|
|
|
|
|
if (rlen <= 2)
|
|
|
|
|
return;
|
|
|
|
|
hbw_set_channel(rmessage[1], rlen - 2, &(rmessage[2]));
|
|
|
|
|
case 'S':
|
|
|
|
|
if (rlen < 2)
|
|
|
|
|
return;
|
|
|
|
|
smessage[0] = 'i';
|
|
|
|
|
smessage[1] = rmessage[1];
|
|
|
|
|
slen = hbw_get_channel(rmessage[1], &(smessage[2])) + 2;
|
|
|
|
@ -392,7 +368,6 @@ static void process(void)
|
|
|
|
|
sendmsg();
|
|
|
|
|
break;
|
|
|
|
|
case 'C':
|
|
|
|
|
hbw_read_config();
|
|
|
|
|
slen = 0;
|
|
|
|
|
sctl = 0x19 | (seq << 5);
|
|
|
|
|
sendmsg();
|
|
|
|
@ -401,14 +376,6 @@ static void process(void)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void eeprom_clear(void)
|
|
|
|
|
{
|
|
|
|
|
uint16_t i;
|
|
|
|
|
|
|
|
|
|
for (i=0; i < EESIZE-4; i++)
|
|
|
|
|
eeprom_write_byte((uint8_t *)i, 0xFF);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void hbw_loop(void)
|
|
|
|
|
{
|
|
|
|
|
rloop();
|
|
|
|
@ -421,111 +388,33 @@ void hbw_loop(void)
|
|
|
|
|
announce();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef ConfigButton
|
|
|
|
|
switch (configstate) {
|
|
|
|
|
case 0:
|
|
|
|
|
if (bit_is_clear(PIND, ConfigButton)) {
|
|
|
|
|
configtimer = hbw_timer;
|
|
|
|
|
configstate = 1;
|
|
|
|
|
configled = 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
if (((hbw_timer - configtimer) > 20) &&
|
|
|
|
|
(bit_is_set(PIND, ConfigButton))) {
|
|
|
|
|
asent = 0;
|
|
|
|
|
configstate = 2;
|
|
|
|
|
configtimer = hbw_timer;
|
|
|
|
|
}
|
|
|
|
|
if ((hbw_timer - configtimer) > 8000) {
|
|
|
|
|
configstate = 3;
|
|
|
|
|
configtimer = hbw_timer;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
if ((hbw_timer - configtimer) > 2000) {
|
|
|
|
|
configstate = 0;
|
|
|
|
|
configled = 0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
configled = ((hbw_timer % 400) > 200)?0:1;
|
|
|
|
|
if (bit_is_set(PIND, ConfigButton)) {
|
|
|
|
|
configstate = 4;
|
|
|
|
|
configtimer = hbw_timer;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
configled = ((hbw_timer % 400) > 200)?0:1;
|
|
|
|
|
if (((hbw_timer - configtimer) > 2000) &&
|
|
|
|
|
(bit_is_set(PIND, ConfigButton))) {
|
|
|
|
|
configstate = 0;
|
|
|
|
|
configled = 0;
|
|
|
|
|
}
|
|
|
|
|
if (((hbw_timer - configtimer) > 20) &&
|
|
|
|
|
(bit_is_clear(PIND, ConfigButton))) {
|
|
|
|
|
configstate = 5;
|
|
|
|
|
configled = 0;
|
|
|
|
|
eeprom_clear();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
if (((hbw_timer - configtimer) > 20) &&
|
|
|
|
|
(bit_is_set(PIND, ConfigButton))) {
|
|
|
|
|
configstate = 0;
|
|
|
|
|
configled = 0;
|
|
|
|
|
asent = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef ConfigLED
|
|
|
|
|
if (configled)
|
|
|
|
|
PORTD |= (1<<ConfigLED);
|
|
|
|
|
else
|
|
|
|
|
PORTD &= ~(1<<ConfigLED);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data, uint32_t target_address)
|
|
|
|
|
{
|
|
|
|
|
if (gotack) {
|
|
|
|
|
gotack = 0;
|
|
|
|
|
waitack = 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_bus_free())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if(waitack > 2) {
|
|
|
|
|
waitack = 0;
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
if (waitack)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
slen = length + 2;
|
|
|
|
|
sctl = 0xF8;
|
|
|
|
|
aaddress = target_address;
|
|
|
|
|
if(aaddress == 0) {
|
|
|
|
|
((uint8_t*)&aaddress)[0]=eeprom_read_byte((const uint8_t *)2);
|
|
|
|
|
((uint8_t*)&aaddress)[1]=eeprom_read_byte((const uint8_t *)3);
|
|
|
|
|
((uint8_t*)&aaddress)[2]=eeprom_read_byte((const uint8_t *)4);
|
|
|
|
|
((uint8_t*)&aaddress)[3]=eeprom_read_byte((const uint8_t *)5);
|
|
|
|
|
// aaddress = eeprom_read_dword((const uint32_t *)2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saddress = aaddress;
|
|
|
|
|
saddress[0] = eeprom_read_byte((const uint8_t *)2);
|
|
|
|
|
saddress[1] = eeprom_read_byte((const uint8_t *)3);
|
|
|
|
|
saddress[2] = eeprom_read_byte((const uint8_t *)4);
|
|
|
|
|
saddress[3] = eeprom_read_byte((const uint8_t *)5);
|
|
|
|
|
smessage[0] = 'i';
|
|
|
|
|
smessage[1] = channel;
|
|
|
|
|
memcpy(&(smessage[2]), data, length);
|
|
|
|
|
sendmsg();
|
|
|
|
|
if (aaddress == 0xFFFFFFFF)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
waitack++;
|
|
|
|
|
waitack = 1;
|
|
|
|
|
gotack = 0;
|
|
|
|
|
sdelay = hbw_timer + 100 + (rand() & 0x1F);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -560,7 +449,7 @@ ISR(USART_RX_vect)
|
|
|
|
|
if ((rind == 1) || (rind == 255))
|
|
|
|
|
return;
|
|
|
|
|
if (rind < 6) {
|
|
|
|
|
((uint8_t*)&rdest)[rind-2] = c;
|
|
|
|
|
rdest[rind-2] = c;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (rind == 6) {
|
|
|
|
@ -568,7 +457,7 @@ ISR(USART_RX_vect)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (rind < 11) {
|
|
|
|
|
((uint8_t*)&raddress)[rind-7] = c;
|
|
|
|
|
raddress[rind-7] = c;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (rind == 11) {
|
|
|
|
@ -590,7 +479,6 @@ ISR(USART_RX_vect)
|
|
|
|
|
if (rcrc)
|
|
|
|
|
return;
|
|
|
|
|
rready = 1;
|
|
|
|
|
rlen = rlen - 2;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|