mirror of
https://github.com/halleysfifthinc/AVCLAN-Mockingboard.git
synced 2026-08-07 01:13:18 +00:00
Complete the switch to jnk0le uart lib
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+24
-31
@@ -29,9 +29,10 @@
|
||||
No acknowledge bits are sent for broadcast frames.
|
||||
*/
|
||||
|
||||
#include "bus.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
#include "avclan.h"
|
||||
#include "com232.h"
|
||||
#include "bus.hpp"
|
||||
#include "frame.hpp"
|
||||
#include "hal/phy.h" // bridge until phy has been ported
|
||||
|
||||
@@ -264,33 +265,29 @@ auto Bus::read(uint16_t address, Frame *in, Frame::Print print) -> Error::Read {
|
||||
|
||||
if (false) { // NOLINT(readability-simplify-boolean-expr)
|
||||
handle_err:;
|
||||
RS232_Print("ERR(read): ");
|
||||
fputs("ERR(read): ", stdout);
|
||||
switch (err.errno) {
|
||||
case BAD_STARTBIT: RS232_Print("bad start bit (other)"); break;
|
||||
case STARTBIT_TOO_SHORT: RS232_Print("bad start bit (short)"); break;
|
||||
case STARTBIT_TOO_LONG: RS232_Print("bad start bit (long)"); break;
|
||||
case BAD_STARTBIT: fputs("bad start bit (other)", stdout); break;
|
||||
case STARTBIT_TOO_SHORT: fputs("bad start bit (short)", stdout); break;
|
||||
case STARTBIT_TOO_LONG: fputs("bad start bit (long)", stdout); break;
|
||||
case BAD_CONTROLLER_PARITY:
|
||||
RS232_Print("reading controller addr.");
|
||||
fputs("reading controller addr.", stdout);
|
||||
goto VERBOSE;
|
||||
case BAD_PERIPHERAL_PARITY:
|
||||
RS232_Print("reading peripheral addr.");
|
||||
fputs("reading peripheral addr.", stdout);
|
||||
goto VERBOSE;
|
||||
case BAD_CONTROL_PARITY: RS232_Print("reading control"); goto VERBOSE;
|
||||
case BAD_LENGTH_PARITY: RS232_Print("reading length"); goto VERBOSE;
|
||||
case BAD_LENGTH_RANGE:
|
||||
RS232_Print("bad length 0x");
|
||||
RS232_PrintHex4(err.val);
|
||||
break;
|
||||
case BAD_DATA_PARITY: RS232_Print("reading data"); goto VERBOSE;
|
||||
case BAD_CONTROL_PARITY: fputs("reading control", stdout); goto VERBOSE;
|
||||
case BAD_LENGTH_PARITY: fputs("reading length", stdout); goto VERBOSE;
|
||||
case BAD_LENGTH_RANGE: printf("bad length 0x%X", err.val & 0x0F); break;
|
||||
case BAD_DATA_PARITY: fputs("reading data", stdout); goto VERBOSE;
|
||||
case BAD_PARITY:
|
||||
__builtin_unreachable();
|
||||
VERBOSE:
|
||||
if (print.verbose) {
|
||||
RS232_Print("; read 0x");
|
||||
RS232_PrintHex(err.val);
|
||||
printf("; read 0x%X", err.val);
|
||||
}
|
||||
}
|
||||
RS232_Print("\n");
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
// Only print if some data has been correctly received
|
||||
@@ -370,32 +367,28 @@ auto Bus::send(const Frame *out, Frame::Print print) -> Error::Send {
|
||||
// back to read mode
|
||||
if (false) { // NOLINT(readability-simplify-boolean-expr)
|
||||
handle_err:;
|
||||
RS232_Print("Error");
|
||||
fputs("Error", stdout);
|
||||
switch (err.errno) {
|
||||
case MUTED: RS232_Print(": Device muted"); break;
|
||||
case BUSY: RS232_Print(": Busy bus"); break;
|
||||
case MUTED: fputs(": Device muted", stdout); break;
|
||||
case BUSY: fputs(": Busy bus", stdout); break;
|
||||
case NAK_ADDRESS:
|
||||
case NAK_CONTROL:
|
||||
case NAK_MESSAGE_LENGTH:
|
||||
case NAK_DATA:
|
||||
case NAK:
|
||||
RS232_Print(" NAK: ");
|
||||
fputs(" NAK: ", stdout);
|
||||
switch (err.errno) {
|
||||
case NAK_ADDRESS: RS232_Print("address"); break;
|
||||
case NAK_CONTROL: RS232_Print("Control"); break;
|
||||
case NAK_MESSAGE_LENGTH: RS232_Print("Message length"); break;
|
||||
case NAK_DATA:
|
||||
RS232_Print(" data[");
|
||||
RS232_PrintDec(err.val);
|
||||
RS232_Print("]");
|
||||
break;
|
||||
case NAK_ADDRESS: fputs("address", stdout); break;
|
||||
case NAK_CONTROL: fputs("Control", stdout); break;
|
||||
case NAK_MESSAGE_LENGTH: fputs("Message length", stdout); break;
|
||||
case NAK_DATA: printf(" data[%u]", err.val); break;
|
||||
case NAK:
|
||||
case MUTED:
|
||||
case BUSY: __builtin_unreachable();
|
||||
}
|
||||
break;
|
||||
}
|
||||
RS232_Print("\n");
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
if (print.print)
|
||||
|
||||
+16
-27
@@ -4,9 +4,9 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "com232.h" // error logging
|
||||
#include "frame.hpp"
|
||||
|
||||
namespace {
|
||||
@@ -30,33 +30,25 @@ void Frame::print(Frame::Print print) const {
|
||||
|
||||
*bptr++ = control;
|
||||
*bptr++ = length;
|
||||
RS232_sendbytes(buffer, 8);
|
||||
RS232_sendbytes(data, length);
|
||||
fwrite(buffer, 1, 8, stdout);
|
||||
fwrite(data, 1, length, stdout);
|
||||
|
||||
bptr = buffer;
|
||||
*bptr++ = 0x17; // End of transmission block
|
||||
*bptr++ = 0x0D; // \r
|
||||
*bptr++ = 0x0A; // \n
|
||||
RS232_sendbytes(buffer, 3);
|
||||
fwrite(buffer, 1, 3, stdout);
|
||||
} else {
|
||||
RS232_PrintHex4(static_cast<uint8_t>(is_unicast));
|
||||
|
||||
RS232_Print(" 0x");
|
||||
RS232_PrintHex12(controller_addr);
|
||||
RS232_Print(" 0x");
|
||||
RS232_PrintHex12(peripheral_addr);
|
||||
|
||||
RS232_Print(" 0x");
|
||||
RS232_PrintHex4(control);
|
||||
|
||||
RS232_Print(" 0x");
|
||||
RS232_PrintHex4(length);
|
||||
printf("%X", static_cast<unsigned>(is_unicast));
|
||||
printf(" 0x%03X", static_cast<unsigned>(controller_addr & 0x0FFF));
|
||||
printf(" 0x%03X", static_cast<unsigned>(peripheral_addr & 0x0FFF));
|
||||
printf(" 0x%X", static_cast<unsigned>(control & 0x0F));
|
||||
printf(" 0x%X", static_cast<unsigned>(length & 0x0F));
|
||||
|
||||
for (uint8_t i = 0; i < length; i++) {
|
||||
RS232_Print(" 0x");
|
||||
RS232_PrintHex8(data[i]);
|
||||
printf(" 0x%02X", static_cast<unsigned>(data[i]));
|
||||
}
|
||||
RS232_Print("\n");
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,21 +88,18 @@ Error::Parse Frame::parse(const uint8_t *bytes, uint8_t len) {
|
||||
|
||||
if (false) { // NOLINT(readability-simplify-boolean-expr)
|
||||
handle_err:;
|
||||
RS232_Print("ERR(parse): ");
|
||||
fputs("ERR(parse): ", stdout);
|
||||
switch (err.errno) {
|
||||
case TOO_SHORT:
|
||||
RS232_Print("not enough bytes too fill AVCLAN frame");
|
||||
break;
|
||||
case TOO_SHORT: puts("not enough bytes too fill AVCLAN frame"); break;
|
||||
case MISMATCH_LENGTH:
|
||||
RS232_Print("frame->length is longer than remaining data");
|
||||
puts("frame->length is longer than remaining data");
|
||||
break;
|
||||
case LENGTH_TOO_BIG:
|
||||
RS232_Print("frame->length exceeds MAXLENGTH: 0x");
|
||||
RS232_PrintHex8(err.val);
|
||||
printf("frame->length exceeds MAXLENGTH: 0x%02X\n",
|
||||
static_cast<unsigned>(err.val));
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
RS232_Print("\n");
|
||||
}
|
||||
|
||||
return err.errno;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright (C) 2026 Allen Hill <allenofthehills@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Generic stdio interface initialization. All user I/O goes through <stdio.h>
|
||||
// functions. Assumptions/invariants:
|
||||
// - stdin MUST be non-blocking (ie. getchar() returns EOF immediately when
|
||||
// empty). Necessary to avoid stalling the REPL poll loop.
|
||||
// - stdout is *raw* — no '\n' -> "\r\n" translation: bare LF and binary frame
|
||||
// payloads (fwrite) are emitted verbatim
|
||||
void stdio_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -7,7 +7,9 @@ target_sources(avclan PRIVATE
|
||||
phy_avr.c
|
||||
media_avr.c
|
||||
cd_timer_avr.c
|
||||
board_avr.c)
|
||||
board_avr.c
|
||||
stdio_avr.c
|
||||
)
|
||||
|
||||
target_include_directories(avclan PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
@@ -63,10 +65,10 @@ set_property(CACHE TCB_CLKSEL PROPERTY STRINGS
|
||||
TCB_CLKSEL_CLKTCA_gc
|
||||
)
|
||||
|
||||
set(USART_RXMODE "USART_RXMODE_CLK2X_gc" CACHE STRING "USART at normal or double speed operation")
|
||||
set(USART_RXMODE "NORMAL" CACHE STRING "USART at normal or double speed operation")
|
||||
set_property(CACHE USART_RXMODE PROPERTY STRINGS
|
||||
USART_RXMODE_CLK2X_gc
|
||||
USART_RXMODE_NORMAL_gc
|
||||
NORMAL
|
||||
DOUBLE_SPEED
|
||||
)
|
||||
|
||||
# Measured wall-clock duration (ms) of one nominal 32768-tick RTC period, used
|
||||
@@ -118,6 +120,20 @@ if(NOT LIBC_VERSION_TEST)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# --- Vendored serial driver (jnk0le AVR-UART-lib), adapted for avrxmega3 -----
|
||||
# usart_config.h set to use the reserved-ISR-register optimization, so r2/r3/r4
|
||||
# must be held off-limits in every TU that shares the MCU.
|
||||
set(JNK0LE_UART_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/jnk0le-AVR-UART-lib)
|
||||
add_library(jnk0le_uart STATIC ${JNK0LE_UART_DIR}/usart.c)
|
||||
target_include_directories(jnk0le_uart SYSTEM PUBLIC ${JNK0LE_UART_DIR})
|
||||
target_compile_definitions(jnk0le_uart
|
||||
PUBLIC $<$<STREQUAL:${USART_RXMODE},DOUBLE_SPEED>:USE_DOUBLE_SPEED>
|
||||
PRIVATE F_CPU=${FREQSEL}
|
||||
)
|
||||
target_compile_options(jnk0le_uart PUBLIC
|
||||
-ffixed-r2 -ffixed-r3 -ffixed-r4) # reserved ISR scratch (SREG=r4, Z=r2:r3)
|
||||
target_link_libraries(avclan PUBLIC jnk0le_uart)
|
||||
|
||||
# --- Compile definitions / options -----------------------------------------
|
||||
target_compile_definitions(avclan PUBLIC
|
||||
FREQSEL=${FREQSEL}
|
||||
@@ -125,7 +141,6 @@ target_compile_definitions(avclan PUBLIC
|
||||
CLK_PRESCALE_DIV=${CLK_PRESCALE_DIV}
|
||||
__CLK_PRESCALE_DIV=__${CLK_PRESCALE_DIV}
|
||||
TCB_CLKSEL=${TCB_CLKSEL}
|
||||
USART_RXMODE=${USART_RXMODE}
|
||||
RTC_STATUS_PERIOD_MS=${RTC_STATUS_PERIOD_MS}
|
||||
)
|
||||
target_compile_options(avclan PUBLIC
|
||||
|
||||
@@ -7,17 +7,35 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/sfr_defs.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <util/atomic.h>
|
||||
|
||||
#include "hal/phy.h"
|
||||
#include "com232.h" // RS232_setRxInterrupt (guard); RS232_Print (Measure)
|
||||
#include "media_avr.h" // media_sync_during_mask (guard)
|
||||
#include "media_avr.h" // media_sync_during_mask (guard)
|
||||
#include "hal/cd_timer.h" // statustimer_enable/disable (guard)
|
||||
|
||||
// F_CPU + TICK_US (timing.h) defined here; F_CPU potentially needed by
|
||||
// avr-libc.
|
||||
#include "timing_avr.h"
|
||||
|
||||
// USART0 TX ring indices owned by the jnk0le lib; the guard consults them to
|
||||
// decide whether to resume the TX drain (DRE interrupt) on leave.
|
||||
extern volatile uint8_t tx0_Head, tx0_Tail;
|
||||
|
||||
// Mask/unmask the USART interrupts during bit-banged AVC-LAN framing. TX is
|
||||
// interrupt-driven, so the DRE (data-register-empty) interrupt is gated
|
||||
// alongside RX; on re-enable, resume the TX drain only if bytes are still
|
||||
// queued (enabling DREIE on an empty ring would transmit garbage).
|
||||
static void console_set_irqs(bool enable) {
|
||||
if (enable) {
|
||||
USART0.CTRLA |= USART_RXCIE_bm;
|
||||
if (tx0_Head != tx0_Tail)
|
||||
USART0.CTRLA |= USART_DREIE_bm;
|
||||
} else {
|
||||
USART0.CTRLA &= ~(USART_RXCIE_bm | USART_DREIE_bm);
|
||||
}
|
||||
}
|
||||
|
||||
// Name difference between avr-libc and Microchip pack
|
||||
#if defined(EVSYS_ASYNCCH00_bm)
|
||||
#define EVSYS_ASYNCCH0_0_bm EVSYS_ASYNCCH00_bm
|
||||
@@ -405,7 +423,7 @@ bool phy_send_startbit() {
|
||||
void phy_guard_enter() {
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
cdtimer_disable();
|
||||
RS232_setRxInterrupt(false);
|
||||
console_set_irqs(false);
|
||||
media_sync_during_guard();
|
||||
}
|
||||
}
|
||||
@@ -414,7 +432,7 @@ void phy_guard_enter() {
|
||||
void phy_guard_leave() {
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
cdtimer_restore(); // Reenable status interrupt if currently playing
|
||||
RS232_setRxInterrupt(true);
|
||||
console_set_irqs(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,9 +449,8 @@ void phy_measure() {
|
||||
|
||||
uint8_t tmp = 0;
|
||||
|
||||
RS232_Print(
|
||||
"Timing config: F_CPU=" STR(F_CPU) ", TCB_CLKSEL=" STR(TCB_CLKSEL) "\n");
|
||||
RS232_Print("Sampling bit (pulse-width and period) timing...\n");
|
||||
puts("Timing config: F_CPU=" STR(F_CPU) ", TCB_CLKSEL=" STR(TCB_CLKSEL));
|
||||
puts("Sampling bit (pulse-width and period) timing...");
|
||||
|
||||
for (uint8_t n = 0; n < 100; n++) {
|
||||
while (pulse_count == tmp) {}
|
||||
@@ -442,20 +459,16 @@ void phy_measure() {
|
||||
tmp = pulse_count;
|
||||
}
|
||||
|
||||
RS232_Print("Pulses:\n");
|
||||
puts("Pulses:");
|
||||
for (uint8_t i = 0; i < 100; i++) {
|
||||
RS232_PrintHex8((uint8_t)(pulses[i] >> 8));
|
||||
RS232_PrintHex8((uint8_t)pulses[i]);
|
||||
RS232_Print("\n");
|
||||
printf("%04X\n", pulses[i]);
|
||||
}
|
||||
|
||||
RS232_Print("Periods:\n");
|
||||
puts("Periods:");
|
||||
for (uint8_t i = 0; i < 100; i++) {
|
||||
RS232_PrintHex8((uint8_t)(periods[i] >> 8));
|
||||
RS232_PrintHex8((uint8_t)periods[i]);
|
||||
RS232_Print("\n");
|
||||
printf("%04X\n", periods[i]);
|
||||
}
|
||||
RS232_Print("\nDone.\n");
|
||||
puts("\nDone.");
|
||||
|
||||
phy_guard_leave();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2026 Allen Hill <allenofthehills@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "hal/stdio.h"
|
||||
#include "timing_avr.h" // IWYU pragma: export
|
||||
#include "usart.h" // jnk0le AVR-UART-lib
|
||||
|
||||
// Raw stdout backend: emit the byte verbatim, no '\n' -> "\r\n" translation
|
||||
// (unlike the lib's uart_putchar). Keeps binary frame payloads intact.
|
||||
static int stdio_putchar(char data, FILE *stream) {
|
||||
(void)stream;
|
||||
uart0_putc(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Non-blocking stdin backend: next received byte, or _FDEV_EOF when the RX ring
|
||||
// is empty (uart0_getData() returns a negative value when there is no data).
|
||||
static int stdio_getchar(FILE *stream) {
|
||||
(void)stream;
|
||||
int16_t c = uart0_getData();
|
||||
return c < 0 ? _FDEV_EOF : c;
|
||||
}
|
||||
|
||||
// One raw RW stream for the stdio UART: verbatim byte output, non-blocking
|
||||
// input.
|
||||
static FILE stdio_stream =
|
||||
FDEV_SETUP_STREAM(stdio_putchar, stdio_getchar, _FDEV_SETUP_RW);
|
||||
|
||||
void stdio_init(void) {
|
||||
// The lib's uart0_init configures the USART registers/baud but not the pins;
|
||||
// keep the ATtiny3216 pin-mux the old driver did.
|
||||
PORTMUX.CTRLB = PORTMUX_USART0_ALTERNATE_gc; // TxD/RxD on PA1/PA2
|
||||
PORTA.DIRSET = PIN1_bm; // TxD output
|
||||
PORTA.DIRCLR = PIN2_bm; // RxD input
|
||||
|
||||
#ifdef USE_DOUBLE_SPEED
|
||||
uart0_init(DOUBLE_BAUD_CALC(1200000));
|
||||
#else
|
||||
uart0_init(BAUD_CALC(1200000));
|
||||
#endif
|
||||
|
||||
stdout = stdin = &stdio_stream; // printf/fputs/fwrite + non-blocking getchar
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 jnk0le
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
# AVR-UART-lib
|
||||
An interrupt-driven USART (RS232) library for AVR microcontrollers, with support for multiple hardware UARTs, using ring
|
||||
buffers for receive/transmit. Designed especially for real-time or high throughput applications, with narrow resource limits.
|
||||
|
||||
## Features
|
||||
- selectable support for up to 4 USART's
|
||||
- up to 255 byte FIFOs
|
||||
- only 2 (+1 wasted slot) byte memory footprint except actual buffer for every implemented RX or TX path
|
||||
- no dynamic memory allocations
|
||||
- extremly light interrupts
|
||||
- software(RTS/CTS) and hardware flow control support (DCE naming)
|
||||
- RS 485 compatibility
|
||||
- MPCM master and slave mode support
|
||||
- printf()/scanf() streams compatibility
|
||||
- V-USB compatibility (25 cycle ISR restriction)
|
||||
- optimized as much as possible to reduce code size and execution times
|
||||
|
||||
## Notes
|
||||
- Lot of terminals sends only CR character as a newline terminator, instead of CRLF or even unix style LF
|
||||
(BTW PuTTY doesn't allow to change this) but in return requires CRLF terminator to show not broken text.
|
||||
This behaviour can be covered by RX_NEWLINE_MODE macro, by default set to CRLF.
|
||||
|
||||
- 0 - CR
|
||||
- 1 - LF
|
||||
- 2 - CRLF (default)
|
||||
|
||||
- In order to reinitialize uart baudrate after any traffic was occurring, function uart_reinit() have to be used. (memory FIFO buffers will not be flushed)
|
||||
|
||||
- Any used IO pin have to be accessible from bottom IO address space. (eg. few ports on mega2560 cannot be used as a control IO)
|
||||
|
||||
- In half duplex (RS485) transmission modes, the application code is responsible of starting transmission only when bus is idle.
|
||||
If RE and DE are shorted together additional pullup on RX pin is required.
|
||||
Pin used as a RS485 control line have to be kept in low state during boot process via a pulldown resistor or at least not driving it high even by an internall pull-up. (especially on multi node buses)
|
||||
|
||||
- In MPCM mode first received byte is address by which device was called (own or general call), application is also responsible of restoring into "idle listening" state withing described bus silence time after receiving entire packet.
|
||||
|
||||
- uart_putc() function is not thread/interrupt-safe nor reentrant. It shouldn't be called from within atomic blocks or interrupt handlers since it re-enables interrupt flag on exit or even hangs in infinite loop waiting for execution of UDRE interrupt.
|
||||
|
||||
- this library is written in c99/gnu99 dialect standard.
|
||||
|
||||
## Flow control
|
||||
|
||||
This library provides a software implementation of CTS/RTS control lines (DTR/DSR respectively) to signal whether buffer is full (not an 80' style handshaking).
|
||||
It should be used, in order to achieve stable bidirectional >100kbps transmissions, especially when using non deterministic usb-serial converters (ft232, ch340 etc).
|
||||
The legendary "noise" that causes losing blocks of characters (not damaging) can also be fixed by that.
|
||||
|
||||
### Config
|
||||
- soft CTS pins have to be configured as an input, and both edge interrupt source (INT/PCINT), before entering uart_init().
|
||||
The application code should call cts_isr_handlers from interrupts corresponding to used pins. (see [example](example(flow control).c))
|
||||
If CTS line goes high during transmission (before UDRE interrupt is fired), only one additional byte will be transmitted. (due to 2 level transmit register)
|
||||
|
||||
- soft RTS pins have to be configured as input without pullup or output in low state, before entering uart_init().
|
||||
If interrupts are not missed, the receiver can accept up to 2 additional bytes after buffer is filled, one from next or ongoing transmission
|
||||
and another one if transmitter misses RTS signal (last one is stored in shift register).
|
||||
|
||||
- Any used IO pin have to be accessible from bottom IO address space. (eg. few ports on mega2560 cannot be used as a flow control IO)
|
||||
|
||||
- For proper operation of hardware RTS, USART_EXTEND_RX_BUFFER have to be defined.
|
||||
|
||||
## ISR timmings (cycles)
|
||||
|
||||
| ISR case | attiny2313 | atmega8 | atmega328 | atmega2560 | lgt8f88A |
|
||||
| --- | --- | --- | --- | --- | --- |
|
||||
| **TX best** | 37 | 40 | 40 | 42 | 30 |
|
||||
| **TX worst** | 38 | 41 | 44 | 46 | 35 |
|
||||
| **RX best** | 36 | 36 | 37 | 39 | 27 |
|
||||
| **RX worst** | 40 | 42 | 43 | 45 | 32 |
|
||||
| **RX rts full** | 41 | 41 | 44 | 46 | 32 |
|
||||
| **RX rts normal** | 40 | 42 | 43 | 45 | 32 |
|
||||
| **BUFFER_SIZE = 256** | x | -1 | -1 | -1 | -1 |
|
||||
| **G_SREG_SAVE** | -4 | -4 | -4 | -4 | -2 |
|
||||
| **G_Z_SAVE** | -6 | -6 | -6 | -6 | -2 |
|
||||
|
||||
- TX best case - send byte from buffer
|
||||
- TX worst case - send byte from buffer and disable UDRIE interrupt (will not generate double shot)
|
||||
- RX best case - load byte and do nothing (buffer full)
|
||||
- RX worst case - load byte and put it into buffer
|
||||
- RX rts full - rise RTS line and disable RXCIE interrupt
|
||||
- RX rts normal - load byte and put it into buffer if there is available space
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,431 @@
|
||||
/*!
|
||||
* \brief
|
||||
*
|
||||
* \author Jan Oleksiewicz <jnk0le@hotmail.com>
|
||||
* \license SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#ifndef USART_CONFIG_H_
|
||||
#define USART_CONFIG_H_
|
||||
|
||||
// DO NOT DEFINE F_CPU, BUFFERS SIZES OR ANY OTHER SHARED MACROS IN 'main.c'
|
||||
// CODE instead of this, define it in makefile (-D flag) or "Project Properties
|
||||
// -> AVR C Compiler -> Symbols"
|
||||
|
||||
// #define NO_USART_RX // disable all receiver code and dependencies
|
||||
// #define NO_USART_TX // disable all transmitter code and dependencies
|
||||
|
||||
// globally enable MPCM operation mode
|
||||
// 9 bit data frame only
|
||||
// always set frame format to 8 data bits
|
||||
// #define USART_MPCM_MODE
|
||||
|
||||
// enables double speed for all available USART interfaces
|
||||
// #define USE_DOUBLE_SPEED
|
||||
|
||||
// echoes back received characters in getchar() function (for reading in
|
||||
// scanf())
|
||||
#define RX_STDIO_GETCHAR_ECHO
|
||||
// #define RX_GETC_ECHO // echoes back received characters in getc() function
|
||||
|
||||
// allow for unix style (\n only) newline terminator in stored strings
|
||||
// not included into putc_noblock
|
||||
// #define PUTC_CONVERT_LF_TO_CRLF
|
||||
|
||||
// a lot of terminals sends only \r character as a newline terminator, instead
|
||||
// of \r\n or even unix style \n (BTW PuTTY doesn't allow to change this) but in
|
||||
// return requires \r\n terminator to show not broken text
|
||||
// 0 - \r, 1 - \n, 2 - \r\n
|
||||
#define RX_NEWLINE_MODE 1
|
||||
|
||||
// do not use prematures that might break compilers ABI (non-gcc calling
|
||||
// conventions), compilers that are not forcing constant number of call-used
|
||||
// registers might generate even better code
|
||||
// #define USART_NO_ABI_BREAKING_PREMATURES
|
||||
|
||||
// use uppercase letters in uart_puthex() function
|
||||
#define USART_PUTHEX_IN_UPPERCASE
|
||||
|
||||
// extend RX buffer by hardware 2/3 byte FIFO
|
||||
// required for hardware and software RTS
|
||||
// #define USART_EXTEND_RX_BUFFER
|
||||
|
||||
// skip FIFO procedure and write directly
|
||||
// data to the UDR register when possible
|
||||
// probably required for full bus utilization at highest speed (f_cpu/8)
|
||||
// #define USART_PUTC_FAST_INSERTIONS
|
||||
|
||||
// do not allocate temporary buffers on stack for integer/float <-> asci
|
||||
// conversions and use globally visible u_tmp_buff[] instead it have to be
|
||||
// declared in application part and have to be at least of 6-17 bytes wide
|
||||
// (depending on what is being converted)
|
||||
// #define USART_NO_LOCAL_BUFFERS
|
||||
|
||||
// max 19 cycles of interrupt latency
|
||||
// 3+PC bytes on stack
|
||||
// will not interrupt itself
|
||||
// #define USART_UNSAFE_TX_INTERRUPT
|
||||
|
||||
// max 23 cycles of interrupt latency
|
||||
// 4+PC bytes on stack
|
||||
// will not interrupt itself
|
||||
// #define USART_UNSAFE_RX_INTERRUPT
|
||||
|
||||
// remap hardware registers of USART1/2/3 to USART0 if only one interface is
|
||||
// used
|
||||
// #define USART_REMAP_LAST_INTERFACE
|
||||
|
||||
// do not generate code for writing to ubrrh if calculated value is zero
|
||||
// prematures out 2 bytes if ubrr is compile time constant
|
||||
// #define USART_SKIP_UBRRH_IF_ZERO
|
||||
|
||||
// prematures out 4 cycles from every isr run
|
||||
// requires one globally reserved lower register
|
||||
#define USART_USE_GLOBALLY_RESERVED_ISR_SREG_SAVE
|
||||
|
||||
// prematures out 6 cycles from every isr run
|
||||
// requires pair of globally reserved lower registers usage of globally reserved
|
||||
// register for temporary storage in interrupts, should be combined with other
|
||||
// interrupts for best results. special care have to be taken when doing so,
|
||||
// since those registers can still be used by other compilation units (fixable
|
||||
// in gcc by -ffixed-n flag, where n is a suppressed register), precompiled
|
||||
// libraries (vprintf, vscanf, qsort, strtod, strtol, strtoul), or even assembly
|
||||
// hardcoded libraries (fft, aes). registers r2-r7 should be used instead of the
|
||||
// higher ones, since those are never used by gcc for eg. argument passing.
|
||||
#define USART_USE_GLOBALLY_RESERVED_ISR_Z_SAVE
|
||||
|
||||
// have to be redeclared under the same name if the same registers are reused in
|
||||
// other instances (libs)
|
||||
#define USART_SREG_SAVE_REG_NAME G_sreg_save
|
||||
#define USART_SREG_SAVE_REG_NUM "r4"
|
||||
|
||||
// have to be redeclared under the same name if the same registers are reused in
|
||||
// other instances (libs)
|
||||
#define USART_Z_SAVE_REG_NAME G_z_save
|
||||
|
||||
// register pair rn and rn+1 (rn+1:rn gives "invalid register name")
|
||||
#define USART_Z_SAVE_REG_NUM "r2"
|
||||
|
||||
// Size of the ring buffers, must be power of 2
|
||||
// default 32
|
||||
#define RX_BUFFER_SIZE 64
|
||||
|
||||
// Size of the ring buffers, must be power of 2
|
||||
// default 32
|
||||
#define TX_BUFFER_SIZE 128
|
||||
|
||||
/*******************config for multiple USART * mcu's*************************/
|
||||
|
||||
// #define NO_USART0 // disable usage of uart0
|
||||
// #define NO_USART1 // disable usage of uart1
|
||||
// #define NO_USART2 // disable usage of uart2
|
||||
// #define NO_USART3 // disable usage of uart3
|
||||
|
||||
// #define RX0_BUFFER_SIZE 128
|
||||
// #define TX0_BUFFER_SIZE 64
|
||||
|
||||
// #define RX1_BUFFER_SIZE 128
|
||||
// #define TX1_BUFFER_SIZE 64
|
||||
|
||||
// #define RX2_BUFFER_SIZE 128
|
||||
// #define TX2_BUFFER_SIZE 64
|
||||
|
||||
// #define RX3_BUFFER_SIZE 128
|
||||
// #define TX3_BUFFER_SIZE 64
|
||||
|
||||
/****** Disable RX interrupts ******/
|
||||
// removes whole receive code (including ISR) and frees RX0 pin
|
||||
// combining with NO_USART_RX is not necessary
|
||||
// #define NO_RX0_INTERRUPT
|
||||
|
||||
// removes whole receive code (including ISR) and frees RX1 pin
|
||||
// #define NO_RX1_INTERRUPT
|
||||
|
||||
// removes whole receive code (including ISR) and frees RX2 pin
|
||||
// #define NO_RX2_INTERRUPT
|
||||
|
||||
// removes whole receive code (including ISR) and frees RX3 pin
|
||||
// #define NO_RX3_INTERRUPT
|
||||
|
||||
/****** Disable TX interrupts ******/
|
||||
// removes whole transmit code (including ISR) and frees TX0 pin
|
||||
// combining with NO_USART_TX is not necessary
|
||||
// #define NO_TX0_INTERRUPT
|
||||
|
||||
// removes whole transmit code (including ISR) and frees TX1 pin
|
||||
// #define NO_TX1_INTERRUPT
|
||||
|
||||
// removes whole transmit code (including ISR) and frees TX2 pin
|
||||
// #define NO_TX2_INTERRUPT
|
||||
|
||||
// removes whole transmit code (including ISR) and frees TX3 pin
|
||||
// #define NO_TX3_INTERRUPT
|
||||
|
||||
// #define USART0_U2X_SPEED // enables double speed for USART0
|
||||
// #define USART1_U2X_SPEED // enables double speed for USART1
|
||||
// #define USART2_U2X_SPEED // enables double speed for USART2
|
||||
// #define USART3_U2X_SPEED // enables double speed for USART3
|
||||
|
||||
// #define RX0_GETC_ECHO
|
||||
// #define RX1_GETC_ECHO
|
||||
// #define RX2_GETC_ECHO
|
||||
// #define RX3_GETC_ECHO
|
||||
|
||||
// #define PUTC0_CONVERT_LF_TO_CRLF
|
||||
// #define PUTC1_CONVERT_LF_TO_CRLF
|
||||
// #define PUTC2_CONVERT_LF_TO_CRLF
|
||||
// #define PUTC3_CONVERT_LF_TO_CRLF
|
||||
|
||||
// #define USART0_EXTEND_RX_BUFFER
|
||||
// #define USART1_EXTEND_RX_BUFFER
|
||||
// #define USART2_EXTEND_RX_BUFFER
|
||||
// #define USART3_EXTEND_RX_BUFFER
|
||||
|
||||
// #define USART0_PUTC_FAST_INSERTIONS
|
||||
// #define USART1_PUTC_FAST_INSERTIONS
|
||||
// #define USART2_PUTC_FAST_INSERTIONS
|
||||
// #define USART3_PUTC_FAST_INSERTIONS
|
||||
|
||||
// #define USART0_MPCM_MODE
|
||||
// #define USART1_MPCM_MODE
|
||||
// #define USART2_MPCM_MODE
|
||||
// #define USART3_MPCM_MODE
|
||||
|
||||
/***********************soft flow control * config*****************************/
|
||||
// define IO instance to enable software CTS
|
||||
// CTS handlers also have to be placed into INT/PCINT interrupt in the
|
||||
// application code, see example(flow control).c
|
||||
|
||||
// #define CTS0_DDR // DDRB
|
||||
// #define CTS0_PORT // PORTB
|
||||
// #define CTS0_PIN // PINB
|
||||
// #define CTS0_IONUM // 0 // pin number
|
||||
|
||||
// #define CTS1_DDR
|
||||
// #define CTS1_PORT
|
||||
// #define CTS1_PIN
|
||||
// #define CTS1_IONUM
|
||||
|
||||
// #define CTS2_DDR
|
||||
// #define CTS2_PORT
|
||||
// #define CTS2_PIN
|
||||
// #define CTS2_IONUM
|
||||
|
||||
// #define CTS3_DDR
|
||||
// #define CTS3_PORT
|
||||
// #define CTS3_PIN
|
||||
// #define CTS3_IONUM
|
||||
|
||||
// define IO instance to enable software RTS
|
||||
|
||||
// #define RTS0_DDR // DDRB
|
||||
// #define RTS0_PORT // PORTB
|
||||
// #define RTS0_PIN // PINB
|
||||
// #define RTS0_IONUM // 1 // pin number
|
||||
|
||||
// #define RTS1_DDR
|
||||
// #define RTS1_PORT
|
||||
// #define RTS1_PIN
|
||||
// #define RTS1_IONUM
|
||||
|
||||
// #define RTS2_DDR
|
||||
// #define RTS2_PORT
|
||||
// #define RTS2_PIN
|
||||
// #define RTS2_IONUM
|
||||
|
||||
// #define RTS3_DDR
|
||||
// #define RTS3_PORT
|
||||
// #define RTS3_PIN
|
||||
// #define RTS3_IONUM
|
||||
|
||||
/*****************************RS 485 config***********************************/
|
||||
// define IO instance to enable half duplex rs485 operation mode // used pin
|
||||
// should be initially kept in low state before boot
|
||||
|
||||
// #define RS485_CONTROL0_DDR // DDRB
|
||||
// #define RS485_CONTROL0_PORT // PORTB
|
||||
// #define RS485_CONTROL0_PIN // PINB
|
||||
// #define RS485_CONTROL0_IONUM // 2 // pin number
|
||||
|
||||
// #define RS485_CONTROL1_DDR
|
||||
// #define RS485_CONTROL1_PORT
|
||||
// #define RS485_CONTROL1_PIN
|
||||
// #define RS485_CONTROL1_IONUM
|
||||
|
||||
// #define RS485_CONTROL2_DDR
|
||||
// #define RS485_CONTROL2_PORT
|
||||
// #define RS485_CONTROL2_PIN
|
||||
// #define RS485_CONTROL2_IONUM
|
||||
|
||||
// #define RS485_CONTROL3_DDR
|
||||
// #define RS485_CONTROL3_PORT
|
||||
// #define RS485_CONTROL3_PIN
|
||||
// #define RS485_CONTROL3_IONUM
|
||||
|
||||
/*****************************MPCM config***********************************/
|
||||
|
||||
#define MPCM0_ADDRESS 0x01
|
||||
#define MPCM1_ADDRESS 0x02
|
||||
#define MPCM2_ADDRESS 0x03
|
||||
#define MPCM3_ADDRESS 0x04
|
||||
|
||||
// #define MPCM0_GCALL_ADDRESS 0x00
|
||||
// #define MPCM1_GCALL_ADDRESS 0x00
|
||||
// #define MPCM2_GCALL_ADDRESS 0x00
|
||||
// #define MPCM3_GCALL_ADDRESS 0x00
|
||||
|
||||
// #define MPCM0_MASTER_ONLY // do not include slave code into RX ISR
|
||||
// #define MPCM1_MASTER_ONLY // do not include slave code into RX ISR
|
||||
// #define MPCM2_MASTER_ONLY // do not include slave code into RX ISR
|
||||
// #define MPCM3_MASTER_ONLY // do not include slave code into RX ISR
|
||||
|
||||
// Optional macros for placing user-defined code that will be executed inside of
|
||||
// USART interrupt handlers. Only inline asm and its input operand lists are
|
||||
// allowed to be put here. Too large code may generate weird cryptic linker
|
||||
// errors, what is caused by exceeded range of branch instructions.
|
||||
// http://www.nongnu.org/avr-libc/user-manual/inline_asm.html
|
||||
|
||||
// example:
|
||||
// #define STH_EVENT "nop \n\t"\ //
|
||||
// "ldi r31, %M[A_MASK] \n\t"\ //
|
||||
// "out %M[TIMADDR], r31 \n\t"
|
||||
// #define OPERAND_LIST [A_MASK] "M" (0x55),\ //
|
||||
// [TIMADDR] "M" (_SFR_IO_ADDR(TCCR0A)),
|
||||
|
||||
// code executed on every ISR call, before feeding UDR (for this racing
|
||||
// implementation only), can be placed here // r30 and r31 are free to use
|
||||
#define TX0_EVERYCAL_EVENT "\n\t"
|
||||
|
||||
// code executed on every byte transmission, can be placed here // r30 and r31
|
||||
// are free to use // r30 contains currently transmitted data byte
|
||||
#define TX0_TRANSMIT_EVENT "\n\t"
|
||||
|
||||
#define TX0_INPUT_OPERAND_LIST
|
||||
|
||||
#if !defined(USART0_EXTEND_RX_BUFFER) // DO NOT CHANGE
|
||||
// code executed before reading UDR register can be placed here // r25 is free
|
||||
// to use // executed before enabling interrupts in unsafe mode
|
||||
#define RX0_FRAMING_EVENT "\n\t"
|
||||
|
||||
// #define USART0_PUSH_BEFORE_RX // frees r30 an r31 for FRAMING_EVENT
|
||||
#else
|
||||
// code executed before reading UDR register can be placed here // r25 and r31
|
||||
// are free to use
|
||||
#define RX0_FRAMING_EVENT "\n\t"
|
||||
#endif
|
||||
|
||||
// code executed on every ISR call, can be placed here // r30 and r31 are free
|
||||
// to use // r25 contains received data byte if 'extended buffer' mode is not
|
||||
// used, free to use otherwise
|
||||
#define RX0_EVERYCALL_EVENT "\n\t"
|
||||
|
||||
// code executed only when databyte was received, before buffer store, can be
|
||||
// placed here // r31 is free to use // r25 contains received data byte, r30
|
||||
// rxn_last_byte buffer index // MPCM ??
|
||||
#define RX0_EARLY_RECEIVE_EVENT "\n\t"
|
||||
|
||||
// code executed only when databyte was received, can be placed here //
|
||||
// r25,r30,r31 are free to use // r25 contains received data byte
|
||||
#define RX0_LATE_RECEIVE_EVENT "\n\t"
|
||||
|
||||
#define RX0_INPUT_OPERAND_LIST
|
||||
|
||||
//************************************************
|
||||
|
||||
#define TX1_EVERYCAL_EVENT "\n\t"
|
||||
#define TX1_TRANSMIT_EVENT "\n\t"
|
||||
|
||||
#define TX1_INPUT_OPERAND_LIST
|
||||
|
||||
#if !defined(USART1_EXTEND_RX_BUFFER) // DO NOT CHANGE
|
||||
#define RX1_FRAMING_EVENT "\n\t"
|
||||
// #define USART1_PUSH_BEFORE_RX
|
||||
#else
|
||||
#define RX1_FRAMING_EVENT "\n\t"
|
||||
#endif
|
||||
|
||||
#define RX1_EVERYCALL_EVENT "\n\t"
|
||||
#define RX1_EARLY_RECEIVE_EVENT "\n\t"
|
||||
#define RX1_LATE_RECEIVE_EVENT "\n\t"
|
||||
|
||||
#define RX1_INPUT_OPERAND_LIST
|
||||
|
||||
//************************************************
|
||||
|
||||
#define TX2_EVERYCAL_EVENT "\n\t"
|
||||
#define TX2_TRANSMIT_EVENT "\n\t"
|
||||
|
||||
#define TX2_INPUT_OPERAND_LIST
|
||||
|
||||
#if !defined(USART2_EXTEND_RX_BUFFER) // DO NOT CHANGE
|
||||
#define RX2_FRAMING_EVENT "\n\t"
|
||||
// #define USART2_PUSH_BEFORE_RX
|
||||
#else
|
||||
#define RX2_FRAMING_EVENT "\n\t"
|
||||
#endif
|
||||
|
||||
#define RX2_EVERYCALL_EVENT "\n\t"
|
||||
#define RX2_EARLY_RECEIVE_EVENT "\n\t"
|
||||
#define RX2_LATE_RECEIVE_EVENT "\n\t"
|
||||
|
||||
#define RX2_INPUT_OPERAND_LIST
|
||||
|
||||
//************************************************
|
||||
|
||||
#define TX3_EVERYCAL_EVENT "\n\t"
|
||||
#define TX3_TRANSMIT_EVENT "\n\t"
|
||||
|
||||
#define TX3_INPUT_OPERAND_LIST
|
||||
|
||||
#if !defined(USART3_EXTEND_RX_BUFFER) // DO NOT CHANGE
|
||||
#define RX3_FRAMING_EVENT "\n\t"
|
||||
// #define USART3_PUSH_BEFORE_RX
|
||||
#else
|
||||
#define RX3_FRAMING_EVENT "\n\t"
|
||||
#endif
|
||||
|
||||
#define RX3_EVERYCALL_EVENT "\n\t"
|
||||
#define RX3_EARLY_RECEIVE_EVENT "\n\t"
|
||||
#define RX3_LATE_RECEIVE_EVENT "\n\t"
|
||||
|
||||
#define RX3_INPUT_OPERAND_LIST
|
||||
|
||||
// events executed inside transmit complete interrupts (last byte has been
|
||||
// transmitted, UDR buffer is empty) if USATRn_NO_NAKED_TXC_INTERRUPT is not
|
||||
// defined then inline asm is required here // any modified regs have to be
|
||||
// pushed first
|
||||
// inline void TXCn_interrupt_event(void)
|
||||
//{
|
||||
// asm volatile("\n\t"
|
||||
// "nop \n\t"
|
||||
// "lpm \n\t"
|
||||
// "st Z+, r0 \n\t"
|
||||
// :: );
|
||||
//}
|
||||
// mpcm ??
|
||||
|
||||
// #define USATR0_NO_NAKED_TXC_INTERRUPT
|
||||
// #define USART0_USE_TXC_INTERRUPT // if rs485 is not used
|
||||
|
||||
inline void TXC0_interrupt_event(void) __attribute__((always_inline));
|
||||
inline void TXC0_interrupt_event(void) {}
|
||||
|
||||
// #define USATR1_NO_NAKED_TXC_INTERRUPT
|
||||
// #define USART1_USE_TXC_INTERRUPT
|
||||
|
||||
inline void TXC1_interrupt_event(void) __attribute__((always_inline));
|
||||
inline void TXC1_interrupt_event(void) {}
|
||||
|
||||
// #define USATR2_NO_NAKED_TXC_INTERRUPT
|
||||
// #define USART2_USE_TXC_INTERRUPT
|
||||
|
||||
inline void TXC2_interrupt_event(void) __attribute__((always_inline));
|
||||
inline void TXC2_interrupt_event(void) {}
|
||||
|
||||
// #define USATR3_NO_NAKED_TXC_INTERRUPT
|
||||
// #define USART3_USE_TXC_INTERRUPT
|
||||
|
||||
inline void TXC3_interrupt_event(void) __attribute__((always_inline));
|
||||
inline void TXC3_interrupt_event(void) {}
|
||||
|
||||
#endif /* USART_CONFIG_H_ */
|
||||
Reference in New Issue
Block a user