Skip to content

Commit

Permalink
Remove USBH_malloc/free and UsbHost wrapper for it
Browse files Browse the repository at this point in the history
  • Loading branch information
danngreen committed Nov 13, 2022
1 parent f438c7e commit 4dc3400
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 32 deletions.
14 changes: 0 additions & 14 deletions examples/usb_midi_audio_host/usbh_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,6 @@ void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hpcd)
}
}

void *USBH_malloc(size_t sz)
{
static MidiStreamingHandle _mem;

if (sz == sizeof(MidiStreamingHandle))
return &_mem;

return NULL;
}

void USBH_free(void *){
// nothing, we're using static allocation
};

/*******************************************************************************
LL Driver Callbacks (HCD -> USB Host Library)
*******************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions examples/usb_midi_audio_host/usbh_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ extern "C" {
*/

/* Memory management macros */
void *USBH_malloc(size_t sz);
void USBH_free(void *ptr);
// void *USBH_malloc(size_t sz);
// void USBH_free(void *ptr);

#define USBH_memset memset
#define USBH_memcpy memcpy
Expand Down
23 changes: 8 additions & 15 deletions examples/usb_midi_audio_host/usbh_host.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ enum class EndPointType : uint8_t {
Intr = USB_EP_TYPE_INTR,
};

// Wrapper for USBH_HandleTypeDef
// That adds helper funcs for common operations
// Stateless wrapper for USBH_HandleTypeDef*
// That adds helper funcs for common operations in C functions that have a phost parameter
class USBHostHandle {
public:
USBH_HandleTypeDef *phost;
Expand All @@ -24,6 +24,9 @@ public:
: phost{phost}
{}

// Returns the user data of the active class, cast to whatever type you're expecting.
// Checks all the pointers before dereferencing them.
// Returns nullptr if it fails
template<typename HandleType>
HandleType *get_class_handle()
{
Expand All @@ -37,11 +40,14 @@ public:
return nullptr;
}

// Returns true if the endpoint specified by the interface index and endpoint index is an IN EP
bool is_in_ep(uint8_t interface, uint8_t ep_index)
{
auto addr = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[ep_index].bEndpointAddress;
return (addr & 0x80U);
}

// Returns true if the endpoint specified by the interface index and endpoint index is an OUT EP
bool is_out_ep(uint8_t interface, uint8_t ep_index) { return !is_in_ep(interface, ep_index); }

void link_endpoint_pipe(EndPoint &ep, uint8_t interface, uint8_t ep_index)
Expand Down Expand Up @@ -74,16 +80,3 @@ public:
}
}
};

// Create a new, zeroed handle
// Uses the user-defined USBH_malloc and USBH_memset
// so that we can inter-operate with other classes from the STM32 Host Library
template<typename HandleType>
HandleType *new_usbh_handle()
{
auto handle = static_cast<HandleType *>(USBH_malloc(sizeof(HandleType)));
if (handle)
USBH_memset(handle, 0, sizeof(HandleType));

return handle;
}
6 changes: 5 additions & 1 deletion examples/usb_midi_audio_host/usbh_midi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ USBH_StatusTypeDef USBH_MIDI_InterfaceInit(USBH_HandleTypeDef *phost)
USBH_StatusTypeDef status;
uint8_t interface;

// phost->pActiveClass->pData = new_usbh_handle<MidiStreamingHandle>();
// In most STM Host Lib Classes, we USBH_malloc() a class handle here.
// But in this class, we already specified it by passing it in the pData field
// of the USBH_ClassTypeDef struct passed to USBH_RegisterClass
// This allows the app to own the class handle, managing its memory as it likes
// without needing dynamic memory or static/globals/singletons
if (phost->pActiveClass->pData == nullptr) {
USBH_DbgLog("Cannot allocate memory for CDC Handle");
return USBH_FAIL;
Expand Down

0 comments on commit 4dc3400

Please sign in to comment.