os-core/kernel/drivers/uart/uart_mini.c

85 lines
1.8 KiB
C
Raw Normal View History

2019-12-01 22:57:02 +01:00
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
2020-01-15 10:56:04 +01:00
* @file UARTMini.c
2019-12-01 22:57:02 +01:00
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#include <aarch64/aarch64.h>
#include <aarch64/bcm2837.h>
2020-01-15 10:56:04 +01:00
#include "UARTMini.h"
2019-12-01 22:57:02 +01:00
int
2020-01-15 10:56:04 +01:00
UARTMini_init(void)
2019-12-01 22:57:02 +01:00
{
unsigned int sel;
/* Enable UART Mini and its registers*/
2020-01-15 10:56:04 +01:00
AArch64_setReg32(AUX_ENABLES, 1);
2019-12-01 22:57:02 +01:00
/* Disable TX and RX interrupts */
2020-01-15 10:56:04 +01:00
AArch64_setReg32(AUX_MU_IER_REG, 0);
/* Disable auto flow control, TX and RX */
2020-01-15 10:56:04 +01:00
AArch64_setReg32(AUX_MU_CNTL_REG, 0);
2019-12-01 22:57:02 +01:00
/* Set 8bit mode */
2020-01-15 10:56:04 +01:00
AArch64_setReg32(AUX_MU_LCR_REG, 3);
2019-12-01 22:57:02 +01:00
/* Set RTS line HIGH */
2020-01-15 10:56:04 +01:00
AArch64_setReg32(AUX_MU_MCR_REG, 0);
2019-12-01 22:57:02 +01:00
/* Set baud rate 115200 */
2020-01-15 10:56:04 +01:00
AArch64_setReg32(AUX_MU_IER_REG, 0);
AArch64_setReg32(AUX_MU_IIR_REG, 0xC6);
AArch64_setReg32(AUX_MU_BAUD_REG, 270);
2019-12-01 22:57:02 +01:00
2020-01-15 10:56:04 +01:00
sel = AArch64_getReg32(GPFSEL1);
2019-12-01 22:57:02 +01:00
/* clean and set ALT5 for GPIO14 */
sel &= ~(7 << 12);
sel |= (2 << 12);
/* clean and set ALT5 for GPIO15 */
sel &= ~(7 << 15);
sel |= (2 << 15);
2020-01-15 10:56:04 +01:00
AArch64_setReg32(GPFSEL1, sel);
2019-12-01 22:57:02 +01:00
2020-01-15 10:56:04 +01:00
AArch64_setReg32(GPPUD, 0);
AArch64_delay(150);
AArch64_setReg32(GPPUDCLK0, (1 << 14) | (1 << 15));
AArch64_delay(150);
AArch64_setReg32(GPPUDCLK0, 0);
2019-12-01 22:57:02 +01:00
/* Enable TX and RX */
2020-01-15 10:56:04 +01:00
AArch64_setReg32(AUX_MU_CNTL_REG, 3);
2019-12-01 22:57:02 +01:00
return 0;
}
int
2020-01-15 10:56:04 +01:00
UARTMini_put(char c)
2019-12-01 22:57:02 +01:00
{
while (1) {
2020-01-15 10:56:04 +01:00
if (AArch64_getReg32(AUX_MU_LSR_REG) & 0x20)
2019-12-01 22:57:02 +01:00
break;
}
2020-01-15 10:56:04 +01:00
AArch64_setReg32(AUX_MU_IO_REG, c);
2019-12-01 22:57:02 +01:00
return 0;
}
int
2020-01-15 10:56:04 +01:00
UARTMini_get(char *pc)
2019-12-01 22:57:02 +01:00
{
while (1) {
2020-01-15 10:56:04 +01:00
if (AArch64_getReg32(AUX_MU_LSR_REG) & 0x01)
2019-12-01 22:57:02 +01:00
break;
}
2020-01-15 10:56:04 +01:00
*pc = AArch64_getReg32(AUX_MU_IO_REG) & 0xFF;
2019-12-01 22:57:02 +01:00
return 0;
}