title: Why is the ARM CMSIS I2C reading function not working?
body: <p>So I started learning ARM-CMSIS and now I am here at learning the I2C Protocol.
  So From some tutorials and guides I now have my code like this:</p>
  <p>This is my read function:</p>
  <pre><code>uint8_t I2C_read(I2C_TypeDef *I2C, uint8_t addr, uint8_t reg) {
      uint8_t data;
      uint32_t temp;
      I2C-&gt;CR1 |= I2C_CR1_ACK;
      I2C-&gt;CR1 |= I2C_CR1_START;
      while (!(I2C-&gt;SR1 &amp; I2C_SR1_SB));
      I2C-&gt;DR = addr;
      while (!(I2C-&gt;SR1 &amp; I2C_SR1_ADDR));
      temp = I2C-&gt;SR2;
      I2C-&gt;DR = reg;
      I2C-&gt;CR1 |= I2C_CR1_START;
      while(!(I2C-&gt;SR1 &amp; I2C_SR1_SB));
      I2C-&gt;DR = (addr | 1);
      while(!(I2C-&gt;SR1 &amp; I2C_SR1_ADDR));
      temp = I2C-&gt;SR2;
      while(!(I2C-&gt;SR1 &amp; I2C_SR1_RXNE));
      I2C-&gt;CR1 &amp;= ~I2C_CR1_ACK;
      I2C-&gt;CR1 |= I2C_CR1_STOP;
      data = I2C-&gt;DR;
      printMSG(&quot;data: %d\r\n&quot;, data);
      return data;
  }
  </code></pre>
  <p>Write function:</p>
  <pre><code>void I2C_write(I2C_TypeDef *I2C, uint8_t dev_addr, uint8_t reg, uint8_t data) {
      uint32_t temp;
  
      I2C-&gt;CR1 |= I2C_CR1_START;                  // Generate Start condition
  
      while(!(I2C-&gt;SR1 &amp; I2C_FLAG_SB));           // Wait until the Start condition is sent
      printMSG(&quot;Write Test1\r\n&quot;);
      I2C-&gt;DR = dev_addr;                         // Send device over to the Date register
      while(!(I2C-&gt;SR1 &amp; I2C_SR1_ADDR));          // Wait until a Slave responds to the address
      temp = I2C-&gt;SR2;
      printMSG(&quot;Write Test2\r\n&quot;);
  
      I2C-&gt;DR = reg;                              // Send internal register to write to
      while (!(I2C-&gt;SR1 &amp; I2C_SR1_TXE));          // Wait until the Transmit register is empty and thus the data is transmitted
      printMSG(&quot;Write Test3\r\n&quot;);
  
      I2C-&gt;DR = data;                             // Put in the data you want to write to the address
      while (!(I2C-&gt;SR1 &amp; I2C_SR1_TXE));
      printMSG(&quot;Write Test4\r\n&quot;);
  
      I2C-&gt;CR1 |= I2C_CR1_STOP;
  }
  </code></pre>
  <p>And this is my I2C initialization function:</p>
  <pre><code>void I2C1_init(void) {
      GPIO_InitTypeDef gpio_init;
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
      RCC-&gt;APB1ENR |= RCC_APB1ENR_I2C1EN;
      RCC-&gt;APB2ENR |= RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN;
      gpio_init.GPIO_Mode = GPIO_Mode_AF_OD;
      gpio_init.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
      gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
      //GPIOB-&gt;ODR |= (1&lt;&lt;6) | (1&lt;&lt;7) | (1&lt;&lt;8) | (1&lt;&lt;9);
      GPIO_Init(GPIOB, &amp;gpio_init);
      I2C1-&gt;CR2 |= 36;
      I2C1-&gt;CCR |= 180;
      I2C1-&gt;TRISE |= 37;
  
      I2C1-&gt;CR1 |= I2C_CR1_ACK;
      // stretch mode enabled by default
      // 7-bit addressing mode enabled by default
      I2C1-&gt;CR1 |= I2C_CR1_PE;
  
  }
  </code></pre>
  <p>It reads the data once and then just stops and I think is stuck in one of the while loops.
  (Did not check which one)</p>
  <p>I followed the documentation of the STM32F103C8T6, I have a STM32F103C8T6 BluePill board.</p>
  <p>With the integrated CMSIS functions it does work :D</p>