zephyr学习笔记---CC3200---Button

zephyr上有button的示例,在【\samples\basic\button】目录下,我做了一些修改,两个Button都使用了,另外也加入了LED,方便调试。功能很简单,就是在按下按钮时向打印一段文字(可使用串口接收),同时切换LED灯的亮灭。

代码如下:

  1. #include <zephyr.h>  
  2. #include <board.h>  
  3. #include <device.h>  
  4. #include <gpio.h>  
  5. #include <misc/util.h>  
  6. #include <misc/printk.h>  
  7.   
  8. /* change to use another GPIO pin interrupt config */  
  9. #define EDGE    (GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW)  
  10. /* change this to enable pull-up/pull-down */  
  11. #define PULL_UP 0  
  12. /* Sleep time */  
  13. #define SLEEP_TIME  500  
  14.   
  15. struct device *devLed0, *devLed2;  
  16. unsigned char led0State = 0, led2State = 0;  
  17. //Button2中断服务函数  
  18. void sw2_pressed(struct device *gpiob, struct gpio_callback *cb,  
  19.             uint32_t pins)  
  20. {  
  21.     printk("You have pressed Button2\n");  
  22.     led2State = led2State ? 0 : 1;  
  23.     gpio_pin_write(devLed2, LED2_GPIO_PIN, led2State);    
  24. }  
  25. //Button3中断服务函数  
  26. void sw3_pressed(struct device *gpiob, struct gpio_callback *cb,  
  27.             uint32_t pins)  
  28. {  
  29.     printk("You have pressed Button3\n");  
  30.     led0State = led0State ? 0 : 1;  
  31.     gpio_pin_write(devLed0, LED0_GPIO_PIN, led0State);  
  32. }  
  33.   
  34. static struct gpio_callback gpio_cb2, gpio_cb3;  
  35.   
  36. void main(void)  
  37. {  
  38.     struct device *gpiob2, *gpiob3;  
  39.     printk("Press the user defined button on the board\n");  
  40.       
  41.     //Button2初始化  
  42.     gpiob2 = device_get_binding(SW2_GPIO_NAME);  
  43.     if (!gpiob2) {  
  44.         printk("error\n");  
  45.         return;  
  46.     }  
  47.     gpio_pin_configure(gpiob2, SW2_GPIO_PIN,  
  48.                GPIO_DIR_IN | GPIO_INT |  PULL_UP | EDGE);  
  49.     gpio_init_callback(&gpio_cb2, sw2_pressed, BIT(SW2_GPIO_PIN));  
  50.     gpio_add_callback(gpiob2, &gpio_cb2);  
  51.     gpio_pin_enable_callback(gpiob2, SW2_GPIO_PIN);  
  52.   
  53.     //Button3初始化  
  54.     gpiob3 = device_get_binding(SW3_GPIO_NAME);  
  55.     if (!gpiob3) {  
  56.         printk("error\n");  
  57.         return;  
  58.     }  
  59.     gpio_pin_configure(gpiob3, SW3_GPIO_PIN,  
  60.                GPIO_DIR_IN | GPIO_INT |  PULL_UP | EDGE);  
  61.     gpio_init_callback(&gpio_cb3, sw3_pressed, BIT(SW3_GPIO_PIN));  
  62.     gpio_add_callback(gpiob3, &gpio_cb3);  
  63.     gpio_pin_enable_callback(gpiob3, SW3_GPIO_PIN);  
  64.   
  65.     //LED 初始化  
  66.     devLed0 = device_get_binding(LED0_GPIO_PORT);  
  67.     devLed2 = device_get_binding(LED2_GPIO_PORT);  
  68.     gpio_pin_configure(devLed0, LED0_GPIO_PIN, GPIO_DIR_OUT);  
  69.     gpio_pin_configure(devLed2, LED2_GPIO_PIN, GPIO_DIR_OUT);  
  70.   
  71.     while (1) {  
  72.         uint32_t val = 0;  
  73.   
  74.         gpio_pin_read(gpiob2, SW2_GPIO_PIN, &val);  
  75.         gpio_pin_read(gpiob3, SW3_GPIO_PIN, &val);  
  76.         k_sleep(SLEEP_TIME);  
  77.     }  
  78. }  

串口调试助手的串口设置请参照下图,运行效果如下图所示:

zephyr学习笔记---CC3200---Button

感觉使用起来还是非常麻烦的,不如TI-RTOS及Contiki方便。当然,需要时可以自己再包装一层。