USBH: Update testhal project to new debugging framework

This commit is contained in:
Diego Ismirlian 2019-09-30 18:55:03 -03:00
parent 938daa12d6
commit 0c260638da
4 changed files with 115 additions and 92 deletions

View File

@ -1,6 +1,6 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
environment/project/0.1003150841/PATH/delimiter=; environment/project/0.1003150841/PATH/delimiter=;
environment/project/0.1003150841/PATH/operation=replace environment/project/0.1003150841/PATH/operation=replace
environment/project/0.1003150841/PATH/value=${PATH};D\:\\toolchains\\gcc-arm-none-eabi-8-2018-q4-major-win32\\bin;${PATH};D\:\\toolchains\\gcc-arm-none-eabi-4_9-2014q4-20141203-win32\\bin;D\:\\toolchains\\msys64\\usr\\bin environment/project/0.1003150841/PATH/value=${PATH};D\:\\toolchains\\gcc-arm-none-eabi-8-2019-q3-update-win32\\bin;D\:\\toolchains\\msys64\\usr\\bin
environment/project/0.1003150841/append=true environment/project/0.1003150841/append=true
environment/project/0.1003150841/appendContributed=true environment/project/0.1003150841/appendContributed=true

View File

@ -712,3 +712,4 @@
#endif /* CHCONF_H */ #endif /* CHCONF_H */
/** @} */ /** @} */

View File

@ -177,7 +177,7 @@
#define HAL_USBH_PORT_DEBOUNCE_TIME 200 #define HAL_USBH_PORT_DEBOUNCE_TIME 200
#define HAL_USBH_PORT_RESET_TIMEOUT 500 #define HAL_USBH_PORT_RESET_TIMEOUT 500
#define HAL_USBH_DEVICE_ADDRESS_STABILIZATION 20 #define HAL_USBH_DEVICE_ADDRESS_STABILIZATION 20
#define HAL_USBH_CONTROL_REQUEST_DEFAULT_TIMEOUT OSAL_MS2I(1000) #define HAL_USBH_CONTROL_REQUEST_DEFAULT_TIMEOUT OSAL_MS2I(1000)
/* MSD */ /* MSD */
#define HAL_USBH_USE_MSD TRUE #define HAL_USBH_USE_MSD TRUE
@ -202,7 +202,7 @@
#define HAL_USBHAOA_MAX_INSTANCES 1 #define HAL_USBHAOA_MAX_INSTANCES 1
/* Uncomment this if you need a filter for AOA devices: /* Uncomment this if you need a filter for AOA devices:
* #define HAL_USBHAOA_FILTER_CALLBACK _try_aoa * #define HAL_USBHAOA_FILTER_CALLBACK _try_aoa
*/ */
#define HAL_USBHAOA_DEFAULT_MANUFACTURER "Diego MFG & Co." #define HAL_USBHAOA_DEFAULT_MANUFACTURER "Diego MFG & Co."
#define HAL_USBHAOA_DEFAULT_MODEL "Diego's device" #define HAL_USBHAOA_DEFAULT_MODEL "Diego's device"
@ -235,9 +235,10 @@
/* debug */ /* debug */
#define USBH_DEBUG_ENABLE TRUE #define USBH_DEBUG_ENABLE TRUE
#define USBH_DEBUG_USBHD USBHD1 #define USBH_DEBUG_MULTI_HOST FALSE
#define USBH_DEBUG_SD SD2 #define USBH_DEBUG_SINGLE_HOST_SELECTION USBHD1
#define USBH_DEBUG_BUFFER 25000 #define USBH_DEBUG_BUFFER 25000
#define USBH_DEBUG_OUTPUT_CALLBACK usbh_debug_output
#define USBH_DEBUG_ENABLE_TRACE FALSE #define USBH_DEBUG_ENABLE_TRACE FALSE
#define USBH_DEBUG_ENABLE_INFO TRUE #define USBH_DEBUG_ENABLE_INFO TRUE

View File

@ -18,7 +18,7 @@
#include "hal.h" #include "hal.h"
#include "ff.h" #include "ff.h"
#include <string.h> #include <string.h>
#include "usbh/debug.h" /* for usbDbgPuts/usbDbgPrintf */ #include "usbh/debug.h" /* for _usbh_dbg/_usbh_dbgf */
#define UVC_TO_MSD_PHOTOS_CAPTURE FALSE #define UVC_TO_MSD_PHOTOS_CAPTURE FALSE
@ -85,6 +85,8 @@ static const ShellConfig shell_cfg1 = {
static void ThreadTestFTDI(void *p) { static void ThreadTestFTDI(void *p) {
(void)p; (void)p;
USBHFTDIPortDriver *const ftdipp = &FTDIPD[0]; USBHFTDIPortDriver *const ftdipp = &FTDIPD[0];
USBHDriver *host = NULL;
(void)host;
chRegSetThreadName("FTDI"); chRegSetThreadName("FTDI");
@ -95,7 +97,8 @@ start:
chThdSleepMilliseconds(100); chThdSleepMilliseconds(100);
} }
usbDbgPuts("FTDI: Connected"); host = usbhftdipGetHost(ftdipp);
_usbh_dbg(host, "FTDI: Connected");
USBHFTDIPortConfig config = { USBHFTDIPortConfig config = {
115200, 115200,
@ -112,7 +115,7 @@ start:
for(;;) { for(;;) {
msg_t m = streamGet(ftdipp); msg_t m = streamGet(ftdipp);
if (m < MSG_OK) { if (m < MSG_OK) {
usbDbgPuts("FTDI: Disconnected"); _usbh_dbg(host, "FTDI: Disconnected");
goto start; goto start;
} }
streamPut(ftdipp, (uint8_t)m); streamPut(ftdipp, (uint8_t)m);
@ -146,10 +149,10 @@ start:
for(;;) { for(;;) {
msg_t m = streamGet(ftdipp); msg_t m = streamGet(ftdipp);
if (m < MSG_OK) { if (m < MSG_OK) {
usbDbgPuts("FTDI: Disconnected"); _usbh_dbg(host, "FTDI: Disconnected");
goto start; goto start;
} }
sdPut(&USBH_DEBUG_SD, (uint8_t)m); sdPut(&SD2, (uint8_t)m);
if (m == 'q') if (m == 'q')
break; break;
} }
@ -169,19 +172,19 @@ start:
st = chVTGetSystemTimeX(); st = chVTGetSystemTimeX();
while (times--) { while (times--) {
if (streamWrite(ftdipp, buf, 1024) < 1024) { if (streamWrite(ftdipp, buf, 1024) < 1024) {
usbDbgPuts("FTDI: Disconnected"); _usbh_dbg(host, "FTDI: Disconnected");
goto start; goto start;
} }
bytes -= 1024; bytes -= 1024;
} }
if (bytes) { if (bytes) {
if (streamWrite(ftdipp, buf, bytes) < bytes) { if (streamWrite(ftdipp, buf, bytes) < bytes) {
usbDbgPuts("FTDI: Disconnected"); _usbh_dbg(host, "FTDI: Disconnected");
goto start; goto start;
} }
} }
et = chVTGetSystemTimeX(); et = chVTGetSystemTimeX();
usbDbgPrintf("\tRate=%uB/s", (config.speed * 100) / (et - st)); _usbh_dbgf(host, "\tRate=%uB/s", (config.speed * 100) / (et - st));
} }
} }
@ -189,7 +192,7 @@ start:
if (0) { if (0) {
for (;;) { for (;;) {
if (streamPut(ftdipp, 'A') != MSG_OK) { if (streamPut(ftdipp, 'A') != MSG_OK) {
usbDbgPuts("FTDI: Disconnected"); _usbh_dbg(host, "FTDI: Disconnected");
goto start; goto start;
} }
chThdSleepMilliseconds(100); chThdSleepMilliseconds(100);
@ -198,7 +201,7 @@ start:
usbhftdipStop(ftdipp); usbhftdipStop(ftdipp);
usbDbgPuts("FTDI: Tests done, restarting in 3s"); _usbh_dbg(host, "FTDI: Tests done, restarting in 3s");
chThdSleepMilliseconds(3000); chThdSleepMilliseconds(3000);
goto start; goto start;
@ -218,6 +221,8 @@ static void ThreadTestAOA(void *p) {
(void)p; (void)p;
USBHAOADriver *const aoap = &USBHAOAD[0]; USBHAOADriver *const aoap = &USBHAOAD[0];
USBHAOAChannel *const aoacp = &aoap->channel; USBHAOAChannel *const aoacp = &aoap->channel;
USBHDriver *host = NULL;
(void)host;
chRegSetThreadName("AOA"); chRegSetThreadName("AOA");
@ -226,11 +231,12 @@ start:
chThdSleepMilliseconds(100); chThdSleepMilliseconds(100);
} }
usbDbgPuts("AOA: Connected"); host = usbhaoaGetHost(aoap);
_usbh_dbg(host, "AOA: Connected");
if (usbhaoaGetChannelState(aoap) != USBHAOA_CHANNEL_STATE_READY) { if (usbhaoaGetChannelState(aoap) != USBHAOA_CHANNEL_STATE_READY) {
usbhaoaChannelStart(aoap); usbhaoaChannelStart(aoap);
usbDbgPuts("AOA: Channel started"); _usbh_dbg(host, "AOA: Channel started");
} }
//loopback //loopback
@ -238,7 +244,7 @@ start:
for(;;) { for(;;) {
msg_t m = streamGet(aoacp); msg_t m = streamGet(aoacp);
if (m < MSG_OK) { if (m < MSG_OK) {
usbDbgPuts("AOA: Disconnected"); _usbh_dbg(host, "AOA: Disconnected");
goto start; goto start;
} }
streamPut(aoacp, (uint8_t)m); streamPut(aoacp, (uint8_t)m);
@ -258,19 +264,19 @@ start:
st = chVTGetSystemTimeX(); st = chVTGetSystemTimeX();
while (times--) { while (times--) {
if (streamWrite(aoacp, buf, 1024) < 1024) { if (streamWrite(aoacp, buf, 1024) < 1024) {
usbDbgPuts("AOA: Disconnected"); _usbh_dbg(host, "AOA: Disconnected");
goto start; goto start;
} }
bytes -= 1024; bytes -= 1024;
} }
if (bytes) { if (bytes) {
if (streamWrite(aoacp, buf, bytes) < bytes) { if (streamWrite(aoacp, buf, bytes) < bytes) {
usbDbgPuts("AOA: Disconnected"); _usbh_dbg(host, "AOA: Disconnected");
goto start; goto start;
} }
} }
et = chVTGetSystemTimeX(); et = chVTGetSystemTimeX();
usbDbgPrintf("\tRate=%uB/s", AOA_WRITE_SPEED_TEST_BYTES / (et - st) * 100); _usbh_dbgf(host, "\tRate=%uB/s", AOA_WRITE_SPEED_TEST_BYTES / (et - st) * 100);
} }
} }
@ -278,7 +284,7 @@ start:
if (0) { if (0) {
for (;;) { for (;;) {
if (streamPut(aoacp, 'A') != MSG_OK) { if (streamPut(aoacp, 'A') != MSG_OK) {
usbDbgPuts("AOA: Disconnected"); _usbh_dbg(host, "AOA: Disconnected");
goto start; goto start;
} }
chThdSleepMilliseconds(100); chThdSleepMilliseconds(100);
@ -287,7 +293,7 @@ start:
usbhaoaChannelStop(aoap); usbhaoaChannelStop(aoap);
usbDbgPuts("AOA: Tests done, restarting in 3s"); _usbh_dbg(host, "AOA: Tests done, restarting in 3s");
chThdSleepMilliseconds(3000); chThdSleepMilliseconds(3000);
goto start; goto start;
@ -305,7 +311,7 @@ static FATFS MSDLUN0FS;
static uint8_t fbuff[10240]; static uint8_t fbuff[10240];
static FIL file; static FIL file;
static FRESULT scan_files(BaseSequentialStream *chp, char *path) { static FRESULT scan_files(USBHDriver *host, BaseSequentialStream *chp, char *path) {
FRESULT res; FRESULT res;
DIR dir; DIR dir;
UINT i; UINT i;
@ -321,12 +327,12 @@ static FRESULT scan_files(BaseSequentialStream *chp, char *path) {
i = strlen(path); i = strlen(path);
path[i++] = '/'; path[i++] = '/';
strcpy(&path[i], fno.fname); strcpy(&path[i], fno.fname);
res = scan_files(chp, path); res = scan_files(host, chp, path);
if (res != FR_OK) if (res != FR_OK)
break; break;
path[--i] = 0; path[--i] = 0;
} else { } else {
usbDbgPrintf("FS: %s/%s", path, fno.fname); _usbh_dbgf(host, "FS: %s/%s", path, fno.fname);
} }
} }
} }
@ -341,11 +347,12 @@ static void ThreadTestMSD(void *p) {
FATFS *fsp; FATFS *fsp;
DWORD clusters; DWORD clusters;
FRESULT res; FRESULT res;
USBHDriver *host = NULL;
chRegSetThreadName("MSD"); chRegSetThreadName("MSD");
#if !UVC_TO_MSD_PHOTOS_CAPTURE #if !UVC_TO_MSD_PHOTOS_CAPTURE
BaseSequentialStream * const chp = (BaseSequentialStream *)&USBH_DEBUG_SD; BaseSequentialStream * const chp = (BaseSequentialStream *)&SD2;
systime_t st, et; systime_t st, et;
uint32_t j; uint32_t j;
#endif #endif
@ -355,14 +362,15 @@ start:
chThdSleepMilliseconds(100); chThdSleepMilliseconds(100);
if (blkGetDriverState(&MSBLKD[0]) == BLK_ACTIVE) { if (blkGetDriverState(&MSBLKD[0]) == BLK_ACTIVE) {
usbDbgPuts("BLK: Active, connect...."); host = usbhmsdLUNGetHost(&MSBLKD[0]);
_usbh_dbg(host, "BLK: Active, connect....");
usbhmsdLUNConnect(&MSBLKD[0]); usbhmsdLUNConnect(&MSBLKD[0]);
} }
if (blkGetDriverState(&MSBLKD[0]) != BLK_READY) { if (blkGetDriverState(&MSBLKD[0]) != BLK_READY) {
continue; continue;
} }
usbDbgPuts("BLK: Ready."); _usbh_dbg(host, "BLK: Ready.");
#if !UVC_TO_MSD_PHOTOS_CAPTURE #if !UVC_TO_MSD_PHOTOS_CAPTURE
//raw read test //raw read test
@ -372,7 +380,7 @@ start:
#define NITERATIONS ((RAW_READ_SZ_MB * 1024UL * 1024UL) / sizeof(fbuff)) #define NITERATIONS ((RAW_READ_SZ_MB * 1024UL * 1024UL) / sizeof(fbuff))
uint32_t start = 0; uint32_t start = 0;
chThdSetPriority(HIGHPRIO); chThdSetPriority(HIGHPRIO);
usbDbgPrintf("BLK: Raw read test (%dMB, %dB blocks)", RAW_READ_SZ_MB, sizeof(fbuff)); _usbh_dbgf(host, "BLK: Raw read test (%dMB, %dB blocks)", RAW_READ_SZ_MB, sizeof(fbuff));
st = chVTGetSystemTime(); st = chVTGetSystemTime();
for (j = 0; j < NITERATIONS; j++) { for (j = 0; j < NITERATIONS; j++) {
if (blkRead(&MSBLKD[0], start, fbuff, NBLOCKS) != HAL_SUCCESS) if (blkRead(&MSBLKD[0], start, fbuff, NBLOCKS) != HAL_SUCCESS)
@ -380,29 +388,29 @@ start:
start += NBLOCKS; start += NBLOCKS;
} }
et = chVTGetSystemTime(); et = chVTGetSystemTime();
usbDbgPrintf("BLK: Raw read in %d ms, %dkB/s", _usbh_dbgf(host, "BLK: Raw read in %d ms, %dkB/s",
et - st, et - st,
(RAW_READ_SZ_MB * 1024UL * 1000) / (et - st)); (RAW_READ_SZ_MB * 1024UL * 1000) / (et - st));
chThdSetPriority(NORMALPRIO); chThdSetPriority(NORMALPRIO);
} }
#endif #endif
usbDbgPuts("FS: Block driver ready, try mount..."); _usbh_dbg(host, "FS: Block driver ready, try mount...");
res = f_mount(&MSDLUN0FS, FATFSDEV_MSD_DRIVE, 1); res = f_mount(&MSDLUN0FS, FATFSDEV_MSD_DRIVE, 1);
if (res != FR_OK) { if (res != FR_OK) {
usbDbgPuts("FS: Can't mount. Check file system."); _usbh_dbg(host, "FS: Can't mount. Check file system.");
continue; continue;
} }
usbDbgPuts("FS: Mounted."); _usbh_dbg(host, "FS: Mounted.");
res = f_getfree(FATFSDEV_MSD_DRIVE, &clusters, &fsp); res = f_getfree(FATFSDEV_MSD_DRIVE, &clusters, &fsp);
if (res != FR_OK) { if (res != FR_OK) {
usbDbgPuts("FS: f_getfree() failed"); _usbh_dbg(host, "FS: f_getfree() failed");
continue; continue;
} }
usbDbgPrintf("FS: %lu free clusters, %lu sectors per cluster, %lu bytes free", _usbh_dbgf(host, "FS: %lu free clusters, %lu sectors per cluster, %lu bytes free",
clusters, (uint32_t)MSDLUN0FS.csize, clusters, (uint32_t)MSDLUN0FS.csize,
clusters * (uint32_t)MSDLUN0FS.csize * MSBLKD[0].info.blk_size); clusters * (uint32_t)MSDLUN0FS.csize * MSBLKD[0].info.blk_size);
@ -419,7 +427,7 @@ start:
//write test //write test
if (1) { if (1) {
usbDbgPuts("FS: Write test (create file /test.dat, 1MB)"); _usbh_dbg(host, "FS: Write test (create file /test.dat, 1MB)");
f_open(&file, FATFSDEV_MSD_DRIVE "/test.dat", FA_CREATE_ALWAYS | FA_WRITE); f_open(&file, FATFSDEV_MSD_DRIVE "/test.dat", FA_CREATE_ALWAYS | FA_WRITE);
src = start; src = start;
st = chVTGetSystemTime(); st = chVTGetSystemTime();
@ -431,7 +439,7 @@ start:
src = start; src = start;
} }
et = chVTGetSystemTime(); et = chVTGetSystemTime();
usbDbgPrintf("FS: Written 1MB in %d ms, %dkB/s", _usbh_dbgf(host, "FS: Written 1MB in %d ms, %dkB/s",
et - st, et - st,
(1024UL*1000) / (et - st)); (1024UL*1000) / (et - st));
f_close(&file); f_close(&file);
@ -439,7 +447,7 @@ start:
//read test //read test
if (1) { if (1) {
usbDbgPuts("FS: Read test (read file /test.dat, 1MB, compare)"); _usbh_dbg(host, "FS: Read test (read file /test.dat, 1MB, compare)");
f_open(&file, FATFSDEV_MSD_DRIVE "/test.dat", FA_READ); f_open(&file, FATFSDEV_MSD_DRIVE "/test.dat", FA_READ);
src = start; src = start;
st = chVTGetSystemTime(); st = chVTGetSystemTime();
@ -447,7 +455,7 @@ start:
if (f_read(&file, fbuff, 512, &bw) != FR_OK) if (f_read(&file, fbuff, 512, &bw) != FR_OK)
goto start; goto start;
if (memcmp(src, fbuff, bw)) { if (memcmp(src, fbuff, bw)) {
usbDbgPrintf("Compare error @%08x", (uint32_t)src); _usbh_dbgf(host, "Compare error @%08x", (uint32_t)src);
goto start; goto start;
} }
src += bw; src += bw;
@ -455,7 +463,7 @@ start:
src = start; src = start;
} }
et = chVTGetSystemTime(); et = chVTGetSystemTime();
usbDbgPrintf("FS: Read 1MB in %d ms, %dkB/s", _usbh_dbgf(host, "FS: Read 1MB in %d ms, %dkB/s",
et - st, et - st,
(1024UL*1000) / (et - st)); (1024UL*1000) / (et - st));
f_close(&file); f_close(&file);
@ -463,14 +471,14 @@ start:
//scan files test //scan files test
if (1) { if (1) {
usbDbgPuts("FS: Scan files test"); _usbh_dbg(host, "FS: Scan files test");
strcpy((char *)fbuff, FATFSDEV_MSD_DRIVE); strcpy((char *)fbuff, FATFSDEV_MSD_DRIVE);
scan_files(chp, (char *)fbuff); scan_files(host, chp, (char *)fbuff);
} }
} }
#endif #endif
usbDbgPuts("FS: Tests done, restarting in 3s"); _usbh_dbg(host, "FS: Tests done, restarting in 3s");
chThdSleepMilliseconds(3000); chThdSleepMilliseconds(3000);
goto start; goto start;
@ -488,12 +496,12 @@ static void _hid_report_callback(USBHHIDDriver *hidp, uint16_t len) {
uint8_t *report = (uint8_t *)hidp->config->report_buffer; uint8_t *report = (uint8_t *)hidp->config->report_buffer;
if (hidp->type == USBHHID_DEVTYPE_BOOT_MOUSE) { if (hidp->type == USBHHID_DEVTYPE_BOOT_MOUSE) {
usbDbgPrintf("Mouse report: buttons=%02x, Dx=%d, Dy=%d", _usbh_dbgf(hidp->dev->host, "Mouse report: buttons=%02x, Dx=%d, Dy=%d",
report[0], report[0],
(int8_t)report[1], (int8_t)report[1],
(int8_t)report[2]); (int8_t)report[2]);
} else if (hidp->type == USBHHID_DEVTYPE_BOOT_KEYBOARD) { } else if (hidp->type == USBHHID_DEVTYPE_BOOT_KEYBOARD) {
usbDbgPrintf("Keyboard report: modifier=%02x, keys=%02x %02x %02x %02x %02x %02x", _usbh_dbgf(hidp->dev->host, "Keyboard report: modifier=%02x, keys=%02x %02x %02x %02x %02x %02x",
report[0], report[0],
report[2], report[2],
report[3], report[3],
@ -502,7 +510,7 @@ static void _hid_report_callback(USBHHIDDriver *hidp, uint16_t len) {
report[6], report[6],
report[7]); report[7]);
} else { } else {
usbDbgPrintf("Generic report, %d bytes", len); _usbh_dbgf(hidp->dev->host, "Generic report, %d bytes", len);
} }
} }
@ -525,21 +533,22 @@ static void ThreadTestHID(void *p) {
for (;;) { for (;;) {
for (i = 0; i < HAL_USBHHID_MAX_INSTANCES; i++) { for (i = 0; i < HAL_USBHHID_MAX_INSTANCES; i++) {
if (usbhhidGetState(&USBHHIDD[i]) == USBHHID_STATE_ACTIVE) { USBHHIDDriver *const hidp = &USBHHIDD[i];
usbDbgPrintf("HID: Connected, HID%d", i); if (usbhhidGetState(hidp) == USBHHID_STATE_ACTIVE) {
usbhhidStart(&USBHHIDD[i], &hidcfg[i]); _usbh_dbgf(hidp->dev->host, "HID: Connected, HID%d", i);
if (usbhhidGetType(&USBHHIDD[i]) != USBHHID_DEVTYPE_GENERIC) { usbhhidStart(hidp, &hidcfg[i]);
usbhhidSetIdle(&USBHHIDD[i], 0, 0); if (usbhhidGetType(hidp) != USBHHID_DEVTYPE_GENERIC) {
usbhhidSetIdle(hidp, 0, 0);
} }
kbd_led_states[i] = 1; kbd_led_states[i] = 1;
} else if (usbhhidGetState(&USBHHIDD[i]) == USBHHID_STATE_READY) { } else if (usbhhidGetState(hidp) == USBHHID_STATE_READY) {
if (usbhhidGetType(&USBHHIDD[i]) == USBHHID_DEVTYPE_BOOT_KEYBOARD) { if (usbhhidGetType(hidp) == USBHHID_DEVTYPE_BOOT_KEYBOARD) {
USBH_DEFINE_BUFFER(uint8_t val); USBH_DEFINE_BUFFER(uint8_t val);
val = kbd_led_states[i] << 1; val = kbd_led_states[i] << 1;
if (val == 0x08) { if (val == 0x08) {
val = 1; val = 1;
} }
usbhhidSetReport(&USBHHIDD[i], 0, USBHHID_REPORTTYPE_OUTPUT, &val, 1); usbhhidSetReport(hidp, 0, USBHHID_REPORTTYPE_OUTPUT, &val, 1);
kbd_led_states[i] = val; kbd_led_states[i] = val;
} }
} }
@ -637,12 +646,12 @@ static void ThreadTestUVC(void *p) {
if (usbhuvcGetState(&USBHUVCD[0]) != USBHUVC_STATE_ACTIVE) if (usbhuvcGetState(&USBHUVCD[0]) != USBHUVC_STATE_ACTIVE)
continue; continue;
usbDbgPuts("UVC: Webcam connected"); _usbh_dbg(uvcdp->dev->host, "UVC: Webcam connected");
/* ************************************ */ /* ************************************ */
/* Find best configuration */ /* Find best configuration */
/* ************************************ */ /* ************************************ */
usbDbgPuts("UVC: Find best configuration"); _usbh_dbg(uvcdp->dev->host, "UVC: Find best configuration");
generic_iterator_t ics; generic_iterator_t ics;
const usbh_uvc_format_mjpeg_t *format; const usbh_uvc_format_mjpeg_t *format;
@ -658,7 +667,7 @@ static void ThreadTestUVC(void *p) {
goto failed; goto failed;
format = (const usbh_uvc_format_mjpeg_t *)ics.curr; format = (const usbh_uvc_format_mjpeg_t *)ics.curr;
usbDbgPrintf("\tSelect bFormatIndex=%d", format->bFormatIndex); _usbh_dbgf(uvcdp->dev->host, "\tSelect bFormatIndex=%d", format->bFormatIndex);
//find the most suitable frame (largest one within the bandwidth requirements) //find the most suitable frame (largest one within the bandwidth requirements)
if (usbhuvcFindVSDescriptor(uvcdp, &ics, UVC_VS_FRAME_MJPEG, TRUE) != HAL_SUCCESS) if (usbhuvcFindVSDescriptor(uvcdp, &ics, UVC_VS_FRAME_MJPEG, TRUE) != HAL_SUCCESS)
@ -668,18 +677,18 @@ static void ThreadTestUVC(void *p) {
const usbh_uvc_frame_mjpeg_t *const frame = (usbh_uvc_frame_mjpeg_t *)ics.curr; const usbh_uvc_frame_mjpeg_t *const frame = (usbh_uvc_frame_mjpeg_t *)ics.curr;
uint32_t frame_sz = frame->wWidth * frame->wHeight; uint32_t frame_sz = frame->wWidth * frame->wHeight;
usbDbgPrintf("\t\tbFrameIndex=%d", frame->bFrameIndex); _usbh_dbgf(uvcdp->dev->host, "\t\tbFrameIndex=%d", frame->bFrameIndex);
usbDbgPrintf("\t\t\twWidth=%d, wHeight=%d", frame->wWidth, frame->wHeight); _usbh_dbgf(uvcdp->dev->host, "\t\t\twWidth=%d, wHeight=%d", frame->wWidth, frame->wHeight);
usbDbgPrintf("\t\t\tdwMinBitRate=%u, dwMaxBitRate=%u", frame->dwMinBitRate, frame->dwMaxBitRate); _usbh_dbgf(uvcdp->dev->host, "\t\t\tdwMinBitRate=%u, dwMaxBitRate=%u", frame->dwMinBitRate, frame->dwMaxBitRate);
usbDbgPrintf("\t\t\tdwMaxVideoFrameBufferSize=%u", frame->dwMaxVideoFrameBufferSize); _usbh_dbgf(uvcdp->dev->host, "\t\t\tdwMaxVideoFrameBufferSize=%u", frame->dwMaxVideoFrameBufferSize);
usbDbgPrintf("\t\t\tdwDefaultFrameInterval=%u", frame->dwDefaultFrameInterval); _usbh_dbgf(uvcdp->dev->host, "\t\t\tdwDefaultFrameInterval=%u", frame->dwDefaultFrameInterval);
uint8_t j; uint8_t j;
for (j = 0; j < frame->bFrameIntervalType; j++) { for (j = 0; j < frame->bFrameIntervalType; j++) {
uint32_t ep_sz = uint32_t ep_sz =
usbhuvcEstimateRequiredEPSize(uvcdp, (const uint8_t *)format, (const uint8_t *)frame, frame->dwFrameInterval[j]); usbhuvcEstimateRequiredEPSize(uvcdp, (const uint8_t *)format, (const uint8_t *)frame, frame->dwFrameInterval[j]);
usbDbgPrintf("\t\t\tdwFrameInterval=%u, estimated EP size=%u", frame->dwFrameInterval[j], ep_sz); _usbh_dbgf(uvcdp->dev->host, "\t\t\tdwFrameInterval=%u, estimated EP size=%u", frame->dwFrameInterval[j], ep_sz);
if (ep_sz > 310) if (ep_sz > 310)
continue; continue;
@ -697,7 +706,7 @@ static void ThreadTestUVC(void *p) {
if (ep_sz < min_ep_sz) { if (ep_sz < min_ep_sz) {
/* new best bitrate */ /* new best bitrate */
min_ep_sz = ep_sz; min_ep_sz = ep_sz;
usbDbgPuts("\t\t\tNew best candidate found"); _usbh_dbg(uvcdp->dev->host, "\t\t\tNew best candidate found");
best_frame_interval_index = j; best_frame_interval_index = j;
best_frame = frame; best_frame = frame;
} }
@ -706,14 +715,14 @@ static void ThreadTestUVC(void *p) {
failed: failed:
if (best_frame == NULL) { if (best_frame == NULL) {
usbDbgPuts("\t\t\tCouldn't find suitable format/frame"); _usbh_dbg(uvcdp->dev->host, "\t\t\tCouldn't find suitable format/frame");
continue; continue;
} }
/* ************************************ */ /* ************************************ */
/* NEGOTIATION */ /* NEGOTIATION */
/* ************************************ */ /* ************************************ */
usbDbgPuts("UVC: Start negotiation"); _usbh_dbg(uvcdp->dev->host, "UVC: Start negotiation");
usbhuvcResetPC(uvcdp); usbhuvcResetPC(uvcdp);
usbh_uvc_ctrl_vs_probecommit_data_t *const pc = usbhuvcGetPC(uvcdp); usbh_uvc_ctrl_vs_probecommit_data_t *const pc = usbhuvcGetPC(uvcdp);
@ -723,42 +732,42 @@ failed:
pc->bFrameIndex = best_frame->bFrameIndex; pc->bFrameIndex = best_frame->bFrameIndex;
pc->dwFrameInterval = best_frame->dwFrameInterval[best_frame_interval_index]; pc->dwFrameInterval = best_frame->dwFrameInterval[best_frame_interval_index];
usbDbgPrintf("\tFirst probe, selecting bFormatIndex=%d, bFrameIndex=%d, dwFrameInterval=%u", _usbh_dbgf(uvcdp->dev->host, "\tFirst probe, selecting bFormatIndex=%d, bFrameIndex=%d, dwFrameInterval=%u",
pc->bFormatIndex, pc->bFrameIndex, pc->dwFrameInterval); pc->bFormatIndex, pc->bFrameIndex, pc->dwFrameInterval);
usbDbgPuts("SET_CUR (PROBE):"); usbhuvcPrintProbeCommit(&uvcdp->pc); _usbh_dbg(uvcdp->dev->host, "SET_CUR (PROBE):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc);
if (usbhuvcProbe(uvcdp) != HAL_SUCCESS) { if (usbhuvcProbe(uvcdp) != HAL_SUCCESS) {
usbDbgPuts("\tFirst probe failed"); _usbh_dbg(uvcdp->dev->host, "\tFirst probe failed");
continue; continue;
} }
usbDbgPuts("GET_CUR (PROBE):"); usbhuvcPrintProbeCommit(&uvcdp->pc); _usbh_dbg(uvcdp->dev->host, "GET_CUR (PROBE):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc);
usbDbgPuts("GET_MIN (PROBE):"); usbhuvcPrintProbeCommit(&uvcdp->pc_min); _usbh_dbg(uvcdp->dev->host, "GET_MIN (PROBE):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc_min);
usbDbgPuts("GET_MAX (PROBE):"); usbhuvcPrintProbeCommit(&uvcdp->pc_max); _usbh_dbg(uvcdp->dev->host, "GET_MAX (PROBE):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc_max);
pc->bmHint = 0x0001; pc->bmHint = 0x0001;
pc->wCompQuality = uvcdp->pc_min.wCompQuality; pc->wCompQuality = uvcdp->pc_min.wCompQuality;
usbDbgPuts("SET_CUR (PROBE):"); usbhuvcPrintProbeCommit(&uvcdp->pc); _usbh_dbg(uvcdp->dev->host, "SET_CUR (PROBE):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc);
usbDbgPrintf("\tSecond probe, selecting wCompQuality=%d", pc->wCompQuality); _usbh_dbgf(uvcdp->dev->host, "\tSecond probe, selecting wCompQuality=%d", pc->wCompQuality);
if (usbhuvcProbe(uvcdp) != HAL_SUCCESS) { if (usbhuvcProbe(uvcdp) != HAL_SUCCESS) {
usbDbgPuts("\tSecond probe failed"); _usbh_dbg(uvcdp->dev->host, "\tSecond probe failed");
continue; continue;
} }
usbDbgPuts("GET_CUR (PROBE):"); usbhuvcPrintProbeCommit(&uvcdp->pc); _usbh_dbg(uvcdp->dev->host, "GET_CUR (PROBE):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc);
usbDbgPuts("GET_MIN (PROBE):"); usbhuvcPrintProbeCommit(&uvcdp->pc_min); _usbh_dbg(uvcdp->dev->host, "GET_MIN (PROBE):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc_min);
usbDbgPuts("GET_MAX (PROBE):"); usbhuvcPrintProbeCommit(&uvcdp->pc_max); _usbh_dbg(uvcdp->dev->host, "GET_MAX (PROBE):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc_max);
/* ************************************ */ /* ************************************ */
/* Commit negotiated parameters */ /* Commit negotiated parameters */
/* ************************************ */ /* ************************************ */
usbDbgPuts("UVC: Commit negotiated parameters"); _usbh_dbg(uvcdp->dev->host, "UVC: Commit negotiated parameters");
usbDbgPuts("SET_CUR (COMMIT):"); usbhuvcPrintProbeCommit(&uvcdp->pc); _usbh_dbg(uvcdp->dev->host, "SET_CUR (COMMIT):"); usbhuvcPrintProbeCommit(uvcdp, &uvcdp->pc);
if (usbhuvcCommit(uvcdp) != HAL_SUCCESS) { if (usbhuvcCommit(uvcdp) != HAL_SUCCESS) {
usbDbgPuts("\tCommit failed"); _usbh_dbg(uvcdp->dev->host, "\tCommit failed");
continue; continue;
} }
usbDbgPuts("UVC: Ready to start streaming"); _usbh_dbg(uvcdp->dev->host, "UVC: Ready to start streaming");
uint32_t npackets = 0; uint32_t npackets = 0;
uint32_t payload = 0; uint32_t payload = 0;
@ -774,7 +783,7 @@ failed:
msg_t msg, ret; msg_t msg, ret;
ret = usbhuvcLockAndFetch(uvcdp, &msg, TIME_INFINITE); ret = usbhuvcLockAndFetch(uvcdp, &msg, TIME_INFINITE);
if (ret == MSG_RESET) { if (ret == MSG_RESET) {
usbDbgPuts("UVC: Driver is unloading"); _usbh_dbg(uvcdp->dev->host, "UVC: Driver is unloading");
break; break;
} else if (ret == MSG_TIMEOUT) { } else if (ret == MSG_TIMEOUT) {
continue; continue;
@ -784,7 +793,7 @@ failed:
usbhuvc_message_data_t *const data = (usbhuvc_message_data_t *)msg; usbhuvc_message_data_t *const data = (usbhuvc_message_data_t *)msg;
if (data->length < data->data[0]) { if (data->length < data->data[0]) {
usbDbgPrintf("UVC: Length error!"); _usbh_dbgf(uvcdp->dev->host, "UVC: Length error!");
goto free_data; goto free_data;
} }
@ -815,20 +824,20 @@ failed:
chsnprintf(fn, sizeof(fn), "/img%d.jpg", frame); chsnprintf(fn, sizeof(fn), "/img%d.jpg", frame);
if (f_open(&fp, fn, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK) { if (f_open(&fp, fn, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK) {
if (with_dht && f_write(&fp, jpeg_header_plus_dht, sizeof(jpeg_header_plus_dht), &bw) != FR_OK) { if (with_dht && f_write(&fp, jpeg_header_plus_dht, sizeof(jpeg_header_plus_dht), &bw) != FR_OK) {
usbDbgPuts("UVC->MSD: File write error"); _usbh_dbg(uvcdp->dev->host, "UVC->MSD: File write error");
f_close(&fp); f_close(&fp);
state = 0; state = 0;
} }
state = 2; state = 2;
} else { } else {
usbDbgPuts("UVC->MSD: File open error"); _usbh_dbg(uvcdp->dev->host, "UVC->MSD: File open error");
state = 0; state = 0;
} }
} }
if (state == 2) { if (state == 2) {
if (f_write(&fp, message_data, message_payload, &bw) != FR_OK) { if (f_write(&fp, message_data, message_payload, &bw) != FR_OK) {
usbDbgPuts("UVC->MSD: File write error"); _usbh_dbg(uvcdp->dev->host, "UVC->MSD: File write error");
f_close(&fp); f_close(&fp);
state = 0; state = 0;
} }
@ -837,7 +846,7 @@ failed:
check_eof: check_eof:
#endif #endif
if (data->data[1] & UVC_HDR_EOF) { if (data->data[1] & UVC_HDR_EOF) {
usbDbgPrintf("UVC: FRAME #%d, delta=%03dticks, #packets=%d, useful_payload=%dbytes, total=%dbytes", _usbh_dbgf(uvcdp->dev->host, "UVC: FRAME #%d, delta=%03dticks, #packets=%d, useful_payload=%dbytes, total=%dbytes",
frame, data->timestamp - last , npackets, payload, total); frame, data->timestamp - last , npackets, payload, total);
last = data->timestamp; last = data->timestamp;
npackets = 0; npackets = 0;
@ -856,17 +865,17 @@ free_data:
const uint8_t *const stat = status->data; const uint8_t *const stat = status->data;
switch (stat[0] & 0x0f) { switch (stat[0] & 0x0f) {
case 1: case 1:
usbDbgPrintf("UVC: STATUS Control event, " _usbh_dbgf(uvcdp->dev->host, "UVC: STATUS Control event, "
"bOriginator=%d, bEvent=%d, bSelector=%d, bAttribute=%d", "bOriginator=%d, bEvent=%d, bSelector=%d, bAttribute=%d",
stat[1], stat[2], stat[3], stat[4]); stat[1], stat[2], stat[3], stat[4]);
break; break;
case 2: case 2:
usbDbgPrintf("UVC: STATUS Streaming event, " _usbh_dbgf(uvcdp->dev->host, "UVC: STATUS Streaming event, "
"bOriginator=%d, bEvent=%d, bValue=%d", "bOriginator=%d, bEvent=%d, bValue=%d",
stat[1], stat[2], stat[3]); stat[1], stat[2], stat[3]);
break; break;
default: default:
usbDbgPrintf("UVC: STATUS unknown status report = %d", stat[0]); _usbh_dbgf(uvcdp->dev->host, "UVC: STATUS unknown status report = %d", stat[0]);
break; break;
} }
usbhuvcFreeStatusMessage(uvcdp, status); usbhuvcFreeStatusMessage(uvcdp, status);
@ -879,6 +888,16 @@ free_data:
} }
#endif #endif
#if USBH_DEBUG_MULTI_HOST
void USBH_DEBUG_OUTPUT_CALLBACK(USBHDriver *host, const uint8_t *buff, size_t len) {
(void)host;
#else
void USBH_DEBUG_OUTPUT_CALLBACK(const uint8_t *buff, size_t len) {
#endif
sdWrite(&SD2, buff, len);
sdWrite(&SD2, (const uint8_t *)"\r\n", 2);
}
int main(void) { int main(void) {
IWDG->KR = 0x5555; IWDG->KR = 0x5555;
@ -928,9 +947,11 @@ int main(void) {
//start //start
#if STM32_USBH_USE_OTG1 #if STM32_USBH_USE_OTG1
usbhStart(&USBHD1); usbhStart(&USBHD1);
_usbh_dbgf(&USBHD1, "Started");
#endif #endif
#if STM32_USBH_USE_OTG2 #if STM32_USBH_USE_OTG2
usbhStart(&USBHD2); usbhStart(&USBHD2);
_usbh_dbgf(&USBHD2, "Started");
#endif #endif
for(;;) { for(;;) {