2024-02-19 07:17 AM
I generated code for STM32H563VIT6 via CubeMX with USBD MSC enabled.
When I now run the software, I get an unaligned memory access error within:
static void USBD_FrameWork_MSCDesc(USBD_DevClassHandleTypeDef *pdev,
uint32_t pConf, uint32_t *Sze)
in the MACRO:
__USBD_FRAMEWORK_SET_EP((pdev->tclasslist[pdev->classId].Eps[1].add),
(USBD_EP_TYPE_BULK),
(uint16_t)(pdev->tclasslist[pdev->classId].Eps[1].size),
(0U), (0U));
which is disassembled to:
676 __USBD_FRAMEWORK_SET_EP((pdev->tclasslist[pdev->classId].Eps[1].add),
08005d9a: adds r3, r4, r1
08005d9c: mov.w r12, #7
08005da0: strb.w r12, [r4, r1]
08005da4: movs r4, #5
08005da6: strb r4, [r3, #1]
08005da8: ldr r4, [r0, #4]
08005daa: mov.w r12, #92 ; 0x5c
08005dae: mla r4, r12, r4, r0
08005db2: ldrb.w r4, [r4, #42] ; 0x2a
08005db6: strb r4, [r3, #2]
08005db8: movs r4, #2
08005dba: strb r4, [r3, #3]
08005dbc: ldr r4, [r0, #4]
08005dbe: mla r4, r12, r4, r0
08005dc2: ldrh r4, [r4, #44] ; 0x2c
08005dc4: strh r4, [r3, #4] <- unaligned access interrupt here
08005dc6: ldrb r0, [r0, #0]
08005dc8: cmp r0, #1
The packed struct is at an unaligned memory location but the Cortex-M33 manual states that unaligned access is allowed for the strh instruction .
Does one have an Idea where this may orginate from?
Regards Pascal
2024-02-19 11:37 PM
Hi guys,
after a few more digging it seems that the bug is created by the STM32CubeMX as the "ux_device_descriptors.h" seems to orginate from here.
Currently I've work-arounded it by hand-editing the macro like:
uint16_t maxPacketSize = (epsize); \
memcpy(&pEpDesc->wMaxPacketSize, &maxPacketSize, sizeof(uint16_t)); \
Unfortunately our goal is to re-generate cubemx code during the build-process
which would not work with hand-edited / patched stuff very well.
Regards Pascal
2024-02-20 12:53 AM
Hi,
If you know, what you want to change - and sure, your right :)
-> can edit in the repo , where cube puzzles code together
-> (..)/STM32Cube/Repository/STM32Cube_FW_H5_V1.1.1/Middlewares/ST/usbx/common/usbx_device_classes/
+
Look in debug setting: whats set for :: exception on unaligned...
2024-02-20 01:03 AM
This one is not set as on your picture.
I also checked up the corresponding core-register of the cpu, the irq on unalingend is also not set here.
Nevertheless it's firing such an IRQ.
2024-02-20 02:37 AM
Hello @stst9180
You mean wMaxPacketSize
field is a 16-bit value and should be aligned on a 2-byte boundary.
pDevDesc->bMaxPacketSize = (uint16_t)USBD_MAX_EP0_SIZE;
Would you please share a failing project to confirm the issue? Also specify your toolchain being used.
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.
2024-02-21 12:07 AM
Hello FBL,
While generating the test-project i found out the following:
The issue is only appearing when the MPU is enabled in the CORTEX_M33 tab.
As this was not done by me manually, some initialization of our project seems to have done this.
The MPU settings are also attached and in the testproj.ioc
For now we may live with a completely disabled MPU.
If this is done the unaligned access does not lead to problems.
Regards
Pascal
2024-02-21 12:13 AM
I also currently see the relation to https://community.st.com/t5/stm32-mcus-products/hardfault-on-unaligned-access-after-enabling-mpu/td-p/338814 now.