- Implement Upstream state machine, with Downstream-dependent device emulation - Add fault LED flashing - Improve fault handling and freakouts - Misc bug fixespull/7/head
parent
fc522bae0a
commit
f24714cd8c
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* led.h
|
||||
*
|
||||
* Created on: 19/08/2015
|
||||
* Author: Robert Fisk
|
||||
*/
|
||||
|
||||
#ifndef INC_LED_H_
|
||||
#define INC_LED_H_
|
||||
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
|
||||
void LED_Init(void);
|
||||
void LED_Fault_SetBlinkRate(uint16_t newBlinkRate);
|
||||
void LED_DoBlinks(void);
|
||||
|
||||
|
||||
|
||||
#define STARTUP_FLASH_DELAY 500 //units = ticks = ms
|
||||
|
||||
//LEDs are on for BLINK_RATE ticks, then off for BLINK_RATE ticks
|
||||
#define LED_FAST_BLINK_RATE 100
|
||||
#define LED_SLOW_BLINK_RATE 500
|
||||
|
||||
|
||||
|
||||
#endif /* INC_LED_H_ */
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* upstream_statemachine.h
|
||||
*
|
||||
* Created on: 20/08/2015
|
||||
* Author: Robert Fisk
|
||||
*/
|
||||
|
||||
#ifndef INC_UPSTREAM_STATEMACHINE_H_
|
||||
#define INC_UPSTREAM_STATEMACHINE_H_
|
||||
|
||||
|
||||
#include "led.h"
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
STATE_TEST_INTERFACE,
|
||||
STATE_WAIT_DEVICE,
|
||||
STATE_DEVICE_ACTIVE,
|
||||
STATE_ERROR
|
||||
} UpstreamStateTypeDef;
|
||||
|
||||
|
||||
|
||||
#define UPSTREAM_STATEMACHINE_FREAKOUT \
|
||||
do { \
|
||||
LED_Fault_SetBlinkRate(LED_FAST_BLINK_RATE); \
|
||||
Upstream_StateMachine_SetErrorState(); \
|
||||
} while (0);
|
||||
|
||||
|
||||
|
||||
void Upstream_InitStateMachine(void);
|
||||
void Upstream_StateMachine_SetErrorState(void);
|
||||
HAL_StatusTypeDef Upstream_StateMachine_CheckClassOperationOk(void);
|
||||
|
||||
|
||||
|
||||
#endif /* INC_UPSTREAM_STATEMACHINE_H_ */
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* led.c
|
||||
*
|
||||
* Created on: 19/08/2015
|
||||
* Author: Robert Fisk
|
||||
*/
|
||||
|
||||
|
||||
#include "led.h"
|
||||
#include "board_config.h"
|
||||
|
||||
|
||||
|
||||
uint16_t FaultLedBlinkRate = 0;
|
||||
uint16_t FaultLedBlinkCounter = 0;
|
||||
uint8_t FaultLedState = 0;
|
||||
|
||||
|
||||
void LED_Init(void)
|
||||
{
|
||||
//RUN_LED_ON;
|
||||
FAULT_LED_ON;
|
||||
HAL_Delay(STARTUP_FLASH_DELAY);
|
||||
//RUN_LED_OFF;
|
||||
FAULT_LED_OFF;
|
||||
}
|
||||
|
||||
|
||||
void LED_Fault_SetBlinkRate(uint16_t newBlinkRate)
|
||||
{
|
||||
FaultLedBlinkRate = newBlinkRate;
|
||||
if (newBlinkRate == 0)
|
||||
{
|
||||
FaultLedState = 0;
|
||||
FAULT_LED_OFF;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void LED_DoBlinks(void)
|
||||
{
|
||||
if (FaultLedBlinkRate > 0)
|
||||
{
|
||||
FaultLedBlinkCounter++;
|
||||
if (FaultLedBlinkCounter >= FaultLedBlinkRate)
|
||||
{
|
||||
FaultLedBlinkCounter = 0;
|
||||
if (FaultLedState)
|
||||
{
|
||||
FaultLedState = 0;
|
||||
FAULT_LED_OFF;
|
||||
}
|
||||
else
|
||||
{
|
||||
FaultLedState = 1;
|
||||
FAULT_LED_ON;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,176 @@
|
||||
/*
|
||||
* upstream_statemachine.c
|
||||
*
|
||||
* Created on: 20/08/2015
|
||||
* Author: Robert Fisk
|
||||
*/
|
||||
|
||||
|
||||
#include "upstream_statemachine.h"
|
||||
#include "upstream_spi.h"
|
||||
#include "upstream_interface_def.h"
|
||||
#include "usb_device.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_msc.h"
|
||||
|
||||
|
||||
UpstreamStateTypeDef UpstreamState = STATE_TEST_INTERFACE;
|
||||
InterfaceCommandClassTypeDef ActiveDeviceClass = COMMAND_CLASS_INTERFACE;
|
||||
|
||||
|
||||
void Upstream_TestInterfaceReplyCallback(UpstreamPacketTypeDef* replyPacket);
|
||||
void Upstream_NotifyDeviceReplyCallback(UpstreamPacketTypeDef* replyPacket);
|
||||
|
||||
|
||||
|
||||
void Upstream_InitStateMachine(void)
|
||||
{
|
||||
UpstreamPacketTypeDef* freePacket;
|
||||
uint16_t i;
|
||||
uint8_t testDataValue;
|
||||
|
||||
Upstream_InitSPI();
|
||||
|
||||
//Prepare SPI test packet
|
||||
freePacket = Upstream_GetFreePacketImmediately();
|
||||
if (freePacket == NULL)
|
||||
{
|
||||
UpstreamState = STATE_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
freePacket->Length = UPSTREAM_PACKET_HEADER_LEN + MSC_MEDIA_PACKET;
|
||||
freePacket->CommandClass = COMMAND_CLASS_INTERFACE;
|
||||
freePacket->Command = COMMAND_INTERFACE_ECHO;
|
||||
testDataValue = 0xFF;
|
||||
|
||||
//Fill our test packet with some junk
|
||||
for (i = 0; i < MSC_MEDIA_PACKET; i++)
|
||||
{
|
||||
freePacket->Data[i] = testDataValue;
|
||||
testDataValue += 39;
|
||||
}
|
||||
|
||||
if (Upstream_TransmitPacket(freePacket) == HAL_OK)
|
||||
{
|
||||
Upstream_ReceivePacket(Upstream_TestInterfaceReplyCallback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Used by upstream_spi freakout macro, indicates we should stop everything.
|
||||
void Upstream_StateMachine_SetErrorState(void)
|
||||
{
|
||||
UpstreamState = STATE_ERROR;
|
||||
if ((ActiveDeviceClass > COMMAND_CLASS_INTERFACE) &&
|
||||
(ActiveDeviceClass < COMMAND_CLASS_ERROR))
|
||||
{
|
||||
USBD_Stop(&hUsbDeviceFS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
HAL_StatusTypeDef Upstream_StateMachine_CheckClassOperationOk(void)
|
||||
{
|
||||
if (UpstreamState == STATE_ERROR)
|
||||
{
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
if (UpstreamState != STATE_DEVICE_ACTIVE)
|
||||
{
|
||||
UPSTREAM_STATEMACHINE_FREAKOUT;
|
||||
return HAL_ERROR;
|
||||
}
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
|
||||
void Upstream_TestInterfaceReplyCallback(UpstreamPacketTypeDef* replyPacket)
|
||||
{
|
||||
uint16_t i;
|
||||
uint8_t testDataValue;
|
||||
|
||||
if (UpstreamState >= STATE_ERROR)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((UpstreamState != STATE_TEST_INTERFACE) ||
|
||||
(replyPacket == NULL))
|
||||
{
|
||||
UPSTREAM_STATEMACHINE_FREAKOUT;
|
||||
return;
|
||||
}
|
||||
|
||||
if (replyPacket->Length != (UPSTREAM_PACKET_HEADER_LEN + MSC_MEDIA_PACKET))
|
||||
{
|
||||
UPSTREAM_STATEMACHINE_FREAKOUT;
|
||||
return;
|
||||
}
|
||||
|
||||
testDataValue = 0xFF;
|
||||
for (i = 0; i < MSC_MEDIA_PACKET; i++)
|
||||
{
|
||||
if (replyPacket->Data[i] != testDataValue)
|
||||
{
|
||||
UPSTREAM_STATEMACHINE_FREAKOUT;
|
||||
return;
|
||||
}
|
||||
testDataValue += 39;
|
||||
}
|
||||
|
||||
|
||||
//SPI interface passed checks. Now we wait for a device to be attached to downstream.
|
||||
UpstreamState = STATE_WAIT_DEVICE;
|
||||
replyPacket->Length = UPSTREAM_PACKET_HEADER_LEN;
|
||||
replyPacket->CommandClass = COMMAND_CLASS_INTERFACE;
|
||||
replyPacket->Command = COMMAND_INTERFACE_NOTIFY_DEVICE;
|
||||
if (Upstream_TransmitPacket(replyPacket) == HAL_OK)
|
||||
{
|
||||
Upstream_ReceivePacket(Upstream_NotifyDeviceReplyCallback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Upstream_NotifyDeviceReplyCallback(UpstreamPacketTypeDef* replyPacket)
|
||||
{
|
||||
if (UpstreamState >= STATE_ERROR)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((UpstreamState != STATE_WAIT_DEVICE) ||
|
||||
(replyPacket == NULL))
|
||||
{
|
||||
UPSTREAM_STATEMACHINE_FREAKOUT;
|
||||
return;
|
||||
}
|
||||
|
||||
if (replyPacket->Length != (UPSTREAM_PACKET_HEADER_LEN + 1))
|
||||
{
|
||||
UPSTREAM_STATEMACHINE_FREAKOUT;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (replyPacket->Data[0])
|
||||
{
|
||||
case COMMAND_CLASS_MASS_STORAGE:
|
||||
USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC);
|
||||
break;
|
||||
|
||||
default:
|
||||
UPSTREAM_STATEMACHINE_FREAKOUT;
|
||||
return;
|
||||
}
|
||||
|
||||
USBD_Start(&hUsbDeviceFS);
|
||||
UpstreamState = STATE_DEVICE_ACTIVE;
|
||||
ActiveDeviceClass = replyPacket->Data[0];
|
||||
|
||||
//The USB device stack will now receive commands from our host.
|
||||
//All we need to do is check for downstream device removal.
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue