diff --git a/common/app/app.c b/common/app/app.c index 75294ff..7b77c00 100644 --- a/common/app/app.c +++ b/common/app/app.c @@ -4,11 +4,14 @@ #include "pixy.h" #include + +bool pixy_connected = false; + void app_init() { system_init(); tft_init(); - pixy_init(); + pixy_connected = (pixy_init()==0); //try to connect to pixy //only testwise tft_clear(WHITE); @@ -21,18 +24,33 @@ void app_init() { -void pixy_led_test(); -void pixy_frame_test(); +int pixy_led_test(); +int pixy_frame_test(); //app event loop void app_process() { - pixy_service(); //send/receive event data + //Note: The only way to detect that pixy has been disconnected is if a command fails. There's no pixy_is_connected method yet :'( - //Code for tests see below - pixy_led_test(); - pixy_frame_test(); - - //system_delay(500); + if(!pixy_connected) { //Pixy not connected + pixy_close(); //Ensure that all pixy resources are freed (failsafe) + if(pixy_init()==0) { //try to connect to pixy + pixy_connected=true; + } + } + + if(pixy_connected) { + pixy_service(); //Send/receive event data from/to pixy failed + + //Code for tests see below + if(pixy_led_test()!=0) { + pixy_connected=false; + } + + /*if(pixy_frame_test()!=0) { + pixy_connected=false; + }*/ + system_delay(500); + } } @@ -42,7 +60,7 @@ int colorind; const uint32_t colors [] = {0xFF0000, 0x00FF00,0x0000FF,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF,0x000000}; const int num_colors = sizeof(colors)/sizeof(uint32_t); -void pixy_led_test() { +int pixy_led_test() { if(colorind==0) { pixy_led_set_max_current(5); } @@ -51,6 +69,12 @@ void pixy_led_test() { int return_value; return_value = pixy_command("led_set", INT32(colors[colorind++]), END_OUT_ARGS, &response, END_IN_ARGS); colorind%=num_colors; + + if(return_value!=0) { + colorind=0; //reset color ind, to start at zero when plugging pixy in again + } + + return return_value; } //---------------------------------------------------------------------------------------------------------------------------- @@ -58,7 +82,7 @@ void pixy_led_test() { int renderBA81(uint8_t renderFlags, uint16_t width, uint16_t height, uint32_t frameLen, uint8_t *frame); -void pixy_frame_test() { +int pixy_frame_test() { uint8_t* videodata; int32_t response; @@ -88,7 +112,8 @@ void pixy_frame_test() { if(return_value==0) { return_value = renderBA81(renderflags,xwidth,ywidth,size,videodata); - } + } + return return_value; } diff --git a/discovery/Makefile b/discovery/Makefile index 08b4fd3..78493b0 100644 --- a/discovery/Makefile +++ b/discovery/Makefile @@ -8,6 +8,7 @@ CROSS_COMPILE=arm-none-eabi- CC=$(CROSS_COMPILE)gcc OBJCOPY=$(CROSS_COMPILE)objcopy GDB=$(CROSS_COMPILE)gdb +SIZE=$(CROSS_COMPILE)size MKDIR=mkdir -p RM=rm -f @@ -90,6 +91,7 @@ stop: #elf to binary %.bin: %.elf $(OBJCOPY) -O binary $< $@ + $(SIZE) $< #Asm files to objects $(OBJ_DIR)/%.o: $(SRC_DIR)/%.s diff --git a/discovery/src/main.c b/discovery/src/main.c index 4703ec0..4b6880d 100644 --- a/discovery/src/main.c +++ b/discovery/src/main.c @@ -1,13 +1,24 @@ #include "app.h" + + +#include "usb_hcd_int.h" +#include "usbh_usr.h" +#include "usbh_core.h" +#include "usbh_msc_core.h" + + +extern USB_OTG_CORE_HANDLE USB_OTG_Core; +extern USBH_HOST USB_Host; + int main(void) { app_init(); while (1) { + USBH_Process(&USB_OTG_Core, &USB_Host); app_process(); - //USBH_Process(&USB_OTG_Core, &USB_Host); } } diff --git a/discovery/src/usbh_msc_core.c b/discovery/src/usbh_msc_core.c index 0bdb405..a985091 100644 --- a/discovery/src/usbh_msc_core.c +++ b/discovery/src/usbh_msc_core.c @@ -1,7 +1,6 @@ #include "usbh_msc_core.h" #include "usbh_core.h" -#include "pixy.h" static USBH_Status USBH_MSC_InterfaceInit (USB_OTG_CORE_HANDLE *pdev , @@ -18,6 +17,8 @@ static USBH_Status USBH_MSC_ClassRequest(USB_OTG_CORE_HANDLE *pdev , extern USB_OTG_CORE_HANDLE USB_OTG_Core; +extern USBH_HOST USB_Host; + USBH_Class_cb_TypeDef USBH_MSC_cb = @@ -41,8 +42,6 @@ typedef struct MSC_Machine_TypeDef; MSC_Machine_TypeDef MSC_Machine; -enum {init,running,down}state; - static USBH_Status USBH_MSC_InterfaceInit ( USB_OTG_CORE_HANDLE *pdev, void *phost) @@ -108,11 +107,6 @@ static USBH_Status USBH_MSC_InterfaceInit ( USB_OTG_CORE_HANDLE *pdev, void USBH_MSC_InterfaceDeInit ( USB_OTG_CORE_HANDLE *pdev, void *phost) { - if(state==running) { - pixy_close(); - state=down; - } - if ( MSC_Machine.hc_num_out) { USB_OTG_HC_Halt(pdev, MSC_Machine.hc_num_out); @@ -133,8 +127,6 @@ static USBH_Status USBH_MSC_ClassRequest(USB_OTG_CORE_HANDLE *pdev , { USBH_Status status = USBH_OK ; - state=init; - return status; } @@ -147,32 +139,11 @@ static USBH_Status USBH_MSC_Handle(USB_OTG_CORE_HANDLE *pdev , if(HCD_IsDeviceConnected(pdev)) { - switch(state) - { - case init: - state = running; - USB_OTG_BSP_mDelay(3000); //let the pixy led flashing pass - pixy_init(); - break; - case running: - pixy_service(); - int appliStatus = pphost->usr_cb->USBH_USR_MSC_Application(); - if(appliStatus == 0) - { - state=running; //stay here - } - else if (appliStatus == 1) - { - status = USBH_APPLY_DEINIT; - pixy_close(); - state=down; - } - break; - case down: - break; - default: - break; - } + int appliStatus = pphost->usr_cb->USBH_USR_MSC_Application(); + if(appliStatus != 0) + { + status = USBH_APPLY_DEINIT; + } } return status; @@ -197,16 +168,37 @@ uint32_t USBH_LL_getTimer() { int USBH_LL_open() { + int timeoutDetect=100; + int timeoutStartup=3000; + cnt_int=0; //reset timer + + while(USB_Host.gState!=HOST_CLASS && cnt_int < timeoutDetect) { + USBH_Process(&USB_OTG_Core, &USB_Host); + } + + if(USB_Host.gState!=HOST_CLASS) { + return -5; // = LIBUSB_ERROR_NOT_FOUND + } + + + //moved to common + cnt_int=0; + while(cnt_int0 && cnt_int>=timeoutMs) { STM_EVAL_LEDOn(LED3); - return -7; //timeout (error code like with libusb + return -7; //timeout (error code like with libusb) } return -1; @@ -234,6 +226,8 @@ int USBH_LL_receive(uint8_t *data, uint32_t len, uint16_t timeoutMs) { USB_OTG_CORE_HANDLE *pdev = &USB_OTG_Core; + if(!HCD_IsDeviceConnected(pdev)) return -1; + USBH_BulkReceiveData (pdev, data, len , @@ -241,7 +235,7 @@ int USBH_LL_receive(uint8_t *data, uint32_t len, uint16_t timeoutMs) { URB_STATE state; cnt_int=0; //reset timer - if(timeoutMs==0) timeoutMs=10000; //Force 10s timeout (testwise) + if(timeoutMs==0) timeoutMs=1000; //Force 1s timeout (testwise) while((state=HCD_GetURB_State(pdev , MSC_Machine.hc_num_in)) == URB_IDLE && (timeoutMs==0 || cnt_int < timeoutMs)); @@ -249,7 +243,7 @@ int USBH_LL_receive(uint8_t *data, uint32_t len, uint16_t timeoutMs) { if(state!=URB_DONE) { if(timeoutMs>0 && cnt_int>=timeoutMs) { STM_EVAL_LEDOn(LED3); - return -7; //timeout (error code like with libusb + return -7; //timeout (error code like with libusb) } return -1; } diff --git a/discovery/src/usbh_usr.c b/discovery/src/usbh_usr.c index 909a876..80fe829 100644 --- a/discovery/src/usbh_usr.c +++ b/discovery/src/usbh_usr.c @@ -1,7 +1,7 @@ #include "usbh_usr.h" #include #include -#include "pixy.h" +//#include "pixy.h"