|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Trampoline SRL |
| 3 | + * Copyright (c) 2019 Giampaolo Mancini <[email protected]> |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "watchdog_api.h" |
| 20 | +#include "nrfx_wdt.h" |
| 21 | + |
| 22 | +#if DEVICE_WATCHDOG |
| 23 | + |
| 24 | +static void dummy(void) { |
| 25 | +} |
| 26 | + |
| 27 | +watchdog_status_t hal_watchdog_init(const watchdog_config_t *config) |
| 28 | +{ |
| 29 | + nrfx_err_t err_code; |
| 30 | + |
| 31 | + // The nRF watchdogs accept a range from 0xF to maximum 0xFFFF_FFFF |
| 32 | + // 32768 Hz ticks. (Plase, see |
| 33 | + // https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v15.3.0%2Fstructnrfx__wdt__config__t.html) |
| 34 | + // Min timeout values in ms range from 0x1 to (0xFFFFFFFF / 0x8000) * 1000 |
| 35 | + if ( config->timeout_ms < 0x1 && config->timeout_ms > 0x07CFFFFF ) { |
| 36 | + return WATCHDOG_STATUS_INVALID_ARGUMENT; |
| 37 | + } |
| 38 | + |
| 39 | + nrfx_wdt_config_t nrf_cfg; |
| 40 | + |
| 41 | + nrf_cfg.reload_value = config->timeout_ms; |
| 42 | + nrf_cfg.behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP_HALT; |
| 43 | + nrf_cfg.interrupt_priority = NRFX_WDT_CONFIG_NO_IRQ; |
| 44 | + |
| 45 | + err_code = nrfx_wdt_init(&nrf_cfg, dummy); |
| 46 | + if (err_code != NRFX_SUCCESS) { |
| 47 | + return WATCHDOG_STATUS_INVALID_ARGUMENT; |
| 48 | + } |
| 49 | + |
| 50 | + nrfx_wdt_channel_id channel; |
| 51 | + if (nrfx_wdt_channel_alloc(&channel) != NRF_SUCCESS) { |
| 52 | + return WATCHDOG_STATUS_INVALID_ARGUMENT; |
| 53 | + } |
| 54 | + nrfx_wdt_enable(); |
| 55 | + nrfx_wdt_feed(); |
| 56 | + |
| 57 | + return WATCHDOG_STATUS_OK; |
| 58 | +} |
| 59 | + |
| 60 | +void hal_watchdog_kick(void) |
| 61 | +{ |
| 62 | + nrfx_wdt_feed(); |
| 63 | +} |
| 64 | + |
| 65 | +watchdog_status_t hal_watchdog_stop(void) |
| 66 | +{ |
| 67 | + return WATCHDOG_STATUS_NOT_SUPPORTED; |
| 68 | +} |
| 69 | + |
| 70 | +uint32_t hal_watchdog_get_reload_value(void) |
| 71 | +{ |
| 72 | + // Convert to milliseconds from 32768 Hz clock ticks. |
| 73 | + return (uint64_t)nrf_wdt_reload_value_get() / 32768U * 1000; |
| 74 | +} |
| 75 | + |
| 76 | +watchdog_features_t hal_watchdog_get_platform_features(void) |
| 77 | +{ |
| 78 | + watchdog_features_t features; |
| 79 | + |
| 80 | + features.max_timeout = 0x07CFFFFF; |
| 81 | + features.update_config = false; |
| 82 | + features.disable_watchdog = false; |
| 83 | + return features; |
| 84 | +} |
| 85 | + |
| 86 | +#endif // DEVICE_WATCHDOG |
0 commit comments