初始提交
This commit is contained in:
63
dtest/dtest3/kl3_os_spi_test/Makefile
Normal file
63
dtest/dtest3/kl3_os_spi_test/Makefile
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
# OUTPUT type
|
||||
# 1 - .out
|
||||
# 2 - .a
|
||||
# 3 - .so
|
||||
OUTPUT_TYPE = 1
|
||||
OUTPUT_NAME = kl3_os_spi_test
|
||||
|
||||
SUB_DIRS = $(TOPDIR)/dtest/dtest3/common
|
||||
|
||||
# .h files dir
|
||||
ADD_INCLUDE += $(TOPDIR)/plc/halphy/inc $(TOPDIR)/inc/compiler/gcc $(TOPDIR)/inc $(TOPDIR)/inc/io_lib $(TOPDIR)/inc/driver $(TOPDIR)/inc/uart $(TOPDIR)/inc/plc_lib $(TOPDIR)/inc/ipc $(TOPDIR)/inc/os_shim $(TOPDIR)/inc/pkt $(TOPDIR)/inc/utils $(TOPDIR)/plc/inc $(TOPDIR)/inc/cli $(TOPDIR)/cli/communicator $(TOPDIR)/inc/dbglog $(TOPDIR)/inc/ftm $(TOPDIR)/plc/halmac/inc $(TOPDIR)/plc/halmac/hw/inc/desc $(TOPDIR)/driver/inc $(TOPDIR)/driver/inc/pib $(TOPDIR)/dtest/dtest3/common
|
||||
|
||||
# predefined macro
|
||||
PRE_MARCO += _MODULE_NAME_=\"SPI\" _MODULE_VERSION_=\"V1.0.0\"
|
||||
|
||||
LD_SCRIPT = link_soc.lds
|
||||
OPT_FLAG = -O0
|
||||
|
||||
ifeq ($(gcc), arm)
|
||||
ADD_INCLUDE += $(TOPDIR)/os/freertos/src/portable/ARM_CM3
|
||||
else
|
||||
ADD_INCLUDE += $(TOPDIR)/os/freertos/src/portable/RISCV
|
||||
endif
|
||||
|
||||
ifeq ($(gcc),arm)
|
||||
ADD_LIB = cm3
|
||||
ADD_LIBDIR = $(TOPDIR)/startup/cm3
|
||||
else
|
||||
ifeq ($(target), kunlun3)
|
||||
ADD_LIB = riscv
|
||||
ADD_LIBDIR =$(TOPDIR)/startup/riscv3
|
||||
ADD_INCLUDE += $(TOPDIR)/driver/src/hw3/inc
|
||||
endif
|
||||
endif
|
||||
|
||||
# lib dir
|
||||
ADD_LIBDIR += $(TOPDIR)/plc $(TOPDIR)/driver $(TOPDIR)/common $(TOPDIR)/os $(TOPDIR)/pib
|
||||
# lib need to ld together
|
||||
ADD_LIB += os driver common pib
|
||||
|
||||
#####################################################
|
||||
|
||||
ifdef TOPDIR
|
||||
include $(TOPDIR)/build/makefile.cfg
|
||||
else
|
||||
include $(CURDIR)/build/makefile.cfg
|
||||
TOPDIR = $(CURDIR)
|
||||
export TOPDIR
|
||||
endif
|
||||
|
||||
dump:
|
||||
$(OBJDUMP) -D -S -l $(OUTPUT_FULL_NAME) > $(OUTPUT_FULL_NAME).dump
|
||||
|
||||
# display the obj files and output name
|
||||
debug:
|
||||
@echo TOPDIR=$(TOPDIR)
|
||||
@echo OUTPUT_LIB=$(OUTPUT_FULL_NAME)
|
||||
@echo DEPS=$(DEPS)
|
||||
@echo OBJECTS=$(OBJECTS)
|
||||
@echo SRCS=$(SRCS)
|
||||
@echo OBJECTS folder=$(foreach dirname, $(SUB_DIRS), $(addprefix $(BIN_DIR)/, $(dirname)))
|
||||
@echo output_name=$(OUTPUT_FULL_NAME)
|
848
dtest/dtest3/kl3_os_spi_test/kl3_os_spi_test.c
Normal file
848
dtest/dtest3/kl3_os_spi_test/kl3_os_spi_test.c
Normal file
@@ -0,0 +1,848 @@
|
||||
/****************************************************************************
|
||||
|
||||
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"
|
||||
#include "spi.h"
|
||||
#include "iot_gptmr.h"
|
||||
#include "gp_timer.h"
|
||||
#include "iot_gpio.h"
|
||||
#include "dtest_printf.h"
|
||||
#include "iot_share_task.h"
|
||||
#include "uart.h"
|
||||
#include "apb.h"
|
||||
#include "irq.h"
|
||||
#include "rtc.h"
|
||||
#include "ahb.h"
|
||||
|
||||
extern struct uart_ctrl uart_e_ctrl;
|
||||
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,
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
desc_t *pdesc_rx=NULL;
|
||||
desc_t *pdesc_tx=NULL;
|
||||
desc_t *pdesc_rx_end=NULL;
|
||||
desc_t *pdesc_tx_end=NULL;
|
||||
#define TEST_DESC_NUM 10 /* RX = TX = TEST_DESC_NUM */
|
||||
#define TEST_BUF_SIZE 64 /* Each buffer has this size */
|
||||
|
||||
#define DMA_TEST_DEVICE DMA_DEV_UART0// DMA_DEV_SPI0
|
||||
|
||||
#if DMA_TEST_DEVICE == 4
|
||||
#include "iot_spi_api.h"
|
||||
#include "apb.h"
|
||||
#include "gpio_mtx.h"
|
||||
|
||||
void dma_spi_gpio_config()
|
||||
{
|
||||
gpio_mtx_enable();
|
||||
/* 10<->clk, 11<-> CS, 15<->MOSI, 12<->MISO */
|
||||
gpio_sig_info_t info1 = {
|
||||
4,
|
||||
{
|
||||
{IO_TYPE_OUT, 0, 10, 0xff, 20},
|
||||
{IO_TYPE_OUT, 0, 11, 0xff, 21},
|
||||
{IO_TYPE_OUT, 0, 15, 0xff, 22},
|
||||
{IO_TYPE_IN, 0, 12, 16, 0xff}
|
||||
}
|
||||
};
|
||||
gpio_module_pin_select(&info1);
|
||||
gpio_module_sig_select(&info1, GPIO_MTX_MODE_MATRIX);
|
||||
}
|
||||
|
||||
#endif
|
||||
void dma_desc_tail_exchange(desc_t*desc0, desc_t*desc1)
|
||||
{
|
||||
volatile unsigned int tail;
|
||||
tail = desc0->tail_lable[0];
|
||||
desc0->tail_lable[0] = desc1->tail_lable[0];
|
||||
desc1->tail_lable[0] = tail;
|
||||
}
|
||||
int int_stack[0x20];
|
||||
void dma_uart_handler(int device, int status)
|
||||
{
|
||||
desc_t *st, *end, *pnt;
|
||||
iot_pkt_t **pkt;
|
||||
static int int_stack_index=0;
|
||||
|
||||
int_stack[(int_stack_index++)&0x1F] = status;
|
||||
|
||||
if(DMA_INT_IN_SUC_EOF&status)
|
||||
{
|
||||
end = st = pdesc_rx_end;
|
||||
|
||||
while((end->n_ptr != st) && (end->n_ptr->owner != DESC_OWNER_DMA)) end = end->n_ptr;
|
||||
|
||||
pdesc_rx_end = end->n_ptr;
|
||||
pnt = pdesc_tx;
|
||||
while(st != end->n_ptr)
|
||||
{
|
||||
/* prepare pkt before exchange */
|
||||
pkt = (iot_pkt_t**)EXTEN_POINTER(st);
|
||||
iot_pkt_set_tail(*pkt, (*pkt)->data + st->length);
|
||||
|
||||
pkt = (iot_pkt_t**)EXTEN_POINTER(pnt);
|
||||
iot_pkt_set_data(*pkt, (*pkt)->head);
|
||||
iot_pkt_set_tail(*pkt, (*pkt)->head);
|
||||
|
||||
dma_desc_tail_exchange(pnt, st);
|
||||
|
||||
pkt = (iot_pkt_t**)EXTEN_POINTER(st);
|
||||
DMA_MAKE_DESC(st, (*pkt)->data, TEST_BUF_SIZE, 0, 0, 0, 0, DESC_OWNER_DMA);
|
||||
|
||||
/* send pkt recieved */
|
||||
pkt = (iot_pkt_t**)EXTEN_POINTER(pnt);
|
||||
DMA_MAKE_DESC(pnt, (*pkt)->data, TEST_BUF_SIZE, iot_pkt_block_len(*pkt, IOT_PKT_BLOCK_DATA),\
|
||||
0, 0, 0, DESC_OWNER_DMA);
|
||||
|
||||
st = st->n_ptr;
|
||||
pnt = pnt->n_ptr;
|
||||
}
|
||||
|
||||
end = pnt->l_ptr;
|
||||
end->n_ptr = NULL;
|
||||
pdesc_tx_end->n_ptr = pdesc_tx;
|
||||
if(DESC_OWNER_CPU == pdesc_tx_end->owner)
|
||||
{
|
||||
dma_hw_start_send(DMA_TEST_DEVICE, pdesc_tx);
|
||||
}
|
||||
pdesc_tx = pnt;
|
||||
pdesc_tx_end = end;
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
#if 0
|
||||
void dma_test(void)
|
||||
{
|
||||
int cnt;
|
||||
desc_t *rx, *tx;
|
||||
iot_pkt_t **pkt;
|
||||
|
||||
dma_hw_init(NULL);
|
||||
|
||||
#if(DMA_TEST_DEVICE == 4)
|
||||
sdma_cfg dma_cfg;
|
||||
dma_spi_gpio_config();
|
||||
(void)iot_spi_module_init(NULL);
|
||||
dma_cfg.rx_enable =1;
|
||||
dma_cfg.tx_enable =1;
|
||||
dma_cfg.rx_datalevel = 8;
|
||||
dma_cfg.tx_datalevel = 8;
|
||||
(void)iot_spi_dev_register_detail(DEVICE_SPI0_MASTER, NULL, NULL,
|
||||
&dma_cfg, NULL, 0x0);
|
||||
#endif
|
||||
|
||||
const int desc_size = sizeof(desc_t) + sizeof(iot_pkt_t*);
|
||||
#if 1
|
||||
pdesc_rx = (desc_t*)os_mem_malloc(0, TEST_DESC_NUM*desc_size);
|
||||
pdesc_tx = (desc_t*)os_mem_malloc(0, TEST_DESC_NUM*desc_size);
|
||||
#endif
|
||||
|
||||
if(NULL == pdesc_tx
|
||||
||NULL == pdesc_rx)
|
||||
{
|
||||
if(pdesc_tx)
|
||||
os_mem_free(pdesc_tx);
|
||||
pdesc_tx = NULL;
|
||||
return ;
|
||||
}
|
||||
|
||||
(void)dma_hw_open(DMA_TEST_DEVICE, (dma_int_handler)dma_uart_handler, NULL);
|
||||
|
||||
rx = pdesc_rx;
|
||||
tx = pdesc_tx;
|
||||
|
||||
/* To fill descriptors */
|
||||
for(cnt=0; cnt<TEST_DESC_NUM; cnt++)
|
||||
{
|
||||
/* for RX */
|
||||
pkt = (iot_pkt_t**)EXTEN_POINTER(rx);
|
||||
*pkt = iot_pkt_alloc(TEST_BUF_SIZE+4, IOT_DRIVER_MID);
|
||||
DMA_MAKE_DESC(rx, (*pkt)->data, TEST_BUF_SIZE, 0, 0, 0, 0, DESC_OWNER_DMA);
|
||||
os_mem_set((*pkt)->data, 'a'+cnt, TEST_BUF_SIZE);
|
||||
rx->n_ptr = (desc_t*)((int)rx + desc_size);
|
||||
rx->l_ptr = (desc_t*)((int)rx - desc_size);
|
||||
/* for TX */
|
||||
pkt = (iot_pkt_t**)EXTEN_POINTER(tx);
|
||||
*pkt = iot_pkt_alloc(TEST_BUF_SIZE+4, IOT_DRIVER_MID);
|
||||
DMA_MAKE_DESC(tx, (*pkt)->data, TEST_BUF_SIZE, TEST_BUF_SIZE, 0, 0, 0, DESC_OWNER_DMA);
|
||||
DMA_INTR_ENA(tx);
|
||||
os_mem_set((*pkt)->data, 'A'+cnt, TEST_BUF_SIZE);
|
||||
tx->n_ptr = (desc_t*)((int)tx + desc_size);
|
||||
tx->l_ptr = (desc_t*)((int)tx - desc_size);
|
||||
|
||||
rx = (desc_t*)((int)rx + desc_size);
|
||||
tx = (desc_t*)((int)tx + desc_size);
|
||||
}
|
||||
rx = (desc_t*)((int)pdesc_rx + desc_size*(TEST_DESC_NUM-1));
|
||||
tx = (desc_t*)((int)pdesc_tx + desc_size*(TEST_DESC_NUM-1));
|
||||
|
||||
pdesc_rx->l_ptr = rx;
|
||||
rx->n_ptr = pdesc_rx;
|
||||
pdesc_tx->l_ptr = tx;
|
||||
tx->n_ptr = NULL;
|
||||
|
||||
pdesc_tx_end = tx;
|
||||
pdesc_rx_end = pdesc_rx;
|
||||
|
||||
dma_hw_start_recieve(DMA_TEST_DEVICE, pdesc_rx);
|
||||
dma_hw_start_send(DMA_TEST_DEVICE, pdesc_tx);
|
||||
|
||||
}
|
||||
#endif
|
||||
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);
|
||||
|
||||
/* gp timer init */
|
||||
iot_gp_timer_init();
|
||||
|
||||
/* share task init */
|
||||
iot_share_task_init();
|
||||
|
||||
/* gpio init */
|
||||
iot_gpio_module_init();
|
||||
//dma_test();
|
||||
}
|
||||
|
||||
/* rx full */
|
||||
#define RXFIM_MASK 0x00000010
|
||||
/* rx overflow */
|
||||
#define RXOIM_MASK 0x00000008
|
||||
/* rx underflow */
|
||||
#define RXUIM_MASK 0x00000004
|
||||
/* tx fifo overflow */
|
||||
#define TXOIM_MASK 0x00000002
|
||||
/* tx fifo empty */
|
||||
#define TXEIM_MASK 0x00000001
|
||||
#define SPI_TEST_PORT DEVICE_SPI2_MASTER
|
||||
|
||||
struct spi_interrupt_obj
|
||||
{
|
||||
int int_mask;
|
||||
int_isr isr;
|
||||
iot_irq_t handle;
|
||||
spi_cfg cfg;
|
||||
iot_spi_gpio_sel_t gpio;
|
||||
uint8_t used;
|
||||
};
|
||||
|
||||
static volatile int interrupt_status = 0;
|
||||
static volatile int interrupt_cnt = 0;
|
||||
static char buf_for_isr[32];
|
||||
static volatile uint32_t timer_isr1_cnt = 0;
|
||||
static volatile uint32_t timer_isr2_cnt = 0;
|
||||
static volatile uint32_t gpio_isr_cnt = 0;
|
||||
extern volatile iot_interrupt g_intr_info[128];
|
||||
extern struct spi_interrupt_obj spi_obj[DEVICE_SPI_CNT];
|
||||
|
||||
void spi_isr_test(int status)
|
||||
{
|
||||
int read_cnt;
|
||||
interrupt_status = status;
|
||||
interrupt_cnt++;
|
||||
(void)read_cnt;
|
||||
if (status & RXFIM_MASK) {
|
||||
dw_ssi_ctrl.set_int_disable(SPI_TEST_PORT, RXFIM_MASK);
|
||||
read_cnt = iot_spi_read_data(SPI_TEST_PORT, buf_for_isr, 32);
|
||||
}
|
||||
if (status & RXOIM_MASK) {
|
||||
dw_ssi_ctrl.set_int_disable(SPI_TEST_PORT, RXOIM_MASK);
|
||||
read_cnt = iot_spi_read_data(SPI_TEST_PORT, buf_for_isr, 32);
|
||||
}
|
||||
if (status & RXUIM_MASK) {
|
||||
dw_ssi_ctrl.set_int_disable(SPI_TEST_PORT, RXUIM_MASK);
|
||||
}
|
||||
if (status & TXOIM_MASK) {
|
||||
dw_ssi_ctrl.set_int_disable(SPI_TEST_PORT, TXOIM_MASK);
|
||||
}
|
||||
if (status & TXEIM_MASK) {
|
||||
dw_ssi_ctrl.set_int_disable(SPI_TEST_PORT, TXEIM_MASK);
|
||||
}
|
||||
uart_e_ctrl.puts(UART_PT0, (uint8_t *)"spi_isr\n\r",
|
||||
sizeof("spi_isr\n\r"));
|
||||
}
|
||||
|
||||
uint8_t timer_isr2(uint32_t data)
|
||||
{
|
||||
timer_isr2_cnt++;
|
||||
uart_e_ctrl.puts(UART_PT0, (uint8_t *)"timer_isr2\n\r", sizeof("timer_isr2\n\r"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t timer_isr1(uint32_t data)
|
||||
{
|
||||
/* 产生 spi 中断 */
|
||||
dw_ssi_ctrl.set_int_enable(SPI_TEST_PORT, TXEIM_MASK);
|
||||
|
||||
static uint8_t gptimer_id = 0xff;
|
||||
/* 产生gpio中断 */
|
||||
iot_gpio_value_set(63, 0);
|
||||
/* 产生timer中断 */
|
||||
if (0xff == gptimer_id) {
|
||||
gptimer_id = iot_gp_timer_create(0, NULL, timer_isr2, 0);
|
||||
}
|
||||
iot_gp_timer_set(gptimer_id, 10000, 0);
|
||||
iot_gp_timer_start(gptimer_id);
|
||||
/* 等待timer中断产生 */
|
||||
while((gp_timer_get_int_status(gptimer_id) & 0x2) == 0);
|
||||
timer_isr1_cnt++;
|
||||
uart_e_ctrl.puts(UART_PT0, (uint8_t *)"timer_isr1\n\r", sizeof("timer_isr1\n\r"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gpio_isr_test(int x)
|
||||
{
|
||||
gpio_isr_cnt++;
|
||||
uart_e_ctrl.puts(UART_PT0, (uint8_t *)"gpio_isr\n\r", sizeof("gpio_isr\n\r"));
|
||||
return;
|
||||
}
|
||||
|
||||
/* 仅用于测试 */
|
||||
void iot_spi_port_close(int port)
|
||||
{
|
||||
spi_obj[port].used = 0;
|
||||
}
|
||||
|
||||
void spi_test_entry(int dev, spi_cfg * cfg, tm_cfg *tm, int is_speed, int int_mask)
|
||||
{
|
||||
#define DATA_BUF_SIZE 10
|
||||
/* spi configs */
|
||||
spi_cfg *master_basic_cfg = cfg, *slave_basic_cfg = cfg;
|
||||
tm_cfg *tm_cfg = tm;
|
||||
iot_spi_cfg_t master_gpio_cfg, slave_gpio_cfg;
|
||||
|
||||
char master_tx_buf[DATA_BUF_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
char master_rx_buf[DATA_BUF_SIZE] = {0};
|
||||
char slave_tx_buf[DATA_BUF_SIZE] = {0};
|
||||
char slave_rx_buf[DATA_BUF_SIZE] = {0};
|
||||
char inv_buf[64];
|
||||
uint32_t i = 0;
|
||||
uint32_t tx_rx_cnt;
|
||||
|
||||
master_gpio_cfg.gpio.clk = 58;
|
||||
master_gpio_cfg.gpio.cs = 59;
|
||||
master_gpio_cfg.gpio.miso = 60;
|
||||
master_gpio_cfg.gpio.mosi = 61;
|
||||
master_gpio_cfg.port = dev;
|
||||
|
||||
// slave_gpio_cfg.gpio.clk = 28;
|
||||
// slave_gpio_cfg.gpio.cs = 39;
|
||||
// slave_gpio_cfg.gpio.miso = 27;
|
||||
// slave_gpio_cfg.gpio.mosi = 26;
|
||||
slave_gpio_cfg.gpio.clk = 58;
|
||||
slave_gpio_cfg.gpio.cs = 59;
|
||||
slave_gpio_cfg.gpio.miso = 60;
|
||||
slave_gpio_cfg.gpio.mosi = 61;
|
||||
|
||||
slave_gpio_cfg.port = DEVICE_SPI0_SLAVER;
|
||||
|
||||
slave_basic_cfg->dev_type = SPI_SLAVER;
|
||||
/* 初始化,速度测试使用回环测试 */
|
||||
if (is_speed) {
|
||||
master_gpio_cfg.gpio.miso = 9;
|
||||
}
|
||||
iot_spi_dev_open(&master_gpio_cfg);
|
||||
iot_spi_dev_register_detail(master_gpio_cfg.port, master_basic_cfg,
|
||||
tm_cfg, NULL, NULL, int_mask);
|
||||
iot_spi_read_data(master_gpio_cfg.port, inv_buf, sizeof(inv_buf));
|
||||
|
||||
if (0 == is_speed) {
|
||||
iot_spi_dev_open(&slave_gpio_cfg);
|
||||
iot_spi_dev_register_detail(slave_gpio_cfg.port, slave_basic_cfg,
|
||||
tm_cfg, NULL, NULL, 0);
|
||||
iot_spi_read_data(slave_gpio_cfg.port, inv_buf, sizeof(inv_buf));
|
||||
}
|
||||
|
||||
/* master send */
|
||||
tx_rx_cnt = iot_spi_write_data(master_gpio_cfg.port , master_tx_buf, DATA_BUF_SIZE);
|
||||
iot_printf("master tx cnt:%u\n", tx_rx_cnt);
|
||||
iot_printf("master tx buf: ");
|
||||
for (i = 0; i < DATA_BUF_SIZE; i++) {
|
||||
iot_printf("%u ", master_tx_buf[i]);
|
||||
if (i + 1 == DATA_BUF_SIZE) iot_printf("\n");
|
||||
}
|
||||
|
||||
os_delay(20);
|
||||
if (is_speed) {
|
||||
tx_rx_cnt = iot_spi_read_data(master_gpio_cfg.port, master_rx_buf, DATA_BUF_SIZE);
|
||||
iot_printf("master rx cnt:%u\n", tx_rx_cnt);
|
||||
iot_printf("master rx buf: ");
|
||||
for (i = 0; i < DATA_BUF_SIZE; i++) {
|
||||
iot_printf("%u ", master_rx_buf[i]);
|
||||
if (i + 1 == DATA_BUF_SIZE) iot_printf("\n");
|
||||
}
|
||||
iot_spi_port_close(master_gpio_cfg.port);
|
||||
return;
|
||||
} else {
|
||||
iot_spi_read_data(master_gpio_cfg.port, inv_buf, DATA_BUF_SIZE);
|
||||
}
|
||||
|
||||
/* slave 读取有效数据 */
|
||||
tx_rx_cnt = iot_spi_read_data(slave_gpio_cfg.port, slave_rx_buf, DATA_BUF_SIZE);
|
||||
iot_printf("slave rx cnt:%u\n", tx_rx_cnt);
|
||||
iot_printf("slave rx buf: ");
|
||||
for (i = 0; i < DATA_BUF_SIZE; i++) {
|
||||
iot_printf("%u ", slave_rx_buf[i]);
|
||||
if (i + 1 == DATA_BUF_SIZE) iot_printf("\n");
|
||||
}
|
||||
/* master read */
|
||||
/* 数据修改过后写入待发送buff */
|
||||
for (i = 0; i < DATA_BUF_SIZE; i++) {
|
||||
slave_tx_buf[i] = slave_rx_buf[i] + 5;
|
||||
}
|
||||
iot_spi_write_data(slave_gpio_cfg.port, slave_tx_buf, DATA_BUF_SIZE);
|
||||
iot_spi_write_data(master_gpio_cfg.port , master_tx_buf, DATA_BUF_SIZE);
|
||||
os_delay(20);
|
||||
tx_rx_cnt = iot_spi_read_data(master_gpio_cfg.port, master_rx_buf, DATA_BUF_SIZE);
|
||||
iot_printf("master rx cnt: %u\n", tx_rx_cnt);
|
||||
iot_printf("master rx buf: ");
|
||||
for (i = 0; i < DATA_BUF_SIZE; i++) {
|
||||
iot_printf("%u ", master_rx_buf[i]);
|
||||
if (i + 1 == DATA_BUF_SIZE) iot_printf("\n");
|
||||
}
|
||||
/* 清除rx_fifo中的数据 */
|
||||
iot_spi_read_data(slave_gpio_cfg.port, inv_buf, sizeof(inv_buf));
|
||||
|
||||
iot_spi_port_close(master_gpio_cfg.port);
|
||||
iot_spi_port_close(DEVICE_SPI0_SLAVER);
|
||||
return;
|
||||
}
|
||||
|
||||
int spi_read_test(int dev, char *buf, int rd_size)
|
||||
{
|
||||
int rx_byte = 0, data;
|
||||
char *pnt = buf;
|
||||
|
||||
if (0 == rd_size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((rx_byte<rd_size)) {
|
||||
data = dw_ssi_ctrl.get(dev);
|
||||
rx_byte += 1;
|
||||
|
||||
if (pnt)
|
||||
*(pnt++) = (char)(data & 0xFF);
|
||||
}
|
||||
return rx_byte;
|
||||
}
|
||||
int spi_write_test(int dev, char *buf, int wt_size)
|
||||
{
|
||||
int tx_byte = 0, data = 0xffff;
|
||||
char *pnt = buf;
|
||||
|
||||
if (0 == wt_size) {
|
||||
return 0;
|
||||
}
|
||||
while ((tx_byte < wt_size)) {
|
||||
if (pnt) {
|
||||
data = (int)(*(pnt++));
|
||||
}
|
||||
|
||||
dw_ssi_ctrl.put(dev, data);
|
||||
tx_byte += 1;
|
||||
}
|
||||
|
||||
return tx_byte;
|
||||
}
|
||||
|
||||
void spi_test_int_entry(int dev, spi_cfg * cfg, tm_cfg *tm, int_isr isr, int int_mask)
|
||||
{
|
||||
/* spi configs */
|
||||
spi_cfg *master_basic_cfg = cfg, *slave_basic_cfg = cfg;
|
||||
tm_cfg *tm_cfg = tm;
|
||||
iot_spi_cfg_t master_gpio_cfg, slave_gpio_cfg;
|
||||
|
||||
master_gpio_cfg.gpio.clk = 11;
|
||||
master_gpio_cfg.gpio.cs = 12;
|
||||
master_gpio_cfg.gpio.miso = 10;
|
||||
master_gpio_cfg.gpio.mosi = 9;
|
||||
master_gpio_cfg.port = dev;
|
||||
|
||||
slave_gpio_cfg.gpio.clk = 11;
|
||||
slave_gpio_cfg.gpio.cs = 12;
|
||||
slave_gpio_cfg.gpio.miso = 10;
|
||||
slave_gpio_cfg.gpio.mosi = 9;
|
||||
|
||||
slave_gpio_cfg.port = DEVICE_SPI0_SLAVER;
|
||||
slave_basic_cfg->dev_type = SPI_SLAVER;
|
||||
/* 初始化 */
|
||||
iot_spi_dev_open(&master_gpio_cfg);
|
||||
iot_spi_dev_open(&slave_gpio_cfg);
|
||||
iot_spi_dev_register_detail(master_gpio_cfg.port, master_basic_cfg,
|
||||
tm_cfg, NULL, isr, int_mask);
|
||||
iot_spi_dev_register_detail(slave_gpio_cfg.port, slave_basic_cfg,
|
||||
tm_cfg, NULL, NULL, 0);
|
||||
|
||||
char master_tx_buf[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
char inv_buf[64] = {0};
|
||||
|
||||
/* master send */
|
||||
iot_spi_read_data(master_gpio_cfg.port, inv_buf, sizeof(inv_buf));
|
||||
iot_spi_read_data(slave_gpio_cfg.port, inv_buf, sizeof(inv_buf));
|
||||
/* tx overflow int */
|
||||
spi_write_test(master_gpio_cfg.port , master_tx_buf, 10);
|
||||
spi_write_test(master_gpio_cfg.port , master_tx_buf, 10);
|
||||
(void)master_tx_buf;
|
||||
os_delay(20);
|
||||
spi_read_test(master_gpio_cfg.port , inv_buf, 20);
|
||||
|
||||
iot_spi_port_close(master_gpio_cfg.port);
|
||||
iot_spi_port_close(DEVICE_SPI0_SLAVER);
|
||||
return;
|
||||
}
|
||||
|
||||
void spi_param_test(void)
|
||||
{
|
||||
/* speed */
|
||||
spi_cfg cfg = {
|
||||
/* master mode. */
|
||||
.dev_type = SPI_MASTER,
|
||||
/* transmit and receive. */
|
||||
.trs_mode = TMOD_TRANCIEVER,
|
||||
/* standard spi mode. */
|
||||
.frm_fmt = FRM_STD,
|
||||
/* 8 data frame size for transmiting. */
|
||||
.dfrm_sz = SPI_DFRAME_SIZE_8,
|
||||
/* slave select enable. */
|
||||
.cs_en = DEVICE_SPI_DEFAULT_CS_EN,
|
||||
/* spi clk frequency. */
|
||||
.frq = DEVICE_SPI_DEFAULT_FREQUENCY,
|
||||
.rx_thd = DEVICE_SPI_DEFAULT_RX_THRESHOULD,
|
||||
.tx_thd = DEVICE_SPI_DEFAULT_TX_THRESHOULD
|
||||
};
|
||||
tm_cfg tm = {
|
||||
/* clk polar high when spi is idle. */
|
||||
.scpol = 1,
|
||||
/* 2ND edge of clk phase. */
|
||||
.scph = 1
|
||||
};
|
||||
/* 通信速度测试 */
|
||||
|
||||
dcase_start("start ........................................ speed 100K\n");
|
||||
cfg.frq = 100 * 1000;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 1, 0);
|
||||
dcase_start("start ........................................ speed 1M\n");
|
||||
cfg.frq = 1000 * 1000;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 1, 0);
|
||||
dcase_start("start ........................................ speed 3M\n");
|
||||
cfg.frq = 3 * 1000 * 1000;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 1, 0);
|
||||
dcase_start("start ........................................ speed 5M\n");
|
||||
cfg.frq = 5 * 1000 * 1000;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 1, 0);
|
||||
dcase_start("start ........................................ speed 10M\n");
|
||||
cfg.frq = 10 * 1000 * 1000;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 1, 0);
|
||||
dcase_start("start ........................................ speed 20M\n");
|
||||
cfg.frq = 20 * 1000 * 1000;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 1, 0);
|
||||
|
||||
/* 时钟相位及和极性测试 */
|
||||
dcase_start("start ........................................ scpol 0, scph 0\n");
|
||||
cfg.frq = 100 * 1000;
|
||||
tm.scpol = 0;
|
||||
tm.scph = 0;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 0, 0);
|
||||
dcase_start("start ........................................ scpol 1, scph 0\n");
|
||||
tm.scpol = 1;
|
||||
tm.scph = 0;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 0, 0);
|
||||
dcase_start("start ........................................ scpol 0, scph 1\n");
|
||||
tm.scpol = 0;
|
||||
tm.scph = 1;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 0, 0);
|
||||
dcase_start("start ........................................ scpol 1, scph 1\n");
|
||||
tm.scpol = 1;
|
||||
tm.scph = 1;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 0, 0);
|
||||
|
||||
dcase_start("start ........................................ SPI_DFRAME_SIZE_8\n");
|
||||
cfg.dfrm_sz = SPI_DFRAME_SIZE_8;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 0, 0);
|
||||
|
||||
dcase_start("start ........................................ SPI_DFRAME_SIZE_16\n");
|
||||
cfg.dfrm_sz = SPI_DFRAME_SIZE_16;
|
||||
spi_test_entry(SPI_TEST_PORT, &cfg, &tm, 0, 0);
|
||||
}
|
||||
|
||||
void spi_interrupt_test(void)
|
||||
{
|
||||
int i;
|
||||
char *type_str[] = {"RXFIM", "RXOIM", "RXUIM", "TXOIM", "TXEIM"};
|
||||
int status_table[] = {RXFIM_MASK, RXOIM_MASK, RXUIM_MASK, TXOIM_MASK, TXEIM_MASK};
|
||||
/* speed */
|
||||
spi_cfg cfg = {
|
||||
/* master mode. */
|
||||
.dev_type = SPI_MASTER,
|
||||
/* transmit and receive. */
|
||||
.trs_mode = TMOD_TRANCIEVER,
|
||||
/* standard spi mode. */
|
||||
.frm_fmt = FRM_STD,
|
||||
/* 8 data frame size for transmiting. */
|
||||
.dfrm_sz = SPI_DFRAME_SIZE_8,
|
||||
/* slave select enable. */
|
||||
.cs_en = DEVICE_SPI_DEFAULT_CS_EN,
|
||||
/* spi clk frequency. */
|
||||
.frq = DEVICE_SPI_DEFAULT_FREQUENCY,
|
||||
.rx_thd = DEVICE_SPI_DEFAULT_RX_THRESHOULD,
|
||||
.tx_thd = DEVICE_SPI_DEFAULT_TX_THRESHOULD
|
||||
};
|
||||
tm_cfg tm = {
|
||||
/* clk polar high when spi is idle. */
|
||||
.scpol = 1,
|
||||
/* 2ND edge of clk phase. */
|
||||
.scph = 1
|
||||
};
|
||||
dcase_start("intterupt test start ..........\n");
|
||||
for (i = 0; i < 5; i++) {
|
||||
dprintf("interrupt %s TEST\n", type_str[i]);
|
||||
spi_test_int_entry(SPI_TEST_PORT, &cfg, &tm, spi_isr_test, status_table[i]);
|
||||
dprintf("intterupt status:0x%x test:%s intterupt cnt:%d\n",
|
||||
interrupt_status, interrupt_status & status_table[i] ? "OK":"ERR",
|
||||
interrupt_cnt);
|
||||
os_delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void interrupt_priority_test(void)
|
||||
{
|
||||
static uint8_t gptimer_id = 0xff;
|
||||
|
||||
for (int i = 0; i < 26; i++) {
|
||||
iot_printf("cpu:%d vector:%d priority:%d\n", g_intr_info[i].cpu,
|
||||
g_intr_info[i].vector, g_intr_info[i].priority);
|
||||
}
|
||||
iot_printf("\n");
|
||||
iot_gpio_value_set(63, 1);
|
||||
if (gptimer_id == 0xff) {
|
||||
gptimer_id = iot_gp_timer_create(0, NULL, timer_isr1, 0);
|
||||
}
|
||||
iot_gp_timer_set(gptimer_id, 100000, 0);
|
||||
iot_gp_timer_start(gptimer_id);
|
||||
}
|
||||
|
||||
extern volatile iot_interrupt g_intr_info[128];
|
||||
|
||||
void iot_task_1(void *arg)
|
||||
{
|
||||
#define INT_GPIO 62
|
||||
iot_printf("task 1 entry....\n");
|
||||
test_init();
|
||||
|
||||
iot_gpio_open_as_interrupt(INT_GPIO);
|
||||
iot_gpio_open_as_output(63);
|
||||
// iot_gpio_interrupt_config(INT_GPIO, GPIO_INT_EDGE_FALLING, gpio_isr_test, 0, 0);
|
||||
/* 直接调用gpio_interrupt_config,在中断中处理 最后一个参数 GPIO_INT_ISR,否则会在share task中处理 */
|
||||
iot_gpio_interrupt_config_detail(INT_GPIO, GPIO_INT_EDGE_FALLING, gpio_isr_test, 0, 0);
|
||||
iot_gpio_interrupt_enable(INT_GPIO, 1);
|
||||
iot_gpio_value_set(63, 1);
|
||||
|
||||
while(1) {
|
||||
dstart();
|
||||
dversion();
|
||||
spi_param_test();
|
||||
spi_interrupt_test();
|
||||
interrupt_priority_test();
|
||||
dprintf("gpio_isr_cnt:%d spi_isr_cnt:%d\n", gpio_isr_cnt, interrupt_cnt);
|
||||
// iot_printf("task 1 running....\n");
|
||||
os_delay(2000);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void iot_task_2(void *arg)
|
||||
{
|
||||
iot_printf("task 2 entry....\n");
|
||||
|
||||
for(;;) {
|
||||
// iot_printf("task 2 running....\n");
|
||||
os_delay(1000);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int32_t iot_task_init()
|
||||
{
|
||||
|
||||
os_create_task(iot_task_2, NULL, 8);
|
||||
|
||||
/* start plc lib task */
|
||||
test_init_handle = os_create_task(iot_task_1, NULL, 9);
|
||||
|
||||
//create the tasks;
|
||||
if(test_init_handle != NULL) {
|
||||
iot_printf("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();
|
||||
|
||||
iot_printf("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;
|
||||
}
|
Reference in New Issue
Block a user