User manual

6 OnRISC Hardware API
RS485 receive control There are two modes to handle own transmitted messages in 2-wire
mode:
with echo (EPLD_RS485_RTS_ECHO, EPLD_RS485_ART_ECHO): both outgoing and incoming mes-
sages will be received by application
without echo (EPLD_RS485_ART_2W, EPLD_RS485_RTS_2W): application receives only incoming
messages
Baud rate generation interface The OnRISC provides full support for the 16C950 UART baud
rate generation. This allows the user to use the serial interfaces with arbitrary speeds up to
3,6Mbit/s. To use this capability the current baud rate must be set to B38400 and the custom
divisor to the negative value of the desired baud rate. In the source code below the baud rate will
be set to 500000bit/s, so the custom divisor was set to -500000 (see the lines 26-39) and the current
baud rate to 38400 (see the lines 41-66).
1 #include <s t d i o . h>
2 #include <s t d l i b . h>
3 #include <s ys / ty pe s . h>
4 #include <s ys / time . h>
5 #include <s ys / i o c t l . h>
6 #include <s ys / st a t . h>
7 #include <u ni st d . h>
8 #include <t er mi o s . h>
9 #include < f c n t l . h>
10 #include <l i n u x / s e r i a l . h>
11 #include <s t r i n g . h>
12
13 int main ( in t argc , char argv )
14 {
15 int fd , r e t ;
16 stru c t t er mi os s e r_ te rm io s ;
17 stru c t s e r i a l _ s t r u c t s s_ st ;
18
19 f d = open ( " / dev / ttyS 1 " , (O_RDWR | O_NOCTTY) ) ;
20 i f ( fd < 0 )
21 {
22 p er r o r ( " open " ) ;
23 return 1;
24 }
25
26 // s e t custom d i v i s o r t o 500000 t o a c h i ev e 500000 b i t /s
27 i f ( i o c t l ( fd , TIOCGSERIAL, &s s _s t ) <0)
28 {
29 p er r o r ( "TIOCGSERIAL" ) ;
30 return 1;
31 }
32 s s_ st . c us to m_ di vi so r = 500000;
33 s s_ st . f l a g s |= ASYNC_SPD_CUST;
34
35 i f ( i o c t l ( fd , TIOCSSERIAL, &ss _st )<0)
36 {
37 p er r o r ( "TIOCSSERIAL" ) ;
38 return 1;
39 }
40
41 // s e t baud r a t e t o 38400 b i t /s t o a c t i v a t e custom d i v i s o r
42 r e t = t c g e t a t t r ( fd , &s er _t er m io s ) ;
43 i f ( r e t <0 )
44 {
45 p er r o r ( " g e t a t t r " ) ;
46 return 1;
47 }
48 . . .
49 r e t = c f s e t i s p e e d (& se r_ te rm io s , B38400 ) ;
May 2014 OnRISC User Manual 43