title: Strange behaviour of STM32F407 while writhing to CAN_BTR register
body: <p>I defined following user type:</p>
  <pre><code>typedef struct
  {
      union
      {
          struct
          {
              uint32_t BRP :10;
              uint32_t Reserved_1 :6;
              uint32_t TS1 :4;
              uint32_t TS2 :3;
              uint32_t Reserved_2 :1;
              uint32_t SJW :2;
              uint32_t Reserved_3 :4;
              uint32_t LBKM :1;
              uint32_t SILM :1;
  
          };
          uint32_t reg;
      };
  }__attribute__((packed, aligned(1))) _CAN_BTR;
  </code></pre>
  <p>Then created variable:</p>
  <pre><code>_CAN_BTR *CAN_BTR = (_CAN_BTR*) &amp;(CAN-&gt;BTR);
  </code></pre>
  <p>But when wrighting with following code i get wrong results:</p>
  <pre><code>CAN_BTR-&gt;TS1 = 9;
  CAN_BTR-&gt;TS2 = 4;
  CAN_BTR-&gt;BRP = 6;
  CAN_BTR-&gt;SJW = 3;
  </code></pre>
  <p>At the end it seems like problem in the wrighting to register, because everithing is fine until</p>
  <pre><code>strb    r3, [r2, #2]
  </code></pre>
  <p>where <strong>r3</strong> hold correct value <strong>r2</strong> holds base address of CAN_BTR register and #2 ossfet pionts to third byte of register which holds TS1 value.</p>
  <p>After some experiments with asm i figureg out that for some reason</p>
  <pre><code>strb    r3, [r2, #2]
  </code></pre>
  <p>always modifyes first (offset 0) and third (offset 2) byte of that register. With local varible there is no such problem.</p>
  <p>CAN_BTR default value is <strong>0x1230000</strong> and aftar any of following instructons</p>
  <pre><code>strb    r3, [r2, #0]
  strb    r3, [r2, #1]
  strb    r3, [r2, #2]
  strb    r3, [r2, #3]
  </code></pre>
  <p>it became <strong>0x1290129</strong>.</p>
  <p>Is there any hardware bug or restriction which i dont know of? I checked out errata for STM32F407 but there nothing about something similar in CAN section.</p>
  <p>Thanks in advance for answers.</p>
  <p>*<strong>UPDATE</strong></p>
  <p>There is more about code - disassembly listnig:</p>
  <pre><code> 79       CAN_BTR-&gt;TS1 = 9;
  080006da:   ldr     r2, [r7, #16]
  080006dc:   ldrb    r3, [r2, #2]
  080006de:   movs    r1, #9
  080006e0:   bfi     r3, r1, #0, #4
  080006e4:   strb    r3, [r2, #2]
   80       CAN_BTR-&gt;TS2 = 4;
  080006e6:   ldr     r2, [r7, #16]
  080006e8:   ldrb    r3, [r2, #2]
  080006ea:   movs    r1, #4
  080006ec:   bfi     r3, r1, #4, #3
  080006f0:   strb    r3, [r2, #2]
   81       CAN_BTR-&gt;BRP = 6;
  080006f2:   ldr     r3, [r7, #16]
  080006f4:   movs    r2, #0
  080006f6:   orr.w   r2, r2, #6
  080006fa:   strb    r2, [r3, #0]
  080006fc:   ldrb    r2, [r3, #1]
  080006fe:   bic.w   r2, r2, #3
  08000702:   strb    r2, [r3, #1]
   82       CAN_BTR-&gt;SJW = 3;
  08000704:   ldr     r2, [r7, #16]
  08000706:   ldrb    r3, [r2, #3]
  08000708:   orr.w   r3, r3, #3
  0800070c:   strb    r3, [r2, #3]
  </code></pre>
  <p>Before first line register have default value <strong>0x1230000</strong>.
  After this line</p>
  <pre><code>080006e4:   strb    r3, [r2, #2]
  </code></pre>
  <p>register value is <strong>0x1290129</strong>.</p>
  <p>Is there any more code i need to show to illustrate my problem?</p>
  <p>*<strong>UPDATE 2</strong></p>
  <p>Now type defined as:</p>
  <pre><code>typedef struct
  {
      union
      {
          struct
          {
              uint32_t BRP :10;
              uint32_t  :6;
              uint32_t TS1 :4;
              uint32_t TS2 :3;
              uint32_t  :1;
              uint32_t SJW :2;
              uint32_t  :4;
              uint32_t LBKM :1;
              uint32_t SILM :1;
  
          };
          uint32_t reg;
      };
  } _CAN_BTR;
  </code></pre>
  <p>but this lead to identical asm code:</p>
  <pre><code> 86       CAN_BTR-&gt;TS1 = 9;
  08000718:   ldr     r2, [r7, #16]
  0800071a:   ldrb    r3, [r2, #2]
  0800071c:   movs    r1, #9
  0800071e:   bfi     r3, r1, #0, #4
  08000722:   strb    r3, [r2, #2]
  </code></pre>
  <p>*<strong>SOULTION</strong></p>
  <p>As pionetd out by <a href="https://stackoverflow.com/users/6110094/0">0___________</a> problem was in accessing CAN register as bytes, but reference manual permits only word(32bit) acess. Thanks!</p>