<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Type struct pointer issue</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/35645/type-struct-pointer-issue</link><description> 
Hello, 
I&amp;#39;m stuck with hardfault handler. 

 
When I try to execute read value from pointer address: 

 
struct S2E_Packet { uint8_t module_type[3]; uint8_t fw_ver[3]; 
} __attribute__((packed)) S2E_Packet; 

 
struct S2E_Packet s2e_packet; 

 
AddressSrc</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/140965?ContentTypeID=1</link><pubDate>Fri, 10 Jun 2016 03:10:49 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9bb04251-775e-4b56-b11e-52ffe25ebd71</guid><dc:creator>Jakub Okaj</dc:creator><description>&lt;p&gt;&lt;p&gt;
Solution with &amp;quot;__packed uint32_t*&amp;quot; works like a charm;)&lt;br /&gt;
Now I can read memory area of struct value in 4 byte sequence and use
for flash write function.&lt;/p&gt;

&lt;p&gt;
Many thaks for all advices.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/137341?ContentTypeID=1</link><pubDate>Wed, 08 Jun 2016 11:22:16 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:729fa439-972a-47fb-aab6-296e468913b0</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;I just want to read struct value in memory area and simply
doesn&amp;#39;t care what is in the struct value.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
If that&amp;#39;s what you&amp;#39;re trying to do, how did pointers to 32-bit
integers even enter the picture? There&amp;#39;s nothing related uint32_t in
that job description. A uint32_t isn&amp;#39;t even big enough to hold all
the bytes of the existing struct.&lt;/p&gt;

&lt;p&gt;
If all you want to do is move sequences of bytes from one place to
the another, use (void *) for passing around the pointers, and
(uint8_t *) to actually access the data. That&amp;#39;s what they exist for.
There&amp;#39;s no point even thinking about (uint32_t *) for this.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/137339?ContentTypeID=1</link><pubDate>Wed, 08 Jun 2016 08:57:57 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c2f14e57-6bfe-4736-8b45-43dcf85473d4</guid><dc:creator>Robert McNamara</dc:creator><description>&lt;p&gt;&lt;pre&gt;
__attribute__((packed))   and  __packed are very similar, but not the same.

struct S2E_Packet
{
   uint8_t module_type[3];
   uint8_t fw_ver[3];
&lt;b&gt;   uint32_t uint1;   // &amp;amp;se2_Packet.uint1 gives you a (uint32_t *)&lt;/b&gt;
} __attribute__((packed)) S2E_Packet;

__packed struct S2E_Packet
{
    uint8_t module_type[3];
    uint8_t fw_ver[3];
&lt;b&gt;    uint32_t uint1;  // &amp;amp;se2_Packet.uint1 gives you a (__packed uint32_t *)&lt;/b&gt;
} S2E_Packet;


If you use the following cast, it works with aligned or un-aligned data. No Hard Fault.
&lt;b&gt;uint32_t test = *(__packed uint32_t*)AddressSrc;

&lt;/b&gt;


Google &amp;quot;Cortex-M0 data alignment&amp;quot; and you will get good information


&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/149061?ContentTypeID=1</link><pubDate>Wed, 08 Jun 2016 08:49:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:bc4173cc-e93d-4639-9b66-8f1c314713b8</guid><dc:creator>Westonsupermare Pier</dc:creator><description>&lt;p&gt;&lt;p&gt;
View a quick Google&lt;br /&gt;

&lt;a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0497a/BABFAIGG.html"&gt;infocenter.arm.com/.../index.jsp&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/137340?ContentTypeID=1</link><pubDate>Wed, 08 Jun 2016 08:37:58 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0d3308fd-338f-485a-883b-f18db7b65e0d</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Just make sure you align the data at an address evenly dividable
by 4. Or make use of a uint8_t pointer to extract the four bytes and
then combine them into a uint32_t value.&lt;/p&gt;

&lt;p&gt;
The memory controller on x86 systems is nice and hides issues with
unaligned data (except for potential bandwidth loss). But lots of
32-bit processors do not have a nice memory controller that makes two
32-bit accesses to be able to combine some bytes from the first word
and some bytes from the second word before handing over to the
processor core.&lt;/p&gt;

&lt;p&gt;
The compiler+linker will normally align the data. When the
compiler sees that data is packed, then it knows it can&amp;#39;t trust the
data to be properly aligned so it will instead produce alternative
(larger/slower) code that can handle unaligned accesses. But your
type cast throws away that attribute so the compiler assumes the
pointer points to aligned data and is safe to use as-is.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/129233?ContentTypeID=1</link><pubDate>Wed, 08 Jun 2016 08:28:13 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e51fe6e9-ba40-4eb6-9659-57f70a76d07e</guid><dc:creator>Jakub Okaj</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thanks for answer.&lt;br /&gt;
I know that maybe this kind of pointer cast is &amp;quot;brutal&amp;quot; way but I
just want to read struct value in memory area and simply doesn&amp;#39;t care
what is in the struct value.&lt;/p&gt;

&lt;p&gt;
Pier,&lt;br /&gt;
Can You tell me where I can find more about Cortex-M0 unaligned
32-bit limit?&lt;br /&gt;
It is sth new for me.&lt;/p&gt;

&lt;p&gt;
Thanks everybody for answers.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/120775?ContentTypeID=1</link><pubDate>Tue, 07 Jun 2016 15:30:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6fe05795-9782-4825-a473-1c6cf10ec365</guid><dc:creator>Westonsupermare Pier</dc:creator><description>&lt;p&gt;&lt;p&gt;
The Cortex-M0 does not permit unaligned 32-bit reads, ie ones
where the address is not divisible by 4. It will Hard Fault, so use
memcpy() to get data from an unaligned address into a variable.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/108793?ContentTypeID=1</link><pubDate>Tue, 07 Jun 2016 14:33:54 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:048d333e-a6d0-42e6-8fbb-55ffacda13cc</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;It is strange but it works when I switch to char&lt;/i&gt;&lt;br /&gt;
Not really. Welcome to the world of a CPU that actually cares about
alignment.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;Why I cannot read uint32_t type from pointer?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Because there &lt;b&gt;is no&lt;/b&gt; uint32_t object at the address you&amp;#39;ve
brutally forced that pointer to point to.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/108794?ContentTypeID=1</link><pubDate>Tue, 07 Jun 2016 14:07:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:fbcae43c-3b52-45ce-ae04-a7f16a8e2dbf</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Note that you are playing with packed data.&lt;/p&gt;

&lt;p&gt;
Then you type-cast into a pointer that does not know anything
about the data being packet. So if AddressSrc isn&amp;#39;t a 32-bit-aligned
value then you will get into troubles when reading/writing using your
pointer.&lt;/p&gt;

&lt;p&gt;
Be very, very careful when playing with type casts. Especially
that you don&amp;#39;t strip some volatile, packed or other important
attribute.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/83202?ContentTypeID=1</link><pubDate>Tue, 07 Jun 2016 09:52:56 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9929f2ea-16eb-4e75-bb34-2abe626e48ed</guid><dc:creator>Jakub Okaj</dc:creator><description>&lt;p&gt;&lt;p&gt;
Processor is STM32F072RB.&lt;br /&gt;
Fault is connected with instruction:&lt;br /&gt;
uint32_t test = *(uint32_t*)AddressSrc;&lt;/p&gt;

&lt;p&gt;
It is strange but it works when I switch to char:&lt;br /&gt;
AddressSrc=(uint32_t)&amp;amp;s2e_packet;&lt;br /&gt;
char test = *(uint32_t*)AddressSrc;&lt;br /&gt;
Now I can read first byte of struct variable.&lt;/p&gt;

&lt;p&gt;
But I need 4 byte for flash write function.;(&lt;/p&gt;

&lt;p&gt;
Why I cannot read uint32_t type from pointer?&lt;br /&gt;
What else can I check?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Type struct pointer issue</title><link>https://community.arm.com/thread/64779?ContentTypeID=1</link><pubDate>Tue, 07 Jun 2016 08:57:10 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:4f5f68db-053c-414b-a3c0-c8ab132cd84b</guid><dc:creator>Westonsupermare Pier</dc:creator><description>&lt;p&gt;&lt;p&gt;
What processor is being used?&lt;br /&gt;
Can you be more specific about what instruction is faulting, and the
register/process state at that point?&lt;br /&gt;
Can you step with a debugger?&lt;br /&gt;
Can you use a Hard Fault Handler that provides details of the
fault?&lt;br /&gt;
Compilers check for syntax errors not functional ones.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>