Commit 74b6993d authored by Dean Camera's avatar Dean Camera
Browse files

Cleanup and partially fix AVRISP-MKII project's TPI programming support.

parent 85738118
......@@ -392,7 +392,7 @@ bool RNDIS_Host_IsPacketReceived(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfac
Pipe_Unfreeze();
PacketWaiting = Pipe_IsINReceived();
PacketWaiting = (Pipe_IsINReceived() && Pipe_BytesInPipe());
Pipe_Freeze();
......@@ -412,6 +412,9 @@ uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceIn
if (!(Pipe_IsReadWriteAllowed()))
{
if (Pipe_IsINReceived())
Pipe_ClearIN();
*PacketLength = 0;
Pipe_Freeze();
return PIPE_RWSTREAM_NoError;
......@@ -419,11 +422,27 @@ uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceIn
RNDIS_Packet_Message_t DeviceMessage;
if (Pipe_BytesInPipe() < sizeof(RNDIS_Packet_Message_t))
{
printf("*SIZE YARG: %d*\r\n", Pipe_BytesInPipe());
*PacketLength = 0;
Pipe_ClearIN();
return RNDIS_COMMAND_FAILED;
}
if ((ErrorCode = Pipe_Read_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t),
NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
{
return ErrorCode;
}
if (DeviceMessage.MessageType != REMOTE_NDIS_PACKET_MSG)
{
printf("****YARG****\r\n");
*PacketLength = 0;
Pipe_ClearIN();
return RNDIS_COMMAND_FAILED;
}
*PacketLength = (uint16_t)DeviceMessage.DataLength;
......@@ -431,7 +450,9 @@ uint8_t RNDIS_Host_ReadPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceIn
NO_STREAM_CALLBACK);
Pipe_Read_Stream_LE(Buffer, *PacketLength, NO_STREAM_CALLBACK);
Pipe_ClearIN();
if (!(Pipe_BytesInPipe()))
Pipe_ClearIN();
Pipe_Freeze();
......@@ -455,21 +476,25 @@ uint8_t RNDIS_Host_SendPacket(USB_ClassInfo_RNDIS_Host_t* const RNDISInterfaceIn
Pipe_SelectPipe(RNDISInterfaceInfo->Config.DataOUTPipeNumber);
}
Pipe_Unfreeze();
RNDIS_Packet_Message_t DeviceMessage;
DeviceMessage.MessageType = REMOTE_NDIS_PACKET_MSG;
memset(&DeviceMessage, 0, sizeof(RNDIS_Packet_Message_t));
DeviceMessage.MessageType = REMOTE_NDIS_PACKET_MSG;
DeviceMessage.MessageLength = (sizeof(RNDIS_Packet_Message_t) + PacketLength);
DeviceMessage.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
DeviceMessage.DataLength = PacketLength;
DeviceMessage.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
DeviceMessage.DataLength = PacketLength;
Pipe_Unfreeze();
if ((ErrorCode = Pipe_Write_Stream_LE(&DeviceMessage, sizeof(RNDIS_Packet_Message_t),
NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
{
if (RNDISInterfaceInfo->State.BidirectionalDataEndpoints)
Pipe_SetPipeToken(PIPE_TOKEN_IN);
return ErrorCode;
}
Pipe_Write_Stream_LE(Buffer, PacketLength, NO_STREAM_CALLBACK);
Pipe_ClearOUT();
......
......@@ -40,7 +40,7 @@
#warning TPI Protocol support is currently incomplete and is not suitable for general use.
/** Sends the given pointer address to the target's TPI pointer register */
void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress)
static void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress)
{
/* Send the given 16-bit address to the target, LSB first */
XPROGTarget_SendByte(TPI_CMD_SSTPR | 0);
......@@ -49,6 +49,28 @@ void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress)
XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[1]);
}
/** Sends a SIN command to the target with the specified I/O address, ready for the data byte to be written.
*
* \param Address 6-bit I/O address to write to in the target's I/O memory space
*/
static void TINYNVM_SendReadNVMRegister(uint8_t Address)
{
/* The TPI command for reading from the I/O space uses wierd addressing, where the I/O address's upper
* two bits of the 6-bit address are shifted left once */
XPROGTarget_SendByte(TPI_CMD_SIN | ((Address & 0x30) << 1) | (Address & 0x0F));
}
/** Sends a SOUT command to the target with the specified I/O address, ready for the data byte to be read.
*
* \param Address 6-bit I/O address to read from in the target's I/O memory space
*/
static void TINYNVM_SendWriteNVMRegister(uint8_t Address)
{
/* The TPI command for writing to the I/O space uses wierd addressing, where the I/O address's upper
* two bits of the 6-bit address are shifted left once */
XPROGTarget_SendByte(TPI_CMD_SOUT | ((Address & 0x30) << 1) | (Address & 0x0F));
}
/** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read.
*
* \return Boolean true if the NVM controller became ready within the timeout period, false otherwise
......@@ -78,7 +100,7 @@ bool TINYNVM_WaitWhileNVMControllerBusy(void)
while (TimeoutMSRemaining)
{
/* Send the SIN command to read the TPI STATUS register to see the NVM bus is active */
XPROGTarget_SendByte(TPI_CMD_SIN | XPROG_Param_NVMCSRRegAddr);
TINYNVM_SendReadNVMRegister(XPROG_Param_NVMCSRRegAddr);
if (XPROGTarget_ReceiveByte() & (1 << 7))
return true;
}
......@@ -101,7 +123,7 @@ bool TINYNVM_ReadMemory(const uint32_t ReadAddress, uint8_t* ReadBuffer, uint16_
return false;
/* Set the NVM control register to the NO OP command for memory reading */
XPROGTarget_SendByte(TPI_CMD_SOUT | XPROG_Param_NVMCMDRegAddr);
TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
XPROGTarget_SendByte(TINY_NVM_CMD_NOOP);
/* Send the address of the location to read from */
......@@ -132,7 +154,7 @@ bool TINYNVM_WriteMemory(const uint32_t WriteAddress, const uint8_t* WriteBuffer
return false;
/* Set the NVM control register to the WORD WRITE command for memory reading */
XPROGTarget_SendByte(TPI_CMD_SOUT | XPROG_Param_NVMCMDRegAddr);
TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
XPROGTarget_SendByte(TINY_NVM_CMD_WORDWRITE);
/* Send the address of the location to write to */
......@@ -159,7 +181,7 @@ bool TINYNVM_EraseMemory(void)
return false;
/* Set the NVM control register to the CHIP ERASE command to erase the target */
XPROGTarget_SendByte(TPI_CMD_SOUT | XPROG_Param_NVMCMDRegAddr);
TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);
XPROGTarget_SendByte(TINY_NVM_CMD_CHIPERASE);
/* Wait until the NVM bus is ready again */
......
......@@ -61,11 +61,16 @@
#define TINY_NVM_CMD_SECTIONERASE 0x14
#define TINY_NVM_CMD_WORDWRITE 0x1D
/* Function Prototypes: */
void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress);
/* Function Prototypes: */
bool TINYNVM_WaitWhileNVMBusBusy(void);
bool TINYNVM_ReadMemory(const uint32_t ReadAddress, uint8_t* ReadBuffer, uint16_t ReadLength);
bool TINYNVM_WriteMemory(const uint32_t WriteAddress, const uint8_t* WriteBuffer, uint16_t WriteLength);
bool TINYNVM_EraseMemory(void);
#if defined(INCLUDE_FROM_TINYNVM_C)
static void TINYNVM_SendReadNVMRegister(uint8_t Address);
static void TINYNVM_SendWriteNVMRegister(uint8_t Address);
static void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress);
#endif
#endif
......@@ -38,24 +38,11 @@
#if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
/** Sends the given NVM register address to the target.
*
* \param[in] Register NVM register whose absolute address is to be sent
*/
void XMEGANVM_SendNVMRegAddress(const uint8_t Register)
{
/* Determine the absolute register address from the NVM base memory address and the NVM register address */
uint32_t Address = XPROG_Param_NVMBase | Register;
/* Send the calculated 32-bit address to the target, LSB first */
XMEGANVM_SendAddress(Address);
}
/** Sends the given 32-bit absolute address to the target.
*
* \param[in] AbsoluteAddress Absolute address to send to the target
*/
void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress)
static void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress)
{
/* Send the given 32-bit address to the target, LSB first */
XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[0]);
......@@ -64,6 +51,19 @@ void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress)
XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[3]);
}
/** Sends the given NVM register address to the target.
*
* \param[in] Register NVM register whose absolute address is to be sent
*/
static void XMEGANVM_SendNVMRegAddress(const uint8_t Register)
{
/* Determine the absolute register address from the NVM base memory address and the NVM register address */
uint32_t Address = XPROG_Param_NVMBase | Register;
/* Send the calculated 32-bit address to the target, LSB first */
XMEGANVM_SendAddress(Address);
}
/** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read or CRC
* calculation.
*
......
......@@ -106,8 +106,6 @@
#define XMEGA_NVM_CMD_READEEPROM 0x06
/* Function Prototypes: */
void XMEGANVM_SendNVMRegAddress(const uint8_t Register);
void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress);
bool XMEGANVM_WaitWhileNVMBusBusy(void);
bool XMEGANVM_WaitWhileNVMControllerBusy(void);
bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand, uint32_t* const CRCDest);
......@@ -118,4 +116,9 @@
const uint8_t* WriteBuffer, uint16_t WriteSize);
bool XMEGANVM_EraseMemory(const uint8_t EraseCommand, const uint32_t Address);
#if defined(INCLUDE_FROM_XMEGANVM_C)
static void XMEGANVM_SendNVMRegAddress(const uint8_t Register);
static void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress);
#endif
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment