Files
kunlun/mfgtool/oem_tool/hw/oem_tool_hw.c

791 lines
23 KiB
C
Raw Permalink Normal View History

2024-09-28 14:24:04 +08:00
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
#include "iot_uart_api.h"
#include "oem_common.h"
#include "oem_tool_hw.h"
uint8_t gpio_func_sel(uint8_t gpio)
{
if ((gpio <= 28) || (gpio >= 43)) {
return 0;
} else {
return 3;
}
}
uint32_t gpio_mtx_get_sig_inid(uint8_t dev, uint8_t io)
{
uint32_t inid = 0xff;
switch(dev){
case UART0:
if (io == RX ){
inid = 9;
}
break;
case UART1:
if (io == RX ){
inid = 11;
}
break;
case UART2:
if (io == RX ){
inid = 13;
}
break;
case UART3:
if (io == RX ){
inid = 15;
}
break;
case SPI0:
if (io == MISO ){
inid = 16;
}
break;
case SPI1:
if (io == MISO ){
inid = 17;
}
break;
case ZC:
if (io == PHASE_A ){
inid = 48;
} else if (io == PHASE_B ){
inid = 49;
} else if (io == PHASE_C ){
inid = 50;
}
break;
default:
break;
}
return inid;
}
uint32_t gpio_mtx_get_sig_outid(uint8_t dev, uint8_t io)
{
uint32_t outid = 0xff;
switch(dev){
case UART0:
if(io == TX){
outid = 13;
}
break;
case UART1:
if(io == TX){
outid = 15;
}
break;
case UART2:
if(io == TX){
outid = 17;
}
break;
case UART3:
if(io == TX){
outid = 19;
}
break;
case SPI0:
if(io == CS){
outid = 21;
} else if (io == CLK ){
outid = 20;
} else if (io == MOSI ){
outid = 22;
}
break;
case SPI1:
if(io == CS){
outid = 24;
} else if (io == CLK ){
outid = 23;
} else if (io == MOSI ){
outid = 25;
}
break;
case LED:
if(io == TX){
outid = 49;
} else if (io == RX ){
outid = 50;
} else if (io == PWR ){
outid = 52;
} else if (io == RS485 ){
outid = 51;
} else if (io == PHASE_A ){
outid = 51;
} else if (io == PHASE_B ){
outid = 52;
} else if (io == PHASE_C ){
outid = 53;
}
break;
case TX_E:
if (io == PHASE_A ){
outid = 67;
} else if (io == PHASE_B ){
outid = 68;
} else if (io == PHASE_C ){
outid = 69;
}
break;
case RX_E:
if (io == PHASE_A ){
outid = 70;
} else if (io == PHASE_B ){
outid = 71;
} else if (io == PHASE_C ){
outid = 72;
}
break;
case GLNA:
if (io == PHASE_A ){
outid = 73;
} else if (io == PHASE_B ){
outid = 74;
} else if (io == PHASE_C ){
outid = 75;
}
break;
default:
break;
}
return outid;
}
void config_gpio_pin(uint8_t gpio, dev_type dev, dev_io io )
{
iot_pin_cfg_v0_t pin;
uint32_t inid = gpio_mtx_get_sig_inid(dev, io);
uint32_t outid = gpio_mtx_get_sig_outid(dev, io);
switch(dev){
case UART0:
if(io == TX){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
if(gpio == 30){
pin.func = 0;
} else {
pin.func = gpio_func_sel(gpio);
}
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == RX ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
if(gpio == 31){
pin.func = 0;
} else {
pin.func = gpio_func_sel(gpio);
}
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
}
break;
case UART1:
if(io == TX){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == RX ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
}
break;
case UART2:
if(io == TX){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == RX ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
}
break;
case UART3:
if(io == TX){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == RX ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
}
break;
case SPI0:
if(io == CS){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == CLK ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == MOSI ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == MISO ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
}
break;
case SPI1:
if(io == CS){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == CLK ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == MOSI ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == MISO ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
}
break;
case LED:
if(io == TX){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == RX ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PWR ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == RS485 ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_A ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_B ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_C ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
}
break;
case TX_E:
if (io == PHASE_A ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_B ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_C ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
}
break;
case RX_E:
if (io == PHASE_A ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_B ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_C ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
}
break;
case GLNA:
if (io == PHASE_A ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_B ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
} else if (io == PHASE_C ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
}
break;
case ZC:
if (io == PHASE_A ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
} else if (io == PHASE_B ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
} else if (io == PHASE_C ){
pin.gpio = gpio;
pin.io = IO_TYPE_IN;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = 0xff;
pin.used = 1;
}
break;
case IIC:
if (io == SDA ){
pin.gpio = gpio;
pin.io = IO_TYPE_IO;
pin.func = gpio_func_sel(gpio);
pin.inid = inid;
pin.outid = outid;
pin.used = 1;
} else if (io == SCL ){
pin.gpio = gpio;
pin.io = IO_TYPE_OUT;
pin.func = gpio_func_sel(gpio);
pin.inid = 0xff;
pin.outid = outid;
pin.used = 1;
}
break;
default:
break;
}
oemcfg.board_cfg[0].pin[gpio].io = pin.io;
oemcfg.board_cfg[0].pin[gpio].func = pin.func;
oemcfg.board_cfg[0].pin[gpio].inid = pin.inid;
oemcfg.board_cfg[0].pin[gpio].outid = pin.outid;
oemcfg.board_cfg[0].pin[gpio].used = pin.used;
}
void config_uart_map(dev_type dev, uart_type uart )
{
iot_uart_cfg_v0_t map;
memset(&map, 0x0, sizeof(iot_uart_cfg_v0_t));
switch(dev){
case UART0:
map.port = 0;
map.used = 1;
if(uart == METER ){
map.type = UART_METER_PORT;
} else if(uart == CLI ){
map.type = UART_CLI_PORT;
} else if(uart == LOG ){
map.type = UART_PRINT;
}
else if(uart == R485 ){
map.type = UART_RS485_PORT;
}
else if(uart == IR ){
map.type = UART_IR_PORT;
}
else if(uart == EXT ){
map.type = UART_EXT_CHIP;
}
break;
case UART1:
map.port = 1;
map.used = 1;
if(uart == METER ){
map.type = UART_METER_PORT;
} else if(uart == CLI ){
map.type = UART_CLI_PORT;
} else if(uart == LOG ){
map.type = UART_PRINT;
}
else if(uart == R485 ){
map.type = UART_RS485_PORT;
}
else if(uart == IR ){
map.type = UART_IR_PORT;
}
else if(uart == EXT ){
map.type = UART_EXT_CHIP;
}
break;
case UART2:
map.port = 2;
map.used = 1;
if(uart == METER ){
map.type = UART_METER_PORT;
} else if(uart == CLI ){
map.type = UART_CLI_PORT;
} else if(uart == LOG ){
map.type = UART_PRINT;
}
else if(uart == R485 ){
map.type = UART_RS485_PORT;
}
else if(uart == IR ){
map.type = UART_IR_PORT;
}
else if(uart == EXT ){
map.type = UART_EXT_CHIP;
}
break;
case UART3:
map.port = 3;
map.used = 1;
if(uart == METER ){
map.type = UART_METER_PORT;
} else if(uart == CLI ){
map.type = UART_CLI_PORT;
} else if(uart == LOG ){
map.type = UART_PRINT;
}
else if(uart == R485 ){
map.type = UART_RS485_PORT;
}
else if(uart == IR ){
map.type = UART_IR_PORT;
}
else if(uart == EXT ){
map.type = UART_EXT_CHIP;
}
break;
default:
break;
}
oemcfg.board_cfg[0].port[dev].port = dev;
oemcfg.board_cfg[0].port[dev].type = map.type;
oemcfg.board_cfg[0].port[dev].used = 1;
}
void load_database_hw (const char *filename)
{
FILE *fp = fopen (filename, "r");
char buf[2048];
unsigned line_no;
dev_type dev = DEV_TYPE_MAX;
dev_io io = DEV_IO_MAX;
uart_type uart = UART_MAX;
if (fp == NULL)
die ("error opening %s: %s", filename, strerror (errno));
line_no = 0;
while (fgets (buf, sizeof (buf), fp) != NULL){
line_no++;
if (buf[0] == '#')
continue;
if (is_whitespace (buf))
continue;
chomp_trailing_whitespace (buf);
if (isspace (buf[0])){
char *start = buf + 1;
while (*start && isspace (*start))
start++;
io = DEV_IO_MAX;
uart = UART_MAX;
if (starts_with (start, "tx ")){
//printf("tx gpio:%d\n", atoi (peek_next_token (start)));
io = TX;
}
else if(starts_with (start, "rx ")){
//printf("rx gpio:%d\n", atoi (peek_next_token (start)));
io = RX;
}
else if (starts_with (start, "power ")){
//printf("power gpio:%d\n", atoi (peek_next_token (start)));
io = PWR;
}
else if(starts_with (start, "rs485 ")){
//printf("rs485 gpio:%d\n", atoi (peek_next_token (start)));
io = RS485;
}
else if(starts_with (start, "cs ")){
//printf("cs gpio:%d\n", atoi (peek_next_token (start)));
io = CS;
}
else if(starts_with (start, "clk ")){
//printf("clk gpio:%d\n", atoi (peek_next_token (start)));
io = CLK;
}
else if(starts_with (start, "mosi ")){
//printf("mosi gpio:%d\n", atoi (peek_next_token (start)));
io = MOSI;
}
else if(starts_with (start, "miso ")){
//printf("miso gpio:%d\n", atoi (peek_next_token (start)));
io = MISO;
}
else if(starts_with (start, "phase_a ")){
//printf("phase_a gpio:%d\n", atoi (peek_next_token (start)));
io = PHASE_A;
}
else if(starts_with (start, "phase_b ")){
//printf("phase_b gpio:%d\n", atoi (peek_next_token (start)));
io = PHASE_B;
}
else if(starts_with (start, "phase_c ")){
//printf("phase_c gpio:%d\n", atoi (peek_next_token (start)));
io = PHASE_C;
}
else if(starts_with (start, "sda ")){
//printf("phase_c gpio:%d\n", atoi (peek_next_token (start)));
io = SDA;
}
else if(starts_with (start, "scl ")){
//printf("phase_c gpio:%d\n", atoi (peek_next_token (start)));
io = SCL;
}
else if(starts_with (start, "bid ")){
//printf("board id:%d\n", atoi (peek_next_token (start)));
oemcfg.board_id = atoi (peek_next_token (start));
}
else if(starts_with (start, "vid ")){
//printf("vendor id:%s\n", peek_next_token (start));
oemcfg.misc_cfg.vendor_id = htons(*(uint16_t*)(peek_next_token (start)));
}
else if(starts_with (start, "mid ")){
//printf("module type:%d\n", atoi (peek_next_token (start)));;
oemcfg.base_cfg.module_type = atoi(peek_next_token (start));
}
else if(starts_with (start, "meter")){
//printf("meter uart\n");
uart = METER;
}
else if(starts_with (start, "log")){
//printf("log uart\n");
uart = LOG;
}
else if(starts_with (start, "cli")){
//printf("cli uart\n");
uart = CLI;
}
else if(starts_with (start, "rs485")){
//printf("rs485 uart\n");
uart = R485;
}
else if(starts_with (start, "ir")){
//printf("ir uart\n");
uart = IR;
}
else if(starts_with (start, "ext")){
//printf(ext uart\n");
uart = EXT;
}
else
die ("error on %s, line %u: unrecognized field starting with %s",
filename, line_no, start);
if(dev != DEV_TYPE_MAX && io != DEV_IO_MAX){
uint8_t gpio = atoi (peek_next_token (start));
config_gpio_pin(gpio,dev, io);
}
if(dev != DEV_TYPE_MAX && uart != UART_MAX){
config_uart_map(dev, uart);
}
} else {
char *start = buf;
//printf("\n%s\n", buf);
while (*start && isspace (*start))
start++;
dev = DEV_TYPE_MAX;
if(starts_with (start, "uart0")){
dev = UART0;
}
else if(starts_with (start, "uart1")){
dev = UART1;
}
else if(starts_with (start, "uart2")){
dev = UART2;
}
else if(starts_with (start, "uart3")){
dev = UART3;
}
else if(starts_with (start, "led")){
dev = LED;
}
else if(starts_with (start, "spi0")){
dev = SPI0;
}
else if(starts_with (start, "spi1")){
dev = SPI1;
}
else if(starts_with (start, "tx_e")){
dev = TX_E;
}
else if(starts_with (start, "rx_e")){
dev = RX_E;
}
else if(starts_with (start, "glna")){
dev = GLNA;
}
else if(starts_with (start, "zc")){
dev = ZC;
}
else if(starts_with (start, "iic")){
dev = IIC;
}
}
}
fclose (fp);
}
void save_database_hw(const char *filename)
{
FILE *fp;
uint8_t *buf = (uint8_t*)&oemcfg;
if(filename == NULL){
fp = fopen ("oem.bin", "wb");
} else {
fp = fopen (filename, "wb");
}
//printf("oem len:%d\n", (uint32_t)sizeof(iot_oem_cfg_t));
oemcfg.base_cfg.oem_crc = iot_getcrc8(buf + sizeof(uint8_t),
sizeof(iot_oem_cfg_t) - sizeof(uint8_t));
if (fp == NULL)
die ("error write oem.bin: %s", strerror (errno));
fwrite(buf, sizeof(iot_oem_cfg_t), 1, fp);
fclose(fp);
}
void oem_bin_file_parse_hw(const char *filename)
{
(void)filename;
}