【RTT&英飞凌PSoC6评估板】HELLO和PWM测试
[复制链接]
前几天一直都是使用Eclipse作为开发工具,这个工具用起来感觉不是很习惯,下面的测试使用的是比较熟悉的keil IDE,首先打开:project-creator软件,这是独立的软件。
得益于离线项目的实现,测试的速度大大的提高了。要不然每个项目都要等上好久,而且运气不好还可能失败。
选择模板项目,HAL PWM Square Wave
创建后,打开device-configrator
因为要使用SWO引脚为UART引脚 ,需要修改SWD设置,去除SWO引脚设置,
然后将WCO时钟去掉,CLK_LF为ILO源
这样板子就可以跑起来了。今天搞明白设置前的勾是干什么地,这个勾选上后,保存时就可以生成对应的程序了。
#if defined (CY_USING_HAL)
const cyhal_resource_inst_t CYBSP_USER_LED1_obj =
{
.type = CYHAL_RSC_GPIO,
.block_num = CYBSP_USER_LED1_PORT_NUM,
.channel_num = CYBSP_USER_LED1_PIN,
};
#endif //defined (CY_USING_HAL)
void init_cycfg_pins(void)
{
Cy_GPIO_Pin_Init(CYBSP_USER_LED1_PORT, CYBSP_USER_LED1_PIN, &CYBSP_USER_LED1_config);
}
void reserve_cycfg_pins(void)
{
#if defined (CY_USING_HAL)
cyhal_hwmgr_reserve(&CYBSP_USER_LED1_obj);
#endif //defined (CY_USING_HAL)
}
在hello项目中的没有选择自动生成,是因为在main.c中有对应的初始化代码,选上就冲突了。
将引脚CYBSP_DEBUG_UART_RX和CYBSP_DEBUG_UART_TX设置到P6_4和P6_5上面,先选上勾保存save一下,生成程序。同时去除原来的设置。去掉勾在保存一次,就只保留设置了,而不会生成程序化程序了。同样的操作修改CYBSP_USER_LED1, CYBSP_USER_LED P0.0为PWM输出,后面说明。
修改完成,打开keil
可以发现主要的测试语句。
/*******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the PWM square wave code example
* for ModusToolbox.
*
* Related Document: See README.md
*
********************************************************************************
* Copyright 2019-2023, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
*
* This software, including source code, documentation and related
* materials ("Software") is owned by Cypress Semiconductor Corporation
* or one of its affiliates ("Cypress") and is protected by and subject to
* worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ("EULA").
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software
* source code solely for use in connection with Cypress's
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
*******************************************************************************/
/*******************************************************************************
* Header Files
*******************************************************************************/
#include "cybsp.h"
#include "cyhal.h"
#include "cy_retarget_io.h"
#include <inttypes.h>
/*******************************************************************************
* Macros
*******************************************************************************/
/* PWM Frequency = 2Hz */
#define PWM_FREQUENCY (2u)
/* PWM Duty-cycle = 50% */
#define PWM_DUTY_CYCLE (50.0f)
/*******************************************************************************
* Global Variables
*******************************************************************************/
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
/*******************************************************************************
* Function Definitions
*******************************************************************************/
/*******************************************************************************
* Function Name: handle_error
********************************************************************************
* Summary:
* User defined error handling function.
*
* Parameters:
* status - status for evaluation.
*
* Return:
* void
*
*******************************************************************************/
void handle_error(cy_rslt_t status)
{
if (CY_RSLT_SUCCESS != status)
{
/* Halt the CPU while debugging */
CY_ASSERT(0);
}
}
/*******************************************************************************
* Function Name: check_status
********************************************************************************
* Summary:
* Prints the message and waits forever when an error occurs.
*
* Parameters:
* message - message to print if status is non-zero.
* status - status for evaluation.
*
* Return:
* void
*
*******************************************************************************/
void check_status(char *message, cy_rslt_t status)
{
if (CY_RSLT_SUCCESS != status)
{
printf("\r\n=====================================================\r\n");
printf("\nFAIL: %s\r\n", message);
printf("Error Code: 0x%08" PRIX32 "\n", status);
printf("\r\n=====================================================\r\n");
while(true);
}
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function for the CPU. It configures the PWM and puts the CPU
* in Sleep mode to save power.
*
* Parameters:
* void
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
/* PWM object */
cyhal_pwm_t pwm_led_control;
/* API return code */
cy_rslt_t result;
#if defined(CY_DEVICE_SECURE)
cyhal_wdt_t wdt_obj;
/* Clear watchdog timer so that it doesn't trigger a reset */
result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
CY_ASSERT(CY_RSLT_SUCCESS == result);
cyhal_wdt_free(&wdt_obj);
#endif
/* Initialize the device and board peripherals */
result = cybsp_init();
handle_error(result);
/* Enable global interrupts */
__enable_irq();
/* Initialize the retarget-io to use the debug UART port */
result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
CY_RETARGET_IO_BAUDRATE);
handle_error(result);
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("****************** "
"HAL: PWM square wave "
"****************** \r\n\n");
/* In this example, PWM output is routed to the user LED on the kit.
See HAL API Reference document for API details. */
/* Initialize the PWM */
result = cyhal_pwm_init(&pwm_led_control, CYBSP_USER_LED, NULL);
check_status("API cyhal_pwm_init failed with error code", result);
/* Set the PWM output frequency and duty cycle */
result = cyhal_pwm_set_duty_cycle(&pwm_led_control, PWM_DUTY_CYCLE,
PWM_FREQUENCY);
check_status("API cyhal_pwm_set_duty_cycle failed with error code", result);
/* Start the PWM */
result = cyhal_pwm_start(&pwm_led_control);
check_status("API cyhal_pwm_start failed with error code", result);
printf("PWM started successfully. Entering the sleep mode...\r\n");
for (;;)
{
/* Put the CPU into sleep mode to save power */
cyhal_syspm_sleep();
}
}
/* [] END OF FILE */
主要的代码是cyhal_pwm_init()和cyhal_pwm_set_duty_cycle()两个函数,可以发现引脚参数为CYBSP_USER_LED,所以就可以将LED1点亮了。这两个函数将LCD MTB CAT1 Peripheral driver library函数库的功能封装其中。与cy_retarget_io_init()函数一样其中隐藏了很多细节。编译运行后就可以看到LED1不停的闪烁,而且不需要程序控制电平的变化。
程序运行后进入到sleep状态。
|