#define BEEP_NOTIFICATION_SOFTWARE_TIMERS_TASK_STACK_SIZE 128
#define BEEP_NOTIFICATION_SOFTWARE_TIMERS_TASK_PRIORITY 6
xTaskCreate((TaskFunction_t)BeepSoftwareTimers, \
(const char *)"beep_software_timers", \
(uint16_t)BEEP_NOTIFICATION_SOFTWARE_TIMERS_TASK_STACK_SIZE, \
(void *)NULL, \
(UBaseType_t)BEEP_NOTIFICATION_SOFTWARE_TIMERS_TASK_PRIORITY, \
(TaskHandle_t *)&g_beepSoftwareTimersTask_Handler);
static void BeepSoftwareTimers(void)
{
g_beepNotifyTimer_BeepOn_2ms = xTimerCreate((const char *)"beepNotification_beep_on_2ms",
(TickType_t)pdMS_TO_TICKS(beepDelayTimeToTick_2ms), // timer period
(UBaseType_t)pdFALSE, // pdTRUE - period mode, pdFALSE - one times mode
(void *)BeepSoftwareTimer_ID_1, // software timer id
(TimerCallbackFunction_t)beepNotificationSoftwareTimer_BeepOn_2ms_Callback);
if (NULL == g_beepNotifyTimer_BeepOn_2ms)
{
// TODO
}
g_beepNotifyTimer_BeepOn_100ms = xTimerCreate((const char *)"beepNotification_beep_on_100ms",
(TickType_t)pdMS_TO_TICKS(beepDelayTimeToTick_100ms), // timer period
(UBaseType_t)pdFALSE, // pdTRUE - period mode, pdFALSE - one times mode
(void *)BeepSoftwareTimer_ID_2, // software timer id
(TimerCallbackFunction_t)beepNotificationSoftwareTimer_BeepOn_100ms_Callback);
if (NULL == g_beepNotifyTimer_BeepOn_100ms)
{
}
g_generalNotify_BeepOn_100ms = xTimerCreate((const char *)"generalNotification_BeepOn_100ms",
(TickType_t)pdMS_TO_TICKS(beepDelayTimeToTick_100ms), // timer period, please do not set to too small
(UBaseType_t)pdTRUE, // pdTRUE - period mode, pdFALSE - one times mode
(void *)BeepSoftwareTimer_ID_3, // software timer id
(TimerCallbackFunction_t)GeneralNotificationSoftwareTimer_BeepOn_100ms_Callback);
if (NULL == g_generalNotify_BeepOn_100ms)
{
}
g_beepNotifyTimer_BeepOff_20ms = xTimerCreate((const char *)"beepNotification_beep_off_20ms",
(TickType_t)pdMS_TO_TICKS(beepDelayTimeToTick_20ms), // timer period
(UBaseType_t)pdFALSE, // pdTRUE - period mode, pdFALSE - one times mode
(void *)BeepSoftwareTimer_ID_4, // software timer id
(TimerCallbackFunction_t)beepNotificationSoftwareTimer_BeepOff_20ms_Callback);
if (NULL == g_beepNotifyTimer_BeepOff_20ms)
{
}
g_beepNotifyTimer_BeepOff_100ms = xTimerCreate((const char *)"beepNotification_beep_off_100ms",
(TickType_t)pdMS_TO_TICKS(beepDelayTimeToTick_100ms), // timer period
(UBaseType_t)pdFALSE, // pdTRUE - period mode, pdFALSE - one times mode
(void *)BeepSoftwareTimer_ID_5, // software timer id
(TimerCallbackFunction_t)beepNotificationSoftwareTimer_BeepOff_100ms_Callback);
if (NULL == g_beepNotifyTimer_BeepOff_100ms)
{
}
g_beepNotifyTimer_BeepOff_200ms = xTimerCreate((const char *)"beepNotification_beep_off_200ms",
(TickType_t)pdMS_TO_TICKS(beepDelayTimeToTick_100ms), // timer period
(UBaseType_t)pdFALSE, // pdTRUE - period mode, pdFALSE - one times mode
(void *)BeepSoftwareTimer_ID_6, // software timer id
(TimerCallbackFunction_t)beepNotificationSoftwareTimer_BeepOff_200ms_Callback);
if (NULL == g_beepNotifyTimer_BeepOff_200ms)
{
}
g_beepNotifyTimer_BeepOff_500ms = xTimerCreate((const char *)"beepNotification_beep_off_500ms",
(TickType_t)pdMS_TO_TICKS(beepDelayTimeToTick_100ms), // timer period
(UBaseType_t)pdFALSE, // pdTRUE - period mode, pdFALSE - one times mode
(void *)BeepSoftwareTimer_ID_7, // software timer id
(TimerCallbackFunction_t)beepNotificationSoftwareTimer_BeepOff_500ms_Callback);
if (NULL == g_beepNotifyTimer_BeepOff_500ms)
{
}
vTaskDelete(g_beepNotificationSoftwareTimersTask_Handler); // 创建完定时器后将该任务删除
}
// FreeRTOS related to soft timer configuration options
#define configUSE_TIMERS 1 // 1 - enable soft timer, 0 - disable soft timer
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 1) // soft timer priority
#define configTIMER_QUEUE_LENGTH 10 // soft timer queue len
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE * 2) // soft timer task stack size
// IO port operation macro definition, refer to <> chapter 5, page 87 ~ 92
#define BITBAND(addr, bitnum) ((addr & 0xF0000000) + 0x2000000 + ((addr & 0xFFFFF) << 5) + (bitnum << 2))
#define MEM_ADDR(addr) (*((volatile unsigned long *)(addr)))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr, n)
#define BUZZER_GPIO_PORT 8
#define BEEP_CTRL PAout(BUZZER_GPIO_PORT)
#define EVEN_NUMBER 0
#define ODD_NUMBER 1
#define PARITY_OPERATION 2
static void GeneralNotificationSoftwareTimer_BeepOn_100ms_Callback(void *parameter)
{
UNUSED(parameter);
static uint8_t g_notificationCounter = BeepSoftwareTimerCounters_Min;
g_notificationCounter++;
if (g_notificationCounter < BeepSoftwareTimerCounters_3) // you can set 3, 5, 7 ... (BeepSoftwareTimerCounters_3)
{
if (EVEN_NUMBER == (g_notificationCounter % PARITY_OPERATION))
{
BEEP_CTRL = BEEP_OFF;
}
else if (ODD_NUMBER == (g_notificationCounter % PARITY_OPERATION))
{
BEEP_CTRL = BEEP_ON;
}
}
else
{
xTimerStop(g_generalNotify_BeepOn_100ms, BeepSoftwareTimerTicksToWait_Min);
g_notificationCounter = BeepSoftwareTimerCounters_Min;
}
}
我这里配置的是循环启动定时器,每隔100ms启动一次定时器,启动定时器后,过100ms 就会进回调函数,然后在回调函数里面判断进来回调函数次数是奇数次还是偶数次,奇数次进回调就把蜂鸣器打开,偶数次就会把蜂鸣器关闭,同时计数蜂鸣器打开的次数,可以控制蜂鸣器响几次
xTimerStop(g_generalNotify_BeepOn_100ms, BeepSoftwareTimerTicksToWait_Min); // BeepSoftwareTimerTicksToWait_Min = 0,
TimerHandle_t xTimerCreate(const char * const pcTimerName,
const TickType_t xTimerPeriod,
const UBaseType_t uxAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction);
/*
* startup the timer, which create by xTimerCreate()
* xTimer: software timer handle, xTimerCreate() first parameter
* xTicksToWait: when you want to startup the timer, usually set 0
* startup success: return pdPASS
* startup failure: return errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY, errQUEUE_BLOCKED, errQUEUE_YIELD
*/
#define xTimerStart(xTimer, xTicksToWait) xTimerGenericCommand((xTimer), tmrCOMMAND_START, (xTaskGetTickCount()), NULL, (xTicksToWait))
#define xTimerStartFromISR(xTimer, pxHigherPriorityTaskWoken) xTimerGenericCommand((xTimer), tmrCOMMAND_START_FROM_ISR, (xTaskGetTickCountFromISR()), (pxHigherPriorityTaskWoken), 0U)