2019-10-07 12:42 AM
2019-10-07 12:42 AM
Hello people,
I have the following problem,
I can send a message with the USB, but the first time I receive the following error .... the code goes to HardFalut:
void HardFault_Handler (void)
{
while (1)
{
}
}
The stack is
_Min_Heap_Size = 0x1000; / * required amount of heap * /
_Min_Stack_Size = 0x1000; / * required amount of stack * /
What do you recommend?
Thanks
Alberto
2019-10-10 08:07 AM
Hello Alberto,
could you please write more info about your project - ST example from repository or CubeMX generated? What class do you use? From which function you entered HardFault? Are you sure there is no access to uninitialized memory?
Best regards,
Lubos
2019-10-10 08:11 AM
Have a Hard Fault Handler that outputs actionable information. At the very least figure out the registers and faulting instructions to get some direction about where to look
2019-10-10 11:34 PM
Thanks for your interest,
I generated the code with
STM32CubeMX v5.3.0 and STM32CubeF1 Firmware Package V1.8.0 / 26-June-2019
The USB is set as "Mode: Device Only"
Class for FS IP: "Communication Device Class (Virtual Port Com)"
The example program is:
do {
2-> if (USBD_Interface_fops_FS.Receive(&UserRxBufferFS[0], &ulLen) == USBD_OK) {
if (ulLen != 0) {
memcpy(UserTxBufferFS, UserRxBufferFS, ulLen);
if (((USBD_CDC_HandleTypeDef*)(hUsbDeviceFS.pClassData))->TxState == 0) {
if (CDC_Transmit_FS(&UserTxBufferFS[0], ulLen) == USBD_OK) {
Nop();
}
}
}
}
} while (1);
Enter in line 2, then....
static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
/* USER CODE BEGIN 6 */
static uint16_t u16Len = 0;
*Len = strlen(Buf);
3)-> USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); <----enter
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
return (USBD_OK);
/* USER CODE END 6 */
}
Enter line 3, then....
uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff)
{
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData;
hcdc->RxBuffer = pbuff;
return USBD_OK;
} <---- when return,
when it returns then it jumps to the function HardFault_Handler
void HardFault_Handler(void)
{
while (1)
{
}
}
that's all
If I comment on the reception and fill the transmission buffer with a message, the transmission works.
Best regards,
Alberto
2019-10-11 12:35 AM
Hello Alberto,
USBD_Interface_fops_FS.Receive function, which call CDC_Receive_FS in CubeMX generated code with FS, is callback called by USB middleware when you receive data, so you don't need (and should not ) to call it in while loop in main. USB device functionality is mostly handled in interrupt in ST library, including allocation of heap space for USB structures. If you're calling your functions before space allocation in USBD_CDC_Init is complete, USBD_CDC_SetRxBuffer assign buffer to structure without allocated memory -> HF.
Please follow examples in repository or STM32 USB training to see proposed work flow.
Best regards,
Lubos
2019-10-11 01:00 AM
Thanks
I try to see what you told me.
Alberto