Commit b93d5cc9 authored by Matthieu Cattin's avatar Matthieu Cattin

usbdmsd: Add USB mass storage device test project and MSD drivers.

parent 009d1cae
/***************************************************************************//**
* @file msdbot.c
* @brief Implements the host side of the Bulk Only Transport protocol for
* USB Mass Storage class Devices.
* @author Energy Micro AS
* @version 3.20.2
*******************************************************************************
* @section License
* <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
*******************************************************************************
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* 4. The source and compiled code may only be used on Energy Micro "EFM32"
* microcontrollers and "EFR4" radios.
*
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
* obligation to support this Software. Energy Micro AS is providing the
* Software "AS IS", with no express or implied warranties of any kind,
* including, but not limited to, any implied warranties of merchantability
* or fitness for any particular purpose or warranties against infringement
* of any proprietary rights of a third party.
*
* Energy Micro AS will not be liable for any consequential, incidental, or
* special damages, or any other relief, or for any claim by any third party,
* arising from your use of this Software.
*
*****************************************************************************/
#include "em_usb.h"
#include "msdbot.h"
/** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
#define TIMEOUT_1SEC 1000
#define TIMEOUT_2SEC 2000
#define DEFAULT_TIMEOUT TIMEOUT_2SEC
/* Bulk endpoint "handles". */
static USBH_Ep_TypeDef *epOut = NULL;
static USBH_Ep_TypeDef *epIn = NULL;
/* Bulk transfer timeout scale factor. */
static int timeoutFactor;
/* CSW buffer. */
STATIC_UBUF(csw, CSW_LEN);
static MSDBOT_CSW_TypeDef *pCsw = (MSDBOT_CSW_TypeDef*) csw;
/* Function prototypes. */
static bool CswMeaningful(MSDBOT_CBW_TypeDef *pCbw);
static bool CswValid(MSDBOT_CBW_TypeDef *pCbw);
static void ResetRecovery(void);
/** @endcond */
/***************************************************************************//**
* @brief
* MSDBOT module initialization.
*
* @param[in] out
* Pointer to an MSD bulk OUT endpoint structure.
*
* @param[in] in
* Pointer to an MSD bulk IN endpoint structure.
*
* @return
* @ref MSDBOT_STATUS_OK on success, else @ref MSDBOT_INIT_ERROR.
******************************************************************************/
int MSDBOT_Init(USBH_Ep_TypeDef *out, USBH_Ep_TypeDef *in)
{
/* Check if typedef's are properly packed. */
if ((sizeof(MSDBOT_CBW_TypeDef) != CBW_LEN) ||
(sizeof(MSDBOT_CSW_TypeDef) != CSW_LEN))
{
DEBUG_USB_API_PUTS("\nMSDBOT_Init(), typedef size error");
EFM_ASSERT(false);
return MSDBOT_INIT_ERROR;
}
/* Keep a local copy of bulk IN/OUT endpoint handles. */
epOut = out;
epIn = in;
/* Transfer timeouts are scaled according to device bus speed. */
if (epIn->parentDevice->speed == PORT_FULL_SPEED)
timeoutFactor = 512;
else
timeoutFactor = 64;
return MSDBOT_STATUS_OK;
}
/***************************************************************************//**
* @brief
* Perform an MSD Bulk Only Transfer (BOT).
*
* @param[in] cbw
* Pointer to a Command Block Wrapper (CBW) data structure.
*
* @param[in] data
* Data buffer for data to be transferred.
*
* @return
* A positive (or zero) value indicating the number of bytes transferred.
* @n A negative value indicates a transfer error code enumerated in
* @ref MSDBOT_Status_TypeDef.
******************************************************************************/
int MSDBOT_Xfer(void* cbw, void* data)
{
uint32_t len;
int timeout, result, direction, retVal;
MSDBOT_CBW_TypeDef *pCbw = (MSDBOT_CBW_TypeDef*) cbw;
/* Send CBW. */
result = USBH_WriteB(epOut, cbw, CBW_LEN, DEFAULT_TIMEOUT);
if (result != CBW_LEN)
{
ResetRecovery();
return MSDBOT_XFER_ERROR;
}
retVal = 0;
direction = pCbw->Direction;
len = pCbw->dCBWDataTransferLength;
/* Send/receive data (optional phase). */
if (len)
{
timeout = DEFAULT_TIMEOUT + (len / timeoutFactor);
if (direction)
result = USBH_ReadB(epIn, data, len, timeout);
else
result = USBH_WriteB(epOut, data, len, timeout);
retVal = result;
if (result == USB_STATUS_EP_STALLED)
{
if (direction)
USBH_UnStallEpB(epIn);
else
USBH_UnStallEpB(epOut);
}
else if (result <= 0)
{
ResetRecovery();
return MSDBOT_XFER_ERROR;
}
}
/* Retrieve CSW. */
result = USBH_ReadB(epIn, csw, CSW_LEN, DEFAULT_TIMEOUT);
if (result != CSW_LEN)
{
if (result == USB_STATUS_EP_STALLED)
{
if (direction)
USBH_UnStallEpB(epIn);
else
USBH_UnStallEpB(epOut);
result = USBH_ReadB(epIn, csw, CSW_LEN, DEFAULT_TIMEOUT);
if (result != CSW_LEN)
{
ResetRecovery();
return MSDBOT_XFER_ERROR;
}
}
else
{
ResetRecovery();
return MSDBOT_XFER_ERROR;
}
}
if (CswValid(pCbw) && CswMeaningful(pCbw))
{
if (pCsw->bCSWStatus == USB_CLASS_MSD_CSW_CMDPASSED)
{
return retVal;
}
return MSDBOT_CMD_FAILED;
}
ResetRecovery();
return MSDBOT_XFER_ERROR;
}
/** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
/***************************************************************************//**
* @brief
* Check if a Command Status Wrapper (CSW) is meaningful.
* The term "meaningful" is defined in the MSD BOT standard.
*
* @param[in] cbw
* Pointer to a Command Block Wrapper (CBW) data structure.
*
* @return
* True if the CSW is meaningful, false otherwise.
******************************************************************************/
static bool CswMeaningful(MSDBOT_CBW_TypeDef *cbw)
{
if (((pCsw->bCSWStatus == USB_CLASS_MSD_CSW_CMDPASSED) ||
(pCsw->bCSWStatus == USB_CLASS_MSD_CSW_CMDFAILED)) &&
(pCsw->dCSWDataResidue <= cbw->dCBWDataTransferLength))
return true;
return false;
}
/***************************************************************************//**
* @brief
* Check if a Command Status Wrapper (CSW) is valid.
* The term "valid" is defined in the MSD BOT standard.
*
* @param[in] cbw
* Pointer to a Command Block Wrapper (CBW) data structure.
*
* @return
* True if the CSW is valid, false otherwise.
******************************************************************************/
static bool CswValid(MSDBOT_CBW_TypeDef *cbw)
{
if ((pCsw->dCSWSignature == CSW_SIGNATURE) &&
(pCsw->dCSWTag == cbw->dCBWTag))
return true;
return false;
}
/***************************************************************************//**
* @brief
* Perform an MSD BOT reset recovery operation.
******************************************************************************/
static void ResetRecovery(void)
{
USBH_ControlMsgB(&epIn->parentDevice->ep0,
USB_SETUP_DIR_H2D | USB_SETUP_RECIPIENT_INTERFACE |
USB_SETUP_TYPE_CLASS_MASK, /* bmRequestType */
USB_MSD_BOTRESET, /* bRequest */
0, /* wValue */
0, /* wIndex */
0, /* wLength */
NULL, /* void* data */
TIMEOUT_1SEC); /* int timeout */
USBH_UnStallEpB(epIn);
USBH_UnStallEpB(epOut);
}
/** @endcond */
/***************************************************************************//**
* @file msdbot.h
* @brief Definitions for the Bulk Only Transport protocol of
* USB Mass Storage devices.
* @author Energy Micro AS
* @version 3.20.2
*******************************************************************************
* @section License
* <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
*******************************************************************************
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* 4. The source and compiled code may only be used on Energy Micro "EFM32"
* microcontrollers and "EFR4" radios.
*
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
* obligation to support this Software. Energy Micro AS is providing the
* Software "AS IS", with no express or implied warranties of any kind,
* including, but not limited to, any implied warranties of merchantability
* or fitness for any particular purpose or warranties against infringement
* of any proprietary rights of a third party.
*
* Energy Micro AS will not be liable for any consequential, incidental, or
* special damages, or any other relief, or for any claim by any third party,
* arising from your use of this Software.
*
*****************************************************************************/
#ifndef __MSDBOT_H
#define __MSDBOT_H
/***************************************************************************//**
* @addtogroup Drivers
* @{
******************************************************************************/
/***************************************************************************//**
* @addtogroup Msd
* @{
******************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
#define CBW_LEN 31
#define CSW_LEN 13
#define BOT_DIR_IN 0x80 /* bmCBWFlags value for bulk IN cmds. */
#define BOT_DIR_OUT 0x00 /* bmCBWFlags value for bulk OUT cmds. */
#define CSW_SIGNATURE 0x53425355 /* Ascii USBS */
#define CBW_SIGNATURE 0x43425355 /* Ascii USBC */
/** @endcond */
/**************************************************************************//**
* @brief Bulk Only Transport (BOT) Command Block Wrapper (CBW) typedef.
*****************************************************************************/
EFM32_PACK_START(1)
typedef struct
{
uint32_t dCBWSignature; /**< The CBW signature (ascii USBC). */
uint32_t dCBWTag; /**< The CBW tag. */
uint32_t dCBWDataTransferLength; /**< BOT transfer length. */
union
{
struct
{
uint8_t Reserved1 : 6; /**< Reserved, expect 0. */
uint8_t Obsolete : 1; /**< Obsolete, expect 0. */
uint8_t Direction : 1; /**< BOT transfer directon, 1=BOT IN */
};
uint8_t bmCBWFlags; /**< CBW Flags. */
};
union
{
struct
{
uint8_t Lun : 4; /**< Device Logical Unit Number. */
uint8_t Reserved2 : 4; /**< Reserved, expect 0. */
};
uint8_t bCBWLUN;
};
union
{
struct
{
uint8_t CBLength : 5; /**< SCSI CDB length. */
uint8_t Reserved3 : 3; /**< Reserved, expect 0. */
};
uint8_t bCBWCBLength; /**< SCSI CDB length. */
};
uint8_t CBWCB[ 16 ]; /**< SCSI CDB. */
} __attribute__ ((packed)) MSDBOT_CBW_TypeDef;
EFM32_PACK_END()
/**************************************************************************//**
* @brief Bulk Only Transport (BOT) Command Status Wrapper (CSW) typedef.
*****************************************************************************/
EFM32_PACK_START(1)
typedef struct
{
uint32_t dCSWSignature; /**< The CSW signature (ascii USBS). */
uint32_t dCSWTag; /**< The CSW tag. */
uint32_t dCSWDataResidue; /**< BOT transfer residue count. */
uint8_t bCSWStatus; /**< BOT command execution result. */
} __attribute__ ((packed)) MSDBOT_CSW_TypeDef;
EFM32_PACK_END()
/** @brief MSDBOT status enumerator. */
typedef enum
{
MSDBOT_STATUS_OK = 0, /**< MSDBOT status, no error. */
MSDBOT_INIT_ERROR = -1, /**< MSDBOT initialization failure. */
MSDBOT_CMD_FAILED = -2, /**< MSDBOT command failure. */
MSDBOT_XFER_ERROR = -3, /**< MSDBOT transfer error. */
} MSDBOT_Status_TypeDef;
/*** MSDBOT Function prototypes ***/
#if defined(USB_HOST)
int MSDBOT_Init(USBH_Ep_TypeDef *out, USBH_Ep_TypeDef *in);
int MSDBOT_Xfer(void* cbw, void* data);
#endif
#ifdef __cplusplus
}
#endif
/** @} (end group Msd) */
/** @} (end group Drivers) */
#endif /* __MSDBOT_H */
This diff is collapsed.
/***************************************************************************//**
* @file msdd.h
* @brief Mass Storage class Device (MSD) driver.
* @author Energy Micro AS
* @version 3.20.2
*******************************************************************************
* @section License
* <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
*******************************************************************************
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* 4. The source and compiled code may only be used on Energy Micro "EFM32"
* microcontrollers and "EFR4" radios.
*
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
* obligation to support this Software. Energy Micro AS is providing the
* Software "AS IS", with no express or implied warranties of any kind,
* including, but not limited to, any implied warranties of merchantability
* or fitness for any particular purpose or warranties against infringement
* of any proprietary rights of a third party.
*
* Energy Micro AS will not be liable for any consequential, incidental, or
* special damages, or any other relief, or for any claim by any third party,
* arising from your use of this Software.
*
*****************************************************************************/
#ifndef __MSDD_H
#define __MSDD_H
/***************************************************************************//**
* @addtogroup Drivers
* @{
******************************************************************************/
/***************************************************************************//**
* @addtogroup Msd
* @{
******************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
#define MEDIA_BUFSIZ 4096 /**< Intermediate media storage buffer size */
/**************************************************************************//**
* @brief Status info for one BOT CBW -> Data I/O -> CSW cycle.
*****************************************************************************/
typedef struct
{
bool valid; /**< True if the CBW is valid. */
uint8_t direction; /**< Set if BOT direction is IN. */
uint8_t *pData; /**< Media data pointer. */
uint32_t lba; /**< SCSI Read/Write lba address. */
uint32_t xferLen; /**< BOT transfer length. */
uint32_t maxBurst; /**< Max length of one transfer. */
enum { XFER_MEMORYMAPPED = 0, XFER_INDIRECT } xferType;
/**< MSD media access type. */
} MSDD_CmdStatus_TypeDef;
/*** MSDD Device Driver Function prototypes ***/
bool MSDD_Handler(void);
void MSDD_Init(int activityLedPort, uint32_t activityLedPin);
#ifdef __cplusplus
}
#endif
/** @} (end group Msd) */
/** @} (end group Drivers) */
#endif /* __MSDD_H */
This diff is collapsed.
This diff is collapsed.
# Copyright (C) 2014 Julian Lewis
# @author Maciej Suminski <maciej.suminski@cern.ch>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you may find one here:
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# or you may search the http://www.gnu.org website for the version 2 license,
# or you may write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
####################################################################
# Makefile #
####################################################################
.SUFFIXES: # ignore builtin rules
.PHONY: all debug release clean flash
####################################################################
# Definitions #
####################################################################
DEVICE = EFM32GG330F1024
PROJECTNAME = freewatch
# Name of interface configuration file used by OpenOCD
OOCD_IFACE ?= stlink-v2-1
OBJ_DIR = build
EXE_DIR = exe
LST_DIR = lst
####################################################################
# Definitions of toolchain. #
# You might need to do changes to match your system setup #
####################################################################
# Change path to the tools according to your system configuration
# DO NOT add trailing whitespace chars, they do matter !
WINDOWSCS ?= GNU Tools ARM Embedded\4.7 2012q4
LINUXCS ?= /opt/gcc-arm-none-eabi-4_8-2014q2
RMDIRS := rm -rf
RMFILES := rm -rf
ALLFILES := /*.*
NULLDEVICE := /dev/null
SHELLNAMES := $(ComSpec)$(COMSPEC)
# Try autodetecting the environment
ifeq ($(SHELLNAMES),)
# Assume we are making on a Linux platform
TOOLDIR := $(LINUXCS)
else
QUOTE :="
ifneq ($(COMSPEC),)
# Assume we are making on a mingw/msys/cygwin platform running on Windows
# This is a convenient place to override TOOLDIR, DO NOT add trailing
# whitespace chars, they do matter !
TOOLDIR := $(PROGRAMFILES)/$(WINDOWSCS)
ifeq ($(findstring cygdrive,$(shell set)),)
# We were not on a cygwin platform
NULLDEVICE := NUL
endif
else
# Assume we are making on a Windows platform
# This is a convenient place to override TOOLDIR, DO NOT add trailing
# whitespace chars, they do matter !
SHELL := $(SHELLNAMES)
TOOLDIR := $(ProgramFiles)/$(WINDOWSCS)
RMDIRS := rd /s /q
RMFILES := del /s /q
ALLFILES := \*.*
NULLDEVICE := NUL
endif
endif
# Create directories and do a clean which is compatible with parallel make
$(shell mkdir $(OBJ_DIR)>$(NULLDEVICE) 2>&1)
$(shell mkdir $(EXE_DIR)>$(NULLDEVICE) 2>&1)
$(shell mkdir $(LST_DIR)>$(NULLDEVICE) 2>&1)
ifeq (clean,$(findstring clean, $(MAKECMDGOALS)))
ifneq ($(filter $(MAKECMDGOALS),all debug release),)
$(shell $(RMFILES) $(OBJ_DIR)$(ALLFILES)>$(NULLDEVICE) 2>&1)
$(shell $(RMFILES) $(EXE_DIR)$(ALLFILES)>$(NULLDEVICE) 2>&1)
$(shell $(RMFILES) $(LST_DIR)$(ALLFILES)>$(NULLDEVICE) 2>&1)
endif
endif
CC = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-gcc$(QUOTE)
LD = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-ld$(QUOTE)
AR = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-ar$(QUOTE)
OBJCOPY = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-objcopy$(QUOTE)
DUMP = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-objdump$(QUOTE)
####################################################################
# Flags #
####################################################################
# -MMD : Don't generate dependencies on system header files.
# -MP : Add phony targets, useful when a h-file is removed from a project.
# -MF : Specify a file to write the dependencies to.
DEPFLAGS = -MMD -MP -MF $(@:.o=.d)
#
# Add -Wa,-ahld=$(LST_DIR)/$(@F:.o=.lst) to CFLAGS to produce assembly list files
#
override CFLAGS += -D$(DEVICE) -Wall -Wextra -mcpu=cortex-m3 -mthumb \
-mfix-cortex-m3-ldrd -ffunction-sections \
-fdata-sections -fomit-frame-pointer -DDEBUG_EFM \
$(DEPFLAGS)
override ASMFLAGS += -x assembler-with-cpp -D$(DEVICE) -Wall -Wextra -mcpu=cortex-m3 -mthumb
#
# NOTE: The -Wl,--gc-sections flag may interfere with debugging using gdb.
#
override LDFLAGS += -Xlinker -Map=$(LST_DIR)/$(PROJECTNAME).map -mcpu=cortex-m3 \
-mthumb -T../common/Device/EnergyMicro/EFM32GG/Source/GCC/efm32gg.ld \
-Wl,--gc-sections
LIBS = -Wl,--start-group -lgcc -lc -lnosys -Wl,--end-group
INCLUDEPATHS += \
-I../common/CMSIS/Include \
-I../common/Device/EnergyMicro/EFM32GG/Include \
-I../common/emlib/inc \
-I../common/drivers \
-I../common/gfx \
-I../common/usb/inc \
-I../common \
-I../common/reptile/fatfs/inc \
-I. \
####################################################################
# Files #
####################################################################
C_SRC += \
../common/Device/EnergyMicro/EFM32GG/Source/system_efm32gg.c \
../common/drivers/dmactrl.c \
../common/drivers/lcd.c \
../common/drivers/lcd_dma.c \
../common/emlib/src/em_assert.c \
../common/emlib/src/em_cmu.c \
../common/emlib/src/em_emu.c \
../common/emlib/src/em_dma.c \
../common/emlib/src/em_gpio.c \
../common/emlib/src/em_i2c.c \
../common/emlib/src/em_int.c \
../common/emlib/src/em_system.c \
../common/emlib/src/em_rtc.c \
../common/emlib/src/em_usart.c \
../common/emlib/src/em_timer.c \
../common/gfx/graphics.c \
../common/gfx/font_helv11.c \
../common/gfx/font_helv17.c \
../common/gfx/font_helv17b.c \
../common/gfx/font_xm4x5.c \
../common/gfx/font_xm4x6.c \
../common/delay.c \
../common/reptile/fatfs/src/diskio.c \
../common/reptile/fatfs/src/ff.c \
../common/drivers/microsd.c \
../common/drivers/msdd.c \
../common/usb/src/em_usbd.c \
../common/usb/src/em_usbdch9.c \
../common/usb/src/em_usbhal.c \
../common/usb/src/em_usbdep.c \
../common/usb/src/em_usbdint.c \
../common/usb/src/em_usbtimer.c \
msddmedia.c \
main.c
s_SRC +=
S_SRC += \
../common/Device/EnergyMicro/EFM32GG/Source/GCC/startup_efm32gg.S
####################################################################
# Rules #
####################################################################
C_FILES = $(notdir $(C_SRC) )
S_FILES = $(notdir $(S_SRC) $(s_SRC) )
#make list of source paths, sort also removes duplicates
C_PATHS = $(sort $(dir $(C_SRC) ) )
S_PATHS = $(sort $(dir $(S_SRC) $(s_SRC) ) )
C_OBJS = $(addprefix $(OBJ_DIR)/, $(C_FILES:.c=.o))
S_OBJS = $(if $(S_SRC), $(addprefix $(OBJ_DIR)/, $(S_FILES:.S=.o)))
s_OBJS = $(if $(s_SRC), $(addprefix $(OBJ_DIR)/, $(S_FILES:.s=.o)))
C_DEPS = $(addprefix $(OBJ_DIR)/, $(C_FILES:.c=.d))
OBJS = $(C_OBJS) $(S_OBJS) $(s_OBJS)
vpath %.c $(C_PATHS)
vpath %.s $(S_PATHS)
vpath %.S $(S_PATHS)
# Default build is debug build
all: debug
debug: CFLAGS += -DDEBUG -O0 -g
debug: $(EXE_DIR)/$(PROJECTNAME).bin
release: CFLAGS += -DNDEBUG -O0 -g
release: $(EXE_DIR)/$(PROJECTNAME).bin
# Create objects from C SRC files
$(OBJ_DIR)/%.o: %.c
@echo "Building file: $<"
$(CC) $(CFLAGS) $(INCLUDEPATHS) -c -o $@ $<
# Assemble .s/.S files
$(OBJ_DIR)/%.o: %.s
@echo "Assembling $<"
$(CC) $(ASMFLAGS) $(INCLUDEPATHS) -c -o $@ $<
$(OBJ_DIR)/%.o: %.S
@echo "Assembling $<"
$(CC) $(ASMFLAGS) $(INCLUDEPATHS) -c -o $@ $<
# Link
$(EXE_DIR)/$(PROJECTNAME).out: $(OBJS)
@echo "Linking target: $@"
$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o $(EXE_DIR)/$(PROJECTNAME).out
# Create binary file
$(EXE_DIR)/$(PROJECTNAME).bin: $(EXE_DIR)/$(PROJECTNAME).out
@echo "Creating binary file"
$(OBJCOPY) -O binary $(EXE_DIR)/$(PROJECTNAME).out $(EXE_DIR)/$(PROJECTNAME).bin
# Uncomment next line to produce assembly listing of entire program
# $(DUMP) -h -S -C $(EXE_DIR)/$(PROJECTNAME).out>$(LST_DIR)/$(PROJECTNAME)out.lst
clean:
ifeq ($(filter $(MAKECMDGOALS),all debug release),)
$(RMDIRS) $(OBJ_DIR) $(LST_DIR) $(EXE_DIR)
endif
flash: $(EXE_DIR)/$(PROJECTNAME).bin
openocd -s ../common/openocd -f interface/$(OOCD_IFACE).cfg -f init.cfg -c "program $(EXE_DIR)/$(PROJECTNAME).bin 0 verify reset"
# include auto-generated dependency files (explicit rules)
ifneq (clean,$(findstring clean, $(MAKECMDGOALS)))
-include $(C_DEPS)
endif
/***************************************************************************//**
* @file descriptors.h
* @brief USB descriptors for MSD device example project.
* @author Energy Micro AS
* @version 3.20.3
*******************************************************************************
* @section License
* <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
*******************************************************************************
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* 4. The source and compiled code may only be used on Energy Micro "EFM32"
* microcontrollers and "EFR4" radios.
*
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
* obligation to support this Software. Energy Micro AS is providing the
* Software "AS IS", with no express or implied warranties of any kind,
* including, but not limited to, any implied warranties of merchantability
* or fitness for any particular purpose or warranties against infringement
* of any proprietary rights of a third party.
*
* Energy Micro AS will not be liable for any consequential, incidental, or
* special damages, or any other relief, or for any claim by any third party,
* arising from your use of this Software.
*
*****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
EFM32_ALIGN(4)
static const USB_DeviceDescriptor_TypeDef deviceDesc __attribute__ ((aligned(4)))=
{
.bLength = USB_DEVICE_DESCSIZE,
.bDescriptorType = USB_DEVICE_DESCRIPTOR,
.bcdUSB = 0x0200,
.bDeviceClass = 0,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = USB_EP0_SIZE,
.idVendor = 0x2544,
.idProduct = 0x0004,
.bcdDevice = 0x0000,
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
.bNumConfigurations = 1
};
EFM32_ALIGN(4)
static const uint8_t configDesc[] __attribute__ ((aligned(4)))=
{
/*** Configuration descriptor ***/
USB_CONFIG_DESCSIZE, /* bLength */
USB_CONFIG_DESCRIPTOR, /* bDescriptorType */
USB_CONFIG_DESCSIZE + /* wTotalLength (LSB) */
USB_INTERFACE_DESCSIZE +
(USB_ENDPOINT_DESCSIZE * NUM_EP_USED),
(USB_CONFIG_DESCSIZE + /* wTotalLength (MSB) */
USB_INTERFACE_DESCSIZE +
(USB_ENDPOINT_DESCSIZE * NUM_EP_USED))>>8,
1, /* bNumInterfaces */
1, /* bConfigurationValue */
0, /* iConfiguration */
#if defined(BUSPOWERED)
CONFIG_DESC_BM_RESERVED_D7, /* bmAttrib: Bus powered */
#else
CONFIG_DESC_BM_RESERVED_D7 | /* bmAttrib: Self powered */
CONFIG_DESC_BM_SELFPOWERED,
#endif
CONFIG_DESC_MAXPOWER_mA( 50 ), /* bMaxPower: 50 mA */
/*** Interface descriptor ***/
USB_INTERFACE_DESCSIZE, /* bLength */
USB_INTERFACE_DESCRIPTOR,/* bDescriptorType */
0, /* bInterfaceNumber */
0, /* bAlternateSetting */
NUM_EP_USED, /* bNumEndpoints */
USB_CLASS_MSD, /* bInterfaceClass */
USB_CLASS_MSD_SCSI_CMDSET, /* bInterfaceSubClass */
USB_CLASS_MSD_BOT_TRANSPORT,/* bInterfaceProtocol*/
0, /* iInterface */
/*** Endpoint descriptors ***/
USB_ENDPOINT_DESCSIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR,/* bDescriptorType */
BULK_OUT, /* bEndpointAddress (OUT)*/
USB_EPTYPE_BULK, /* bmAttributes */
USB_MAX_EP_SIZE, /* wMaxPacketSize (LSB) */
0, /* wMaxPacketSize (MSB) */
0, /* bInterval */
USB_ENDPOINT_DESCSIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR,/* bDescriptorType */
BULK_IN, /* bEndpointAddress (IN) */
USB_EPTYPE_BULK, /* bmAttributes */
USB_MAX_EP_SIZE, /* wMaxPacketSize (LSB) */
0, /* wMaxPacketSize (MSB) */
0, /* bInterval */
};
STATIC_CONST_STRING_DESC_LANGID( langID, 0x04, 0x09 );
STATIC_CONST_STRING_DESC( iManufacturer, 'E','n','e','r','g','y',' ', \
'M','i','c','r','o',' ','A','S' );
STATIC_CONST_STRING_DESC( iProduct , 'E','F','M','3','2',' ','U','S','B', \
' ','M','a','s','s',' ','S','t','o', \
'r','a','g','e',' ','D','e','v','i', \
'c','e' );
STATIC_CONST_STRING_DESC( iSerialNumber, '0','0','0','0','1','2', \
'3','4','5','6','7','8' );
static const void * const strings[] =
{
&langID,
&iManufacturer,
&iProduct,
&iSerialNumber
};
/* Endpoint buffer sizes */
/* 1 = single buffer, 2 = double buffering, 3 = tripple buffering ... */
static const uint8_t bufferingMultiplier[ NUM_EP_USED + 1 ] = { 1, 2, 2 };
static const USBD_Callbacks_TypeDef callbacks =
{
.usbReset = NULL,
.usbStateChange = UsbStateChangeEvent,
.setupCmd = UsbSetupCmd,
.isSelfPowered = NULL,
.sofInt = NULL
};
static const USBD_Init_TypeDef initstruct =
{
.deviceDescriptor = &deviceDesc,
.configDescriptor = configDesc,
.stringDescriptors = strings,
.numberOfStrings = sizeof(strings)/sizeof(void*),
.callbacks = &callbacks,
.bufferingMultiplier = bufferingMultiplier,
.reserved = 0
};
#ifdef __cplusplus
}
#endif
/*
* Copyright (C) 2014 Julian Lewis
* @author Matthieu Cattin <matthieu.cattin@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include "em_chip.h"
#include "em_device.h"
#include "em_usb.h"
#include "em_assert.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "em_gpio.h"
//#include "bsp.h"
//#include "bsp_trace.h"
//#include "retargetserial.h"
#include "msdd.h"
#include "msddmedia.h"
#include "usbconfig.h"
#include "drivers/lcd.h"
#include "gfx/graphics.h"
#include "delay.h"
/*****************************************************************************
* USB Mass Storage Device (MSD) example
*****************************************************************************/
#if defined( BUSPOWERED )
#if ( ( MSD_MEDIA==MSD_PSRAM_MEDIA ) || ( MSD_MEDIA==MSD_SDCARD_MEDIA ) )
#error "Illegal combination of BUSPOWERED and MSD_MEDIA type."
#endif
#endif
/*****************************************************************************
* @brief main - the entrypoint after reset.
*****************************************************************************/
int main(void)
{
char str[20];
/* Setup SysTick Timer for 1 msec interrupts */
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1);
// Just in case something goes wrong
Delay(200);
// Chip errata
CHIP_Init();
CMU_ClockSelectSet( cmuClock_HF, cmuSelect_HFXO );
CMU_OscillatorEnable(cmuOsc_LFXO, true, false);
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
lcd_init();
lcd_clear();
sprintf(str, "USB MSD test");
text(&font_helv17, 5, 10, str);
lcd_update();
if ( !MSDDMEDIA_Init() )
{
sprintf(str, "Media error!");
text(&font_helv17, 5, 30, str);
lcd_update();
EFM_ASSERT( false );
for( ;; ){}
}
MSDD_Init(gpioPortE, 11); // use backlight as activity indicator led
for (;;)
{
if ( MSDD_Handler() )
{
/* There is no pending activity in the MSDD handler. */
/* Enter sleep mode to conserve energy. */
if ( USBD_SafeToEnterEM2() )
EMU_EnterEM2(true);
else
EMU_EnterEM1();
}
}
}
This diff is collapsed.
/***************************************************************************//**
* @file msddmedia.h
* @brief Media interface for Mass Storage class Device (MSD).
* @author Energy Micro AS
* @version 3.20.3
*******************************************************************************
* @section License
* <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
*******************************************************************************
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* 4. The source and compiled code may only be used on Energy Micro "EFM32"
* microcontrollers and "EFR4" radios.
*
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
* obligation to support this Software. Energy Micro AS is providing the
* Software "AS IS", with no express or implied warranties of any kind,
* including, but not limited to, any implied warranties of merchantability
* or fitness for any particular purpose or warranties against infringement
* of any proprietary rights of a third party.
*
* Energy Micro AS will not be liable for any consequential, incidental, or
* special damages, or any other relief, or for any claim by any third party,
* arising from your use of this Software.
*
*****************************************************************************/
#ifndef __MSDDMEDIA_H
#define __MSDDMEDIA_H
#define MSD_SRAM_MEDIA 0 /* 96K "disk" in internal SRAM */
#define MSD_PSRAM_MEDIA 1 /* 4M "disk" in external PSRAM */
#define MSD_SDCARD_MEDIA 2 /* External micro SD-Card "disk" */
#define MSD_FLASH_MEDIA 3 /* 512K "disk" in internal FLASH */
#define MSD_NORFLASH_MEDIA 4 /* 16M "disk" in external NORFLASH */
#if !defined( MSD_MEDIA )
#define MSD_MEDIA MSD_SDCARD_MEDIA /* Select media type */
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*** MSD Media Function prototypes ***/
bool MSDDMEDIA_CheckAccess( MSDD_CmdStatus_TypeDef *pCmd, uint32_t lba, uint32_t sectors );
void MSDDMEDIA_Flush( void );
uint32_t MSDDMEDIA_GetSectorCount( void );
bool MSDDMEDIA_Init( void );
void MSDDMEDIA_Read( MSDD_CmdStatus_TypeDef *pCmd, uint8_t *data, uint32_t sectors );
void MSDDMEDIA_Write( MSDD_CmdStatus_TypeDef *pCmd, uint8_t *data, uint32_t sectors );
#ifdef __cplusplus
}
#endif
#endif /* __MSDDMEDIA_H */
/***************************************************************************//**
* @file usbconfig.h
* @brief USB protocol stack library, application supplied configuration options.
* @author Energy Micro AS
* @version 3.20.3
*******************************************************************************
* @section License
* <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
*******************************************************************************
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* 4. The source and compiled code may only be used on Energy Micro "EFM32"
* microcontrollers and "EFR4" radios.
*
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
* obligation to support this Software. Energy Micro AS is providing the
* Software "AS IS", with no express or implied warranties of any kind,
* including, but not limited to, any implied warranties of merchantability
* or fitness for any particular purpose or warranties against infringement
* of any proprietary rights of a third party.
*
* Energy Micro AS will not be liable for any consequential, incidental, or
* special damages, or any other relief, or for any claim by any third party,
* arising from your use of this Software.
*
*****************************************************************************/
#ifndef __USBCONFIG_H
#define __USBCONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
//#define BUSPOWERED /* Uncomment to build buspowered device */
#define USB_DEVICE /* Compile stack for device mode. */
#if defined(BUSPOWERED)
#define USB_PWRSAVE_MODE (USB_PWRSAVE_MODE_ONSUSPEND)
#else
#define USB_PWRSAVE_MODE (USB_PWRSAVE_MODE_ONSUSPEND | USB_PWRSAVE_MODE_ONVBUSOFF)
#endif
/****************************************************************************
** **
** Specify number of endpoints used (in addition to EP0). **
** **
*****************************************************************************/
#define NUM_EP_USED 2
/****************************************************************************
** **
** Specify number of application timers you need. **
** **
*****************************************************************************/
#define NUM_APP_TIMERS 1
/****************************************************************************
** **
** Configure serial port debug output. **
** **
*****************************************************************************/
#if 0
#if !defined(BUSPOWERED)
/* Define a function for transmitting a single char on the serial port. */
extern int RETARGET_WriteChar(char c);
#define USER_PUTCHAR RETARGET_WriteChar
/* Debug USB API functions (illegal input parameters etc.) */
#define DEBUG_USB_API
#endif /* !defined(BUSPOWERED) */
#endif
#ifdef __cplusplus
}
#endif
#endif /* __USBCONFIG_H */
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