<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/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/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>casting pointers</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/10857/casting-pointers</link><description>Hello! 
 I&amp;#39;ve bumped into the issue with casting... 
 main.c:
static ble_gap_addr_t addr0;
static uint16_t *p_host0;

int main(void) {
 p_host0 = (void*)&amp;amp;addr0.addr[0];
 
 works fine, but... 
 beacon.c:
static ble_gap_addr_t addr1;
static</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 16 Dec 2015 12:11:38 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/10857/casting-pointers" /><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40617?ContentTypeID=1</link><pubDate>Wed, 16 Dec 2015 12:11:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6b9be715-e955-477a-8640-873ac81c014c</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;Awesome.
although gcc doesn&amp;#39;t want this pragma, it just works!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40619?ContentTypeID=1</link><pubDate>Wed, 16 Dec 2015 06:04:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8db60ecb-bcf9-41cf-8c04-72ba43d5aa8b</guid><dc:creator>monpetit</dc:creator><description>&lt;p&gt;...&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void test_something(void)
{
    p_addr = &amp;amp;addr1;
    p_mddr = (my_gap_addr_t*)&amp;amp;addr1;

    show(p_addr);
    show(p_mddr);
    printf(&amp;quot;0x%04x\r\n&amp;quot;, p_mddr-&amp;gt;host);
    puts(&amp;quot;---&amp;quot;);
    
    addr1.addr[0] = 0xab;
    addr1.addr[1] = 0xcd;
    show(p_addr);
    show(p_mddr);
    printf(&amp;quot;0x%04x\r\n&amp;quot;, p_mddr-&amp;gt;host);
    puts(&amp;quot;---&amp;quot;);
    
    const uint8_t buffer[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54 };
    memcpy((void*)addr1.addr, (const void*)buffer, sizeof(buffer));
    show(p_addr);
    show(p_mddr);
    printf(&amp;quot;0x%04x\r\n&amp;quot;, p_mddr-&amp;gt;host);
    puts(&amp;quot;---&amp;quot;);
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40618?ContentTypeID=1</link><pubDate>Wed, 16 Dec 2015 06:03:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:db9eb341-ee02-4ad4-a327-a7cd16e7eb21</guid><dc:creator>monpetit</dc:creator><description>&lt;p&gt;I have tested in another way.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#pragma anon_unions
typedef struct {
    uint8_t addr_type;                    /**&amp;lt; See @ref BLE_GAP_ADDR_TYPES. */
    union {
        uint8_t addr[BLE_GAP_ADDR_LEN];   /**&amp;lt; 48-bit address, LSB format. */
        __packed struct {
            uint16_t host;
            uint32_t net;
        };
    };
} my_gap_addr_t;


static ble_gap_addr_t addr1 = { 0x00, { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 } };
static ble_gap_addr_t *p_addr;
static my_gap_addr_t  *p_mddr;

#define show(P) \
    printf(&amp;quot;(0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x)\r\n&amp;quot;, \
        P-&amp;gt;addr[0], P-&amp;gt;addr[1], P-&amp;gt;addr[2], P-&amp;gt;addr[3], P-&amp;gt;addr[4], P-&amp;gt;addr[5])
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40620?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 19:34:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0305dca-8be5-4ae9-a48f-74c21475a6ef</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;.. even editing SDK&amp;#39;s internals will not help though.. SoftDevice expects the intact ble_gap_addr_t struct.&lt;/p&gt;
&lt;p&gt;So, the only option is to copy the address from/to SDK&amp;#39;s structs into specially aligned space:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;typedef struct {
   uint16_t extra_aligner; // required to access addr[2] by uint32_t pointer
   uint8_t addr[BLE_GAP_ADDR_LEN];
} aligned_addr __attribute__((aligned(32)));

static aligned_addr addr;    
static uint32_t *const p_net = (void*)&amp;amp;aligned_addr.addr[2];
static uint16_t *const p_host = (void*)&amp;amp;aligned_addr.addr[0];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and now it is possible to assign some (see BL specs. as per PUBLIC address!) address in the form NE:TW:OR:K_:HO:ST&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40616?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 17:01:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:95286fd1-7635-4947-adc4-8b8773b46e07</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;I tried to avoid editing SDK, but the only plausible solution I see, is to switch places of struct. members and add alignment (not sure, either it is really required):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ble_gap.h:

/**@brief Bluetooth Low Energy address. */
typedef struct
{
  uint8_t addr[BLE_GAP_ADDR_LEN];       /**&amp;lt; 48-bit address, LSB format. */
  uint8_t addr_type;                    /**&amp;lt; See @ref BLE_GAP_ADDR_TYPES. */
} ble_gap_addr_t __attribute__((aligned(16)));
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40612?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 16:28:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d3a78d2d-f44a-4a17-918a-ff21dfbc97e4</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;it will surely work, but slightly estrange me from the goal.
initial purpose of using 16 bit pointer is simplification of code.
What I need, is to distinguish a subset of addresses, which may consist of more than 255 ones (a resolution, provided by one octet).
So I decided to use 48 bit address, expressed by uint8_t array, in manner of IP: a &amp;quot;network mask&amp;quot; part, as higher 32 bits (conveniently accessed by uint32_t pointer) and &amp;quot;host&amp;quot; part - remaining 16 bits for . So not to bother myself with  traveling across 8-bit array, proposed solution had been chosen. And it is true, that the issue is solely alignment related, so, respecting the alignment restrictions of ARM arch., it will work.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40613?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 15:17:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96277c12-6757-41c6-8f7f-d95a84721582</guid><dc:creator>monpetit</dc:creator><description>&lt;p&gt;Oh I see. Then you can assign the pointer of uint8_t like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static uint8_t *p_host1;
void init_beacon(void) {
    p_host1 = &amp;amp;addr1.addr[0];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;then use it like array, p_host1[0], p_host1[1]...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40611?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 14:56:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:165a87f8-d618-4176-95da-67a48d667315</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;p_host1 holds the address of the value, I know, so when I need the value, I use *p_host1 - dereference it.
I just tried to avoid copying of memory as much as possible...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40615?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 14:51:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5292d49b-62a3-4ab6-afbf-12b6b6be3477</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;definitely it is:&lt;/p&gt;
&lt;p&gt;**Cortex-M0 Devices Generic User Guide&lt;/p&gt;
&lt;p&gt;Home &amp;gt; The Cortex-M0 Instruction Set &amp;gt; About the instruction descriptions &amp;gt; Address alignment&lt;/p&gt;
&lt;p&gt;3.3.4. Address alignment&lt;/p&gt;
&lt;p&gt;An aligned access is an operation where a word-aligned address is used for a word, or multiple word access, or where a halfword-aligned address is used for a halfword access. Byte accesses are always aligned.
There is no support for unaligned accesses on the Cortex-M0 processor. Any attempt to perform an unaligned memory access operation results in a HardFault exception.**&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40610?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 14:51:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a933a409-f18a-4aed-8d20-5b94cd11bb44</guid><dc:creator>monpetit</dc:creator><description>&lt;p&gt;Stan, it seems to me that you have misunderstood the C pointer. Your way of assigning and casting of array elements doesn&amp;#39;t fulfill your intension. The p_host1 of your code holds not &lt;em&gt;values&lt;/em&gt; of addr but the &lt;em&gt;address&lt;/em&gt; of addr. To hold values of array elements you should use &lt;code&gt;memcpy&lt;/code&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40609?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 14:33:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c4dee736-7ff6-4a7f-8a2e-06fd7d4e2e07</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;Hi monpetit,
Thank you for advice!
Does it have better performance, comparing with assignment of pointer and reading the pointed value?
I will need to compare the addr value pretty intensively, so I thought the pointer might optimize memory access a little bit - initialize the pointer once and then just dereference its value when needed...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40608?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 14:25:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:981a046c-45ee-4bd3-b6ee-ab1b8d90f7dc</guid><dc:creator>monpetit</dc:creator><description>&lt;p&gt;Stan,&lt;/p&gt;
&lt;p&gt;If you want copy 2 bytes of array elements, use &lt;code&gt;memcpy(void * destination, const void * source, size_t num)&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static uint8_t host1[2];
void init_beacon(void)
{
    memcpy((void*)host1, (void*)addr1.addr, 2);
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40614?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 14:21:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ddef233-776d-41bb-a0a7-31aadc5c76bc</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;It must be memory alignment issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40607?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 14:20:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:88d54481-808a-4bad-a319-eff00de7be49</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;I think, its rather a memory alignment issue...
when my code runs, addr1.addr has address 0x200029b5
So when I try to initialize a pointer to 32 bit value  by ..29b5 an exception seems to happen.
I tied to init a pointer by address of addr.add[3] (..29b8) and it works fine.
I saw some warnings about such or similar cases in ARM docs, but it was pretty long time ago...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40606?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 13:51:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b576766e-a938-47e5-a5d8-83f456e27bb9</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;I know, its 48 bit. I&amp;#39;m trying to cast first two bytes  of the array element of struct(.addr) to be unit16_t, so I provide the address of the first one &amp;amp;addr.add[0].&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40605?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 13:46:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3dc37a15-cab9-4959-9dcd-bd8e37334fe7</guid><dc:creator>Anders Strand</dc:creator><description>&lt;p&gt;I see. Why is the p_host1 16 bit? The address will be 6 byte = 6*8 = 48 bit. This seems to be a pure C language question. I would advice you to search stack overflow or similar sites.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40601?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 13:38:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a0b3eba4-0b45-413a-8def-9105957b2acb</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;as for the address, I just intent to use 6byte address in manner of IP to easily group/manage big net of beacons (project related).
Under hungs I mean freezes... I don&amp;#39;t have an access to SWD debugger, so my only option to debug is UART line... and I just see, that code doesn&amp;#39;t run after the assignment of the pointer in beacon.c&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40604?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 13:34:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2d609880-008a-4462-8474-89f33e2804ee</guid><dc:creator>Anders Strand</dc:creator><description>&lt;p&gt;Can you explain why you are doing this? Also explain what you mean by &amp;quot;hungs in assignment&amp;quot;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40603?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 13:32:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ccb0a720-8016-41fe-a606-d20502148eb4</guid><dc:creator>Stanislav Silnitskiy</dc:creator><description>&lt;p&gt;Hi Andres!
its not from SDK. Its my own. Looks like I miss something important...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: casting pointers</title><link>https://devzone.nordicsemi.com/thread/40602?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2015 13:30:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dbe525bc-39b0-42c9-9a8b-9012ef0e185c</guid><dc:creator>Anders Strand</dc:creator><description>&lt;p&gt;Have you found this code in the SDK?
Which example are you using?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>