|
|
|
@ -26,13 +26,20 @@ uint8_t sesc;
|
|
|
|
|
uint32_t saddress;
|
|
|
|
|
uint8_t slen;
|
|
|
|
|
uint8_t smessage[62];
|
|
|
|
|
uint8_t slocked;
|
|
|
|
|
uint8_t sbusy;
|
|
|
|
|
|
|
|
|
|
uint16_t sdelay;
|
|
|
|
|
volatile uint16_t sdelay;
|
|
|
|
|
|
|
|
|
|
uint8_t waitack;
|
|
|
|
|
uint32_t aaddress;
|
|
|
|
|
uint8_t actl;
|
|
|
|
|
uint8_t alen;
|
|
|
|
|
uint8_t alink;
|
|
|
|
|
uint8_t amessage[62];
|
|
|
|
|
|
|
|
|
|
uint8_t asent;
|
|
|
|
|
uint8_t dsent;
|
|
|
|
|
uint8_t gotack;
|
|
|
|
|
|
|
|
|
|
uint8_t configled;
|
|
|
|
@ -60,7 +67,6 @@ 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);
|
|
|
|
@ -197,16 +203,18 @@ static uint8_t senddsc(void)
|
|
|
|
|
UDR0 = 0xFE;
|
|
|
|
|
sind = 1;
|
|
|
|
|
sesc = 0;
|
|
|
|
|
dsent = 1;
|
|
|
|
|
sdisc = 1;
|
|
|
|
|
sdelay = 1000 + (rand() & 0x1F);
|
|
|
|
|
return ~0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint8_t is_bus_free(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(sdelay - hbw_timer > 0x7FFF)
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
if(sdelay || slocked)
|
|
|
|
|
return 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void announce(void)
|
|
|
|
@ -233,9 +241,9 @@ static void announce(void)
|
|
|
|
|
smessage[13] = HBWSERIAL7;
|
|
|
|
|
smessage[14] = HBWSERIAL8;
|
|
|
|
|
smessage[15] = HBWSERIAL9;
|
|
|
|
|
sendmsg();
|
|
|
|
|
|
|
|
|
|
asent = 1;
|
|
|
|
|
if (sendmsg())
|
|
|
|
|
asent = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void hbw_init(void)
|
|
|
|
@ -261,30 +269,104 @@ void hbw_init(void)
|
|
|
|
|
UCSR0B = (1 << TXEN0) | (1 << RXEN0) | (1 << RXCIE0);
|
|
|
|
|
UCSR0C = (1 << UPM01) | (1 << UCSZ01) | (1 << UCSZ00);
|
|
|
|
|
|
|
|
|
|
OCR2A = 0xF9;
|
|
|
|
|
OCR2A = 249;
|
|
|
|
|
TIMSK2 |= (1 << OCIE2A);
|
|
|
|
|
TCCR2A |= (1 << WGM21);
|
|
|
|
|
TCCR2B = TCCR2B | (1 << CS21) | (1 << CS20);
|
|
|
|
|
TCCR2B = TCCR2B | (1 << CS22);
|
|
|
|
|
|
|
|
|
|
rind = 0;
|
|
|
|
|
sind = 0;
|
|
|
|
|
sdisc = 0;
|
|
|
|
|
rready = 0;
|
|
|
|
|
asent = 0;
|
|
|
|
|
dsent = 0;
|
|
|
|
|
hbw_timer = 0;
|
|
|
|
|
gotack = 0;
|
|
|
|
|
waitack = 0;
|
|
|
|
|
configled = 0;
|
|
|
|
|
configbutton = 0;
|
|
|
|
|
configstate = 0;
|
|
|
|
|
slocked = 0;
|
|
|
|
|
sbusy = 0;
|
|
|
|
|
alink = 0;
|
|
|
|
|
|
|
|
|
|
readaddr();
|
|
|
|
|
hbw_read_config();
|
|
|
|
|
srand(my_address);
|
|
|
|
|
sdelay = 1000 + (rand() & 0x1F);
|
|
|
|
|
sei();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void process(void)
|
|
|
|
|
void snext(void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t linkchannel;
|
|
|
|
|
|
|
|
|
|
if (amessage[0] != 'K') {
|
|
|
|
|
alink = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (alink < HBW_MAXLINKS) {
|
|
|
|
|
linkchannel = eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS);
|
|
|
|
|
if (amessage[1] == linkchannel) {
|
|
|
|
|
((uint8_t*)&aaddress)[0]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+1);
|
|
|
|
|
((uint8_t*)&aaddress)[1]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+2);
|
|
|
|
|
((uint8_t*)&aaddress)[2]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+3);
|
|
|
|
|
((uint8_t*)&aaddress)[3]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+4);
|
|
|
|
|
linkchannel=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+5);
|
|
|
|
|
if(aaddress == my_address) {
|
|
|
|
|
hbw_receive_key(linkchannel, amessage[3] >> 2, amessage[3] & 0x3);
|
|
|
|
|
} else {
|
|
|
|
|
amessage[2] = linkchannel;
|
|
|
|
|
sbusy = 1;
|
|
|
|
|
alink++;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
alink++;
|
|
|
|
|
}
|
|
|
|
|
alink = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sprocess(void)
|
|
|
|
|
{
|
|
|
|
|
if (gotack) {
|
|
|
|
|
gotack = 0;
|
|
|
|
|
waitack = 0;
|
|
|
|
|
sbusy= 0;
|
|
|
|
|
snext();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_bus_free())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if(waitack > 2) {
|
|
|
|
|
waitack = 0;
|
|
|
|
|
sbusy= 0;
|
|
|
|
|
snext();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saddress = aaddress;
|
|
|
|
|
sctl = actl;
|
|
|
|
|
slen = alen;
|
|
|
|
|
memcpy(smessage,amessage,slen);
|
|
|
|
|
|
|
|
|
|
if (!sendmsg())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (aaddress == 0xFFFFFFFF) {
|
|
|
|
|
sbusy = 0;
|
|
|
|
|
snext();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
waitack++;
|
|
|
|
|
gotack = 0;
|
|
|
|
|
sdelay = 100 + (rand() & 0x1F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rprocess(void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t seq,i;
|
|
|
|
|
uint16_t addr;
|
|
|
|
@ -342,8 +424,6 @@ static void process(void)
|
|
|
|
|
sendmsg();
|
|
|
|
|
break;
|
|
|
|
|
case 'v':
|
|
|
|
|
if (rlen != 2)
|
|
|
|
|
return;
|
|
|
|
|
smessage[0] = HBWMAJOR;
|
|
|
|
|
smessage[1] = HBWMINOR;
|
|
|
|
|
slen = 2;
|
|
|
|
@ -399,6 +479,20 @@ static void process(void)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ((rdest == 0xFFFFFFFF) &&
|
|
|
|
|
((rctl & 0x11) == 0x10)) {
|
|
|
|
|
if(!rlen)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
switch (rmessage[0]) {
|
|
|
|
|
case 'z':
|
|
|
|
|
slocked = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'Z':
|
|
|
|
|
slocked = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void eeprom_clear(void)
|
|
|
|
@ -413,12 +507,21 @@ void hbw_loop(void)
|
|
|
|
|
{
|
|
|
|
|
rloop();
|
|
|
|
|
if (rready) {
|
|
|
|
|
process();
|
|
|
|
|
rprocess();
|
|
|
|
|
rready = 0;
|
|
|
|
|
}
|
|
|
|
|
if (!asent) {
|
|
|
|
|
if (is_bus_free()) {
|
|
|
|
|
announce();
|
|
|
|
|
if (sbusy) {
|
|
|
|
|
sprocess();
|
|
|
|
|
} else {
|
|
|
|
|
if (!dsent) {
|
|
|
|
|
if (is_bus_free()) {
|
|
|
|
|
senddsc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!asent) {
|
|
|
|
|
if (is_bus_free()) {
|
|
|
|
|
announce();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -450,14 +553,14 @@ void hbw_loop(void)
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
configled = ((hbw_timer % 400) > 200)?0:1;
|
|
|
|
|
configled = (hbw_timer & 128)?0:1;
|
|
|
|
|
if (bit_is_set(PIND, ConfigButton)) {
|
|
|
|
|
configstate = 4;
|
|
|
|
|
configtimer = hbw_timer;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
configled = ((hbw_timer % 400) > 200)?0:1;
|
|
|
|
|
configled = (hbw_timer & 128)?0:1;
|
|
|
|
|
if (((hbw_timer - configtimer) > 2000) &&
|
|
|
|
|
(bit_is_set(PIND, ConfigButton))) {
|
|
|
|
|
configstate = 0;
|
|
|
|
@ -467,12 +570,13 @@ void hbw_loop(void)
|
|
|
|
|
(bit_is_clear(PIND, ConfigButton))) {
|
|
|
|
|
configstate = 5;
|
|
|
|
|
configled = 0;
|
|
|
|
|
eeprom_clear();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
configled = (hbw_timer & 512)?0:1;
|
|
|
|
|
if (((hbw_timer - configtimer) > 20) &&
|
|
|
|
|
(bit_is_set(PIND, ConfigButton))) {
|
|
|
|
|
eeprom_clear();
|
|
|
|
|
configstate = 0;
|
|
|
|
|
configled = 0;
|
|
|
|
|
asent = 0;
|
|
|
|
@ -488,52 +592,54 @@ void hbw_loop(void)
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data, uint32_t target_address)
|
|
|
|
|
uint8_t hbw_send_key(uint8_t channel, uint8_t count, uint8_t flag)
|
|
|
|
|
{
|
|
|
|
|
if (gotack) {
|
|
|
|
|
gotack = 0;
|
|
|
|
|
waitack = 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (sbusy)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (!is_bus_free())
|
|
|
|
|
if (alink)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if(waitack > 2) {
|
|
|
|
|
waitack = 0;
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
alen = 4;
|
|
|
|
|
actl = 0xF8;
|
|
|
|
|
amessage[0] = 'K';
|
|
|
|
|
amessage[1] = channel;
|
|
|
|
|
amessage[2] = 0;
|
|
|
|
|
amessage[3] = (flag & 3) | (count << 2);
|
|
|
|
|
|
|
|
|
|
aaddress = 0xFFFFFFFF;
|
|
|
|
|
sbusy = 1;
|
|
|
|
|
alink = 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data)
|
|
|
|
|
{
|
|
|
|
|
if (sbusy)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
saddress = aaddress;
|
|
|
|
|
smessage[0] = 'i';
|
|
|
|
|
smessage[1] = channel;
|
|
|
|
|
memcpy(&(smessage[2]), data, length);
|
|
|
|
|
sendmsg();
|
|
|
|
|
if (aaddress == 0xFFFFFFFF)
|
|
|
|
|
return 1;
|
|
|
|
|
if (alink)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
waitack++;
|
|
|
|
|
gotack = 0;
|
|
|
|
|
sdelay = hbw_timer + 100 + (rand() & 0x1F);
|
|
|
|
|
return 0;
|
|
|
|
|
alen = length + 2;
|
|
|
|
|
actl = 0xF8;
|
|
|
|
|
((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);
|
|
|
|
|
|
|
|
|
|
amessage[0] = 'i';
|
|
|
|
|
amessage[1] = channel;
|
|
|
|
|
memcpy(&(amessage[2]), data, length);
|
|
|
|
|
sbusy = 1;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ISR(USART_RX_vect)
|
|
|
|
|
{
|
|
|
|
|
uint8_t c;
|
|
|
|
|
|
|
|
|
|
sdelay = hbw_timer + 100 + (rand() & 0x1F);
|
|
|
|
|
sdelay = 100 + (rand() & 0x1F);
|
|
|
|
|
|
|
|
|
|
c = UDR0;
|
|
|
|
|
if (c == 0xFE) {
|
|
|
|
@ -600,5 +706,7 @@ ISR(TIMER2_COMPA_vect)
|
|
|
|
|
{
|
|
|
|
|
cli();
|
|
|
|
|
hbw_timer++;
|
|
|
|
|
if (sdelay)
|
|
|
|
|
sdelay--;
|
|
|
|
|
sei();
|
|
|
|
|
}
|
|
|
|
|