diff --git a/bsp/es32f0654/.config b/bsp/es32f0654/.config index ced622f5ff..10825f5a19 100644 --- a/bsp/es32f0654/.config +++ b/bsp/es32f0654/.config @@ -338,6 +338,14 @@ CONFIG_BSP_USING_UART2=y # CONFIG_BSP_USING_I2C0 is not set # CONFIG_BSP_USING_I2C1 is not set +# +# PWM Drivers +# +# CONFIG_BSP_USING_PWM0 is not set +# CONFIG_BSP_USING_PWM1 is not set +# CONFIG_BSP_USING_PWM2 is not set +# CONFIG_BSP_USING_PWM3 is not set + # # Onboard Peripheral Drivers # diff --git a/bsp/es32f0654/README.md b/bsp/es32f0654/README.md index ff25d6a58f..a4e79cabb3 100644 --- a/bsp/es32f0654/README.md +++ b/bsp/es32f0654/README.md @@ -42,8 +42,7 @@ ES-PDS-ES32F0654-V1.0 | UART | 支持 | UART0/1/2/3 | | SPI | 支持 | SPI0/1 | | I2C | 支持 | I2C0/1 | - -| **扩展模块** | **支持情况** | **备注** | +| PWM | 支持 | PWM0/1/2/3 | 更多详细信息请咨询[上海东软载波微电子技术支持](http://www.essemi.com/) diff --git a/bsp/es32f0654/drivers/Kconfig b/bsp/es32f0654/drivers/Kconfig index 90d69f8183..10c965a422 100644 --- a/bsp/es32f0654/drivers/Kconfig +++ b/bsp/es32f0654/drivers/Kconfig @@ -53,6 +53,27 @@ menu "Hardware Drivers Config" default n endmenu + menu "PWM Drivers" + config BSP_USING_PWM0 + bool "Using PWM0 PA08/PA09/PA10/PA11" + select RT_USING_PWM + default n + + config BSP_USING_PWM1 + bool "Using PWM1 PB06/PB07/PB08/PB09" + select RT_USING_PWM + default n + + config BSP_USING_PWM2 + bool "Using PWM2 PA00/PA01" + select RT_USING_PWM + default n + + config BSP_USING_PWM3 + bool "Using PWM3 PC06/PC07" + select RT_USING_PWM + default n + endmenu endmenu menu "Onboard Peripheral Drivers" diff --git a/bsp/es32f0654/drivers/SConscript b/bsp/es32f0654/drivers/SConscript index 10b6261688..17484f3491 100644 --- a/bsp/es32f0654/drivers/SConscript +++ b/bsp/es32f0654/drivers/SConscript @@ -27,6 +27,10 @@ if GetDepend('BSP_USING_I2C0') or GetDepend('BSP_USING_I2C1'): if GetDepend('BSP_USING_SPI_FLASH'): src += ['drv_spiflash.c'] +# add pwm driver code +if GetDepend('BSP_USING_PWM0') or GetDepend('BSP_USING_PWM1') or GetDepend('BSP_USING_PWM2') or GetDepend('BSP_USING_PWM3'): + src += ['drv_pwm.c'] + CPPPATH = [cwd] group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH) diff --git a/bsp/es32f0654/drivers/board.c b/bsp/es32f0654/drivers/board.c index 8918377750..ea95263da4 100644 --- a/bsp/es32f0654/drivers/board.c +++ b/bsp/es32f0654/drivers/board.c @@ -58,14 +58,8 @@ void SystemClock_Config(void) *******************************************************************************/ void SysTick_Configuration(void) { - rt_uint32_t _mclk; - rt_uint32_t _sys_div = READ_BITS(CMU->CFGR, CMU_CFGR_SYSDIV_MSK, CMU_CFGR_SYSDIV_POSS); - - /* get hrc clock*/ - _mclk = cmu_get_clock(); - - /* SYSCLK = MCLK/SYSDIV */ - SysTick_Config(_mclk / (RT_TICK_PER_SECOND << _sys_div)); + /* ticks = sysclk / RT_TICK_PER_SECOND */ + SysTick_Config(cmu_get_sys_clock() / RT_TICK_PER_SECOND); } /** @@ -108,3 +102,20 @@ void rt_hw_board_init(void) rt_console_set_device(RT_CONSOLE_DEVICE_NAME); #endif } + +/** + * This function will delay for some us. + * + * @param us the delay time of us + */ +void rt_hw_us_delay(rt_uint32_t us) +{ + unsigned int start, now, delta, reload, us_tick; + start = SysTick->VAL; + reload = SysTick->LOAD; + us_tick = cmu_get_sys_clock() / 1000000UL; + do{ + now = SysTick->VAL; + delta = start > now ? start - now : reload + start - now; + } while(delta < us_tick * us); +} diff --git a/bsp/es32f0654/drivers/drv_pwm.c b/bsp/es32f0654/drivers/drv_pwm.c new file mode 100644 index 0000000000..f1721fea96 --- /dev/null +++ b/bsp/es32f0654/drivers/drv_pwm.c @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-03-11 wangyq the first version + */ + +#include +#include +#include +#include +#include +#include + +static rt_err_t es32f0_pwm_control(struct rt_device_pwm *device, int cmd, void *arg) +{ + rt_err_t ret = RT_EOK; + timer_channel_t pwm_channel; + timer_oc_init_t tim_ocinit; + timer_handle_t *timer_initstruct = (timer_handle_t *)device->parent.user_data; + struct rt_pwm_configuration *cfg = (struct rt_pwm_configuration *)arg; + + RT_ASSERT(timer_initstruct != RT_NULL); + + tim_ocinit.oc_mode = TIMER_OC_MODE_PWM1; + tim_ocinit.oc_polarity = TIMER_OC_POLARITY_HIGH; + tim_ocinit.oc_fast_en = DISABLE; + tim_ocinit.ocn_polarity = TIMER_OCN_POLARITY_HIGH; + tim_ocinit.ocn_idle = TIMER_OCN_IDLE_RESET; + tim_ocinit.oc_idle = TIMER_OC_IDLE_RESET; + + /* select pwm output channel */ + if (0 == cfg->channel) + { + pwm_channel = TIMER_CHANNEL_1; + } + else if (1 == cfg->channel) + { + pwm_channel = TIMER_CHANNEL_2; + } + else if (2 == cfg->channel) + { + if (timer_initstruct->perh == GP16C2T0 || timer_initstruct->perh == GP16C2T1) + return RT_EINVAL; + pwm_channel = TIMER_CHANNEL_3; + } + else if (3 == cfg->channel) + { + if (timer_initstruct->perh == GP16C2T0 || timer_initstruct->perh == GP16C2T1) + return RT_EINVAL; + pwm_channel = TIMER_CHANNEL_4; + } + else + { + return RT_EINVAL; + } + + switch (cmd) + { + case PWM_CMD_ENABLE: + timer_pwm_start(timer_initstruct, pwm_channel); + break; + + case PWM_CMD_DISABLE: + timer_pwm_stop(timer_initstruct, pwm_channel); + break; + + case PWM_CMD_SET: + /* count registers max 0xFFFF, auto adjust prescaler*/ + do + { + timer_pwm_set_freq(timer_initstruct, 1000000000 / cfg->period); + timer_initstruct->init.prescaler ++; + } + while (timer_initstruct->init.period > 0xFFFF); + /* update 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); + break; + + case PWM_CMD_GET: + cfg->pulse = timer_read_capture_value(timer_initstruct, pwm_channel) * 100 / READ_REG(timer_initstruct->perh->AR); + break; + + default: + break; + } + return ret; +} + +const static struct rt_pwm_ops es32f0_pwm_ops = +{ + es32f0_pwm_control +}; + +int rt_hw_pwm_init(void) +{ + rt_err_t ret = RT_EOK; + gpio_init_t gpio_initstructure; + + gpio_initstructure.mode = GPIO_MODE_OUTPUT; + gpio_initstructure.odos = GPIO_PUSH_PULL; + gpio_initstructure.pupd = GPIO_PUSH_UP; + gpio_initstructure.odrv = GPIO_OUT_DRIVE_NORMAL; + gpio_initstructure.flt = GPIO_FILTER_DISABLE; + gpio_initstructure.type = GPIO_TYPE_TTL; + +#ifdef BSP_USING_PWM0 /* 4 channels */ + static struct rt_device_pwm pwm_dev0; + static timer_handle_t timer_initstruct0; + + timer_initstruct0.perh = AD16C4T0; + timer_pwm_init(&timer_initstruct0); + + /* gpio initialization */ + gpio_initstructure.func = GPIO_FUNC_2; + gpio_init(GPIOA, GPIO_PIN_8, &gpio_initstructure); + gpio_init(GPIOA, GPIO_PIN_9, &gpio_initstructure); + 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); +#endif + +#ifdef BSP_USING_PWM1 /* 4 channels */ + static struct rt_device_pwm pwm_dev1; + static timer_handle_t timer_initstruct1; + + timer_initstruct1.perh = GP16C4T0; + timer_pwm_init(&timer_initstruct1); + + /* gpio initialization */ + gpio_initstructure.func = GPIO_FUNC_2; + gpio_init(GPIOB, GPIO_PIN_6, &gpio_initstructure); + gpio_init(GPIOB, GPIO_PIN_7, &gpio_initstructure); + 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); +#endif + +#ifdef BSP_USING_PWM2 /* 2 channels */ + static struct rt_device_pwm pwm_dev2; + static timer_handle_t timer_initstruct2; + + timer_initstruct2.perh = GP16C2T0; + timer_pwm_init(&timer_initstruct2); + + /* gpio initialization */ + gpio_initstructure.func = GPIO_FUNC_2; + 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); +#endif + +#ifdef BSP_USING_PWM3 /* 2 channels */ + static struct rt_device_pwm pwm_dev3; + static timer_handle_t timer_initstruct3; + + timer_initstruct3.perh = GP16C2T1; + timer_pwm_init(&timer_initstruct3); + + /* gpio initialization */ + gpio_initstructure.func = GPIO_FUNC_3; + 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); +#endif + + return ret; +} +INIT_DEVICE_EXPORT(rt_hw_pwm_init); diff --git a/bsp/es32f0654/drivers/drv_pwm.h b/bsp/es32f0654/drivers/drv_pwm.h new file mode 100644 index 0000000000..f4fcfe7cff --- /dev/null +++ b/bsp/es32f0654/drivers/drv_pwm.h @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd. + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-03-11 wangyq the first version + */ + +#ifndef DRV_PWM_H__ +#define DRV_PWM_H__ + +int rt_hw_pwm_init(void); + +#endif diff --git a/bsp/es32f0654/drivers/drv_uart.c b/bsp/es32f0654/drivers/drv_uart.c index 4a8838108a..e3b89e9afe 100644 --- a/bsp/es32f0654/drivers/drv_uart.c +++ b/bsp/es32f0654/drivers/drv_uart.c @@ -27,62 +27,54 @@ struct es32_uart static rt_err_t es32f0x_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { - gpio_init_t gpio_init_initstructure; + gpio_init_t gpio_initstructure; struct es32_uart *uart; RT_ASSERT(serial != RT_NULL); RT_ASSERT(cfg != RT_NULL); uart = (struct es32_uart *)serial->parent.user_data; /* Initialize tx pin */ - gpio_init_initstructure.mode = GPIO_MODE_OUTPUT; - gpio_init_initstructure.odos = GPIO_PUSH_PULL; - gpio_init_initstructure.pupd = GPIO_PUSH_UP; - gpio_init_initstructure.odrv = GPIO_OUT_DRIVE_NORMAL; - gpio_init_initstructure.flt = GPIO_FILTER_DISABLE; - gpio_init_initstructure.type = GPIO_TYPE_TTL; + gpio_initstructure.mode = GPIO_MODE_OUTPUT; + gpio_initstructure.odos = GPIO_PUSH_PULL; + gpio_initstructure.pupd = GPIO_PUSH_UP; + gpio_initstructure.odrv = GPIO_OUT_DRIVE_NORMAL; + gpio_initstructure.flt = GPIO_FILTER_DISABLE; + gpio_initstructure.type = GPIO_TYPE_TTL; #ifdef BSP_USING_UART0 - gpio_init_initstructure.func = GPIO_FUNC_3; - gpio_init(GPIOB, GPIO_PIN_10, &gpio_init_initstructure); + gpio_initstructure.func = GPIO_FUNC_3; + gpio_init(GPIOB, GPIO_PIN_10, &gpio_initstructure); /* Initialize rx pin ,the same as txpin except mode*/ - gpio_init_initstructure.mode = GPIO_MODE_INPUT; - gpio_init(GPIOB, GPIO_PIN_11, &gpio_init_initstructure); - - NVIC_EnableIRQ(UART0_IRQn); + gpio_initstructure.mode = GPIO_MODE_INPUT; + gpio_init(GPIOB, GPIO_PIN_11, &gpio_initstructure); #endif /* uart0 gpio init */ #ifdef BSP_USING_UART1 - gpio_init_initstructure.func = GPIO_FUNC_3; - gpio_init(GPIOC, GPIO_PIN_10, &gpio_init_initstructure); + gpio_initstructure.func = GPIO_FUNC_3; + gpio_init(GPIOC, GPIO_PIN_10, &gpio_initstructure); /* Initialize rx pin ,the same as txpin except mode*/ - gpio_init_initstructure.mode = GPIO_MODE_INPUT; - gpio_init(GPIOC, GPIO_PIN_11, &gpio_init_initstructure); - - NVIC_EnableIRQ(UART1_IRQn); + gpio_initstructure.mode = GPIO_MODE_INPUT; + gpio_init(GPIOC, GPIO_PIN_11, &gpio_initstructure); #endif /* uart1 gpio init */ #ifdef BSP_USING_UART2 - gpio_init_initstructure.func = GPIO_FUNC_5; - gpio_init(GPIOC, GPIO_PIN_12, &gpio_init_initstructure); + gpio_initstructure.func = GPIO_FUNC_5; + gpio_init(GPIOC, GPIO_PIN_12, &gpio_initstructure); /* Initialize rx pin ,the same as txpin except mode*/ - gpio_init_initstructure.mode = GPIO_MODE_INPUT; - gpio_init(GPIOD, GPIO_PIN_2, &gpio_init_initstructure); - - NVIC_EnableIRQ(BS16T1_UART2_IRQn); + gpio_initstructure.mode = GPIO_MODE_INPUT; + gpio_init(GPIOD, GPIO_PIN_2, &gpio_initstructure); #endif /* uart2 gpio init */ #ifdef BSP_USING_UART3 - gpio_init_initstructure.func = GPIO_FUNC_4; - gpio_init(GPIOC, GPIO_PIN_4, &gpio_init_initstructure); + gpio_initstructure.func = GPIO_FUNC_4; + gpio_init(GPIOC, GPIO_PIN_4, &gpio_initstructure); /* Initialize rx pin ,the same as txpin except mode*/ - gpio_init_initstructure.mode = GPIO_MODE_INPUT; - gpio_init(GPIOC, GPIO_PIN_5, &gpio_init_initstructure); - - NVIC_EnableIRQ(BS16T2_UART3_IRQn); + gpio_initstructure.mode = GPIO_MODE_INPUT; + gpio_init(GPIOC, GPIO_PIN_5, &gpio_initstructure); #endif /* uart3 gpio init */ uart->huart.init.mode = UART_MODE_UART; diff --git a/bsp/es32f0654/project.uvoptx b/bsp/es32f0654/project.uvoptx index d3460f8030..a89435d3f3 100644 --- a/bsp/es32f0654/project.uvoptx +++ b/bsp/es32f0654/project.uvoptx @@ -22,11 +22,11 @@ - rt-thread_es32f065x + rt-thread 0x4 ARM-ADS - 24000000 + 12000000 1 1 @@ -73,11 +73,11 @@ 0 - 1 + 0 0 1 - 255 + 0 0 1 @@ -95,7 +95,7 @@ 1 0 1 - 0 + 1 1 1 0 @@ -117,18 +117,13 @@ 0 - CMSIS_AGDI - -X"Any" -UAny -O206 -S0 -C0 -P00 -N00("ARM CoreSight SW-DP") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP20 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO15 -FD20000000 -FC1000 -FN1 -FF0ES32F065x -FS00 -FL040000 + JL2CM3 + -U12345678 -O78 -S4 -ZTIFSpeedSel2000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD20000000 -FC1000 -FN1 -FF0ES32F065x.FLM -FS00 -FL040000 -FP0($$Device:ES32F0654LT$Flash\ES32F065x.FLM) 0 UL2CM3 - UL2CM3(-S0 -C0 -P0 ) -FN1 -FC1000 -FD20000000 -FF0es32f0xx -FL040000 -FS00 -FP0($$Device:ES32F0654LT$Flash\es32f0xx.FLM) - - - 0 - JL2CM3 - -U12345678 -O78 -S4 -ZTIFSpeedSel2000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -TO18 -TC10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD20000000 -FC1000 -FN1 -FF0ES32F065x -FS00 -FL040000 + UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0ES32F065x -FS00 -FL040000 -FP0($$Device:ES32F0654LT$Flash\ES32F065x.FLM)) diff --git a/bsp/es32f0654/project.uvprojx b/bsp/es32f0654/project.uvprojx index b3cf47908e..3c8cc74030 100644 --- a/bsp/es32f0654/project.uvprojx +++ b/bsp/es32f0654/project.uvprojx @@ -4,7 +4,7 @@
### uVision Project, (C) Keil Software
- rt-thread_es32f065x + rt-thread 0x4 ARM-ADS diff --git a/bsp/es32f0654/rtconfig.h b/bsp/es32f0654/rtconfig.h index 77e177cc25..1e4d3edbfe 100644 --- a/bsp/es32f0654/rtconfig.h +++ b/bsp/es32f0654/rtconfig.h @@ -169,6 +169,9 @@ /* I2C Drivers */ +/* PWM Drivers */ + + /* Onboard Peripheral Drivers */