anyThing Connected Team / mbed-dev

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Fri May 26 12:39:01 2017 +0100
Revision:
165:e614a9f1c9e2
Parent:
targets/TARGET_NORDIC/TARGET_NRF5_SDK13/sdk/drivers_nrf/qdec/nrf_drv_qdec.c@162:e13f6fdb2ac4
This updates the lib to the mbed lib v 143

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 162:e13f6fdb2ac4 1 /*
<> 162:e13f6fdb2ac4 2 * Copyright (c) 2015 Nordic Semiconductor ASA
<> 162:e13f6fdb2ac4 3 * All rights reserved.
<> 162:e13f6fdb2ac4 4 *
<> 162:e13f6fdb2ac4 5 * Redistribution and use in source and binary forms, with or without modification,
<> 162:e13f6fdb2ac4 6 * are permitted provided that the following conditions are met:
<> 162:e13f6fdb2ac4 7 *
<> 162:e13f6fdb2ac4 8 * 1. Redistributions of source code must retain the above copyright notice, this list
<> 162:e13f6fdb2ac4 9 * of conditions and the following disclaimer.
<> 162:e13f6fdb2ac4 10 *
<> 162:e13f6fdb2ac4 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
<> 162:e13f6fdb2ac4 12 * integrated circuit in a product or a software update for such product, must reproduce
<> 162:e13f6fdb2ac4 13 * the above copyright notice, this list of conditions and the following disclaimer in
<> 162:e13f6fdb2ac4 14 * the documentation and/or other materials provided with the distribution.
<> 162:e13f6fdb2ac4 15 *
<> 162:e13f6fdb2ac4 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
<> 162:e13f6fdb2ac4 17 * used to endorse or promote products derived from this software without specific prior
<> 162:e13f6fdb2ac4 18 * written permission.
<> 162:e13f6fdb2ac4 19 *
<> 162:e13f6fdb2ac4 20 * 4. This software, with or without modification, must only be used with a
<> 162:e13f6fdb2ac4 21 * Nordic Semiconductor ASA integrated circuit.
<> 162:e13f6fdb2ac4 22 *
<> 162:e13f6fdb2ac4 23 * 5. Any software provided in binary or object form under this license must not be reverse
<> 162:e13f6fdb2ac4 24 * engineered, decompiled, modified and/or disassembled.
<> 162:e13f6fdb2ac4 25 *
<> 162:e13f6fdb2ac4 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
<> 162:e13f6fdb2ac4 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
<> 162:e13f6fdb2ac4 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 162:e13f6fdb2ac4 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
<> 162:e13f6fdb2ac4 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
<> 162:e13f6fdb2ac4 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
<> 162:e13f6fdb2ac4 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
<> 162:e13f6fdb2ac4 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<> 162:e13f6fdb2ac4 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
<> 162:e13f6fdb2ac4 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 162:e13f6fdb2ac4 36 *
<> 162:e13f6fdb2ac4 37 */
<> 162:e13f6fdb2ac4 38
<> 162:e13f6fdb2ac4 39 #include "sdk_common.h"
<> 162:e13f6fdb2ac4 40 #if NRF_MODULE_ENABLED(QDEC)
<> 162:e13f6fdb2ac4 41 #include <stdint.h>
<> 162:e13f6fdb2ac4 42 #include <stddef.h>
<> 162:e13f6fdb2ac4 43
<> 162:e13f6fdb2ac4 44 #include "nrf.h"
<> 162:e13f6fdb2ac4 45 #include "nrf_gpio.h"
<> 162:e13f6fdb2ac4 46 #include "nrf_error.h"
<> 162:e13f6fdb2ac4 47 #include "nrf_assert.h"
<> 162:e13f6fdb2ac4 48 #include "nrf_drv_common.h"
<> 162:e13f6fdb2ac4 49 #include "nrf_drv_qdec.h"
<> 162:e13f6fdb2ac4 50 #include "app_util_platform.h"
<> 162:e13f6fdb2ac4 51 #include "nrf_assert.h"
<> 162:e13f6fdb2ac4 52
<> 162:e13f6fdb2ac4 53 #define NRF_LOG_MODULE_NAME "QDEC"
<> 162:e13f6fdb2ac4 54
<> 162:e13f6fdb2ac4 55 #if QDEC_CONFIG_LOG_ENABLED
<> 162:e13f6fdb2ac4 56 #define NRF_LOG_LEVEL QDEC_CONFIG_LOG_LEVEL
<> 162:e13f6fdb2ac4 57 #define NRF_LOG_INFO_COLOR QDEC_CONFIG_INFO_COLOR
<> 162:e13f6fdb2ac4 58 #define NRF_LOG_DEBUG_COLOR QDEC_CONFIG_DEBUG_COLOR
<> 162:e13f6fdb2ac4 59 #define EVT_TO_STR(event) (event == NRF_QDEC_EVENT_SAMPLERDY ? "NRF_QDEC_EVENT_SAMPLERDY" : \
<> 162:e13f6fdb2ac4 60 (event == NRF_QDEC_EVENT_REPORTRDY ? "NRF_QDEC_EVENT_REPORTRDY" : \
<> 162:e13f6fdb2ac4 61 (event == NRF_QDEC_EVENT_ACCOF ? "NRF_QDEC_EVENT_ACCOF" : "UNKNOWN EVENT")))
<> 162:e13f6fdb2ac4 62 #else //QDEC_CONFIG_LOG_ENABLED
<> 162:e13f6fdb2ac4 63 #define EVT_TO_STR(event) ""
<> 162:e13f6fdb2ac4 64 #define NRF_LOG_LEVEL 0
<> 162:e13f6fdb2ac4 65 #endif //QDEC_CONFIG_LOG_ENABLED
<> 162:e13f6fdb2ac4 66 #include "nrf_log.h"
<> 162:e13f6fdb2ac4 67 #include "nrf_log_ctrl.h"
<> 162:e13f6fdb2ac4 68
<> 162:e13f6fdb2ac4 69
<> 162:e13f6fdb2ac4 70 static qdec_event_handler_t m_qdec_event_handler = NULL;
<> 162:e13f6fdb2ac4 71 static const nrf_drv_qdec_config_t m_default_config = NRF_DRV_QDEC_DEFAULT_CONFIG;
<> 162:e13f6fdb2ac4 72 static nrf_drv_state_t m_state = NRF_DRV_STATE_UNINITIALIZED;
<> 162:e13f6fdb2ac4 73
<> 162:e13f6fdb2ac4 74 void QDEC_IRQHandler(void)
<> 162:e13f6fdb2ac4 75 {
<> 162:e13f6fdb2ac4 76 nrf_drv_qdec_event_t event;
<> 162:e13f6fdb2ac4 77 if ( nrf_qdec_event_check(NRF_QDEC_EVENT_SAMPLERDY) &&
<> 162:e13f6fdb2ac4 78 nrf_qdec_int_enable_check(NRF_QDEC_INT_SAMPLERDY_MASK) )
<> 162:e13f6fdb2ac4 79 {
<> 162:e13f6fdb2ac4 80 nrf_qdec_event_clear(NRF_QDEC_EVENT_SAMPLERDY);
<> 162:e13f6fdb2ac4 81 NRF_LOG_DEBUG("Event: %s.\r\n", (uint32_t)EVT_TO_STR(NRF_QDEC_EVENT_SAMPLERDY));
<> 162:e13f6fdb2ac4 82
<> 162:e13f6fdb2ac4 83 event.type = NRF_QDEC_EVENT_SAMPLERDY;
<> 162:e13f6fdb2ac4 84 event.data.sample.value = (int8_t)nrf_qdec_sample_get();
<> 162:e13f6fdb2ac4 85 m_qdec_event_handler(event);
<> 162:e13f6fdb2ac4 86 }
<> 162:e13f6fdb2ac4 87
<> 162:e13f6fdb2ac4 88 if ( nrf_qdec_event_check(NRF_QDEC_EVENT_REPORTRDY) &&
<> 162:e13f6fdb2ac4 89 nrf_qdec_int_enable_check(NRF_QDEC_INT_REPORTRDY_MASK) )
<> 162:e13f6fdb2ac4 90 {
<> 162:e13f6fdb2ac4 91 nrf_qdec_event_clear(NRF_QDEC_EVENT_REPORTRDY);
<> 162:e13f6fdb2ac4 92 NRF_LOG_DEBUG("Event: %s.\r\n", (uint32_t)EVT_TO_STR(NRF_QDEC_INT_REPORTRDY_MASK));
<> 162:e13f6fdb2ac4 93
<> 162:e13f6fdb2ac4 94 event.type = NRF_QDEC_EVENT_REPORTRDY;
<> 162:e13f6fdb2ac4 95
<> 162:e13f6fdb2ac4 96 event.data.report.acc = (int16_t)nrf_qdec_accread_get();
<> 162:e13f6fdb2ac4 97 event.data.report.accdbl = (uint16_t)nrf_qdec_accdblread_get();
<> 162:e13f6fdb2ac4 98 m_qdec_event_handler(event);
<> 162:e13f6fdb2ac4 99 }
<> 162:e13f6fdb2ac4 100
<> 162:e13f6fdb2ac4 101 if ( nrf_qdec_event_check(NRF_QDEC_EVENT_ACCOF) &&
<> 162:e13f6fdb2ac4 102 nrf_qdec_int_enable_check(NRF_QDEC_INT_ACCOF_MASK) )
<> 162:e13f6fdb2ac4 103 {
<> 162:e13f6fdb2ac4 104 nrf_qdec_event_clear(NRF_QDEC_EVENT_ACCOF);
<> 162:e13f6fdb2ac4 105 NRF_LOG_DEBUG("Event: %s.\r\n", (uint32_t)EVT_TO_STR(NRF_QDEC_EVENT_ACCOF));
<> 162:e13f6fdb2ac4 106
<> 162:e13f6fdb2ac4 107 event.type = NRF_QDEC_EVENT_ACCOF;
<> 162:e13f6fdb2ac4 108 m_qdec_event_handler(event);
<> 162:e13f6fdb2ac4 109 }
<> 162:e13f6fdb2ac4 110 }
<> 162:e13f6fdb2ac4 111
<> 162:e13f6fdb2ac4 112
<> 162:e13f6fdb2ac4 113 ret_code_t nrf_drv_qdec_init(const nrf_drv_qdec_config_t * p_config,
<> 162:e13f6fdb2ac4 114 qdec_event_handler_t event_handler)
<> 162:e13f6fdb2ac4 115 {
<> 162:e13f6fdb2ac4 116 ret_code_t err_code;
<> 162:e13f6fdb2ac4 117
<> 162:e13f6fdb2ac4 118 if (m_state != NRF_DRV_STATE_UNINITIALIZED)
<> 162:e13f6fdb2ac4 119 {
<> 162:e13f6fdb2ac4 120 err_code = NRF_ERROR_INVALID_STATE;
<> 162:e13f6fdb2ac4 121 NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code));
<> 162:e13f6fdb2ac4 122 return err_code;
<> 162:e13f6fdb2ac4 123 }
<> 162:e13f6fdb2ac4 124
<> 162:e13f6fdb2ac4 125 if (p_config == NULL)
<> 162:e13f6fdb2ac4 126 {
<> 162:e13f6fdb2ac4 127 p_config = &m_default_config;
<> 162:e13f6fdb2ac4 128 }
<> 162:e13f6fdb2ac4 129
<> 162:e13f6fdb2ac4 130 if (event_handler)
<> 162:e13f6fdb2ac4 131 {
<> 162:e13f6fdb2ac4 132 m_qdec_event_handler = event_handler;
<> 162:e13f6fdb2ac4 133 }
<> 162:e13f6fdb2ac4 134 else
<> 162:e13f6fdb2ac4 135 {
<> 162:e13f6fdb2ac4 136 err_code = NRF_ERROR_INVALID_PARAM;
<> 162:e13f6fdb2ac4 137 NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code));
<> 162:e13f6fdb2ac4 138 return err_code;
<> 162:e13f6fdb2ac4 139 }
<> 162:e13f6fdb2ac4 140
<> 162:e13f6fdb2ac4 141 nrf_qdec_sampleper_set(p_config->sampleper);
<> 162:e13f6fdb2ac4 142 nrf_gpio_cfg_input(p_config->pselled, NRF_GPIO_PIN_NOPULL);
<> 162:e13f6fdb2ac4 143 nrf_gpio_cfg_input(p_config->psela, NRF_GPIO_PIN_NOPULL);
<> 162:e13f6fdb2ac4 144 nrf_gpio_cfg_input(p_config->pselb, NRF_GPIO_PIN_NOPULL);
<> 162:e13f6fdb2ac4 145 nrf_qdec_pio_assign( p_config->psela, p_config->pselb, p_config->pselled);
<> 162:e13f6fdb2ac4 146 nrf_qdec_ledpre_set(p_config->ledpre);
<> 162:e13f6fdb2ac4 147 nrf_qdec_ledpol_set(p_config->ledpol);
<> 162:e13f6fdb2ac4 148 nrf_qdec_shorts_enable(NRF_QDEC_SHORT_REPORTRDY_READCLRACC_MASK);
<> 162:e13f6fdb2ac4 149
<> 162:e13f6fdb2ac4 150 if (p_config->dbfen)
<> 162:e13f6fdb2ac4 151 {
<> 162:e13f6fdb2ac4 152 nrf_qdec_dbfen_enable();
<> 162:e13f6fdb2ac4 153 }
<> 162:e13f6fdb2ac4 154 else
<> 162:e13f6fdb2ac4 155 {
<> 162:e13f6fdb2ac4 156 nrf_qdec_dbfen_disable();
<> 162:e13f6fdb2ac4 157 }
<> 162:e13f6fdb2ac4 158
<> 162:e13f6fdb2ac4 159 uint32_t int_mask = NRF_QDEC_INT_ACCOF_MASK;
<> 162:e13f6fdb2ac4 160
<> 162:e13f6fdb2ac4 161 if (p_config->reportper != NRF_QDEC_REPORTPER_DISABLED)
<> 162:e13f6fdb2ac4 162 {
<> 162:e13f6fdb2ac4 163 nrf_qdec_reportper_set(p_config->reportper);
<> 162:e13f6fdb2ac4 164 int_mask |= NRF_QDEC_INT_REPORTRDY_MASK;
<> 162:e13f6fdb2ac4 165 }
<> 162:e13f6fdb2ac4 166
<> 162:e13f6fdb2ac4 167 if (p_config->sample_inten)
<> 162:e13f6fdb2ac4 168 {
<> 162:e13f6fdb2ac4 169 int_mask |= NRF_QDEC_INT_SAMPLERDY_MASK;
<> 162:e13f6fdb2ac4 170 }
<> 162:e13f6fdb2ac4 171
<> 162:e13f6fdb2ac4 172 nrf_qdec_int_enable(int_mask);
<> 162:e13f6fdb2ac4 173 nrf_drv_common_irq_enable(QDEC_IRQn, p_config->interrupt_priority);
<> 162:e13f6fdb2ac4 174
<> 162:e13f6fdb2ac4 175 m_state = NRF_DRV_STATE_INITIALIZED;
<> 162:e13f6fdb2ac4 176
<> 162:e13f6fdb2ac4 177 err_code = NRF_SUCCESS;
<> 162:e13f6fdb2ac4 178 NRF_LOG_INFO("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code));
<> 162:e13f6fdb2ac4 179 return err_code;
<> 162:e13f6fdb2ac4 180 }
<> 162:e13f6fdb2ac4 181
<> 162:e13f6fdb2ac4 182 void nrf_drv_qdec_uninit(void)
<> 162:e13f6fdb2ac4 183 {
<> 162:e13f6fdb2ac4 184 ASSERT(m_state != NRF_DRV_STATE_UNINITIALIZED);
<> 162:e13f6fdb2ac4 185 nrf_drv_qdec_disable();
<> 162:e13f6fdb2ac4 186 nrf_drv_common_irq_disable(QDEC_IRQn);
<> 162:e13f6fdb2ac4 187 m_state = NRF_DRV_STATE_UNINITIALIZED;
<> 162:e13f6fdb2ac4 188 NRF_LOG_INFO("Uninitialized.\r\n");
<> 162:e13f6fdb2ac4 189 }
<> 162:e13f6fdb2ac4 190
<> 162:e13f6fdb2ac4 191 void nrf_drv_qdec_enable(void)
<> 162:e13f6fdb2ac4 192 {
<> 162:e13f6fdb2ac4 193 ASSERT(m_state == NRF_DRV_STATE_INITIALIZED);
<> 162:e13f6fdb2ac4 194 nrf_qdec_enable();
<> 162:e13f6fdb2ac4 195 nrf_qdec_task_trigger(NRF_QDEC_TASK_START);
<> 162:e13f6fdb2ac4 196 m_state = NRF_DRV_STATE_POWERED_ON;
<> 162:e13f6fdb2ac4 197 NRF_LOG_INFO("Enabled.\r\n");
<> 162:e13f6fdb2ac4 198 }
<> 162:e13f6fdb2ac4 199
<> 162:e13f6fdb2ac4 200 void nrf_drv_qdec_disable(void)
<> 162:e13f6fdb2ac4 201 {
<> 162:e13f6fdb2ac4 202 ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
<> 162:e13f6fdb2ac4 203 nrf_qdec_task_trigger(NRF_QDEC_TASK_STOP);
<> 162:e13f6fdb2ac4 204 nrf_qdec_disable();
<> 162:e13f6fdb2ac4 205 m_state = NRF_DRV_STATE_INITIALIZED;
<> 162:e13f6fdb2ac4 206 NRF_LOG_INFO("Disabled.\r\n");
<> 162:e13f6fdb2ac4 207 }
<> 162:e13f6fdb2ac4 208
<> 162:e13f6fdb2ac4 209 void nrf_drv_qdec_accumulators_read(int16_t * p_acc, int16_t * p_accdbl)
<> 162:e13f6fdb2ac4 210 {
<> 162:e13f6fdb2ac4 211 ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
<> 162:e13f6fdb2ac4 212 nrf_qdec_task_trigger(NRF_QDEC_TASK_READCLRACC);
<> 162:e13f6fdb2ac4 213
<> 162:e13f6fdb2ac4 214 *p_acc = (int16_t)nrf_qdec_accread_get();
<> 162:e13f6fdb2ac4 215 *p_accdbl = (int16_t)nrf_qdec_accdblread_get();
<> 162:e13f6fdb2ac4 216
<> 162:e13f6fdb2ac4 217 NRF_LOG_DEBUG("Accumulators data, ACC register:\r\n");
<> 162:e13f6fdb2ac4 218 NRF_LOG_HEXDUMP_DEBUG((uint8_t *)p_acc, sizeof(p_acc));
<> 162:e13f6fdb2ac4 219 NRF_LOG_DEBUG("Accumulators data, ACCDBL register:\r\n");
<> 162:e13f6fdb2ac4 220 NRF_LOG_HEXDUMP_DEBUG((uint8_t *)p_accdbl, sizeof(p_accdbl));
<> 162:e13f6fdb2ac4 221 }
<> 162:e13f6fdb2ac4 222
<> 162:e13f6fdb2ac4 223 void nrf_drv_qdec_task_address_get(nrf_qdec_task_t task, uint32_t * p_task)
<> 162:e13f6fdb2ac4 224 {
<> 162:e13f6fdb2ac4 225 *p_task = (uint32_t)nrf_qdec_task_address_get(task);
<> 162:e13f6fdb2ac4 226 }
<> 162:e13f6fdb2ac4 227
<> 162:e13f6fdb2ac4 228 void nrf_drv_qdec_event_address_get(nrf_qdec_event_t event, uint32_t * p_event)
<> 162:e13f6fdb2ac4 229 {
<> 162:e13f6fdb2ac4 230 *p_event = (uint32_t)nrf_qdec_event_address_get(event);
<> 162:e13f6fdb2ac4 231 }
<> 162:e13f6fdb2ac4 232
<> 162:e13f6fdb2ac4 233 #endif //NRF_MODULE_ENABLED(QDEC)