test

Fork of nRF51822 by Nordic Semiconductor

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 
00037 /**
00038 * @defgroup nrf_gpiote GPIOTE abstraction
00039 * @{
00040 * @ingroup nrf_drivers
00041 * @brief GPIOTE abstraction for configuration of channels.
00042 */
00043 
00044 
00045  /**
00046  * @enum nrf_gpiote_polarity_t
00047  * @brief Polarity for GPIOTE channel enumerator.
00048  */
00049 typedef enum
00050 {
00051   NRF_GPIOTE_POLARITY_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi,       ///<  Low to high
00052   NRF_GPIOTE_POLARITY_HITOLO = GPIOTE_CONFIG_POLARITY_HiToLo,       ///<  High to low
00053   NRF_GPIOTE_POLARITY_TOGGLE = GPIOTE_CONFIG_POLARITY_Toggle        ///<  Toggle
00054 } nrf_gpiote_polarity_t;
00055 
00056 
00057  /**
00058  * @enum nrf_gpiote_outinit_t
00059  * @brief Initial output value for GPIOTE channel enumerator.
00060  */
00061 typedef enum
00062 {
00063   NRF_GPIOTE_INITIAL_VALUE_LOW  = GPIOTE_CONFIG_OUTINIT_Low,       ///<  Low to high
00064   NRF_GPIOTE_INITIAL_VALUE_HIGH = GPIOTE_CONFIG_OUTINIT_High       ///<  High to low
00065 } nrf_gpiote_outinit_t;
00066 
00067 
00068 /**
00069  * @brief Function for configuring GPIOTE channel as output, setting the properly desired output level.
00070  *
00071  *
00072  * @param channel_number specifies the GPIOTE channel [0:3] to configure as an output channel.
00073  * @param pin_number specifies the pin number [0:30] to use in the GPIOTE channel.
00074  * @param polarity specifies the desired polarity in the output GPIOTE channel.
00075  * @param initial_value specifies the initial value of the GPIOTE channel input after the channel configuration.
00076  */
00077 static __INLINE void nrf_gpiote_task_config(uint32_t channel_number, uint32_t pin_number, nrf_gpiote_polarity_t polarity, nrf_gpiote_outinit_t initial_value)
00078 {
00079     /* Check if the output desired is high or low */
00080     if (initial_value == NRF_GPIOTE_INITIAL_VALUE_LOW)
00081     {
00082         /* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
00083         on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the 
00084         correct state in GPIOTE but not in the OUT register. */
00085         NRF_GPIO->OUTCLR = (1 << pin_number);
00086         
00087         /* Configure channel to Pin31, not connected to the pin, and configure as a tasks that will set it to proper level */
00088         NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task       << GPIOTE_CONFIG_MODE_Pos)     |
00089                                              (31UL                          << GPIOTE_CONFIG_PSEL_Pos)     |
00090                                              (GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos);                                    
00091     } 
00092     else 
00093     {
00094         /* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
00095         on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the 
00096         correct state in GPIOTE but not in the OUT register. */
00097         NRF_GPIO->OUTSET = (1 << pin_number);
00098         
00099         /* Configure channel to Pin31, not connected to the pin, and configure as a tasks that will set it to proper level */
00100         NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task       << GPIOTE_CONFIG_MODE_Pos)     |
00101                                              (31UL                          << GPIOTE_CONFIG_PSEL_Pos)     |
00102                                              (GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos);
00103     }
00104 
00105     /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
00106     __NOP();
00107     __NOP();
00108     __NOP(); 
00109 
00110     /* Launch the task to take the GPIOTE channel output to the desired level */
00111     NRF_GPIOTE->TASKS_OUT[channel_number] = 1;
00112     
00113 
00114     /* Finally configure the channel as the caller expects. If OUTINIT works, the channel is configured properly. 
00115        If it does not, the channel output inheritance sets the proper level. */
00116     NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos)     |
00117                                          ((uint32_t)pin_number    << GPIOTE_CONFIG_PSEL_Pos)     |
00118                                          ((uint32_t)polarity      << GPIOTE_CONFIG_POLARITY_Pos) |
00119                                          ((uint32_t)initial_value << GPIOTE_CONFIG_OUTINIT_Pos);
00120 
00121     /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
00122     __NOP();
00123     __NOP();
00124     __NOP(); 
00125 }
00126 
00127 /**
00128  * @brief Function for configuring GPIOTE channel as input, automatically clearing an event that appears in some cases under configuration.
00129  *
00130  * Note that when configuring the channel as input an event might be triggered. Care of disabling interrupts
00131  * for that channel is left to the user.
00132  *
00133  * @param channel_number specifies the GPIOTE channel [0:3] to configure as an input channel.
00134  * @param pin_number specifies the pin number [0:30] to use in the GPIOTE channel.
00135  * @param polarity specifies the desired polarity in the output GPIOTE channel.
00136  */
00137 static __INLINE void nrf_gpiote_event_config(uint32_t channel_number, uint32_t pin_number, nrf_gpiote_polarity_t polarity)
00138 {   
00139     /* Configure the channel as the caller expects */
00140     NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos)     |
00141                                          ((uint32_t)pin_number     << GPIOTE_CONFIG_PSEL_Pos)     |
00142                                          ((uint32_t)polarity       << GPIOTE_CONFIG_POLARITY_Pos);
00143 
00144     /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
00145     __NOP();
00146     __NOP();
00147     __NOP();
00148     
00149     /* Clear the event that appears in some cases */
00150     NRF_GPIOTE->EVENTS_IN[channel_number] = 0; 
00151 }
00152 
00153 
00154 /**
00155  * @brief Function for unconfiguring GPIOTE channel.
00156  *
00157  *
00158  * Note that when unconfiguring the channel, the pin is configured as GPIO PIN_CNF configuration.
00159  *
00160  * @param channel_number specifies the GPIOTE channel [0:3] to unconfigure.
00161  */
00162 static __INLINE void nrf_gpiote_unconfig(uint32_t channel_number)
00163 {   
00164     /* Unonfigure the channel as the caller expects */
00165     NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Disabled   << GPIOTE_CONFIG_MODE_Pos) |
00166                                          (31UL                          << GPIOTE_CONFIG_PSEL_Pos) |
00167                                          (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos);
00168 }
00169 
00170 
00171 /** @} */
00172 
00173 #endif