Software APIs
sw
device
tests
uart_smoketest.c
1
// Copyright lowRISC contributors (OpenTitan project).
2
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3
// SPDX-License-Identifier: Apache-2.0
4
5
#include "dt/dt_uart.h"
6
#include "
sw/device/lib/arch/device.h
"
7
#include "
sw/device/lib/base/mmio.h
"
8
#include "
sw/device/lib/dif/dif_uart.h
"
9
#include "
sw/device/lib/runtime/hart.h
"
10
#include "sw/device/lib/testing/test_framework/check.h"
11
#include "
sw/device/lib/testing/test_framework/ottf_main.h
"
12
13
static
const
uint8_t kSendData[] =
"Smoke test!"
;
14
15
OTTF_DEFINE_TEST_CONFIG(.enable_concurrency =
false
,
16
.console.test_may_clobber =
true
, );
17
18
bool
test_main
(
void
) {
19
dif_uart_t uart;
20
CHECK_DIF_OK(dif_uart_init_from_dt(kDtUart0, &uart));
21
CHECK(
kUartBaudrate
<= UINT32_MAX,
"kUartBaudrate must fit in uint32_t"
);
22
CHECK(
kClockFreqPeripheralHz
<= UINT32_MAX,
23
"kClockFreqPeripheralHz must fit in uint32_t"
);
24
CHECK_DIF_OK(
dif_uart_configure
(
25
&uart, (
dif_uart_config_t
){
26
.baudrate = (uint32_t)
kUartBaudrate
,
27
.clk_freq_hz = (uint32_t)
kClockFreqPeripheralHz
,
28
.parity_enable =
kDifToggleDisabled
,
29
.parity =
kDifUartParityEven
,
30
.tx_enable =
kDifToggleEnabled
,
31
.rx_enable =
kDifToggleEnabled
,
32
}));
33
CHECK_DIF_OK(
34
dif_uart_loopback_set
(&uart,
kDifUartLoopbackSystem
,
kDifToggleEnabled
));
35
CHECK_DIF_OK(
dif_uart_fifo_reset
(&uart,
kDifUartDatapathAll
));
36
37
// Send all bytes in `kSendData`, and check that they are received via
38
// the loopback mechanism.
39
for
(
int
i = 0; i <
sizeof
(kSendData); ++i) {
40
CHECK_DIF_OK(
dif_uart_byte_send_polled
(&uart, kSendData[i]));
41
42
uint8_t receive_byte;
43
CHECK_DIF_OK(
dif_uart_byte_receive_polled
(&uart, &receive_byte));
44
CHECK(receive_byte == kSendData[i]);
45
}
46
47
return
true
;
48
}
Return to
OpenTitan Documentation