1
0
mirror of https://github.com/Optiboot/optiboot.git synced 2025-08-19 09:02:05 +03:00

Attempt RS485 Support (using programmed IO, not the mega0 USART feature.)

This commit is contained in:
WestfW
2021-06-28 00:43:27 -07:00
parent 963abd0282
commit 645a92fa92
3 changed files with 47 additions and 3 deletions

View File

@@ -398,6 +398,10 @@ int main (void) {
MYUART.CTRLA = 0; // Interrupts: all off MYUART.CTRLA = 0; // Interrupts: all off
MYUART.CTRLB = USART_RXEN_bm | USART_TXEN_bm; MYUART.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
#ifdef RS485
RS485_PORT.DIR |= _BV(RS485_BIT);
#endif
// Set up watchdog to trigger after a bit // Set up watchdog to trigger after a bit
// (nominally:, 1s for autoreset, longer for manual) // (nominally:, 1s for autoreset, longer for manual)
watchdogConfig(WDTPERIOD); watchdogConfig(WDTPERIOD);
@@ -542,7 +546,32 @@ int main (void) {
} }
} }
#if RS485
inline void rs485_txon() {
# ifdef RS485_INVERT
RS485_PORT.OUT &= ~_BV(RS485_BIT);
# else
RS485_PORT.OUT |= _BV(RS485_BIT);
# endif
}
inline void rs485_txoff() {
// First, wait for any pending transmits to finish.
MYUART.STATUS = USART_TXCIF_bm; // This clears the TX complete flag
while ((MYUART.STATUS & USART_TXCIF_bm) == 0)
; // spin loop waiting for TX Complete (could be immediately)
# ifdef RS485_INVERT
RS485_PORT.OUT |= _BV(RS485_BIT);
# else
RS485_PORT.OUT &= ~_BV(RS485_BIT);
# endif
}
#else // provide null functions to be optimized away.
inline void rs485_txon() {}
inline void rs485_txoff() {}
#endif
void putch (char ch) { void putch (char ch) {
rs485_txon(); // turn on and leave on till we're done xmitting.
while (0 == (MYUART.STATUS & USART_DREIF_bm)) while (0 == (MYUART.STATUS & USART_DREIF_bm))
; ;
MYUART.TXDATAL = ch; MYUART.TXDATAL = ch;
@@ -550,6 +579,7 @@ void putch (char ch) {
uint8_t getch (void) { uint8_t getch (void) {
uint8_t ch, flags; uint8_t ch, flags;
rs485_txoff(); // To receive, turn off transmitter
while (!(MYUART.STATUS & USART_RXCIF_bm)) while (!(MYUART.STATUS & USART_RXCIF_bm))
; ;
flags = MYUART.RXDATAH; flags = MYUART.RXDATAH;
@@ -657,6 +687,7 @@ static void do_nvmctrl (uint16_t address, uint8_t command, uint8_t data) {
#define OPTFLASHSECT __attribute__((section(".fini8"))) #define OPTFLASHSECT __attribute__((section(".fini8")))
#define OPT2FLASH(o) OPTFLASHSECT const char f##o[] = #o "=" xstr(o) #define OPT2FLASH(o) OPTFLASHSECT const char f##o[] = #o "=" xstr(o)
__attribute__((section(".fini9"))) const char f_delimit = 0xFF;
#ifdef LED_START_FLASHES #ifdef LED_START_FLASHES
OPT2FLASH(LED_START_FLASHES); OPT2FLASH(LED_START_FLASHES);
@@ -675,6 +706,9 @@ OPTFLASHSECT const char f_LED[] = "LED=" LED_NAME;
OPT2FLASH(SUPPORT_EEPROM); OPT2FLASH(SUPPORT_EEPROM);
#endif #endif
#if defined(RS485)
OPTFLASHSECT const char f_rs485[] = "RS485=" RS485_NAME;
#endif
#ifdef BAUD_RATE #ifdef BAUD_RATE
OPT2FLASH(BAUD_RATE); OPT2FLASH(BAUD_RATE);
#endif #endif

View File

@@ -114,6 +114,12 @@ SS_CMD = -DSINGLESPEED=1
endif endif
endif endif
HELPTEXT += "Option RS485=B0 - Pin for optional rs485 tx enable\n"
ifdef RS485
RS485_CMD = -DRS485=$(RS485)
dummy = FORCE
endif
#CPU Options #CPU Options
@@ -152,4 +158,4 @@ ifdef UARTTX
UART_CMD = -DUARTTX=$(UARTTX) UART_CMD = -DUARTTX=$(UARTTX)
endif endif
UART_OPTIONS = $(UART_CMD) $(BAUD_RATE_CMD) $(SOFT_UART_CMD) $(SS_CMD) UART_OPTIONS = $(UART_CMD) $(BAUD_RATE_CMD) $(SOFT_UART_CMD) $(SS_CMD) $(RS485_CMD)

View File

@@ -820,3 +820,7 @@
#ifndef MYUART #ifndef MYUART
# warning No UARTTX pin specified. # warning No UARTTX pin specified.
#endif #endif
#ifdef RS485
#include "pins_rs485.h"
#endif