/**************************************************************************** * * Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. * * This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics Ltd and MAY NOT * be copied by any method or incorporated into another program without * the express written consent of Aerospace C.Power. This Information or any portion * thereof remains the property of Aerospace C.Power. The Information contained herein * is believed to be accurate and Aerospace C.Power assumes no responsibility or * liability for its use in any way and conveys no license or title under * any patent or copyright and makes no representation or warranty that this * Information is free from patent or copyright infringement. * * ****************************************************************************/ /* os shim includes */ #include "os_types.h" #include "os_task.h" #include "os_utils.h" #include "iot_errno_api.h" /* common includes */ #include "iot_io.h" #include "iot_bitops.h" #include "iot_config.h" /* driver includes */ #include "iot_clock.h" #include "iot_uart.h" #include "iot_i2c_api.h" #include "iot_gpio_api.h" #include "i2c_slv_hw.h" /* cli includes */ #include "iot_cli.h" #include "iot_uart_h.h" /* debug includes*/ #include "dbg_io.h" #include "hw_reg_api.h" #include "gpio_mtx.h" #include "i2c_reg.h" #include "pmu_test.h" #define I2C_S_READ (1 << 0) #define I2C_S_WRITE (1 << 1) #define I2C_S_FILE (1 << 2) #define I2C_S_ALL (I2C_S_READ | I2C_S_WRITE) #define TEST_CASE (I2C_S_FILE) os_task_h test_init_handle; extern int platform_init(); uint8_t test_dev_addr = 0x1b; uint8_t i2c_test_data[] = {0xff, 0xfd, 0x80}; iot_i2c_module_cfg_t g_cfg = {0}; int gpio_rst_test(uint8_t gpio) { uint8_t r = ERR_FAIL; r = iot_gpio_open_as_output(gpio); if(r != 0) { iot_printf("\ngpio_set_direction failed!\n"); } if (0 != iot_gpio_value_set(gpio, 1)) { iot_printf("\n WRITE 1 FAILED\n"); r = ERR_FAIL; } else { os_delay(1000); if (0 != iot_gpio_value_set(gpio, 0)) { iot_printf("\n WRITE 0 FAILED\n"); r = ERR_FAIL; } else { r = ERR_OK; } os_delay(1000); if (0 != iot_gpio_value_set(gpio, 1)) { iot_printf("\n WRITE 1 FAILED\n"); r = ERR_FAIL; } else { r = ERR_OK; } } iot_gpio_close(gpio); return r; } int i2c_write_command(uint8_t addr, uint8_t reg1, uint8_t reg2, uint8_t val) { uint8_t ret = 0; char buf[4] = {0}; buf[0] = reg1; buf[1] = reg2; buf[2] = val; ret = iot_i2c_write(g_cfg.port, addr, buf, 3); os_delay(10); // todo : receive buffer from rdata fifo return ret; } int i2c_write_file(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) { uint8_t ret = 0; char buf[33] = {0}; buf[0] = reg; os_mem_cpy(buf+1, data, len); ret = iot_i2c_write(g_cfg.port, addr, buf, len+1); os_delay(10); // todo : receive buffer from rdata fifo return ret; } int i2c_read_file(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) { uint8_t ret = 0; char buf[33] = {0}; buf[0] = reg; ret = iot_i2c_write(g_cfg.port, addr, buf, 1); if (ret) { iot_printf("i2c_read_command write addr error\n"); return ret; } os_delay(10); ret = iot_i2c_read(g_cfg.port, addr, (char *)data, len); //if slave wdata fifo is not empty, the master will receive nak at the end if (ret) { iot_printf("i2c_read_command get value error\n"); return ret; } return ret; } int i2c_read_command(uint8_t addr, uint16_t reg, uint8_t *data, uint8_t len) { uint8_t ret = 0; char buf[4] = {0}; buf[0] = reg >> 8; buf[1] = reg & 0xff; ret = iot_i2c_write(g_cfg.port, addr, buf, 2); if (ret) { iot_printf("i2c_read_command write addr error\n"); return ret; } // todo : if write match write pattern, put buffer info wdata fifo os_delay(10); ret = iot_i2c_read(g_cfg.port, addr, (char *)data, len); //if slave wdata fifo is not empty, the master will receive nak at the end if (ret) { iot_printf("i2c_read_command get value error\n"); return ret; } return ret; } void i2c_test_task() { // gpio reset //gpio_rst_test(2); //gpio_rst_test(3); //gpio_rst_test(4); //gpio_rst_test(5); // i2c master init g_cfg.port = IOT_I2C_PORT_0; g_cfg.nack_wait_num = 1; g_cfg.baud = 50; g_cfg.gpio.scl = 2; g_cfg.gpio.sda = 3; iot_i2c_module_init(&g_cfg); // i2c slave init i2c_s_gpio_sel(0, 4, 5); i2c_s_port_enable(0); i2c_s_set_dev_addr(0, test_dev_addr); i2c_s_reset(0); i2c_s_ena(0); // send i2c command while(1) { #if TEST_CASE & I2C_S_WRITE do { int ret = 0; ret = i2c_write_command(test_dev_addr, i2c_test_data[0], i2c_test_data[1], i2c_test_data[2]); if (ret) { iot_printf("write regs error[%d]\n", ret); } else { iot_printf("write successful, \n"); } } while(0); #endif #if TEST_CASE & I2C_S_READ do { uint8_t val = 0xaa; if(i2c_read_command(test_dev_addr, 0xaa55, &val, 1)) { iot_printf("read error\n"); } else { iot_printf("read value: %02x\n", val); } } while(0); #endif #if TEST_CASE & I2C_S_FILE // test pmu rom code // command control // write reg #define I2C_REG_CTRL (0x23) #define I2C_REG_TRANS (0x45) // read reg #define I2C_REG_RLEN (0x67) // reg control segment #define I2C_CTRL_START (0x11) #define I2C_CTRL_BOOT (0x33) do { int ret = 0; uint8_t test_val = 0x22; ret = i2c_write_file(test_dev_addr, I2C_REG_CTRL, &test_val, 1); iot_printf("send stop command\n"); // send start trans reg uint8_t start_val = I2C_CTRL_START; ret = i2c_write_file(test_dev_addr, I2C_REG_CTRL, &start_val, 1); iot_printf("send start command\n"); // send code uint8_t *p = pmu_test_bin; uint8_t step = 4; uint32_t cnt = pmu_test_bin_len / step; uint32_t left = pmu_test_bin_len % step; for(uint32_t i = 0; i < cnt; i++) { ret = i2c_write_file(test_dev_addr, I2C_REG_TRANS, p+i*step, step); if (ret) { iot_printf("write regs error[%d]\n"); } else { iot_printf("write successful,i =%d\n", i); } } if (left > 0) { ret = i2c_write_file(test_dev_addr, I2C_REG_TRANS, p+cnt*step, left); if (ret) { iot_printf("write regs error[%d]\n"); } else { iot_printf("write left successful, left: %d\n", left); } } // read written length command uint32_t len = 0; ret = i2c_read_file(test_dev_addr, I2C_REG_RLEN, (uint8_t *)&len, 2); if (ret) { iot_printf("read regs error\n"); } else { iot_printf("read successful\n"); iot_printf("len: %x\n", len); } // send boot command uint8_t boot_val = I2C_CTRL_BOOT; ret = i2c_write_file(test_dev_addr, I2C_REG_CTRL, &boot_val, 1); if (ret) { iot_printf("write regs error\n"); } else { iot_printf("write successful\n"); } iot_printf("send boot command\n"); while(1) { os_delay(1000); iot_printf(".............\n"); } while(1); } while(0); #endif } } void i2c_test_task_init() { os_task_h handle; handle = os_create_task(i2c_test_task, NULL, 6); if(handle != NULL) { iot_printf("task create successfully...\n"); } } void i2c_test_init() { /* init common modules */ iot_bitops_init(); /* init os related modules and utilities */ os_utils_init(); /* gpio matrix enable */ gpio_mtx_enable(); /*init uart module*/ iot_uart_init(1); i2c_test_task_init(); } void i2c_init_task(void *arg) { iot_printf("task 1 entry....\n"); for(;;) { i2c_test_init(); os_delete_task(test_init_handle); } } int32_t i2c_task_init() { /* start plc lib task */ test_init_handle = os_create_task(i2c_init_task, NULL, 9); //create the tasks; if(test_init_handle != NULL) { iot_printf("task 1 init successfully...\n"); } return 0; } int32_t i2c_task_start() { os_start_kernel(); return 0; } int32_t iot_platform_init() { platform_init(); system_clock_init(); system_uart_init(); dbg_uart_init(); return 0; } int32_t iot_module_init(void) { //platform intialization; iot_platform_init(); //create all the tasks; i2c_task_init(); iot_printf("starting...\n"); return 0; } int main(void) { //module init; iot_module_init(); //module start; i2c_task_start(); return 0; }