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

2、编译

 cd samples/bluetooth/peripheral
cmake -DBOARD=nrf52_pca10040 -H. -Bbuild
cd build
make menuconfig
make
这样编译生成的固件,使用nRF Toolbox可以模拟心跳,APP中的HRM功能
但是我们要电亮led,所以,下面,写代码
首先拿到GPIO设备驱动的指针
 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,我是配置成了上拉输出:
 (void) gpio_pin_configure(gpio_dev,
,
(GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
(void) gpio_pin_configure(gpio_dev,
,
(GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
(void) gpio_pin_configure(gpio_dev,
,
(GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
(void) gpio_pin_configure(gpio_dev,
,
(GPIO_DIR_OUT | GPIO_PUD_PULL_UP));

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

 gpio_pin_write(gpio_dev,  + (gpio_delay_pin % ), gpio_delay % );
总结:gpio操作的接口函数在include/gpio.h中定义,刚学习这个RTOS,设备驱动慢慢搞,点个灯先test下
 
贴上完整代码:
 #include <zephyr/types.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <misc/printk.h>
#include <misc/byteorder.h>
#include <zephyr.h> #include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h> #include <gatt/hrs.h>
#include <gatt/dis.h>
#include <gatt/bas.h> #include <device.h>
#include <board.h>
#include <gpio.h> #define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) struct bt_conn *default_conn; static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18),
}; static const struct bt_data sd[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
}; static void connected(struct bt_conn *conn, u8_t err)
{
if (err) {
printk("Connection failed (err %u)\n", err);
} else {
default_conn = bt_conn_ref(conn);
printk("Connected\n");
}
} static void disconnected(struct bt_conn *conn, u8_t reason)
{
printk("Disconnected (reason %u)\n", reason); if (default_conn) {
bt_conn_unref(default_conn);
default_conn = NULL;
}
} static struct bt_conn_cb conn_callbacks = {
.connected = connected,
.disconnected = disconnected,
}; static void bt_ready(int err)
{
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
} printk("Bluetooth initialized\n"); hrs_init(0x01);
bas_init();
dis_init(CONFIG_SOC, "Manufacturer"); err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad),
sd, ARRAY_SIZE(sd));
if (err) {
printk("Advertising failed to start (err %d)\n", err);
return;
} printk("Advertising successfully started\n");
} static void auth_cancel(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Pairing cancelled: %s\n", addr);
} static struct bt_conn_auth_cb auth_cb_display = {
.cancel = auth_cancel,
}; void main(void)
{
int err;
int gpio_delay = ;
int gpio_delay_pin = ;
struct device *gpio_dev; err = bt_enable(bt_ready);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
} bt_conn_cb_register(&conn_callbacks);
bt_conn_auth_cb_register(&auth_cb_display); /*
* 添加个人测试代码,LED闪烁测试
*/ gpio_dev = device_get_binding(CONFIG_GPIO_NRF5_P0_DEV_NAME); (void) gpio_pin_configure(gpio_dev,
,
(GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
(void) gpio_pin_configure(gpio_dev,
,
(GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
(void) gpio_pin_configure(gpio_dev,
,
(GPIO_DIR_OUT | GPIO_PUD_PULL_UP));
(void) gpio_pin_configure(gpio_dev,
,
(GPIO_DIR_OUT | GPIO_PUD_PULL_UP)); /* Implement notification. At the moment there is no suitable way
* of starting delayed work so we do it here
*/
while () {
k_sleep(MSEC_PER_SEC); gpio_delay++;
gpio_delay = gpio_delay % ;
gpio_pin_write(gpio_dev, + (gpio_delay_pin % ), gpio_delay % );
if(gpio_delay == )
{
gpio_delay_pin++;
} /* Heartrate measurements simulation */
hrs_notify(); /* Battery level simulation */
bas_notify();
}
}
青风nrf52832跑zephyr——点亮LED-LMLPHP
青风nrf52832跑zephyr——点亮LED-LMLPHP
05-11 22:45