【发布时间】:2021-08-17 17:03:46
【问题描述】:
我决定尝试使用带有 Arduino Uno (ATmega 328p) 的 FreeRTOS 并从定时器挂钩闪烁 LED。但是,似乎我在 FreeRTOS 中设置了错误的计时器,并且找不到错误。我认为 LED 会每 1 秒开/关一次(频率为 1 kHz,计数器限制为 1000),但大约是 14 秒。有什么问题?
这是我的 main.c
#include <avr/io.h>
#include "FreeRTOS.h"
#include "task.h"
#define hookTICK_CALLS_BEFORE_POST ( 1000 )
void vApplicationTickHook( void );
static uint16_t uxCallCounter = 0;
void vApplicationTickHook( void ){
uxCallCounter++;
if( uxCallCounter >= hookTICK_CALLS_BEFORE_POST ){
uxCallCounter = 0;
PORTD ^= 1 << 2;
}
}
int main(void)
{
DDRD |= 1 << 2;
/* Replace with your application code */
vTaskStartScheduler();
while (1)
{
}
}
这是 FreeRTOSConfig.h
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 1
#define configCPU_CLOCK_HZ ( ( unsigned long ) 8000000 ) /*According to fuses, 0xFF 0xDA 0xFD mean using external oscillator at 16MHz with internal set to 8MHz */
#define configTICK_RATE_HZ ( (portTickType ) 1000 )
#define configMAX_PRIORITIES ( 4 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 85 )
#define configTOTAL_HEAP_SIZE ( (size_t ) ( 1500 ) )
#define configMAX_TASK_NAME_LEN ( 8 )
#define configUSE_TRACE_FACILITY 0
#define configUSE_16_BIT_TICKS 1
#define configIDLE_SHOULD_YIELD 0
#define configQUEUE_REGISTRY_SIZE 0
/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 0
#define INCLUDE_uxTaskPriorityGet 0
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 0
#define INCLUDE_vTaskSuspend 0
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
构建选项(Atmel studio 7)
-x c -funsigned-char -funsigned-bitfields -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.6.364\include" -I"../FreeRTOS" -I"../FreeRTOS/Source/include" -I"../FreeRTOS/Source/Portable/Thirdparty/GCC/ATmega" -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -Wall -mmcu=atmega328p -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\ATmega_DFP\1.6.364\gcc\dev\atmega328p" -c -std=gnu99 -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)"
【问题讨论】:
-
你没有显示初始化代码,所以我们不知道什么你实际设置的频率[以及什么你想要的滴答频率]。显然,它比您认为的频率低 14 倍。代替修复它,如何(例如):
#define hookTICK_CALLS_BEFORE_POST ( 1000 / 14 ) -
freeRTOS 和 2k uC 没有太大意义。 IMO 购买 STM32 Nucleo 板并在 ARM Cortex 机器上开始使用 freeRTOS
-
我使用 AVR 进行调试:我只是更了解 avr 汇编器,因此我可以更轻松地跟踪编译器优化。所以我可以更确定我没有搞砸 const/static/volatile。