Project

General

Profile

Revision 1461

Added by Brad Neuman over 13 years ago

updated all the library code to have sensible _init behavior.
Almost all of the library components have a global variable which gets set after init and the functions inside will fail with an error code if init has not been called. Also, the init functions themselves check this variable and will bail out without doing any damage if that init has already been called

View differences:

serial.c
36 36
#include <avr/io.h>
37 37
#include "serial.h"
38 38

  
39
unsigned char usb_initd=0;
40
unsigned char xbee_initd=0;
41

  
39 42
#ifdef USE_STDIO
40 43

  
41 44
#include <stdio.h>
......
57 60
 * This must be called before any other usb function
58 61
 * may be used.
59 62
 **/
60
void usb_init() {
63
int usb_init() {
61 64
  //Set baud rate
62 65
  // - 115200 (both wired and wireless) is UBRR=8, U2X=1
63 66
  // - 9600 is U2X =1, UBRR = 107.
67

  
68
  if(usb_initd) {
69
    return ERROR_INIT_ALREADY_INITD;
70
  }
71

  
64 72
#if (USB_BAUD == 115200)
65 73
  UBRR0H = 0x00;
66 74
  UBRR0L = 8;
......
70 78
  UBRR0L = 103;
71 79
  UCSR0A |= _BV(U2X0);
72 80
#else //Baud rate is defined in the header file, we should not get here
73
  return;
81
  return 0;
74 82
#endif
75 83

  
76 84
  /*Enable receiver and transmitter */
......
84 92
  /* Open the stdio stream corresponding to this port */
85 93
  usb_fd = fdevopen(usb_putc, usb_getc);
86 94
#endif
95

  
96
  usb_initd = 1;
97
  return 0;
98

  
87 99
}
88 100

  
89 101
/**
......
91 103
 * This must be called before any other xbee function
92 104
 * may be used.
93 105
 **/
94
void xbee_init() {
106
int xbee_init() {
107

  
108
  if(xbee_initd) {
109
    return ERROR_INIT_ALREADY_INITD;
110
  }
111

  
95 112
  //Set baud rate
96 113
  // - 115200 (both wired and wireless) is UBRR=8, U2X=1
97 114
  // - 9600 is U2X =1, UBRR = 107.
......
104 121
  UBRR1L = 103;
105 122
  UCSR1A |= _BV(U2X1);
106 123
#else //Baud rate is defined in the header file, we should not get here
107
  return;
124
  return 0;
108 125
#endif
109 126

  
110 127
  //Enable receiver and transmitter
......
118 135
  /* Open the stdio stream corresponding to this port */
119 136
  xbee_fd = fdevopen(xbee_putc, xbee_getc);
120 137
#endif
138

  
139
  xbee_initd = 1;
140
  return 0;
141

  
121 142
}
122 143

  
123 144
/**
......
127 148
 * @return 0 for success, nonzero for failure
128 149
 **/
129 150
int usb_putc(char c) {
151

  
152
  if(!usb_initd)
153
    return ERROR_LIBRARY_NOT_INITD;
154

  
130 155
  // Wait until buffer is clear for sending
131 156
  loop_until_bit_is_set(UCSR0A, UDRE0);
132 157
	
......
142 167
 * @return 0 for success, nonzero for failure
143 168
 **/
144 169
int xbee_putc(char c) {
170

  
171
  if(!xbee_initd)
172
    return ERROR_LIBRARY_NOT_INITD;
173

  
145 174
  // Wait until buffer is clear for sending
146 175
  loop_until_bit_is_set(UCSR1A, UDRE1);
147 176
	
......
158 187
 **/
159 188
int usb_puts(char *s) {
160 189
  char *t = s;
190

  
191
  if(!usb_initd)
192
    return ERROR_LIBRARY_NOT_INITD;
193

  
161 194
  while (*t != 0) {
162 195
    usb_putc(*t);
163 196
    t++;
......
169 202
 * Sends a sequence of characters from program space over USB.
170 203
 *
171 204
 * @param s the string to send
205
 *
206
 * @return 0 if init succesfull, an error code otherwise
172 207
 **/
173
void usb_puts_P (PGM_P s) {
208
int usb_puts_P (PGM_P s) {
174 209
    char buf;
210

  
211
    if(!usb_initd)
212
      return ERROR_LIBRARY_NOT_INITD;
175 213
	
176 214
    while (memcpy_P (&buf, s, sizeof (char)), buf!=0) {
177 215
        usb_putc (buf);
178 216
        s++;
179 217
    }
218

  
219
    return 0;
180 220
}
181 221

  
182 222

  
......
186 226
 * This function blocks execution until a character has been received.
187 227
 * xbee_init must be called before this function may be used.
188 228
 * 
189
 * @return the first character in the usb buffer
229
 * @return the first character in the usb buffer, -1 on error
190 230
 *
191 231
 * @see usb_init, usb_getc_nb
192 232
 **/
193 233
int usb_getc(void) {
234

  
235
  if(!usb_initd)
236
    return -1;
237

  
194 238
  // Wait for the receive buffer to be filled
195 239
  loop_until_bit_is_set(UCSR0A, RXC0);
196 240
	
......
204 248
 * received. xbee_init must be called before this function
205 249
 * may be used.
206 250
 * 
207
 * @return the first character in the xbee buffer
251
 * @return the first character in the xbee buffer, -1 on error
208 252
 * 
209 253
 * @see xbee_init, xbee_getc_nb
210 254
 **/
211 255
int xbee_getc(void) {
256

  
257
  if(!usb_initd)
258
    return -1;
259

  
212 260
  // Wait for the receive buffer to be filled
213 261
  loop_until_bit_is_set(UCSR1A, RXC1);
214 262
	
......
224 272
 * @param c the received character. This will be set if a character has
225 273
 * been received.
226 274
 * 
227
 * @return -1 if no character is available, 0 otherwise
275
 * @return -1 if no character is available, 0 otherwise, positive for error
228 276
 * 
229 277
 * @see usb_init, usb_getc
230 278
 **/
231 279
int usb_getc_nb(char *c) {
280

  
281
  if(!usb_initd)
282
    return ERROR_LIBRARY_NOT_INITD;
283

  
232 284
  // check if the receive buffer is filled
233 285
  if (UCSR0A & _BV(RXC0)) {
234 286
    // Read the receive buffer
......
248 300
 * @param c the received character. This will be set if a character has
249 301
 * been received.
250 302
 * 
251
 * @return -1 if no character is available, 0 otherwise
303
 * @return -1 if no character is available, 0 otherwise, positive for error
252 304
 *
253 305
 * @see xbee_init, xbee_getc
254 306
 **/
255 307
int xbee_getc_nb(char *c) {
308
  if(!xbee_initd)
309
    return ERROR_LIBRARY_NOT_INITD;
310

  
256 311
  // check if the receive buffer is filled
257 312
  if (UCSR1A & _BV(RXC1)) {
258 313
    // Read the receive buffer
......
284 339
int usb_puti(int value ) {
285 340
  unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; 
286 341

  
342
  if(!usb_initd)
343
    return ERROR_LIBRARY_NOT_INITD;
344

  
345

  
287 346
  /* convert int to ascii  */ 
288 347
  if(value<0) { 
289 348
    usb_putc('-'); 
......
329 388
 * @param value the value to print
330 389
 * 
331 390
 * @see usb_init, usb_puti, usb_puts, usb_puth8, hex_digit
391
 *
392
 * @return 0 if init succesfull, an error code otherwise
332 393
 **/
333
void usb_puth16 (uint16_t value)
394
int usb_puth16 (uint16_t value)
334 395
{
396
    if(!usb_initd)
397
      return ERROR_LIBRARY_NOT_INITD;
398

  
335 399
    usb_putc (hex_digit((value >>12)&0xF));
336 400
    usb_putc (hex_digit((value >>8 )&0xF));
337 401
    usb_putc (hex_digit((value >>4 )&0xF));
338 402
    usb_putc (hex_digit( value      &0xF));
403

  
404
    return 0;
339 405
}
340 406

  
341 407
/**
......
346 412
 * @param value the value to print
347 413
 * 
348 414
 * @see usb_init, usb_puti, usb_puts, usb_puth16, hex_digit
415
 *
416
 * @return 0 if init succesfull, an error code otherwise
349 417
 **/
350
void usb_puth8(uint8_t value)
418
int usb_puth8(uint8_t value)
351 419
{
420
    if(!usb_initd)
421
      return ERROR_LIBRARY_NOT_INITD;
422

  
352 423
    usb_putc (hex_digit ((value)>>4 &0xF));
353 424
    usb_putc (hex_digit ( value     &0xF));
425

  
426
    return 0;
354 427
}
355 428

  

Also available in: Unified diff