Didn't mean to upload to root directory
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPProtocol.c
index f51eeae..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
@@ -57,7 +59,7 @@ void ISPProtocol_EnterISPMode(void)
        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;
@@ -73,7 +75,7 @@ void ISPProtocol_EnterISPMode(void)
 
        /* 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-- && !(TimeoutExpired))
+       while (Enter_ISP_Params.SynchLoops-- && TimeoutTicksRemaining)
        {
                uint8_t ResponseBytes[4];
 
@@ -115,7 +117,7 @@ void ISPProtocol_LeaveISPMode(void)
        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 */
@@ -154,7 +156,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
        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);
@@ -175,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;
@@ -295,7 +297,7 @@ void ISPProtocol_ReadMemory(uint8_t V2Command)
        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);
@@ -368,7 +370,7 @@ void ISPProtocol_ChipErase(void)
        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;
@@ -388,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.
  *
@@ -404,7 +489,7 @@ void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command)
        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];
@@ -435,7 +520,7 @@ void ISPProtocol_WriteFuseLock(uint8_t V2Command)
        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 */
@@ -463,7 +548,7 @@ void ISPProtocol_SPIMulti(void)
        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);
@@ -523,9 +608,8 @@ void ISPProtocol_SPIMulti(void)
  */
 void ISPProtocol_DelayMS(uint8_t DelayMS)
 {
-       while (DelayMS-- && !(TimeoutExpired))
+       while (DelayMS-- && TimeoutTicksRemaining)
          Delay_MS(1);
 }
 
 #endif
-