2011-12-10 06:11:31 +00:00
|
|
|
/*
|
|
|
|
* usb_serial.c
|
|
|
|
*
|
|
|
|
* Created on: Dec 9, 2011
|
|
|
|
* Author: Doug
|
2012-05-26 03:21:04 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011-2012 Doug Brown
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2011-12-10 06:11:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "usb_serial.h"
|
|
|
|
#include "../LUFA/Drivers/USB/USB.h"
|
2011-12-26 23:02:39 +00:00
|
|
|
#include "../cdc_device_definition.h"
|
2011-12-10 18:35:41 +00:00
|
|
|
#include "../external_mem.h"
|
2011-12-11 16:35:53 +00:00
|
|
|
#include "../tests/simm_electrical_test.h"
|
2011-12-26 19:31:45 +00:00
|
|
|
#include "../programmer_protocol.h"
|
2012-05-27 22:41:16 +00:00
|
|
|
#include "../led.h"
|
2012-09-30 21:41:22 +00:00
|
|
|
#include <stdbool.h>
|
2011-12-11 16:35:53 +00:00
|
|
|
|
2012-09-15 01:13:19 +00:00
|
|
|
#define MAX_CHIP_SIZE (2UL * 1024UL * 1024UL)
|
2011-12-11 16:35:53 +00:00
|
|
|
#define READ_CHUNK_SIZE_BYTES 1024UL
|
2011-12-17 04:13:34 +00:00
|
|
|
#define WRITE_CHUNK_SIZE_BYTES 1024UL
|
2012-10-17 01:36:55 +00:00
|
|
|
#define ERASE_SECTOR_SIZE_BYTES (256UL * 1024UL)
|
2011-12-11 16:35:53 +00:00
|
|
|
#if ((READ_CHUNK_SIZE_BYTES % 4) != 0)
|
|
|
|
#error Read chunk size should be a multiple of 4 bytes
|
|
|
|
#endif
|
2011-12-17 04:13:34 +00:00
|
|
|
#if ((WRITE_CHUNK_SIZE_BYTES % 4) != 0)
|
|
|
|
#error Write chunk size should be a multiple of 4 bytes
|
|
|
|
#endif
|
2011-12-10 06:11:31 +00:00
|
|
|
|
|
|
|
void USBSerial_Init(void)
|
|
|
|
{
|
|
|
|
USB_Init();
|
|
|
|
}
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Internal state so we know how to interpret the next-received byte
|
2011-12-11 16:35:53 +00:00
|
|
|
typedef enum ProgrammerCommandState
|
|
|
|
{
|
|
|
|
WaitingForCommand = 0,
|
2013-07-04 19:10:18 +00:00
|
|
|
//ReadingByteWaitingForAddress, // TODO
|
2012-05-20 05:58:17 +00:00
|
|
|
ReadingChipsReadLength,
|
2011-12-11 16:35:53 +00:00
|
|
|
ReadingChips,
|
2013-07-04 19:10:18 +00:00
|
|
|
//ReadingChipsUnableSendError, // TODO
|
2012-10-17 01:36:55 +00:00
|
|
|
WritingChips,
|
2012-10-18 04:31:00 +00:00
|
|
|
ErasePortionReadingPosLength,
|
|
|
|
ReadingChipsReadStartPos,
|
|
|
|
WritingChipsReadingStartPos,
|
2013-07-04 19:10:18 +00:00
|
|
|
ReadingChipsMask,
|
2011-12-11 16:35:53 +00:00
|
|
|
} ProgrammerCommandState;
|
|
|
|
static ProgrammerCommandState curCommandState = WaitingForCommand;
|
2012-01-27 04:42:42 +00:00
|
|
|
|
|
|
|
// State info for reading/writing
|
2013-07-04 19:10:18 +00:00
|
|
|
//static uint8_t byteAddressReceiveCount = 0;
|
2011-12-11 16:35:53 +00:00
|
|
|
static uint16_t curReadIndex;
|
2012-05-20 05:58:17 +00:00
|
|
|
static uint32_t readLength;
|
|
|
|
static uint8_t readLengthByteIndex;
|
2011-12-17 04:13:34 +00:00
|
|
|
static int16_t writePosInChunk = -1;
|
|
|
|
static uint16_t curWriteIndex = 0;
|
2012-09-30 21:41:22 +00:00
|
|
|
static bool verifyDuringWrite = false;
|
2012-10-17 01:36:55 +00:00
|
|
|
static uint32_t erasePosition;
|
|
|
|
static uint32_t eraseLength;
|
2013-07-04 19:10:18 +00:00
|
|
|
static uint8_t chipsMask = ALL_CHIPS;
|
2011-12-11 16:35:53 +00:00
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Private functions
|
2011-12-11 16:35:53 +00:00
|
|
|
void USBSerial_HandleWaitingForCommandByte(uint8_t byte);
|
2011-12-17 04:13:34 +00:00
|
|
|
void USBSerial_HandleReadingChipsByte(uint8_t byte);
|
2012-05-20 05:58:17 +00:00
|
|
|
void USBSerial_HandleReadingChipsReadLengthByte(uint8_t byte);
|
2011-12-11 16:35:53 +00:00
|
|
|
void USBSerial_SendReadDataChunk(void);
|
2011-12-17 04:13:34 +00:00
|
|
|
void USBSerial_HandleWritingChipsByte(uint8_t byte);
|
2011-12-18 18:18:30 +00:00
|
|
|
void USBSerial_ElectricalTest_Fail_Handler(uint8_t index1, uint8_t index2);
|
2012-10-17 01:36:55 +00:00
|
|
|
void USBSerial_HandleErasePortionReadPosLengthByte(uint8_t byte);
|
2012-10-18 04:31:00 +00:00
|
|
|
void USBSerial_HandleReadingChipsReadStartPosByte(uint8_t byte);
|
|
|
|
void USBSerial_HandleWritingChipsReadingStartPosByte(uint8_t byte);
|
2013-07-04 19:10:18 +00:00
|
|
|
void USBSerial_HandleReadingChipsMaskByte(uint8_t byte);
|
2011-12-17 04:13:34 +00:00
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Read/write to USB serial macros -- easier than retyping
|
|
|
|
// CDC_Device_XXX(&VirtualSerial_CDC_Interface...) every time
|
2011-12-17 04:13:34 +00:00
|
|
|
#define SendByte(b) CDC_Device_SendByte(&VirtualSerial_CDC_Interface, b)
|
|
|
|
#define ReadByte() CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)
|
|
|
|
#define SendData(d, l) CDC_Device_SendData(&VirtualSerial_CDC_Interface, d, l)
|
2011-12-11 16:35:53 +00:00
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Should be called periodically in the main loop
|
2011-12-10 06:11:31 +00:00
|
|
|
void USBSerial_Check(void)
|
|
|
|
{
|
2012-01-27 04:42:42 +00:00
|
|
|
// If we're configured, read a byte (if one is available) and process it
|
2011-12-17 04:13:34 +00:00
|
|
|
if (USB_DeviceState == DEVICE_STATE_Configured)
|
2011-12-11 16:35:53 +00:00
|
|
|
{
|
2011-12-17 04:13:34 +00:00
|
|
|
int16_t recvByte = ReadByte();
|
2011-12-11 16:35:53 +00:00
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Did we get a byte? If so, hand it off to the correct handler
|
|
|
|
// function based on the current state
|
2011-12-11 16:35:53 +00:00
|
|
|
if (recvByte >= 0)
|
|
|
|
{
|
|
|
|
switch (curCommandState)
|
|
|
|
{
|
|
|
|
case WaitingForCommand:
|
|
|
|
USBSerial_HandleWaitingForCommandByte((uint8_t)recvByte);
|
|
|
|
break;
|
2012-05-20 05:58:17 +00:00
|
|
|
case ReadingChipsReadLength:
|
|
|
|
USBSerial_HandleReadingChipsReadLengthByte((uint8_t)recvByte);
|
|
|
|
break;
|
2011-12-17 04:13:34 +00:00
|
|
|
case ReadingChips:
|
|
|
|
USBSerial_HandleReadingChipsByte((uint8_t)recvByte);
|
|
|
|
break;
|
|
|
|
case WritingChips:
|
|
|
|
USBSerial_HandleWritingChipsByte((uint8_t)recvByte);
|
|
|
|
break;
|
2012-10-17 01:36:55 +00:00
|
|
|
case ErasePortionReadingPosLength:
|
|
|
|
USBSerial_HandleErasePortionReadPosLengthByte((uint8_t)recvByte);
|
|
|
|
break;
|
2012-10-18 04:31:00 +00:00
|
|
|
case ReadingChipsReadStartPos:
|
|
|
|
USBSerial_HandleReadingChipsReadStartPosByte((uint8_t)recvByte);
|
|
|
|
break;
|
|
|
|
case WritingChipsReadingStartPos:
|
|
|
|
USBSerial_HandleWritingChipsReadingStartPosByte((uint8_t)recvByte);
|
|
|
|
break;
|
2013-07-04 19:10:18 +00:00
|
|
|
case ReadingChipsMask:
|
|
|
|
USBSerial_HandleReadingChipsMaskByte((uint8_t)recvByte);
|
|
|
|
break;
|
2011-12-11 16:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-17 04:13:34 +00:00
|
|
|
}
|
2011-12-11 16:35:53 +00:00
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// And do the periodic CDC and USB tasks...
|
2011-12-10 06:11:31 +00:00
|
|
|
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
|
|
|
|
USB_USBTask();
|
|
|
|
}
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// If we're in the "waiting for command" state, handle the command...
|
2011-12-11 16:35:53 +00:00
|
|
|
void USBSerial_HandleWaitingForCommandByte(uint8_t byte)
|
|
|
|
{
|
|
|
|
switch (byte)
|
|
|
|
{
|
2012-01-27 04:42:42 +00:00
|
|
|
// Asked to enter waiting mode -- we're already there, so say OK.
|
2011-12-11 16:35:53 +00:00
|
|
|
case EnterWaitingMode:
|
2011-12-26 19:31:45 +00:00
|
|
|
SendByte(CommandReplyOK);
|
2011-12-11 16:35:53 +00:00
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// Asked to do the electrical test. Reply OK, and then do the test,
|
|
|
|
// sending whatever replies necessary
|
2011-12-11 16:35:53 +00:00
|
|
|
case DoElectricalTest:
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(CommandReplyOK);
|
2012-05-13 16:40:04 +00:00
|
|
|
// Force LUFA to send initial "OK" reply immediately in this case
|
|
|
|
// so the caller gets immediate feedback that the test has started
|
|
|
|
CDC_Device_Flush(&VirtualSerial_CDC_Interface);
|
2011-12-18 18:18:30 +00:00
|
|
|
SIMMElectricalTest_Run(USBSerial_ElectricalTest_Fail_Handler);
|
|
|
|
SendByte(ProgrammerElectricalTestDone);
|
2011-12-11 16:35:53 +00:00
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// Asked to identify the chips in the SIMM. Identify them and send reply.
|
2011-12-11 16:35:53 +00:00
|
|
|
case IdentifyChips:
|
|
|
|
{
|
2012-05-14 00:48:35 +00:00
|
|
|
struct ChipID chips[NUM_CHIPS];
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(CommandReplyOK);
|
2011-12-11 16:35:53 +00:00
|
|
|
ExternalMem_IdentifyChips(chips);
|
2011-12-18 18:18:30 +00:00
|
|
|
int x;
|
2012-05-14 00:48:35 +00:00
|
|
|
for (x = 0; x < NUM_CHIPS; x++)
|
2011-12-18 18:18:30 +00:00
|
|
|
{
|
|
|
|
SendByte(chips[x].manufacturerID);
|
|
|
|
SendByte(chips[x].deviceID);
|
|
|
|
}
|
|
|
|
SendByte(ProgrammerIdentifyDone);
|
2011-12-11 16:35:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-01-27 04:42:42 +00:00
|
|
|
// Asked to read a single byte from each SIMM. Change the state and reply.
|
2011-12-11 16:35:53 +00:00
|
|
|
case ReadByte:
|
2013-07-04 19:10:18 +00:00
|
|
|
/*curCommandState = ReadingByteWaitingForAddress;
|
2011-12-11 16:35:53 +00:00
|
|
|
byteAddressReceiveCount = 0;
|
2013-07-04 19:10:18 +00:00
|
|
|
SendByte(CommandReplyOK);*/
|
|
|
|
SendByte(CommandReplyInvalid); // not implemented yet
|
2011-12-11 16:35:53 +00:00
|
|
|
break;
|
2012-10-18 04:31:00 +00:00
|
|
|
// Asked to read all four chips. Set the state, reply with the first chunk.
|
|
|
|
// This will read from the BEGINNING of the SIMM every time. Use
|
|
|
|
// ReadChipsAt to specify a start position
|
2011-12-11 16:35:53 +00:00
|
|
|
case ReadChips:
|
2012-05-20 05:58:17 +00:00
|
|
|
curCommandState = ReadingChipsReadLength;
|
2011-12-11 16:35:53 +00:00
|
|
|
curReadIndex = 0;
|
2012-05-20 05:58:17 +00:00
|
|
|
readLengthByteIndex = 0;
|
|
|
|
readLength = 0;
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(CommandReplyOK);
|
2011-12-11 16:35:53 +00:00
|
|
|
break;
|
2012-10-18 04:31:00 +00:00
|
|
|
case ReadChipsAt:
|
|
|
|
curCommandState = ReadingChipsReadStartPos;
|
|
|
|
curReadIndex = 0;
|
|
|
|
readLengthByteIndex = 0;
|
|
|
|
readLength = 0;
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// Erase the chips and reply OK. (TODO: Sometimes erase might fail)
|
2011-12-11 16:35:53 +00:00
|
|
|
case EraseChips:
|
2013-07-04 19:10:18 +00:00
|
|
|
ExternalMem_EraseChips(chipsMask);
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// Begin writing the chips. Change the state, reply, wait for chunk of data
|
2011-12-11 16:35:53 +00:00
|
|
|
case WriteChips:
|
2011-12-17 04:13:34 +00:00
|
|
|
curCommandState = WritingChips;
|
|
|
|
curWriteIndex = 0;
|
|
|
|
writePosInChunk = -1;
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2012-10-18 04:31:00 +00:00
|
|
|
case WriteChipsAt:
|
|
|
|
curCommandState = WritingChipsReadingStartPos;
|
|
|
|
curWriteIndex = 0;
|
|
|
|
readLengthByteIndex = 0;
|
|
|
|
writePosInChunk = -1;
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// Asked for the current bootloader state. We are in the program right now,
|
|
|
|
// so reply accordingly.
|
2011-12-25 04:09:39 +00:00
|
|
|
case GetBootloaderState:
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
SendByte(BootloaderStateInProgrammer);
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// Enter the bootloader. Wait a bit, then jump to the bootloader location.
|
2011-12-25 04:09:39 +00:00
|
|
|
case EnterBootloader:
|
|
|
|
SendByte(CommandReplyOK);
|
2012-05-20 06:07:22 +00:00
|
|
|
CDC_Device_Flush(&VirtualSerial_CDC_Interface);
|
|
|
|
|
|
|
|
// Insert a small delay to ensure that it arrives before rebooting.
|
|
|
|
_delay_ms(1000);
|
2012-01-27 04:42:42 +00:00
|
|
|
|
|
|
|
// Done with the USB interface -- the bootloader will re-initialize it.
|
2011-12-25 04:09:39 +00:00
|
|
|
USB_Disable();
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Disable interrupts so nothing weird happens...
|
2011-12-25 04:09:39 +00:00
|
|
|
cli();
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Wait a little bit to let everything settle and let the program
|
|
|
|
// close the port after the USB disconnect
|
2011-12-25 04:09:39 +00:00
|
|
|
_delay_ms(2000);
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// And, of course, go into the bootloader.
|
2011-12-25 04:09:39 +00:00
|
|
|
__asm__ __volatile__ ( "jmp 0xE000" );
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// Enter the programmer. We're already there, so reply OK.
|
2011-12-25 04:09:39 +00:00
|
|
|
case EnterProgrammer:
|
|
|
|
// Already in the programmer
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2012-09-02 22:56:49 +00:00
|
|
|
// Set the SIMM type to the older, smaller chip size (2MB and below)
|
|
|
|
case SetSIMMTypePLCC32_2MB:
|
|
|
|
ExternalMem_SetChipType(ChipType8BitData_4MBitSize);
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
|
|
|
case SetSIMMTypeLarger:
|
|
|
|
ExternalMem_SetChipType(ChipType8Bit16BitData_16MBitSize);
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2012-09-30 21:41:22 +00:00
|
|
|
case SetVerifyWhileWriting:
|
|
|
|
verifyDuringWrite = true;
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
|
|
|
case SetNoVerifyWhileWriting:
|
|
|
|
verifyDuringWrite = false;
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2012-10-17 01:36:55 +00:00
|
|
|
case ErasePortion:
|
|
|
|
readLengthByteIndex = 0;
|
|
|
|
eraseLength = 0;
|
|
|
|
erasePosition = 0;
|
|
|
|
curCommandState = ErasePortionReadingPosLength;
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2013-07-04 19:10:18 +00:00
|
|
|
case SetChipsMask:
|
|
|
|
curCommandState = ReadingChipsMask;
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// We don't know what this command is, so reply that it was invalid.
|
2011-12-17 04:13:34 +00:00
|
|
|
default:
|
|
|
|
SendByte(CommandReplyInvalid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// If we're in the "reading chips" state, handle the incoming byte...
|
2011-12-17 04:13:34 +00:00
|
|
|
void USBSerial_HandleReadingChipsByte(uint8_t byte)
|
|
|
|
{
|
2012-01-27 04:42:42 +00:00
|
|
|
// The byte should be a reply from the computer. It should be either:
|
|
|
|
// 1) ComputerReadOK -- meaning it got the chunk we just sent
|
|
|
|
// or
|
|
|
|
// 2) ComputerReadCancel -- meaning the user canceled the read
|
2011-12-17 04:13:34 +00:00
|
|
|
switch (byte)
|
|
|
|
{
|
|
|
|
case ComputerReadOK:
|
2012-01-27 04:42:42 +00:00
|
|
|
// If they have confirmed the final data chunk, let them know
|
|
|
|
// that they have finished, and enter command state.
|
2012-05-20 05:58:17 +00:00
|
|
|
if (curReadIndex >= readLength)
|
2011-12-17 04:13:34 +00:00
|
|
|
{
|
2012-05-27 22:41:16 +00:00
|
|
|
LED_Off();
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(ProgrammerReadFinished);
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
}
|
2012-01-27 04:42:42 +00:00
|
|
|
else // There's more data left to read, so read it and send it to them!
|
2011-12-17 04:13:34 +00:00
|
|
|
{
|
2012-05-27 22:41:16 +00:00
|
|
|
LED_Toggle();
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(ProgrammerReadMoreData);
|
|
|
|
USBSerial_SendReadDataChunk();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ComputerReadCancel:
|
2012-01-27 04:42:42 +00:00
|
|
|
// If they've canceled, let them know we got their request and go back
|
|
|
|
// to "waiting for command" state
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(ProgrammerReadConfirmCancel);
|
|
|
|
curCommandState = WaitingForCommand;
|
2011-12-11 16:35:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-20 05:58:17 +00:00
|
|
|
// If we're figuring out the length to read, grab it now...
|
|
|
|
void USBSerial_HandleReadingChipsReadLengthByte(uint8_t byte)
|
|
|
|
{
|
|
|
|
// There will be four bytes, so count up until we know the length. If they
|
|
|
|
// have sent all four bytes, send the first read chunk.
|
|
|
|
readLength |= (((uint32_t)byte) << (8*readLengthByteIndex));
|
|
|
|
if (++readLengthByteIndex >= 4)
|
|
|
|
{
|
|
|
|
// Ensure it's within limits and a multiple of 1024
|
2012-10-18 04:31:00 +00:00
|
|
|
if ((curReadIndex + readLength > NUM_CHIPS * MAX_CHIP_SIZE) ||
|
2012-05-20 05:58:17 +00:00
|
|
|
(readLength % READ_CHUNK_SIZE_BYTES) ||
|
2012-10-18 04:31:00 +00:00
|
|
|
(curReadIndex % READ_CHUNK_SIZE_BYTES) ||
|
|
|
|
(readLength == 0))// Ensure it's within limits and a multiple of 1024
|
2012-05-20 05:58:17 +00:00
|
|
|
{
|
|
|
|
SendByte(ProgrammerReadError);
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-18 04:31:00 +00:00
|
|
|
// Convert the length/pos into the number of chunks we need to send
|
2012-05-20 05:58:17 +00:00
|
|
|
readLength /= READ_CHUNK_SIZE_BYTES;
|
2012-10-18 04:31:00 +00:00
|
|
|
curReadIndex /= READ_CHUNK_SIZE_BYTES;
|
2012-05-20 05:58:17 +00:00
|
|
|
curCommandState = ReadingChips;
|
|
|
|
SendByte(ProgrammerReadOK);
|
|
|
|
USBSerial_SendReadDataChunk();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Read the next chunk of data from the SIMM and send it off over the serial.
|
2011-12-11 16:35:53 +00:00
|
|
|
void USBSerial_SendReadDataChunk(void)
|
|
|
|
{
|
2012-01-27 04:42:42 +00:00
|
|
|
// Here's a buffer we will use to read the next chunk of data.
|
|
|
|
// It's static because the stack is NOT big enough for it. If I start
|
|
|
|
// running low on RAM, I could pull this out of the function and share it
|
|
|
|
// with other functions, but I'm not bothering with that for now.
|
2011-12-11 16:35:53 +00:00
|
|
|
static union
|
|
|
|
{
|
2012-05-20 05:58:17 +00:00
|
|
|
uint32_t readChunks[READ_CHUNK_SIZE_BYTES / NUM_CHIPS];
|
2011-12-11 16:35:53 +00:00
|
|
|
uint8_t readChunkBytes[READ_CHUNK_SIZE_BYTES];
|
|
|
|
} chunks;
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Read the next chunk of data, send it over USB, and make sure
|
|
|
|
// we sent it correctly.
|
2012-05-20 05:58:17 +00:00
|
|
|
ExternalMem_Read(curReadIndex * (READ_CHUNK_SIZE_BYTES/NUM_CHIPS),
|
|
|
|
chunks.readChunks, READ_CHUNK_SIZE_BYTES/NUM_CHIPS);
|
2012-01-27 04:42:42 +00:00
|
|
|
uint8_t retVal = SendData((const char *)chunks.readChunkBytes,
|
|
|
|
READ_CHUNK_SIZE_BYTES);
|
|
|
|
|
|
|
|
// If for some reason there was an error, mark it as such. Otherwise,
|
|
|
|
// increment our pointer so we know the next chunk of data to send.
|
2011-12-11 16:35:53 +00:00
|
|
|
if (retVal != ENDPOINT_RWSTREAM_NoError)
|
|
|
|
{
|
2013-07-04 19:10:18 +00:00
|
|
|
//curCommandState = ReadingChipsUnableSendError; // TODO: not implemented
|
|
|
|
curCommandState = WaitingForCommand;
|
2011-12-11 16:35:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curReadIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Handles a received byte from the computer while we're in the "writing chips"
|
|
|
|
// mode.
|
2011-12-17 04:13:34 +00:00
|
|
|
void USBSerial_HandleWritingChipsByte(uint8_t byte)
|
|
|
|
{
|
2012-01-27 04:42:42 +00:00
|
|
|
// A buffer we use to store the incoming data. This, too, could be shared
|
|
|
|
// with other functions if I end up running out of RAM. Again, I'm not
|
|
|
|
// bothering with that yet, but this could easily be shared with the
|
|
|
|
// read function.
|
2011-12-17 04:13:34 +00:00
|
|
|
static union
|
|
|
|
{
|
|
|
|
uint32_t writeChunks[WRITE_CHUNK_SIZE_BYTES / 4];
|
|
|
|
uint8_t writeChunkBytes[WRITE_CHUNK_SIZE_BYTES];
|
|
|
|
} chunks;
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// This means we have just started the entire process or just finished
|
|
|
|
// a chunk, so see what the computer has decided for us to do.
|
2011-12-17 04:13:34 +00:00
|
|
|
if (writePosInChunk == -1)
|
|
|
|
{
|
|
|
|
switch (byte)
|
|
|
|
{
|
2012-01-27 04:42:42 +00:00
|
|
|
// The computer asked to write more data to the SIMM.
|
2011-12-17 04:13:34 +00:00
|
|
|
case ComputerWriteMore:
|
|
|
|
writePosInChunk = 0;
|
2012-01-27 04:42:42 +00:00
|
|
|
// Make sure we don't write past the capacity of the chips.
|
2012-10-18 04:31:00 +00:00
|
|
|
if (curWriteIndex < MAX_CHIP_SIZE / (WRITE_CHUNK_SIZE_BYTES/NUM_CHIPS))
|
2011-12-17 04:13:34 +00:00
|
|
|
{
|
|
|
|
SendByte(ProgrammerWriteOK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-18 04:31:00 +00:00
|
|
|
LED_Off();
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(ProgrammerWriteError);
|
2012-10-18 04:31:00 +00:00
|
|
|
curCommandState = WaitingForCommand;
|
2011-12-17 04:13:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// The computer said that it's done writing.
|
2011-12-17 04:13:34 +00:00
|
|
|
case ComputerWriteFinish:
|
2012-05-27 22:41:16 +00:00
|
|
|
LED_Off();
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(ProgrammerWriteOK);
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
break;
|
2012-01-27 04:42:42 +00:00
|
|
|
// The computer asked to cancel.
|
2011-12-17 04:13:34 +00:00
|
|
|
case ComputerWriteCancel:
|
2012-05-27 22:41:16 +00:00
|
|
|
LED_Off();
|
2011-12-17 04:13:34 +00:00
|
|
|
SendByte(ProgrammerWriteConfirmCancel);
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-27 04:42:42 +00:00
|
|
|
else // Interpret the incoming byte as data to write to the SIMM.
|
2011-12-17 04:13:34 +00:00
|
|
|
{
|
2012-01-27 04:42:42 +00:00
|
|
|
// Save the byte, and check if we've filled up an entire chunk
|
2011-12-17 04:13:34 +00:00
|
|
|
chunks.writeChunkBytes[writePosInChunk++] = byte;
|
|
|
|
if (writePosInChunk >= WRITE_CHUNK_SIZE_BYTES)
|
|
|
|
{
|
2012-01-27 04:42:42 +00:00
|
|
|
// We filled up the chunk, write it out and confirm it, then wait
|
|
|
|
// for the next command from the computer!
|
2012-10-18 04:31:00 +00:00
|
|
|
uint8_t writeResult = ExternalMem_Write(curWriteIndex * (WRITE_CHUNK_SIZE_BYTES/NUM_CHIPS),
|
2013-07-04 19:10:18 +00:00
|
|
|
chunks.writeChunks, WRITE_CHUNK_SIZE_BYTES/NUM_CHIPS, chipsMask, verifyDuringWrite);
|
2012-09-30 21:41:22 +00:00
|
|
|
|
|
|
|
// But if we asked to verify, make sure it came out OK.
|
|
|
|
if (verifyDuringWrite && (writeResult != 0))
|
|
|
|
{
|
|
|
|
// Uh oh -- verification failure.
|
|
|
|
LED_Off();
|
|
|
|
// Send the fail bit along with a mask of failed chips.
|
|
|
|
SendByte(ProgrammerWriteVerificationError | writeResult);
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SendByte(ProgrammerWriteOK);
|
|
|
|
curWriteIndex++;
|
|
|
|
writePosInChunk = -1;
|
|
|
|
LED_Toggle();
|
|
|
|
}
|
2011-12-17 04:13:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// Whenever an electrical test failure occurs, this handler will be called
|
|
|
|
// by it. It sends out a failure notice followed by indexes of the two
|
|
|
|
// shorted pins.
|
2011-12-18 18:18:30 +00:00
|
|
|
void USBSerial_ElectricalTest_Fail_Handler(uint8_t index1, uint8_t index2)
|
|
|
|
{
|
|
|
|
SendByte(ProgrammerElectricalTestFail);
|
|
|
|
SendByte(index1);
|
|
|
|
SendByte(index2);
|
|
|
|
}
|
|
|
|
|
2012-10-17 01:36:55 +00:00
|
|
|
// If we're figuring out the position/length to erase, parse it here.
|
|
|
|
void USBSerial_HandleErasePortionReadPosLengthByte(uint8_t byte)
|
|
|
|
{
|
|
|
|
// Read in the position and length to erase
|
|
|
|
if (readLengthByteIndex < 4)
|
|
|
|
{
|
|
|
|
erasePosition |= (((uint32_t)byte) << (8*readLengthByteIndex));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eraseLength |= (((uint32_t)byte) << (8*(readLengthByteIndex - 4)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++readLengthByteIndex >= 8)
|
|
|
|
{
|
|
|
|
ChipType chipType = ExternalMem_GetChipType();
|
|
|
|
bool eraseSuccess = false;
|
|
|
|
|
|
|
|
// Ensure they are both within limits of sector size erasure
|
2012-10-17 04:25:15 +00:00
|
|
|
if (((erasePosition % ERASE_SECTOR_SIZE_BYTES) == 0) &&
|
|
|
|
((eraseLength % ERASE_SECTOR_SIZE_BYTES) == 0))
|
2012-10-17 01:36:55 +00:00
|
|
|
{
|
|
|
|
uint32_t boundary = eraseLength + erasePosition;
|
|
|
|
|
|
|
|
// Ensure they are within the limits of the chip size too
|
|
|
|
if (chipType == ChipType8BitData_4MBitSize)
|
|
|
|
{
|
|
|
|
if (boundary <= (2 * 1024UL * 1024UL))
|
|
|
|
{
|
|
|
|
// OK! We're erasing certain sectors of a 2 MB SIMM.
|
|
|
|
SendByte(ProgrammerErasePortionOK);
|
|
|
|
CDC_Device_Flush(&VirtualSerial_CDC_Interface);
|
|
|
|
if (ExternalMem_EraseSectors(erasePosition/NUM_CHIPS,
|
2013-07-04 19:10:18 +00:00
|
|
|
eraseLength/NUM_CHIPS, chipsMask))
|
2012-10-17 01:36:55 +00:00
|
|
|
{
|
|
|
|
eraseSuccess = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (chipType == ChipType8Bit16BitData_16MBitSize)
|
|
|
|
{
|
|
|
|
if (boundary <= (8 * 1024UL * 1024UL))
|
|
|
|
{
|
|
|
|
// OK! We're erasing certain sectors of an 8 MB SIMM.
|
|
|
|
SendByte(ProgrammerErasePortionOK);
|
|
|
|
CDC_Device_Flush(&VirtualSerial_CDC_Interface);
|
|
|
|
if (ExternalMem_EraseSectors(erasePosition/NUM_CHIPS,
|
2013-07-04 19:10:18 +00:00
|
|
|
eraseLength/NUM_CHIPS, chipsMask))
|
2012-10-17 01:36:55 +00:00
|
|
|
{
|
|
|
|
eraseSuccess = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eraseSuccess)
|
|
|
|
{
|
|
|
|
// Not on a sector boundary for erase position and/or length
|
|
|
|
SendByte(ProgrammerErasePortionFinished);
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not on a sector boundary for erase position and/or length
|
|
|
|
SendByte(ProgrammerErasePortionError);
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 04:31:00 +00:00
|
|
|
void USBSerial_HandleReadingChipsReadStartPosByte(uint8_t byte)
|
|
|
|
{
|
|
|
|
// There will be four bytes, so count up until we know the position. If they
|
|
|
|
// have sent all four bytes, then start reading the length
|
|
|
|
curReadIndex |= (((uint32_t)byte) << (8*readLengthByteIndex));
|
|
|
|
if (++readLengthByteIndex >= 4)
|
|
|
|
{
|
|
|
|
readLengthByteIndex = 0;
|
|
|
|
curCommandState = ReadingChipsReadLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void USBSerial_HandleWritingChipsReadingStartPosByte(uint8_t byte)
|
|
|
|
{
|
|
|
|
// There will be four bytes, so count up until we know the position. If they
|
|
|
|
// have sent all four bytes, then confirm the write and begin
|
|
|
|
curWriteIndex |= (((uint32_t)byte) << (8*readLengthByteIndex));
|
|
|
|
if (++readLengthByteIndex >= 4)
|
|
|
|
{
|
|
|
|
// Got it...now, is it valid? If so, allow the write to begin
|
|
|
|
if ((curWriteIndex % WRITE_CHUNK_SIZE_BYTES) ||
|
|
|
|
(curWriteIndex >= NUM_CHIPS * MAX_CHIP_SIZE))
|
|
|
|
{
|
|
|
|
SendByte(ProgrammerWriteError);
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Convert write size into an index appropriate for rest of code
|
|
|
|
curWriteIndex /= WRITE_CHUNK_SIZE_BYTES;
|
|
|
|
SendByte(ProgrammerWriteOK);
|
|
|
|
curCommandState = WritingChips;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 19:10:18 +00:00
|
|
|
void USBSerial_HandleReadingChipsMaskByte(uint8_t byte)
|
|
|
|
{
|
|
|
|
// Single byte follows containing mask of chips we're programming
|
|
|
|
if (byte <= 0x0F)
|
|
|
|
{
|
|
|
|
// Mask has to be less than or equal to 0x0F because there are only
|
|
|
|
// four valid mask bits.
|
|
|
|
chipsMask = byte;
|
|
|
|
SendByte(CommandReplyOK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SendByte(CommandReplyError);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done either way; now we're waiting for a command to arrive
|
|
|
|
curCommandState = WaitingForCommand;
|
|
|
|
}
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// LUFA event handler for when the USB configuration changes.
|
2011-12-10 06:11:31 +00:00
|
|
|
void EVENT_USB_Device_ConfigurationChanged(void)
|
|
|
|
{
|
|
|
|
bool ConfigSuccess = true;
|
|
|
|
|
|
|
|
ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
|
|
|
|
}
|
|
|
|
|
2012-01-27 04:42:42 +00:00
|
|
|
// LUFA event handler for when a USB control request is received
|
2011-12-10 06:11:31 +00:00
|
|
|
void EVENT_USB_Device_ControlRequest(void)
|
|
|
|
{
|
|
|
|
CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
|
|
|
|
}
|