81 lines
2.5 KiB
C

/*****************************************************************************
* MIDI Footswitch *
* Copyright (C) 2014-2018 Stefan Kalscheuer *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation version 3. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
*****************************************************************************/
/**
* MIDI Footswitch
*
* @file main.h
* @author Stefan Kalscheuer
* @date 2014-06-17
* @brief USART helper funcitons
*/
/**
* Initialize USART interface with given BAUD rate.
*
* @param baud Baud rate.
* @return void
*/
void USART_Init(uint32_t baud) {
/* Calculate UBRR */
uint16_t ubrr_val = (uint16_t) ((F_CPU / (baud * 16L)) - 1);
/* Set baud rate */
UBRRH = (uint8_t) (ubrr_val >> 8);
UBRRL = (uint8_t) ubrr_val;
/* Enable transmitter */
UCSRB |= (1 << TXEN);
/* Set frame format: 8data, 1stop bit */
UCSRC = (3 << UCSZ0);
}
/**
* Receive USART data from buffer.
*
* @return Data byte from buffer.
*/
unsigned char USART_Receive(void) {
/* Wait for data to be received */
while (!(UCSRA & (1 << RXC)));
/* Get and return received data from buffer */
return UDR;
}
/**
* Transmit USART data byte.
*
* @param data Data byte to transmit.
* @return void
*/
void USART_Transmit(uint8_t data) {
/* Wait for empty transmit buffer */
while (!(UCSRA & (1 << UDRE)));
/* Put data into buffer, sends the data */
UDR = data;
}
/**
* Flush USART buffer.
*
* @return void
*/
void USART_Flush(void) {
uint8_t dummy;
while (UCSRA & (1 << RXC)) {
dummy = UDR;
}
}