/*******************************************************************************
* Function Name: Bootloader_HostLink
********************************************************************************
*
* Summary:
*  Causes the bootloader to attempt to read data being transmitted by the
*  host application.  If data is sent from the host, this establishes the
*  communication interface to process all requests.
*
* Parameters:
*  timeOut:
*   The amount of time to listen for data before giving up. Timeout is
*   measured in 10s of ms.  Use 0 for an infinite wait.
*
* Return:
*   None
*
*******************************************************************************/
static void Bootloader_HostLink(uint32 timeout) 
{
  uint16    CYDATA numberRead;
  cystatus  CYDATA readStat;
  uint16    bytesToRead;
  uint16    bytesToWrite;  
  uint32    counterVal;
  
  static const uint8 deviceID[7] = {'A','V','R','B', 'O', 'O', 'T'};
  static const uint8 swID[2] = {'1', '0'};
  static const uint8 hwID[2] = {'1', '0'};
  
  uint32 currentAddress = 0;

  uint8     packetBuffer[Bootloader_SIZEOF_COMMAND_BUFFER];

  /* Initialize communications channel. */
  CyBtldrCommStart();
  counterVal = 0;
  do
  {
    do
    {
      readStat = CyBtldrCommRead(packetBuffer,
                                 Bootloader_SIZEOF_COMMAND_BUFFER,
                                 &numberRead,
                                 10);
      counterVal = Reset_Timer_ReadCounter() * -1;
      #ifdef USE_UART
      char dbstring[24];
      sprintf(dbstring,"%ld\r", counterVal);
      UART_PutString(dbstring);
      CyDelay(50);
      #endif
      if (counterVal >= timeout)
      {    
        Bootloader_SET_RUN_TYPE(Bootloader_SCHEDULE_BTLDB);
        CySoftwareReset();
      }
    } while ( readStat != CYRET_SUCCESS );

    Reset_Timer_Stop();
    Reset_Timer_WriteCounter(0);
    Reset_Timer_Start();
    
    switch(packetBuffer[0])
    {
    /*************************************************************************
    *   Set read/write address
    *************************************************************************/
    case 'A':
      currentAddress = packetBuffer[1] << 9;
      currentAddress |= packetBuffer[2] << 1;
      packetBuffer[0] = '\r';
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;
      
    /*************************************************************************
    *   Set read/write address
    *************************************************************************/
    case 'H':
      currentAddress = packetBuffer[1] << 17;
      currentAddress |= packetBuffer[2] << 9;
      currentAddress |= packetBuffer[3] << 1;
      packetBuffer[0] = '\r';
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;
      
    /*************************************************************************
    *   Erase chip (unimplemented)
    *************************************************************************/
    case 'e':
      break;

    /*************************************************************************
    *   Enter/Leave bootloader mode - UNUSED
    *************************************************************************/
    case 'P':
    case 'L':
      packetBuffer[0] = '\r';
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;

    /*************************************************************************
    *   Exit bootloader
    *************************************************************************/
    case 'E':
      /* Normally, the CyBootloader checks the validity of the app here. We will
      *   assume that the app is valid b/c it was checked as it was being
      *   uploaded. */
      Bootloader_SET_RUN_TYPE(Bootloader_SCHEDULE_BTLDB);
      packetBuffer[0] = '\r';
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      while (USBUART_CDCIsReady() == 0)
      { /* wait for USB to finish sending response to the exit command */ }
      CySoftwareReset();
      /* Will never get here */
      break;
      
    /*************************************************************************
    *   Block read
    *************************************************************************/
    case 'g':
      bytesToRead = packetBuffer[1] << 8;
      bytesToRead |= packetBuffer[2];
      int16 idx;
      for(idx = 0u; idx < bytesToRead; idx++)
      {
        packetBuffer[idx] = Bootloader_GET_CODE_BYTE(currentAddress + idx);
      }
      CyBtldrCommWrite(packetBuffer, bytesToRead, NULL, 0);
      break;
      
    /*************************************************************************
    *   Block load
    *************************************************************************/  
    case 'B':
      bytesToWrite = packetBuffer[1]<<8 | packetBuffer[2];
      packetBuffer[0] = BlockLoad(packetBuffer[3], bytesToWrite, \
                                  packetBuffer + 4, currentAddress);
      if ((packetBuffer[0] == '\r') && packetBuffer[3] == 'F')
      {
        currentAddress += bytesToWrite;
      }
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;
    /*************************************************************************
    *   Report device ID
    *************************************************************************/
    case 'S':
      CyBtldrCommWrite((uint8*)deviceID, 8, NULL, 0);
      break;
    /*************************************************************************
    *   Report firmware revision
    *************************************************************************/
    case 'V':
      CyBtldrCommWrite((uint8*)swID, 2, NULL, 0);
      break;
    /*************************************************************************
    *   Report programmer type ('S' for "Serial")
    *************************************************************************/
    case 'p':
      packetBuffer[0] = 'S';
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;
    /*************************************************************************
    *   Report autoincrement address support ('Y' for "Yes")
    *************************************************************************/
    case 'a':
      packetBuffer[0] = 'Y';
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;
    /*************************************************************************
    *   Report block write support ('Y' for "Yes", then two bytes of block size
    *    written MSB first.
    *************************************************************************/
    case 'b':
      packetBuffer[0] = 'Y';
      packetBuffer[1] = 0x01;
      packetBuffer[2] = 0x00;
      CyBtldrCommWrite(packetBuffer, 3, NULL, 0);
      break;
    /*************************************************************************
    *   Report hardware device version
    *************************************************************************/
    case 'v':
      CyBtldrCommWrite((uint8*)hwID, 2, NULL, 0);
      break;
    /*************************************************************************
    *   Report (bogus) part ID, followed by list terminator 0x00.
    *************************************************************************/
    case 't':
      packetBuffer[0] = 0x44;
      packetBuffer[1] = 0;
      CyBtldrCommWrite(packetBuffer, 2, NULL, 0);
      break;
    /*************************************************************************
    *   Report device signature (this is that of the Atmega32u4)
    *************************************************************************/
    case 's':
      packetBuffer[0] = 0x87;
      packetBuffer[1] = 0x95;
      packetBuffer[2] = 0x1e;
      CyBtldrCommWrite(packetBuffer, 3, NULL, 0);
      break;
    /*************************************************************************
    *   Unimplemented fuse AVR fuse register read/write
    *************************************************************************/
    case 'N':  
    case 'Q':
    case 'F':
    case 'r':
      packetBuffer[0] = 0x00;
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;
    /*************************************************************************
    *   Unused but implemented in AVRDUDE, so some response needed.
    *************************************************************************/
    case 'T':
    case 'x':
    case 'y':
      packetBuffer[0] = '\r';
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;
    /*************************************************************************
    *   Unsupported command
    *************************************************************************/
    default:
      packetBuffer[0] = '?';
      CyBtldrCommWrite(packetBuffer, 1, NULL, 0);
      break;
    }
  } while (1);
}
int main(void)
{

/*  Initialise hardware timers and ports. All start off as inputs */
/*  PD0 is RxD
    PD1 is TxD */
/** PORTB is set as outputs on bits 0,3,4 to set controls.
    The SPI output ports SCK and MOSI stay as inputs until ready to program.
    Set the Ports PB0-2 as outputs and set high to turn off all LEDs
    PB0 = programming mode LED
    PB3 = programming completed LED (and serial comms switch over)
    PB4 = programming active LED (and reset out)
    PB5 = MOSI
    PB6 = MISO
    PB7 = SCK */

    sbi(ACSR,7);                                        // Turn off Analogue Comparator
    initbootuart();           	                        // Initialize UART.
    uint8_t sigByte1=0;                                 // Target Definition defaults
    uint8_t sigByte2=0;
    uint8_t sigByte3=0;
    uint8_t fuseBits=0;
    uint8_t highFuseBits=0;
    uint8_t extendedFuseBits=0;
    uint8_t lockBits=0;

/*---------------------------------------------------------------------------*/
/* Main loop. We exit this only with an "E" command. */
    {
        for(;;)
        {
            command=recchar();                          // Loop and wait for command character.

/** 'a' Check autoincrement status.
This allows a block of data to be uploaded to consecutive addresses without
specifying the address each time */
            if (command=='a')
            {
                sendchar('Y');                          // Yes, we will autoincrement.
            }

/** 'A' Set address.
This is stored and incremented for each upload/download.
NOTE: Flash addresses are given as word addresses, not byte addresses. */
            else if (command=='A')                       // Set address
            {
                address = (recchar()<<8);               // Set address high byte first.
                address |= recchar();                   // Then low byte.
                sendchar('\r');                         // Send OK back.
            }

/** 'b' Check block load support. This returns the allowed block size.
We will not buffer anything so we will use the FLASH page size to limit
the programmer's blocks to those that will fit the target's page. This then avoids
the long delay when the page is committed, that may cause incoming data to be lost.
This should not be called before the P command is executed, and the target device
characteristics obtained. The fPageSize characteristic should be non zero.*/
           else if (command=='b')
            {
                uint16_t blockLength = ((uint16_t)fPageSize<<1);
                if (fPageSize > 0) sendchar('Y');       // Report block load supported.
                else sendchar('N');
                sendchar(high(blockLength));            // MSB first.
                sendchar(low(blockLength));             // Report FLASH pagesize (bytes).
            }

/** 'p' Get programmer type. This returns just 'S' for serial. */
            else if (command=='p')
            {
                sendchar('S');                          // Answer 'SERIAL'.
            }

/** 'S' Return programmer identifier. Always 7 digits. We call it AVRSPRG */
            else if (command=='S')
            {
                sendchar('A');
                sendchar('V');                          // ID always 7 characters.
                sendchar('R');
                sendchar('S');
                sendchar('P');
                sendchar('R');
                sendchar('G');
            }

/** 'V' Return software version. */
            else if (command=='V')
            {
                sendchar('0');
                sendchar('0');
            }

/** 't' Return supported device codes. This returns a list of devices that can be programmed.
This is only used by AVRPROG so we will not use it - we work with signature bytes instead. */
            else if(command=='t')
            {
                sendchar( 0 );                          // Send list terminator.
            }

/** 'x' Set LED. */
            else if ((command=='x') || (command=='y') || (command=='T'))
            {
//                if (command=='x') sbi(PORTB,LED);
//                else if (command=='y') cbi(PORTB,LED);
                recchar();                              // Discard sent value
                sendchar('\r');                         // Send OK back.
            }

/** 'P' Enter programming mode.
This starts the programming of the device. Pulse the reset line high while SCK is low.
Send the command and ensure that the echoed second byte is correct, otherwise redo.
With this we get the device signature and search the table for its characteristics.
A timeout is provided in case the device doesn't respond. This will allow fall through
to an ultimate error response.
The reset line is held low until programming mode is exited. */
            else if (command=='P')
            {
                outb(DDRB,(inb(DDRB) | 0xB9));          // Setup SPI output ports
                outb(PORTB,(inb(PORTB) | 0xB9));        // SCK and MOSI high, and LEDs off
                uint8_t retry = 10;
                uint8_t result = 0;
                while ((result != 0x53) && (retry-- > 0))
                {
                    cbi(PORTB,SCK);                     // Set serial clock low
                    sbi(PORTB,RESET);                   // Pulse reset line off
                    _delay_us(100);                     // Delay to let CPU know that programming will occur
                    cbi(PORTB,RESET);                   // Pulse reset line on
                    _delay_us(25000);                   // 25ms delay
                    writeCommand(0xAC,0x53,0x00,0x00);  // "Start programming" command
                    result=buffer[2];
                }
/** Once we are in programming mode, grab the signature bytes and extract all information
about the target device such as its memory sizes, page sizes and capabilities. */
                writeCommand(0x30,0x00,0x00,0x00);
                sigByte1 = buffer[3];
                writeCommand(0x30,0x00,0x01,0x00);
                sigByte2 = buffer[3];
                writeCommand(0x30,0x00,0x02,0x00);      // Signature Bytes
                sigByte3 = buffer[3];
/* Check for device support. If the first signature byte is not 1E, then the device is
either not an Atmel device, is locked, or is not responding.*/
                uint8_t found=FALSE;                    // Indicates if the target device is supported
                uint8_t partNo = 0;
                if (sigByte1 == 0x1E)
                {
                    while ((partNo < NUMPARTS) && (! found))
                    {
                        found = ((part[partNo][0] == sigByte2) && (part[partNo][1] == sigByte3));
                        partNo++;
                    }
                }
                if (found)
                {
                    partNo--;
                    sendchar('\r');
                    fPageSize = part[partNo][2];
                    ePageSize = part[partNo][3];
                    canCheckBusy = part[partNo][4];
                    lfCapability = part[partNo][5];
                    buffer[3] = 0;                      // In case we cannot read these
                    if (lfCapability & 0x08) writeCommand(0x50,0x08,0x00,0x00);  // Read Extended Fuse Bits
                    extendedFuseBits = buffer[3];
                    if (lfCapability & 0x04) writeCommand(0x58,0x08,0x00,0x00);  // Read High Fuse Bits
                    highFuseBits = buffer[3];
                    if (lfCapability & 0x02) writeCommand(0x50,0x00,0x00,0x00);  // Read Fuse Bits
                    fuseBits = buffer[3];
                    if (lfCapability & 0x01) writeCommand(0x58,0x00,0x00,0x00);  // Read Lock Bits
                    lockBits = buffer[3];
                }
                else                                    // Not found?
                {
                    sbi(PORTB,RESET);                   // Lift reset line
                    sendchar('?');                      // Device cannot be programmed
                    outb(DDRB,(inb(DDRB) & ~0xA0));     // Set SPI ports to inputs
                }
            }

/** 'L' Leave programming mode. */
            else if(command=='L')
            {
                sbi(PORTB,RESET);                       // Turn reset line off
                sendchar('\r');                         // Answer OK.
                outb(DDRB,(inb(DDRB) & ~0xA0));         // Set SPI ports to inputs
            }

/** 'e' Chip erase.
This requires several ms. Ensure that the command has finished before acknowledging. */
            else if (command=='e')
            {
                writeCommand(0xAC,0x80,0x00,0x00);      // Erase command
                pollDelay(FALSE);
                sendchar('\r');                         // Send OK back.
            }

/** 'R' Read program memory */
            else if (command=='R')
            {
/** Send high byte, then low byte of flash word.
Send each byte from the address specified (note address variable is modified).*/
                lsbAddress = low(address);
                msbAddress = high(address);
                writeCommand(0x28,msbAddress,lsbAddress,0x00);  // Read high byte
                sendchar(buffer[3]);
                writeCommand(0x20,msbAddress,lsbAddress,0x00);  // Read low byte
                sendchar(buffer[3]);
                address++;                              // Auto-advance to next Flash word.
            }

/** 'c' Write to program memory page buffer, low byte.
NOTE: Always use this command before sending the high byte. It is written to the
page but the address is not incremented.*/
            else if (command=='c')
            {
                received = recchar();
                writeCommand(0x40,0x00,address & 0x7F,received);    // Low byte
                sendchar('\r');                         // Send OK back.
            }

/** 'C' Write to program memory page buffer, high byte.
This will cause the word to be written to the page and the address incremented. It is
the responsibility of the user to issue a page write command.*/
            else if (command=='C')
            {
                received = recchar();
                writeCommand(0x48,0x00,address & 0x7F,received);    // High Byte
                address++;                              // Auto-advance to next Flash word.
                sendchar('\r');                         // Send OK back.
            }

/** 'm' Issue Page Write. This writes the target device page buffer to the Flash.
The address is that of the page, with the lower bits masked out.
This requires several ms. Ensure that the command has finished before acknowledging.
We could check for end of memory here but that would require storing the Flash capacity
for each device. The calling program will know in any case if it has overstepped.*/
            else if (command== 'm')
            {
                writeCommand(0x4C,(address>>8) & 0x7F,address & 0xE0,0x00);  // Write Page
                pollDelay(TRUE);                        // Short delay
                sendchar('\r');                         // Send OK back.
            }
/** 'D' Write EEPROM memory
This writes the byte directly to the EEPROM at the specified address.
This requires several ms. Ensure that the command has finished before acknowledging.*/
            else if (command == 'D')
            {
                lsbAddress = low(address);
                msbAddress = high(address);
                writeCommand(0xC0,msbAddress,lsbAddress,recchar());     // EEPROM byte
                address++;                              // Auto-advance to next EEPROM byte.
                pollDelay(FALSE);                       // Long delay
                sendchar('\r');                         // Send OK back.
            }

/** 'd' Read EEPROM memory */
            else if (command == 'd')
            {
                lsbAddress = low(address);
                msbAddress = high(address);
                writeCommand(0xA0,msbAddress,lsbAddress, 0x00);
                sendchar(buffer[3]);
                address++;                              // Auto-advance to next EEPROM byte.
            }

/** 'B' Start block load.
 The address must have already been set otherwise it will be undefined. */
            else if (command=='B')
            {
                tempInt = (recchar()<<8);               // Get block size high byte first.
                tempInt |= recchar();                   // Low Byte.
                sendchar(BlockLoad(tempInt,recchar(),&address));  // Block load.
            }

/** 'g' Start block read.
 The address must have already been set otherwise it will be undefined.*/
            else if (command=='g')
            {
                tempInt = (recchar()<<8);               // Get block size high byte first.
                tempInt |= recchar();                   // Low Byte.
                command = recchar();                    // Get memory type
                BlockRead(tempInt,command,&address);    // Block read
            }
/** 'r' Read lock bits. */
            else if (command=='r')
            {
                sendchar(lockBits);
            }

/** 'l' Write lockbits. */
            else if (command=='l')
            {
                if (lfCapability & 0x10) writeCommand(0xAC,0xE0,0x00,recchar());
                sendchar('\r');                         // Send OK back.
            }

/** 'F' Read fuse bits. */
            else if (command=='F')
            {
                sendchar(fuseBits);
            }

/** 'f' Write fuse bits. */
            else if (command=='f')
            {
                if (lfCapability & 0x20) writeCommand(0xAC,0xA0,0x00,recchar()); // Fuse byte
                sendchar('\r');                         // Send OK back.
            }

/** 'N' Read high fuse bits. */
            else if (command=='N')
            {
                sendchar(highFuseBits);
            }

/** 'n' Write high fuse bits. */
            else if (command=='n')
            {
                if (lfCapability & 0x40) writeCommand(0xAC,0xA8,0x00,recchar()); // High Fuse byte
                sendchar('\r');                         // Send OK back.
            }

/** 'Q' Read extended fuse bits. */
            else if (command=='Q')
            {
                sendchar(extendedFuseBits);
            }

/** 'q' Write extended fuse bits. */
            else if (command=='q')
            {
                if (lfCapability & 0x80) writeCommand(0xAC,0xA4,0x00,recchar()); // Extended Fuse byte
                sendchar('\r');                         // Send OK back.
            }

/** 's' Return signature bytes. Sent Most Significant Byte first. */
            else if (command=='s')
            {
                sendchar(sigByte3);
                sendchar(sigByte2);
                sendchar(sigByte1);
            }

/** 'E' Exit bootloader.
At this command we enter serial passthrough and don't return from it until a
hardware reset occurs.
At the same time we should lift the reset from the target. Spin endlessly so we
don't interpret serial data, and wait for our own hard reset.*/
            else if (command=='E')
            {
                sendchar('\r');
                sbi(PORTB,RESET);                       // Pulse reset line off
                cbi(PORTB,PASSTHROUGH);                 // Change to serial passthrough
                outb(DDRB,(inb(DDRB) & ~0xA0));         // Set SPI ports to inputs
                for (;;);                    // Spin endlessly
            }

/** The last command to accept is ESC (synchronization).
This is used to abort any command by filling in remaining parameters, after which
it is simply ignored.
Otherwise, the command is not recognized and a "?" is returned.*/
            else if (command!=0x1b) sendchar('?');       // If not ESC, then it is unrecognized
        }
#ifndef REMOVE_BLOCK_SUPPORT
            // Check block load support.
            else if(val=='b')
		    {
    			sendchar('Y'); // Report block load supported.
    			sendchar((BLOCKSIZE>>8) & 0xFF); // MSB first.
    			sendchar(BLOCKSIZE&0xFF); // Report BLOCKSIZE (bytes).
    		}


            // Start block load.
    		else if(val=='B')
    		{
	    	    temp_int = (recchar()<<8) | recchar(); // Get block size.
		    	val = recchar(); // Get memtype.
			    sendchar( BlockLoad(temp_int,val,&address) ); // Block load.
		    }
		
		    
		    // Start block read.
    		else if(val=='g')
    		{
	    	    temp_int = (recchar()<<8) | recchar(); // Get block size.
    			val = recchar(); // Get memtype
	    		BlockRead(temp_int,val,&address); // Block read
    		}		
#endif /* REMOVE_BLOCK_SUPPORT */

#ifndef REMOVE_FLASH_BYTE_SUPPORT            
            // Read program memory.
            else if(val=='R')
Esempio n. 4
0
int main(void){
	unsigned int temp_int;
	unsigned char val = 0;

	CTS_DDR_REG |= (1<<CTS_PIN);	// set to output
	CTS_PORT_REG |= (1<<CTS_PIN);	// set high

	RTS_DDR_REG &= ~(1<<CTS_PIN);	// set to input
	RTS_PORT_REG |= (1<<CTS_PIN);	// enable pull-up
	
	mod_led_init();

	InitTWI();
	
	initbootuart(); // Initialize UART.

	/* Main loop */
	while(true)
	{
		val = recchar(); // Wait for command character.

		switch(val) {
			case 'P':
			case 'L':
			case 'E':
				sendchar('\r');
				break;
		
			// Read lock byte -> execute command
			case 'r':
				switch(command_char) {
					case 'a':
						// NOT SUPPORTED in new code.
						read_and_send( TWI_CMD_AVERSION );
						break;

					case 'b':
						// NOT SUPPORTED in new code.
						read_and_send( TWI_CMD_BVERSION );
						break;

					case 'd':
						// Read CRCHI
						sendchar(CRC_HI);
						break;

					case 'e':
						// Read CRCLO
						sendchar(CRC_LO);
						break;

					case 'f':
						// Status condition
						// NOT SUPPORTED in new code.
						read_and_send(TWI_CMD_GETERRCONDN);
						break;

					default:
						sendchar(0xFF);
						break;
				}
				break;

			case 'l':
				// Write lock byte -> load command. NOT SUPPORTED in new code.
				// NOTE: This looks like a hijacked command to do a CRC check.
				command_char = recchar();
#if 0
				if( command_char == 'c' )
				{
					send_command( TWI_CMD_CRCCHECK );
					read_from_slave();
					CRC_HI= statusCode;
					read_from_slave();
					CRC_LO = statusCode;
				}
#endif
				sendchar('\r');
				break;

			case 'N':
				// Read high fuse bits -> BVERSION
				read_and_send( TWI_CMD_BVERSION );
				break;

			case 'F':
				// Low Fuse Bits -> AVERSION
				read_and_send( TWI_CMD_AVERSION );
				break;

			case	'a':
				sendchar('Y'); // Yes, we do auto-increment.
				break;

			case 'A':
				addr =(recchar()<<8) | recchar(); // Read address high and low byte.
				if(addr > MAX__APP_ADDR) over_size_flag = 1;
				//+ 15mar17 ndp - send address to Slave.
				slaveCmdBuff[0] = CMD_RECV_ADRS;
				slaveCmdBuff[1] = (uint8_t)((addr>>8) & 0x00FF);				// AH
				slaveCmdBuff[2] = (uint8_t)(addr & 0x00FF);						// AL
				(void) MasterTransmit( SLAVE_ADDRESS, slaveCmdBuff, 3 );
				//-
				sendchar('\r'); // Send OK back.
				break;

			case 'e':
				// Chip erase.	NOT SUPPORTED in new code.
#if 0
				runApp[0] =  TWI_CMD_ERASEFLASH;
				runApp[1] =  TWI_CMD_ERASEFLASH;
				get_slave_status();
				success = MasterTransmit( SLAVE_ADDRESS, runApp, 2 );
#endif
				sendchar('\r'); // Send OK back.
				break;
		
			case 'b':
				// Check block load support.
				sendchar('Y'); // Report block load supported.
				sendchar((BLOCKSIZE>>8) & 0xFF); // MSB first.
				sendchar(BLOCKSIZE&0xFF); // Report BLOCKSIZE (bytes).
				over_size_flag = 0;		// ndp 1-29-2017 fix
				break;
		
			case 'B':
				// Start block load.
				temp_int = (recchar()<<8) | recchar();	// Get block size.
				val = recchar();						// Get memtype.
				sendchar( BlockLoad(temp_int, val) );	// Block load.
// mod_led_toggle(4);			// Need a short delay here.
			 	pageBuffer[0] = CMD_RECV_DATA;					// Address was sent in 'A' command service.
				pageBuffer[1] = (uint8_t)(temp_int & 0x00FF);	// NL..Only block size less than 256 supported.
				// NOTE: Always sends PAGE_SIZE even if less data received from Host.
				success = MasterTransmit( SLAVE_ADDRESS, pageBuffer, pageBuffer[1]+2 );

				break;
		
			case 'S':
				// Return programmer identifier.
				sendchar('A'); // Return 'AVRBOOT'.
				sendchar('V'); // Software identifier (aka programmer signature) is always 7 characters.
				sendchar('R');
				sendchar('B');
				sendchar('O');
				sendchar('O');
				sendchar('T');
				reps =0;
				break;
		
			case 'V':
				// Return software version.
				// NOTE: TODO Should implement in new code.
//				send_command(TWI_CMD_EXECUTEAPP);
				// Disable bootloader mode for slave
				sendchar('2');
				sendchar('0');
				break;

			case 's':
				// Return signature bytes [for the Target device ATtiny85].
				slaveCmdBuff[0] = CMD_GET_SIG;
				(void) MasterTransmit( SLAVE_ADDRESS, slaveCmdBuff, 1 );
 mod_led_toggle(200);			// Need a short delay here to let Slave set up data.
				(void) MasterReceive( SLAVE_ADDRESS, slaveCmdBuff, 3 );
				sendchar( slaveCmdBuff[2] );
				sendchar( slaveCmdBuff[1] );
				sendchar( slaveCmdBuff[0] );
				break;
		
			/* Add missing command .. ndp 01-29-2017
			 * Return Flash Data.
			 *
			 * TODO: Need to read from Slave.
			 */
			case 'g':
 				temp_int = (recchar()<<8) | recchar();	// Get block size.
				val = recchar();						// Get mem type.
				// NOTE: Address was sent in 'A' command process.
				slaveCmdBuff[0] = CMD_GET_DATA;
				slaveCmdBuff[1] = (uint8_t)(temp_int & 0x00FF);
				(void) MasterTransmit( SLAVE_ADDRESS, slaveCmdBuff, 2 );
 mod_led_toggle(200);			// Need a short delay here to let Slave set up data.
				(void) MasterReceive( SLAVE_ADDRESS, pageBuffer, (temp_int & 0x00FF) );

				for(int i=0; i<temp_int; ++i)
				{
					sendchar( pageBuffer[i] );
				}
				break;

			default:
				if(val != 0x1b) {                  // If not ESC, then it is unrecognized...
					sendchar('?');
				}
				break;
		} // end: switch()
	} // end: while(true)
} // end: main