-
Notifications
You must be signed in to change notification settings - Fork 243
/
os.c
98 lines (78 loc) · 1.77 KB
/
os.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
#include <stddef.h>
#include <stdint.h>
#include "reg.h"
#include "threads.h"
/* USART TXE Flag
* This flag is cleared when data is written to USARTx_DR and
* set when that data is transferred to the TDR
*/
#define USART_FLAG_TXE ((uint16_t) 0x0080)
void usart_init(void)
{
*(RCC_APB2ENR) |= (uint32_t) (0x00000001 | 0x00000004);
*(RCC_APB1ENR) |= (uint32_t) (0x00020000);
/* USART2 Configuration, Rx->PA3, Tx->PA2 */
*(GPIOA_CRL) = 0x00004B00;
*(GPIOA_CRH) = 0x44444444;
*(GPIOA_ODR) = 0x00000000;
*(GPIOA_BSRR) = 0x00000000;
*(GPIOA_BRR) = 0x00000000;
*(USART2_CR1) = 0x0000000C;
*(USART2_CR2) = 0x00000000;
*(USART2_CR3) = 0x00000000;
*(USART2_CR1) |= 0x2000;
}
void print_str(const char *str)
{
while (*str) {
while (!(*(USART2_SR) & USART_FLAG_TXE));
*(USART2_DR) = (*str & 0xFF);
str++;
}
}
static void delay(volatile int count)
{
count *= 50000;
while (count--);
}
static void busy_loop(void *str)
{
while (1) {
print_str(str);
print_str(": Running...\n");
delay(1000);
}
}
void test1(void *userdata)
{
busy_loop(userdata);
}
void test2(void *userdata)
{
busy_loop(userdata);
}
void test3(void *userdata)
{
busy_loop(userdata);
}
/* 72MHz */
#define CPU_CLOCK_HZ 72000000
/* 100 ms per tick. */
#define TICK_RATE_HZ 10
int main(void)
{
const char *str1 = "Task1", *str2 = "Task2", *str3 = "Task3";
usart_init();
if (thread_create(test1, (void *) str1) == -1)
print_str("Thread 1 creation failed\r\n");
if (thread_create(test2, (void *) str2) == -1)
print_str("Thread 2 creation failed\r\n");
if (thread_create(test3, (void *) str3) == -1)
print_str("Thread 3 creation failed\r\n");
/* SysTick configuration */
*SYSTICK_LOAD = (CPU_CLOCK_HZ / TICK_RATE_HZ) - 1UL;
*SYSTICK_VAL = 0;
*SYSTICK_CTRL = 0x07;
thread_start();
return 0;
}