2018-01-28 17:14:12 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#ifdef USE_SERIAL
|
|
|
|
|
2018-06-28 13:55:15 +00:00
|
|
|
#define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
|
|
|
|
|
|
|
|
// Serial pulse period in microseconds.
|
|
|
|
#define SELECT_SERIAL_SPEED 1
|
|
|
|
#if SELECT_SERIAL_SPEED == 0
|
|
|
|
// Very High speed
|
|
|
|
#define SERIAL_DELAY 4 // micro sec
|
|
|
|
#define READ_WRITE_START_ADJUST 30 // cycles
|
|
|
|
#define READ_WRITE_WIDTH_ADJUST 10 // cycles
|
|
|
|
#elif SELECT_SERIAL_SPEED == 1
|
|
|
|
// High speed
|
|
|
|
#define SERIAL_DELAY 6 // micro sec
|
|
|
|
#define READ_WRITE_START_ADJUST 23 // cycles
|
|
|
|
#define READ_WRITE_WIDTH_ADJUST 10 // cycles
|
|
|
|
#elif SELECT_SERIAL_SPEED == 2
|
|
|
|
// Middle speed
|
|
|
|
#define SERIAL_DELAY 12 // micro sec
|
|
|
|
#define READ_WRITE_START_ADJUST 25 // cycles
|
|
|
|
#define READ_WRITE_WIDTH_ADJUST 10 // cycles
|
|
|
|
#elif SELECT_SERIAL_SPEED == 3
|
|
|
|
// Low speed
|
|
|
|
#define SERIAL_DELAY 24 // micro sec
|
|
|
|
#define READ_WRITE_START_ADJUST 25 // cycles
|
|
|
|
#define READ_WRITE_WIDTH_ADJUST 10 // cycles
|
|
|
|
#elif SELECT_SERIAL_SPEED == 4
|
|
|
|
// Very Low speed
|
|
|
|
#define SERIAL_DELAY 50 // micro sec
|
|
|
|
#define READ_WRITE_START_ADJUST 25 // cycles
|
|
|
|
#define READ_WRITE_WIDTH_ADJUST 10 // cycles
|
|
|
|
#else
|
|
|
|
#error Illegal Serial Speed
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
|
|
|
|
#define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
|
|
|
|
|
|
|
|
#define SLAVE_INT_WIDTH 1
|
|
|
|
#define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
|
2018-01-28 17:14:12 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-06-28 13:55:15 +00:00
|
|
|
|
|
|
|
inline static
|
|
|
|
void serial_delay_half1(void) {
|
|
|
|
_delay_us(SERIAL_DELAY_HALF1);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static
|
|
|
|
void serial_delay_half2(void) {
|
|
|
|
_delay_us(SERIAL_DELAY_HALF2);
|
2018-01-28 17:14:12 +00:00
|
|
|
}
|
2018-06-28 13:55:15 +00:00
|
|
|
|
2018-01-28 17:14:12 +00:00
|
|
|
inline static
|
|
|
|
void serial_output(void) {
|
|
|
|
SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make the serial pin an input with pull-up resistor
|
|
|
|
inline static
|
2018-06-28 13:55:15 +00:00
|
|
|
void serial_input_with_pullup(void) {
|
2018-01-28 17:14:12 +00:00
|
|
|
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) {
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_input_with_pullup();
|
2018-01-28 17:14:12 +00:00
|
|
|
|
2018-07-12 17:23:30 +00:00
|
|
|
#if SERIAL_PIN_MASK == _BV(PD0)
|
2018-01-28 17:14:12 +00:00
|
|
|
// Enable INT0
|
|
|
|
EIMSK |= _BV(INT0);
|
|
|
|
// Trigger on falling edge of INT0
|
|
|
|
EICRA &= ~(_BV(ISC00) | _BV(ISC01));
|
2018-07-12 17:23:30 +00:00
|
|
|
#elif SERIAL_PIN_MASK == _BV(PD2)
|
2018-01-28 17:14:12 +00:00
|
|
|
// Enable INT2
|
|
|
|
EIMSK |= _BV(INT2);
|
|
|
|
// Trigger on falling edge of INT2
|
|
|
|
EICRA &= ~(_BV(ISC20) | _BV(ISC21));
|
2018-07-12 17:23:30 +00:00
|
|
|
#else
|
|
|
|
#error unknown SERIAL_PIN_MASK value
|
2018-01-28 17:14:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-06-28 13:55:15 +00:00
|
|
|
// Used by the sender to synchronize timing with the reciver.
|
2018-01-28 17:14:12 +00:00
|
|
|
static
|
|
|
|
void sync_recv(void) {
|
2018-06-28 13:55:15 +00:00
|
|
|
for (int i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
|
|
|
|
}
|
2018-01-28 17:14:12 +00:00
|
|
|
// This shouldn't hang if the slave disconnects because the
|
|
|
|
// serial line will float to high if the slave does disconnect.
|
|
|
|
while (!serial_read_pin());
|
|
|
|
}
|
|
|
|
|
2018-06-28 13:55:15 +00:00
|
|
|
// Used by the reciver to send a synchronization signal to the sender.
|
2018-01-28 17:14:12 +00:00
|
|
|
static
|
|
|
|
void sync_send(void) {
|
|
|
|
serial_low();
|
|
|
|
serial_delay();
|
|
|
|
serial_high();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads a byte from the serial line
|
|
|
|
static
|
|
|
|
uint8_t serial_read_byte(void) {
|
|
|
|
uint8_t byte = 0;
|
2018-06-28 13:55:15 +00:00
|
|
|
_delay_sub_us(READ_WRITE_START_ADJUST);
|
2018-01-28 17:14:12 +00:00
|
|
|
for ( uint8_t i = 0; i < 8; ++i) {
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_delay_half1(); // read the middle of pulses
|
2018-01-28 17:14:12 +00:00
|
|
|
byte = (byte << 1) | serial_read_pin();
|
2018-06-28 13:55:15 +00:00
|
|
|
_delay_sub_us(READ_WRITE_WIDTH_ADJUST);
|
|
|
|
serial_delay_half2();
|
2018-01-28 17:14:12 +00:00
|
|
|
}
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends a byte with MSB ordering
|
|
|
|
static
|
|
|
|
void serial_write_byte(uint8_t data) {
|
2018-06-28 13:55:15 +00:00
|
|
|
uint8_t b = 1<<7;
|
|
|
|
while( b ) {
|
|
|
|
if(data & b) {
|
2018-01-28 17:14:12 +00:00
|
|
|
serial_high();
|
|
|
|
} else {
|
|
|
|
serial_low();
|
|
|
|
}
|
2018-06-28 13:55:15 +00:00
|
|
|
b >>= 1;
|
2018-01-28 17:14:12 +00:00
|
|
|
serial_delay();
|
|
|
|
}
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_low(); // sync_send() / senc_recv() need raise edge
|
2018-01-28 17:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// interrupt handle to be used by the slave device
|
|
|
|
ISR(SERIAL_PIN_INTERRUPT) {
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_output();
|
2018-01-28 17:14:12 +00:00
|
|
|
|
2018-06-28 13:55:15 +00:00
|
|
|
// slave send phase
|
2018-01-28 17:14:12 +00:00
|
|
|
uint8_t checksum = 0;
|
|
|
|
for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
|
|
|
|
sync_send();
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_write_byte(serial_slave_buffer[i]);
|
2018-01-28 17:14:12 +00:00
|
|
|
checksum += serial_slave_buffer[i];
|
|
|
|
}
|
|
|
|
sync_send();
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_write_byte(checksum);
|
2018-01-28 17:14:12 +00:00
|
|
|
|
2018-06-28 13:55:15 +00:00
|
|
|
// slave switch to input
|
|
|
|
sync_send(); //0
|
|
|
|
serial_delay_half1(); //1
|
|
|
|
serial_low(); //2
|
|
|
|
serial_input_with_pullup(); //2
|
|
|
|
serial_delay_half1(); //3
|
2018-01-28 17:14:12 +00:00
|
|
|
|
2018-06-28 13:55:15 +00:00
|
|
|
// slave recive phase
|
2018-01-28 17:14:12 +00:00
|
|
|
uint8_t checksum_computed = 0;
|
|
|
|
for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
|
2018-06-28 13:55:15 +00:00
|
|
|
sync_recv();
|
2018-01-28 17:14:12 +00:00
|
|
|
serial_master_buffer[i] = serial_read_byte();
|
|
|
|
checksum_computed += serial_master_buffer[i];
|
|
|
|
}
|
2018-06-28 13:55:15 +00:00
|
|
|
sync_recv();
|
2018-01-28 17:14:12 +00:00
|
|
|
uint8_t checksum_received = serial_read_byte();
|
|
|
|
|
|
|
|
if ( checksum_computed != checksum_received ) {
|
|
|
|
status |= SLAVE_DATA_CORRUPT;
|
|
|
|
} else {
|
|
|
|
status &= ~SLAVE_DATA_CORRUPT;
|
|
|
|
}
|
2018-06-28 13:55:15 +00:00
|
|
|
|
|
|
|
sync_recv(); //weit master output to high
|
2018-01-28 17:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
bool serial_slave_DATA_CORRUPT(void) {
|
|
|
|
return status & SLAVE_DATA_CORRUPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copies the serial_slave_buffer to the master and sends the
|
|
|
|
// serial_master_buffer to the slave.
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// 0 => no error
|
|
|
|
// 1 => slave did not respond
|
2018-06-28 13:55:15 +00:00
|
|
|
// 2 => checksum error
|
2018-01-28 17:14:12 +00:00
|
|
|
int serial_update_buffers(void) {
|
|
|
|
// this code is very time dependent, so we need to disable interrupts
|
|
|
|
cli();
|
|
|
|
|
|
|
|
// signal to the slave that we want to start a transaction
|
|
|
|
serial_output();
|
|
|
|
serial_low();
|
2018-06-28 13:55:15 +00:00
|
|
|
_delay_us(SLAVE_INT_WIDTH);
|
2018-01-28 17:14:12 +00:00
|
|
|
|
|
|
|
// wait for the slaves response
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_input_with_pullup();
|
|
|
|
_delay_us(SLAVE_INT_RESPONSE_TIME);
|
2018-01-28 17:14:12 +00:00
|
|
|
|
|
|
|
// check if the slave is present
|
|
|
|
if (serial_read_pin()) {
|
|
|
|
// slave failed to pull the line low, assume not present
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_output();
|
|
|
|
serial_high();
|
2018-01-28 17:14:12 +00:00
|
|
|
sei();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-28 13:55:15 +00:00
|
|
|
// master recive phase
|
2018-01-28 17:14:12 +00:00
|
|
|
// if the slave is present syncronize with it
|
|
|
|
|
|
|
|
uint8_t checksum_computed = 0;
|
|
|
|
// receive data from the slave
|
|
|
|
for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) {
|
|
|
|
sync_recv();
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_slave_buffer[i] = serial_read_byte();
|
2018-01-28 17:14:12 +00:00
|
|
|
checksum_computed += serial_slave_buffer[i];
|
|
|
|
}
|
|
|
|
sync_recv();
|
2018-06-28 13:55:15 +00:00
|
|
|
uint8_t checksum_received = serial_read_byte();
|
2018-01-28 17:14:12 +00:00
|
|
|
|
|
|
|
if (checksum_computed != checksum_received) {
|
2018-06-28 13:55:15 +00:00
|
|
|
serial_output();
|
|
|
|
serial_high();
|
2018-01-28 17:14:12 +00:00
|
|
|
sei();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2018-06-28 13:55:15 +00:00
|
|
|
// master switch to output
|
|
|
|
sync_recv(); //0
|
|
|
|
serial_delay(); //1
|
|
|
|
serial_low(); //3
|
|
|
|
serial_output(); // 3
|
|
|
|
serial_delay_half1(); //4
|
|
|
|
|
|
|
|
// master send phase
|
2018-01-28 17:14:12 +00:00
|
|
|
uint8_t checksum = 0;
|
2018-06-28 13:55:15 +00:00
|
|
|
|
2018-01-28 17:14:12 +00:00
|
|
|
for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) {
|
2018-06-28 13:55:15 +00:00
|
|
|
sync_send();
|
2018-01-28 17:14:12 +00:00
|
|
|
serial_write_byte(serial_master_buffer[i]);
|
|
|
|
checksum += serial_master_buffer[i];
|
|
|
|
}
|
2018-06-28 13:55:15 +00:00
|
|
|
sync_send();
|
2018-01-28 17:14:12 +00:00
|
|
|
serial_write_byte(checksum);
|
|
|
|
|
|
|
|
// always, release the line when not in use
|
2018-06-28 13:55:15 +00:00
|
|
|
sync_send();
|
2018-01-28 17:14:12 +00:00
|
|
|
|
|
|
|
sei();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|