forked from embedded2013/freertos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
290 lines (244 loc) · 6.42 KB
/
main.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#define USE_STDPERIPH_DRIVER
#include "stm32f10x.h"
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include <string.h>
/* Filesystem includes */
#include "filesystem.h"
#include "fio.h"
/* String includes */
#include "string-util.h"
#include "semi_host.h"
extern const char _sromfs;
static void setup_hardware();
volatile xSemaphoreHandle serial_tx_wait_sem = NULL;
volatile xQueueHandle serial_rx_queue = NULL;
/* IRQ handler to handle USART2 interruptss (both transmit and receive
* interrupts). */
void USART2_IRQHandler()
{
static signed portBASE_TYPE xHigherPriorityTaskWoken;
char rx_msg; // Recieve the char from RX
/* If this interrupt is for a transmit... */
if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET) {
/* "give" the serial_tx_wait_sem semaphore to notfiy processes
* that the buffer has a spot free for the next byte.
*/
xSemaphoreGiveFromISR(serial_tx_wait_sem, &xHigherPriorityTaskWoken);
/* Diables the transmit interrupt. */
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
/* If this interrupt is for a receive... */
else if ( USART_GetITStatus( USART2, USART_IT_RXNE ) != RESET )
{
/* Receive the char from rx */
rx_msg = USART_ReceiveData( USART2 );
/* Queue the received char */
if ( !xQueueSendToBackFromISR( serial_rx_queue, &rx_msg, \
&xHigherPriorityTaskWoken ) )
{
/*
* If there was an error queueing the recieved char,
* freeze.
*/
while(1);
}
}
else {
/* Only transmit and receive interrupts should be enabled.
* If this is another type of interrupt, freeze.
*/
while(1);
}
if (xHigherPriorityTaskWoken) {
taskYIELD();
}
}
void send_byte(char ch)
{
/* Wait until the RS232 port can receive another byte (this semaphore
* is "given" by the RS232 port interrupt when the buffer has room for
* another byte.
*/
while (!xSemaphoreTake(serial_tx_wait_sem, portMAX_DELAY));
/* Send the byte and enable the transmit interrupt (it is disabled by
* the interrupt).
*/
USART_SendData(USART2, ch);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
}
char recieve_byte()
{
char msg;
/* Wait for a byte to be queued by the recieve interrupt handler. */
while( !xQueueReceive( serial_rx_queue, &msg, portMAX_DELAY ) );
return msg;
}
void print_to_console( const char *str )
{
/* Send the string to the console.
* fd = 1 for stdout. */
fio_write( 1, str, strlen( str ) );
}
void sPuts( const char *str )
{
/* Display the string with new line. */
print_to_console( str );
print_to_console( "\r\n" );
}
/*
* Show all tasks and their states that are
* running on the OS. */
void cmd_ps()
{
char listBuf[512] = {0};
sPuts( "Name\t State Priority Stack Num" );
vTaskList( listBuf );
print_to_console( listBuf );
}
/*
* The command would be passed to the host.
*/
void cmd_host( const char *cmd )
{
/* The command format is :"host <command>" */
const char *host_cmd = &cmd[5];
semihost_system( host_cmd, strlen( host_cmd ) );
}
/* Display the content of the file specified. */
void cmd_cat( const char *filename )
{
char buffer[129];
int fd = fs_open( filename, 0, O_RDONLY ), count;
if ( fd < 0 )
{
sPuts( "The file dose not exist!" );
return;
}
while( ( count = fio_read( fd, buffer, sizeof(buffer) - 1 ) ) > 0 )
{
buffer[ count ] = '\0';
print_to_console( buffer );
}
sPuts( "" );
fio_close( fd );
return;
}
/* Parse the command and return the command ID */
int getCommand( const char *cmd )
{
/* Hello: Print out the welcome message. */
if ( strncmp( cmd, "Hello", 5 ) == 0 )
sPuts( "Hi, Welcome to FreeRTOS!" );
/* ps: Show all the task that are on the OS. */
else if ( strncmp( cmd, "ps", 2 ) == 0 )
cmd_ps();
/* host <command>: The command passed to the host. */
else if ( strncmp( cmd, "host", 4 ) == 0 )
cmd_host( cmd );
/* cat <path/filename>: Display the content of the file. */
else if ( strncmp( cmd, "cat", 3 ) == 0 )
cmd_cat( &cmd[4] );
/* Command not found, show the message. */
else
sPuts( "Command Not found!" );
}
#define MAX_SERIAL_LEN 100
/* Function Key ASCII code macro */
#define ESC 27
#define BACKSPACE 127
void shellEnv()
{
char serial_buf[ MAX_SERIAL_LEN ], ch;
char prompt[] = "LanKuDot@FreeRTOS~$ ", newLine[] = "\n\r";
int curr_pos, done;
/* Infinite loop for running shell environmrnt. */
while (1)
{
/* Show prompt each time waiting for user input. */
print_to_console( prompt );
/* Initialize the relatived variable. */
curr_pos = 0;
done = 0;
do
{
/* Recieve a byte from the RS232 port ( this call will
* block ).
*/
ch = recieve_byte();
/* If the byte is an end-of-line character, than finish
* the string and indicate we are done.
*/
if ( curr_pos >= MAX_SERIAL_LEN-1 || (ch == '\n') || (ch == '\r') )
{
serial_buf[ curr_pos ] = '\0';
done = -1;
}
/* Backspace key pressed */
else if ( ch == BACKSPACE )
{
/* The char is not at the begin of the line. */
if ( curr_pos != 0 )
{
--curr_pos;
/* Cover the last character with space, and shift the
* cursor left. */
print_to_console( "\b \b" );
}
}
/* Function/Arrow Key pressed */
else if ( ch == ESC )
{
/* Arrow Key: ESC[A ~ ESC[D
* Function Key: ESC[1~ ~ ESC[6~ */
ch = recieve_byte();
if ( ch == '[' )
{
ch = recieve_byte();
if ( ch >= '1' && ch <= '6' )
{
/* Discard '~' */
recieve_byte();
}
continue;
}
}
/* Otherwise, add the character to the response string */
else
{
serial_buf[ curr_pos++ ] = ch;
/* Display the char that just typed */
// print_to_console( &ch ); // Bug: Press 1, print a lot...
fio_write( 1, &ch, 1 );
}
} while ( !done ); // end character recieving loop
/* Direct to the new line */
print_to_console( newLine );
/* Deal with the command! */
getCommand( serial_buf );
} // end infinite while loop
} // end of function shellEnv
int main()
{
init_rs232();
enable_rs232_interrupts();
enable_rs232();
fs_init();
fio_init();
/* Create the queue used by the serial task. Messages for write to
* the RS232. */
serial_rx_queue = xQueueCreate( 1, sizeof(char) );
vSemaphoreCreateBinary(serial_tx_wait_sem);
/* Create a task that run the sell environment. */
xTaskCreate( shellEnv, (signed portCHAR *)"Shell",
512, NULL, tskIDLE_PRIORITY + 5, NULL );
/* Start running the tasks. */
vTaskStartScheduler();
return 0;
}
void vApplicationTickHook()
{
}