from bbc microbit library

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nrf_gpiote.h Source File

nrf_gpiote.h

00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 #ifndef NRF_GPIOTE_H__
00033 #define NRF_GPIOTE_H__
00034 
00035 #include "nrf.h"
00036 #include <stdint.h>
00037 #include <stddef.h>
00038 #include <stdbool.h>
00039 
00040 /**
00041 * @defgroup nrf_gpiote_abs GPIOTE abstraction
00042 * @{
00043 * @ingroup nrf_gpiote
00044 * @brief GPIOTE abstraction for configuration of channels.
00045 */
00046 #ifdef NRF51
00047 #define NUMBER_OF_GPIO_TE 4
00048 #elif defined NRF52
00049 #define NUMBER_OF_GPIO_TE 8
00050 #else
00051 #error "Chip family not specified"
00052 #endif
00053 
00054  /**
00055  * @enum nrf_gpiote_polarity_t
00056  * @brief Polarity for the GPIOTE channel.
00057  */
00058 typedef enum
00059 {
00060   NRF_GPIOTE_POLARITY_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi,       ///<  Low to high.
00061   NRF_GPIOTE_POLARITY_HITOLO = GPIOTE_CONFIG_POLARITY_HiToLo,       ///<  High to low.
00062   NRF_GPIOTE_POLARITY_TOGGLE = GPIOTE_CONFIG_POLARITY_Toggle        ///<  Toggle.
00063 } nrf_gpiote_polarity_t;
00064 
00065 
00066  /**
00067  * @enum nrf_gpiote_outinit_t
00068  * @brief Initial output value for the GPIOTE channel.
00069  */
00070 typedef enum
00071 {
00072   NRF_GPIOTE_INITIAL_VALUE_LOW  = GPIOTE_CONFIG_OUTINIT_Low,       ///<  Low to high.
00073   NRF_GPIOTE_INITIAL_VALUE_HIGH = GPIOTE_CONFIG_OUTINIT_High       ///<  High to low.
00074 } nrf_gpiote_outinit_t;
00075 
00076 /**
00077  * @brief Tasks.
00078  */
00079 typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
00080 {
00081     NRF_GPIOTE_TASKS_OUT_0     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[0]), /**< Out task 0.*/
00082     NRF_GPIOTE_TASKS_OUT_1     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[1]), /**< Out task 1.*/
00083     NRF_GPIOTE_TASKS_OUT_2     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[2]), /**< Out task 2.*/
00084     NRF_GPIOTE_TASKS_OUT_3     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[3]), /**< Out task 3.*/
00085     /*lint -restore*/
00086 } nrf_gpiote_tasks_t;
00087 
00088 /**
00089  * @brief Events.
00090  */
00091 typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
00092 {
00093     NRF_GPIOTE_EVENTS_IN_0     = offsetof(NRF_GPIOTE_Type, EVENTS_IN[0]), /**< In event 0.*/
00094     NRF_GPIOTE_EVENTS_IN_1     = offsetof(NRF_GPIOTE_Type, EVENTS_IN[1]), /**< In event 1.*/
00095     NRF_GPIOTE_EVENTS_IN_2     = offsetof(NRF_GPIOTE_Type, EVENTS_IN[2]), /**< In event 2.*/
00096     NRF_GPIOTE_EVENTS_IN_3     = offsetof(NRF_GPIOTE_Type, EVENTS_IN[3]), /**< In event 3.*/
00097     NRF_GPIOTE_EVENTS_PORT     = offsetof(NRF_GPIOTE_Type, EVENTS_PORT), /**<  Port event.*/
00098     /*lint -restore*/
00099 } nrf_gpiote_events_t;
00100 
00101 /**
00102  * @enum nrf_gpiote_int_t
00103  * @brief GPIOTE interrupts.
00104  */
00105 typedef enum
00106 {
00107     NRF_GPIOTE_INT_IN0_MASK  = GPIOTE_INTENSET_IN0_Msk,  /**< GPIOTE interrupt from IN0. */
00108     NRF_GPIOTE_INT_IN1_MASK  = GPIOTE_INTENSET_IN1_Msk,  /**< GPIOTE interrupt from IN1. */
00109     NRF_GPIOTE_INT_IN2_MASK  = GPIOTE_INTENSET_IN2_Msk,  /**< GPIOTE interrupt from IN2. */
00110     NRF_GPIOTE_INT_IN3_MASK  = GPIOTE_INTENSET_IN3_Msk,  /**< GPIOTE interrupt from IN3. */
00111     NRF_GPIOTE_INT_PORT_MASK = (int)GPIOTE_INTENSET_PORT_Msk, /**< GPIOTE interrupt from PORT event. */
00112 } nrf_gpiote_int_t;
00113 
00114 #define NRF_GPIOTE_INT_IN_MASK (NRF_GPIOTE_INT_IN0_MASK | NRF_GPIOTE_INT_IN1_MASK |\
00115                                 NRF_GPIOTE_INT_IN2_MASK | NRF_GPIOTE_INT_IN3_MASK)
00116 /**
00117  * @brief Function for activating a specific GPIOTE task.
00118  *
00119  * @param[in]  task Task.
00120  */
00121 __STATIC_INLINE void nrf_gpiote_task_set(nrf_gpiote_tasks_t task);
00122 
00123 /**
00124  * @brief Function for getting the address of a specific GPIOTE task.
00125  *
00126  * @param[in] task Task.
00127  *
00128  * @returns Address.
00129  */
00130 __STATIC_INLINE uint32_t nrf_gpiote_task_addr_get(nrf_gpiote_tasks_t task);
00131 
00132 /**
00133  * @brief Function for getting the state of a specific GPIOTE event.
00134  *
00135  * @param[in] event Event.
00136  */
00137 __STATIC_INLINE bool nrf_gpiote_event_is_set(nrf_gpiote_events_t event);
00138 
00139 /**
00140  * @brief Function for clearing a specific GPIOTE event.
00141  *
00142  * @param[in] event Event.
00143  */
00144 __STATIC_INLINE void nrf_gpiote_event_clear(nrf_gpiote_events_t event);
00145 
00146 /**
00147  * @brief Function for getting the address of a specific GPIOTE event.
00148  *
00149  * @param[in] event Event.
00150  *
00151  * @return Address
00152  */
00153 __STATIC_INLINE uint32_t nrf_gpiote_event_addr_get(nrf_gpiote_events_t event);
00154 
00155 /**@brief Function for enabling interrupts.
00156  *
00157  * @param[in]  mask          Interrupt mask to be enabled.
00158  */
00159 __STATIC_INLINE void nrf_gpiote_int_enable(uint32_t mask);
00160 
00161 /**@brief Function for disabling interrupts.
00162  *
00163  * @param[in]  mask          Interrupt mask to be disabled.
00164  */
00165 __STATIC_INLINE void nrf_gpiote_int_disable(uint32_t mask);
00166 
00167 /**@brief Function for checking if interrupts are enabled.
00168  *
00169  * @param[in]  mask          Mask of interrupt flags to check.
00170  *
00171  * @return                   Mask with enabled interrupts.
00172  */
00173 __STATIC_INLINE uint32_t nrf_gpiote_int_is_enabled(uint32_t mask);
00174 
00175 /**@brief Function for enabling a GPIOTE event.
00176  *
00177  * @param[in]  idx        Task-Event index.
00178  */
00179 __STATIC_INLINE void nrf_gpiote_event_enable(uint32_t idx);
00180 
00181 /**@brief Function for disabling a GPIOTE event.
00182  *
00183  * @param[in]  idx        Task-Event index.
00184  */
00185 __STATIC_INLINE void nrf_gpiote_event_disable(uint32_t idx);
00186 
00187 /**@brief Function for configuring a GPIOTE event.
00188  *
00189  * @param[in]  idx        Task-Event index.
00190  * @param[in]  pin        Pin associated with event.
00191  * @param[in]  polarity   Transition that should generate an event.
00192  */
00193 __STATIC_INLINE void nrf_gpiote_event_configure(uint32_t idx, uint32_t pin,
00194                                                 nrf_gpiote_polarity_t polarity);
00195 
00196 /**@brief Function for getting the pin associated with a GPIOTE event.
00197  *
00198  * @param[in]  idx        Task-Event index.
00199  *
00200  * @return Pin number.
00201  */
00202 __STATIC_INLINE uint32_t nrf_gpiote_event_pin_get(uint32_t idx);
00203 
00204 /**@brief Function for getting the polarity associated with a GPIOTE event.
00205  *
00206  * @param[in]  idx        Task-Event index.
00207  *
00208  * @return Polarity.
00209  */
00210 __STATIC_INLINE nrf_gpiote_polarity_t nrf_gpiote_event_polarity_get(uint32_t idx);
00211 
00212 /**@brief Function for enabling a GPIOTE task.
00213  *
00214  * @param[in]  idx        Task-Event index.
00215  */
00216 __STATIC_INLINE void nrf_gpiote_task_enable(uint32_t idx);
00217 
00218 /**@brief Function for disabling a GPIOTE task.
00219  *
00220  * @param[in]  idx        Task-Event index.
00221  */
00222 __STATIC_INLINE void nrf_gpiote_task_disable(uint32_t idx);
00223 
00224 /**@brief Function for configuring a GPIOTE task.
00225  * @note  Function is not configuring mode field so task is disabled after this function is called.
00226  *
00227  * @param[in]  idx        Task-Event index.
00228  * @param[in]  pin        Pin associated with event.
00229  * @param[in]  polarity   Transition that should generate an event.
00230  * @param[in]  init_val   Initial value of pin.
00231  */
00232 __STATIC_INLINE void nrf_gpiote_task_configure(uint32_t idx, uint32_t pin,
00233                                                nrf_gpiote_polarity_t polarity,
00234                                                nrf_gpiote_outinit_t  init_val);
00235 
00236 /**@brief Function for forcing a specific state on the pin connected to GPIOTE.
00237  *
00238  * @param[in]  idx        Task-Event index.
00239  * @param[in]  init_val   Pin state.
00240  */
00241 __STATIC_INLINE void nrf_gpiote_task_force(uint32_t idx, nrf_gpiote_outinit_t init_val);
00242 
00243 /**@brief Function for resetting a GPIOTE task event configuration to the default state.
00244  *
00245  * @param[in]  idx        Task-Event index.
00246  */
00247 __STATIC_INLINE void nrf_gpiote_te_default(uint32_t idx);
00248 
00249 #ifndef SUPPRESS_INLINE_IMPLEMENTATION
00250 __STATIC_INLINE void nrf_gpiote_task_set(nrf_gpiote_tasks_t task)
00251 {
00252     *(__IO uint32_t *)((uint32_t)NRF_GPIOTE + task) = 0x1UL;
00253 }
00254 
00255 __STATIC_INLINE uint32_t nrf_gpiote_task_addr_get(nrf_gpiote_tasks_t task)
00256 {
00257     return ((uint32_t)NRF_GPIOTE + task);
00258 }
00259 
00260 __STATIC_INLINE bool nrf_gpiote_event_is_set(nrf_gpiote_events_t event)
00261 {
00262     return (*(uint32_t *)nrf_gpiote_event_addr_get(event) == 0x1UL) ? true : false;
00263 }
00264 
00265 __STATIC_INLINE void nrf_gpiote_event_clear(nrf_gpiote_events_t event)
00266 {
00267     *(uint32_t *)nrf_gpiote_event_addr_get(event) = 0;
00268 #if __CORTEX_M == 0x04
00269     volatile uint32_t dummy = *((volatile uint32_t *)nrf_gpiote_event_addr_get(event));
00270     (void)dummy;
00271 #endif
00272 }
00273 
00274 __STATIC_INLINE uint32_t nrf_gpiote_event_addr_get(nrf_gpiote_events_t event)
00275 {
00276     return ((uint32_t)NRF_GPIOTE + event);
00277 }
00278 
00279 __STATIC_INLINE void nrf_gpiote_int_enable(uint32_t mask)
00280 {
00281     NRF_GPIOTE->INTENSET = mask;
00282 }
00283 
00284 __STATIC_INLINE void nrf_gpiote_int_disable(uint32_t mask)
00285 {
00286     NRF_GPIOTE->INTENCLR = mask;
00287 }
00288 
00289 __STATIC_INLINE uint32_t nrf_gpiote_int_is_enabled(uint32_t mask)
00290 {
00291     return (NRF_GPIOTE->INTENSET & mask);
00292 }
00293 
00294 __STATIC_INLINE void nrf_gpiote_event_enable(uint32_t idx)
00295 {
00296    NRF_GPIOTE->CONFIG[idx] |= GPIOTE_CONFIG_MODE_Event;
00297 }
00298 
00299 __STATIC_INLINE void nrf_gpiote_event_disable(uint32_t idx)
00300 {
00301    NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_MODE_Event;
00302 }
00303 
00304 __STATIC_INLINE void nrf_gpiote_event_configure(uint32_t idx, uint32_t pin, nrf_gpiote_polarity_t polarity)
00305 {
00306   NRF_GPIOTE->CONFIG[idx] &= ~(GPIOTE_CONFIG_PSEL_Msk | GPIOTE_CONFIG_POLARITY_Msk);
00307   NRF_GPIOTE->CONFIG[idx] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) |
00308                               ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk);
00309 }
00310 
00311 __STATIC_INLINE uint32_t nrf_gpiote_event_pin_get(uint32_t idx)
00312 {
00313     return ((NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_PSEL_Msk) >> GPIOTE_CONFIG_PSEL_Pos);
00314 }
00315 
00316 __STATIC_INLINE nrf_gpiote_polarity_t nrf_gpiote_event_polarity_get(uint32_t idx)
00317 {
00318     return (nrf_gpiote_polarity_t)((NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_POLARITY_Msk) >> GPIOTE_CONFIG_POLARITY_Pos);
00319 }
00320 
00321 __STATIC_INLINE void nrf_gpiote_task_enable(uint32_t idx)
00322 {
00323     uint32_t final_config = NRF_GPIOTE->CONFIG[idx] | GPIOTE_CONFIG_MODE_Task;
00324     /* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
00325     on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the
00326     correct state in GPIOTE but not in the OUT register. */
00327     /* Configure channel to Pin31, not connected to the pin, and configure as a tasks that will set it to proper level */
00328     NRF_GPIOTE->CONFIG[idx] = final_config | ((31 << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk);
00329     __NOP();
00330     __NOP();
00331     __NOP();
00332     NRF_GPIOTE->CONFIG[idx] = final_config;
00333 }
00334 
00335 __STATIC_INLINE void nrf_gpiote_task_disable(uint32_t idx)
00336 {
00337     NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_MODE_Task;
00338 }
00339 
00340 __STATIC_INLINE void nrf_gpiote_task_configure(uint32_t idx, uint32_t pin,
00341                                                 nrf_gpiote_polarity_t polarity,
00342                                                 nrf_gpiote_outinit_t  init_val)
00343 {
00344   NRF_GPIOTE->CONFIG[idx] &= ~(GPIOTE_CONFIG_PSEL_Msk |
00345                                GPIOTE_CONFIG_POLARITY_Msk |
00346                                GPIOTE_CONFIG_OUTINIT_Msk);
00347 
00348   NRF_GPIOTE->CONFIG[idx] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) |
00349                              ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk) |
00350                              ((init_val << GPIOTE_CONFIG_OUTINIT_Pos) & GPIOTE_CONFIG_OUTINIT_Msk);
00351 }
00352 
00353 __STATIC_INLINE void nrf_gpiote_task_force(uint32_t idx, nrf_gpiote_outinit_t init_val)
00354 {
00355     NRF_GPIOTE->CONFIG[idx] = (NRF_GPIOTE->CONFIG[idx] & ~GPIOTE_CONFIG_OUTINIT_Msk) 
00356                               | ((init_val << GPIOTE_CONFIG_OUTINIT_Pos) & GPIOTE_CONFIG_OUTINIT_Msk);
00357 }
00358 
00359 __STATIC_INLINE void nrf_gpiote_te_default(uint32_t idx)
00360 {
00361     NRF_GPIOTE->CONFIG[idx] = 0;
00362 }
00363 #endif //SUPPRESS_INLINE_IMPLEMENTATION
00364 /** @} */
00365 
00366 #endif