Refactor a bunch of stuff in Upstream project to avoid endless confusion

with Downstream project.

Also fixed a couple of things in upstream_spi.c and
upstream_interface_msc.c but due to file rename the diff is not
obvious...
pull/7/head
Robert Fisk 10 years ago
parent 7b4e4b3c14
commit 4350666330

@ -109,7 +109,7 @@
</toolChain> </toolChain>
</folderInfo> </folderInfo>
<sourceEntries> <sourceEntries>
<entry excluding="Src/downstream_interface_msc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c|Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_wwdg.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_smartcard.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sdram.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pccard.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nor.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nand.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_iwdg.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_irda.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma2d.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dcmi.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_msp_template.c|Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf_template.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/> <entry excluding="Src/upstream_interface_msc.c|Src/downstream_interface_msc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c|Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_wwdg.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_smartcard.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sdram.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pccard.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nor.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nand.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_iwdg.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_irda.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma2d.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dcmi.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_msp_template.c|Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf_template.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries> </sourceEntries>
</configuration> </configuration>
</storageModule> </storageModule>
@ -234,7 +234,7 @@
</toolChain> </toolChain>
</folderInfo> </folderInfo>
<sourceEntries> <sourceEntries>
<entry excluding="Src/downstream_interface_msc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c|Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_wwdg.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_smartcard.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sdram.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pccard.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nor.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nand.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_iwdg.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_irda.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma2d.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dcmi.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_msp_template.c|Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf_template.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/> <entry excluding="Src/upstream_interface_msc.c|Src/downstream_interface_msc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c|Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fmc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_wwdg.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_smartcard.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sdram.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pccard.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nor.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nand.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_iwdg.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_irda.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hash_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma2d.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dcmi.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c|Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_msp_template.c|Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf_template.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries> </sourceEntries>
</configuration> </configuration>
</storageModule> </storageModule>

@ -1,39 +0,0 @@
/*
* downstream_interface_msc.h
*
* Created on: 4/07/2015
* Author: Robert Fisk
*/
#ifndef INC_DOWNSTREAM_INTERFACE_MSC_H_
#define INC_DOWNSTREAM_INTERFACE_MSC_H_
#include "downstream_spi.h"
typedef void (*DownstreamInterfaceMSCCallbackTypeDef)(HAL_StatusTypeDef result);
typedef void (*DownstreamInterfaceMSCCallbackPacketTypeDef)(HAL_StatusTypeDef result,
DownstreamPacketTypeDef* downstreamPacket,
uint16_t dataLength);
typedef void (*DownstreamInterfaceMSCCallbackUintPacketTypeDef)(HAL_StatusTypeDef result,
uint32_t result_uint[],
DownstreamPacketTypeDef* downstreamPacket);
HAL_StatusTypeDef DownstreamInterface_TestReady(DownstreamInterfaceMSCCallbackTypeDef callback);
HAL_StatusTypeDef DownstreamInterface_GetCapacity(DownstreamInterfaceMSCCallbackUintPacketTypeDef callback);
HAL_StatusTypeDef DownstreamInterface_BeginRead(DownstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount,
uint32_t readByteCount);
HAL_StatusTypeDef DownstreamInterface_GetStreamDataPacket(DownstreamInterfaceMSCCallbackPacketTypeDef callback);
HAL_StatusTypeDef DownstreamInterface_BeginWrite(DownstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount);
HAL_StatusTypeDef DownstreamInterface_PutStreamDataPacket(DownstreamPacketTypeDef* packetToSend,
uint32_t dataLength);
#endif /* INC_DOWNSTREAM_INTERFACE_MSC_H_ */

@ -1,89 +0,0 @@
/*
* downstream_spi.h
*
* Created on: 21/06/2015
* Author: Robert Fisk
*/
#ifndef INC_DOWNSTREAM_SPI_H_
#define INC_DOWNSTREAM_SPI_H_
#include "usbd_config.h"
#define DOWNSTREAM_PACKET_HEADER_LEN (2) //Min length = CommandClass & Command bytes
#define DOWNSTREAM_PACKET_LEN (DOWNSTREAM_PACKET_HEADER_LEN + MSC_MEDIA_PACKET)
#define DOWNSTREAM_PACKET_LEN_MIN (DOWNSTREAM_PACKET_HEADER_LEN)
#define SPI_INTERFACE_FREAKOUT_VOID \
do { \
while (1); \
/*DownstreamInterfaceState = INTERFACE_STATE_ERROR;*/ \
/*return;*/ \
} while (0);
#define SPI_INTERFACE_FREAKOUT_HAL_ERROR \
do { \
while (1); \
/*DownstreamInterfaceState = INTERFACE_STATE_ERROR;*/ \
/*return HAL_ERROR;*/ \
} while (0);
typedef enum
{
INTERFACE_STATE_RESET = 0,
INTERFACE_STATE_WAITING_CLIENT = 1,
INTERFACE_STATE_IDLE = 2,
INTERFACE_STATE_TX_SIZE_WAIT = 3,
INTERFACE_STATE_TX_SIZE = 4,
INTERFACE_STATE_TX_PACKET_WAIT = 5,
INTERFACE_STATE_TX_PACKET = 6,
INTERFACE_STATE_RX_SIZE_WAIT = 7,
INTERFACE_STATE_RX_SIZE = 8,
INTERFACE_STATE_RX_PACKET_WAIT = 9,
INTERFACE_STATE_RX_PACKET = 10,
INTERFACE_STATE_ERROR = 11
} InterfaceStateTypeDef;
typedef enum
{
NOT_BUSY = 0,
BUSY = 1
} PacketBusyTypeDef;
typedef struct
{
PacketBusyTypeDef Busy; //Everything after Busy should be word-aligned
uint16_t Length __ALIGN_END; //Packet length includes CommandClass, Command, and Data
uint8_t CommandClass;
uint8_t Command;
uint8_t Data[MSC_MEDIA_PACKET]; //Should (must?) be word-aligned, for USB copy routine
uint8_t RxCrc;
}
DownstreamPacketTypeDef;
typedef void (*FreePacketCallbackTypeDef)(DownstreamPacketTypeDef* freePacket);
typedef void (*SpiPacketReceivedCallbackTypeDef)(DownstreamPacketTypeDef* replyPacket);
void Downstream_InitInterface(void);
HAL_StatusTypeDef Downstream_GetFreePacket(FreePacketCallbackTypeDef callback);
DownstreamPacketTypeDef* Downstream_GetFreePacketImmediately(void);
void Downstream_ReleasePacket(DownstreamPacketTypeDef* packetToRelease);
HAL_StatusTypeDef Downstream_SendPacket(DownstreamPacketTypeDef* packetToWrite);
HAL_StatusTypeDef Downstream_GetPacket(SpiPacketReceivedCallbackTypeDef callback);
void Downstream_TxOkInterrupt(void);
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
#endif /* INC_DOWNSTREAM_SPI_H_ */

@ -5,13 +5,13 @@
* Author: Robert Fisk * Author: Robert Fisk
*/ */
#ifndef INC_DOWNSTREAM_INTERFACE_DEF_H_ #ifndef INC_UPSTREAM_INTERFACE_DEF_H_
#define INC_DOWNSTREAM_INTERFACE_DEF_H_ #define INC_UPSTREAM_INTERFACE_DEF_H_
//*************** //***************
// Attention! // Attention!
// Keep this file synchronised with upstream_interface_def.h // Keep this file synchronised with downstream_interface_def.h
// in the Downstream project. // in the Downstream project.
//*************** //***************
@ -43,4 +43,4 @@ typedef enum
InterfaceCommandMscTypeDef; InterfaceCommandMscTypeDef;
#endif /* INC_DOWNSTREAM_INTERFACE_DEF_H_ */ #endif /* INC_UPSTREAM_INTERFACE_DEF_H_ */

@ -0,0 +1,39 @@
/*
* upstream_interface_msc.h
*
* Created on: 4/07/2015
* Author: Robert Fisk
*/
#ifndef INC_UPSTREAM_INTERFACE_MSC_H_
#define INC_UPSTREAM_INTERFACE_MSC_H_
#include <upstream_spi.h>
typedef void (*UpstreamInterfaceMSCCallbackTypeDef)(HAL_StatusTypeDef result);
typedef void (*UpstreamInterfaceMSCCallbackPacketTypeDef)(HAL_StatusTypeDef result,
UpstreamPacketTypeDef* upstreamPacket,
uint16_t dataLength);
typedef void (*UpstreamInterfaceMSCCallbackUintPacketTypeDef)(HAL_StatusTypeDef result,
uint32_t result_uint[],
UpstreamPacketTypeDef* upstreamPacket);
HAL_StatusTypeDef UpstreamInterface_TestReady(UpstreamInterfaceMSCCallbackTypeDef callback);
HAL_StatusTypeDef UpstreamInterface_GetCapacity(UpstreamInterfaceMSCCallbackUintPacketTypeDef callback);
HAL_StatusTypeDef UpstreamInterface_BeginRead(UpstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount,
uint32_t readByteCount);
HAL_StatusTypeDef UpstreamInterface_GetStreamDataPacket(UpstreamInterfaceMSCCallbackPacketTypeDef callback);
HAL_StatusTypeDef UpstreamInterface_BeginWrite(UpstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount);
HAL_StatusTypeDef UpstreamInterface_PutStreamDataPacket(UpstreamPacketTypeDef* packetToSend,
uint32_t dataLength);
#endif /* INC_UPSTREAM_INTERFACE_MSC_H_ */

@ -0,0 +1,89 @@
/*
* upstream_spi.h
*
* Created on: 21/06/2015
* Author: Robert Fisk
*/
#ifndef INC_UPSTREAM_SPI_H_
#define INC_UPSTREAM_SPI_H_
#include "usbd_config.h"
#define UPSTREAM_PACKET_HEADER_LEN (2) //Min length = CommandClass & Command bytes
#define UPSTREAM_PACKET_LEN (UPSTREAM_PACKET_HEADER_LEN + MSC_MEDIA_PACKET)
#define UPSTREAM_PACKET_LEN_MIN (UPSTREAM_PACKET_HEADER_LEN)
#define SPI_INTERFACE_FREAKOUT_VOID \
do { \
while (1); \
/*UpstreamInterfaceState = INTERFACE_STATE_ERROR;*/ \
/*return;*/ \
} while (0);
#define SPI_INTERFACE_FREAKOUT_HAL_ERROR \
do { \
while (1); \
/*UpstreamInterfaceState = INTERFACE_STATE_ERROR;*/ \
/*return HAL_ERROR;*/ \
} while (0);
typedef enum
{
UPSTREAM_INTERFACE_RESET,
UPSTREAM_INTERFACE_WAITING_CLIENT,
UPSTREAM_INTERFACE_IDLE,
UPSTREAM_INTERFACE_TX_SIZE_WAIT,
UPSTREAM_INTERFACE_TX_SIZE,
UPSTREAM_INTERFACE_TX_PACKET_WAIT,
UPSTREAM_INTERFACE_TX_PACKET,
UPSTREAM_INTERFACE_RX_SIZE_WAIT,
UPSTREAM_INTERFACE_RX_SIZE,
UPSTREAM_INTERFACE_RX_PACKET_WAIT,
UPSTREAM_INTERFACE_RX_PACKET,
UPSTREAM_INTERFACE_ERROR
} InterfaceStateTypeDef;
typedef enum
{
NOT_BUSY,
BUSY
} PacketBusyTypeDef;
typedef struct
{
PacketBusyTypeDef Busy; //Everything after Busy should be word-aligned
uint16_t Length __ALIGN_END; //Packet length includes CommandClass, Command, and Data
uint8_t CommandClass;
uint8_t Command;
uint8_t Data[MSC_MEDIA_PACKET]; //Should (must?) be word-aligned, for USB copy routine
uint8_t RxCrc;
}
UpstreamPacketTypeDef;
typedef void (*FreePacketCallbackTypeDef)(UpstreamPacketTypeDef* freePacket);
typedef void (*SpiPacketReceivedCallbackTypeDef)(UpstreamPacketTypeDef* replyPacket);
void Upstream_InitSPI(void);
HAL_StatusTypeDef Upstream_GetFreePacket(FreePacketCallbackTypeDef callback);
UpstreamPacketTypeDef* Upstream_GetFreePacketImmediately(void);
void Upstream_ReleasePacket(UpstreamPacketTypeDef* packetToRelease);
HAL_StatusTypeDef Upstream_SendPacket(UpstreamPacketTypeDef* packetToWrite);
HAL_StatusTypeDef Upstream_GetPacket(SpiPacketReceivedCallbackTypeDef callback);
void Upstream_TxOkInterrupt(void);
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi);
#endif /* INC_UPSTREAM_SPI_H_ */

@ -37,7 +37,7 @@
#include "usbd_msc_bot.h" #include "usbd_msc_bot.h"
#include "usbd_msc_scsi.h" #include "usbd_msc_scsi.h"
#include "usbd_ioreq.h" #include "usbd_ioreq.h"
#include "downstream_spi.h" #include <upstream_spi.h>
/** @addtogroup USBD_MSC_BOT /** @addtogroup USBD_MSC_BOT
* @{ * @{
@ -80,7 +80,7 @@ typedef struct
uint8_t bot_status; uint8_t bot_status;
uint16_t bot_data_length; uint16_t bot_data_length;
uint8_t* bot_data; uint8_t* bot_data;
DownstreamPacketTypeDef* bot_packet; //Not NULL indicates we currently own a downstream packet buffer, and should free it when we are done. UpstreamPacketTypeDef* bot_packet; //Not NULL indicates we currently own an upstream packet buffer, and should free it when we are done.
USBD_MSC_BOT_CBWTypeDef cbw; USBD_MSC_BOT_CBWTypeDef cbw;
USBD_MSC_BOT_CSWTypeDef csw; USBD_MSC_BOT_CSWTypeDef csw;

@ -1,105 +0,0 @@
/**
******************************************************************************
* @file usbd_msc_storage.h
* @author MCD Application Team
* @version V2.3.0
* @date 04-November-2014
* @brief Header file for the usbd_msc_storage.c file
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USBD_MSC_STORAGE_H
#define __USBD_MSC_STORAGE_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbd_msc.h"
/** @addtogroup STM32_USB_DEVICE_LIBRARY
* @{
*/
/** @defgroup USBD_STORAGE
* @brief header file for the usbd_msc_storage.c file
* @{
*/
/** @defgroup USBD_STORAGE_Exported_Defines
* @{
*/
/**
* @}
*/
/** @defgroup USBD_STORAGE_Exported_Types
* @{
*/
/**
* @}
*/
/** @defgroup USBD_STORAGE_Exported_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBD_STORAGE_Exported_Variables
* @{
*/
extern USBD_StorageTypeDef USBD_MSC_Template_fops;
/**
* @}
*/
/** @defgroup USBD_STORAGE_Exported_FunctionsPrototype
* @{
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBD_MSC_STORAGE_H */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -41,9 +41,9 @@
*/ */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include <upstream_interface_def.h>
#include <upstream_spi.h>
#include "usbd_msc.h" #include "usbd_msc.h"
#include "downstream_interface_def.h"
#include "downstream_spi.h"
/** @addtogroup STM32_USB_DEVICE_LIBRARY /** @addtogroup STM32_USB_DEVICE_LIBRARY
* @{ * @{
@ -542,7 +542,7 @@ uint8_t USBD_MSC_BufferFreed(USBD_HandleTypeDef *pdev)
{ {
if (((USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData)->bot_packet != NULL) if (((USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData)->bot_packet != NULL)
{ {
Downstream_ReleasePacket(((USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData)->bot_packet); Upstream_ReleasePacket(((USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData)->bot_packet);
((USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData)->bot_packet = NULL; ((USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData)->bot_packet = NULL;
} }
} }

@ -26,14 +26,14 @@
*/ */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include <upstream_interface_def.h>
#include <upstream_interface_msc.h>
#include <upstream_spi.h>
#include "usbd_msc_bot.h" #include "usbd_msc_bot.h"
#include "usbd_msc_scsi.h" #include "usbd_msc_scsi.h"
#include "usbd_msc.h" #include "usbd_msc.h"
#include "usbd_msc_data.h" #include "usbd_msc_data.h"
#include "usbd_descriptors.h" #include "usbd_descriptors.h"
#include "downstream_interface_def.h"
#include "downstream_interface_msc.h"
#include "downstream_spi.h"
/** @addtogroup STM32_USB_DEVICE_LIBRARY /** @addtogroup STM32_USB_DEVICE_LIBRARY
@ -106,16 +106,16 @@ static int8_t SCSI_CheckAddressRange (uint32_t blk_offset , uint16_t blk_nbr);
void SCSI_TestUnitReadyCallback(HAL_StatusTypeDef result); void SCSI_TestUnitReadyCallback(HAL_StatusTypeDef result);
void SCSI_ReadCapacity10Callback(HAL_StatusTypeDef result, void SCSI_ReadCapacity10Callback(HAL_StatusTypeDef result,
uint32_t result_uint[], uint32_t result_uint[],
DownstreamPacketTypeDef* downstreamPacket); UpstreamPacketTypeDef* upstreamPacket);
void SCSI_ReadFormatCapacityCallback(HAL_StatusTypeDef result, void SCSI_ReadFormatCapacityCallback(HAL_StatusTypeDef result,
uint32_t result_uint[], uint32_t result_uint[],
DownstreamPacketTypeDef* packetToUse); UpstreamPacketTypeDef* packetToUse);
void SCSI_Read10BeginCallback(HAL_StatusTypeDef result); void SCSI_Read10BeginCallback(HAL_StatusTypeDef result);
void SCSI_Read10ReplyCallback(HAL_StatusTypeDef result, void SCSI_Read10ReplyCallback(HAL_StatusTypeDef result,
DownstreamPacketTypeDef* downstreamPacket, UpstreamPacketTypeDef* upstreamPacket,
uint16_t dataLength); uint16_t dataLength);
void SCSI_Write10BeginCallback(HAL_StatusTypeDef result); void SCSI_Write10BeginCallback(HAL_StatusTypeDef result);
void SCSI_Write10FreePacketCallback(DownstreamPacketTypeDef* freePacket); void SCSI_Write10FreePacketCallback(UpstreamPacketTypeDef* freePacket);
/** /**
@ -234,7 +234,7 @@ void SCSI_TestUnitReady(void)
return; return;
} }
if (DownstreamInterface_TestReady(SCSI_TestUnitReadyCallback) != HAL_OK) if (UpstreamInterface_TestReady(SCSI_TestUnitReadyCallback) != HAL_OK)
{ {
SCSI_TestUnitReadyCallback(HAL_ERROR); SCSI_TestUnitReadyCallback(HAL_ERROR);
} }
@ -273,9 +273,9 @@ static void SCSI_Inquiry(void)
{ {
uint8_t* pPage; uint8_t* pPage;
uint16_t len; uint16_t len;
DownstreamPacketTypeDef* freePacket; UpstreamPacketTypeDef* freePacket;
freePacket = Downstream_GetFreePacketImmediately(); freePacket = Upstream_GetFreePacketImmediately();
SCSI_ProcessCmd_hmsc->bot_packet = freePacket; SCSI_ProcessCmd_hmsc->bot_packet = freePacket;
SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data; SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data;
@ -314,7 +314,7 @@ static void SCSI_Inquiry(void)
*/ */
static void SCSI_ReadCapacity10(void) static void SCSI_ReadCapacity10(void)
{ {
if (DownstreamInterface_GetCapacity(SCSI_ReadCapacity10Callback) != HAL_OK) if (UpstreamInterface_GetCapacity(SCSI_ReadCapacity10Callback) != HAL_OK)
{ {
SCSI_ReadCapacity10Callback(HAL_ERROR, NULL, NULL); SCSI_ReadCapacity10Callback(HAL_ERROR, NULL, NULL);
} }
@ -323,7 +323,7 @@ static void SCSI_ReadCapacity10(void)
void SCSI_ReadCapacity10Callback(HAL_StatusTypeDef result, void SCSI_ReadCapacity10Callback(HAL_StatusTypeDef result,
uint32_t result_uint[], uint32_t result_uint[],
DownstreamPacketTypeDef* downstreamPacket) UpstreamPacketTypeDef* upstreamPacket)
{ {
if (result != HAL_OK) if (result != HAL_OK)
{ {
@ -335,8 +335,8 @@ void SCSI_ReadCapacity10Callback(HAL_StatusTypeDef result,
return; return;
} }
SCSI_ProcessCmd_hmsc->bot_packet = downstreamPacket; SCSI_ProcessCmd_hmsc->bot_packet = upstreamPacket;
SCSI_ProcessCmd_hmsc->bot_data = downstreamPacket->Data; SCSI_ProcessCmd_hmsc->bot_data = upstreamPacket->Data;
SCSI_ProcessCmd_hmsc->scsi_blk_nbr = result_uint[0]; SCSI_ProcessCmd_hmsc->scsi_blk_nbr = result_uint[0];
SCSI_ProcessCmd_hmsc->scsi_blk_size = result_uint[1]; SCSI_ProcessCmd_hmsc->scsi_blk_size = result_uint[1];
@ -364,7 +364,7 @@ void SCSI_ReadCapacity10Callback(HAL_StatusTypeDef result,
*/ */
static void SCSI_ReadFormatCapacity(void) static void SCSI_ReadFormatCapacity(void)
{ {
if (DownstreamInterface_GetCapacity(SCSI_ReadFormatCapacityCallback) != HAL_OK) if (UpstreamInterface_GetCapacity(SCSI_ReadFormatCapacityCallback) != HAL_OK)
{ {
SCSI_ReadFormatCapacityCallback(HAL_ERROR, NULL, NULL); SCSI_ReadFormatCapacityCallback(HAL_ERROR, NULL, NULL);
} }
@ -373,7 +373,7 @@ static void SCSI_ReadFormatCapacity(void)
void SCSI_ReadFormatCapacityCallback(HAL_StatusTypeDef result, void SCSI_ReadFormatCapacityCallback(HAL_StatusTypeDef result,
uint32_t result_uint[], uint32_t result_uint[],
DownstreamPacketTypeDef* packetToUse) UpstreamPacketTypeDef* packetToUse)
{ {
if (result != HAL_OK) if (result != HAL_OK)
{ {
@ -416,9 +416,9 @@ void SCSI_ReadFormatCapacityCallback(HAL_StatusTypeDef result,
static void SCSI_ModeSense6 (void) static void SCSI_ModeSense6 (void)
{ {
uint16_t len = 8; uint16_t len = 8;
DownstreamPacketTypeDef* freePacket; UpstreamPacketTypeDef* freePacket;
freePacket = Downstream_GetFreePacketImmediately(); freePacket = Upstream_GetFreePacketImmediately();
SCSI_ProcessCmd_hmsc->bot_packet = freePacket; SCSI_ProcessCmd_hmsc->bot_packet = freePacket;
SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data; SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data;
@ -442,9 +442,9 @@ static void SCSI_ModeSense6 (void)
static void SCSI_ModeSense10(void) static void SCSI_ModeSense10(void)
{ {
uint16_t len = 8; uint16_t len = 8;
DownstreamPacketTypeDef* freePacket; UpstreamPacketTypeDef* freePacket;
freePacket = Downstream_GetFreePacketImmediately(); freePacket = Upstream_GetFreePacketImmediately();
SCSI_ProcessCmd_hmsc->bot_packet = freePacket; SCSI_ProcessCmd_hmsc->bot_packet = freePacket;
SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data; SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data;
@ -469,9 +469,9 @@ static void SCSI_ModeSense10(void)
static void SCSI_RequestSense(void) static void SCSI_RequestSense(void)
{ {
uint8_t i; uint8_t i;
DownstreamPacketTypeDef* freePacket; UpstreamPacketTypeDef* freePacket;
freePacket = Downstream_GetFreePacketImmediately(); freePacket = Upstream_GetFreePacketImmediately();
SCSI_ProcessCmd_hmsc->bot_packet = freePacket; SCSI_ProcessCmd_hmsc->bot_packet = freePacket;
SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data; SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data;
@ -589,7 +589,7 @@ static void SCSI_Read10(void)
return; return;
} }
if (DownstreamInterface_BeginRead(SCSI_Read10BeginCallback, if (UpstreamInterface_BeginRead(SCSI_Read10BeginCallback,
SCSI_ProcessCmd_hmsc->scsi_blk_addr, SCSI_ProcessCmd_hmsc->scsi_blk_addr,
SCSI_ProcessCmd_hmsc->scsi_blk_len, SCSI_ProcessCmd_hmsc->scsi_blk_len,
SCSI_ProcessCmd_hmsc->cbw.dDataLength) != HAL_OK) SCSI_ProcessCmd_hmsc->cbw.dDataLength) != HAL_OK)
@ -600,7 +600,7 @@ static void SCSI_Read10(void)
} }
//hmsc->bot_state is already USBD_BOT_DATA_IN //hmsc->bot_state is already USBD_BOT_DATA_IN
if (DownstreamInterface_GetStreamDataPacket(SCSI_Read10ReplyCallback) != HAL_OK) if (UpstreamInterface_GetStreamDataPacket(SCSI_Read10ReplyCallback) != HAL_OK)
{ {
SCSI_Read10ReplyCallback(HAL_ERROR, NULL, 0); SCSI_Read10ReplyCallback(HAL_ERROR, NULL, 0);
} }
@ -620,7 +620,7 @@ void SCSI_Read10BeginCallback(HAL_StatusTypeDef result)
} }
SCSI_ProcessCmd_hmsc->bot_state = USBD_BOT_DATA_IN; SCSI_ProcessCmd_hmsc->bot_state = USBD_BOT_DATA_IN;
if (DownstreamInterface_GetStreamDataPacket(SCSI_Read10ReplyCallback) != HAL_OK) if (UpstreamInterface_GetStreamDataPacket(SCSI_Read10ReplyCallback) != HAL_OK)
{ {
SCSI_Read10ReplyCallback(HAL_ERROR, NULL, 0); SCSI_Read10ReplyCallback(HAL_ERROR, NULL, 0);
} }
@ -628,7 +628,7 @@ void SCSI_Read10BeginCallback(HAL_StatusTypeDef result)
void SCSI_Read10ReplyCallback(HAL_StatusTypeDef result, void SCSI_Read10ReplyCallback(HAL_StatusTypeDef result,
DownstreamPacketTypeDef* downstreamPacket, UpstreamPacketTypeDef* upstreamPacket,
uint16_t dataLength) uint16_t dataLength)
{ {
if (result != HAL_OK) if (result != HAL_OK)
@ -644,8 +644,8 @@ void SCSI_Read10ReplyCallback(HAL_StatusTypeDef result,
if (SCSI_ProcessCmd_hmsc->bot_packet != NULL) if (SCSI_ProcessCmd_hmsc->bot_packet != NULL)
while (1); /////////////////////////////////////////! while (1); /////////////////////////////////////////!
SCSI_ProcessCmd_hmsc->bot_packet = downstreamPacket; SCSI_ProcessCmd_hmsc->bot_packet = upstreamPacket;
SCSI_ProcessCmd_hmsc->bot_data = downstreamPacket->Data; SCSI_ProcessCmd_hmsc->bot_data = upstreamPacket->Data;
USBD_LL_Transmit (SCSI_ProcessCmd_pdev, USBD_LL_Transmit (SCSI_ProcessCmd_pdev,
MSC_EPIN_ADDR, MSC_EPIN_ADDR,
SCSI_ProcessCmd_hmsc->bot_data, SCSI_ProcessCmd_hmsc->bot_data,
@ -716,7 +716,7 @@ static void SCSI_Write10(void)
return; return;
} }
if (DownstreamInterface_BeginWrite(SCSI_Write10BeginCallback, if (UpstreamInterface_BeginWrite(SCSI_Write10BeginCallback,
SCSI_ProcessCmd_hmsc->scsi_blk_addr, SCSI_ProcessCmd_hmsc->scsi_blk_addr,
SCSI_ProcessCmd_hmsc->scsi_blk_len) != HAL_OK) SCSI_ProcessCmd_hmsc->scsi_blk_len) != HAL_OK)
{ {
@ -728,7 +728,7 @@ static void SCSI_Write10(void)
//hmsc->bot_state is already USBD_BOT_DATA_OUT //hmsc->bot_state is already USBD_BOT_DATA_OUT
dataLength = MIN(SCSI_ProcessCmd_hmsc->csw.dDataResidue, MSC_MEDIA_PACKET); dataLength = MIN(SCSI_ProcessCmd_hmsc->csw.dDataResidue, MSC_MEDIA_PACKET);
if (DownstreamInterface_PutStreamDataPacket(SCSI_ProcessCmd_hmsc->bot_packet, if (UpstreamInterface_PutStreamDataPacket(SCSI_ProcessCmd_hmsc->bot_packet,
dataLength) != HAL_OK) dataLength) != HAL_OK)
{ {
SCSI_SenseCode(SCSI_ProcessCmd_pdev, SCSI_SenseCode(SCSI_ProcessCmd_pdev,
@ -748,7 +748,7 @@ static void SCSI_Write10(void)
} }
/* Prepare EP to Receive next packet */ /* Prepare EP to Receive next packet */
if (Downstream_GetFreePacket(SCSI_Write10FreePacketCallback) != HAL_OK) if (Upstream_GetFreePacket(SCSI_Write10FreePacketCallback) != HAL_OK)
{ {
SCSI_SenseCode(SCSI_ProcessCmd_pdev, SCSI_SenseCode(SCSI_ProcessCmd_pdev,
SCSI_ProcessCmd_lun, SCSI_ProcessCmd_lun,
@ -782,7 +782,7 @@ void SCSI_Write10BeginCallback(HAL_StatusTypeDef result)
/* Prepare EP to receive first data packet */ /* Prepare EP to receive first data packet */
SCSI_ProcessCmd_hmsc->bot_state = USBD_BOT_DATA_OUT; SCSI_ProcessCmd_hmsc->bot_state = USBD_BOT_DATA_OUT;
if (Downstream_GetFreePacket(SCSI_Write10FreePacketCallback) != HAL_OK) if (Upstream_GetFreePacket(SCSI_Write10FreePacketCallback) != HAL_OK)
{ {
SCSI_SenseCode(SCSI_ProcessCmd_pdev, SCSI_SenseCode(SCSI_ProcessCmd_pdev,
SCSI_ProcessCmd_lun, SCSI_ProcessCmd_lun,
@ -793,7 +793,7 @@ void SCSI_Write10BeginCallback(HAL_StatusTypeDef result)
} }
void SCSI_Write10FreePacketCallback(DownstreamPacketTypeDef* freePacket) void SCSI_Write10FreePacketCallback(UpstreamPacketTypeDef* freePacket)
{ {
SCSI_ProcessCmd_hmsc->bot_packet = freePacket; SCSI_ProcessCmd_hmsc->bot_packet = freePacket;
SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data; SCSI_ProcessCmd_hmsc->bot_data = freePacket->Data;

@ -1,232 +0,0 @@
/*
* downstream_interface_msc.c
*
* Created on: 4/07/2015
* Author: Robert Fisk
*/
#include "stm32f4xx_hal.h"
#include "downstream_interface_msc.h"
#include "downstream_interface_def.h"
#include "downstream_spi.h"
//Stuff we need to save for our callbacks to use:
DownstreamInterfaceMSCCallbackTypeDef TestReadyCallback;
DownstreamInterfaceMSCCallbackUintPacketTypeDef GetCapacityCallback;
DownstreamInterfaceMSCCallbackPacketTypeDef GetStreamDataCallback;
uint32_t ByteCount;
DownstreamPacketTypeDef* ReadStreamPacket;
uint8_t ReadStreamBusy;
static void DownstreamInterface_TestReadyReplyCallback(DownstreamPacketTypeDef* replyPacket);
static void DownstreamInterface_GetCapacityReplyCallback(DownstreamPacketTypeDef* replyPacket);
static void DownstreamInterface_GetStreamDataPacketCallback(DownstreamPacketTypeDef* replyPacket);
static void DownstreamInterface_BeginWriteReplyCallback(DownstreamPacketTypeDef* replyPacket);
HAL_StatusTypeDef DownstreamInterface_TestReady(DownstreamInterfaceMSCCallbackTypeDef callback)
{
DownstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
TestReadyCallback = callback;
freePacket = Downstream_GetFreePacketImmediately();
freePacket->Length = DOWNSTREAM_PACKET_HEADER_LEN;
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_TEST_UNIT_READY;
tempResult = Downstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
return tempResult;
}
return Downstream_GetPacket(DownstreamInterface_TestReadyReplyCallback);
}
void DownstreamInterface_TestReadyReplyCallback(DownstreamPacketTypeDef* replyPacket)
{
if ((replyPacket->Length != (DOWNSTREAM_PACKET_HEADER_LEN + 1)) ||
(replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) ||
(replyPacket->Data[0] != HAL_OK))
{
Downstream_ReleasePacket(replyPacket);
TestReadyCallback(HAL_ERROR);
return;
}
Downstream_ReleasePacket(replyPacket);
TestReadyCallback(HAL_OK);
}
HAL_StatusTypeDef DownstreamInterface_GetCapacity(DownstreamInterfaceMSCCallbackUintPacketTypeDef callback)
{
DownstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
GetCapacityCallback = callback;
freePacket = Downstream_GetFreePacketImmediately();
freePacket->Length = DOWNSTREAM_PACKET_HEADER_LEN;
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_GET_CAPACITY;
tempResult = Downstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
return tempResult;
}
return Downstream_GetPacket(DownstreamInterface_GetCapacityReplyCallback);
}
void DownstreamInterface_GetCapacityReplyCallback(DownstreamPacketTypeDef* replyPacket)
{
uint32_t uint[2];
if ((replyPacket->Length != (DOWNSTREAM_PACKET_HEADER_LEN + 8) ||
(replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG)))
{
GetCapacityCallback(HAL_ERROR, NULL, NULL);
return;
}
uint[0] = *(uint32_t*)&(replyPacket->Data[0]);
uint[1] = *(uint32_t*)&(replyPacket->Data[4]);
GetCapacityCallback(HAL_OK, uint, replyPacket); //usb_msc_scsi will use this packet, so don't release now
}
HAL_StatusTypeDef DownstreamInterface_BeginRead(DownstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount,
uint32_t readByteCount)
{
DownstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
ReadStreamPacket = NULL; //Prepare for GetStreamDataPacket's use
ReadStreamBusy = 0;
TestReadyCallback = callback;
ByteCount = readByteCount;
freePacket = Downstream_GetFreePacketImmediately();
freePacket->Length = DOWNSTREAM_PACKET_HEADER_LEN + (4 * 3);
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_BEGIN_READ;
*(uint64_t*)&(freePacket->Data[0]) = readBlockStart;
*(uint32_t*)&(freePacket->Data[8]) = readBlockCount;
tempResult = Downstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
TestReadyCallback(tempResult);
}
return Downstream_GetPacket(DownstreamInterface_TestReadyReplyCallback); //Re-use TestReadyReplyCallback because it does exactly what we want!
}
HAL_StatusTypeDef DownstreamInterface_GetStreamDataPacket(DownstreamInterfaceMSCCallbackPacketTypeDef callback)
{
GetStreamDataCallback = callback;
if (ReadStreamBusy != 0)
{
return HAL_OK;
}
ReadStreamBusy = 1;
if (ReadStreamPacket && GetStreamDataCallback) //Do we have a stored packet and an address to send it?
{
DownstreamInterface_GetStreamDataPacketCallback(ReadStreamPacket); //Send it now!
ReadStreamPacket = NULL;
return HAL_OK; //Our callback will call us again, so we don't need to get a packet in this case.
}
return Downstream_GetPacket(DownstreamInterface_GetStreamDataPacketCallback);
}
void DownstreamInterface_GetStreamDataPacketCallback(DownstreamPacketTypeDef* replyPacket)
{
uint16_t dataLength;
ReadStreamBusy = 0;
if (GetStreamDataCallback == NULL)
{
ReadStreamPacket = replyPacket; //We used up our callback already, so save this one for later.
return;
}
if (((replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) == 0) || //Any 'command' reply (as opposed to 'data' reply) is an automatic fail here
(replyPacket->Length <= DOWNSTREAM_PACKET_HEADER_LEN) || //Should be at least one data byte in the reply.
(replyPacket->Length > ByteCount))
{
GetStreamDataCallback(HAL_ERROR, NULL);
return;
}
dataLength = replyPacket->Length - DOWNSTREAM_PACKET_HEADER_LEN;
ByteCount -= dataLength;
GetStreamDataCallback(HAL_OK, replyPacket, dataLength); //usb_msc_scsi will use this packet, so don't release now
if (ByteCount > 0)
{
DownstreamInterface_GetStreamDataPacket(NULL); //Try to get the next packet now, before USB asks for it
}
}
HAL_StatusTypeDef DownstreamInterface_BeginWrite(DownstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount)
{
DownstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
TestReadyCallback = callback;
freePacket = Downstream_GetFreePacketImmediately();
freePacket->Length = DOWNSTREAM_PACKET_HEADER_LEN + (4 * 3);
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_BEGIN_WRITE;
*(uint64_t*)&(freePacket->Data[0]) = readBlockStart;
*(uint32_t*)&(freePacket->Data[8]) = readBlockCount;
tempResult = Downstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
TestReadyCallback(tempResult);
}
return Downstream_GetPacket(DownstreamInterface_BeginWriteReplyCallback);
}
void DownstreamInterface_BeginWriteReplyCallback(DownstreamPacketTypeDef* replyPacket)
{
if ((replyPacket->Length != (DOWNSTREAM_PACKET_HEADER_LEN + 1)) ||
(replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) ||
((replyPacket->Data[0] != HAL_OK) && (replyPacket->Data[0] != HAL_BUSY)))
{
Downstream_ReleasePacket(replyPacket);
TestReadyCallback(HAL_ERROR);
return;
}
Downstream_ReleasePacket(replyPacket);
TestReadyCallback(replyPacket->Data[0]);
}
HAL_StatusTypeDef DownstreamInterface_PutStreamDataPacket(DownstreamPacketTypeDef* packetToSend,
uint32_t dataLength)
{
packetToSend->Length = dataLength + DOWNSTREAM_PACKET_HEADER_LEN;
packetToSend->CommandClass = COMMAND_CLASS_MASS_STORAGE | COMMAND_CLASS_DATA_FLAG;
packetToSend->Command = COMMAND_MSC_BEGIN_WRITE;
return Downstream_SendPacket(packetToSend);
}

@ -1,245 +0,0 @@
/*
* downstream_interface_msc_mock.c
*
* Created on: 17/07/2015
* Author: Robert Fisk
*
* This file replaces downstream_interface_msc.c to allow operational
* testing of Upstream, without Downstream in place and communicating
* over SPI. It still attempts to write downstream packets out the SPI port.
*/
#include "stm32f4xx_hal.h"
#include "downstream_interface_msc.h"
#include "downstream_interface_def.h"
#include "downstream_spi.h"
//Stuff we need to save for our callbacks to use:
DownstreamInterfaceMSCCallbackTypeDef TestReadyCallback;
DownstreamInterfaceMSCCallbackUintPacketTypeDef GetCapacityCallback;
DownstreamInterfaceMSCCallbackPacketTypeDef GetStreamDataCallback;
uint32_t ByteCount;
DownstreamPacketTypeDef* ReadStreamPacket;
uint8_t ReadStreamBusy;
static void DownstreamInterface_TestReadyReplyCallback(DownstreamPacketTypeDef* replyPacket);
static void DownstreamInterface_GetCapacityReplyCallback(DownstreamPacketTypeDef* replyPacket);
static void DownstreamInterface_GetStreamDataPacketCallback(DownstreamPacketTypeDef* replyPacket);
static void DownstreamInterface_BeginWriteReplyCallback(DownstreamPacketTypeDef* replyPacket);
HAL_StatusTypeDef DownstreamInterface_TestReady(DownstreamInterfaceMSCCallbackTypeDef callback)
{
DownstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
TestReadyCallback = callback;
freePacket = Downstream_GetFreePacketImmediately();
freePacket->Length = DOWNSTREAM_PACKET_HEADER_LEN;
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_TEST_UNIT_READY;
tempResult = Downstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
return tempResult;
}
//return Downstream_GetPacket(DownstreamInterface_TestReadyReplyCallback);
return Downstream_GetFreePacket(DownstreamInterface_TestReadyReplyCallback);
}
void DownstreamInterface_TestReadyReplyCallback(DownstreamPacketTypeDef* replyPacket)
{
// if ((replyPacket->Length != (DOWNSTREAM_PACKET_HEADER_LEN + 1)) ||
// (replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) ||
// (replyPacket->Data[0] != HAL_OK))
// {
// Downstream_ReleasePacket(replyPacket);
// TestReadyCallback(HAL_ERROR);
// return;
// }
Downstream_ReleasePacket(replyPacket);
TestReadyCallback(HAL_OK);
}
HAL_StatusTypeDef DownstreamInterface_GetCapacity(DownstreamInterfaceMSCCallbackUintPacketTypeDef callback)
{
DownstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
GetCapacityCallback = callback;
freePacket = Downstream_GetFreePacketImmediately();
freePacket->Length = DOWNSTREAM_PACKET_HEADER_LEN;
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_GET_CAPACITY;
tempResult = Downstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
return tempResult;
}
//return Downstream_GetPacket(DownstreamInterface_GetCapacityReplyCallback);
return Downstream_GetFreePacket(DownstreamInterface_GetCapacityReplyCallback);
}
void DownstreamInterface_GetCapacityReplyCallback(DownstreamPacketTypeDef* replyPacket)
{
uint32_t uint[2];
// if ((replyPacket->Length != (DOWNSTREAM_PACKET_HEADER_LEN + 8) ||
// (replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG)))
// {
// GetCapacityCallback(HAL_ERROR, NULL, NULL);
// }
*(uint32_t*)&(replyPacket->Data[0]) = 262144; //* 512B = 128MB
*(uint32_t*)&(replyPacket->Data[4]) = 512;
uint[0] = *(uint32_t*)&(replyPacket->Data[0]);
uint[1] = *(uint32_t*)&(replyPacket->Data[4]);
GetCapacityCallback(HAL_OK, uint, replyPacket); //usb_msc_scsi will use this packet, so don't release now
}
HAL_StatusTypeDef DownstreamInterface_BeginRead(DownstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount,
uint32_t readByteCount)
{
DownstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
ReadStreamPacket = NULL; //Prepare for GetStreamDataPacket's use
ReadStreamBusy = 0;
TestReadyCallback = callback;
ByteCount = readByteCount;
freePacket = Downstream_GetFreePacketImmediately();
freePacket->Length = DOWNSTREAM_PACKET_HEADER_LEN + (4 * 3);
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_BEGIN_READ;
*(uint64_t*)&(freePacket->Data[0]) = readBlockStart;
*(uint32_t*)&(freePacket->Data[8]) = readBlockCount;
tempResult = Downstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
TestReadyCallback(tempResult);
}
//return Downstream_GetPacket(DownstreamInterface_TestReadyReplyCallback); //Re-use TestReadyReplyCallback because it does exactly what we want!
return Downstream_GetFreePacket(DownstreamInterface_TestReadyReplyCallback);
}
HAL_StatusTypeDef DownstreamInterface_GetStreamDataPacket(DownstreamInterfaceMSCCallbackPacketTypeDef callback)
{
GetStreamDataCallback = callback;
if (ReadStreamBusy != 0)
{
return HAL_OK;
}
ReadStreamBusy = 1;
if (ReadStreamPacket && GetStreamDataCallback) //Do we have a stored packet and an address to send it?
{
DownstreamInterface_GetStreamDataPacketCallback(ReadStreamPacket); //Send it now!
ReadStreamPacket = NULL;
return HAL_OK; //Our callback will call us again, so we don't need to get a packet in this case.
}
//return Downstream_GetPacket(DownstreamInterface_GetStreamDataPacketCallback);
return Downstream_GetFreePacket(DownstreamInterface_GetStreamDataPacketCallback);
}
void DownstreamInterface_GetStreamDataPacketCallback(DownstreamPacketTypeDef* replyPacket)
{
uint16_t dataLength;
ReadStreamBusy = 0;
if (GetStreamDataCallback == NULL)
{
ReadStreamPacket = replyPacket; //We used up our callback already, so save this one for later.
return;
}
// if (((replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) == 0) || //Any 'command' reply (as opposed to 'data' reply) is an automatic fail here
// (replyPacket->Length <= DOWNSTREAM_PACKET_HEADER_LEN) || //Should be at least one data byte in the reply.
// (replyPacket->Length > ByteCount))
// {
// GetStreamDataCallback(HAL_ERROR, NULL);
// return;
// }
replyPacket->Length = MIN((ByteCount + DOWNSTREAM_PACKET_HEADER_LEN), (MSC_MEDIA_PACKET + DOWNSTREAM_PACKET_HEADER_LEN));
dataLength = replyPacket->Length - DOWNSTREAM_PACKET_HEADER_LEN;
ByteCount -= dataLength;
GetStreamDataCallback(HAL_OK, replyPacket, dataLength); //usb_msc_scsi will use this packet, so don't release now
if (ByteCount > 0)
{
DownstreamInterface_GetStreamDataPacket(NULL); //Try to get the next packet now, before USB asks for it
}
}
HAL_StatusTypeDef DownstreamInterface_BeginWrite(DownstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount)
{
DownstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
TestReadyCallback = callback;
freePacket = Downstream_GetFreePacketImmediately();
freePacket->Length = DOWNSTREAM_PACKET_HEADER_LEN + (4 * 3);
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_BEGIN_WRITE;
*(uint64_t*)&(freePacket->Data[0]) = readBlockStart;
*(uint32_t*)&(freePacket->Data[8]) = readBlockCount;
tempResult = Downstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
TestReadyCallback(tempResult);
}
//return Downstream_GetPacket(DownstreamInterface_BeginWriteReplyCallback);
return Downstream_GetFreePacket(DownstreamInterface_BeginWriteReplyCallback);
}
void DownstreamInterface_BeginWriteReplyCallback(DownstreamPacketTypeDef* replyPacket)
{
// if ((replyPacket->Length != (DOWNSTREAM_PACKET_HEADER_LEN + 1)) ||
// (replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) ||
// ((replyPacket->Data[0] != HAL_OK) && (replyPacket->Data[0] != HAL_BUSY)))
// {
// Downstream_ReleasePacket(replyPacket);
// TestReadyCallback(HAL_ERROR);
// return;
// }
Downstream_ReleasePacket(replyPacket);
TestReadyCallback(replyPacket->Data[0]);
}
HAL_StatusTypeDef DownstreamInterface_PutStreamDataPacket(DownstreamPacketTypeDef* packetToSend,
uint32_t dataLength)
{
packetToSend->Length = dataLength + DOWNSTREAM_PACKET_HEADER_LEN;
packetToSend->CommandClass = COMMAND_CLASS_MASS_STORAGE | COMMAND_CLASS_DATA_FLAG;
packetToSend->Command = COMMAND_MSC_BEGIN_WRITE;
return Downstream_SendPacket(packetToSend);
}

@ -33,10 +33,10 @@
*/ */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include <interrupts.h> #include <interrupts.h>
#include <upstream_spi.h>
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
#include "stm32f4xx.h" #include "stm32f4xx.h"
#include "board_config.h" #include "board_config.h"
#include "downstream_spi.h"
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
@ -88,7 +88,7 @@ void EXTI3_IRQHandler(void)
{ {
SPI_DMA_ACTIVE_ON; SPI_DMA_ACTIVE_ON;
__HAL_GPIO_EXTI_CLEAR_IT(3); __HAL_GPIO_EXTI_CLEAR_IT(3);
Downstream_TxOkInterrupt(); Upstream_TxOkInterrupt();
SPI_DMA_ACTIVE_OFF; SPI_DMA_ACTIVE_OFF;
} }
///////////////////////// /////////////////////////

@ -33,7 +33,7 @@
*/ */
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include <downstream_spi.h> #include <upstream_spi.h>
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
#include "usb_device.h" #include "usb_device.h"
#include "board_config.h" #include "board_config.h"
@ -64,7 +64,7 @@ int main(void)
GPIO_Init(); GPIO_Init();
USB_Device_Init(); USB_Device_Init();
Downstream_InitInterface(); Upstream_InitSPI();
while (1) while (1)

@ -0,0 +1,232 @@
/*
* upstream_interface_msc.c
*
* Created on: 4/07/2015
* Author: Robert Fisk
*/
#include <upstream_interface_def.h>
#include <upstream_interface_msc.h>
#include <upstream_spi.h>
#include "stm32f4xx_hal.h"
//Stuff we need to save for our callbacks to use:
UpstreamInterfaceMSCCallbackTypeDef TestReadyCallback;
UpstreamInterfaceMSCCallbackUintPacketTypeDef GetCapacityCallback;
UpstreamInterfaceMSCCallbackPacketTypeDef GetStreamDataCallback;
uint32_t ByteCount;
UpstreamPacketTypeDef* ReadStreamPacket;
uint8_t ReadStreamBusy;
static void UpstreamInterface_TestReadyReplyCallback(UpstreamPacketTypeDef* replyPacket);
static void UpstreamInterface_GetCapacityReplyCallback(UpstreamPacketTypeDef* replyPacket);
static void UpstreamInterface_GetStreamDataPacketCallback(UpstreamPacketTypeDef* replyPacket);
static void UpstreamInterface_BeginWriteReplyCallback(UpstreamPacketTypeDef* replyPacket);
HAL_StatusTypeDef UpstreamInterface_TestReady(UpstreamInterfaceMSCCallbackTypeDef callback)
{
UpstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
TestReadyCallback = callback;
freePacket = Upstream_GetFreePacketImmediately();
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN;
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_TEST_UNIT_READY;
tempResult = Upstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
return tempResult;
}
return Upstream_GetPacket(UpstreamInterface_TestReadyReplyCallback);
}
void UpstreamInterface_TestReadyReplyCallback(UpstreamPacketTypeDef* replyPacket)
{
if ((replyPacket->Length != (UPSTREAM_PACKET_HEADER_LEN + 1)) ||
(replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) ||
(replyPacket->Data[0] != HAL_OK))
{
Upstream_ReleasePacket(replyPacket);
TestReadyCallback(HAL_ERROR);
return;
}
Upstream_ReleasePacket(replyPacket);
TestReadyCallback(HAL_OK);
}
HAL_StatusTypeDef UpstreamInterface_GetCapacity(UpstreamInterfaceMSCCallbackUintPacketTypeDef callback)
{
UpstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
GetCapacityCallback = callback;
freePacket = Upstream_GetFreePacketImmediately();
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN;
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_GET_CAPACITY;
tempResult = Upstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
return tempResult;
}
return Upstream_GetPacket(UpstreamInterface_GetCapacityReplyCallback);
}
void UpstreamInterface_GetCapacityReplyCallback(UpstreamPacketTypeDef* replyPacket)
{
uint32_t uint[2];
if ((replyPacket->Length != (UPSTREAM_PACKET_HEADER_LEN + 8) ||
(replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG)))
{
GetCapacityCallback(HAL_ERROR, NULL, NULL);
return;
}
uint[0] = *(uint32_t*)&(replyPacket->Data[0]);
uint[1] = *(uint32_t*)&(replyPacket->Data[4]);
GetCapacityCallback(HAL_OK, uint, replyPacket); //usb_msc_scsi will use this packet, so don't release now
}
HAL_StatusTypeDef UpstreamInterface_BeginRead(UpstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount,
uint32_t readByteCount)
{
UpstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
ReadStreamPacket = NULL; //Prepare for GetStreamDataPacket's use
ReadStreamBusy = 0;
TestReadyCallback = callback;
ByteCount = readByteCount;
freePacket = Upstream_GetFreePacketImmediately();
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN + (4 * 3);
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_BEGIN_READ;
*(uint64_t*)&(freePacket->Data[0]) = readBlockStart;
*(uint32_t*)&(freePacket->Data[8]) = readBlockCount;
tempResult = Upstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
TestReadyCallback(tempResult);
}
return Upstream_GetPacket(UpstreamInterface_TestReadyReplyCallback); //Re-use TestReadyReplyCallback because it does exactly what we want!
}
HAL_StatusTypeDef UpstreamInterface_GetStreamDataPacket(UpstreamInterfaceMSCCallbackPacketTypeDef callback)
{
GetStreamDataCallback = callback;
if (ReadStreamBusy != 0)
{
return HAL_OK;
}
ReadStreamBusy = 1;
if (ReadStreamPacket && GetStreamDataCallback) //Do we have a stored packet and an address to send it?
{
UpstreamInterface_GetStreamDataPacketCallback(ReadStreamPacket); //Send it now!
ReadStreamPacket = NULL;
return HAL_OK; //Our callback will call us again, so we don't need to get a packet in this case.
}
return Upstream_GetPacket(UpstreamInterface_GetStreamDataPacketCallback);
}
void UpstreamInterface_GetStreamDataPacketCallback(UpstreamPacketTypeDef* replyPacket)
{
uint16_t dataLength;
ReadStreamBusy = 0;
if (GetStreamDataCallback == NULL)
{
ReadStreamPacket = replyPacket; //We used up our callback already, so save this one for later.
return;
}
if (((replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) == 0) || //Any 'command' reply (as opposed to 'data' reply) is an automatic fail here
(replyPacket->Length <= UPSTREAM_PACKET_HEADER_LEN) || //Should be at least one data byte in the reply.
(replyPacket->Length > ByteCount))
{
GetStreamDataCallback(HAL_ERROR, NULL, NULL);
return;
}
dataLength = replyPacket->Length - UPSTREAM_PACKET_HEADER_LEN;
ByteCount -= dataLength;
GetStreamDataCallback(HAL_OK, replyPacket, dataLength); //usb_msc_scsi will use this packet, so don't release now
if (ByteCount > 0)
{
UpstreamInterface_GetStreamDataPacket(NULL); //Try to get the next packet now, before USB asks for it
}
}
HAL_StatusTypeDef UpstreamInterface_BeginWrite(UpstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount)
{
UpstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
TestReadyCallback = callback;
freePacket = Upstream_GetFreePacketImmediately();
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN + (4 * 3);
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_BEGIN_WRITE;
*(uint64_t*)&(freePacket->Data[0]) = readBlockStart;
*(uint32_t*)&(freePacket->Data[8]) = readBlockCount;
tempResult = Upstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
TestReadyCallback(tempResult);
}
return Upstream_GetPacket(UpstreamInterface_BeginWriteReplyCallback);
}
void UpstreamInterface_BeginWriteReplyCallback(UpstreamPacketTypeDef* replyPacket)
{
if ((replyPacket->Length != (UPSTREAM_PACKET_HEADER_LEN + 1)) ||
(replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) ||
((replyPacket->Data[0] != HAL_OK) && (replyPacket->Data[0] != HAL_BUSY)))
{
Upstream_ReleasePacket(replyPacket);
TestReadyCallback(HAL_ERROR);
return;
}
Upstream_ReleasePacket(replyPacket);
TestReadyCallback(replyPacket->Data[0]);
}
HAL_StatusTypeDef UpstreamInterface_PutStreamDataPacket(UpstreamPacketTypeDef* packetToSend,
uint32_t dataLength)
{
packetToSend->Length = dataLength + UPSTREAM_PACKET_HEADER_LEN;
packetToSend->CommandClass = COMMAND_CLASS_MASS_STORAGE | COMMAND_CLASS_DATA_FLAG;
packetToSend->Command = COMMAND_MSC_BEGIN_WRITE;
return Upstream_SendPacket(packetToSend);
}

@ -0,0 +1,245 @@
/*
* upstream_interface_msc_mock.c
*
* Created on: 17/07/2015
* Author: Robert Fisk
*
* This file replaces upstream_interface_msc.c to allow operational
* testing of Upstream, without Upstream in place and communicating
* over SPI. It still attempts to write downstream packets out the SPI port.
*/
#include <upstream_interface_def.h>
#include <upstream_interface_msc.h>
#include <upstream_spi.h>
#include "stm32f4xx_hal.h"
//Stuff we need to save for our callbacks to use:
UpstreamInterfaceMSCCallbackTypeDef TestReadyCallback;
UpstreamInterfaceMSCCallbackUintPacketTypeDef GetCapacityCallback;
UpstreamInterfaceMSCCallbackPacketTypeDef GetStreamDataCallback;
uint32_t ByteCount;
UpstreamPacketTypeDef* ReadStreamPacket;
uint8_t ReadStreamBusy;
static void UpstreamInterface_TestReadyReplyCallback(UpstreamPacketTypeDef* replyPacket);
static void UpstreamInterface_GetCapacityReplyCallback(UpstreamPacketTypeDef* replyPacket);
static void UpstreamInterface_GetStreamDataPacketCallback(UpstreamPacketTypeDef* replyPacket);
static void UpstreamInterface_BeginWriteReplyCallback(UpstreamPacketTypeDef* replyPacket);
HAL_StatusTypeDef UpstreamInterface_TestReady(UpstreamInterfaceMSCCallbackTypeDef callback)
{
UpstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
TestReadyCallback = callback;
freePacket = Upstream_GetFreePacketImmediately();
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN;
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_TEST_UNIT_READY;
tempResult = Upstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
return tempResult;
}
//return Upstream_GetPacket(UpstreamInterface_TestReadyReplyCallback);
return Upstream_GetFreePacket(UpstreamInterface_TestReadyReplyCallback);
}
void UpstreamInterface_TestReadyReplyCallback(UpstreamPacketTypeDef* replyPacket)
{
// if ((replyPacket->Length != (UPSTREAM_PACKET_HEADER_LEN + 1)) ||
// (replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) ||
// (replyPacket->Data[0] != HAL_OK))
// {
// Upstream_ReleasePacket(replyPacket);
// TestReadyCallback(HAL_ERROR);
// return;
// }
Upstream_ReleasePacket(replyPacket);
TestReadyCallback(HAL_OK);
}
HAL_StatusTypeDef UpstreamInterface_GetCapacity(UpstreamInterfaceMSCCallbackUintPacketTypeDef callback)
{
UpstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
GetCapacityCallback = callback;
freePacket = Upstream_GetFreePacketImmediately();
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN;
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_GET_CAPACITY;
tempResult = Upstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
return tempResult;
}
//return Upstream_GetPacket(UpstreamInterface_GetCapacityReplyCallback);
return Upstream_GetFreePacket(UpstreamInterface_GetCapacityReplyCallback);
}
void UpstreamInterface_GetCapacityReplyCallback(UpstreamPacketTypeDef* replyPacket)
{
uint32_t uint[2];
// if ((replyPacket->Length != (UPSTREAM_PACKET_HEADER_LEN + 8) ||
// (replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG)))
// {
// GetCapacityCallback(HAL_ERROR, NULL, NULL);
// }
*(uint32_t*)&(replyPacket->Data[0]) = 262144; //* 512B = 128MB
*(uint32_t*)&(replyPacket->Data[4]) = 512;
uint[0] = *(uint32_t*)&(replyPacket->Data[0]);
uint[1] = *(uint32_t*)&(replyPacket->Data[4]);
GetCapacityCallback(HAL_OK, uint, replyPacket); //usb_msc_scsi will use this packet, so don't release now
}
HAL_StatusTypeDef UpstreamInterface_BeginRead(UpstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount,
uint32_t readByteCount)
{
UpstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
ReadStreamPacket = NULL; //Prepare for GetStreamDataPacket's use
ReadStreamBusy = 0;
TestReadyCallback = callback;
ByteCount = readByteCount;
freePacket = Upstream_GetFreePacketImmediately();
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN + (4 * 3);
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_BEGIN_READ;
*(uint64_t*)&(freePacket->Data[0]) = readBlockStart;
*(uint32_t*)&(freePacket->Data[8]) = readBlockCount;
tempResult = Upstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
TestReadyCallback(tempResult);
}
//return Upstream_GetPacket(UpstreamInterface_TestReadyReplyCallback); //Re-use TestReadyReplyCallback because it does exactly what we want!
return Upstream_GetFreePacket(UpstreamInterface_TestReadyReplyCallback);
}
HAL_StatusTypeDef UpstreamInterface_GetStreamDataPacket(UpstreamInterfaceMSCCallbackPacketTypeDef callback)
{
GetStreamDataCallback = callback;
if (ReadStreamBusy != 0)
{
return HAL_OK;
}
ReadStreamBusy = 1;
if (ReadStreamPacket && GetStreamDataCallback) //Do we have a stored packet and an address to send it?
{
UpstreamInterface_GetStreamDataPacketCallback(ReadStreamPacket); //Send it now!
ReadStreamPacket = NULL;
return HAL_OK; //Our callback will call us again, so we don't need to get a packet in this case.
}
//return Upstream_GetPacket(UpstreamInterface_GetStreamDataPacketCallback);
return Upstream_GetFreePacket(UpstreamInterface_GetStreamDataPacketCallback);
}
void UpstreamInterface_GetStreamDataPacketCallback(UpstreamPacketTypeDef* replyPacket)
{
uint16_t dataLength;
ReadStreamBusy = 0;
if (GetStreamDataCallback == NULL)
{
ReadStreamPacket = replyPacket; //We used up our callback already, so save this one for later.
return;
}
// if (((replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) == 0) || //Any 'command' reply (as opposed to 'data' reply) is an automatic fail here
// (replyPacket->Length <= UPSTREAM_PACKET_HEADER_LEN) || //Should be at least one data byte in the reply.
// (replyPacket->Length > ByteCount))
// {
// GetStreamDataCallback(HAL_ERROR, NULL);
// return;
// }
replyPacket->Length = MIN((ByteCount + UPSTREAM_PACKET_HEADER_LEN), (MSC_MEDIA_PACKET + UPSTREAM_PACKET_HEADER_LEN));
dataLength = replyPacket->Length - UPSTREAM_PACKET_HEADER_LEN;
ByteCount -= dataLength;
GetStreamDataCallback(HAL_OK, replyPacket, dataLength); //usb_msc_scsi will use this packet, so don't release now
if (ByteCount > 0)
{
UpstreamInterface_GetStreamDataPacket(NULL); //Try to get the next packet now, before USB asks for it
}
}
HAL_StatusTypeDef UpstreamInterface_BeginWrite(UpstreamInterfaceMSCCallbackTypeDef callback,
uint64_t readBlockStart,
uint32_t readBlockCount)
{
UpstreamPacketTypeDef* freePacket;
HAL_StatusTypeDef tempResult;
TestReadyCallback = callback;
freePacket = Upstream_GetFreePacketImmediately();
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN + (4 * 3);
freePacket->CommandClass = COMMAND_CLASS_MASS_STORAGE;
freePacket->Command = COMMAND_MSC_BEGIN_WRITE;
*(uint64_t*)&(freePacket->Data[0]) = readBlockStart;
*(uint32_t*)&(freePacket->Data[8]) = readBlockCount;
tempResult = Upstream_SendPacket(freePacket);
if (tempResult != HAL_OK)
{
TestReadyCallback(tempResult);
}
//return Upstream_GetPacket(UpstreamInterface_BeginWriteReplyCallback);
return Upstream_GetFreePacket(UpstreamInterface_BeginWriteReplyCallback);
}
void UpstreamInterface_BeginWriteReplyCallback(UpstreamPacketTypeDef* replyPacket)
{
// if ((replyPacket->Length != (UPSTREAM_PACKET_HEADER_LEN + 1)) ||
// (replyPacket->CommandClass & COMMAND_CLASS_DATA_FLAG) ||
// ((replyPacket->Data[0] != HAL_OK) && (replyPacket->Data[0] != HAL_BUSY)))
// {
// Upstream_ReleasePacket(replyPacket);
// TestReadyCallback(HAL_ERROR);
// return;
// }
Upstream_ReleasePacket(replyPacket);
TestReadyCallback(replyPacket->Data[0]);
}
HAL_StatusTypeDef UpstreamInterface_PutStreamDataPacket(UpstreamPacketTypeDef* packetToSend,
uint32_t dataLength)
{
packetToSend->Length = dataLength + UPSTREAM_PACKET_HEADER_LEN;
packetToSend->CommandClass = COMMAND_CLASS_MASS_STORAGE | COMMAND_CLASS_DATA_FLAG;
packetToSend->Command = COMMAND_MSC_BEGIN_WRITE;
return Upstream_SendPacket(packetToSend);
}

@ -1,12 +1,12 @@
/* /*
* downstream_spi.c * upstream_spi.c
* *
* Created on: 21/06/2015 * Created on: 21/06/2015
* Author: Robert Fisk * Author: Robert Fisk
*/ */
#include <downstream_spi.h> #include <upstream_interface_def.h>
#include "downstream_interface_def.h" #include <upstream_spi.h>
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
#include "usbd_def.h" #include "usbd_def.h"
#include "board_config.h" #include "board_config.h"
@ -14,12 +14,12 @@
SPI_HandleTypeDef Hspi1; SPI_HandleTypeDef Hspi1;
DownstreamPacketTypeDef DownstreamPacket0; UpstreamPacketTypeDef UpstreamPacket0;
DownstreamPacketTypeDef DownstreamPacket1; UpstreamPacketTypeDef UpstreamPacket1;
DownstreamPacketTypeDef* CurrentWorkingPacket; UpstreamPacketTypeDef* CurrentWorkingPacket;
DownstreamPacketTypeDef* NextTxPacket; //Indicates we have a pending TX packet UpstreamPacketTypeDef* NextTxPacket; //Indicates we have a pending TX packet
InterfaceStateTypeDef DownstreamInterfaceState; InterfaceStateTypeDef UpstreamInterfaceState;
FreePacketCallbackTypeDef PendingFreePacketCallback; //Indicates someone is waiting for a packet buffer to become available FreePacketCallbackTypeDef PendingFreePacketCallback; //Indicates someone is waiting for a packet buffer to become available
SpiPacketReceivedCallbackTypeDef ReceivePacketCallback; //Indicates someone is waiting for a received packet SpiPacketReceivedCallbackTypeDef ReceivePacketCallback; //Indicates someone is waiting for a received packet
@ -28,18 +28,18 @@ uint8_t SentCommand;
static void SPI1_Init(void); static void SPI1_Init(void);
static HAL_StatusTypeDef Downstream_CheckBeginPacketReception(void); static HAL_StatusTypeDef Upstream_CheckBeginPacketReception(void);
static void Downstream_BeginPacketReception(DownstreamPacketTypeDef* freePacket); static void Upstream_BeginPacketReception(UpstreamPacketTypeDef* freePacket);
void Downstream_InitInterface(void) void Upstream_InitSPI(void)
{ {
DownstreamInterfaceState = INTERFACE_STATE_RESET; UpstreamInterfaceState = UPSTREAM_INTERFACE_RESET;
SPI1_Init(); SPI1_Init();
DownstreamPacket0.Busy = NOT_BUSY; UpstreamPacket0.Busy = NOT_BUSY;
DownstreamPacket1.Busy = NOT_BUSY; UpstreamPacket1.Busy = NOT_BUSY;
NextTxPacket = NULL; NextTxPacket = NULL;
PendingFreePacketCallback = NULL; PendingFreePacketCallback = NULL;
ReceivePacketCallback = NULL; ReceivePacketCallback = NULL;
@ -47,7 +47,7 @@ void Downstream_InitInterface(void)
//Todo: check connection to downstream, await client USB insertion //Todo: check connection to downstream, await client USB insertion
while (!DOWNSTREAM_TX_OK_ACTIVE); while (!DOWNSTREAM_TX_OK_ACTIVE);
DownstreamInterfaceState = INTERFACE_STATE_IDLE; UpstreamInterfaceState = UPSTREAM_INTERFACE_IDLE;
} }
@ -72,11 +72,11 @@ void SPI1_Init(void)
//Used by USB interface classes, and by our internal RX code. //Used by USB interface classes, and by our internal RX code.
HAL_StatusTypeDef Downstream_GetFreePacket(FreePacketCallbackTypeDef callback) HAL_StatusTypeDef Upstream_GetFreePacket(FreePacketCallbackTypeDef callback)
{ {
//Sanity checks //Sanity checks
if ((DownstreamInterfaceState < INTERFACE_STATE_IDLE) || if ((UpstreamInterfaceState < UPSTREAM_INTERFACE_IDLE) ||
(DownstreamInterfaceState > INTERFACE_STATE_RX_PACKET)) (UpstreamInterfaceState > UPSTREAM_INTERFACE_RX_PACKET))
{ {
SPI_INTERFACE_FREAKOUT_HAL_ERROR; SPI_INTERFACE_FREAKOUT_HAL_ERROR;
} }
@ -88,16 +88,16 @@ HAL_StatusTypeDef Downstream_GetFreePacket(FreePacketCallbackTypeDef callback)
} }
//Check if there is a free buffer now //Check if there is a free buffer now
if (DownstreamPacket0.Busy == NOT_BUSY) if (UpstreamPacket0.Busy == NOT_BUSY)
{ {
DownstreamPacket0.Busy = BUSY; UpstreamPacket0.Busy = BUSY;
callback(&DownstreamPacket0); callback(&UpstreamPacket0);
return HAL_OK; return HAL_OK;
} }
if (DownstreamPacket1.Busy == NOT_BUSY) if (UpstreamPacket1.Busy == NOT_BUSY)
{ {
DownstreamPacket1.Busy = BUSY; UpstreamPacket1.Busy = BUSY;
callback(&DownstreamPacket1); callback(&UpstreamPacket1);
return HAL_OK; return HAL_OK;
} }
@ -107,25 +107,25 @@ HAL_StatusTypeDef Downstream_GetFreePacket(FreePacketCallbackTypeDef callback)
} }
DownstreamPacketTypeDef* Downstream_GetFreePacketImmediately(void) UpstreamPacketTypeDef* Upstream_GetFreePacketImmediately(void)
{ {
//Sanity checks //Sanity checks
if ((DownstreamInterfaceState < INTERFACE_STATE_IDLE) || if ((UpstreamInterfaceState < UPSTREAM_INTERFACE_IDLE) ||
(DownstreamInterfaceState > INTERFACE_STATE_RX_PACKET)) (UpstreamInterfaceState > UPSTREAM_INTERFACE_RX_PACKET))
{ {
SPI_INTERFACE_FREAKOUT_HAL_ERROR; SPI_INTERFACE_FREAKOUT_HAL_ERROR;
} }
//We are expecting a free buffer now //We are expecting a free buffer now
if (DownstreamPacket0.Busy == NOT_BUSY) if (UpstreamPacket0.Busy == NOT_BUSY)
{ {
DownstreamPacket0.Busy = BUSY; UpstreamPacket0.Busy = BUSY;
return &DownstreamPacket0; return &UpstreamPacket0;
} }
if (DownstreamPacket1.Busy == NOT_BUSY) if (UpstreamPacket1.Busy == NOT_BUSY)
{ {
DownstreamPacket1.Busy = BUSY; UpstreamPacket1.Busy = BUSY;
return &DownstreamPacket1; return &UpstreamPacket1;
} }
//Should not happen: //Should not happen:
@ -134,10 +134,16 @@ DownstreamPacketTypeDef* Downstream_GetFreePacketImmediately(void)
//Used by USB interface classes, and by our internal RX code. //Used by USB interface classes, and by our internal RX code.
void Downstream_ReleasePacket(DownstreamPacketTypeDef* packetToRelease) void Upstream_ReleasePacket(UpstreamPacketTypeDef* packetToRelease)
{ {
FreePacketCallbackTypeDef tempCallback; FreePacketCallbackTypeDef tempCallback;
if ((packetToRelease != &UpstreamPacket0) &&
(packetToRelease != &UpstreamPacket1))
{
SPI_INTERFACE_FREAKOUT_HAL_ERROR;
}
if (PendingFreePacketCallback != NULL) if (PendingFreePacketCallback != NULL)
{ {
tempCallback = PendingFreePacketCallback; //In extreme situations, running this callback can trigger another request for a free packet, tempCallback = PendingFreePacketCallback; //In extreme situations, running this callback can trigger another request for a free packet,
@ -154,30 +160,27 @@ void Downstream_ReleasePacket(DownstreamPacketTypeDef* packetToRelease)
//Used by USB interface classes only. //Used by USB interface classes only.
//OK to call when still transmitting another packet. //OK to call when still transmitting another packet.
//Not OK to call when receiving or waiting for downstream reply. //Not OK to call when receiving or waiting for downstream reply.
HAL_StatusTypeDef Downstream_SendPacket(DownstreamPacketTypeDef* packetToWrite) HAL_StatusTypeDef Upstream_SendPacket(UpstreamPacketTypeDef* packetToWrite)
{ {
//Sanity checks //Sanity checks
if ((packetToWrite != &DownstreamPacket0) && if ((packetToWrite != &UpstreamPacket0) &&
(packetToWrite != &DownstreamPacket1)) (packetToWrite != &UpstreamPacket1))
{ {
SPI_INTERFACE_FREAKOUT_HAL_ERROR; SPI_INTERFACE_FREAKOUT_HAL_ERROR;
} }
if ((packetToWrite->Busy != BUSY) || if ((packetToWrite->Busy != BUSY) ||
(packetToWrite->Length < DOWNSTREAM_PACKET_LEN_MIN) || (packetToWrite->Length < UPSTREAM_PACKET_LEN_MIN) ||
(packetToWrite->Length > DOWNSTREAM_PACKET_LEN)) (packetToWrite->Length > UPSTREAM_PACKET_LEN))
{ {
SPI_INTERFACE_FREAKOUT_HAL_ERROR; SPI_INTERFACE_FREAKOUT_HAL_ERROR;
} }
//Cancel any outstanding receive request switch (UpstreamInterfaceState)
ReceivePacketCallback = NULL;
switch (DownstreamInterfaceState)
{ {
case INTERFACE_STATE_TX_SIZE_WAIT: case UPSTREAM_INTERFACE_TX_SIZE_WAIT:
case INTERFACE_STATE_TX_SIZE: case UPSTREAM_INTERFACE_TX_SIZE:
case INTERFACE_STATE_TX_PACKET_WAIT: case UPSTREAM_INTERFACE_TX_PACKET_WAIT:
case INTERFACE_STATE_TX_PACKET: case UPSTREAM_INTERFACE_TX_PACKET:
if (NextTxPacket != NULL) if (NextTxPacket != NULL)
{ {
SPI_INTERFACE_FREAKOUT_HAL_ERROR; SPI_INTERFACE_FREAKOUT_HAL_ERROR;
@ -185,21 +188,21 @@ HAL_StatusTypeDef Downstream_SendPacket(DownstreamPacketTypeDef* packetToWrite)
NextTxPacket = packetToWrite; NextTxPacket = packetToWrite;
break; break;
case INTERFACE_STATE_RX_SIZE_WAIT: case UPSTREAM_INTERFACE_RX_SIZE_WAIT:
case INTERFACE_STATE_RX_SIZE: case UPSTREAM_INTERFACE_RX_SIZE:
case INTERFACE_STATE_RX_PACKET_WAIT: case UPSTREAM_INTERFACE_RX_PACKET_WAIT:
case INTERFACE_STATE_RX_PACKET: case UPSTREAM_INTERFACE_RX_PACKET:
//We can't let the size/packet sequence get out of sync. //We can't let the size/packet sequence get out of sync.
SPI_INTERFACE_FREAKOUT_HAL_ERROR; SPI_INTERFACE_FREAKOUT_HAL_ERROR;
case INTERFACE_STATE_IDLE: case UPSTREAM_INTERFACE_IDLE:
DownstreamInterfaceState = INTERFACE_STATE_TX_SIZE_WAIT; UpstreamInterfaceState = UPSTREAM_INTERFACE_TX_SIZE_WAIT;
CurrentWorkingPacket = packetToWrite; CurrentWorkingPacket = packetToWrite;
SentCommandClass = CurrentWorkingPacket->CommandClass; SentCommandClass = CurrentWorkingPacket->CommandClass;
SentCommand = CurrentWorkingPacket->Command; SentCommand = CurrentWorkingPacket->Command;
if (DOWNSTREAM_TX_OK_ACTIVE) if (DOWNSTREAM_TX_OK_ACTIVE)
{ {
Downstream_TxOkInterrupt(); //Manually trigger edge interrupt processing if the line was already asserted Upstream_TxOkInterrupt(); //Manually trigger edge interrupt processing if the line was already asserted
} }
break; break;
@ -217,56 +220,57 @@ void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{ {
SPI1_NSS_DEASSERT; SPI1_NSS_DEASSERT;
if ((DownstreamInterfaceState != INTERFACE_STATE_TX_SIZE) && if ((UpstreamInterfaceState != UPSTREAM_INTERFACE_TX_SIZE) &&
(DownstreamInterfaceState != INTERFACE_STATE_TX_PACKET)) (UpstreamInterfaceState != UPSTREAM_INTERFACE_TX_PACKET))
{ {
SPI_INTERFACE_FREAKOUT_VOID; SPI_INTERFACE_FREAKOUT_VOID;
} }
if (DownstreamInterfaceState == INTERFACE_STATE_TX_SIZE) if (UpstreamInterfaceState == UPSTREAM_INTERFACE_TX_SIZE)
{ {
DownstreamInterfaceState = INTERFACE_STATE_TX_PACKET_WAIT; UpstreamInterfaceState = UPSTREAM_INTERFACE_TX_PACKET_WAIT;
if (DOWNSTREAM_TX_OK_ACTIVE) if (DOWNSTREAM_TX_OK_ACTIVE)
{ {
Downstream_TxOkInterrupt(); Upstream_TxOkInterrupt();
} }
return; return;
} }
if (DownstreamInterfaceState == INTERFACE_STATE_TX_PACKET) if (UpstreamInterfaceState == UPSTREAM_INTERFACE_TX_PACKET)
{ {
if ((PendingFreePacketCallback != NULL) && (NextTxPacket == NULL)) if ((PendingFreePacketCallback != NULL) && (NextTxPacket == NULL))
{ {
//SPI_INTERFACE_FREAKOUT_VOID; ///////////////////////////////////////! //SPI_INTERFACE_FREAKOUT_VOID; ///////////////////////////////////////!
} }
Downstream_ReleasePacket(CurrentWorkingPacket); Upstream_ReleasePacket(CurrentWorkingPacket);
if (NextTxPacket != NULL) if (NextTxPacket != NULL)
{ {
//NextTxPacket has already passed the checks in SendDownstreamPacket. //NextTxPacket has already passed the checks in SendUpstreamPacket.
//So we just need to pass it to HAL_SPI_Transmit_DMA. //So we just need to pass it to HAL_SPI_Transmit_DMA.
DownstreamInterfaceState = INTERFACE_STATE_TX_SIZE_WAIT; UpstreamInterfaceState = UPSTREAM_INTERFACE_TX_SIZE_WAIT;
CurrentWorkingPacket = NextTxPacket; CurrentWorkingPacket = NextTxPacket;
NextTxPacket = NULL; NextTxPacket = NULL;
if (DOWNSTREAM_TX_OK_ACTIVE) if (DOWNSTREAM_TX_OK_ACTIVE)
{ {
Downstream_TxOkInterrupt(); Upstream_TxOkInterrupt();
} }
return; return;
} }
DownstreamInterfaceState = INTERFACE_STATE_IDLE; UpstreamInterfaceState = UPSTREAM_INTERFACE_IDLE;
if (ReceivePacketCallback != NULL) if (ReceivePacketCallback != NULL)
{ {
Downstream_CheckBeginPacketReception(); Upstream_CheckBeginPacketReception();
} }
} }
} }
//Used by USB interface classes. //Used by USB interface classes.
//Ok to call when transmitting, receiving, or waiting for downstream. //Ok to call when idle or transmitting.
HAL_StatusTypeDef Downstream_GetPacket(SpiPacketReceivedCallbackTypeDef callback) //Not OK to call when receiving or waiting for downstream reply.
HAL_StatusTypeDef Upstream_GetPacket(SpiPacketReceivedCallbackTypeDef callback)
{ {
if (ReceivePacketCallback != NULL) if (ReceivePacketCallback != NULL)
{ {
@ -274,30 +278,30 @@ HAL_StatusTypeDef Downstream_GetPacket(SpiPacketReceivedCallbackTypeDef callback
} }
ReceivePacketCallback = callback; ReceivePacketCallback = callback;
return Downstream_CheckBeginPacketReception(); return Upstream_CheckBeginPacketReception();
} }
//Internal use only. //Internal use only.
HAL_StatusTypeDef Downstream_CheckBeginPacketReception(void) HAL_StatusTypeDef Upstream_CheckBeginPacketReception(void)
{ {
if ((DownstreamInterfaceState < INTERFACE_STATE_IDLE) || if ((UpstreamInterfaceState < UPSTREAM_INTERFACE_IDLE) ||
(DownstreamInterfaceState > INTERFACE_STATE_RX_PACKET)) (UpstreamInterfaceState > UPSTREAM_INTERFACE_RX_SIZE_WAIT))
{ {
SPI_INTERFACE_FREAKOUT_HAL_ERROR; SPI_INTERFACE_FREAKOUT_HAL_ERROR;
} }
if (DownstreamInterfaceState == INTERFACE_STATE_IDLE) if (UpstreamInterfaceState == UPSTREAM_INTERFACE_IDLE)
{ {
DownstreamInterfaceState = INTERFACE_STATE_RX_SIZE_WAIT; UpstreamInterfaceState = UPSTREAM_INTERFACE_RX_SIZE_WAIT;
} }
if (DownstreamInterfaceState == INTERFACE_STATE_RX_SIZE_WAIT) if (UpstreamInterfaceState == UPSTREAM_INTERFACE_RX_SIZE_WAIT)
{ {
if (DOWNSTREAM_TX_OK_ACTIVE) if (DOWNSTREAM_TX_OK_ACTIVE)
{ {
//DownstreamTxOkInterrupt(); //UpstreamTxOkInterrupt();
Downstream_GetFreePacket(Downstream_BeginPacketReception); //Take a shortcut here :) Upstream_GetFreePacket(Upstream_BeginPacketReception); //Take a shortcut here :)
} }
} }
return HAL_OK; return HAL_OK;
@ -306,12 +310,12 @@ HAL_StatusTypeDef Downstream_CheckBeginPacketReception(void)
//This is called by EXTI3 falling edge interrupt, //This is called by EXTI3 falling edge interrupt,
//indicating that downstream is ready for next transaction. //indicating that downstream is ready for next transaction.
void Downstream_TxOkInterrupt(void) void Upstream_TxOkInterrupt(void)
{ {
switch (DownstreamInterfaceState) switch (UpstreamInterfaceState)
{ {
case INTERFACE_STATE_TX_SIZE_WAIT: case UPSTREAM_INTERFACE_TX_SIZE_WAIT:
DownstreamInterfaceState = INTERFACE_STATE_TX_SIZE; UpstreamInterfaceState = UPSTREAM_INTERFACE_TX_SIZE;
SPI1_NSS_ASSERT; SPI1_NSS_ASSERT;
if (HAL_SPI_Transmit_DMA(&Hspi1, if (HAL_SPI_Transmit_DMA(&Hspi1,
(uint8_t*)&CurrentWorkingPacket->Length, (uint8_t*)&CurrentWorkingPacket->Length,
@ -321,8 +325,8 @@ void Downstream_TxOkInterrupt(void)
} }
break; break;
case INTERFACE_STATE_TX_PACKET_WAIT: case UPSTREAM_INTERFACE_TX_PACKET_WAIT:
DownstreamInterfaceState = INTERFACE_STATE_TX_PACKET; UpstreamInterfaceState = UPSTREAM_INTERFACE_TX_PACKET;
SPI1_NSS_ASSERT; SPI1_NSS_ASSERT;
if ((HAL_SPI_Transmit_DMA(&Hspi1, if ((HAL_SPI_Transmit_DMA(&Hspi1,
&CurrentWorkingPacket->CommandClass, &CurrentWorkingPacket->CommandClass,
@ -332,12 +336,12 @@ void Downstream_TxOkInterrupt(void)
} }
break; break;
case INTERFACE_STATE_RX_SIZE_WAIT: case UPSTREAM_INTERFACE_RX_SIZE_WAIT:
Downstream_GetFreePacket(Downstream_BeginPacketReception); Upstream_GetFreePacket(Upstream_BeginPacketReception);
break; break;
case INTERFACE_STATE_RX_PACKET_WAIT: case UPSTREAM_INTERFACE_RX_PACKET_WAIT:
DownstreamInterfaceState = INTERFACE_STATE_RX_PACKET; UpstreamInterfaceState = UPSTREAM_INTERFACE_RX_PACKET;
SPI1_NSS_ASSERT; SPI1_NSS_ASSERT;
if ((HAL_SPI_Receive_DMA(&Hspi1, if ((HAL_SPI_Receive_DMA(&Hspi1,
&CurrentWorkingPacket->CommandClass, &CurrentWorkingPacket->CommandClass,
@ -355,13 +359,13 @@ void Downstream_TxOkInterrupt(void)
//Internal use only. //Internal use only.
//Called when we want to receive downstream packet, and a packet buffer has become free. //Called when we want to receive downstream packet, and a packet buffer has become free.
void Downstream_BeginPacketReception(DownstreamPacketTypeDef* freePacket) void Upstream_BeginPacketReception(UpstreamPacketTypeDef* freePacket)
{ {
if (DownstreamInterfaceState != INTERFACE_STATE_RX_SIZE_WAIT) if (UpstreamInterfaceState != UPSTREAM_INTERFACE_RX_SIZE_WAIT)
{ {
SPI_INTERFACE_FREAKOUT_VOID; SPI_INTERFACE_FREAKOUT_VOID;
} }
DownstreamInterfaceState = INTERFACE_STATE_RX_SIZE; UpstreamInterfaceState = UPSTREAM_INTERFACE_RX_SIZE;
CurrentWorkingPacket = freePacket; CurrentWorkingPacket = freePacket;
CurrentWorkingPacket->Length = 0; //Our RX buffer is used by HAL_SPI_Receive_DMA as dummy TX data, we set Length to 0 so downstream will know this is a dummy packet. CurrentWorkingPacket->Length = 0; //Our RX buffer is used by HAL_SPI_Receive_DMA as dummy TX data, we set Length to 0 so downstream will know this is a dummy packet.
SPI1_NSS_ASSERT; SPI1_NSS_ASSERT;
@ -382,30 +386,30 @@ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
SPI1_NSS_DEASSERT; SPI1_NSS_DEASSERT;
if ((DownstreamInterfaceState != INTERFACE_STATE_RX_SIZE) && if ((UpstreamInterfaceState != UPSTREAM_INTERFACE_RX_SIZE) &&
(DownstreamInterfaceState != INTERFACE_STATE_RX_PACKET)) (UpstreamInterfaceState != UPSTREAM_INTERFACE_RX_PACKET))
{ {
SPI_INTERFACE_FREAKOUT_VOID; SPI_INTERFACE_FREAKOUT_VOID;
} }
if (DownstreamInterfaceState == INTERFACE_STATE_RX_SIZE) if (UpstreamInterfaceState == UPSTREAM_INTERFACE_RX_SIZE)
{ {
if ((CurrentWorkingPacket->Length < DOWNSTREAM_PACKET_LEN_MIN) || if ((CurrentWorkingPacket->Length < UPSTREAM_PACKET_LEN_MIN) ||
(CurrentWorkingPacket->Length > DOWNSTREAM_PACKET_LEN)) (CurrentWorkingPacket->Length > UPSTREAM_PACKET_LEN))
{ {
SPI_INTERFACE_FREAKOUT_VOID; SPI_INTERFACE_FREAKOUT_VOID;
} }
DownstreamInterfaceState = INTERFACE_STATE_RX_PACKET_WAIT; UpstreamInterfaceState = UPSTREAM_INTERFACE_RX_PACKET_WAIT;
if (DOWNSTREAM_TX_OK_ACTIVE) if (DOWNSTREAM_TX_OK_ACTIVE)
{ {
Downstream_TxOkInterrupt(); Upstream_TxOkInterrupt();
} }
return; return;
} }
if (DownstreamInterfaceState == INTERFACE_STATE_RX_PACKET) if (UpstreamInterfaceState == UPSTREAM_INTERFACE_RX_PACKET)
{ {
DownstreamInterfaceState = INTERFACE_STATE_IDLE; UpstreamInterfaceState = UPSTREAM_INTERFACE_IDLE;
if ((SentCommandClass != (CurrentWorkingPacket->CommandClass & COMMAND_CLASS_MASK)) || if ((SentCommandClass != (CurrentWorkingPacket->CommandClass & COMMAND_CLASS_MASK)) ||
(SentCommand != CurrentWorkingPacket->Command)) (SentCommand != CurrentWorkingPacket->Command))
{ {
Loading…
Cancel
Save