1、实验环境
-
硬件平台:CYUSB3KIT-003 EZ-USB® FX3™ SuperSpeed Explorer Kit
-
sdk版本:EZ-USB FX3 SDK1.3 / SuperSpeed Explorer Kit 1.0
-
实验例程:cyfxuvc_an75779(cypress官网下载demo)
2、实验目的
- 使用CYUSB3KIT-003的gpio_50(板子丝印I2S_CLK)输出pwm
3、程序添加
-
3.1 修改main函数gpio配置
GPIOs 50, 51 and 52 are used as complex GPIO
/*GPIOs 50, 51 and 52 are used as complex GPIO*/
io_cfg.gpioComplexEn[0] = 0;
io_cfg.gpioComplexEn[1] = 0x001C0000;
- 3.2 添加pwm的gpio初始化函数
/* GPIO application initialization function. */
void
CyFxGpioInit (void)
{
CyU3PGpioComplexConfig_t gpioConfig;
CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
//一个工程只能初始化一次gpioClock,如果其它地方初始化了,这里要屏蔽
#if 0
CyU3PGpioClock_t gpioClock;
/* Init the GPIO module. The GPIO block will be running
* with a fast clock at SYS_CLK / 2 and slow clock is not
* used. For the DVK, the SYS_CLK is running at 403 MHz.*/
gpioClock.fastClkDiv = 2;
gpioClock.slowClkDiv = 0;
gpioClock.simpleDiv = CY_U3P_GPIO_SIMPLE_DIV_BY_2;
gpioClock.clkSrc = CY_U3P_SYS_CLK;
gpioClock.halfDiv = 0;
// apiRetStatus = CyU3PGpioInit(&gpioClock, NULL);
if (apiRetStatus != 0)
{
/* Error Handling */
CyU3PDebugPrint (4, "CyU3PGpioInit failed, error code = %d\n", apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}
#endif
/* Configure GPIO 50 as PWM output */
gpioConfig.outValue = CyFalse;
gpioConfig.inputEn = CyFalse;
gpioConfig.driveLowEn = CyTrue;
gpioConfig.driveHighEn = CyTrue;
gpioConfig.pinMode = CY_U3P_GPIO_MODE_PWM;
gpioConfig.intrMode = CY_U3P_GPIO_NO_INTR;
gpioConfig.timerMode = CY_U3P_GPIO_TIMER_HIGH_FREQ;
gpioConfig.timer = 0;
gpioConfig.period = 10; //周期
gpioConfig.threshold = 5; //占空比
apiRetStatus = CyU3PGpioSetComplexConfig(50, &gpioConfig);
if (apiRetStatus != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "CyU3PGpio50SetComplexConfig failed, error code = %d\n",
apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}
/* Configure GPIO 51 as input for single time low measurement. */
gpioConfig.outValue = CyTrue;
gpioConfig.inputEn = CyTrue;
gpioConfig.driveLowEn = CyFalse;
gpioConfig.driveHighEn = CyFalse;
/* For doing one time measurements, this value should be
* set to static. For doing contiunous measurement, this
* value should be set to the required mode. */
gpioConfig.pinMode = CY_U3P_GPIO_MODE_STATIC;
gpioConfig.intrMode = CY_U3P_GPIO_NO_INTR;
gpioConfig.timerMode = CY_U3P_GPIO_TIMER_HIGH_FREQ;
gpioConfig.timer = 0;
/* Load the period to the maximum value
* so that the count is not reset. */
gpioConfig.period = 0xFFFFFFFF;
gpioConfig.threshold = 0;
apiRetStatus = CyU3PGpioSetComplexConfig(51, &gpioConfig);
if (apiRetStatus != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "CyU3PGpio51SetComplexConfig failed, error code = %d\n",
apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}
/* Enable a weak pullup on GPIO 52. */
apiRetStatus = CyU3PGpioSetIoMode (52, CY_U3P_GPIO_IO_MODE_WPU);
if (apiRetStatus != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "CyU3PGpioSetIoMode failed, error code = %d\n",
apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}
/* Configure GPIO 52 as input for counter mode. */
gpioConfig.outValue = CyTrue;
gpioConfig.inputEn = CyTrue;
gpioConfig.driveLowEn = CyFalse;
gpioConfig.driveHighEn = CyFalse;
gpioConfig.pinMode = CY_U3P_GPIO_MODE_STATIC;
gpioConfig.intrMode = CY_U3P_GPIO_NO_INTR;
gpioConfig.timerMode = CY_U3P_GPIO_TIMER_NEG_EDGE;
gpioConfig.timer = 0;
/* Load the period to the maximum value
* so that the count is not reset. */
gpioConfig.period = 0xFFFFFFFF;
gpioConfig.threshold = 0;
apiRetStatus = CyU3PGpioSetComplexConfig(52, &gpioConfig);
if (apiRetStatus != CY_U3P_SUCCESS)
{
CyU3PDebugPrint (4, "CyU3PGpioSetComplexConfig failed, error code = %d\n",
apiRetStatus);
CyFxAppErrorHandler(apiRetStatus);
}
}
注意:一个工程只能初始化一次gpioClock,代码中屏蔽了初始化代码 ,因为其它地方已经初始化,根据实际情况决定要不要打开
- 3.3调用pwm初始化函数
void
UVCAppThread_Entry (
uint32_t input)
{
CyU3PReturnStatus_t apiRetStatus;
uint32_t flag;
/* Initialize the Uart Debug Module */
CyFxUVCApplnDebugInit ();
/* Initialize the I2C interface */
CyFxUVCApplnI2CInit ();
/* Initialize the UVC Application */
CyFxUVCApplnInit ();
/*add by allen gpio 50 output pwm*/
CyFxGpioInit();
ps:可以自己新建一个线程专门控制pwm,也可以在原有线程中添加,本例在官方原有线程中添加的。
- 3.4 pwm频率和占空比动态调整API
/** \brief Update the timer period and threshold associated with a complex GPIO.
**Description**\n
The function updates the timer period and threshold value associated with a
complex GPIO. This operation is only permitted if the corresponding is configured
in the PWM or STATIC modes.
**Return value**\n
* CY_U3P_SUCCESS - If the operation is successful.\n
* CY_U3P_ERROR_NOT_STARTED - If the GPIO block has not been initialized.\n
* CY_U3P_ERROR_BAD_ARGUMENT - If the IO pin is invalid.
**\see
*\see CyU3PGpioSetComplexConfig
*\see CyU3PGpioComplexGetThreshold
*/
extern CyU3PReturnStatus_t
CyU3PGpioComplexUpdate (
uint8_t gpioId, /**< The complex GPIO to sample. */
uint32_t threshold, /**< New timer threshold value to be updated. */
uint32_t period /**< New timer period value to be updated. */
);
4 测试
- 使用示波器可以看到pwm输出