Microbug / nRF51822

Fork of nRF51822 by Nordic Semiconductor

Committer:
finneyj
Date:
Fri May 15 12:12:21 2015 +0000
Revision:
177:7a1917171a20
Parent:
103:138bdc859cc9
bug fix - redefined symbol

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 103:138bdc859cc9 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
rgrover1 103:138bdc859cc9 2 *
rgrover1 103:138bdc859cc9 3 * The information contained herein is property of Nordic Semiconductor ASA.
rgrover1 103:138bdc859cc9 4 * Terms and conditions of usage are described in detail in NORDIC
rgrover1 103:138bdc859cc9 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
rgrover1 103:138bdc859cc9 6 *
rgrover1 103:138bdc859cc9 7 * Licensees are granted free, non-transferable use of the information. NO
rgrover1 103:138bdc859cc9 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
rgrover1 103:138bdc859cc9 9 * the file.
rgrover1 103:138bdc859cc9 10 *
rgrover1 103:138bdc859cc9 11 */
rgrover1 103:138bdc859cc9 12 #ifndef NRF_GPIOTE_H__
rgrover1 103:138bdc859cc9 13 #define NRF_GPIOTE_H__
rgrover1 103:138bdc859cc9 14
rgrover1 103:138bdc859cc9 15 #include "nrf.h"
rgrover1 103:138bdc859cc9 16
rgrover1 103:138bdc859cc9 17 /**
rgrover1 103:138bdc859cc9 18 * @defgroup nrf_gpiote GPIOTE abstraction
rgrover1 103:138bdc859cc9 19 * @{
rgrover1 103:138bdc859cc9 20 * @ingroup nrf_drivers
rgrover1 103:138bdc859cc9 21 * @brief GPIOTE abstraction for configuration of channels.
rgrover1 103:138bdc859cc9 22 */
rgrover1 103:138bdc859cc9 23
rgrover1 103:138bdc859cc9 24
rgrover1 103:138bdc859cc9 25 /**
rgrover1 103:138bdc859cc9 26 * @enum nrf_gpiote_polarity_t
rgrover1 103:138bdc859cc9 27 * @brief Polarity for GPIOTE channel enumerator.
rgrover1 103:138bdc859cc9 28 */
rgrover1 103:138bdc859cc9 29 typedef enum
rgrover1 103:138bdc859cc9 30 {
rgrover1 103:138bdc859cc9 31 NRF_GPIOTE_POLARITY_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi, ///< Low to high
rgrover1 103:138bdc859cc9 32 NRF_GPIOTE_POLARITY_HITOLO = GPIOTE_CONFIG_POLARITY_HiToLo, ///< High to low
rgrover1 103:138bdc859cc9 33 NRF_GPIOTE_POLARITY_TOGGLE = GPIOTE_CONFIG_POLARITY_Toggle ///< Toggle
rgrover1 103:138bdc859cc9 34 } nrf_gpiote_polarity_t;
rgrover1 103:138bdc859cc9 35
rgrover1 103:138bdc859cc9 36
rgrover1 103:138bdc859cc9 37 /**
rgrover1 103:138bdc859cc9 38 * @enum nrf_gpiote_outinit_t
rgrover1 103:138bdc859cc9 39 * @brief Initial output value for GPIOTE channel enumerator.
rgrover1 103:138bdc859cc9 40 */
rgrover1 103:138bdc859cc9 41 typedef enum
rgrover1 103:138bdc859cc9 42 {
rgrover1 103:138bdc859cc9 43 NRF_GPIOTE_INITIAL_VALUE_LOW = GPIOTE_CONFIG_OUTINIT_Low, ///< Low to high
rgrover1 103:138bdc859cc9 44 NRF_GPIOTE_INITIAL_VALUE_HIGH = GPIOTE_CONFIG_OUTINIT_High ///< High to low
rgrover1 103:138bdc859cc9 45 } nrf_gpiote_outinit_t;
rgrover1 103:138bdc859cc9 46
rgrover1 103:138bdc859cc9 47
rgrover1 103:138bdc859cc9 48 /**
rgrover1 103:138bdc859cc9 49 * @brief Function for configuring GPIOTE channel as output, setting the properly desired output level.
rgrover1 103:138bdc859cc9 50 *
rgrover1 103:138bdc859cc9 51 *
rgrover1 103:138bdc859cc9 52 * @param channel_number specifies the GPIOTE channel [0:3] to configure as an output channel.
rgrover1 103:138bdc859cc9 53 * @param pin_number specifies the pin number [0:30] to use in the GPIOTE channel.
rgrover1 103:138bdc859cc9 54 * @param polarity specifies the desired polarity in the output GPIOTE channel.
rgrover1 103:138bdc859cc9 55 * @param initial_value specifies the initial value of the GPIOTE channel input after the channel configuration.
rgrover1 103:138bdc859cc9 56 */
rgrover1 103:138bdc859cc9 57 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)
rgrover1 103:138bdc859cc9 58 {
rgrover1 103:138bdc859cc9 59 /* Check if the output desired is high or low */
rgrover1 103:138bdc859cc9 60 if (initial_value == NRF_GPIOTE_INITIAL_VALUE_LOW)
rgrover1 103:138bdc859cc9 61 {
rgrover1 103:138bdc859cc9 62 /* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
rgrover1 103:138bdc859cc9 63 on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the
rgrover1 103:138bdc859cc9 64 correct state in GPIOTE but not in the OUT register. */
rgrover1 103:138bdc859cc9 65 NRF_GPIO->OUTCLR = (1 << pin_number);
rgrover1 103:138bdc859cc9 66
rgrover1 103:138bdc859cc9 67 /* Configure channel to Pin31, not connected to the pin, and configure as a tasks that will set it to proper level */
rgrover1 103:138bdc859cc9 68 NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) |
rgrover1 103:138bdc859cc9 69 (31UL << GPIOTE_CONFIG_PSEL_Pos) |
rgrover1 103:138bdc859cc9 70 (GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos);
rgrover1 103:138bdc859cc9 71 }
rgrover1 103:138bdc859cc9 72 else
rgrover1 103:138bdc859cc9 73 {
rgrover1 103:138bdc859cc9 74 /* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
rgrover1 103:138bdc859cc9 75 on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the
rgrover1 103:138bdc859cc9 76 correct state in GPIOTE but not in the OUT register. */
rgrover1 103:138bdc859cc9 77 NRF_GPIO->OUTSET = (1 << pin_number);
rgrover1 103:138bdc859cc9 78
rgrover1 103:138bdc859cc9 79 /* Configure channel to Pin31, not connected to the pin, and configure as a tasks that will set it to proper level */
rgrover1 103:138bdc859cc9 80 NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) |
rgrover1 103:138bdc859cc9 81 (31UL << GPIOTE_CONFIG_PSEL_Pos) |
rgrover1 103:138bdc859cc9 82 (GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos);
rgrover1 103:138bdc859cc9 83 }
rgrover1 103:138bdc859cc9 84
rgrover1 103:138bdc859cc9 85 /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
rgrover1 103:138bdc859cc9 86 __NOP();
rgrover1 103:138bdc859cc9 87 __NOP();
rgrover1 103:138bdc859cc9 88 __NOP();
rgrover1 103:138bdc859cc9 89
rgrover1 103:138bdc859cc9 90 /* Launch the task to take the GPIOTE channel output to the desired level */
rgrover1 103:138bdc859cc9 91 NRF_GPIOTE->TASKS_OUT[channel_number] = 1;
rgrover1 103:138bdc859cc9 92
rgrover1 103:138bdc859cc9 93
rgrover1 103:138bdc859cc9 94 /* Finally configure the channel as the caller expects. If OUTINIT works, the channel is configured properly.
rgrover1 103:138bdc859cc9 95 If it does not, the channel output inheritance sets the proper level. */
rgrover1 103:138bdc859cc9 96 NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) |
rgrover1 103:138bdc859cc9 97 ((uint32_t)pin_number << GPIOTE_CONFIG_PSEL_Pos) |
rgrover1 103:138bdc859cc9 98 ((uint32_t)polarity << GPIOTE_CONFIG_POLARITY_Pos) |
rgrover1 103:138bdc859cc9 99 ((uint32_t)initial_value << GPIOTE_CONFIG_OUTINIT_Pos);
rgrover1 103:138bdc859cc9 100
rgrover1 103:138bdc859cc9 101 /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
rgrover1 103:138bdc859cc9 102 __NOP();
rgrover1 103:138bdc859cc9 103 __NOP();
rgrover1 103:138bdc859cc9 104 __NOP();
rgrover1 103:138bdc859cc9 105 }
rgrover1 103:138bdc859cc9 106
rgrover1 103:138bdc859cc9 107 /**
rgrover1 103:138bdc859cc9 108 * @brief Function for configuring GPIOTE channel as input, automatically clearing an event that appears in some cases under configuration.
rgrover1 103:138bdc859cc9 109 *
rgrover1 103:138bdc859cc9 110 * Note that when configuring the channel as input an event might be triggered. Care of disabling interrupts
rgrover1 103:138bdc859cc9 111 * for that channel is left to the user.
rgrover1 103:138bdc859cc9 112 *
rgrover1 103:138bdc859cc9 113 * @param channel_number specifies the GPIOTE channel [0:3] to configure as an input channel.
rgrover1 103:138bdc859cc9 114 * @param pin_number specifies the pin number [0:30] to use in the GPIOTE channel.
rgrover1 103:138bdc859cc9 115 * @param polarity specifies the desired polarity in the output GPIOTE channel.
rgrover1 103:138bdc859cc9 116 */
rgrover1 103:138bdc859cc9 117 static __INLINE void nrf_gpiote_event_config(uint32_t channel_number, uint32_t pin_number, nrf_gpiote_polarity_t polarity)
rgrover1 103:138bdc859cc9 118 {
rgrover1 103:138bdc859cc9 119 /* Configure the channel as the caller expects */
rgrover1 103:138bdc859cc9 120 NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos) |
rgrover1 103:138bdc859cc9 121 ((uint32_t)pin_number << GPIOTE_CONFIG_PSEL_Pos) |
rgrover1 103:138bdc859cc9 122 ((uint32_t)polarity << GPIOTE_CONFIG_POLARITY_Pos);
rgrover1 103:138bdc859cc9 123
rgrover1 103:138bdc859cc9 124 /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
rgrover1 103:138bdc859cc9 125 __NOP();
rgrover1 103:138bdc859cc9 126 __NOP();
rgrover1 103:138bdc859cc9 127 __NOP();
rgrover1 103:138bdc859cc9 128
rgrover1 103:138bdc859cc9 129 /* Clear the event that appears in some cases */
rgrover1 103:138bdc859cc9 130 NRF_GPIOTE->EVENTS_IN[channel_number] = 0;
rgrover1 103:138bdc859cc9 131 }
rgrover1 103:138bdc859cc9 132
rgrover1 103:138bdc859cc9 133
rgrover1 103:138bdc859cc9 134 /**
rgrover1 103:138bdc859cc9 135 * @brief Function for unconfiguring GPIOTE channel.
rgrover1 103:138bdc859cc9 136 *
rgrover1 103:138bdc859cc9 137 *
rgrover1 103:138bdc859cc9 138 * Note that when unconfiguring the channel, the pin is configured as GPIO PIN_CNF configuration.
rgrover1 103:138bdc859cc9 139 *
rgrover1 103:138bdc859cc9 140 * @param channel_number specifies the GPIOTE channel [0:3] to unconfigure.
rgrover1 103:138bdc859cc9 141 */
rgrover1 103:138bdc859cc9 142 static __INLINE void nrf_gpiote_unconfig(uint32_t channel_number)
rgrover1 103:138bdc859cc9 143 {
rgrover1 103:138bdc859cc9 144 /* Unonfigure the channel as the caller expects */
rgrover1 103:138bdc859cc9 145 NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Disabled << GPIOTE_CONFIG_MODE_Pos) |
rgrover1 103:138bdc859cc9 146 (31UL << GPIOTE_CONFIG_PSEL_Pos) |
rgrover1 103:138bdc859cc9 147 (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos);
rgrover1 103:138bdc859cc9 148 }
rgrover1 103:138bdc859cc9 149
rgrover1 103:138bdc859cc9 150
rgrover1 103:138bdc859cc9 151 /** @} */
rgrover1 103:138bdc859cc9 152
rgrover1 103:138bdc859cc9 153 #endif