[1] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : boot_tty_driver.c |
---|
| 3 | // Date : 18/01/2017 |
---|
| 4 | // Author : Alain Greiner / Vu Son |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 7 | |
---|
| 8 | #include <boot_config.h> |
---|
| 9 | #include <boot_tty_driver.h> |
---|
| 10 | #include <hal_types.h> |
---|
| 11 | #include <boot_utils.h> |
---|
| 12 | |
---|
| 13 | #ifndef SEG_TTY_BASE |
---|
| 14 | # error "The SEG_TTY_BASE value should be defined in the 'boot_config.h' file" |
---|
| 15 | #endif |
---|
| 16 | |
---|
| 17 | #ifndef IO_CXY |
---|
| 18 | # error "The IO_CXY value should be defined in the 'boot_config.h' file" |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | /**************************************************************************** |
---|
| 23 | * Internal functions. * |
---|
| 24 | ****************************************************************************/ |
---|
| 25 | |
---|
| 26 | /**************************************************************************** |
---|
| 27 | * This function returns the value of the a TTY register. * |
---|
| 28 | * @ reg : TTY register to be read. * |
---|
| 29 | * @ returns the value stored in 'reg'. * |
---|
| 30 | ****************************************************************************/ |
---|
| 31 | static uint32_t boot_tty_get_register( uint32_t reg ) |
---|
| 32 | { |
---|
| 33 | cxy_t cxy = IO_CXY; |
---|
| 34 | uint32_t * ptr = (uint32_t *)SEG_TTY_BASE + reg; |
---|
| 35 | |
---|
| 36 | return boot_remote_lw( XPTR( cxy , ptr ) ); |
---|
| 37 | |
---|
| 38 | } // boot_tty_get_register() |
---|
| 39 | |
---|
| 40 | /**************************************************************************** |
---|
| 41 | * This function sets a new value to a TTY register. * |
---|
| 42 | * @ reg : TTY register to be configured. * |
---|
| 43 | * @ val : new value to be written to 'reg'. * |
---|
| 44 | ****************************************************************************/ |
---|
| 45 | static void boot_tty_set_register( uint32_t reg, |
---|
| 46 | uint32_t val ) |
---|
| 47 | { |
---|
| 48 | cxy_t cxy = IO_CXY; |
---|
| 49 | uint32_t * ptr = (uint32_t *)SEG_TTY_BASE + reg; |
---|
| 50 | |
---|
| 51 | boot_remote_sw( XPTR( cxy , ptr ) , val ); |
---|
| 52 | |
---|
| 53 | } // boot_tty_set_register() |
---|
| 54 | |
---|
| 55 | /**************************************************************************** |
---|
| 56 | * Driver API functions. * |
---|
| 57 | ****************************************************************************/ |
---|
| 58 | |
---|
| 59 | int boot_tty_write( char * buf, |
---|
| 60 | uint32_t nbytes ) |
---|
| 61 | { |
---|
| 62 | uint32_t nb_printed; /* Iterator for printing loop. */ |
---|
| 63 | uint32_t nb_test; /* Iterator for retry loop. */ |
---|
| 64 | uint32_t error; /* Used to detect if an error occurs. */ |
---|
| 65 | |
---|
| 66 | /* Printing to the boot TTY terminal. */ |
---|
| 67 | for (nb_printed = 0; nb_printed < nbytes; nb_printed++) |
---|
| 68 | { |
---|
| 69 | // Polling the TTY driver status. |
---|
| 70 | if ((boot_tty_get_register(TTY_STATUS) & TTY_WRITE_BUSY)) |
---|
| 71 | { |
---|
| 72 | // TTY_WRITE_BUSY bit of TTY_STATUS register is set, keeps polling. |
---|
| 73 | error = 1; |
---|
| 74 | for (nb_test = 0; nb_test < 10000; nb_test++) |
---|
| 75 | if ((boot_tty_get_register(TTY_STATUS) & TTY_WRITE_BUSY) == 0) |
---|
| 76 | { |
---|
| 77 | error = 0; |
---|
| 78 | break; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // Reporting an error if the TTY_WRITE_BUSY bit is still set after |
---|
| 82 | // 10000 retries. |
---|
| 83 | if (error) |
---|
| 84 | return -1; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | // Writing a character to the boot TTY terminal. |
---|
| 88 | // Special treatment for a newline: Carriage Return before Line Feed. |
---|
| 89 | if (buf[nb_printed] == '\n') |
---|
| 90 | boot_tty_set_register(TTY_WRITE, (uint32_t)'\r'); |
---|
| 91 | boot_tty_set_register(TTY_WRITE, (uint32_t)buf[nb_printed]); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return 0; |
---|
| 95 | |
---|
| 96 | } // boot_tty_write() |
---|