summaryrefslogtreecommitdiff
path: root/firmware/os/platform/stm32/usart.c
blob: 302cb8527528c0adb7ade37edb8f35a3fcf2c730 (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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <plat/gpio.h>
#include <plat/usart.h>
#include <plat/pwr.h>
#include <usart.h>
#include <gpio.h>

struct StmUsart {
  volatile uint16_t SR;
  uint8_t unused0[2];
  volatile uint16_t DR;
  uint8_t unused1[2];
  volatile uint16_t BRR;
  uint8_t unused2[2];
  volatile uint16_t CR1;
  uint8_t unused3[2];
  volatile uint16_t CR2;
  uint8_t unused4[2];
  volatile uint16_t CR3;
  uint8_t unused5[2];
  volatile uint16_t GTPR;
  uint8_t unused6[2];
};

static const uint32_t mUsartPorts[] = {
    USART1_BASE,
    USART2_BASE,
    USART3_BASE,
    UART4_BASE,
    UART5_BASE,
    USART6_BASE,
};

static const uint32_t mUsartPeriphs[] = {
    PERIPH_APB2_USART1,
    PERIPH_APB1_USART2,
    PERIPH_APB1_USART3,
    PERIPH_APB1_UART4,
    PERIPH_APB1_UART5,
    PERIPH_APB2_USART6,
};

static uint8_t mUsartBusses[] = {
    PERIPH_BUS_APB2,
    PERIPH_BUS_APB1,
    PERIPH_BUS_APB1,
    PERIPH_BUS_APB1,
    PERIPH_BUS_APB1,
    PERIPH_BUS_APB2,
};

static bool mUsartHasFlowControl[] = {
    true,
    true,
    true,
    false,
    false,
    true,
};

static enum StmGpioAltFunc mUsartAlt[] = {
    GPIO_AF_USART1,
    GPIO_AF_USART2,
    GPIO_AF00,
    GPIO_AF00,
    GPIO_AF00,
    GPIO_AF_USART6,
};

void usartOpen(struct usart* __restrict usart, UsartPort port,
                uint32_t txGpioNum, uint32_t rxGpioNum,
                uint32_t baud, UsartDataBitsCfg data_bits,
                UsatStopBitsCfg stop_bits, UsartParityCfg parity,
                UsartFlowControlCfg flow_control)
{
    static const uint16_t stopBitsVals[] = {0x1000, 0x0000, 0x3000, 0x2000}; // indexed by UsatStopBitsCfg
    static const uint16_t wordLengthVals[] = {0x0000, 0x1000}; // indexed by UsartDataBitsCfg
    static const uint16_t parityVals[] = {0x0000, 0x0400, 0x0600}; // indexed by UsartParityCfg
    static const uint16_t flowCtrlVals[] = {0x0000, 0x0100, 0x0200, 0x0300}; // indexed by UsartFlowControlCfg
    struct StmUsart *block = (struct StmUsart*)mUsartPorts[usart->unit = --port];
    uint32_t baseClk, div, intPart, fraPart;

    /* configure tx/rx gpios */

    usart->rx = gpioRequest(rxGpioNum); /* rx */
    gpioConfigAlt(usart->rx, GPIO_SPEED_LOW, GPIO_PULL_UP, GPIO_OUT_PUSH_PULL, mUsartAlt[port]);
    usart->tx = gpioRequest(txGpioNum); /* tx */
    gpioConfigAlt(usart->tx, GPIO_SPEED_LOW, GPIO_PULL_UP, GPIO_OUT_PUSH_PULL, mUsartAlt[port]);

    /* enable clock */
    pwrUnitClock(mUsartBusses[port], mUsartPeriphs[port], true);

    /* sanity checks */
    if (!mUsartHasFlowControl[port])
        flow_control = USART_FLOW_CONTROL_NONE;

    /* basic config as required + oversample by 8, tx+rx on */
    block->CR2 = (block->CR2 &~ 0x3000) | stopBitsVals[stop_bits];
    block->CR1 = (block->CR1 &~ 0x1600) | wordLengthVals[data_bits] | parityVals[parity] | 0x800C;
    block->CR3 = (block->CR3 &~ 0x0300) | flowCtrlVals[flow_control];

    /* clocking calc */
    baseClk = pwrGetBusSpeed(mUsartBusses[port]);
    div = (baseClk * 25) / (baud * 2);
    intPart = div / 100;
    fraPart = div % 100;

    /* clocking munging */
    intPart = intPart << 4;
    fraPart = ((fraPart * 8 + 50) / 100) & 7;
    block->BRR = intPart | fraPart;

    /* enable */
    block->CR1 |= 0x2000;
}

void usartClose(const struct usart* __restrict usart)
{
    struct StmUsart *block = (struct StmUsart*)mUsartPorts[usart->unit];

    /* Disable USART */
    block->CR1 &=~ 0x2000;

    /* Disable USART clock */
    pwrUnitClock(mUsartBusses[usart->unit], mUsartPeriphs[usart->unit], false);

    /* Release gpios */
    gpioRelease(usart->rx);
    gpioRelease(usart->tx);
}

void usartPutchar(const struct usart* __restrict usart, char c)
{
    struct StmUsart *block = (struct StmUsart*)mUsartPorts[usart->unit];

    /* wait for ready */
    while (!(block->SR & 0x0080));

    /* send */
    block->DR = (uint8_t)c;
}