1 /*
2 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
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 <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "cmsis_os2.h"
21 #include "iot_errno.h"
22 #include "iot_gpio.h"
23 #include "iot_gpio_ex.h"
24 #include "iot_i2c.h"
25 #include "nfc.h"
26 #include "ohos_init.h"
27
28 #define I2C_TASK_STACK_SIZE (1024 * 8)
29 #define I2C_TASK_PRIO 25
30
31 #define TEXT "Welcome to BearPi-HM_Nano!"
32 #define WEB "openharmony.cn"
33
34 #define WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA 6
35 #define WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL 6
36 #define WIFI_IOT_I2C_IDX 1
37 #define WIFI_IOT_I2C_BAUDRATE 400000
38 #define TASK_DELAY_1S 1000000
39
40 /**
41 * @brief i2c task writes data to NFC tag
42 *
43 */
I2cTask(void)44 static void I2cTask(void)
45 {
46 uint8_t ret;
47
48 // GPIO_0 multiplexed to I2C1_SDA
49 IoTGpioInit(0);
50 IoTGpioSetFunc(0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA);
51
52 // GPIO_1 multiplexed to I2C1_SCL
53 IoTGpioInit(1);
54 IoTGpioSetFunc(1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL);
55
56 // baudrate: 400kbps
57 IoTI2cInit(WIFI_IOT_I2C_IDX, WIFI_IOT_I2C_BAUDRATE);
58
59 printf("I2C Test Start\n");
60
61 ret = storeText(NDEFFirstPos, (uint8_t *)TEXT);
62 if (ret != 1) {
63 printf("NFC Write Data Falied :%d \n", ret);
64 }
65 ret = storeUrihttp(NDEFLastPos, (uint8_t *)WEB);
66 if (ret != 1) {
67 printf("NFC Write Data Falied :%d \n", ret);
68 }
69 while (1) {
70 printf("=======================================\n");
71 printf("***********I2C_NFC_example**********\n");
72 printf("=======================================\n");
73 printf("Please use the mobile phone with NFC function close to the development board!\n");
74 usleep(TASK_DELAY_1S);
75 }
76 }
77
78 /**
79 * @brief Main Entry of the I2c Example
80 *
81 */
I2cExampleEntry(void)82 static void I2cExampleEntry(void)
83 {
84 osThreadAttr_t attr;
85
86 attr.name = "I2cTask";
87 attr.attr_bits = 0U;
88 attr.cb_mem = NULL;
89 attr.cb_size = 0U;
90 attr.stack_mem = NULL;
91 attr.stack_size = I2C_TASK_STACK_SIZE;
92 attr.priority = I2C_TASK_PRIO;
93
94 if (osThreadNew((osThreadFunc_t)I2cTask, NULL, &attr) == NULL) {
95 printf("Falied to create I2cTask!\n");
96 }
97 }
98
99 APP_FEATURE_INIT(I2cExampleEntry);