前言
CYT4BB7芯片,在开发过程中,通过Eclipse IDE for ModusToolbox™ 3.4的开发环境可以配置很多功能,其中就包括串口。配置起来很省事,配置完成后保存代码自动生成,在main方法中给串口初始化一下就可以实现发送的功能了。以下就是配置串口的前置条件和方法。
1.准备环境
安装Eclipse IDE for ModusToolbox™ 3.4开发环境,以及gcc编译相关软件。具体参考英飞凌官方文档。
2.创建Application
2.1 在quick panel中点击New Application按钮
2.2 Create from MPN
2.3 选择对应芯片型号
2.4 点击Close
2.5 Next
2.6 Create
3. 创建Application后首次编译
3.1 注掉报错代码
打开main.c文件,并注释掉#include "mtb_hal.h",否则编译会报错
3.2 首次编译
点击Quick Panel中的Build Project
4. 配置串口
4.1 打开配置工具
4.2 选择配置串口
4.3 串口配置
此时还没有配置后,如果按CTRL+S后会报错,因为时钟和引脚都还没配置。
4.4 时钟配置
4.4.1 System配置
在配置时钟之前,需要先配置System,以便时钟可以正确选择。
UART打算采用Periheral group1组的时钟,因此需要使能CLK_HF2,通路选择CLK_PATH3
Periheral group1组的时钟频率为100MHz。
4.4.2 时钟设置和关联
4.4.3 再次配置串口
OverSample需要设置成12后,实际波特率才能接近115200,但是会存在一些误差。
引脚选择15.0是接收,15.1是发送。
4.5 保存并生成代码
5. 编码实现串口发送
5.1 配置后生成的代码
5.2 时钟等的初始化
main函数中的cybsp_init方法,会调用cycfg_config_init()方法。这个初始化方法已经把系统、时钟、引脚等都初始化好了。
void cycfg_config_init(void)
{
init_cycfg_system();
init_cycfg_clocks();
init_cycfg_routing();
init_cycfg_peripherals();
init_cycfg_pins();
}
5.3 串口初始化
参考网页中的初始化方法(网页是在配置中点击打开网页的)
给出main.c的代码示例
/*******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the Empty Application Example
* for ModusToolbox.
*
* Related Document: See README.md
*
*******************************************************************************
* Copyright 2024, 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 "mtb_hal.h"
#include "cybsp.h"
/*******************************************************************************
* Macros
*******************************************************************************/
/*******************************************************************************
* Global Variables
*******************************************************************************/
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
/*******************************************************************************
* Function Definitions
*******************************************************************************/
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function for CPU. It...
* 1.
* 2.
*
* Parameters:
* void
*
* Return:
* int
*
*******************************************************************************/
uint8_t txBuffer[1024] = "Hello World!\r\n";
cy_stc_scb_uart_context_t uartContext;
void uart_send_datas(uint8_t *buf, uint32_t len)
{
Cy_SCB_UART_PutArrayBlocking(SCB9, buf, len);
/* Blocking wait for transfer completion */
while (!Cy_SCB_UART_IsTxComplete(SCB9))
{
}
}
void delay_ms(uint32_t ms)
{
uint32_t tmp = 0, outj = 0;
for(outj = 0; outj < ms; outj++)
{
for(tmp = 0; tmp < 3200; tmp++)
{
__ASM ("NOP");
}
}
}
int main(void)
{
cy_rslt_t result;
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
Cy_SCB_UART_Init(SCB9, &scb_9_config, &uartContext);
Cy_SCB_UART_Enable(SCB9);
/* Enable global interrupts */
__enable_irq();
for (;;)
{
uart_send_datas(txBuffer, 14);
delay_ms(1000);
}
}
/* [] END OF FILE */
6. 编译烧录
编译完成后,烧录固件。