R1 code for micro:bit based train controller code, requires second micro:bit running rx code to operate - see https://meanderingpi.wordpress.com/ for more information

Fork of nrf51-sdk by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nrf_wdt.h Source File

nrf_wdt.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 
00033 /**
00034  * @defgroup nrf_wdt_hal WDT HAL
00035  * @{
00036  * @ingroup nrf_wdt
00037  *
00038  * @brief Hardware access layer for accessing the watchdog timer (WDT) peripheral.
00039  */
00040 
00041 #ifndef NRF_WDT_H__
00042 #define NRF_WDT_H__
00043 
00044 #include <stddef.h>
00045 #include <stdbool.h>
00046 #include <stdint.h>
00047 
00048 #include "nrf.h"
00049 
00050 #define NRF_WDT_CHANNEL_NUMBER 0x8UL
00051 #define NRF_WDT_RR_VALUE       0x6E524635UL /* Fixed value, shouldn't be modified.*/
00052 
00053 #define NRF_WDT_TASK_SET       1UL
00054 #define NRF_WDT_EVENT_CLEAR    0UL
00055 
00056 /**
00057  * @enum nrf_wdt_task_t
00058  * @brief WDT tasks.
00059  */
00060 typedef enum
00061 {
00062     /*lint -save -e30 -esym(628,__INTADDR__)*/
00063     NRF_WDT_TASK_START = offsetof(NRF_WDT_Type, TASKS_START), /**< Task for starting WDT. */
00064     /*lint -restore*/
00065 } nrf_wdt_task_t;
00066 
00067 /**
00068  * @enum nrf_wdt_event_t
00069  * @brief WDT events.
00070  */
00071 typedef enum
00072 {
00073     /*lint -save -e30*/
00074     NRF_WDT_EVENT_TIMEOUT = offsetof(NRF_WDT_Type, EVENTS_TIMEOUT), /**< Event from WDT time-out. */
00075     /*lint -restore*/
00076 } nrf_wdt_event_t;
00077 
00078 /**
00079  * @enum nrf_wdt_behaviour_t
00080  * @brief WDT behavior in CPU SLEEP or HALT mode.
00081  */
00082 typedef enum
00083 {
00084     NRF_WDT_BEHAVIOUR_RUN_SLEEP        = WDT_CONFIG_SLEEP_Msk,                       /**< WDT will run when CPU is in SLEEP mode. */
00085     NRF_WDT_BEHAVIOUR_RUN_HALT         = WDT_CONFIG_HALT_Msk,                        /**< WDT will run when CPU is in HALT mode. */
00086     NRF_WDT_BEHAVIOUR_RUN_SLEEP_HALT   = WDT_CONFIG_SLEEP_Msk | WDT_CONFIG_HALT_Msk, /**< WDT will run when CPU is in SLEEP or HALT mode. */
00087     NRF_WDT_BEHAVIOUR_PAUSE_SLEEP_HALT = 0,                                          /**< WDT will be paused when CPU is in SLEEP or HALT mode. */
00088 } nrf_wdt_behaviour_t;
00089 
00090 /**
00091  * @enum nrf_wdt_rr_register_t
00092  * @brief WDT reload request registers.
00093  */
00094 typedef enum
00095 {
00096     NRF_WDT_RR0 = 0, /**< Reload request register 0. */
00097     NRF_WDT_RR1,     /**< Reload request register 1. */
00098     NRF_WDT_RR2,     /**< Reload request register 2. */
00099     NRF_WDT_RR3,     /**< Reload request register 3. */
00100     NRF_WDT_RR4,     /**< Reload request register 4. */
00101     NRF_WDT_RR5,     /**< Reload request register 5. */
00102     NRF_WDT_RR6,     /**< Reload request register 6. */
00103     NRF_WDT_RR7      /**< Reload request register 7. */
00104 } nrf_wdt_rr_register_t;
00105 
00106 /**
00107  * @enum nrf_wdt_int_mask_t
00108  * @brief WDT interrupts.
00109  */
00110 typedef enum
00111 {
00112     NRF_WDT_INT_TIMEOUT_MASK = WDT_INTENSET_TIMEOUT_Msk, /**< WDT interrupt from time-out event. */
00113 } nrf_wdt_int_mask_t;
00114 
00115 /**
00116  * @brief Function for configuring the watchdog behavior when the CPU is sleeping or halted.
00117  *
00118  * @param behaviour Watchdog behavior when CPU is in SLEEP or HALT mode.
00119  */
00120 __STATIC_INLINE void nrf_wdt_behaviour_set(nrf_wdt_behaviour_t behaviour)
00121 {
00122     NRF_WDT->CONFIG = behaviour;
00123 }
00124 
00125 
00126 /**
00127  * @brief Function for starting the watchdog.
00128  *
00129  * @param[in]  task             Task.
00130  */
00131 __STATIC_INLINE void nrf_wdt_task_trigger(nrf_wdt_task_t task)
00132 {
00133     *((volatile uint32_t *)((uint8_t *)NRF_WDT + task)) = NRF_WDT_TASK_SET;
00134 }
00135 
00136 
00137 /**
00138  * @brief Function for clearing the WDT event.
00139  *
00140  * @param[in]  event       Event.
00141  */
00142 __STATIC_INLINE void nrf_wdt_event_clear(nrf_wdt_event_t event)
00143 {
00144     *((volatile uint32_t *)((uint8_t *)NRF_WDT + (uint32_t)event)) = NRF_WDT_EVENT_CLEAR;
00145 }
00146 
00147 
00148 /**
00149  * @brief Function for retrieving the state of the WDT event.
00150  *
00151  * @param[in]  event       Event.
00152  *
00153  * @retval     true              If the event is set.
00154  * @retval     false             If the event is not set.
00155  */
00156 __STATIC_INLINE bool nrf_wdt_event_check(nrf_wdt_event_t event)
00157 {
00158     return (bool)*((volatile uint32_t *)((uint8_t *)NRF_WDT + event));
00159 }
00160 
00161 
00162 /**
00163  * @brief Function for enabling a specific interrupt.
00164  *
00165  * @param[in]  int_mask         Interrupt.
00166  */
00167 __STATIC_INLINE void nrf_wdt_int_enable(uint32_t int_mask)
00168 {
00169     NRF_WDT->INTENSET = int_mask;
00170 }
00171 
00172 
00173 /**
00174  * @brief Function for retrieving the state of given interrupt.
00175  *
00176  * @param[in]  int_mask         Interrupt.
00177  *
00178  * @retval     true                   Interrupt is enabled.
00179  * @retval     false                  Interrupt is not enabled.
00180  */
00181 __STATIC_INLINE bool nrf_wdt_int_enable_check(uint32_t int_mask)
00182 {
00183     return (bool)(NRF_WDT->INTENSET & int_mask);
00184 }
00185 
00186 
00187 /**
00188  * @brief Function for disabling a specific interrupt.
00189  *
00190  * @param[in]  int_mask         Interrupt.
00191  */
00192 __STATIC_INLINE void nrf_wdt_int_disable(uint32_t int_mask)
00193 {
00194     NRF_WDT->INTENCLR = int_mask;
00195 }
00196 
00197 
00198 /**
00199  * @brief Function for returning the address of a specific WDT task register.
00200  *
00201  * @param[in]  task             Task.
00202  */
00203 __STATIC_INLINE uint32_t nrf_wdt_task_address_get(nrf_wdt_task_t task)
00204 {
00205     return ((uint32_t)NRF_WDT + task);
00206 }
00207 
00208 
00209 /**
00210  * @brief Function for returning the address of a specific WDT event register.
00211  *
00212  * @param[in]  event       Event.
00213  *
00214  * @retval     address of requested event register
00215  */
00216 __STATIC_INLINE uint32_t nrf_wdt_event_address_get(nrf_wdt_event_t event)
00217 {
00218     return ((uint32_t)NRF_WDT + event);
00219 }
00220 
00221 
00222 /**
00223  * @brief Function for retrieving the watchdog status.
00224  *
00225  * @retval     true             If the watchdog is started.
00226  * @retval     false            If the watchdog is not started.
00227  */
00228 __STATIC_INLINE bool nrf_wdt_started(void)
00229 {
00230     return (bool)(NRF_WDT->RUNSTATUS);
00231 }
00232 
00233 
00234 /**
00235  * @brief Function for retrieving the watchdog reload request status.
00236  *
00237  * @param[in]  rr_register      Reload request register to check.
00238  *
00239  * @retval     true             If a reload request is running.
00240  * @retval     false            If no reload request is running.
00241  */
00242 __STATIC_INLINE bool nrf_wdt_request_status(nrf_wdt_rr_register_t rr_register)
00243 {
00244     return (bool)(((NRF_WDT->REQSTATUS) >> rr_register) & 0x1UL);
00245 }
00246 
00247 
00248 /**
00249  * @brief Function for setting the watchdog reload value.
00250  *
00251  * @param[in]  reload_value     Watchdog counter initial value.
00252  */
00253 __STATIC_INLINE void nrf_wdt_reload_value_set(uint32_t reload_value)
00254 {
00255     NRF_WDT->CRV = reload_value;
00256 }
00257 
00258 
00259 /**
00260  * @brief Function for retrieving the watchdog reload value.
00261  *
00262  * @retval                      Reload value.
00263  */
00264 __STATIC_INLINE uint32_t nrf_wdt_reload_value_get(void)
00265 {
00266     return (uint32_t)NRF_WDT->CRV;
00267 }
00268 
00269 
00270 /**
00271  * @brief Function for enabling a specific reload request register.
00272  *
00273  * @param[in]  rr_register       Reload request register to enable.
00274  */
00275 __STATIC_INLINE void nrf_wdt_reload_request_enable(nrf_wdt_rr_register_t rr_register)
00276 {
00277     NRF_WDT->RREN |= 0x1UL << rr_register;
00278 }
00279 
00280 
00281 /**
00282  * @brief Function for disabling a specific reload request register.
00283  *
00284  * @param[in]  rr_register       Reload request register to disable.
00285  */
00286 __STATIC_INLINE void nrf_wdt_reload_request_disable(nrf_wdt_rr_register_t rr_register)
00287 {
00288     NRF_WDT->RREN &= ~(0x1UL << rr_register);
00289 }
00290 
00291 
00292 /**
00293  * @brief Function for retrieving the status of a specific reload request register.
00294  *
00295  * @param[in]  rr_register       Reload request register to check.
00296  *
00297  * @retval     true              If the reload request register is enabled.
00298  * @retval     false             If the reload request register is not enabled.
00299  */
00300 __STATIC_INLINE bool nrf_wdt_reload_request_is_enabled(nrf_wdt_rr_register_t rr_register)
00301 {
00302     return (bool)(NRF_WDT->RREN & (0x1UL << rr_register));
00303 }
00304 
00305 
00306 /**
00307  * @brief Function for setting a specific reload request register.
00308  *
00309  * @param[in]  rr_register       Reload request register to set.
00310  */
00311 __STATIC_INLINE void nrf_wdt_reload_request_set(nrf_wdt_rr_register_t rr_register)
00312 {
00313     NRF_WDT->RR[rr_register] = NRF_WDT_RR_VALUE;
00314 }
00315 
00316 
00317 #endif
00318 
00319 /** @} */