硬汉嵌入式论坛

 找回密码
 立即注册
查看: 838|回复: 4
收起左侧

[技术讨论] GD32E5的官方文档DMA部分是否有有误?

[复制链接]

1

主题

4

回帖

7

积分

新手上路

积分
7
QQ
发表于 2023-2-14 01:47:56 | 显示全部楼层 |阅读模式
起因是在用GD32E5做东西,需要用DMA+TIM的方式驱动WS2812,选用了PA0做TIMER1_CH0的PWM输出,根据官方文档的描述,TIMER1_CH0对应的DMA是DMA0_CH4,但是实际配下来没有输出,因此去查了一下固件库中提供的案例。案例中有timer0_dma的例子,使用的是PA8作TIMER0_CH0,使用的DMA是DMA0_CH4,又和官方文档不一致了。请问各位大佬,这各问题是因为我理解不透彻,还是官网文档本身的问题.
在这里贴一个GD32E5官方文档的链接 https://www.gd32mcu.com/data/doc ... anual_Rev1.6_CN.pdf ,我所提到的问题位于文档的第244页。
下面给出官方的timer0_ch0_dma配置。这个问题困扰我好几天了,真心希望各位大佬指点一二。
[C] 纯文本查看 复制代码
/*!
    \brief      configure the GPIO ports
    \param[in]  none
    \param[out] none
    \retval     none
*/
void gpio_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_AF);

    /*configure PA8(TIMER0 CH0) as alternate function*/
    gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
}

/*!
    \brief      configure the DMA peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void dma_config(void)
{
    dma_parameter_struct dma_init_struct;

    /* enable DMA clock */
    rcu_periph_clock_enable(RCU_DMA0);

    /* initialize DMA channel5 */
    dma_deinit(DMA0,DMA_CH4);

    /* DMA channel5 initialize */
    dma_init_struct.periph_addr = (uint32_t)TIMER0_CH0CV;
    dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
    dma_init_struct.memory_addr = (uint32_t)buffer;
    dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
    dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
    dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
    dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
    dma_init_struct.number = 3;
    dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
    dma_init(DMA0,DMA_CH4,&dma_init_struct);
    
    dma_circulation_enable(DMA0,DMA_CH4);

    /* enable DMA channel5 */
    dma_channel_enable(DMA0,DMA_CH4);
}

/*!
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void timer_config(void)
{
    /* TIMER0 DMA Transfer example -------------------------------------------------
    TIMER0CLK = 180MHz, Prescaler = 179 
    TIMER0 counter clock = systemcoreclock/180 = 1MHz.

    the objective is to configure TIMER0 channel 1 to generate PWM
    signal with a frequency equal to 1KHz and a variable duty cycle(25%,50%,75%) that is 
    changed by the DMA after a specific number of update DMA request.

    the number of this repetitive requests is defined by the TIMER0 repetition counter,
    each 2 update requests, the TIMER0 Channel 0 duty cycle changes to the next new 
    value defined by the buffer . 
    -----------------------------------------------------------------------------*/
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER0);

    timer_deinit(TIMER0);

    /* TIMER0 configuration */
    timer_initpara.prescaler         = 179;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 999;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 1;
    timer_init(TIMER0,&timer_initpara);

    /* CH0 configuration in PWM1 mode */
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_HIGH;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,buffer[0]);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    /* TIMER0 primary output enable */
    timer_primary_output_config(TIMER0,ENABLE);
    /* channel DMA request source selection */
    timer_channel_dma_request_source_select(TIMER0,TIMER_DMAREQUEST_UPDATEEVENT);
    /* configure the TIMER DMA transfer */ 
    timer_dma_transfer_config(TIMER0,TIMER_DMACFG_DMATA_CH0CV,TIMER_DMACFG_DMATC_1TRANSFER);
    /* TIMER0 update DMA request enable */
    timer_dma_enable(TIMER0,TIMER_DMA_UPD);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER0);

    /* TIMER0 counter enable */
    timer_enable(TIMER0);
}

回复

使用道具 举报

1

主题

4

回帖

7

积分

新手上路

积分
7
QQ
 楼主| 发表于 2023-2-14 02:02:01 | 显示全部楼层
好像找到了一个点,官方文档中为DMA0_CH4配置的是TIMER0_UP,这样就和文档吻合了,如果我需要配置TIMER1_CH0,那么上面代码中第106行的配置应该改成
[C] 纯文本查看 复制代码
    timer_dma_enable(TIMER1, TIMER_DMA_CH0D);
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107049
QQ
发表于 2023-2-14 14:52:41 | 显示全部楼层
GD32有没有类似CubeMX的配置工具,排查这方便问题还剩点事
回复

使用道具 举报

1

主题

4

回帖

7

积分

新手上路

积分
7
QQ
 楼主| 发表于 2023-2-14 16:12:36 | 显示全部楼层
eric2013 发表于 2023-2-14 14:52
GD32有没有类似CubeMX的配置工具,排查这方便问题还剩点事

谢硬汉回复!gd32目前还没有类似的工具,我也是一时兴起想玩一下国产芯片,然后基于和GD32E5同样M33内核的STM32L5使用cubemx生成了gcc的模板工程,自己改了一下启动文件和链接文件搞得gcc编译。现在我已经把波形弄出来了,文档确实没有错误,是我理解错了,官方的案例使用的TIMER0的update去请求DMA_CH4,然后就对应上了。但是目前还是有一个配置了memory地址自增但是dma发送的时候却没有自增的问题,还得继续查。
回复

使用道具 举报

1

主题

4

回帖

7

积分

新手上路

积分
7
QQ
 楼主| 发表于 2023-2-15 01:06:57 | 显示全部楼层
应该是搞定了,问题就是文档没有看仔细,TIMER1是32位的定时器,所以发送给TIMER1的数据要用32位的格式,同时初始化DMA的时候也得将外设地址宽度设置为32位,下面分享配置代码
[C] 纯文本查看 复制代码
#define TIMER1_CH0CV ((uint32_t)TIMER1 + 0x34)
uint32_t buffer[10] = {144, 81, 144, 81, 144, 81, 144, 81};

void gpio_config(void);
void timer_config(void);
void dma_config(void);

/*!
    \brief      configure the GPIO ports
    \param[in]  none
    \param[out] none
    \retval     none
*/
void gpio_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_AF);

    /*configure PA0(TIMER1 CH0) as alternate function*/
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
}

/*!
    \brief      configure the DMA peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void dma_config(void)
{
    dma_parameter_struct dma_init_struct;

    /* enable DMA clock */
    rcu_periph_clock_enable(RCU_DMA0);

    /* initialize DMA0 channel4 */
    dma_deinit(DMA0, DMA_CH4);

    /* DMA channel4 initialize */
    dma_init_struct.periph_addr = (uint32_t)TIMER1_CH0CV;
    dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
    dma_init_struct.memory_addr = (uint32_t)buffer;
    dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
    dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_32BIT;
    dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
    dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
    dma_init_struct.number = 10;
    dma_init_struct.priority = DMA_PRIORITY_HIGH;
    dma_init(DMA0, DMA_CH4, &dma_init_struct);

    dma_circulation_enable(DMA0, DMA_CH4);

    /* enable DMA channel5 */
    dma_channel_enable(DMA0, DMA_CH4);
}

/*!
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void timer_config(void)
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER1);

    timer_deinit(TIMER1);

    /* TIMER1 configuration */
    timer_initpara.prescaler = 1 - 1;
    timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = 225 - 1;
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1, &timer_initpara);

    /* CH0 configuration in PWM1 mode */
    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0, 0);
    timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER1, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);

    /* TIMER1 primary output enable */
    timer_primary_output_config(TIMER1, ENABLE);
    /* channel DMA request source selection */
    timer_channel_dma_request_source_select(TIMER1, TIMER_DMAREQUEST_CHANNELEVENT);
    /* configure the TIMER DMA transfer */
    timer_dma_transfer_config(TIMER1, TIMER_DMACFG_DMATA_CH0CV, TIMER_DMACFG_DMATC_1TRANSFER);
    /* TIMER1 update DMA request enable */
    timer_dma_enable(TIMER1, TIMER_DMA_CH0D);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER1);

    /* TIMER1 counter enable */
    timer_enable(TIMER1);
}

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    gpio_config();
    dma_config();
    timer_config();

    while (1)
        ;
}


在配置中需要注意的是传给DMA0的数组元素要是32位的,并且使    dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_32BIT;,最后将timer_channel_dma_request_source_select设置为TIMER_DMAREQUEST_CHANNELEVENT,将timer_dma_enable设置为TIMER_DMA_CH0D,以将DMA0_CH4绑定位TIMER1_CH0的输出,就可以输出设置的波形了。
在我的例子里,芯片工作在180Mhz,TIMER1设置为1分频,所以TIMER1的时钟是也是180Mhz,而WS2812需要的是800khz的频率,所以将计数周期设置为225即可满足180M/225=800K,然后根据WS2812的需求,高电平占空设置为225*(800/1250)=144,低电平占空设置为225*(450/1250)=81即可得到驱动WS2812所需要bit。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-16 07:12 , Processed in 0.242914 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表