【CYW20819】四、运行第一个例子

1. 前言

本文介绍如何快速将DEMO板跑起来,为了现象明显,选择和LED灯有关的。

本文参考 Getting Started with CYW20819

2. 导入工程

2.1 New Application

在这里插入图片描述

2.2 选择具体的DEMO板

在这里插入图片描述

2.3 选择应用

在这里插入图片描述

2.4 创建成功

在这里插入图片描述
在这里插入图片描述

3. 编译

在这里插入图片描述

4. 下载

将USB连接线将DEMO板和电脑了连接在一起,在电脑端可以识别到两个COM口,然后点击PWM_GPIO_Program。
在这里插入图片描述
在这里插入图片描述

5. 现象

下载成功后,可以看到LED1常亮,按下第3个按键,可以看到LED2是个红色的呼吸灯。

6. 代码框架解读

6.1 main函数

赛普拉斯的main函数是 application_start。

void application_start(void)
{
    wiced_result_t result = WICED_BT_SUCCESS;

    wiced_set_debug_uart(WICED_ROUTE_DEBUG_TO_PUART);
    WICED_BT_TRACE("*************Starting PWM GPIO Application**********\n\r");
    WICED_BT_TRACE("Press Button SW3 on the kit to toggle LED1 and "
                    "control PWM on LED2\n\r");
    /* Register BT stack callback*/
    result = wiced_bt_stack_init(bt_cback, NULL, NULL);
	
    if(WICED_BT_SUCCESS != result){
        WICED_BT_TRACE("Stack Initialization Failed!!\n\r");
    }
}

6.2 蓝牙协议栈回调函数

/* Register BT stack callback*/
result = wiced_bt_stack_init(bt_cback, NULL, NULL);
wiced_result_t bt_cback(wiced_bt_management_evt_t event,
                        wiced_bt_management_evt_data_t *p_event_data)
{
    wiced_result_t result = WICED_BT_SUCCESS;

    switch(event)
    {
    /* BlueTooth stack enabled */
    case BTM_ENABLED_EVT:
        /* Initaializing GPIO's */
        wiced_hal_gpio_configure_pin(CYBSP_D6, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_HIGH);
        wiced_hal_gpio_configure_pin(CYBSP_D7, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);

        /* Initializing PWM */
        wiced_hal_gpio_select_function(CYBSP_D6,WICED_PWM1);
        wiced_hal_gpio_select_function(CYBSP_D7,WICED_PWM1);
        wiced_hal_gpio_select_function(LED2,WICED_PWM1);
		
        /* Turn on LED1 */
        wiced_hal_gpio_set_pin_output(LED1, 0);
		
        /* Registering interrupt*/
        wiced_hal_gpio_register_pin_for_interrupt(WICED_GPIO_PIN_BUTTON_1,
                                                  button_cb,
                                                  NULL);
        wiced_hal_gpio_configure_pin(WICED_GPIO_PIN_BUTTON_1,
                                    (GPIO_INPUT_ENABLE|GPIO_PULL_UP|GPIO_EN_INT_FALLING_EDGE),
                                     GPIO_PIN_OUTPUT_HIGH);

        /* Start a thread to control LED blinking
         * and Get memory for the thread handle */
        led_thread = wiced_rtos_create_thread();
        if (NULL!= led_thread){
            wiced_rtos_init_thread(
                    led_thread,                     // Thread handle
                    PRIORITY_MEDIUM,                // Priority
                    "PWM_thread",                   // Name
                    PWM_control,                    // Function
                    THREAD_STACK_MIN_SIZE,          // Stack
                    NULL);                          // Function argument
        }
        else{
            WICED_BT_TRACE("failed to create thread!!\n\r");
            result = WICED_ERROR;
        }
        break;

    default:
        break;
    }
    return result;
}

6.3 按键中断

/* Registering interrupt*/
wiced_hal_gpio_register_pin_for_interrupt(WICED_GPIO_PIN_BUTTON_1,
                                          button_cb,
                                          NULL);
wiced_hal_gpio_configure_pin(WICED_GPIO_PIN_BUTTON_1,
                            (GPIO_INPUT_ENABLE|GPIO_PULL_UP|GPIO_EN_INT_FALLING_EDGE),
                            GPIO_PIN_OUTPUT_HIGH);
void button_cb(void * data , uint8_t port_pin)
{
    uint32_t led;
    /*Toggle LED*/
    led = wiced_hal_gpio_get_pin_output(LED1);
    wiced_hal_gpio_set_pin_output(LED1, !led);

    pwm_flag = !pwm_flag;
    wiced_hal_gpio_clear_pin_interrupt_status(port_pin);
}

6.4 LED初始化

/* Initaializing GPIO's */
wiced_hal_gpio_configure_pin(CYBSP_D6, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_HIGH);
wiced_hal_gpio_configure_pin(CYBSP_D7, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);

/* Initializing PWM */
wiced_hal_gpio_select_function(CYBSP_D6,WICED_PWM1);
wiced_hal_gpio_select_function(CYBSP_D7,WICED_PWM1);
wiced_hal_gpio_select_function(LED2,WICED_PWM1);

/* Turn on LED1 */
wiced_hal_gpio_set_pin_output(LED1, 0);

6.5 LED PWM 处理线程

/* Start a thread to control LED blinking
 * and Get memory for the thread handle */
led_thread = wiced_rtos_create_thread();
if (NULL!= led_thread){
    wiced_rtos_init_thread(
            led_thread,                     // Thread handle
            PRIORITY_MEDIUM,                // Priority
            "PWM_thread",                   // Name
            PWM_control,                    // Function
            THREAD_STACK_MIN_SIZE,          // Stack
            NULL);                          // Function argument
}
else{
    WICED_BT_TRACE("failed to create thread!!\n\r");
    result = WICED_ERROR;
}
void PWM_control(uint32_t arg)
{
    uint32_t led;
    uint32_t duty_cycle = 0;
    uint8_t  incr = TRUE;
	uint32_t current_duty_cycle = duty_cycle;
	
	PwmClockType clk = LHL_CLK;
	wiced_pwm_config_t config;
	
    /*Uncomment below lines for PMU Clock */
    // clk=PMU_CLK;
	// wiced_hal_aclk_enable(PWM_INP_CLK_IN_HZ, WICED_ACLK1, WICED_ACLK_FREQ_24_MHZ);

    wiced_hal_pwm_get_params(PWM_INP_CLK_IN_HZ, duty_cycle, PWM_FREQ_IN_HZ, &config);
    wiced_hal_pwm_start(PWM1,
                        clk,
                        config.toggle_count,
                        config.init_count,
                        INVERT_LED);

    while(1)
    {
        if(TRUE == pwm_flag)
        {
            if(TRUE == incr){
                duty_cycle += DUTY_STEP_SIZE;
                if (duty_cycle == 100){
                    incr = FALSE;
				}
			}
			
            else{
                duty_cycle -= DUTY_STEP_SIZE;
                if(duty_cycle == 0){
                    incr = TRUE;
				}
            }

            wiced_hal_pwm_get_params(PWM_INP_CLK_IN_HZ,
                                     duty_cycle,
                                     PWM_FREQ_IN_HZ,
                                     &config);

            wiced_hal_pwm_change_values(PWM1,
                                        config.toggle_count,
                                        config.init_count);

        }
		
        else if (current_duty_cycle != duty_cycle){
			WICED_BT_TRACE("\nCurrent PWM duty cycle: %d%%\n\r",current_duty_cycle=duty_cycle);
		}
        
        /* Send the thread to sleep for a period of time */
        wiced_rtos_delay_milliseconds(THREAD_DELAY_IN_MS,
                                      ALLOW_THREAD_TO_SLEEP );
    }

}

6.6 I/O操作

6.6.1 配置

/* Initaializing GPIO's */
wiced_hal_gpio_configure_pin(CYBSP_D6, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_HIGH);
wiced_hal_gpio_configure_pin(CYBSP_D7, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);

6.6.2 写

/* Turn on LED1 */
wiced_hal_gpio_set_pin_output(LED1, 0);

6.6.3 读

uint32_t led;
/*Toggle LED*/
led = wiced_hal_gpio_get_pin_output(LED1);

6.6.3 PWM输出

/* Initializing PWM */
wiced_hal_gpio_select_function(CYBSP_D6,WICED_PWM1);
wiced_hal_gpio_select_function(CYBSP_D7,WICED_PWM1);
wiced_hal_gpio_select_function(LED2,WICED_PWM1);
wiced_hal_pwm_get_params(PWM_INP_CLK_IN_HZ, duty_cycle, PWM_FREQ_IN_HZ, &config);
wiced_hal_pwm_start(PWM1,
                    clk,
                    config.toggle_count,
                    config.init_count,
                    INVERT_LED);

wiced_hal_pwm_change_values(PWM1,
                            config.toggle_count,
                            config.init_count);                    

6.7 延时

/* Thread will delay for 5ms */
#define THREAD_DELAY_IN_MS          (5)

/* Send the thread to sleep for a period of time */
wiced_rtos_delay_milliseconds(THREAD_DELAY_IN_MS,
                              ALLOW_THREAD_TO_SLEEP );

7. 总结

本文通过导入运行在线例子PWM_GPIO来演示CYW20819的编译和下载以及初步浏览了代码框架。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

强人电子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值