2021-04-27 12:36:18 +08:00
|
|
|
/**
|
|
|
|
* @file main.c
|
2021-06-20 12:25:46 +08:00
|
|
|
* @brief
|
|
|
|
*
|
2021-04-27 12:36:18 +08:00
|
|
|
* Copyright (c) 2021 Bouffalolab team
|
2021-06-20 12:25:46 +08:00
|
|
|
*
|
2021-04-27 12:36:18 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2021-06-20 12:25:46 +08:00
|
|
|
*
|
2021-04-27 12:36:18 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2021-06-20 12:25:46 +08:00
|
|
|
*
|
2021-04-27 12:36:18 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2021-06-20 12:25:46 +08:00
|
|
|
*
|
2021-04-27 12:36:18 +08:00
|
|
|
*/
|
|
|
|
#include "hal_uart.h"
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include "semphr.h"
|
|
|
|
|
|
|
|
static uint8_t freertos_heap[4096];
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
static HeapRegion_t xHeapRegions[] = {
|
|
|
|
{ (uint8_t *)freertos_heap, 0 },
|
|
|
|
{ NULL, 0 }, /* Terminates the array. */
|
|
|
|
{ NULL, 0 } /* Terminates the array. */
|
2021-04-27 12:36:18 +08:00
|
|
|
};
|
|
|
|
static StackType_t consumer_stack[512];
|
|
|
|
static StaticTask_t consumer_handle;
|
|
|
|
static StackType_t producer_stack[512];
|
|
|
|
static StaticTask_t producer_handle;
|
|
|
|
|
|
|
|
uint8_t sharedBuf[16];
|
|
|
|
SemaphoreHandle_t sem_empty = NULL;
|
|
|
|
SemaphoreHandle_t sem_full = NULL;
|
|
|
|
SemaphoreHandle_t mtx_lock = NULL;
|
|
|
|
|
|
|
|
void vAssertCalled(void)
|
|
|
|
{
|
|
|
|
MSG("vAssertCalled\r\n");
|
2021-06-20 12:25:46 +08:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
;
|
2021-04-27 12:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void vApplicationTickHook(void)
|
|
|
|
{
|
|
|
|
//MSG("vApplicationTickHook\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void vApplicationStackOverflowHook(void)
|
|
|
|
{
|
|
|
|
MSG("vApplicationStackOverflowHook\r\n");
|
2021-06-20 12:25:46 +08:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
;
|
2021-04-27 12:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void vApplicationMallocFailedHook(void)
|
|
|
|
{
|
|
|
|
MSG("vApplicationMallocFailedHook\r\n");
|
2021-06-20 12:25:46 +08:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
;
|
2021-04-27 12:36:18 +08:00
|
|
|
}
|
|
|
|
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
|
|
|
|
{
|
|
|
|
/* If the buffers to be provided to the Idle task are declared inside this
|
|
|
|
function then they must be declared static - otherwise they will be allocated on
|
|
|
|
the stack and so not exists after this function exits. */
|
|
|
|
static StaticTask_t xIdleTaskTCB;
|
2021-06-20 12:25:46 +08:00
|
|
|
static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE];
|
2021-04-27 12:36:18 +08:00
|
|
|
|
|
|
|
/* Pass out a pointer to the StaticTask_t structure in which the Idle task's
|
|
|
|
state will be stored. */
|
|
|
|
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
|
|
|
|
|
|
|
|
/* Pass out the array that will be used as the Idle task's stack. */
|
|
|
|
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
|
|
|
|
|
|
|
|
/* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
|
|
|
|
Note that, as the array is necessarily of type StackType_t,
|
|
|
|
configMINIMAL_STACK_SIZE is specified in words, not bytes. */
|
|
|
|
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* configSUPPORT_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
|
|
|
|
application must provide an implementation of vApplicationGetTimerTaskMemory()
|
|
|
|
to provide the memory that is used by the Timer service task. */
|
|
|
|
void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize)
|
|
|
|
{
|
|
|
|
/* If the buffers to be provided to the Timer task are declared inside this
|
|
|
|
function then they must be declared static - otherwise they will be allocated on
|
|
|
|
the stack and so not exists after this function exits. */
|
|
|
|
static StaticTask_t xTimerTaskTCB;
|
2021-06-20 12:25:46 +08:00
|
|
|
static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH];
|
2021-04-27 12:36:18 +08:00
|
|
|
|
|
|
|
/* Pass out a pointer to the StaticTask_t structure in which the Timer
|
|
|
|
task's state will be stored. */
|
|
|
|
*ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
|
|
|
|
|
|
|
|
/* Pass out the array that will be used as the Timer task's stack. */
|
|
|
|
*ppxTimerTaskStackBuffer = uxTimerTaskStack;
|
|
|
|
|
|
|
|
/* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
|
|
|
|
Note that, as the array is necessarily of type StackType_t,
|
|
|
|
configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */
|
|
|
|
*pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void consumer_task(void *pvParameters)
|
|
|
|
{
|
|
|
|
MSG("Consumer task enter \r\n");
|
|
|
|
vTaskDelay(1000);
|
|
|
|
MSG("Consumer task start \r\n");
|
|
|
|
MSG("begin to loop %s\n", __FILE__);
|
2021-06-20 12:25:46 +08:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (xSemaphoreTake(sem_full, portMAX_DELAY) == pdTRUE) {
|
|
|
|
xSemaphoreTake(mtx_lock, portMAX_DELAY);
|
|
|
|
MSG("Consumer get:%s\r\n", sharedBuf);
|
2021-04-27 12:36:18 +08:00
|
|
|
xSemaphoreGive(mtx_lock);
|
|
|
|
xSemaphoreGive(sem_empty);
|
2021-06-20 12:25:46 +08:00
|
|
|
} else {
|
2021-04-27 12:36:18 +08:00
|
|
|
MSG("Take sem_full fail\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void producer_task(void *pvParameters)
|
|
|
|
{
|
|
|
|
uint8_t buf = 100;
|
2021-06-20 12:25:46 +08:00
|
|
|
|
2021-04-27 12:36:18 +08:00
|
|
|
MSG("Producer task enter \r\n");
|
|
|
|
vTaskDelay(1000);
|
|
|
|
MSG("Producer task start \r\n");
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
while (1) {
|
|
|
|
if (xSemaphoreTake(sem_empty, portMAX_DELAY) == pdTRUE) {
|
|
|
|
xSemaphoreTake(mtx_lock, portMAX_DELAY);
|
2021-04-27 12:36:18 +08:00
|
|
|
buf++;
|
2021-06-20 12:25:46 +08:00
|
|
|
sprintf((char *)sharedBuf, "%d", buf);
|
|
|
|
MSG("Producer generates:%s\r\n", sharedBuf);
|
2021-04-27 12:36:18 +08:00
|
|
|
xSemaphoreGive(mtx_lock);
|
|
|
|
xSemaphoreGive(sem_full);
|
|
|
|
vTaskDelay(buf);
|
2021-06-20 12:25:46 +08:00
|
|
|
} else {
|
2021-04-27 12:36:18 +08:00
|
|
|
MSG("Take sem_empty fail\r\n");
|
|
|
|
}
|
|
|
|
}
|
2021-06-20 12:25:46 +08:00
|
|
|
|
2021-04-27 12:36:18 +08:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
bflb_platform_init(0);
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
xHeapRegions[0].xSizeInBytes = 4096;
|
2021-04-27 12:36:18 +08:00
|
|
|
vPortDefineHeapRegions(xHeapRegions);
|
2021-06-20 12:25:46 +08:00
|
|
|
|
2021-04-27 12:36:18 +08:00
|
|
|
/* Create semaphore */
|
|
|
|
vSemaphoreCreateBinary(sem_empty);
|
|
|
|
vSemaphoreCreateBinary(sem_full);
|
|
|
|
vSemaphoreCreateBinary(mtx_lock);
|
2021-06-20 12:25:46 +08:00
|
|
|
|
|
|
|
if (sem_empty == NULL || sem_full == NULL || mtx_lock == NULL) {
|
2021-04-27 12:36:18 +08:00
|
|
|
MSG("Create sem fail\r\n");
|
|
|
|
BL_CASE_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MSG("[OS] Starting consumer task...\r\n");
|
2021-06-20 12:25:46 +08:00
|
|
|
xTaskCreateStatic(consumer_task, (char *)"consumer_task", sizeof(consumer_stack) / 4, NULL, 16, consumer_stack, &consumer_handle);
|
2021-04-27 12:36:18 +08:00
|
|
|
MSG("[OS] Starting producer task...\r\n");
|
2021-06-20 12:25:46 +08:00
|
|
|
xTaskCreateStatic(producer_task, (char *)"producer_task", sizeof(producer_stack) / 4, NULL, 15, producer_stack, &producer_handle);
|
2021-04-27 12:36:18 +08:00
|
|
|
|
|
|
|
vTaskStartScheduler();
|
|
|
|
|
2021-07-01 19:31:37 +08:00
|
|
|
BL_CASE_SUCCESS;
|
2021-06-20 12:25:46 +08:00
|
|
|
while (1) {
|
2021-07-01 19:31:37 +08:00
|
|
|
bflb_platform_delay_ms(100);
|
2021-04-27 12:36:18 +08:00
|
|
|
}
|
|
|
|
}
|