368 lines
		
	
	
		
			8.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			368 lines
		
	
	
		
			8.4 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_plc_lib.h"
 | ||
|  | #include "iot_dbglog_api.h"
 | ||
|  | #include "iot_config.h"
 | ||
|  | 
 | ||
|  | /* driver includes */ | ||
|  | #include "iot_clock.h"
 | ||
|  | #include "iot_uart.h"
 | ||
|  | 
 | ||
|  | /* cli includes */ | ||
|  | #include "iot_cli.h"
 | ||
|  | #include "iot_uart_h.h"
 | ||
|  | 
 | ||
|  | /* debug includes*/ | ||
|  | #include "dbg_io.h"
 | ||
|  | 
 | ||
|  | #include "uart.h"
 | ||
|  | #include "apb_dma.h"
 | ||
|  | extern int platform_init(); | ||
|  | 
 | ||
|  | os_task_h test_init_handle; | ||
|  | 
 | ||
|  | static const iot_pkt_config_t test_pkt_config = | ||
|  | { | ||
|  |     { | ||
|  |         { | ||
|  |             256, | ||
|  |             50, | ||
|  |             PKT_OWNER_ALL, | ||
|  |         }, | ||
|  |         { | ||
|  |             600, | ||
|  |             50, | ||
|  |             PKT_OWNER_ALL, | ||
|  |             }, | ||
|  |         { | ||
|  |             1100, | ||
|  |             15, | ||
|  |             PKT_OWNER_ALL, | ||
|  |         }, | ||
|  |         { | ||
|  |             2200, | ||
|  |             6, | ||
|  |             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 == DMA_DEV_SPI0
 | ||
|  | #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 = { | ||
|  |         SIG_TYPE_SPI, | ||
|  |         { | ||
|  |             {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 ; | ||
|  | } | ||
|  | 
 | ||
|  | void dma_test(void) | ||
|  | { | ||
|  |     int cnt; | ||
|  |     desc_t *rx, *tx; | ||
|  |     iot_pkt_t **pkt; | ||
|  | 
 | ||
|  | #if DMA_TEST_DEVICE == DMA_DEV_SPI0
 | ||
|  |     sdma_cfg dma_cfg; | ||
|  |     dma_spi_gpio_config(); | ||
|  |     (void)iot_spi_module_init(); | ||
|  |     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 0
 | ||
|  |     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); | ||
|  | #else
 | ||
|  |     pdesc_rx = (desc_t*)0xFF0000; | ||
|  |     pdesc_tx = (desc_t*)0xFF8000; | ||
|  | #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); | ||
|  |         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); | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | 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); | ||
|  | 
 | ||
|  |     dma_test(); | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | void iot_task_1(void *arg) | ||
|  | { | ||
|  |     iot_printf("task 1 entry....\n"); | ||
|  | 
 | ||
|  |     for(;;) { | ||
|  |        test_init(); | ||
|  |        os_delete_task(test_init_handle); | ||
|  |     } | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | 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 1 init successfully...\n"); | ||
|  |     } | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | int32_t iot_task_start() | ||
|  | { | ||
|  |     //start the tasks;
 | ||
|  |     os_start_kernel(); | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | static int32_t iot_platform_init() | ||
|  | { | ||
|  |     /*platform intialization*/ | ||
|  |     platform_init(); | ||
|  | 
 | ||
|  |     //resource initializations;
 | ||
|  |     system_clock_init(); | ||
|  | 
 | ||
|  |     system_uart_init(); | ||
|  | 
 | ||
|  |     dbg_uart_init(); | ||
|  | 
 | ||
|  |     dbg_uart_stage1_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; | ||
|  | } |