Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | /* * Copyright (c) 2016 Open-RnD Sp. z o.o. * Copyright (c) 2017 RnDity Sp. z o.o. * * SPDX-License-Identifier: Apache-2.0 */ #include <watchdog.h> #include <soc.h> #include <errno.h> #include <assert.h> #include "iwdg_stm32.h" /* Minimal timeout in microseconds. */ #define IWDG_TIMEOUT_MIN 100 /* Maximal timeout in microseconds. */ #define IWDG_TIMEOUT_MAX 26214400 #define IS_IWDG_TIMEOUT(__TIMEOUT__) \ (((__TIMEOUT__) >= IWDG_TIMEOUT_MIN) && \ ((__TIMEOUT__) <= IWDG_TIMEOUT_MAX)) /* * Status register need 5 RC LSI divided by prescaler clock to be updated. * With higher prescaler (256U), and according to HSI variation, * we need to wait at least 6 cycles so 48 ms. */ #define IWDG_DEFAULT_TIMEOUT 48u /** * @brief Calculates prescaler & reload values. * * @param timeout Timeout value in microseconds. * @param prescaler Pointer to prescaler value. * @param reload Pointer to reload value. */ static void iwdg_stm32_convert_timeout(u32_t timeout, u32_t *prescaler, u32_t *reload) { assert(IS_IWDG_TIMEOUT(timeout)); u16_t divider = 0; u8_t shift = 0; /* Convert timeout to seconds. */ float m_timeout = (float)timeout / 1000000 * LSI_VALUE; do { divider = 4 << shift; shift++; } while ((m_timeout / divider) > 0xFFF); /* * Value of the 'shift' variable corresponds to the * defines of LL_IWDG_PRESCALER_XX type. */ *prescaler = --shift; *reload = (uint32_t)(m_timeout / divider) - 1; } static void iwdg_stm32_enable(struct device *dev) { IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev); LL_IWDG_Enable(iwdg); } static void iwdg_stm32_disable(struct device *dev) { /* watchdog cannot be stopped once started */ ARG_UNUSED(dev); } static int iwdg_stm32_set_config(struct device *dev, struct wdt_config *config) { IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev); u32_t timeout = config->timeout; u32_t prescaler = 0; u32_t reload = 0; u32_t tickstart; assert(IS_IWDG_TIMEOUT(timeout)); iwdg_stm32_convert_timeout(timeout, &prescaler, &reload); assert(IS_IWDG_PRESCALER(prescaler)); assert(IS_IWDG_RELOAD(reload)); LL_IWDG_EnableWriteAccess(iwdg); LL_IWDG_SetPrescaler(iwdg, prescaler); LL_IWDG_SetReloadCounter(iwdg, reload); #if defined(CONFIG_SOC_SERIES_STM32F3X) || defined(CONFIG_SOC_SERIES_STM32L4X) /* Neither STM32F1X nor STM32F4 series supports window option. */ LL_IWDG_SetWindow(iwdg, 0x0FFF); #endif tickstart = k_uptime_get_32(); while (LL_IWDG_IsReady(iwdg) == 0) { if ((k_uptime_get_32() - tickstart) > IWDG_DEFAULT_TIMEOUT) { return -ENODEV; } } LL_IWDG_ReloadCounter(iwdg); return 0; } static void iwdg_stm32_get_config(struct device *dev, struct wdt_config *config) { IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev); u32_t prescaler = LL_IWDG_GetPrescaler(iwdg); u32_t reload = LL_IWDG_GetReloadCounter(iwdg); /* Timeout given in microseconds. */ config->timeout = (u32_t)((4 << prescaler) * (reload + 1) * (1000000 / LSI_VALUE)); } static void iwdg_stm32_reload(struct device *dev) { IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev); LL_IWDG_ReloadCounter(iwdg); } static const struct wdt_driver_api iwdg_stm32_api = { .enable = iwdg_stm32_enable, .disable = iwdg_stm32_disable, .get_config = iwdg_stm32_get_config, .set_config = iwdg_stm32_set_config, .reload = iwdg_stm32_reload, }; static int iwdg_stm32_init(struct device *dev) { IWDG_TypeDef *iwdg = IWDG_STM32_STRUCT(dev); struct wdt_config config; config.timeout = CONFIG_IWDG_STM32_TIMEOUT; LL_IWDG_Enable(iwdg); iwdg_stm32_set_config(dev, &config); /* * The ST production value for the option bytes where WDG_SW bit is * present is 0x00FF55AA, namely the Software watchdog mode is * enabled by default. * If the IWDG is started by either hardware option or software access, * the LSI oscillator is forced ON and cannot be disabled. * * t_IWDG(ms) = t_LSI(ms) x 4 x 2^(IWDG_PR[2:0]) x (IWDG_RLR[11:0] + 1) */ return 0; } static struct iwdg_stm32_data iwdg_stm32_dev_data = { .Instance = IWDG }; DEVICE_AND_API_INIT(iwdg_stm32, CONFIG_IWDG_STM32_DEVICE_NAME, iwdg_stm32_init, &iwdg_stm32_dev_data, NULL, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &iwdg_stm32_api); |