blob: 2b5cb2d643bcd89533fccf9bb0ab3581cb185740 (
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
|
/**
* @file serial.c
* Provides basic serial IO (through STM debug stuff)
*
* Copyright (C) 2018 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <arch/stm/stm32l476xx.h>
#include "gpio.h"
#include "clock.h"
void serial_init(uint32_t baud)
{
gpio_mode(GPIOA, 2, ALTERNATE);
gpio_mode(GPIOA, 3, ALTERNATE);
GPIOA->AFR[0] &= ~(0x0000FF00);
GPIOA->AFR[0] |= 0x00007700;
// Start usart device
RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN;
USART2->BRR = 80000000L / baud;
USART2->CR1 |= USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
}
void serial_put(int c)
{
while (!(USART2->ISR & USART_ISR_TXE));
USART2->TDR = c & 0xFF;
}
char serial_get(void)
{
while (!(USART2->ISR & USART_ISR_RXNE))
clock_delay(10);
return USART2->RDR & 0xFF;
}
void serial_gets(char *buf, int max)
{
uint16_t index = 0;
do {
buf[index] = serial_get();
serial_put(buf[index]);
} while (buf[index] != '\r' && index++ < max);
buf[index] = '\0';
}
|