What is the correct way to create slave I2C using FreeRTOS on STM32?
<p>I am new in ARM and try to create slave device on STM32F103x. Also I need Serial Wire debugging and FreeRTOS tasking.</p>

<p>I create new project by STM32CubeMX. There are options to "Use FreeRTOS functions" in NVIC configuration. But I did not find any documentation, references or demos about how to use that.</p>

<p><a href="https://i.stack.imgur.com/Nt21n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nt21n.png" alt="stm32cube_nvic_options"></a></p>

<p><strong>EDIT 1:</strong> So I make in such way for now:</p>

<pre class="lang-c prettyprint-override"><code>#include &lt;string.h&gt;

#define MASTER_REQ_READ   0x44
#define MASTER_REQ_EXEC   0x34

#define CONN_Pin GPIO_PIN_13
#define CONN_GPIO_Port GPIOC

/* Recieve protocol struct from Main controller */
struct saatProtoExec {
    uint8_t target;
    uint8_t command;
} execCommand;

uint8_t execBufferSize = sizeof(execCommand);


int main(void)
{
  /*******************************/

  /* definition and creation of i2cWait */  
  osThreadDef(i2cWait, i2cWaitForData, osPriorityRealtime, 0, 128);
  i2cWaitHandle = osThreadCreate(osThread(i2cWait), NULL);

  osKernelStart();
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  while (1)
  {
  }

}

/* i2cWaitForData function */
void i2cWaitForData(void const * argument)
{
    /* Infinite loop */
    for(;;)
    {
        while(HAL_I2C_Slave_Receive_IT(&amp;hi2c1, (uint8_t*)&amp;bTransferRequest, 1)!= HAL_OK)
        {
            vTaskDelay(1);
        }

        /*  Wait for I2C to get ready */
        while (HAL_I2C_GetState(&amp;hi2c1) != HAL_I2C_STATE_READY)
        {
            vTaskDelay(1);
        }

        /* Got command from main controller */
        if (bTransferRequest == MASTER_REQ_EXEC)
        {
            /* Recieve buffer */
            uint8_t execRxBuffer[execBufferSize];
            printf("EXEC BUFFER SIZE %d\n", execBufferSize);

            /* Got data */
            while(HAL_I2C_Slave_Receive_IT(&amp;hi2c1, (uint8_t*)execRxBuffer, execBufferSize)!= HAL_OK);

            /*  Wait for I2C to get ready */
            while (HAL_I2C_GetState(&amp;hi2c1) != HAL_I2C_STATE_READY)
            {
                vTaskDelay(1);
            }

            /* Lets print recieved RAW data */
            for (uint8_t k = 0; k &lt; sizeof(execRxBuffer); k++){
                printf("0x%X\n", execRxBuffer[k]);
            }

            /* Lets combine recieved data to owr struct */
            memcpy(&amp;execCommand, execRxBuffer, execBufferSize);

            /* Lets print data from struct */
            printf("TARGET: 0x%X\n", execCommand.target);
            printf("COMMAND: %u\n", execCommand.command);

            /* Flush Rx buffers */
            Flush_Buffer((uint8_t*)execRxBuffer, execBufferSize);

            /* Toggle LED2 */
            HAL_GPIO_TogglePin(CONN_GPIO_Port, CONN_Pin);

        }

        osDelay(1);
    }
}
</code></pre>

<p>It works fine in non-blocking mode, but anyway: is it correct to do this?
Or may be it would be right to use Semaphores?</p>