1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Enable Serial RX/TX Only mode (needed by I2S)

This commit is contained in:
John Doe 2015-06-20 23:26:26 +03:00
parent 80d5508b73
commit ca8ebdbb97
2 changed files with 51 additions and 32 deletions

View File

@ -42,12 +42,12 @@ extern "C" {
#define UART_TX_FIFO_SIZE 0x80 #define UART_TX_FIFO_SIZE 0x80
struct uart_ { struct uart_ {
int uart_nr; int uart_nr;
int baud_rate; int baud_rate;
bool rxEnabled; bool rxEnabled;
bool txEnabled; bool txEnabled;
uint8_t rxPin; uint8_t rxPin;
uint8_t txPin; uint8_t txPin;
}; };
static const int UART0 = 0; static const int UART0 = 0;
@ -271,7 +271,7 @@ int uart_get_baudrate(uart_t* uart) {
return uart->baud_rate; return uart->baud_rate;
} }
uart_t* uart_init(int uart_nr, int baudrate, byte config) { uart_t* uart_init(int uart_nr, int baudrate, byte config, byte mode) {
uint32_t conf1 = 0x00000000; uint32_t conf1 = 0x00000000;
uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t)); uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t));
@ -284,24 +284,25 @@ uart_t* uart_init(int uart_nr, int baudrate, byte config) {
switch(uart->uart_nr) { switch(uart->uart_nr) {
case UART0: case UART0:
pinMode(1, SPECIAL); uart->rxEnabled = (mode != SERIAL_TX_ONLY);
pinMode(3, SPECIAL); uart->txEnabled = (mode != SERIAL_RX_ONLY);
uart->rxEnabled = true; uart->rxPin = (uart->rxEnabled)?3:255;
uart->txEnabled = true; uart->txPin = (uart->txEnabled)?1:255;
uart->rxPin = 3; if(uart->rxEnabled) pinMode(uart->rxPin, SPECIAL);
uart->txPin = 1; if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
break; break;
case UART1: case UART1:
pinMode(2, SPECIAL);
uart->rxEnabled = false; uart->rxEnabled = false;
uart->txEnabled = true; uart->txEnabled = (mode != SERIAL_RX_ONLY);
uart->rxPin = 255; uart->rxPin = 255;
uart->txPin = 2; uart->txPin = (uart->txEnabled)?2:255;
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
break; break;
case UART_NO: case UART_NO:
default: default:
// big fail! // big fail!
break; os_free(uart);
return 0;
} }
uart_set_baudrate(uart, baudrate); uart_set_baudrate(uart, baudrate);
USC0(uart->uart_nr) = config; USC0(uart->uart_nr) = config;
@ -356,22 +357,30 @@ void uart_swap(uart_t* uart) {
return; return;
switch(uart->uart_nr) { switch(uart->uart_nr) {
case UART0: case UART0:
if(uart->txPin == 1 && uart->rxPin == 3) { if((uart->txPin == 1 && uart->txEnabled) || (uart->rxPin == 3 && uart->rxEnabled)) {
pinMode(15, FUNCTION_4); //TX if(uart->txEnabled) pinMode(15, FUNCTION_4); //TX
pinMode(13, FUNCTION_4); //RX if(uart->rxEnabled) pinMode(13, FUNCTION_4); //RX
IOSWAP |= (1 << IOSWAPU0); IOSWAP |= (1 << IOSWAPU0);
pinMode(1, INPUT); //TX if(uart->txEnabled){ //TX
pinMode(3, INPUT); //RX pinMode(1, INPUT);
uart->rxPin = 13; uart->txPin = 15;
uart->txPin = 15; }
if(uart->rxEnabled){ //RX
pinMode(3, INPUT);
uart->rxPin = 13;
}
} else { } else {
pinMode(1, SPECIAL); //TX if(uart->txEnabled) pinMode(1, SPECIAL); //TX
pinMode(3, SPECIAL); //RX if(uart->rxEnabled) pinMode(3, SPECIAL); //RX
IOSWAP &= ~(1 << IOSWAPU0); IOSWAP &= ~(1 << IOSWAPU0);
pinMode(15, INPUT); //TX if(uart->txEnabled){ //TX
pinMode(13, INPUT); //RX pinMode(15, INPUT);
uart->rxPin = 3; uart->txPin = 1;
uart->txPin = 1; }
if(uart->rxEnabled){ //RX
pinMode(13, INPUT); //RX
uart->rxPin = 3;
}
} }
break; break;
@ -471,14 +480,14 @@ HardwareSerial::HardwareSerial(int uart_nr) :
_uart_nr(uart_nr), _uart(0), _tx_buffer(0), _rx_buffer(0), _written(false) { _uart_nr(uart_nr), _uart(0), _tx_buffer(0), _rx_buffer(0), _written(false) {
} }
void HardwareSerial::begin(unsigned long baud, byte config) { void HardwareSerial::begin(unsigned long baud, byte config, byte mode) {
// disable debug for this interface // disable debug for this interface
if(uart_get_debug() == _uart_nr) { if(uart_get_debug() == _uart_nr) {
uart_set_debug(UART_NO); uart_set_debug(UART_NO);
} }
_uart = uart_init(_uart_nr, baud, config); _uart = uart_init(_uart_nr, baud, config, mode);
if(_uart == 0) { if(_uart == 0) {
return; return;
@ -518,7 +527,10 @@ void HardwareSerial::setDebugOutput(bool en) {
if(_uart == 0) if(_uart == 0)
return; return;
if(en) { if(en) {
uart_set_debug(_uart->uart_nr); if(_uart->txEnabled)
uart_set_debug(_uart->uart_nr);
else
uart_set_debug(UART_NO);
} else { } else {
// disable debug for this interface // disable debug for this interface
if(uart_get_debug() == _uart_nr) { if(uart_get_debug() == _uart_nr) {

View File

@ -60,6 +60,10 @@
#define SERIAL_7O2 0x3b #define SERIAL_7O2 0x3b
#define SERIAL_8O2 0x3f #define SERIAL_8O2 0x3f
#define SERIAL_FULL 0
#define SERIAL_RX_ONLY 1
#define SERIAL_TX_ONLY 2
class cbuf; class cbuf;
struct uart_; struct uart_;
@ -70,9 +74,12 @@ class HardwareSerial: public Stream {
HardwareSerial(int uart_nr); HardwareSerial(int uart_nr);
void begin(unsigned long baud) { void begin(unsigned long baud) {
begin(baud, SERIAL_8N1); begin(baud, SERIAL_8N1, SERIAL_FULL);
} }
void begin(unsigned long, uint8_t); void begin(unsigned long baud, uint8_t config) {
begin(baud, config, SERIAL_FULL);
}
void begin(unsigned long, uint8_t, uint8_t);
void end(); void end();
void swap(); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO1 as RX and TX void swap(); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO1 as RX and TX
int available(void) override; int available(void) override;