-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdi_uart.c
70 lines (61 loc) · 1.96 KB
/
updi_uart.c
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
// ======================================================================
// Virtual UART to be used in combination with upditerm
//
// Copyright 2024 Dick Streefland
//
// This is free software, licensed under the terms of the GNU General
// Public License as published by the Free Software Foundation.
// ======================================================================
#include <stdio.h>
#include <avr/io.h>
#include "updi_uart.h"
// I/O registers for the virtual UART
#define UART_FLAGS GPIOR2
#define UART_RX GPIOR3
// Bit masks for the flags register
#define FLAG_ENABLE 0x01 // UART is enabled
#define FLAG_RX 0x02 // RX buffer full
// Undocumented registers for OCD messaging, used for transmitting
#define SYSCFG_OCDM _SFR_MEM8(0x0F18)
#define SYSCFG_OCDMS _SFR_MEM8(0x0F19)
// ----------------------------------------------------------------------
// Check whether the UART has been enabled by upditerm
// ----------------------------------------------------------------------
extern _Bool updi_uart_enabled ( void )
{
return (UART_FLAGS & FLAG_ENABLE);
}
// ----------------------------------------------------------------------
// Transmit a byte
// ----------------------------------------------------------------------
extern void updi_uart_tx ( uint8_t byte )
{
while ( UART_FLAGS & FLAG_ENABLE )
{
if ( ! SYSCFG_OCDMS )
{
SYSCFG_OCDM = byte;
break;
}
}
}
// ----------------------------------------------------------------------
// Check if a byte is available in the input buffer
// ----------------------------------------------------------------------
extern _Bool updi_uart_rx_poll ( void )
{
return (UART_FLAGS & FLAG_RX);
}
// ----------------------------------------------------------------------
// Receive a byte
// ----------------------------------------------------------------------
extern uint8_t updi_uart_rx ( void )
{
uint8_t byte;
while ( ! (UART_FLAGS & FLAG_RX) )
{
}
byte = UART_RX;
UART_FLAGS &= ~ FLAG_RX;
return byte;
}