void UART::on_udre_interrupt() { int c = m_obuf->getchar(); if (c != IOStream::EOF) *UDRn() = c; else { *UCSRnB() &= ~_BV(UDRIE0); *UCSRnB() |= _BV(TXCIE0); } }
bool UART::end() { // Disable receiver and transmitter interrupt *UCSRnB() &= ~(_BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0)); return (true); }
int UART::putchar(char c) { // Check if the buffer is full while (m_obuf->putchar(c) == IOStream::EOF) yield(); // Enable the transmitter *UCSRnB() |= _BV(UDRIE0); return (c & 0xff); }
/**************************************************************************//** \brief Sets USART module parameters. \param usartmode - pointer to HAL_UsartDescriptor_t \return none. ******************************************************************************/ void halSetUsartConfig(HAL_UsartDescriptor_t *usartMode) { UCSRnB(usartMode->tty) = 0x00; // disable usart UBRRn(usartMode->tty) = usartMode->baudrate; // usart speed if (USART_MODE_ASYNC == usartMode->mode) { UCSRnA(usartMode->tty) = (uint8_t)USART_DOUBLE_SPEED << U2X0; // Double the USART Transmition Speed UCSRnC(usartMode->tty) = 0x00; } else { UCSRnA(usartMode->tty) = 0; UCSRnC(usartMode->tty) = usartMode->edge; // edge select } UCSRnC(usartMode->tty) |= usartMode->mode; UCSRnC(usartMode->tty) |= usartMode->dataLength; // character size UCSRnC(usartMode->tty) |= usartMode->parity; // parity mode UCSRnC(usartMode->tty) |= usartMode->stopbits; // stop bit select UCSRnA(usartMode->tty) |= (1 << RXC0); // clear receive interrupt UCSRnB(usartMode->tty) |= (1 << RXEN1) | (1 << TXEN1); // usart enable UCSRnB(usartMode->tty) |= (1 << RXCIE0) ; // receive interrupt enable }
bool UART::begin(uint32_t baudrate, uint8_t format) { uint16_t setting = (F_CPU / (baudrate * 8L)) - 1; // Check if double rate is not possible if (setting > 4095) { setting = (F_CPU / (baudrate * 16L)) - 1; } else { *UCSRnA() = _BV(U2X0); } // Set baudrate and format *UBRRn() = setting; *UCSRnC() = format; // Enable receiver and transmitter interrupt *UCSRnB() = (_BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0)); return (true); }
void UART::on_tx_interrupt() { *UCSRnB() &= ~_BV(TXCIE0); on_transmit_completed(); }