summaryrefslogtreecommitdiffstats
path: root/org.rtems.cdt.toolchain/templates/serial/src/Basename.c
blob: def460abff98b6fad65323136cbd71e14954055b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 *  File Name   : $(baseName).c
 *  Author      : $(author)
 *  Version     :
 *  Description : A simple serial and shell test program if defined TEST_COM1;
 *                A serial remote debug test program otherwise
 *  Copyright   : $(copyright)
 */

#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>

// BSP specific include
#include <bsp.h>
#include <bsp/uart.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>

extern int BSPConsolePort;

/* Temporarily worked around the issue with remote_debug in i386.
   "int     remote_debug;" is defined in m68k-stub.c, but only declared
   in i386-stub.c. Also except "remote_debug = !(remote_debug);", nowhere else
   value is assigned to remote_debug.
 */
int remote_debug = 1;

/*
 * Setup GDB as described in c\src\lib\libbsp\i386\shared\comm\GDB.HOWTO.
 *
 * Later found similar function: init_remote_gdb in
 * c\src\lib\libbsp\i386\shared\comm\gdb_glue.c. But it does not fit with the
 * following hardware configuration:
 *       there exist VGA console and COM1, but not COM2.
 */
void setupRemoteGDB(void) {
   // Initialize GDB glue
   /*
   if(BSPConsolePort != BSP_UART_COM2) {
        // If com2 is not used as console use it for debugging
       i386_stub_glue_init(BSP_UART_COM2);
   } else { // Otherwise use com1  */
       i386_stub_glue_init(BSP_UART_COM1);
   //}

   // Initialize GDB stub itself
   set_debug_traps();

   // Initialize GDB break in capability
   // It has to be called after set_debug_traps
   i386_stub_glue_init_breakin();

   // Put breakpoint in
   breakpoint();
}

void testIO(char *devName) {
  char buffer[256];

  #ifdef TEST_COM1
    printf("*** Simple COM1 Test (9600 8N1) ***\n");
  #else
    printf("*** Simple Remote Debug Test ***\n");
  #endif

  int fd = open(devName, O_RDWR | O_NOCTTY | _FNDELAY);

  int numBytes = write(fd, "Hello, I'm waiting for input...\r\n", 33);
  if (numBytes < 0) {
    printf("\nFailed to write to %s!\n", devName);
  }

  numBytes = read(fd, buffer, 255);
  if (numBytes < 0) {
    printf("\nFailed to read from %s!\n", devName);

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

  close(fd);
 }

void startShell(void) {
  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 */
  );
}

rtems_task Init(rtems_task_argument ignored) {
#ifdef TEST_COM1
  char devName[] = "/dev/ttyS1";
#else
  char devName[] = "/dev/console";

  setupRemoteGDB();
#endif

  testIO(devName);

  startShell();

  exit( 0 );
}