-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.c
131 lines (109 loc) · 3.15 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
#include "stm32mp1xx.h"
#include "stm32mp1xx_ll_gpio.h"
#include "stm32mp1xx_ll_usart.h"
#include <stdint.h>
void delay_long();
void soft_breakpoint();
void write(const char *str);
static inline void delay_for_uart(void);
static inline void init_leds();
const char *global_constant_string = "And hi to you too!\r\n";
void main()
{
// Test UART
UART4->TDR = 'X';
delay_for_uart();
UART4->TDR = 'Y';
delay_for_uart();
UART4->TDR = 'Z';
delay_for_uart();
UART4->TDR = '\r';
delay_for_uart();
UART4->TDR = '\n';
delay_for_uart();
// Test LEDs
init_leds();
LL_GPIO_SetOutputPin(GPIOI, LL_GPIO_PIN_8);
LL_GPIO_ResetOutputPin(GPIOI, LL_GPIO_PIN_8);
LL_GPIO_SetOutputPin(GPIOI, LL_GPIO_PIN_9);
LL_GPIO_ResetOutputPin(GPIOI, LL_GPIO_PIN_9);
LL_GPIO_SetOutputPin(GPIOZ, LL_GPIO_PIN_6);
LL_GPIO_ResetOutputPin(GPIOZ, LL_GPIO_PIN_6);
LL_GPIO_SetOutputPin(GPIOZ, LL_GPIO_PIN_7);
LL_GPIO_ResetOutputPin(GPIOZ, LL_GPIO_PIN_7);
// Test function calls (tests the stack)
// So far, every function call has been inline
const char *s = "Hello world from bare-metal!\r\n";
write(s);
// Test global static memory
while (*global_constant_string != '\0') {
UART4->TDR = *global_constant_string;
global_constant_string++;
delay_for_uart();
}
// Blink those lights forever!
while (1) {
LL_GPIO_SetOutputPin(GPIOI, LL_GPIO_PIN_8);
delay_long();
LL_GPIO_ResetOutputPin(GPIOI, LL_GPIO_PIN_8);
LL_GPIO_SetOutputPin(GPIOI, LL_GPIO_PIN_9);
delay_long();
LL_GPIO_ResetOutputPin(GPIOI, LL_GPIO_PIN_9);
LL_GPIO_SetOutputPin(GPIOZ, LL_GPIO_PIN_6);
delay_long();
LL_GPIO_ResetOutputPin(GPIOZ, LL_GPIO_PIN_6);
LL_GPIO_SetOutputPin(GPIOZ, LL_GPIO_PIN_7);
delay_long();
LL_GPIO_ResetOutputPin(GPIOZ, LL_GPIO_PIN_7);
};
}
void delay_long()
{
uint32_t i = 0x1000000;
while (i--)
;
}
void write(const char *str)
{
while (*str) {
UART4->TDR = *str++;
delay_for_uart();
}
}
// Inline because we don't want to assume function calling and returning works yet!
static inline void delay_for_uart(void)
{
while ((UART4->ISR & USART_ISR_TXFT) == 0)
;
}
// Inline because we don't want to assume function calling and returning works yet!
static inline void init_leds()
{
// OSD32MP1 board:
// GPIO I, pin 8 = red D2
// GPIO I, pin 9 = green D2
// GPIO Z, pin 6 = red D1
// GPIO Z, pin 7 = green D1
// all LEDs are active low
// Enable RCC for GPIO I and GPIO Z (for the LEDs on the OSD32 board)
RCC->MC_AHB4ENSETR |= RCC_MC_AHB4ENSETR_GPIOIEN;
RCC->MC_AHB5ENSETR |= RCC_MC_AHB5ENSETR_GPIOZEN;
LL_GPIO_SetPinMode(GPIOI, LL_GPIO_PIN_8, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinOutputType(GPIOI, LL_GPIO_PIN_8, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinSpeed(GPIOI, LL_GPIO_PIN_8, LL_GPIO_SPEED_FREQ_MEDIUM);
LL_GPIO_SetPinMode(GPIOI, LL_GPIO_PIN_9, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinOutputType(GPIOI, LL_GPIO_PIN_9, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinSpeed(GPIOI, LL_GPIO_PIN_9, LL_GPIO_SPEED_FREQ_MEDIUM);
}
// Handy utility when using a debugger
void soft_breakpoint()
{
volatile int stop = 1;
while (stop) {
// Attach a debugger and manually change the value at the address of `stop` in RAM from 1 to 0
}
}
void IRQ_Initialize(void)
{
// do nothing, just a stub
}