Changeset 295 for soft/giet_vm/giet_libs/stdio.c
- Timestamp:
- Mar 26, 2014, 6:44:44 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/giet_libs/stdio.c
r289 r295 19 19 20 20 //////////////////////////////////////////////////////////////////////////////////// 21 // giet_procid()22 ////////////////////////////////////////////////////////////////////////////////////23 // This function returns the processor identifier.24 ////////////////////////////////////////////////////////////////////////////////////25 int giet_procid()26 {27 return sys_call( SYSCALL_PROCID,28 0, 0, 0, 0 );29 }30 ////////////////////////////////////////////////////////////////////////////////////31 // giet_proctime()32 ////////////////////////////////////////////////////////////////////////////////////33 // This function returns the local processor time (clock cycles since boot)34 ////////////////////////////////////////////////////////////////////////////////////35 int giet_proctime()36 {37 return sys_call( SYSCALL_PROCTIME,38 0, 0, 0, 0 );39 }40 41 42 ////////////////////////////////////////////////////////////////////////////////////43 21 ///////////////////// TTY device related system calls ///////////////////////////// 44 22 //////////////////////////////////////////////////////////////////////////////////// 45 23 46 //////////////////////////////////////////////////////////////////////////////////// 47 // giet_tty_putc() 48 //////////////////////////////////////////////////////////////////////////////////// 49 // This function displays a single ascii character on a terminal. 50 // The terminal index must be defined in the task context in the boot phase. 51 // It doesn't use the TTY_PUT_IRQ interrupt, and the associated kernel buffer. 52 // - Returns 1 if the character has been written, 0 otherwise. 53 //////////////////////////////////////////////////////////////////////////////////// 54 int giet_tty_putc(char byte) 55 { 56 return sys_call(SYSCALL_TTY_WRITE, 57 (unsigned int)(&byte), 58 1, 59 0xFFFFFFFF, 60 0); 61 } 62 //////////////////////////////////////////////////////////////////////////////////// 63 // giet_tty_puts() 64 //////////////////////////////////////////////////////////////////////////////////// 65 // This function displays a string on a terminal. 66 // The terminal index must be defined in the task context in the boot phase. 67 // The string must be terminated by a NUL character. 68 // It doesn't use the TTY_PUT_IRQ interrupt, and the associated kernel buffer. 69 // - Returns the number of written characters. 70 //////////////////////////////////////////////////////////////////////////////////// 71 int giet_tty_puts(char * buf) 72 { 73 unsigned int length = 0; 74 while (buf[length] != 0) { length++; } 75 return sys_call(SYSCALL_TTY_WRITE, 76 (unsigned int)buf, 77 length, 78 0xFFFFFFFF, 79 0); 80 } 81 //////////////////////////////////////////////////////////////////////////////////// 82 // giet_tty_putw() 83 //////////////////////////////////////////////////////////////////////////////////// 84 // This function displays the value of a 32-bit word with decimal characters. 85 // The terminal index must be defined in the task context in the boot phase. 86 // It doesn't use the TTY_PUT_IRQ interrupt, and the associated kernel buffer. 87 // Returns the number of written characters (should be equal to ten). 88 //////////////////////////////////////////////////////////////////////////////////// 89 int giet_tty_putw(unsigned int val) 90 { 91 char buf[10]; 92 unsigned int i; 93 for (i = 0; i < 10; i++) 94 { 95 buf[9 - i] = (val % 10) + 0x30; 96 val = val / 10; 97 } 98 return sys_call(SYSCALL_TTY_WRITE, 99 (unsigned int)buf, 100 10, 101 0xFFFFFFFF, 102 0); 103 } 104 //////////////////////////////////////////////////////////////////////////////////// 105 // giet_tty_getc() 106 //////////////////////////////////////////////////////////////////////////////////// 107 // This blocking function fetches a single ascii character from a terminal. 108 // The terminal index must be defined in the task context in the boot phase. 109 // It uses the IRQ_GET interrupt, and the associated kernel buffer. 110 // - Returns 0 when completed. 111 //////////////////////////////////////////////////////////////////////////////////// 112 int giet_tty_getc(char * byte) 113 { 114 unsigned int ret = 0; 115 while (ret == 0) 24 //////////////////////////////////////// 25 void giet_tty_printf( char* format, ...) 26 { 27 va_list args; 28 va_start( args, format ); 29 30 int ret; // return value from the syscalls 31 unsigned int channel = 0xFFFFFFFF; 32 33 printf_text: 34 35 while (*format) 36 { 37 unsigned int i; 38 for (i = 0 ; format[i] && (format[i] != '%') ; i++); 39 if (i) 40 { 41 ret = sys_call(SYSCALL_TTY_WRITE, 42 (unsigned int)format, 43 i, 44 channel, 45 0); 46 47 if (ret != i) goto return_error; 48 49 format += i; 50 } 51 if (*format == '%') 52 { 53 format++; 54 goto printf_arguments; 55 } 56 } 57 58 va_end( args ); 59 return; 60 61 printf_arguments: 62 63 { 64 char buf[20]; 65 char * pbuf; 66 unsigned int len = 0; 67 static const char HexaTab[] = "0123456789ABCDEF"; 68 unsigned int i; 69 70 switch (*format++) 71 { 72 case ('c'): /* char conversion */ 73 { 74 int val = va_arg( args, int ); 75 len = 1; 76 buf[0] = val; 77 pbuf = &buf[0]; 78 break; 79 } 80 case ('d'): /* 32 bits decimal signed integer */ 81 { 82 int val = va_arg( args, int ); 83 if (val < 0) 84 { 85 val = -val; 86 ret = sys_call(SYSCALL_TTY_WRITE, 87 (unsigned int)"-", 88 1, 89 channel, 90 0); 91 if (ret != 1) goto return_error; 92 } 93 for(i = 0; i < 10; i++) 94 { 95 buf[9 - i] = HexaTab[val % 10]; 96 if (!(val /= 10)) break; 97 } 98 len = i + 1; 99 pbuf = &buf[9 - i]; 100 break; 101 } 102 case ('u'): /* 32 bits decimal unsigned integer */ 103 { 104 unsigned int val = va_arg( args, unsigned int ); 105 for(i = 0; i < 10; i++) 106 { 107 buf[9 - i] = HexaTab[val % 10]; 108 if (!(val /= 10)) break; 109 } 110 len = i + 1; 111 pbuf = &buf[9 - i]; 112 break; 113 } 114 case ('x'): /* 32 bits hexadecimal integer */ 115 { 116 unsigned int val = va_arg( args, unsigned int ); 117 ret = sys_call(SYSCALL_TTY_WRITE, 118 (unsigned int)"0x", 119 2, 120 channel, 121 0); 122 if (ret != 2) goto return_error; 123 for(i = 0; i < 8; i++) 124 { 125 buf[7 - i] = HexaTab[val % 16]; 126 if (!(val /= 16)) break; 127 } 128 len = i + 1; 129 pbuf = &buf[7 - i]; 130 break; 131 } 132 case ('l'): /* 64 bits hexadecimal unsigned */ 133 { 134 unsigned long long val = va_arg( args, unsigned long long ); 135 ret = sys_call(SYSCALL_TTY_WRITE, 136 (unsigned int)"0x", 137 2, 138 channel, 139 0); 140 if (ret != 2) goto return_error; 141 for(i = 0; i < 16; i++) 142 { 143 buf[15 - i] = HexaTab[val % 16]; 144 if (!(val /= 16)) break; 145 } 146 len = i + 1; 147 pbuf = &buf[15 - i]; 148 break; 149 } 150 case ('s'): /* string */ 151 { 152 char* str = va_arg( args, char* ); 153 while (str[len]) 154 { 155 len++; 156 } 157 pbuf = str; 158 break; 159 } 160 default: 161 goto return_error; 162 } 163 164 ret = sys_call(SYSCALL_TTY_WRITE, 165 (unsigned int)pbuf, 166 len, 167 channel, 168 0); 169 if (ret != len) goto return_error; 170 171 goto printf_text; 172 } 173 174 return_error: 175 176 va_end( args ); 177 giet_exit("error in giet_tty_printf()"); 178 } // end giet_tty_printf() 179 180 //////////////////////////////////////// 181 void giet_shr_printf( char* format, ...) 182 { 183 va_list args; 184 va_start( args, format ); 185 186 int ret; // return value from the syscalls 187 unsigned int channel = 0; 188 unsigned int sr_save; 189 190 sys_call( SYSCALL_TTY_GET_LOCK, 191 channel, 192 (unsigned int)&sr_save, 193 0, 0 ); 194 195 printf_text: 196 197 while (*format) 198 { 199 unsigned int i; 200 for (i = 0 ; format[i] && (format[i] != '%') ; i++); 201 if (i) 202 { 203 ret = sys_call(SYSCALL_TTY_WRITE, 204 (unsigned int)format, 205 i, 206 channel, 207 0); 208 209 if (ret != i) goto return_error; 210 211 format += i; 212 } 213 if (*format == '%') 214 { 215 format++; 216 goto printf_arguments; 217 } 218 } 219 220 sys_call( SYSCALL_TTY_RELEASE_LOCK, 221 channel, 222 (unsigned int)&sr_save, 223 0, 0 ); 224 225 va_end( args ); 226 return; 227 228 printf_arguments: 229 230 { 231 char buf[20]; 232 char * pbuf; 233 unsigned int len = 0; 234 static const char HexaTab[] = "0123456789ABCDEF"; 235 unsigned int i; 236 237 switch (*format++) 238 { 239 case ('c'): /* char conversion */ 240 { 241 int val = va_arg( args, int ); 242 len = 1; 243 buf[0] = val; 244 pbuf = &buf[0]; 245 break; 246 } 247 case ('d'): /* 32 bits decimal signed integer */ 248 { 249 int val = va_arg( args, int ); 250 if (val < 0) 251 { 252 val = -val; 253 ret = sys_call(SYSCALL_TTY_WRITE, 254 (unsigned int)"-", 255 1, 256 channel, 257 0); 258 if (ret != 1) goto return_error; 259 } 260 for(i = 0; i < 10; i++) 261 { 262 buf[9 - i] = HexaTab[val % 10]; 263 if (!(val /= 10)) break; 264 } 265 len = i + 1; 266 pbuf = &buf[9 - i]; 267 break; 268 } 269 case ('u'): /* 32 bits decimal unsigned integer */ 270 { 271 unsigned int val = va_arg( args, unsigned int ); 272 for(i = 0; i < 10; i++) 273 { 274 buf[9 - i] = HexaTab[val % 10]; 275 if (!(val /= 10)) break; 276 } 277 len = i + 1; 278 pbuf = &buf[9 - i]; 279 break; 280 } 281 case ('x'): /* 32 bits hexadecimal integer */ 282 { 283 unsigned int val = va_arg( args, unsigned int ); 284 ret = sys_call(SYSCALL_TTY_WRITE, 285 (unsigned int)"0x", 286 2, 287 channel, 288 0); 289 if (ret != 2) goto return_error; 290 for(i = 0; i < 8; i++) 291 { 292 buf[7 - i] = HexaTab[val % 16]; 293 if (!(val /= 16)) break; 294 } 295 len = i + 1; 296 pbuf = &buf[7 - i]; 297 break; 298 } 299 case ('l'): /* 64 bits hexadecimal unsigned */ 300 { 301 unsigned long long val = va_arg( args, unsigned long long ); 302 ret = sys_call(SYSCALL_TTY_WRITE, 303 (unsigned int)"0x", 304 2, 305 channel, 306 0); 307 if (ret != 2) goto return_error; 308 for(i = 0; i < 16; i++) 309 { 310 buf[15 - i] = HexaTab[val % 16]; 311 if (!(val /= 16)) break; 312 } 313 len = i + 1; 314 pbuf = &buf[15 - i]; 315 break; 316 } 317 case ('s'): /* string */ 318 { 319 char* str = va_arg( args, char* ); 320 while (str[len]) 321 { 322 len++; 323 } 324 pbuf = str; 325 break; 326 } 327 default: 328 goto return_error; 329 } 330 331 ret = sys_call(SYSCALL_TTY_WRITE, 332 (unsigned int)pbuf, 333 len, 334 channel, 335 0); 336 if (ret != len) goto return_error; 337 338 goto printf_text; 339 } 340 341 return_error: 342 343 sys_call( SYSCALL_TTY_RELEASE_LOCK, 344 channel, 345 (unsigned int)&sr_save, 346 0, 0 ); 347 348 va_end( args ); 349 giet_exit("error in giet_shr_printf()"); 350 } // end giet_shr_printf() 351 352 353 ///////////////////////////////// 354 void giet_tty_getc( char * byte ) 355 { 356 int ret; 357 358 do 116 359 { 117 360 ret = sys_call(SYSCALL_TTY_READ, … … 120 363 0xFFFFFFFF, // channel index from task context 121 364 0); 122 } 123 return 0; 124 } 125 //////////////////////////////////////////////////////////////////////////////////// 126 // giet_tty_gets() 127 //////////////////////////////////////////////////////////////////////////////////// 128 // This blocking function fetches a string from a terminal to a fixed length buffer. 129 // The terminal index must be defined in the task context in the boot phase. 130 // It uses the TTY_GET_IRQ interrupt, anf the associated kernel buffer. 131 // - Returns 0 when completed. 132 // - Up to (bufsize - 1) characters (including the non printable characters) 133 // will be copied into buffer, and the string is always completed by a NUL 134 // character. 135 // - The <LF> character is interpreted, and the function close the string with a 136 // NUL character if <LF> is read. 137 // - The <DEL> character is interpreted, and the corresponding character(s) are 138 // removed from the target buffer. 139 //////////////////////////////////////////////////////////////////////////////////// 140 int giet_tty_gets( char* buf, 141 unsigned int bufsize) 142 { 143 unsigned int ret; 365 if ( ret < 0 ) giet_exit("error in giet_tty_getc()"); 366 } 367 while (ret != 1); 368 } 369 370 ///////////////////////////////////// 371 void giet_tty_gets( char* buf, 372 unsigned int bufsize ) 373 { 374 int ret; 144 375 unsigned char byte; 145 unsigned int index = 0;146 376 unsigned int index = 0; 377 147 378 while (index < (bufsize - 1)) 148 379 { … … 154 385 0xFFFFFFFF, 155 386 0); 387 if ( ret < 0 ) giet_exit("error in giet_tty_gets()"); 156 388 } 157 389 while (ret != 1); … … 172 404 } 173 405 buf[index] = 0; 174 return 0; 175 } 176 //////////////////////////////////////////////////////////////////////////////////// 177 // giet_tty_getw() 178 //////////////////////////////////////////////////////////////////////////////////// 179 // This blocking function fetches a string of decimal characters (most 180 // significant digit first) to build a 32-bit unsigned integer. 181 // The terminal index must be defined in the task context in the boot phase. 182 // It uses the TTY_GET_IRQ interrupt, anf the associated kernel buffer. 183 // - Returns necessarily 0 when completed. 184 // 185 // - The non-blocking system function _tty_read_irq is called several times, 186 // and the decimal characters are written in a 32 characters buffer until a 187 // <LF> character is read. 188 // - The <DEL> character is interpreted, and previous characters can be 189 // cancelled. All others characters are ignored. 190 // - When the <LF> character is received, the string is converted to an 191 // unsigned int value. If the number of decimal digit is too large for the 32 192 // bits range, the zero value is returned. 193 //////////////////////////////////////////////////////////////////////////////////// 194 int giet_tty_getw(unsigned int * val) 406 } 407 408 /////////////////////////////////////// 409 void giet_tty_getw( unsigned int* val ) 195 410 { 196 411 unsigned char buf[32]; 197 unsigned char byte; 198 unsigned int save = 0; 199 unsigned int dec = 0; 200 unsigned int done = 0; 201 unsigned int overflow = 0; 202 unsigned int max = 0; 203 unsigned int i; 204 unsigned int ret; 205 412 unsigned int string_byte = 0x00000000; // string containing one single byte 413 unsigned int string_cancel = 0x00082008; // string containing BS/SPACE/BS 414 unsigned int save = 0; 415 unsigned int dec = 0; 416 unsigned int done = 0; 417 unsigned int overflow = 0; 418 unsigned int length = 0; 419 unsigned int i; 420 unsigned int channel = 0xFFFFFFFF; 421 int ret; // return value from syscalls 422 423 // get characters 206 424 while (done == 0) 207 425 { 426 // read one character 208 427 do 209 428 { 210 ret = sys_call(SYSCALL_TTY_READ, 211 (unsigned int)(&byte), 212 1, 213 0xFFFFFFFF, 214 0); 429 ret = sys_call( SYSCALL_TTY_READ, 430 (unsigned int)(&string_byte), 431 1, 432 channel, 433 0); 434 if ( ret < 0 ) giet_exit("error in giet_tty_getw()"); 215 435 } 216 436 while (ret != 1); 217 437 218 if ((byte > 0x2F) && (byte < 0x3A)) /* decimal character */ 219 { 220 buf[max] = byte; 221 max++; 222 giet_tty_putc(byte); 223 } 224 else if ((byte == 0x0A)) /* LF */ 438 // analyse character 439 if ((string_byte > 0x2F) && (string_byte < 0x3A)) /* decimal character */ 440 { 441 buf[length] = (unsigned char)string_byte; 442 length++; 443 444 // echo 445 ret = sys_call( SYSCALL_TTY_WRITE, 446 (unsigned int)(&string_byte), 447 1, 448 channel, 449 0 ); 450 if ( ret < 0 ) giet_exit("error in giet_tty_gets()"); 451 } 452 else if (string_byte == 0x0A) /* LF character */ 225 453 { 226 454 done = 1; 227 455 } 228 else if (byte == 0x7F) /* DEL */ 229 { 230 if (max > 0) 231 { 232 max--; /* cancel the character */ 233 giet_tty_putc(0x08); 234 giet_tty_putc(0x20); 235 giet_tty_putc(0x08); 236 } 237 } 238 if (max == 32) /* decimal string overflow */ 239 { 240 for (i = 0; i < max; i++) 241 { 242 /* cancel the string */ 243 giet_tty_putc(0x08); 244 giet_tty_putc(0x20); 245 giet_tty_putc(0x08); 246 } 247 giet_tty_putc(0x30); 248 *val = 0; /* return 0 value */ 249 return 0; 250 } 251 } 252 253 /* string conversion */ 254 for (i = 0; i < max; i++) 255 { 256 dec = dec * 10 + (buf[i] - 0x30); 257 if (dec < save) overflow = 1; 258 save = dec; 259 } 260 261 /* check overflow */ 262 if (overflow == 0) 263 { 264 *val = dec; /* return decimal value */ 265 } 266 else 267 { 268 for (i = 0; i < max; i++) 269 { 270 /* cancel the string */ 271 giet_tty_putc(0x08); 272 giet_tty_putc(0x20); 273 giet_tty_putc(0x08); 274 } 275 giet_tty_putc(0x30); 276 *val = 0; /* return 0 value */ 277 } 278 return 0; 279 } 280 //////////////////////////////////////////////////////////////////////////////////// 281 // giet_tty_printf() 282 //////////////////////////////////////////////////////////////////////////////////// 283 // This function is a simplified version of the mutek_printf() function. 284 // The terminal index must be defined in the calling task context. 285 // It doesn't use the IRQ_PUT interrupt, and the associated kernel buffer. 286 // Only a limited number of formats are supported: 287 // - %d : signed decimal 288 // - %u : unsigned decimal 289 // - %x : hexadecimal 290 // - %c : char 291 // - %s : string 292 // - Returns 0 if success, > 0 if error. 293 //////////////////////////////////////////////////////////////////////////////////// 294 int giet_tty_printf(char * format, ...) 295 { 296 va_list ap; 297 va_start(ap, format); 298 unsigned int ret; 299 300 if (NB_TTY_CHANNELS == 1) 301 { 302 ret = sys_call(SYSCALL_TTY_LOCK, 0, 0, 0, 0); // Get TTY lock 303 } 304 305 printf_text: 306 307 while (*format) 308 { 309 unsigned int i; 310 for (i = 0 ; format[i] && (format[i] != '%') ; i++); 311 if (i) 312 { 313 ret = sys_call(SYSCALL_TTY_WRITE, 314 (unsigned int)format, 315 i, 316 0xFFFFFFFF, 317 0); 318 319 if (ret != i) goto return_error; 320 321 format += i; 322 } 323 if (*format == '%') 324 { 325 format++; 326 goto printf_arguments; 327 } 328 } 329 330 if (NB_TTY_CHANNELS == 1) 331 { 332 ret = sys_call(SYSCALL_TTY_LOCK, 1, 0, 0, 0); // Release TTY lock 333 } 334 335 va_end(ap); 336 return 0; 337 338 printf_arguments: 339 340 { 341 int val = va_arg(ap, long); 342 char buf[20]; 343 char * pbuf; 344 unsigned int len = 0; 345 static const char HexaTab[] = "0123456789ABCDEF"; 346 unsigned int i; 347 348 switch (*format++) { 349 case ('c'): /* char conversion */ 350 len = 1; 351 buf[0] = val; 352 pbuf = buf; 353 break; 354 case ('d'): /* decimal signed integer */ 355 if (val < 0) 356 { 357 val = -val; 358 ret = sys_call(SYSCALL_TTY_WRITE, 359 (unsigned int)"-", 360 1, 361 0xFFFFFFFF, 362 0); 363 if (ret != 1) goto return_error; 364 } 365 case ('u'): /* decimal unsigned integer */ 366 for(i = 0; i < 10; i++) 367 { 368 buf[9 - i] = HexaTab[val % 10]; 369 if (!(val /= 10)) break; 370 } 371 len = i + 1; 372 pbuf = &buf[9 - i]; 373 break; 374 case ('x'): /* hexadecimal integer */ 375 ret = sys_call(SYSCALL_TTY_WRITE, 376 (unsigned int)"0x", 377 2, 378 0xFFFFFFFF, 379 0); 380 if (ret != 2) goto return_error; /* return error */ 381 for(i = 0; i < 8; i++) 382 { 383 buf[7 - i] = HexaTab[val % 16U]; 384 if (!(val /= 16U)) break; 385 } 386 len = i + 1; 387 pbuf = &buf[7 - i]; 388 break; 389 case ('s'): /* string */ 390 { 391 char * str = (char *) val; 392 while (str[len]) 393 { 394 len++; 395 } 396 pbuf = (char *) val; 397 } 398 break; 399 default: 400 goto printf_text; 401 } 402 403 ret = sys_call(SYSCALL_TTY_WRITE, 404 (unsigned int)pbuf, 405 len, 406 0xFFFFFFFF, 407 0); 408 if (ret != len) goto return_error; 409 410 goto printf_text; 411 } 412 413 return_error: 414 if (NB_TTY_CHANNELS == 1) 415 { 416 ret = sys_call(SYSCALL_TTY_LOCK, 1, 0, 0, 0); // Release TTY lock 417 } 418 419 return 1; 420 } 421 456 else if ( (string_byte == 0x7F) || /* DEL character */ 457 (string_byte == 0x08) ) /* BS character */ 458 { 459 if ( length > 0 ) 460 { 461 length--; // cancel the character 462 463 ret = sys_call( SYSCALL_TTY_WRITE, 464 (unsigned int)(&string_cancel), 465 3, 466 channel, 467 0 ); 468 if ( ret < 0 ) giet_exit("error in giet_tty_gets()"); 469 } 470 } 471 472 // test buffer overflow 473 if ( length >= 32 ) 474 { 475 overflow = 1; 476 done = 1; 477 } 478 } // end while characters 479 480 // string to int conversion with overflow detection 481 if ( overflow == 0 ) 482 { 483 for (i = 0; (i < length) && (overflow == 0) ; i++) 484 { 485 dec = dec * 10 + (buf[i] - 0x30); 486 if (dec < save) overflow = 1; 487 save = dec; 488 } 489 } 490 491 // final evaluation 492 if ( overflow == 0 ) 493 { 494 // return value 495 *val = dec; 496 } 497 else 498 { 499 // cancel all echo characters 500 for (i = 0; i < length ; i++) 501 { 502 ret = sys_call( SYSCALL_TTY_WRITE, 503 (unsigned int)(&string_cancel), 504 3, 505 channel, 506 0 ); 507 if ( ret < 0 ) giet_exit("error in giet_tty_gets()"); 508 } 509 // echo character '0' 510 string_byte = 0x30; 511 ret = sys_call( SYSCALL_TTY_WRITE, 512 (unsigned int)(&string_byte), 513 1, 514 channel, 515 0 ); 516 if ( ret < 0 ) giet_exit(); 517 518 // return 0 value 519 *val = 0; 520 } 521 } 422 522 423 523 … … 426 526 ////////////////////////////////////////////////////////////////////////////////// 427 527 428 ////////////////////////////////////////////////////////////////////////////////// 429 // giet_timer_start() 430 ////////////////////////////////////////////////////////////////////////////////// 431 // This function activates the private user timer allocated to the calling task 432 // in the boot phase. 433 // - Returns 0 if success, > 0 if error. 434 ////////////////////////////////////////////////////////////////////////////////// 435 int giet_timer_start() 436 { 437 return sys_call( SYSCALL_TIMER_START, 438 0, 0, 0, 0 ); 439 } 440 ////////////////////////////////////////////////////////////////////////////////// 441 // giet_timer_stop() 442 ////////////////////////////////////////////////////////////////////////////////// 443 // This function activates the user timer allocated to the calling task. 444 // - Returns 0 if success, > 0 if error. 445 ////////////////////////////////////////////////////////////////////////////////// 446 int giet_timer_stop() 447 { 448 return sys_call( SYSCALL_TIMER_STOP, 449 0, 0, 0, 0 ); 528 /////////////////////// 529 void giet_timer_start() 530 { 531 if ( sys_call( SYSCALL_TIMER_START, 0, 0, 0, 0 ) ) 532 giet_exit("error in giet_timer_start()"); 533 } 534 535 ////////////////////// 536 void giet_timer_stop() 537 { 538 if ( sys_call( SYSCALL_TIMER_STOP, 0, 0, 0, 0 ) ) 539 giet_exit("error in giet_timer_stop()"); 450 540 } 451 541 … … 455 545 ////////////////////////////////////////////////////////////////////////////////// 456 546 457 ////////////////////////////////////////////////////////////////////////////////// 458 // giet_fb_sync_write() 459 ////////////////////////////////////////////////////////////////////////////////// 460 // This blocking function use a memory copy strategy to transfer data from a 461 // user buffer to the frame buffer device in kernel space. 462 // offset : offset (in bytes) in the frame buffer 463 // buffer : base address of the memory buffer 464 // length : number of bytes to be transfered 465 // - Returns 0 if success, > 0 if error (e.g. memory buffer not in user space). 466 ////////////////////////////////////////////////////////////////////////////////// 467 int giet_fb_sync_write( unsigned int offset, 547 //////////////////////////////////////////// 548 void giet_fb_sync_write( unsigned int offset, 468 549 void * buffer, 469 550 unsigned int length ) 470 551 { 471 return sys_call( SYSCALL_FB_SYNC_WRITE, 472 offset, 473 (unsigned int)buffer, 474 length, 475 0 ); 476 } 477 ////////////////////////////////////////////////////////////////////////////////// 478 // giet_fb_sync_read() 479 ////////////////////////////////////////////////////////////////////////////////// 480 // This blocking function use a memory copy strategy to transfer data from the 481 // frame buffer device in kernel space to an user buffer. 482 // offset : offset (in bytes) in the frame buffer 483 // buffer : base address of the user buffer 484 // length : number of bytes to be transfered 485 // - Returns 0 if success, > 0 if error (e.g. memory buffer not in user space). 486 ////////////////////////////////////////////////////////////////////////////////// 487 int giet_fb_sync_read( unsigned int offset, 488 void * buffer, 489 unsigned int length ) 490 { 491 return sys_call( SYSCALL_FB_SYNC_READ, 492 offset, 493 (unsigned int)buffer, 494 length, 495 0 ); 496 } 497 ////////////////////////////////////////////////////////////////////////////////// 498 // giet_fb_cma_init() 499 ////////////////////////////////////////////////////////////////////////////////// 500 // This function initializes the two chbuf SRC an DST used by the CMA controller 501 // and activates the CMA channel allocated to the calling task. 502 // - buf0 : first user buffer virtual address 503 // - buf0 : second user buffer virtual address 504 // - length : buffer size (bytes) 505 // - Returns 0 if success, > 0 if error. 506 ////////////////////////////////////////////////////////////////////////////////// 507 int giet_fb_cma_init( void * buf0, 508 void * buf1, 509 unsigned int length ) 510 { 511 return sys_call( SYSCALL_FB_CMA_INIT, 512 (unsigned int)buf0, 513 (unsigned int)buf1, 514 length, 515 0 ); 516 } 517 ////////////////////////////////////////////////////////////////////////////////// 518 // giet_fb_cma_write() 519 ////////////////////////////////////////////////////////////////////////////////// 520 // This function set the valid status for one of the SRC user buffer. 521 // and reset the valid status for the DST frame buffer. 522 // - bufffer_id : 0 => buf0 valid is set / not 0 => buf1 valid is set 523 // - Returns 0 if success, > 0 if error. 524 ////////////////////////////////////////////////////////////////////////////////// 525 int giet_fb_cma_write( unsigned int buffer_id ) 526 { 527 return sys_call( SYSCALL_FB_CMA_WRITE, 528 buffer_id, 529 0, 0, 0 ); 530 } 531 ////////////////////////////////////////////////////////////////////////////////// 532 // giet_fb_cma_stop() 533 ////////////////////////////////////////////////////////////////////////////////// 534 // This function desactivates the CMA channel allocated to the calling task. 535 // - Returns 0 if success, > 0 if error. 536 ////////////////////////////////////////////////////////////////////////////////// 537 int giet_fb_cma_stop( ) 538 { 539 return sys_call( SYSCALL_FB_CMA_STOP, 540 0, 0, 0, 0 ); 552 if ( sys_call( SYSCALL_FB_SYNC_WRITE, 553 offset, 554 (unsigned int)buffer, 555 length, 556 0 ) ) giet_exit("error in giet_fb_sync_write()"); 557 } 558 559 /////////////////////////////////////////// 560 void giet_fb_sync_read( unsigned int offset, 561 void * buffer, 562 unsigned int length ) 563 { 564 if ( sys_call( SYSCALL_FB_SYNC_READ, 565 offset, 566 (unsigned int)buffer, 567 length, 568 0 ) ) giet_exit("error in giet_fb_sync_read()"); 569 } 570 571 ///////////////////////////////////////// 572 void giet_fb_cma_init( void * buf0, 573 void * buf1, 574 unsigned int length ) 575 { 576 if ( sys_call( SYSCALL_FB_CMA_INIT, 577 (unsigned int)buf0, 578 (unsigned int)buf1, 579 length, 580 0 ) ) giet_exit("error in giet_fb_cma_init()"); 581 } 582 583 /////////////////////////////////////////////// 584 void giet_fb_cma_write( unsigned int buffer_id ) 585 { 586 if ( sys_call( SYSCALL_FB_CMA_WRITE, 587 buffer_id, 588 0, 0, 0 ) ) giet_exit("error in giet_fb_cma_write()"); 589 } 590 591 //////////////////////// 592 void giet_fb_cma_stop() 593 { 594 if ( sys_call( SYSCALL_FB_CMA_STOP, 595 0, 0, 0, 0 ) ) giet_exit("error in giet_fb_cma_stop()"); 541 596 } 542 597 … … 546 601 ////////////////////////////////////////////////////////////////////////////////// 547 602 548 ////////////////////////////////////////////////////////////////////////////////// 549 // giet_nic_cma_init() 550 ////////////////////////////////////////////////////////////////////////////////// 551 // This function initializes the memory chbuf used by the CMA controller, 552 // activates the NIC channel allocated to the calling task, and the CMA channel. 553 // - tx : RX channel if 0 / TX channel if non 0 554 // - buf0 : first user buffer virtual address 555 // - buf1 : second user buffer virtual address 556 // - length : buffer size (bytes) 557 // - Returns 0 if success, > 0 if error 558 ////////////////////////////////////////////////////////////////////////////////// 559 int giet_nic_cma_start() 560 { 561 return sys_call( SYSCALL_NIC_CMA_START, 562 0, 0, 0, 0 ); 563 } 564 ////////////////////////////////////////////////////////////////////////////////// 565 // giet_nic_cma_stop() 566 ////////////////////////////////////////////////////////////////////////////////// 567 // This function desactivates the NIC channel and the two CMA channels 568 // allocated to the calling task. 569 // - Returns 0 if success, > 0 if error. 570 ////////////////////////////////////////////////////////////////////////////////// 571 int giet_nic_cma_stop( ) 572 { 573 return sys_call( SYSCALL_NIC_CMA_STOP, 574 0, 0, 0, 0 ); 575 } 576 577 578 ////////////////////////////////////////////////////////////////////////////////// 579 ///////////////////// Miscellaneous system calls ///////////////////////////////// 580 ////////////////////////////////////////////////////////////////////////////////// 581 582 /////////////////////////////////////////////////////////////////////////////////// 583 // giet_assert() 584 /////////////////////////////////////////////////////////////////////////////////// 585 // This function uses the giet_tty_puts() and giet_exit() system calls. 586 /////////////////////////////////////////////////////////////////////////////////// 587 void giet_assert( unsigned int condition, 588 char* string ) 589 { 590 if ( condition == 0 ) 591 { 592 giet_tty_puts( string ); 593 giet_exit(); 594 } 595 } 596 ////////////////////////////////////////////////////////////////////////////////// 597 // giet_vobj_get_vbase() 598 ////////////////////////////////////////////////////////////////////////////////// 599 // This function writes in argument (vobj_vaddr) the virtual base address 600 // of a vobj (defined in the mapping_info data structure), identified by 601 // the two arguments (vspace_name and vobj_name). 602 // The (vobj_type) argument is redundant, and used for coherence checking. 603 // - Returns the address if success, 0 if error ( not defined or wrong type ) 604 ////////////////////////////////////////////////////////////////////////////////// 605 int giet_vobj_get_vbase( char* vspace_name, 606 char* vobj_name, 607 unsigned int vobj_type, 608 unsigned int* vobj_vaddr ) 609 { 610 return sys_call( SYSCALL_VOBJ_GET_VBASE, 611 (unsigned int) vspace_name, 612 (unsigned int) vobj_name, 613 (unsigned int) vobj_type, 614 (unsigned int) vobj_vaddr ); 615 } 616 //////////////////////////////////////////////////////////////////////////////////// 617 // giet_proc_number() 618 //////////////////////////////////////////////////////////////////////////////////// 619 // This function returns in the buffer argument the number of processors 620 // in the cluster specified by the cluster_id argument. 621 // - Returns 0 if success, > 0 if error ( cluster index too large ) 622 //////////////////////////////////////////////////////////////////////////////////// 623 int giet_proc_number( unsigned int cluster_id, 624 unsigned int* buffer ) 625 { 626 return sys_call(SYSCALL_PROC_NUMBER, cluster_id, (unsigned int) buffer, 0, 0); 627 } 628 ////////////////////////////////////////////////////////////////////////////////// 629 // giet_exit() 630 ////////////////////////////////////////////////////////////////////////////////// 631 // This function stops execution of the calling task with a TTY message, 632 // the user task is descheduled and becomes not runable. 633 // It does not consume processor cycles anymore. 634 ////////////////////////////////////////////////////////////////////////////////// 635 void giet_exit() 636 { 637 sys_call( SYSCALL_EXIT, 638 0, 0, 0, 0 ); 639 } 640 ////////////////////////////////////////////////////////////////////////////////// 641 // giet_context_switch() 642 ////////////////////////////////////////////////////////////////////////////////// 643 // The user task calling this function is descheduled and 644 // the processor is allocated to another task. 645 ////////////////////////////////////////////////////////////////////////////////// 646 int giet_context_switch() 647 { 648 return sys_call( SYSCALL_CTX_SWITCH, 649 0, 0, 0, 0 ); 650 } 651 ////////////////////////////////////////////////////////////////////////////////// 652 // giet_proc_task_id() 653 ////////////////////////////////////////////////////////////////////////////////// 654 // This functions returns the local task id. 655 // If processor has n tasks the local task index is ranging from 0 to n-1 656 ////////////////////////////////////////////////////////////////////////////////// 657 int giet_proc_task_id() 658 { 659 return sys_call( SYSCALL_LOCAL_TASK_ID, 660 0, 0, 0, 0 ); 661 } 662 ////////////////////////////////////////////////////////////////////////////////// 663 // giet_heap_info() 664 ////////////////////////////////////////////////////////////////////////////////// 665 // This function returns the base address and size of the current task's heap 666 ////////////////////////////////////////////////////////////////////////////////// 667 int giet_heap_info( unsigned int* vaddr, 668 unsigned int* length ) 669 { 670 return sys_call( SYSCALL_HEAP_INFO, 671 (unsigned int)vaddr, 672 (unsigned int)length, 673 0, 0 ); 674 } 675 ////////////////////////////////////////////////////////////////////////////////// 676 // giet_global_task_id() 677 ////////////////////////////////////////////////////////////////////////////////// 678 // This functions returns the global task id, which is unique in all the giet. 679 ////////////////////////////////////////////////////////////////////////////////// 680 int giet_global_task_id() 681 { 682 return sys_call( SYSCALL_GLOBAL_TASK_ID, 683 0, 0, 0, 0 ); 684 } 685 686 ////////////////////////////////////////////////////////////////////////////////// 687 // giet_thread_id() 688 ////////////////////////////////////////////////////////////////////////////////// 689 // This functions returns the thread index of the current task. 690 ////////////////////////////////////////////////////////////////////////////////// 691 int giet_thread_id() 692 { 693 return sys_call( SYSCALL_THREAD_ID, 694 0, 0, 0, 0 ); 603 ///////////////////////// 604 void giet_nic_cma_start() 605 { 606 if ( sys_call( SYSCALL_NIC_CMA_START, 0, 0, 0, 0 ) ) 607 giet_exit("error in giet_nic_cma_start()"); 608 } 609 610 ///////////////////////// 611 void giet_nic_cma_stop() 612 { 613 if ( sys_call( SYSCALL_NIC_CMA_STOP, 0, 0, 0, 0 ) ) 614 giet_exit("error in giet_nic_cma_stop()"); 695 615 } 696 616 … … 699 619 /////////////////////////////////////////////////////////////////////////////////// 700 620 701 /////////////////////////////////////////////////////////////////////////////////// 702 // giet_fat_open() 703 /////////////////////////////////////////////////////////////////////////////////// 704 // Open a file identified by a pathname, and contained in the system FAT. 705 // The read/write flags are not supported yet: no effect. 706 /////////////////////////////////////////////////////////////////////////////////// 621 /////////////////////////////////////////// 707 622 int giet_fat_open( const char* pathname, 708 623 unsigned int flags ) … … 713 628 0, 0 ); 714 629 } 630 631 //////////////////////////////////// 632 void giet_fat_read( unsigned int fd, 633 void* buffer, 634 unsigned int count, 635 unsigned int offset ) 636 { 637 if ( sys_call( SYSCALL_FAT_READ, 638 fd, 639 (unsigned int)buffer, 640 count, 641 offset ) != count ) giet_exit("in giet_fat_read()"); 642 } 643 644 ///////////////////////////////////// 645 void giet_fat_write( unsigned int fd, 646 void* buffer, 647 unsigned int count, 648 unsigned int offset ) 649 { 650 if ( sys_call( SYSCALL_FAT_WRITE, 651 fd, 652 (unsigned int)buffer, 653 count, 654 offset ) != count ) giet_exit("in giet_fat_write()"); 655 } 656 657 /* variant implementing the UNIX spec 715 658 /////////////////////////////////////////////////////////////////////////////////// 716 // giet_fat_read() 659 // This system call writes to a file identified by the "fd" file descriptor. 660 // - "buffer" is the source buffer virtual address (must be word aligned). 661 // - "count" is a number of bytes (must be multiple of 4). 662 // It uses the implicit "lseek" pointer in file descriptor. 717 663 /////////////////////////////////////////////////////////////////////////////////// 718 // Read "count" sectors from a file identified by "fd", skipping "offset" 719 // sectors in file, and writing into the user "buffer". 720 // The user buffer base address shoulb be 64 bytes aligned. 721 /////////////////////////////////////////////////////////////////////////////////// 722 // This system call specification should evolve to the UNIX specification: 723 // - count must be a number of bytes, with no alignment constraint on user buffer. 724 // - offset argument should be removed and replaced by an implicit "lseek" pointer 725 // stored in the file descriptor. 726 // This suppose to implement a sectors cache 727 /////////////////////////////////////////////////////////////////////////////////// 728 int giet_fat_read( unsigned int fd, 729 void* buffer, 730 unsigned int count, 731 unsigned int offset ) 732 { 733 return sys_call( SYSCALL_FAT_READ, 734 fd, 735 (unsigned int)buffer, 736 count, 737 offset ); 738 } 739 /////////////////////////////////////////////////////////////////////////////////// 740 // giet_fat_write() 741 /////////////////////////////////////////////////////////////////////////////////// 742 // Write "count" sectors from a file identified by "fd", skipping "offset" 743 // sectors in file, and reading from the user "buffer". 744 // The user buffer base address shoulb be 64 bytes aligned. 745 /////////////////////////////////////////////////////////////////////////////////// 746 // This system call specification should evolve to the UNIX specification: 747 // - count must be a number of bytes, with no alignment constraint on buffer 748 // - offset argument should be removed and replaced by an implicit "lseek" pointer 749 // stored in the file descriptor. 750 // This suppose to implement a sectors cache 751 /////////////////////////////////////////////////////////////////////////////////// 752 int giet_fat_write( unsigned int fd, 664 void giet_fat_write( unsigned int fd, 753 665 void* buffer, 754 unsigned int count, 755 unsigned int offset ) 666 unsigned int count ) // number of bytes 756 667 { 757 668 return sys_call( SYSCALL_FAT_WRITE, 758 669 fd, 759 670 (unsigned int)buffer, 760 count, 761 offset ); 762 } 763 /////////////////////////////////////////////////////////////////////////////////// 764 // giet_fat_lseek() 765 /////////////////////////////////////////////////////////////////////////////////// 766 // Change the lseek file pointer value for a file identified by "fd". 767 /////////////////////////////////////////////////////////////////////////////////// 768 int giet_fat_lseek( unsigned int fd, 671 count, 0 ); 672 } 673 */ 674 675 ///////////////////////////////////// 676 void giet_fat_lseek( unsigned int fd, 769 677 unsigned int offset, 770 unsigned int whence) 771 { 772 return sys_call( SYSCALL_FAT_LSEEK, 773 fd, 774 offset, 775 whence, 776 0 ); 777 } 778 779 /////////////////////////////////////////////////////////////////////////////////// 780 // giet_fat_fstat() 781 /////////////////////////////////////////////////////////////////////////////////// 782 // Return stats of a file identified by "fd". 783 // (Only the file_size in sectors for this moment) 784 /////////////////////////////////////////////////////////////////////////////////// 785 int giet_fat_fstat( unsigned int fd ) 786 { 787 return sys_call( SYSCALL_FAT_FSTAT, 788 fd, 789 0, 0, 0 ); 790 } 791 792 /////////////////////////////////////////////////////////////////////////////////// 793 // giet_fat_close() 794 /////////////////////////////////////////////////////////////////////////////////// 795 // Close a file identified by "fd". 796 /////////////////////////////////////////////////////////////////////////////////// 797 int giet_fat_close( unsigned int fd ) 798 { 799 return sys_call( SYSCALL_FAT_CLOSE, 800 fd, 801 0, 0, 0 ); 802 } 803 804 805 /////////////////////////////////////////////////////////////////////////////////// 806 ////////////////// Pseudo system calls (no syscall instruction) /////////////////// 807 /////////////////////////////////////////////////////////////////////////////////// 808 809 /////////////////////////////////////////////////////////////////////////////////// 810 // giet_rand() 811 // This function returns a pseudo-random value derived from the processor cycle 812 // count. This value is comprised between 0 & 65535. 813 /////////////////////////////////////////////////////////////////////////////////// 678 unsigned int whence ) 679 { 680 if ( sys_call( SYSCALL_FAT_LSEEK, 681 fd, 682 offset, 683 whence, 684 0 ) ) giet_exit("in giet_fat_lseek()"); 685 } 686 687 ////////////////////////////////////// 688 void giet_fat_fstat( unsigned int fd ) 689 { 690 if ( sys_call( SYSCALL_FAT_FSTAT, 691 fd, 692 0, 0, 0 ) ) giet_exit("in giet_fat_lseek()"); 693 } 694 695 ///////////////////////////////////// 696 void giet_fat_close( unsigned int fd ) 697 { 698 if ( sys_call( SYSCALL_FAT_CLOSE, 699 fd, 700 0, 0, 0 ) ) giet_exit("in giet_fat_close()"); 701 } 702 703 704 ////////////////////////////////////////////////////////////////////////////////// 705 ///////////////////// Miscellaneous system calls ///////////////////////////////// 706 ////////////////////////////////////////////////////////////////////////////////// 707 708 ///////////////// 709 int giet_procid() 710 { 711 return sys_call( SYSCALL_PROCID, 712 0, 0, 0, 0 ); 713 } 714 715 //////////////////// 716 int giet_proctime() 717 { 718 return sys_call( SYSCALL_PROCTIME, 719 0, 0, 0, 0 ); 720 } 721 722 /////////////////////// 723 int giet_proc_task_id() 724 { 725 return sys_call( SYSCALL_LOCAL_TASK_ID, 726 0, 0, 0, 0 ); 727 } 728 729 ///////////////////////// 730 int giet_global_task_id() 731 { 732 return sys_call( SYSCALL_GLOBAL_TASK_ID, 733 0, 0, 0, 0 ); 734 } 735 736 //////////////////// 737 int giet_thread_id() 738 { 739 return sys_call( SYSCALL_THREAD_ID, 740 0, 0, 0, 0 ); 741 } 742 743 /////////////// 814 744 int giet_rand() 815 745 { … … 822 752 } 823 753 } 754 755 ////////////////////////////// 756 void giet_exit( char* string ) 757 { 758 sys_call( SYSCALL_EXIT, 759 (unsigned int)string, 760 0, 0, 0 ); 761 } 762 763 ///////////////////////////////////////// 764 void giet_assert( unsigned int condition, 765 char* string ) 766 { 767 if ( condition == 0 ) giet_exit( string ); 768 } 769 770 //////////////////////////////////////////////////// 771 void giet_vobj_get_vbase( char* vspace_name, 772 char* vobj_name, 773 unsigned int* vobj_vaddr ) 774 { 775 if ( sys_call( SYSCALL_VOBJ_GET_VBASE, 776 (unsigned int) vspace_name, 777 (unsigned int) vobj_name, 778 (unsigned int) vobj_vaddr, 779 0 ) ) giet_exit("in giet_vobj_get_vbase()"); 780 } 781 782 /////////////////////////////////////////////// 783 void giet_proc_number( unsigned int cluster_id, 784 unsigned int* buffer ) 785 { 786 if ( sys_call( SYSCALL_PROC_NUMBER, 787 cluster_id, 788 (unsigned int) buffer, 789 0, 0) ) giet_exit("in giet_proc_number()"); 790 } 791 792 ////////////////////////// 793 void giet_context_switch() 794 { 795 sys_call( SYSCALL_CTX_SWITCH, 796 0, 0, 0, 0 ); 797 } 798 799 ///////////////////////////////////////// 800 void giet_heap_info( unsigned int* vaddr, 801 unsigned int* length ) 802 { 803 if ( sys_call( SYSCALL_HEAP_INFO, 804 (unsigned int)vaddr, 805 (unsigned int)length, 806 0, 0 ) ) giet_exit("in giet_heap_info()"); 807 } 808 824 809 825 810 // Local Variables:
Note: See TracChangeset
for help on using the changeset viewer.