forked from embedded2013/freertos
/
main.c
143 lines (117 loc) · 3.1 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
#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 Processing */
#include "string.h"
#include "util.h"
/* Shell commands */
#include "shell.h"
/* Debug : Unit Testing */
#ifdef UNIT_TEST
#include "unit_test.h"
#endif
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;
/* 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 {
/* Only transmit and receive interrupts should be enabled.
* If this is another type of interrupt, freeze.
*/
while(1);
}
if (xHigherPriorityTaskWoken) {
taskYIELD();
}
}
typedef struct {
char ch;
} serial_ch_msg;
void receive_byte(char ch)
{
serial_ch_msg msg;
while (!xQueueReceive(serial_rx_queue, &msg, portMAX_DELAY));
return msg.ch;
}
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);
}
void read_romfs_task(void *pvParameters)
{
char buf[128];
size_t count;
int fd = fs_open("/romfs/test.txt", 0, O_RDONLY);
do {
//Read from /romfs/test.txt to buffer
count = fio_read(fd, buf, sizeof(buf));
//Write buffer to fd 1 (stdout, through uart)
fio_write(1, buf, count);
} while (count);
while (1);
}
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);
serial_rx_queue = xQueueCreate(1, sizeof(serial_ch_msg));
#ifdef UNIT_TEST
xTaskCreate(unit_test_task,
(signed portCHAR *) "Unit Test",
256, // stack size
NULL,
tskIDLE_PRIORITY + 2,
NULL);
#endif
xTaskCreate(shell_task,
(signed portCHAR*) "Shell",
512, /* Stack Size*/
NULL,
tskIDLE_PRIORITY + 1,
NULL);
/* Start running the tasks. */
vTaskStartScheduler();
return 0;
}
void vApplicationTickHook()
{
}