当前位置:网站首页>GD32 RT-Thread DAC驱动函数
GD32 RT-Thread DAC驱动函数
2022-06-30 09:37:00 【Sky_Lannister】
GD32 DAC驱动函数
驱动函数包含三个文件,drv_dac.c、drv_dac.h、dac_config.h,使用BSP_USING_DAC0\BSP_USING_DAC1进行开启关闭,移植注意事项参见 移植完整版RT-Thread到GD32F4XX(详细) 中关于dac移植相关内容,移植完成后使用及验证方法同stm32
drv_dac.c
/** ****************************************************************************** * @file drv_dac.c * @author yangFei * @brief 按照GD32库实现dac驱动 * * @version V1.0.0 * @date 2022.06.06 * @verbatim * * @endverbatim ****************************************************************************** * @note * @attention * * Change Logs: * Date Author Notes * 2022.06.06 yangFei 适用芯片为GD32f4xx,使用官方提供标准库,例程移植自https://github.com/RT-Thread/rt- thread/tree/master/bsp/gd32/gd32407v-start * ****************************************************************************** */
#include <board.h>
#if defined(BSP_USING_DAC0) || defined(BSP_USING_DAC1)
#include "drv_dac.h"
#include "dac_config.h"
//#define DRV_DEBUG
#define LOG_TAG "drv.dac"
#include <drv_log.h>
static DAC_HandleTypeDef dac_config[] =
{
#ifdef BSP_USING_DAC0
DAC0_CONFIG,
#endif
#ifdef BSP_USING_DAC1
DAC1_CONFIG,
#endif
};
struct gd32_dac
{
DAC_HandleTypeDef DAC_Handler;
struct rt_dac_device gd32_dac_device;
};
static struct gd32_dac gd32_dac_obj[sizeof(dac_config) / sizeof(dac_config[0])];
static rt_err_t gd32_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
{
DAC_HandleTypeDef *gd32_dac_handler;
RT_ASSERT(device != RT_NULL);
gd32_dac_handler = device->parent.user_data;
#if defined(SOC_SERIES_GD32F4xx)
dac_enable(gd32_dac_handler->Instance);
#endif
return RT_EOK;
}
static rt_err_t gd32_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
{
DAC_HandleTypeDef *gd32_dac_handler;
RT_ASSERT(device != RT_NULL);
gd32_dac_handler = device->parent.user_data;
#if defined(SOC_SERIES_GD32F4xx)
dac_disable(gd32_dac_handler->Instance);
#endif
return RT_EOK;
}
static rt_err_t gd32_set_dac_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
DAC_HandleTypeDef *gd32_dac_handler;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(value != RT_NULL);
gd32_dac_handler = device->parent.user_data;
/* config dac out channel*/
/* set dac channel out value*/
dac_data_set(gd32_dac_handler->Instance, DAC_ALIGN_12B_R, *value);
/* start dac */
dac_enable(gd32_dac_handler->Instance);
return RT_EOK;
}
static const struct rt_dac_ops stm_dac_ops =
{
.disabled = gd32_dac_disabled,
.enabled = gd32_dac_enabled,
.convert = gd32_set_dac_value,
};
static int gd32_dac_init(void)
{
int result = RT_EOK;
/* save dac name */
char name_buf[5] = {
'd', 'a', 'c', '0', 0};
int i = 0;
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_DAC);
dac_deinit();
for (i = 0; i < sizeof(dac_config) / sizeof(dac_config[0]); i++)
{
/* dac init */
name_buf[3] = '0';
gd32_dac_obj[i].DAC_Handler = dac_config[i];
dac_trigger_disable(gd32_dac_obj[i].DAC_Handler.Instance);
dac_wave_mode_config(gd32_dac_obj[i].DAC_Handler.Instance, DAC_WAVE_DISABLE);
dac_output_buffer_enable(gd32_dac_obj[i].DAC_Handler.Instance);
#if defined(DAC0)
if (gd32_dac_obj[i].DAC_Handler.Instance == DAC0)
{
name_buf[3] = '0';
gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_4);
}
#endif
#if defined(DAC1)
if (gd32_dac_obj[i].DAC_Handler.Instance == DAC1)
{
name_buf[3] = '1';
gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_5);
}
#endif
/* register dac device */
if (rt_hw_dac_register(&gd32_dac_obj[i].gd32_dac_device, name_buf, &stm_dac_ops, &gd32_dac_obj[i].DAC_Handler) == RT_EOK)
{
LOG_D("%s init success", name_buf);
}
else
{
LOG_E("%s register failed", name_buf);
result = -RT_ERROR;
}
}
return result;
}
INIT_DEVICE_EXPORT(gd32_dac_init);
#endif /* BSP_USING_DAC */
/************************ (C) COPYRIGHT *****END OF FILE****/
drv_dac.h
/** ****************************************************************************** * @file drv_dac.h * @author yangFei * @brief 按照GD32库实现dac驱动 存放hal库中关于dac相关的定义 * * @version V1.0.0 * @date 2022.06.06 * @verbatim * * @endverbatim ****************************************************************************** * @note * @attention * * Change Logs: * Date Author Notes * 2022.06.06 yangFei * ****************************************************************************** */
#ifndef _DRV_DAC_H
#define _DRV_DAC_H
#define DAC_CHANNEL_0 0x00000000U
#define DAC_CHANNEL_1 0x00000010U
/** * @brief HAL State structures definition */
typedef enum
{
HAL_DAC_STATE_RESET = 0x00U, /*!< DAC not yet initialized or disabled */
HAL_DAC_STATE_READY = 0x01U, /*!< DAC initialized and ready for use */
HAL_DAC_STATE_BUSY = 0x02U, /*!< DAC internal processing is ongoing */
HAL_DAC_STATE_TIMEOUT = 0x03U, /*!< DAC timeout state */
HAL_DAC_STATE_ERROR = 0x04U /*!< DAC error state */
} HAL_DAC_StateTypeDef;
/** * @brief DAC handle Structure definition */
typedef struct
{
uint32_t Instance; /*!< Register base address */
__IO HAL_DAC_StateTypeDef State; /*!< DAC communication state */
HAL_LockTypeDef Lock; /*!< DAC locking object */
// DMA_HandleTypeDef *DMA_Handle1; /*!< Pointer DMA handler for channel 1 */
// DMA_HandleTypeDef *DMA_Handle2; /*!< Pointer DMA handler for channel 2 */
__IO uint32_t ErrorCode; /*!< DAC Error code */
} DAC_HandleTypeDef;
#endif /* _DRV_DAC_H */
/************************ (C) COPYRIGHT *****END OF FILE****/
dac_config.h
/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2020-06-16 thread-liu first version */
#ifndef __DAC_CONFIG_H__
#define __DAC_CONFIG_H__
#include <rtthread.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BSP_USING_DAC0
#ifndef DAC0_CONFIG
#define DAC0_CONFIG \ {
\ .Instance = DAC0, \ }
#endif /* DAC0_CONFIG */
#endif /* BSP_USING_DAC0 */
#ifdef BSP_USING_DAC1
#ifndef DAC1_CONFIG
#define DAC1_CONFIG \ {
\ .Instance = DAC1, \ }
#endif /* DAC1_CONFIG */
#endif /* BSP_USING_DAC1 */
#ifdef __cplusplus
}
#endif
#endif /* __DAC_CONFIG_H__ */
边栏推荐
- ‘Failed to fetch current robot state‘ when using the ‘plan_ kinematic_ path‘ service #868
- C語言實現掃雷遊戲,附詳解及完整代碼
- Theme Studio
- Rider does not prompt after opening unity script
- 郭琳加冕 2022第三季完美大师 全球人气季军
- Magnetic levitation 3D lamp
- Eth is not connected to the ore pool
- 陈颢天 荣获第七届少儿模特明星盛典全国总决赛 全国总冠军
- 1033 To Fill or Not to Fill
- South China Industrial Group launched digital economy and successfully held the city chain technology conference
猜你喜欢

2022第六季完美童模 托克逊赛区 决赛圆满落幕

NLopt--非线性优化--原理介绍及使用方法

MySQL index, transaction and storage engine of database (3)

采坑:Didn‘t receive robot state (joint angles) with recent timestamp within 1 seconds.

Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi

Great Wall digital art digital collection platform releases the creation Badge

著名画家史国良《丰收时节》数字藏品上线长城数艺

Detailed explanation of SolidWorks mass characteristics (inertia tensor, moment of inertia, inertia spindle)

MySQL index, transaction and storage engine of database (1)

机器人系统动力学——惯性参数
随机推荐
[AGC] build service 3- authentication service example
What is the real performance of CK5, the king machine of CKB?
MIT-6874-Deep Learning in the Life Sciences Week6
机器人系统动力学——惯性参数
Description of event flow
2022 Season 6 perfect children's model toxon division finals came to a successful conclusion
Theme Studio
事件委托的使用与说明》
Input limit input
NLopt--非线性优化--原理介绍及使用方法
GD32 RT-Thread OTA/Bootloader驱动函数
"Kunming City coffee map" was opened again, and coffee brought the city closer
6.Redis新数据类型
GNN hands on practice (II): reproduction graph attention network gat
陈颢天 荣获第七届少儿模特明星盛典全国总决赛 全国总冠军
基于强化学习的股票量化交易Automated-Stock-Trading-Ensemble-Strategy
戴森设计大奖,以可持续化设计改变世界
Description of event object
Why can't you rob scientists of NFT
打通供应链 深圳礼品展助跨境电商寻破局之道