forked from embedded2013/freertos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
134 lines (109 loc) · 3.32 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
#define USE_STDPERIPH_DRIVER
#include "stm32f10x.h"
#include "stm32_p103.h"
#include "romfs.h"
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* Filesystem includes */
#include "filesystem.h"
#include "fio.h"
#include "string.h"
#include "shell.h"
extern const uint8_t _sromfs;
volatile xSemaphoreHandle serial_tx_wait_sem = NULL;
volatile xQueueHandle serial_rx_queue = NULL;
/* Queue structure used for passing characters. */
typedef struct {
char ch;
} serial_ch_msg;
/* IRQ handler to handle USART2 interruptss (both transmit and receive
* interrupts). */
void USART2_IRQHandler()
{
static signed portBASE_TYPE xHigherPriorityTaskWoken;
serial_ch_msg rx_msg;
/* 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 byte from the buffer. */
rx_msg.ch = USART_ReceiveData(USART2);
/* Queue the received byte. */
if(!xQueueSendToBackFromISR(serial_rx_queue, &rx_msg, &xHigherPriorityTaskWoken)) {
/* If there was an error queueing the received byte,
* 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 receive_byte()
{
serial_ch_msg msg;
/* Wait for a byte to be queued by the receive interrupts handler. */
while (!xQueueReceive(serial_rx_queue, &msg, portMAX_DELAY));
return msg.ch;
}
int receive_byte_noblock(char *ch)
{
serial_ch_msg msg;
int rval = xQueueReceive(serial_rx_queue, &msg, 10);
if ( rval == pdTRUE) {
*ch = msg.ch;
}
return rval;
}
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. */
vSemaphoreCreateBinary(serial_tx_wait_sem);
serial_rx_queue = xQueueCreate(1, sizeof(serial_ch_msg));
register_romfs("romfs", &_sromfs);
/* Create shell task */
xTaskCreate(shell_task,
(signed portCHAR *) "Shell",
512 /* stack size */, NULL, tskIDLE_PRIORITY + 2, NULL);
/* Start running the tasks. */
vTaskStartScheduler();
return 0;
}
void vApplicationTickHook()
{
}