LevelMeter-Display  2.0
An ultrasonic Level Meter for Stormwater Cisterns
uart.h
Go to the documentation of this file.
1 // uart.h
2 
3 #include <stdio.h>
4 #include "Config.h"
5 
6 #define USE_RTS // comment this out if your circuit does not have RTS/CTS-lines
7 #define UART0_DE_MASK 0x04
8 #define UART0_PORT PORTE
9 #define UART1_DE_MASK 0x02
10 #define UART1_RTS_MASK 0x10
11 #define UART1_CTS_MASK 0x20
12 #define UART1_PORT PORTD
13 #define UART1_PIN PIND
14 
15 #define SENSOR_BAUD 9600
16 #define HOST_BAUD 9600
17 #define SENSOR_UBRR (F_CPU/16/SENSOR_BAUD-1)
18 #define HOST_UBRR (F_CPU/16/HOST_BAUD-1)
19 
20 // Buffer Sizes. Must be <255 since the Pointers are only uint8_t
21 #define UART0_BUFSIZE 40
22 #define UART1_BUFSIZE 200
23 
24 extern uint8_t charsInRbuf0;
25 extern uint8_t computedChecksum;
26 extern volatile char cksum0;
27 extern volatile char rbuf0[];
28 extern volatile uint8_t tbuf1_tail,tbuf1_head;
29 extern volatile uint8_t byteFromHost;
30 extern volatile uint8_t hostCommandsLost;
31 
32 extern FILE uart0,uart1;
33 extern void InitUart(void);
34 extern void putc0(char);
35 extern void putd0(char);
36 extern int putchar_uart1(char,FILE*);
37 extern int hprintf_P(const char *fmt, ... );
38 extern void usart1FlushRx(void);
39 extern bool Tbub1IsEmpty(void);
40 
41 
44 static inline void Uart0EnableLineDriver(void)
45 {
47 }
48 
51 static inline void Uart0DisableLineDriver(void)
52 {
54 }
55 
58 static inline void Uart1EnableLineDriver(void)
59 {
61 }
62 
65 static inline void Uart1DisableLineDriver(void)
66 {
68 }
69 
75 static inline void UsartTimerCallback(void)
76 {
77 #ifdef USE_RTS
78  if (config.useRts)
79  {
81  {
82  UCSR1B|=0b01100000; // enable UDRE and TX Complete interrupt
83  }
84  }
85 #endif
86 }
87 
90 static inline void AssertRTS(void)
91 {
92 #ifdef USE_RTS // Defined out...
93  if (config.useRts) // Config'd out...
95 #endif
96 }
97 
100 static inline void ReleaseRTS(void)
101 {
102 #ifdef USE_RTS
103  if (config.useRts)
105 #endif
106 }
bool useRts
Shall the serial communication use RTS/CTS-handshake?
Definition: config.h:72
static void Uart0DisableLineDriver(void)
Disables the RS485 Line Driver on Uart0.
Definition: uart.h:51
static void ReleaseRTS(void)
Releases RTS to the Host Line, i.e. stops the Host from sending Data.
Definition: uart.h:100
#define UART1_PORT
Definition: uart.h:12
Structures and Constants for Configuration Handling.
uint8_t computedChecksum
Definition: uart.c:42
uint8_t charsInRbuf0
Definition: uart.c:41
#define UART1_PIN
Definition: uart.h:13
int putchar_uart1(char, FILE *)
The Putchar-Routine for USART1 (Host-Interface)
Definition: uart.c:160
static void Uart1DisableLineDriver(void)
Disables the RS485 Line Driver on Uart1.
Definition: uart.h:65
static void Uart0EnableLineDriver(void)
Enables the RS485 Line Driver on Uart0.
Definition: uart.h:44
#define UART1_DE_MASK
Definition: uart.h:9
static void UsartTimerCallback(void)
Timer Callback. Checks for CTS from the Host to resume Communication.
Definition: uart.h:75
volatile uint8_t tbuf1_tail
Definition: uart.c:36
#define UART0_DE_MASK
Definition: uart.h:7
static void AssertRTS(void)
Asserts RTS to the Host Line, i.e. allows the Host to send Data.
Definition: uart.h:90
static void Uart1EnableLineDriver(void)
Enables the RS485 Line Driver on Uart1.
Definition: uart.h:58
void usart1FlushRx(void)
Definition: uart.c:364
#define UART1_RTS_MASK
Definition: uart.h:10
volatile uint8_t tbuf1_head
Definition: uart.c:36
FILE uart0
volatile uint8_t byteFromHost
Contains the last byte received from the host.
Definition: uart.c:43
void putd0(char)
Sends a Data Char (bit9=0) over USART0.
Definition: uart.c:109
int hprintf_P(const char *fmt,...)
printf_P for Communication with the Host
Definition: uart.c:375
bool Tbub1IsEmpty(void)
Tell if Transmit Buffer 1 is empty.
Definition: uart.c:191
#define UART0_PORT
Definition: uart.h:8
void putc0(char)
Sends a Command Char (bit9=1) over USART0.
Definition: uart.c:98
volatile char cksum0
Keeps track of the Checksum for USART0. It is automatically reset by command bytes.
Definition: uart.c:40
confStruct_t config
The Configuration. It is read on startup from the EEPROM.
Definition: config.c:32
volatile char rbuf0[]
Receive Buffer for UART0.
Definition: uart.c:33
volatile uint8_t hostCommandsLost
Counts the number of commands lost due to buffer overrun.
Definition: uart.c:44
FILE uart1
Definition: uart.c:47
void InitUart(void)
Initialize USART Hardware and Pointers.
Definition: uart.c:57
#define UART1_CTS_MASK
Definition: uart.h:11