X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/f3d370a7778dc8e374efc3b76e26f8ecefc27e84..fa1a092901319b3b41bf09e0c4cb9cbbec470d6b:/Projects/AVRISP-MKII/Lib/V2Protocol.c diff --git a/Projects/AVRISP-MKII/Lib/V2Protocol.c b/Projects/AVRISP-MKII/Lib/V2Protocol.c index 976f24b9e..431f9c2d5 100644 --- a/Projects/AVRISP-MKII/Lib/V2Protocol.c +++ b/Projects/AVRISP-MKII/Lib/V2Protocol.c @@ -42,12 +42,22 @@ uint32_t CurrentAddress; /** Flag to indicate that the next read/write operation must update the device's current address */ bool MustSetAddress; - -/** ISR for the management of the command execution timeout counter */ -ISR(TIMER0_COMPA_vect, ISR_BLOCK) +/** Initializes the hardware and software associated with the V2 protocol command handling. */ +void V2Protocol_Init(void) { - if (TimeoutMSRemaining) - TimeoutMSRemaining--; + #if defined(ADC) + /* Initialize the ADC converter for VTARGET level detection on supported AVR models */ + ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128); + ADC_SetupChannel(VTARGET_ADC_CHANNEL); + ADC_StartReading(VTARGET_ADC_CHANNEL_MASK | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC); + #endif + + /* Millisecond timer initialization for managing the command timeout counter */ + OCR0A = ((F_CPU / 64) / 1000); + TCCR0A = (1 << WGM01); + TCCR0B = ((1 << CS01) | (1 << CS00)); + + V2Params_LoadNonVolatileParamValues(); } /** Master V2 Protocol packet handler, for received V2 Protocol packets from a connected host. @@ -58,10 +68,8 @@ void V2Protocol_ProcessCommand(void) { uint8_t V2Command = Endpoint_Read_Byte(); - /* Set total command processing timeout value, enable timeout management interrupt */ TimeoutMSRemaining = COMMAND_TIMEOUT_MS; - TIMSK0 |= (1 << OCIE0A); - + switch (V2Command) { case CMD_SIGN_ON: @@ -121,11 +129,9 @@ void V2Protocol_ProcessCommand(void) V2Protocol_UnknownCommand(V2Command); break; } - - /* Disable timeout management interrupt once processing has completed */ - TIMSK0 &= ~(1 << OCIE0A); Endpoint_WaitUntilReady(); + Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM); Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT); } @@ -144,6 +150,7 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command) } Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); Endpoint_Write_Byte(V2Command); @@ -155,12 +162,13 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command) static void V2Protocol_SignOn(void) { Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); Endpoint_Write_Byte(CMD_SIGN_ON); Endpoint_Write_Byte(STATUS_CMD_OK); Endpoint_Write_Byte(sizeof(PROGRAMMER_ID) - 1); - Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1)); + Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1), NO_STREAM_CALLBACK); Endpoint_ClearIN(); } @@ -170,6 +178,7 @@ static void V2Protocol_SignOn(void) static void V2Protocol_ResetProtection(void) { Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); Endpoint_Write_Byte(CMD_RESET_PROTECTION); @@ -192,6 +201,7 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command) ParamValue = Endpoint_Read_Byte(); Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); Endpoint_Write_Byte(V2Command); @@ -222,9 +232,10 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command) */ static void V2Protocol_LoadAddress(void) { - Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress)); + Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NO_STREAM_CALLBACK); Endpoint_ClearOUT(); + Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); MustSetAddress = true;