HarmonyOS小熊派 | HarmonyOS基础外设开发--I2C控制NFC芯片

举报
Yuchuan 发表于 2021/07/07 23:44:46 2021/07/07
【摘要】 基础外设开发I2C控制NFC芯片

BearPi-HM_Nano开发板基础外设开发——I2C控制NFC芯片

本示例将演示如何在BearPi-HM_Nano开发板上使用I2C协议向NFC芯片写入数据

BearPi-HM_Nano

I2C API分析

本示例主要使用了以下API完成I2C采样的功能

1、IoTI2cInit()

/**
 * @brief Initializes an I2C device with a specified baud rate.
 *
 *
 *
 * @param id Indicates the I2C device ID.
 * @param baudrate Indicates the I2C baud rate.
 * @return Returns {@link IOT_SUCCESS} if the I2C device is initialized;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTI2cInit(unsigned int id, unsigned int baudrate);

描述:

用指定的频率初始化I2C设备

参数:

名字 描述
id I2C设备ID.
baudrate I2C频率

2、IoTI2cSetBaudrate()

/**
 * @brief Sets the baud rate for an I2C device.
 *
 * @param id Indicates the I2C device ID.
 * @param baudrate Indicates the baud rate to set.
 * @return Returns {@link IOT_SUCCESS} if the baud rate is set;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTI2cSetBaudrate(unsigned int id, unsigned int baudrate);

描述:

为I2C设备设置频率

参数:

名字 描述
id I2C设备ID.
baudrate I2C频率

3、IoTI2cWrite()

/**
 * @brief Writes data to an I2C device.
 *
 *
 *
 * @param id Indicates the I2C device ID.
 * @param deviceAddr Indicates the I2C device address.
 * @param data Indicates the pointer to the data to write.
 * @param dataLen Indicates the length of the data to write.
 * @return Returns {@link IOT_SUCCESS} if the data is written to the I2C device successfully;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, const unsigned char *data, unsigned int dataLen);

描述:

将数据写入I2C设备

参数:

名字 描述
id I2C设备ID.
deviceAddr I2C设备地址
i2cData 表示写入的数据

4、IoTI2cRead()

/**
 * @brief Reads data from an I2C device.
 *
 * The data read will be saved to the address specified by <b>i2cData</b>.
 *
 * @param id Indicates the I2C device ID.
 * @param deviceAddr Indicates the I2C device address.
 * @param data Indicates the pointer to the data to read.
 * @param dataLen Indicates the length of the data to read.
 * @return Returns {@link IOT_SUCCESS} if the data is read from the I2C device successfully;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTI2cRead(unsigned int id, unsigned short deviceAddr, unsigned char *data, unsigned int dataLen);

描述:

从I2C设备读取数据。读取的数据将保存到i2cData指定的地址

参数:

名字 描述
id I2C设备ID.
deviceAddr I2C设备地址
i2cData 表示要读取的数据指向的指针

硬件设计

如下图所示,NFC芯片使用的是I2C协议,I2C_SCL与GPIO_0相连接,I2C_SDA与GPIO_1相连接,所以需要编写软件使用GPIO_0和GPIO_1产生I2C信号去控制NFC芯片

软件设计

主要代码分析

这部分代码为I2C初始化的代码,首先用 IoTGpioSetFunc() 函数将GPIO_0复用为I2C1_SDA,GPIO_1复用为I2C1_SCL。然后调用IoTI2cInit()函数初始化I2C1端口,最后使用 IoTI2cSetBaudrate() 函数设置I2C1的频率为400kbps.

yuchuan_nfc_i2c.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_i2c.h"
#include "nfc.h"

#define NFC_THREAD_I2C_STACK_SIZE 1024 * 8
#define NFC_THREAD_I2C_PRIORITY 25
#define IOT_I2C1_SDA_GPIO_0 0
#define IOT_I2C1_SCL_GPIO_1 1
#define YUCHUAN_TEXT "Welcome to Yuchuan HarmonyOS"
#define HUAYING_TEXT "Welcome to Huaying HarmonyOS"
#define WEB "harmonyos.com"
#define IOT_I2C_IDX_1 1

void yuchuanNFCI2CTask(void *argument)
{
    (void)argument;
    uint8_t ret;
    //初始化GPIO_0
    IoTGpioInit(IOT_I2C1_SDA_GPIO_0);
    //设置GPIO_0复用功能为I2C1_SDA
    IoTGpioSetFunc(IOT_I2C1_SDA_GPIO_0,IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);

    //初始化GPIO_1
    IoTGpioInit(IOT_I2C1_SCL_GPIO_1);
    //设置GPIO_1复用功能为I2C1_SCL
    IoTGpioSetFunc(IOT_I2C1_SCL_GPIO_1,IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);

    //设置baudrate:400kbps
    IoTI2cInit(IOT_I2C_IDX_1,400000);

    printf("I2C NFC Yuchuan Start!!!\n");
    ret = storeText(NDEFFirstPos,(uint8_t *)YUCHUAN_TEXT);
    if (ret != 1)
    {
        /* code */
        printf("NFC Write TEXT Data Falied %d!!!\n",ret);
    }
    ret = storeText(NDEFMiddlePos,(uint8_t *)HUAYING_TEXT);
    if (ret != 1)
    {
        /* code */
        printf("NFC Write Text Data Falied %d!!!\n",ret);
    }
    
    ret = storeUrihttp(NDEFLastPos,(uint8_t *)WEB);
    if (ret != 1)
    {
        /* code */
        printf("NFC Write WEB Data Falied %d\n",ret);
    }
    while (1)
    {
        /* code */
        printf("=======================================\r\n");
        printf("***********I2C_NFC_Yuchuan_Example**********\r\n");
        printf("=======================================\r\n");
        printf("Please use the mobile phone with NFC function close to the development board!\r\n");
        usleep(1000000);
    }
    
}

static void yuchuanNFCI2CExample(void)
{
    osThreadAttr_t threadAttr;

    threadAttr.attr_bits = 0U;
    threadAttr.cb_mem = NULL;
    threadAttr.cb_size = 0U;
    threadAttr.stack_mem = NULL;
    threadAttr.stack_size = NFC_THREAD_I2C_STACK_SIZE;
    threadAttr.priority = NFC_THREAD_I2C_PRIORITY;

    threadAttr.name = "YuchuanThreadNFCI2C";
    if (osThreadNew((osThreadFunc_t)yuchuanNFCI2CTask,NULL,&threadAttr) == NULL)
    {
        /* code */
        printf("Falied To Create YuchuanThread NFC I2C Task\n");
    }
    

}

APP_FEATURE_INIT(yuchuanNFCI2CExample);

编译调试

//applications/sample/BearPi/BearPi-HM_Nano/sample/B5_YuchuanNFCI2C

static_library("yuchuanNFCI2C"){
    sources = [
        "nfc/NT3H.c",
        "nfc/nfc.c",
        "nfc/ndef/rtd/nfcForum.c",
        "nfc/ndef/rtd/rtdText.c",
        "nfc/ndef/rtd/rtdUri.c",
        "nfc/ndef/ndef.c",
        "yuchuan_nfc_i2c.c",
    ]

    cflags = [
        "-Wno-unused-variable" 
    ]

    cflags += [
        "-Wno-unused-but-set-variable" 
    ]

    cflags += [ 
        "-Wno-unused-parameter" 
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "nfc",
        "nfc/ndef",
        "nfc/ndef/rtd",
    ]
}

修改 BUILD.gn 文件

修改 applications\BearPi\BearPi-HM_Nano\sample 路径下 BUILD.gn 文件,指定 yuchuanNFCI2C 参与编译。

# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/lite/config/component/lite_component.gni")

lite_component("sample") {
    features = [
        #"A1_kernal_thread:thread_example",
        #"A2_kernel_timer:timer_example",
        #"A3_kernel_event:event_example",
        #"A4_kernel_mutex:mutex_example",
        #"A5_kernel_semaphore:semaphore_example",
        #"A6_kernel_message:message_example",

        #"B1_basic_led_blink:led_example",
        #"B2_basic_button:button_example",
        #"B3_basic_pwm_led:pwm_example",
        #"B4_basic_adc:adc_example", 
        #"B5_basic_i2c_nfc:i2c_example",
        #"B6_basic_uart:uart_example",
        
        #"C1_e53_sf1_mq2:e53_sf1_example",
        #"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
        #"C3_e53_sc1_pls:e53_sc1_example",
        #"C4_e53_sc2_axis:e53_sc2_example",
        #"C5_e53_is1_infrared:e53_is1_example",

        #"D1_iot_wifi_ap:wifi_ap",
        #"D2_iot_wifi_sta_connect:wifi_sta_connect",        
        #"D3_iot_udp_client:udp_client",
        #"D4_iot_tcp_server:tcp_server",
        #"D5_iot_mqtt:iot_mqtt",        
        #"D6_iot_cloud_oc:oc_mqtt",
        #"D7_iot_cloud_onenet:onenet_mqtt",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        #"A1_YuchuanThread:yuchuanThread",
        #"A2_YuchuanTimer:yuchuanTimer",
        #"A4_YuchuanMutex:yuchuanMutex",
        #"A5_YuchuanSemaphore:yuchuanSemaphore",
        #"A3_YuchuanEvent:yuchuanEvent",
        #"A6_YuchuanMessage:yuchuanMessageQueue",
        #"B3_YuchuanBasicPWM:yuchuanPWM",
        #"B4_YuchuanBasicADC:yuchuanBasicADC",
        "B5_YuchuanNFCI2C:yuchuanNFCI2C",
    ]
}

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,并请使用带有NFC功能的手机靠近开发板,能读取数据。

ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!

hiview init success.I2C NFC Yuchuan Start!!!
00 00:00:00 0 196 D 0/HIVIEW: log limit init success.
00 00:00:00 0 196 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b10e8 TaskPool:0xe4b7c
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b10f4 TaskPool:0xe4b9c
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b15a4 TaskPool:0xe4bbc
00 00:00:00 0 228 I 1/SAMGR: Init service 0x4b10f4 <time: 0ms> success!
00 00:00:00 0 128 I 1/SAMGR: Init service 0x4b10e8 <time: 0ms> success!
00 00:00:00 0 72 I 1/SAMGR: Init service 0x4b15a4 <time: 0ms> success!
00 00:00:00 0 72 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 128 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 128 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 128 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!
=======================================
***********I2C_NFC_Yuchuan_Example**********
=======================================
Please use the mobile phone with NFC function close to the development board!

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。