X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/80d85090baa9b5c6abf4c3e550a63486d4252839..3a03da970558de9211fc1a43b12bf69e38854d09:/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c?ds=sidebyside diff --git a/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c b/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c index 2ce11a8ee..aff84a962 100644 --- a/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c +++ b/Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c @@ -64,7 +64,7 @@ void ISPProtocol_EnterISPMode(void) CurrentAddress = 0; - /* Set up the synchronous USART to generate the recovery clock on XCK pin */ + /* Set up the synchronous USART to generate the .5MHz recovery clock on XCK pin */ UBRR1 = (F_CPU / 500000UL); UCSR1B = (1 << TXEN1); UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1); @@ -72,11 +72,11 @@ void ISPProtocol_EnterISPMode(void) /* Perform execution delay, initialize SPI bus */ ISPProtocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS); - SPI_Init(ISPTarget_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER); + ISPTarget_Init(); /* Continuously attempt to synchronize with the target until either the number of attempts specified * by the host has exceeded, or the the device sends back the expected response values */ - while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED) && TimeoutMSRemaining) + while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED) && TimeoutTicksRemaining) { uint8_t ResponseBytes[4]; @@ -86,7 +86,7 @@ void ISPProtocol_EnterISPMode(void) for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++) { ISPProtocol_DelayMS(Enter_ISP_Params.ByteDelay); - ResponseBytes[RByte] = SPI_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]); + ResponseBytes[RByte] = ISPTarget_TransferByte(Enter_ISP_Params.EnterProgBytes[RByte]); } /* Check if polling disabled, or if the polled value matches the expected value */ @@ -124,7 +124,7 @@ void ISPProtocol_LeaveISPMode(void) /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */ ISPProtocol_DelayMS(Leave_ISP_Params.PreDelayMS); ISPTarget_ChangeTargetResetLine(false); - SPI_ShutDown(); + ISPTarget_ShutDown(); ISPProtocol_DelayMS(Leave_ISP_Params.PostDelayMS); /* Turn off the synchronous USART to terminate the recovery clock on XCK pin */ @@ -184,7 +184,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command) uint16_t PollAddress = 0; uint8_t PollValue = (V2Command == CMD_PROGRAM_FLASH_ISP) ? Write_Memory_Params.PollValue1 : Write_Memory_Params.PollValue2; - uint8_t* NextWriteByte = Write_Memory_Params.ProgData; + uint8_t* NextWriteByte = Write_Memory_Params.ProgData; /* Check the programming mode desired by the host, either Paged or Word memory writes */ if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK) @@ -204,10 +204,10 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command) bool IsOddByte = (CurrentByte & 0x01); uint8_t ByteToWrite = *(NextWriteByte++); - SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]); - SPI_SendByte(CurrentAddress >> 8); - SPI_SendByte(CurrentAddress & 0xFF); - SPI_SendByte(ByteToWrite); + ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[0]); + ISPTarget_SendByte(CurrentAddress >> 8); + ISPTarget_SendByte(CurrentAddress & 0xFF); + ISPTarget_SendByte(ByteToWrite); /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high * or low byte at the current word address */ @@ -231,13 +231,13 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command) /* If the current page must be committed, send the PROGRAM PAGE command to the target */ if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK) { - SPI_SendByte(Write_Memory_Params.ProgrammingCommands[1]); - SPI_SendByte(StartAddress >> 8); - SPI_SendByte(StartAddress & 0xFF); - SPI_SendByte(0x00); + ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[1]); + ISPTarget_SendByte(StartAddress >> 8); + ISPTarget_SendByte(StartAddress & 0xFF); + ISPTarget_SendByte(0x00); - /* Check if polling is possible, if not switch to timed delay mode */ - if (!(PollAddress)) + /* Check if polling is possible and enabled, if not switch to timed delay mode */ + if (!(PollAddress) && (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_VALUE_MASK)) { Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_VALUE_MASK; Write_Memory_Params.ProgrammingMode |= PROG_MODE_PAGED_TIMEDELAY_MASK; @@ -266,15 +266,18 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command) MustLoadExtendedAddress = false; } - SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]); - SPI_SendByte(CurrentAddress >> 8); - SPI_SendByte(CurrentAddress & 0xFF); - SPI_SendByte(ByteToWrite); + ISPTarget_SendByte(Write_Memory_Params.ProgrammingCommands[0]); + ISPTarget_SendByte(CurrentAddress >> 8); + ISPTarget_SendByte(CurrentAddress & 0xFF); + ISPTarget_SendByte(ByteToWrite); /* AVR FLASH addressing requires us to modify the write command based on if we are writing a high * or low byte at the current word address */ if (V2Command == CMD_PROGRAM_FLASH_ISP) Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK; + + /* Save previous programming mode in case we modify it for the current word */ + uint8_t PreviousProgrammingMode = Write_Memory_Params.ProgrammingMode; if (ByteToWrite != PollValue) { @@ -283,10 +286,18 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command) PollAddress = (CurrentAddress & 0xFFFF); } + else if (!(Write_Memory_Params.ProgrammingMode & PROG_MODE_WORD_READYBUSY_MASK)) + { + Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_WORD_VALUE_MASK; + Write_Memory_Params.ProgrammingMode |= PROG_MODE_WORD_TIMEDELAY_MASK; + } ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue, Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]); + /* Restore previous programming mode mask in case the current word needed to change it */ + Write_Memory_Params.ProgrammingMode = PreviousProgrammingMode; + /* Abort the programming loop early if the byte/word programming failed */ if (ProgrammingStatus != STATUS_CMD_OK) break; @@ -343,10 +354,10 @@ void ISPProtocol_ReadMemory(uint8_t V2Command) } /* Read the next byte from the desired memory space in the device */ - SPI_SendByte(Read_Memory_Params.ReadMemoryCommand); - SPI_SendByte(CurrentAddress >> 8); - SPI_SendByte(CurrentAddress & 0xFF); - Endpoint_Write_Byte(SPI_ReceiveByte()); + ISPTarget_SendByte(Read_Memory_Params.ReadMemoryCommand); + ISPTarget_SendByte(CurrentAddress >> 8); + ISPTarget_SendByte(CurrentAddress & 0xFF); + Endpoint_Write_Byte(ISPTarget_ReceiveByte()); /* Check if the endpoint bank is currently full, if so send the packet */ if (!(Endpoint_IsReadWriteAllowed())) @@ -406,7 +417,7 @@ void ISPProtocol_ChipErase(void) /* Send the chip erase commands as given by the host to the device */ for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++) - SPI_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]); + ISPTarget_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]); /* Use appropriate command completion check as given by the host (delay or busy polling) */ if (!(Erase_Chip_Params.PollMethod)) @@ -442,7 +453,7 @@ void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command) /* Send the Fuse or Lock byte read commands as given by the host to the device, store response */ for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++) - ResponseBytes[RByte] = SPI_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]); + ResponseBytes[RByte] = ISPTarget_TransferByte(Read_FuseLockSigOSCCAL_Params.ReadCommandBytes[RByte]); Endpoint_Write_Byte(V2Command); Endpoint_Write_Byte(STATUS_CMD_OK); @@ -471,7 +482,7 @@ void ISPProtocol_WriteFuseLock(uint8_t V2Command) /* Send the Fuse or Lock byte program commands as given by the host to the device */ for (uint8_t SByte = 0; SByte < sizeof(Write_FuseLockSig_Params.WriteCommandBytes); SByte++) - SPI_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]); + ISPTarget_SendByte(Write_FuseLockSig_Params.WriteCommandBytes[SByte]); Endpoint_Write_Byte(V2Command); Endpoint_Write_Byte(STATUS_CMD_OK); @@ -507,9 +518,9 @@ void ISPProtocol_SPIMulti(void) while (CurrTxPos < SPI_Multi_Params.RxStartAddr) { if (CurrTxPos < SPI_Multi_Params.TxBytes) - SPI_SendByte(SPI_Multi_Params.TxData[CurrTxPos]); + ISPTarget_SendByte(SPI_Multi_Params.TxData[CurrTxPos]); else - SPI_SendByte(0); + ISPTarget_SendByte(0); CurrTxPos++; } @@ -518,9 +529,9 @@ void ISPProtocol_SPIMulti(void) while (CurrRxPos < SPI_Multi_Params.RxBytes) { if (CurrTxPos < SPI_Multi_Params.TxBytes) - Endpoint_Write_Byte(SPI_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++])); + Endpoint_Write_Byte(ISPTarget_TransferByte(SPI_Multi_Params.TxData[CurrTxPos++])); else - Endpoint_Write_Byte(SPI_ReceiveByte()); + Endpoint_Write_Byte(ISPTarget_ReceiveByte()); /* Check to see if we have filled the endpoint bank and need to send the packet */ if (!(Endpoint_IsReadWriteAllowed())) @@ -552,13 +563,8 @@ void ISPProtocol_SPIMulti(void) */ void ISPProtocol_DelayMS(uint8_t DelayMS) { - while (DelayMS-- && TimeoutMSRemaining) - { - if (TimeoutMSRemaining) - TimeoutMSRemaining--; - - _delay_ms(1); - } + while (DelayMS-- && TimeoutTicksRemaining) + _delay_ms(1); } #endif \ No newline at end of file