387 lines
7.8 KiB
C
387 lines
7.8 KiB
C
/****************************************************************************
|
|
|
|
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 shim includes */
|
|
#include "os_types.h"
|
|
#include "os_task.h"
|
|
#include "os_utils.h"
|
|
|
|
/* common includes */
|
|
#include "iot_io.h"
|
|
#include "iot_bitops.h"
|
|
#include "iot_pkt.h"
|
|
#include "iot_ipc.h"
|
|
#include "iot_dbglog_api.h"
|
|
#include "iot_config.h"
|
|
|
|
/* driver includes */
|
|
#include "iot_clock.h"
|
|
#include "iot_uart.h"
|
|
#include "uart.h"
|
|
#include "apb_dma.h"
|
|
#include "dma_hw.h"
|
|
|
|
/* cli includes */
|
|
#include "iot_cli.h"
|
|
#include "iot_uart_h.h"
|
|
|
|
/* debug includes*/
|
|
#include "dbg_io.h"
|
|
/* driver includes */
|
|
#include "cpu.h"
|
|
#include "iot_gpio_api.h"
|
|
#include "hw_reg_api.h"
|
|
#include "pin_rf.h"
|
|
#include "dtest_printf.h"
|
|
#include "iot_share_task.h"
|
|
#include "ahb.h"
|
|
#include "rtc.h"
|
|
#include "clk.h"
|
|
|
|
extern int platform_init();
|
|
|
|
os_task_h test_init_handle;
|
|
|
|
static const iot_pkt_config_t test_pkt_config =
|
|
{
|
|
{
|
|
{
|
|
256,
|
|
1,
|
|
PKT_OWNER_ALL,
|
|
},
|
|
{
|
|
600,
|
|
1,
|
|
PKT_OWNER_ALL,
|
|
},
|
|
{
|
|
1100,
|
|
1,
|
|
PKT_OWNER_ALL,
|
|
},
|
|
{
|
|
2200,
|
|
1,
|
|
PKT_OWNER_ALL,
|
|
},
|
|
{
|
|
0,
|
|
0,
|
|
PKT_OWNER_NONE,
|
|
},
|
|
{
|
|
0,
|
|
0,
|
|
PKT_OWNER_NONE,
|
|
},
|
|
{
|
|
0,
|
|
0,
|
|
PKT_OWNER_NONE,
|
|
},
|
|
{
|
|
0,
|
|
0,
|
|
PKT_OWNER_NONE,
|
|
},
|
|
}
|
|
};
|
|
|
|
|
|
|
|
#define UART_BAUD_RATE 115200
|
|
|
|
|
|
#define GPIO_FUNC_MASK 0x70
|
|
|
|
#define TEST_OUTPUT 28
|
|
|
|
#define USE_HEAD_TAIL 0
|
|
|
|
static uint32_t g_cpu_cycle;
|
|
|
|
int int_mode = GPIO_INT_LEVEL_LOW;
|
|
|
|
enum _vuart_check_type{
|
|
VUART_NONE = 0,
|
|
VUART_ODD = 1,
|
|
VUART_EVEN = 2,
|
|
VUART_MARK = 3,
|
|
VUART_SPACE = 4,
|
|
};
|
|
|
|
static void gpio_func_set(uint8_t gpio_no, uint8_t func)
|
|
{
|
|
uint32_t tmp;
|
|
uint32_t addr;
|
|
|
|
addr = CFG_GPIO00_PIN_CFG_ADDR + (gpio_no << 2);
|
|
|
|
tmp = DIG_PIN_READ_REG(addr);
|
|
/* Set function */
|
|
tmp &= ~(GPIO_FUNC_MASK);
|
|
tmp |= (func<<4)&GPIO_FUNC_MASK;
|
|
/* Set pull-up */
|
|
tmp &= ~0x40;
|
|
tmp |= 0x80;
|
|
DIG_PIN_WRITE_REG(addr, tmp);
|
|
|
|
return ;
|
|
}
|
|
|
|
static uint16_t get_high_bit_num(uint8_t d) {
|
|
uint16_t num = 0;
|
|
for (int i = 0;i < 8;i++) {
|
|
if (d & (1 << i)) {
|
|
num++;
|
|
}
|
|
}
|
|
return num;
|
|
}
|
|
|
|
static int put_start() {
|
|
// 高电平停止位
|
|
#if USE_HEAD_TAIL
|
|
iot_gpio_value_set(TEST_OUTPUT, 1);
|
|
iot_delay_cpu_cycle(g_cpu_cycle * 10);
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
static int put_byte(uint8_t data,int check) {
|
|
uint16_t p = data;
|
|
int bit_num = 8;
|
|
uint16_t high_bit_num = get_high_bit_num(data);
|
|
if (check != VUART_NONE) {
|
|
bit_num += 1;
|
|
if (check == VUART_ODD) {
|
|
p |= ((~high_bit_num) & 1)<<8;
|
|
} else if (check == VUART_EVEN) {
|
|
p |= (high_bit_num & 1)<<8;
|
|
} else if (check == VUART_MARK) {
|
|
p |= 1<<8;
|
|
}
|
|
// 其余情况默认校验位为0
|
|
}
|
|
os_disable_irq();
|
|
// 低电平起始位
|
|
iot_gpio_value_set(TEST_OUTPUT, 0);
|
|
iot_delay_cpu_cycle(g_cpu_cycle);
|
|
for (int i = 0;i < bit_num;i++) {
|
|
iot_gpio_value_set(TEST_OUTPUT, p&0x01);
|
|
iot_delay_cpu_cycle(g_cpu_cycle);
|
|
p >>= 1;
|
|
}
|
|
// 高电平停止位
|
|
iot_gpio_value_set(TEST_OUTPUT, 1);
|
|
iot_delay_cpu_cycle(g_cpu_cycle);
|
|
|
|
os_enable_irq();
|
|
return 0;
|
|
}
|
|
|
|
static int put_end() {
|
|
#if USE_HEAD_TAIL
|
|
iot_delay_cpu_cycle(g_cpu_cycle * 10);
|
|
#endif
|
|
// 低电平
|
|
iot_gpio_value_set(TEST_OUTPUT, 0);
|
|
iot_delay_cpu_cycle(g_cpu_cycle);
|
|
return 0;
|
|
}
|
|
|
|
|
|
static int puts_data(const uint8_t* data, int size) {
|
|
put_start();
|
|
for (int i = 0;i < size;i++) {
|
|
put_byte(data[i],VUART_EVEN);
|
|
}
|
|
put_end();
|
|
return size;
|
|
}
|
|
|
|
// 模拟电表回复表地址
|
|
static const uint8_t g_data[] = {
|
|
0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0x68,0x21,0x00,0xc3,0x05,0x50,0x00,
|
|
0x00,0x00,0x00,0x00,0x00,0xd2,0xa9,0x85,0x01,0x02,0x40,0x01,0x02,0x00,0x01,
|
|
0x09,0x06,0x00,0x00,0x00,0x00,0x00,0x50,0x00,0x00,0x88,0xb0,0x16
|
|
};
|
|
|
|
void test_task_gpio(){
|
|
int r;
|
|
// g_cpu_cycle = g_cpu_freq / UART_BAUD_RATE;
|
|
g_cpu_cycle = 1027;
|
|
dprintf("test_task....\n");
|
|
gpio_func_set(TEST_OUTPUT, 0);
|
|
|
|
r = iot_gpio_open_as_output(TEST_OUTPUT);
|
|
r |= iot_gpio_set_pull_mode(TEST_OUTPUT, GPIO_PULL_UP);
|
|
if(r != 0)
|
|
{
|
|
dprintf("\ngpio config failed!\n");
|
|
}
|
|
|
|
for(;;) {
|
|
#if USE_HEAD_TAIL
|
|
dprintf("puts data with head and tail.\n");
|
|
#else
|
|
dprintf("puts data without head and tail.\n");
|
|
#endif
|
|
// puts_data((uint8_t *)"hello world.\n", 13);
|
|
// puts_data((uint8_t []){0xfe,0xfe,0xfe,0xfe,0xaa,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}, 13);
|
|
puts_data(g_data, 43);
|
|
os_delay(500);
|
|
|
|
}
|
|
}
|
|
|
|
void test_init()
|
|
{
|
|
/* init common modules */
|
|
iot_bitops_init();
|
|
|
|
/* init os related modules and utilities */
|
|
os_utils_init();
|
|
|
|
/* init dbglog module */
|
|
iot_dbglog_init();
|
|
|
|
/* init pkt module */
|
|
iot_pkt_init(&test_pkt_config);
|
|
|
|
/* init ipc module */
|
|
//iot_ipc_init();
|
|
|
|
/*init uart module*/
|
|
iot_uart_init(1);
|
|
|
|
iot_share_task_init();
|
|
|
|
/* init gpio module */
|
|
iot_gpio_module_init();
|
|
|
|
test_task_gpio();
|
|
}
|
|
|
|
|
|
void iot_task_1(void *arg)
|
|
{
|
|
dprintf("task 1 entry....\n");
|
|
test_init();
|
|
|
|
for(;;) {
|
|
os_delete_task(test_init_handle);
|
|
dprintf("task 1 running....\n");
|
|
os_delay(1000);
|
|
}
|
|
|
|
}
|
|
|
|
int32_t iot_task_init()
|
|
{
|
|
/* start plc lib task */
|
|
test_init_handle = os_create_task(iot_task_1, NULL, 9);
|
|
|
|
//create the tasks;
|
|
if(test_init_handle != NULL) {
|
|
dprintf("task 1 init successfully...\n");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int32_t iot_task_start()
|
|
{
|
|
//start the tasks;
|
|
os_start_kernel();
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void cache_init()
|
|
{
|
|
cache_enable(AHB_CACHE_D0);
|
|
cache_set_buffer_mode(AHB_CACHE_D0, 1);
|
|
cache_enable(AHB_CACHE_D1);
|
|
cache_set_buffer_mode(AHB_CACHE_D1, 1);
|
|
}
|
|
|
|
static int32_t iot_platform_init()
|
|
{
|
|
cache_init();
|
|
/*platform intialization*/
|
|
platform_init();
|
|
|
|
//resource initializations;
|
|
system_clock_init();
|
|
|
|
system_uart_init();
|
|
|
|
dbg_uart_init();
|
|
|
|
dbg_uart_stage1_init();
|
|
|
|
/* rtc init, idle used rtc lock */
|
|
iot_rtc_init();
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int32_t iot_module_init(void)
|
|
{
|
|
//platform intialization;
|
|
// 在这里面重启的
|
|
iot_platform_init();
|
|
|
|
//create all the tasks;
|
|
iot_task_init();
|
|
|
|
dprintf("starting...\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
int32_t iot_module_start(void)
|
|
{
|
|
int32_t res = 0;
|
|
|
|
res = iot_task_start();
|
|
|
|
return res;
|
|
}
|
|
#include "apb_dma.h"
|
|
int main(void)
|
|
{
|
|
//module init;
|
|
// 这个函数里产生了重启
|
|
iot_module_init();
|
|
iot_module_start();
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
extern int uart_e_putc(int port, char c);
|
|
int puts(const char* str, size_t len) {
|
|
if (len == 0) {
|
|
len = strlen(str);
|
|
}
|
|
for (int i = 0;i < len;i++) {
|
|
uart_e_putc(0,str[i]);
|
|
}
|
|
return len;
|
|
}
|
|
*/ |