123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- #define INCLUDE_FROM_V2PROTOCOL_C
- #include "V2Protocol.h"
- uint32_t CurrentAddress;
- bool MustLoadExtendedAddress;
- ISR(TIMER0_COMPA_vect, ISR_NOBLOCK)
- {
- if (TimeoutTicksRemaining)
- TimeoutTicksRemaining--;
- else
- TCCR0B = 0;
- }
- void V2Protocol_Init(void)
- {
- #if defined(ADC) && !defined(NO_VTARGET_DETECT)
-
- ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
- ADC_SetupChannel(VTARGET_ADC_CHANNEL);
- ADC_StartReading(VTARGET_REF_MASK | ADC_RIGHT_ADJUSTED | VTARGET_ADC_CHANNEL_MASK);
- #endif
-
- OCR0A = (((F_CPU / 1024) / 100) - 1);
- TCCR0A = (1 << WGM01);
- TIMSK0 = (1 << OCIE0A);
- V2Params_LoadNonVolatileParamValues();
- #if defined(ENABLE_ISP_PROTOCOL)
- ISPTarget_ConfigureRescueClock();
- #endif
- }
- void V2Protocol_ProcessCommand(void)
- {
- uint8_t V2Command = Endpoint_Read_8();
-
- TimeoutTicksRemaining = COMMAND_TIMEOUT_TICKS;
- TCCR0B = ((1 << CS02) | (1 << CS00));
- switch (V2Command)
- {
- case CMD_SIGN_ON:
- V2Protocol_SignOn();
- break;
- case CMD_SET_PARAMETER:
- case CMD_GET_PARAMETER:
- V2Protocol_GetSetParam(V2Command);
- break;
- case CMD_LOAD_ADDRESS:
- V2Protocol_LoadAddress();
- break;
- case CMD_RESET_PROTECTION:
- V2Protocol_ResetProtection();
- break;
- #if defined(ENABLE_ISP_PROTOCOL)
- case CMD_ENTER_PROGMODE_ISP:
- ISPProtocol_EnterISPMode();
- break;
- case CMD_LEAVE_PROGMODE_ISP:
- ISPProtocol_LeaveISPMode();
- break;
- case CMD_PROGRAM_FLASH_ISP:
- case CMD_PROGRAM_EEPROM_ISP:
- ISPProtocol_ProgramMemory(V2Command);
- break;
- case CMD_READ_FLASH_ISP:
- case CMD_READ_EEPROM_ISP:
- ISPProtocol_ReadMemory(V2Command);
- break;
- case CMD_CHIP_ERASE_ISP:
- ISPProtocol_ChipErase();
- break;
- case CMD_READ_FUSE_ISP:
- case CMD_READ_LOCK_ISP:
- case CMD_READ_SIGNATURE_ISP:
- case CMD_READ_OSCCAL_ISP:
- ISPProtocol_ReadFuseLockSigOSCCAL(V2Command);
- break;
- case CMD_PROGRAM_FUSE_ISP:
- case CMD_PROGRAM_LOCK_ISP:
- ISPProtocol_WriteFuseLock(V2Command);
- break;
- case CMD_SPI_MULTI:
- ISPProtocol_SPIMulti();
- break;
- #endif
- #if defined(ENABLE_XPROG_PROTOCOL)
- case CMD_XPROG_SETMODE:
- XPROGProtocol_SetMode();
- break;
- case CMD_XPROG:
- XPROGProtocol_Command();
- break;
- #endif
- default:
- V2Protocol_UnknownCommand(V2Command);
- break;
- }
-
- TCCR0B = 0;
- Endpoint_WaitUntilReady();
- Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR);
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
- }
- static void V2Protocol_UnknownCommand(const uint8_t V2Command)
- {
-
- while (Endpoint_BytesInEndpoint() == AVRISP_DATA_EPSIZE)
- {
- Endpoint_ClearOUT();
- Endpoint_WaitUntilReady();
- }
- Endpoint_ClearOUT();
- Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
- Endpoint_Write_8(V2Command);
- Endpoint_Write_8(STATUS_CMD_UNKNOWN);
- Endpoint_ClearIN();
- }
- static void V2Protocol_SignOn(void)
- {
- Endpoint_ClearOUT();
- Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
- Endpoint_Write_8(CMD_SIGN_ON);
- Endpoint_Write_8(STATUS_CMD_OK);
- Endpoint_Write_8(sizeof(PROGRAMMER_ID) - 1);
- Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1), NULL);
- Endpoint_ClearIN();
- }
- static void V2Protocol_ResetProtection(void)
- {
- Endpoint_ClearOUT();
- Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
- Endpoint_Write_8(CMD_RESET_PROTECTION);
- Endpoint_Write_8(STATUS_CMD_OK);
- Endpoint_ClearIN();
- }
- static void V2Protocol_GetSetParam(const uint8_t V2Command)
- {
- uint8_t ParamID = Endpoint_Read_8();
- uint8_t ParamValue;
- if (V2Command == CMD_SET_PARAMETER)
- ParamValue = Endpoint_Read_8();
- Endpoint_ClearOUT();
- Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
- Endpoint_Write_8(V2Command);
- uint8_t ParamPrivs = V2Params_GetParameterPrivileges(ParamID);
- if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))
- {
- Endpoint_Write_8(STATUS_CMD_OK);
- V2Params_SetParameterValue(ParamID, ParamValue);
- }
- else if ((V2Command == CMD_GET_PARAMETER) && (ParamPrivs & PARAM_PRIV_READ))
- {
- Endpoint_Write_8(STATUS_CMD_OK);
- Endpoint_Write_8(V2Params_GetParameterValue(ParamID));
- }
- else
- {
- Endpoint_Write_8(STATUS_CMD_FAILED);
- }
- Endpoint_ClearIN();
- }
- static void V2Protocol_LoadAddress(void)
- {
- Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NULL);
- Endpoint_ClearOUT();
- Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
- if (CurrentAddress & (1UL << 31))
- MustLoadExtendedAddress = true;
- Endpoint_Write_8(CMD_LOAD_ADDRESS);
- Endpoint_Write_8(STATUS_CMD_OK);
- Endpoint_ClearIN();
- }
|