/
hello-world.c
75 lines (62 loc) · 1.44 KB
/
hello-world.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
#include <stdio.h>
#include <errno.h>
#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/stm32/f1/gpio.h>
#include <libopencm3/stm32/usart.h>
/* Set up all the peripherals */
void setup(void)
{
rcc_clock_setup_in_hsi_out_48mhz();
rcc_peripheral_enable_clock(&RCC_APB2ENR,
RCC_APB2ENR_IOPAEN |
RCC_APB2ENR_IOPBEN |
RCC_APB2ENR_AFIOEN |
RCC_APB2ENR_USART1EN);
/* GPIO pin for the LED */
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
/* GPIO pin for USART TX */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
/* Setup USART parameters. */
usart_set_baudrate(USART1, 19200);
usart_set_databits(USART1, 8);
usart_set_stopbits(USART1, USART_STOPBITS_1);
usart_set_parity(USART1, USART_PARITY_NONE);
usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
usart_set_mode(USART1, USART_MODE_TX);
/* Finally enable the USART. */
usart_enable(USART1);
}
/* Provide _write syscall used by libc */
int _write(int file, char *ptr, int len)
{
int i;
if (file == 1) {
for (i = 0; i < len; i++) {
usart_send_blocking(USART1, ptr[i]);
}
return i;
} else {
errno = EIO;
return -1;
}
}
/* Delay execution for some arbitrary amount of time */
void delay(void)
{
int i;
for (i = 0; i < 8000000; i++) {
__asm__("nop");
}
}
int main(void)
{
setup();
while (1) {
gpio_toggle(GPIOB, GPIO2);
printf("Hello, world!\n");
delay();
}
return 0;
}