从0开始创建FX3工程之一

来源:互联网 发布:linux vncserver 安装 编辑:程序博客网 时间:2024/05/18 18:55

我们一般都是在已有的工程上修改,添加新的功能,现在从0开始创建一个新的工程。
1. 创建工程 菜单栏,File-new-project- Cypress
这里写图片描述
点击next,进入下图:
这里写图片描述
输入项目名,点击finish即可。(有三个模板可供选择,此处我们不需要勾选)
2. 而后我们看到项目有三个文件,mekefile先不管,里面很多问题。
cyfxtx.c :This file provides the application specific exception handlers and memory allocation routines. 了解即可。不需要更改此文件。
cyfx_gcc_startup.S 是启动汇编代码,作用是初始化bss和堆栈等,最够跳入main函数(b main)。不需要更改此文件。
2. 下一步我们要做的就是创建main函数,并添加我们的功能代码了。
我们添加两个文件main.c main.h .
main函数的框架cypress已经给我们规定好了.
main.h :

#ifndef MAIN_H_#define MAIN_H_#include "cyu3types.h"#define THREAD_STACK       (0x0200)    /* App thread stack size */#define THREAD_PRIORITY    (8)         /* App thread priority */#endif /* MAIN_H_ */

main.c :

#include "cyu3system.h"#include "cyu3os.h"#include "cyu3error.h"#include "cyu3gpio.h"#include "cyu3uart.h"#include "cyu3utils.h"#include "main.h"CyU3PThread Thread1;CyU3PThread Thread2;voidThread1_Entry (        uint32_t input){    for (;;)    {        CyU3PThreadSleep (100);    }}voidThread2_Entry (        uint32_t input){    for (;;)    {        CyU3PThreadSleep (100);    }}/* Application define function which creates the application threads. */voidCyFxApplicationDefine (        void){//CyFxApplicationDefine用来创建线程的。此处我们创建2个线程    void *ptr    = NULL;    uint32_t ret = CY_U3P_ERROR_MEMORY_ERROR;    // Create thread1    ptr = CyU3PMemAlloc (THREAD_STACK);    if (ptr != NULL)    {        ret = CyU3PThreadCreate (                &Thread1,                              /* Thread structure. */                "Thread 1",                     /* Thread ID and name. */                Thread1_Entry,                         /* Thread entry function. */                0,                                     /* Thread input parameter. */                ptr,                                   /* Pointer to the allocated thread stack. */                THREAD_STACK,                          /* Allocated thread stack size. */                THREAD_PRIORITY,                       /* Thread priority. */                THREAD_PRIORITY,                       /* Thread pre-emption threshold: No preemption. */                CYU3P_NO_TIME_SLICE,                   /* No time slice. */                CYU3P_AUTO_START                       /* Start the thread immediately. */                );    }    else    {        ret = CY_U3P_ERROR_MEMORY_ERROR;    }    if (ret != CY_U3P_SUCCESS)    {        goto InitFail;    }    // Create thread2    ptr = CyU3PMemAlloc (THREAD_STACK);    if (ptr != NULL)    {       ret = CyU3PThreadCreate (               &Thread2,                              /* Thread structure. */               "Thread 2",                            /* Thread ID and name. */               Thread2_Entry,                         /* Thread entry function. */               0,                                     /* Thread input parameter. */               ptr,                                   /* Pointer to the allocated thread stack. */               THREAD_STACK,                          /* Allocated thread stack size. */               THREAD_PRIORITY,                       /* Thread priority. */               THREAD_PRIORITY,                       /* Thread pre-emption threshold: No preemption. */               CYU3P_NO_TIME_SLICE,                   /* No time slice. */               CYU3P_AUTO_START                       /* Start the thread immediately. */               );    }    else    {       ret = CY_U3P_ERROR_MEMORY_ERROR;    }    if (ret != CY_U3P_SUCCESS)    {       goto InitFail;    }    return;InitFail:    /* As the initialization failed, there is nothing much we can do. Just reset the device     * so that we go back to the boot-loader. */    CyU3PDeviceReset (CyFalse);}intmain (void){    CyU3PIoMatrixConfig_t io_cfg;    CyU3PReturnStatus_t status = CY_U3P_SUCCESS;    /* Initialize the device */    //CPU时钟设置、VIC(向量中断控制器)初始化、设置PLLs(锁相环)    //input clk = 19.2 M;SYS_CLK_PLL = 384M,/2....    // Start with the default clock at 384 MHz    /* Initialize the device */    /* For default configuration, pass in NULL as parameter. This will set CPU divider     * to 2 (~200MHz), DMA and MMIO dividers to 2 (~100MHz); and assume 32KHz standby     * clock is supplied     */    status = CyU3PDeviceInit (NULL);    if (status != CY_U3P_SUCCESS)    {        goto handle_fatal_error;    }    /* Initialize the caches. Enable both Instruction and Data caches. */    //一般在有大数据处理的时候才会打开数据cache,如果是简单的处理打开了cache可能会降低整个系统的效率!cache size:8k.    status = CyU3PDeviceCacheControl (CyTrue, CyTrue, CyTrue);    if (status != CY_U3P_SUCCESS)    {        goto handle_fatal_error;    }    /* Configure the IO matrix for the device. */    CyU3PMemSet ((uint8_t *)&io_cfg, 0, sizeof(io_cfg));    io_cfg.isDQ32Bit = CyFalse;    io_cfg.s0Mode = CY_U3P_SPORT_INACTIVE;    io_cfg.s1Mode = CY_U3P_SPORT_INACTIVE;    io_cfg.useUart   = CyTrue;    io_cfg.useI2C    = CyFalse;    io_cfg.useI2S    = CyFalse;    io_cfg.useSpi    = CyFalse;    io_cfg.lppMode   = CY_U3P_IO_MATRIX_LPP_UART_ONLY;    /* No GPIOs are enabled. */    io_cfg.gpioSimpleEn[0]  = 0;    io_cfg.gpioSimpleEn[1]  = 0;    io_cfg.gpioComplexEn[0] = 0;    io_cfg.gpioComplexEn[1] = 0;    status = CyU3PDeviceConfigureIOMatrix (&io_cfg);    if (status != CY_U3P_SUCCESS)    {        goto handle_fatal_error;    }    /* This is a non returnable call for initializing the RTOS kernel */    CyU3PKernelEntry ();//执行到此会跳入: CyFxApplicationDefine().    /* Dummy return to make the compiler happy */    return 0;handle_fatal_error:    /* Cannot recover from this error. */    while (1);}

此程序很简单,只是创建2个线程,线程里sleep。分别在两个线程的 CyU3PThreadSleep (100);添加断点,F6 单步调试会发现程序在两个线程之间循环执行。

原创粉丝点击