366 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			366 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | #include "uart.h"
 | ||
|  | #include "uart_e.h"
 | ||
|  | #include "hw_reg_api.h"
 | ||
|  | #include "os_types.h"
 | ||
|  | #include "dbg_io.h"
 | ||
|  | #include "cpl_types.h"
 | ||
|  | #include "iot_diag.h"
 | ||
|  | #include "iot_io.h"
 | ||
|  | extern void uart_e_init(); | ||
|  | int IRAM_ATTR uart_e_getc(int port); | ||
|  | void test_uart_init() | ||
|  | { | ||
|  |     uart_e_init(0); | ||
|  | } | ||
|  |        | ||
|  | int32_t test_uart0_getc() | ||
|  | { | ||
|  |     int32_t c = 6; | ||
|  |     c = uart_e_getc(0); | ||
|  |     if (c == -6) { | ||
|  |         return -1; | ||
|  |     } | ||
|  |     //iot_printf("%d\n", c);
 | ||
|  |     return c; | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | void clear_needed_memory(uint32_t start_addr, uint32_t size) { | ||
|  |     //iot_printf("cnn_clear_needed_memory start at %08x\n", start_addr);
 | ||
|  |     //for (uint32_t i = 0; i < size; i = i + 4) {
 | ||
|  |     //    SOC_WRITE_REG(start_addr + i, 0);
 | ||
|  |     //}
 | ||
|  |     //volatile uint32_t *writer = (volatile uint32_t *)start_addr;
 | ||
|  |     //for (uint32_t i = 0; i < 1; i++) {
 | ||
|  |     //    *writer = 0;
 | ||
|  |     //}
 | ||
|  |     if (size > 0x200000) { | ||
|  |         iot_printf("clear%08x\n", size); | ||
|  |         while(1){ | ||
|  |             iot_printf("a"); | ||
|  |         } | ||
|  |     } | ||
|  |     os_mem_set((int8_t *)start_addr, 0x0, size); | ||
|  |     iot_printf("clearStart%08x\n", start_addr); | ||
|  |     iot_printf("clearSize%08x\n", size); | ||
|  |     iot_printf("clearfinished\n"); | ||
|  | } | ||
|  | 
 | ||
|  | void verify_8bits(uint32_t out_addr, uint32_t golden_addr, uint32_t length) { | ||
|  | 
 | ||
|  |     uint32_t incorrect_number = 0; | ||
|  |     uint32_t out = 0; | ||
|  |     uint32_t golden = 0; | ||
|  | 
 | ||
|  |     iot_printf("o addr: %x\n", out_addr); | ||
|  |     iot_printf("g addr: %x\n", golden_addr); | ||
|  | 
 | ||
|  |     for (uint32_t i = 0; i < length; i = i + 4) { | ||
|  |         out = SOC_READ_REG(out_addr + i); | ||
|  |         golden = SOC_READ_REG(golden_addr + i); | ||
|  |         if (out != golden) { | ||
|  |             incorrect_number++; | ||
|  |             iot_printf("n %d, g: %08x, o: %08x\n", i, golden, out); | ||
|  |         } | ||
|  |     } | ||
|  |     if (incorrect_number == 0) { | ||
|  |         iot_printf("all correct!\n"); | ||
|  |     } else { | ||
|  |         iot_printf("not all correct!\n"); | ||
|  |         //while(1){};
 | ||
|  |     } | ||
|  |     iot_printf("finished~"); | ||
|  | } | ||
|  | 
 | ||
|  | void verify_float(uint32_t out_addr, uint32_t golden_addr, uint32_t length) { | ||
|  |     uint32_t incorrect_number = 0; | ||
|  |   | ||
|  |     iot_printf("o addr: %x\n", out_addr); | ||
|  |     iot_printf("g addr: %x\n", golden_addr); | ||
|  |     uint32_t *out32 = (uint32_t *)out_addr; | ||
|  |     uint32_t *gol32 = (uint32_t *)golden_addr; | ||
|  |     float *outf = (float *)out_addr; | ||
|  |     float *golf = (float *)golden_addr; | ||
|  |     for (uint32_t i = 0; i < length; i++) { | ||
|  |         if (outf[i] != golf[i]) { | ||
|  |             incorrect_number++; | ||
|  |             iot_printf("n %d, g: %08x, o: %08x\n", i, gol32[i], out32[i]); | ||
|  |             iot_printf("n %d, ", i); | ||
|  |             iot_printf("g: %f, ", golf[i]); | ||
|  |             iot_printf("o: %f\n", outf[i]); | ||
|  |         } | ||
|  |     }  | ||
|  |     if (incorrect_number == 0) { | ||
|  |         iot_printf("all correct!\n"); | ||
|  |     } else { | ||
|  |         iot_printf("not all correct!\n"); | ||
|  |     } | ||
|  |     iot_printf("finished~"); | ||
|  | }   | ||
|  | 
 | ||
|  | void read_case_from_python(uint8_t *config_read, uint32_t *config_get, uint8_t *data_get) { | ||
|  |     volatile uint8_t *writer = data_get; | ||
|  |     int32_t c; | ||
|  |     size_t data_sz = 0; | ||
|  |     uint8_t state = 0; | ||
|  |     uint32_t destin_size = 120; | ||
|  |     for(uint32_t i = 0; i < 120; i++) { | ||
|  |         config_read[i] = 0; | ||
|  |     } | ||
|  |     while (1) { | ||
|  |         c = test_uart0_getc(0); | ||
|  |         if (c < 0) { | ||
|  |             break; | ||
|  |         } | ||
|  |     } | ||
|  |     iot_printf("send start!"); | ||
|  |     while (state < 2) { | ||
|  |         c = test_uart0_getc(0); | ||
|  |         if (c < 0) { | ||
|  |             continue; | ||
|  |         } | ||
|  |         if (state == 0) { | ||
|  |             config_read[data_sz] = (unsigned char)c; | ||
|  |             data_sz++; | ||
|  |             if (data_sz == destin_size) { | ||
|  |                 // cnn_ext_mem_dump((uint32_t)config_read, data_sz);
 | ||
|  |                 data_sz = 0; | ||
|  |                 for (uint8_t j = 0; j < 52; j++) { | ||
|  |                     config_get[j] = (config_read[2 * j + 1] << 8) + config_read[2 * j]; | ||
|  |                     // iot_printf("config_get[%d] = %04x\n", j, config_get[j]);
 | ||
|  |                 } | ||
|  |                 state = 1; | ||
|  |                 config_get[52] = config_read[104] + (config_read[105] << 8) + (config_read[106] << 16) + (config_read[107] << 24); | ||
|  |                 config_get[53] = config_read[108] + (config_read[109] << 8) + (config_read[110] << 16) + (config_read[111] << 24); | ||
|  |                 config_get[54] = config_read[112] + (config_read[113] << 8) + (config_read[114] << 16) + (config_read[115] << 24); | ||
|  |                 config_get[55] = config_read[116] + (config_read[117] << 8) + (config_read[118] << 16) + (config_read[119] << 24); | ||
|  |                 destin_size = config_get[53] + config_get[52] + config_get[55] + config_get[54]; | ||
|  |                 writer = data_get; | ||
|  |                 iot_printf("w%08x\n", config_get[52]); | ||
|  |                 iot_printf("b%08x\n", config_get[53]); | ||
|  |                 iot_printf("i%08x\n", config_get[54]); | ||
|  |                 iot_printf("o%08x\n", config_get[55]); | ||
|  |                 iot_printf("d%08x\n", destin_size); | ||
|  |                 iot_printf("get%08x\n", (uint32_t)data_get); | ||
|  |                 clear_needed_memory((uint32_t)data_get, destin_size + config_get[55]); | ||
|  |                 while (1) { | ||
|  |                     c = test_uart0_getc(0); | ||
|  |                     data_sz++; | ||
|  |                     // iot_printf("receive after: %d, this is number %d\n", c, data_sz);
 | ||
|  |                     if (c < 0) { | ||
|  |                         break; | ||
|  |                     } | ||
|  |                 } | ||
|  |                 data_sz = 0; | ||
|  |                 iot_printf("config"); | ||
|  |             } | ||
|  |         } else { | ||
|  |             (*(writer + data_sz)) = (unsigned char)c; | ||
|  |             data_sz++; | ||
|  |             if (data_sz == destin_size) { | ||
|  |                 // iot_printf("weight__\n");
 | ||
|  |                 iot_printf("destin_size %08x\n", destin_size); | ||
|  |                 iot_printf("writer addr %08x\n", (uint32_t)writer); | ||
|  |                 state = 2; | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | void read_nn_case_from_python(uint8_t *config_read, uint32_t *config_get) { | ||
|  |     volatile uint8_t *writer = (uint8_t *)0x18000000; | ||
|  |     int32_t c; | ||
|  |     size_t data_sz = 0; | ||
|  |     uint8_t state = 0; | ||
|  |     uint32_t destin_size = 120; | ||
|  |     for(uint32_t i = 0; i < 120; i++) { | ||
|  |         config_read[i] = 0; | ||
|  |     } | ||
|  |     while (1) { | ||
|  |         c = test_uart0_getc(0); | ||
|  |         if (c < 0) { | ||
|  |             break; | ||
|  |         } | ||
|  |     } | ||
|  |     iot_printf("send start!"); | ||
|  |     while (state < 2) { | ||
|  |         c = test_uart0_getc(0); | ||
|  |         if (c < 0) { | ||
|  |             continue; | ||
|  |         } | ||
|  |         if (state == 0) { | ||
|  |             config_read[data_sz] = (unsigned char)c; | ||
|  |             data_sz++; | ||
|  |             if (data_sz == destin_size) { | ||
|  |                 // cnn_ext_mem_dump((uint32_t)config_read, data_sz);
 | ||
|  |                 data_sz = 0; | ||
|  |                 for (uint8_t j = 0; j < 52; j++) { | ||
|  |                     config_get[j] = (config_read[2 * j + 1] << 8) + config_read[2 * j]; | ||
|  |                     // iot_printf("config_get[%d] = %04x\n", j, config_get[j]);
 | ||
|  |                 } | ||
|  |                 state = 1; | ||
|  |                 config_get[52] = config_read[104] + (config_read[105] << 8) + (config_read[106] << 16) + (config_read[107] << 24); | ||
|  |                 config_get[53] = config_read[108] + (config_read[109] << 8) + (config_read[110] << 16) + (config_read[111] << 24); | ||
|  |                 config_get[54] = config_read[112] + (config_read[113] << 8) + (config_read[114] << 16) + (config_read[115] << 24); | ||
|  |                 config_get[55] = config_read[116] + (config_read[117] << 8) + (config_read[118] << 16) + (config_read[119] << 24); | ||
|  |                 destin_size = config_get[53] + config_get[52] + config_get[55] + config_get[54]; | ||
|  |                 iot_printf("weight size = %08x\n", config_get[52]); | ||
|  |                 iot_printf("bias   size = %08x\n", config_get[53]); | ||
|  |                 iot_printf("input  size = %08x\n", config_get[54]); | ||
|  |                 iot_printf("output size = %08x\n", config_get[55]); | ||
|  |                 iot_printf("destin size = %08x\n", destin_size); | ||
|  |                 clear_needed_memory(0x10000000, config_get[53] + config_get[54] + 2 * config_get[55]); | ||
|  |                 clear_needed_memory(0x18000000, config_get[52]); | ||
|  |                 while (1) { | ||
|  |                     c = test_uart0_getc(0); | ||
|  |                     data_sz++; | ||
|  |                     // iot_printf("receive after: %d, this is number %d\n", c, data_sz);
 | ||
|  |                     if (c < 0) { | ||
|  |                         break; | ||
|  |                     } | ||
|  |                 } | ||
|  |                 data_sz = 0; | ||
|  |                 iot_printf("config"); | ||
|  |             } | ||
|  |         } else { | ||
|  |             *writer = (unsigned char)c; | ||
|  |             writer++; | ||
|  |             data_sz++; | ||
|  |             if (data_sz == config_get[52]) { | ||
|  |                 writer = (uint8_t *)0x10000000; | ||
|  |             } | ||
|  |             if (data_sz == destin_size) { | ||
|  |                 // iot_printf("weight__\n");
 | ||
|  |                 iot_printf("destin_size = %08x\n", destin_size); | ||
|  |                 iot_printf("writer addr = %08x\n", (uint32_t)writer); | ||
|  |                 state = 2; | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | void read_case_from_python_matrix(uint8_t *config_read, uint32_t *config_get) { | ||
|  |     volatile uint8_t *writer = (uint8_t *)0x10000000; | ||
|  |     int32_t c; | ||
|  |     size_t data_sz = 0; | ||
|  |     uint8_t state = 0; | ||
|  |     uint32_t destin_size = 120; | ||
|  |     for(uint32_t i = 0; i < 120; i++) { | ||
|  |         config_read[i] = 0; | ||
|  |     } | ||
|  |     iot_printf("send start!"); | ||
|  |     while (state < 2) { | ||
|  |         c = test_uart0_getc(0); | ||
|  |         if (c < 0) { | ||
|  |             continue; | ||
|  |         } | ||
|  |         if (state == 0) { | ||
|  |             config_read[data_sz] = (unsigned char)c; | ||
|  |             data_sz++; | ||
|  |             if (data_sz == destin_size) { | ||
|  |                 // cnn_ext_mem_dump((uint32_t)config_read, data_sz);
 | ||
|  |                 data_sz = 0; | ||
|  |                 for (uint8_t j = 0; j < 52; j++) { | ||
|  |                     config_get[j] = (config_read[2 * j + 1] << 8) + config_read[2 * j]; | ||
|  |                     // iot_printf("config_get[%d] = %04x\n", j, config_get[j]);
 | ||
|  |                 } | ||
|  |                 state = 1; | ||
|  |                 config_get[54] = config_read[112] + (config_read[113] << 8) + (config_read[114] << 16) + (config_read[115] << 24); | ||
|  |                 config_get[55] = config_read[116] + (config_read[117] << 8) + (config_read[118] << 16) + (config_read[119] << 24); | ||
|  |                 writer = (uint8_t *)0x10000000; | ||
|  |                 destin_size = config_get[54] + config_get[55]; | ||
|  |                 iot_printf("input  size = %08x\n", config_get[54]); | ||
|  |                 iot_printf("output size = %08x\n", config_get[55]); | ||
|  |                 clear_needed_memory(0x10000000, config_get[54]); | ||
|  |                 clear_needed_memory(0x18000000, 2 * config_get[55]); | ||
|  |                 while (1) { | ||
|  |                     c = test_uart0_getc(0); | ||
|  |                     data_sz++; | ||
|  |                     // iot_printf("receive after: %d, this is number %d\n", c, data_sz);
 | ||
|  |                     if (c < 0) { | ||
|  |                         break; | ||
|  |                     } | ||
|  |                 } | ||
|  |                 data_sz = 0; | ||
|  |                 iot_printf("config"); | ||
|  |             } | ||
|  |         } else { | ||
|  |             *writer = (unsigned char)c; | ||
|  |             writer++; | ||
|  |             data_sz++; | ||
|  |             if (data_sz == config_get[54]) { | ||
|  |                 writer = (uint8_t *)0x18000000; | ||
|  |             } | ||
|  |             if (data_sz == destin_size) { | ||
|  |                 // iot_printf("weight__\n");
 | ||
|  |                 iot_printf("destin_size = %08x\n", destin_size); | ||
|  |                 iot_printf("writer addr = %08x\n", (uint32_t)writer); | ||
|  |                 state = 2; | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | void read_case_from_python_matrix_multi(uint8_t *config_read, uint32_t *config_get) { | ||
|  |     volatile uint8_t *writer = (uint8_t *)0x10000000; | ||
|  |     int32_t c; | ||
|  |     size_t data_sz = 0; | ||
|  |     uint8_t state = 0; | ||
|  |     uint32_t destin_size = 120; | ||
|  |     for(uint32_t i = 0; i < 120; i++) { | ||
|  |         config_read[i] = 0; | ||
|  |     } | ||
|  |     iot_printf("send start!"); | ||
|  |     while (state < 2) { | ||
|  |         c = test_uart0_getc(0); | ||
|  |         if (c < 0) { | ||
|  |             continue; | ||
|  |         } | ||
|  |         if (state == 0) { | ||
|  |             config_read[data_sz] = (unsigned char)c; | ||
|  |             data_sz++; | ||
|  |             if (data_sz == destin_size) { | ||
|  |                 // cnn_ext_mem_dump((uint32_t)config_read, data_sz);
 | ||
|  |                 data_sz = 0; | ||
|  |                 for (uint8_t j = 0; j < 52; j++) { | ||
|  |                     config_get[j] = (config_read[2 * j + 1] << 8) + config_read[2 * j]; | ||
|  |                     // iot_printf("config_get[%d] = %04x\n", j, config_get[j]);
 | ||
|  |                 } | ||
|  |                 state = 1; | ||
|  |                 config_get[53] = config_read[108] + (config_read[109] << 8) + (config_read[110] << 16) + (config_read[111] << 24); | ||
|  |                 config_get[54] = config_read[112] + (config_read[113] << 8) + (config_read[114] << 16) + (config_read[115] << 24); | ||
|  |                 config_get[55] = config_read[116] + (config_read[117] << 8) + (config_read[118] << 16) + (config_read[119] << 24); | ||
|  |                 writer = (uint8_t *)0x10000000; | ||
|  |                 destin_size = config_get[53] + config_get[54] + config_get[55]; | ||
|  |                 iot_printf("inputA size = %08x\n", config_get[53]); | ||
|  |                 iot_printf("inputB size = %08x\n", config_get[54]); | ||
|  |                 iot_printf("output size = %08x\n", config_get[55]); | ||
|  |                 clear_needed_memory(0x10000000, config_get[53] + config_get[54]); | ||
|  |                 clear_needed_memory(0x18000000, 2 * config_get[55]); | ||
|  |                 while (1) { | ||
|  |                     c = test_uart0_getc(0); | ||
|  |                     data_sz++; | ||
|  |                     // iot_printf("receive after: %d, this is number %d\n", c, data_sz);
 | ||
|  |                     if (c < 0) { | ||
|  |                         break; | ||
|  |                     } | ||
|  |                 } | ||
|  |                 data_sz = 0; | ||
|  |                 iot_printf("config"); | ||
|  |             } | ||
|  |         } else { | ||
|  |             *writer = (unsigned char)c; | ||
|  |             writer++; | ||
|  |             data_sz++; | ||
|  |             if (data_sz == config_get[54] + config_get[53]) { | ||
|  |                 writer = (uint8_t *)0x18000000; | ||
|  |             } | ||
|  |             if (data_sz == destin_size) { | ||
|  |                 // iot_printf("weight__\n");
 | ||
|  |                 iot_printf("destin_size = %08x\n", destin_size); | ||
|  |                 iot_printf("writer addr = %08x\n", (uint32_t)writer); | ||
|  |                 state = 2; | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | } |