LIS3DH & BLE broadcast example for VTT Node V3 & mbed

Dependencies:   BLE_API TMP_nrf51 mbed nRF51822

Committer:
jejuho
Date:
Mon Jan 25 13:27:15 2016 +0000
Revision:
1:bd7fd35251ab
Parent:
0:de3e4a57ebe0
Initial version.

Who changed what in which revision?

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