[ECOS] serial output problem:cyg_io_write and printf

Andrew Lunn andrew@lunn.ch
Wed Mar 9 13:24:00 GMT 2005


On Wed, Mar 09, 2005 at 05:06:22PM +0800, liu hua wrote:
> When I use cyg_io_write() write serial(/dev/tty0) in the main() function,it 
> is ok. But when I use cyg_io_write() in a thread created in main(),the 
> serial port have no output message.
> 
> The simple program is:
> 	int main(void)
> 	{
> 	    cyg_io_handle_t handle;
> 	    Cyg_ErrNo err;
> 	    char read_string[50];
> 	    const char test_string[] = "serial example is working 
> 	    correctly!\n";
> 	    cyg_uint32 len = strlen(test_string);
> 	
> 	    printf("Starting serial example\n");
> 	    err = cyg_io_lookup( "/dev/tty0", &handle );
> 	    if (ENOERR == err) {
> 	        printf("Found /dev/tty0. Writing string....\n");
> 	        err = cyg_io_write( handle, test_string, &len );
> 	    }    
> 	    if (ENOERR == err) {
> 	        printf("I think I wrote the string. Did you see it?\n");
> 	      
> 	    }
> 	}
> the output message is:
>           Starting serial example
>           Found /dev/tty0. Writing string....
>           I think I wrote the string. Did you see it?
>           serial example is working correctly!
> 
> But,if I use follow program:
> 	static void simple_prog(CYG_ADDRESS data)
> 	{
> 	    cyg_io_handle_t handle;
> 	    Cyg_ErrNo err;
> 	    char read_string[50];
> 	    const char test_string[] = "serial example is working 
> 	    correctly!\n";
> 	    cyg_uint32 len = strlen(test_string);
> 	
> 	    printf("Starting serial example\n");
> 	    err = cyg_io_lookup( "/dev/tty0", &handle );
> 	    if (ENOERR == err) {
> 	        printf("Found /dev/tty0. Writing string....\n");
> 	        err = cyg_io_write( handle, test_string, &len );
> 	    }    
> 	    if (ENOERR == err) {
> 	        printf("I think I wrote the string. Did you see it?\n");
> 	      
> 	    }
> 	}
> 	int main(void)
> 	{
> 	    cyg_thread_create(4, simple_prog, (cyg_addrword_t) 0, "serial",
> 	                      (void *)stack[0], STACKSIZE, &thread[0], 
> &thread_obj[0]);
> 	    cyg_thread_resume(thread[0]);
> 	}
> the output messages is:
>          Starting serial example
>          Found /dev/tty0. Writing string....
>          I think I wrote the string. Did you see it?
> There is no cyg_io_write() output message "serial example is working 
> correctly!" .
> 
> In this program,I have two problem:
>  1) In a thread created in main(),why cyg_io_write() cannt write serial 
> port?
>  2) In main(),the cyg_io_write() can write serial port,but the output 
> message is in the last line (after all printf()).Why?

printf() will be using the diag serial driver. This is designed to
directly write its output to the serial hardware using polled IO. 

By openning /dev/tty0 you are using the interrupt driven serial driver
and i think buffered IO. Im guessing that when main ends the serial
drivers buffers are getting flushed and so the buffered output is then
being sent. When you are using a thread this flushing is not
happening.

The is cyg_io_get_config you can call to force the buffer to be
flushed. See

http://ecos.sourceware.org/docs-latest/ref/io-serial-driver-details.html

        Andrew. 

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss



More information about the Ecos-discuss mailing list