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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [devs/] [serial/] [cortexm/] [stm32/] [current/] [src/] [stm32_serial.c] - Blame information for rev 810

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      devs/serial/cortexm/stm32/stm32_serial.c
4
//
5
//      ST STM32 Serial I/O Interface Module
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
43
// Date:          2008-09-10
44
// Purpose:       ST STM32 Serial I/O module
45
// Description: 
46
//
47
//####DESCRIPTIONEND####
48
//
49
//==========================================================================
50
 
51
#include <pkgconf/hal.h>
52
#include <pkgconf/infra.h>
53
#include <pkgconf/system.h>
54
#include <pkgconf/io_serial.h>
55
#include <pkgconf/io.h>
56
#include <pkgconf/kernel.h>
57
 
58
#include <cyg/io/io.h>
59
#include <cyg/hal/hal_io.h>
60
#include <cyg/hal/hal_intr.h>
61
#include <cyg/hal/hal_cache.h>
62
#include <cyg/io/devtab.h>
63
#include <cyg/io/serial.h>
64
#include <cyg/infra/diag.h>
65
#include <cyg/infra/cyg_type.h>
66
#include <cyg/infra/cyg_ass.h>
67
 
68
 
69
#ifdef CYGPKG_IO_SERIAL_CORTEXM_STM32
70
 
71
#include "stm32_serial.h"
72
 
73
//==========================================================================
74
 
75
#define STM32_RXBUFSIZE 16
76
 
77
typedef struct stm32_serial_info
78
{
79
    CYG_WORD            uart;
80
    CYG_ADDRWORD        base;
81
    CYG_WORD            int_num;
82
    cyg_int32           rx_pin;
83
    cyg_int32           tx_pin;
84
    cyg_int32           rts_pin;
85
    cyg_int32           cts_pin;
86
 
87
    cyg_bool            tx_active;
88
 
89
    volatile cyg_uint8  buf[STM32_RXBUFSIZE];
90
    volatile int        buf_head;
91
    volatile int        buf_tail;
92
 
93
    cyg_interrupt       serial_interrupt;
94
    cyg_handle_t        serial_interrupt_handle;
95
} stm32_serial_info;
96
 
97
//==========================================================================
98
 
99
static bool stm32_serial_init(struct cyg_devtab_entry *tab);
100
static bool stm32_serial_putc_interrupt(serial_channel *chan, unsigned char c);
101
#if (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL0) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BUFSIZE == 0) \
102
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL1) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BUFSIZE == 0) \
103
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL2) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BUFSIZE == 0) \
104
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL3) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BUFSIZE == 0) \
105
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL4) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BUFSIZE == 0)
106
static bool stm32_serial_putc_polled(serial_channel *chan, unsigned char c);
107
#endif
108
static Cyg_ErrNo stm32_serial_lookup(struct cyg_devtab_entry **tab,
109
                                    struct cyg_devtab_entry *sub_tab,
110
                                    const char *name);
111
static unsigned char stm32_serial_getc_interrupt(serial_channel *chan);
112
#if (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL0) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BUFSIZE == 0) \
113
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL1) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BUFSIZE == 0) \
114
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL2) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BUFSIZE == 0) \
115
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL3) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BUFSIZE == 0) \
116
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL4) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BUFSIZE == 0)
117
static unsigned char stm32_serial_getc_polled(serial_channel *chan);
118
#endif
119
static Cyg_ErrNo stm32_serial_set_config(serial_channel *chan, cyg_uint32 key,
120
                                        const void *xbuf, cyg_uint32 *len);
121
static void stm32_serial_start_xmit(serial_channel *chan);
122
static void stm32_serial_stop_xmit(serial_channel *chan);
123
 
124
static cyg_uint32 stm32_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
125
static void       stm32_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);
126
 
127
//==========================================================================
128
 
129
#if (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL0) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BUFSIZE > 0) \
130
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL1) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BUFSIZE > 0) \
131
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL2) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BUFSIZE > 0) \
132
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL3) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BUFSIZE > 0) \
133
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL4) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BUFSIZE > 0)
134
static SERIAL_FUNS(stm32_serial_funs_interrupt,
135
                   stm32_serial_putc_interrupt,
136
                   stm32_serial_getc_interrupt,
137
                   stm32_serial_set_config,
138
                   stm32_serial_start_xmit,
139
                   stm32_serial_stop_xmit
140
    );
141
#endif
142
 
143
#if (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL0) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BUFSIZE == 0) \
144
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL1) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BUFSIZE == 0) \
145
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL2) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BUFSIZE == 0) \
146
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL3) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BUFSIZE == 0) \
147
 || (defined(CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL4) && CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BUFSIZE == 0)
148
static SERIAL_FUNS(stm32_serial_funs_polled,
149
                   stm32_serial_putc_polled,
150
                   stm32_serial_getc_polled,
151
                   stm32_serial_set_config,
152
                   stm32_serial_start_xmit,
153
                   stm32_serial_stop_xmit
154
    );
155
#endif
156
 
157
//==========================================================================
158
 
159
#ifdef CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL0
160
static stm32_serial_info stm32_serial_info0 = {
161
    uart            : 0,
162
    base            : (CYG_ADDRWORD) CYGHWR_HAL_STM32_UART1,
163
    int_num         : CYGNUM_HAL_INTERRUPT_UART1,
164
    rx_pin          : CYGHWR_HAL_STM32_UART1_RX,
165
    tx_pin          : CYGHWR_HAL_STM32_UART1_TX,
166
    rts_pin         : CYGHWR_HAL_STM32_UART1_RTS,
167
    cts_pin         : CYGHWR_HAL_STM32_UART1_CTS,
168
};
169
 
170
#if CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BUFSIZE > 0
171
static unsigned char stm32_serial_out_buf0[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BUFSIZE];
172
static unsigned char stm32_serial_in_buf0[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BUFSIZE];
173
 
174
static SERIAL_CHANNEL_USING_INTERRUPTS(stm32_serial_channel0,
175
                                       stm32_serial_funs_interrupt,
176
                                       stm32_serial_info0,
177
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BAUD),
178
                                       CYG_SERIAL_STOP_DEFAULT,
179
                                       CYG_SERIAL_PARITY_DEFAULT,
180
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
181
                                       CYG_SERIAL_FLAGS_DEFAULT,
182
                                       &stm32_serial_out_buf0[0], sizeof(stm32_serial_out_buf0),
183
                                       &stm32_serial_in_buf0[0], sizeof(stm32_serial_in_buf0)
184
    );
185
#else
186
static SERIAL_CHANNEL(stm32_serial_channel0,
187
                      stm32_serial_funs_polled,
188
                      stm32_serial_info0,
189
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL0_BAUD),
190
                      CYG_SERIAL_STOP_DEFAULT,
191
                      CYG_SERIAL_PARITY_DEFAULT,
192
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
193
                      CYG_SERIAL_FLAGS_DEFAULT
194
    );
195
#endif
196
 
197
DEVTAB_ENTRY(stm32_serial_io0,
198
             CYGDAT_IO_SERIAL_CORTEXM_STM32_SERIAL0_NAME,
199
             0,                     // Does not depend on a lower level interface
200
             &cyg_io_serial_devio,
201
             stm32_serial_init,
202
             stm32_serial_lookup,     // Serial driver may need initializing
203
             &stm32_serial_channel0
204
    );
205
#endif //  CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL0
206
 
207
//==========================================================================
208
 
209
#ifdef CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL1
210
static stm32_serial_info stm32_serial_info1 = {
211
    uart            : 1,
212
    base            : (CYG_ADDRWORD) CYGHWR_HAL_STM32_UART2,
213
    int_num         : CYGNUM_HAL_INTERRUPT_UART2,
214
    rx_pin          : CYGHWR_HAL_STM32_UART2_RX,
215
    tx_pin          : CYGHWR_HAL_STM32_UART2_TX,
216
    rts_pin         : CYGHWR_HAL_STM32_UART2_RTS,
217
    cts_pin         : CYGHWR_HAL_STM32_UART2_CTS,
218
};
219
 
220
#if CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BUFSIZE > 0
221
static unsigned char stm32_serial_out_buf1[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BUFSIZE];
222
static unsigned char stm32_serial_in_buf1[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BUFSIZE];
223
 
224
static SERIAL_CHANNEL_USING_INTERRUPTS(stm32_serial_channel1,
225
                                       stm32_serial_funs_interrupt,
226
                                       stm32_serial_info1,
227
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BAUD),
228
                                       CYG_SERIAL_STOP_DEFAULT,
229
                                       CYG_SERIAL_PARITY_DEFAULT,
230
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
231
                                       CYG_SERIAL_FLAGS_DEFAULT,
232
                                       &stm32_serial_out_buf1[0], sizeof(stm32_serial_out_buf1),
233
                                       &stm32_serial_in_buf1[0], sizeof(stm32_serial_in_buf1)
234
    );
235
#else
236
static SERIAL_CHANNEL(stm32_serial_channel1,
237
                      stm32_serial_funs_polled,
238
                      stm32_serial_info1,
239
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL1_BAUD),
240
                      CYG_SERIAL_STOP_DEFAULT,
241
                      CYG_SERIAL_PARITY_DEFAULT,
242
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
243
                      CYG_SERIAL_FLAGS_DEFAULT
244
    );
245
#endif
246
 
247
DEVTAB_ENTRY(stm32_serial_io1,
248
             CYGDAT_IO_SERIAL_CORTEXM_STM32_SERIAL1_NAME,
249
             0,                     // Does not depend on a lower level interface
250
             &cyg_io_serial_devio,
251
             stm32_serial_init,
252
             stm32_serial_lookup,     // Serial driver may need initializing
253
             &stm32_serial_channel1
254
    );
255
#endif //  CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL1
256
 
257
//==========================================================================
258
 
259
#ifdef CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL2
260
static stm32_serial_info stm32_serial_info2 = {
261
    uart            : 2,
262
    base            : (CYG_ADDRWORD) CYGHWR_HAL_STM32_UART3,
263
    int_num         : CYGNUM_HAL_INTERRUPT_UART3,
264
    rx_pin          : CYGHWR_HAL_STM32_UART3_RX,
265
    tx_pin          : CYGHWR_HAL_STM32_UART3_TX,
266
    rts_pin         : CYGHWR_HAL_STM32_UART3_RTS,
267
    cts_pin         : CYGHWR_HAL_STM32_UART3_CTS,
268
};
269
 
270
#if CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BUFSIZE > 0
271
static unsigned char stm32_serial_out_buf2[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BUFSIZE];
272
static unsigned char stm32_serial_in_buf2[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BUFSIZE];
273
 
274
static SERIAL_CHANNEL_USING_INTERRUPTS(stm32_serial_channel2,
275
                                       stm32_serial_funs_interrupt,
276
                                       stm32_serial_info2,
277
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BAUD),
278
                                       CYG_SERIAL_STOP_DEFAULT,
279
                                       CYG_SERIAL_PARITY_DEFAULT,
280
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
281
                                       CYG_SERIAL_FLAGS_DEFAULT,
282
                                       &stm32_serial_out_buf2[0], sizeof(stm32_serial_out_buf2),
283
                                       &stm32_serial_in_buf2[0], sizeof(stm32_serial_in_buf2)
284
    );
285
#else
286
static SERIAL_CHANNEL(stm32_serial_channel2,
287
                      stm32_serial_funs_polled,
288
                      stm32_serial_info2,
289
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL2_BAUD),
290
                      CYG_SERIAL_STOP_DEFAULT,
291
                      CYG_SERIAL_PARITY_DEFAULT,
292
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
293
                      CYG_SERIAL_FLAGS_DEFAULT
294
    );
295
#endif
296
 
297
DEVTAB_ENTRY(stm32_serial_io2,
298
             CYGDAT_IO_SERIAL_CORTEXM_STM32_SERIAL2_NAME,
299
             0,                     // Does not depend on a lower level interface
300
             &cyg_io_serial_devio,
301
             stm32_serial_init,
302
             stm32_serial_lookup,     // Serial driver may need initializing
303
             &stm32_serial_channel2
304
    );
305
#endif //  CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL2
306
 
307
//==========================================================================
308
 
309
#ifdef CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL3
310
static stm32_serial_info stm32_serial_info3 = {
311
    uart            : 3,
312
    base            : (CYG_ADDRWORD) CYGHWR_HAL_STM32_UART4,
313
    int_num         : CYGNUM_HAL_INTERRUPT_UART4,
314
    rx_pin          : CYGHWR_HAL_STM32_UART4_RX,
315
    tx_pin          : CYGHWR_HAL_STM32_UART4_TX,
316
    rts_pin         : CYGHWR_HAL_STM32_UART4_RTS,
317
    cts_pin         : CYGHWR_HAL_STM32_UART4_CTS,
318
};
319
 
320
#if CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BUFSIZE > 0
321
static unsigned char stm32_serial_out_buf3[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BUFSIZE];
322
static unsigned char stm32_serial_in_buf3[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BUFSIZE];
323
 
324
static SERIAL_CHANNEL_USING_INTERRUPTS(stm32_serial_channel3,
325
                                       stm32_serial_funs_interrupt,
326
                                       stm32_serial_info3,
327
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BAUD),
328
                                       CYG_SERIAL_STOP_DEFAULT,
329
                                       CYG_SERIAL_PARITY_DEFAULT,
330
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
331
                                       CYG_SERIAL_FLAGS_DEFAULT,
332
                                       &stm32_serial_out_buf3[0], sizeof(stm32_serial_out_buf3),
333
                                       &stm32_serial_in_buf3[0], sizeof(stm32_serial_in_buf3)
334
    );
335
#else
336
static SERIAL_CHANNEL(stm32_serial_channel3,
337
                      stm32_serial_funs_polled,
338
                      stm32_serial_info3,
339
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL3_BAUD),
340
                      CYG_SERIAL_STOP_DEFAULT,
341
                      CYG_SERIAL_PARITY_DEFAULT,
342
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
343
                      CYG_SERIAL_FLAGS_DEFAULT
344
    );
345
#endif
346
 
347
DEVTAB_ENTRY(stm32_serial_io3,
348
             CYGDAT_IO_SERIAL_CORTEXM_STM32_SERIAL3_NAME,
349
             0,                     // Does not depend on a lower level interface
350
             &cyg_io_serial_devio,
351
             stm32_serial_init,
352
             stm32_serial_lookup,     // Serial driver may need initializing
353
             &stm32_serial_channel3
354
    );
355
#endif //  CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL3
356
 
357
//==========================================================================
358
 
359
#ifdef CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL4
360
static stm32_serial_info stm32_serial_info4 = {
361
    uart            : 4,
362
    base            : (CYG_ADDRWORD) CYGHWR_HAL_STM32_UART5,
363
    int_num         : CYGNUM_HAL_INTERRUPT_UART5,
364
    rx_pin          : CYGHWR_HAL_STM32_UART5_RX,
365
    tx_pin          : CYGHWR_HAL_STM32_UART5_TX,
366
    rts_pin         : CYGHWR_HAL_STM32_UART5_RTS,
367
    cts_pin         : CYGHWR_HAL_STM32_UART5_CTS,
368
};
369
 
370
#if CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BUFSIZE > 0
371
static unsigned char stm32_serial_out_buf4[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BUFSIZE];
372
static unsigned char stm32_serial_in_buf4[CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BUFSIZE];
373
 
374
static SERIAL_CHANNEL_USING_INTERRUPTS(stm32_serial_channel4,
375
                                       stm32_serial_funs_interrupt,
376
                                       stm32_serial_info4,
377
                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BAUD),
378
                                       CYG_SERIAL_STOP_DEFAULT,
379
                                       CYG_SERIAL_PARITY_DEFAULT,
380
                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,
381
                                       CYG_SERIAL_FLAGS_DEFAULT,
382
                                       &stm32_serial_out_buf4[0], sizeof(stm32_serial_out_buf4),
383
                                       &stm32_serial_in_buf4[0], sizeof(stm32_serial_in_buf4)
384
    );
385
#else
386
static SERIAL_CHANNEL(stm32_serial_channel4,
387
                      stm32_serial_funs_polled,
388
                      stm32_serial_info4,
389
                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_CORTEXM_STM32_SERIAL4_BAUD),
390
                      CYG_SERIAL_STOP_DEFAULT,
391
                      CYG_SERIAL_PARITY_DEFAULT,
392
                      CYG_SERIAL_WORD_LENGTH_DEFAULT,
393
                      CYG_SERIAL_FLAGS_DEFAULT
394
    );
395
#endif
396
 
397
DEVTAB_ENTRY(stm32_serial_io4,
398
             CYGDAT_IO_SERIAL_CORTEXM_STM32_SERIAL4_NAME,
399
             0,                     // Does not depend on a lower level interface
400
             &cyg_io_serial_devio,
401
             stm32_serial_init,
402
             stm32_serial_lookup,     // Serial driver may need initializing
403
             &stm32_serial_channel4
404
    );
405
#endif //  CYGPKG_IO_SERIAL_CORTEXM_STM32_SERIAL4
406
 
407
 
408
//==========================================================================
409
// Internal function to actually configure the hardware to desired baud
410
// rate, etc.
411
 
412
static bool
413
stm32_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init)
414
{
415
    stm32_serial_info * const stm32_chan = (stm32_serial_info *)chan->dev_priv;
416
    const CYG_ADDRWORD base = stm32_chan->base;
417
    cyg_uint32 parity = select_parity[new_config->parity];
418
    cyg_uint32 word_length = ((new_config->word_length == CYGNUM_SERIAL_WORD_LENGTH_8) &&
419
        (new_config->parity != CYGNUM_SERIAL_PARITY_NONE)) ?
420
        CYGHWR_HAL_STM32_UART_CR1_M_9 : CYGHWR_HAL_STM32_UART_CR1_M_8;
421
    cyg_uint32 stop_bits = select_stop_bits[new_config->stop];
422
    cyg_uint32 cr1 = 0;
423
    cyg_uint32 cr2 = 0;
424
    cyg_uint32 cr3 = 0;
425
 
426
    // Set up FIFO buffer
427
    stm32_chan->buf_head = stm32_chan->buf_tail = 0;
428
 
429
    // Set up GPIO pins
430
    CYGHWR_HAL_STM32_GPIO_SET( stm32_chan->rx_pin );
431
    CYGHWR_HAL_STM32_GPIO_SET( stm32_chan->tx_pin );
432
    CYGHWR_HAL_STM32_GPIO_SET( stm32_chan->rts_pin );
433
    CYGHWR_HAL_STM32_GPIO_SET( stm32_chan->cts_pin );
434
 
435
    // Select line parameters
436
    cr1 |= parity|word_length;
437
    cr2 |= stop_bits;
438
 
439
    cr1 |= CYGHWR_HAL_STM32_UART_CR1_TE | CYGHWR_HAL_STM32_UART_CR1_RE;
440
 
441
    HAL_WRITE_UINT32( base+CYGHWR_HAL_STM32_UART_CR1, cr1 );
442
    HAL_WRITE_UINT32( base+CYGHWR_HAL_STM32_UART_CR2, cr2 );
443
 
444
    // Set up baud rate
445
    hal_stm32_uart_setbaud( base, select_baud[new_config->baud] );
446
 
447
    // Enable the uart
448
    cr1 |= CYGHWR_HAL_STM32_UART_CR1_UE;
449
    HAL_WRITE_UINT32( base+CYGHWR_HAL_STM32_UART_CR1, cr1 );
450
 
451
 
452
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
453
    // Handle RTS by hand but leave CTS to be handled by the UART hardware
454
     if ( (new_config->flags & CYGNUM_SERIAL_FLOW_RTSCTS_TX) && stm32_chan->cts_pin != CYGHWR_HAL_STM32_GPIO_NONE )
455
     {
456
         cr3 |= CYGHWR_HAL_STM32_UART_CR3_CTSE;
457
     }
458
#endif
459
 
460
    if(1)
461
    {
462
        // Enable receive and error interrupts
463
 
464
        cr1 |= CYGHWR_HAL_STM32_UART_CR1_RXNEIE;
465
        cr3 |= CYGHWR_HAL_STM32_UART_CR3_EIE;
466
 
467
        HAL_WRITE_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
468
    }
469
 
470
    HAL_WRITE_UINT32( base+CYGHWR_HAL_STM32_UART_CR3, cr3 );
471
 
472
    stm32_chan->tx_active = false;
473
 
474
    if (new_config != &chan->config)
475
        chan->config = *new_config;
476
 
477
    return true;
478
}
479
 
480
//==========================================================================
481
// Function to initialize the device.  Called at bootstrap time.
482
 
483
static bool
484
stm32_serial_init(struct cyg_devtab_entry *tab)
485
{
486
    serial_channel * const chan = (serial_channel *) tab->priv;
487
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
488
    int res;
489
 
490
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
491
    if (chan->out_cbuf.len != 0) {
492
        cyg_drv_interrupt_create(stm32_chan->int_num,
493
                                 0x80,
494
                                 (cyg_addrword_t)chan,
495
                                 stm32_serial_ISR,
496
                                 stm32_serial_DSR,
497
                                 &stm32_chan->serial_interrupt_handle,
498
                                 &stm32_chan->serial_interrupt);
499
        cyg_drv_interrupt_attach(stm32_chan->serial_interrupt_handle);
500
        cyg_drv_interrupt_unmask(stm32_chan->int_num);
501
 
502
    }
503
 
504
    res = stm32_serial_config_port(chan, &chan->config, true);
505
    return res;
506
}
507
 
508
//==========================================================================
509
// This routine is called when the device is "looked" up (i.e. attached)
510
 
511
static Cyg_ErrNo
512
stm32_serial_lookup(struct cyg_devtab_entry **tab,
513
                  struct cyg_devtab_entry *sub_tab,
514
                  const char *name)
515
{
516
    serial_channel * const chan = (serial_channel *) (*tab)->priv;
517
 
518
    (chan->callbacks->serial_init)(chan);  // Really only required for interrupt driven devices
519
    return ENOERR;
520
}
521
 
522
//==========================================================================
523
// Send a character to the device output buffer.
524
// Return 'true' if character is sent to device
525
 
526
static bool
527
stm32_serial_putc_interrupt(serial_channel *chan, unsigned char c)
528
{
529
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
530
    const CYG_ADDRWORD base = stm32_chan->base;
531
    cyg_uint32 status;
532
 
533
    HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_SR, status );
534
 
535
    if (status & CYGHWR_HAL_STM32_UART_SR_TXE)
536
    {
537
        HAL_WRITE_UINT32( base + CYGHWR_HAL_STM32_UART_DR, c );
538
        return true;
539
    }
540
 
541
    return false;
542
}
543
 
544
//==========================================================================
545
 
546
static bool
547
stm32_serial_putc_polled(serial_channel *chan, unsigned char c)
548
{
549
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
550
    const CYG_ADDRWORD base = stm32_chan->base;
551
    cyg_uint32 status;
552
 
553
     do {
554
         HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_SR, status );
555
     } while ((status & CYGHWR_HAL_STM32_UART_SR_TXE) == 0);
556
 
557
     HAL_WRITE_UINT32( base + CYGHWR_HAL_STM32_UART_DR, c );
558
 
559
    return true;
560
}
561
 
562
//==========================================================================
563
// Fetch a character from the device input buffer
564
 
565
static unsigned char
566
stm32_serial_getc_interrupt(serial_channel *chan)
567
{
568
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
569
    const CYG_ADDRWORD base = stm32_chan->base;
570
    CYG_WORD32 c;
571
 
572
    // Read data
573
    HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_DR, c);
574
    return (unsigned char) (c&0xFF);
575
}
576
 
577
//==========================================================================
578
 
579
static unsigned char
580
stm32_serial_getc_polled(serial_channel *chan)
581
{
582
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
583
    const CYG_ADDRWORD base = stm32_chan->base;
584
    cyg_uint32 stat;
585
    cyg_uint32 c;
586
 
587
    do {
588
        HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_SR, stat );
589
    } while ((stat & CYGHWR_HAL_STM32_UART_SR_RXNE) == 0);
590
 
591
    HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_DR, c);
592
 
593
    return (unsigned char) (c&0xFF);
594
}
595
 
596
//==========================================================================
597
// Set up the device characteristics; baud rate, etc.
598
 
599
static Cyg_ErrNo
600
stm32_serial_set_config(serial_channel *chan, cyg_uint32 key,
601
                       const void *xbuf, cyg_uint32 *len)
602
{
603
    switch (key) {
604
    case CYG_IO_SET_CONFIG_SERIAL_INFO:
605
      {
606
        cyg_serial_info_t *config = (cyg_serial_info_t *)xbuf;
607
        if ( *len < sizeof(cyg_serial_info_t) ) {
608
            return -EINVAL;
609
        }
610
        *len = sizeof(cyg_serial_info_t);
611
        if ( true != stm32_serial_config_port(chan, config, false) )
612
            return -EINVAL;
613
      }
614
      break;
615
 
616
#ifdef CYGOPT_IO_SERIAL_FLOW_CONTROL_HW
617
 
618
    case CYG_IO_SET_CONFIG_SERIAL_HW_RX_FLOW_THROTTLE:
619
      {
620
          stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
621
          cyg_uint32 *f = (cyg_uint32 *)xbuf;
622
 
623
          if ( *len < sizeof(*f) )
624
              return -EINVAL;
625
 
626
          if ( chan->config.flags & CYGNUM_SERIAL_FLOW_RTSCTS_RX )
627
          {
628
              stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
629
 
630
              // Note that the RTS line is active-low, so set it to 1
631
              // to throttle and 0 to allow the data to flow.
632
              if( *f )
633
                  CYGHWR_HAL_STM32_GPIO_OUT( stm32_chan->rts_pin, 1 );
634
              else
635
                  CYGHWR_HAL_STM32_GPIO_OUT( stm32_chan->rts_pin, 0 );
636
          }
637
      }
638
      break;
639
 
640
    case CYG_IO_SET_CONFIG_SERIAL_HW_FLOW_CONFIG:
641
      {
642
        stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
643
        Cyg_ErrNo result = ENOERR;
644
 
645
        // If the client is asking for DSR/DTR, refuse to do it.
646
        if (0 != (chan->config.flags & (CYGNUM_SERIAL_FLOW_DSRDTR_RX | CYGNUM_SERIAL_FLOW_DSRDTR_TX)))
647
        {
648
            chan->config.flags &= ~(CYGNUM_SERIAL_FLOW_DSRDTR_RX | CYGNUM_SERIAL_FLOW_DSRDTR_TX);
649
            result = -ENOSUPP;
650
        }
651
 
652
        // If the client is asking for RTS/CTS then only allow it if
653
        // the port has RTS/CTS lines attached to it.
654
        if (0 != (chan->config.flags & (CYGNUM_SERIAL_FLOW_RTSCTS_RX | CYGNUM_SERIAL_FLOW_RTSCTS_TX)))
655
        {
656
            if( stm32_chan->rts_pin != CYGHWR_HAL_STM32_GPIO_NONE &&
657
                stm32_chan->cts_pin != CYGHWR_HAL_STM32_GPIO_NONE )
658
            {
659
                chan->config.flags &= (CYGNUM_SERIAL_FLOW_RTSCTS_RX | CYGNUM_SERIAL_FLOW_RTSCTS_TX);
660
            }
661
            else
662
            {
663
                chan->config.flags &= ~(CYGNUM_SERIAL_FLOW_RTSCTS_RX | CYGNUM_SERIAL_FLOW_RTSCTS_TX);
664
                result = -ENOSUPP;
665
            }
666
        }
667
        return result;
668
      }
669
 
670
#endif
671
 
672
    default:
673
        return -EINVAL;
674
    }
675
    return ENOERR;
676
}
677
 
678
//==========================================================================
679
// Enable the transmitter on the device
680
 
681
static void
682
stm32_serial_start_xmit(serial_channel *chan)
683
{
684
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
685
    const CYG_ADDRWORD base = stm32_chan->base;
686
    cyg_uint32 cr1;
687
 
688
    if( !stm32_chan->tx_active )
689
    {
690
        stm32_chan->tx_active = true;
691
        HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
692
        cr1 |= CYGHWR_HAL_STM32_UART_CR1_TXEIE;
693
        HAL_WRITE_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
694
    }
695
 
696
}
697
 
698
//==========================================================================
699
// Disable the transmitter on the device
700
 
701
static void
702
stm32_serial_stop_xmit(serial_channel *chan)
703
{
704
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
705
    const CYG_ADDRWORD base = stm32_chan->base;
706
    cyg_uint32 cr1;
707
 
708
    if( stm32_chan->tx_active )
709
    {
710
        stm32_chan->tx_active = false;
711
        HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
712
        cr1 &= ~CYGHWR_HAL_STM32_UART_CR1_TXEIE;
713
        HAL_WRITE_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
714
    }
715
 
716
}
717
 
718
//==========================================================================
719
// Serial I/O - low level interrupt handler (ISR)
720
//
721
// This ISR does rather more than other serial driver ISRs. Normally,
722
// the ISR just masks the interrupt vector and schedules the DSR,
723
// which then handles all IO. However, if the processor is running out
724
// of external RAM it is too slow to handle higher baud rates using
725
// that technique. Something that is exacerbated by the lack of FIFOs
726
// in the USART hardware.
727
//
728
// Instead, this ISR receives any incoming data into a circular
729
// buffer, essentially providing the FIFO lacking in the
730
// hardware. Transmission is still offloaded to the DSR. Only TX
731
// interrupts are masked while this is done to prevent an interrupt
732
// loop, and to avoid blocking RX interrupts.
733
 
734
static cyg_uint32
735
stm32_serial_ISR(cyg_vector_t vector, cyg_addrword_t data)
736
{
737
    serial_channel * const chan = (serial_channel *) data;
738
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
739
    const CYG_ADDRWORD base = stm32_chan->base;
740
    cyg_uint32 stat;
741
    cyg_uint32 ret = CYG_ISR_HANDLED;
742
    cyg_drv_interrupt_acknowledge(vector);
743
 
744
    HAL_READ_UINT32(base + CYGHWR_HAL_STM32_UART_SR, stat);
745
 
746
    if( stat & CYGHWR_HAL_STM32_UART_SR_RXNE )
747
    {
748
        cyg_uint32 c;
749
 
750
        while( stat & CYGHWR_HAL_STM32_UART_SR_RXNE )
751
        {
752
            int next = stm32_chan->buf_head+1;
753
 
754
            if( next == STM32_RXBUFSIZE ) next = 0;
755
 
756
            HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_DR, c);
757
 
758
            if( next != stm32_chan->buf_tail )
759
            {
760
                stm32_chan->buf[stm32_chan->buf_head] = c&0xFF;
761
                stm32_chan->buf_head = next;
762
                ret |= CYG_ISR_CALL_DSR;
763
            }
764
            else
765
            {
766
                // TODO: deal with buffer overflow
767
            }
768
 
769
            HAL_READ_UINT32(base + CYGHWR_HAL_STM32_UART_SR, stat);
770
        }
771
    }
772
    else if( stat & CYGHWR_HAL_STM32_UART_SR_TXE )
773
    {
774
        cyg_uint32 cr1;
775
        HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
776
        cr1 &= ~CYGHWR_HAL_STM32_UART_CR1_TXEIE;
777
        HAL_WRITE_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
778
 
779
        ret |= CYG_ISR_CALL_DSR;
780
    }
781
 
782
 
783
    if( stat & CYGHWR_HAL_STM32_UART_SR_CTS )
784
    {
785
        // Clear CTS status if we see it.
786
        stat &= ~CYGHWR_HAL_STM32_UART_SR_CTS;
787
        HAL_WRITE_UINT32( base + CYGHWR_HAL_STM32_UART_SR, stat );
788
    }
789
 
790
    if( stat & (CYGHWR_HAL_STM32_UART_SR_FE|CYGHWR_HAL_STM32_UART_SR_NE|CYGHWR_HAL_STM32_UART_SR_ORE) )
791
    {
792
        // TODO: Handle hardware errors
793
    }
794
 
795
    return ret;
796
}
797
 
798
//==========================================================================
799
// Serial I/O - high level interrupt handler (DSR)
800
 
801
static void
802
stm32_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
803
{
804
    serial_channel * const chan = (serial_channel *) data;
805
    stm32_serial_info * const stm32_chan = (stm32_serial_info *) chan->dev_priv;
806
    const CYG_ADDRWORD base = stm32_chan->base;
807
    CYG_WORD32 stat;
808
 
809
    while( stm32_chan->buf_head != stm32_chan->buf_tail )
810
    {
811
        int next = stm32_chan->buf_tail+1;
812
        cyg_uint8 c;
813
 
814
        if( next == STM32_RXBUFSIZE ) next = 0;
815
        c = stm32_chan->buf[stm32_chan->buf_tail];
816
        stm32_chan->buf_tail = next;
817
 
818
        (chan->callbacks->rcv_char)(chan, c);
819
    }
820
 
821
    HAL_READ_UINT32(base + CYGHWR_HAL_STM32_UART_SR, stat);
822
 
823
    if( stm32_chan->tx_active && stat & CYGHWR_HAL_STM32_UART_SR_TXE )
824
    {
825
        cyg_uint32 cr1;
826
 
827
        (chan->callbacks->xmt_char)(chan);
828
 
829
        if( stm32_chan->tx_active )
830
        {
831
            HAL_READ_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
832
            cr1 |= CYGHWR_HAL_STM32_UART_CR1_TXEIE;
833
            HAL_WRITE_UINT32( base + CYGHWR_HAL_STM32_UART_CR1, cr1 );
834
        }
835
    }
836
}
837
 
838
//==========================================================================
839
#endif // CYGPKG_IO_SERIAL_CORTEXM_STM32
840
// end of stm32_serial.c

powered by: WebSVN 2.1.0

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