How to add serial driver to RTEMS?

Jay Monkman jtm-list-rtems at smoothsmoothie.com
Thu Sep 2 04:46:06 UTC 2004


On Wed, Sep 01, 2004 at 07:14:27PM +0800, Yanjun Luo wrote:
> my code:
> -----------------------------------------------------------------
> #define TEST_STRING	"This is only a USART test!\n"
> 
>   fd0 = fopen ("/dev/console", O_RDWR);
>   if(fd0 == -1) printf("/dev/console open failed!!\n");       
>   else
>   	printf("console opened!\n");
>   fwrite(TEST_STRING,1,sizeof(TEST_STRING),fd0);
> -----------------------------------------------------------------  

This is wrong. The second argument to fopen() is a string, like "r+"
or "a". You are using the argument for open(), which takes a number.

fopen() returns a FILE*, and open() returns an int. On failure, fopen
returns a NULL, so your test (fd0 == -1) is wrong.

Your call to open() probably failed because you didn't increase the
maximum number of file descriptors. The default is 3, just enough for
stdin, stdout, and stderr. You set the value by defining
CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS before #include-ing
confdefs.h.

Here's my test program:

#include <bsp.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>


rtems_task Init(
  rtems_task_argument ignored
)
{
    int fd;
    extern int errno;
    FILE *fp;
    char string[] = "   testing ... 1 ... 2 ... 3\n";
    
    printf("Starting test\n");

    printf("Trying open()\n");
    fd = open("/dev/console", O_RDWR);
    if (fd < 1) {
        printf("open() returned an error (%d). errno = %d\n",
               fd, errno);
    } else {
        printf("Going to write() to /dev/console\n");
        write(fd, string, strlen(string));
        close(fd);
    }
    printf("open() done\n");

    printf("Trying fopen()\n");
    fp = fopen("/dev/console", "r+");
    if (fp == NULL) {
        printf("fopen() returned NULL. errno = %d\n", errno);
    } else {
        printf("Going to fwrite() to /dev/console\n");
        fwrite(string, strlen(string), 1, fp);
        fclose(fp);
    }
    printf("fopen() done\n");


  
    exit( 0 ); 


}

/* configuration information */

#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 10

#define CONFIGURE_TEST_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_MAXIMUM_TASKS 1

#define CONFIGURE_INIT

#include <confdefs.h>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.rtems.org/pipermail/users/attachments/20040901/2bb8937e/attachment-0001.bin>


More information about the users mailing list