1 /*
2 * Copyright (c) 2021 HPMicro
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #include "hpm_gptmr_drv.h"
9
gptmr_channel_get_default_config(GPTMR_Type * ptr,gptmr_channel_config_t * config)10 void gptmr_channel_get_default_config(GPTMR_Type *ptr, gptmr_channel_config_t *config)
11 {
12 (void) ptr;
13 config->mode = gptmr_work_mode_no_capture;
14 config->dma_request_event = gptmr_dma_request_disabled;
15 config->synci_edge = gptmr_synci_edge_none;
16 for (uint8_t i = 0; i < GPTMR_CH_CMP_COUNT; i++) {
17 config->cmp[i] = 0;
18 }
19 config->reload = 0xFFFFFFFFUL;
20 config->cmp_initial_polarity_high = true;
21 config->enable_cmp_output = true;
22 config->enable_sync_follow_previous_channel = false;
23 config->enable_software_sync = false;
24 config->debug_mode = true;
25 }
26
gptmr_channel_config(GPTMR_Type * ptr,uint8_t ch_index,gptmr_channel_config_t * config,bool enable)27 hpm_stat_t gptmr_channel_config(GPTMR_Type *ptr,
28 uint8_t ch_index,
29 gptmr_channel_config_t *config,
30 bool enable)
31 {
32 uint32_t v = 0;
33 uint32_t tmp_value;
34
35 if (config->enable_sync_follow_previous_channel && !ch_index) {
36 return status_invalid_argument;
37 }
38
39 if (config->dma_request_event != gptmr_dma_request_disabled) {
40 v |= GPTMR_CHANNEL_CR_DMAEN_MASK
41 | GPTMR_CHANNEL_CR_DMASEL_SET(config->dma_request_event);
42 }
43 v |= GPTMR_CHANNEL_CR_CAPMODE_SET(config->mode)
44 | GPTMR_CHANNEL_CR_DBGPAUSE_SET(config->debug_mode)
45 | GPTMR_CHANNEL_CR_SWSYNCIEN_SET(config->enable_software_sync)
46 | GPTMR_CHANNEL_CR_CMPINIT_SET(config->cmp_initial_polarity_high)
47 | GPTMR_CHANNEL_CR_SYNCFLW_SET(config->enable_sync_follow_previous_channel)
48 | GPTMR_CHANNEL_CR_CMPEN_SET(config->enable_cmp_output)
49 | GPTMR_CHANNEL_CR_CEN_SET(enable)
50 | config->synci_edge;
51
52 for (uint8_t i = GPTMR_CH_CMP_COUNT; i > 0; i--) {
53 tmp_value = config->cmp[i - 1];
54 if (tmp_value > 0) {
55 tmp_value--;
56 }
57 ptr->CHANNEL[ch_index].CMP[i - 1] = GPTMR_CHANNEL_CMP_CMP_SET(tmp_value);
58 }
59 tmp_value = config->reload;
60 if (tmp_value > 0) {
61 tmp_value--;
62 }
63 ptr->CHANNEL[ch_index].RLD = GPTMR_CHANNEL_RLD_RLD_SET(tmp_value);
64 ptr->CHANNEL[ch_index].CR = v;
65 return status_success;
66 }
67
68 #if defined(HPM_IP_FEATURE_GPTMR_MONITOR) && (HPM_IP_FEATURE_GPTMR_MONITOR == 1)
gptmr_channel_get_default_monitor_config(GPTMR_Type * ptr,gptmr_channel_monitor_config_t * config)69 void gptmr_channel_get_default_monitor_config(GPTMR_Type *ptr, gptmr_channel_monitor_config_t *config)
70 {
71 (void) ptr;
72 config->max_value = 0;
73 config->min_value = 0;
74 config->monitor_type = monitor_signal_high_level_time;
75 }
76
gptmr_channel_monitor_config(GPTMR_Type * ptr,uint8_t ch_index,gptmr_channel_monitor_config_t * config,bool enable)77 hpm_stat_t gptmr_channel_monitor_config(GPTMR_Type *ptr, uint8_t ch_index, gptmr_channel_monitor_config_t *config, bool enable)
78 {
79 if ((ptr == NULL) || (config->max_value < config->min_value)) {
80 return status_invalid_argument;
81 }
82 gptmr_channel_set_monitor_type(ptr, ch_index, config->monitor_type);
83 gptmr_update_cmp(ptr, ch_index, 0, config->min_value + 1);
84 gptmr_update_cmp(ptr, ch_index, 1, config->max_value + 1);
85 gptmr_channel_config_update_reload(ptr, ch_index, 0xFFFFFFFF);
86 gptmr_channel_set_capmode(ptr, ch_index, gptmr_work_mode_measure_width);
87 if (enable == true) {
88 gptmr_channel_reset_count(ptr, ch_index);
89 gptmr_channel_enable_monitor(ptr, ch_index);
90 } else {
91 gptmr_channel_disable_monitor(ptr, ch_index);
92 }
93 return status_success;
94 }
95
96 #endif
97