Project

General

Profile

StarterWare » uartEcho.c

Modified uartEcho Example (change to UART1) - Michael Williamson, 04/04/2012 02:30 PM

 
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
/*
10
* Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ 
11
*
12
*  Redistribution and use in source and binary forms, with or without 
13
*  modification, are permitted provided that the following conditions 
14
*  are met:
15
*
16
*    Redistributions of source code must retain the above copyright 
17
*    notice, this list of conditions and the following disclaimer.
18
*
19
*    Redistributions in binary form must reproduce the above copyright
20
*    notice, this list of conditions and the following disclaimer in the 
21
*    documentation and/or other materials provided with the   
22
*    distribution.
23
*
24
*    Neither the name of Texas Instruments Incorporated nor the names of
25
*    its contributors may be used to endorse or promote products derived
26
*    from this software without specific prior written permission.
27
*
28
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
29
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
30
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31
*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
32
*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
33
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
34
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
37
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
38
*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
*/
40

    
41
#include "hw_psc_OMAPL138.h"
42
#include "soc_OMAPL138.h"
43
#include "interrupt.h"
44
#include "evmOMAPL138.h"
45
#include "hw_types.h"
46
#include "uart.h"
47
#include "psc.h"
48

    
49
/****************************************************************************/
50
/*                      LOCAL FUNCTION PROTOTYPES                           */
51
/****************************************************************************/
52
static void ConfigureIntUART(void);
53
static void SetupInt(void);
54
static void UARTIsr(void);
55

    
56
/****************************************************************************/
57
/*                      GLOBAL VARIABLES                                    */
58
/****************************************************************************/
59
char txArray[] = "StarterWare UART echo application\n\r";
60

    
61
/****************************************************************************/
62
/*                      LOCAL FUNCTION DEFINITIONS                          */
63
/****************************************************************************/
64

    
65
#define REGS_UART SOC_UART_1_REGS
66
#define UART_INT  SYS_INT_UARTINT1
67

    
68
int main(void)
69
{
70
    unsigned int intFlags = 0;
71
    unsigned int config = 0;
72

    
73
    /* Enabling the PSC for UART2.*/
74
    PSCModuleControl(SOC_PSC_1_REGS, HW_PSC_UART1, PSC_POWERDOMAIN_ALWAYS_ON,
75
		     PSC_MDCTL_NEXT_ENABLE);
76

    
77
    PSCModuleControl(SOC_PSC_1_REGS, HW_PSC_UART2, PSC_POWERDOMAIN_ALWAYS_ON,
78
		     PSC_MDCTL_NEXT_ENABLE);
79

    
80
    /* Setup PINMUX */
81
    UARTPinMuxSetup(2, FALSE);
82
    UARTPinMuxSetup(1, FALSE);
83
    
84
    /* Enabling the transmitter and receiver*/
85
    UARTEnable(REGS_UART);
86

    
87
    /* 1 stopbit, 8-bit character, no parity */
88
    config = UART_WORDL_8BITS;
89

    
90
    /* Configuring the UART parameters*/
91
    UARTConfigSetExpClk(REGS_UART, SOC_UART_2_MODULE_FREQ,
92
                        BAUD_115200, config,
93
                        UART_OVER_SAMP_RATE_16);
94

    
95
    /* Enabling the FIFO and flushing the Tx and Rx FIFOs.*/
96
    UARTFIFOEnable(REGS_UART);
97

    
98
    /* Setting the UART Receiver Trigger Level*/
99
    UARTFIFOLevelSet(REGS_UART, UART_RX_TRIG_LEVEL_1);
100
    
101
    /*
102
    ** Enable AINTC to handle interrupts. Also enable IRQ interrupt in ARM 
103
    ** processor.
104
    */
105
    SetupInt();
106

    
107
    /* Configure AINTC to receive and handle UART interrupts. */
108
    ConfigureIntUART();
109

    
110
    /* Preparing the 'intFlags' variable to be passed as an argument.*/
111
    intFlags |= (UART_INT_LINE_STAT  |  \
112
                 UART_INT_TX_EMPTY |    \
113
                 UART_INT_RXDATA_CTI);
114

    
115
    /* Enable the Interrupts in UART.*/
116
    UARTIntEnable(REGS_UART, intFlags);
117

    
118
    UARTCharPutNonBlocking(REGS_UART, '1');
119

    
120
    while(1);
121
}
122

    
123
/*
124
** \brief   Interrupt Service Routine(ISR) to be executed on UART interrupts.
125
**          Depending on the source of interrupt, this 
126
**          1> writes to the serial communication console, or 
127
**          2> reads from the serial communication console, or 
128
**          3> reads the byte in RBR if receiver line error has occured.
129
*/
130

    
131
static void UARTIsr()
132
{
133
    static unsigned int length = sizeof(txArray);
134
    static unsigned int count = 0;
135
    unsigned char rxData = 0;
136
    unsigned int int_id = 0;
137

    
138
    /* This determines the cause of UART2 interrupt.*/
139
    int_id = UARTIntStatus(REGS_UART);
140

    
141
#ifdef _TMS320C6X
142
    // Clear UART2 system interrupt in DSPINTC
143
    IntEventClear(UART_INT);
144
#else
145
    /* Clears the system interupt status of UART2 in AINTC. */
146
    IntSystemStatusClear(UART_INT);
147
#endif
148
  
149
    /* Checked if the cause is transmitter empty condition.*/
150
    if(UART_INTID_TX_EMPTY == int_id)
151
    {
152
        if(0 < length)
153
        {
154
            /* Write a byte into the THR if THR is free. */
155
            UARTCharPutNonBlocking(REGS_UART, txArray[count]);
156
            length--;
157
            count++;
158
        }
159
        if(0 == length)
160
        {
161
            /* Disable the Transmitter interrupt in UART.*/
162
            UARTIntDisable(REGS_UART, UART_INT_TX_EMPTY);
163
        }
164
     }
165

    
166
    /* Check if the cause is receiver data condition.*/
167
    if(UART_INTID_RX_DATA == int_id)
168
    {
169
        rxData = UARTCharGetNonBlocking(REGS_UART);
170
        UARTCharPutNonBlocking(REGS_UART, rxData);
171
    }
172

    
173

    
174
    /* Check if the cause is receiver line error condition.*/
175
    if(UART_INTID_RX_LINE_STAT == int_id)
176
    {
177
        while(UARTRxErrorGet(REGS_UART))
178
        {
179
            /* Read a byte from the RBR if RBR has data.*/
180
            UARTCharGetNonBlocking(REGS_UART);
181
        }
182
    }
183

    
184
    return;
185
}
186

    
187
/*
188
** \brief   This function invokes necessary functions to configure the ARM
189
**          processor and ARM Interrupt Controller(AINTC) to receive and
190
**          handle interrupts.
191
*/
192

    
193

    
194
static void SetupInt(void)
195
{
196
	int i;
197
#ifdef _TMS320C6X
198
	// Initialize the DSP INTC
199
	IntDSPINTCInit();
200

    
201
	// Enable DSP interrupts globally
202
	IntGlobalEnable();
203
#else
204
    /* Initialize the ARM Interrupt Controller(AINTC). */
205
    IntAINTCInit();
206

    
207
    for (i = 0; i < NUM_INTERRUPTS; i++)
208
    {
209
    	IntUnRegister(i);
210
    }
211

    
212
    /* Enable IRQ in CPSR.*/     
213
    IntMasterIRQEnable();
214

    
215
    /* Enable the interrupts in GER of AINTC.*/
216
    IntGlobalEnable();
217

    
218
    /* Enable the interrupts in HIER of AINTC.*/
219
    IntIRQEnable();
220
#endif
221
}
222

    
223
/*
224
** \brief  This function confiugres the AINTC to receive UART interrupts.
225
*/ 
226
static void ConfigureIntUART(void)
227
{
228
#ifdef _TMS320C6X
229
	IntRegister(C674X_MASK_INT4, UARTIsr);
230
	IntEventMap(C674X_MASK_INT4, UART_INT);
231
	IntEnable(C674X_MASK_INT4);
232
#else
233
    /* Registers the UARTIsr in the Interrupt Vector Table of AINTC. */
234
    IntRegister(UART_INT, UARTIsr);
235

    
236
    /* Map the channel number 2 of AINTC to UART2 system interrupt. */
237
    IntChannelSet(UART_INT, 2);
238

    
239
    IntSystemEnable(UART_INT);
240
#endif
241
}
242

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