Project

General

Profile

RE: StarterWare question ... » uartEcho.c

Stéphane Peter, 03/20/2012 07:34 AM

 
1
/**
2
 * \file  uartEcho.c
3
 *
4
 * \brief This is a sample application file which invokes some APIs
5
 *        from the UART device abstraction library to perform configuration,
6
 *        transmission and reception operations.
7
 */
8

    
9
/* Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
10
 * ALL RIGHTS RESERVED */
11

    
12
#include "hw_psc_OMAPL138.h"
13
#include "soc_OMAPL138.h"
14
#include "interrupt.h"
15
#include "evmOMAPL138.h"
16
#include "hw_types.h"
17
#include "uart.h"
18
#include "psc.h"
19

    
20
/****************************************************************************/
21
/*                      LOCAL FUNCTION PROTOTYPES                           */
22
/****************************************************************************/
23
static void ConfigureIntUART(void);
24
static void SetupInt(void);
25
static void UARTIsr(void);
26

    
27
/****************************************************************************/
28
/*                      GLOBAL VARIABLES                                    */
29
/****************************************************************************/
30
char txArray[] = "StarterWare UART echo application\n\r";
31

    
32
/****************************************************************************/
33
/*                      LOCAL FUNCTION DEFINITIONS                          */
34
/****************************************************************************/
35

    
36
int main(void)
37
{
38
    unsigned int intFlags = 0;
39
    unsigned int config = 0;
40

    
41
    /* Enabling the PSC for UART2.*/
42
    PSCModuleControl(SOC_PSC_1_REGS, HW_PSC_UART1, PSC_POWERDOMAIN_ALWAYS_ON,
43
		     PSC_MDCTL_NEXT_ENABLE);
44

    
45
    /* Setup PINMUX */
46
    UARTPinMuxSetup(1, FALSE);
47
    
48
    /* Enabling the transmitter and receiver*/
49
    UARTEnable(SOC_UART_1_REGS);
50

    
51
    /* 1 stopbit, 8-bit character, no parity */
52
    config = UART_WORDL_8BITS;
53

    
54
    /* Configuring the UART parameters*/
55
    UARTConfigSetExpClk(SOC_UART_1_REGS, SOC_UART_1_MODULE_FREQ,
56
                        BAUD_115200, config,
57
                        UART_OVER_SAMP_RATE_16);
58

    
59
    /* Enabling the FIFO and flushing the Tx and Rx FIFOs.*/
60
    UARTFIFOEnable(SOC_UART_1_REGS);
61

    
62
    /* Setting the UART Receiver Trigger Level*/
63
    UARTFIFOLevelSet(SOC_UART_1_REGS, UART_RX_TRIG_LEVEL_1);
64
    
65
    /*
66
    ** Enable AINTC to handle interrupts. Also enable IRQ interrupt in ARM 
67
    ** processor.
68
    */
69
    SetupInt();
70

    
71
    /* Configure AINTC to receive and handle UART interrupts. */
72
    ConfigureIntUART();
73

    
74
    /* Preparing the 'intFlags' variable to be passed as an argument.*/
75
    intFlags |= (UART_INT_LINE_STAT  |  \
76
                 UART_INT_TX_EMPTY |    \
77
                 UART_INT_RXDATA_CTI);
78

    
79
    /* Enable the Interrupts in UART.*/
80
    UARTIntEnable(SOC_UART_1_REGS, intFlags);
81

    
82
    while(1);
83
}
84

    
85
/*
86
** \brief   Interrupt Service Routine(ISR) to be executed on UART interrupts.
87
**          Depending on the source of interrupt, this 
88
**          1> writes to the serial communication console, or 
89
**          2> reads from the serial communication console, or 
90
**          3> reads the byte in RBR if receiver line error has occured.
91
*/
92

    
93
static void UARTIsr()
94
{
95
    static unsigned int length = sizeof(txArray);
96
    static unsigned int count = 0;
97
    unsigned char rxData = 0;
98
    unsigned int int_id = 0;
99

    
100
    /* This determines the cause of UART1 interrupt.*/
101
    int_id = UARTIntStatus(SOC_UART_1_REGS);
102

    
103
#ifdef _TMS320C6X
104
    // Clear UART1 system interrupt in DSPINTC
105
    IntEventClear(SYS_INT_UART1_INT);
106
#else
107
    /* Clears the system interrupt status of UART1 in AINTC. */
108
    IntSystemStatusClear(SYS_INT_UARTINT1);
109
#endif
110
  
111
    /* Checked if the cause is transmitter empty condition.*/
112
    if(UART_INTID_TX_EMPTY == int_id)
113
    {
114
        if(0 < length)
115
        {
116
            /* Write a byte into the THR if THR is free. */
117
            UARTCharPutNonBlocking(SOC_UART_1_REGS, txArray[count]);
118
            length--;
119
            count++;
120
        }
121
        if(0 == length)
122
        {
123
            /* Disable the Transmitter interrupt in UART.*/
124
            UARTIntDisable(SOC_UART_1_REGS, UART_INT_TX_EMPTY);
125
        }
126
     }
127

    
128
    /* Check if the cause is receiver data condition.*/
129
    if(UART_INTID_RX_DATA == int_id)
130
    {
131
        rxData = UARTCharGetNonBlocking(SOC_UART_1_REGS);
132
        UARTCharPutNonBlocking(SOC_UART_1_REGS, rxData);
133
    }
134

    
135

    
136
    /* Check if the cause is receiver line error condition.*/
137
    if(UART_INTID_RX_LINE_STAT == int_id)
138
    {
139
        while(UARTRxErrorGet(SOC_UART_1_REGS))
140
        {
141
            /* Read a byte from the RBR if RBR has data.*/
142
            UARTCharGetNonBlocking(SOC_UART_1_REGS);
143
        }
144
    }
145

    
146
    return;
147
}
148

    
149
/*
150
** \brief   This function invokes necessary functions to configure the ARM
151
**          processor and ARM Interrupt Controller(AINTC) to receive and
152
**          handle interrupts.
153
*/
154

    
155

    
156
static void SetupInt(void)
157
{
158
#ifdef _TMS320C6X
159
	// Initialize the DSP INTC
160
	IntDSPINTCInit();
161

    
162
	// Enable DSP interrupts globally
163
	IntGlobalEnable();
164
#else
165
    /* Initialize the ARM Interrupt Controller(AINTC). */
166
    IntAINTCInit();
167

    
168
    /* Enable IRQ in CPSR.*/     
169
    IntMasterIRQEnable();
170

    
171
    /* Enable the interrupts in GER of AINTC.*/
172
    IntGlobalEnable();
173

    
174
    /* Enable the interrupts in HIER of AINTC.*/
175
    IntIRQEnable();
176
#endif
177
}
178

    
179
/*
180
** \brief  This function configures the AINTC to receive UART interrupts.
181
*/ 
182
static void ConfigureIntUART(void)
183
{
184
#ifdef _TMS320C6X
185
	IntRegister(C674X_MASK_INT4, UARTIsr);
186
	IntEventMap(C674X_MASK_INT4, SYS_INT_UART2_INT);
187
	IntEnable(C674X_MASK_INT4);
188
#else
189
    /* Registers the UARTIsr in the Interrupt Vector Table of AINTC. */
190
    IntRegister(SYS_INT_UARTINT1, UARTIsr);
191

    
192
    /* Map the channel number 2 of AINTC to UART1 system interrupt. */
193
    IntChannelSet(SYS_INT_UARTINT1, 1);
194

    
195
    IntSystemEnable(SYS_INT_UARTINT1);
196
#endif
197
}
198

    
199
/****************************END OF FILE*************************************/
    (1-1/1)
    Go to top
    Add picture from clipboard (Maximum size: 1 GB)