|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/drivertest/drivertest_mps2_isr_signal.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/nuttx.h> |
| 26 | +#include <stdbool.h> |
| 27 | +#include <stdint.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#include <stdarg.h> |
| 31 | +#include <stddef.h> |
| 32 | +#include <setjmp.h> |
| 33 | +#include <string.h> |
| 34 | +#include <cmocka.h> |
| 35 | +#include <signal.h> |
| 36 | + |
| 37 | +#include <nuttx/arch.h> |
| 38 | +#include <nuttx/irq.h> |
| 39 | +#include <nuttx/semaphore.h> |
| 40 | +#include <nuttx/serial/uart_cmsdk.h> |
| 41 | + |
| 42 | +#include <pthread.h> |
| 43 | + |
| 44 | +/**************************************************************************** |
| 45 | + * Pre-processor Definitions |
| 46 | + ****************************************************************************/ |
| 47 | + |
| 48 | +#define CONFIG_TEST_ISR_SIGNAL_US 10000 |
| 49 | + |
| 50 | +#define MPS2_ADDR2REG_PTR(base, off) (uint32_t*)((uint32_t*)(base) + (off)) |
| 51 | +#define MPS2_IRQ_FROMBASE(base, off) ((base) + (off)) |
| 52 | + |
| 53 | +/* https://developer.arm.com/documentation/101104/0200/programmers-model/ |
| 54 | + * base-element/cmsdk-timer |
| 55 | + */ |
| 56 | + |
| 57 | +#define MPS_TIMER_CTRL_OFFSET 0 |
| 58 | +#define MPS_TIMER_VALUE_OFFSET 1 |
| 59 | +#define MPS_TIMER_RELOAD_OFFSET 2 |
| 60 | +#define MPS_TIMER_CLEAR_OFFSET 3 |
| 61 | + |
| 62 | +#define MPS_TIMER_CTRL_ENABLE (1<<0) |
| 63 | +#define MPS_TIMER_CTRL_IE (1<<3) |
| 64 | + |
| 65 | +static_assert(NVIC_SYSH_PRIORITY_DEFAULT == 0x80, "prio"); |
| 66 | + |
| 67 | +/**************************************************************************** |
| 68 | + * Private Types |
| 69 | + ****************************************************************************/ |
| 70 | + |
| 71 | +typedef struct mps2_an500_timer_s |
| 72 | +{ |
| 73 | + volatile uint32_t *reload; |
| 74 | + volatile uint32_t *ctrl; |
| 75 | + volatile uint32_t *clear; |
| 76 | + pid_t pid; |
| 77 | + int irq; |
| 78 | +} mps2_an500_timer_t; |
| 79 | + |
| 80 | +/**************************************************************************** |
| 81 | + * Private Functions Prototypes |
| 82 | + ****************************************************************************/ |
| 83 | + |
| 84 | +static int timer_irq_handle(int irq, void *context, void *arg); |
| 85 | + |
| 86 | +/**************************************************************************** |
| 87 | + * Private Data |
| 88 | + ****************************************************************************/ |
| 89 | + |
| 90 | +static int last_sig = 0; |
| 91 | + |
| 92 | +static mps2_an500_timer_t timer[2]; |
| 93 | + |
| 94 | +static const int armv7m_gpio_base = 16; |
| 95 | + |
| 96 | +/**************************************************************************** |
| 97 | + * Private Functions |
| 98 | + ****************************************************************************/ |
| 99 | + |
| 100 | +static void mps_timer_init(void) |
| 101 | +{ |
| 102 | + static const uint32_t timerbase[] = |
| 103 | + { |
| 104 | + 0x40000000, 0x40001000 |
| 105 | + }; |
| 106 | + |
| 107 | + static const int timerirq[] = |
| 108 | + { |
| 109 | + 8, 9 |
| 110 | + }; |
| 111 | + |
| 112 | +#ifdef CONFIG_ARCH_IRQPRIO |
| 113 | + static const int timer_irq_prio[] = |
| 114 | + { |
| 115 | + 0x80, 0xa0 |
| 116 | + }; |
| 117 | +#endif |
| 118 | + |
| 119 | + for (int i = 0; i < 2; i++) |
| 120 | + { |
| 121 | + mps2_an500_timer_t *t = &timer[i]; |
| 122 | + |
| 123 | + t->reload = MPS2_ADDR2REG_PTR(timerbase[i], MPS_TIMER_RELOAD_OFFSET); |
| 124 | + t->ctrl = MPS2_ADDR2REG_PTR(timerbase[i], MPS_TIMER_CTRL_OFFSET); |
| 125 | + t->clear = MPS2_ADDR2REG_PTR(timerbase[i], MPS_TIMER_CLEAR_OFFSET); |
| 126 | + t->irq = MPS2_IRQ_FROMBASE(armv7m_gpio_base, timerirq[i]); |
| 127 | + |
| 128 | + irq_attach(t->irq, timer_irq_handle, t); |
| 129 | + up_enable_irq(t->irq); |
| 130 | + |
| 131 | +#ifdef CONFIG_ARCH_IRQPRIO |
| 132 | + up_prioritize_irq(t->irq, timer_irq_prio[i]); |
| 133 | +#endif |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +static int timer_irq_handle(int irq, void *context, void *arg) |
| 138 | +{ |
| 139 | + mps2_an500_timer_t *t = arg; |
| 140 | + *t->clear = 1; |
| 141 | + *t->ctrl = 0; |
| 142 | + |
| 143 | + tgkill(t->pid, t->pid, SIGINT); |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +static void timer_begin_test(mps2_an500_timer_t *t, uint32_t reload_us) |
| 148 | +{ |
| 149 | + uint32_t reload = reload_us * 25; |
| 150 | + *t->reload = reload; |
| 151 | + |
| 152 | + *t->ctrl = MPS_TIMER_CTRL_IE | MPS_TIMER_CTRL_ENABLE; |
| 153 | +} |
| 154 | + |
| 155 | +static void timer_end_test(mps2_an500_timer_t *t) |
| 156 | +{ |
| 157 | + *t->ctrl = 0; |
| 158 | + *t->clear = 1; |
| 159 | +} |
| 160 | + |
| 161 | +static void test_sa_handler(int signo) |
| 162 | +{ |
| 163 | + last_sig = signo; |
| 164 | +} |
| 165 | + |
| 166 | +static void test_irq_signal(void **argv) |
| 167 | +{ |
| 168 | + mps2_an500_timer_t *t = &timer[1]; |
| 169 | + struct timespec ts; |
| 170 | + |
| 171 | + signal(SIGINT, test_sa_handler); |
| 172 | + |
| 173 | + clock_gettime(CLOCK_MONOTONIC, &ts); |
| 174 | + |
| 175 | + t->pid = getpid(); |
| 176 | + timer_begin_test(t, CONFIG_TEST_ISR_SIGNAL_US); |
| 177 | + |
| 178 | + /* Runing thread interrupt by exception_direct causing signal miss */ |
| 179 | + |
| 180 | + while (true) |
| 181 | + { |
| 182 | + struct timespec now; |
| 183 | + up_udelay(1000); |
| 184 | + clock_gettime(CLOCK_MONOTONIC, &now); |
| 185 | + if (now.tv_sec - ts.tv_sec > 1 || last_sig != 0) |
| 186 | + { |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + assert_int_equal(last_sig, SIGINT); |
| 192 | +} |
| 193 | + |
| 194 | +static int setup(void **argv) |
| 195 | +{ |
| 196 | + mps_timer_init(); |
| 197 | + return 0; |
| 198 | +} |
| 199 | + |
| 200 | +static int teardown(void **argv) |
| 201 | +{ |
| 202 | + timer_end_test(&timer[0]); |
| 203 | + timer_end_test(&timer[1]); |
| 204 | + return 0; |
| 205 | +} |
| 206 | + |
| 207 | +/**************************************************************************** |
| 208 | + * Public Functions |
| 209 | + ****************************************************************************/ |
| 210 | + |
| 211 | +int main(int argc, char *argv[]) |
| 212 | +{ |
| 213 | + const struct CMUnitTest tests[] = { |
| 214 | + cmocka_unit_test_prestate_setup_teardown( |
| 215 | + test_irq_signal, setup, teardown, NULL), |
| 216 | + }; |
| 217 | + |
| 218 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 219 | +} |
0 commit comments