当前位置:网站首页>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
- “昆明城市咖啡地圖”活動再度開啟
- Configure Yii: display MySQL extension module verification failed
- 机器人系统动力学——惯性参数
- “昆明城市咖啡地图”活动再度开启
- Great Wall digital art digital collection platform releases the creation Badge
- 1033 To Fill or Not to Fill
- The famous painter shiguoliang's "harvest season" digital collection was launched on the Great Wall Digital Art
- Use and description of event delegation
- Chen Haotian won the national championship of the national finals of the 7th children's model star ceremony
猜你喜欢
The digital collection of sunanmin's lotus heart clearing was launched on the Great Wall Digital Art
著名画家史国良《丰收时节》数字藏品上线长城数艺
新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动
7. development of mobile login function
华南产业集团发力数字经济,城链科技发布会成功召开
The preliminary round of the sixth season of 2022 perfect children's model Hefei competition area was successfully concluded
“昆明城市咖啡地图”活动再度开启
[JVM] G1 garbage collector
Splendid China: public welfare tourism for the middle-aged and the elderly - entering Foshan nursing home
Detailed explanation of commissioning methods and techniques
随机推荐
AttributeError: ‘Version‘ object has no attribute ‘major‘
基于强化学习的股票量化交易Automated-Stock-Trading-Ensemble-Strategy
After recording 7000 IELTS words in 100 sentences, there are only 1043 words (including simple words such as I and you)
The URL copied by the browser and pasted into the document is a hyperlink
L'activité "Kunming City coffee map" a rouvert
The human agent of kDa, Jinbei kd6, takes you to explore the metauniverse
Theme Studio
“昆明城市咖啡地圖”活動再度開啟
JS obtient la chaîne spécifiée spécifiant la position du caractère & sous - chaîne spécifiant la plage de position du caractère 【 détails simples 】
半钢同轴射频线的史密斯圆图查看和网络分析仪E5071C的射频线匹配校准
input限制输入
OSError: [Errno 28] No space left on device
乡村振兴公益基金启动暨古茶树非遗保护公益行发布
Js獲取指定字符串指定字符比特置&指定字符比特置區間的子串【簡單詳細】
背课文记单词,读课文记单词,读文章记单词;40篇文章搞定3500词;71篇文章突破中考单词;15篇文章贯通四级词汇;15篇文章贯通六级词汇
[JVM] brief introduction to CMS
The digital collection of sunanmin's lotus heart clearing was launched on the Great Wall Digital Art
AttributeError: ‘Version‘ object has no attribute ‘major‘
7. development of mobile login function
Description of event flow