Update master branch with new features from development branch (#20)
* Major changes in DC/DC control and board support + ThingSet library added * Small platformio.ini fixes - added libignore for mbed-USBDevice to all STM32F072 builds - add map file generation. Use text editor or AMAP (http://www.sikorskiy.net/prj/amap/) to view and learn where the precious flash and ram goes to. - fixed board define for LS 0.10 board * Travis fix and more - Now runs only a selected subset of all configurations (all STM32F072/L073) - Added a src_filter to the cloudsolar_03 since L073 does not have CAN. Maybe there is a better way to do this. Also added libignore for USB to this environment. - Added memory.map to .gitignore * STML0 support improved + small changes - STM32L0 now widely supported: PWM signal generation, ADC via DMA and display via I2C are working - Old files for USB support (only F0) included into repository, as files in original mbed repository don't compile. - CAN bus support only for F0 (added header defines to allow compilation) * Cell & Pack Config refactoring: First work on #13. Reorganized battery init a bit to get a better idea of how to split things. All known chemistries are now supported with safe values (please check the values). Added data for NMC (aka "normal" LiIo cells such as most 18650) A configuration now declares itself to be valid or not after battery_init. BAT_TYPE_NONE (formerly BAT_TYPE_DEFAULT, which equals 0) will not pass check and thus is an invalid configuration, as a result an unconfigured battery_t struct will not be considered valid by accident. We should not continue to start normal operation if a battery configuration is not valid. However, this is not yet implemented. Also slightly changed interface to battery_init, now we pass the number of cells. Moved the static pub_enabled into main.cpp to get ride of a ton of "unused static variable" warnings. * ThingSet moved to separate library * Removed serious bug in PWM signal generation resulting in PWM with 0% duty cycle * LED control via PWM for STM32L0 board added * Gets rid of USB deprecated warnings in application code This simply disables any warning regarding the use of deprecated internals in mbed for now. Since the "new" USBDevice code does not run on F0 or L0, we use old one which works but generates warnings a lot. If they ever remove the deprecated interface, we'll know sure enough since then code will no longer compile. * More battery init refactoring In addition to the mandatory arguments cell type, number in series and capacity, the code now accepts a partially configure battery_t structure (you provide various voltage levels and charge currents) for Lithium based cells, checks the configuration values for sanity and tries to adjust if outside valid operational limits. The defaults try to balance between charging stress and capacity loss. Charge is by default to about 90% maximum full charge, and discharge stops around 20%-30%. For lead acid cells all voltage levels and currents are set / calculated based on the mandatory parameters, no configuration of voltage levels possible. We may change that, but I don't have enough no knowledge about valid ranges etc. I added a high voltage lithium chemistry for cells which have typically 4.35 voltage limit (vs. 4.2 normally). * Added user configuration for battery and system operation mode Copy the most relevant template to config.cpp and then compile. config.cpp is intentionally not part of the git managed source code since it represents the user specific configuration part which varies across different firmware builds. * Turn off MBED Deprecated Warning for Old USB Stack attempt 2 This time cross platform compatible... And also fix the travis build issue * Boost mode added and first updates for timer-based DC/DC control - system_mode now distinguishes between MPPT_BOOST and MPPT_BUCK - new function system_control() which does all important and fast control stuff necessary for the DC/DC (will be included into interrupt in the future) - small correction in _dcdc_output_control() algorithm after test with e-bike battery solar charging * Naming of structs and enums adjusted * Documentation amended and separate docs folder created. * UNIT_TEST defines removed * Timer for DC/DC control implemented, ADC timer updated * Files restructured / renamed - config.h is now pcb.h and contains only hardware-specific settings - anything which communicates to the outside world (display, serial interface, etc) is now renamed to more generic "interface" instead of "output" and "communication". Use interface_<DEVICE>.cpp for implementations of specific interfaces. * Updates in user configuration and interfaces - Battery configuration can now be adjusted in battery_init_user in config.cpp - New function battery_update_settings implemented to check new battery configuration for plausibility (also needed for later user-configuration via serial interface) - Templates for GSM and LoRa interfaces created - Restructuring of main to allow different interface configurations * Travis CI adjusted to new config.h * Load output templates added * ThingSet library updated, interface processing updated, publication functions added * Load output implementation updated and moved to load.cpp/h * Energy calculation updated and eTotal changed to uint32_t * Update firmware.md * Issue monitor_speed typing mistake We found a typing issue: "monitor_speed" is mistyped as "monitor_port". * Major updates of recent months
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* Standard descriptor types */
|
||||
#define DEVICE_DESCRIPTOR (1)
|
||||
#define CONFIGURATION_DESCRIPTOR (2)
|
||||
#define STRING_DESCRIPTOR (3)
|
||||
#define INTERFACE_DESCRIPTOR (4)
|
||||
#define ENDPOINT_DESCRIPTOR (5)
|
||||
#define QUALIFIER_DESCRIPTOR (6)
|
||||
|
||||
/* Standard descriptor lengths */
|
||||
#define DEVICE_DESCRIPTOR_LENGTH (0x12)
|
||||
#define CONFIGURATION_DESCRIPTOR_LENGTH (0x09)
|
||||
#define INTERFACE_DESCRIPTOR_LENGTH (0x09)
|
||||
#define ENDPOINT_DESCRIPTOR_LENGTH (0x07)
|
||||
|
||||
|
||||
/*string offset*/
|
||||
#define STRING_OFFSET_LANGID (0)
|
||||
#define STRING_OFFSET_IMANUFACTURER (1)
|
||||
#define STRING_OFFSET_IPRODUCT (2)
|
||||
#define STRING_OFFSET_ISERIAL (3)
|
||||
#define STRING_OFFSET_ICONFIGURATION (4)
|
||||
#define STRING_OFFSET_IINTERFACE (5)
|
||||
|
||||
/* USB Specification Release Number */
|
||||
#define USB_VERSION_2_0 (0x0200)
|
||||
|
||||
/* Least/Most significant byte of short integer */
|
||||
#define LSB(n) ((n)&0xff)
|
||||
#define MSB(n) (((n)&0xff00)>>8)
|
||||
|
||||
/* Convert physical endpoint number to descriptor endpoint number */
|
||||
#define PHY_TO_DESC(endpoint) (((endpoint)>>1) | (((endpoint) & 1) ? 0x80:0))
|
||||
|
||||
/* bmAttributes in configuration descriptor */
|
||||
/* C_RESERVED must always be set */
|
||||
#define C_RESERVED (1U<<7)
|
||||
#define C_SELF_POWERED (1U<<6)
|
||||
#define C_REMOTE_WAKEUP (1U<<5)
|
||||
|
||||
/* bMaxPower in configuration descriptor */
|
||||
#define C_POWER(mA) ((mA)/2)
|
||||
|
||||
/* bmAttributes in endpoint descriptor */
|
||||
#define E_CONTROL (0x00)
|
||||
#define E_ISOCHRONOUS (0x01)
|
||||
#define E_BULK (0x02)
|
||||
#define E_INTERRUPT (0x03)
|
||||
|
||||
/* For isochronous endpoints only: */
|
||||
#define E_NO_SYNCHRONIZATION (0x00)
|
||||
#define E_ASYNCHRONOUS (0x04)
|
||||
#define E_ADAPTIVE (0x08)
|
||||
#define E_SYNCHRONOUS (0x0C)
|
||||
#define E_DATA (0x00)
|
||||
#define E_FEEDBACK (0x10)
|
||||
#define E_IMPLICIT_FEEDBACK (0x20)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,271 @@
|
||||
/* 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 USBDEVICE_H
|
||||
#define USBDEVICE_H
|
||||
|
||||
#include "mbed.h"
|
||||
#include "USBDevice_Types.h"
|
||||
#include "USBHAL.h"
|
||||
|
||||
class USBDevice: public USBHAL
|
||||
{
|
||||
public:
|
||||
USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
|
||||
|
||||
/*
|
||||
* Check if the device is configured
|
||||
*
|
||||
* @returns true if configured, false otherwise
|
||||
*/
|
||||
bool configured(void);
|
||||
|
||||
/*
|
||||
* Connect a device
|
||||
*
|
||||
* @param blocking: block if not configured
|
||||
*/
|
||||
void connect(bool blocking = true);
|
||||
|
||||
/*
|
||||
* Disconnect a device
|
||||
*/
|
||||
void disconnect(void);
|
||||
|
||||
/*
|
||||
* Add an endpoint
|
||||
*
|
||||
* @param endpoint endpoint which will be added
|
||||
* @param maxPacket Maximum size of a packet which can be sent for this endpoint
|
||||
* @returns true if successful, false otherwise
|
||||
*/
|
||||
bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
|
||||
|
||||
/*
|
||||
* Start a reading on a certain endpoint.
|
||||
* You can access the result of the reading by USBDevice_read
|
||||
*
|
||||
* @param endpoint endpoint which will be read
|
||||
* @param maxSize the maximum length that can be read
|
||||
* @return true if successful
|
||||
*/
|
||||
bool readStart(uint8_t endpoint, uint32_t maxSize);
|
||||
|
||||
/*
|
||||
* Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
|
||||
* must be called.
|
||||
*
|
||||
* Warning: blocking
|
||||
*
|
||||
* @param endpoint endpoint which will be read
|
||||
* @param buffer buffer will be filled with the data received
|
||||
* @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 endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
|
||||
|
||||
/*
|
||||
* Read a certain endpoint.
|
||||
*
|
||||
* Warning: non blocking
|
||||
*
|
||||
* @param endpoint endpoint which will be read
|
||||
* @param buffer buffer will be filled with the data received (if data are available)
|
||||
* @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 endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
|
||||
|
||||
/*
|
||||
* Write a certain endpoint.
|
||||
*
|
||||
* Warning: blocking
|
||||
*
|
||||
* @param endpoint endpoint to write
|
||||
* @param buffer data contained in buffer will be write
|
||||
* @param size the number of bytes to write
|
||||
* @param maxSize the maximum length that can be written on this endpoint
|
||||
*/
|
||||
bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
|
||||
|
||||
|
||||
/*
|
||||
* Write a certain endpoint.
|
||||
*
|
||||
* Warning: non blocking
|
||||
*
|
||||
* @param endpoint endpoint to write
|
||||
* @param buffer data contained in buffer will be write
|
||||
* @param size the number of bytes to write
|
||||
* @param maxSize the maximum length that can be written on this endpoint
|
||||
*/
|
||||
bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
|
||||
|
||||
|
||||
/*
|
||||
* Called by USBDevice layer on bus reset. Warning: Called in ISR context
|
||||
*
|
||||
* May be used to reset state
|
||||
*/
|
||||
virtual void USBCallback_busReset(void) {};
|
||||
|
||||
/*
|
||||
* 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() { return false; };
|
||||
|
||||
/*
|
||||
* 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) {};
|
||||
|
||||
/*
|
||||
* 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) { return false; };
|
||||
|
||||
/*
|
||||
* Called by USBDevice layer. Set interface/alternate of the device.
|
||||
*
|
||||
* @param interface Number of the interface to be configured
|
||||
* @param alternate Number of the alternate to be configured
|
||||
* @returns true if class handles this request
|
||||
*/
|
||||
virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
|
||||
|
||||
/*
|
||||
* 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 configuration descriptor
|
||||
*
|
||||
* @returns pointer to the configuration descriptor
|
||||
*/
|
||||
virtual uint8_t * configurationDesc(){return NULL;};
|
||||
|
||||
/*
|
||||
* Get string lang id descriptor
|
||||
*
|
||||
* @return pointer to the string lang id descriptor
|
||||
*/
|
||||
virtual uint8_t * stringLangidDesc();
|
||||
|
||||
/*
|
||||
* Get string manufacturer descriptor
|
||||
*
|
||||
* @returns pointer to the string manufacturer descriptor
|
||||
*/
|
||||
virtual uint8_t * stringImanufacturerDesc();
|
||||
|
||||
/*
|
||||
* Get string product descriptor
|
||||
*
|
||||
* @returns pointer to the string product descriptor
|
||||
*/
|
||||
virtual uint8_t * stringIproductDesc();
|
||||
|
||||
/*
|
||||
* Get string serial descriptor
|
||||
*
|
||||
* @returns pointer to the string serial descriptor
|
||||
*/
|
||||
virtual uint8_t * stringIserialDesc();
|
||||
|
||||
/*
|
||||
* Get string configuration descriptor
|
||||
*
|
||||
* @returns pointer to the string configuration descriptor
|
||||
*/
|
||||
virtual uint8_t * stringIConfigurationDesc();
|
||||
|
||||
/*
|
||||
* Get string interface descriptor
|
||||
*
|
||||
* @returns pointer to the string interface descriptor
|
||||
*/
|
||||
virtual uint8_t * stringIinterfaceDesc();
|
||||
|
||||
/*
|
||||
* Get the length of the report descriptor
|
||||
*
|
||||
* @returns length of the report descriptor
|
||||
*/
|
||||
virtual uint16_t reportDescLength() { return 0; };
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
virtual void busReset(void);
|
||||
virtual void EP0setupCallback(void);
|
||||
virtual void EP0out(void);
|
||||
virtual void EP0in(void);
|
||||
virtual void connectStateChanged(unsigned int connected);
|
||||
virtual void suspendStateChanged(unsigned int suspended);
|
||||
uint8_t * findDescriptor(uint8_t descriptorType);
|
||||
CONTROL_TRANSFER * getTransferPtr(void);
|
||||
|
||||
uint16_t VENDOR_ID;
|
||||
uint16_t PRODUCT_ID;
|
||||
uint16_t PRODUCT_RELEASE;
|
||||
|
||||
private:
|
||||
bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
|
||||
bool requestGetDescriptor(void);
|
||||
bool controlOut(void);
|
||||
bool controlIn(void);
|
||||
bool requestSetAddress(void);
|
||||
bool requestSetConfiguration(void);
|
||||
bool requestSetFeature(void);
|
||||
bool requestClearFeature(void);
|
||||
bool requestGetStatus(void);
|
||||
bool requestSetup(void);
|
||||
bool controlSetup(void);
|
||||
void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
|
||||
bool requestGetConfiguration(void);
|
||||
bool requestGetInterface(void);
|
||||
bool requestSetInterface(void);
|
||||
|
||||
CONTROL_TRANSFER transfer;
|
||||
USB_DEVICE device;
|
||||
|
||||
uint16_t currentInterface;
|
||||
uint8_t currentAlternate;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,83 @@
|
||||
/* 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 USBDEVICE_TYPES_H
|
||||
#define USBDEVICE_TYPES_H
|
||||
|
||||
/* Standard requests */
|
||||
#define GET_STATUS (0)
|
||||
#define CLEAR_FEATURE (1)
|
||||
#define SET_FEATURE (3)
|
||||
#define SET_ADDRESS (5)
|
||||
#define GET_DESCRIPTOR (6)
|
||||
#define SET_DESCRIPTOR (7)
|
||||
#define GET_CONFIGURATION (8)
|
||||
#define SET_CONFIGURATION (9)
|
||||
#define GET_INTERFACE (10)
|
||||
#define SET_INTERFACE (11)
|
||||
|
||||
/* bmRequestType.dataTransferDirection */
|
||||
#define HOST_TO_DEVICE (0)
|
||||
#define DEVICE_TO_HOST (1)
|
||||
|
||||
/* bmRequestType.Type*/
|
||||
#define STANDARD_TYPE (0)
|
||||
#define CLASS_TYPE (1)
|
||||
#define VENDOR_TYPE (2)
|
||||
#define RESERVED_TYPE (3)
|
||||
|
||||
/* bmRequestType.Recipient */
|
||||
#define DEVICE_RECIPIENT (0)
|
||||
#define INTERFACE_RECIPIENT (1)
|
||||
#define ENDPOINT_RECIPIENT (2)
|
||||
#define OTHER_RECIPIENT (3)
|
||||
|
||||
/* Descriptors */
|
||||
#define DESCRIPTOR_TYPE(wValue) (wValue >> 8)
|
||||
#define DESCRIPTOR_INDEX(wValue) (wValue & 0xff)
|
||||
|
||||
typedef struct {
|
||||
struct {
|
||||
uint8_t dataTransferDirection;
|
||||
uint8_t Type;
|
||||
uint8_t Recipient;
|
||||
} bmRequestType;
|
||||
uint8_t bRequest;
|
||||
uint16_t wValue;
|
||||
uint16_t wIndex;
|
||||
uint16_t wLength;
|
||||
} SETUP_PACKET;
|
||||
|
||||
typedef struct {
|
||||
SETUP_PACKET setup;
|
||||
uint8_t *ptr;
|
||||
uint32_t remaining;
|
||||
uint8_t direction;
|
||||
bool zlp;
|
||||
bool notify;
|
||||
} CONTROL_TRANSFER;
|
||||
|
||||
typedef enum {ATTACHED, POWERED, DEFAULT, ADDRESS, CONFIGURED} DEVICE_STATE;
|
||||
|
||||
typedef struct {
|
||||
volatile DEVICE_STATE state;
|
||||
uint8_t configuration;
|
||||
bool suspended;
|
||||
} USB_DEVICE;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
/* 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 USBENDPOINTS_H
|
||||
#define USBENDPOINTS_H
|
||||
|
||||
/* SETUP packet size */
|
||||
#define SETUP_PACKET_SIZE (8)
|
||||
|
||||
/* Options flags for configuring endpoints */
|
||||
#define DEFAULT_OPTIONS (0)
|
||||
#define SINGLE_BUFFERED (1U << 0)
|
||||
#define ISOCHRONOUS (1U << 1)
|
||||
#define RATE_FEEDBACK_MODE (1U << 2) /* Interrupt endpoints only */
|
||||
|
||||
/* Endpoint transfer status, for endpoints > 0 */
|
||||
typedef enum {
|
||||
EP_COMPLETED, /* Transfer completed */
|
||||
EP_PENDING, /* Transfer in progress */
|
||||
EP_INVALID, /* Invalid parameter */
|
||||
EP_STALLED, /* Endpoint stalled */
|
||||
} EP_STATUS;
|
||||
|
||||
/* Include configuration for specific target */
|
||||
#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC4088) || defined(TARGET_LPC2460) || defined(TARGET_LPC4088_DM)
|
||||
#include "USBEndpoints_LPC17_LPC23.h"
|
||||
#elif defined(TARGET_LPC11UXX) || defined(TARGET_LPC1347) || defined (TARGET_LPC11U6X) || defined (TARGET_LPC1549)
|
||||
#include "USBEndpoints_LPC11U.h"
|
||||
#elif defined(TARGET_KL25Z) | defined(TARGET_KL43Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D50M) | defined(TARGET_K64F) | defined(TARGET_K22F) | defined(TARGET_TEENSY3_1)
|
||||
#include "USBEndpoints_KL25Z.h"
|
||||
#elif defined (TARGET_STM32F4)
|
||||
#include "USBEndpoints_STM32F4.h"
|
||||
#elif defined(TARGET_STM32L1)||defined(TARGET_STM32F1)||defined(TARGET_STM32F0)||defined(TARGET_STM32L0)
|
||||
#include "USBEndpoints_STM32L1.h"
|
||||
#elif defined (TARGET_RZ_A1H)
|
||||
#include "USBEndpoints_RZ_A1H.h"
|
||||
#elif defined(TARGET_Maxim)
|
||||
#include "USBEndpoints_Maxim.h"
|
||||
#else
|
||||
#error "Unknown target type"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,67 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#define NUMBER_OF_LOGICAL_ENDPOINTS (4)
|
||||
#define NUMBER_OF_PHYSICAL_ENDPOINTS (NUMBER_OF_LOGICAL_ENDPOINTS * 2)
|
||||
|
||||
/* Define physical endpoint numbers */
|
||||
|
||||
/* Endpoint No. Type(s) MaxPacket DoubleBuffer */
|
||||
/* ---------------- ------------ ---------- --- */
|
||||
#define EP0OUT (0) /* Control 64 No */
|
||||
#define EP0IN (1) /* Control 64 No */
|
||||
#define EP1OUT (2) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP1IN (3) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP2OUT (4) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP2IN (5) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP3OUT (6) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP3IN (7) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
|
||||
/* Maximum Packet sizes */
|
||||
|
||||
#define MAX_PACKET_SIZE_EP0 (64)
|
||||
#define MAX_PACKET_SIZE_EP1 (64) /* Int/Bulk */
|
||||
#define MAX_PACKET_SIZE_EP2 (64) /* Int/Bulk */
|
||||
#define MAX_PACKET_SIZE_EP3 (64) /* Int/Bulk */
|
||||
|
||||
#define MAX_PACKET_SIZE_EP1_ISO (1023) /* Isochronous */
|
||||
#define MAX_PACKET_SIZE_EP2_ISO (1023) /* Isochronous */
|
||||
#define MAX_PACKET_SIZE_EP3_ISO (1023) /* Isochronous */
|
||||
|
||||
/* Generic endpoints - intended to be portable accross devices */
|
||||
/* and be suitable for simple USB devices. */
|
||||
|
||||
/* Bulk endpoint */
|
||||
#define EPBULK_OUT (EP2OUT)
|
||||
#define EPBULK_IN (EP2IN)
|
||||
#define EPBULK_OUT_callback EP2_OUT_callback
|
||||
#define EPBULK_IN_callback EP2_IN_callback
|
||||
/* Interrupt endpoint */
|
||||
#define EPINT_OUT (EP1OUT)
|
||||
#define EPINT_IN (EP1IN)
|
||||
#define EPINT_OUT_callback EP1_OUT_callback
|
||||
#define EPINT_IN_callback EP1_IN_callback
|
||||
/* Isochronous endpoint */
|
||||
#define EPISO_OUT (EP3OUT)
|
||||
#define EPISO_IN (EP3IN)
|
||||
#define EPISO_OUT_callback EP3_OUT_callback
|
||||
#define EPISO_IN_callback EP3_IN_callback
|
||||
|
||||
#define MAX_PACKET_SIZE_EPBULK (MAX_PACKET_SIZE_EP2)
|
||||
#define MAX_PACKET_SIZE_EPINT (MAX_PACKET_SIZE_EP1)
|
||||
#define MAX_PACKET_SIZE_EPISO (MAX_PACKET_SIZE_EP3_ISO)
|
||||
@@ -0,0 +1,67 @@
|
||||
/* Copyright (c) 2010-2015 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.
|
||||
*/
|
||||
|
||||
#define NUMBER_OF_LOGICAL_ENDPOINTS (4)
|
||||
#define NUMBER_OF_PHYSICAL_ENDPOINTS (NUMBER_OF_LOGICAL_ENDPOINTS * 2)
|
||||
|
||||
/* Define physical endpoint numbers */
|
||||
|
||||
/* Endpoint No. Type(s) MaxPacket DoubleBuffer */
|
||||
/* ---------------- ------------ ---------- --- */
|
||||
#define EP0OUT (0) /* Control 64 No */
|
||||
#define EP0IN (1) /* Control 64 No */
|
||||
#define EP1OUT (2) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP1IN (3) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP2OUT (4) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP2IN (5) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP3OUT (6) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
#define EP3IN (7) /* Int/Bulk/Iso 64/64/1023 Yes */
|
||||
|
||||
/* Maximum Packet sizes */
|
||||
|
||||
#define MAX_PACKET_SIZE_EP0 (64)
|
||||
#define MAX_PACKET_SIZE_EP1 (64) /* Int/Bulk */
|
||||
#define MAX_PACKET_SIZE_EP2 (64) /* Int/Bulk */
|
||||
#define MAX_PACKET_SIZE_EP3 (64) /* Int/Bulk */
|
||||
|
||||
#define MAX_PACKET_SIZE_EP1_ISO (192) /* Isochronous */
|
||||
#define MAX_PACKET_SIZE_EP2_ISO (192) /* Isochronous */
|
||||
#define MAX_PACKET_SIZE_EP3_ISO (192) /* Isochronous */
|
||||
|
||||
/* Generic endpoints - intended to be portable accross devices */
|
||||
/* and be suitable for simple USB devices. */
|
||||
|
||||
/* Bulk endpoint */
|
||||
#define EPBULK_OUT (EP2OUT)
|
||||
#define EPBULK_IN (EP2IN)
|
||||
#define EPBULK_OUT_callback EP2_OUT_callback
|
||||
#define EPBULK_IN_callback EP2_IN_callback
|
||||
/* Interrupt endpoint */
|
||||
#define EPINT_OUT (EP1OUT)
|
||||
#define EPINT_IN (EP1IN)
|
||||
#define EPINT_OUT_callback EP1_OUT_callback
|
||||
#define EPINT_IN_callback EP1_IN_callback
|
||||
/* Isochronous endpoint */
|
||||
#define EPISO_OUT (EP3OUT)
|
||||
#define EPISO_IN (EP3IN)
|
||||
#define EPISO_OUT_callback EP3_OUT_callback
|
||||
#define EPISO_IN_callback EP3_IN_callback
|
||||
|
||||
#define MAX_PACKET_SIZE_EPBULK (MAX_PACKET_SIZE_EP2)
|
||||
#define MAX_PACKET_SIZE_EPINT (MAX_PACKET_SIZE_EP1)
|
||||
#define MAX_PACKET_SIZE_EPISO (MAX_PACKET_SIZE_EP3_ISO)
|
||||
@@ -0,0 +1,129 @@
|
||||
/* 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 USBBUSINTERFACE_H
|
||||
#define USBBUSINTERFACE_H
|
||||
|
||||
#include "mbed.h"
|
||||
#include "USBEndpoints.h"
|
||||
#include "mbed_toolchain.h"
|
||||
|
||||
//#ifdef __GNUC__
|
||||
//#define __packed __attribute__ ((__packed__))
|
||||
//#endif
|
||||
|
||||
class USBHAL {
|
||||
public:
|
||||
/* Configuration */
|
||||
USBHAL();
|
||||
~USBHAL();
|
||||
void connect(void);
|
||||
void disconnect(void);
|
||||
void configureDevice(void);
|
||||
void unconfigureDevice(void);
|
||||
void setAddress(uint8_t address);
|
||||
void remoteWakeup(void);
|
||||
|
||||
/* Endpoint 0 */
|
||||
void EP0setup(uint8_t *buffer);
|
||||
void EP0read(void);
|
||||
void EP0readStage(void);
|
||||
uint32_t EP0getReadResult(uint8_t *buffer);
|
||||
void EP0write(uint8_t *buffer, uint32_t size);
|
||||
void EP0getWriteResult(void);
|
||||
void EP0stall(void);
|
||||
|
||||
/* Other endpoints */
|
||||
EP_STATUS endpointRead(uint8_t endpoint, uint32_t maximumSize);
|
||||
EP_STATUS endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead);
|
||||
EP_STATUS endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size);
|
||||
EP_STATUS endpointWriteResult(uint8_t endpoint);
|
||||
void stallEndpoint(uint8_t endpoint);
|
||||
void unstallEndpoint(uint8_t endpoint);
|
||||
bool realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options);
|
||||
bool getEndpointStallState(unsigned char endpoint);
|
||||
uint32_t endpointReadcore(uint8_t endpoint, uint8_t *buffer);
|
||||
|
||||
protected:
|
||||
virtual void busReset(void){};
|
||||
virtual void EP0setupCallback(void){};
|
||||
virtual void EP0out(void){};
|
||||
virtual void EP0in(void){};
|
||||
virtual void connectStateChanged(unsigned int connected){};
|
||||
virtual void suspendStateChanged(unsigned int suspended){};
|
||||
virtual void SOF(int frameNumber){};
|
||||
|
||||
virtual bool EP1_OUT_callback(){return false;};
|
||||
virtual bool EP1_IN_callback(){return false;};
|
||||
virtual bool EP2_OUT_callback(){return false;};
|
||||
virtual bool EP2_IN_callback(){return false;};
|
||||
virtual bool EP3_OUT_callback(){return false;};
|
||||
virtual bool EP3_IN_callback(){return false;};
|
||||
#if !defined(TARGET_STM32F4)||defined(TARGET_STM32L1)||defined(TARGET_STM32F1)||defined(TARGET_STM32F0)
|
||||
virtual bool EP4_OUT_callback(){return false;};
|
||||
virtual bool EP4_IN_callback(){return false;};
|
||||
#if !(defined(TARGET_LPC11UXX) || defined(TARGET_LPC11U6X) || defined(TARGET_LPC1347) || defined(TARGET_LPC1549))
|
||||
virtual bool EP5_OUT_callback(){return false;};
|
||||
virtual bool EP5_IN_callback(){return false;};
|
||||
virtual bool EP6_OUT_callback(){return false;};
|
||||
virtual bool EP6_IN_callback(){return false;};
|
||||
virtual bool EP7_OUT_callback(){return false;};
|
||||
virtual bool EP7_IN_callback(){return false;};
|
||||
virtual bool EP8_OUT_callback(){return false;};
|
||||
virtual bool EP8_IN_callback(){return false;};
|
||||
virtual bool EP9_OUT_callback(){return false;};
|
||||
virtual bool EP9_IN_callback(){return false;};
|
||||
virtual bool EP10_OUT_callback(){return false;};
|
||||
virtual bool EP10_IN_callback(){return false;};
|
||||
virtual bool EP11_OUT_callback(){return false;};
|
||||
virtual bool EP11_IN_callback(){return false;};
|
||||
virtual bool EP12_OUT_callback(){return false;};
|
||||
virtual bool EP12_IN_callback(){return false;};
|
||||
virtual bool EP13_OUT_callback(){return false;};
|
||||
virtual bool EP13_IN_callback(){return false;};
|
||||
virtual bool EP14_OUT_callback(){return false;};
|
||||
virtual bool EP14_IN_callback(){return false;};
|
||||
virtual bool EP15_OUT_callback(){return false;};
|
||||
virtual bool EP15_IN_callback(){return false;};
|
||||
#endif
|
||||
#endif
|
||||
|
||||
private:
|
||||
void usbisr(void);
|
||||
static void _usbisr(void);
|
||||
static USBHAL * instance;
|
||||
|
||||
#if defined(TARGET_LPC11UXX) || defined(TARGET_LPC11U6X) || defined(TARGET_LPC1347) || defined(TARGET_LPC1549)
|
||||
bool (USBHAL::*epCallback[10 - 2])(void);
|
||||
#elif defined(TARGET_STM32F4)
|
||||
bool (USBHAL::*epCallback[8 - 2])(void);
|
||||
#else
|
||||
bool (USBHAL::*epCallback[32 - 2])(void);
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(TARGET_STM32L1)||defined(TARGET_STM32F1)||defined(TARGET_STM32F0)
|
||||
public: // HAL_PCD
|
||||
void SetupStageCallback();
|
||||
void DataInStageCallback(uint8_t epnum);
|
||||
void DataOutStageCallback(uint8_t epnum);
|
||||
void ResetCallback();
|
||||
void SOFCallback();
|
||||
#endif // TARGET_STM32L1
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,416 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#if defined(TARGET_STM32F4)
|
||||
|
||||
#include "USBHAL.h"
|
||||
#include "USBRegs_STM32.h"
|
||||
#include "pinmap.h"
|
||||
|
||||
USBHAL * USBHAL::instance;
|
||||
|
||||
static volatile int epComplete = 0;
|
||||
|
||||
static uint32_t bufferEnd = 0;
|
||||
static const uint32_t rxFifoSize = 512;
|
||||
static uint32_t rxFifoCount = 0;
|
||||
|
||||
static uint32_t setupBuffer[MAX_PACKET_SIZE_EP0 >> 2];
|
||||
|
||||
uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
USBHAL::USBHAL(void) {
|
||||
NVIC_DisableIRQ(OTG_FS_IRQn);
|
||||
epCallback[0] = &USBHAL::EP1_OUT_callback;
|
||||
epCallback[1] = &USBHAL::EP1_IN_callback;
|
||||
epCallback[2] = &USBHAL::EP2_OUT_callback;
|
||||
epCallback[3] = &USBHAL::EP2_IN_callback;
|
||||
epCallback[4] = &USBHAL::EP3_OUT_callback;
|
||||
epCallback[5] = &USBHAL::EP3_IN_callback;
|
||||
|
||||
// Enable power and clocking
|
||||
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
|
||||
|
||||
#if defined(TARGET_STM32F407VG) || defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE) || defined(TARGET_STM32F429ZI)
|
||||
pin_function(PA_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
|
||||
pin_function(PA_9, STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLDOWN, GPIO_AF10_OTG_FS));
|
||||
pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS));
|
||||
pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
|
||||
pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
|
||||
#else
|
||||
pin_function(PA_8, STM_PIN_DATA(2, 10));
|
||||
pin_function(PA_9, STM_PIN_DATA(0, 0));
|
||||
pin_function(PA_10, STM_PIN_DATA(2, 10));
|
||||
pin_function(PA_11, STM_PIN_DATA(2, 10));
|
||||
pin_function(PA_12, STM_PIN_DATA(2, 10));
|
||||
|
||||
// Set ID pin to open drain with pull-up resistor
|
||||
pin_mode(PA_10, OpenDrain);
|
||||
GPIOA->PUPDR &= ~(0x3 << 20);
|
||||
GPIOA->PUPDR |= 1 << 20;
|
||||
|
||||
// Set VBUS pin to open drain
|
||||
pin_mode(PA_9, OpenDrain);
|
||||
#endif
|
||||
|
||||
RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
|
||||
|
||||
// Enable interrupts
|
||||
OTG_FS->GREGS.GAHBCFG |= (1 << 0);
|
||||
|
||||
// Turnaround time to maximum value - too small causes packet loss
|
||||
OTG_FS->GREGS.GUSBCFG |= (0xF << 10);
|
||||
|
||||
// Unmask global interrupts
|
||||
OTG_FS->GREGS.GINTMSK |= (1 << 3) | // SOF
|
||||
(1 << 4) | // RX FIFO not empty
|
||||
(1 << 12); // USB reset
|
||||
|
||||
OTG_FS->DREGS.DCFG |= (0x3 << 0) | // Full speed
|
||||
(1 << 2); // Non-zero-length status OUT handshake
|
||||
|
||||
OTG_FS->GREGS.GCCFG |= (1 << 19) | // Enable VBUS sensing
|
||||
(1 << 16); // Power Up
|
||||
|
||||
instance = this;
|
||||
NVIC_SetVector(OTG_FS_IRQn, (uint32_t)&_usbisr);
|
||||
NVIC_SetPriority(OTG_FS_IRQn, 1);
|
||||
}
|
||||
|
||||
USBHAL::~USBHAL(void) {
|
||||
}
|
||||
|
||||
void USBHAL::connect(void) {
|
||||
NVIC_EnableIRQ(OTG_FS_IRQn);
|
||||
}
|
||||
|
||||
void USBHAL::disconnect(void) {
|
||||
NVIC_DisableIRQ(OTG_FS_IRQn);
|
||||
}
|
||||
|
||||
void USBHAL::configureDevice(void) {
|
||||
// Not needed
|
||||
}
|
||||
|
||||
void USBHAL::unconfigureDevice(void) {
|
||||
// Not needed
|
||||
}
|
||||
|
||||
void USBHAL::setAddress(uint8_t address) {
|
||||
OTG_FS->DREGS.DCFG |= (address << 4);
|
||||
EP0write(0, 0);
|
||||
}
|
||||
|
||||
bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket,
|
||||
uint32_t flags) {
|
||||
uint32_t epIndex = endpoint >> 1;
|
||||
|
||||
uint32_t type;
|
||||
switch (endpoint) {
|
||||
case EP0IN:
|
||||
case EP0OUT:
|
||||
type = 0;
|
||||
break;
|
||||
case EPISO_IN:
|
||||
case EPISO_OUT:
|
||||
type = 1;
|
||||
case EPBULK_IN:
|
||||
case EPBULK_OUT:
|
||||
type = 2;
|
||||
break;
|
||||
case EPINT_IN:
|
||||
case EPINT_OUT:
|
||||
type = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
// Generic in or out EP controls
|
||||
uint32_t control = (maxPacket << 0) | // Packet size
|
||||
(1 << 15) | // Active endpoint
|
||||
(type << 18); // Endpoint type
|
||||
|
||||
if (endpoint & 0x1) { // In Endpoint
|
||||
// Set up the Tx FIFO
|
||||
if (endpoint == EP0IN) {
|
||||
OTG_FS->GREGS.DIEPTXF0_HNPTXFSIZ = ((maxPacket >> 2) << 16) |
|
||||
(bufferEnd << 0);
|
||||
}
|
||||
else {
|
||||
OTG_FS->GREGS.DIEPTXF[epIndex - 1] = ((maxPacket >> 2) << 16) |
|
||||
(bufferEnd << 0);
|
||||
}
|
||||
bufferEnd += maxPacket >> 2;
|
||||
|
||||
// Set the In EP specific control settings
|
||||
if (endpoint != EP0IN) {
|
||||
control |= (1 << 28); // SD0PID
|
||||
}
|
||||
|
||||
control |= (epIndex << 22) | // TxFIFO index
|
||||
(1 << 27); // SNAK
|
||||
OTG_FS->INEP_REGS[epIndex].DIEPCTL = control;
|
||||
|
||||
// Unmask the interrupt
|
||||
OTG_FS->DREGS.DAINTMSK |= (1 << epIndex);
|
||||
}
|
||||
else { // Out endpoint
|
||||
// Set the out EP specific control settings
|
||||
control |= (1 << 26); // CNAK
|
||||
OTG_FS->OUTEP_REGS[epIndex].DOEPCTL = control;
|
||||
|
||||
// Unmask the interrupt
|
||||
OTG_FS->DREGS.DAINTMSK |= (1 << (epIndex + 16));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// read setup packet
|
||||
void USBHAL::EP0setup(uint8_t *buffer) {
|
||||
memcpy(buffer, setupBuffer, MAX_PACKET_SIZE_EP0);
|
||||
}
|
||||
|
||||
void USBHAL::EP0readStage(void) {
|
||||
}
|
||||
|
||||
void USBHAL::EP0read(void) {
|
||||
}
|
||||
|
||||
uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {
|
||||
uint32_t* buffer32 = (uint32_t *) buffer;
|
||||
uint32_t length = rxFifoCount;
|
||||
for (uint32_t i = 0; i < length; i += 4) {
|
||||
buffer32[i >> 2] = OTG_FS->FIFO[0][0];
|
||||
}
|
||||
|
||||
rxFifoCount = 0;
|
||||
return length;
|
||||
}
|
||||
|
||||
void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
|
||||
endpointWrite(0, buffer, size);
|
||||
}
|
||||
|
||||
void USBHAL::EP0getWriteResult(void) {
|
||||
}
|
||||
|
||||
void USBHAL::EP0stall(void) {
|
||||
// If we stall the out endpoint here then we have problems transferring
|
||||
// and setup requests after the (stalled) get device qualifier requests.
|
||||
// TODO: Find out if this is correct behavior, or whether we are doing
|
||||
// something else wrong
|
||||
stallEndpoint(EP0IN);
|
||||
// stallEndpoint(EP0OUT);
|
||||
}
|
||||
|
||||
EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
|
||||
uint32_t epIndex = endpoint >> 1;
|
||||
uint32_t size = (1 << 19) | // 1 packet
|
||||
(maximumSize << 0); // Packet size
|
||||
// if (endpoint == EP0OUT) {
|
||||
size |= (1 << 29); // 1 setup packet
|
||||
// }
|
||||
OTG_FS->OUTEP_REGS[epIndex].DOEPTSIZ = size;
|
||||
OTG_FS->OUTEP_REGS[epIndex].DOEPCTL |= (1 << 31) | // Enable endpoint
|
||||
(1 << 26); // Clear NAK
|
||||
|
||||
epComplete &= ~(1 << endpoint);
|
||||
return EP_PENDING;
|
||||
}
|
||||
|
||||
EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {
|
||||
if (!(epComplete & (1 << endpoint))) {
|
||||
return EP_PENDING;
|
||||
}
|
||||
|
||||
uint32_t* buffer32 = (uint32_t *) buffer;
|
||||
uint32_t length = rxFifoCount;
|
||||
for (uint32_t i = 0; i < length; i += 4) {
|
||||
buffer32[i >> 2] = OTG_FS->FIFO[endpoint >> 1][0];
|
||||
}
|
||||
rxFifoCount = 0;
|
||||
*bytesRead = length;
|
||||
return EP_COMPLETED;
|
||||
}
|
||||
|
||||
EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {
|
||||
uint32_t epIndex = endpoint >> 1;
|
||||
OTG_FS->INEP_REGS[epIndex].DIEPTSIZ = (1 << 19) | // 1 packet
|
||||
(size << 0); // Size of packet
|
||||
OTG_FS->INEP_REGS[epIndex].DIEPCTL |= (1 << 31) | // Enable endpoint
|
||||
(1 << 26); // CNAK
|
||||
OTG_FS->DREGS.DIEPEMPMSK = (1 << epIndex);
|
||||
|
||||
while ((OTG_FS->INEP_REGS[epIndex].DTXFSTS & 0XFFFF) < ((size + 3) >> 2));
|
||||
|
||||
for (uint32_t i=0; i<(size + 3) >> 2; i++, data+=4) {
|
||||
OTG_FS->FIFO[epIndex][0] = *(uint32_t *)data;
|
||||
}
|
||||
|
||||
epComplete &= ~(1 << endpoint);
|
||||
|
||||
return EP_PENDING;
|
||||
}
|
||||
|
||||
EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
|
||||
if (epComplete & (1 << endpoint)) {
|
||||
epComplete &= ~(1 << endpoint);
|
||||
return EP_COMPLETED;
|
||||
}
|
||||
|
||||
return EP_PENDING;
|
||||
}
|
||||
|
||||
void USBHAL::stallEndpoint(uint8_t endpoint) {
|
||||
if (endpoint & 0x1) { // In EP
|
||||
OTG_FS->INEP_REGS[endpoint >> 1].DIEPCTL |= (1 << 30) | // Disable
|
||||
(1 << 21); // Stall
|
||||
}
|
||||
else { // Out EP
|
||||
OTG_FS->DREGS.DCTL |= (1 << 9); // Set global out NAK
|
||||
OTG_FS->OUTEP_REGS[endpoint >> 1].DOEPCTL |= (1 << 30) | // Disable
|
||||
(1 << 21); // Stall
|
||||
}
|
||||
}
|
||||
|
||||
void USBHAL::unstallEndpoint(uint8_t endpoint) {
|
||||
|
||||
}
|
||||
|
||||
bool USBHAL::getEndpointStallState(uint8_t endpoint) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void USBHAL::remoteWakeup(void) {
|
||||
}
|
||||
|
||||
|
||||
void USBHAL::_usbisr(void) {
|
||||
instance->usbisr();
|
||||
}
|
||||
|
||||
|
||||
void USBHAL::usbisr(void) {
|
||||
if (OTG_FS->GREGS.GINTSTS & (1 << 11)) { // USB Suspend
|
||||
suspendStateChanged(1);
|
||||
};
|
||||
|
||||
if (OTG_FS->GREGS.GINTSTS & (1 << 12)) { // USB Reset
|
||||
suspendStateChanged(0);
|
||||
|
||||
// Set SNAK bits
|
||||
OTG_FS->OUTEP_REGS[0].DOEPCTL |= (1 << 27);
|
||||
OTG_FS->OUTEP_REGS[1].DOEPCTL |= (1 << 27);
|
||||
OTG_FS->OUTEP_REGS[2].DOEPCTL |= (1 << 27);
|
||||
OTG_FS->OUTEP_REGS[3].DOEPCTL |= (1 << 27);
|
||||
|
||||
OTG_FS->DREGS.DIEPMSK = (1 << 0);
|
||||
|
||||
bufferEnd = 0;
|
||||
|
||||
// Set the receive FIFO size
|
||||
OTG_FS->GREGS.GRXFSIZ = rxFifoSize >> 2;
|
||||
bufferEnd += rxFifoSize >> 2;
|
||||
|
||||
// Create the endpoints, and wait for setup packets on out EP0
|
||||
realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0);
|
||||
realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0);
|
||||
endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
|
||||
|
||||
OTG_FS->GREGS.GINTSTS = (1 << 12);
|
||||
}
|
||||
|
||||
if (OTG_FS->GREGS.GINTSTS & (1 << 4)) { // RX FIFO not empty
|
||||
uint32_t status = OTG_FS->GREGS.GRXSTSP;
|
||||
|
||||
uint32_t endpoint = (status & 0xF) << 1;
|
||||
uint32_t length = (status >> 4) & 0x7FF;
|
||||
uint32_t type = (status >> 17) & 0xF;
|
||||
|
||||
rxFifoCount = length;
|
||||
|
||||
if (type == 0x6) {
|
||||
// Setup packet
|
||||
for (uint32_t i=0; i<length; i+=4) {
|
||||
setupBuffer[i >> 2] = OTG_FS->FIFO[0][i >> 2];
|
||||
}
|
||||
rxFifoCount = 0;
|
||||
}
|
||||
|
||||
if (type == 0x4) {
|
||||
// Setup complete
|
||||
EP0setupCallback();
|
||||
endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
|
||||
}
|
||||
|
||||
if (type == 0x2) {
|
||||
// Out packet
|
||||
if (endpoint == EP0OUT) {
|
||||
EP0out();
|
||||
}
|
||||
else {
|
||||
epComplete |= (1 << endpoint);
|
||||
if ((instance->*(epCallback[endpoint - 2]))()) {
|
||||
epComplete &= (1 << endpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (uint32_t i=0; i<rxFifoCount; i+=4) {
|
||||
(void) OTG_FS->FIFO[0][0];
|
||||
}
|
||||
OTG_FS->GREGS.GINTSTS = (1 << 4);
|
||||
}
|
||||
|
||||
if (OTG_FS->GREGS.GINTSTS & (1 << 18)) { // In endpoint interrupt
|
||||
// Loop through the in endpoints
|
||||
for (uint32_t i=0; i<4; i++) {
|
||||
if (OTG_FS->DREGS.DAINT & (1 << i)) { // Interrupt is on endpoint
|
||||
|
||||
if (OTG_FS->INEP_REGS[i].DIEPINT & (1 << 7)) {// Tx FIFO empty
|
||||
// If the Tx FIFO is empty on EP0 we need to send a further
|
||||
// packet, so call EP0in()
|
||||
if (i == 0) {
|
||||
EP0in();
|
||||
}
|
||||
// Clear the interrupt
|
||||
OTG_FS->INEP_REGS[i].DIEPINT = (1 << 7);
|
||||
// Stop firing Tx empty interrupts
|
||||
// Will get turned on again if another write is called
|
||||
OTG_FS->DREGS.DIEPEMPMSK &= ~(1 << i);
|
||||
}
|
||||
|
||||
// If the transfer is complete
|
||||
if (OTG_FS->INEP_REGS[i].DIEPINT & (1 << 0)) { // Tx Complete
|
||||
epComplete |= (1 << (1 + (i << 1)));
|
||||
OTG_FS->INEP_REGS[i].DIEPINT = (1 << 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
OTG_FS->GREGS.GINTSTS = (1 << 18);
|
||||
}
|
||||
|
||||
if (OTG_FS->GREGS.GINTSTS & (1 << 3)) { // Start of frame
|
||||
SOF((OTG_FS->GREGS.GRXSTSR >> 17) & 0xF);
|
||||
OTG_FS->GREGS.GINTSTS = (1 << 3);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,411 @@
|
||||
/* Copyright (c) 2010-2015 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.
|
||||
*/
|
||||
|
||||
#if defined(TARGET_STM32L1)||defined(TARGET_STM32F1)||defined(TARGET_STM32F0)
|
||||
#include "USBDevice.h"
|
||||
|
||||
#if defined(TARGET_STM32F1)
|
||||
#define USB_LP_IRQn USB_LP_CAN1_RX0_IRQn
|
||||
const uint8_t PCD_EP_TYPE_CTRL = EP_TYPE_CTRL;
|
||||
const uint8_t PCD_EP_TYPE_INTR = EP_TYPE_INTR;
|
||||
const uint8_t PCD_EP_TYPE_BULK = EP_TYPE_BULK;
|
||||
const uint8_t PCD_EP_TYPE_ISOC = EP_TYPE_ISOC;
|
||||
|
||||
#elif defined(TARGET_STM32L1)
|
||||
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) {
|
||||
__SYSCFG_CLK_ENABLE(); // for SYSCFG_PMC_USB_PU
|
||||
if (state == 1) {
|
||||
__HAL_SYSCFG_USBPULLUP_ENABLE();
|
||||
} else {
|
||||
__HAL_SYSCFG_USBPULLUP_DISABLE();
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined(TARGET_STM32L0)||defined(TARGET_STM32F0)
|
||||
#define USB_LP_IRQn USB_IRQn
|
||||
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) {
|
||||
if (state == 1) {
|
||||
SET_BIT(USB->BCDR, USB_BCDR_DPPU); // DP Pull-up
|
||||
} else {
|
||||
CLEAR_BIT(USB->BCDR, USB_BCDR_DPPU);
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined(TARGET_STM32F3)
|
||||
#define USB_LP_IRQn USB_LP_CAN1_RX0_IRQn
|
||||
#endif
|
||||
|
||||
static PCD_HandleTypeDef hpcd_USB_FS;
|
||||
static volatile int epComplete = 0;
|
||||
|
||||
USBHAL * USBHAL::instance;
|
||||
uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {return 0;}
|
||||
|
||||
USBHAL::USBHAL(void) {
|
||||
hpcd_USB_FS.pData = this;
|
||||
hpcd_USB_FS.Instance = USB;
|
||||
hpcd_USB_FS.Init.dev_endpoints = 8;
|
||||
hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
|
||||
hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_8;
|
||||
hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
|
||||
hpcd_USB_FS.Init.Sof_enable = DISABLE;
|
||||
hpcd_USB_FS.Init.low_power_enable = DISABLE;
|
||||
hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
|
||||
NVIC_SetVector(USB_LP_IRQn, (uint32_t)&_usbisr);
|
||||
HAL_PCD_Init(&hpcd_USB_FS);
|
||||
HAL_PCD_Start(&hpcd_USB_FS);
|
||||
}
|
||||
|
||||
void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd) {
|
||||
//__USB_CLK_ENABLE();
|
||||
__HAL_RCC_USB_CLK_ENABLE();
|
||||
HAL_NVIC_SetPriority(USB_LP_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(USB_LP_IRQn);
|
||||
}
|
||||
|
||||
void HAL_PCD_MspDeInit(PCD_HandleTypeDef* hpcd) {
|
||||
//__USB_CLK_DISABLE(); // Peripheral clock disable
|
||||
__HAL_RCC_USB_CLK_DISABLE();
|
||||
HAL_NVIC_DisableIRQ(USB_LP_IRQn); // Peripheral interrupt Deinit
|
||||
}
|
||||
|
||||
USBHAL::~USBHAL(void) {
|
||||
HAL_PCD_DeInit(&hpcd_USB_FS);
|
||||
}
|
||||
|
||||
void USBHAL::connect(void) {
|
||||
HAL_PCD_DevConnect(&hpcd_USB_FS);
|
||||
}
|
||||
|
||||
void USBHAL::disconnect(void) {
|
||||
HAL_PCD_DevDisconnect(&hpcd_USB_FS);
|
||||
}
|
||||
|
||||
void USBHAL::configureDevice(void) {
|
||||
// Not needed
|
||||
}
|
||||
void USBHAL::unconfigureDevice(void) {
|
||||
// Not needed
|
||||
}
|
||||
|
||||
void USBHAL::setAddress(uint8_t address) {
|
||||
HAL_PCD_SetAddress(&hpcd_USB_FS, address);
|
||||
}
|
||||
|
||||
class PacketBufferAreaManager {
|
||||
public:
|
||||
PacketBufferAreaManager(int bufsize_):bufsize(bufsize_) {
|
||||
reset();
|
||||
}
|
||||
void reset() {
|
||||
head = 0;
|
||||
tail = bufsize;
|
||||
}
|
||||
int allocBuf(int maxPacketSize) {
|
||||
head += 4;
|
||||
tail -= maxPacketSize;
|
||||
if (tail < head) {
|
||||
return 0;
|
||||
}
|
||||
return tail;
|
||||
}
|
||||
private:
|
||||
int head,tail;
|
||||
int bufsize;
|
||||
} PktBufArea(512);
|
||||
|
||||
bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) {
|
||||
int pmaadress = PktBufArea.allocBuf(maxPacket);
|
||||
MBED_ASSERT(pmaadress != 0);
|
||||
if (pmaadress == 0) {
|
||||
return false;
|
||||
}
|
||||
PCD_HandleTypeDef *hpcd = &hpcd_USB_FS;
|
||||
uint8_t ep_type;
|
||||
switch(endpoint) {
|
||||
case EP0OUT:
|
||||
HAL_PCDEx_PMAConfig(hpcd, 0x00, PCD_SNG_BUF, pmaadress);
|
||||
HAL_PCD_EP_Open(hpcd, 0x00, maxPacket, PCD_EP_TYPE_CTRL);
|
||||
break;
|
||||
case EP0IN:
|
||||
HAL_PCDEx_PMAConfig(hpcd, 0x80, PCD_SNG_BUF, pmaadress);
|
||||
HAL_PCD_EP_Open(hpcd, 0x80, maxPacket, PCD_EP_TYPE_CTRL);
|
||||
break;
|
||||
case EPINT_OUT:
|
||||
HAL_PCDEx_PMAConfig(hpcd, 0x01, PCD_SNG_BUF, pmaadress);
|
||||
HAL_PCD_EP_Open(hpcd, 0x01, maxPacket, PCD_EP_TYPE_INTR);
|
||||
break;
|
||||
case EPINT_IN:
|
||||
HAL_PCDEx_PMAConfig(hpcd, 0x81, PCD_SNG_BUF, pmaadress);
|
||||
HAL_PCD_EP_Open(hpcd, 0x81, maxPacket, PCD_EP_TYPE_INTR);
|
||||
break;
|
||||
case EPBULK_OUT:
|
||||
HAL_PCDEx_PMAConfig(hpcd, 0x02, PCD_SNG_BUF, pmaadress);
|
||||
HAL_PCD_EP_Open(hpcd, 0x02, maxPacket, PCD_EP_TYPE_BULK);
|
||||
break;
|
||||
case EPBULK_IN:
|
||||
HAL_PCDEx_PMAConfig(hpcd, 0x82, PCD_SNG_BUF, pmaadress);
|
||||
HAL_PCD_EP_Open(hpcd, 0x82, maxPacket, PCD_EP_TYPE_BULK);
|
||||
break;
|
||||
case EP3OUT:
|
||||
HAL_PCDEx_PMAConfig(hpcd, 0x03, PCD_SNG_BUF, pmaadress);
|
||||
ep_type = (flags & ISOCHRONOUS) ? PCD_EP_TYPE_ISOC : PCD_EP_TYPE_BULK;
|
||||
HAL_PCD_EP_Open(hpcd, 0x03, maxPacket, ep_type);
|
||||
break;
|
||||
case EP3IN:
|
||||
HAL_PCDEx_PMAConfig(hpcd, 0x83, PCD_SNG_BUF, pmaadress);
|
||||
ep_type = (flags & ISOCHRONOUS) ? PCD_EP_TYPE_ISOC : PCD_EP_TYPE_BULK;
|
||||
HAL_PCD_EP_Open(hpcd, 0x83, maxPacket, ep_type);
|
||||
break;
|
||||
default:
|
||||
MBED_ASSERT(0);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// read setup packet
|
||||
void USBHAL::EP0setup(uint8_t *buffer) {
|
||||
memcpy(buffer, hpcd_USB_FS.Setup, 8);
|
||||
}
|
||||
|
||||
void USBHAL::EP0readStage(void) {
|
||||
}
|
||||
|
||||
void USBHAL::EP0read(void) {
|
||||
endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0);
|
||||
}
|
||||
|
||||
class rxTempBufferManager {
|
||||
uint8_t buf0[MAX_PACKET_SIZE_EP0];
|
||||
uint8_t buf1[MAX_PACKET_SIZE_EP1];
|
||||
uint8_t buf2[MAX_PACKET_SIZE_EP2];
|
||||
uint8_t buf3[MAX_PACKET_SIZE_EP3_ISO];
|
||||
public:
|
||||
uint8_t* ptr(uint8_t endpoint, int maxPacketSize) {
|
||||
switch(endpoint) {
|
||||
case EP0OUT:
|
||||
MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP0);
|
||||
break;
|
||||
case EP1OUT:
|
||||
MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP1);
|
||||
break;
|
||||
case EP2OUT:
|
||||
MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP2);
|
||||
break;
|
||||
case EP3OUT:
|
||||
MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP3_ISO);
|
||||
break;
|
||||
}
|
||||
return ptr(endpoint);
|
||||
}
|
||||
uint8_t* ptr(uint8_t endpoint) {
|
||||
switch(endpoint) {
|
||||
case EP0OUT: return buf0;
|
||||
case EP1OUT: return buf1;
|
||||
case EP2OUT: return buf2;
|
||||
case EP3OUT: return buf3;
|
||||
}
|
||||
MBED_ASSERT(0);
|
||||
return NULL;
|
||||
}
|
||||
} rxtmp;
|
||||
|
||||
uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {
|
||||
const uint8_t endpoint = EP0OUT;
|
||||
uint32_t length = HAL_PCD_EP_GetRxCount(&hpcd_USB_FS, endpoint>>1);
|
||||
memcpy(buffer, rxtmp.ptr(endpoint), length);
|
||||
return length;
|
||||
}
|
||||
|
||||
void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
|
||||
endpointWrite(EP0IN, buffer, size);
|
||||
}
|
||||
|
||||
void USBHAL::EP0getWriteResult(void) {
|
||||
}
|
||||
|
||||
void USBHAL::EP0stall(void) {
|
||||
// If we stall the out endpoint here then we have problems transferring
|
||||
// and setup requests after the (stalled) get device qualifier requests.
|
||||
// TODO: Find out if this is correct behavior, or whether we are doing
|
||||
// something else wrong
|
||||
stallEndpoint(EP0IN);
|
||||
// stallEndpoint(EP0OUT);
|
||||
}
|
||||
|
||||
EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
|
||||
HAL_PCD_EP_Receive(&hpcd_USB_FS, endpoint>>1, rxtmp.ptr(endpoint, maximumSize), maximumSize);
|
||||
epComplete &= ~(1 << endpoint);
|
||||
return EP_PENDING;
|
||||
}
|
||||
|
||||
EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {
|
||||
if (!(epComplete & (1 << endpoint))) {
|
||||
return EP_PENDING;
|
||||
}
|
||||
int len = HAL_PCD_EP_GetRxCount(&hpcd_USB_FS, endpoint>>1);
|
||||
memcpy(buffer, rxtmp.ptr(endpoint), len);
|
||||
*bytesRead = len;
|
||||
return EP_COMPLETED;
|
||||
}
|
||||
|
||||
EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {
|
||||
HAL_PCD_EP_Transmit(&hpcd_USB_FS, endpoint>>1, data, size);
|
||||
epComplete &= ~(1 << endpoint);
|
||||
return EP_PENDING;
|
||||
}
|
||||
|
||||
EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
|
||||
if (epComplete & (1 << endpoint)) {
|
||||
epComplete &= ~(1 << endpoint);
|
||||
return EP_COMPLETED;
|
||||
}
|
||||
return EP_PENDING;
|
||||
}
|
||||
|
||||
void USBHAL::stallEndpoint(uint8_t endpoint) {
|
||||
PCD_HandleTypeDef *hpcd = &hpcd_USB_FS;
|
||||
switch(endpoint) {
|
||||
case EP0IN:
|
||||
HAL_PCD_EP_SetStall(hpcd, 0x80);
|
||||
break;
|
||||
case EP0OUT:
|
||||
HAL_PCD_EP_SetStall(hpcd, 0x00);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void USBHAL::unstallEndpoint(uint8_t endpoint) {
|
||||
}
|
||||
|
||||
bool USBHAL::getEndpointStallState(uint8_t endpoint) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void USBHAL::remoteWakeup(void) {}
|
||||
|
||||
void USBHAL::_usbisr(void) {
|
||||
HAL_PCD_IRQHandler(&hpcd_USB_FS);
|
||||
}
|
||||
|
||||
void USBHAL::usbisr(void) {}
|
||||
|
||||
void USBHAL::SetupStageCallback() {
|
||||
EP0setupCallback();
|
||||
}
|
||||
|
||||
void USBHAL::DataInStageCallback(uint8_t epnum) {
|
||||
switch(epnum) {
|
||||
case 0: // EP0IN
|
||||
EP0in();
|
||||
break;
|
||||
case 1:
|
||||
epComplete |= (1<<EP1IN);
|
||||
if (EP1_IN_callback()) {
|
||||
epComplete &= ~(1<<EP1IN);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
epComplete |= (1<<EP2IN);
|
||||
if (EP2_IN_callback()) {
|
||||
epComplete &= ~(1<<EP2IN);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
epComplete |= (1<<EP3IN);
|
||||
if (EP3_IN_callback()) {
|
||||
epComplete &= ~(1<<EP3IN);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
MBED_ASSERT(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void USBHAL::DataOutStageCallback(uint8_t epnum) {
|
||||
switch(epnum) {
|
||||
case 0: // EP0OUT
|
||||
if ((hpcd_USB_FS.Setup[0]&0x80) == 0x00) { // host to device ?
|
||||
EP0out();
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
epComplete |= (1<<EP1OUT);
|
||||
if (EP1_OUT_callback()) {
|
||||
epComplete &= ~(1<<EP1OUT);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
epComplete |= (1<<EP2OUT);
|
||||
if (EP2_OUT_callback()) {
|
||||
epComplete &= ~(1<<EP2OUT);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
epComplete |= (1<<EP3OUT);
|
||||
if (EP3_OUT_callback()) {
|
||||
epComplete &= ~(1<<EP3OUT);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
MBED_ASSERT(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void USBHAL::ResetCallback() {
|
||||
PktBufArea.reset();
|
||||
realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0);
|
||||
realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0);
|
||||
}
|
||||
|
||||
void USBHAL::SOFCallback() {
|
||||
SOF(hpcd_USB_FS.Instance->FNR & USB_FNR_FN);
|
||||
}
|
||||
|
||||
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) {
|
||||
reinterpret_cast<USBHAL*>(hpcd->pData)->SetupStageCallback();
|
||||
}
|
||||
|
||||
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) {
|
||||
reinterpret_cast<USBHAL*>(hpcd->pData)->DataInStageCallback(epnum);
|
||||
}
|
||||
|
||||
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) {
|
||||
reinterpret_cast<USBHAL*>(hpcd->pData)->DataOutStageCallback(epnum);
|
||||
}
|
||||
|
||||
void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) {
|
||||
reinterpret_cast<USBHAL*>(hpcd->pData)->ResetCallback();
|
||||
}
|
||||
|
||||
void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
|
||||
reinterpret_cast<USBHAL*>(hpcd->pData)->SOFCallback();
|
||||
}
|
||||
|
||||
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) {
|
||||
if (hpcd->Init.low_power_enable) {
|
||||
SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file usb_regs.h
|
||||
* @author MCD Application Team
|
||||
* @version V2.1.0
|
||||
* @date 19-March-2012
|
||||
* @brief hardware registers
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT 2012 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __USB_OTG_REGS_H__
|
||||
#define __USB_OTG_REGS_H__
|
||||
|
||||
typedef struct //000h
|
||||
{
|
||||
__IO uint32_t GOTGCTL; /* USB_OTG Control and Status Register 000h*/
|
||||
__IO uint32_t GOTGINT; /* USB_OTG Interrupt Register 004h*/
|
||||
__IO uint32_t GAHBCFG; /* Core AHB Configuration Register 008h*/
|
||||
__IO uint32_t GUSBCFG; /* Core USB Configuration Register 00Ch*/
|
||||
__IO uint32_t GRSTCTL; /* Core Reset Register 010h*/
|
||||
__IO uint32_t GINTSTS; /* Core Interrupt Register 014h*/
|
||||
__IO uint32_t GINTMSK; /* Core Interrupt Mask Register 018h*/
|
||||
__IO uint32_t GRXSTSR; /* Receive Sts Q Read Register 01Ch*/
|
||||
__IO uint32_t GRXSTSP; /* Receive Sts Q Read & POP Register 020h*/
|
||||
__IO uint32_t GRXFSIZ; /* Receive FIFO Size Register 024h*/
|
||||
__IO uint32_t DIEPTXF0_HNPTXFSIZ; /* EP0 / Non Periodic Tx FIFO Size Register 028h*/
|
||||
__IO uint32_t HNPTXSTS; /* Non Periodic Tx FIFO/Queue Sts reg 02Ch*/
|
||||
uint32_t Reserved30[2]; /* Reserved 030h*/
|
||||
__IO uint32_t GCCFG; /* General Purpose IO Register 038h*/
|
||||
__IO uint32_t CID; /* User ID Register 03Ch*/
|
||||
uint32_t Reserved40[48]; /* Reserved 040h-0FFh*/
|
||||
__IO uint32_t HPTXFSIZ; /* Host Periodic Tx FIFO Size Reg 100h*/
|
||||
__IO uint32_t DIEPTXF[3];/* dev Periodic Transmit FIFO */
|
||||
}
|
||||
USB_OTG_GREGS;
|
||||
|
||||
typedef struct // 800h
|
||||
{
|
||||
__IO uint32_t DCFG; /* dev Configuration Register 800h*/
|
||||
__IO uint32_t DCTL; /* dev Control Register 804h*/
|
||||
__IO uint32_t DSTS; /* dev Status Register (RO) 808h*/
|
||||
uint32_t Reserved0C; /* Reserved 80Ch*/
|
||||
__IO uint32_t DIEPMSK; /* dev IN Endpoint Mask 810h*/
|
||||
__IO uint32_t DOEPMSK; /* dev OUT Endpoint Mask 814h*/
|
||||
__IO uint32_t DAINT; /* dev All Endpoints Itr Reg 818h*/
|
||||
__IO uint32_t DAINTMSK; /* dev All Endpoints Itr Mask 81Ch*/
|
||||
uint32_t Reserved20; /* Reserved 820h*/
|
||||
uint32_t Reserved9; /* Reserved 824h*/
|
||||
__IO uint32_t DVBUSDIS; /* dev VBUS discharge Register 828h*/
|
||||
__IO uint32_t DVBUSPULSE; /* dev VBUS Pulse Register 82Ch*/
|
||||
__IO uint32_t DTHRCTL; /* dev thr 830h*/
|
||||
__IO uint32_t DIEPEMPMSK; /* dev empty msk 834h*/
|
||||
}
|
||||
USB_OTG_DREGS;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
__IO uint32_t DIEPCTL; /* dev IN Endpoint Control Reg 900h + (ep_num * 20h) + 00h*/
|
||||
uint32_t Reserved04; /* Reserved 900h + (ep_num * 20h) + 04h*/
|
||||
__IO uint32_t DIEPINT; /* dev IN Endpoint Itr Reg 900h + (ep_num * 20h) + 08h*/
|
||||
uint32_t Reserved0C; /* Reserved 900h + (ep_num * 20h) + 0Ch*/
|
||||
__IO uint32_t DIEPTSIZ; /* IN Endpoint Txfer Size 900h + (ep_num * 20h) + 10h*/
|
||||
uint32_t Reserved14;
|
||||
__IO uint32_t DTXFSTS;/*IN Endpoint Tx FIFO Status Reg 900h + (ep_num * 20h) + 18h*/
|
||||
uint32_t Reserved1C; /* Reserved 900h+(ep_num*20h)+1Ch-900h+ (ep_num * 20h) + 1Ch*/
|
||||
}
|
||||
USB_OTG_INEPREGS;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
__IO uint32_t DOEPCTL; /* dev OUT Endpoint Control Reg B00h + (ep_num * 20h) + 00h*/
|
||||
uint32_t Reserved04; /* Reserved B00h + (ep_num * 20h) + 04h*/
|
||||
__IO uint32_t DOEPINT; /* dev OUT Endpoint Itr Reg B00h + (ep_num * 20h) + 08h*/
|
||||
uint32_t Reserved0C; /* Reserved B00h + (ep_num * 20h) + 0Ch*/
|
||||
__IO uint32_t DOEPTSIZ; /* dev OUT Endpoint Txfer Size B00h + (ep_num * 20h) + 10h*/
|
||||
uint32_t Reserved14[3];
|
||||
}
|
||||
USB_OTG_OUTEPREGS;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
__IO uint32_t HCFG; /* Host Configuration Register 400h*/
|
||||
__IO uint32_t HFIR; /* Host Frame Interval Register 404h*/
|
||||
__IO uint32_t HFNUM; /* Host Frame Nbr/Frame Remaining 408h*/
|
||||
uint32_t Reserved40C; /* Reserved 40Ch*/
|
||||
__IO uint32_t HPTXSTS; /* Host Periodic Tx FIFO/ Queue Status 410h*/
|
||||
__IO uint32_t HAINT; /* Host All Channels Interrupt Register 414h*/
|
||||
__IO uint32_t HAINTMSK; /* Host All Channels Interrupt Mask 418h*/
|
||||
}
|
||||
USB_OTG_HREGS;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
__IO uint32_t HCCHAR;
|
||||
__IO uint32_t HCSPLT;
|
||||
__IO uint32_t HCINT;
|
||||
__IO uint32_t HCINTMSK;
|
||||
__IO uint32_t HCTSIZ;
|
||||
uint32_t Reserved[3];
|
||||
}
|
||||
USB_OTG_HC_REGS;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
USB_OTG_GREGS GREGS;
|
||||
uint32_t RESERVED0[188];
|
||||
USB_OTG_HREGS HREGS;
|
||||
uint32_t RESERVED1[9];
|
||||
__IO uint32_t HPRT;
|
||||
uint32_t RESERVED2[47];
|
||||
USB_OTG_HC_REGS HC_REGS[8];
|
||||
uint32_t RESERVED3[128];
|
||||
USB_OTG_DREGS DREGS;
|
||||
uint32_t RESERVED4[50];
|
||||
USB_OTG_INEPREGS INEP_REGS[4];
|
||||
uint32_t RESERVED5[96];
|
||||
USB_OTG_OUTEPREGS OUTEP_REGS[4];
|
||||
uint32_t RESERVED6[160];
|
||||
__IO uint32_t PCGCCTL;
|
||||
uint32_t RESERVED7[127];
|
||||
__IO uint32_t FIFO[4][1024];
|
||||
}
|
||||
USB_OTG_CORE_REGS;
|
||||
|
||||
|
||||
#define OTG_FS_BASE (AHB2PERIPH_BASE + 0x0000)
|
||||
#define OTG_FS ((USB_OTG_CORE_REGS *) OTG_FS_BASE)
|
||||
|
||||
#endif //__USB_OTG_REGS_H__
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
|
||||
Reference in New Issue
Block a user