初始提交

This commit is contained in:
2024-09-28 14:24:04 +08:00
commit c756587541
5564 changed files with 2413077 additions and 0 deletions

View File

@@ -0,0 +1,366 @@
/****************************************************************************
*
* Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
*
* This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics 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.
*
* ****************************************************************************/
#include "iot_app_api.h"
#include "iot_app_ckbq_dev_drv_api.h"
#include "iot_uart_api.h"
#include "iot_gpio_api.h"
#include "iot_pkt_api.h"
#include "mcp_io_drv.h"
#include "os_utils_api.h"
#include "iot_io_api.h"
#include "iot_adc_api.h"
typedef struct _app_drv_dev_resource_
{
app_press_key_int_func_t calback_ps_key;
}app_rsc_t;
app_rsc_t g_app_rsc = {0};
#define ADC_SWITCH(adc) (((adc) > 320) ? 1 : 0)
int32_t iot_app_drv_switch_key_val_get(void)
{
int32_t keymap = 0;
int32_t val;
uint8_t timeout;
val = iot_adc_poll_done(ADC_CHANNEL0, ADC_ACC16, ADC_GAIN_3V, &timeout);
if((!timeout) && ADC_SWITCH(val))
{
keymap |= IOT_APP_DRV_KEY_1;
}
val = iot_adc_poll_done(ADC_CHANNEL1, ADC_ACC16, ADC_GAIN_3V, &timeout);
if((!timeout) && ADC_SWITCH(val))
{
keymap |= IOT_APP_DRV_KEY_2;
}
val = iot_adc_poll_done(ADC_CHANNEL2, ADC_ACC16, ADC_GAIN_3V, &timeout);
if((!timeout) && ADC_SWITCH(val))
{
keymap |= IOT_APP_DRV_KEY_3;
}
val = iot_adc_poll_done(ADC_CHANNEL3, ADC_ACC16, ADC_GAIN_3V, &timeout);
if((!timeout) && ADC_SWITCH(val))
{
keymap |= IOT_APP_DRV_KEY_4;
}
return keymap;
}
static int32_t iot_app_drv_tf_init(void)
{
return APP_DRV_OK;
}
int32_t iot_app_drv_tf_write(uint32_t offset, uint8_t *p_data, uint32_t dlen)
{
return APP_DRV_OK;
}
int32_t iot_app_drv_tf_read(uint32_t offset, uint8_t *p_data, uint32_t dlen)
{
return APP_DRV_OK;
}
void iot_app_drv_led_switch(uint32_t led_type, uint32_t on)
{
uint32_t gpio, val;
gpio = (led_type == IOT_APP_DRV_LED_PLC) ? IOT_APP_DRV_LED_PLC_EXT_GPIO
: IOT_APP_DRV_LED_WARN_EXT_GPIO;
val = on ? 0 : 1;
mcp_gpio_set_value(gpio, val);
return;
}
static int32_t iot_app_drv_press_key_int_handle(int32_t arg)
{
uint32_t pending;
(void)arg;
if(g_app_rsc.calback_ps_key)
{
/* This handle runs in task enviroment */
pending = ~ mcp_gpio_get_int_gpio(); /* Val = 0 => key pressed. */
pending >>= 8; /* Port B : 8 ~ 15 bits */
//iot_cus_printf("\nPRESS : KEY#0x%x", pending);
g_app_rsc.calback_ps_key(pending & IOT_APP_DRV_KEY_MASK);
}
iot_gpio_interrupt_enable(IOT_APP_DRV_PS_KEY_INT_GPIO, 1);
/* Clear pending interrupts */
mcp_gpio_get_int_gpio();
return APP_DRV_OK;
}
static int32_t iot_app_drv_mcp_init(void)
{
uint32_t ret;
if (ERR_OK != mcp_init())
{
return APP_DRV_ERROR;
}
os_delay(100);
ret = mcp_gpio_set_dir(IOT_APP_DRV_PS_KEY_1_EXT_GPIO, MCP_GPIO_INPUT);
ret |= mcp_gpio_set_dir(IOT_APP_DRV_PS_KEY_2_EXT_GPIO, MCP_GPIO_INPUT);
ret |= mcp_gpio_set_dir(IOT_APP_DRV_PS_KEY_3_EXT_GPIO, MCP_GPIO_INPUT);
ret |= mcp_gpio_set_dir(IOT_APP_DRV_PS_KEY_4_EXT_GPIO, MCP_GPIO_INPUT);
ret |= mcp_gpio_set_dir(IOT_APP_DRV_LED_PLC_EXT_GPIO, MCP_GPIO_OUTPUT);
ret |= mcp_gpio_set_dir(IOT_APP_DRV_LED_WARN_EXT_GPIO, MCP_GPIO_OUTPUT);
if(ERR_OK != ret)
{
return APP_DRV_ERROR;
}
ret = mcp_gpio_set_pullup(IOT_APP_DRV_PS_KEY_1_EXT_GPIO, 1);
ret |= mcp_gpio_set_pullup(IOT_APP_DRV_PS_KEY_2_EXT_GPIO, 1);
ret |= mcp_gpio_set_pullup(IOT_APP_DRV_PS_KEY_3_EXT_GPIO, 1);
ret |= mcp_gpio_set_pullup(IOT_APP_DRV_PS_KEY_4_EXT_GPIO, 1);
ret |= mcp_gpio_set_pullup(IOT_APP_DRV_LED_PLC_EXT_GPIO, 1);
ret |= mcp_gpio_set_pullup(IOT_APP_DRV_LED_WARN_EXT_GPIO, 1);
if(ERR_OK != ret)
{
return APP_DRV_ERROR;
}
ret = mcp_gpio_set_interrupt(IOT_APP_DRV_PS_KEY_1_EXT_GPIO, 1, MCP_GPIO_FALLING);
ret |= mcp_gpio_set_interrupt(IOT_APP_DRV_PS_KEY_2_EXT_GPIO, 1, MCP_GPIO_FALLING);
ret |= mcp_gpio_set_interrupt(IOT_APP_DRV_PS_KEY_3_EXT_GPIO, 1, MCP_GPIO_FALLING);
ret |= mcp_gpio_set_interrupt(IOT_APP_DRV_PS_KEY_4_EXT_GPIO, 1, MCP_GPIO_FALLING);
if(ERR_OK != ret)
{
return APP_DRV_ERROR;
}
return APP_DRV_OK;
}
static int32_t iot_app_drv_press_key_init(app_press_key_int_func_t func)
{
if (NULL == func)
{
return APP_DRV_ERROR;
}
g_app_rsc.calback_ps_key = func;
if ((ERR_OK != iot_gpio_open_as_interrupt(IOT_APP_DRV_PS_KEY_INT_GPIO))
|| (ERR_OK != iot_gpio_interrupt_config(IOT_APP_DRV_PS_KEY_INT_GPIO,
GPIO_INT_EDGE_FALLING, (iot_gpio_isr_func)iot_app_drv_press_key_int_handle,
0, GPIO_INT_FUNC_ENABLE_AUTOSTOP))
|| (ERR_OK != iot_gpio_interrupt_enable(IOT_APP_DRV_PS_KEY_INT_GPIO, 1)))
{
g_app_rsc.calback_ps_key = NULL;
iot_gpio_close(IOT_APP_DRV_PS_KEY_INT_GPIO);
return APP_DRV_ERROR;
}
/* Clear pending interrupts */
(void)mcp_gpio_get_int_gpio();
return APP_DRV_OK;
}
int32_t iot_app_drv_init(app_press_key_int_func_t func)
{
if(APP_DRV_OK != iot_app_drv_mcp_init())
{
return APP_DRV_ERROR;
}
if(APP_DRV_OK != iot_app_drv_press_key_init(func))
{
return APP_DRV_ERROR;
}
if(APP_DRV_OK != iot_app_drv_tf_init())
{
return APP_DRV_ERROR;
}
return APP_DRV_OK;
}
#define CKBQ_TEST_ROUTINE
#ifdef CKBQ_TEST_ROUTINE
#include "os_task_api.h"
#include "mcp_io_drv.h"
extern void iot_print_config(bool_t enable);
uint32_t mcp_read_command(uint8_t reg, uint8_t *data, uint8_t len);
void iot_app_drv_write_wifi_log(iot_pkt_t *p_log)
{
iot_pkt_free(p_log);
}
void iot_app_drv_press_key_func(uint32_t key)
{
iot_pkt_t *p_press_key;
uint8_t *p_data;
int32_t len;
p_press_key = iot_pkt_alloc(128, 0);
if(NULL == p_press_key)
{
iot_cus_printf("\r\nCKBTEST : iot_pkt_alloc in iot_app_drv_press_key_func failed \r\n");
return;
}
p_data = iot_pkt_data(p_press_key);
len = iot_sprintf((char *)p_data, "\r\nCKBTEST : PS1:%d, PS2:%d, PS3:%d, PS4:%d \r\n.",
((key & IOT_APP_DRV_KEY_1) ? 1 : 0),
((key & IOT_APP_DRV_KEY_2) ? 1 : 0),
((key & IOT_APP_DRV_KEY_3) ? 1 : 0),
((key & IOT_APP_DRV_KEY_4) ? 1 : 0));
iot_cus_printf("%s", p_data);
iot_pkt_put(p_press_key, len);
iot_app_drv_write_wifi_log(p_press_key);
return;
}
void iot_app_drv_test_task(void *arg)
{
iot_pkt_t *p_log_pkt;
uint8_t *p_data;//, val, pend =0 , intena = 0, defval = 0, intcon = 0, dir = 0;
int32_t len, sw_key;//, ret;
iot_cus_printf("\r\nCKBTEST : enter iot_app_drv_test_task \r\n");
if(APP_DRV_OK != iot_app_drv_init((void *)iot_app_drv_press_key_func))
{
iot_cus_printf("\r\nCKBTEST : iot_app_drv_init failed \r\n");
}
for(;;)
{
os_delay(5000);
p_log_pkt = iot_pkt_alloc(128, 0);
if(NULL == p_log_pkt)
{
iot_cus_printf("\r\nCKBTEST : iot_pkt_alloc in for(;;) failed \r\n");
continue;
}
sw_key = iot_app_drv_switch_key_val_get();
if(sw_key & IOT_APP_DRV_KEY_1)
{
iot_app_drv_led_switch(IOT_APP_DRV_LED_WARN, 1);
}
else
{
iot_app_drv_led_switch(IOT_APP_DRV_LED_WARN, 0);
}
if(sw_key & IOT_APP_DRV_KEY_4)
{
iot_app_drv_led_switch(IOT_APP_DRV_LED_PLC, 1);
}
else
{
iot_app_drv_led_switch(IOT_APP_DRV_LED_PLC, 0);
}
p_data = iot_pkt_data(p_log_pkt);
len = iot_sprintf((char *)p_data, "\r\nCKBTEST : SW1:%d, SW2:%d, SW3:%d, SW4:%d.\r\n",
((sw_key & IOT_APP_DRV_KEY_1) ? 1 : 0),
((sw_key & IOT_APP_DRV_KEY_2) ? 1 : 0),
((sw_key & IOT_APP_DRV_KEY_3) ? 1 : 0),
((sw_key & IOT_APP_DRV_KEY_4) ? 1 : 0));
iot_cus_printf("%s", p_data);
iot_pkt_put(p_log_pkt, len);
iot_app_drv_write_wifi_log(p_log_pkt);
#if 0
ret = 0;
ret |= mcp_read_command(IODIR(0), &dir, 1);
ret |= mcp_read_command(GPIO(0), &val, 1);
ret |= mcp_read_command(INTF(0), &pend, 1);
ret |= mcp_read_command(GPINTEN(0), &intena, 1);
ret |= mcp_read_command(DEFVAL(0), &defval, 1);
ret |= mcp_read_command(INTCON(0), &intcon, 1);
iot_cus_printf("\r\nCKBTEST : pt-a ret#%d dir#%x,val#%x,pend#%x,intena#%x,defval#%x,intcon#%x",
ret, dir, val, pend, intena, defval, intcon);
ret = 0;
ret |= mcp_read_command(IODIR(8), &dir, 1);
ret |= mcp_read_command(GPIO(8), &val, 1);
ret |= mcp_read_command(INTF(8), &pend, 1);
ret |= mcp_read_command(GPINTEN(8), &intena, 1);
ret |= mcp_read_command(DEFVAL(8), &defval, 1);
ret |= mcp_read_command(INTCON(8), &intcon, 1);
iot_cus_printf("\r\nCKBTEST : pt-b ret#%d dir#%x,val#%x,pend#%x,intena#%x,defval#%x,intcon#%x",
ret, dir, val, pend, intena, defval, intcon);
#endif
}
return;
}
void iot_app_drv_test_start(void)
{
os_task_h os_task;
os_task = os_create_task(iot_app_drv_test_task, NULL, 6);
if(NULL == os_task)
{
iot_cus_printf("\r\nCKBTEST : create iot_app_drv_test_task failed \r\n");
}
else
{
iot_cus_printf("\r\nCKBTEST : create iot_app_drv_test_task OK \r\n");
}
iot_print_config(0);
return;
}
#endif

View File

@@ -0,0 +1,60 @@
/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics 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.
****************************************************************************/
#ifndef _INCLUDE_IOT_APP_CKBQ_DEV_DRV_API_H_
#define _INCLUDE_IOT_APP_CKBQ_DEV_DRV_API_H_
#ifndef BIT
#define BIT(n) (1 << (n))
#endif
#define IOT_APP_DRV_PS_KEY_INT_GPIO 48
#define IOT_APP_DRV_LED_PLC_EXT_GPIO 1
#define IOT_APP_DRV_LED_WARN_EXT_GPIO 2
#define IOT_APP_DRV_PS_KEY_1_EXT_GPIO 8
#define IOT_APP_DRV_PS_KEY_2_EXT_GPIO 9
#define IOT_APP_DRV_PS_KEY_3_EXT_GPIO 10
#define IOT_APP_DRV_PS_KEY_4_EXT_GPIO 11
typedef enum _iot_app_drv_led_type
{
IOT_APP_DRV_LED_WARN,
IOT_APP_DRV_LED_PLC
}app_led_type_e;
enum _iot_app_drv_key_enum
{
IOT_APP_DRV_KEY_1 = BIT(0),
IOT_APP_DRV_KEY_2 = BIT(1),
IOT_APP_DRV_KEY_3 = BIT(2),
IOT_APP_DRV_KEY_4 = BIT(3),
IOT_APP_DRV_KEY_MASK = BIT(0) | BIT(1) | BIT(2) | BIT(3),
};
typedef void(*app_press_key_int_func_t)(uint32_t key);
#define APP_DRV_ERROR (-1)
#define APP_DRV_OK (0)
void iot_app_drv_led_switch(uint32_t led_type, uint32_t on);
int32_t iot_app_drv_switch_key_val_get(void);
int32_t iot_app_drv_init(app_press_key_int_func_t func);
#endif

View File

@@ -0,0 +1,283 @@
/****************************************************************************
*
* Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
*
* This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics 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.
*
* ****************************************************************************/
#include "iot_errno.h"
#include "iot_app_api.h"
#include "iot_i2c_api.h"
#include "mcp_io_drv.h"
#include "iot_io_api.h"
#include "os_utils_api.h"
#include "iot_gpio_api.h"
iot_i2c_module_cfg_t g_cfg;
static uint32_t mcp_write_command(uint8_t reg, uint8_t val)
{
uint8_t ret;
char buf[3];
buf[0] = reg;
buf[1] = val;
ret = iot_i2c_write(g_cfg.port, MCP_SLAVER_ADDR, buf, 2);
os_delay(100);
//iot_cus_printf("\nMCP : WRITE REG#%02x, DATA#%02x", 0xFF & ((int)reg), 0xFF & ((int)val));
return (ERR_OK == ret) ? ERR_OK : ERR_FAIL;
}
uint32_t mcp_read_command(uint8_t reg, uint8_t *data, uint8_t len)
{
uint8_t ret;
char buf[3];
buf[0] = reg;
ret = iot_i2c_write(g_cfg.port, MCP_SLAVER_ADDR, buf, 1);
os_delay(10);
if (ERR_OK != ret) {
return ERR_FAIL;
}
ret = iot_i2c_read(g_cfg.port, MCP_SLAVER_ADDR, (char *)data, len);
os_delay(100);
if (ERR_OK != ret) {
iot_cus_printf("\nMCP : READ REG#%02x , TIMEOUT", 0xFF & ((int)reg));
return ERR_FAIL;
}
// iot_cus_printf("\nMCP : READ REG#%02x, DATA#%02x", 0xFF & ((int)reg), 0xFF & ((int)*data));
return ERR_OK;
}
uint32_t mcp_gpio_get_value(uint32_t gpio)
{
uint32_t ret;
uint8_t data;
if (!MCP_GPIO_VALID(gpio)) {
return ERR_FAIL;
}
ret = mcp_read_command(GPIO(gpio), &data, 1);
if (ERR_OK != ret) {
return ERR_FAIL;
}
return MCP_IO_GET(data, gpio);
}
uint32_t mcp_gpio_set_value(uint32_t gpio, uint32_t val)
{
uint32_t ret;
uint8_t data;
if (!MCP_GPIO_VALID(gpio)) {
return ERR_FAIL;
}
ret = mcp_read_command(GPIO(gpio), &data, 1);
if (ERR_OK != ret) {
return ERR_FAIL;
}
if (0 == val) {
MCP_IO_CLEAR(data, gpio);
} else {
MCP_IO_SET(data, gpio);
}
ret = mcp_write_command(GPIO(gpio), data);
if (ERR_OK != ret) {
return ERR_FAIL;
}
return ERR_OK;
}
uint32_t mcp_gpio_set_dir(uint32_t gpio, uint32_t dir)
{
uint32_t ret;
uint8_t data;
if (!MCP_GPIO_VALID(gpio)) {
return ERR_FAIL;
}
ret = mcp_read_command(IODIR(gpio), &data, 1);
if (ERR_OK != ret) {
return ERR_FAIL;
}
if (MCP_GPIO_INPUT == dir) {
MCP_IO_SET(data, gpio);
} else {
MCP_IO_CLEAR(data, gpio);
}
ret = mcp_write_command(IODIR(gpio), data);
if (ERR_OK != ret) {
return ERR_FAIL;
}
return ERR_OK;
}
uint32_t mcp_gpio_set_interrupt(uint32_t gpio, uint32_t enable, uint32_t mode)
{
uint32_t ret;
uint8_t gpinten, defval, intcon;
if ((!MCP_GPIO_VALID(gpio)
|| (enable && (MCP_GPIO_RAISING != mode) && (MCP_GPIO_FALLING != mode)
&& (MCP_GPIO_BOTH_EDGE != mode)))) {
return ERR_FAIL;
}
ret = mcp_read_command(GPINTEN(gpio), &gpinten, 1);
if (enable) {
ret |= mcp_read_command(DEFVAL(gpio), &defval, 1);
ret |= mcp_read_command(INTCON(gpio), &intcon, 1);
}
if (ERR_OK != ret) {
return ERR_FAIL;
}
if (enable) {
MCP_IO_SET(gpinten, gpio);
if (MCP_GPIO_RAISING == mode) {
MCP_IO_SET(intcon, gpio);
MCP_IO_CLEAR(defval, gpio);
} else if (MCP_GPIO_FALLING == mode) {
MCP_IO_SET(intcon, gpio);
MCP_IO_SET(defval, gpio);
} else {
MCP_IO_CLEAR(intcon, gpio);
}
ret = mcp_write_command(DEFVAL(gpio), defval);
ret |= mcp_write_command(INTCON(gpio), intcon);
if (ERR_OK != ret) {
return ERR_FAIL;
}
} else {
MCP_IO_CLEAR(gpinten, gpio);
}
ret = mcp_write_command(GPINTEN(gpio), gpinten);
if (ERR_OK != ret) {
return ERR_FAIL;
}
return ERR_OK;
}
uint32_t mcp_gpio_get_int_gpio(void)
{
uint32_t port, final_pend = 0;
uint8_t intcap[MCP_GPMAX], intf, int_flag = 0;
for (port = 0; port < MCP_GPMAX; port++) {
mcp_read_command(INTF(port * MCP_MAX_GPIO_NUM_PER_PORT), &intf, 1);
if (intf) {
int_flag = 1; /* Walk all port, no break. */
}
}
if (0 == int_flag) {
return 0;
}
/* Read GPIO or INTCAP will clear the interrupt satus. */
for (port = 0; port < MCP_GPMAX; port++) {
mcp_read_command(INTCAP(port * MCP_MAX_GPIO_NUM_PER_PORT), &intcap[port], 1);
}
for (port = MCP_GPMAX; port > 0; port--) {
final_pend <<= (8 * sizeof(intcap[0]));
final_pend |= intcap[port - 1];
}
return final_pend;
}
uint32_t mcp_gpio_set_pullup(uint32_t gpio, uint32_t enable)
{
uint32_t ret;
uint8_t data;
if (!MCP_GPIO_VALID(gpio)) {
return ERR_FAIL;
}
ret = mcp_read_command(GPPU(gpio), &data, 1);
if (ERR_OK != ret) {
return ERR_FAIL;
}
if (enable) {
MCP_IO_SET(data, gpio);
} else {
MCP_IO_CLEAR(data, gpio);
}
ret = mcp_write_command(GPPU(gpio), data);
if (ERR_OK != ret) {
return ERR_FAIL;
}
return ERR_OK;
}
uint32_t mcp_init(void)
{
g_cfg.port = IOT_I2C_PORT_0;
g_cfg.nack_wait_num = 10;
g_cfg.baud = 400;
g_cfg.gpio.scl = MCP_SCL_GPIO;
g_cfg.gpio.sda = MCP_SDA_GPIO;
if ((ERR_OK != iot_gpio_open_as_output(g_cfg.gpio.scl))
|| (ERR_OK != iot_gpio_open_as_output(g_cfg.gpio.sda)))
{
return ERR_FAIL;
}
iot_gpio_set_pull_mode(g_cfg.gpio.scl, GPIO_PULL_UP);
iot_gpio_set_pull_mode(g_cfg.gpio.sda, GPIO_PULL_UP);
if (ERR_OK != iot_i2c_module_init(&g_cfg))
{
iot_gpio_close(g_cfg.gpio.scl);
iot_gpio_close(g_cfg.gpio.sda);
return ERR_FAIL;
}
return ERR_OK;
}

View File

@@ -0,0 +1,233 @@
/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics 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.
****************************************************************************/
#ifndef _INCLUDE_MCP_IO_DRV_H_
#define _INCLUDE_MCP_IO_DRV_H_
#ifdef __cplusplus
extern "C" {
#endif
#define MCP_SLAVER_ADDR (0x20)
#define MCP_GPA 0x0
#define MCP_GPB 0x1
#define MCP_GPMAX 0x2
/* 32 & 33 conflicted with LED_RX & LED_TX */
#define MCP_SDA_GPIO 32
#define MCP_SCL_GPIO 33
#define MCP_MAX_GPIO_NUM_PER_PORT 8
#define MCP_MAX_GPIO_NUM (MCP_MAX_GPIO_NUM_PER_PORT * MCP_GPMAX)
#define MCP_GPIO_VALID(io) (((io) < MCP_MAX_GPIO_NUM && (io) >= 0) ? 1 : 0)
/*
GPIO <---> PORT
0 GPA0
1 GPA1
2 GPA2
3 GPA3
4 GPA4
5 GPA5
6 GPA6
7 GPA7
8 GPB0
9 GPB1
10 GPB2
11 GPB3
12 GPB4
13 GPB5
14 GPB6
15 GPB7
*/
/* mcp23017 register addr use bank = 0 ,here wo don't use bank = 1*/
#define IODIRA (0x00)
#define IPOLA (0x02)
#define GPINTENA (0x04)
#define DEFVALA (0x06)
#define INTCONA (0x08)
#define IOCONA (0x0A)
#define GPPUA (0x0C)
#define INTFA (0x0E)
#define INTCAPA (0x10)
#define GPIOA (0x12)
#define OLATA (0x14)
#define IODIRB (0x01)
#define IPOLB (0x03)
#define GPINTENB (0x05)
#define DEFVALB (0x07)
#define INTCONB (0x09)
#define IOCONB (0x0B)
#define GPPUB (0x0D)
#define INTFB (0x0F)
#define INTCAPB (0x11)
#define GPIOB (0x13)
#define OLATB (0x15)
#define IO_2_GP(io) ((io) < MCP_MAX_GPIO_NUM_PER_PORT ? MCP_GPA : MCP_GPB)
#define IO_SHIFT(io) ((io) < MCP_MAX_GPIO_NUM_PER_PORT ? (io) : (io) - MCP_MAX_GPIO_NUM_PER_PORT)
/*
Controls the direction of the data I/O.
1 = Pin is configured as an input.
0 = Pin is configured as an output.
*/
#define IODIR(io) (IO_2_GP(io) == MCP_GPA ? IODIRA : IODIRB)
/*
This register allows the user to configure the polarity on
the corresponding GPIO port bits.
1 = GPIO register bit will reflect the opposite logic state of the input pin.
0 = GPIO register bit will reflect the same logic state of the input pin.
*/
#define IPOL(io) (IO_2_GP(io) == MCP_GPA ? IPOLA : IPOLB)
/*
The GPINTEN register controls the interrupt-onchange feature for each pin.
1 = Enable GPIO input pin for interrupt-on-change event.
0 = Disable GPIO input pin for interrupt-on-change event.
*/
#define GPINTEN(io) (IO_2_GP(io) == MCP_GPA ? GPINTENA : GPINTENB)
/*
The default comparison value is configured in the
DEFVAL register. If enabled (via GPINTEN and
INTCON) to compare against the DEFVAL register, an
opposite value on the associated pin will cause an
interrupt to occur.
*/
#define DEFVAL(io) (IO_2_GP(io) == MCP_GPA ? DEFVALA : DEFVALB)
/*
The INTCON register controls how the associated pin
value is compared for the interrupt-on-change feature.
If a bit is set, the corresponding I/O pin is compared
against the associated bit in the DEFVAL register. If a
bit value is clear, the corresponding I/O pin is compared
against the previous value
1 = The corresponding I/O pin is compared against the associated bit in
the DEFVAL register.
0 = Pin value is compared against the previous pin value.
*/
#define INTCON(io) (IO_2_GP(io) == MCP_GPA ? INTCONA : INTCONB)
/*
Keep default value.
*/
#define IOCON(io) (IO_2_GP(io) == MCP_GPA ? IOCONA : IOCONB)
/*
The GPPU register controls the pull-up resistors for the
port pins. If a bit is set and the corresponding pin is
configured as an input, the corresponding port pin is
internally pulled up with a 100 kΩ resistor.
1 = Pull-up enabled.
0 = Pull-up disabled.
*/
#define GPPU(io) (IO_2_GP(io) == MCP_GPA ? GPPUA : GPPUB)
/*
The INTF register reflects the interrupt condition on the
port pins of any pin that is enabled for interrupts via the
GPINTEN register. A set bit indicates that the
associated pin caused the interrupt.
1 = Pin caused interrupt.
0 = Interrupt not pending.
*/
#define INTF(io) (IO_2_GP(io) == MCP_GPA ? INTFA : INTFB)
/*
The INTCAP register captures the GPIO port value at
the time the interrupt occurred. The register is read
only and is updated only when an interrupt occurs. The
register will remain unchanged until the interrupt is
cleared via a read of INTCAP or GPIO.
1 = Logic-high.
0 = Logic-low
*/
#define INTCAP(io) (IO_2_GP(io) == MCP_GPA ? INTCAPA : INTCAPB)
/*
The GPIO register reflects the value on the port.
Reading from this register reads the port. Writing to this
register modifies the Output Latch (OLAT) register.
1 = Logic-high.
0 = Logic-low.
*/
#define GPIO(io) (IO_2_GP(io) == MCP_GPA ? GPIOA : GPIOB)
/*
The OLAT register provides access to the output
latches. A read from this register results in a read of the
OLAT and not the port itself. A write to this register
modifies the output latches that modifies the pins
configured as outputs.
1 = Logic-high.
0 = Logic-low.
*/
#define OLAT(io) (IO_2_GP(io) == MCP_GPA ? OLATA : OLATB)
#define MCP_IO_BIT(io) (1 << IO_SHIFT(io))
#define MCP_IO_SET(data, io) ((data) |= MCP_IO_BIT(io))
#define MCP_IO_GET(data, io) (((data) & MCP_IO_BIT(io)) ? 1 : 0)
#define MCP_IO_CLEAR(data, io) ((data) &= (~ MCP_IO_BIT(io)))
enum mcp_gpio_mode
{
MCP_GPIO_INPUT,
MCP_GPIO_OUTPUT
};
/**
* @brief Modes of interrupt. Only when gpio_mode set as GPIO_INTERRUPT,
* int_mode is available.
*/
enum mcp_gpio_int_trigger_mode
{
MCP_GPIO_RAISING, /**< Interrupt triggered when the voltage of this
GPIO switchs from LOW to HIGH. */
MCP_GPIO_FALLING, /**< Interrupt triggered when the voltage of this
GPIO switchs from HIGH to LOW. */
MCP_GPIO_BOTH_EDGE, /**< Interrupt triggered when the voltage of this GPIO
switchs to HIGH or LOW . */
MCP_GPIO_INVALID /**< Invalid value */
};
uint32_t mcp_gpio_get_value(uint32_t gpio);
uint32_t mcp_gpio_set_value(uint32_t gpio, uint32_t val);
uint32_t mcp_gpio_set_dir(uint32_t gpio, uint32_t dir);
uint32_t mcp_gpio_set_interrupt(uint32_t gpio, uint32_t enable, uint32_t mode);
uint32_t mcp_gpio_get_int_gpio(void);
uint32_t mcp_gpio_set_pullup(uint32_t gpio, uint32_t enable);
uint32_t mcp_init(void);
#ifdef __cplusplus
}
#endif
#endif/*__GPIO_EX_H*/

View File

@@ -0,0 +1,120 @@
/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics 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_ship header files */
#include "os_task_api.h"
#include "os_event_api.h"
#include "os_timer_api.h"
#include "os_utils_api.h"
/* iot common header files */
#include "iot_plc_api.h"
#include "iot_module_api.h"
#include "iot_queue_api.h"
#include "iot_config_api.h"
#include "iot_plc_msg_api.h"
#include "iot_board_api.h"
#include "iot_mem_pool_api.h"
#include "iot_config_api.h"
#include "iot_app_api.h"
#include "iot_errno_api.h"
#include "iot_io_api.h"
#include "iot_dbglog_api.h"
#include "iot_uart_api.h"
#include "iot_utils_api.h"
#include "iot_ckb.h"
#if (IOT_STA_CONTROL_MODE == 1)
static iot_ckb_task_data_t *ckb_task_data;
static uint32_t iot_ckb_task_init()
{
uint32_t ret = ERR_OK;
iot_task_config_t task_cfg;
iot_cus_printf("Initializing ckb task start\n");
ckb_task_data = os_mem_malloc(IOT_APP_CKB_MID,
sizeof(*ckb_task_data));
if (!ckb_task_data) {
ret = ERR_NOMEM;
goto error_1;
}
os_mem_set(ckb_task_data, 0, sizeof(iot_ckb_task_data_t));
ckb_task_data->link_id = IOT_CKB_TASK_LIKE_ID;
ckb_task_data->dev_role = IOT_PLC_DEV_ROLE_INVALID;
ckb_task_data->band_id = PLC_LIB_FREQ_BAND_1;
task_cfg.stack_size = 0;
task_cfg.task_prio = IOT_CKB_TASK_PRIO;
task_cfg.msg_size = sizeof(iot_ckb_task_msg_t);
task_cfg.msg_cnt = IOT_CKB_TASK_POOL_SIZE;
task_cfg.queue_cnt = IOT_CKB_TASK_QUEUE_MAX_PRIO;
task_cfg.queue_cfg[IOT_CKB_TASK_QUEUE_HP].quota = 0;
task_cfg.msg_exe_func = NULL;
task_cfg.msg_cancel_func = NULL;
ckb_task_data->task_handle =
iot_task_create(IOT_APP_CKB_MID, &task_cfg);
if (ckb_task_data->task_handle == NULL) {
ret = ERR_FAIL;
goto error_2;
}
ckb_task_data->info_pool.remain_cnt = IOT_CKB_POOL_NUM;
goto success;
error_2:
os_mem_free(ckb_task_data);
ckb_task_data = NULL;
error_1:
success:
iot_cus_printf("Initializing ckb task done, ret: %lu\n", ret);
return ret;
}
/*
* app_entry: entry for sta_conn_less app
* @return:
* ERR_PENDING - if application want to delay the plc network formation.
* otherwise - plc network formation will be started automatically.
*/
uint32_t app_ckb_entry()
{
uint32_t ret = ERR_PENDING;
/* if the firmware is release version(0), disabel log printing, if not,
* enable the log printing
*/
if (iot_version_type() == 1) {
iot_cus_print_config(true);
} else {
iot_cus_print_config(false);
}
if (iot_ckb_task_init()) {
return ERR_OK;
}
return ret;
}
#endif /* IOT_STA_CONTROL_MODE==1 */

168
app/chaokongbao/iot_ckb.h Normal file
View File

@@ -0,0 +1,168 @@
/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics 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.
****************************************************************************/
#ifndef _IOT_PLC_CKB_H
#define _IOT_PLC_CKB_H
#include "iot_task_api.h"
#ifdef __cplusplus
extern "C" {
#endif
#define UART_0_BAUD_RATE 9600
/* define priorities for message to be handle */
#define IOT_CKB_TASK_QUEUE_HP 0
#define IOT_CKB_TASK_QUEUE_LP 1
#define IOT_CKB_TASK_QUEUE_MAX_PRIO 2
#define IOT_CKB_TASK_LIKE_ID 2
#define IOT_CKB_TASK_POOL_SIZE 128
/* message type */
#define IOT_CKB_MAC_MSG 0
#define IOT_CKB_TIMER_MSG 1
#define IOT_CKB_UART_RECV_MSG 2
/* message id for timer */
#define IOT_CKB_TIMER_CHECK 1 /* check cache data in buffer */
/* check cache data in sendq interval, unit is ms */
#define IOT_CKB_PERIODIC_TIMER_INTERVAL (250)
/* count for boardcast sta connection_less's mode config interval */
#define IOT_CKB_BCAST_TIMER_CNT 4
/* max life-span cnt, which decide how long will the buff keep the frame info */
#define IOT_CKB_MAX_LIFE_SPAN_CNT 16
/* parameter sent to sta for meter reading timeout config, uint is 100ms */
#define IOT_CKB_CFG_MR_TIMEOUT 16
/* Macro to define msdu pkt pool number*/
#define IOT_CKB_POOL_NUM 8
/* UART received buffer size */
#define IOT_CKB_UART_RECV_BUF_SIZE 256
/* sta control configuring effective duration, unit is sec */
#define IOT_CKB_MODE_CFG_DUR 30
/* uart frame protocal */
#define IOT_CKB_PROTO_TYPE_TRANSPARENT 0
#define IOT_CKB_PROTO_TYPE_645_1997 1
#define IOT_CKB_PROTO_TYPE_645_2007 2
#define IOT_CKB_PROTO_TYPE_69845 3
/* define target device type ID */
#define IOT_CKB_TARGET_ID_CCO 0
#define IOT_CKB_TARGET_ID_STA 1
/* defining target device type identifier mask */
#define IOT_CKB_TARGET_ID_MSK_CCO \
(1 << IOT_CKB_TARGET_ID_CCO)
#define IOT_CKB_TARGET_ID_MSK_STA \
(1 << IOT_CKB_TARGET_ID_STA)
/* rerty invterval , unit is IOT_CKB_PERIODIC_TIMER_INTERVAL */
#define IOT_CKB_APP_MAX_RETRY_INTV_CNT 8
#pragma pack(push) /* save the pack status */
#pragma pack(1) /* 1 byte align */
typedef struct _ckb_mode_cfg {
/* band ID to be configured, see PLC_LIB_FREQ_BAND_X */
uint8_t band_id;
/* configuring effective duration, uint is sec */
uint8_t dur;
/* the target device ID group that needs to respond to this command.
* see IOT_CKB_TARGET_ID_MSK_XXX.
*/
uint32_t target_id_mask;
} ckb_mode_cfg_t;
#pragma pack(pop) /* restore the pack status */
/* store the frame and infor extracted from the frame */
typedef struct _frame_pkt_t {
/* msdu sequnce number */
uint16_t app_sn;
/* dst meter addresss */
uint8_t dst_mac[IOT_MAC_ADDR_LEN];
/* full 645/698 frame */
iot_pkt_t *data;
} frame_pkt_t;
/* frame infor extracted from the frame */
typedef struct _frame_info_t {
/* life-span counter */
uint16_t span_cnt;
/* msdu been sent counter, 0 means the buffer is not used */
uint8_t send_cnt;
/* frame data and releated infor */
frame_pkt_t frm;
} frame_info_t;
/* frame infor pool */
typedef struct _info_pool_t {
/* how many buffers are not used */
uint8_t remain_cnt;
/* infor extracted from the frame */
frame_info_t frm_info[IOT_CKB_POOL_NUM];
} info_pool_t;
typedef struct _iot_ckb_msg {
/* standard iot_task message */
iot_task_msg_t msg;
/* pointer to message data */
void *data;
}iot_ckb_task_msg_t;
typedef struct _iot_ckb_task_data {
/* app sending sequnce number for msdu */
uint16_t app_sn;
/* handle of the ping iot_task */
iot_task_h task_handle;
/* a flag indicating if app registered successfully */
uint8_t app_registered;
/* link id used for sending msdu */
uint8_t link_id;
/* indication for device is ready for data transmition or not */
uint8_t dev_ready;
/* device type */
uint8_t dev_type;
/* role of local device */
uint8_t dev_role;
/* mac address of local device */
uint8_t mac_addr[IOT_MAC_ADDR_LEN];
/* frequency band value, see PLC_LIB_FREQ_BAND_X*/
uint8_t band_id;
/* handle of this app */
iot_plc_app_h app_handle;
/* frame infor pool */
info_pool_t info_pool;
}iot_ckb_task_data_t;
/*
* brief: entry for ckb app
* @return:
* @retval: ERR_PENDING - if app want to delay the plc network formation.
* @retval: other - plc network formation will be started automatically.
*/
uint32_t app_ckb_entry();
#ifdef __cplusplus
}
#endif
#endif //_IOT_PLC_CKB_H