ch32v30x_dac.c 9.1 KB
Newer Older
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/********************************** (C) COPYRIGHT  *******************************
* File Name          : ch32v30x_dac.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : This file provides all the DAC firmware functions.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
****************************************************************************************/
#include "ch32v30x_dac.h"
#include "ch32v30x_rcc.h"

/* CTLR register Mask */
#define CTLR_CLEAR_MASK    ((uint32_t)0x00000FFE)

/* DAC Dual Channels SWTR masks */
#define DUAL_SWTR_SET      ((uint32_t)0x00000003)
#define DUAL_SWTR_RESET    ((uint32_t)0xFFFFFFFC)

/* DHR registers offsets */
#define DHR12R1_OFFSET     ((uint32_t)0x00000008)
#define DHR12R2_OFFSET     ((uint32_t)0x00000014)
#define DHR12RD_OFFSET     ((uint32_t)0x00000020)

/* DOR register offset */
#define DOR_OFFSET         ((uint32_t)0x0000002C)

/*********************************************************************
 * @fn      DAC_DeInit
 *
 * @brief   Deinitializes the DAC peripheral registers to their default reset values.
 *
 * @return  none
 */
void DAC_DeInit(void)
{
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);
}

/*********************************************************************
 * @fn      DAC_Init
 *
 * @brief   Initializes the DAC peripheral according to the specified parameters in
 *        the DAC_InitStruct.
 *
 * @param   DAC_Channel - the selected DAC channel.
 *            DAC_Channel_1 - DAC Channel1 selected
 *            DAC_Channel_2 - DAC Channel2 selected
 *          DAC_InitStruct - pointer to a DAC_InitTypeDef structure.
 *
 * @return  none
 */
void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef *DAC_InitStruct)
{
    uint32_t tmpreg1 = 0, tmpreg2 = 0;

    tmpreg1 = DAC->CTLR;
    tmpreg1 &= ~(CTLR_CLEAR_MASK << DAC_Channel);
    tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration |
               DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | DAC_InitStruct->DAC_OutputBuffer);
    tmpreg1 |= tmpreg2 << DAC_Channel;
    DAC->CTLR = tmpreg1;
}

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

/*********************************************************************
 * @fn      DAC_Cmd
 *
 * @brief   Enables or disables the specified DAC channel.
 *
 * @param   DAC_Channel - the selected DAC channel.
 *            DAC_Channel_1 - DAC Channel1 selected
 *            DAC_Channel_2 - DAC Channel2 selected
 *          NewState - new state of the DAC channel(ENABLE or DISABLE).
 *
 * @return  none
 */
void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState)
{
    if(NewState != DISABLE)
    {
        DAC->CTLR |= (DAC_EN1 << DAC_Channel);
    }
    else
    {
        DAC->CTLR &= ~(DAC_EN1 << DAC_Channel);
    }
}

/*********************************************************************
 * @fn      DAC_DMACmd
 *
 * @brief   Enables or disables the specified DAC channel DMA request.
 *
 * @param   DAC_Channel - the selected DAC channel.
 *            DAC_Channel_1 - DAC Channel1 selected
 *            DAC_Channel_2 - DAC Channel2 selected
 *          NewState - new state of the DAC channel(ENABLE or DISABLE).
 *
 * @return  none
 */
void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState)
{
    if(NewState != DISABLE)
    {
        DAC->CTLR |= (DAC_DMAEN1 << DAC_Channel);
    }
    else
    {
        DAC->CTLR &= ~(DAC_DMAEN1 << DAC_Channel);
    }
}

/*********************************************************************
 * @fn      DAC_SoftwareTriggerCmd
 *
 * @brief   Enables or disables the selected DAC channel software trigger.
 *
 * @param   DAC_Channel - the selected DAC channel.
 *            DAC_Channel_1 - DAC Channel1 selected
 *            DAC_Channel_2 - DAC Channel2 selected
 *          NewState - new state of the DAC channel(ENABLE or DISABLE).
 *
 * @return  none
 */
void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState)
{
    if(NewState != DISABLE)
    {
        DAC->SWTR |= (uint32_t)DAC_SWTRIG1 << (DAC_Channel >> 4);
    }
    else
    {
        DAC->SWTR &= ~((uint32_t)DAC_SWTRIG1 << (DAC_Channel >> 4));
    }
}

/*********************************************************************
 * @fn      DAC_DualSoftwareTriggerCmd
 *
 * @brief   Enables or disables the two DAC channel software trigger.
 *
 * @param   NewState - new state of the DAC channel(ENABLE or DISABLE).
 *
 * @return  none
 */
void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)
{
    if(NewState != DISABLE)
    {
        DAC->SWTR |= DUAL_SWTR_SET;
    }
    else
    {
        DAC->SWTR &= DUAL_SWTR_RESET;
    }
}

/*********************************************************************
 * @fn      DAC_WaveGenerationCmd
 *
 * @brief   Enables or disables the selected DAC channel wave generation.
 *
 * @param   DAC_Channel - the selected DAC channel.
 *            DAC_Channel_1 - DAC Channel1 selected
 *            DAC_Channel_2 - DAC Channel2 selected
 *          DAC_Wave - Specifies the wave type to enable or disable.
 *            DAC_Wave_Noise - noise wave generation
 *            DAC_Wave_Triangle - triangle wave generation
 *          NewState - new state of the DAC channel(ENABLE or DISABLE).
 *
 * @return  none
 */
void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState)
{
    if(NewState != DISABLE)
    {
        DAC->CTLR |= DAC_Wave << DAC_Channel;
    }
    else
    {
        DAC->CTLR &= ~(DAC_Wave << DAC_Channel);
    }
}

/*********************************************************************
 * @fn      DAC_SetChannel1Data
 *
 * @brief   Set the specified data holding register value for DAC channel1.
 *
 * @param   DAC_Align - Specifies the data alignment for DAC channel1.
 *            DAC_Align_8b_R - 8bit right data alignment selected
 *            DAC_Align_12b_L - 12bit left data alignment selected
 *            DAC_Align_12b_R - 12bit right data alignment selected
 *          Data - Data to be loaded in the selected data holding register.
 *
 * @return  none
 */
void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)
{
    __IO uint32_t tmp = 0;

    tmp = (uint32_t)DAC_BASE;
    tmp += DHR12R1_OFFSET + DAC_Align;

    *(__IO uint32_t *)tmp = Data;
}

/*********************************************************************
 * @fn      DAC_SetChannel2Data
 *
 * @brief   Set the specified data holding register value for DAC channel2.
 *
 * @param   DAC_Align - Specifies the data alignment for DAC channel1.
 *            DAC_Align_8b_R - 8bit right data alignment selected
 *            DAC_Align_12b_L - 12bit left data alignment selected
 *            DAC_Align_12b_R - 12bit right data alignment selected
 *            Data - Data to be loaded in the selected data holding register.
 *
 * @return  none
 */
void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data)
{
    __IO uint32_t tmp = 0;

    tmp = (uint32_t)DAC_BASE;
    tmp += DHR12R2_OFFSET + DAC_Align;

    *(__IO uint32_t *)tmp = Data;
}

/*********************************************************************
 * @fn      DAC_SetDualChannelData
 *
 * @brief   Set the specified data holding register value for two DAC.
 *
 * @param   DAC_Align - Specifies the data alignment for DAC channel1.
 *            DAC_Align_8b_R - 8bit right data alignment selected
 *            DAC_Align_12b_L - 12bit left data alignment selected
 *            DAC_Align_12b_R - 12bit right data alignment selected
 *            Data - Data to be loaded in the selected data holding register.
 *          Data1 - Data for DAC Channel1.
 *          Data2 - Data for DAC Channel2
 *
 * @return  none
 */
void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1)
{
    uint32_t data = 0, tmp = 0;

    if(DAC_Align == DAC_Align_8b_R)
    {
        data = ((uint32_t)Data2 << 8) | Data1;
    }
    else
    {
        data = ((uint32_t)Data2 << 16) | Data1;
    }

    tmp = (uint32_t)DAC_BASE;
    tmp += DHR12RD_OFFSET + DAC_Align;

    *(__IO uint32_t *)tmp = data;
}

/*********************************************************************
 * @fn      DAC_GetDataOutputValue
 *
 * @brief   Returns the last data output value of the selected DAC channel.
 *
 * @param   DAC_Channel - the selected DAC channel.
 *            DAC_Channel_1 - DAC Channel1 selected
 *            DAC_Channel_2 - DAC Channel2 selected
 *
 * @return  none
 */
uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)
{
    __IO uint32_t tmp = 0;

    tmp = (uint32_t)DAC_BASE;
    tmp += DOR_OFFSET + ((uint32_t)DAC_Channel >> 2);

    return (uint16_t)(*(__IO uint32_t *)tmp);
}