• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ssd1306_oled.h"
17 #include "c081_nfc.h"
18 #include "iot_i2c.h"
19 #include "ohos_init.h"
20 #include "cmsis_os2.h"
21 #include "iot_gpio.h"
22 #include "iot_gpio_ex.h"
23 #include "app_demo_config.h"
24 
25 unsigned char readReg = 0;
26 #define SEND_CMD_LEN (2)
27 #define NFC_TASK_SLEEP_1MS     (1)
28 
29 /* i2c read */
WriteRead(unsigned char regHigh8bitCmd,unsigned char regLow8bitCmd,unsigned char * recvData,unsigned char sendLen,unsigned char readLen)30 unsigned int WriteRead(unsigned char regHigh8bitCmd, unsigned char regLow8bitCmd,
31     unsigned char* recvData, unsigned char sendLen, unsigned char readLen)
32 {
33     IotI2cData co8iNfcI2cReadData = {0};
34     IotI2cData c081NfcI2cWriteCmdAddr = {0};
35 
36     unsigned char sendUserCmd[SEND_CMD_LEN] = {regHigh8bitCmd, regLow8bitCmd};
37     (void)memset_s(&co8iNfcI2cReadData, sizeof(IotI2cData), 0x0, sizeof(IotI2cData));
38 
39     c081NfcI2cWriteCmdAddr.sendBuf = sendUserCmd;
40     c081NfcI2cWriteCmdAddr.sendLen = sendLen;
41     co8iNfcI2cReadData.receiveBuf = recvData;
42     co8iNfcI2cReadData.receiveLen = readLen;
43 
44     readReg = NFC_CLEAN; // 消除stop信号
45 
46     IoTI2cWrite(IOT_I2C_IDX_0, C081_NFC_ADDR & 0xFE,
47                 c081NfcI2cWriteCmdAddr.sendBuf, c081NfcI2cWriteCmdAddr.sendLen);
48 
49     IoTI2cRead(IOT_I2C_IDX_0, C081_NFC_ADDR | I2C_RD,
50         co8iNfcI2cReadData.receiveBuf, co8iNfcI2cReadData.receiveLen);
51     return 0;
52 }
53 
54 #define EEPROM_CMD_1    (0x3B1)
55 #define EEPROM_CMD_2    (0x3B5)
56 #define SEND_EEPROM_CMD_LEN (1)
57 #define SEND_EEPROM_DATA_1  (1)
58 #define SEND_EEPROM_DATA_2  (3)
59 
60 /* NFC 芯片配置 ,平时不要调用 NFC init */
NfcInit(void)61 void NfcInit(void)
62 {
63     // uint8_t wbuf[5]={0x05,0x72,0xF7,0x60,0x02}; // 芯片默认配置
64     unsigned char wBuf[5] = {0x05, 0x78, 0xF7, 0x90, 0x02}; // 芯片默认配置
65     /* 读取字节的时候屏蔽csn引脚,写eep的时候打开 */
66     IoSetFunc(IOT_IO_NAME_GPIO9, IOT_IO_FUNC_GPIO_9_GPIO);
67     IoTGpioSetDir(IOT_GPIO_IDX_9, IOT_GPIO_DIR_OUT);
68     IoTGpioSetOutputVal(IOT_IO_NAME_GPIO9, IOT_GPIO_VALUE0);
69 
70     Fm11WriteEep(EEPROM_CMD_1, SEND_EEPROM_CMD_LEN, &wBuf[SEND_EEPROM_DATA_1]); /* send EEPROM cmd */
71     Fm11WriteEep(EEPROM_CMD_2, SEND_EEPROM_CMD_LEN, &wBuf[SEND_EEPROM_DATA_2]); /* send EEPROM cmd */
72 }
73 
NfcTask(const char * param)74 void *NfcTask(const char* param)
75 {
76     (void) param;
77     IoTGpioInit(IOT_IO_NAME_GPIO_13);
78     IoSetFunc(IOT_IO_NAME_GPIO_13, IOT_IO_FUNC_GPIO_13_I2C0_SDA);
79     IoTGpioInit(IOT_IO_NAME_GPIO_14);
80     IoSetFunc(IOT_IO_NAME_GPIO_14, IOT_IO_FUNC_GPIO_14_I2C0_SCL);
81 
82     IoTI2cInit(IOT_I2C_IDX_0, HI_I2C_IDX_BAUDRATE); // baud 400k
83     IoTI2cSetBaudrate(IOT_I2C_IDX_0, HI_I2C_IDX_BAUDRATE);
84     printf("nfc task\r\n");
85     NfcRead();
86 }
87 
88 /* c08i nfc task */
NfcExampleEntry(void)89 void NfcExampleEntry(void)
90 {
91     osThreadAttr_t attr = {0};
92 
93     attr.stack_size = C08I_NFC_DEMO_TASK_STAK_SIZE;
94     attr.priority = C08I_NFC_TASK_PRIORITY;
95     attr.name = "nfcTask";
96     attr.attr_bits = 0U;
97     attr.cb_mem = NULL;
98     attr.cb_size = 0U;
99     attr.stack_mem = NULL;
100 
101     if (osThreadNew((osThreadFunc_t)NfcTask, NULL, &attr) == NULL) {
102         printf("[nfcExample] Failed to create LedTask!\n");
103     }
104 }
105 
106 SYS_RUN(NfcExampleEntry);
107 
108 /* nfc display */
AppNfcDisplay(char * param)109 void *AppNfcDisplay(char* param)
110 {
111     for (; ;) {
112         OledNfcDisplay();
113         TaskMsleep(NFC_TASK_SLEEP_1MS);
114     }
115 }
116 
117 /* nfc display task */
NfcDisplayExampleEntry(void)118 void NfcDisplayExampleEntry(void)
119 {
120     osThreadAttr_t attr = {0};
121     attr.stack_size = NFC_DISPLAY_TASK_STAK_SIZE;
122     attr.priority = C08I_NFC_DEMO_TASK_PRIORITY;
123     attr.name = "app_nfc_display";
124     attr.attr_bits = 0U;
125     attr.cb_mem = NULL;
126     attr.cb_size = 0U;
127     attr.stack_mem = NULL;
128     if (osThreadNew((osThreadFunc_t)AppNfcDisplay, NULL, &attr) == NULL) {
129         printf("[nfcDisplayExampleEntry] Failed to create LedTask!\n");
130     }
131 }
132 
133 SYS_RUN(NfcDisplayExampleEntry);