Files
obsw/bsp_z7/main.c
T
2025-10-29 09:53:01 +01:00

234 lines
7.1 KiB
C

/* Standard includes. */
#include <limits.h>
#include <stdio.h>
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
/* Xilinx includes. */
// #include "platform.h"
#include "xil_exception.h"
#include "xparameters.h"
#include "xscugic.h"
#include "xscutimer.h"
#include "xuartps_hw.h"
/*
* Configure the hardware as necessary to run this demo.
*/
static void prvSetupHardware(void);
/*
* The Xilinx projects use a BSP that do not allow the start up code to be
* altered easily. Therefore the vector table used by FreeRTOS is defined in
* FreeRTOS_asm_vectors.S, which is part of this project. Switch to use the
* FreeRTOS vector table.
*/
extern void vPortInstallFreeRTOSVectorTable(void);
/* Prototypes for the standard FreeRTOS callback/hook functions implemented
within this file. */
void vApplicationMallocFailedHook(void);
void vApplicationIdleHook(void);
void vApplicationStackOverflowHook(TaskHandle_t pxTask, char *pcTaskName);
void vApplicationTickHook(void);
/* The private watchdog is used as the timer that generates run time
stats. This frequency means it will overflow quite quickly. */
XScuWdt xWatchDogInstance;
/*-----------------------------------------------------------*/
/* The interrupt controller is initialised in this file, and made available to
other modules. */
XScuGic xInterruptController;
extern SemaphoreHandle_t malloc_mutex;
/*-----------------------------------------------------------*/
void mission(void);
void initFreeRTOSHelper();
int main(void) {
// Enable UARTs, so qemu knows we use them (should already be enabled by fsbl
// on actual hw)
XUartPs_WriteReg(XPS_UART0_BASEADDR, XUARTPS_CR_OFFSET,
XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN);
XUartPs_WriteReg(XPS_UART1_BASEADDR, XUARTPS_CR_OFFSET,
XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN);
/* Configure the hardware ready to run. */
prvSetupHardware();
mission();
}
static void prvSetupHardware(void) {
BaseType_t xStatus;
XScuGic_Config *pxGICConfig;
/* Ensure no interrupts execute while the scheduler is in an inconsistent
state. Interrupts are automatically enabled when the scheduler is
started. */
portDISABLE_INTERRUPTS();
/* Obtain the configuration of the GIC. */
pxGICConfig = XScuGic_LookupConfig(XPAR_SCUGIC_SINGLE_DEVICE_ID);
/* Sanity check the FreeRTOSConfig.h settings are correct for the
hardware. */
configASSERT(pxGICConfig);
configASSERT(pxGICConfig->CpuBaseAddress ==
(configINTERRUPT_CONTROLLER_BASE_ADDRESS +
configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET));
configASSERT(pxGICConfig->DistBaseAddress ==
configINTERRUPT_CONTROLLER_BASE_ADDRESS);
/* Install a default handler for each GIC interrupt. */
xStatus = XScuGic_CfgInitialize(&xInterruptController, pxGICConfig,
pxGICConfig->CpuBaseAddress);
configASSERT(xStatus == XST_SUCCESS);
(void)xStatus; /* Remove compiler warning if configASSERT() is not defined. */
// TODO we can alter the startup code...
/* The Xilinx projects use a BSP that do not allow the start up code to be
altered easily. Therefore the vector table used by FreeRTOS is defined in
FreeRTOS_asm_vectors.S, which is part of this project. Switch to use the
FreeRTOS vector table. */
vPortInstallFreeRTOSVectorTable();
}
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
void vInitialiseTimerForRunTimeStats(void) {
XScuWdt_Config *pxWatchDogInstance;
uint32_t ulValue;
const uint32_t ulMaxDivisor = 0xff, ulDivisorShift = 0x08;
pxWatchDogInstance = XScuWdt_LookupConfig(XPAR_SCUWDT_0_DEVICE_ID);
XScuWdt_CfgInitialize(&xWatchDogInstance, pxWatchDogInstance,
pxWatchDogInstance->BaseAddr);
ulValue = XScuWdt_GetControlReg(&xWatchDogInstance);
ulValue |= ulMaxDivisor << ulDivisorShift;
XScuWdt_SetControlReg(&xWatchDogInstance, ulValue);
XScuWdt_LoadWdt(&xWatchDogInstance, UINT_MAX);
XScuWdt_SetTimerMode(&xWatchDogInstance);
XScuWdt_Start(&xWatchDogInstance);
}
#ifndef ARM_SEMIHOSTING
void done() {
while (1)
;
}
void done_error() {
// makes no difference
done();
}
#else // enable semihosting interface for done()
void done() {
// Call semihosting interface to signal exit
// see https://github.com/ARM-software/abi-aa -> Miscellaneous material ->
// Semihosting for AArch32 and AArch64
register int reg0 asm("r0");
register int reg1 asm("r1");
reg0 = 0x18; // SYS_EXIT
reg1 = 0x20026; // ADP_Stopped_ApplicationExit
asm("svc 0x123456"); // syscall to semihosting interface
}
void done_error() {
// same as done(), will make qemu return 1
register int reg0 asm("r0");
register int reg1 asm("r1");
reg0 = 0x18; // SYS_EXIT
reg1 = 0x20023; // ADP_Stopped_RunTimeErrorUnknown
asm("svc 0x123456"); // syscall to semihosting interface
}
#endif /* SEMIHOSTING */
void vApplicationIdleHook(void) {
volatile size_t xFreeHeapSpace, xMinimumEverFreeHeapSpace;
/* This is just a trivial example of an idle hook. It is called on each
cycle of the idle task. It must *NOT* attempt to block. In this case the
idle task just queries the amount of FreeRTOS heap that remains. See the
memory management section on the http://www.FreeRTOS.org web site for memory
management options. If there is a lot of heap memory free then the
configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
RAM. */
// xFreeHeapSpace = xPortGetFreeHeapSize();
// xMinimumEverFreeHeapSpace = xPortGetMinimumEverFreeHeapSize();
// /* Remove compiler warning about xFreeHeapSpace being set but never used.
// */ (void)xFreeHeapSpace; (void)xMinimumEverFreeHeapSpace;
}
void vApplicationTickHook(void) {
#if (mainSELECTED_APPLICATION == 1)
{
/* The full demo includes a software timer demo/test that requires
prodding periodically from the tick interrupt. */
vTimerPeriodicISRTests();
/* Call the periodic queue overwrite from ISR demo. */
vQueueOverwritePeriodicISRDemo();
/* Call the periodic event group from ISR demo. */
vPeriodicEventGroupsProcessing();
/* Use task notifications from an interrupt. */
xNotifyTaskFromISR();
/* Use mutexes from interrupts. */
vInterruptSemaphorePeriodicTest();
/* Writes to stream buffer byte by byte to test the stream buffer trigger
level functionality. */
vPeriodicStreamBufferProcessing();
/* Writes a string to a string buffer four bytes at a time to demonstrate
a stream being sent from an interrupt to a task. */
vBasicStreamBufferSendFromISR();
#if (configUSE_QUEUE_SETS == 1)
{
vQueueSetAccessQueueSetFromISR();
}
#endif
/* Test flop alignment in interrupts - calling printf from an interrupt
is BAD! */
#if (configASSERT_DEFINED == 1)
{
char cBuf[20];
UBaseType_t uxSavedInterruptStatus;
uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
{
sprintf(cBuf, "%1.3f", 1.234);
}
portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedInterruptStatus);
configASSERT(strcmp(cBuf, "1.234") == 0);
}
#endif /* configASSERT_DEFINED */
}
#endif
}