|
| 1 | +/* unit-raspi3-uart-rx.c |
| 2 | + * |
| 3 | + * Exercises the real uart_rx() from hal/raspi3.c against an emulated |
| 4 | + * PL011 register block. The register accessors are absolute MMIO |
| 5 | + * pointers, so the Makefile extracts the function verbatim and they are |
| 6 | + * redefined over a local array below before it is included. |
| 7 | + * |
| 8 | + * Copyright (C) 2026 wolfSSL Inc. |
| 9 | + * |
| 10 | + * This file is part of wolfBoot. |
| 11 | + * |
| 12 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation; either version 3 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * wolfBoot is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License |
| 23 | + * along with this program; if not, write to the Free Software |
| 24 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 25 | + */ |
| 26 | + |
| 27 | +#include <check.h> |
| 28 | +#include <stdint.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +/* Emulated register block, indexed by register offset. */ |
| 33 | +static unsigned int uart_regs[0x20]; |
| 34 | + |
| 35 | +#define UART0_DR ((volatile unsigned int*)&uart_regs[0x00 / 4]) |
| 36 | +#define UART0_RSR ((volatile unsigned int*)&uart_regs[0x04 / 4]) |
| 37 | +#define UART0_FR ((volatile unsigned int*)&uart_regs[0x18 / 4]) |
| 38 | + |
| 39 | +#define RXFE 0x10 |
| 40 | + |
| 41 | +#include "raspi3_uart_rx_extract.h" |
| 42 | + |
| 43 | +static void setup(void) |
| 44 | +{ |
| 45 | + memset(uart_regs, 0, sizeof(uart_regs)); |
| 46 | +} |
| 47 | + |
| 48 | +static void teardown(void) |
| 49 | +{ |
| 50 | +} |
| 51 | + |
| 52 | +START_TEST(test_empty_fifo_returns_zero) |
| 53 | +{ |
| 54 | + uint8_t c = 0xA5; |
| 55 | + |
| 56 | + *UART0_FR = RXFE; |
| 57 | + *UART0_DR = 'x'; |
| 58 | + |
| 59 | + ck_assert_int_eq(uart_rx(&c), 0); |
| 60 | + ck_assert_uint_eq(c, 0xA5); |
| 61 | +} |
| 62 | +END_TEST |
| 63 | + |
| 64 | +START_TEST(test_character_received) |
| 65 | +{ |
| 66 | + uint8_t c = 0; |
| 67 | + |
| 68 | + *UART0_FR = 0; |
| 69 | + *UART0_DR = 'a'; |
| 70 | + |
| 71 | + ck_assert_int_eq(uart_rx(&c), 1); |
| 72 | + ck_assert_uint_eq(c, 'a'); |
| 73 | +} |
| 74 | +END_TEST |
| 75 | + |
| 76 | +/* Bits above the data field must not reach the caller. The receive |
| 77 | + * error field at [11:8] is covered by test_receive_error_dropped. */ |
| 78 | +START_TEST(test_status_bits_masked_off) |
| 79 | +{ |
| 80 | + uint8_t c = 0; |
| 81 | + |
| 82 | + *UART0_FR = 0; |
| 83 | + *UART0_DR = 0xF000U | 'b'; |
| 84 | + |
| 85 | + ck_assert_int_eq(uart_rx(&c), 1); |
| 86 | + ck_assert_uint_eq(c, 'b'); |
| 87 | +} |
| 88 | +END_TEST |
| 89 | + |
| 90 | +START_TEST(test_receive_error_dropped) |
| 91 | +{ |
| 92 | + const unsigned int err[4] = { 0x800, 0x400, 0x200, 0x100 }; |
| 93 | + uint8_t c; |
| 94 | + int i; |
| 95 | + |
| 96 | + for (i = 0; i < 4; i++) { |
| 97 | + c = 0xA5; |
| 98 | + *UART0_FR = 0; |
| 99 | + *UART0_DR = err[i] | 'c'; |
| 100 | + *UART0_RSR = 0xF; |
| 101 | + |
| 102 | + ck_assert_int_eq(uart_rx(&c), 0); |
| 103 | + ck_assert_uint_eq(c, 0xA5); |
| 104 | + ck_assert_uint_eq(*UART0_RSR, 0); |
| 105 | + } |
| 106 | +} |
| 107 | +END_TEST |
| 108 | + |
| 109 | +Suite *raspi3_uart_rx_suite(void) |
| 110 | +{ |
| 111 | + Suite *s = suite_create("raspi3-uart-rx"); |
| 112 | + TCase *tc = tcase_create("raspi3-uart-rx"); |
| 113 | + |
| 114 | + tcase_add_checked_fixture(tc, setup, teardown); |
| 115 | + tcase_add_test(tc, test_empty_fifo_returns_zero); |
| 116 | + tcase_add_test(tc, test_character_received); |
| 117 | + tcase_add_test(tc, test_status_bits_masked_off); |
| 118 | + tcase_add_test(tc, test_receive_error_dropped); |
| 119 | + suite_add_tcase(s, tc); |
| 120 | + |
| 121 | + return s; |
| 122 | +} |
| 123 | + |
| 124 | +int main(void) |
| 125 | +{ |
| 126 | + int fails; |
| 127 | + Suite *s = raspi3_uart_rx_suite(); |
| 128 | + SRunner *sr = srunner_create(s); |
| 129 | + |
| 130 | + srunner_run_all(sr, CK_NORMAL); |
| 131 | + fails = srunner_ntests_failed(sr); |
| 132 | + srunner_free(sr); |
| 133 | + |
| 134 | + return fails; |
| 135 | +} |
0 commit comments