fremoclock  1_3A01
FREMO Clock - Software for UTG
uart.c
Go to the documentation of this file.
1 
12 #include "sysdef.h" // #define SERIAL_OUT_ON, #define HOST_BAUD_RATE
13 #if defined(SERIAL_OUT_ON) | defined(__DOXYGEN__)
14  #include <avr/io.h> // UCSR0A, UDRE0, U2X0, UCSR0B, TXEN0, UCSR0C,
15  // UCSZ00, UCSZ01, UBRR0H, UBRR0L, UDR0
16  #include <stddef.h> // NULL
17  #include <stdio.h> // struct FILE, stdout,
18  // #define FDEV_SETUP_STREAM(),
19  // #define _FDEV_SETUP_WRITE
20 #endif
21 #include "uart.h"
22 
23 #if defined(SERIAL_OUT_ON) | defined(__DOXYGEN__)
24 
25  // define registers used by MCU used
26  #if defined(__AVR_ATmega48__) | defined(__AVR_ATmega48A__) \
27  | defined(__AVR_ATmega48P__) | defined(__AVR_ATmega48PA__) \
28  | defined(__AVR_ATmega88__) | defined(__AVR_ATmega88A__) \
29  | defined(__AVR_ATmega88P__) | defined(__AVR_ATmega88AP__) \
30  | defined(__AVR_ATmega168__) | defined(__AVR_ATmega168A__) \
31  | defined(__AVR_ATmega168P__) | defined(__AVR_ATmega168PA__) \
32  | defined(__AVR_ATmega328__) | defined(__AVR_ATmega328P__)
33  #define UART_CONTROL_A_REG UCSR0A
34  #define UART_DATA_REG_EMPTY_BIT UDRE0 // bit 5
35  #define UART_DOUBLE_SPEED_BIT U2X0 // bit 1
36  #define UART_CONTROL_B_REG UCSR0B
37  #define UART_TX_ENABLE_BIT TXEN0 // bit 3
38  #define UART_CONTROL_C_REG UCSR0C
39  // 8 bit, no parity, 1 stop bit:
40  #define UART_8N1_BITS ((1<<UCSZ00) | (1<<UCSZ01)) // bits 5..1
41  #define UART_BAUDRATE_HIGH_REG UBRR0H
42  #define UART_BAUDRATE_LOW_REG UBRR0L
43  #define UART_DATA_REG UDR0
44  #elif defined(__AVR_ATmega8__)
45  #define UART_CONTROL_A_REG UCSRA
46  #define UART_DATA_REG_EMPTY_BIT UDRE // bit 5
47  #define UART_DOUBLE_SPEED_BIT U2X // bit 1
48  #define UART_CONTROL_B_REG UCSRB
49  #define UART_TX_ENABLE_BIT TXEN // bit 3
50  #define UART_CONTROL_C_REG UCSRC
51  // 8 bit, no parity, 1 stop bit:
52  #define UART_8N1_BITS ((1<<UCSZ0) | (1<<UCSZ1)) // bits 5..1
53  #define UART_BAUDRATE_HIGH_REG UBRRH
54  #define UART_BAUDRATE_LOW_REG UBRRL
55  #define UART_DATA_REG UDR
56  #else
57  #error "unknown MCU"
58  #endif
59 
64  static int uart_putchar(char c, FILE *stream)
65  {
66  if (c == '\n') uart_putchar('\r', stream);
67 
68  // wait for empty transmit buffer
69  while(!(UART_CONTROL_A_REG & (1<<UART_DATA_REG_EMPTY_BIT)));
70 
71  // put data into buffer, sends the data
72  UART_DATA_REG = c;
73  return 0;
74  }
75 
81  static FILE uart_stdout
82  = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
83 
84 #endif
85 
86 #if defined(SERIAL_OUT_ON)
87 
88  // for comment see uart.h
89  void log_init(void)
90  {
91  // -- init uart
92  #define BAUD HOST_BAUD_RATE // BAUD is input for setbaud.h
93  #include <util/setbaud.h> // #define UBRRH_VALUE, UBRRL_VALUE, USE_2X
94  UART_BAUDRATE_HIGH_REG = UBRRH_VALUE;
95  UART_BAUDRATE_LOW_REG = UBRRL_VALUE;
96  #if USE_2X
97  // double transmission speed
98  UART_CONTROL_A_REG |= (1<<UART_DOUBLE_SPEED_BIT);
99  #else
100  UART_CONTROL_A_REG &= ~(1<<UART_DOUBLE_SPEED_BIT);
101  #endif
102  UART_CONTROL_C_REG = UART_8N1_BITS; // set line mode
103  UART_CONTROL_B_REG = (1<<UART_TX_ENABLE_BIT); // enable transmitter
104 
105  // -- redirect stdout to uart
106  stdout = &uart_stdout;
107  }
108 
109 #endif
Minimal uart library with output only for debugging.
static int uart_putchar(char c, FILE *stream)
Write char to uart.
Definition: uart.c:64
void log_init(void)
Call once in start of main to initialize logging to uart with LOG() and LOG_P() macros.
static FILE uart_stdout
Variable of type file used to redirect stdout to uart_putchr.
Definition: uart.c:82
System wide Definitions for FREMO Clock.