1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | |
---|
4 | /* |
---|
5 | * Copyright (c) 2016 embedded brains GmbH. All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | * POSSIBILITY OF SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #include <bsp/sc16is752.h> |
---|
30 | |
---|
31 | #include <rtems/irq-extension.h> |
---|
32 | |
---|
33 | static void atsam_sc16i752_irqs_handler(void *arg) |
---|
34 | { |
---|
35 | atsam_sc16is752_spi_context *ctx = arg; |
---|
36 | |
---|
37 | sc16is752_interrupt_handler(&ctx->base.base); |
---|
38 | PIO_EnableIt(&ctx->irq_pin); |
---|
39 | } |
---|
40 | |
---|
41 | static void atsam_sc16i752_interrupt(const Pin *pin, void *arg) |
---|
42 | { |
---|
43 | atsam_sc16is752_spi_context *ctx = arg; |
---|
44 | |
---|
45 | if(PIO_ItIsActive(&ctx->irq_pin)) { |
---|
46 | PIO_DisableIt(&ctx->irq_pin); |
---|
47 | rtems_interrupt_server_entry_submit(&ctx->irqs_entry); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | static bool atsam_sc16is752_install_interrupt(sc16is752_context *base) |
---|
52 | { |
---|
53 | atsam_sc16is752_spi_context *ctx = (atsam_sc16is752_spi_context *)base; |
---|
54 | rtems_status_code sc; |
---|
55 | uint8_t rv; |
---|
56 | |
---|
57 | sc = rtems_interrupt_server_entry_initialize(ctx->irqs_index, |
---|
58 | &ctx->irqs_entry); |
---|
59 | rtems_interrupt_server_action_prepend(&ctx->irqs_entry, |
---|
60 | &ctx->irqs_action, atsam_sc16i752_irqs_handler, ctx); |
---|
61 | |
---|
62 | rv = PIO_Configure(&ctx->irq_pin, 1); |
---|
63 | PIO_ConfigureIt(&ctx->irq_pin, atsam_sc16i752_interrupt, ctx); |
---|
64 | PIO_EnableIt(&ctx->irq_pin); |
---|
65 | |
---|
66 | return (sc == RTEMS_SUCCESSFUL) && (rv == 1); |
---|
67 | } |
---|
68 | |
---|
69 | static void atsam_sc16is752_remove_interrupt(sc16is752_context *base) |
---|
70 | { |
---|
71 | atsam_sc16is752_spi_context *ctx = (atsam_sc16is752_spi_context *)base; |
---|
72 | rtems_status_code sc; |
---|
73 | |
---|
74 | PIO_DisableIt(&ctx->irq_pin); |
---|
75 | sc = PIO_RemoveIt(&ctx->irq_pin, atsam_sc16i752_interrupt, ctx); |
---|
76 | assert(sc == RTEMS_SUCCESSFUL); |
---|
77 | } |
---|
78 | |
---|
79 | int atsam_sc16is752_spi_create( |
---|
80 | atsam_sc16is752_spi_context *ctx, |
---|
81 | const atsam_sc16is752_spi_config *config |
---|
82 | ) |
---|
83 | { |
---|
84 | ctx->base.base.mode = config->mode; |
---|
85 | ctx->base.base.input_frequency = config->input_frequency; |
---|
86 | ctx->base.base.install_irq = atsam_sc16is752_install_interrupt; |
---|
87 | ctx->base.base.remove_irq = atsam_sc16is752_remove_interrupt; |
---|
88 | ctx->base.spi_path = config->spi_path; |
---|
89 | ctx->base.cs = config->spi_chip_select; |
---|
90 | ctx->base.speed_hz = config->spi_speed_hz; |
---|
91 | ctx->irq_pin = config->irq_pin; |
---|
92 | ctx->irqs_index = config->server_index; |
---|
93 | |
---|
94 | return sc16is752_spi_create(&ctx->base, config->device_path); |
---|
95 | } |
---|