|
| 1 | +/**************************************************************************** |
| 2 | + * sched/irq/irq_attach_wqueue.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/config.h> |
| 26 | + |
| 27 | +#include <errno.h> |
| 28 | +#include <stdio.h> |
| 29 | + |
| 30 | +#include <nuttx/irq.h> |
| 31 | +#include <nuttx/wqueue.h> |
| 32 | +#include <debug.h> |
| 33 | + |
| 34 | +#include "irq/irq.h" |
| 35 | +#include "sched/sched.h" |
| 36 | + |
| 37 | +/**************************************************************************** |
| 38 | + * Privte Types |
| 39 | + ****************************************************************************/ |
| 40 | + |
| 41 | +/* This is the type of the list of interrupt handlers, one for each IRQ. |
| 42 | + * This type provided all of the information necessary to irq_dispatch to |
| 43 | + * transfer control to interrupt handlers after the occurrence of an |
| 44 | + * interrupt. |
| 45 | + */ |
| 46 | + |
| 47 | +struct irq_work_info_s |
| 48 | +{ |
| 49 | + xcpt_t handler; /* Address of the interrupt handler */ |
| 50 | + xcpt_t isrwork; /* Address of the interrupt worked handler */ |
| 51 | + FAR void *arg; /* The argument provided to the interrupt handler. */ |
| 52 | + int irq; /* Irq id */ |
| 53 | + struct work_s work; /* Interrupt work to the wq */ |
| 54 | + |
| 55 | + FAR struct kwork_wqueue_s *wqueue; /* Work queue. */ |
| 56 | +}; |
| 57 | + |
| 58 | +/**************************************************************************** |
| 59 | + * Private Data |
| 60 | + ****************************************************************************/ |
| 61 | + |
| 62 | +#ifdef CONFIG_ARCH_MINIMAL_VECTORTABLE |
| 63 | +static struct irq_work_info_s |
| 64 | +g_irq_work_vector[CONFIG_ARCH_NUSER_INTERRUPTS]; |
| 65 | +#else |
| 66 | +static struct irq_work_info_s g_irq_work_vector[NR_IRQS]; |
| 67 | +#endif |
| 68 | + |
| 69 | +static mutex_t g_irq_wqueue_lock = NXMUTEX_INITIALIZER; |
| 70 | +static FAR struct kwork_wqueue_s *g_irq_wqueue[CONFIG_IRQ_NWORKS]; |
| 71 | + |
| 72 | +/**************************************************************************** |
| 73 | + * Private Functions |
| 74 | + ****************************************************************************/ |
| 75 | + |
| 76 | +static |
| 77 | +inline_function FAR struct kwork_wqueue_s *irq_get_wqueue(int priority) |
| 78 | +{ |
| 79 | + FAR struct kwork_wqueue_s *queue; |
| 80 | + int wqueue_priority; |
| 81 | + int i; |
| 82 | + |
| 83 | + nxmutex_lock(&g_irq_wqueue_lock); |
| 84 | + for (i = 0; g_irq_wqueue[i] != NULL && i < CONFIG_IRQ_NWORKS; i++) |
| 85 | + { |
| 86 | + wqueue_priority = work_queue_priority_wq(g_irq_wqueue[i]); |
| 87 | + DEBUGASSERT(wqueue_priority >= SCHED_PRIORITY_MIN && |
| 88 | + wqueue_priority <= SCHED_PRIORITY_MAX); |
| 89 | + |
| 90 | + if (wqueue_priority == priority) |
| 91 | + { |
| 92 | + nxmutex_unlock(&g_irq_wqueue_lock); |
| 93 | + return g_irq_wqueue[i]; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + DEBUGASSERT(i < CONFIG_IRQ_NWORKS); |
| 98 | + |
| 99 | + queue = work_queue_create("isrwork", priority, |
| 100 | + CONFIG_IRQ_WORK_STACKSIZE, 1); |
| 101 | + |
| 102 | + g_irq_wqueue[i] = queue; |
| 103 | + nxmutex_unlock(&g_irq_wqueue_lock); |
| 104 | + return queue; |
| 105 | +} |
| 106 | + |
| 107 | +/* Default interrupt handler for worked interrupts. |
| 108 | + * Useful for oneshot interrupts. |
| 109 | + */ |
| 110 | + |
| 111 | +static void irq_work_handler(FAR void *arg) |
| 112 | +{ |
| 113 | + FAR struct irq_work_info_s *info = arg; |
| 114 | + |
| 115 | + info->isrwork(info->irq, NULL, info->arg); |
| 116 | +} |
| 117 | + |
| 118 | +static int irq_default_handler(int irq, FAR void *regs, FAR void *arg) |
| 119 | +{ |
| 120 | + FAR struct irq_work_info_s *info = arg; |
| 121 | + int ret; |
| 122 | + |
| 123 | + ret = info->handler(irq, regs, arg); |
| 124 | + |
| 125 | + if (ret == IRQ_WAKE_THREAD) |
| 126 | + { |
| 127 | + work_queue_wq(info->wqueue, &info->work, irq_work_handler, info, 0); |
| 128 | + ret = OK; |
| 129 | + } |
| 130 | + |
| 131 | + return ret; |
| 132 | +} |
| 133 | + |
| 134 | +/**************************************************************************** |
| 135 | + * Public Functions |
| 136 | + ****************************************************************************/ |
| 137 | + |
| 138 | +/**************************************************************************** |
| 139 | + * Name: irq_attach_wqueue |
| 140 | + * |
| 141 | + * Description: |
| 142 | + * Configure the IRQ subsystem so that IRQ number 'irq' is dispatched to |
| 143 | + * 'wqueue' |
| 144 | + * |
| 145 | + * Input Parameters: |
| 146 | + * irq - Irq num |
| 147 | + * isr - Function to be called when the IRQ occurs, called in interrupt |
| 148 | + * context. |
| 149 | + * If isr is NULL the default handler is installed(irq_default_handler). |
| 150 | + * isrwork - called in thread context, If the isrwork is NULL, |
| 151 | + * then the ISR is being detached. |
| 152 | + * arg - privdate data |
| 153 | + * priority - isrwork pri |
| 154 | + * |
| 155 | + * Returned Value: |
| 156 | + * Zero on success; a negated errno value on failure. |
| 157 | + * |
| 158 | + ****************************************************************************/ |
| 159 | + |
| 160 | +int irq_attach_wqueue(int irq, xcpt_t isr, xcpt_t isrwork, |
| 161 | + FAR void *arg, int priority) |
| 162 | +{ |
| 163 | + FAR struct irq_work_info_s *info; |
| 164 | + |
| 165 | +#if NR_IRQS > 0 |
| 166 | + int ndx; |
| 167 | + |
| 168 | + if ((unsigned)irq >= NR_IRQS) |
| 169 | + { |
| 170 | + return -EINVAL; |
| 171 | + } |
| 172 | + |
| 173 | + ndx = IRQ_TO_NDX(irq); |
| 174 | + if (ndx < 0) |
| 175 | + { |
| 176 | + return ndx; |
| 177 | + } |
| 178 | + |
| 179 | + /* If the isrwork is NULL, then the ISR is being detached. */ |
| 180 | + |
| 181 | + info = &g_irq_work_vector[ndx]; |
| 182 | + |
| 183 | + if (isrwork == NULL) |
| 184 | + { |
| 185 | + irq_detach(irq); |
| 186 | + info->isrwork = NULL; |
| 187 | + info->handler = NULL; |
| 188 | + info->arg = NULL; |
| 189 | + info->wqueue = NULL; |
| 190 | + return OK; |
| 191 | + } |
| 192 | + |
| 193 | + info->isrwork = isrwork; |
| 194 | + info->handler = isr; |
| 195 | + info->arg = arg; |
| 196 | + info->irq = irq; |
| 197 | + if (info->wqueue == NULL) |
| 198 | + { |
| 199 | + info->wqueue = irq_get_wqueue(priority); |
| 200 | + } |
| 201 | + |
| 202 | + irq_attach(irq, irq_default_handler, info); |
| 203 | +#endif /* NR_IRQS */ |
| 204 | + |
| 205 | + return OK; |
| 206 | +} |
| 207 | + |
0 commit comments