-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
executable file
·141 lines (118 loc) · 4.41 KB
/
serial.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdint.h>
#include "uart_irda_cir.h"
#include "soc_AM335x.h"
#include "interrupt.h"
#include "beaglebone.h"
#include "consoleUtils.h"
#include "hw_types.h"
#include "serial.h"
/******************************************************************************
** INTERNAL MACRO DEFINITIONS
******************************************************************************/
#define BAUD_RATE_115200 (115200)
#define UART_MODULE_INPUT_CLK (48000000)
/******************************************************************************
** INTERNAL FUNCTION PROTOTYPES
******************************************************************************/
static void UARTIsr(void);
static void UartInterruptEnable(void);
static void UartBaudRateSet(void);
/******************************************************************************
** INTERNAL VARIABLES
******************************************************************************/
// Callback function pointer for when we receive a character
static void (*g_rxIsrCallback)(uint8_t) = 0;
/******************************************************************************
** FUNCTION DEFINITIONS
******************************************************************************/
void Serial_init(void)
{
/* Configuring the system clocks for UART0 instance. */
UART0ModuleClkConfig();
/* Performing the Pin Multiplexing for UART0 instance. */
UARTPinMuxSetup(0);
/* Performing a module reset. */
UARTModuleReset(SOC_UART_0_REGS);
/* Performing Baud Rate settings. */
UartBaudRateSet();
/* Switching to Configuration Mode B. */
UARTRegConfigModeEnable(SOC_UART_0_REGS, UART_REG_CONFIG_MODE_B);
/* Programming the Line Characteristics. */
UARTLineCharacConfig(SOC_UART_0_REGS,
(UART_FRAME_WORD_LENGTH_8 | UART_FRAME_NUM_STB_1),
UART_PARITY_NONE);
/* Disabling write access to Divisor Latches. */
UARTDivisorLatchDisable(SOC_UART_0_REGS);
/* Disabling Break Control. */
UARTBreakCtl(SOC_UART_0_REGS, UART_BREAK_COND_DISABLE);
/* Switching to UART16x operating mode. */
UARTOperatingModeSelect(SOC_UART_0_REGS, UART16x_OPER_MODE);
/* Select the console type based on compile time check */
ConsoleUtilsSetType(CONSOLE_UART);
/* Performing Interrupt configurations. */
UartInterruptEnable();
}
void Serial_setRxIsrCallback(void (*rxIsrCallback)(uint8_t))
{
g_rxIsrCallback = rxIsrCallback;
}
/******************************************************************************
** INTERNAL FUNCTIONS
******************************************************************************/
/*
** A wrapper function performing Baud Rate settings.
*/
static void UartBaudRateSet(void)
{
unsigned int divisorValue = 0;
/* Computing the Divisor Value. */
divisorValue = UARTDivisorValCompute(UART_MODULE_INPUT_CLK,
BAUD_RATE_115200,
UART16x_OPER_MODE,
UART_MIR_OVERSAMPLING_RATE_42);
/* Programming the Divisor Latches. */
UARTDivisorLatchWrite(SOC_UART_0_REGS, divisorValue);
}
/*
** A wrapper function performing Interrupt configurations.
*/
static void UartInterruptEnable(void)
{
/* Enabling IRQ in CPSR of ARM processor. */
IntMasterIRQEnable();
/* Configuring AINTC to receive UART0 interrupts. */
/* ..Initializing the ARM Interrupt Controller. */
IntAINTCInit();
/* ..Registering the Interrupt Service Routine(ISR). */
IntRegister(SYS_INT_UART0INT, UARTIsr);
/* ..Setting the priority for the system interrupt in AINTC. */
IntPrioritySet(SYS_INT_UART0INT, 0, AINTC_HOSTINT_ROUTE_IRQ);
/* ..Enabling the system interrupt in AINTC. */
IntSystemEnable(SYS_INT_UART0INT);
/* Enabling the specified UART interrupts. */
UARTIntEnable(SOC_UART_0_REGS, (UART_INT_RHR_CTI));
}
/*
** Interrupt Service Routine for UART.
*/
static void UARTIsr(void)
{
unsigned int intId = 0;
/* Checking the source of UART interrupt. */
intId = UARTIntIdentityGet(SOC_UART_0_REGS);
switch (intId) {
case UART_INTID_RX_THRES_REACH:
// Call the user's RX function (if any)
if (g_rxIsrCallback != 0) {
uint8_t rxByte = UARTCharGetNonBlocking(SOC_UART_0_REGS);
UARTCharPutNonBlocking(SOC_UART_0_REGS, rxByte);
g_rxIsrCallback(rxByte);
} else {
// Indicate that there's no callback function registered.
UARTCharPutNonBlocking(SOC_UART_0_REGS, '!');
}
break;
default:
break;
}
}