426 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			426 lines
		
	
	
		
			10 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"
 | ||
|  | 
 | ||
|  | extern int platform_init(); | ||
|  | extern void uart_e_set_rx_idle(int port, uint32_t br); | ||
|  | extern void uart_dma_init(int port, int br); | ||
|  | extern void uart_dma_read(uint8_t *bufptr, uint32_t size, void (*callback) (void*, uint32_t), void* dummy, uint8_t pause); | ||
|  | extern void uart_dma_write(uint8_t *bufptr, uint32_t size, void (*callback) (void*, uint32_t), void* dummy); | ||
|  | extern void uart_dma_finish_transfers(int port); | ||
|  | extern void  dma_hw_stop_recieve(int dev); | ||
|  | os_task_h test_init_handle; | ||
|  | 
 | ||
|  | 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_UART1//  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; | ||
|  | } | ||
|  | 
 | ||
|  | void rx_callback(char *buffer) | ||
|  | { | ||
|  | iot_printf("RX 0x%x  %c\r\n",buffer, buffer[0]); | ||
|  | } | ||
|  | 
 | ||
|  | void Tx_callback(char *buffer) | ||
|  | { | ||
|  | iot_printf("TX 0x%x  %c\r\n",buffer, buffer[0]); | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | int int_stack[0x20]; | ||
|  | void dma_uart_handler(int device, int status) | ||
|  | { | ||
|  |     desc_t *st, *end, *pnt; | ||
|  |     static int int_stack_index=0; | ||
|  | 
 | ||
|  |     int_stack[(int_stack_index++)&0x1F] = status; | ||
|  | 
 | ||
|  |     if(0x1ff&status)//DMA_INT_IN_SUC_EOF
 | ||
|  |     { | ||
|  |         if(status&0x3f) | ||
|  |         { | ||
|  |            end = st = pdesc_rx_end; | ||
|  | 
 | ||
|  |            while((end->n_ptr != st) && (end->n_ptr->owner != DESC_OWNER_DMA)) | ||
|  |            { | ||
|  | 
 | ||
|  |               rx_callback((char *)end->buf); | ||
|  |                 DMA_MAKE_DESC(end, end->buf, TEST_BUF_SIZE, TEST_BUF_SIZE, 0, 0, 0, DESC_OWNER_DMA); | ||
|  |               end = end->n_ptr; | ||
|  |            } | ||
|  |            pdesc_rx_end = end->n_ptr; | ||
|  |           if(end->n_ptr == st) | ||
|  |              dma_hw_start_recieve(DMA_TEST_DEVICE, pdesc_rx_end); | ||
|  |         } | ||
|  | 
 | ||
|  |         if(status&0x1c0) | ||
|  |         { | ||
|  |             pnt = pdesc_tx; | ||
|  |             while ((pnt->owner != DESC_OWNER_DMA) ) | ||
|  |             { | ||
|  |                Tx_callback((char *)pnt->buf); | ||
|  |                pnt = pnt->n_ptr; | ||
|  |                if(pnt==NULL) | ||
|  |                   break; | ||
|  |             } | ||
|  |             if(pnt == NULL) | ||
|  |             { | ||
|  |                 dma_hw_start_send(DMA_TEST_DEVICE, pdesc_tx); | ||
|  |             } | ||
|  |        } | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  |     return ; | ||
|  | } | ||
|  | 
 | ||
|  | void dma_test(void) | ||
|  | { | ||
|  |     int cnt; | ||
|  |     desc_t *rx, *tx; | ||
|  |     //iot_pkt_t **pkt;
 | ||
|  |     uint8_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 = (uint8_t*)os_mem_malloc(IOT_DRIVER_MID, TEST_BUF_SIZE);  // *pkt = iot_pkt_alloc(TEST_BUF_SIZE+4, IOT_DRIVER_MID);
 | ||
|  |         DMA_MAKE_DESC(rx, pkt, TEST_BUF_SIZE, 0, 0, 0, 0, DESC_OWNER_DMA); | ||
|  |         os_mem_set(pkt, '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 = (uint8_t*)os_mem_malloc(IOT_DRIVER_MID, TEST_BUF_SIZE); //*pkt = iot_pkt_alloc(TEST_BUF_SIZE+4, IOT_DRIVER_MID);
 | ||
|  |         DMA_MAKE_DESC(tx, pkt, TEST_BUF_SIZE, TEST_BUF_SIZE, 0, 0, 0, DESC_OWNER_DMA); | ||
|  |         DMA_INTR_ENA(tx); | ||
|  |         os_mem_set(pkt, '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); | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t *r_buf=NULL, *w_buf=NULL; | ||
|  | 
 | ||
|  | uint8_t r_bufptr[100]={0}; | ||
|  | uint8_t w_bufptr[1200]={0};//{'F','I','N','I','S','H','!','f','i','n','i','s','h','!','\n'};
 | ||
|  | uint8_t tick=1; | ||
|  | uint8_t seq=0; | ||
|  | uint32_t hdr=0; | ||
|  | uint8_t a='O', b='K', c='!'; | ||
|  | uint8_t ping_pong = 0; | ||
|  | uint32_t cnt=0; | ||
|  | void write_t(void* p, uint32_t t) | ||
|  | { | ||
|  |    // iot_printf("WR 0x%x, %d \r\n", *(char *)p, t);
 | ||
|  | } | ||
|  | 
 | ||
|  | void read_t(void* p, uint32_t t) | ||
|  | { | ||
|  |     uint32_t tmp_num=0; | ||
|  |     uint8_t  pause=1; | ||
|  |     uint8_t i=0; | ||
|  |     //static uint8_t cnt=0;
 | ||
|  |     //iot_printf("int: ");
 | ||
|  | 
 | ||
|  |    if(seq == 0 ) | ||
|  |     { | ||
|  |       tmp_num=3; | ||
|  |       uart_dma_read(&r_buf[0x10], tmp_num, read_t, 0, pause); | ||
|  |       //iot_printf("%c    , %d\r\n",*(char *)p,t);
 | ||
|  |       //uart_e_set_rx_idle(1,0x10);
 | ||
|  |       hdr = *(char *)p; | ||
|  |       //cnt=0;
 | ||
|  | 
 | ||
|  |       while (i<t) | ||
|  |       { | ||
|  |           w_bufptr[cnt]=*(char *)(p+i); | ||
|  |           cnt++; | ||
|  |           i++; | ||
|  |       } | ||
|  | 
 | ||
|  |       tmp_num=3; | ||
|  |       seq=1; | ||
|  |       uart_dma_write(&w_bufptr[0], t, write_t, 0); //  iot_printf("RD 0x%x, %d \r\n", *(char *)p, t);
 | ||
|  |     }else if(seq==1){ | ||
|  |       tmp_num=16; | ||
|  |       pause=0; | ||
|  |       uart_dma_read(&r_buf[0x20], tmp_num, read_t, 0, pause); | ||
|  |       //iot_printf("%c,%c,%c, %d\r\n",*(char *)p, *(char *)(p+1), *(char *)(p+2), t);
 | ||
|  |       //uart_e_set_rx_idle(1,0x100);
 | ||
|  |       tmp_num=16; | ||
|  |       //iot_printf("Rx BEF HDR 0x%x \r\n", hdr);
 | ||
|  |       hdr = (hdr << 24) | (*(char *)p << 16) | ((*(char *)(p+1)) << 8) | *(char *)(p+2); | ||
|  |       while (i<t) | ||
|  |       { | ||
|  |           w_bufptr[cnt]=*(char *)(p+i); | ||
|  |           cnt++; | ||
|  |           i++; | ||
|  |       } | ||
|  |       seq = 2; | ||
|  |       //iot_printf("Rx AFT HDR 0x%x \r\n", hdr);
 | ||
|  |       uart_dma_write(&w_bufptr[cnt-i], t, write_t, 0);//iot_printf("RD 0x%x, %d \r\n", hdr, t);
 | ||
|  |     }else if(seq==2) | ||
|  |     { | ||
|  |       //iot_printf("Rx HDR 0x%x, %c--%c,%c,%c,%c %d\r\n",hdr, *(char *)p, *(char *)(p+6), *(char *)(p+7), *(char *)(p+8), *(char *)(p+9), t);
 | ||
|  |       hdr=0; | ||
|  |       tmp_num=1; | ||
|  |       uart_dma_read(&r_buf[0x10], tmp_num, read_t, 0, pause); | ||
|  |       seq=0; | ||
|  |       tick=0; | ||
|  |       while (i<t) | ||
|  |       { | ||
|  |           w_bufptr[cnt]=*(char *)(p+i); | ||
|  |           cnt++; | ||
|  |           i++; | ||
|  |       } | ||
|  |       w_bufptr[cnt]= '!'; | ||
|  |       cnt++; | ||
|  | //if(cnt>=16*21)
 | ||
|  | //{
 | ||
|  |     uart_dma_write(&w_bufptr[cnt-i-1], t+1, write_t, 0); | ||
|  |     cnt=0; | ||
|  | //}
 | ||
|  |       //uart_e_set_rx_idle(1,0x10);
 | ||
|  |       //uart_dma_write(&w_bufptr[0], (cnt+1), write_t, 0);
 | ||
|  |       return; | ||
|  |     } | ||
|  |     //uart_dma_read(&r_bufptr[0], tmp_num, read_t, 0, pause);
 | ||
|  | } | ||
|  | 
 | ||
|  | void test_init() | ||
|  | { | ||
|  |     /* init common modules */ | ||
|  |     iot_bitops_init(); | ||
|  |     uart_dma_init(1,115200); | ||
|  |     //uart_e_set_rx_idle(1,0x10);
 | ||
|  |     r_buf = (uint8_t *)os_mem_malloc(UNKNOWN_MID, 0x100); | ||
|  |     w_buf = (uint8_t *)os_mem_malloc(UNKNOWN_MID, 0x100); | ||
|  |     uart_dma_read(&r_buf[0], 1, read_t, 0, 1); | ||
|  | 
 | ||
|  |     while(1) | ||
|  |     { | ||
|  |        if(tick == 0) | ||
|  |        { | ||
|  |           // uart_dma_write(&w_bufptr[0], 15, write_t, 0);
 | ||
|  |            tick=1; | ||
|  |        } | ||
|  |         os_delay(100); | ||
|  |     } | ||
|  | 
 | ||
|  |     /*init uart module*/ | ||
|  |     iot_uart_init(1); | ||
|  | 
 | ||
|  |     dma_test(); | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | void iot_task_1(void *arg) | ||
|  | { | ||
|  |     iot_printf("task 1 entry....\n"); | ||
|  | 
 | ||
|  |     for(;;) { | ||
|  |        test_init(); | ||
|  | 	   while(1) | ||
|  |        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(); | ||
|  | 
 | ||
|  |     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; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 |