wiki:TBR/UserManual/Simple_Serial_Test_Program

Version 2 (modified by Robert, on 11/19/08 at 13:10:00) (diff)

Simple Serial Test Program

/*
 *  File name: init.c
 *
 *  A simple serial and shell test program.
 *
 *  COPYRIGHT (c) 2008.
 *  RobertF
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 */

#define CONFIGURE_INIT

#include <stdio.h>
#include <stdlib.h>

#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

#include <rtems.h>
#include <rtems/shell.h>
#include <bsp.h>
#include <bsp/tty_drv.h>


extern rtems_task Init(rtems_task_argument argument);

#define CONFIGURE_APPLICATION_EXTRA_DRIVERS  TTY1_DRIVER_TABLE_ENTRY

#ifdef RTEMS_BSP_HAS_IDE_DRIVER
#include <libchip/ata.h> /* for ata driver prototype */
#include <libchip/ide_ctrl.h> /* for general ide driver prototype */
#endif

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#ifdef RTEMS_BSP_HAS_IDE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_IDE_DRIVER
#endif
#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM

/*
 * these values are higher than needed...
 */
#define CONFIGURE_MAXIMUM_TASKS             20
#define CONFIGURE_MAXIMUM_SEMAPHORES        20
#define CONFIGURE_MAXIMUM_MESSAGE_QUEUES    20
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 20
#define STACK_CHECKER_ON
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_EXTRA_TASK_STACKS         (6 * RTEMS_MINIMUM_STACK_SIZE)

#define CONFIGURE_MALLOC_STATISTICS


#include <rtems/confdefs.h>


#define CONFIGURE_SHELL_COMMANDS_INIT
#define CONFIGURE_SHELL_COMMANDS_ALL

#include <rtems/shellconfig.h>


void testCom1(void) {
  char buffer[256];

  printf("*** Simple COM1 Test (9600 8N1) ***\n");

  int fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | _FNDELAY);

  printf("\nOpened COM1, fd=%d\n\n", fd);

  int numBytes = write(fd, "Hello, I'm waiting for input...\r\n", 33);

  if (numBytes < 0) {
    printf("\nFailed to send from COM1!\n");
  }

  numBytes = read(fd, buffer, 255);

  if (numBytes < 0) {
    printf("\nFailed to read from COM1!\n");

  } else {
    buffer[numBytes] = 0; // terminate
    printf(buffer);
  }

  close(fd);
}

rtems_task Init(rtems_task_argument ignored) {

  testCom1();

  printf("\n====== starting shell ======\n");
  rtems_shell_init(
    "SHLL",                          /* task_name */
    RTEMS_MINIMUM_STACK_SIZE * 4,    /* task_stacksize */
    100,                             /* task_priority */
    "/dev/console",                  /* devname */
    0,                               /* forever */
    1                                /* wait */
  );

  exit( 0 );
}