Qi Yao / LinkNode---test123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

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 
00047 #define NUMBER_OF_GPIO_TE 4
00048 
00049  /**
00050  * @enum nrf_gpiote_polarity_t
00051  * @brief Polarity for the GPIOTE channel.
00052  */
00053 typedef enum
00054 {
00055   NRF_GPIOTE_POLARITY_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi,       ///<  Low to high.
00056   NRF_GPIOTE_POLARITY_HITOLO = GPIOTE_CONFIG_POLARITY_HiToLo,       ///<  High to low.
00057   NRF_GPIOTE_POLARITY_TOGGLE = GPIOTE_CONFIG_POLARITY_Toggle        ///<  Toggle.
00058 } nrf_gpiote_polarity_t;
00059 
00060 
00061  /**
00062  * @enum nrf_gpiote_outinit_t
00063  * @brief Initial output value for the GPIOTE channel.
00064  */
00065 typedef enum
00066 {
00067   NRF_GPIOTE_INITIAL_VALUE_LOW  = GPIOTE_CONFIG_OUTINIT_Low,       ///<  Low to high.
00068   NRF_GPIOTE_INITIAL_VALUE_HIGH = GPIOTE_CONFIG_OUTINIT_High       ///<  High to low.
00069 } nrf_gpiote_outinit_t;
00070 
00071 /**
00072  * @brief Tasks.
00073  */
00074 typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
00075 {
00076     NRF_GPIOTE_TASKS_OUT_0     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[0]), /**< Out task 0.*/
00077     NRF_GPIOTE_TASKS_OUT_1     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[1]), /**< Out task 1.*/
00078     NRF_GPIOTE_TASKS_OUT_2     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[2]), /**< Out task 2.*/
00079     NRF_GPIOTE_TASKS_OUT_3     = offsetof(NRF_GPIOTE_Type, TASKS_OUT[3]), /**< Out task 3.*/
00080     /*lint -restore*/
00081 } nrf_gpiote_tasks_t;
00082 
00083 /**
00084  * @brief Events.
00085  */
00086 typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
00087 {
00088     NRF_GPIOTE_EVENTS_IN_0     = offsetof(NRF_GPIOTE_Type, EVENTS_IN[0]), /**< In event 0.*/
00089     NRF_GPIOTE_EVENTS_IN_1     = offsetof(NRF_GPIOTE_Type, EVENTS_IN[1]), /**< In event 1.*/
00090     NRF_GPIOTE_EVENTS_IN_2     = offsetof(NRF_GPIOTE_Type, EVENTS_IN[2]), /**< In event 2.*/
00091     NRF_GPIOTE_EVENTS_IN_3     = offsetof(NRF_GPIOTE_Type, EVENTS_IN[3]), /**< In event 3.*/
00092     NRF_GPIOTE_EVENTS_PORT     = offsetof(NRF_GPIOTE_Type, EVENTS_PORT), /**<  Port event.*/
00093     /*lint -restore*/
00094 } nrf_gpiote_events_t;
00095 
00096 /**
00097  * @enum nrf_gpiote_int_t
00098  * @brief GPIOTE interrupts.
00099  */
00100 typedef enum
00101 {
00102     NRF_GPIOTE_INT_IN0_MASK  = GPIOTE_INTENSET_IN0_Msk,  /**< GPIOTE interrupt from IN0. */
00103     NRF_GPIOTE_INT_IN1_MASK  = GPIOTE_INTENSET_IN1_Msk,  /**< GPIOTE interrupt from IN1. */
00104     NRF_GPIOTE_INT_IN2_MASK  = GPIOTE_INTENSET_IN2_Msk,  /**< GPIOTE interrupt from IN2. */
00105     NRF_GPIOTE_INT_IN3_MASK  = GPIOTE_INTENSET_IN3_Msk,  /**< GPIOTE interrupt from IN3. */
00106     NRF_GPIOTE_INT_PORT_MASK = (int)GPIOTE_INTENSET_PORT_Msk, /**< GPIOTE interrupt from PORT event. */
00107 } nrf_gpiote_int_t;
00108 
00109 #define NRF_GPIOTE_INT_IN_MASK (NRF_GPIOTE_INT_IN0_MASK | NRF_GPIOTE_INT_IN1_MASK |\
00110                                 NRF_GPIOTE_INT_IN2_MASK | NRF_GPIOTE_INT_IN3_MASK)
00111 /**
00112  * @brief Function for activating a specific GPIOTE task.
00113  *
00114  * @param[in]  task Task.
00115  */
00116 __STATIC_INLINE void nrf_gpiote_task_set(nrf_gpiote_tasks_t task);
00117 
00118 /**
00119  * @brief Function for getting the address of a specific GPIOTE task.
00120  *
00121  * @param[in] task Task.
00122  *
00123  * @returns Address.
00124  */
00125 __STATIC_INLINE uint32_t nrf_gpiote_task_addr_get(nrf_gpiote_tasks_t task);
00126 
00127 /**
00128  * @brief Function for getting the state of a specific GPIOTE event.
00129  *
00130  * @param[in] event Event.
00131  */
00132 __STATIC_INLINE bool nrf_gpiote_event_is_set(nrf_gpiote_events_t event);
00133 
00134 /**
00135  * @brief Function for clearing a specific GPIOTE event.
00136  *
00137  * @param[in] event Event.
00138  */
00139 __STATIC_INLINE void nrf_gpiote_event_clear(nrf_gpiote_events_t event);
00140 
00141 /**
00142  * @brief Function for getting the address of a specific GPIOTE event.
00143  *
00144  * @param[in] event Event.
00145  *
00146  * @return Address
00147  */
00148 __STATIC_INLINE uint32_t nrf_gpiote_event_addr_get(nrf_gpiote_events_t event);
00149 
00150 /**@brief Function for enabling interrupts.
00151  *
00152  * @param[in]  mask          Interrupt mask to be enabled.
00153  */
00154 __STATIC_INLINE void nrf_gpiote_int_enable(uint32_t mask);
00155 
00156 /**@brief Function for disabling interrupts.
00157  *
00158  * @param[in]  mask          Interrupt mask to be disabled.
00159  */
00160 __STATIC_INLINE void nrf_gpiote_int_disable(uint32_t mask);
00161 
00162 /**@brief Function for checking if interrupts are enabled.
00163  *
00164  * @param[in]  mask          Mask of interrupt flags to check.
00165  *
00166  * @return                   Mask with enabled interrupts.
00167  */
00168 __STATIC_INLINE uint32_t nrf_gpiote_int_is_enabled(uint32_t mask);
00169 
00170 /**@brief Function for enabling a GPIOTE event.
00171  *
00172  * @param[in]  idx        Task-Event index.
00173  */
00174 __STATIC_INLINE void nrf_gpiote_event_enable(uint32_t idx);
00175 
00176 /**@brief Function for disabling a GPIOTE event.
00177  *
00178  * @param[in]  idx        Task-Event index.
00179  */
00180 __STATIC_INLINE void nrf_gpiote_event_disable(uint32_t idx);
00181 
00182 /**@brief Function for configuring a GPIOTE event.
00183  *
00184  * @param[in]  idx        Task-Event index.
00185  * @param[in]  pin        Pin associated with event.
00186  * @param[in]  polarity   Transition that should generate an event.
00187  */
00188 __STATIC_INLINE void nrf_gpiote_event_configure(uint32_t idx, uint32_t pin,
00189                                                 nrf_gpiote_polarity_t polarity);
00190 
00191 /**@brief Function for getting the pin associated with a GPIOTE event.
00192  *
00193  * @param[in]  idx        Task-Event index.
00194  *
00195  * @return Pin number.
00196  */
00197 __STATIC_INLINE uint32_t nrf_gpiote_event_pin_get(uint32_t idx);
00198 
00199 /**@brief Function for getting the polarity associated with a GPIOTE event.
00200  *
00201  * @param[in]  idx        Task-Event index.
00202  *
00203  * @return Polarity.
00204  */
00205 __STATIC_INLINE nrf_gpiote_polarity_t nrf_gpiote_event_polarity_get(uint32_t idx);
00206 
00207 /**@brief Function for enabling a GPIOTE task.
00208  *
00209  * @param[in]  idx        Task-Event index.
00210  */
00211 __STATIC_INLINE void nrf_gpiote_task_enable(uint32_t idx);
00212 
00213 /**@brief Function for disabling a GPIOTE task.
00214  *
00215  * @param[in]  idx        Task-Event index.
00216  */
00217 __STATIC_INLINE void nrf_gpiote_task_disable(uint32_t idx);
00218 
00219 /**@brief Function for configuring a GPIOTE task.
00220  *
00221  * @param[in]  idx        Task-Event index.
00222  * @param[in]  pin        Pin associated with event.
00223  * @param[in]  polarity   Transition that should generate an event.
00224  * @param[in]  init_val   Initial value of pin.
00225  */
00226 __STATIC_INLINE void nrf_gpiote_task_configure(uint32_t idx, uint32_t pin,
00227                                                nrf_gpiote_polarity_t polarity,
00228                                                nrf_gpiote_outinit_t  init_val);
00229 
00230 /**@brief Function for forcing a specific state on the pin connected to GPIOTE.
00231  *
00232  * @param[in]  idx        Task-Event index.
00233  * @param[in]  init_val   Pin state.
00234  */
00235 __STATIC_INLINE void nrf_gpiote_task_force(uint32_t idx, nrf_gpiote_outinit_t init_val);
00236 
00237 /**@brief Function for resetting a GPIOTE task event configuration to the default state.
00238  *
00239  * @param[in]  idx        Task-Event index.
00240  */
00241 __STATIC_INLINE void nrf_gpiote_te_default(uint32_t idx);
00242 
00243 #ifndef SUPPRESS_INLINE_IMPLEMENTATION
00244 __STATIC_INLINE void nrf_gpiote_task_set(nrf_gpiote_tasks_t task)
00245 {
00246     *(__IO uint32_t *)((uint32_t)NRF_GPIOTE + task) = 0x1UL;
00247 }
00248 
00249 __STATIC_INLINE uint32_t nrf_gpiote_task_addr_get(nrf_gpiote_tasks_t task)
00250 {
00251     return ((uint32_t)NRF_GPIOTE + task);
00252 }
00253 
00254 __STATIC_INLINE bool nrf_gpiote_event_is_set(nrf_gpiote_events_t event)
00255 {
00256     return (*(uint32_t *)nrf_gpiote_event_addr_get(event) == 0x1UL) ? true : false;
00257 }
00258 
00259 __STATIC_INLINE void nrf_gpiote_event_clear(nrf_gpiote_events_t event)
00260 {
00261     *(uint32_t *)nrf_gpiote_event_addr_get(event) = 0;
00262 }
00263 
00264 __STATIC_INLINE uint32_t nrf_gpiote_event_addr_get(nrf_gpiote_events_t event)
00265 {
00266     return ((uint32_t)NRF_GPIOTE + event);
00267 }
00268 
00269 __STATIC_INLINE void nrf_gpiote_int_enable(uint32_t mask)
00270 {
00271     NRF_GPIOTE->INTENSET = mask;
00272 }
00273 
00274 __STATIC_INLINE void nrf_gpiote_int_disable(uint32_t mask)
00275 {
00276     NRF_GPIOTE->INTENCLR = mask;
00277 }
00278 
00279 __STATIC_INLINE uint32_t nrf_gpiote_int_is_enabled(uint32_t mask)
00280 {
00281     return (NRF_GPIOTE->INTENSET & mask);
00282 }
00283 
00284 __STATIC_INLINE void nrf_gpiote_event_enable(uint32_t idx)
00285 {
00286    NRF_GPIOTE->CONFIG[idx] |= GPIOTE_CONFIG_MODE_Event;
00287 }
00288 
00289 __STATIC_INLINE void nrf_gpiote_event_disable(uint32_t idx)
00290 {
00291    NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_MODE_Event;
00292 }
00293 
00294 __STATIC_INLINE void nrf_gpiote_event_configure(uint32_t idx, uint32_t pin, nrf_gpiote_polarity_t polarity)
00295 {
00296   NRF_GPIOTE->CONFIG[idx] &= ~(GPIOTE_CONFIG_PSEL_Msk | GPIOTE_CONFIG_POLARITY_Msk);
00297   NRF_GPIOTE->CONFIG[idx] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) |
00298                               ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk);
00299 }
00300 
00301 __STATIC_INLINE uint32_t nrf_gpiote_event_pin_get(uint32_t idx)
00302 {
00303     return ((NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_PSEL_Msk) >> GPIOTE_CONFIG_PSEL_Pos);
00304 }
00305 
00306 __STATIC_INLINE nrf_gpiote_polarity_t nrf_gpiote_event_polarity_get(uint32_t idx)
00307 {
00308     return (nrf_gpiote_polarity_t)((NRF_GPIOTE->CONFIG[idx] & GPIOTE_CONFIG_POLARITY_Msk) >> GPIOTE_CONFIG_POLARITY_Pos);
00309 }
00310 
00311 __STATIC_INLINE void nrf_gpiote_task_enable(uint32_t idx)
00312 {
00313     uint32_t final_config = NRF_GPIOTE->CONFIG[idx] | GPIOTE_CONFIG_MODE_Task;
00314     /* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
00315     on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the
00316     correct state in GPIOTE but not in the OUT register. */
00317     /* Configure channel to Pin31, not connected to the pin, and configure as a tasks that will set it to proper level */
00318     NRF_GPIOTE->CONFIG[idx] = final_config | ((31 << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk);
00319     __NOP();
00320     __NOP();
00321     __NOP();
00322     NRF_GPIOTE->CONFIG[idx] = final_config;
00323 }
00324 
00325 __STATIC_INLINE void nrf_gpiote_task_disable(uint32_t idx)
00326 {
00327     NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_MODE_Task;
00328 }
00329 
00330 __STATIC_INLINE void nrf_gpiote_task_configure(uint32_t idx, uint32_t pin,
00331                                                 nrf_gpiote_polarity_t polarity,
00332                                                 nrf_gpiote_outinit_t  init_val)
00333 {
00334   NRF_GPIOTE->CONFIG[idx] &= ~(GPIOTE_CONFIG_PSEL_Msk |
00335                                GPIOTE_CONFIG_POLARITY_Msk |
00336                                GPIOTE_CONFIG_OUTINIT_Msk);
00337 
00338   NRF_GPIOTE->CONFIG[idx] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) |
00339                              ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk) |
00340                              ((init_val << GPIOTE_CONFIG_OUTINIT_Pos) & GPIOTE_CONFIG_OUTINIT_Msk);
00341 }
00342 
00343 __STATIC_INLINE void nrf_gpiote_task_force(uint32_t idx, nrf_gpiote_outinit_t init_val)
00344 {
00345   NRF_GPIOTE->CONFIG[idx] &= ~GPIOTE_CONFIG_OUTINIT_Msk;
00346   NRF_GPIOTE->CONFIG[idx] |= (init_val << GPIOTE_CONFIG_OUTINIT_Pos) & GPIOTE_CONFIG_OUTINIT_Msk;
00347 }
00348 
00349 __STATIC_INLINE void nrf_gpiote_te_default(uint32_t idx)
00350 {
00351     NRF_GPIOTE->CONFIG[idx] = 0;
00352 }
00353 #endif //SUPPRESS_INLINE_IMPLEMENTATION
00354 /** @} */
00355 
00356 #endif