• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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