Lines Matching +full:current +full:- +full:limiting

5  * SPDX-License-Identifier: Apache-2.0
20 #include <zephyr/dt-bindings/pinctrl/ifx_cat1-pinctrl.h>
72 uintptr_t cnt = POINTER_TO_UINT(_CYHAL_TCPWM_DATA[i].base->CNT); in get_hw_block_info()
78 hw_resource->type = CYHAL_RSC_TCPWM; in get_hw_block_info()
79 hw_resource->block_num = i; in get_hw_block_info()
80 hw_resource->channel_num = ((reg_addr_ptr - cnt) / sizeof(TCPWM_CNT_Type)); in get_hw_block_info()
82 if (hw_resource->channel_num >= _CYHAL_TCPWM_DATA[i].num_channels) { in get_hw_block_info()
83 return -EINVAL; in get_hw_block_info()
88 return -EINVAL; in get_hw_block_info()
94 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_event_callback()
95 const struct ifx_cat1_counter_config *const config = dev->config; in ifx_cat1_counter_event_callback()
98 if ((data->alarm_cfg_counter.callback != NULL) && in ifx_cat1_counter_event_callback()
100 data->alarm_irq_flag)) { in ifx_cat1_counter_event_callback()
101 /* Alarm works as one-shot, so disable event */ in ifx_cat1_counter_event_callback()
102 cyhal_timer_enable_event(&data->counter_obj, CYHAL_TIMER_IRQ_CAPTURE_COMPARE, in ifx_cat1_counter_event_callback()
103 config->irq_priority, false); in ifx_cat1_counter_event_callback()
106 data->alarm_cfg_counter.callback(dev, 1, cyhal_timer_read(&data->counter_obj), in ifx_cat1_counter_event_callback()
107 data->alarm_cfg_counter.user_data); in ifx_cat1_counter_event_callback()
108 data->alarm_irq_flag = false; in ifx_cat1_counter_event_callback()
112 if ((data->top_value_cfg_counter.callback != NULL) && in ifx_cat1_counter_event_callback()
116 data->top_value_cfg_counter.callback(dev, data->top_value_cfg_counter.user_data); in ifx_cat1_counter_event_callback()
126 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_set_int_pending()
127 const struct ifx_cat1_counter_config *const config = dev->config; in ifx_cat1_counter_set_int_pending()
129 cyhal_timer_enable_event(&data->counter_obj, CYHAL_TIMER_IRQ_CAPTURE_COMPARE, in ifx_cat1_counter_set_int_pending()
130 config->irq_priority, true); in ifx_cat1_counter_set_int_pending()
131 Cy_TCPWM_SetInterrupt(data->counter_obj.tcpwm.base, in ifx_cat1_counter_set_int_pending()
132 _CYHAL_TCPWM_CNT_NUMBER(data->counter_obj.tcpwm.resource), in ifx_cat1_counter_set_int_pending()
141 struct ifx_cat1_counter_data *data = dev->data; in ifx_cat1_counter_init()
142 const struct ifx_cat1_counter_config *config = dev->config; in ifx_cat1_counter_init()
145 if (get_hw_block_info(config->reg_addr, &data->hw_resource) != 0) { in ifx_cat1_counter_init()
146 return -EIO; in ifx_cat1_counter_init()
150 .resource = &data->hw_resource, in ifx_cat1_counter_init()
155 rslt = cyhal_timer_init_cfg(&data->counter_obj, &timer_configurator); in ifx_cat1_counter_init()
157 return -EIO; in ifx_cat1_counter_init()
161 data->alarm_irq_flag = false; in ifx_cat1_counter_init()
162 data->counter_cfg.compare_value = 0; in ifx_cat1_counter_init()
163 data->counter_cfg.period = config->counter_info.max_top_value; in ifx_cat1_counter_init()
164 data->counter_cfg.direction = CYHAL_TIMER_DIR_UP; in ifx_cat1_counter_init()
165 data->counter_cfg.is_compare = true; in ifx_cat1_counter_init()
166 data->counter_cfg.is_continuous = true; in ifx_cat1_counter_init()
167 data->counter_cfg.value = 0; in ifx_cat1_counter_init()
170 rslt = cyhal_timer_configure(&data->counter_obj, &data->counter_cfg); in ifx_cat1_counter_init()
172 return -EIO; in ifx_cat1_counter_init()
175 if (config->external_pin == NC) { in ifx_cat1_counter_init()
177 rslt = cyhal_timer_set_frequency(&data->counter_obj, config->counter_info.freq); in ifx_cat1_counter_init()
179 return -EIO; in ifx_cat1_counter_init()
182 rslt = cyhal_gpio_init(config->external_pin, CYHAL_GPIO_DIR_INPUT, in ifx_cat1_counter_init()
186 return -EIO; in ifx_cat1_counter_init()
189 rslt = cyhal_gpio_enable_output(config->external_pin, CYHAL_SIGNAL_TYPE_EDGE, in ifx_cat1_counter_init()
190 (cyhal_source_t *)&data->signal_source); in ifx_cat1_counter_init()
194 return -EIO; in ifx_cat1_counter_init()
198 rslt = cyhal_timer_connect_digital(&data->counter_obj, data->signal_source, in ifx_cat1_counter_init()
202 return -EIO; in ifx_cat1_counter_init()
207 cyhal_timer_register_callback(&data->counter_obj, ifx_cat1_counter_event_callback, in ifx_cat1_counter_init()
217 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_start()
219 if (cyhal_timer_start(&data->counter_obj) != CY_RSLT_SUCCESS) { in ifx_cat1_counter_start()
220 return -EIO; in ifx_cat1_counter_start()
229 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_stop()
231 if (cyhal_timer_stop(&data->counter_obj) != CY_RSLT_SUCCESS) { in ifx_cat1_counter_stop()
232 return -EIO; in ifx_cat1_counter_stop()
242 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_get_value()
244 *ticks = cyhal_timer_read(&data->counter_obj); in ifx_cat1_counter_get_value()
256 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_set_top_value()
257 const struct ifx_cat1_counter_config *const config = dev->config; in ifx_cat1_counter_set_top_value()
260 data->top_value_cfg_counter = *cfg; in ifx_cat1_counter_set_top_value()
261 data->counter_cfg.period = cfg->ticks; in ifx_cat1_counter_set_top_value()
264 if (cfg->ticks > config->counter_info.max_top_value) { in ifx_cat1_counter_set_top_value()
265 return -ENOTSUP; in ifx_cat1_counter_set_top_value()
268 ticks_gt_period = cfg->ticks > data->counter_cfg.period; in ifx_cat1_counter_set_top_value()
270 if (!(cfg->flags & COUNTER_TOP_CFG_DONT_RESET)) { in ifx_cat1_counter_set_top_value()
271 data->counter_cfg.value = 0u; in ifx_cat1_counter_set_top_value()
272 } else if (ticks_gt_period && (cfg->flags & COUNTER_TOP_CFG_RESET_WHEN_LATE)) { in ifx_cat1_counter_set_top_value()
273 data->counter_cfg.value = 0u; in ifx_cat1_counter_set_top_value()
277 * counter value with current value of counter (read by in ifx_cat1_counter_set_top_value()
280 data->counter_cfg.value = cyhal_timer_read(&data->counter_obj); in ifx_cat1_counter_set_top_value()
284 ((ticks_gt_period == true) && (cfg->flags & COUNTER_TOP_CFG_RESET_WHEN_LATE))) { in ifx_cat1_counter_set_top_value()
287 if (config->external_pin == NC) { in ifx_cat1_counter_set_top_value()
288 rslt = cyhal_timer_configure(&data->counter_obj, &data->counter_cfg); in ifx_cat1_counter_set_top_value()
290 return -EIO; in ifx_cat1_counter_set_top_value()
293 TCPWM_CNT_PERIOD(data->counter_obj.tcpwm.base, in ifx_cat1_counter_set_top_value()
295 data->counter_obj.tcpwm.resource)) = cfg->ticks; in ifx_cat1_counter_set_top_value()
301 if (cfg->callback != NULL) { in ifx_cat1_counter_set_top_value()
302 cyhal_timer_enable_event(&data->counter_obj, CYHAL_TIMER_IRQ_TERMINAL_COUNT, in ifx_cat1_counter_set_top_value()
303 config->irq_priority, true); in ifx_cat1_counter_set_top_value()
313 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_get_top_value()
315 return data->counter_cfg.period; in ifx_cat1_counter_get_top_value()
320 /* Return true if value equals 2^n - 1 */ in counter_is_bit_mask()
328 /* refer to https://tbrindus.ca/how-builtin-expect-works/ for 'likely' usage */ in ifx_cat1_counter_ticks_add()
333 to_top = top - val1; in ifx_cat1_counter_ticks_add()
335 return (val2 <= to_top) ? (val1 + val2) : (val2 - to_top - 1U); in ifx_cat1_counter_ticks_add()
340 /* refer to https://tbrindus.ca/how-builtin-expect-works/ for 'likely' usage */ in ifx_cat1_counter_ticks_sub()
342 return (val - old) & top; in ifx_cat1_counter_ticks_sub()
345 /* if top is not 2^n-1 */ in ifx_cat1_counter_ticks_sub()
346 return (val >= old) ? (val - old) : (val + top + 1U - old); in ifx_cat1_counter_ticks_sub()
356 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_set_alarm()
357 const struct ifx_cat1_counter_config *const config = dev->config; in ifx_cat1_counter_set_alarm()
359 uint32_t val = alarm_cfg->ticks; in ifx_cat1_counter_set_alarm()
361 uint32_t flags = alarm_cfg->flags; in ifx_cat1_counter_set_alarm()
367 if (alarm_cfg->ticks > top_val) { in ifx_cat1_counter_set_alarm()
368 return -EINVAL; in ifx_cat1_counter_set_alarm()
372 max_rel_val = top_val - data->guard_period; in ifx_cat1_counter_set_alarm()
379 * by limiting relative distance between CC and counter. in ifx_cat1_counter_set_alarm()
387 val = ifx_cat1_counter_ticks_add(cyhal_timer_read(&data->counter_obj), val, in ifx_cat1_counter_set_alarm()
394 uint32_t curr = cyhal_timer_read(&data->counter_obj); in ifx_cat1_counter_set_alarm()
395 uint32_t diff = ifx_cat1_counter_ticks_sub((val - 1), curr, top_val); in ifx_cat1_counter_set_alarm()
403 data->alarm_irq_flag = true; in ifx_cat1_counter_set_alarm()
408 return -ETIME; in ifx_cat1_counter_set_alarm()
414 data->alarm_cfg_counter = *alarm_cfg; in ifx_cat1_counter_set_alarm()
415 data->counter_cfg.compare_value = val; in ifx_cat1_counter_set_alarm()
419 * counter value with current value of counter (read by in ifx_cat1_counter_set_alarm()
422 data->counter_cfg.value = cyhal_timer_read(&data->counter_obj); in ifx_cat1_counter_set_alarm()
425 if (config->external_pin == NC) { in ifx_cat1_counter_set_alarm()
426 rslt = cyhal_timer_configure(&data->counter_obj, &data->counter_cfg); in ifx_cat1_counter_set_alarm()
428 return -EINVAL; in ifx_cat1_counter_set_alarm()
431 TCPWM_CNT_CC(data->counter_obj.tcpwm.base, in ifx_cat1_counter_set_alarm()
432 _CYHAL_TCPWM_CNT_NUMBER(data->counter_obj.tcpwm.resource)) = in ifx_cat1_counter_set_alarm()
433 data->counter_cfg.compare_value; in ifx_cat1_counter_set_alarm()
436 cyhal_timer_enable_event(&data->counter_obj, CYHAL_TIMER_IRQ_CAPTURE_COMPARE, in ifx_cat1_counter_set_alarm()
437 config->irq_priority, true); in ifx_cat1_counter_set_alarm()
448 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_cancel_alarm()
449 const struct ifx_cat1_counter_config *const config = dev->config; in ifx_cat1_counter_cancel_alarm()
451 cyhal_timer_enable_event(&data->counter_obj, CYHAL_TIMER_IRQ_CAPTURE_COMPARE, in ifx_cat1_counter_cancel_alarm()
452 config->irq_priority, false); in ifx_cat1_counter_cancel_alarm()
460 const struct ifx_cat1_counter_config *const config = dev->config; in ifx_cat1_counter_get_pending_int()
462 return NVIC_GetPendingIRQ(config->irqn); in ifx_cat1_counter_get_pending_int()
470 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_get_guard_period()
472 return data->guard_period; in ifx_cat1_counter_get_guard_period()
482 struct ifx_cat1_counter_data *const data = dev->data; in ifx_cat1_counter_set_guard_period()
484 data->guard_period = guard; in ifx_cat1_counter_set_guard_period()