|
| 1 | +/* |
| 2 | +Copyright (c) 2020 SparkFun Electronics |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include "core-extend/HardwareSerial.h" |
| 24 | + |
| 25 | +UART Serial; |
| 26 | + |
| 27 | +// redirect stdout / stdin to Serial |
| 28 | +FileHandle *mbed::mbed_override_console(int) |
| 29 | +{ |
| 30 | + return (FileHandle*)&Serial; |
| 31 | +} |
| 32 | + |
| 33 | +UART::UART(PinName tx, PinName rx, PinName rts, PinName cts) : |
| 34 | + UnbufferedSerial(tx, rx) |
| 35 | +{ |
| 36 | +#ifdef DEVICE_SERIAL_FC |
| 37 | + mbed::SerialBase::Flow control = mbed::SerialBase::Disabled; |
| 38 | + PinName flow1 = rts; |
| 39 | + pinName flow2 = cts; |
| 40 | + bool has_rts = (rts != NC); |
| 41 | + bool has_cts = (cts != NC); |
| 42 | + if(has_rts && has_cts){ |
| 43 | + control = mbed::SerialBase::RTSCTS; |
| 44 | + }else{ |
| 45 | + if(has_rts){ |
| 46 | + control = mbed::SerialBase::RTS; |
| 47 | + flow1 = rts; |
| 48 | + } |
| 49 | + if(has_cts){ |
| 50 | + control = mbed::SerialBase::CTS; |
| 51 | + flow1 = cts; |
| 52 | + } |
| 53 | + } |
| 54 | + BufferedSerial::set_flow_control(control, flow1, flow2); |
| 55 | +#endif // DEVICE_SERIAL_FC |
| 56 | +} |
| 57 | + |
| 58 | +UART::UART(pin_size_t tx, pin_size_t rx, pin_size_t rts, pin_size_t cts) : |
| 59 | + UART(pinNameByNumber(tx), pinNameByNumber(rx), pinNameByNumber(rts), pinNameByNumber(cts)) |
| 60 | +{ |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +UART::UART( void ) : |
| 65 | + UART(STDIO_UART_TX, STDIO_UART_RX) |
| 66 | +{ |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +UART::~UART( void ){ |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +void UART::rxISR( void ){ |
| 75 | + char c; |
| 76 | + while(UnbufferedSerial::readable()) { |
| 77 | + UnbufferedSerial::read(&c, 1); |
| 78 | + _rxbuf.store_char(c); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void UART::begin(unsigned long baudrate, uint16_t config){ |
| 83 | + mbed::SerialBase::Parity parity; |
| 84 | + int stop_bits; |
| 85 | + int bits; |
| 86 | + |
| 87 | + switch(config & SERIAL_PARITY_MASK){ |
| 88 | + case SERIAL_PARITY_EVEN : parity = mbed::SerialBase::Even; break; |
| 89 | + case SERIAL_PARITY_ODD : parity = mbed::SerialBase::Odd; break; |
| 90 | + case SERIAL_PARITY_MARK : parity = mbed::SerialBase::Forced1; break; |
| 91 | + case SERIAL_PARITY_SPACE : parity = mbed::SerialBase::Forced0; break; |
| 92 | + case SERIAL_PARITY_NONE : |
| 93 | + default : |
| 94 | + parity = mbed::SerialBase::None; |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + switch(config & SERIAL_STOP_BIT_MASK){ |
| 99 | + case SERIAL_STOP_BIT_1_5 : |
| 100 | + case SERIAL_STOP_BIT_2 : |
| 101 | + stop_bits = 2; |
| 102 | + break; |
| 103 | + case SERIAL_STOP_BIT_1 : |
| 104 | + default : |
| 105 | + stop_bits = 1; |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + switch(config & SERIAL_DATA_MASK){ |
| 110 | + case SERIAL_DATA_5 : bits = 5; break; |
| 111 | + case SERIAL_DATA_6 : bits = 6; break; |
| 112 | + case SERIAL_DATA_7 : bits = 7; break; |
| 113 | + case SERIAL_DATA_8 : |
| 114 | + default : |
| 115 | + bits = 8; |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + // disable that pesky FIFO |
| 120 | + AM_CRITICAL_BEGIN |
| 121 | + UARTn(0)->LCRH_b.FEN = 0; |
| 122 | + UARTn(1)->LCRH_b.FEN = 0; |
| 123 | + AM_CRITICAL_END |
| 124 | + |
| 125 | + mbed::UnbufferedSerial::set_blocking (false); |
| 126 | + mbed::UnbufferedSerial::baud((int)baudrate); |
| 127 | + mbed::UnbufferedSerial::format(bits, parity, stop_bits); |
| 128 | + mbed::UnbufferedSerial::attach(mbed::callback(this, &UART::rxISR), mbed::UnbufferedSerial::RxIrq); |
| 129 | +} |
| 130 | + |
| 131 | +void UART::begin(unsigned long baudrate){ |
| 132 | + begin(baudrate, SERIAL_8N1); |
| 133 | +} |
| 134 | + |
| 135 | +void UART::end( void ){ |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +int UART::available(void){ |
| 140 | + return _rxbuf.available(); |
| 141 | +} |
| 142 | + |
| 143 | +int UART::peek(void){ |
| 144 | + return _rxbuf.peek(); |
| 145 | +} |
| 146 | + |
| 147 | +int UART::read(void){ |
| 148 | + return _rxbuf.read_char(); |
| 149 | +} |
| 150 | + |
| 151 | +void UART::flush(void){ |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +size_t UART::write(uint8_t c){ |
| 156 | + return write(&c, 1); |
| 157 | +} |
| 158 | + |
| 159 | +size_t UART::write(const uint8_t* buffer, size_t size){ |
| 160 | + while (!UnbufferedSerial::writeable()){}; |
| 161 | + int result = UnbufferedSerial::write((void*)buffer, size); |
| 162 | + return (result < 0) ? 0 : result; |
| 163 | +} |
| 164 | + |
| 165 | +int UART::printf(const char *format, ...){ |
| 166 | + |
| 167 | + va_list args; |
| 168 | + va_start(args, format); |
| 169 | + const int space = vsnprintf(NULL, 0, format, args) + 1; |
| 170 | + char buf[space]; |
| 171 | + memset(buf, 0x00, space); |
| 172 | + vsnprintf(buf, space, format, args); |
| 173 | + va_end(args); |
| 174 | + |
| 175 | + int size = strlen(buf); |
| 176 | + write(buf, size); |
| 177 | + return size; |
| 178 | +} |
0 commit comments