Didn't mean to upload to root directory
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPProtocol.c
index 2e31c18..4dc37a4 100644 (file)
@@ -1,13 +1,15 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2011.
+     Copyright (C) Dean Camera, 2018.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2018  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  
+  Function ISPProtocol_Calibrate() copyright 2018 Jacob September
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
@@ -18,7 +20,7 @@
   advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
-  The author disclaim all warranties with regard to this
+  The author disclaims all warranties with regard to this
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
@@ -52,12 +54,12 @@ void ISPProtocol_EnterISPMode(void)
                uint8_t PollValue;
                uint8_t PollIndex;
                uint8_t EnterProgBytes[4];
-       } ATTR_PACKED Enter_ISP_Params;
+       } Enter_ISP_Params;
 
        Endpoint_Read_Stream_LE(&Enter_ISP_Params, sizeof(Enter_ISP_Params), NULL);
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        uint8_t ResponseStatus = STATUS_CMD_FAILED;
@@ -69,10 +71,11 @@ void ISPProtocol_EnterISPMode(void)
        ISPTarget_EnableTargetISP();
 
        ISPTarget_ChangeTargetResetLine(true);
+       ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
 
        /* 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_OK) && !(TimeoutExpired))
+       while (Enter_ISP_Params.SynchLoops-- && TimeoutTicksRemaining)
        {
                uint8_t ResponseBytes[4];
 
@@ -86,12 +89,14 @@ void ISPProtocol_EnterISPMode(void)
                if (!(Enter_ISP_Params.PollIndex) || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue))
                {
                        ResponseStatus = STATUS_CMD_OK;
+                       break;
                }
                else
                {
                        ISPTarget_ChangeTargetResetLine(false);
                        ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
                        ISPTarget_ChangeTargetResetLine(true);
+                       ISPProtocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);
                }
        }
 
@@ -107,12 +112,12 @@ void ISPProtocol_LeaveISPMode(void)
        {
                uint8_t PreDelayMS;
                uint8_t PostDelayMS;
-       } ATTR_PACKED Leave_ISP_Params;
+       } Leave_ISP_Params;
 
        Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params), NULL);
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */
@@ -141,17 +146,17 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                uint8_t  ProgrammingCommands[3];
                uint8_t  PollValue1;
                uint8_t  PollValue2;
-               uint8_t  ProgData[256];        // Note, the Jungo driver has a very short ACK timeout period, need to buffer the
-       } ATTR_PACKED Write_Memory_Params; // whole page and ACK the packet as fast as possible to prevent it from aborting
+               uint8_t  ProgData[256]; // Note, the Jungo driver has a very short ACK timeout period, need to buffer the
+       } Write_Memory_Params;      // whole page and ACK the packet as fast as possible to prevent it from aborting
 
        Endpoint_Read_Stream_LE(&Write_Memory_Params, (sizeof(Write_Memory_Params) -
                                                       sizeof(Write_Memory_Params.ProgData)), NULL);
-       Write_Memory_Params.BytesToWrite = be16_to_cpu(Write_Memory_Params.BytesToWrite);
-       
+       Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);
+
        if (Write_Memory_Params.BytesToWrite > sizeof(Write_Memory_Params.ProgData))
        {
                Endpoint_ClearOUT();
-               Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+               Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
                Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
                Endpoint_Write_8(V2Command);
@@ -172,7 +177,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
        }
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        uint8_t  ProgrammingStatus = STATUS_CMD_OK;
@@ -230,22 +235,22 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                        if (ProgrammingStatus != STATUS_CMD_OK)
                          break;
 
-                       /* Must reset the polling address afterwards, so it is not erronously used for the next byte */
+                       /* Must reset the polling address afterwards, so it is not erroneously used for the next byte */
                        PollAddress = 0;
                }
-               
+
                /* EEPROM just increments the address each byte, flash needs to increment on each word and
                 * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
-                * address boundary has been crossed */
+                * address boundary has been crossed during FLASH memory programming */
                if ((CurrentByte & 0x01) || (V2Command == CMD_PROGRAM_EEPROM_ISP))
                {
                        CurrentAddress++;
 
-                       if ((V2Command != CMD_PROGRAM_EEPROM_ISP) && !(CurrentAddress & 0xFFFF))
+                       if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
                          MustLoadExtendedAddress = true;
                }
        }
-       
+
        /* 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)
        {
@@ -268,7 +273,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                /* Check to see if the FLASH address has crossed the extended address boundary */
                if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
                  MustLoadExtendedAddress = true;
-       }       
+       }
 
        Endpoint_Write_8(V2Command);
        Endpoint_Write_8(ProgrammingStatus);
@@ -286,13 +291,13 @@ void ISPProtocol_ReadMemory(uint8_t V2Command)
        {
                uint16_t BytesToRead;
                uint8_t  ReadMemoryCommand;
-       } ATTR_PACKED Read_Memory_Params;
+       } Read_Memory_Params;
 
        Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params), NULL);
-       Read_Memory_Params.BytesToRead = be16_to_cpu(Read_Memory_Params.BytesToRead);
-       
+       Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);
+
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(V2Command);
@@ -360,12 +365,12 @@ void ISPProtocol_ChipErase(void)
                uint8_t EraseDelayMS;
                uint8_t PollMethod;
                uint8_t EraseCommandBytes[4];
-       } ATTR_PACKED Erase_Chip_Params;
+       } Erase_Chip_Params;
 
        Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params), NULL);
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        uint8_t ResponseStatus = STATUS_CMD_OK;
@@ -385,6 +390,89 @@ void ISPProtocol_ChipErase(void)
        Endpoint_ClearIN();
 }
 
+/** Global volatile variables used in ISRs relating to ISPProtocol_Calibrate() */
+volatile uint16_t HalfCyclesRemaining;
+volatile uint8_t  ResponseTogglesRemaining;
+
+/** ISR to toggle MOSI pin when TIMER1 overflows */
+ISR(TIMER1_OVF_vect)
+{
+       PINB |= (1 << PB2);     // toggle PB2 (MOSI) by writing 1 to its bit in PINB
+       HalfCyclesRemaining--;
+}
+
+/** ISR to listen for toggles on MISO pin */
+ISR(PCINT0_vect)
+{
+       ResponseTogglesRemaining--;
+}
+
+/** Handler for the CMD_OSCCAL command, entering RC-calibration mode as specified in AVR053 */
+void ISPProtocol_Calibrate(void)
+{
+       #define CALIB_CLOCK                     32768
+               // CALIB_TICKS uses 2x frequency because we toggle twice per cycle
+               //  and adds 1/2 denom. to nom. to ensure rounding instead of flooring of integer division
+       #define CALIB_TICKS                     ( (F_CPU+CALIB_CLOCK) / (2*CALIB_CLOCK) )
+               // Per AVR053, calibration guaranteed to take 1024 cycles (2048 half-cycles) or fewer;
+               //  add some cycles for response delay (5-10 after success) and response itself
+       #define HALF_CYCLE_LIMIT        (2*1024 + 50)
+       #define SUCCESS_TOGGLE_NUM      8
+       
+       uint8_t ResponseStatus = STATUS_CMD_OK;
+       
+       /* Don't entirely know why this is needed, something to do with the USB communication back to PC */
+       Endpoint_ClearOUT();
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
+       Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
+       
+       /* Enable pullup on MISO and release ~RESET */
+       DDRB    =  ~(1 << PB3);                                 // explicitly set all PORTB to outputs except PB3 (MISO)
+       PORTB  |= ( (1 << PB4) | (1 << PB3) );  // set PB4 (TARG_RST) high (i.e. not reset) and enable pullup on PB3 (MISO)
+       
+       /* Set up MISO pin (PCINT3) to listen for toggles */
+       PCMSK0  = (1 << PCINT3);        // set mask to enable PCINT on only Pin 3 (MISO)
+       
+       /* Set up timer that fires at a rate of 65536 Hz - this will drive the MOSI toggle */
+       OCR1A   = CALIB_TICKS - 1;              // zero-indexed counter; for 16MHz system clock, this becomes 243
+       TCCR1A  = ( (1 << WGM11) | (1 << WGM10) );                                      // set for fast PWM, TOP = OCR1A
+       TCCR1B  = ( (1 << WGM13) | (1 << WGM12) | (1 << CS10) );        //  ... and no clock prescaling
+       TCNT1   = 0;                                                                                            // reset counter
+
+       /* Initialize counter variables */
+       HalfCyclesRemaining                     = HALF_CYCLE_LIMIT;
+       ResponseTogglesRemaining        = SUCCESS_TOGGLE_NUM;
+
+       /* Turn on interrupts */
+       uint8_t OldSREG = SREG; // save current global interrupt state
+       PCICR  |= (1 << PCIE0); // enable interrupts for PCINT7:0 (don't touch setting for PCINT12:8)
+       TIMSK1  = (1 << TOIE1); // enable T1 OVF interrupt (and no other T1 interrupts)
+       sei();                                  // enable global interrupts
+
+       /* Let device do its calibration, wait for reponse on MISO */
+       while ( HalfCyclesRemaining && ResponseTogglesRemaining )
+       {
+               // do nothing...
+       }
+       
+       /* Disable interrupts, restore SREG */
+       PCICR  &= ~(1 << PCIE0);
+       TIMSK1  = 0;
+       SREG    = OldSREG;
+       
+       /* Check if device responded with a success message or if we timed out */
+       if (ResponseTogglesRemaining)
+       {
+               ResponseStatus = STATUS_CMD_TOUT;
+       }
+
+       /* Report back to PC via USB */
+       Endpoint_Write_8(CMD_OSCCAL);
+       Endpoint_Write_8(ResponseStatus);
+       Endpoint_ClearIN();
+       
+} // void ISPProtocol_Calibrate(void)
+
 /** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,
  *  reading the requested configuration byte from the device.
  *
@@ -396,12 +484,12 @@ void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command)
        {
                uint8_t RetByte;
                uint8_t ReadCommandBytes[4];
-       } ATTR_PACKED Read_FuseLockSigOSCCAL_Params;
+       } Read_FuseLockSigOSCCAL_Params;
 
        Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params), NULL);
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        uint8_t ResponseBytes[4];
@@ -427,12 +515,12 @@ void ISPProtocol_WriteFuseLock(uint8_t V2Command)
        struct
        {
                uint8_t WriteCommandBytes[4];
-       } ATTR_PACKED Write_FuseLockSig_Params;
+       } Write_FuseLockSig_Params;
 
        Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params), NULL);
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        /* Send the Fuse or Lock byte program commands as given by the host to the device */
@@ -454,13 +542,13 @@ void ISPProtocol_SPIMulti(void)
                uint8_t RxBytes;
                uint8_t RxStartAddr;
                uint8_t TxData[255];
-       } ATTR_PACKED SPI_Multi_Params;
+       } SPI_Multi_Params;
 
        Endpoint_Read_Stream_LE(&SPI_Multi_Params, (sizeof(SPI_Multi_Params) - sizeof(SPI_Multi_Params.TxData)), NULL);
        Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes, NULL);
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(CMD_SPI_MULTI);
@@ -512,13 +600,15 @@ void ISPProtocol_SPIMulti(void)
        }
 }
 
-/** Blocking delay for a given number of milliseconds.
+/** Blocking delay for a given number of milliseconds. This provides a simple wrapper around
+ *  the avr-libc provided delay function, so that the delay function can be called with a
+ *  constant value (to prevent run-time floating point operations being required).
  *
  *  \param[in] DelayMS  Number of milliseconds to delay for
  */
 void ISPProtocol_DelayMS(uint8_t DelayMS)
 {
-       while (DelayMS-- && !(TimeoutExpired))
+       while (DelayMS-- && TimeoutTicksRemaining)
          Delay_MS(1);
 }