--- /dev/null
+/*\r
+ FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
+\r
+ This file is part of the FreeRTOS distribution.\r
+\r
+ FreeRTOS is free software; you can redistribute it and/or modify it under \r
+ the terms of the GNU General Public License (version 2) as published by the \r
+ Free Software Foundation and modified by the FreeRTOS exception.\r
+ **NOTE** The exception to the GPL is included to allow you to distribute a\r
+ combined work that includes FreeRTOS without being obliged to provide the \r
+ source code for proprietary components outside of the FreeRTOS kernel. \r
+ Alternative commercial license and support terms are also available upon \r
+ request. See the licensing section of http://www.FreeRTOS.org for full \r
+ license details.\r
+\r
+ FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\r
+ more details.\r
+\r
+ You should have received a copy of the GNU General Public License along\r
+ with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
+ Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
+\r
+\r
+ ***************************************************************************\r
+ * *\r
+ * Looking for a quick start? Then check out the FreeRTOS eBook! *\r
+ * See http://www.FreeRTOS.org/Documentation for details *\r
+ * *\r
+ ***************************************************************************\r
+\r
+ 1 tab == 4 spaces!\r
+\r
+ Please ensure to read the configuration and relevant port sections of the\r
+ online documentation.\r
+\r
+ http://www.FreeRTOS.org - Documentation, latest information, license and\r
+ contact details.\r
+\r
+ http://www.SafeRTOS.com - A version that is certified for use in safety\r
+ critical systems.\r
+\r
+ http://www.OpenRTOS.com - Commercial support, development, porting,\r
+ licensing and training services.\r
+*/\r
+\r
+/*-----------------------------------------------------------\r
+ * Implementation of functions defined in portable.h for the NIOS2 port.\r
+ *----------------------------------------------------------*/\r
+\r
+/* Standard Includes. */\r
+#include <string.h>\r
+#include <errno.h>\r
+\r
+/* Altera includes. */\r
+#include "sys/alt_irq.h"\r
+#include "altera_avalon_timer_regs.h"\r
+#include "priv/alt_irq_table.h"\r
+\r
+/* Scheduler includes. */\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+\r
+/* Interrupts are enabled. */\r
+#define portINITIAL_ESTATUS ( portSTACK_TYPE ) 0x01 \r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* \r
+ * Setup the timer to generate the tick interrupts.\r
+ */\r
+static void prvSetupTimerInterrupt( void );\r
+\r
+/*\r
+ * Call back for the alarm function.\r
+ */\r
+void vPortSysTickHandler( void * context, alt_u32 id );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void prvReadGp( unsigned portLONG *ulValue )\r
+{ \r
+ asm( "stw gp, (r4) " );\r
+};\r
+/*-----------------------------------------------------------*/\r
+\r
+/* \r
+ * See header file for description. \r
+ */\r
+portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
+{ \r
+portSTACK_TYPE *pxFramePointer = pxTopOfStack - 1;\r
+portSTACK_TYPE xGlobalPointer;\r
+\r
+ prvReadGp( &xGlobalPointer ); \r
+\r
+ /* End of stack marker. */\r
+ *pxTopOfStack = 0xdeadbeef;\r
+ pxTopOfStack--;\r
+ \r
+ *pxTopOfStack = ( portSTACK_TYPE ) pxFramePointer; \r
+ pxTopOfStack--;\r
+ \r
+ *pxTopOfStack = xGlobalPointer; \r
+ \r
+ /* Space for R23 to R16. */\r
+ pxTopOfStack -= 9;\r
+\r
+ *pxTopOfStack = ( portSTACK_TYPE ) pxCode; \r
+ pxTopOfStack--;\r
+\r
+ *pxTopOfStack = portINITIAL_ESTATUS; \r
+\r
+ /* Space for R15 to R5. */ \r
+ pxTopOfStack -= 12;\r
+ \r
+ *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; \r
+\r
+ /* Space for R3 to R1, muldiv and RA. */\r
+ pxTopOfStack -= 5;\r
+ \r
+ return pxTopOfStack;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/* \r
+ * See header file for description. \r
+ */\r
+portBASE_TYPE xPortStartScheduler( void )\r
+{\r
+ /* Start the timer that generates the tick ISR. Interrupts are disabled\r
+ here already. */\r
+ prvSetupTimerInterrupt();\r
+ \r
+ /* Start the first task. */\r
+ asm volatile ( " movia r2, restore_sp_from_pxCurrentTCB \n"\r
+ " jmp r2 " );\r
+\r
+ /* Should not get here! */\r
+ return 0;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortEndScheduler( void )\r
+{\r
+ /* It is unlikely that the NIOS2 port will require this function as there\r
+ is nothing to return to. */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/*\r
+ * Setup the systick timer to generate the tick interrupts at the required\r
+ * frequency.\r
+ */\r
+void prvSetupTimerInterrupt( void )\r
+{\r
+ /* Try to register the interrupt handler. */\r
+ if ( -EINVAL == alt_irq_register( SYS_CLK_IRQ, 0x0, vPortSysTickHandler ) )\r
+ { \r
+ /* Failed to install the Interrupt Handler. */\r
+ asm( "break" );\r
+ }\r
+ else\r
+ {\r
+ /* Configure SysTick to interrupt at the requested rate. */\r
+ IOWR_ALTERA_AVALON_TIMER_CONTROL( SYS_CLK_BASE, ALTERA_AVALON_TIMER_CONTROL_STOP_MSK );\r
+ IOWR_ALTERA_AVALON_TIMER_PERIODL( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) & 0xFFFF );\r
+ IOWR_ALTERA_AVALON_TIMER_PERIODH( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) >> 16 );\r
+ IOWR_ALTERA_AVALON_TIMER_CONTROL( SYS_CLK_BASE, ALTERA_AVALON_TIMER_CONTROL_CONT_MSK | ALTERA_AVALON_TIMER_CONTROL_START_MSK | ALTERA_AVALON_TIMER_CONTROL_ITO_MSK ); \r
+ } \r
+\r
+ /* Clear any already pending interrupts generated by the Timer. */\r
+ IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortSysTickHandler( void * context, alt_u32 id )\r
+{\r
+ /* Increment the Kernel Tick. */\r
+ vTaskIncrementTick();\r
+\r
+ /* If using preemption, also force a context switch. */\r
+ #if configUSE_PREEMPTION == 1\r
+ vTaskSwitchContext();\r
+ #endif\r
+\r
+ /* Clear the interrupt. */\r
+ IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/** This function is a re-implementation of the Altera provided function.\r
+ * The function is re-implemented to prevent it from enabling an interrupt\r
+ * when it is registered. Interrupts should only be enabled after the FreeRTOS.org\r
+ * kernel has its scheduler started so that contexts are saved and switched \r
+ * correctly.\r
+ */\r
+int alt_irq_register( alt_u32 id, void* context, void (*handler)(void*, alt_u32) )\r
+{\r
+ int rc = -EINVAL; \r
+ alt_irq_context status;\r
+\r
+ if (id < ALT_NIRQ)\r
+ {\r
+ /* \r
+ * interrupts are disabled while the handler tables are updated to ensure\r
+ * that an interrupt doesn't occur while the tables are in an inconsistant\r
+ * state.\r
+ */\r
+ \r
+ status = alt_irq_disable_all ();\r
+ \r
+ alt_irq[id].handler = handler;\r
+ alt_irq[id].context = context;\r
+ \r
+ rc = (handler) ? alt_irq_enable (id): alt_irq_disable (id);\r
+ \r
+ /* alt_irq_enable_all(status); This line is removed to prevent the interrupt from being immediately enabled. */\r
+ }\r
+ \r
+ return rc; \r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
--- /dev/null
+/*\r
+ FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
+\r
+ This file is part of the FreeRTOS distribution.\r
+\r
+ FreeRTOS is free software; you can redistribute it and/or modify it under \r
+ the terms of the GNU General Public License (version 2) as published by the \r
+ Free Software Foundation and modified by the FreeRTOS exception.\r
+ **NOTE** The exception to the GPL is included to allow you to distribute a\r
+ combined work that includes FreeRTOS without being obliged to provide the \r
+ source code for proprietary components outside of the FreeRTOS kernel. \r
+ Alternative commercial license and support terms are also available upon \r
+ request. See the licensing section of http://www.FreeRTOS.org for full \r
+ license details.\r
+\r
+ FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\r
+ more details.\r
+\r
+ You should have received a copy of the GNU General Public License along\r
+ with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
+ Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
+\r
+\r
+ ***************************************************************************\r
+ * *\r
+ * Looking for a quick start? Then check out the FreeRTOS eBook! *\r
+ * See http://www.FreeRTOS.org/Documentation for details *\r
+ * *\r
+ ***************************************************************************\r
+\r
+ 1 tab == 4 spaces!\r
+\r
+ Please ensure to read the configuration and relevant port sections of the\r
+ online documentation.\r
+\r
+ http://www.FreeRTOS.org - Documentation, latest information, license and\r
+ contact details.\r
+\r
+ http://www.SafeRTOS.com - A version that is certified for use in safety\r
+ critical systems.\r
+\r
+ http://www.OpenRTOS.com - Commercial support, development, porting,\r
+ licensing and training services.\r
+*/\r
+\r
+.extern vTaskSwitchContext\r
+ \r
+.set noat\r
+\r
+# Exported to start the first task.\r
+.globl restore_sp_from_pxCurrentTCB \r
+ \r
+# Entry point for exceptions.\r
+.section .exceptions.entry, "xa" \r
+\r
+# Save the entire context of a task.\r
+save_context:\r
+ addi ea, ea, -4 # Point to the next instruction.\r
+ addi sp, sp, -116 # Create space on the stack.\r
+ stw ra, 0(sp)\r
+ # Leave a gap for muldiv 0\r
+ stw at, 8(sp) \r
+ stw r2, 12(sp)\r
+ stw r3, 16(sp)\r
+ stw r4, 20(sp)\r
+ stw r5, 24(sp) \r
+ stw r6, 28(sp) \r
+ stw r7, 32(sp) \r
+ stw r8, 36(sp) \r
+ stw r9, 40(sp) \r
+ stw r10, 44(sp)\r
+ stw r11, 48(sp)\r
+ stw r12, 52(sp)\r
+ stw r13, 56(sp)\r
+ stw r14, 60(sp)\r
+ stw r15, 64(sp)\r
+ rdctl r5, estatus # Save the eStatus\r
+ stw r5, 68(sp)\r
+ stw ea, 72(sp) # Save the PC\r
+ stw r16, 76(sp) # Save the remaining registers\r
+ stw r17, 80(sp)\r
+ stw r18, 84(sp)\r
+ stw r19, 88(sp)\r
+ stw r20, 92(sp)\r
+ stw r21, 96(sp)\r
+ stw r22, 100(sp)\r
+ stw r23, 104(sp)\r
+ stw gp, 108(sp)\r
+ stw fp, 112(sp)\r
+\r
+save_sp_to_pxCurrentTCB:\r
+ movia et, pxCurrentTCB # Load the address of the pxCurrentTCB pointer\r
+ ldw et, (et) # Load the value of the pxCurrentTCB pointer\r
+ stw sp, (et) # Store the stack pointer into the top of the TCB\r
+ \r
+ .section .exceptions.irqtest, "xa" \r
+hw_irq_test:\r
+ /*\r
+ * Test to see if the exception was a software exception or caused \r
+ * by an external interrupt, and vector accordingly.\r
+ */\r
+ rdctl r4, ipending # Load the Pending Interrupts indication\r
+ rdctl r5, estatus # Load the eStatus (enabled interrupts).\r
+ andi r2, r5, 1 # Are interrupts enabled globally.\r
+ beq r2, zero, soft_exceptions # Interrupts are not enabled.\r
+ beq r4, zero, soft_exceptions # There are no interrupts triggered.\r
+\r
+ .section .exceptions.irqhandler, "xa"\r
+hw_irq_handler:\r
+ call alt_irq_handler # Call the alt_irq_handler to deliver to the registered interrupt handler.\r
+\r
+ .section .exceptions.irqreturn, "xa"\r
+restore_sp_from_pxCurrentTCB:\r
+ movia et, pxCurrentTCB # Load the address of the pxCurrentTCB pointer\r
+ ldw et, (et) # Load the value of the pxCurrentTCB pointer\r
+ ldw sp, (et) # Load the stack pointer with the top value of the TCB\r
+\r
+restore_context:\r
+ ldw ra, 0(sp) # Restore the registers.\r
+ # Leave a gap for muldiv 0.\r
+ ldw at, 8(sp)\r
+ ldw r2, 12(sp)\r
+ ldw r3, 16(sp)\r
+ ldw r4, 20(sp)\r
+ ldw r5, 24(sp) \r
+ ldw r6, 28(sp) \r
+ ldw r7, 32(sp) \r
+ ldw r8, 36(sp) \r
+ ldw r9, 40(sp) \r
+ ldw r10, 44(sp)\r
+ ldw r11, 48(sp)\r
+ ldw r12, 52(sp)\r
+ ldw r13, 56(sp)\r
+ ldw r14, 60(sp)\r
+ ldw r15, 64(sp)\r
+ ldw et, 68(sp) # Load the eStatus\r
+ wrctl estatus, et # Write the eStatus\r
+ ldw ea, 72(sp) # Load the Program Counter\r
+ ldw r16, 76(sp)\r
+ ldw r17, 80(sp)\r
+ ldw r18, 84(sp)\r
+ ldw r19, 88(sp)\r
+ ldw r20, 92(sp)\r
+ ldw r21, 96(sp)\r
+ ldw r22, 100(sp)\r
+ ldw r23, 104(sp)\r
+ ldw gp, 108(sp)\r
+ ldw fp, 112(sp)\r
+ addi sp, sp, 116 # Release stack space\r
+\r
+ eret # Return to address ea, loading eStatus into Status.\r
+ \r
+ .section .exceptions.soft, "xa"\r
+soft_exceptions:\r
+ ldw et, 0(ea) # Load the instruction where the interrupt occured.\r
+ movhi at, %hi(0x003B683A) # Load the registers with the trap instruction code\r
+ ori at, at, %lo(0x003B683A)\r
+ cmpne et, et, at # Compare the trap instruction code to the last excuted instruction\r
+ beq et, r0, call_scheduler # its a trap so switchcontext\r
+ break # This is an un-implemented instruction or muldiv problem.\r
+ br restore_context # its something else\r
+\r
+call_scheduler:\r
+ addi ea, ea, 4 # A trap was called, increment the program counter so it is not called again.\r
+ stw ea, 72(sp) # Save the new program counter to the context.\r
+ call vTaskSwitchContext # Pick the next context.\r
+ br restore_sp_from_pxCurrentTCB # Switch in the task context and restore. \r
--- /dev/null
+/*\r
+ FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
+\r
+ This file is part of the FreeRTOS distribution.\r
+\r
+ FreeRTOS is free software; you can redistribute it and/or modify it under \r
+ the terms of the GNU General Public License (version 2) as published by the \r
+ Free Software Foundation and modified by the FreeRTOS exception.\r
+ **NOTE** The exception to the GPL is included to allow you to distribute a\r
+ combined work that includes FreeRTOS without being obliged to provide the \r
+ source code for proprietary components outside of the FreeRTOS kernel. \r
+ Alternative commercial license and support terms are also available upon \r
+ request. See the licensing section of http://www.FreeRTOS.org for full \r
+ license details.\r
+\r
+ FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\r
+ more details.\r
+\r
+ You should have received a copy of the GNU General Public License along\r
+ with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
+ Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
+\r
+\r
+ ***************************************************************************\r
+ * *\r
+ * Looking for a quick start? Then check out the FreeRTOS eBook! *\r
+ * See http://www.FreeRTOS.org/Documentation for details *\r
+ * *\r
+ ***************************************************************************\r
+\r
+ 1 tab == 4 spaces!\r
+\r
+ Please ensure to read the configuration and relevant port sections of the\r
+ online documentation.\r
+\r
+ http://www.FreeRTOS.org - Documentation, latest information, license and\r
+ contact details.\r
+\r
+ http://www.SafeRTOS.com - A version that is certified for use in safety\r
+ critical systems.\r
+\r
+ http://www.OpenRTOS.com - Commercial support, development, porting,\r
+ licensing and training services.\r
+*/\r
+\r
+#ifndef PORTMACRO_H\r
+#define PORTMACRO_H\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+#include "sys/alt_irq.h"\r
+\r
+/*-----------------------------------------------------------\r
+ * Port specific definitions. \r
+ *\r
+ * The settings in this file configure FreeRTOS correctly for the\r
+ * given hardware and compiler.\r
+ *\r
+ * These settings should not be altered.\r
+ *-----------------------------------------------------------\r
+ */\r
+\r
+/* Type definitions. */\r
+#define portCHAR char\r
+#define portFLOAT float\r
+#define portDOUBLE double\r
+#define portLONG long\r
+#define portSHORT short\r
+#define portSTACK_TYPE unsigned portLONG\r
+#define portBASE_TYPE long\r
+\r
+#if( configUSE_16_BIT_TICKS == 1 )\r
+ typedef unsigned portSHORT portTickType;\r
+ #define portMAX_DELAY ( portTickType ) 0xffff\r
+#else\r
+ typedef unsigned portLONG portTickType;\r
+ #define portMAX_DELAY ( portTickType ) 0xffffffff\r
+#endif\r
+/*-----------------------------------------------------------*/ \r
+\r
+/* Architecture specifics. */\r
+#define portSTACK_GROWTH ( -1 )\r
+#define portTICK_RATE_MS ( ( portTickType ) 1000 / configTICK_RATE_HZ ) \r
+#define portBYTE_ALIGNMENT 4\r
+#define portNOP() asm volatile ( "NOP" )\r
+#define portCRITICAL_NESTING_IN_TCB 1\r
+/*-----------------------------------------------------------*/ \r
+\r
+extern void vTaskSwitchContext( void );\r
+#define portYIELD() asm volatile ( "trap" );\r
+#define portEND_SWITCHING_ISR( xSwitchRequired ) if( xSwitchRequired ) vTaskSwitchContext()\r
+\r
+\r
+/* Include the port_asm.S file where the Context saving/restoring is defined. */\r
+__asm__( "\n\t.globl save_context" );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+extern void vTaskEnterCritical( void );\r
+extern void vTaskExitCritical( void );\r
+\r
+#define portDISABLE_INTERRUPTS() alt_irq_disable_all()\r
+#define portENABLE_INTERRUPTS() alt_irq_enable_all( 0x01 );\r
+#define portENTER_CRITICAL() vTaskEnterCritical()\r
+#define portEXIT_CRITICAL() vTaskExitCritical()\r
+/*-----------------------------------------------------------*/\r
+\r
+/* Task function macros as described on the FreeRTOS.org WEB site. */\r
+#define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )\r
+#define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* PORTMACRO_H */\r
+\r