Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:48

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 
0004 /*
0005  * Copyright (c) 2016 embedded brains GmbH & Co. KG
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #include <bsp/sc16is752.h>
0030 
0031 #include <rtems/irq-extension.h>
0032 
0033 static void atsam_sc16i752_irqs_handler(void *arg)
0034 {
0035   atsam_sc16is752_spi_context *ctx = arg;
0036 
0037     sc16is752_interrupt_handler(&ctx->base.base);
0038     PIO_EnableIt(&ctx->irq_pin);
0039 }
0040 
0041 static void atsam_sc16i752_interrupt(const Pin *pin, void *arg)
0042 {
0043   atsam_sc16is752_spi_context *ctx = arg;
0044 
0045   if(PIO_ItIsActive(&ctx->irq_pin)) {
0046     PIO_DisableIt(&ctx->irq_pin);
0047     rtems_interrupt_server_entry_submit(&ctx->irqs_entry);
0048   }
0049 }
0050 
0051 static bool atsam_sc16is752_install_interrupt(sc16is752_context *base)
0052 {
0053   atsam_sc16is752_spi_context *ctx = (atsam_sc16is752_spi_context *)base;
0054   rtems_status_code sc;
0055   uint8_t rv;
0056 
0057   sc = rtems_interrupt_server_entry_initialize(ctx->irqs_index,
0058     &ctx->irqs_entry);
0059   rtems_interrupt_server_action_prepend(&ctx->irqs_entry,
0060     &ctx->irqs_action, atsam_sc16i752_irqs_handler, ctx);
0061 
0062   rv = PIO_Configure(&ctx->irq_pin, 1);
0063   PIO_ConfigureIt(&ctx->irq_pin, atsam_sc16i752_interrupt, ctx);
0064   PIO_EnableIt(&ctx->irq_pin);
0065 
0066   return (sc == RTEMS_SUCCESSFUL) && (rv == 1);
0067 }
0068 
0069 static void atsam_sc16is752_remove_interrupt(sc16is752_context *base)
0070 {
0071   atsam_sc16is752_spi_context *ctx = (atsam_sc16is752_spi_context *)base;
0072   rtems_status_code sc;
0073 
0074   PIO_DisableIt(&ctx->irq_pin);
0075   sc = PIO_RemoveIt(&ctx->irq_pin, atsam_sc16i752_interrupt, ctx);
0076   assert(sc == RTEMS_SUCCESSFUL);
0077 }
0078 
0079 int atsam_sc16is752_spi_create(
0080   atsam_sc16is752_spi_context      *ctx,
0081   const atsam_sc16is752_spi_config *config
0082 )
0083 {
0084   ctx->base.base.mode = config->mode;
0085   ctx->base.base.input_frequency = config->input_frequency;
0086   ctx->base.base.install_irq = atsam_sc16is752_install_interrupt;
0087   ctx->base.base.remove_irq = atsam_sc16is752_remove_interrupt;
0088   ctx->base.spi_path = config->spi_path;
0089   ctx->base.cs = config->spi_chip_select;
0090   ctx->base.speed_hz = config->spi_speed_hz;
0091   ctx->irq_pin = config->irq_pin;
0092   ctx->irqs_index = config->server_index;
0093 
0094   return sc16is752_spi_create(&ctx->base, config->device_path);
0095 }