Merge pull request #2459 from wangyq2018/es32f0654

[bsp/es32f0654] fixed pwm timing inaccuracy when period is too small.
This commit is contained in:
Bernard Xiong 2019-03-19 03:04:14 +08:00 committed by GitHub
commit 56c8f8bcc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 104 additions and 62 deletions

View File

@ -13,9 +13,9 @@ ES-PDS-ES32F0654 是东软载波微电子官方推出的一款基于 ARM Cortex-
开发板外观如下图所示:
ES-PDS-ES32F0654-V1.0
ES-PDS-ES32F0654-V1.1
![ES32F0654](figures/ES-PDS-ES32F0654-V1.0.jpg)
![ES32F0654](figures/ES-PDS-ES32F0654-V1.1.jpg)
该开发板常用 **板载资源** 如下:
@ -34,7 +34,7 @@ ES-PDS-ES32F0654-V1.0
| **板载外设** | **支持情况** | **备注** |
| :---------------- | :----------: | :------------------------------------|
| SPI FLASH | 支持 | |
| SPI FLASH | 支持 | SPI0 |
| **片上外设** | **支持情况** | **备注** |
| :---------------- | :----------: | :------------------------------------|
@ -56,7 +56,7 @@ ES-PDS-ES32F0654-V1.0
使用ESlinkⅡ(mini)连接开发板如下图所示:
ESLinkⅡ(mini) + ES-PDS-ES32F0654-V1.0
ESLinkⅡ(mini) + ES-PDS-ES32F0654-V1.1
![ESLinkII](figures/ESLinkII-mini.jpg)

View File

@ -114,8 +114,10 @@ void rt_hw_us_delay(rt_uint32_t us)
start = SysTick->VAL;
reload = SysTick->LOAD;
us_tick = cmu_get_sys_clock() / 1000000UL;
do{
do
{
now = SysTick->VAL;
delta = start > now ? start - now : reload + start - now;
} while(delta < us_tick * us);
}
while (delta < us_tick * us);
}

View File

@ -257,7 +257,7 @@ rt_err_t es32f0_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
{
return RT_ENOSYS;
}
/**pin no. convert to dec no.**/
/* pin no. convert to dec no. */
for (irqindex = 0; irqindex < 16; irqindex++)
{
if ((0x01 << irqindex) == index->pin)
@ -327,7 +327,7 @@ rt_err_t es32f0_pin_irq_enable(struct rt_device *device, rt_base_t pin,
const struct pin_irq_map *irqmap;
rt_base_t level;
rt_int32_t irqindex = -1;
/*Configure GPIO_InitStructure & EXTI_InitStructure*/
/* Configure GPIO_InitStructure & EXTI_InitStructure */
gpio_init_t gpio_initstruct;
exti_init_t exti_initstruct;
exti_initstruct.filter = DISABLE;
@ -341,7 +341,7 @@ rt_err_t es32f0_pin_irq_enable(struct rt_device *device, rt_base_t pin,
}
if (enabled == PIN_IRQ_ENABLE)
{
/**pin no. convert to dec no.**/
/* pin no. convert to dec no. */
for (irqindex = 0; irqindex < 16; irqindex++)
{
if ((0x01 << irqindex) == index->pin)
@ -421,7 +421,7 @@ INIT_BOARD_EXPORT(rt_hw_pin_init);
rt_inline void pin_irq_hdr(uint16_t GPIO_Pin)
{
uint16_t irqno;
/**pin no. convert to dec no.**/
/* pin no. convert to dec no. */
for (irqno = 0; irqno < 16; irqno++)
{
if ((0x01 << irqno) == GPIO_Pin)

View File

@ -19,17 +19,14 @@
#ifdef RT_USING_I2C
#define TIMEOUT 0x0FFF
/*define i2c Instance*/
struct rt_i2c_bus_device _i2c_device0;
struct rt_i2c_bus_device _i2c_device1;
i2c_handle_t _h_i2c0, _h_i2c1;
/* I2C struct definition */
static i2c_handle_t _h_i2c0, _h_i2c1;
static void _i2c_init(void)
{
gpio_init_t gpio_instruct; //i2c function init
gpio_init_t gpio_instruct;
/* Initialize I2C Pin*/
/* Initialize I2C Pin */
gpio_instruct.mode = GPIO_MODE_OUTPUT;
gpio_instruct.odos = GPIO_PUSH_PULL;
gpio_instruct.pupd = GPIO_PUSH_UP;
@ -52,7 +49,7 @@ static void _i2c_init(void)
i2c_init(&_h_i2c0);
/* I2C0_SCL->PB8, I2C0_SDA->PB9 */
gpio_init(GPIOB, GPIO_PIN_8 | GPIO_PIN_9, &gpio_instruct);
#endif/*BSP_USING_I2C0*/
#endif
#ifdef BSP_USING_I2C1
/* Initialize i2c function */
@ -68,7 +65,7 @@ static void _i2c_init(void)
i2c_init(&_h_i2c1);
/* I2C1_SCL->PB10, I2C1_SDA->PB11 */
gpio_init(GPIOB, GPIO_PIN_10 | GPIO_PIN_11, &gpio_instruct);
#endif/*BSP_USING_I2C1*/
#endif
}
static rt_size_t es32f0_master_xfer(struct rt_i2c_bus_device *bus,
@ -120,6 +117,8 @@ int rt_hw_i2c_init(void)
_i2c_init();
#ifdef BSP_USING_I2C0
/* define i2c Instance */
static struct rt_i2c_bus_device _i2c_device0;
rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
_i2c_device0.ops = &es32f0_i2c_ops;
_i2c_device0.priv = &_h_i2c0;
@ -127,6 +126,8 @@ int rt_hw_i2c_init(void)
#endif
#ifdef BSP_USING_I2C1
/* define i2c Instance */
static struct rt_i2c_bus_device _i2c_device1;
rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
_i2c_device1.ops = &es32f0_i2c_ops;
_i2c_device1.priv = &_h_i2c1;
@ -136,5 +137,5 @@ int rt_hw_i2c_init(void)
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_i2c_init);
/* end of i2c driver */
#endif

View File

@ -12,12 +12,42 @@
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <ald_cmu.h>
#include <ald_timer.h>
#include <ald_gpio.h>
static void pwm_set_freq(timer_handle_t *timer_initstruct, uint32_t ns)
{
uint64_t _arr = (uint64_t)cmu_get_pclk1_clock() * ns / 1000000000 /
(timer_initstruct->init.prescaler + 1) - 1;
WRITE_REG(timer_initstruct->perh->AR, (uint32_t)_arr);
timer_initstruct->init.period = (uint32_t)_arr;
}
static void pwm_set_duty(timer_handle_t *timer_initstruct, timer_channel_t ch, uint32_t ns)
{
uint64_t tmp = (uint64_t)cmu_get_pclk1_clock() * ns / 1000000000 /
(timer_initstruct->init.prescaler + 1) - 1;
if (ch == TIMER_CHANNEL_1)
WRITE_REG(timer_initstruct->perh->CCVAL1, (uint32_t)tmp);
else if (ch == TIMER_CHANNEL_2)
WRITE_REG(timer_initstruct->perh->CCVAL2, (uint32_t)tmp);
else if (ch == TIMER_CHANNEL_3)
WRITE_REG(timer_initstruct->perh->CCVAL3, (uint32_t)tmp);
else if (ch == TIMER_CHANNEL_4)
WRITE_REG(timer_initstruct->perh->CCVAL4, (uint32_t)tmp);
else
{
;/* do nothing */
}
}
static rt_err_t es32f0_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
{
rt_err_t ret = RT_EOK;
uint32_t _ccep;
timer_channel_t pwm_channel;
timer_oc_init_t tim_ocinit;
timer_handle_t *timer_initstruct = (timer_handle_t *)device->parent.user_data;
@ -33,21 +63,21 @@ static rt_err_t es32f0_pwm_control(struct rt_device_pwm *device, int cmd, void *
tim_ocinit.oc_idle = TIMER_OC_IDLE_RESET;
/* select pwm output channel */
if (0 == cfg->channel)
if (1 == cfg->channel)
{
pwm_channel = TIMER_CHANNEL_1;
}
else if (1 == cfg->channel)
else if (2 == cfg->channel)
{
pwm_channel = TIMER_CHANNEL_2;
}
else if (2 == cfg->channel)
else if (3 == cfg->channel)
{
if (timer_initstruct->perh == GP16C2T0 || timer_initstruct->perh == GP16C2T1)
return RT_EINVAL;
pwm_channel = TIMER_CHANNEL_3;
}
else if (3 == cfg->channel)
else if (4 == cfg->channel)
{
if (timer_initstruct->perh == GP16C2T0 || timer_initstruct->perh == GP16C2T1)
return RT_EINVAL;
@ -69,21 +99,24 @@ static rt_err_t es32f0_pwm_control(struct rt_device_pwm *device, int cmd, void *
break;
case PWM_CMD_SET:
/* count registers max 0xFFFF, auto adjust prescaler*/
_ccep = timer_initstruct->perh->CCEP;
/* count registers max 0xFFFF, auto adjust prescaler */
do
{
timer_pwm_set_freq(timer_initstruct, 1000000000 / cfg->period);
pwm_set_freq(timer_initstruct, cfg->period);
timer_initstruct->init.prescaler ++;
}
while (timer_initstruct->init.period > 0xFFFF);
/* update prescaler */
WRITE_REG(timer_initstruct->perh->PRES, -- timer_initstruct->init.prescaler);
WRITE_REG(timer_initstruct->perh->PRES, --timer_initstruct->init.prescaler);
timer_oc_config_channel(timer_initstruct, &tim_ocinit, pwm_channel);
timer_pwm_set_duty(timer_initstruct, pwm_channel, cfg->pulse * 100 / cfg->period);
pwm_set_duty(timer_initstruct, pwm_channel, cfg->pulse);
timer_initstruct->perh->CCEP = _ccep;
break;
case PWM_CMD_GET:
cfg->pulse = timer_read_capture_value(timer_initstruct, pwm_channel) * 100 / READ_REG(timer_initstruct->perh->AR);
cfg->pulse = timer_read_capture_value(timer_initstruct, pwm_channel) * 100 /
READ_REG(timer_initstruct->perh->AR);
break;
default:
@ -123,7 +156,8 @@ int rt_hw_pwm_init(void)
gpio_init(GPIOA, GPIO_PIN_10, &gpio_initstructure);
gpio_init(GPIOA, GPIO_PIN_11, &gpio_initstructure);
ret = rt_device_pwm_register(&pwm_dev0, "pwm0", &es32f0_pwm_ops, &timer_initstruct0);
ret = rt_device_pwm_register(&pwm_dev0, "pwm0", &es32f0_pwm_ops,
&timer_initstruct0);
#endif
#ifdef BSP_USING_PWM1 /* 4 channels */
@ -140,7 +174,8 @@ int rt_hw_pwm_init(void)
gpio_init(GPIOB, GPIO_PIN_8, &gpio_initstructure);
gpio_init(GPIOB, GPIO_PIN_9, &gpio_initstructure);
ret = rt_device_pwm_register(&pwm_dev1, "pwm1", &es32f0_pwm_ops, &timer_initstruct1);
ret = rt_device_pwm_register(&pwm_dev1, "pwm1", &es32f0_pwm_ops,
&timer_initstruct1);
#endif
#ifdef BSP_USING_PWM2 /* 2 channels */
@ -155,7 +190,8 @@ int rt_hw_pwm_init(void)
gpio_init(GPIOA, GPIO_PIN_0, &gpio_initstructure);
gpio_init(GPIOA, GPIO_PIN_1, &gpio_initstructure);
ret = rt_device_pwm_register(&pwm_dev2, "pwm2", &es32f0_pwm_ops, &timer_initstruct2);
ret = rt_device_pwm_register(&pwm_dev2, "pwm2", &es32f0_pwm_ops,
&timer_initstruct2);
#endif
#ifdef BSP_USING_PWM3 /* 2 channels */
@ -170,7 +206,8 @@ int rt_hw_pwm_init(void)
gpio_init(GPIOC, GPIO_PIN_6, &gpio_initstructure);
gpio_init(GPIOC, GPIO_PIN_7, &gpio_initstructure);
ret = rt_device_pwm_register(&pwm_dev3, "pwm3", &es32f0_pwm_ops, &timer_initstruct3);
ret = rt_device_pwm_register(&pwm_dev3, "pwm3", &es32f0_pwm_ops,
&timer_initstruct3);
#endif
return ret;

View File

@ -82,7 +82,7 @@ rt_err_t spi_configure(struct rt_spi_device *device,
/* config spi clock */
if (cfg->max_hz >= cmu_get_pclk1_clock() / 2)
{
/*pclk1 max speed 48MHz, spi master max speed 10MHz*/
/* pclk1 max speed 48MHz, spi master max speed 10MHz */
if (cmu_get_pclk1_clock() / 2 <= 10000000)
{
hspi->init.baud = SPI_BAUD_2;
@ -98,7 +98,7 @@ rt_err_t spi_configure(struct rt_spi_device *device,
}
else if (cfg->max_hz >= cmu_get_pclk1_clock() / 4)
{
/*pclk1 max speed 48MHz, spi master max speed 10MHz*/
/* pclk1 max speed 48MHz, spi master max speed 10MHz */
if (cmu_get_pclk1_clock() / 4 <= 10000000)
{
hspi->init.baud = SPI_BAUD_4;
@ -150,7 +150,7 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
hspi = (spi_handle_t *)device->bus->parent.user_data;
cs = device->parent.user_data;
/***only send data*****/
/* only send data */
if (message->recv_buf == RT_NULL)
{
if (message->cs_take)
@ -166,7 +166,7 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
return RT_ERROR;
}
/***only receive data*****/
/* only receive data */
if (message->send_buf == RT_NULL)
{
if (message->cs_take)
@ -182,7 +182,7 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
return RT_ERROR;
}
/***send & receive*****/
/* send & receive */
else
{
if (message->cs_take)
@ -208,8 +208,8 @@ const struct rt_spi_ops es32f0_spi_ops =
spixfer,
};
struct rt_spi_bus _spi_bus0, _spi_bus1;
spi_handle_t _spi0, _spi1;
static struct rt_spi_bus _spi_bus0, _spi_bus1;
static spi_handle_t _spi0, _spi1;
int es32f0_spi_register_bus(SPI_TypeDef *SPIx, const char *name)
{
struct rt_spi_bus *spi_bus;
@ -222,17 +222,17 @@ int es32f0_spi_register_bus(SPI_TypeDef *SPIx, const char *name)
spi_bus = &_spi_bus0;
spi = &_spi0;
/*SPI0 gpio init*/
/* SPI0 gpio init */
gpio_instruct.mode = GPIO_MODE_OUTPUT;
gpio_instruct.odos = GPIO_PUSH_PULL;
gpio_instruct.func = GPIO_FUNC_4;
gpio_instruct.type = GPIO_TYPE_CMOS;
gpio_instruct.flt = GPIO_FILTER_DISABLE;
/*PB3->SPI0_SCK, PB5->SPI0_MOSI*/
/* PB3->SPI0_SCK, PB5->SPI0_MOSI */
gpio_init(GPIOB, GPIO_PIN_3 | GPIO_PIN_5, &gpio_instruct);
/*PB4->SPI0_MISO*/
/* PB4->SPI0_MISO */
gpio_instruct.mode = GPIO_MODE_INPUT;
gpio_init(GPIOB, GPIO_PIN_4, &gpio_instruct);
}
@ -242,17 +242,17 @@ int es32f0_spi_register_bus(SPI_TypeDef *SPIx, const char *name)
spi_bus = &_spi_bus1;
spi = &_spi1;
/*SPI1 gpio init*/
/* SPI1 gpio init */
gpio_instruct.mode = GPIO_MODE_OUTPUT;
gpio_instruct.odos = GPIO_PUSH_PULL;
gpio_instruct.func = GPIO_FUNC_4;
gpio_instruct.type = GPIO_TYPE_CMOS;
gpio_instruct.flt = GPIO_FILTER_DISABLE;
/*PB13->SPI1_SCK, PB15->SPI1_MOSI*/
/* PB13->SPI1_SCK, PB15->SPI1_MOSI */
gpio_init(GPIOB, GPIO_PIN_13 | GPIO_PIN_15, &gpio_instruct);
/*PB14->SPI1_MISO*/
/* PB14->SPI1_MISO */
gpio_instruct.mode = GPIO_MODE_INPUT;
gpio_init(GPIOB, GPIO_PIN_14, &gpio_instruct);
}
@ -267,7 +267,7 @@ int es32f0_spi_register_bus(SPI_TypeDef *SPIx, const char *name)
rt_err_t es32f0_spi_device_attach(rt_uint32_t pin, const char *bus_name, const char *device_name)
{
/*define spi Instance*/
/* define spi Instance */
struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
RT_ASSERT(spi_device != RT_NULL);
struct es32f0_hw_spi_cs *cs_pin = (struct es32f0_hw_spi_cs *)rt_malloc(sizeof(struct es32f0_hw_spi_cs));
@ -293,4 +293,5 @@ int rt_hw_spi_init(void)
return result;
}
INIT_BOARD_EXPORT(rt_hw_spi_init);
#endif /*RT_USING_SPI*/
#endif

View File

@ -20,7 +20,7 @@ struct es32f0_hw_spi_cs
rt_uint32_t pin;
};
//cannot be used before completion init
/* cannot be used before completion init */
rt_err_t es32f0_spi_device_attach(rt_uint32_t pin, const char *bus_name, const char *device_name);
int rt_hw_spi_init(void);

View File

@ -27,4 +27,5 @@ static int rt_hw_spi_flash_init(void)
return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
#endif

View File

@ -45,7 +45,7 @@ static rt_err_t es32f0x_configure(struct rt_serial_device *serial, struct serial
gpio_initstructure.func = GPIO_FUNC_3;
gpio_init(GPIOB, GPIO_PIN_10, &gpio_initstructure);
/* Initialize rx pin ,the same as txpin except mode*/
/* Initialize rx pin ,the same as txpin except mode */
gpio_initstructure.mode = GPIO_MODE_INPUT;
gpio_init(GPIOB, GPIO_PIN_11, &gpio_initstructure);
#endif /* uart0 gpio init */
@ -54,7 +54,7 @@ static rt_err_t es32f0x_configure(struct rt_serial_device *serial, struct serial
gpio_initstructure.func = GPIO_FUNC_3;
gpio_init(GPIOC, GPIO_PIN_10, &gpio_initstructure);
/* Initialize rx pin ,the same as txpin except mode*/
/* Initialize rx pin ,the same as txpin except mode */
gpio_initstructure.mode = GPIO_MODE_INPUT;
gpio_init(GPIOC, GPIO_PIN_11, &gpio_initstructure);
#endif /* uart1 gpio init */
@ -63,7 +63,7 @@ static rt_err_t es32f0x_configure(struct rt_serial_device *serial, struct serial
gpio_initstructure.func = GPIO_FUNC_5;
gpio_init(GPIOC, GPIO_PIN_12, &gpio_initstructure);
/* Initialize rx pin ,the same as txpin except mode*/
/* Initialize rx pin ,the same as txpin except mode */
gpio_initstructure.mode = GPIO_MODE_INPUT;
gpio_init(GPIOD, GPIO_PIN_2, &gpio_initstructure);
#endif /* uart2 gpio init */
@ -72,7 +72,7 @@ static rt_err_t es32f0x_configure(struct rt_serial_device *serial, struct serial
gpio_initstructure.func = GPIO_FUNC_4;
gpio_init(GPIOC, GPIO_PIN_4, &gpio_initstructure);
/* Initialize rx pin ,the same as txpin except mode*/
/* Initialize rx pin ,the same as txpin except mode */
gpio_initstructure.mode = GPIO_MODE_INPUT;
gpio_init(GPIOC, GPIO_PIN_5, &gpio_initstructure);
#endif /* uart3 gpio init */
@ -102,7 +102,7 @@ static rt_err_t es32f0x_configure(struct rt_serial_device *serial, struct serial
UART_DATA_INV_DISABLE(&uart->huart);
}
/*enable rx int*/
/* enable rx int */
uart_interrupt_config(&uart->huart, UART_IT_RXRD, ENABLE);
return RT_EOK;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 123 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 KiB

After

Width:  |  Height:  |  Size: 1.8 MiB