4
0
mirror of https://github.com/RT-Thread/rt-thread.git synced 2025-01-20 16:53:30 +08:00
stevetong459 fb61c7960b
本次PR涉及①BSP驱动新增②F103库更新③GCC、IAR适配及MDK更新④README文件及由更新驱动引起的脚本改动。 (#5638)
* 本次提交包括①BSP驱动新增②F103库更新③GCC、IAR适配及MDK更新④README文件及由更新驱动引起的脚本改动。
详情如下:
一、BSP驱动新增
这是本次PR的主要目的,现新增了如下BSP驱动:
ADC、DAC、RTC、PWM、HWTIMER、I2C、SPI和WDT等八个驱动文件。
二、F103库更新:
本次提交使用2022年3月初极海官网发布的最新F103库,主要增加了版权声明、USB驱动及其他代码调整。
三、编译器适配:
1、新增GCC编译支持,在ENV工具下编译能成功且输出的bin文件能够使开发板闪灯。
2、新增IAR工程支持。
3、由F103的SDK更新,MDK的工程也进行了相应更新。
四、其他
1、README文件做了修改,加入了scons编译后的jlink下载说明和官网链接。
2、Kconfig、SConscript脚本根据驱动更新做了修改。

* 格式化代码(AStyle + Formatting)

* 增加BSP APM版权声明

* 在ci添加当前bsp的路径,以能够验证gcc能否正常编译

* 路径的“\”改为“/”
2022-03-08 12:03:06 +08:00

185 lines
4.1 KiB
C

/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-03-04 stevetong459 first version
*/
#include <board.h>
#if defined(BSP_USING_DAC1)
#define LOG_TAG "drv.dac"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
struct apm32_dac
{
const char *name;
DAC_T *dac;
DAC_Config_T dac_conf;
struct rt_dac_device dac_dev;
};
static struct apm32_dac dac_config[] =
{
#if defined(BSP_USING_DAC1)
{
"dac1",
DAC,
{
DAC_TRIGGER_SOFT,
DAC_OUTPUT_BUFFER_DISABLE,
DAC_WAVE_GENERATION_NONE,
DAC_TRIANGLE_AMPLITUDE_4095,
},
RT_NULL
}
#endif
};
/**
* @brief This function will enable dac.
*
* @param device is a pointer to dac device.
*
* @param channel is the dac channel.
*
* @return RT_EOK indicates successful enable dac, other value indicates failed.
*/
static rt_err_t _dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
{
GPIO_Config_T GPIO_ConfigStruct;
struct apm32_dac *cfg = (struct apm32_dac *)device->parent.user_data;
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
GPIO_ConfigStruct.mode = GPIO_MODE_ANALOG;
if (channel == 1)
{
GPIO_ConfigStruct.pin = GPIO_PIN_4;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
DAC_Config(DAC_CHANNEL_1, &cfg->dac_conf);
DAC_Enable(DAC_CHANNEL_1);
}
else if (channel == 2)
{
GPIO_ConfigStruct.pin = GPIO_PIN_5;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
DAC_Config(DAC_CHANNEL_2, &cfg->dac_conf);
DAC_Enable(DAC_CHANNEL_2);
}
else
{
LOG_E("dac channel must be 1 or 2.");
return -RT_ERROR;
}
return RT_EOK;
}
/**
* @brief This function will disable dac.
*
* @param device is a pointer to dac device.
*
* @param channel is the dac channel.
*
* @return RT_EOK indicates successful disable dac, other value indicates failed.
*/
static rt_err_t _dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
{
if (channel == 1)
{
DAC_Disable(DAC_CHANNEL_1);
}
else if (channel == 2)
{
DAC_Disable(DAC_CHANNEL_2);
}
else
{
LOG_E("dac channel must be 1 or 2.");
return -RT_ERROR;
}
return RT_EOK;
}
/**
* @brief This function will set the vaule of dac.
*
* @param device is a pointer to dac device.
*
* @param channel is the dac channel.
*
* @param value is a pointer to dac value to be convert.
*
* @return RT_EOK indicates successful set dac value, other value indicates failed.
*/
static rt_err_t _dac_set_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
if (channel == 1)
{
DAC_ConfigChannel1Data(DAC_ALIGN_12BIT_R, *value);
DAC_EnableSoftwareTrigger(DAC_CHANNEL_1);
}
else if (channel == 2)
{
DAC_ConfigChannel2Data(DAC_ALIGN_12BIT_R, *value);
DAC_EnableSoftwareTrigger(DAC_CHANNEL_2);
}
else
{
LOG_E("dac channel must be 1 or 2.");
return -RT_ERROR;
}
return RT_EOK;
}
static const struct rt_dac_ops _dac_ops =
{
.disabled = _dac_disabled,
.enabled = _dac_enabled,
.convert = _dac_set_value,
};
/**
* @brief DAC initialization function.
*
* @return RT_EOK indicates successful initialization, other value indicates failed;
*/
static int rt_hw_dac_init(void)
{
rt_err_t result = RT_EOK;
rt_size_t obj_num = sizeof(dac_config) / sizeof(struct apm32_dac);
rt_uint32_t i = 0;
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_DAC);
for (i = 0; i < obj_num; i++)
{
/* register dac device */
if (rt_hw_dac_register(&dac_config[i].dac_dev, dac_config[i].name, &_dac_ops, dac_config) == RT_EOK)
{
LOG_D("%s init success", dac_config[i].name);
}
else
{
LOG_E("%s init failed", dac_config[i].name);
result = -RT_ERROR;
}
}
return result;
}
INIT_DEVICE_EXPORT(rt_hw_dac_init);
#endif /* BSP_USING_DACX */