SSD1306 OLED display driver 1.7.8 This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays

ssd1306_uart.c
1 /*
2 MIT License
3
4 Copyright (c) 2018, Alexey Dynda
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
28 #include "ssd1306_uart.h"
29
30 #if defined(CONFIG_AVR_UART_AVAILABLE) && defined(CONFIG_AVR_UART_ENABLE)
31
32 volatile uint8_t g_uart_put_ptr = 0;
33 volatile uint8_t g_uart_buf[UART_BUFFER_RX];
34 static uint8_t s_uart_get_ptr = 0;
35 static uint8_t s_uart_interrupt = 0;
36
37 #undef BAUD
38 #define BAUD 115200
39 // We need to define BAUD_TOL too high, because otherwise,
40 // the compiler will give a warning "Baud rate achieved is higher than allowed"
41 // I would recommend not to use 115200 on atmega328p
42 #define BAUD_TOL 4
43 #include <util/setbaud.h>
44 static const uint8_t ubrr0h_115200 = UBRRH_VALUE;
45 static const uint8_t ubrr0l_115200 = UBRRL_VALUE;
46 #ifdef USE_2X
47 static const uint8_t u2x0_115200 = 1;
48 #else
49 static const uint8_t u2x0_115200 = 0;
50 #endif
51
52 #undef BAUD
53 #define BAUD 57600
54 #include <util/setbaud.h>
55 static const uint8_t ubrr0h_57600 = UBRRH_VALUE;
56 static const uint8_t ubrr0l_57600 = UBRRL_VALUE;
57 #ifdef USE_2X
58 static const uint8_t u2x0_57600 = 1;
59 #else
60 static const uint8_t u2x0_57600 = 0;
61 #endif
62
63 #undef BAUD
64 #define BAUD 38400
65 #include <util/setbaud.h>
66 static const uint8_t ubrr0h_38400 = UBRRH_VALUE;
67 static const uint8_t ubrr0l_38400 = UBRRL_VALUE;
68 #ifdef USE_2X
69 static const uint8_t u2x0_38400 = 1;
70 #else
71 static const uint8_t u2x0_38400 = 0;
72 #endif
73
74 #undef BAUD
75 #define BAUD 19200
76 #include <util/setbaud.h>
77 static const uint8_t ubrr0h_19200 = UBRRH_VALUE;
78 static const uint8_t ubrr0l_19200 = UBRRL_VALUE;
79 #ifdef USE_2X
80 static const uint8_t u2x0_19200 = 1;
81 #else
82 static const uint8_t u2x0_19200 = 0;
83 #endif
84
85 void uart_init_internal(uint32_t baud, uint8_t interrupt)
86 {
87 s_uart_interrupt = interrupt;
88 switch (baud)
89 {
90 case 19200:
91 UBRR0H = ubrr0h_19200;
92 UBRR0L = ubrr0l_19200;
93 if (u2x0_19200) UCSR0A |= _BV(U2X0); else UCSR0A &= ~(_BV(U2X0));
94 break;
95 case 38400:
96 UBRR0H = ubrr0h_38400;
97 UBRR0L = ubrr0l_38400;
98 if (u2x0_38400) UCSR0A |= _BV(U2X0); else UCSR0A &= ~(_BV(U2X0));
99 break;
100 case 57600:
101 UBRR0H = ubrr0h_57600;
102 UBRR0L = ubrr0l_57600;
103 if (u2x0_57600) UCSR0A |= _BV(U2X0); else UCSR0A &= ~(_BV(U2X0));
104 break;
105 case 115200:
106 UBRR0H = ubrr0h_115200;
107 UBRR0L = ubrr0l_115200;
108 if (u2x0_115200) UCSR0A |= _BV(U2X0); else UCSR0A &= ~(_BV(U2X0));
109 break;
110 default:
111 break;
112 }
113 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
114 UCSR0B = _BV(RXEN0) | _BV(TXEN0) | (s_uart_interrupt ? _BV(RXCIE0) : 0);
115 }
116
117 void uart_send_byte(uint8_t c)
118 {
119 loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
120 UDR0 = c;
121 }
122
123 static inline uint8_t uart_byte_available_direct(void)
124 {
125 return bit_is_set(UCSR0A, RXC0);
126 }
127
128 uint8_t uart_byte_available(void)
129 {
130 if (!s_uart_interrupt)
131 {
132 while (uart_byte_available_direct()) __uart_read_byte();
133 }
134 return g_uart_put_ptr != s_uart_get_ptr;
135 }
136
137 uint8_t uart_read_byte(void)
138 {
139 uint8_t data = g_uart_buf[s_uart_get_ptr];
140 s_uart_get_ptr = (s_uart_get_ptr + 1) & (UART_BUFFER_RX - 1);
141 return data;
142 }
143
144 #endif
145
UART_BUFFER_RX
#define UART_BUFFER_RX
Definition: ssd1306_uart.h:49
uart_read_byte
uint8_t uart_read_byte(void)
Reads byte from UART RX buffer.
Definition: ssd1306_uart.c:137
uart_byte_available
uint8_t uart_byte_available(void)
Returns non-zero code if there are bytes in RX buffer.
Definition: ssd1306_uart.c:128
ssd1306_uart.h
uart_send_byte
void uart_send_byte(uint8_t c)
Sends single byte over UART.
Definition: ssd1306_uart.c:117