forked from embedded2014/freertos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
156 lines (132 loc) · 3.87 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
#define USE_STDPERIPH_DRIVER
#include "stm32f10x.h"
#include "stm32_p103.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"
#include "romfs.h"
#include "clib.h"
#include "shell.h"
/* _sromfs symbol can be found in main.ld linker script
* it contains file system structure of test_romfs directory
*/
extern const unsigned char _sromfs;
static void setup_hardware();
volatile xSemaphoreHandle serial_tx_wait_sem = NULL;
/* Add for serial input */
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;
/* 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){
char msg = USART_ReceiveData(USART2);
/* If there is an error when queueing the received byte, freeze! */
if(!xQueueSendToBackFromISR(serial_rx_queue, &msg, &xHigherPriorityTaskWoken))
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 recv_byte()
{
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
char msg;
while(!xQueueReceive(serial_rx_queue, &msg, portMAX_DELAY));
return msg;
}
void command_prompt(void *pvParameters)
{
char buf[128];
char *argv[20];
fio_printf(1, "\rWelcome to FreeRTOS Shell\r\n");
while(1){
fio_printf(1, "\r>>");
fio_read(0, buf, 127);
int n=parse_command(buf, argv);
/* will return pointer to the command function */
cmdfunc *fptr=do_command(argv[0]);
if(fptr!=NULL)
fptr(n, argv);
else
fio_printf(2, "\r\n\"%s\" command not found.\r\n", argv[0]);
}
}
void semihost_sysinfo(void *pvParameters)
{
int rnt = host_system("echo \'Write system info to \"./sysinfo\" periodically.\'");
char buf[1024];
while(1) {
/* Clear buf */
strcpy (buf, "echo \'");
/* Retrieve task list */
vTaskList((signed char *)&buf[strlen(buf)], "\0");
strcat (&buf[ strlen(buf) ], (const char*) "\' > ./sysinfo");
rnt = host_system(buf);
vTaskDelay(200);
}
}
int main()
{
init_rs232();
enable_rs232_interrupts();
enable_rs232();
fs_init();
fio_init();
register_romfs("romfs", &_sromfs);
/* Create the queue used by the serial task. Messages for write to
* the RS232. */
vSemaphoreCreateBinary(serial_tx_wait_sem);
/* Add for serial input
* Reference: www.freertos.org/a00116.html */
serial_rx_queue = xQueueCreate(1, sizeof(char));
/* Create a task to output text read from romfs. */
xTaskCreate(command_prompt,
(signed portCHAR *) "Command Prompt",
512 /* stack size */, NULL, configMAX_PRIORITIES, NULL);
xTaskCreate(semihost_sysinfo,
(signed portCHAR *) "Semihost Sysinfo",
512 /* stack size */, NULL, tskIDLE_PRIORITY + 2, NULL);
/* Start running the tasks. */
vTaskStartScheduler();
return 0;
}
void vApplicationTickHook()
{
}