summaryrefslogtreecommitdiffstatshomepage
path: root/stm/usart.c
blob: 307aff16621815a65e14a71e2e4b9c2d41fe51e7 (plain) (blame)
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
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_usart.h>

#include "misc.h"
#include "usart.h"

static bool is_enabled;

// USART6 on PC6 (TX), PC7 (RX)
void usart_init(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART6, &USART_InitStructure);

    USART_Cmd(USART6, ENABLE);

    is_enabled = true;
}

bool usart_is_enabled(void) {
    return is_enabled;
}

bool usart_rx_any(void) {
    return USART_GetFlagStatus(USART6, USART_FLAG_RXNE) == SET;
}

int usart_rx_char(void) {
    return USART_ReceiveData(USART6);
}

void usart_tx_char(int c) {
    if (is_enabled) {
        // wait until the end of any previous transmission
        uint32_t timeout = 100000;
        while (USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET && --timeout > 0) {
        }
        USART_SendData(USART6, c);
    }
}

void usart_tx_str(const char *str) {
    for (; *str; str++) {
        usart_tx_char(*str);
    }
}

void usart_tx_strn_cooked(const char *str, int len) {
    for (const char *top = str + len; str < top; str++) {
        if (*str == '\n') {
            usart_tx_char('\r');
        }
        usart_tx_char(*str);
    }
}