/* test_fir_drv.c * Host-side test of fir_drv.c. The hardware is replaced by an array of * uint32_t that stands in for the register block; the driver only ever sees * a base pointer, so it cannot tell the difference. * * Build and run: * clang -std=c11 -Wall -Wextra -Werror fir_drv.c test_fir_drv.c -o test_fir_drv && ./test_fir_drv * Exit status 0 means every check passed. */ #include #include #include "fir_regs.h" #define N_WORDS 16u static volatile uint32_t mock_regs[N_WORDS]; static int failures = 0; static void check(int cond, const char *what) { if (!cond) { printf("FAIL: %s\n", what); failures++; } } static void reset_mock(void) { for (unsigned i = 0; i < N_WORDS; i++) { mock_regs[i] = 0; } mock_regs[FIR_REG_ID / 4u] = FIR_ID_VALUE; } int main(void) { fir_dev dev; /* fir_init checks the ID register. */ reset_mock(); check(fir_init(&dev, mock_regs) == 0, "fir_init accepts the right ID"); mock_regs[FIR_REG_ID / 4u] = 0xDEADBEEFu; check(fir_init(&dev, mock_regs) == -1, "fir_init rejects a wrong ID"); /* Coefficients land at the right offsets, sign-extended. */ reset_mock(); (void)fir_init(&dev, mock_regs); const int16_t coef[4] = {-39, 156, 32767, -32768}; check(fir_set_coefs(&dev, coef, 4) == 0, "fir_set_coefs returns 0"); check(mock_regs[FIR_REG_COEF0 / 4u + 0] == 0xFFFFFFD9u, "COEF0 = -39 sign-extended"); check(mock_regs[FIR_REG_COEF0 / 4u + 1] == 0x0000009Cu, "COEF1 = 156"); check(mock_regs[FIR_REG_COEF0 / 4u + 2] == 0x00007FFFu, "COEF2 = 32767"); check(mock_regs[FIR_REG_COEF0 / 4u + 3] == 0xFFFF8000u, "COEF3 = -32768"); check(mock_regs[FIR_REG_COEF0 / 4u + 4] == 0u, "COEF4 untouched"); /* Too many coefficients: rejected before any write. */ reset_mock(); (void)fir_init(&dev, mock_regs); int16_t many[FIR_N_COEF + 1]; memset(many, 0x11, sizeof many); check(fir_set_coefs(&dev, many, FIR_N_COEF + 1) == -1, "too many coefficients rejected"); check(mock_regs[FIR_REG_COEF0 / 4u] == 0u, "nothing written on error"); /* fir_start writes ENABLE and START together. */ fir_start(&dev); check(mock_regs[FIR_REG_CTRL / 4u] == (FIR_CTRL_ENABLE | FIR_CTRL_START), "CTRL = ENABLE | START"); /* fir_wait_done: done already set, then a timeout while busy. */ mock_regs[FIR_REG_STATUS / 4u] = FIR_STATUS_DONE; check(fir_wait_done(&dev, 100) == 1, "DONE seen on the first poll"); mock_regs[FIR_REG_STATUS / 4u] = FIR_STATUS_BUSY; check(fir_wait_done(&dev, 100) == -1, "timeout while BUSY"); if (failures == 0) { printf("PASS: fir_drv host test\n"); return 0; } printf("FAIL: %d checks failed\n", failures); return 1; }