summaryrefslogtreecommitdiff
path: root/bsp/mm32f327x/Libraries/MM32F327x/HAL_Lib/Src/hal_dac.c
blob: b3583f15577a4a0ade256da4d7b117d3e3c93fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
////////////////////////////////////////////////////////////////////////////////
/// @file     hal_dac.c
/// @author   AE TEAM
/// @brief    THIS FILE PROVIDES ALL THE DAC FIRMWARE FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#define _HAL_DAC_C_

// Files includes
#include "hal_dac.h"
#include "hal_rcc.h"

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Hardware_Abstract_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup DAC_HAL
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup DAC_Exported_Functions
/// @{


////////////////////////////////////////////////////////////////////////////////
/// @brief  Deinitializes the DAC peripheral registers to their default reset values.
/// @param  None.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_DeInit(void)
{
    exRCC_APB1PeriphReset(RCC_APB1ENR_DAC);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Initializes the DAC peripheral according to the specified parameters in the DAC_InitStruct.
/// @param channel: the selected DAC channel.
/// @param DAC_InitStruct: pointer to a DAC_InitTypeDef structure that contains the configuration information for the specified
/// DAC channel.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_Init(emDACCH_TypeDef channel, DAC_InitTypeDef* init_struct)
{
    DAC->CR &= ~((DAC_CR_BOFF1 | DAC_CR_TEN1 | DAC_CR_TSEL1 | DAC_CR_WAVE1 | DAC_CR_MAMP1) << channel);
    DAC->CR |= (((u32)(init_struct->DAC_Trigger) | (u32)(init_struct->DAC_WaveGeneration) |
                 (u32)(init_struct->DAC_LFSRUnmask_TriangleAmplitude) | (u32)(init_struct->DAC_OutputBuffer))
                << channel);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Fills each DAC_InitStruct member with its default value.
/// @param DAC_InitStruct : pointer to a DAC_InitTypeDef structure which will be initialized.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_StructInit(DAC_InitTypeDef* init_struct)
{
    init_struct->DAC_Trigger                      = DAC_Trigger_None;
    init_struct->DAC_WaveGeneration               = DAC_WaveGeneration_None;
    init_struct->DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_1;
    init_struct->DAC_OutputBuffer                 = DAC_OutputBuffer_Enable;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Enables or disables the specified DAC channel.
/// @param channel: the selected DAC channel.
/// @param state: new state of the DAC channel.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_Cmd(emDACCH_TypeDef channel, FunctionalState state)
{
    (state) ? (DAC->CR |= DAC_CR_EN1 << channel) : (DAC->CR &= ~(DAC_CR_EN1 << channel));
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Enables or disables the specified DAC channel DMA request.
/// @param channel: the selected DAC channel.
/// @param state: new state of the selected DAC channel DMA request.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_DMACmd(emDACCH_TypeDef channel, FunctionalState state)
{
    (state) ? (DAC->CR |= DAC_CR_DMAEN1 << channel) : (DAC->CR &= ~(DAC_CR_DMAEN1 << channel));
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Enables or disables the selected DAC channel software trigger.
/// @param channel: the selected DAC channel.
/// @param state: new state of the selected DAC channel software trigger.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_SoftwareTriggerCmd(emDACCH_TypeDef channel, FunctionalState state)
{
    (state) ? (DAC->SWTRIGR |= (DAC_SWTRIGR_SWTRIG1 << (channel >> 4)))
    : (DAC->SWTRIGR &= ~(DAC_SWTRIGR_SWTRIG1 << (channel >> 4)));
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Enables or disables simultaneously the two DAC channels software triggers.
/// @param state: new state of the DAC channels software triggers.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_DualSoftwareTriggerCmd(FunctionalState state)
{
    (state) ? (DAC->SWTRIGR |= (DAC_SWTRIGR_SWTRIG1 | DAC_SWTRIGR_SWTRIG2))
    : (DAC->SWTRIGR &= ~(DAC_SWTRIGR_SWTRIG1 | DAC_SWTRIGR_SWTRIG2));
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Enables or disables the selected DAC channel wave generation.
/// @param channel: the selected DAC channel.
/// @param wave: Specifies the wave type to enable or disable.
/// @param state: new state of the selected DAC channel wave generation.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_WaveGenerationCmd(emDACCH_TypeDef channel, emDACWAVE_TypeDef wave, FunctionalState state)
{
    (state) ? (DAC->CR |= wave << channel) : (DAC->CR &= ~(wave << channel));
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Set the specified data holding register value for DAC channel1.
/// @param alignement: Specifies the data alignement for DAC channel1.
/// @param data : data to be loaded in the selected data holding register.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_SetChannel1Data(emDACALIGN_TypeDef alignement, u16 data)
{
    *((u32*)(DAC_BASE + DHR12R1_Offset + alignement)) = data;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Set the specified data holding register value for DAC channel2.
/// @param alignement: Specifies the data alignement for DAC channel2.
/// @param data : data to be loaded in the selected data holding
///   register.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_SetChannel2Data(emDACALIGN_TypeDef alignement, u16 data)
{
    *((u32*)(DAC_BASE + DHR12R2_Offset + alignement)) = data;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Set the specified data holding register value for dual channel DAC.
/// @param alignement: Specifies the data alignement for dual channel DAC.
/// @param data2: data for DAC Channel2 to be loaded in the selected data holding register.
/// @param data1: data for DAC Channel1 to be loaded in the selected data holding register.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void DAC_SetDualChannelData(emDACALIGN_TypeDef alignement, u16 data2, u16 data1)
{
    u32 data = ((alignement == DAC_Align_8b_R) ? ((data2 << 8) | data1) : ((data2 << 16) | data1));
    *((u32*)(DAC_BASE + DHR12RD_Offset + alignement)) = data;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Returns the last data output value of the selected DAC cahnnel.
/// @param channel: the selected DAC channel.
/// @retval  The selected DAC channel data output value.
////////////////////////////////////////////////////////////////////////////////
u16 DAC_GetDataOutputValue(emDACCH_TypeDef channel)
{
    return (*(vu32*)(DAC_BASE + DOR_Offset + (channel >> 2)));
}
/// @}

/// @}

/// @}