1 /*! 2 * \file gpio.h 3 * 4 * \brief GPIO driver implementation 5 * 6 * \remark: Relies on the specific board GPIO implementation as well as on 7 * IO expander driver implementation if one is available on the target 8 * board. 9 * 10 * \copyright Revised BSD License, see section \ref LICENSE. 11 * 12 * \code 13 * ______ _ 14 * / _____) _ | | 15 * ( (____ _____ ____ _| |_ _____ ____| |__ 16 * \____ \| ___ | (_ _) ___ |/ ___) _ \ 17 * _____) ) ____| | | || |_| ____( (___| | | | 18 * (______/|_____)_|_|_| \__)_____)\____)_| |_| 19 * (C)2013-2017 Semtech 20 * 21 * \endcode 22 * 23 * \author Miguel Luis ( Semtech ) 24 * 25 * \author Gregory Cristian ( Semtech ) 26 */ 27 #ifndef __GPIO_H__ 28 #define __GPIO_H__ 29 30 #ifdef __cplusplus 31 extern "C" 32 { 33 #endif 34 35 #include <stdint.h> 36 #include "pinName-board.h" 37 #include "pinName-ioe.h" 38 39 /*! 40 * Board GPIO pin names 41 */ 42 typedef enum 43 { 44 MCU_PINS, 45 IOE_PINS, 46 47 // Not connected 48 NC = (int)0xFFFFFFFF 49 }PinNames; 50 51 /*! 52 * Operation Mode for the GPIO 53 */ 54 typedef enum 55 { 56 PIN_INPUT = 0, 57 PIN_OUTPUT, 58 PIN_ALTERNATE_FCT, 59 PIN_ANALOGIC 60 }PinModes; 61 62 /*! 63 * Add a pull-up, a pull-down or nothing on the GPIO line 64 */ 65 typedef enum 66 { 67 PIN_NO_PULL = 0, 68 PIN_PULL_UP, 69 PIN_PULL_DOWN 70 }PinTypes; 71 72 /*! 73 * Define the GPIO as Push-pull type or Open Drain 74 */ 75 typedef enum 76 { 77 PIN_PUSH_PULL = 0, 78 PIN_OPEN_DRAIN 79 }PinConfigs; 80 81 /*! 82 * Define the GPIO IRQ on a rising, falling or both edges 83 */ 84 typedef enum 85 { 86 NO_IRQ = 0, 87 IRQ_RISING_EDGE, 88 IRQ_FALLING_EDGE, 89 IRQ_RISING_FALLING_EDGE 90 }IrqModes; 91 92 /*! 93 * Define the IRQ priority on the GPIO 94 */ 95 typedef enum 96 { 97 IRQ_VERY_LOW_PRIORITY = 0, 98 IRQ_LOW_PRIORITY, 99 IRQ_MEDIUM_PRIORITY, 100 IRQ_HIGH_PRIORITY, 101 IRQ_VERY_HIGH_PRIORITY 102 }IrqPriorities; 103 104 /*! 105 * GPIO IRQ handler function prototype 106 */ 107 typedef void( GpioIrqHandler )( void* context ); 108 109 /*! 110 * Structure for the GPIO 111 */ 112 typedef struct 113 { 114 PinNames pin; 115 uint16_t pinIndex; 116 void *port; 117 uint16_t portIndex; 118 PinTypes pull; 119 void* Context; 120 GpioIrqHandler* IrqHandler; 121 }Gpio_t; 122 123 /*! 124 * \brief Initializes the given GPIO object 125 * 126 * \param [IN] obj Pointer to the GPIO object 127 * \param [IN] pin Pin name ( please look in pinName-board.h file ) 128 * \param [IN] mode Pin mode [PIN_INPUT, PIN_OUTPUT, 129 * PIN_ALTERNATE_FCT, PIN_ANALOGIC] 130 * \param [IN] config Pin config [PIN_PUSH_PULL, PIN_OPEN_DRAIN] 131 * \param [IN] type Pin type [PIN_NO_PULL, PIN_PULL_UP, PIN_PULL_DOWN] 132 * \param [IN] value Default output value at initialization 133 */ 134 void GpioInit( Gpio_t *obj, PinNames pin, PinModes mode, PinConfigs config, PinTypes type, uint32_t value ); 135 136 /*! 137 * \brief Sets a user defined object pointer 138 * 139 * \param [IN] context User defined data object pointer to pass back 140 * on IRQ handler callback 141 */ 142 void GpioSetContext( Gpio_t *obj, void* context ); 143 144 /*! 145 * \brief GPIO IRQ Initialization 146 * 147 * \param [IN] obj Pointer to the GPIO object 148 * \param [IN] irqMode IRQ mode [NO_IRQ, IRQ_RISING_EDGE, 149 * IRQ_FALLING_EDGE, IRQ_RISING_FALLING_EDGE] 150 * \param [IN] irqPriority IRQ priority [IRQ_VERY_LOW_PRIORITY, IRQ_LOW_PRIORITY 151 * IRQ_MEDIUM_PRIORITY, IRQ_HIGH_PRIORITY 152 * IRQ_VERY_HIGH_PRIORITY] 153 * \param [IN] irqHandler Callback function pointer 154 */ 155 void GpioSetInterrupt( Gpio_t *obj, IrqModes irqMode, IrqPriorities irqPriority, GpioIrqHandler *irqHandler ); 156 157 /*! 158 * \brief Removes the interrupt from the object 159 * 160 * \param [IN] obj Pointer to the GPIO object 161 */ 162 void GpioRemoveInterrupt( Gpio_t *obj ); 163 164 /*! 165 * \brief Writes the given value to the GPIO output 166 * 167 * \param [IN] obj Pointer to the GPIO object 168 * \param [IN] value New GPIO output value 169 */ 170 void GpioWrite( Gpio_t *obj, uint32_t value ); 171 172 /*! 173 * \brief Toggle the value to the GPIO output 174 * 175 * \param [IN] obj Pointer to the GPIO object 176 */ 177 void GpioToggle( Gpio_t *obj ); 178 179 /*! 180 * \brief Reads the current GPIO input value 181 * 182 * \param [IN] obj Pointer to the GPIO object 183 * \retval value Current GPIO input value 184 */ 185 uint32_t GpioRead( Gpio_t *obj ); 186 187 #ifdef __cplusplus 188 } 189 #endif 190 191 #endif // __GPIO_H__ 192