• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include "iot_errno.h"
19 #include "iot_gpio.h"
20 #include "iot_flash.h"
21 #include "iot_i2c.h"
22 #include "iot_pwm.h"
23 #include "iot_uart.h"
24 #include "iot_watchdog.h"
25 #include "lowpower.h"
26 #include "reset.h"
27 
28 #include "iot_test.h"
29 #include "typedef.h"
30 #include "target_util_pub.h"
31 #include "securec.h"
32 
33 #define GPIO15 15
34 #define TEST_PIN GPIO15
35 
36 #define CHECK_RESULT(ret) do {if ((ret) != IOT_SUCCESS) { printf("test failed %s:%d\n", __FILE__, __LINE__); return ret; }} while(0)
37 
gpio_cb(char * arg)38 static void gpio_cb(char *arg)
39 {
40 	printf("gpio_cb : got %d\n", *arg);
41 }
42 
iot_test_gpio(void)43 int iot_test_gpio(void)
44 {
45 	unsigned int ret;
46 	unsigned int id;
47 	IotGpioValue value;
48 	IotGpioDir dir;
49 
50 	printf("iot_test_gpio begin...\r\n");
51 
52 	id = TEST_PIN;
53 
54 //****************** test output ****************//
55 	ret = IoTGpioInit(id);
56 	CHECK_RESULT(ret);
57 
58 	ret = IoTGpioSetDir(id, IOT_GPIO_DIR_OUT);
59 	CHECK_RESULT(ret);
60 
61 	ret = IoTGpioGetDir(id, &dir);
62 	CHECK_RESULT(ret);
63 	if (dir != IOT_GPIO_DIR_OUT) {
64 		printf("set IOT_GPIO_DIR_OUT, but got %d\n", dir);
65 		return -1;
66 	}
67 
68 	ret = IoTGpioSetOutputVal(id, IOT_GPIO_VALUE1);
69 	CHECK_RESULT(ret);
70 
71 	ret = IoTGpioGetOutputVal(id, &value);
72 	CHECK_RESULT(ret);
73 	if (value != IOT_GPIO_VALUE1) {
74 		printf("set IOT_GPIO_VALUE1, but got %d\n", value);
75 		return -1;
76 	}
77 
78 	ret = IoTGpioSetOutputVal(id, IOT_GPIO_VALUE0);
79 	CHECK_RESULT(ret);
80 
81 	ret = IoTGpioGetOutputVal(id, &value);
82 	CHECK_RESULT(ret);
83 	if (value != IOT_GPIO_VALUE0) {
84 		printf("set IOT_GPIO_VALUE0, but got %d\n", value);
85 		return -1;
86 	}
87 
88 //****************** test input & interrupt ****************//
89 	ret = IoTGpioSetDir(id, IOT_GPIO_DIR_IN);
90 	CHECK_RESULT(ret);
91 
92 	ret = IoTGpioGetInputVal(id, &value);
93 	CHECK_RESULT(ret);
94 
95 	ret = IoTGpioRegisterIsrFunc(id, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_RISE_LEVEL_HIGH, gpio_cb, (char *)&id);
96 	CHECK_RESULT(ret);
97 
98 	ret = IoTGpioSetIsrMask(id, 1);
99 	CHECK_RESULT(ret);
100 
101 	ret = IoTGpioSetIsrMask(id, 0);
102 	CHECK_RESULT(ret);
103 
104 	ret = IoTGpioUnregisterIsrFunc(id);
105 	CHECK_RESULT(ret);
106 
107 	ret = IoTGpioDeinit(id);
108 	CHECK_RESULT(ret);
109 
110 	printf("iot_test_gpio ok.\r\n");
111 	return 0;
112 }
113 
iot_test_flash(void)114 int iot_test_flash(void)
115 {
116 #define FLASH_TEST_SIZE (4096 * 2)
117 	unsigned int start_addr,size,i;
118 	static unsigned char buf[FLASH_TEST_SIZE];
119 	//1. flash erase
120 	start_addr = 0x1F0000;
121 	size = FLASH_TEST_SIZE;
122 	memset(buf,0,FLASH_TEST_SIZE);
123 	IoTFlashErase(start_addr,size);
124 	IoTFlashRead(start_addr,size,buf);
125 	for(i=0;i<FLASH_TEST_SIZE;i++)
126 	{
127 		if(buf[i] != 0xff)
128 			break;
129 	}
130 	if(i == FLASH_TEST_SIZE)
131 		printf("flash Erase success!\r\n");
132 	else
133 		printf("flash Erase fail!!!\r\n");
134 
135 	//2. flash write
136 	memset(buf,0x5a,FLASH_TEST_SIZE);
137 	IoTFlashWrite(start_addr,size,buf,0);
138 	memset(buf,0,FLASH_TEST_SIZE);
139 
140 	IoTFlashRead(start_addr,size,buf);
141 	for(i=0;i<FLASH_TEST_SIZE;i++)
142 	{
143 		if(buf[i] != 0x5a)
144 			break;
145 	}
146 	if(i == FLASH_TEST_SIZE)
147 		printf("flash write success 11!\r\n");
148 	else
149 		printf("flash write fail!!!\r\n");
150 
151 	//3. flash erase & write
152 	memset(buf,0xa5,FLASH_TEST_SIZE);
153 	IoTFlashWrite(start_addr,size,buf,1);
154 	memset(buf,0,FLASH_TEST_SIZE);
155 
156 	IoTFlashRead(start_addr,size,buf);
157 	for(i=0;i<FLASH_TEST_SIZE;i++)
158 	{
159 		if(buf[i] != 0xa5)
160 			break;
161 	}
162 	if(i == FLASH_TEST_SIZE)
163 		printf("flash write success 22!\r\n");
164 	else
165 		printf("flash write fail!!!\r\n");
166 	return 0;
167 }
168 
iot_test_i2c(void)169 int iot_test_i2c(void)
170 {
171 	return 113;
172 }
173 
174 #define TEST_PWM_PORT 3
175 
iot_test_pwm(void)176 int iot_test_pwm(void)
177 {
178 	unsigned int ret;
179 	unsigned int port;
180 
181 	printf("iot_test_pwm begin...\r\n");
182 
183 	port = TEST_PWM_PORT;
184 
185 	ret = IoTPwmInit(port);
186 	CHECK_RESULT(ret);
187 
188 	ret = IoTPwmStart(port, 30, 1000);
189 	CHECK_RESULT(ret);
190 
191 	delay_ms(500);
192 
193 	ret = IoTPwmStop(port);
194 	CHECK_RESULT(ret);
195 
196 
197 	ret = IoTPwmDeinit(port);
198 	CHECK_RESULT(ret);
199 
200 	printf("iot_test_pwm ok.\r\n");
201 	return 0;
202 }
203 
204 #define TEST_UART_PORT 1
205 
iot_test_uart(void)206 int iot_test_uart(void)
207 {
208 #define BUFFER_LEN 32
209 
210 	unsigned int ret;
211 	unsigned int port;
212 	IotUartAttribute param;
213 	unsigned char buffer[BUFFER_LEN];
214 
215 	printf("iot_test_uart begin...\r\n");
216 
217 	param.baudRate = 115200;
218 	param.dataBits = IOT_UART_DATA_BIT_8;
219 	param.stopBits = IOT_UART_STOP_BIT_1;
220 	param.parity = IOT_UART_PARITY_NONE;
221 	param.rxBlock = IOT_UART_BLOCK_STATE_NONE_BLOCK;
222 	param.txBlock = IOT_UART_BLOCK_STATE_NONE_BLOCK;
223 
224 	port = TEST_UART_PORT;
225 	ret = IoTUartInit(port, &param);
226 	CHECK_RESULT(ret);
227 
228 	printf("read from serial...\n");
229 	delay_ms(500);
230 
231 	ret = IoTUartRead(port, buffer, BUFFER_LEN - 1);
232 	buffer[BUFFER_LEN - 1] = '\0';
233 	printf("IoTUartRead : ret=%d\n", ret);
234 
235 	memcpy_s(buffer, BUFFER_LEN, "send some string\n", 17 + 1);
236 	ret = IoTUartWrite(port, buffer, 17 + 1);
237 	printf("IoTUartWrite : ret=%d\n", ret);
238 
239 	printf("iot_test_uart ok.\r\n");
240 	return 0;
241 }
242 
iot_test_watchdog(void)243 int iot_test_watchdog(void)
244 {
245 	int i;
246 
247 	printf("iot_test_watchdog begin...\r\n");
248 
249 	IoTWatchDogEnable();
250 	for (i = 0; i < 5; i++) {
251 		printf("kick wdg %d\n", i);
252 		IoTWatchDogKick();
253 		delay_ms(500);
254 	}
255 	//delay_ms(5000);
256 	IoTWatchDogDisable();
257 
258 	printf("iot_test_watchdog ok.\r\n");
259 	return 0;
260 }
261 
iot_test_lowpower(void)262 int iot_test_lowpower(void)
263 {
264 	return 117;
265 }
266 
iot_test_reset(void)267 int iot_test_reset(void)
268 {
269 	printf("iot_test_reset begin...\r\n");
270 
271 	delay_ms(2000);
272 	printf("reboot device\r\n");
273 	delay_ms(200);
274 	RebootDevice(0);
275 
276 	printf("iot_test_reset ok.\r\n");
277 	return 0;
278 }
279 
280