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 * Date : 2022-04-29
16 * Author : ylc
17 */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "ohos_init.h"
24 #include "cmsis_os2.h"
25 #include "iot_flash.h"
26 #include "iot_errno.h"
27
28 // hi3861
29 // flash地址0x400000 - 0x600000
30 // 每页512字节
31
32 #define TRUE 1
33
34 #define FLASH_READ_WRITE_ADDR (0x200000 - BUFSIZ)
35
36 static char *str = "flash test@ylc: Hello, BearPi!";
37 static uint8_t test_buf[BUFSIZ];
38
in_flash_read(uint32_t addr,uint8_t * buf,uint16_t len)39 void in_flash_read(uint32_t addr, uint8_t *buf, uint16_t len)
40 {
41 IoTFlashRead(addr, len, (uint8_t *)buf);
42 }
43
in_flash_write(uint32_t addr,uint8_t * buf,uint16_t len)44 void in_flash_write(uint32_t addr, uint8_t *buf, uint16_t len)
45 {
46 IoTFlashWrite(addr, len, (uint8_t *)buf, TRUE);
47 }
48
49 /**************测试flash读写**************/
in_flash_test(void)50 static void in_flash_test(void)
51 {
52 // 将数据写入flash末尾
53 in_flash_write(FLASH_READ_WRITE_ADDR, (uint8_t *)str, strlen(str) + 1);
54 // 将最后200字节数据读到空白缓存中
55 in_flash_read(FLASH_READ_WRITE_ADDR, (uint8_t *)test_buf, BUFSIZ);
56 // 展示读取结果
57 printf("%s", test_buf);
58 }
59
60 APP_FEATURE_INIT(in_flash_test);