-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTRX_uart.h
87 lines (64 loc) · 2.6 KB
/
TRX_uart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*|==========================================================================|*/
/*| ____ _ _ _ _ |*/
/*| | \ _ _ | |_ |_|| |_ ___ ___ | |_ |*/
/*| | | || | || . || || _|| -_|| _|| | |*/
/*| |____/ |___||___||_||_| |___||___||_|_| |*/
/*| |*/
/*| [email protected] |*/
/*|==========================================================================|*/
/*| File name: TRX_uart.h |*/
/*| |*/
/*| Description: UART driver for an AtMega328. |*/
/*| Can implement RX interrupt. If using it, make sure to globally enable |*/
/*| interrupts, using sei() function. |*/
/*| |*/
/*|==========================================================================|*/
/*| 24/04/2022 | Creation |*/
/*| | |*/
/*|==========================================================================|*/
#ifndef SERIAL_H
#define SERIAL_H
#include <stdint.h>
#include <util/delay.h>
#include <avr/io.h>
#ifndef UBRR0
#define STX_BAUDRATE 9600
#define STX_US_PER_BIT (1000000/STX_BAUDRATE)
#define STX_PORT PORTB
#define STX_DDR DDRB
#define STX_MASK 0x02
#define SRX_BAUDRATE 9600
#define SRX_US_PER_BIT (1000000/STX_BAUDRATE)
#define SRX_PORT PORTB
#define SRX_DDR DDRB
#define SRX_PIN PINB
#define SRX_MASK 0x01
#define TIMER_FREQ (F_CPU/64)
// Prescaler at 64 (16MHz clock, 9600 baud)
#define TCCR1_CFG 0x07
#define BAUDRATE 9600
#define CLK_PER_BIT (TIMER_FREQ/BAUDRATE)
#endif
#define TRX_NEWLINE {TRX_putc(0x0d); TRX_putc(0x0a);}
// Simple speed
#define TRX_SSPEED 0
// Double speed
#define TRX_DSPEED 1
#ifdef UBRR0
void TRX_init(uint32_t baudrate, uint8_t doubleSpeed);
#endif
#ifndef UBRR0
void STX_init();
void SRX_init();
#endif
void TRX_putc(const char);
void TRX_print(char*);
void TRX_printHex8(const uint8_t);
void TRX_printHex16(const uint16_t);
void TRX_printHex32(const uint32_t v);
void TRX_printDec(const uint32_t v);
unsigned char TRX_rxAvailable();
char TRX_getChar();
unsigned char TRX_getHex8();
void TRX_enableRXInterrupt(void(*rxHandler)(void));
#endif