OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [cortexm/] [lpc17xx/] [var/] [current/] [src/] [hal_diag.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=============================================================================
2
//
3
//      hal_diag.c
4
//
5
//      HAL diagnostic output code
6
//
7
//=============================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2008, 2010  Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//=============================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    nickg, jani, ilijak
43
// Contributors: jskov, gthomas
44
// Date:         2010-12-15
45
// Purpose:      HAL diagnostic output
46
// Description:  Implementations of HAL diagnostic output support.
47
//
48
//####DESCRIPTIONEND####
49
//
50
//=============================================================================
51
 
52
#include <pkgconf/hal.h>
53
#include CYGBLD_HAL_PLATFORM_H
54
 
55
#include <cyg/infra/cyg_type.h>        // base types
56
#include <cyg/infra/cyg_trac.h>        // tracing
57
 
58
#include <cyg/hal/hal_arch.h>          // SAVE/RESTORE GP macros
59
#include <cyg/hal/hal_io.h>            // IO macros
60
#include <cyg/hal/hal_if.h>            // interface API
61
#include <cyg/hal/hal_intr.h>          // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
62
#include <cyg/hal/hal_misc.h>          // Helper functions
63
#include <cyg/hal/drv_api.h>           // CYG_ISR_HANDLED
64
 
65
#include <cyg/hal/var_io.h>            // USART registers
66
#include <cyg/hal/lpc17xx_misc.h>      // peripheral identifiers
67
 
68
//-----------------------------------------------------------------------------
69
 
70
typedef struct {
71
    cyg_uint32      uart;
72
    CYG_ADDRESS     base;
73
    cyg_int32       msec_timeout;
74
    int             isr_vector;
75
    int             baud_rate;
76
    cyg_uint8       periph_id;
77
    int             irq_state;
78
} channel_data_t;
79
 
80
static channel_data_t lpc17xx_ser_channels[] = {
81
#if CYGINT_HAL_LPC17XX_UART0>0
82
    {0,
83
     CYGHWR_HAL_LPC17XX_REG_UART0_BASE,
84
     1000,
85
     CYGNUM_HAL_INTERRUPT_UART0,
86
     CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD,
87
     CYNUM_HAL_LPC17XX_PCLK_UART0},
88
#endif
89
#if CYGINT_HAL_LPC17XX_UART1>0
90
    {1,
91
     CYGHWR_HAL_LPC17XX_REG_UART1_BASE,
92
     1000,
93
     CYGNUM_HAL_INTERRUPT_UART1,
94
     CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD,
95
     CYNUM_HAL_LPC17XX_PCLK_UART1}
96
#endif
97
};
98
 
99
#define HAL_PLF_SER_CHANNELS lpc17xx_ser_channels
100
 
101
 
102
//===========================================================================
103
// Initialize diagnostic serial channel
104
//===========================================================================
105
static void
106
hal_plf_serial_init_channel(void *__ch_data)
107
{
108
    channel_data_t *chan = (channel_data_t *) __ch_data;
109
    CYG_ADDRESS     base = chan->base;
110
 
111
    hal_plf_uart_setbaud(base, chan->baud_rate);
112
 
113
    // 8-1-no parity.
114
    HAL_WRITE_UINT32(base + CYGHWR_HAL_LPC17XX_REG_UxLCR,
115
                     CYGHWR_HAL_LPC17XX_REG_UxLCR_WORD_LENGTH_8 |
116
                     CYGHWR_HAL_LPC17XX_REG_UxLCR_STOP_1);
117
 
118
    // Reset and enable FIFO
119
    HAL_WRITE_UINT32(base + CYGHWR_HAL_LPC17XX_REG_UxFCR,
120
                     CYGHWR_HAL_LPC17XX_REG_UxFCR_FIFO_ENA |
121
                     CYGHWR_HAL_LPC17XX_REG_UxFCR_RX_FIFO_RESET |
122
                     CYGHWR_HAL_LPC17XX_REG_UxFCR_TX_FIFO_RESET);
123
}
124
 
125
 
126
//===========================================================================
127
// Write single character
128
//===========================================================================
129
void
130
hal_plf_serial_putc(void *__ch_data, char c)
131
{
132
    CYG_ADDRESS     base = ((channel_data_t *) __ch_data)->base;
133
    cyg_uint32      sr;
134
 
135
    CYGARC_HAL_SAVE_GP();
136
 
137
    do {
138
        HAL_READ_UINT32(base + CYGHWR_HAL_LPC17XX_REG_UxLSR, sr);
139
    } while ((sr & CYGHWR_HAL_LPC17XX_REG_UxLSR_THRE) == 0);
140
 
141
    HAL_WRITE_UINT32(base + CYGHWR_HAL_LPC17XX_REG_UxTHR, c);
142
 
143
    CYGARC_HAL_RESTORE_GP();
144
}
145
 
146
static cyg_bool
147
hal_plf_serial_getc_nonblock(void *__ch_data, cyg_uint8 *ch)
148
{
149
    CYG_ADDRESS     base = ((channel_data_t *) __ch_data)->base;
150
    cyg_uint32      sr;
151
    cyg_uint32      c;
152
 
153
    CYGARC_HAL_SAVE_GP();
154
 
155
    HAL_READ_UINT32(base + CYGHWR_HAL_LPC17XX_REG_UxLSR, sr);
156
 
157
    if ((sr & CYGHWR_HAL_LPC17XX_REG_UxLSR_RDR) == 0)
158
        return false;
159
 
160
    HAL_READ_UINT32(base + CYGHWR_HAL_LPC17XX_REG_UxRBR, c);
161
 
162
    *ch = (cyg_uint8)c;
163
 
164
    CYGARC_HAL_RESTORE_GP();
165
 
166
    return true;
167
}
168
 
169
cyg_uint8
170
hal_plf_serial_getc(void *__ch_data)
171
{
172
    cyg_uint8       ch;
173
 
174
    CYGARC_HAL_SAVE_GP();
175
 
176
    while (!hal_plf_serial_getc_nonblock(__ch_data, &ch));
177
 
178
    CYGARC_HAL_RESTORE_GP();
179
 
180
    return ch;
181
}
182
 
183
//=============================================================================
184
// Virtual vector HAL diagnostics
185
 
186
#if defined(CYGSEM_HAL_VIRTUAL_VECTOR_DIAG)
187
 
188
static void
189
hal_plf_serial_write(void *__ch_data, const cyg_uint8 *__buf,
190
                     cyg_uint32 __len)
191
{
192
    CYGARC_HAL_SAVE_GP();
193
 
194
    while (__len-- > 0)
195
        hal_plf_serial_putc(__ch_data, *__buf++);
196
 
197
    CYGARC_HAL_RESTORE_GP();
198
}
199
 
200
static void
201
hal_plf_serial_read(void *__ch_data, cyg_uint8 *__buf, cyg_uint32 __len)
202
{
203
    CYGARC_HAL_SAVE_GP();
204
 
205
    while (__len-- > 0)
206
        *__buf++ = hal_plf_serial_getc(__ch_data);
207
 
208
    CYGARC_HAL_RESTORE_GP();
209
}
210
 
211
cyg_bool
212
hal_plf_serial_getc_timeout(void *__ch_data, cyg_uint8 *ch)
213
{
214
    int             delay_count;
215
    channel_data_t *chan = (channel_data_t *) __ch_data;
216
    cyg_bool        res;
217
 
218
    CYGARC_HAL_SAVE_GP();
219
 
220
    // Delay in 10 us steps
221
    delay_count = chan->msec_timeout * 100;
222
 
223
    while(true) {
224
        res = hal_plf_serial_getc_nonblock(__ch_data, ch);
225
        if (res || 0 == delay_count--)
226
            break;
227
 
228
        CYGACC_CALL_IF_DELAY_US(10);
229
    }
230
 
231
    CYGARC_HAL_RESTORE_GP();
232
 
233
    return res;
234
}
235
 
236
static int
237
hal_plf_serial_control(void *__ch_data, __comm_control_cmd_t __func, ...)
238
{
239
    channel_data_t *chan = (channel_data_t *) __ch_data;
240
    CYG_ADDRESS     base = ((channel_data_t *) __ch_data)->base;
241
    int             ret = 0;
242
 
243
    va_list         ap;
244
 
245
    CYGARC_HAL_SAVE_GP();
246
 
247
    va_start(ap, __func);
248
 
249
    switch (__func) {
250
    case __COMMCTL_IRQ_ENABLE:
251
        chan->irq_state = 1;
252
        HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
253
        HAL_INTERRUPT_UNMASK(chan->isr_vector);
254
        HAL_WRITE_UINT32(base + CYGHWR_HAL_LPC17XX_REG_UxIER,
255
                         CYGHWR_HAL_LPC17XX_REG_UxIER_RXDATA_INT);
256
        break;
257
    case __COMMCTL_IRQ_DISABLE:
258
        ret = chan->irq_state;
259
        chan->irq_state = 0;
260
        HAL_INTERRUPT_MASK(chan->isr_vector);
261
        HAL_WRITE_UINT32(base + CYGHWR_HAL_LPC17XX_REG_UxIER,
262
                         CYGHWR_HAL_LPC17XX_REG_UxIER_RXDATA_INT);
263
        break;
264
    case __COMMCTL_DBG_ISR_VECTOR:
265
        ret = chan->isr_vector;
266
        break;
267
    case __COMMCTL_SET_TIMEOUT:
268
        {
269
            va_list         ap;
270
 
271
            va_start(ap, __func);
272
 
273
            ret = chan->msec_timeout;
274
            chan->msec_timeout = va_arg(ap, cyg_uint32);
275
 
276
            va_end(ap);
277
        }
278
    case __COMMCTL_GETBAUD:
279
        ret = chan->baud_rate;
280
        break;
281
    case __COMMCTL_SETBAUD:
282
        chan->baud_rate = va_arg(ap, cyg_int32);
283
        // Should we verify this value here?
284
        hal_plf_uart_setbaud(base, chan->baud_rate);
285
        ret = 0;
286
        break;
287
    default:
288
        break;
289
    }
290
    va_end(ap);
291
    CYGARC_HAL_RESTORE_GP();
292
    return ret;
293
}
294
 
295
static int
296
hal_plf_serial_isr(void *__ch_data, int *__ctrlc,
297
                   CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
298
{
299
    channel_data_t *chan = (channel_data_t *) __ch_data;
300
    cyg_uint8       ch;
301
 
302
    CYGARC_HAL_SAVE_GP();
303
 
304
    *__ctrlc = 0;
305
 
306
    if (hal_plf_serial_getc_nonblock(__ch_data, &ch)) {
307
        if (cyg_hal_is_break((char *)&ch, 1))
308
            *__ctrlc = 1;
309
    }
310
 
311
    HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
312
 
313
    CYGARC_HAL_RESTORE_GP();
314
 
315
    return 1;
316
}
317
 
318
static void
319
hal_plf_serial_init(void)
320
{
321
    hal_virtual_comm_table_t *comm;
322
    int             cur;
323
    int             i;
324
 
325
    cur =
326
        CYGACC_CALL_IF_SET_CONSOLE_COMM
327
        (CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
328
 
329
    for (i = 0; i < CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS; i++) {
330
        hal_plf_serial_init_channel(&lpc17xx_ser_channels[i]);
331
 
332
        CYGACC_CALL_IF_SET_CONSOLE_COMM(i);
333
        comm = CYGACC_CALL_IF_CONSOLE_PROCS();
334
        CYGACC_COMM_IF_CH_DATA_SET(*comm, &lpc17xx_ser_channels[i]);
335
        CYGACC_COMM_IF_WRITE_SET(*comm, hal_plf_serial_write);
336
        CYGACC_COMM_IF_READ_SET(*comm, hal_plf_serial_read);
337
        CYGACC_COMM_IF_PUTC_SET(*comm, hal_plf_serial_putc);
338
        CYGACC_COMM_IF_GETC_SET(*comm, hal_plf_serial_getc);
339
        CYGACC_COMM_IF_CONTROL_SET(*comm, hal_plf_serial_control);
340
        CYGACC_COMM_IF_DBG_ISR_SET(*comm, hal_plf_serial_isr);
341
        CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, hal_plf_serial_getc_timeout);
342
    }
343
 
344
    // Restore original console
345
    CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
346
 
347
    // Set debug channel baud rate if different
348
#if (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD != CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL_BAUD)
349
    lpc17xx_ser_channels[CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL]->baud_rate =
350
        CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL_BAUD;
351
    update_baud_rate(&lpc17xx_ser_channels
352
                     [CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL]);
353
#endif
354
 
355
}
356
 
357
void
358
cyg_hal_plf_comms_init(void)
359
{
360
    static int      initialized = 0;
361
 
362
    if (initialized)
363
        return;
364
 
365
    initialized = 1;
366
 
367
    hal_plf_serial_init();
368
}
369
 
370
#endif // CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
371
 
372
//=============================================================================
373
// Non-Virtual vector HAL diagnostics
374
 
375
#if !defined(CYGSEM_HAL_VIRTUAL_VECTOR_DIAG)
376
 
377
void
378
hal_plf_diag_init(void)
379
{
380
    hal_plf_serial_init(&lpc17xx_ser_channels
381
                        [CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL]);
382
}
383
 
384
void
385
hal_plf_diag_putc(char c)
386
{
387
    hal_plf_serial_putc(&lpc17xx_ser_channels
388
                        [CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL], c);
389
}
390
 
391
cyg_uint8
392
hal_plf_diag_getc(void)
393
{
394
    return
395
        hal_plf_serial_getc(&lpc17xx_ser_channels
396
                            [CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL]);
397
}
398
 
399
#endif // if !defined(CYGSEM_HAL_VIRTUAL_VECTOR_DIAG)
400
 
401
//-----------------------------------------------------------------------------
402
// End of hal_diag.c

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.