2021-01-11 00:25:02 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <util/delay.h>
|
|
|
|
#include <can.h>
|
|
|
|
|
2021-01-18 20:25:01 +01:00
|
|
|
static bool report_change = false;
|
|
|
|
|
2021-01-17 15:38:11 +01:00
|
|
|
static void init() {
|
|
|
|
// Enable GPIO: PB2, PB3, PB4 output, low; PB5 input, pull-up on
|
|
|
|
PORTB = (PORTB & ~(_BV(PB2) | _BV(PB3) | _BV(PB4))) | _BV(PB5);
|
|
|
|
DDRB = (DDRB & ~_BV(PB5)) | (_BV(PB2) | _BV(PB3) | _BV(PB4));
|
|
|
|
|
|
|
|
// Initialize CAN interface
|
|
|
|
can_init(BITRATE_125_KBPS);
|
|
|
|
// Regular receive/transmit mode
|
|
|
|
can_set_mode(NORMAL_MODE);
|
2021-01-11 00:25:02 +01:00
|
|
|
|
2021-01-18 20:25:01 +01:00
|
|
|
// Filter for messages addressed to us (RTR on)
|
|
|
|
can_filter_t us = {
|
|
|
|
.id = 0x001,
|
|
|
|
.mask = 0x00f,
|
2021-01-17 15:38:11 +01:00
|
|
|
.flags = {
|
|
|
|
.rtr = 1,
|
|
|
|
.extended = 0,
|
|
|
|
},
|
|
|
|
};
|
2021-01-18 20:25:01 +01:00
|
|
|
can_set_filter(0, &us);
|
|
|
|
// Filter for public messages (RTR off)
|
|
|
|
can_filter_t pub = {
|
|
|
|
.id = 0x00f,
|
|
|
|
.mask = 0x00f,
|
2021-01-17 15:38:11 +01:00
|
|
|
.flags = {
|
|
|
|
.rtr = 0,
|
|
|
|
.extended = 0,
|
|
|
|
},
|
|
|
|
};
|
2021-01-18 20:25:01 +01:00
|
|
|
can_set_filter(1, &pub);
|
2021-01-11 00:25:02 +01:00
|
|
|
|
2021-01-17 15:38:11 +01:00
|
|
|
// Enable interrupts to start reception
|
|
|
|
sei();
|
2021-01-11 00:25:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UART bit-uart_bang 8 bits on the debug port, LSB first, 1 stop bit, no parity, 9600 baud
|
|
|
|
void uart_bang(uint8_t data) {
|
|
|
|
// Start bit: low
|
|
|
|
PORTB &= ~_BV(PB4);
|
|
|
|
_delay_us(102);
|
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
|
|
// data bit
|
|
|
|
if (data & (_BV(i))) {
|
|
|
|
PORTB |= _BV(PB4);
|
|
|
|
} else {
|
|
|
|
PORTB &= ~_BV(PB4);
|
|
|
|
}
|
|
|
|
_delay_us(102);
|
|
|
|
}
|
|
|
|
// Stop bit: high (and stay there after)
|
|
|
|
PORTB |= _BV(PB4);
|
|
|
|
_delay_us(102);
|
|
|
|
}
|
|
|
|
|
2021-01-17 15:38:11 +01:00
|
|
|
// Bit-bang pattern (LSB first) out the data line
|
2021-01-11 00:25:02 +01:00
|
|
|
uint16_t bang(uint16_t data) {
|
|
|
|
uint16_t in = 0;
|
|
|
|
for (uint8_t i = 0; i < 16; i++) {
|
|
|
|
// Data bit
|
|
|
|
if (data & 1) {
|
|
|
|
PORTB |= _BV(PB4);
|
|
|
|
} else {
|
|
|
|
PORTB &= ~_BV(PB4);
|
|
|
|
}
|
2021-01-17 15:38:11 +01:00
|
|
|
// Clock up
|
2021-01-11 00:25:02 +01:00
|
|
|
PORTB |= _BV(PB2);
|
|
|
|
_delay_us(4);
|
2021-01-17 15:38:11 +01:00
|
|
|
// Clock down
|
2021-01-11 00:25:02 +01:00
|
|
|
PORTB &= ~_BV(PB2);
|
|
|
|
_delay_us(4);
|
|
|
|
// Read
|
|
|
|
if (PINB & _BV(PB5)) {
|
|
|
|
in |= 1;
|
|
|
|
}
|
|
|
|
// Shift
|
|
|
|
data >>= 1;
|
|
|
|
in <<= 1;
|
|
|
|
}
|
2021-01-17 15:38:11 +01:00
|
|
|
// Clear data
|
|
|
|
PORTB &= ~_BV(PB4);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strobe output
|
|
|
|
void strobe() {
|
2021-01-11 00:25:02 +01:00
|
|
|
// Strobe
|
|
|
|
PORTB |= _BV(PB3);
|
|
|
|
_delay_us(4);
|
|
|
|
PORTB &= ~_BV(PB3);
|
|
|
|
}
|
|
|
|
|
2021-01-17 15:38:11 +01:00
|
|
|
static void send_status() {
|
|
|
|
// Bit-bang register contents in (also send out zeros, but don't strobe)
|
|
|
|
uint16_t status = bang(0);
|
|
|
|
|
|
|
|
if (can_check_free_buffer()) {
|
|
|
|
can_t msg = {
|
|
|
|
.id = 0x11,
|
|
|
|
.flags = {
|
|
|
|
.rtr = 0,
|
|
|
|
},
|
|
|
|
.length = 2,
|
|
|
|
.data = { (uint8_t) (status >> 8), (uint8_t) status, },
|
|
|
|
};
|
|
|
|
can_send_message(&msg);
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 00:25:02 +01:00
|
|
|
|
2021-01-17 15:38:11 +01:00
|
|
|
static void fire_relay(uint8_t num) {
|
2021-01-18 12:48:26 +01:00
|
|
|
if (num == 0) {
|
|
|
|
bang(0);
|
|
|
|
strobe();
|
|
|
|
} else if (num <= 5) {
|
|
|
|
uint16_t out = 0x0080 >> (num - 1);
|
2021-01-17 15:38:11 +01:00
|
|
|
bang(out);
|
|
|
|
strobe();
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 00:25:02 +01:00
|
|
|
|
2021-01-17 15:38:11 +01:00
|
|
|
static void loop() {
|
|
|
|
// Check for new messages
|
|
|
|
if (can_check_message()) {
|
|
|
|
can_t msg = { 0 };
|
|
|
|
uint8_t status = can_get_message(&msg);
|
|
|
|
if (status != 0) {
|
|
|
|
switch (msg.id) {
|
|
|
|
case 0x11:
|
|
|
|
if (msg.flags.rtr) {
|
|
|
|
send_status();
|
|
|
|
}
|
|
|
|
case 0x21:
|
|
|
|
if (msg.length == 1) {
|
2021-01-18 12:48:26 +01:00
|
|
|
// stop (d0 = 0) or fire relay (d0 = 1..5)
|
2021-01-17 15:38:11 +01:00
|
|
|
fire_relay(msg.data[0]);
|
2021-01-18 12:44:57 +01:00
|
|
|
// also send out the current status
|
|
|
|
send_status();
|
2021-01-11 00:25:02 +01:00
|
|
|
}
|
2021-01-18 20:25:01 +01:00
|
|
|
case 0x4f:
|
|
|
|
if (msg.length == 1) {
|
|
|
|
report_change = msg.data[0] ? true : false;
|
|
|
|
}
|
|
|
|
break;
|
2021-01-11 00:25:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 15:38:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
init();
|
2021-01-11 00:25:02 +01:00
|
|
|
while (1) {
|
2021-01-17 15:38:11 +01:00
|
|
|
loop();
|
2021-01-11 00:25:02 +01:00
|
|
|
}
|
|
|
|
}
|