` 本帖最后由 lustao 于 2019-4-3 13:15 编辑
freertos+dht11+rgy灯
Maxim带的例子有freertos的,看着研究了下,加在vTask0加了 2019-3-12 15:36:19 用彩色灯制作红绿灯程序,加在vTask1加了6 天前DHT11程序
- /*******************************************************************************
- * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
- * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- *
- * Except as contained in this notice, the name of Maxim Integrated
- * Products, Inc. shall not be used except as stated in the Maxim Integrated
- * Products, Inc. Branding Policy.
- *
- * The mere transfer of this software does not imply any licenses
- * of trade secrets, proprietary technology, copyrights, patents,
- * trademarks, maskwork rights, or any other form of intellectual
- * property whatsoever. Maxim Integrated Products, Inc. retains all
- * ownership rights.
- ********************************************************************************/
- /* **** Includes **** */
- /* config.h is the required application configuration; RAM layout, stack, chip type etc. */
- #include "mxc_config.h"
- #include "board.h"
- #include
- #include
- #include
- #include
- #include
- #include
- /* FreeRTOS */
- #include "FreeRTOS.h"
- #include "task.h"
- #include "semphr.h"
- /* FreeRTOS+ */
- #include "FreeRTOS_CLI.h"
- /* Maxim CMSIS SDK */
- #include "rtc.h"
- #include "uart.h"
- #include "lp.h"
- #include "led.h"
- #include "board.h"
- #include "pb.h"
- /* FreeRTOS+CLI */
- void vRegisterCLICommands(void);
- /* Mutual exclusion (mutex) semaphores */
- SemaphoreHandle_t xGPIOmutex;
- /* Task IDs */
- TaskHandle_t cmd_task_id;
- /* Enables/disables tick-less mode */
- unsigned int disable_tickless = 1;
- /* **** macros **** */
- /* Stringification macros */
- #define STRING(x) STRING_(x)
- #define STRING_(x) #x
- /* Console ISR selection */
- #if (CONSOLE_UART==0)
- #define UARTx_IRQHandler UART0_IRQHandler
- #define UARTx_IRQn UART0_IRQn
- #elif (CONSOLE_UART==1)
- #define UARTx_IRQHandler UART1_IRQHandler
- #define UARTx_IRQn UART1_IRQn
- #else
- #error "Please update ISR macro for UART CONSOLE_UART"
- #endif
- extern mxc_uart_regs_t *ConsoleUART; /* Set by board.c */
- gpio_cfg_t uart_rx_pin = { PORT_0, PIN_6, GPIO_FUNC_IN, GPIO_PAD_PULL_UP};
- /* **** Definitions **** */
- volatile int buttonPressed = 0;
- gpio_cfg_t dht_in = {PORT_0, PIN_4, GPIO_FUNC_IN, GPIO_PAD_NONE};
- #define SECS_PER_HR (60 * 60)
- #define SECS_PER_DAY (24 * SECS_PER_HR)
- #define MSEC_TO_RSSA(x) (0 - ((x * 256) / 1000)) /* Converts a time in milleseconds to the equivalent RSSA register value. */
- /* Array sizes */
- #define CMD_LINE_BUF_SIZE 80
- #define OUTPUT_BUF_SIZE 512
- /**
- * @brief vTask0
- * @details
- */
- void vTask0(void *pvParameters)
- {
- gpio_cfg_t gpio_out0 = {PORT_0, PIN_9, GPIO_FUNC_OUT, GPIO_PAD_NONE};
- gpio_cfg_t gpio_out1 = {PORT_0, PIN_13, GPIO_FUNC_OUT, GPIO_PAD_NONE};
- /*int count = 0;*/
- /* Setup output pin FOR led. */
- GPIO_Config(&gpio_out0);
- GPIO_Config(&gpio_out1);
- printf("
- ***** GPIO led ******
- ");
- printf(
- "1. This example outputs the same state onto P0.9 (led) and outputs the same state onto P0.13 (led).
- ");
- while (1) {
- GPIO_OutClr(&gpio_out1);
- GPIO_OutClr(&gpio_out0);
- TickType_t xLastWakeTime;
- /* Get task start time */
- xLastWakeTime = xTaskGetTickCount();
- GPIO_OutSet(&gpio_out0);
- /* Wait 1 second until next run */
- vTaskDelayUntil(&xLastWakeTime, configTICK_RATE_HZ*10); // wait for 10 seconds
- GPIO_OutClr(&gpio_out0);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- // mxc_delay(MXC_DELAY_MSEC(300));
- GPIO_OutSet(&gpio_out0);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutClr(&gpio_out0);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutSet(&gpio_out0);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutClr(&gpio_out0);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutSet(&gpio_out0);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutSet(&gpio_out1);
- xLastWakeTime = xTaskGetTickCount();
- GPIO_OutSet(&gpio_out0);
- vTaskDelayUntil(&xLastWakeTime, configTICK_RATE_HZ*2);
- //mxc_delay(MXC_DELAY_MSEC(2000));
- GPIO_OutClr(&gpio_out1);
- GPIO_OutClr(&gpio_out0);
- xLastWakeTime = xTaskGetTickCount();
- GPIO_OutSet(&gpio_out1);
- vTaskDelayUntil(&xLastWakeTime, configTICK_RATE_HZ*10); // wait for 10 seconds
- GPIO_OutClr(&gpio_out1);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutSet(&gpio_out1);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutClr(&gpio_out1);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutSet(&gpio_out1);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutClr(&gpio_out1);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutSet(&gpio_out1);
- vTaskDelay( 300 * configTICK_RATE_HZ/1000);
- GPIO_OutSet(&gpio_out1);
- xLastWakeTime = xTaskGetTickCount();
- GPIO_OutSet(&gpio_out0);
- vTaskDelayUntil(&xLastWakeTime, configTICK_RATE_HZ*2);
- //mxc_delay(MXC_DELAY_MSEC(2000));
- GPIO_OutClr(&gpio_out1);
- GPIO_OutClr(&gpio_out0);
- }
- }
- void printTime(void)
- {
- int day, hr, min, sec;
- double subsec;
- subsec = RTC_GetSubSecond() / 256.0;
- sec = RTC_GetSecond();
- day = sec / SECS_PER_DAY;
- sec -= day * SECS_PER_DAY;
- hr = sec / SECS_PER_HR;
- sec -= hr * SECS_PER_HR;
- min = sec /60;
- sec -= min * 60;
- subsec += sec;
- printf("Current Time (dd:hh:mm:ss.subs): %02d:%02d:%02d:%05.2f
- ", day, hr, min, subsec);
- }
- void MicroSeconds(int us) {
- /* Demonstrates the TMR driver delay */
- // TMR_Delay(MXC_TMR0, USEC(us), NULL); // todo check if this is μsec or msec
- uint32_t i = 0;
- for( i=0; i < us * 4; i++) {
- asm(" nop");
- }
- }
- /*从DHT11读取一个位,返回值:1/0。
- 每一bit数据都以50us的低电平时序开始,高电平的宽度决定了bit数据位的0或1,
- 高电平状态在26~28us时,表示数据位为0,高电平状态在70us时,表示数据位为1.
- DHT11_DQ_IN是读取对应端口引脚的输入电平,在读之前,该引脚要配置为上拉下拉输入模式
- */
- char DHT11_Read_Bit(void)
- {
- char retry=0;
- while(GPIO_InGet(&dht_in)&&retry<100)//读取高电平中,等待变为低电平,/*&&与<的优先级是<高于&&*/
- {
- retry++;
- MicroSeconds(1);//SysTickDelay(1);
- }
- retry=0;
- while(!GPIO_InGet(&dht_in)&&retry<100)//读取低电平中,等待变高电平
- {
- retry++;
- MicroSeconds( 1);//SysTickDelay(1);
- }
- MicroSeconds( 40);//SysTickDelay(40);//等待40us
- if(GPIO_InGet(&dht_in))return 1;
- else return 0;
- }
- char DhtReadByte(void)
- {
- char j,dat;
- dat=0;
- for(j=0;j<8;j++)
- {
- dat<<=1;
- dat|=DHT11_Read_Bit();
- }
- return(dat); }
- void vTask1(void *pvParameters)
- {
- // gpio_cfg_t dht_in = {PORT_0, PIN_4, GPIO_FUNC_IN, GPIO_PAD_NONE};
- gpio_cfg_t dht_out = {PORT_0, PIN_4, GPIO_FUNC_OUT, GPIO_PAD_NONE};
- int count = 0;
- printf("
- ***** DHT11 on P0.4 ******
- ");
- printf(
- "1. An interrupt is set up on P0.12. when that interrupt occurs.using a push button (S1) to start DHT11
- ");
- /* Wait 1 second until next run */
- vTaskDelay( configTICK_RATE_HZ);
- int i=0;
- char check; // 校验字节
- char TemHig,TemLow,HumiHig,HumiLow;
- while (1) {
- if (buttonPressed) {
- // Show the time elapsed.
- printTime();
- printf("buttonPressed count = %d
- ", count++);
- // Delay for switch debouncing.
- vTaskDelay( configTICK_RATE_HZ/10);//TMR_Delay(MXC_TMR0, MSEC(100), &sys_tmr_cfg);
- printf("configTICK_RATE_HZ = %d
- ", configTICK_RATE_HZ);
- // Re-arm switch detection.
- i++;
- buttonPressed = 0;
- GPIO_Config(&dht_out);
- GPIO_OutClr(&dht_out);
- // 主机拉低18ms DelayMs(18);
- vTaskDelay( 19* configTICK_RATE_HZ/1000);//TMR_Delay(MXC_TMR0, MSEC(18), &sys_tmr_cfg);
- vTaskSuspendAll();
- GPIO_OutSet(&dht_out);
- MicroSeconds( 20);
- GPIO_Config(&dht_in);
- xTaskResumeAll();
- vTaskDelay( 10* configTICK_RATE_HZ);//TMR_Delay(MXC_TMR0, MSEC(15000), &sys_tmr_cfg); // 主机延时15s
- GPIO_Config(&dht_out);
- GPIO_OutClr(&dht_out);
- // 主机拉低18ms DelayMs(18);
- vTaskDelay( 19* configTICK_RATE_HZ/1000);//TMR_Delay(MXC_TMR0, MSEC(18), &sys_tmr_cfg);
- vTaskSuspendAll();
- GPIO_OutSet(&dht_out);
- MicroSeconds( 20);
- GPIO_Config(&dht_in);
- while (!GPIO_InGet(&dht_in));
- while (GPIO_InGet(&dht_in));
- HumiHig = DhtReadByte();
- HumiLow = DhtReadByte();
- TemHig = DhtReadByte();
- TemLow = DhtReadByte();
- check = DhtReadByte();
- xTaskResumeAll();
-
- if(check==HumiHig + HumiLow + TemHig + TemLow)
- {printf("HumiHig = %d. %d
- ", HumiHig,HumiLow);
- printf("TemHig = %d. %d
- ", TemHig,TemLow);
- printf("check = %d
- ", check);}
- else{
- printf(" error check .count = %d
- ", count);
- printTime();
- }
- }
- else{
- vTaskDelay( 100* configTICK_RATE_HZ/1000);
- }
- }
- }
- /**
- * @brief vTickTockTask
- * @details This task writes the current RTOS tick time to the console
- */
- void vTickTockTask(void *pvParameters)
- {
- TickType_t ticks = 0;
- TickType_t xLastWakeTime;
- /* Get task start time */
- xLastWakeTime = xTaskGetTickCount();
-
- while (1) {
- ticks = xTaskGetTickCount();
- printf("Uptime is 0x%08x (%u seconds), tickless-idle is %s
- ",
- ticks, ticks / configTICK_RATE_HZ,
- disable_tickless ? "disabled" : "ENABLED");
- vTaskDelayUntil(&xLastWakeTime, (configTICK_RATE_HZ * 60));
- }
- }
- /**
- * @brief UART0_IRQHandler
- * @details This function overrides the weakly-declared interrupt handler
- * in system_max326xx.c and is needed for asynchronous UART
- * calls to work properly
- */
- void UARTx_IRQHandler(void)
- {
- UART_Handler(ConsoleUART);
- }
- /**
- * @brief vCmdLineTask_cb
- * @details Callback on asynchronous reads to wake the waiting command
- * processor task
- */
- void vCmdLineTask_cb(uart_req_t *req, int error)
- {
- BaseType_t xHigherPriorityTaskWoken;
- /* Wake the task */
- xHigherPriorityTaskWoken = pdFALSE;
- vTaskNotifyGiveFromISR(cmd_task_id, &xHigherPriorityTaskWoken);
- portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
- }
- /**
- * @brief vCmdLineTask
- * @details The command line task provides a prompt on the serial
- * interface and takes input from the user to evaluate
- * via the FreeRTOS+CLI parser.
- * @note FreeRTOS+CLI is part of FreeRTOS+ and has
- * different licensing requirements. Please see
- * http://www.freertos.org/FreeRTOS-Plus for more information
- */
- void vCmdLineTask(void *pvParameters)
- {
- unsigned char tmp;
- unsigned int index; /* Index into buffer */
- unsigned int x;
- char buffer[CMD_LINE_BUF_SIZE]; /* Buffer for input */
- char output[OUTPUT_BUF_SIZE]; /* Buffer for output */
- BaseType_t xMore;
- uart_req_t async_read_req;
-
- memset(buffer, 0, CMD_LINE_BUF_SIZE);
- index = 0;
-
- /* Register available CLI commands */
- vRegisterCLICommands();
- /* Enable UART0 interrupt */
- NVIC_ClearPendingIRQ(UARTx_IRQn);
- NVIC_DisableIRQ(UARTx_IRQn);
- NVIC_SetPriority(UARTx_IRQn, 1);
- NVIC_EnableIRQ(UARTx_IRQn);
- /* Async read will be used to wake process */
- async_read_req.data = &tmp;
- async_read_req.len = 1;
- async_read_req.callback = vCmdLineTask_cb;
-
- printf("
- Enter 'help' to view a list of available commands.
- ");
- printf("cmd> ");
- fflush(stdout);
-
- while (1) {
- /* Register async read request */
- if (UART_ReadAsync(ConsoleUART, &async_read_req) != E_NO_ERROR) {
- printf("Error registering async request. Command line unavailable.
- ");
- vTaskDelay(portMAX_DELAY);
- }
- /* Hang here until ISR wakes us for a character */
- ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
- /* Check that we have a valid character */
- if (async_read_req.num > 0) {
- /* Process character */
- do {
- if (tmp == 0x08) {
- /* Backspace */
- if (index > 0) {
- index--;
- printf("x08 x08");
- }
- fflush(stdout);
- } else if (tmp == 0x03) {
- /* ^C abort */
- index = 0;
- printf("^C");
- printf("
- cmd> ");
- fflush(stdout);
- } else if ((tmp == '
') || (tmp == ' - ')) {
- printf("
- ");
- /* Null terminate for safety */
- buffer[index] = 0x00;
- /* Evaluate */
- do {
- xMore = FreeRTOS_CLIProcessCommand(buffer, output, OUTPUT_BUF_SIZE);
- /* If xMore == pdTRUE, then output buffer contains no null termination, so
- * we know it is OUTPUT_BUF_SIZE. If pdFALSE, we can use strlen.
- */
- for (x = 0; x < (xMore == pdTRUE ? OUTPUT_BUF_SIZE : strlen(output)) ; x++) {
- putchar(*(output+x));
- }
- } while (xMore != pdFALSE);
- /* New prompt */
- index = 0;
- printf("
- cmd> ");
- fflush(stdout);
- } else if (index < CMD_LINE_BUF_SIZE) {
- putchar(tmp);
- buffer[index++] = tmp;
- fflush(stdout);
- } else {
- /* Throw away data and beep terminal */
- putchar(0x07);
- fflush(stdout);
- }
- /* If more characters are ready, process them here */
- } while ((UART_NumReadAvail(MXC_UART_GET_UART(CONSOLE_UART)) > 0) &&
- UART_Read(MXC_UART_GET_UART(CONSOLE_UART), (uint8_t *)&tmp, 1, NULL));
- }
- }
- }
- #if configUSE_TICKLESS_IDLE
- /**
- * @brief freertos_permit_tickless
- * @details Determine if any hardware activity should prevent
- * low-power tickless operation.
- */
- int freertos_permit_tickless(void)
- {
- if (disable_tickless == 1) {
- return E_BUSY;
- }
- return UART_PrepForSleep(MXC_UART_GET_UART(CONSOLE_UART));
- }
- void buttonHandler(void *pb)
- {
- buttonPressed = 1;
- }
- #endif
- void RTC_IRQHandler(void)
- {
- MXC_RTC->ctrl &= ~(MXC_F_RTC_CTRL_ALSF);
- }
- /**
- * @brief main()
- * @detials This program demonstrates FreeRTOS tasks, mutexes,
- * and the FreeRTOS+CLI extension.
- */
- int main(void)
- {
- #if configUSE_TICKLESS_IDLE
- /* The RTC must be enabled for tickless operation */
- sys_cfg_rtc_t sys_cfg;
- sys_cfg.tmr = MXC_TMR0;
- RTC_Init(MXC_RTC, 0, 0, &sys_cfg);
- RTC_EnableRTCE(MXC_RTC);
- NVIC_ClearPendingIRQ(RTC_IRQn);
- NVIC_EnableIRQ(RTC_IRQn);
- LP_EnableRTCAlarmWakeup();
- #endif
- #if configUSE_TICKLESS_IDLE
- /* Configure wake-up for GPIO pin corresponding to pushbutton */
- LP_EnableGPIOWakeup((gpio_cfg_t *)&pb_pin[0]);
- PB_RegisterCallback(0, buttonHandler);
- #endif
-
- /* Print banner (RTOS scheduler not running) */
- printf("
- -=- %s FreeRTOS (%s) Demo -=-
- ", STRING(TARGET), tskKERNEL_VERSION_NUMBER);
- #if configUSE_TICKLESS_IDLE
- printf("Tickless idle is configured. Type 'tickless 1' to enable.
- ");
- #endif
- /* Create mutexes */
- xGPIOmutex = xSemaphoreCreateMutex();
- if (xGPIOmutex == NULL) {
- printf("xSemaphoreCreateMutex failed to create a mutex.
- ");
- } else {
- /* Configure task */
- if ((xTaskCreate(vTask0, (const char *)"Task0",
- configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+1, NULL) != pdPASS) ||
- (xTaskCreate(vTask1, (const char *)"Task1",
- configMINIMAL_STACK_SIZE+128, NULL, tskIDLE_PRIORITY+1, NULL) != pdPASS) ||
- (xTaskCreate(vTickTockTask, (const char *)"TickTock",
- 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+2, NULL) != pdPASS) ||
- (xTaskCreate(vCmdLineTask, (const char *)"CmdLineTask",
- configMINIMAL_STACK_SIZE+CMD_LINE_BUF_SIZE+OUTPUT_BUF_SIZE, NULL, tskIDLE_PRIORITY+1, &cmd_task_id) != pdPASS)) {
- printf("xTaskCreate() failed to create a task.
- ");
- } else {
- /* Start scheduler */
- printf("Starting scheduler.
- ");
- vTaskStartScheduler();
- }
- }
-
- /* This code is only reached if the scheduler failed to start */
- printf("ERROR: FreeRTOS did not start due to above error!
- ");
- while (1) {
- __NOP();
- }
- /* Quiet GCC warnings */
- return -1;
- }
复制代码
检测运行成功
com
- -=- 32660 FreeRTOS (V8.2.1) Demo -=-
- Tickless idle is configured. Type 'tickless 1' to enable.
- Starting scheduler.
- Uptime is 0x00000000 (0 seconds), tickless-idle is disabled
- ***** GPIO led ******
- 1. This example outputs the same state onto P0.9 (led) and outputs the same state onto P0.13 (led).
- ***** DHT11 on P0.4 ******
- 1. An interrupt is set up on P0.12. when that interrupt occurs.using a push button (S1) to start DHT11
- Enter 'help' to view a list of available commands.
- cmd> ps
- Task State Priority Stack #
- ************************************************
- CmdLineTask R 1 449 4
- IDLE R 0 97 5
- Task0 B 1 70 1
- TickTock B 2 126 3
- Task1 B 1 192 2
- cmd> Current Time (dd:hh:mm:ss.subs): 00:00:00:23.33
- buttonPressed count = 0
- configTICK_RATE_HZ =
- HumiHig = 28. 0
- TemHig = 16. 9
- check = 53
- ps
- Task State Priority Stack #
- ************************************************
- CmdLineTask R 1 398 4
- IDLE R 0 97 5
- Task0 B 1 70 1
- TickTock B 2 126 3
- Task1 B 1 90 2
- cmd> Uptime is 0x00003c00 (60 seconds), tickless-idle is disabled
- Uptime is 0x00007800 (120 seconds), tickless-idle is disabled
- ps
- Task State Priority Stack #
- ************************************************
- CmdLineTask R 1 398 4
- IDLE R 0 97 5
- Task0 B 1 70 1
- TickTock B 2 100 3
- Task1 B 1 90 2
- cmd> Current Time (dd:hh:mm:ss.subs): 00:00:02:42.38
- buttonPressed count = 1
- configTICK_RATE_HZ =
- HumiHig = 28. 0
- TemHig = 17. 1
- check = 46
- Uptime is 0x0000b401 (180 seconds), tickless-idle is disabled
- Uptime is 0x0000f000 (240 seconds), tickless-idle is disabled
- Current Time (dd:hh:mm:ss.subs): 00:00:04:00.53
- buttonPressed count = 2
- configTICK_RATE_HZ =
- HumiHig = 29. 0
- TemHig = 17. 0
- check = 46
- Current Time (dd:hh:mm:ss.subs): 00:00:04:26.76
- buttonPressed count = 3
- configTICK_RATE_HZ =
- HumiHig = 29. 0
- TemHig = 17. 0
- check = 46
- Current Time (dd:hh:mm:ss.subs): 00:00:04:42.96
- buttonPressed count = 4
- configTICK_RATE_HZ =
- HumiHig = 47. 0
- TemHig = 17. 5
- check = 69
- Uptime is 0x00012c00 (300 seconds), tickless-idle is disabled
- Uptime is 0x00016800 (360 seconds), tickless-idle is disabled
复制代码
`
|