|
-
-
- #include "HardwareSerial.h"
- #include "core_pins.h"
- #include "Arduino.h"
-
-
-
-
-
- #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
- #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
- #define IO_REG_TYPE uint32_t
- #define IO_REG_BASE_ATTR
- #define IO_REG_MASK_ATTR
- #define DIRECT_READ(base, mask) ((*((base)+2) & (mask)) ? 1 : 0)
- #define DIRECT_MODE_INPUT(base, mask) (*((base)+1) &= ~(mask))
- #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+1) |= (mask))
- #define DIRECT_WRITE_LOW(base, mask) (*((base)+34) = (mask))
- #define DIRECT_WRITE_HIGH(base, mask) (*((base)+33) = (mask))
-
- #define UART_CLOCK 24000000
-
- #if defined(__IMXRT1052__)
- SerialEventCheckingFunctionPointer HardwareSerial::serial_event_handler_checks[8] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
- #else
- SerialEventCheckingFunctionPointer HardwareSerial::serial_event_handler_checks[7] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
- #endif
- uint8_t HardwareSerial::serial_event_handlers_active = 0;
-
-
- #define CTRL_ENABLE (LPUART_CTRL_TE | LPUART_CTRL_RE | LPUART_CTRL_RIE | LPUART_CTRL_ILIE)
- #define CTRL_TX_ACTIVE (CTRL_ENABLE | LPUART_CTRL_TIE)
- #define CTRL_TX_COMPLETING (CTRL_ENABLE | LPUART_CTRL_TCIE)
- #define CTRL_TX_INACTIVE CTRL_ENABLE
-
-
- int nvic_execution_priority(void)
- {
- uint32_t priority=256;
- uint32_t primask, faultmask, basepri, ipsr;
-
-
-
- __asm__ volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
- if (faultmask) return -1;
- __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
- if (primask) return 0;
- __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
- if (ipsr) {
- if (ipsr < 16) priority = 0;
- else priority = NVIC_GET_PRIORITY(ipsr - 16);
- }
- __asm__ volatile("mrs %0, basepri\n" : "=r" (basepri)::);
- if (basepri > 0 && basepri < priority) priority = basepri;
- return priority;
- }
-
-
- void HardwareSerial::begin(uint32_t baud, uint16_t format)
- {
-
- float base = (float)UART_CLOCK / (float)baud;
- float besterr = 1e20;
- int bestdiv = 1;
- int bestosr = 4;
- for (int osr=4; osr <= 32; osr++) {
- float div = base / (float)osr;
- int divint = (int)(div + 0.5f);
- if (divint < 1) divint = 1;
- else if (divint > 8191) divint = 8191;
- float err = ((float)divint - div) / div;
- if (err < 0.0f) err = -err;
- if (err <= besterr) {
- besterr = err;
- bestdiv = divint;
- bestosr = osr;
- }
- }
-
- rx_buffer_head_ = 0;
- rx_buffer_tail_ = 0;
- tx_buffer_head_ = 0;
- tx_buffer_tail_ = 0;
- rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
- rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
-
- transmitting_ = 0;
-
- hardware->ccm_register |= hardware->ccm_value;
-
-
-
- *(portControlRegister(hardware->rx_pins[rx_pin_index_].pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;
- *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = hardware->rx_pins[rx_pin_index_].mux_val;
- if (hardware->rx_pins[rx_pin_index_].select_input_register) {
- *(hardware->rx_pins[rx_pin_index_].select_input_register) = hardware->rx_pins[rx_pin_index_].select_val;
- }
-
- *(portControlRegister(hardware->tx_pins[tx_pin_index_].pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
- *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = hardware->tx_pins[tx_pin_index_].mux_val;
-
-
-
-
- port->BAUD = LPUART_BAUD_OSR(bestosr - 1) | LPUART_BAUD_SBR(bestdiv);
- port->PINCFG = 0;
-
-
- attachInterruptVector(hardware->irq, hardware->irq_handler);
- NVIC_SET_PRIORITY(hardware->irq, hardware->irq_priority);
- NVIC_ENABLE_IRQ(hardware->irq);
- uint16_t tx_fifo_size = (((port->FIFO >> 4) & 0x7) << 2);
- uint8_t tx_water = (tx_fifo_size < 16) ? tx_fifo_size >> 1 : 7;
- uint16_t rx_fifo_size = (((port->FIFO >> 0) & 0x7) << 2);
- uint8_t rx_water = (rx_fifo_size < 16) ? rx_fifo_size >> 1 : 7;
-
-
- port->WATER = LPUART_WATER_RXWATER(rx_water) | LPUART_WATER_TXWATER(tx_water);
- port->FIFO |= LPUART_FIFO_TXFE | LPUART_FIFO_RXFE;
-
-
-
- uint32_t ctrl = CTRL_TX_INACTIVE;
-
-
-
- ctrl |= (format & (LPUART_CTRL_PT | LPUART_CTRL_PE) );
- if (format & 0x04) ctrl |= LPUART_CTRL_M;
- if ((format & 0x0F) == 0x04) ctrl |= LPUART_CTRL_R9T8;
-
-
- if (format & 0x20) ctrl |= LPUART_CTRL_TXINV;
-
-
- port->CTRL = ctrl;
-
-
-
- if (format & 0x08) port->BAUD |= LPUART_BAUD_M10;
-
-
- uint32_t c = port->STAT & ~LPUART_STAT_RXINV;
- if (format & 0x10) c |= LPUART_STAT_RXINV;
- port->STAT = c;
-
-
- if ( format & 0x100) port->BAUD |= LPUART_BAUD_SBNS;
-
-
- enableSerialEvents();
- };
-
- inline void HardwareSerial::rts_assert()
- {
- DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
- }
-
- inline void HardwareSerial::rts_deassert()
- {
- DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
- }
-
-
- void HardwareSerial::end(void)
- {
- if (!(hardware->ccm_register & hardware->ccm_value)) return;
- while (transmitting_) yield();
- port->CTRL = 0;
-
-
- *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
- *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
-
-
-
- rx_buffer_head_ = 0;
- rx_buffer_tail_ = 0;
- if (rts_pin_baseReg_) rts_deassert();
-
- disableSerialEvents();
- }
-
- void HardwareSerial::transmitterEnable(uint8_t pin)
- {
- while (transmitting_) ;
- pinMode(pin, OUTPUT);
- transmit_pin_baseReg_ = PIN_TO_BASEREG(pin);
- transmit_pin_bitmask_ = PIN_TO_BITMASK(pin);
- DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
- }
-
- void HardwareSerial::setRX(uint8_t pin)
- {
- if (pin != hardware->rx_pins[rx_pin_index_].pin) {
- for (uint8_t rx_pin_new_index = 0; rx_pin_new_index < cnt_rx_pins; rx_pin_new_index++) {
- if (pin == hardware->rx_pins[rx_pin_index_].pin) {
-
- *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = 5;
-
-
- rx_pin_index_ = rx_pin_new_index;
- *(portControlRegister(hardware->rx_pins[rx_pin_index_].pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(3) | IOMUXC_PAD_HYS;;
- *(portConfigRegister(hardware->rx_pins[rx_pin_index_].pin)) = hardware->rx_pins[rx_pin_index_].mux_val;
- if (hardware->rx_pins[rx_pin_index_].select_input_register) {
- *(hardware->rx_pins[rx_pin_index_].select_input_register) = hardware->rx_pins[rx_pin_index_].select_val;
- }
- break;
- }
- }
- }
- }
-
- void HardwareSerial::setTX(uint8_t pin, bool opendrain)
- {
- uint8_t tx_pin_new_index = tx_pin_index_;
-
- if (pin != hardware->tx_pins[tx_pin_index_].pin) {
- for (tx_pin_new_index = 0; tx_pin_new_index < cnt_tx_pins; tx_pin_new_index++) {
- if (pin == hardware->tx_pins[tx_pin_index_].pin) {
- break;
- }
- }
- if (tx_pin_new_index == cnt_tx_pins) return;
- }
-
-
-
- if (tx_pin_new_index != tx_pin_index_) {
- *(portConfigRegister(hardware->tx_pins[tx_pin_index_].pin)) = 5;
-
- *(portConfigRegister(hardware->tx_pins[tx_pin_new_index].pin)) = hardware->tx_pins[tx_pin_new_index].mux_val;
- }
-
-
- tx_pin_index_ = tx_pin_new_index;
- if (opendrain)
- *(portControlRegister(pin)) = IOMUXC_PAD_ODE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
- else
- *(portControlRegister(pin)) = IOMUXC_PAD_SRE | IOMUXC_PAD_DSE(3) | IOMUXC_PAD_SPEED(3);
- }
-
-
- bool HardwareSerial::attachRts(uint8_t pin)
- {
- if (!(hardware->ccm_register & hardware->ccm_value)) return 0;
- if (pin < CORE_NUM_DIGITAL) {
- rts_pin_baseReg_ = PIN_TO_BASEREG(pin);
- rts_pin_bitmask_ = PIN_TO_BITMASK(pin);
- pinMode(pin, OUTPUT);
- rts_assert();
- } else {
- rts_pin_baseReg_ = NULL;
- return 0;
- }
- return 1;
- }
-
- bool HardwareSerial::attachCts(uint8_t pin)
- {
- if (!(hardware->ccm_register & hardware->ccm_value)) return false;
- if ((pin != 0xff) && (pin == hardware->cts_pin)) {
-
- *(portControlRegister(pin)) = IOMUXC_PAD_DSE(7) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_PUS(0) | IOMUXC_PAD_HYS;
- *(portConfigRegister(hardware->cts_pin)) = hardware->cts_mux_val;
- port->MODIR |= LPUART_MODIR_TXCTSE;
- return true;
- } else {
- port->MODIR &= ~LPUART_MODIR_TXCTSE;
- return false;
- }
- }
-
- void HardwareSerial::clear(void)
- {
-
- rx_buffer_head_ = rx_buffer_tail_;
- if (rts_pin_baseReg_) rts_assert();
- }
-
- int HardwareSerial::availableForWrite(void)
- {
- uint32_t head, tail;
-
- head = tx_buffer_head_;
- tail = tx_buffer_tail_;
- if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
- return tail - head - 1;
- }
-
-
-
-
- int HardwareSerial::available(void)
- {
- uint32_t head, tail;
-
- head = rx_buffer_head_;
- tail = rx_buffer_tail_;
- if (head >= tail) return head - tail;
- return rx_buffer_total_size_ + head - tail;
- }
-
- void HardwareSerial::addStorageForRead(void *buffer, size_t length)
- {
- rx_buffer_storage_ = (BUFTYPE*)buffer;
- if (buffer) {
- rx_buffer_total_size_ = rx_buffer_total_size_ + length;
- } else {
- rx_buffer_total_size_ = rx_buffer_total_size_;
- }
-
- rts_low_watermark_ = rx_buffer_total_size_ - hardware->rts_low_watermark;
- rts_high_watermark_ = rx_buffer_total_size_ - hardware->rts_high_watermark;
- }
-
- void HardwareSerial::addStorageForWrite(void *buffer, size_t length)
- {
- tx_buffer_storage_ = (BUFTYPE*)buffer;
- if (buffer) {
- tx_buffer_total_size_ = tx_buffer_total_size_ + length;
- } else {
- tx_buffer_total_size_ = tx_buffer_total_size_;
- }
- }
-
- int HardwareSerial::peek(void)
- {
- uint32_t head, tail;
-
- head = rx_buffer_head_;
- tail = rx_buffer_tail_;
- if (head == tail) return -1;
- if (++tail >= rx_buffer_total_size_) tail = 0;
- if (tail < rx_buffer_size_) {
- return rx_buffer_[tail];
- } else {
- return rx_buffer_storage_[tail-rx_buffer_size_];
- }
- }
-
- int HardwareSerial::read(void)
- {
- uint32_t head, tail;
- int c;
-
- head = rx_buffer_head_;
- tail = rx_buffer_tail_;
- if (head == tail) return -1;
- if (++tail >= rx_buffer_total_size_) tail = 0;
- if (tail < rx_buffer_size_) {
- c = rx_buffer_[tail];
- } else {
- c = rx_buffer_storage_[tail-rx_buffer_size_];
- }
- rx_buffer_tail_ = tail;
- if (rts_pin_baseReg_) {
- uint32_t avail;
- if (head >= tail) avail = head - tail;
- else avail = rx_buffer_total_size_ + head - tail;
-
- if (avail <= rts_low_watermark_) rts_assert();
- }
- return c;
- }
-
- void HardwareSerial::flush(void)
- {
- while (transmitting_) yield();
- }
-
- size_t HardwareSerial::write(uint8_t c)
- {
-
- return write9bit(c);
- }
-
- size_t HardwareSerial::write9bit(uint32_t c)
- {
- uint32_t head, n;
-
-
- if (transmit_pin_baseReg_) DIRECT_WRITE_HIGH(transmit_pin_baseReg_, transmit_pin_bitmask_);
- head = tx_buffer_head_;
- if (++head >= tx_buffer_total_size_) head = 0;
- while (tx_buffer_tail_ == head) {
- int priority = nvic_execution_priority();
- if (priority <= hardware->irq_priority) {
- if ((port->STAT & LPUART_STAT_TDRE)) {
- uint32_t tail = tx_buffer_tail_;
- if (++tail >= tx_buffer_total_size_) tail = 0;
- if (tail < tx_buffer_size_) {
- n = tx_buffer_[tail];
- } else {
- n = tx_buffer_storage_[tail-tx_buffer_size_];
- }
- port->DATA = n;
- tx_buffer_tail_ = tail;
- }
- } else if (priority >= 256)
- {
- yield();
- }
- }
-
-
- if (head < tx_buffer_size_) {
- tx_buffer_[head] = c;
- } else {
- tx_buffer_storage_[head - tx_buffer_size_] = c;
- }
- __disable_irq();
- transmitting_ = 1;
- tx_buffer_head_ = head;
- port->CTRL |= LPUART_CTRL_TIE;
- __enable_irq();
-
- return 1;
- }
-
- void HardwareSerial::IRQHandler()
- {
-
- uint32_t head, tail, n;
- uint32_t ctrl;
-
-
-
- if (port->STAT & (LPUART_STAT_RDRF | LPUART_STAT_IDLE)) {
-
-
- uint8_t avail = (port->WATER >> 24) & 0x7;
- if (avail) {
- uint32_t newhead;
- head = rx_buffer_head_;
- tail = rx_buffer_tail_;
- do {
- n = port->DATA & 0x3ff;
- newhead = head + 1;
-
- if (newhead >= rx_buffer_total_size_) newhead = 0;
- if (newhead != rx_buffer_tail_) {
- head = newhead;
- if (newhead < rx_buffer_size_) {
- rx_buffer_[head] = n;
- } else {
- rx_buffer_storage_[head-rx_buffer_size_] = n;
- }
- }
- } while (--avail > 0) ;
- rx_buffer_head_ = head;
- if (rts_pin_baseReg_) {
- uint32_t avail;
- if (head >= tail) avail = head - tail;
- else avail = rx_buffer_total_size_ + head - tail;
- if (avail >= rts_high_watermark_) rts_deassert();
- }
- }
-
-
- if (port->STAT & LPUART_STAT_IDLE) {
- port->STAT |= LPUART_STAT_IDLE;
- }
-
-
- }
-
-
- ctrl = port->CTRL;
- if ((ctrl & LPUART_CTRL_TIE) && (port->STAT & LPUART_STAT_TDRE))
- {
-
-
- head = tx_buffer_head_;
- tail = tx_buffer_tail_;
- do {
- if (head == tail) break;
- if (++tail >= tx_buffer_total_size_) tail = 0;
- if (tail < tx_buffer_size_) {
- n = tx_buffer_[tail];
- } else {
- n = tx_buffer_storage_[tail-tx_buffer_size_];
- }
- port->DATA = n;
- } while (((port->WATER >> 8) & 0x7) < 4);
- tx_buffer_tail_ = tail;
- if (head == tail) {
- port->CTRL &= ~LPUART_CTRL_TIE;
- port->CTRL |= LPUART_CTRL_TCIE;
- }
-
- }
-
- if ((ctrl & LPUART_CTRL_TCIE) && (port->STAT & LPUART_STAT_TC))
- {
- transmitting_ = 0;
- if (transmit_pin_baseReg_) DIRECT_WRITE_LOW(transmit_pin_baseReg_, transmit_pin_bitmask_);
-
- port->CTRL &= ~LPUART_CTRL_TCIE;
- }
-
- }
-
-
- void HardwareSerial::processSerialEvents()
- {
- if (!serial_event_handlers_active) return;
- uint8_t handlers_still_to_process = serial_event_handlers_active;
- for (uint8_t i = 0; i < 8; i++) {
- if (serial_event_handler_checks[i]) {
- (*serial_event_handler_checks[i])();
- if (--handlers_still_to_process == 0) return;
- }
- }
- }
-
- void HardwareSerial::enableSerialEvents()
- {
- if (!serial_event_handler_checks[hardware->serial_index]) {
- serial_event_handler_checks[hardware->serial_index] = hardware->serial_event_handler_check;
- serial_event_handlers_active++;
- }
- }
-
- void HardwareSerial::disableSerialEvents()
- {
- if (serial_event_handler_checks[hardware->serial_index]) {
- serial_event_handler_checks[hardware->serial_index] = nullptr;
- serial_event_handlers_active--;
- }
- }
|