Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:42

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSDeviceSPIGPIO
0007  *
0008  * @brief This header file provides the interfaces of @ref RTEMSDeviceSPIGPIO.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2024 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifndef _RTEMS_DEV_SPI_SPI_GPIO_H
0037 #define _RTEMS_DEV_SPI_SPI_GPIO_H
0038 
0039 #include <rtems.h>
0040 #include <stdbool.h>
0041 
0042 #ifdef __cplusplus
0043 extern "C" {
0044 #endif /* __cplusplus */
0045 
0046 /**
0047  * @defgroup RTEMSDeviceSPIGPIO
0048  *
0049  * @ingroup SPI
0050  *
0051  * @brief A simple SPI bus driver that just uses GPIO pins
0052  *
0053  * Implements a SPI that is emulated by toggling GPIO pins. Callbacks are used
0054  * to set or get the pins so that it can be easily adapted to various GPIO
0055  * controllers.
0056  *
0057  * NOTE: This driver is slow! If you need performance: Don't use it. The
0058  * intended use case is (for example) a one time configuration of some SPI
0059  * peripheral.
0060  *
0061  * The driver will just work as fast as it can. Setting a speed limit is
0062  * currently not supported.
0063  *
0064  * @{
0065  */
0066 
0067 #define SPI_GPIO_MAX_CS 4
0068 
0069 /**
0070  * @brief Type of the functions that set pins
0071  *
0072  * Set a GPIO pin to @a level. @a level is false for low or true for high. @a
0073  * arg is an application specific parameter.
0074  */
0075 typedef void (spi_gpio_set_pin_fn) (void *arg, bool level);
0076 
0077 /**
0078  * @brief Type of the functions that read pins
0079  *
0080  * Get current level of GPIO pin. Should return either 0 for low or anything
0081  * else for high. @a arg is an application specific parameter.
0082  */
0083 typedef bool (spi_gpio_get_pin_fn) (void *arg);
0084 
0085 /**
0086  * @brief Parameters for the driver
0087  *
0088  * Parameters that should be provided by the application while registering the
0089  * driver. Mainly functions to set single pins.
0090  */
0091 struct spi_gpio_params {
0092   /** Function for setting the clock pin */
0093   spi_gpio_set_pin_fn *set_clk;
0094   /** Application specific argument for the clock pin setter function */
0095   void *set_clk_arg;
0096   /** Function for setting the mosi pin */
0097   spi_gpio_set_pin_fn *set_mosi;
0098   /** Application specific argument for the mosi pin setter function */
0099   void *set_mosi_arg;
0100   /** Function for reading the miso pin */
0101   spi_gpio_get_pin_fn *get_miso;
0102   /** Application specific argument for the miso pin getter function */
0103   void *get_miso_arg;
0104   /** Functions for setting the cs pins */
0105   spi_gpio_set_pin_fn *set_cs[SPI_GPIO_MAX_CS];
0106   /** Application specific arguments for the cs pin getter functions */
0107   void *set_cs_arg[SPI_GPIO_MAX_CS];
0108 
0109   /**
0110    * Idle level of the CS pin.
0111    *
0112    * Set the value to 0 for a high active CS or to 1 for a low active CS pin.
0113    */
0114   bool cs_idle[SPI_GPIO_MAX_CS];
0115 };
0116 
0117 /**
0118  * Register a new SPI GPIO instance at the device path @a device.
0119  *
0120  * To save memory, @a params will be used directly. Make sure that the structure
0121  * remains valid during the complete application run time.
0122  *
0123  * @returns RTEMS_SUCCESSFUL if registering the bus worked or an error code if
0124  * it didn't.
0125  */
0126 rtems_status_code spi_gpio_init(
0127     const char *device,
0128     const struct spi_gpio_params *params
0129   );
0130 
0131 /** @} */
0132 
0133 #ifdef __cplusplus
0134 }
0135 #endif /* __cplusplus */
0136 
0137 #endif /* _RTEMS_DEV_SPI_SPI_GPIO_H */