Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: USBMSD_CDC_11U35test
USBCDCMSC.h
- Committer:
- k4zuki
- Date:
- 2015-04-22
- Revision:
- 2:8f01347859d0
- Parent:
- 1:0c31d9b30900
- Child:
- 3:178491b4d4f3
File content as of revision 2:8f01347859d0:
/* Copyright (c) 2010-2011 mbed.org, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef USBCDCMSC_H
#define USBCDCMSC_H
/* These headers are included for child class. */
#include "USBEndpoints.h"
#include "USBDescriptor.h"
#include "USBDevice_Types.h"
#include "USBDevice.h"
#include "Stream.h"
#include "CircBuffer.h"
#include "USBSDFileSystem.h"
class USBCDCMSC: public USBDevice, public Stream {
public:
/*
* Constructor
*
* @param vendor_id Your vendor_id
* @param product_id Your product_id
* @param product_release Your preoduct_release
*/
USBCDCMSC(USBSDFileSystem *sd, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001);
/**
* Send a character. You can use puts, printf.
*
* @param c character to be sent
* @returns true if there is no error, false otherwise
*/
virtual int _putc(int c);
/**
* Read a character: blocking
*
* @returns character read
*/
virtual int _getc();
/**
* Check the number of bytes available.
*
* @returns the number of bytes available
*/
uint8_t available();
/**
* Write a block of data.
*
* For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written.
*
* @param buf pointer on data which will be written
* @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes)
*
* @returns true if successfull
*/
bool writeBlock(uint8_t * buf, uint16_t size);
/**
* Attach a member function to call when a packet is received.
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
*/
template<typename T>
void attach(T* tptr, void (T::*mptr)(void)) {
if((mptr != NULL) && (tptr != NULL)) {
rx.attach(tptr, mptr);
}
}
/**
* Attach a callback called when a packet is received
*
* @param fptr function pointer
*/
void attach(void (*fn)(void)) {
if(fn != NULL) {
rx.attach(fn);
}
}
/**
* Connect the USB MSD device. Establish disk initialization before really connect the device.
*
* @returns true if successful
*/
bool connect();
protected:
/*
* Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
*
* @returns pointer to the device descriptor
*/
virtual uint8_t * deviceDesc();
/*
* Get string product descriptor
*
* @returns pointer to the string product descriptor
*/
virtual uint8_t * stringIproductDesc();
/*
* Get string interface descriptor
*
* @returns pointer to the string interface descriptor
*/
virtual uint8_t * stringIinterfaceDesc();
/*
* Get configuration descriptor
*
* @returns pointer to the configuration descriptor
*/
virtual uint8_t * configurationDesc();
/*
* Send a buffer
*
* @param endpoint endpoint which will be sent the buffer
* @param buffer buffer to be sent
* @param size length of the buffer
* @returns true if successful
*/
bool send(uint8_t * buffer, uint16_t size);
/*
* Read a buffer from a certain endpoint. Warning: blocking
*
* @param endpoint endpoint to read
* @param buffer buffer where will be stored bytes
* @param size the number of bytes read will be stored in *size
* @param maxSize the maximum length that can be read
* @returns true if successful
*/
bool readEP(uint8_t * buffer, uint32_t * size);
/*
* Read a buffer from a certain endpoint. Warning: non blocking
*
* @param endpoint endpoint to read
* @param buffer buffer where will be stored bytes
* @param size the number of bytes read will be stored in *size
* @param maxSize the maximum length that can be read
* @returns true if successful
*/
bool readEP_NB(uint8_t * buffer, uint32_t * size);
/*
* Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
* This is used to handle extensions to standard requests
* and class specific requests
*
* @returns true if class handles this request
*/
virtual bool USBCallback_request();
/*
* Called by USBDevice on Endpoint0 request completion
* if the 'notify' flag has been set to true. Warning: Called in ISR context
*
* In this case it is used to indicate that a HID report has
* been received from the host on endpoint 0
*
* @param buf buffer received on endpoint 0
* @param length length of this buffer
*/
virtual void USBCallback_requestCompleted(uint8_t *buf, uint32_t length); //from USBCDC class
/*
* Called by USBDevice layer. Set configuration of the device.
* For instance, you can add all endpoints that you need on this function.
*
* @param configuration Number of the configuration
*/
virtual bool USBCallback_setConfiguration(uint8_t configuration);
/*
* Called by USBCallback_requestCompleted when CDC line coding is changed
* Warning: Called in ISR
*
* @param baud The baud rate
* @param bits The number of bits in a word (5-8)
* @param parity The parity
* @param stop The number of stop bits (1 or 2)
*/
virtual void lineCodingChanged(int baud, int bits, int parity, int stop){
if (settingsChangedCallback) {
settingsChangedCallback(baud, bits, parity, stop);
}
}
virtual bool EP2_OUT_callback();
/*
* Callback called when a MSD packet is received
*/
virtual bool EP3_OUT_callback();
/*
* Callback called when a MSD packet has been sent
*/
virtual bool EP3_IN_callback();
private:
FunctionPointer rx;
CircBuffer<uint8_t> cdcbuf;
int cdcbreak;
// MSC Bulk-only Stage
enum Stage {
READ_CBW, // wait a CBW
ERROR, // error
PROCESS_CBW, // process a CBW request
SEND_CSW, // send a CSW
WAIT_CSW, // wait that a CSW has been effectively sent
};
// Bulk-only CBW
typedef __packed struct {
uint32_t Signature;
uint32_t Tag;
uint32_t DataLength;
uint8_t Flags;
uint8_t LUN;
uint8_t CBLength;
uint8_t CB[16];
} CBW;
// Bulk-only CSW
typedef __packed struct {
uint32_t Signature;
uint32_t Tag;
uint32_t DataResidue;
uint8_t Status;
} CSW;
//state of the bulk-only state machine
Stage stage;
// current CBW
CBW cbw;
// CSW which will be sent
CSW csw;
// addr where will be read or written data
uint32_t addr;
// length of a reading or writing
uint32_t length;
// memory OK (after a memoryVerify)
bool memOK;
// cache in RAM before writing in memory. Useful also to read a block.
uint8_t * page;
int BlockSize;
int MemorySize;
int BlockCount;
int _status;
USBSDFileSystem *_sd;
void CBWDecode(uint8_t * buf, uint16_t size);
void sendCSW (void);
bool inquiryRequest (void);
bool msd_write (uint8_t * buf, uint16_t size);
bool readFormatCapacity();
bool readCapacity (void);
bool infoTransfer (void);
void memoryRead (void);
bool modeSense6 (void);
void testUnitReady (void);
bool requestSense (void);
void memoryVerify (uint8_t * buf, uint16_t size);
void memoryWrite (uint8_t * buf, uint16_t size);
void reset();
void fail();
int isBreak();
int disk_initialize();
int _disk_write(const uint8_t *buffer, int block_number);
int disk_read(uint8_t *buffer, int block_number);
int _disk_status();
int disk_sectors();
int disk_size();
void (*settingsChangedCallback)(int baud, int bits, int parity, int stop);
};
#endif