• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #ifndef HPM_DAC_DRV_H
8 #define HPM_DAC_DRV_H
9 
10 #include "hpm_common.h"
11 #include "hpm_dac_regs.h"
12 #include "hpm_soc_feature.h"
13 
14 /* The range of DAC output value setting is from 0.0 to 10000.0, which is mapped to 0 to 100 in percentage. */
15 #define DAC_OUTPUT(PERCENT) (PERCENT / 10000.0f * DAC_SOC_MAX_DATA)
16 
17 #define DAC_AHB_ERROR_EVENT     DAC_IRQ_EN_AHB_ERROR_MASK
18 #define DAC_FIFO_EMPTY_EVENT    DAC_IRQ_EN_FIFO_EMPTY_MASK
19 #define DAC_BUF1_COMPLETE_EVENT DAC_IRQ_EN_BUF1_CMPT_MASK
20 #define DAC_BUF0_COMPLETE_EVENT DAC_IRQ_EN_BUF0_CMPT_MASK
21 
22 
23 typedef enum {
24     dac_mode_direct = 0,
25     dac_mode_step,
26     dac_mode_buffer,
27     dac_mode_trig
28 } dac_mode_t;
29 
30 typedef enum {
31     dac_ana_div_2 = 0,
32     dac_ana_div_4,
33     dac_ana_div_6,
34     dac_ana_div_8
35 } dac_ana_div_t;
36 
37 typedef struct {
38     bool sync_mode;
39     uint8_t dac_mode;
40     uint8_t ana_div;
41 } dac_config_t;
42 
43 typedef enum {
44     dac_step_up = 0,
45     dac_step_down
46 } dac_step_direction_t;
47 
48 typedef enum {
49     dac_round_mode_oneshot = 0,
50     dac_round_mode_loop
51 } dac_round_mode_t;
52 
53 typedef struct {
54     uint16_t start_point;
55     uint16_t end_point;
56     uint8_t round_mode;
57     uint8_t up_down;
58     uint8_t step_num;
59 } dac_step_config_t;
60 
61 typedef enum {
62     dac_data_stru_2_point = 0,
63     dac_data_stru_1_point
64 } dac_data_structure_t;
65 
66 typedef enum {
67     dac_burst_single = 0,
68     dac_burst_incr4  = 3,
69     dac_burst_incr8  = 5
70 } dac_burst_type_t;
71 
72 typedef struct {
73     uint32_t start_addr;
74     uint8_t stop;
75     uint16_t len;
76 } dac_buffer_t;
77 
78 typedef struct {
79     uint8_t buf_data_mode;
80     uint8_t burst;
81     dac_buffer_t buf0;
82     dac_buffer_t buf1;
83 } dac_buffer_config_t;
84 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
89 void dac_get_default_config(dac_config_t *config);
90 hpm_stat_t dac_init(DAC_Type *ptr, dac_config_t *config);
91 hpm_stat_t dac_set_direct_config(DAC_Type *ptr, uint16_t data);
92 hpm_stat_t dac_set_step_config(DAC_Type *ptr, uint8_t step_cfg_idx, dac_step_config_t *config);
93 hpm_stat_t dac_set_buffer_config(DAC_Type *ptr, dac_buffer_config_t *config);
94 hpm_stat_t dac_set_output_frequency(DAC_Type *ptr, uint32_t dac_input_freq, uint32_t dac_output_freq);
95 hpm_stat_t dac_set_step_sw_trigger(DAC_Type *ptr, uint8_t step_sw_trig_idx);
96 void dac_set_buffer_sw_trigger(DAC_Type *ptr);
97 void dac_set_hw_trigger_enable(DAC_Type *ptr, bool enable);
98 hpm_stat_t dac_external_dma_request_enable(DAC_Type *ptr, uint8_t buf_idx, bool enable);
99 void dac_set_buffer_dma_reset(DAC_Type *ptr);
100 void dac_enable_conversion(DAC_Type *ptr, bool enable);
101 void dac_enable_interrupts(DAC_Type *ptr, uint32_t mask);
102 uint32_t dac_get_status_flags(DAC_Type *ptr);
103 void dac_set_status_flags(DAC_Type *ptr, uint32_t mask);
104 uint8_t dac_get_current_buffer_index(DAC_Type *ptr);
105 uint16_t dac_get_current_buffer_offset(DAC_Type *ptr);
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif
112