378 lines
9.5 KiB
C
378 lines
9.5 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 "iot_uart_h.h"
|
||
#include "iot_dma.h"
|
||
#include "uart.h"
|
||
#include "apb_dma.h"
|
||
#include "dma_hw.h"
|
||
#include "apb_hw.h"
|
||
#include "apb.h"
|
||
#include "cpu.h"
|
||
#include "ahb.h"
|
||
#include "rtc.h"
|
||
|
||
/* debug includes*/
|
||
#include "dbg_io.h"
|
||
|
||
#define TEST_DESC_NUM 10 /* RX = TX = TEST_DESC_NUM */
|
||
#define TEST_BUF_SIZE 64 /* Each buffer has this size */
|
||
|
||
static desc_t *pdesc_rx = NULL;
|
||
static desc_t *pdesc_tx = NULL;
|
||
static desc_t *pdesc_rx_end = NULL;
|
||
static desc_t *pdesc_tx_end = NULL;
|
||
|
||
static uint32_t dma_dev_addr;
|
||
|
||
extern int platform_init();
|
||
static os_task_h test_init_handle;
|
||
static int g_uart_dma_dev;
|
||
|
||
STATUS IRAM_ATTR dma_hw_start(int dev);
|
||
STATUS IRAM_ATTR dma_hw_tx_suspend(int dev, bool_t eb);
|
||
|
||
static const iot_pkt_config_t test_pkt_config =
|
||
{
|
||
{
|
||
{
|
||
256,
|
||
25,
|
||
PKT_OWNER_ALL,
|
||
},
|
||
{
|
||
600,
|
||
25,
|
||
PKT_OWNER_ALL,
|
||
},
|
||
{
|
||
1100,
|
||
10,
|
||
PKT_OWNER_ALL,
|
||
},
|
||
{
|
||
2200,
|
||
3,
|
||
PKT_OWNER_ALL,
|
||
},
|
||
{
|
||
0,
|
||
0,
|
||
PKT_OWNER_NONE,
|
||
},
|
||
{
|
||
0,
|
||
0,
|
||
PKT_OWNER_NONE,
|
||
},
|
||
{
|
||
0,
|
||
0,
|
||
PKT_OWNER_NONE,
|
||
},
|
||
{
|
||
0,
|
||
0,
|
||
PKT_OWNER_NONE,
|
||
},
|
||
}
|
||
};
|
||
|
||
static 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;
|
||
}
|
||
|
||
void dma_uart_handler(int device, int status)
|
||
{
|
||
desc_t *st, *end, *pnt;
|
||
iot_pkt_t **pkt;
|
||
|
||
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);
|
||
DMA_SET_DESC_ADDR(st, dma_dev_addr, (*pkt)->data);
|
||
DMA_INTR_ENA(st);
|
||
|
||
/* 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);
|
||
DMA_SET_DESC_ADDR(pnt, (*pkt)->data, dma_dev_addr);
|
||
DMA_INTR_ENA(pnt);
|
||
|
||
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(g_uart_dma_dev, pdesc_tx);
|
||
}
|
||
pdesc_tx = pnt;
|
||
pdesc_tx_end = end;
|
||
}
|
||
if (DMA_INT_OUT_SUSPEND & status) {
|
||
/* resume to send after 'suspend' */
|
||
dma_hw_tx_suspend(g_uart_dma_dev, 0);
|
||
}
|
||
if (DMA_INT_OUT_DONE & status) {
|
||
|
||
}
|
||
|
||
return ;
|
||
}
|
||
|
||
static void uart_dma_test(void)
|
||
{
|
||
int uart_port;
|
||
int cnt;
|
||
desc_t *rx, *tx;
|
||
iot_pkt_t **pkt;
|
||
const int desc_size = sizeof(desc_t) + sizeof(iot_pkt_t*);
|
||
|
||
if (0 == cpu_get_mhartid()) {
|
||
uart_port = 2; /* uart 2 only run in dma 0 */
|
||
g_uart_dma_dev = DMA_DEV_UART2;
|
||
} else if (1 == cpu_get_mhartid()) {
|
||
uart_port = 6; /* uart 6 only run in dma 1 */
|
||
g_uart_dma_dev = DMA_DEV_UART6;
|
||
} else {
|
||
IOT_ASSERT(0);
|
||
}
|
||
|
||
apb_enable(APB_GMTX);
|
||
g_uart_ctrl.init(uart_port);
|
||
g_uart_ctrl.config(uart_port, 115200, UART_DATA_8_BITS,
|
||
UART_STOP_BITS_1, UART_PARITY_DISABLE);
|
||
iot_uart_set_pin(uart_port, 28, 29);
|
||
|
||
(void)dma_hw_open(g_uart_dma_dev, (dma_int_handler)dma_uart_handler, NULL);
|
||
dma_dev_addr = dma_hw_get_dev_fifo_addr(g_uart_dma_dev);
|
||
|
||
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);
|
||
|
||
if (NULL == pdesc_tx || NULL == pdesc_rx) {
|
||
if (pdesc_tx) {
|
||
os_mem_free(pdesc_tx);
|
||
pdesc_tx = NULL;
|
||
}
|
||
if (pdesc_rx) {
|
||
os_mem_free(pdesc_rx);
|
||
pdesc_rx = NULL;
|
||
}
|
||
|
||
return ;
|
||
}
|
||
|
||
// first desc
|
||
rx = pdesc_rx;
|
||
tx = pdesc_tx;
|
||
|
||
/* To fill descriptors */
|
||
for (cnt = 0; cnt < TEST_DESC_NUM; cnt++) {
|
||
// fill desc
|
||
|
||
/* 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);
|
||
DMA_SET_DESC_ADDR(rx, dma_dev_addr, (*pkt)->data);
|
||
DMA_INTR_ENA(rx);
|
||
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);
|
||
// next desc
|
||
rx = (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);
|
||
/* test pause */
|
||
if (cnt == 3) {
|
||
DMA_PAUSE_ENA(tx, 1);
|
||
}
|
||
DMA_SET_DESC_ADDR(tx, (*pkt)->data, dma_dev_addr);
|
||
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);
|
||
// next desc
|
||
tx = (desc_t*)((int)tx + desc_size);
|
||
}
|
||
// last desc
|
||
rx = (desc_t*)((int)pdesc_rx + desc_size * (TEST_DESC_NUM - 1));
|
||
tx = (desc_t*)((int)pdesc_tx + desc_size * (TEST_DESC_NUM - 1));
|
||
|
||
// desc link roll,环接收
|
||
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(g_uart_dma_dev, pdesc_rx);
|
||
dma_hw_start_send(g_uart_dma_dev, pdesc_tx);
|
||
os_delay(1000);
|
||
/* continue to send after 'pause' */
|
||
iot_printf("dma test desc pause\r\n");
|
||
dma_hw_start(g_uart_dma_dev);
|
||
for (volatile int i = 0; i < 100; i++);
|
||
dma_hw_tx_suspend(g_uart_dma_dev, 1);
|
||
iot_printf("dma test desc suspend\r\n");
|
||
|
||
iot_printf("uart[%d] dma open ok...\n", uart_port);
|
||
}
|
||
|
||
static void iot_sub_system_init()
|
||
{
|
||
/* init common modules */
|
||
iot_bitops_init();
|
||
|
||
/* init os related modules and utilities */
|
||
os_utils_init();
|
||
|
||
/* init pkt module */
|
||
iot_pkt_init(&test_pkt_config);
|
||
|
||
/* init ipc module */
|
||
iot_ipc_init();
|
||
|
||
/* init dma module */
|
||
iot_dma_init();
|
||
}
|
||
|
||
static void iot_task_1(void *arg)
|
||
{
|
||
iot_printf("\ntest:dma need read n * 64 bytes, then send it back!\n");
|
||
iot_sub_system_init();
|
||
|
||
uart_dma_test();
|
||
for (;;) {
|
||
os_delay(1000);
|
||
os_delete_task(test_init_handle);
|
||
}
|
||
}
|
||
|
||
static 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) {
|
||
iot_printf("task create successfully...\n");
|
||
}
|
||
|
||
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();
|
||
|
||
iot_printf("iot platform init successfully...\n");
|
||
|
||
return 0;
|
||
}
|
||
|
||
static int32_t iot_task_start()
|
||
{
|
||
//start the tasks;
|
||
os_start_kernel();
|
||
|
||
return 0;
|
||
}
|
||
|
||
int main(void)
|
||
{
|
||
//platform intialization;
|
||
iot_platform_init();
|
||
|
||
//create all the tasks;
|
||
iot_task_init();
|
||
|
||
iot_task_start();
|
||
|
||
return 0;
|
||
}
|