|
|
|
@ -85,7 +85,6 @@ void serial_delay_half2(void) {
|
|
|
|
|
inline static void serial_output(void) ALWAYS_INLINE;
|
|
|
|
|
inline static
|
|
|
|
|
void serial_output(void) {
|
|
|
|
|
debug_output_mode();
|
|
|
|
|
SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -93,7 +92,6 @@ void serial_output(void) {
|
|
|
|
|
inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
|
|
|
|
|
inline static
|
|
|
|
|
void serial_input_with_pullup(void) {
|
|
|
|
|
debug_input_mode();
|
|
|
|
|
SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
|
|
|
|
|
SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
|
|
|
|
|
}
|
|
|
|
@ -117,7 +115,6 @@ void serial_high(void) {
|
|
|
|
|
|
|
|
|
|
void soft_serial_initiator_init(SSTD_t *sstd_table)
|
|
|
|
|
{
|
|
|
|
|
serial_debug_init();
|
|
|
|
|
Transaction_table = sstd_table;
|
|
|
|
|
serial_output();
|
|
|
|
|
serial_high();
|
|
|
|
@ -125,7 +122,6 @@ void soft_serial_initiator_init(SSTD_t *sstd_table)
|
|
|
|
|
|
|
|
|
|
void soft_serial_target_init(SSTD_t *sstd_table)
|
|
|
|
|
{
|
|
|
|
|
serial_debug_init();
|
|
|
|
|
Transaction_table = sstd_table;
|
|
|
|
|
serial_input_with_pullup();
|
|
|
|
|
|
|
|
|
@ -148,25 +144,20 @@ void soft_serial_target_init(SSTD_t *sstd_table)
|
|
|
|
|
static void sync_recv(void) NO_INLINE;
|
|
|
|
|
static
|
|
|
|
|
void sync_recv(void) {
|
|
|
|
|
debug_sync_start();
|
|
|
|
|
for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
|
|
|
|
|
debug_sync_end(); debug_sync_start();
|
|
|
|
|
}
|
|
|
|
|
// This shouldn't hang if the target disconnects because the
|
|
|
|
|
// serial line will float to high if the target does disconnect.
|
|
|
|
|
while (!serial_read_pin());
|
|
|
|
|
debug_sync_end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Used by the reciver to send a synchronization signal to the sender.
|
|
|
|
|
static void sync_send(void)NO_INLINE;
|
|
|
|
|
static
|
|
|
|
|
void sync_send(void) {
|
|
|
|
|
debug_sync_start();
|
|
|
|
|
serial_low();
|
|
|
|
|
serial_delay();
|
|
|
|
|
serial_high();
|
|
|
|
|
debug_sync_end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reads a byte from the serial line
|
|
|
|
@ -177,29 +168,21 @@ static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
|
|
|
|
|
_delay_sub_us(READ_WRITE_START_ADJUST);
|
|
|
|
|
for( i = 0, byte = 0, p = 0; i < bit; i++ ) {
|
|
|
|
|
serial_delay_half1(); // read the middle of pulses
|
|
|
|
|
debug_recvsample();
|
|
|
|
|
if( serial_read_pin() ) {
|
|
|
|
|
byte = (byte << 1) | 1; p ^= 1;
|
|
|
|
|
} else {
|
|
|
|
|
byte = (byte << 1) | 0; p ^= 0;
|
|
|
|
|
}
|
|
|
|
|
debug_recvsample();
|
|
|
|
|
_delay_sub_us(READ_WRITE_WIDTH_ADJUST);
|
|
|
|
|
serial_delay_half2();
|
|
|
|
|
debug_dummy_delay_recv();
|
|
|
|
|
}
|
|
|
|
|
/* recive parity bit */
|
|
|
|
|
serial_delay_half1(); // read the middle of pulses
|
|
|
|
|
debug_recvsample();
|
|
|
|
|
pb = serial_read_pin();
|
|
|
|
|
debug_recvsample();
|
|
|
|
|
_delay_sub_us(READ_WRITE_WIDTH_ADJUST);
|
|
|
|
|
serial_delay_half2();
|
|
|
|
|
debug_dummy_delay_recv();
|
|
|
|
|
|
|
|
|
|
if( p == pb ) debug_parity_on();
|
|
|
|
|
*pterrcount += (p != pb)? 1 : 0;
|
|
|
|
|
debug_parity_off();
|
|
|
|
|
|
|
|
|
|
return byte;
|
|
|
|
|
}
|
|
|
|
@ -214,18 +197,12 @@ void serial_write_chunk(uint8_t data, uint8_t bit) {
|
|
|
|
|
} else {
|
|
|
|
|
serial_low(); p ^= 0;
|
|
|
|
|
}
|
|
|
|
|
debug_recvsample();
|
|
|
|
|
serial_delay();
|
|
|
|
|
debug_recvsample();
|
|
|
|
|
debug_dummy_delay_send();
|
|
|
|
|
}
|
|
|
|
|
/* send parity bit */
|
|
|
|
|
if(p & 1) { serial_high(); }
|
|
|
|
|
else { serial_low(); }
|
|
|
|
|
debug_recvsample();
|
|
|
|
|
serial_delay();
|
|
|
|
|
debug_recvsample();
|
|
|
|
|
debug_dummy_delay_send();
|
|
|
|
|
|
|
|
|
|
serial_low(); // sync_send() / senc_recv() need raise edge
|
|
|
|
|
}
|
|
|
|
@ -237,9 +214,7 @@ void serial_send_packet(uint8_t *buffer, uint8_t size) {
|
|
|
|
|
uint8_t data;
|
|
|
|
|
data = buffer[i];
|
|
|
|
|
sync_send();
|
|
|
|
|
debug_bytewidth_start();
|
|
|
|
|
serial_write_chunk(data,8);
|
|
|
|
|
debug_bytewidth_end();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -250,9 +225,7 @@ uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
|
|
|
|
|
for (uint8_t i = 0; i < size; ++i) {
|
|
|
|
|
uint8_t data;
|
|
|
|
|
sync_recv();
|
|
|
|
|
debug_bytewidth_start();
|
|
|
|
|
data = serial_read_chunk(&pecount, 8);
|
|
|
|
|
debug_bytewidth_end();
|
|
|
|
|
buffer[i] = data;
|
|
|
|
|
}
|
|
|
|
|
return pecount == 0;
|
|
|
|
@ -278,9 +251,6 @@ void change_reciver2sender(void) {
|
|
|
|
|
|
|
|
|
|
// interrupt handle to be used by the target device
|
|
|
|
|
ISR(SERIAL_PIN_INTERRUPT) {
|
|
|
|
|
debug_output_mode(); debug_input_mode(); // indicate intterupt entry
|
|
|
|
|
debug_recvsample(); debug_recvsample(); // indicate intterupt entry
|
|
|
|
|
debug_sync_start(); debug_sync_end(); // indicate intterupt entry
|
|
|
|
|
|
|
|
|
|
#ifndef SERIAL_USE_MULTI_TRANSACTION
|
|
|
|
|
serial_low();
|
|
|
|
@ -291,9 +261,7 @@ ISR(SERIAL_PIN_INTERRUPT) {
|
|
|
|
|
uint8_t tid;
|
|
|
|
|
uint8_t pecount = 0;
|
|
|
|
|
sync_recv();
|
|
|
|
|
debug_bytewidth_start();
|
|
|
|
|
tid = serial_read_chunk(&pecount,4);
|
|
|
|
|
debug_bytewidth_end();
|
|
|
|
|
if(pecount> 0)
|
|
|
|
|
return;
|
|
|
|
|
serial_delay_half1();
|
|
|
|
@ -318,16 +286,13 @@ ISR(SERIAL_PIN_INTERRUPT) {
|
|
|
|
|
trans->initiator2target_buffer_size) ) {
|
|
|
|
|
*trans->status = TRANSACTION_ACCEPTED;
|
|
|
|
|
} else {
|
|
|
|
|
debug_parity_on();
|
|
|
|
|
*trans->status = TRANSACTION_DATA_ERROR;
|
|
|
|
|
debug_parity_off();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
*trans->status = TRANSACTION_ACCEPTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sync_recv(); //weit initiator output to high
|
|
|
|
|
debug_output_mode(); debug_input_mode(); // indicate intterupt exit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/////////
|
|
|
|
@ -372,21 +337,17 @@ int soft_serial_transaction(int sstd_index) {
|
|
|
|
|
#else
|
|
|
|
|
// send transaction table index
|
|
|
|
|
sync_send();
|
|
|
|
|
debug_bytewidth_start();
|
|
|
|
|
_delay_sub_us(TID_SEND_ADJUST);
|
|
|
|
|
serial_write_chunk(sstd_index, 4);
|
|
|
|
|
debug_bytewidth_end();
|
|
|
|
|
serial_delay_half1();
|
|
|
|
|
|
|
|
|
|
// wait for the target response (step1 low->high)
|
|
|
|
|
serial_input_with_pullup();
|
|
|
|
|
while( !serial_read_pin() ) {
|
|
|
|
|
_delay_sub_us(2);
|
|
|
|
|
debug_output_mode(); debug_input_mode(); // indicate check pin timeing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if the target is present (step2 high->low)
|
|
|
|
|
debug_output_mode(); _delay_sub_us(1); debug_input_mode(); // indicate check pin timeing
|
|
|
|
|
for( int i = 0; serial_read_pin(); i++ ) {
|
|
|
|
|
if (i > SLAVE_INT_ACK_WIDTH + 1) {
|
|
|
|
|
// slave failed to pull the line low, assume not present
|
|
|
|
@ -396,9 +357,7 @@ int soft_serial_transaction(int sstd_index) {
|
|
|
|
|
sei();
|
|
|
|
|
return TRANSACTION_NO_RESPONSE;
|
|
|
|
|
}
|
|
|
|
|
debug_output_mode();
|
|
|
|
|
_delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
|
|
|
|
|
debug_input_mode(); // indicate check pin timeing
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -407,10 +366,8 @@ int soft_serial_transaction(int sstd_index) {
|
|
|
|
|
if( trans->target2initiator_buffer_size > 0 ) {
|
|
|
|
|
if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
|
|
|
|
|
trans->target2initiator_buffer_size) ) {
|
|
|
|
|
debug_parity_on();
|
|
|
|
|
serial_output();
|
|
|
|
|
serial_high();
|
|
|
|
|
debug_parity_off();
|
|
|
|
|
*trans->status = TRANSACTION_DATA_ERROR;
|
|
|
|
|
sei();
|
|
|
|
|
return TRANSACTION_DATA_ERROR;
|
|
|
|
@ -428,7 +385,6 @@ int soft_serial_transaction(int sstd_index) {
|
|
|
|
|
|
|
|
|
|
// always, release the line when not in use
|
|
|
|
|
sync_send();
|
|
|
|
|
debug_input_mode(); debug_output_mode(); // indicate intterupt exit
|
|
|
|
|
|
|
|
|
|
*trans->status = TRANSACTION_END;
|
|
|
|
|
sei();
|
|
|
|
|