[RVB2601 Creative Application Development] User Experience 02 -- KV Storage
[Copy link]
After a period of modular development of the RVB2601 creative project, I am ready to start integrating the modules into a complete system. I will record the integration process in separate chapters. This chapter records the KV component.
1. Introduction to KV storage system
KV , or " Key-Value ", is a lightweight component for persistent storage in YoC ( Yun on Chip ), mainly used for Nor Flash . The API description from the README.md of the SDK ( kv v7.4.3 ) is shown in the figure below.
Figure 2-1 KV interface description
KV storage must be built on the basis of Flash partitions, so the partitions must be created first. In the " Hello World " case, the partition initialization code is commented out, so you need to remove the comments - this is also a pitfall, which troubled me for a long time at the time (actually, it was also due to carelessness).
Figure 2-2 Partition initialization is commented out by default
2. KV persistence of project parameters
In my project, I need to store some configuration parameters. It is unnecessary to set up a file system for these parameters, and the KV component can meet the needs.
Here we import “ Hello World Demo ” and add KV initialization code in “ app/src/init.c ” .
#include <stdbool.h>
#include <aos/aos.h>
#include <yoc/yoc.h>
#include <yoc/partition.h>
#include <devices/devicelist.h>
#include "board.h"
#include "app_init.h"
#include "drv/pin.h"
const char *TAG = "INIT";
#ifndef CONSOLE_UART_IDX
#define CONSOLE_UART_IDX 0
#endif
/* --------------------by AITA Mr.Fei, global variables--------------------- */
#define ENDIAN_LIMIT 32 //endian counts limit
char GID[9]; //gateway id
char EID[ENDIAN_LIMIT][9];//endians' id
int endian_count = 1; //actually endian counts
int period = 10; //polling period -- minutes
int timeout = 15; //timeout of waiting USR220 resp -- seconds
int repeat = 3; //repeat times when timeout
int aita_InitKV(void) {
char buf[10] = {0};
int ret = -1;
ret = partition_init();
if (ret > 0) {
LOGI(TAG, "find %d partitions\n", ret);
} else {
LOGE(TAG, "partition init failed\n");
return ret;
}
ret = aos_kv_init("kv");
if(ret == 0) {
LOGE(TAG, "kv init done\n");
} else {
LOGE(TAG, "kv init failed\n");
return -1;
}
ret = aos_kv_getstring("endian_count", &buf, 10);
if(ret > 0) {
endian_count = atoi(buf);
endian_count = (endian_count<ENDIAN_LIMIT) ? endian_count : ENDIAN_LIMIT;
printf("endian_count: %d\n", endian_count);
char eid[5] = "EIDx";
for(int i=0; i<endian_count; i++) {
eid[3] = i+0x30;
aos_kv_getstring(eid, EID[i], 9);
printf("EID No.%d : %s\n", i, EID[i]);
}
} else {
LOGE(TAG, "Endian Count load failed\n");
return ret;
}
ret = aos_kv_getstring("period", &buf, 10);
if(ret > 0) {
period = atoi(buf);
printf("period: %d\n", period);
} else {
LOGE(TAG, "period load failed\n");
return ret;
}
ret = aos_kv_getstring("timeout", &buf, 10);
if(ret > 0) {
timeout = atoi(buf);
printf("timeout: %d\n", timeout);
} else {
LOGE(TAG, "timeout load failed\n");
return ret;
}
ret = aos_kv_getstring("repeat", &buf, 10);
if(ret > 0) {
repeat = atoi(buf);
printf("repeat: %d\n", repeat);
} else {
LOGE(TAG, "repeat load failed\n");
return ret;
}
return 0;
}
void board_yoc_init()
{
board_init();
// uart_csky_register(CONSOLE_UART_IDX);
console_init(CONSOLE_UART_IDX, 115200, 128);
ulog_init();
aos_set_log_level(AOS_LL_DEBUG);
LOGI(TAG, "Build:%s,%s",__DATE__, __TIME__);
aita_InitKV();
board_cli_init();
}
main.c first tests the KV component and outputs the KV value in the console when the system starts. In order not to be affected by the timed output of " Hello World ", the code is changed to a running light function. For specific implementation, you can refer to the blog post of blogger TL-LED "【Flathead RVB2601 Development Board Trial Experience】GPIO Output Test ( https://en.eeworld.com/bbs/thread-1197715-1-1.html )".
#include <stdlib.h>
#include <string.h>
#include <aos/aos.h>
#include "aos/cli.h"
#include "main.h"
#include "app_init.h"
#include "oled.h"
#define TAG "app"
int main(void) {
board_yoc_init();
LOGD(TAG, "%s\n", aos_get_app_version());
aita_InitBSP();
while (1) {
aita_FlashLED();
}
return 0;
}
void aita_InitBSP(void) {
oled_init();
aita_InitLED();
}
Here is my own code for LED GPIO initialization and running light implementation.
#include <aos/aos.h>
#include <drv/pin.h>
#include <drv/gpio_pin.h>
csi_gpio_pin_t aita_led_red_pin;
csi_gpio_pin_t aita_led_green_pin;
csi_gpio_pin_t aita_led_blue_pin;
void aita_InitLED(void) {
//set pin gpio function
csi_pin_set_mux(PA7, PIN_FUNC_GPIO);
csi_pin_set_mux(PA25, PIN_FUNC_GPIO);
csi_pin_set_mux(PA4, PIN_FUNC_GPIO);
//init pin device & set direction
csi_gpio_pin_init(&aita_led_red_pin, PA7);
csi_gpio_pin_dir(&aita_led_red_pin, GPIO_DIRECTION_OUTPUT);
csi_gpio_pin_init(&aita_led_green_pin, PA25);
csi_gpio_pin_dir(&aita_led_green_pin, GPIO_DIRECTION_OUTPUT);
csi_gpio_pin_init(&aita_led_blue_pin, PA4);
csi_gpio_pin_dir(&aita_led_blue_pin, GPIO_DIRECTION_OUTPUT);
//set leds' state at startup
csi_gpio_pin_write(&aita_led_red_pin, GPIO_PIN_HIGH);
csi_gpio_pin_write(&aita_led_green_pin, GPIO_PIN_HIGH);
csi_gpio_pin_write(&aita_led_blue_pin, GPIO_PIN_HIGH);
}
void aita_FlashLED(void) {
csi_gpio_pin_write(&aita_led_red_pin, GPIO_PIN_LOW);
csi_gpio_pin_write(&aita_led_green_pin, GPIO_PIN_HIGH);
csi_gpio_pin_write(&aita_led_blue_pin, GPIO_PIN_HIGH);
aos_msleep(1000);
csi_gpio_pin_write(&aita_led_red_pin, GPIO_PIN_HIGH);
csi_gpio_pin_write(&aita_led_green_pin, GPIO_PIN_LOW);
csi_gpio_pin_write(&aita_led_blue_pin, GPIO_PIN_HIGH);
aos_msleep(1000);
csi_gpio_pin_write(&aita_led_red_pin, GPIO_PIN_HIGH);
csi_gpio_pin_write(&aita_led_green_pin, GPIO_PIN_HIGH);
csi_gpio_pin_write(&aita_led_blue_pin, GPIO_PIN_LOW);
aos_msleep(1000);
csi_gpio_pin_write(&aita_led_red_pin, GPIO_PIN_HIGH);
csi_gpio_pin_write(&aita_led_green_pin, GPIO_PIN_HIGH);
csi_gpio_pin_write(&aita_led_blue_pin, GPIO_PIN_HIGH);
aos_msleep(1000);
}
Figure 2-3 Console output of KV storage value at startup
Of course, these KV values are written in advance. The project has enabled the KV console command, so that the project parameters can be modified at any time. Considering that in actual application, the serial port can also be connected for debugging on site, so the console is used as a way to configure parameters. To enable the console KV command, just call the api , which is written in the board_cli_init() function in this example .
#include <aos/debug.h>
#include <aos/cli.h>
void board_cli_init()
{
aos_cli_init();
extern void cli_reg_cmd_ps(void);
cli_reg_cmd_ps();
extern void cli_reg_cmd_free(void);
cli_reg_cmd_free();
//cli command of kv func
extern void cli_reg_cmd_kvtool(void);
cli_reg_cmd_kvtool();
}
|