繁体   English   中英

在STM32上使用FreeRTOS创建从I2C的正确方法是什么?

[英]What is the correct way to create slave I2C using FreeRTOS on STM32?

我是ARM新手,尝试在STM32F103x上创建从设备。 我还需要串行线调试和FreeRTOS任务。

我通过STM32CubeMX创建了新项目。 在NVIC配置中有“使用FreeRTOS功能”的选项。 但我没有找到任何关于如何使用它的文档,参考或演示。

stm32cube_nvic_options

编辑1:所以我现在这样做:

#include <string.h>

#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(&hi2c1, (uint8_t*)&bTransferRequest, 1)!= HAL_OK)
        {
            vTaskDelay(1);
        }

        /*  Wait for I2C to get ready */
        while (HAL_I2C_GetState(&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(&hi2c1, (uint8_t*)execRxBuffer, execBufferSize)!= HAL_OK);

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

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

            /* Lets combine recieved data to owr struct */
            memcpy(&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);
    }
}

它在非阻塞模式下工作正常,但无论如何:这样做是否正确? 或者可能是使用信号量是正确的?

我可以推荐这个用户手册: 使用RTOS在STM32Cube上开发应用程序

它有关于FreeRTOS的描述有以下几点:

  • 免费的RTOS源组织
  • 在STM32上移植FreeRTOS
  • FreeRTOS API
  • FreeRTOS内存管理
  • FreeRTOS低功耗
  • FreeRTOS配置

它还有主要操作系统功能的应用示例:

  • 线程创建示例
  • 信号量示例(线程之间,来自ISR)
  • 互斥体的例子
  • 队列示例
  • 定时器的例子
  • 低功耗的例子

STM32CubeF1包中提供了一个关于线程创建的示例项目:

\STM32Cube_FW_F1_V1.4.0\Projects\STM32F103RB-Nucleo\Applications\FreeRTOS\FreeRTOS_ThreadCreation

此示例使用STM32F103RB。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM