CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 7888|回复: 0

CubieBLE-1109 GPIO 使用说明

[复制链接]
发表于 2018-9-22 11:23:23 | 显示全部楼层 |阅读模式
一、软硬件版本

硬件:方糖电子CubieBLE-1109开发板
软件:SDK版本CubieBLE-1109_V1.0

二、实验目的
实现按键和LED灯的控制,提供基于zephyr下在方糖电子BLE开发板1109下的GPIO使用方法。

三、实验步骤
1、说明
本实验是基于peripheral_bas例程实现。

2、实验总体步骤
1)配置GPIO复用功能
2)binding GPIO设备
3)配置GPIO参数
4)使用GPIO

1)、配置GPIO复用功能
参考ATB110XDatasheet 1.0.pdf 11.4节,学习GPIO寄存器的描述 ,对应于代码中的配置在board_atb110x_dvb_v10.h板级文件中。
#define BOARD_PIN_CONFIG     \
      {2,3 | GPIO_CTL_SMIT | GPIO_CTL_PADDRV_LEVEL(3)},\
      {3,3 | GPIO_CTL_SMIT | GPIO_CTL_PADDRV_LEVEL(3)},\
      {8,13 },\
      {9,13 }, \
      {10,13 },\
      {11,13 },\
      {1,2},
其中,上面宏定义中元素的第一项是GPIO引脚序号,第二项即datasheetGPIO寄存器的配置参数,例如: 如果我们要使用GPIO7,使用普通IO功能,那么配置是{7,0},对应的datasheet


2)binding GPIO设备
      structdevice *gpio;
      gpio= device_get_binding("gpio");
      if(!gpio) {
           printk("cannotfound device gpio\n");
           return;
      }

API说明
      structdevice *device_get_binding(const char *name)
           Retrievethe device structure for a driver by name.
           Deviceobjects are created via the DEVICE_INIT() macro and placed in memory by the linker.If a driver needs to bind to another driver it can use this function to retrievethe device structure of the lower level driver by the name the driver exposes tothe system.
           Return
                 pointerto device structure; NULL if not found or cannot be used.
           Parameters
                 name:device name to search for.
配置GPIO为输出模式
      gpio_pin_configure(gpio_dev,8, GPIO_DIR_OUT | GPIO_POL_INV);
配置GPIO为输入模式
      ret= gpio_pin_configure(gpio_dev, KEY_1, (GPIO_DIR_IN | GPIO_INT |
                               GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW| GPIO_PUD_PULL_UP |
                               GPIO_INT_DEBOUNCE));
      if(ret)
      {
           printk("Errorconfiguring %d!\n", KEY_1);
      }
API说明
      staticint gpio_pin_configure(struct device *port, u32_t pin, int flags)
           Configurea single pin.
           Return
                 0if successful, negative errno code on failure.
           Parameters
                 portointer to device structure for the driver instance.
                 pinin number to configure.
                 flags:Flags for pin configuration. IN/OUT, interrupt …
flags
      **GPIO pin to be input. */
      #defineGPIO_DIR_IN          (0 << 0)
      
      /**GPIO pin to be output. */
      #defineGPIO_DIR_OUT       (1 << 0)
      
      /**@cond INTERNAL_HIDDEN */
      #defineGPIO_DIR_MASK           0x1
      /**@endcond */
      
      /**GPIO pin to trigger interrupt. */
      #defineGPIO_INT         (1 << 1)
      
      /**GPIO pin trigger on level low or falling edge. */
      #defineGPIO_INT_ACTIVE_LOW      (0 << 2)
      
      /**GPIO pin trigger on level high or rising edge. */
      #defineGPIO_INT_ACTIVE_HIGH     (1 << 2)
      
      /**GPIO pin trigger to be synchronized to clock pulses. */
      #defineGPIO_INT_CLOCK_SYNC     (1 << 3)
      
      /**Enable GPIO pin debounce. */
      #defineGPIO_INT_DEBOUNCE       (1 << 4)
      
      /**Do Level trigger. */
      #defineGPIO_INT_LEVEL          (0 << 5)
      
      /**Do Edge trigger. */
      #defineGPIO_INT_EDGE           (1 << 5)
      
      /**Interrupt triggers on both rising and falling edge. */
      #defineGPIO_INT_DOUBLE_EDGE    (1 << 6)
      
      /*
       * GPIO_POL_* define the polarity of the GPIO(1 bit).
       */
      
      /**@cond INTERNAL_HIDDEN */
      #defineGPIO_POL_POS       7
      /**@endcond */
      
      /**GPIO pin polarity is normal. */
      #defineGPIO_POL_NORMAL      (0 << GPIO_POL_POS)
      
      /**GPIO pin polarity is inverted. */
      #defineGPIO_POL_INV       (1 <<GPIO_POL_POS)
      
      /**@cond INTERNAL_HIDDEN */
      #defineGPIO_POL_MASK          (1 <<GPIO_POL_POS)
      /**@endcond */
      
      /*
       * GPIO_PUD_* are related to pull-up/pull-down.
       */
      
      /**@cond INTERNAL_HIDDEN */
      #defineGPIO_PUD_POS       8
      /**@endcond */
      
      /**GPIO pin to have no pull-up or pull-down. */
      #defineGPIO_PUD_NORMAL      (0 <<GPIO_PUD_POS)
      
      /**Enable GPIO pin pull-up. */
      #defineGPIO_PUD_PULL_UP      (1 <<GPIO_PUD_POS)
      
      /**Enable GPIO pin pull-down. */
      #defineGPIO_PUD_PULL_DOWN (2 <<GPIO_PUD_POS)
      
      /**@cond INTERNAL_HIDDEN */
      #defineGPIO_PUD_MASK          (3 <<GPIO_PUD_POS)
配置GPIO为中断输入时,需要添加中断回调函数
void gpio_callback(struct device *port,
                   struct gpio_callback *cb, u32_tpins)
{
    val= ~val;
      printk("%d%d triggered\n", KEY_1, val);
}
void led_key_init(void)
{
      intret;
      gpio_dev= device_get_binding("gpio");
      if(!gpio_dev)
      {
           {
                 printk("cannotfound dev gpio \n");
                 printk("error\n");
            }
           return;
      }
   
      /*KEY */
      ret= gpio_pin_configure(gpio_dev, KEY_1, (GPIO_DIR_IN | GPIO_INT |
                               GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW| GPIO_PUD_PULL_UP |
                               GPIO_INT_DEBOUNCE));
      if(ret)
      {
           printk("Errorconfiguring %d!\n", KEY_1);
      }
      gpio_init_callback(&gpio_cb,gpio_callback, BIT(KEY_1));
      ret= gpio_add_callback(gpio_dev, &gpio_cb);
      if(ret)
      {
           printk("Cannotsetup callback!\n");
      }
      ret= gpio_pin_enable_callback(gpio_dev, KEY_1);
      if(ret)
      {
           printk("Errorenabling callback!\n");
      }
}
API说明
      staticvoid gpio_init_callback(struct gpio_callback *callback, gpio_callback_handler_thandler, u32_t pin_mask)
           Helperto initialize a struct gpio_callback properly.
           Parameters
                 callback:A valid Application’s callback structure pointer.
                 handler:A valid handler function pointer.
                 pin_mask:A bit mask of relevant pins for the handler
      staticint gpio_add_callback(struct device *port, struct gpio_callback *callback)
           Addan application callback.
      
           Note:enables to add as many callback as needed on the same port.
      
           Return
                 0if successful, negative errno code on failure.
           Parameters
                 portointer to the device structure for the driver instance.
      callback:A valid Application’s callback structure pointer.
      staticint gpio_pin_enable_callback(struct device *port, u32_t pin)
           Enablecallback(s) for a single pin.
           Noteepending on the driver implementation, this function will enable the pin to triggeran interruption. So as a semantic detail, if no callback is registered, of coursenone will be called.
           Return
                 0if successful, negative errno code on failure.
           Parameters
                 portointer to the device structure for the driver instance.
                 pinin number where the callback function is enabled.
GPIO读写操作
      gpio_pin_write(gpio_dev,LED_1,0);
      gpio_pin_read(gpio_dev,LED_1,value);
API说明
      staticint gpio_pin_write(struct device *port, u32_t pin, u32_t value)
           Writethe data value to a single pin.
      
           Return
                 0if successful, negative errno code on failure.
           Parameters
                 portointer to the device structure for the driver instance.
                 pinin number where the data is written.
                 value:Value set on the pin.
      staticint gpio_pin_read(struct device *port, u32_t pin, u32_t *value)
           Readthe data value of a single pin.
      
           Readthe input state of a pin, returning the value 0 or 1.
      
           Return
                 0if successful, negative errno code on failure.
           Parameters
                 portointer to the device structure for the driver instance.
                 pinin number where data is read.
                 value:Integer pointer to receive the data values from the pin.
示例源码
源码实现:基于peripheral_bas例程实现GPIO0178四个引脚的中断输入和输出功能
/* main.c - Application main entry point */
/*
*Copyright (c) 2018 Actions Semiconductor, Inc
*
*SPDX-License-Identifier: Apache-2.0
*/
#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 <bas.h>
#include <gpio.h>
/* trigger low edge */
#define EDGE       (GPIO_INT_EDGE |GPIO_INT_ACTIVE_LOW)  
const u32_t button_pins[] = {0,1, 7, 8};
#define BUTTON_TOTAL sizeof(button_pins)
struct gpio_callback button_cb[BUTTON_TOTAL];
void button_pressed(struct device *gpiob, structgpio_callback *cb,
           u32_tpins)
{
      printk("pin=%dButton pressed at %d\n", find_lsb_set(pins) - 1, k_cycle_get_32());
}
void button_init(void)
{
      inti;     
      structdevice *gpio;
      gpio= device_get_binding("gpio");
      if(!gpio) {
           printk("cannotfound device gpio\n");
           return;
      }
      for(i = 0; i < BUTTON_TOTAL; i++) {
           gpio_pin_configure(gpio,button_pins,
                      GPIO_DIR_IN| GPIO_INT | GPIO_PUD_PULL_UP | EDGE);
           gpio_init_callback(&button_cb,button_pressed, BIT(button_pins));
           gpio_add_callback(gpio,&button_cb);
           gpio_pin_enable_callback(gpio,button_pins);
      
//       gpio_pin_configure(gpio, button_pins,
//                    GPIO_DIR_OUT| GPIO_POL_INV);
//       gpio_pin_write(gpio, button_pins, 1);
      }
}
#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,
           0x0f,0x18, /* bas */
      ),
};
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("Connectionfailed (err %x)\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 %x)\n", reason);
      if(default_conn) {
           bt_conn_unref(default_conn);
           default_conn= NULL;
      }   
}
static struct bt_conn_cb conn_callbacks = {
      .connected= connected,
      .disconnected= disconnected,
};
u8_t bt_ready_flag;
__init_once_text static void bt_ready(int err)
{
      if(err) {
           printk("Bluetoothinit failed (err %d)\n", err);
           return;
      }
      bt_ready_flag= 1;
      
      printk("Bluetoothinitialized\n");
      
      /*battery service */
      bas_init();
      
      err= bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad),
                       sd, ARRAY_SIZE(sd));
      if(err) {
           printk("Advertisingfailed to start (err %d)\n", err);
           return;
      }
      printk("Advertisingsuccessfully started\n");
}
void app_main(void)
{
      interr;
   
//   led_key_init();
    button_init();
      printk("button_init\n");
      err= bt_enable(bt_ready);
      if(err) {
           printk("Bluetoothinit failed (err %d)\n", err);
           return;
      }
      bt_conn_cb_register(&conn_callbacks);
      
      /*Implement notification. At the moment there is no suitable way
       * of starting delayed work so we do it here
       */
      while(1) {
           k_sleep(MSEC_PER_SEC);           
           /*Battery level simulation */
           bas_notify();
      }   
      
}
/* board_atb110x_dvb_v10.h */
/*
*Copyright (c) 2018 Actions (Zhuhai) Technology Co., Ltd
*
*SPDX-License-Identifier: Apache-2.0
*/
#define CONFIG_UART_0 1
//#define BOARD_PIN_CONFIG   \
//   {2,3 | GPIO_CTL_SMIT | GPIO_CTL_PADDRV_LEVEL(3)},\
//   {3,3 | GPIO_CTL_SMIT | GPIO_CTL_PADDRV_LEVEL(3)},\
//   {8,13 },\
//   {9,13 }, \
//   {10,13 },\
//   {11,13 },\
//   {1,2},
#define BOARD_PIN_CONFIG     \
      {2,3 | GPIO_CTL_SMIT | GPIO_CTL_PADDRV_LEVEL(3)},\
      {3,3 | GPIO_CTL_SMIT | GPIO_CTL_PADDRV_LEVEL(3)}
/* all managed led */
#define LED_CTL_GPIOS                 {1,}
/* app led pin */
#define LED_LPOWER_PIN                  1
#define LED_PAIR_PIN                  1
#define LED_BTN_PIN                  1
/* pwm driven led */
#define LED_CTL_PWMS            {1,}
#define LED_LPOWER_PWM   1
#define LED_PAIR_PWM   1
#define LED_BTN_PWM   1
#define LED_PWM   1
#define PWM_CTRL_POL_ACTIVE_HIGH 1
/* key reg val mapping to key val */
#define MXKEYPAD_MASK 0xc6
#define MXKEYPAD_MAPS \
      {0x00020400,0x00000000, 0x00000000,  KEY_1},\
      {0x00004000,0x00020000, 0x00000000,  KEY_2},\
      {0x00800000,0x04000000, 0x00000000,  KEY_5},\
      {0x00000000,0x40800000, 0x00000000,  KEY_6},\
      {0x00000000,0x00000000, 0x00000200,  KEY_7},\
      {0x00000000,0x00000000, 0x00000080,  KEY_8},\
      {0x00c00000,0x44840000, 0x00000000,  KEY_9},
/* key val mapping to standard key */
#define KEY_MAPS \
      {KEY_1,                     REMOTE_KEY_POWER,              0xFF},\
      {KEY_2,                     REMOTE_KEY_MENU,                                      0xFF},\
      {KEY_5,                      REMOTE_KEY_VOL_INC,                             0xFF},\
      {KEY_6,                      REMOTE_KEY_VOL_DEC,                                  0xFF},\
      {KEY_7,                      REMOTE_KEY_BACK,                                       0xFF},\
      {KEY_8,                      REMOTE_KEY_VOICE_COMMAND,                                      0xFF},\
      {KEY_9,     REMOTE_COMB_KEY_OK_BACK,             0xFF},
#define CUSTOMER_CODE 0x10EF

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-3-29 17:01 , Processed in 0.034381 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部