1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19356: usbus/msc: add CONFIG_USBUS_MSC_AUTO_MTD option to create LUNs on init r=dylad a=benpicco



Co-authored-by: Benjamin Valentin <benjamin.valentin@bht-berlin.de>
This commit is contained in:
bors[bot] 2023-03-09 19:49:51 +00:00 committed by GitHub
commit bdecf57516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 130 additions and 76 deletions

View File

@ -0,0 +1,77 @@
/*
* Copyright (C) 2023 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup drivers_mtd
* @{
* @brief Default MTD device configuration
*
* Helpers for generic MTD use.
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef MTD_DEFAULT_H
#define MTD_DEFAULT_H
#include "board.h"
#include "mtd.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Number of MTD devices
*/
#ifndef MTD_NUMOF
#if defined(MTD_3)
#define MTD_NUMOF 4
#elif defined(MTD_2)
#define MTD_NUMOF 3
#elif defined(MTD_1)
#define MTD_NUMOF 2
#elif defined(MTD_0)
#define MTD_NUMOF 1
#else
#define MTD_NUMOF 0
#endif
#endif
/**
* @brief Get the default MTD device by index
*
* @param[in] idx Index of the MTD device
*
* @return MTD_0 for @p idx 0 and so on
* NULL if no MTD device exists for the given index
*/
static inline mtd_dev_t *mtd_default_get_dev(unsigned idx)
{
switch (idx) {
#ifdef MTD_0
case 0: return MTD_0;
#endif
#ifdef MTD_1
case 1: return MTD_1;
#endif
#ifdef MTD_2
case 2: return MTD_2;
#endif
#ifdef MTD_3
case 3: return MTD_3;
#endif
}
return NULL;
}
#ifdef __cplusplus
}
#endif
#endif /* MTD_DEFAULT_H */
/** @} */

View File

@ -39,6 +39,10 @@ usbus_cdcecm_device_t cdcecm;
#include "usb/usbus/dfu.h"
static usbus_dfu_device_t dfu;
#endif
#ifdef MODULE_USBUS_MSC
#include "usb/usbus/msc.h"
static usbus_msc_device_t msc;
#endif
static char _stack[USBUS_STACKSIZE];
static usbus_t usbus;
@ -66,6 +70,16 @@ void auto_init_usb(void)
usbus_dfu_init(&usbus, &dfu, USB_DFU_PROTOCOL_RUNTIME_MODE);
#endif
#ifdef MODULE_USBUS_MSC
/* Initialize Mass Storage Class */
usbus_msc_init(&usbus, &msc);
#endif
/* Finally initialize USBUS thread */
usbus_create(_stack, USBUS_STACKSIZE, USBUS_PRIO, USBUS_TNAME, &usbus);
}
usbus_t *usbus_auto_init_get(void)
{
return &usbus;
}

View File

@ -73,6 +73,16 @@ extern "C" {
#endif
#endif
/**
* @brief USBUS MSC auto MTD setting
*
* When set to 1, the USBUS MSC module will automatically create a LUN for
* each MTD device defined in `board.h`.
*/
#ifndef CONFIG_USBUS_MSC_AUTO_MTD
#define CONFIG_USBUS_MSC_AUTO_MTD 1
#endif
/**
* @brief USBUS endpoint 0 buffer size
*

View File

@ -25,7 +25,7 @@
#include <stdint.h>
#include "usb/usbus.h"
#include "usb/usbus/msc/scsi.h"
#include "mtd.h"
#include "mtd_default.h"
#ifdef __cplusplus
extern "C" {

View File

@ -16,6 +16,13 @@ menuconfig MODULE_USBUS_MSC
if MODULE_USBUS_MSC
config USBUS_MSC_AUTO_MTD
bool "Automatically export all MTD devices via USB"
default true
help
This will automatically export all MTD devices that follow
the default naming scheme on startup.
config USBUS_MSC_VENDOR_ID
string "MSC Vendor ID"
default "RIOT-OS"

View File

@ -354,6 +354,13 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler)
/* Prepare to receive first bytes from Host */
usbdev_ep_xmit(msc->ep_out->ep, msc->out_buf, CONFIG_USBUS_EP0_SIZE);
/* Auto-configure all MTD devices */
if (CONFIG_USBUS_MSC_AUTO_MTD) {
for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) {
usbus_msc_add_lun(usbus, mtd_default_get_dev(i));
}
}
}
static int _control_handler(usbus_t *usbus, usbus_handler_t *handler,

View File

@ -25,40 +25,12 @@
#include <string.h>
#include "od.h"
#include "mtd.h"
#include "mtd_default.h"
#include "shell.h"
#include "board.h"
#include "macros/units.h"
#include "test_utils/expect.h"
#ifndef MTD_NUMOF
#ifdef MTD_0
#define MTD_NUMOF 1
#else
#define MTD_NUMOF 0
#endif
#endif
static mtd_dev_t *_get_mtd_dev(unsigned idx)
{
switch (idx) {
#ifdef MTD_0
case 0: return MTD_0;
#endif
#ifdef MTD_1
case 1: return MTD_1;
#endif
#ifdef MTD_2
case 2: return MTD_2;
#endif
#ifdef MTD_3
case 3: return MTD_3;
#endif
}
return NULL;
}
static mtd_dev_t *_get_dev(int argc, char **argv)
{
if (argc < 2) {
@ -73,7 +45,7 @@ static mtd_dev_t *_get_dev(int argc, char **argv)
return NULL;
}
return _get_mtd_dev(idx);
return mtd_default_get_dev(idx);
}
static uint64_t _get_size(mtd_dev_t *dev)
@ -311,7 +283,7 @@ static int cmd_info(int argc, char **argv)
for (int i = 0; i < MTD_NUMOF; ++i) {
printf(" -=[ MTD_%d ]=-\n", i);
_print_info(_get_mtd_dev(i));
_print_info(mtd_default_get_dev(i));
}
return 0;
}
@ -481,7 +453,7 @@ int main(void)
for (int i = 0; i < MTD_NUMOF; ++i) {
printf("init MTD_%d… ", i);
mtd_dev_t *dev = _get_mtd_dev(i);
mtd_dev_t *dev = mtd_default_get_dev(i);
int res = mtd_init(dev);
if (res) {
printf("error: %d\n", res);

View File

@ -18,8 +18,6 @@ USEMODULE += ps
USEMODULE += shell
USEMODULE += usbus_msc
USEMODULE += ztimer_msec
# Purposely disable auto_attach for this application
CFLAGS += -DCONFIG_USBUS_AUTO_ATTACH=0
# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

View File

@ -22,39 +22,13 @@
#include <stdio.h>
#include <string.h>
#include "mtd.h"
#include "mtd_default.h"
#include "shell.h"
#include "usb/usbus.h"
#include "usb/usbus/msc.h"
#include "ztimer.h"
#ifndef MTD_NUMOF
#define MTD_NUMOF 0
#endif
static char _stack[USBUS_STACKSIZE];
static usbus_t usbus;
static usbus_msc_device_t msc;
static mtd_dev_t *_get_mtd_dev(unsigned idx)
{
switch (idx) {
#ifdef MTD_0
case 0: return MTD_0;
#endif
#ifdef MTD_1
case 1: return MTD_1;
#endif
#ifdef MTD_2
case 2: return MTD_2;
#endif
#ifdef MTD_3
case 3: return MTD_3;
#endif
}
return NULL;
}
static usbus_t *usbus;
static int _cmd_add_lun(int argc, char **argv)
{
@ -76,8 +50,8 @@ static int _cmd_add_lun(int argc, char **argv)
puts("error: invalid MTD device specified");
return -2;
}
mtd_dev = _get_mtd_dev(dev);
ret = usbus_msc_add_lun(&usbus, mtd_dev);
mtd_dev = mtd_default_get_dev(dev);
ret = usbus_msc_add_lun(usbus, mtd_dev);
if (ret != 0) {
printf("Cannot add LUN device (error:%d %s)\n", ret, strerror(-ret));
}
@ -104,8 +78,8 @@ static int _cmd_remove_lun(int argc, char **argv)
puts("error: invalid MTD device specified");
return -2;
}
mtd_dev = _get_mtd_dev(dev);
ret = usbus_msc_remove_lun(&usbus, mtd_dev);
mtd_dev = mtd_default_get_dev(dev);
ret = usbus_msc_remove_lun(usbus, mtd_dev);
if (ret == -EAGAIN) {
printf("MTD device was not registered\n");
}
@ -118,7 +92,7 @@ static int _cmd_usb_attach(int argc, char **argv)
(void)argv;
static const usbopt_enable_t _enable = USBOPT_ENABLE;
usbdev_set(usbus.dev, USBOPT_ATTACH, &_enable,
usbdev_set(usbus->dev, USBOPT_ATTACH, &_enable,
sizeof(usbopt_enable_t));
return 0;
}
@ -129,7 +103,7 @@ static int _cmd_usb_detach(int argc, char **argv)
(void)argv;
static const usbopt_enable_t _enable = USBOPT_DISABLE;
usbdev_set(usbus.dev, USBOPT_ATTACH, &_enable,
usbdev_set(usbus->dev, USBOPT_ATTACH, &_enable,
sizeof(usbopt_enable_t));
return 0;
}
@ -160,16 +134,11 @@ int main(void)
/* Get driver context */
usbdev_t *usbdev = usbdev_get_ctx(0);
assert(usbdev);
/* Initialize basic usbus struct, don't start the thread yet */
usbus_init(&usbus, usbdev);
usbus_t *usbus_auto_init_get(void);
usbus = usbus_auto_init_get();
/* Initialize Mass Storage Class */
usbus_msc_init(&usbus, &msc);
/* Create USBUS thread */
usbus_create(_stack, USBUS_STACKSIZE, USBUS_PRIO, USBUS_TNAME, &usbus);
/* start shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];