title: STM32 Bare Metal C - Can't get LED to work
body: <p>So I'm currently following a course for the STM32 Microprocessor however, I seem to fail at even the most basic thing: turning a LED on. The complete code is at the bottom of this post.</p>
  
  <p>Important:</p>
  
  <ul>
  <li>The hardware is functioning properly.</li>
  <li>I am using a STM32L432KC.</li>
  </ul>
  
  <p>First of all, we have to figure out on what pin the built-in LED is. According to the manufacturers manual, the LED should be on pin D13 (PB3).
  <a href="https://i.stack.imgur.com/D5C0e.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D5C0e.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>Okay so we're looking for PB3. According to the datasheet of the STM32L432KC, PB3 is on the B port and therefore connected to the high performance bus as suggested in the image below.
  <a href="https://i.stack.imgur.com/FUaGP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FUaGP.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>Cool. So our bus is AHB2 and we're working with GPIOB. Now we have to enable the clock on that bus using the RCC_AHB3ENR register. Now, this is the part where I'm probably going to make mistakes (as this post otherwise wouldn't exist), so please pay close attention. If I understand correctly, I want bit 1 to be set to 1 as this indicates that 'GPIOBEN' is set to 'IO port B clock enabled.'.
  <a href="https://i.stack.imgur.com/ro7GR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ro7GR.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/UOzpq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UOzpq.png" alt="enter image description here"></a></p>
  
  <p>This leads me to believe that I should set the bus register as follows:</p>
  
  <pre class="lang-c prettyprint-override"><code>RCC-&gt;AHB2ENR |= 0x2;
  </code></pre>
  
  <hr>
  
  <p>Next up I have to set the mode of the GPIO pin to output. According to the course and my documentation, this is done using GPIOx_MODER.
  <a href="https://i.stack.imgur.com/VmA3S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VmA3S.png" alt="enter image description here"></a></p>
  
  <p>This leads me to believe that I should set the GPIO mode as follows:</p>
  
  <pre class="lang-c prettyprint-override"><code>GPIOB-&gt;MODER |= 0x40;
  </code></pre>
  
  <hr>
  
  <p>And last but not least to turn the actual LED on we have to set the output data register which is GPIOx_ODR.
  <a href="https://i.stack.imgur.com/f45ca.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f45ca.png" alt="enter image description here"></a></p>
  
  <p>This leads me to believe that I should set the data as follows:</p>
  
  <pre class="lang-c prettyprint-override"><code>GPIOB-&gt;ODR = 0x8;
  </code></pre>
  
  <hr>
  
  <p>I'm not sure where I'm going wrong but this is the first time I'm working with registers on such a deep level. I must be overlooking something but I've tried multiple examples and had no success. All help is appreciated.
  This is the complete code:</p>
  
  <pre class="lang-c prettyprint-override"><code>// PB3 - User LED
  // RCC-&gt;AHB2ENR
  // GPIOx_MODER
  // GPIOx_ODR
  
  #include "stm32l4xx.h"
  
  int main(void)
  {
      RCC-&gt;AHB2ENR |= 0x2;
      GPIOB-&gt;MODER |= 0x40;
  
      while(1)
      {
          GPIOB-&gt;ODR = 0x8;
      }
  }
  </code></pre>