diff --git a/FluxEngine.cydsn/main.c b/FluxEngine.cydsn/main.c index 5785321..5ac26c9 100644 --- a/FluxEngine.cydsn/main.c +++ b/FluxEngine.cydsn/main.c @@ -14,12 +14,13 @@ #define STEP_TOWARDS0 1 #define STEP_AWAYFROM0 0 -static uint32_t clock = 0; +static volatile uint32_t clock = 0; +static volatile bool index_irq = false; + static bool motor_on = false; static uint32_t motor_on_time = 0; static bool homed = false; static int current_track = 0; -static volatile bool index_irq = false; #if 0 static uint8_t td[BUFFER_COUNT]; @@ -107,10 +108,15 @@ CY_ISR(dma_finished_isr) } #endif +static void wait_until_writeable(int ep) +{ + while (USBFS_GetEPState(ep) != USBFS_IN_BUFFER_EMPTY) + ; +} + static void send_reply(struct any_frame* f) { - while (USBFS_GetEPState(FLUXENGINE_CMD_IN_EP_NUM) != USBFS_IN_BUFFER_EMPTY) - ; + wait_until_writeable(FLUXENGINE_CMD_IN_EP_NUM); USBFS_LoadInEP(FLUXENGINE_CMD_IN_EP_NUM, (uint8_t*) f, f->f.size); } @@ -172,23 +178,41 @@ static void cmd_measure_speed(struct any_frame* f) { start_motor(); - UART_PutString("wait for index\n"); index_irq = false; while (!index_irq) ; index_irq = false; int start_clock = clock; - UART_PutString("wait for another index\n"); while (!index_irq) ; int end_clock = clock; - UART_PutString("done\n"); DECLARE_REPLY_FRAME(struct speed_frame, F_FRAME_MEASURE_SPEED_REPLY); r.period_ms = end_clock - start_clock; send_reply((struct any_frame*) &r); } +static void cmd_bulk_test(struct any_frame* f) +{ + uint8_t buffer[64]; + + for (int x=0; x<16; x++) + for (int y=0; y<256; y++) + { + for (unsigned z=0; zperiod_ms; } +static void large_bulk_transfer(int ep, void* buffer, int total_len) +{ + int count = 0; + while (count < total_len) + { + int len = total_len - count; + int i = libusb_bulk_transfer(device, ep, + ((uint8_t*)buffer)+count, len, &len, TIMEOUT); + if (i < 0) + error("data transfer failed: %s", libusb_strerror(i)); + count += len; + } +} + +void usb_bulk_test(void) +{ + struct any_frame f = { .f = {.type = F_FRAME_BULK_TEST_CMD, .size = sizeof(f)} }; + usb_cmd_send(&f, f.f.size); + + uint8_t bulk_buffer[16*256*64]; + int total_len = sizeof(bulk_buffer); + double start_time = gettime(); + large_bulk_transfer(FLUXENGINE_DATA_IN_EP, bulk_buffer, total_len); + double elapsed_time = gettime() - start_time; + + printf("Transferred %d bytes in %d ms (%d kB/s)\n", + total_len, (int)(elapsed_time * 1000.0), + (int)((total_len / 1024.0) / elapsed_time)); + for (int x=0; x<16; x++) + for (int y=0; y<256; y++) + for (int z=0; z<64; z++) + { + int offset = x*16384 + y*64 + z; + if (bulk_buffer[offset] != (uint8_t)(x+y+z)) + error("data transfer corrupted at 0x%x (%d.%d.%d)", offset, x, y, z); + } + + await_reply(F_FRAME_BULK_TEST_REPLY); +}