#include "header.h" // MACROS // USART #define USART_BAUDRATE 115200 #define INPUT_SIZE 7 // GLOBAL VARIABLES // // FUNCTIONS // void uart_init() { UBRR0H = (unsigned char) (BAUD_PRESCALER(USART_BAUDRATE) >> 8); // 20.11.5 : UBRRnL and UBRRnH – USART Baud Rate Registers UBRR0L = (unsigned char) BAUD_PRESCALER(USART_BAUDRATE); UCSR0C = DATA_EIGHT_BIT; // 20.11.4 : set Frame Format UCSR0B = TRANSMITTER_ENABLED; // 20.11.3 : enable Receiver, Transmitter, and interrupts } // char uart_rx(void) { // while (!TEST(UCSR0A, RXC0)); // 20.11.2 : do nothing until there are unread data in the receive buffer (UDRn), (RXCn flag in UCSRnA register set to 1 when buffer has data) // return UDR0; // 20.11.1 : get data in buffer, UDRn – USART I/O Data Register (read and write) // } void uart_tx(char c) { while (!TEST(UCSR0A, UDRE0)); // 20.11.2 : do nothing until UDRn buffer is empty, (UDREn flag in UCSRnA register set to 1 when buffer empty) UDR0 = (unsigned char) c; // 20.11.1 : Put data into buffer, UDRn – USART I/O Data Register (read and write) } uint16_t uart_printstr(const char* str) { uint16_t size = 0; while (*str) { uart_tx(*str); str++; size++; } return size; } uint16_t uart_printstr_endl(const char* str) { uint16_t size = 0; size += uart_printstr(str); size += uart_printstr("\r\n"); return size; } uint16_t uart_printstr_itoa_base(uint64_t value, uint8_t base) { char buffer[100] = {0}; uint16_t size = 0; int_to_string_base((uint16_t)value, buffer, base); size += uart_printstr(buffer); return size; } uint16_t uart_printstr_itoa_base_endl(uint64_t value, uint8_t base) { uint16_t size = 0; size += uart_printstr_itoa_base(value, base); size += uart_printstr("\r\n"); return size; } // ISR(USART_RX_vect) { // Table 12-6 : we select the code for USART Receive // char received_char = UDR0; // read received character // if (received_char == '\b' || received_char == 127) { // if backspace is received // if (input_index <= 0) { // return; // } // if (input_index >= INPUT_SIZE) { // return; // } // remove_last_character(); // input_index--; // uart_tx('\b'); // Move cursor back // uart_tx(' '); // Erase the character on screen // uart_tx('\b'); // Move cursor back again // } else if (received_char == '\n' || received_char == '\r') { // if enter is received // if (input_index != INPUT_SIZE) { // return; // } // set_color((char *)color_input); // reset_input(); // } else { // any other character // if (input_index >= INPUT_SIZE) { // return; // } // if (!is_valid_color_input(received_char)) { // return; // } // fill_str(received_char); // uart_tx(received_char); // input_index++; // } // } // ISR(USART_TX_vect) { // Table 12-6 : we select the code for USART Transmit // char received_char = UDR0; // read received character // }