cancel
Showing results for 
Search instead for 
Did you mean: 

USBX HOST printer example

ABasi.2
Senior

Hi!

i have managed to port the exisiting example of USBX device printer to the H563 micro and use it.

now i'm trying to do the same thing with the USBX HOST printer class.

but there are two problems:

1) there isn't an example

2) if i create a new project and select USBX HOST printer i think there are some missing files in the cube generation

for the device printer class  the cube generate  app_usbx_device.c app_usbx_device.h ux_device_printer.c and ux_device_printer.h 

for the Host printer the cude generate only app_usbx_host.c and app_usbx_host.h

am i missing something?

my cube ide is 1.18.0 release

 

thank you !

1 ACCEPTED SOLUTION

Accepted Solutions
FBL
ST Employee

Hi @ABasi.2 

So far, no example is provided to cover Printer class. CubeMX imports Middleware files and leaves it to developer to tailor their application in app_usbx_host.c/h. Here is a suggestion to implement functions to initialize and communicate with the printer.

/* * ux_host_printer.c * */ #include "ux_host_printer.h" #include "ux_api.h" typedef struct { UX_HOST_CLASS_PRINTER *pPrinterClass; } PrinterData_t; static PrinterData_t sgrPrinterData; bool ux_host_printer_IsPrinterConnected(void) { bool bReturn = false; if (sgrPrinterData.pPrinterClass != NULL) { bReturn = true; } else { /* No printer connected */ } return bReturn; } bool ux_host_printer_Send(uint8_t *_pData, uint32_t _iLength) { bool bReturn = false; uint32_t iSend; if ((sgrPrinterData.pPrinterClass != NULL) && (_pData != NULL)) { if (ux_host_class_printer_write(sgrPrinterData.pPrinterClass, _pData, _iLength, &iSend) == UX_SUCCESS) { if (iSend == _iLength) { bReturn = true; } else { /* Not all data has been sent to the printer */ } } else { /* Failed to send data to the printer */ } } else { /* No printer available or invalid parameter */ } return bReturn; } bool ux_host_printer_Receive(uint8_t *_pData, uint32_t *_pReceivedLength, uint32_t _iRequestedLength) { bool bReturn = false; if ((sgrPrinterData.pPrinterClass != NULL) && (_pData != NULL) && (_pReceivedLength != NULL) && (_iRequestedLength > 0uL)) { if (ux_host_class_printer_read(sgrPrinterData.pPrinterClass, _pData, _iRequestedLength, _pReceivedLength) == UX_SUCCESS) { bReturn = true; } else { /* Failed to read data from printer */ } } else { /* No printer available or invalid parameter */ } return bReturn; } void ux_host_printer_AddPrinter(UX_HOST_CLASS_PRINTER *_pPrinterClass) { if (_pPrinterClass != NULL) { sgrPrinterData.pPrinterClass = _pPrinterClass; } else { /* Invalid parameter */ } } void ux_host_printer_RemovePrinter(void) { sgrPrinterData.pPrinterClass = NULL; }
View more

Then, ensure your header file declares the necessary functions.

/* * ux_host_printer.h * */ #include <stdbool.h> #include <stdint.h> #include "ux_api.h" #include "ux_host_class_printer.h" bool ux_host_printer_IsPrinterConnected(void); bool ux_host_printer_Send(uint8_t *_pData, uint32_t _iLength); bool ux_host_printer_Receive(uint8_t *_pData, uint32_t *_pReceivedLength, uint32_t _iRequestedLength); void ux_host_printer_AddPrinter(UX_HOST_CLASS_PRINTER *_pPrinterClass); void ux_host_printer_RemovePrinter(void);

 

When ux_host_event_callback is called in app_usbx_host.c, handle device insertion and removal as follows:

switch (event) { case UX_DEVICE_INSERTION: /* USER CODE BEGIN UX_DEVICE_INSERTION */ if (current_class -> ux_host_class_entry_function == ux_host_class_printer_entry) { ux_host_printer_AddPrinter((UX_HOST_CLASS_PRINTER *)current_instance); } /* USER CODE END UX_DEVICE_INSERTION */ break; case UX_DEVICE_REMOVAL: /* USER CODE BEGIN UX_DEVICE_REMOVAL */ if (current_class -> ux_host_class_entry_function == ux_host_class_printer_entry) { ux_host_printer_RemovePrinter(); } else { /* Another device is removed */ } /* USER CODE END UX_DEVICE_REMOVAL */ break;

In MX_USBX_Host_Init, initialize host printer class after /* USER CODE BEGIN MX_USBX_Host_Init1 */ 

/* Initialize the host printer class */ if (ux_host_stack_class_register(_ux_system_host_class_printer_name, ux_host_class_printer_entry) != UX_SUCCESS) { /* USER CODE BEGIN USBX_HOST_STORAGE_REGISTER_ERROR */ return UX_ERROR; /* USER CODE END USBX_HOST_STORAGE_REGISTER_ERROR */ }

I hope this helps! An internal ticket 208191 is submitted to CubeMX to add ux_host_printer.c and ux_host_printer.h in CubeMX generated code.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


View solution in original post

2 REPLIES 2
FBL
ST Employee

Hi @ABasi.2 

So far, no example is provided to cover Printer class. CubeMX imports Middleware files and leaves it to developer to tailor their application in app_usbx_host.c/h. Here is a suggestion to implement functions to initialize and communicate with the printer.

/* * ux_host_printer.c * */ #include "ux_host_printer.h" #include "ux_api.h" typedef struct { UX_HOST_CLASS_PRINTER *pPrinterClass; } PrinterData_t; static PrinterData_t sgrPrinterData; bool ux_host_printer_IsPrinterConnected(void) { bool bReturn = false; if (sgrPrinterData.pPrinterClass != NULL) { bReturn = true; } else { /* No printer connected */ } return bReturn; } bool ux_host_printer_Send(uint8_t *_pData, uint32_t _iLength) { bool bReturn = false; uint32_t iSend; if ((sgrPrinterData.pPrinterClass != NULL) && (_pData != NULL)) { if (ux_host_class_printer_write(sgrPrinterData.pPrinterClass, _pData, _iLength, &iSend) == UX_SUCCESS) { if (iSend == _iLength) { bReturn = true; } else { /* Not all data has been sent to the printer */ } } else { /* Failed to send data to the printer */ } } else { /* No printer available or invalid parameter */ } return bReturn; } bool ux_host_printer_Receive(uint8_t *_pData, uint32_t *_pReceivedLength, uint32_t _iRequestedLength) { bool bReturn = false; if ((sgrPrinterData.pPrinterClass != NULL) && (_pData != NULL) && (_pReceivedLength != NULL) && (_iRequestedLength > 0uL)) { if (ux_host_class_printer_read(sgrPrinterData.pPrinterClass, _pData, _iRequestedLength, _pReceivedLength) == UX_SUCCESS) { bReturn = true; } else { /* Failed to read data from printer */ } } else { /* No printer available or invalid parameter */ } return bReturn; } void ux_host_printer_AddPrinter(UX_HOST_CLASS_PRINTER *_pPrinterClass) { if (_pPrinterClass != NULL) { sgrPrinterData.pPrinterClass = _pPrinterClass; } else { /* Invalid parameter */ } } void ux_host_printer_RemovePrinter(void) { sgrPrinterData.pPrinterClass = NULL; }
View more

Then, ensure your header file declares the necessary functions.

/* * ux_host_printer.h * */ #include <stdbool.h> #include <stdint.h> #include "ux_api.h" #include "ux_host_class_printer.h" bool ux_host_printer_IsPrinterConnected(void); bool ux_host_printer_Send(uint8_t *_pData, uint32_t _iLength); bool ux_host_printer_Receive(uint8_t *_pData, uint32_t *_pReceivedLength, uint32_t _iRequestedLength); void ux_host_printer_AddPrinter(UX_HOST_CLASS_PRINTER *_pPrinterClass); void ux_host_printer_RemovePrinter(void);

 

When ux_host_event_callback is called in app_usbx_host.c, handle device insertion and removal as follows:

switch (event) { case UX_DEVICE_INSERTION: /* USER CODE BEGIN UX_DEVICE_INSERTION */ if (current_class -> ux_host_class_entry_function == ux_host_class_printer_entry) { ux_host_printer_AddPrinter((UX_HOST_CLASS_PRINTER *)current_instance); } /* USER CODE END UX_DEVICE_INSERTION */ break; case UX_DEVICE_REMOVAL: /* USER CODE BEGIN UX_DEVICE_REMOVAL */ if (current_class -> ux_host_class_entry_function == ux_host_class_printer_entry) { ux_host_printer_RemovePrinter(); } else { /* Another device is removed */ } /* USER CODE END UX_DEVICE_REMOVAL */ break;

In MX_USBX_Host_Init, initialize host printer class after /* USER CODE BEGIN MX_USBX_Host_Init1 */ 

/* Initialize the host printer class */ if (ux_host_stack_class_register(_ux_system_host_class_printer_name, ux_host_class_printer_entry) != UX_SUCCESS) { /* USER CODE BEGIN USBX_HOST_STORAGE_REGISTER_ERROR */ return UX_ERROR; /* USER CODE END USBX_HOST_STORAGE_REGISTER_ERROR */ }

I hope this helps! An internal ticket 208191 is submitted to CubeMX to add ux_host_printer.c and ux_host_printer.h in CubeMX generated code.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


ABasi.2
Senior

Hello FBL

thank you for your kinde response and for the code

 

i will try!

best reguards!