[En-Nut-Discussion] stm32_can.c fails to build

Uwe Bonnes bon at elektron.ikp.physik.tu-darmstadt.de
Sun Aug 19 15:30:25 CEST 2012


>>>>> "Harald" == Harald Kipp <harald.kipp at egnite.de> writes:

    Harald> I got the following problem when trying to build Nut/OS for
    Harald> STM32:
    Harald> ------------------------------------------------------------------------
    Harald> Target : stm32_can-cm3-gcc-bld Command: SET
    Harald> PATH=e:\ethernut\nut-trunk\nut\tools\win32;%ProgramFiles(x86)%\yagarto\bin;e:\apps\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;&make
    Harald> clean all install Error : 2
    Harald> ------------------------------------------------------------------------
    Harald> " [CC] ../.././nut/arch/cm3/dev/stm/stm32_can.c"
    Harald> ../.././nut/arch/cm3/dev/stm/stm32_can.c: In function
    Harald> 'StmCanSendMsg': ../.././nut/arch/cm3/dev/stm/stm32_can.c:867:5:
    Harald> error: dereferencing type-punned pointer will break
    Harald> strict-aliasing rules [-Werror=strict-aliasing]
    Harald> ../.././nut/arch/cm3/dev/stm/stm32_can.c: In function
    Harald> 'CanInput': ../.././nut/arch/cm3/dev/stm/stm32_can.c:941:5:
    Harald> error: dereferencing type-punned pointer will break
    Harald> strict-aliasing rules [-Werror=strict-aliasing] cc1.exe: all
    Harald> warnings being treated as errors

    Harald> make[1]: *** [cm3/dev/stm/stm32_can.o] Error 1 make[1]: Leaving
    Harald> directory
    Harald> `E:/ethernut/ethernut-5.0.5/stm32_can-cm3-gcc-bld/arch' make:
    Harald> *** [all] Error 2

Okay, I compiled with -O0 for easier debugging, so the alias warning didn't
appear. GCC knows about the  __attribute__((__may_alias__)). Would appended
patch be a sensible solution? It compiles to a single 32-bit access as
opposed to the other proposed solutions. Is a solution like appended
acceptable?

Where to put the __MAY_ALIAS definition?

Thanks
-- 
Uwe Bonnes                bon at elektron.ikp.physik.tu-darmstadt.de

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
diff --git a/nut/arch/cm3/dev/stm/stm32_can.c b/nut/arch/cm3/dev/stm/stm32_can.c
index e56c98f..5e91af4 100644
--- a/nut/arch/cm3/dev/stm/stm32_can.c
+++ b/nut/arch/cm3/dev/stm/stm32_can.c
@@ -849,10 +849,18 @@ static int CANGetFreeMailbox(NUTCANBUS *bus)
  * @param frame Container for CAN message to be sent
  * @return Result code. See @ref CAN_RESULT
  */
+#if defined(GCC)
+#define __MAY_ALIAS (__attribute__((__may_alias__)))
+#else
+#define __MAY_ALIAS
+#endif
+
 static int StmCanSendMsg(NUTCANBUS  *bus, CANFRAME *frame)
 {
     CAN_TypeDef *CANx = (CAN_TypeDef*)bus->bus_base;
     __IO uint32_t *CANBBx = bus->bb_base;
+    uint32_t __MAY_ALIAS *tdlr = (uint32_t*) &(frame->byte[0]);
+    uint32_t __MAY_ALIAS *tdhr = (uint32_t*) &(frame->byte[4]);
     uint32_t tir;
     CAN_TxMailBox_TypeDef *tx_mailbox;
     int index = CANGetFreeMailbox(bus);
@@ -864,8 +872,8 @@ static int StmCanSendMsg(NUTCANBUS  *bus, CANFRAME *frame)
     }
 
     tx_mailbox = &(CANx->sTxMailBox[index]);
-    tx_mailbox->TDLR = *(uint32_t*) &(frame->byte[0]);
-    tx_mailbox->TDHR = *(uint32_t*) &(frame->byte[4]);
+    tx_mailbox->TDLR = *tdlr;
+    tx_mailbox->TDHR = *tdhr;
     tx_mailbox->TDTR = frame->len;
     if (frame->ext)
     {
@@ -910,6 +918,8 @@ int CanInput(NUTCANBUS *bus, CANFRAME * frame)
 {
     CANBUSINFO *ci = bus->bus_ci;
     __IO uint32_t *CANBBx = bus->bb_base;
+    uint32_t __MAY_ALIAS *rdlr = (uint32_t*) &(frame->byte[0]);
+    uint32_t __MAY_ALIAS *rdhr = (uint32_t*) &(frame->byte[4]);
     CANBUFFER *rxbuf = &(ci->can_RxBuf);
    CAN_FIFOMailBox_TypeDef *dataPtr ;
 
@@ -938,8 +948,8 @@ int CanInput(NUTCANBUS *bus, CANFRAME * frame)
         frame->id &= 0x3ff;
     frame->ext = (dataPtr->RIR & CAN_RI0R_IDE)?1:0;
     frame->rtr = (dataPtr->RIR & CAN_RI0R_RTR)?1:0;
-    *(uint32_t*)&frame->byte[0] = dataPtr->RDLR;
-    *(uint32_t*)&frame->byte[4] = dataPtr->RDHR;
+    *rdlr = dataPtr->RDLR;
+    *rdhr = dataPtr->RDHR;
     frame->len = dataPtr->RDTR & 0xf;
     return 0;
 }


More information about the En-Nut-Discussion mailing list