mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jul 17 09:15:10 2015 +0100
Revision:
592:a274ee790e56
Synchronized with git revision e7144f83a8d75df80c4877936b6ffe552b0be9e6

Full URL: https://github.com/mbedmicro/mbed/commit/e7144f83a8d75df80c4877936b6ffe552b0be9e6/

More API implementation for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 592:a274ee790e56 1 #include "rtc_count.h"
mbed_official 592:a274ee790e56 2 #include <gclk.h>
mbed_official 592:a274ee790e56 3
mbed_official 592:a274ee790e56 4 #if !defined(__DOXYGEN__)
mbed_official 592:a274ee790e56 5 struct rtc_module *_rtc_instance[RTC_INST_NUM];
mbed_official 592:a274ee790e56 6 #endif
mbed_official 592:a274ee790e56 7
mbed_official 592:a274ee790e56 8 /**
mbed_official 592:a274ee790e56 9 * \brief Determines if the hardware module(s) are currently synchronizing to the bus.
mbed_official 592:a274ee790e56 10 *
mbed_official 592:a274ee790e56 11 * Checks to see if the underlying hardware peripheral module(s) are currently
mbed_official 592:a274ee790e56 12 * synchronizing across multiple clock domains to the hardware bus, This
mbed_official 592:a274ee790e56 13 * function can be used to delay further operations on a module until such time
mbed_official 592:a274ee790e56 14 * that it is ready, to prevent blocking delays for synchronization in the
mbed_official 592:a274ee790e56 15 * user application.
mbed_official 592:a274ee790e56 16 *
mbed_official 592:a274ee790e56 17 * \param[in] module RTC hardware module
mbed_official 592:a274ee790e56 18 *
mbed_official 592:a274ee790e56 19 * \return Synchronization status of the underlying hardware module(s).
mbed_official 592:a274ee790e56 20 *
mbed_official 592:a274ee790e56 21 * \retval true if the module synchronization is ongoing
mbed_official 592:a274ee790e56 22 * \retval false if the module has completed synchronization
mbed_official 592:a274ee790e56 23 */
mbed_official 592:a274ee790e56 24 static bool rtc_count_is_syncing(struct rtc_module *const module)
mbed_official 592:a274ee790e56 25 {
mbed_official 592:a274ee790e56 26 /* Sanity check arguments */
mbed_official 592:a274ee790e56 27 Assert(module);
mbed_official 592:a274ee790e56 28 Assert(module->hw);
mbed_official 592:a274ee790e56 29
mbed_official 592:a274ee790e56 30 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 31
mbed_official 592:a274ee790e56 32 if (rtc_module->MODE0.STATUS.reg & RTC_STATUS_SYNCBUSY) {
mbed_official 592:a274ee790e56 33 return true;
mbed_official 592:a274ee790e56 34 }
mbed_official 592:a274ee790e56 35
mbed_official 592:a274ee790e56 36 return false;
mbed_official 592:a274ee790e56 37 }
mbed_official 592:a274ee790e56 38
mbed_official 592:a274ee790e56 39 /**
mbed_official 592:a274ee790e56 40 * \brief Enables the RTC module.
mbed_official 592:a274ee790e56 41 *
mbed_official 592:a274ee790e56 42 * Enables the RTC module once it has been configured, ready for use. Most
mbed_official 592:a274ee790e56 43 * module configuration parameters cannot be altered while the module is enabled.
mbed_official 592:a274ee790e56 44 *
mbed_official 592:a274ee790e56 45 * \param[in,out] module RTC hardware module
mbed_official 592:a274ee790e56 46 */
mbed_official 592:a274ee790e56 47 void rtc_count_enable(struct rtc_module *const module)
mbed_official 592:a274ee790e56 48 {
mbed_official 592:a274ee790e56 49 /* Sanity check arguments */
mbed_official 592:a274ee790e56 50 Assert(module);
mbed_official 592:a274ee790e56 51 Assert(module->hw);
mbed_official 592:a274ee790e56 52
mbed_official 592:a274ee790e56 53 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 54
mbed_official 592:a274ee790e56 55 #if RTC_COUNT_ASYNC == true
mbed_official 592:a274ee790e56 56 system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_RTC);
mbed_official 592:a274ee790e56 57 #endif
mbed_official 592:a274ee790e56 58
mbed_official 592:a274ee790e56 59 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 60 /* Wait for synchronization */
mbed_official 592:a274ee790e56 61 }
mbed_official 592:a274ee790e56 62
mbed_official 592:a274ee790e56 63 /* Enable RTC module. */
mbed_official 592:a274ee790e56 64 rtc_module->MODE0.CTRL.reg |= RTC_MODE0_CTRL_ENABLE;
mbed_official 592:a274ee790e56 65 }
mbed_official 592:a274ee790e56 66
mbed_official 592:a274ee790e56 67 /**
mbed_official 592:a274ee790e56 68 * \brief Disables the RTC module.
mbed_official 592:a274ee790e56 69 *
mbed_official 592:a274ee790e56 70 * Disables the RTC module.
mbed_official 592:a274ee790e56 71 *
mbed_official 592:a274ee790e56 72 * \param[in,out] module RTC hardware module
mbed_official 592:a274ee790e56 73 */
mbed_official 592:a274ee790e56 74 void rtc_count_disable(struct rtc_module *const module)
mbed_official 592:a274ee790e56 75 {
mbed_official 592:a274ee790e56 76 /* Sanity check arguments */
mbed_official 592:a274ee790e56 77 Assert(module);
mbed_official 592:a274ee790e56 78 Assert(module->hw);
mbed_official 592:a274ee790e56 79
mbed_official 592:a274ee790e56 80 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 81
mbed_official 592:a274ee790e56 82 #if RTC_COUNT_ASYNC == true
mbed_official 592:a274ee790e56 83 system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_RTC);
mbed_official 592:a274ee790e56 84 #endif
mbed_official 592:a274ee790e56 85
mbed_official 592:a274ee790e56 86 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 87 /* Wait for synchronization */
mbed_official 592:a274ee790e56 88 }
mbed_official 592:a274ee790e56 89
mbed_official 592:a274ee790e56 90 /* Disable RTC module. */
mbed_official 592:a274ee790e56 91 rtc_module->MODE0.CTRL.reg &= ~RTC_MODE0_CTRL_ENABLE;
mbed_official 592:a274ee790e56 92 }
mbed_official 592:a274ee790e56 93
mbed_official 592:a274ee790e56 94 /**
mbed_official 592:a274ee790e56 95 * \brief Resets the RTC module.
mbed_official 592:a274ee790e56 96 * Resets the RTC to hardware defaults.
mbed_official 592:a274ee790e56 97 *
mbed_official 592:a274ee790e56 98 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 99 */
mbed_official 592:a274ee790e56 100 void rtc_count_reset(struct rtc_module *const module)
mbed_official 592:a274ee790e56 101 {
mbed_official 592:a274ee790e56 102 /* Sanity check arguments */
mbed_official 592:a274ee790e56 103 Assert(module);
mbed_official 592:a274ee790e56 104 Assert(module->hw);
mbed_official 592:a274ee790e56 105
mbed_official 592:a274ee790e56 106 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 107
mbed_official 592:a274ee790e56 108 /* Disable module before reset. */
mbed_official 592:a274ee790e56 109 rtc_count_disable(module);
mbed_official 592:a274ee790e56 110
mbed_official 592:a274ee790e56 111 #if RTC_COUNT_ASYNC == true
mbed_official 592:a274ee790e56 112 module->registered_callback = 0;
mbed_official 592:a274ee790e56 113 module->enabled_callback = 0;
mbed_official 592:a274ee790e56 114 #endif
mbed_official 592:a274ee790e56 115
mbed_official 592:a274ee790e56 116 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 117 /* Wait for synchronization */
mbed_official 592:a274ee790e56 118 }
mbed_official 592:a274ee790e56 119
mbed_official 592:a274ee790e56 120 /* Initiate software reset. */
mbed_official 592:a274ee790e56 121 rtc_module->MODE0.CTRL.reg |= RTC_MODE0_CTRL_SWRST;
mbed_official 592:a274ee790e56 122 }
mbed_official 592:a274ee790e56 123
mbed_official 592:a274ee790e56 124 /**
mbed_official 592:a274ee790e56 125 * \internal Applies the given configuration.
mbed_official 592:a274ee790e56 126 *
mbed_official 592:a274ee790e56 127 * Sets the configurations given from the configuration structure to the
mbed_official 592:a274ee790e56 128 * hardware module.
mbed_official 592:a274ee790e56 129 *
mbed_official 592:a274ee790e56 130 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 131 * \param[in] config Pointer to the configuration structure.
mbed_official 592:a274ee790e56 132 *
mbed_official 592:a274ee790e56 133 * \return Status of the configuration procedure.
mbed_official 592:a274ee790e56 134 * \retval STATUS_OK RTC configurations was set successfully.
mbed_official 592:a274ee790e56 135 * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were given.
mbed_official 592:a274ee790e56 136 */
mbed_official 592:a274ee790e56 137 static enum status_code _rtc_count_set_config(
mbed_official 592:a274ee790e56 138 struct rtc_module *const module,
mbed_official 592:a274ee790e56 139 const struct rtc_count_config *const config)
mbed_official 592:a274ee790e56 140 {
mbed_official 592:a274ee790e56 141 /* Sanity check arguments */
mbed_official 592:a274ee790e56 142 Assert(module);
mbed_official 592:a274ee790e56 143 Assert(module->hw);
mbed_official 592:a274ee790e56 144
mbed_official 592:a274ee790e56 145 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 146
mbed_official 592:a274ee790e56 147 rtc_module->MODE0.CTRL.reg = RTC_MODE0_CTRL_MODE(0) | config->prescaler;
mbed_official 592:a274ee790e56 148
mbed_official 592:a274ee790e56 149 /* Set mode and clear on match if applicable. */
mbed_official 592:a274ee790e56 150 switch (config->mode) {
mbed_official 592:a274ee790e56 151 case RTC_COUNT_MODE_32BIT:
mbed_official 592:a274ee790e56 152 /* Set 32bit mode and clear on match if applicable. */
mbed_official 592:a274ee790e56 153 rtc_module->MODE0.CTRL.reg |= RTC_MODE0_CTRL_MODE(0);
mbed_official 592:a274ee790e56 154
mbed_official 592:a274ee790e56 155 /* Check if clear on compare match should be set. */
mbed_official 592:a274ee790e56 156 if (config->clear_on_match) {
mbed_official 592:a274ee790e56 157 /* Set clear on match. */
mbed_official 592:a274ee790e56 158 rtc_module->MODE0.CTRL.reg |= RTC_MODE0_CTRL_MATCHCLR;
mbed_official 592:a274ee790e56 159 }
mbed_official 592:a274ee790e56 160 /* Set compare values. */
mbed_official 592:a274ee790e56 161 for (uint8_t i = 0; i < RTC_NUM_OF_COMP32; i++) {
mbed_official 592:a274ee790e56 162 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 163 /* Wait for synchronization */
mbed_official 592:a274ee790e56 164 }
mbed_official 592:a274ee790e56 165
mbed_official 592:a274ee790e56 166 rtc_count_set_compare(module, config->compare_values[i],
mbed_official 592:a274ee790e56 167 (enum rtc_count_compare)i);
mbed_official 592:a274ee790e56 168 }
mbed_official 592:a274ee790e56 169 break;
mbed_official 592:a274ee790e56 170
mbed_official 592:a274ee790e56 171 case RTC_COUNT_MODE_16BIT:
mbed_official 592:a274ee790e56 172 /* Set 16bit mode. */
mbed_official 592:a274ee790e56 173 rtc_module->MODE1.CTRL.reg |= RTC_MODE1_CTRL_MODE(1);
mbed_official 592:a274ee790e56 174
mbed_official 592:a274ee790e56 175 /* Check if match on clear is set, and return invalid
mbed_official 592:a274ee790e56 176 * argument if set. */
mbed_official 592:a274ee790e56 177 if (config->clear_on_match) {
mbed_official 592:a274ee790e56 178 Assert(false);
mbed_official 592:a274ee790e56 179 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 180 }
mbed_official 592:a274ee790e56 181 /* Set compare values. */
mbed_official 592:a274ee790e56 182 for (uint8_t i = 0; i < RTC_NUM_OF_COMP16; i++) {
mbed_official 592:a274ee790e56 183 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 184 /* Wait for synchronization */
mbed_official 592:a274ee790e56 185 }
mbed_official 592:a274ee790e56 186
mbed_official 592:a274ee790e56 187 rtc_count_set_compare(module, config->compare_values[i],
mbed_official 592:a274ee790e56 188 (enum rtc_count_compare)i);
mbed_official 592:a274ee790e56 189 }
mbed_official 592:a274ee790e56 190 break;
mbed_official 592:a274ee790e56 191 default:
mbed_official 592:a274ee790e56 192 Assert(false);
mbed_official 592:a274ee790e56 193 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 194 }
mbed_official 592:a274ee790e56 195
mbed_official 592:a274ee790e56 196 /* Check to set continuously clock read update mode. */
mbed_official 592:a274ee790e56 197 if (config->continuously_update) {
mbed_official 592:a274ee790e56 198 /* Set continuously mode. */
mbed_official 592:a274ee790e56 199 rtc_module->MODE0.READREQ.reg |= RTC_READREQ_RCONT;
mbed_official 592:a274ee790e56 200 }
mbed_official 592:a274ee790e56 201
mbed_official 592:a274ee790e56 202 /* Return status OK if everything was configured. */
mbed_official 592:a274ee790e56 203 return STATUS_OK;
mbed_official 592:a274ee790e56 204 }
mbed_official 592:a274ee790e56 205
mbed_official 592:a274ee790e56 206 /**
mbed_official 592:a274ee790e56 207 * \brief Initializes the RTC module with given configurations.
mbed_official 592:a274ee790e56 208 *
mbed_official 592:a274ee790e56 209 * Initializes the module, setting up all given configurations to provide
mbed_official 592:a274ee790e56 210 * the desired functionality of the RTC.
mbed_official 592:a274ee790e56 211 *
mbed_official 592:a274ee790e56 212 * \param[out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 213 * \param[in] hw Pointer to hardware instance
mbed_official 592:a274ee790e56 214 * \param[in] config Pointer to the configuration structure.
mbed_official 592:a274ee790e56 215 *
mbed_official 592:a274ee790e56 216 * \return Status of the initialization procedure.
mbed_official 592:a274ee790e56 217 * \retval STATUS_OK If the initialization was run stressfully.
mbed_official 592:a274ee790e56 218 * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were given.
mbed_official 592:a274ee790e56 219 */
mbed_official 592:a274ee790e56 220 enum status_code rtc_count_init(
mbed_official 592:a274ee790e56 221 struct rtc_module *const module,
mbed_official 592:a274ee790e56 222 Rtc *const hw,
mbed_official 592:a274ee790e56 223 const struct rtc_count_config *const config)
mbed_official 592:a274ee790e56 224 {
mbed_official 592:a274ee790e56 225 /* Sanity check arguments */
mbed_official 592:a274ee790e56 226 Assert(module);
mbed_official 592:a274ee790e56 227 Assert(hw);
mbed_official 592:a274ee790e56 228 Assert(config);
mbed_official 592:a274ee790e56 229
mbed_official 592:a274ee790e56 230 /* Initialize device instance */
mbed_official 592:a274ee790e56 231 module->hw = hw;
mbed_official 592:a274ee790e56 232
mbed_official 592:a274ee790e56 233 /* Turn on the digital interface clock */
mbed_official 592:a274ee790e56 234 system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBA, PM_APBAMASK_RTC);
mbed_official 592:a274ee790e56 235
mbed_official 592:a274ee790e56 236 /* Set up GCLK */
mbed_official 592:a274ee790e56 237 struct system_gclk_chan_config gclk_chan_conf;
mbed_official 592:a274ee790e56 238 system_gclk_chan_get_config_defaults(&gclk_chan_conf);
mbed_official 592:a274ee790e56 239 gclk_chan_conf.source_generator = GCLK_GENERATOR_2;
mbed_official 592:a274ee790e56 240 system_gclk_chan_set_config(RTC_GCLK_ID, &gclk_chan_conf);
mbed_official 592:a274ee790e56 241 system_gclk_chan_enable(RTC_GCLK_ID);
mbed_official 592:a274ee790e56 242
mbed_official 592:a274ee790e56 243 /* Reset module to hardware defaults. */
mbed_official 592:a274ee790e56 244 rtc_count_reset(module);
mbed_official 592:a274ee790e56 245
mbed_official 592:a274ee790e56 246 /* Save conf_struct internally for continued use. */
mbed_official 592:a274ee790e56 247 module->mode = config->mode;
mbed_official 592:a274ee790e56 248 module->continuously_update = config->continuously_update;
mbed_official 592:a274ee790e56 249
mbed_official 592:a274ee790e56 250 # if (RTC_INST_NUM == 1)
mbed_official 592:a274ee790e56 251 _rtc_instance[0] = module;
mbed_official 592:a274ee790e56 252 # else
mbed_official 592:a274ee790e56 253 /* Register this instance for callbacks*/
mbed_official 592:a274ee790e56 254 _rtc_instance[_rtc_get_inst_index(hw)] = module;
mbed_official 592:a274ee790e56 255 # endif
mbed_official 592:a274ee790e56 256
mbed_official 592:a274ee790e56 257 /* Set config and return status. */
mbed_official 592:a274ee790e56 258 return _rtc_count_set_config(module, config);
mbed_official 592:a274ee790e56 259 }
mbed_official 592:a274ee790e56 260
mbed_official 592:a274ee790e56 261 /**
mbed_official 592:a274ee790e56 262 * \brief Set the current count value to desired value.
mbed_official 592:a274ee790e56 263 *
mbed_official 592:a274ee790e56 264 * Sets the value of the counter to the specified value.
mbed_official 592:a274ee790e56 265 *
mbed_official 592:a274ee790e56 266 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 267 * \param[in] count_value The value to be set in count register.
mbed_official 592:a274ee790e56 268 *
mbed_official 592:a274ee790e56 269 * \return Status of setting the register.
mbed_official 592:a274ee790e56 270 * \retval STATUS_OK If everything was executed correctly.
mbed_official 592:a274ee790e56 271 * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided.
mbed_official 592:a274ee790e56 272 */
mbed_official 592:a274ee790e56 273 enum status_code rtc_count_set_count(
mbed_official 592:a274ee790e56 274 struct rtc_module *const module,
mbed_official 592:a274ee790e56 275 const uint32_t count_value)
mbed_official 592:a274ee790e56 276 {
mbed_official 592:a274ee790e56 277 /* Sanity check arguments */
mbed_official 592:a274ee790e56 278 Assert(module);
mbed_official 592:a274ee790e56 279 Assert(module->hw);
mbed_official 592:a274ee790e56 280
mbed_official 592:a274ee790e56 281 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 282
mbed_official 592:a274ee790e56 283 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 284 /* Wait for synchronization */
mbed_official 592:a274ee790e56 285 }
mbed_official 592:a274ee790e56 286
mbed_official 592:a274ee790e56 287 /* Set count according to mode */
mbed_official 592:a274ee790e56 288 switch(module->mode) {
mbed_official 592:a274ee790e56 289 case RTC_COUNT_MODE_32BIT:
mbed_official 592:a274ee790e56 290 /* Write value to register. */
mbed_official 592:a274ee790e56 291 rtc_module->MODE0.COUNT.reg = count_value;
mbed_official 592:a274ee790e56 292
mbed_official 592:a274ee790e56 293 break;
mbed_official 592:a274ee790e56 294
mbed_official 592:a274ee790e56 295 case RTC_COUNT_MODE_16BIT:
mbed_official 592:a274ee790e56 296 /* Check if 16-bit value is provided. */
mbed_official 592:a274ee790e56 297 if(count_value > 0xffff) {
mbed_official 592:a274ee790e56 298 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 299 }
mbed_official 592:a274ee790e56 300
mbed_official 592:a274ee790e56 301 /* Write value to register. */
mbed_official 592:a274ee790e56 302 rtc_module->MODE1.COUNT.reg = (uint32_t)count_value;
mbed_official 592:a274ee790e56 303
mbed_official 592:a274ee790e56 304 break;
mbed_official 592:a274ee790e56 305
mbed_official 592:a274ee790e56 306 default:
mbed_official 592:a274ee790e56 307 Assert(false);
mbed_official 592:a274ee790e56 308 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 309 }
mbed_official 592:a274ee790e56 310 return STATUS_OK;
mbed_official 592:a274ee790e56 311 }
mbed_official 592:a274ee790e56 312
mbed_official 592:a274ee790e56 313 /**
mbed_official 592:a274ee790e56 314 * \brief Get the current count value.
mbed_official 592:a274ee790e56 315 *
mbed_official 592:a274ee790e56 316 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 317 *
mbed_official 592:a274ee790e56 318 * Returns the current count value.
mbed_official 592:a274ee790e56 319 *
mbed_official 592:a274ee790e56 320 * \return The current counter value as a 32-bit unsigned integer.
mbed_official 592:a274ee790e56 321 */
mbed_official 592:a274ee790e56 322 uint32_t rtc_count_get_count(struct rtc_module *const module)
mbed_official 592:a274ee790e56 323 {
mbed_official 592:a274ee790e56 324 /* Sanity check arguments */
mbed_official 592:a274ee790e56 325 Assert(module);
mbed_official 592:a274ee790e56 326 Assert(module->hw);
mbed_official 592:a274ee790e56 327
mbed_official 592:a274ee790e56 328 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 329
mbed_official 592:a274ee790e56 330 /* Initialize return value. */
mbed_official 592:a274ee790e56 331 uint32_t ret_val;
mbed_official 592:a274ee790e56 332
mbed_official 592:a274ee790e56 333 /* Change of read method based on value of continuously_update value in
mbed_official 592:a274ee790e56 334 * the configuration structure. */
mbed_official 592:a274ee790e56 335 if(!(module->continuously_update)) {
mbed_official 592:a274ee790e56 336 /* Request read on count register. */
mbed_official 592:a274ee790e56 337 rtc_module->MODE0.READREQ.reg = RTC_READREQ_RREQ;
mbed_official 592:a274ee790e56 338
mbed_official 592:a274ee790e56 339 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 340 /* Wait for synchronization */
mbed_official 592:a274ee790e56 341 }
mbed_official 592:a274ee790e56 342 }
mbed_official 592:a274ee790e56 343
mbed_official 592:a274ee790e56 344 /* Read value based on mode. */
mbed_official 592:a274ee790e56 345 switch (module->mode) {
mbed_official 592:a274ee790e56 346 case RTC_COUNT_MODE_32BIT:
mbed_official 592:a274ee790e56 347 /* Return count value in 32-bit mode. */
mbed_official 592:a274ee790e56 348 ret_val = rtc_module->MODE0.COUNT.reg;
mbed_official 592:a274ee790e56 349
mbed_official 592:a274ee790e56 350 break;
mbed_official 592:a274ee790e56 351
mbed_official 592:a274ee790e56 352 case RTC_COUNT_MODE_16BIT:
mbed_official 592:a274ee790e56 353 /* Return count value in 16-bit mode. */
mbed_official 592:a274ee790e56 354 ret_val = (uint32_t)rtc_module->MODE1.COUNT.reg;
mbed_official 592:a274ee790e56 355
mbed_official 592:a274ee790e56 356 break;
mbed_official 592:a274ee790e56 357
mbed_official 592:a274ee790e56 358 default:
mbed_official 592:a274ee790e56 359 Assert(false);
mbed_official 592:a274ee790e56 360 /* Counter not initialized. Assume counter value 0.*/
mbed_official 592:a274ee790e56 361 ret_val = 0;
mbed_official 592:a274ee790e56 362 break;
mbed_official 592:a274ee790e56 363 }
mbed_official 592:a274ee790e56 364
mbed_official 592:a274ee790e56 365 return ret_val;
mbed_official 592:a274ee790e56 366 }
mbed_official 592:a274ee790e56 367
mbed_official 592:a274ee790e56 368 /**
mbed_official 592:a274ee790e56 369 * \brief Set the compare value for the specified compare.
mbed_official 592:a274ee790e56 370 *
mbed_official 592:a274ee790e56 371 * Sets the value specified by the implementer to the requested compare.
mbed_official 592:a274ee790e56 372 *
mbed_official 592:a274ee790e56 373 * \note Compare 4 and 5 are only available in 16-bit mode.
mbed_official 592:a274ee790e56 374 *
mbed_official 592:a274ee790e56 375 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 376 * \param[in] comp_value The value to be written to the compare.
mbed_official 592:a274ee790e56 377 * \param[in] comp_index Index of the compare to set.
mbed_official 592:a274ee790e56 378 *
mbed_official 592:a274ee790e56 379 * \return Status indicating if compare was successfully set.
mbed_official 592:a274ee790e56 380 * \retval STATUS_OK If compare was successfully set.
mbed_official 592:a274ee790e56 381 * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided.
mbed_official 592:a274ee790e56 382 * \retval STATUS_ERR_BAD_FORMAT If the module was not initialized in a mode.
mbed_official 592:a274ee790e56 383 */
mbed_official 592:a274ee790e56 384 enum status_code rtc_count_set_compare(
mbed_official 592:a274ee790e56 385 struct rtc_module *const module,
mbed_official 592:a274ee790e56 386 const uint32_t comp_value,
mbed_official 592:a274ee790e56 387 const enum rtc_count_compare comp_index)
mbed_official 592:a274ee790e56 388 {
mbed_official 592:a274ee790e56 389 /* Sanity check arguments */
mbed_official 592:a274ee790e56 390 Assert(module);
mbed_official 592:a274ee790e56 391 Assert(module->hw);
mbed_official 592:a274ee790e56 392
mbed_official 592:a274ee790e56 393 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 394
mbed_official 592:a274ee790e56 395 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 396 /* Wait for synchronization */
mbed_official 592:a274ee790e56 397 }
mbed_official 592:a274ee790e56 398
mbed_official 592:a274ee790e56 399 /* Set compare values based on operation mode. */
mbed_official 592:a274ee790e56 400 switch (module->mode) {
mbed_official 592:a274ee790e56 401 case RTC_COUNT_MODE_32BIT:
mbed_official 592:a274ee790e56 402 /* Check sanity of comp_index. */
mbed_official 592:a274ee790e56 403 if ((uint32_t)comp_index > RTC_NUM_OF_COMP32) {
mbed_official 592:a274ee790e56 404 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 405 }
mbed_official 592:a274ee790e56 406
mbed_official 592:a274ee790e56 407 /* Set compare value for COMP. */
mbed_official 592:a274ee790e56 408 rtc_module->MODE0.COMP[comp_index].reg = comp_value;
mbed_official 592:a274ee790e56 409
mbed_official 592:a274ee790e56 410 break;
mbed_official 592:a274ee790e56 411
mbed_official 592:a274ee790e56 412 case RTC_COUNT_MODE_16BIT:
mbed_official 592:a274ee790e56 413 /* Check sanity of comp_index. */
mbed_official 592:a274ee790e56 414 if ((uint32_t)comp_index > RTC_NUM_OF_COMP16) {
mbed_official 592:a274ee790e56 415 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 416 }
mbed_official 592:a274ee790e56 417
mbed_official 592:a274ee790e56 418 /* Check that 16-bit value is provided. */
mbed_official 592:a274ee790e56 419 if (comp_value > 0xffff) {
mbed_official 592:a274ee790e56 420 Assert(false);
mbed_official 592:a274ee790e56 421 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 422 }
mbed_official 592:a274ee790e56 423
mbed_official 592:a274ee790e56 424 /* Set compare value for COMP. */
mbed_official 592:a274ee790e56 425 rtc_module->MODE1.COMP[comp_index].reg = comp_value & 0xffff;
mbed_official 592:a274ee790e56 426
mbed_official 592:a274ee790e56 427 break;
mbed_official 592:a274ee790e56 428
mbed_official 592:a274ee790e56 429 default:
mbed_official 592:a274ee790e56 430 Assert(false);
mbed_official 592:a274ee790e56 431 return STATUS_ERR_BAD_FORMAT;
mbed_official 592:a274ee790e56 432 }
mbed_official 592:a274ee790e56 433
mbed_official 592:a274ee790e56 434 /* Return status if everything is OK. */
mbed_official 592:a274ee790e56 435 return STATUS_OK;
mbed_official 592:a274ee790e56 436 }
mbed_official 592:a274ee790e56 437
mbed_official 592:a274ee790e56 438 /**
mbed_official 592:a274ee790e56 439 * \brief Get the current compare value of specified compare.
mbed_official 592:a274ee790e56 440 *
mbed_official 592:a274ee790e56 441 * Retrieves the current value of the specified compare.
mbed_official 592:a274ee790e56 442 *
mbed_official 592:a274ee790e56 443 * \note Compare 4 and 5 are only available in 16-bit mode.
mbed_official 592:a274ee790e56 444 *
mbed_official 592:a274ee790e56 445 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 446 * \param[out] comp_value Pointer to 32-bit integer that will be populated with
mbed_official 592:a274ee790e56 447 * the current compare value.
mbed_official 592:a274ee790e56 448 * \param[in] comp_index Index of compare to check.
mbed_official 592:a274ee790e56 449 *
mbed_official 592:a274ee790e56 450 * \return Status of the reading procedure.
mbed_official 592:a274ee790e56 451 * \retval STATUS_OK If the value was read correctly.
mbed_official 592:a274ee790e56 452 * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided.
mbed_official 592:a274ee790e56 453 * \retval STATUS_ERR_BAD_FORMAT If the module was not initialized in a mode.
mbed_official 592:a274ee790e56 454 */
mbed_official 592:a274ee790e56 455 enum status_code rtc_count_get_compare(
mbed_official 592:a274ee790e56 456 struct rtc_module *const module,
mbed_official 592:a274ee790e56 457 uint32_t *const comp_value,
mbed_official 592:a274ee790e56 458 const enum rtc_count_compare comp_index)
mbed_official 592:a274ee790e56 459 {
mbed_official 592:a274ee790e56 460 /* Sanity check arguments */
mbed_official 592:a274ee790e56 461 Assert(module);
mbed_official 592:a274ee790e56 462 Assert(module->hw);
mbed_official 592:a274ee790e56 463
mbed_official 592:a274ee790e56 464 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 465
mbed_official 592:a274ee790e56 466 switch (module->mode) {
mbed_official 592:a274ee790e56 467 case RTC_COUNT_MODE_32BIT:
mbed_official 592:a274ee790e56 468 /* Check sanity of comp_index. */
mbed_official 592:a274ee790e56 469 if ((uint32_t)comp_index > RTC_NUM_OF_COMP32) {
mbed_official 592:a274ee790e56 470 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 471 }
mbed_official 592:a274ee790e56 472
mbed_official 592:a274ee790e56 473 /* Get compare value for COMP. */
mbed_official 592:a274ee790e56 474 *comp_value = rtc_module->MODE0.COMP[comp_index].reg;
mbed_official 592:a274ee790e56 475
mbed_official 592:a274ee790e56 476 break;
mbed_official 592:a274ee790e56 477
mbed_official 592:a274ee790e56 478 case RTC_COUNT_MODE_16BIT:
mbed_official 592:a274ee790e56 479 /* Check sanity of comp_index. */
mbed_official 592:a274ee790e56 480 if ((uint32_t)comp_index > RTC_NUM_OF_COMP16) {
mbed_official 592:a274ee790e56 481 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 482 }
mbed_official 592:a274ee790e56 483
mbed_official 592:a274ee790e56 484 /* Get compare value for COMP. */
mbed_official 592:a274ee790e56 485 *comp_value = (uint32_t)rtc_module->MODE1.COMP[comp_index].reg;
mbed_official 592:a274ee790e56 486
mbed_official 592:a274ee790e56 487 break;
mbed_official 592:a274ee790e56 488
mbed_official 592:a274ee790e56 489 default:
mbed_official 592:a274ee790e56 490 Assert(false);
mbed_official 592:a274ee790e56 491 return STATUS_ERR_BAD_FORMAT;
mbed_official 592:a274ee790e56 492 }
mbed_official 592:a274ee790e56 493 /* Return status showing everything is OK. */
mbed_official 592:a274ee790e56 494 return STATUS_OK;
mbed_official 592:a274ee790e56 495 }
mbed_official 592:a274ee790e56 496
mbed_official 592:a274ee790e56 497 /**
mbed_official 592:a274ee790e56 498 * \brief Retrieves the value of period.
mbed_official 592:a274ee790e56 499 *
mbed_official 592:a274ee790e56 500 * Retrieves the value of the period for the 16-bit mode counter.
mbed_official 592:a274ee790e56 501 *
mbed_official 592:a274ee790e56 502 * \note Only available in 16-bit mode.
mbed_official 592:a274ee790e56 503 *
mbed_official 592:a274ee790e56 504 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 505 * \param[out] period_value Pointer to value for return argument.
mbed_official 592:a274ee790e56 506 *
mbed_official 592:a274ee790e56 507 * \return Status of getting the period value.
mbed_official 592:a274ee790e56 508 * \retval STATUS_OK If the period value was read correctly.
mbed_official 592:a274ee790e56 509 * \retval STATUS_ERR_UNSUPPORTED_DEV If incorrect mode was set.
mbed_official 592:a274ee790e56 510 */
mbed_official 592:a274ee790e56 511 enum status_code rtc_count_get_period(
mbed_official 592:a274ee790e56 512 struct rtc_module *const module,
mbed_official 592:a274ee790e56 513 uint16_t *const period_value)
mbed_official 592:a274ee790e56 514 {
mbed_official 592:a274ee790e56 515 /* Sanity check arguments */
mbed_official 592:a274ee790e56 516 Assert(module);
mbed_official 592:a274ee790e56 517 Assert(module->hw);
mbed_official 592:a274ee790e56 518
mbed_official 592:a274ee790e56 519 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 520
mbed_official 592:a274ee790e56 521 /* Check that correct mode is set. */
mbed_official 592:a274ee790e56 522 if (module->mode != RTC_COUNT_MODE_16BIT) {
mbed_official 592:a274ee790e56 523 return STATUS_ERR_UNSUPPORTED_DEV;
mbed_official 592:a274ee790e56 524 }
mbed_official 592:a274ee790e56 525
mbed_official 592:a274ee790e56 526 /* Returns the value. */
mbed_official 592:a274ee790e56 527 *period_value = rtc_module->MODE1.PER.reg;
mbed_official 592:a274ee790e56 528
mbed_official 592:a274ee790e56 529 return STATUS_OK;
mbed_official 592:a274ee790e56 530 }
mbed_official 592:a274ee790e56 531
mbed_official 592:a274ee790e56 532 /**
mbed_official 592:a274ee790e56 533 * \brief Set the given value to the period.
mbed_official 592:a274ee790e56 534 *
mbed_official 592:a274ee790e56 535 * Sets the given value to the period.
mbed_official 592:a274ee790e56 536 *
mbed_official 592:a274ee790e56 537 * \note Only available in 16-bit mode.
mbed_official 592:a274ee790e56 538 *
mbed_official 592:a274ee790e56 539 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 540 * \param[in] period_value The value to set to the period.
mbed_official 592:a274ee790e56 541 *
mbed_official 592:a274ee790e56 542 * \return Status of setting the period value.
mbed_official 592:a274ee790e56 543 * \retval STATUS_OK If the period was set correctly.
mbed_official 592:a274ee790e56 544 * \retval STATUS_ERR_UNSUPPORTED_DEV If module is not operated in 16-bit mode.
mbed_official 592:a274ee790e56 545 */
mbed_official 592:a274ee790e56 546 enum status_code rtc_count_set_period(
mbed_official 592:a274ee790e56 547 struct rtc_module *const module,
mbed_official 592:a274ee790e56 548 const uint16_t period_value)
mbed_official 592:a274ee790e56 549 {
mbed_official 592:a274ee790e56 550 /* Sanity check arguments */
mbed_official 592:a274ee790e56 551 Assert(module);
mbed_official 592:a274ee790e56 552 Assert(module->hw);
mbed_official 592:a274ee790e56 553
mbed_official 592:a274ee790e56 554 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 555
mbed_official 592:a274ee790e56 556 /* Check that correct mode is set. */
mbed_official 592:a274ee790e56 557 if (module->mode != RTC_COUNT_MODE_16BIT) {
mbed_official 592:a274ee790e56 558 return STATUS_ERR_UNSUPPORTED_DEV;
mbed_official 592:a274ee790e56 559 }
mbed_official 592:a274ee790e56 560
mbed_official 592:a274ee790e56 561 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 562 /* Wait for synchronization */
mbed_official 592:a274ee790e56 563 }
mbed_official 592:a274ee790e56 564
mbed_official 592:a274ee790e56 565 /* Write value to register. */
mbed_official 592:a274ee790e56 566 rtc_module->MODE1.PER.reg = period_value;
mbed_official 592:a274ee790e56 567
mbed_official 592:a274ee790e56 568 return STATUS_OK;
mbed_official 592:a274ee790e56 569 }
mbed_official 592:a274ee790e56 570
mbed_official 592:a274ee790e56 571 /**
mbed_official 592:a274ee790e56 572 * \brief Check if RTC compare match has occurred.
mbed_official 592:a274ee790e56 573 *
mbed_official 592:a274ee790e56 574 * Checks the compare flag to see if a match has occurred. The compare flag is
mbed_official 592:a274ee790e56 575 * set when there is a compare match between counter and the compare.
mbed_official 592:a274ee790e56 576 *
mbed_official 592:a274ee790e56 577 * \note Compare 4 and 5 are only available in 16-bit mode.
mbed_official 592:a274ee790e56 578 *
mbed_official 592:a274ee790e56 579 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 580 * \param[in] comp_index Index of compare to check current flag.
mbed_official 592:a274ee790e56 581 */
mbed_official 592:a274ee790e56 582 bool rtc_count_is_compare_match(
mbed_official 592:a274ee790e56 583 struct rtc_module *const module,
mbed_official 592:a274ee790e56 584 const enum rtc_count_compare comp_index)
mbed_official 592:a274ee790e56 585 {
mbed_official 592:a274ee790e56 586 /* Sanity check arguments */
mbed_official 592:a274ee790e56 587 Assert(module);
mbed_official 592:a274ee790e56 588 Assert(module->hw);
mbed_official 592:a274ee790e56 589
mbed_official 592:a274ee790e56 590 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 591
mbed_official 592:a274ee790e56 592 /* Check sanity. */
mbed_official 592:a274ee790e56 593 switch (module->mode) {
mbed_official 592:a274ee790e56 594 case RTC_COUNT_MODE_32BIT:
mbed_official 592:a274ee790e56 595 /* Check sanity for 32-bit mode. */
mbed_official 592:a274ee790e56 596 if (comp_index > RTC_NUM_OF_COMP32) {
mbed_official 592:a274ee790e56 597 return false;
mbed_official 592:a274ee790e56 598 }
mbed_official 592:a274ee790e56 599
mbed_official 592:a274ee790e56 600 break;
mbed_official 592:a274ee790e56 601
mbed_official 592:a274ee790e56 602 case RTC_COUNT_MODE_16BIT:
mbed_official 592:a274ee790e56 603 /* Check sanity for 16-bit mode. */
mbed_official 592:a274ee790e56 604 if (comp_index > RTC_NUM_OF_COMP16) {
mbed_official 592:a274ee790e56 605 return false;
mbed_official 592:a274ee790e56 606 }
mbed_official 592:a274ee790e56 607
mbed_official 592:a274ee790e56 608 break;
mbed_official 592:a274ee790e56 609
mbed_official 592:a274ee790e56 610 default:
mbed_official 592:a274ee790e56 611 Assert(false);
mbed_official 592:a274ee790e56 612 return false;
mbed_official 592:a274ee790e56 613 }
mbed_official 592:a274ee790e56 614
mbed_official 592:a274ee790e56 615 /* Set status of INTFLAG as return argument. */
mbed_official 592:a274ee790e56 616 return (rtc_module->MODE0.INTFLAG.reg & (1 << comp_index));
mbed_official 592:a274ee790e56 617 }
mbed_official 592:a274ee790e56 618
mbed_official 592:a274ee790e56 619 /**
mbed_official 592:a274ee790e56 620 * \brief Clears RTC compare match flag.
mbed_official 592:a274ee790e56 621 *
mbed_official 592:a274ee790e56 622 * Clears the compare flag. The compare flag is set when there is a compare
mbed_official 592:a274ee790e56 623 * match between the counter and the compare.
mbed_official 592:a274ee790e56 624 *
mbed_official 592:a274ee790e56 625 * \note Compare 4 and 5 are only available in 16-bit mode.
mbed_official 592:a274ee790e56 626 *
mbed_official 592:a274ee790e56 627 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 628 * \param[in] comp_index Index of compare to check current flag.
mbed_official 592:a274ee790e56 629 *
mbed_official 592:a274ee790e56 630 * \return Status indicating if flag was successfully cleared.
mbed_official 592:a274ee790e56 631 * \retval STATUS_OK If flag was successfully cleared.
mbed_official 592:a274ee790e56 632 * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided.
mbed_official 592:a274ee790e56 633 * \retval STATUS_ERR_BAD_FORMAT If the module was not initialized in a mode.
mbed_official 592:a274ee790e56 634 */
mbed_official 592:a274ee790e56 635 enum status_code rtc_count_clear_compare_match(
mbed_official 592:a274ee790e56 636 struct rtc_module *const module,
mbed_official 592:a274ee790e56 637 const enum rtc_count_compare comp_index)
mbed_official 592:a274ee790e56 638 {
mbed_official 592:a274ee790e56 639 /* Sanity check arguments */
mbed_official 592:a274ee790e56 640 Assert(module);
mbed_official 592:a274ee790e56 641 Assert(module->hw);
mbed_official 592:a274ee790e56 642
mbed_official 592:a274ee790e56 643 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 644
mbed_official 592:a274ee790e56 645 /* Check sanity. */
mbed_official 592:a274ee790e56 646 switch (module->mode) {
mbed_official 592:a274ee790e56 647 case RTC_COUNT_MODE_32BIT:
mbed_official 592:a274ee790e56 648 /* Check sanity for 32-bit mode. */
mbed_official 592:a274ee790e56 649 if (comp_index > RTC_NUM_OF_COMP32) {
mbed_official 592:a274ee790e56 650 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 651 }
mbed_official 592:a274ee790e56 652
mbed_official 592:a274ee790e56 653 break;
mbed_official 592:a274ee790e56 654
mbed_official 592:a274ee790e56 655 case RTC_COUNT_MODE_16BIT:
mbed_official 592:a274ee790e56 656 /* Check sanity for 16-bit mode. */
mbed_official 592:a274ee790e56 657 if (comp_index > RTC_NUM_OF_COMP16) {
mbed_official 592:a274ee790e56 658 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 659 }
mbed_official 592:a274ee790e56 660
mbed_official 592:a274ee790e56 661 break;
mbed_official 592:a274ee790e56 662
mbed_official 592:a274ee790e56 663 default:
mbed_official 592:a274ee790e56 664 Assert(false);
mbed_official 592:a274ee790e56 665 return STATUS_ERR_BAD_FORMAT;
mbed_official 592:a274ee790e56 666 }
mbed_official 592:a274ee790e56 667
mbed_official 592:a274ee790e56 668 /* Clear INTFLAG. */
mbed_official 592:a274ee790e56 669 rtc_module->MODE0.INTFLAG.reg = RTC_MODE1_INTFLAG_CMP(1 << comp_index);
mbed_official 592:a274ee790e56 670
mbed_official 592:a274ee790e56 671 return STATUS_OK;
mbed_official 592:a274ee790e56 672 }
mbed_official 592:a274ee790e56 673
mbed_official 592:a274ee790e56 674 /**
mbed_official 592:a274ee790e56 675 * \brief Calibrate for too-slow or too-fast oscillator.
mbed_official 592:a274ee790e56 676 *
mbed_official 592:a274ee790e56 677 * When used, the RTC will compensate for an inaccurate oscillator. The
mbed_official 592:a274ee790e56 678 * RTC module will add or subtract cycles from the RTC prescaler to adjust the
mbed_official 592:a274ee790e56 679 * frequency in approximately 1 PPM steps. The provided correction value should
mbed_official 592:a274ee790e56 680 * be between 0 and 127, allowing for a maximum 127 PPM correction.
mbed_official 592:a274ee790e56 681 *
mbed_official 592:a274ee790e56 682 * If no correction is needed, set value to zero.
mbed_official 592:a274ee790e56 683 *
mbed_official 592:a274ee790e56 684 * \note Can only be used when the RTC is operated in 1Hz.
mbed_official 592:a274ee790e56 685 *
mbed_official 592:a274ee790e56 686 * \param[in,out] module Pointer to the software instance struct
mbed_official 592:a274ee790e56 687 * \param[in] value Ranging from -127 to 127 used for the correction.
mbed_official 592:a274ee790e56 688 *
mbed_official 592:a274ee790e56 689 * \return Status of the calibration procedure.
mbed_official 592:a274ee790e56 690 * \retval STATUS_OK If calibration was executed correctly.
mbed_official 592:a274ee790e56 691 * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided.
mbed_official 592:a274ee790e56 692 */
mbed_official 592:a274ee790e56 693 enum status_code rtc_count_frequency_correction(
mbed_official 592:a274ee790e56 694 struct rtc_module *const module,
mbed_official 592:a274ee790e56 695 const int8_t value)
mbed_official 592:a274ee790e56 696 {
mbed_official 592:a274ee790e56 697 /* Sanity check arguments */
mbed_official 592:a274ee790e56 698 Assert(module);
mbed_official 592:a274ee790e56 699 Assert(module->hw);
mbed_official 592:a274ee790e56 700
mbed_official 592:a274ee790e56 701 Rtc *const rtc_module = module->hw;
mbed_official 592:a274ee790e56 702
mbed_official 592:a274ee790e56 703 /* Check if valid argument. */
mbed_official 592:a274ee790e56 704 if (abs(value) > 0x7F) {
mbed_official 592:a274ee790e56 705 /* Value bigger than allowed, return invalid argument. */
mbed_official 592:a274ee790e56 706 return STATUS_ERR_INVALID_ARG;
mbed_official 592:a274ee790e56 707 }
mbed_official 592:a274ee790e56 708
mbed_official 592:a274ee790e56 709 uint32_t new_correction_value;
mbed_official 592:a274ee790e56 710
mbed_official 592:a274ee790e56 711 /* Load the new correction value as a positive value, sign added later */
mbed_official 592:a274ee790e56 712 new_correction_value = abs(value);
mbed_official 592:a274ee790e56 713
mbed_official 592:a274ee790e56 714 /* Convert to positive value and adjust register sign bit. */
mbed_official 592:a274ee790e56 715 if (value < 0) {
mbed_official 592:a274ee790e56 716 new_correction_value |= RTC_FREQCORR_SIGN;
mbed_official 592:a274ee790e56 717 }
mbed_official 592:a274ee790e56 718
mbed_official 592:a274ee790e56 719 while (rtc_count_is_syncing(module)) {
mbed_official 592:a274ee790e56 720 /* Wait for synchronization */
mbed_official 592:a274ee790e56 721 }
mbed_official 592:a274ee790e56 722
mbed_official 592:a274ee790e56 723 /* Set value. */
mbed_official 592:a274ee790e56 724 rtc_module->MODE0.FREQCORR.reg = new_correction_value;
mbed_official 592:a274ee790e56 725
mbed_official 592:a274ee790e56 726 return STATUS_OK;
mbed_official 592:a274ee790e56 727 }
mbed_official 592:a274ee790e56 728