青风nrf52832跑zephyr——点亮LED

zephyr版本:1.10
硬件:采用青风nrf52832开发板
开发环境:虚拟机Ubuntu16.04编译+Windows7 64bit烧录
 
使用的是 zephyr-zephyr-v1.10.0/samples/bluetooth/peripheral_hr 例程
由于1.10版本采用的是cmake,所以编译指令和之前Kbuild有所区别:
1、export两个环境变量:(参考doc文件下的入门文档)
1 export ZEPHYR_GCC_VARIANT=zephyr
2 export ZEPHYR_SDK_INSTALL_DIR=/opt/zephyr-sdk

2、编译

1 cd samples/bluetooth/peripheral
2 cmake -DBOARD=nrf52_pca10040 -H. -Bbuild
3 cd build
4 make menuconfig          
5 make
这样编译生成的固件,使用nRF Toolbox可以模拟心跳,APP中的HRM功能
但是我们要电亮led,所以,下面,写代码
首先拿到GPIO设备驱动的指针
1 gpio_dev = device_get_binding(CONFIG_GPIO_NRF5_P0_DEV_NAME);
其中CONFIG_GPIO_NRF5_P0_DEV_NAME是在make menuconfig中配置
Device Drivers  --->
    [*] GPIO Drivers  --->
         -*-  Nordic Semiconductor nRF5X-based GPIO driver  ---> 
                 [*]  nRF5x GPIO Port P0 options                                          
                (myGPIO) GPIO Port P0 Device Name                        
               (1)    GPIOTE P0 interrupt priority 
 
第二步,配置下gpio端口的四个pin,p0.17,p0.18,p0.19,p0.20,我是配置成了上拉输出:
 1 (void) gpio_pin_configure(gpio_dev,
 2                   17,
 3                   (GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
 4 (void) gpio_pin_configure(gpio_dev,
 5                   18,
 6                   (GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
 7 (void) gpio_pin_configure(gpio_dev,
 8                   19,
 9                   (GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
10 (void) gpio_pin_configure(gpio_dev,
11                   20,
12                   (GPIO_DIR_OUT | GPIO_PUD_PULL_UP));

下面就可以操作gpio了,我们按pin来操作

1 gpio_pin_write(gpio_dev, 17 + (gpio_delay_pin % 4), gpio_delay % 2);
总结:gpio操作的接口函数在include/gpio.h中定义,刚学习这个RTOS,设备驱动慢慢搞,点个灯先test下
 
贴上完整代码:
  1 #include <zephyr/types.h>
  2 #include <stddef.h>
  3 #include <string.h>
  4 #include <errno.h>
  5 #include <misc/printk.h>
  6 #include <misc/byteorder.h>
  7 #include <zephyr.h>
  8 
  9 #include <bluetooth/bluetooth.h>
 10 #include <bluetooth/hci.h>
 11 #include <bluetooth/conn.h>
 12 #include <bluetooth/uuid.h>
 13 #include <bluetooth/gatt.h>
 14 
 15 #include <gatt/hrs.h>
 16 #include <gatt/dis.h>
 17 #include <gatt/bas.h>
 18 
 19 #include <device.h>
 20 #include <board.h>
 21 #include <gpio.h>
 22 
 23 
 24 
 25 
 26 #define DEVICE_NAME        CONFIG_BT_DEVICE_NAME
 27 #define DEVICE_NAME_LEN        (sizeof(DEVICE_NAME) - 1)
 28 
 29 struct bt_conn *default_conn;
 30 
 31 static const struct bt_data ad[] = {
 32     BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
 33     BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18),
 34 };
 35 
 36 static const struct bt_data sd[] = {
 37     BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
 38 };
 39 
 40 static void connected(struct bt_conn *conn, u8_t err)
 41 {
 42     if (err) {
 43         printk("Connection failed (err %u)
", err);
 44     } else {
 45         default_conn = bt_conn_ref(conn);
 46         printk("Connected
");
 47     }
 48 }
 49 
 50 static void disconnected(struct bt_conn *conn, u8_t reason)
 51 {
 52     printk("Disconnected (reason %u)
", reason);
 53 
 54     if (default_conn) {
 55         bt_conn_unref(default_conn);
 56         default_conn = NULL;
 57     }
 58 }
 59 
 60 static struct bt_conn_cb conn_callbacks = {
 61     .connected = connected,
 62     .disconnected = disconnected,
 63 };
 64 
 65 static void bt_ready(int err)
 66 {
 67     if (err) {
 68         printk("Bluetooth init failed (err %d)
", err);
 69         return;
 70     }
 71 
 72     printk("Bluetooth initialized
");
 73 
 74     hrs_init(0x01);
 75     bas_init();
 76     dis_init(CONFIG_SOC, "Manufacturer");
 77 
 78     err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad),
 79                   sd, ARRAY_SIZE(sd));
 80     if (err) {
 81         printk("Advertising failed to start (err %d)
", err);
 82         return;
 83     }
 84 
 85     printk("Advertising successfully started
");
 86 }
 87 
 88 static void auth_cancel(struct bt_conn *conn)
 89 {
 90     char addr[BT_ADDR_LE_STR_LEN];
 91 
 92     bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
 93 
 94     printk("Pairing cancelled: %s
", addr);
 95 }
 96 
 97 static struct bt_conn_auth_cb auth_cb_display = {
 98     .cancel = auth_cancel,
 99 };
100 
101 void main(void)
102 {
103     int err;
104     int gpio_delay = 0;
105     int gpio_delay_pin = 0;
106     struct device *gpio_dev;
107 
108     err = bt_enable(bt_ready);
109     if (err) {
110         printk("Bluetooth init failed (err %d)
", err);
111         return;
112     }
113 
114     bt_conn_cb_register(&conn_callbacks);
115     bt_conn_auth_cb_register(&auth_cb_display);
116 
117     /*
118      *    添加个人测试代码,LED闪烁测试
119      */
120 
121     gpio_dev = device_get_binding(CONFIG_GPIO_NRF5_P0_DEV_NAME);
122 
123     (void) gpio_pin_configure(gpio_dev,
124                   17,
125                   (GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
126     (void) gpio_pin_configure(gpio_dev,
127                   18,
128                   (GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
129     (void) gpio_pin_configure(gpio_dev,
130                   19,
131                   (GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
132     (void) gpio_pin_configure(gpio_dev,
133                   20,
134                   (GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
135     
136 
137     /* Implement notification. At the moment there is no suitable way
138      * of starting delayed work so we do it here
139      */
140     while (1) {
141         k_sleep(MSEC_PER_SEC);
142 
143         gpio_delay++;
144         gpio_delay = gpio_delay % 2;
145         gpio_pin_write(gpio_dev, 17 + (gpio_delay_pin % 4), gpio_delay % 2);
146         if(gpio_delay == 0)
147         {
148             gpio_delay_pin++;
149         }
150 
151         /* Heartrate measurements simulation */
152         hrs_notify();
153 
154         /* Battery level simulation */
155         bas_notify();
156     }
157 }
原文地址:https://www.cnblogs.com/skawu/p/8110540.html