]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/sci.c
Excluding from Git annoying preference files that are deleted and regenerated constantly.
[pes-rpp/rpp-lib.git] / rpp / src / rpp / sci.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * File : sci.c
20  * Abstract:
21  *     Serial Communication Interface RPP API implementation file.
22  *
23  * References:
24  *     sci.h
25  *     RPP API documentation.
26  */
27
28
29 #include "rpp/rpp.h"
30
31 #if rppCONFIG_INCLUDE_SCI == 1
32 #include <stdio.h>
33
34 #if rppCONFIG_DRV == 1
35 #include "drv/drv.h"
36 #endif
37
38 static boolean_t initialized = FALSE;
39
40 int8_t rpp_sci_init()
41 {
42     if(initialized) {
43         return FAILURE;
44     }
45     initialized = TRUE;
46
47     #if rppCONFIG_DRV == 1
48     drv_sci_init();
49     #endif
50
51    return SUCCESS;
52 }
53
54
55 boolean_t rpp_sci_setup(uint32_t baud)
56 {
57     boolean_t known = FALSE;
58
59     // FIXME This is a standard list of baud rates. This should include only
60     // tested baud rates.
61     switch(baud) {
62         case 110:
63             known = TRUE;
64             break;
65         case 300:
66             known = TRUE;
67             break;
68         case 600:
69             known = TRUE;
70             break;
71         case 1200:
72             known = TRUE;
73             break;
74         case 2400:
75             known = TRUE;
76             break;
77         case 4800:
78             known = TRUE;
79             break;
80         case 9600:
81             known = TRUE;
82             break;
83         case 14400:
84             known = TRUE;
85             break;
86         case 19200:
87             known = TRUE;
88             break;
89         case 28800:
90             known = TRUE;
91             break;
92         case 38400:
93             known = TRUE;
94             break;
95         case 56000:
96             known = TRUE;
97             break;
98         case 57600:
99             known = TRUE;
100             break;
101         case 115200:
102             known = TRUE;
103             break;
104     }
105
106     #if rppCONFIG_DRV == 1
107     drv_sci_set_baudrate(baud);
108     #endif
109
110     return known;
111 }
112
113
114 uint16_t rpp_sci_available()
115 {
116     uint16_t available = 0;
117
118     #if rppCONFIG_DRV == 1
119     available = drv_sci_available();
120     #endif
121
122     return available;
123 }
124
125
126 int8_t rpp_sci_read(uint32_t amount, uint8_t* buffer)
127 {
128     #if rppCONFIG_DRV == 1
129     drv_sci_receive(amount, buffer, portMAX_DELAY);
130     #endif
131
132     return SUCCESS;
133 }
134
135
136 int8_t rpp_sci_read_nb(uint32_t amount, uint8_t* buffer)
137 {
138     #if rppCONFIG_DRV == 1
139     if(drv_sci_receive(amount, buffer, 0) != SUCCESS) {
140         return FAILURE;
141     }
142     #endif
143
144     return SUCCESS;
145 }
146
147
148 int8_t rpp_sci_write(uint32_t amount, uint8_t* data)
149 {
150     #if rppCONFIG_DRV == 1
151     drv_sci_send(amount, data, portMAX_DELAY);
152     #endif
153     return SUCCESS;
154 }
155
156
157 int8_t rpp_sci_write_nb(uint32_t amount, uint8_t* data)
158 {
159     #if rppCONFIG_DRV == 1
160     if(drv_sci_send(amount, data, 0) != SUCCESS) {
161         return FAILURE;
162     }
163     #endif
164     return SUCCESS;
165 }
166
167
168 int32_t rpp_sci_printf(const char *format, ...)
169 {
170     char str[MAX_BUFFER_LEN];
171     int length = -1;
172
173     va_list argList;
174     va_start(argList, format);
175
176     length = vsnprintf(str, sizeof(str), format, argList);
177
178     va_end(argList);
179
180     if(length > 0) {
181         #if rppCONFIG_DRV == 1
182         if(drv_sci_send((uint32_t)length,
183                         (uint8_t*)str,
184                         portMAX_DELAY) != SUCCESS) {
185             return -1;
186         }
187         #endif
188     }
189
190     return length;
191 }
192
193
194 #endif /* rppCONFIG_INCLUDE_SCI */
195