summaryrefslogtreecommitdiff
path: root/prototype/fw/src/main.c
blob: de920eb704e6f7b807314502ded7447e9bfce594 (plain)
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
#include <global.h>

int main(void) {

    RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
    RCC->APB2ENR |= RCC_APB2ENR_USART1EN;

    GPIOA->MODER |= (2 << GPIO_MODER_MODER9_Pos);
    GPIOA->AFR[1] = (7 << (9-8)*4);

    SystemCoreClockUpdate();
    int apb2_clock = SystemCoreClock / APB2_PRESC;
    
    int baudrate = 115200;

    USART1->CR1 = USART_CR1_MME | USART_CR1_TE;
    USART1->BRR = (apb2_clock + baudrate/2) / baudrate;
    USART1->CR1 |= USART_CR1_UE;

    char s[12] = {
        '0', '0', '0', '0', '0',
        ' ', 'T', 'E', 'S', 'T',
        '\n', '\0'};
    int line = 0;
    char *c = s;
    USART1->TDR = *(c++); /* Kick off transmission */
    while (23) {
        if (USART1->ISR & USART_ISR_TXE) {
            for (int i=0; i<100; i++)
                asm volatile ("nop");
            USART1->TDR = *(c++);
            if (!*c) {
                c = s;
                line++;
                int tmp = line;
                tmp %= 100000;
                s[0] = '0' + tmp/10000;
                tmp %= 10000;
                s[1] = '0' + tmp/1000;
                tmp %= 1000;
                s[2] = '0' + tmp/100;
                tmp %= 100;
                s[3] = '0' + tmp/10;
                tmp %= 10;
                s[4] = '0' + tmp;
            }
        }
    }
}

void __libc_init_array (void) __attribute__((weak));
void __libc_init_array ()
{
}