|
@@ -0,0 +1,225 @@
|
|
|
|
+
|
|
|
|
+ * WARNING: be careful changing this code, it is very timing dependent
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#ifndef F_CPU
|
|
|
|
+#define F_CPU 16000000
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+#include <avr/io.h>
|
|
|
|
+#include <avr/interrupt.h>
|
|
|
|
+#include <util/delay.h>
|
|
|
|
+#include <stdbool.h>
|
|
|
|
+
|
|
|
|
+#include "serial.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define SERIAL_DELAY 24
|
|
|
|
+
|
|
|
|
+uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
|
|
|
|
+uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
|
|
|
|
+
|
|
|
|
+#define SLAVE_DATA_CORRUPT (1<<0)
|
|
|
|
+volatile uint8_t status = 0;
|
|
|
|
+
|
|
|
|
+inline static
|
|
|
|
+void serial_delay(void) {
|
|
|
|
+ _delay_us(SERIAL_DELAY);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline static
|
|
|
|
+void serial_output(void) {
|
|
|
|
+ SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+inline static
|
|
|
|
+void serial_input(void) {
|
|
|
|
+ SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
|
|
|
|
+ SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline static
|
|
|
|
+uint8_t serial_read_pin(void) {
|
|
|
|
+ return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline static
|
|
|
|
+void serial_low(void) {
|
|
|
|
+ SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline static
|
|
|
|
+void serial_high(void) {
|
|
|
|
+ SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void serial_master_init(void) {
|
|
|
|
+ serial_output();
|
|
|
|
+ serial_high();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void serial_slave_init(void) {
|
|
|
|
+ serial_input();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ EIMSK |= _BV(INT0);
|
|
|
|
+
|
|
|
|
+ EICRA &= ~(_BV(ISC00) | _BV(ISC01));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static
|
|
|
|
+void sync_recv(void) {
|
|
|
|
+ serial_input();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ while (!serial_read_pin());
|
|
|
|
+ serial_delay();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static
|
|
|
|
+void sync_send(void) {
|
|
|
|
+ serial_output();
|
|
|
|
+
|
|
|
|
+ serial_low();
|
|
|
|
+ serial_delay();
|
|
|
|
+
|
|
|
|
+ serial_high();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static
|
|
|
|
+uint8_t serial_read_byte(void) {
|
|
|
|
+ uint8_t byte = 0;
|
|
|
|
+ serial_input();
|
|
|
|
+ for ( uint8_t i = 0; i < 8; ++i) {
|
|
|
|
+ byte = (byte << 1) | serial_read_pin();
|
|
|
|
+ serial_delay();
|
|
|
|
+ _delay_us(1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return byte;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static
|
|
|
|
+void serial_write_byte(uint8_t data) {
|
|
|
|
+ uint8_t b = 8;
|
|
|
|
+ serial_output();
|
|
|
|
+ while( b-- ) {
|
|
|
|
+ if(data & (1 << b)) {
|
|
|
|
+ serial_high();
|
|
|
|
+ } else {
|
|
|
|
+ serial_low();
|
|
|
|
+ }
|
|
|
|
+ serial_delay();
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ISR(SERIAL_PIN_INTERRUPT) {
|
|
|
|
+ sync_send();
|
|
|
|
+
|
|
|
|
+ uint8_t checksum = 0;
|
|
|
|
+ for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
|
|
|
|
+ serial_write_byte(serial_slave_buffer[i]);
|
|
|
|
+ sync_send();
|
|
|
|
+ checksum += serial_slave_buffer[i];
|
|
|
|
+ }
|
|
|
|
+ serial_write_byte(checksum);
|
|
|
|
+ sync_send();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ serial_delay();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ _delay_us(SERIAL_DELAY/2);
|
|
|
|
+
|
|
|
|
+ uint8_t checksum_computed = 0;
|
|
|
|
+ for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
|
|
|
|
+ serial_master_buffer[i] = serial_read_byte();
|
|
|
|
+ sync_send();
|
|
|
|
+ checksum_computed += serial_master_buffer[i];
|
|
|
|
+ }
|
|
|
|
+ uint8_t checksum_received = serial_read_byte();
|
|
|
|
+ sync_send();
|
|
|
|
+
|
|
|
|
+ serial_input();
|
|
|
|
+
|
|
|
|
+ if ( checksum_computed != checksum_received ) {
|
|
|
|
+ status |= SLAVE_DATA_CORRUPT;
|
|
|
|
+ } else {
|
|
|
|
+ status &= ~SLAVE_DATA_CORRUPT;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+inline
|
|
|
|
+bool serial_slave_DATA_CORRUPT(void) {
|
|
|
|
+ return status & SLAVE_DATA_CORRUPT;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+int serial_update_buffers(void) {
|
|
|
|
+
|
|
|
|
+ cli();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ serial_output();
|
|
|
|
+ serial_low();
|
|
|
|
+ _delay_us(1);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ serial_input();
|
|
|
|
+ serial_high();
|
|
|
|
+ _delay_us(SERIAL_DELAY);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (serial_read_pin()) {
|
|
|
|
+
|
|
|
|
+ sei();
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ sync_recv();
|
|
|
|
+
|
|
|
|
+ uint8_t checksum_computed = 0;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
|
|
|
|
+ serial_slave_buffer[i] = serial_read_byte();
|
|
|
|
+ sync_recv();
|
|
|
|
+ checksum_computed += serial_slave_buffer[i];
|
|
|
|
+ }
|
|
|
|
+ uint8_t checksum_received = serial_read_byte();
|
|
|
|
+ sync_recv();
|
|
|
|
+
|
|
|
|
+ if (checksum_computed != checksum_received) {
|
|
|
|
+ sei();
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ uint8_t checksum = 0;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
|
|
|
|
+ serial_write_byte(serial_master_buffer[i]);
|
|
|
|
+ sync_recv();
|
|
|
|
+ checksum += serial_master_buffer[i];
|
|
|
|
+ }
|
|
|
|
+ serial_write_byte(checksum);
|
|
|
|
+ sync_recv();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ serial_output();
|
|
|
|
+ serial_high();
|
|
|
|
+
|
|
|
|
+ sei();
|
|
|
|
+ return 0;
|
|
|
|
+}
|