Skip to content
Browse files

Switched to single-byte program operation

  • Loading branch information...
1 parent f1cb403 commit 32fe0003b4065956a7b30c117da0927f144490b7 @makestuff committed
Showing with 210 additions and 182 deletions.
  1. +1 −1 firmware/fx2/app.c
  2. +172 −160 firmware/fx2/jtag.c
  3. +1 −1 firmware/fx2/jtag.h
  4. +26 −14 nero.c
  5. +10 −6 prog.h
View
2 firmware/fx2/app.c
@@ -308,7 +308,7 @@ uint8 handleVendorCommand(uint8 cmd) {
if ( SETUP_TYPE == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR) ) {
EP0BCL = 0x00; // Allow host transfer in
while ( EP0CS & bmEPBUSY ); // Wait for data
- jtagShiftBegin(*((uint32 *)EP0BUF), SETUPDAT[2]); // Init numBits & flagByte
+ jtagShiftBegin(*((uint32 *)EP0BUF), (ProgOp)SETUPDAT[4], SETUPDAT[2]); // Init numBits & flagByte
return true;
// Now that numBits & flagByte are set, this operation will continue in mainLoop()...
}
View
332 firmware/fx2/jtag.c
@@ -22,12 +22,14 @@
#include "defs.h"
#include "../../vendorCommands.h"
#include "debug.h"
+#include "../../prog.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// NeroJTAG Stuff
////////////////////////////////////////////////////////////////////////////////////////////////////
static xdata uint32 m_numBits = 0UL;
+static xdata ProgOp m_progOp = PROG_NOP;
static xdata uint8 m_flagByte = 0x00;
// THIS MUST BE THE FIRST FUNCTION IN THE FILE!
@@ -254,8 +256,9 @@ static uint8 shiftInOut(uint8 c) {
// Kick off a shift operation. Next time jtagExecuteShift() runs, it will execute the shift.
//
-void jtagShiftBegin(uint32 numBits, uint8 flagByte) {
+void jtagShiftBegin(uint32 numBits, ProgOp progOp, uint8 flagByte) {
m_numBits = numBits;
+ m_progOp = progOp;
m_flagByte = flagByte;
}
@@ -263,187 +266,196 @@ void jtagShiftBegin(uint32 numBits, uint8 flagByte) {
//
#define bitsToBytes(x) ((x>>3) + (x&7 ? 1 : 0))
-// Actually execute the shift operation initiated by jtagBeginShift(). This is done in a
-// separate method because vendor commands cannot read & write to bulk endpoints.
-//
-void jtagShiftExecute(void) {
- if ( !(m_flagByte & bmDOJTAG) ) {
- return; // Nothing to do
- }
- //if ( m_numBits == 0UL ) {
- // return; // Nothing to do
- //}
-
- // Are there any JTAG send/receive operations to execute?
- if ( (m_flagByte & bmSENDMASK) == bmSENDDATA ) {
- if ( m_flagByte & bmNEEDRESPONSE ) {
- // The host is giving us data, and is expecting a response (xdr)
- xdata uint16 bitsRead, bitsRemaining;
- xdata uint8 *inPtr, *outPtr, bytesRead, bytesRemaining;
- while ( m_numBits ) {
- while ( EP01STAT & bmEP1OUTBSY ); // Wait for some EP1OUT data
- while ( EP01STAT & bmEP1INBSY ); // Wait for space for EP1IN data
- bitsRead = (m_numBits >= (ENDPOINT_SIZE<<3)) ? ENDPOINT_SIZE<<3 : m_numBits;
- bytesRead = EP1OUTBC;
-
- inPtr = EP1OUTBUF;
- outPtr = EP1INBUF;
- if ( bitsRead == m_numBits ) {
- // This is the last chunk
- xdata uint8 tdoByte, tdiByte, leftOver, i;
- bitsRemaining = (bitsRead-1) & 0xFFF8; // Now an integer number of bytes
- leftOver = (uint8)(bitsRead - bitsRemaining); // How many bits in last byte (1-8)
- bytesRemaining = (bitsRemaining>>3);
- while ( bytesRemaining-- ) {
- *outPtr++ = shiftInOut(*inPtr++);
- }
- tdiByte = *inPtr++; // Now do the bits in the final byte
- tdoByte = 0x00;
- i = 1;
- while ( i && leftOver ) {
- leftOver--;
- if ( (m_flagByte & bmISLAST) && !leftOver ) {
- TMS = 1; // Exit Shift-DR state on next clock
- }
- TDI = tdiByte & 1;
- tdiByte >>= 1;
- if ( TDO ) {
- tdoByte |= i;
- }
- TCK = 1;
- TCK = 0;
- i <<= 1;
- }
- *outPtr = tdoByte;
- } else {
- // This is not the last chunk
- bytesRemaining = (bitsRead>>3);
- while ( bytesRemaining-- ) {
- *outPtr++ = shiftInOut(*inPtr++);
- }
+static const xdata uint8 *m_inPtr;
+static xdata uint8 *m_outPtr;
+
+static void jtagIsSendingIsReceiving(void) {
+ xdata uint16 bitsRead, bitsRemaining;
+ xdata uint8 bytesRead, bytesRemaining;
+ while ( m_numBits ) {
+ while ( EP01STAT & bmEP1OUTBSY ); // Wait for some EP1OUT data
+ while ( EP01STAT & bmEP1INBSY ); // Wait for space for EP1IN data
+ bitsRead = (m_numBits >= (ENDPOINT_SIZE<<3)) ? ENDPOINT_SIZE<<3 : m_numBits;
+ bytesRead = EP1OUTBC;
+
+ m_inPtr = EP1OUTBUF;
+ m_outPtr = EP1INBUF;
+ if ( bitsRead == m_numBits ) {
+ // This is the last chunk
+ xdata uint8 tdoByte, tdiByte, leftOver, i;
+ bitsRemaining = (bitsRead-1) & 0xFFF8; // Now an integer number of bytes
+ leftOver = (uint8)(bitsRead - bitsRemaining); // How many bits in last byte (1-8)
+ bytesRemaining = (bitsRemaining>>3);
+ while ( bytesRemaining-- ) {
+ *m_outPtr++ = shiftInOut(*m_inPtr++);
+ }
+ tdiByte = *m_inPtr++; // Now do the bits in the final byte
+ tdoByte = 0x00;
+ i = 1;
+ while ( i && leftOver ) {
+ leftOver--;
+ if ( (m_flagByte & bmISLAST) && !leftOver ) {
+ TMS = 1; // Exit Shift-DR state on next clock
+ }
+ TDI = tdiByte & 1;
+ tdiByte >>= 1;
+ if ( TDO ) {
+ tdoByte |= i;
}
- EP1OUTBC = 0x00; // ready to accept more data from host
- EP1INBC = bytesRead; // send response back to host
- m_numBits -= bitsRead;
+ TCK = 1;
+ TCK = 0;
+ i <<= 1;
}
+ *m_outPtr = tdoByte;
} else {
- // The host is giving us data, but does not need a response (xdn)
- xdata uint16 bitsRead, bitsRemaining, bytesRead, bytesRemaining;
- while ( m_numBits ) {
- while ( EP01STAT & bmEP1OUTBSY ); // Wait for some EP2OUT data
- bitsRead = (m_numBits >= (ENDPOINT_SIZE<<3)) ? ENDPOINT_SIZE<<3 : m_numBits;
- bytesRead = EP1OUTBC;
-
- if ( bitsRead == m_numBits ) {
- // This is the last chunk
- xdata uint8 tdiByte, leftOver, i;
- inPtr = EP1OUTBUF;
- bitsRemaining = (bitsRead-1) & 0xFFF8; // Now an integer number of bytes
- leftOver = (uint8)(bitsRead - bitsRemaining); // How many bits in last byte (1-8)
- bytesRemaining = (bitsRemaining>>3);
- while ( bytesRemaining-- ) {
- shiftOut(*inPtr++);
- }
- tdiByte = *inPtr; // Now do the bits in the final byte
- i = 1;
- while ( i && leftOver ) {
- leftOver--;
- if ( (m_flagByte & bmISLAST) && !leftOver ) {
- TMS = 1; // Exit Shift-DR state on next clock
- }
- TDI = tdiByte & 1;
- tdiByte >>= 1;
- TCK = 1;
- TCK = 0;
- i <<= 1;
- }
- } else {
- // This is not the last chunk, so we've to 512 bytes to shift
- blockShiftOut(64);
- }
- EP1OUTBC = 0x00; // ready to accept more data from host
- m_numBits -= bitsRead;
+ // This is not the last chunk
+ bytesRemaining = (bitsRead>>3);
+ while ( bytesRemaining-- ) {
+ *m_outPtr++ = shiftInOut(*m_inPtr++);
}
}
- } else {
- if ( m_flagByte & bmNEEDRESPONSE ) {
- // The host is not giving us data, but is expecting a response (x0r)
- xdata uint16 bitsRead, bitsRemaining, bytesRead, bytesRemaining;
- xdata uint8 tdiByte;
- if ( (m_flagByte & bmSENDMASK) == bmSENDZEROS ) {
- tdiByte = 0x00;
- } else {
- tdiByte = 0xFF;
+ EP1OUTBC = 0x00; // ready to accept more data from host
+ EP1INBC = bytesRead; // send response back to host
+ m_numBits -= bitsRead;
+ }
+ m_progOp = PROG_NOP;
+}
+
+static void jtagIsSendingNotReceiving(void) {
+ xdata uint16 bitsRead, bitsRemaining, bytesRead, bytesRemaining;
+ while ( m_numBits ) {
+ while ( EP01STAT & bmEP1OUTBSY ); // Wait for some EP2OUT data
+ bitsRead = (m_numBits >= (ENDPOINT_SIZE<<3)) ? ENDPOINT_SIZE<<3 : m_numBits;
+ bytesRead = EP1OUTBC;
+
+ if ( bitsRead == m_numBits ) {
+ // This is the last chunk
+ xdata uint8 tdiByte, leftOver, i;
+ m_inPtr = EP1OUTBUF;
+ bitsRemaining = (bitsRead-1) & 0xFFF8; // Now an integer number of bytes
+ leftOver = (uint8)(bitsRead - bitsRemaining); // How many bits in last byte (1-8)
+ bytesRemaining = (bitsRemaining>>3);
+ while ( bytesRemaining-- ) {
+ shiftOut(*m_inPtr++);
}
- while ( m_numBits ) {
- while ( EP01STAT & bmEP1INBSY ); // Wait for space for EP1IN data
- bitsRead = (m_numBits >= (ENDPOINT_SIZE<<3)) ? ENDPOINT_SIZE<<3 : m_numBits;
- bytesRead = bitsToBytes(bitsRead);
-
- outPtr = EP1INBUF;
- if ( bitsRead == m_numBits ) {
- // This is the last chunk
- xdata uint8 tdoByte, leftOver, i;
- bitsRemaining = (bitsRead-1) & 0xFFF8; // Now an integer number of bytes
- leftOver = (uint8)(bitsRead - bitsRemaining); // How many bits in last byte (1-8)
- bytesRemaining = (bitsRemaining>>3);
- while ( bytesRemaining-- ) {
- *outPtr++ = shiftInOut(tdiByte);
- }
- tdoByte = 0x00;
- i = 1;
- TDI = tdiByte & 1;
- while ( i && leftOver ) {
- leftOver--;
- if ( (m_flagByte & bmISLAST) && !leftOver ) {
- TMS = 1; // Exit Shift-DR state on next clock
- }
- if ( TDO ) {
- tdoByte |= i;
- }
- TCK = 1;
- TCK = 0;
- i <<= 1;
- }
- *outPtr = tdoByte;
- } else {
- // This is not the last chunk
- bytesRemaining = (bitsRead>>3);
- while ( bytesRemaining-- ) {
- *outPtr++ = shiftInOut(tdiByte);
- }
+ tdiByte = *m_inPtr; // Now do the bits in the final byte
+ i = 1;
+ while ( i && leftOver ) {
+ leftOver--;
+ if ( (m_flagByte & bmISLAST) && !leftOver ) {
+ TMS = 1; // Exit Shift-DR state on next clock
}
- EP1INBC = bytesRead; // send response back to host
- m_numBits -= bitsRead;
+ TDI = tdiByte & 1;
+ tdiByte >>= 1;
+ TCK = 1;
+ TCK = 0;
+ i <<= 1;
}
} else {
- // The host is not giving us data, and does not need a response (x0n)
- xdata uint32 bitsRemaining, bytesRemaining;
- xdata uint8 tdiByte, leftOver;
- if ( (m_flagByte & bmSENDMASK) == bmSENDZEROS ) {
- tdiByte = 0x00;
- } else {
- tdiByte = 0xFF;
- }
- bitsRemaining = (m_numBits-1) & 0xFFFFFFF8; // Now an integer number of bytes
- leftOver = (uint8)(m_numBits - bitsRemaining); // How many bits in last byte (1-8)
+ // This is not the last chunk, so we've to 512 bytes to shift
+ blockShiftOut(64);
+ }
+ EP1OUTBC = 0x00; // ready to accept more data from host
+ m_numBits -= bitsRead;
+ }
+ m_progOp = PROG_NOP;
+}
+
+static void jtagNotSendingIsReceiving(void) {
+ // The host is not giving us data, but is expecting a response (x0r)
+ xdata uint16 bitsRead, bitsRemaining, bytesRead, bytesRemaining;
+ const xdata uint8 tdiByte = (m_flagByte & bmSENDONES) ? 0xFF : 0x00;
+ while ( m_numBits ) {
+ while ( EP01STAT & bmEP1INBSY ); // Wait for space for EP1IN data
+ bitsRead = (m_numBits >= (ENDPOINT_SIZE<<3)) ? ENDPOINT_SIZE<<3 : m_numBits;
+ bytesRead = bitsToBytes(bitsRead);
+
+ m_outPtr = EP1INBUF;
+ if ( bitsRead == m_numBits ) {
+ // This is the last chunk
+ xdata uint8 tdoByte, leftOver, i;
+ bitsRemaining = (bitsRead-1) & 0xFFF8; // Now an integer number of bytes
+ leftOver = (uint8)(bitsRead - bitsRemaining); // How many bits in last byte (1-8)
bytesRemaining = (bitsRemaining>>3);
while ( bytesRemaining-- ) {
- shiftOut(tdiByte);
+ *m_outPtr++ = shiftInOut(tdiByte);
}
+ tdoByte = 0x00;
+ i = 1;
TDI = tdiByte & 1;
- while ( leftOver ) {
+ while ( i && leftOver ) {
leftOver--;
if ( (m_flagByte & bmISLAST) && !leftOver ) {
TMS = 1; // Exit Shift-DR state on next clock
}
+ if ( TDO ) {
+ tdoByte |= i;
+ }
TCK = 1;
TCK = 0;
+ i <<= 1;
}
- m_numBits = 0UL;
+ *m_outPtr = tdoByte;
+ } else {
+ // This is not the last chunk
+ bytesRemaining = (bitsRead>>3);
+ while ( bytesRemaining-- ) {
+ *m_outPtr++ = shiftInOut(tdiByte);
+ }
+ }
+ EP1INBC = bytesRead; // send response back to host
+ m_numBits -= bitsRead;
+ }
+ m_progOp = PROG_NOP;
+}
+
+static void jtagNotSendingNotReceiving(void) {
+ // The host is not giving us data, and does not need a response (x0n)
+ xdata uint32 bitsRemaining, bytesRemaining;
+ const xdata uint8 tdiByte = (m_flagByte & bmSENDONES) ? 0xFF : 0x00;
+ xdata uint8 leftOver;
+ bitsRemaining = (m_numBits-1) & 0xFFFFFFF8; // Now an integer number of bytes
+ leftOver = (uint8)(m_numBits - bitsRemaining); // How many bits in last byte (1-8)
+ bytesRemaining = (bitsRemaining>>3);
+ while ( bytesRemaining-- ) {
+ shiftOut(tdiByte);
+ }
+ TDI = tdiByte & 1;
+ while ( leftOver ) {
+ leftOver--;
+ if ( (m_flagByte & bmISLAST) && !leftOver ) {
+ TMS = 1; // Exit Shift-DR state on next clock
}
+ TCK = 1;
+ TCK = 0;
+ }
+ m_progOp = PROG_NOP;
+}
+
+// Actually execute the shift operation initiated by jtagBeginShift(). This is done in a
+// separate method because vendor commands cannot read & write to bulk endpoints.
+//
+void jtagShiftExecute(void) {
+ switch ( m_progOp ) {
+ case PROG_JTAG_ISSENDING_ISRECEIVING:
+ // The host is giving us data, and is expecting a response (xdr)
+ jtagIsSendingIsReceiving();
+ break;
+ case PROG_JTAG_ISSENDING_NOTRECEIVING:
+ // The host is giving us data, but does not need a response (xdn)
+ jtagIsSendingNotReceiving();
+ break;
+ case PROG_JTAG_NOTSENDING_ISRECEIVING:
+ // The host is not giving us data, but is expecting a response (x0r)
+ jtagNotSendingIsReceiving();
+ break;
+ case PROG_JTAG_NOTSENDING_NOTRECEIVING:
+ jtagNotSendingNotReceiving();
+ break;
+ case PROG_NOP:
+ default:
+ break;
}
- m_flagByte = 0x00;
}
// Keep TMS and TDI as they are, and clock the JTAG state machine "numClocks" times.
View
2 firmware/fx2/jtag.h
@@ -43,7 +43,7 @@ sbit at (0x80 + 16*JTAG_PORT + TCK_BIT) TCK; // Port bit to use for TCK
#define ERROR_CSVF_BAD_COMMAND 2
// Kick off a shift operation. Next time jtagExecuteShift() runs, it will execute the shift.
-void jtagShiftBegin(uint32 numBits, uint8 flagByte);
+void jtagShiftBegin(uint32 numBits, ProgOp progOp, uint8 flagByte);
// Return true if there's a shift operation pending
bool jtagIsShiftPending(void);
View
40 nero.c
@@ -28,7 +28,7 @@
// -------------------------------------------------------------------------------------------------
static NeroStatus beginShift(
- struct FLContext *handle, uint32 numBits, uint8 mode, const char **error
+ struct FLContext *handle, uint32 numBits, ProgOp progOp, uint8 mode, const char **error
) WARN_UNUSED_RESULT;
static NeroStatus doSend(
@@ -217,34 +217,46 @@ NeroStatus neroShift(
NeroStatus returnCode, nStatus;
uint32 numBytes;
uint16 chunkSize;
+ ProgOp progOp;
uint8 mode = 0x00;
- bool sendData = false;
+ bool isSending = false;
+ bool isReceiving = false;
- if ( inData == ZEROS ) {
- mode |= bmSENDZEROS;
- } else if ( inData == ONES ) {
+ if ( inData == ONES ) {
mode |= bmSENDONES;
- } else {
- mode |= bmSENDDATA;
- sendData = true;
+ } else if ( inData != ZEROS ) {
+ isSending = true;
}
if ( outData ) {
- mode |= bmNEEDRESPONSE;
+ isReceiving = true;
}
if ( isLast ) {
mode |= bmISLAST;
}
- nStatus = beginShift(handle, numBits, mode, error);
+ if ( isSending ) {
+ if ( isReceiving ) {
+ progOp = PROG_JTAG_ISSENDING_ISRECEIVING;
+ } else {
+ progOp = PROG_JTAG_ISSENDING_NOTRECEIVING;
+ }
+ } else {
+ if ( isReceiving ) {
+ progOp = PROG_JTAG_NOTSENDING_ISRECEIVING;
+ } else {
+ progOp = PROG_JTAG_NOTSENDING_NOTRECEIVING;
+ }
+ }
+ nStatus = beginShift(handle, numBits, progOp, mode, error);
CHECK_STATUS(nStatus, "neroShift()", NERO_BEGIN_SHIFT);
numBytes = bitsToBytes(numBits);
while ( numBytes ) {
chunkSize = (numBytes>=handle->endpointSize) ? handle->endpointSize : (uint16)numBytes;
- if ( sendData ) {
+ if ( isSending ) {
nStatus = doSend(handle, inData, chunkSize, error);
CHECK_STATUS(nStatus, "neroShift()", NERO_SEND);
inData += chunkSize;
}
- if ( outData ) {
+ if ( isReceiving ) {
nStatus = doReceive(handle, outData, chunkSize, error);
CHECK_STATUS(nStatus, "neroShift()", NERO_RECEIVE);
outData += chunkSize;
@@ -356,7 +368,7 @@ static NeroStatus portMap(
// Kick off a shift operation on the micro. This will be followed by a bunch of sends and receives.
//
static NeroStatus beginShift(
- struct FLContext *handle, uint32 numBits, uint8 mode, const char **error)
+ struct FLContext *handle, uint32 numBits, ProgOp progOp, uint8 mode, const char **error)
{
NeroStatus returnCode = NERO_SUCCESS;
int uStatus;
@@ -369,7 +381,7 @@ static NeroStatus beginShift(
handle->device,
CMD_JTAG_CLOCK_DATA, // bRequest
(uint8)mode, // wValue
- 0x0000, // wIndex
+ (uint8)progOp, // wIndex
leNumBits.bytes, // send bit count
4, // wLength
5000, // timeout (ms)
View
16 prog.h
@@ -17,11 +17,15 @@
#ifndef PROG_H
#define PROG_H
-#define bmNEEDRESPONSE (1<<0)
-#define bmISLAST (1<<1)
-#define bmSENDZEROS (0<<2)
-#define bmSENDONES (1<<2)
-#define bmSENDDATA (2<<2)
-#define bmSENDMASK (3<<2)
+typedef enum {
+ PROG_NOP,
+ PROG_JTAG_ISSENDING_ISRECEIVING,
+ PROG_JTAG_ISSENDING_NOTRECEIVING,
+ PROG_JTAG_NOTSENDING_ISRECEIVING,
+ PROG_JTAG_NOTSENDING_NOTRECEIVING
+} ProgOp;
+
+#define bmISLAST (1<<0)
+#define bmSENDONES (1<<1)
#endif

0 comments on commit 32fe000

Please sign in to comment.
Something went wrong with that request. Please try again.