Lines Matching +full:divider +full:- +full:val

4  * SPDX-License-Identifier: Apache-2.0
35 timer_source_div_t source_div; /* Clock source divider */
55 /* Alarm-related data */
67 static uint32_t r_agt_ticks_sub(uint32_t val, uint32_t old, uint32_t top);
74 reg->AGTCR = AGT_AGTCR_START_TIMER; in counter_ra_agt_start()
76 while (!(reg->AGTCR & BIT(R_AGTX0_AGT16_CTRL_AGTCR_TCSTF_Pos)) && likely(--timeout)) in counter_ra_agt_start()
79 return timeout > 0 ? 0 : -EIO; in counter_ra_agt_start()
87 reg->AGTCR = AGT_AGTCR_STOP_TIMER; in counter_ra_agt_stop()
89 while ((reg->AGTCR & BIT(R_AGTX0_AGT16_CTRL_AGTCR_TCSTF_Pos)) && likely(--timeout)) in counter_ra_agt_stop()
92 return timeout > 0 ? 0 : -EIO; in counter_ra_agt_stop()
97 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_read()
99 return data->agt_reg->AGT16.AGT; in counter_ra_agt_read()
109 const struct counter_ra_agt_config *config = dev->config; in counter_ra_agt_get_top_value()
111 return config->info.max_top_value; in counter_ra_agt_get_top_value()
116 const struct counter_ra_agt_config *config = dev->config; in counter_ra_agt_set_top_value()
117 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_set_top_value()
120 if (cfg->ticks != config->info.max_top_value) { in counter_ra_agt_set_top_value()
121 return -ENOTSUP; in counter_ra_agt_set_top_value()
124 if (cfg->callback == NULL) { in counter_ra_agt_set_top_value()
126 p_reg_ctrl->AGTCR_b.TCMAF = 0; in counter_ra_agt_set_top_value()
129 p_reg_ctrl->AGTCR_b.TCMAF = 1; in counter_ra_agt_set_top_value()
132 data->top_cb = cfg->callback; in counter_ra_agt_set_top_value()
133 data->top_cb_data = cfg->user_data; in counter_ra_agt_set_top_value()
141 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_set_compare_value()
143 data->agt_reg->AGT16.AGTCMA = value; in counter_ra_agt_set_compare_value()
148 const struct counter_ra_agt_config *config = dev->config; in counter_ra_agt_enable_channel_irq()
152 p_reg_ctrl->AGTCMSR |= BIT(R_AGTX0_AGT16_CTRL_AGTCMSR_TCMEA_Pos); in counter_ra_agt_enable_channel_irq()
153 irq_enable(config->channel_irq); in counter_ra_agt_enable_channel_irq()
158 const struct counter_ra_agt_config *config = dev->config; in counter_ra_agt_clear_channel_irq()
161 reg_ctrl->AGTCR_b.TCMAF = 0; in counter_ra_agt_clear_channel_irq()
162 R_BSP_IrqStatusClear(config->channel_irq); in counter_ra_agt_clear_channel_irq()
163 NVIC_ClearPendingIRQ(config->channel_irq); in counter_ra_agt_clear_channel_irq()
169 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_set_alarm()
170 const struct counter_ra_agt_config *config = dev->config; in counter_ra_agt_set_alarm()
172 const bool absolute = alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE; in counter_ra_agt_set_alarm()
174 struct counter_ra_agt_alarm *const alarm = &data->alarm; in counter_ra_agt_set_alarm()
175 uint16_t val = alarm_cfg->ticks; in counter_ra_agt_set_alarm() local
182 if (alarm_cfg->ticks > counter_ra_agt_get_top_value(dev)) { in counter_ra_agt_set_alarm()
184 return -EINVAL; in counter_ra_agt_set_alarm()
187 if (alarm->callback) { in counter_ra_agt_set_alarm()
189 return -EBUSY; in counter_ra_agt_set_alarm()
191 alarm->callback = alarm_cfg->callback; in counter_ra_agt_set_alarm()
192 alarm->data = alarm_cfg->user_data; in counter_ra_agt_set_alarm()
196 * occur reference: RA6M5 User manual - 22.3.2 Reload Register and AGT Compare Match A/B in counter_ra_agt_set_alarm()
204 max_rel_val = top - data->guard_period; in counter_ra_agt_set_alarm()
205 irq_on_late = alarm_cfg->flags & COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE; in counter_ra_agt_set_alarm()
216 irq_on_late = (val < (top / 2U)); in counter_ra_agt_set_alarm()
220 val = r_agt_ticks_sub(now, val, top); in counter_ra_agt_set_alarm()
223 counter_ra_agt_set_compare_value(dev, chan, val); in counter_ra_agt_set_alarm()
225 /* Decrement value to detect also case when val == counter_ra_agt_read(dev). in counter_ra_agt_set_alarm()
228 diff = r_agt_ticks_sub(now, val - 1, top); in counter_ra_agt_set_alarm()
232 err = -ETIME; in counter_ra_agt_set_alarm()
241 NVIC_SetPendingIRQ(config->channel_irq); in counter_ra_agt_set_alarm()
243 alarm->callback = NULL; in counter_ra_agt_set_alarm()
244 alarm->data = NULL; in counter_ra_agt_set_alarm()
257 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_cancel_alarm()
262 p_reg_ctrl->AGTCMSR &= ~BIT(R_AGTX0_AGT16_CTRL_AGTCMSR_TCMEA_Pos); in counter_ra_agt_cancel_alarm()
264 data->alarm.callback = NULL; in counter_ra_agt_cancel_alarm()
265 data->alarm.data = NULL; in counter_ra_agt_cancel_alarm()
274 return (reg->AGTCR & AGT_AGTCR_FLAGS_MASK) != 0; in counter_ra_agt_get_pending_int()
279 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_get_freq()
287 info.period_counts = data->period; in counter_ra_agt_get_freq()
298 const struct counter_ra_agt_config *config = dev->config; in counter_ra_agt_agti_isr()
299 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_agti_isr()
302 R_BSP_IrqStatusClear(config->cycle_end_irq); in counter_ra_agt_agti_isr()
304 const uint32_t agtcr = reg_ctrl->AGTCR; in counter_ra_agt_agti_isr()
307 if (data->top_cb) { in counter_ra_agt_agti_isr()
308 data->top_cb(dev, data->top_cb_data); in counter_ra_agt_agti_isr()
312 reg_ctrl->AGTCR = (uint8_t)(agtcr & ~(BIT(R_AGTX0_AGT16_CTRL_AGTCR_TUNDF_Pos) | in counter_ra_agt_agti_isr()
318 const struct counter_ra_agt_config *config = dev->config; in counter_ra_agt_agtcmai_isr()
319 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_agtcmai_isr()
321 struct counter_ra_agt_alarm *const alarm = &data->alarm; in counter_ra_agt_agtcmai_isr()
323 const uint32_t val = data->agt_reg->AGT16.AGTCMA; in counter_ra_agt_agtcmai_isr() local
325 const counter_alarm_callback_t cb = alarm->callback; in counter_ra_agt_agtcmai_isr()
326 void *cb_data = alarm->data; in counter_ra_agt_agtcmai_isr()
328 alarm->callback = NULL; in counter_ra_agt_agtcmai_isr()
329 alarm->data = NULL; in counter_ra_agt_agtcmai_isr()
332 reg_ctrl->AGTCMSR &= ~BIT(R_AGTX0_AGT16_CTRL_AGTCMSR_TCMEA_Pos); in counter_ra_agt_agtcmai_isr()
334 R_BSP_IrqStatusClear(config->channel_irq); in counter_ra_agt_agtcmai_isr()
337 cb(dev, config->channel, val, cb_data); in counter_ra_agt_agtcmai_isr()
340 reg_ctrl->AGTCR_b.TCMAF = 0; in counter_ra_agt_agtcmai_isr()
345 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_get_guard_period()
347 return data->guard_period; in counter_ra_agt_get_guard_period()
352 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_set_guard_period()
356 return -EINVAL; in counter_ra_agt_set_guard_period()
359 data->guard_period = guard; in counter_ra_agt_set_guard_period()
366 const struct counter_ra_agt_config *config = dev->config; in counter_ra_agt_init()
367 struct counter_ra_agt_data *data = dev->data; in counter_ra_agt_init()
369 data->agt_reg = (R_AGTX0_Type *)config->dt_reg; in counter_ra_agt_init()
374 R_BSP_MODULE_START(FSP_IP_AGT, config->channel); in counter_ra_agt_init()
377 p_reg_ctrl->AGTCR = 0U; in counter_ra_agt_init()
382 FSP_HARDWARE_REGISTER_WAIT(0U, p_reg_ctrl->AGTCR_b.TCSTF); in counter_ra_agt_init()
387 p_reg_ctrl->AGTMR2 = 0U; in counter_ra_agt_init()
389 /* Set count source and divider and configure pins */ in counter_ra_agt_init()
392 /* Set period register and update duty cycle if output mode is used for one-shot or periodic in counter_ra_agt_init()
395 r_agt_period_register_set(dev, data->period_counts); in counter_ra_agt_init()
398 p_reg_ctrl->AGTCMSR |= BIT(R_AGTX0_AGT16_CTRL_AGTCMSR_TCMEA_Pos); in counter_ra_agt_init()
405 struct counter_ra_agt_data *data = dev->data; in r_agt_reg_ctrl_get()
406 agt_reg_ctrl_t *p_reg_ctrl = &data->agt_reg->AGT16.CTRL; in r_agt_reg_ctrl_get()
413 const struct counter_ra_agt_config *config = dev->config; in r_agt_hardware_cfg()
415 /* Update the divider for PCLKB */ in r_agt_hardware_cfg()
417 uint32_t count_source_int = (uint32_t)config->count_source; in r_agt_hardware_cfg()
421 uint32_t agtioc = config->agtio_filter; in r_agt_hardware_cfg()
422 uint32_t mode = config->measurement_mode & R_AGTX0_AGT16_CTRL_AGTMR1_TMOD_Msk; in r_agt_hardware_cfg()
425 if (AGT_CLOCK_PCLKB == config->count_source) { in r_agt_hardware_cfg()
426 if (TIMER_SOURCE_DIV_1 != config->source_div) { in r_agt_hardware_cfg()
430 count_source_int = config->source_div ^ 2U; in r_agt_hardware_cfg()
435 else if (AGT_CLOCK_AGT_UNDERFLOW != config->count_source) { in r_agt_hardware_cfg()
436 /* Update the divider for LOCO/subclock */ in r_agt_hardware_cfg()
437 agtmr2 = config->source_div; in r_agt_hardware_cfg()
439 /* No divider can be used when count source is AGT_CLOCK_AGT_UNDERFLOW */ in r_agt_hardware_cfg()
448 p_reg_ctrl->AGTIOC = (uint8_t)agtioc; in r_agt_hardware_cfg()
449 p_reg_ctrl->AGTCMSR = (uint8_t)agtcmsr; in r_agt_hardware_cfg()
450 p_reg_ctrl->AGTMR1 = (uint8_t)agtmr1; in r_agt_hardware_cfg()
451 p_reg_ctrl->AGTMR2 = (uint8_t)agtmr2; in r_agt_hardware_cfg()
456 struct counter_ra_agt_data *data = dev->data; in r_agt_period_register_set()
459 data->period = period_counts; in r_agt_period_register_set()
462 uint32_t period_reg = (period_counts - 1U); in r_agt_period_register_set()
464 data->agt_reg->AGT16.AGT = (uint16_t)period_reg; in r_agt_period_register_set()
470 uint8_t count_source_int = p_reg->AGTMR1_b.TCK; in r_agt_clock_frequency_get()
471 timer_source_div_t divider = TIMER_SOURCE_DIV_1; in r_agt_clock_frequency_get() local
478 divider = (timer_source_div_t)count_source_int; in r_agt_clock_frequency_get()
480 if (divider != 0U) { in r_agt_clock_frequency_get()
481 /* Set divider to 3 to divide by 8 when AGTMR1.TCK is 1 (PCLKB / 8). Set in r_agt_clock_frequency_get()
482 * divider to 1 to divide by 2 when AGTMR1.TCK is 3 (PCLKB / 2). XOR with 2 in r_agt_clock_frequency_get()
485 divider ^= 2U; in r_agt_clock_frequency_get()
492 divider = (timer_source_div_t)p_reg->AGTMR2_b.CKS; in r_agt_clock_frequency_get()
495 clock_freq_hz >>= divider; in r_agt_clock_frequency_get()
505 uint32_t agtcr_tstart = p_reg->AGTCR_b.TSTART; in r_agt_common_preamble()
507 FSP_HARDWARE_REGISTER_WAIT(agtcr_tstart, p_reg->AGTCR_b.TCSTF); in r_agt_common_preamble()
512 static uint32_t r_agt_ticks_sub(uint32_t val, uint32_t old, uint32_t top) in r_agt_ticks_sub() argument
515 return (val - old) & top; in r_agt_ticks_sub()
518 /* if top is not 2^n-1 */ in r_agt_ticks_sub()
519 return (val >= old) ? (val - old) : val + top + 1 - old; in r_agt_ticks_sub()
572 R_ICU->IELSR[DT_IRQ_BY_NAME(TIMER(n), agti, irq)] = \
579 R_ICU->IELSR[DT_IRQ_BY_NAME(TIMER(n), agtcmai, irq)] = \