Files
charge-controller-firmware/lib/USBDevice/USBDevice.h
T
Martin Jäger 112f36b08b 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
2019-02-16 09:53:36 +01:00

272 lines
8.2 KiB
C++

/* 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