2019-10-11 10:59:13 +08:00
|
|
|
#if 1
|
2019-10-11 01:54:56 +08:00
|
|
|
/*
|
|
|
|
* FreeRTOS Kernel V10.2.1
|
|
|
|
* Copyright (C) 2019 Amazon.com, Inc. or its affiliates. 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 THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS 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.
|
|
|
|
*
|
|
|
|
* http://www.FreeRTOS.org
|
|
|
|
* http://aws.amazon.com/freertos
|
|
|
|
*
|
|
|
|
* 1 tab == 4 spaces!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* FreeRTOS kernel includes. */
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <task.h>
|
|
|
|
|
2019-10-11 10:59:13 +08:00
|
|
|
/* Freedom metal driver includes. */
|
|
|
|
#include <metal/cpu.h>
|
|
|
|
#include <metal/led.h>
|
|
|
|
#include <metal/button.h>
|
|
|
|
|
|
|
|
|
2019-10-11 01:54:56 +08:00
|
|
|
/******************************************************************************
|
|
|
|
* This project provides two demo applications. A simple blinky style project,
|
|
|
|
* and a more comprehensive test and demo application. The
|
|
|
|
* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
|
|
|
|
* select between the two. The simply blinky demo is implemented and described
|
|
|
|
* in main_blinky.c. The more comprehensive test and demo application is
|
|
|
|
* implemented and described in main_full.c.
|
|
|
|
*
|
|
|
|
* This file implements the code that is not demo specific, including the
|
|
|
|
* hardware setup and standard FreeRTOS hook functions.
|
|
|
|
*
|
|
|
|
* ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
|
|
|
|
* THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
|
|
|
|
* APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#warning Also test in QEMU and add instructions above.
|
|
|
|
|
2019-10-11 10:59:13 +08:00
|
|
|
/* Index to first HART (there is only one). */
|
|
|
|
#define mainHART_0 0
|
2019-10-11 01:54:56 +08:00
|
|
|
|
|
|
|
/* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
|
|
|
|
or 0 to run the more comprehensive test and demo application. */
|
|
|
|
#define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 1
|
|
|
|
|
|
|
|
/*
|
|
|
|
* main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
|
|
|
|
* main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
|
|
|
|
*/
|
|
|
|
#if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1
|
|
|
|
extern void main_blinky( void );
|
|
|
|
#else
|
|
|
|
extern void main_full( void );
|
|
|
|
#endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
|
|
|
|
|
|
|
|
/* Prototypes for the standard FreeRTOS callback/hook functions implemented
|
|
|
|
within this file. See https://www.freertos.org/a00016.html */
|
|
|
|
void vApplicationMallocFailedHook( void );
|
|
|
|
void vApplicationIdleHook( void );
|
|
|
|
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
|
|
|
|
void vApplicationTickHook( void );
|
|
|
|
|
2019-10-11 10:59:13 +08:00
|
|
|
/* Setup the hardware to run this demo. */
|
|
|
|
static void prvSetupHardware( void );
|
2019-10-11 01:54:56 +08:00
|
|
|
|
2019-10-11 10:59:13 +08:00
|
|
|
/* Used by the Freedom Metal drivers. */
|
|
|
|
static struct metal_led *pxLED = NULL;
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------*/
|
2019-10-11 01:54:56 +08:00
|
|
|
|
|
|
|
int main( void )
|
|
|
|
{
|
2019-10-11 10:59:13 +08:00
|
|
|
prvSetupHardware();
|
|
|
|
|
2019-10-11 01:54:56 +08:00
|
|
|
/* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
|
|
|
|
of this file. */
|
|
|
|
#if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
|
|
|
|
{
|
|
|
|
main_blinky();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
main_full();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
2019-10-11 10:59:13 +08:00
|
|
|
static void prvSetupHardware( void )
|
|
|
|
{
|
|
|
|
struct metal_cpu *pxCPU;
|
|
|
|
struct metal_interrupt *pxInterruptController;
|
|
|
|
|
|
|
|
/* Initialise the red LED. */
|
|
|
|
pxLED = metal_led_get_rgb( "LD0", "red" );
|
|
|
|
configASSERT( pxLED );
|
|
|
|
metal_led_enable( pxLED );
|
|
|
|
metal_led_off( pxLED );
|
|
|
|
|
|
|
|
/* Initialise the interrupt controller. */
|
|
|
|
pxCPU = metal_cpu_get( mainHART_0 );
|
|
|
|
configASSERT( pxCPU );
|
|
|
|
pxInterruptController = metal_cpu_interrupt_controller( pxCPU );
|
|
|
|
configASSERT( pxInterruptController );
|
|
|
|
metal_interrupt_init( pxInterruptController );
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
2019-10-11 01:54:56 +08:00
|
|
|
void vApplicationMallocFailedHook( void )
|
|
|
|
{
|
|
|
|
/* vApplicationMallocFailedHook() will only be called if
|
|
|
|
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
|
|
|
|
function that will get called if a call to pvPortMalloc() fails.
|
|
|
|
pvPortMalloc() is called internally by the kernel whenever a task, queue,
|
|
|
|
timer or semaphore is created. It is also called by various parts of the
|
|
|
|
demo application. If heap_1.c or heap_2.c are used, then the size of the
|
|
|
|
heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
|
|
|
|
FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
|
|
|
|
to query the size of free heap space that remains (although it does not
|
|
|
|
provide information on how the remaining heap might be fragmented). */
|
|
|
|
taskDISABLE_INTERRUPTS();
|
|
|
|
for( ;; );
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
void vApplicationIdleHook( void )
|
|
|
|
{
|
|
|
|
/* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
|
|
|
|
to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
|
|
|
|
task. It is essential that code added to this hook function never attempts
|
|
|
|
to block in any way (for example, call xQueueReceive() with a block time
|
|
|
|
specified, or call vTaskDelay()). If the application makes use of the
|
|
|
|
vTaskDelete() API function (as this demo application does) then it is also
|
|
|
|
important that vApplicationIdleHook() is permitted to return to its calling
|
|
|
|
function, because it is the responsibility of the idle task to clean up
|
|
|
|
memory allocated by the kernel to any task that has since been deleted. */
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
|
|
|
|
{
|
|
|
|
( void ) pcTaskName;
|
|
|
|
( void ) pxTask;
|
|
|
|
|
|
|
|
/* Run time stack overflow checking is performed if
|
|
|
|
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
|
|
|
|
function is called if a stack overflow is detected. */
|
|
|
|
taskDISABLE_INTERRUPTS();
|
|
|
|
for( ;; );
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
void vApplicationTickHook( void )
|
|
|
|
{
|
|
|
|
/* The tests in the full demo expect some interaction with interrupts. */
|
|
|
|
#if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
|
|
|
|
{
|
|
|
|
extern void vFullDemoTickHook( void );
|
|
|
|
vFullDemoTickHook();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
void vAssertCalled( void )
|
|
|
|
{
|
|
|
|
taskDISABLE_INTERRUPTS();
|
2019-10-11 10:59:13 +08:00
|
|
|
for( ;; );
|
2019-10-11 01:54:56 +08:00
|
|
|
}
|
2019-10-11 10:59:13 +08:00
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
void handle_trap( void )
|
|
|
|
{
|
|
|
|
#warning Not implemented.
|
|
|
|
|
|
|
|
configASSERT( metal_cpu_get( mainHART_0 ) == 0x00 );
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
void vToggleLED( void )
|
|
|
|
{
|
|
|
|
metal_led_toggle( pxLED );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void prvSetupTimerInterrupt( void )
|
|
|
|
{
|
|
|
|
int rc, up_cnt, dn_cnt;
|
|
|
|
|
|
|
|
// Setup Timer and its interrupt so we can toggle LEDs on 1s cadence
|
|
|
|
tmr_intr = metal_cpu_timer_interrupt_controller(pxCPU);
|
|
|
|
if (tmr_intr == NULL) {
|
|
|
|
printf("TIMER interrupt controller is null.\n");
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
metal_interrupt_init(tmr_intr);
|
|
|
|
tmr_id = metal_cpu_timer_get_interrupt_id(pxCPU);
|
|
|
|
rc = metal_interrupt_register_handler(tmr_intr, tmr_id, timer_isr, pxCPU);
|
|
|
|
if (rc < 0) {
|
|
|
|
printf("TIMER interrupt handler registration failed\n");
|
|
|
|
return (rc * -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lastly CPU interrupt
|
|
|
|
if (metal_interrupt_enable(pxInterruptController, 0) == -1) {
|
|
|
|
printf("CPU interrupt enable failed\n");
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Red -> Green -> Blue, repeat
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
// Turn on RED
|
|
|
|
wait_for_timer(pxLED);
|
|
|
|
|
|
|
|
// Turn on Green
|
|
|
|
wait_for_timer(led0_green);
|
|
|
|
|
|
|
|
// Turn on Blue
|
|
|
|
wait_for_timer(led0_blue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <metal/cpu.h>
|
|
|
|
#include <metal/led.h>
|
|
|
|
#include <metal/button.h>
|
|
|
|
#include <metal/switch.h>
|
|
|
|
|
|
|
|
#define RTC_FREQ 32768
|
|
|
|
|
|
|
|
struct metal_cpu *pxCPU;
|
|
|
|
struct metal_interrupt *pxInterruptController, *tmr_intr;
|
|
|
|
int tmr_id;
|
|
|
|
volatile uint32_t timer_isr_flag;
|
|
|
|
|
|
|
|
|
|
|
|
void timer_isr (int id, void *data) {
|
|
|
|
|
|
|
|
// Disable Timer interrupt
|
|
|
|
metal_interrupt_disable(tmr_intr, tmr_id);
|
|
|
|
|
|
|
|
// Flag showing we hit timer isr
|
|
|
|
timer_isr_flag = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait_for_timer(struct metal_led *which_led) {
|
|
|
|
|
|
|
|
// clear global timer isr flag
|
|
|
|
timer_isr_flag = 0;
|
|
|
|
|
|
|
|
// Turn on desired LED
|
|
|
|
metal_led_on(which_led);
|
|
|
|
|
|
|
|
// Set timer
|
|
|
|
metal_cpu_set_mtimecmp(pxCPU, metal_cpu_get_mtime(pxCPU) + RTC_FREQ);
|
|
|
|
|
|
|
|
// Enable Timer interrupt
|
|
|
|
metal_interrupt_enable(tmr_intr, tmr_id);
|
|
|
|
|
|
|
|
// wait till timer triggers and isr is hit
|
|
|
|
while (timer_isr_flag == 0){};
|
|
|
|
|
|
|
|
timer_isr_flag = 0;
|
|
|
|
|
|
|
|
// Turn off this LED
|
|
|
|
metal_led_off(which_led);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|