diff --git a/libraries/AltSoftSerial/AltSoftSerial.cpp b/libraries/AltSoftSerial/AltSoftSerial.cpp index 80fae5e..833f0af 100644 --- a/libraries/AltSoftSerial/AltSoftSerial.cpp +++ b/libraries/AltSoftSerial/AltSoftSerial.cpp @@ -41,7 +41,7 @@ bool timing_error=false; -static uint8_t rx_state; +static volatile uint8_t rx_state; static uint8_t rx_byte; static uint8_t rx_bit = 0; static uint16_t rx_target; @@ -50,8 +50,9 @@ static volatile uint8_t rx_buffer_tail; #define RX_BUFFER_SIZE 80 static volatile uint8_t rx_buffer[RX_BUFFER_SIZE]; -const uint32_t cycles_per_bit = (ALTSS_BASE_FREQ + 4800 / 2) / 4800; -const uint16_t rx_stop_ticks= cycles_per_bit * 37 / 4; +const static uint32_t cycles_per_bit = 1667UL; +// (ALTSS_BASE_FREQ + 9600 / 2) / 9600UL; +const static uint32_t rx_stop_ticks = 15419UL; #ifndef INPUT_PULLUP #define INPUT_PULLUP INPUT @@ -61,7 +62,9 @@ const uint16_t rx_stop_ticks= cycles_per_bit * 37 / 4; void softserial_init() { - // rx_stop_ticks = cycles_per_bit * 37 / 4; + //Serial.printf("cycles_per_bit = %d\n", cycles_per_bit); + CONFIG_TIMER_NOPRESCALE(); + pinMode(INPUT_CAPTURE_PIN, INPUT_PULLUP); digitalWrite(OUTPUT_COMPARE_A_PIN, HIGH); pinMode(OUTPUT_COMPARE_A_PIN, OUTPUT); @@ -69,6 +72,7 @@ void softserial_init() rx_buffer_head = 0; rx_buffer_tail = 0; ENABLE_INT_INPUT_CAPTURE(); + DISABLE_INT_COMPARE_A(); } void softserial_end(void) @@ -80,16 +84,12 @@ void softserial_end(void) // TODO: restore timer to original settings? } -static uint8_t state, bit, head, tail, out; -ISR(COMPARE_A_INTERRUPT) -{ -} - /****************************************/ /** Reception **/ /****************************************/ ISR(CAPTURE_INTERRUPT) { + uint8_t bit, head, state; uint16_t capture, target; uint16_t offset, offset_overflow; @@ -142,6 +142,7 @@ ISR(CAPTURE_INTERRUPT) ISR(COMPARE_B_INTERRUPT) { + uint8_t state, bit, head; DISABLE_INT_COMPARE_B(); CONFIG_CAPTURE_FALLING_EDGE(); state = rx_state; @@ -163,7 +164,7 @@ ISR(COMPARE_B_INTERRUPT) const int softserial_read() { - + uint8_t head, tail, out; head = rx_buffer_head; tail = rx_buffer_tail; if (head == tail) return -1; @@ -175,6 +176,7 @@ const int softserial_read() const int softserial_available() { + uint8_t head, tail; head = rx_buffer_head; tail = rx_buffer_tail; if (head >= tail) return head - tail;