/* FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd. All rights reserved VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. This file is part of the FreeRTOS distribution. FreeRTOS is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (version 2) as published by the Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. *************************************************************************** >>! NOTE: The modification to the GPL is included to allow you to !<< >>! distribute a combined work that includes FreeRTOS without being !<< >>! obliged to provide the source code for proprietary components !<< >>! outside of the FreeRTOS kernel. !<< *************************************************************************** FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Full license text is available on the following link: http://www.freertos.org/a00114.html *************************************************************************** * * * FreeRTOS provides completely free yet professionally developed, * * robust, strictly quality controlled, supported, and cross * * platform software that is more than just the market leader, it * * is the industry's de facto standard. * * * * Help yourself get started quickly while simultaneously helping * * to support the FreeRTOS project by purchasing a FreeRTOS * * tutorial book, reference manual, or both: * * http://www.FreeRTOS.org/Documentation * * * *************************************************************************** http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading the FAQ page "My application does not run, what could be wrong?". Have you defined configASSERT()? http://www.FreeRTOS.org/support - In return for receiving this top quality embedded software for free we request you assist our global community by participating in the support forum. http://www.FreeRTOS.org/training - Investing in training allows your team to be as productive as possible as early as possible. Now you can receive FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers Ltd, and the world's leading authority on the world's leading RTOS. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, including FreeRTOS+Trace - an indispensable productivity tool, a DOS compatible FAT file system, and our tiny thread aware UDP/IP stack. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS licenses offer ticketed support, indemnification and commercial middleware. http://www.SafeRTOS.com - High Integrity Systems also provide a safety engineered and independently SIL3 certified version for use in safety and mission critical applications that require provable dependability. 1 tab == 4 spaces! */ /****************************************************************************** * This project provides three demo applications. A simple blinky style * project, a more comprehensive test and demo application, and an lwIP example. * The mainSELECTED_APPLICATION setting (defined in this file) is used to * select between the three. 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. The lwIP example is implemented * and described in main_lwIP.c. * * This file implements the code that is not demo specific, including the * hardware setup and FreeRTOS hook functions. * * !!! IMPORTANT NOTE !!! * The GCC libraries that ship with the Xilinx SDK make use of the floating * point registers. To avoid this causing corruption it is necessary to avoid * their use. For this reason main.c contains very basic C implementations of * the standard C library functions memset(), memcpy() and memcmp(), which are * are used by FreeRTOS itself. Defining these functions in the project * prevents the linker pulling them in from the library. Any other standard C * library functions that are used by the application must likewise be defined * in C. * * 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! * */ /* Standard includes. */ #include #include /* Scheduler include files. */ #include "FreeRTOS.h" #include "task.h" #include "semphr.h" /* Standard demo includes. */ //#include "partest.h" #include "mem_control.h" #include "pixel_constants.h" #include "TimerDemo.h" #include "QueueOverwrite.h" #include "EventGroupsDemo.h" #include "TaskNotify.h" #include "IntSemTest.h" /* Xilinx includes. */ #include "platform.h" #include "xparameters.h" #include "xscutimer.h" #include "xscugic.h" #include "xil_exception.h" /* mainSELECTED_APPLICATION is used to select between three demo applications, * as described at the top of this file. * * When mainSELECTED_APPLICATION is set to 0 the simple blinky example will * be run. * * When mainSELECTED_APPLICATION is set to 1 the comprehensive test and demo * application will be run. * * When mainSELECTED_APPLICATION is set to 2 the lwIP example will be run. */ #define mainSELECTED_APPLICATION 0 /*-----------------------------------------------------------*/ /* * 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; /*-----------------------------------------------------------*/ int main( void ) { int16_t trigger_buffer[2048] = {0}; u16 i = 0; u16 j = 0; u16 k = 0; int16_t temp_in; uint16_t trigger_buffer_start_addr = 0; uint16_t triggered = 0; int16_t volt_division_position = 75; int16_t volt_division_setting = 0; int16_t volt_division_row_start = 20; int16_t volt_division_row_stop = 30; int16_t volt_division_column_start = 0; int16_t volt_division_column_stop = 0; int16_t trigger_position = 10; int16_t trigger_value = 0; uint32_t encoder_in = 0; int16_t encoder1_count = 0; int16_t encoder2_count = 0; int16_t encoder3_count = 0; uint16_t button1 = 0; uint16_t button2 = 0; uint16_t buffer_index_width = COLUMN_SCOPE_AREA_STOP - COLUMN_SCOPE_AREA_START; uint16_t start_buffer_index = CENTER_BUFFER - (buffer_index_width/2); uint16_t end_buffer_index = CENTER_BUFFER + (buffer_index_width/2); uint16_t pixel_column = 0; int16_t pixel_row = 0; int16_t pixel_row_next = 0; uint16_t trigger_row = 0; uint16_t y_product = 0; uint16_t y_divide = 0; /* Configure the hardware ready to run the demo. */ prvSetupHardware(); // Initialize GPIO: initialize_trigger_buff_gpio(); initialize_trigger_buff_settings_gpio(); initialize_screen_buffer_gpio(); initialize_encoder_gpio(); set_subsample_counter( 1 ); set_trigger_level(200); while(1) { triggered = 0; while( !triggered ) // wait for triggering to complete, check encoders and calculate trigger values while waiting { // Get inputs from Encoders and Buttons encoder_in = read_encoder_values(); encoder1_count = encoder_in & 0x0003F; if (encoder1_count & 0b100000) encoder1_count = encoder1_count | 0xFFC0; encoder1_count *= -1; // Invert value due to incorrect wiring encoder3_count = (encoder_in>>12) & 0x0003F; if (encoder3_count & 0b100000) encoder3_count = encoder3_count | 0xFFC0; encoder3_count *= -1; // Invert value due to incorrect wiring button1 = (encoder_in>>18) & 0x00001; button2 = (encoder_in>>19) & 0x00001; reset_encoders(); // Take encoder inputs in, make sure the internal counters stay within a certain range if(button1) // Button1 is active low. If it's not pressed, we want to modify V division volt_division_position += encoder1_count; else trigger_position += encoder1_count; if (volt_division_position > 80) volt_division_position = 80; if (volt_division_position < 50) volt_division_position = 50; if (trigger_position > 100) trigger_position = 100; if (trigger_position < -100) trigger_position = -100; // trigger_position = 100 -> 10V // trigger_position = -100 -> -10V // 1V = 205 bits trigger_value = (trigger_position *205)/10; set_trigger_level(trigger_value); // Set trigger setting // Update trigger status triggered = check_trigger_status(); } // Erase the V and T division marker areas for (i = ROW_V_DIV_START; i <= ROW_V_DIV_STOP; i++) for (j = COLUMN_V_DIV_START; j <= COLUMN_V_DIV_STOP; j++ ) { { writePixel(i, j, 0b000); } } // Write a yellow block in position of correct V/division settings volt_division_setting = volt_division_position/10; switch(volt_division_setting) { case(7): volt_division_column_start = 465; volt_division_column_stop = 475; y_product = 100; y_divide = 820; break; case(6): volt_division_column_start = 445; volt_division_column_stop = 455; y_product = 100; y_divide = 410; break; case(5): volt_division_column_start = 405; volt_division_column_stop = 415; y_product = 100; y_divide = 205; break; } // Draw voltage division marker for (i = volt_division_row_start; i <= volt_division_row_stop; i++) { for( j = volt_division_column_start; j <= volt_division_column_stop; j++ ) { writePixel(i, j, 0b001); // Draw yellow pixel } } trigger_buffer_start_addr = read_trigger_buff_addr(); // Read start of circular buffer // Read all values of trigger buffer memory for (i = 0; i < 2048; i++) { // Read in trigger buffer values, starting at the point where it triggered temp_in = read_trigger_buff( trigger_buffer_start_addr + i); // if (0x0800 & temp_in) // If sign bit == 1 trigger_buffer[i] = (0xF000 | temp_in); // Extend sign bits for top 4 bits else // If sign bit == 0 trigger_buffer[i] = temp_in; // Sign bits unchanged } // Calculate trigger level line trigger_row = CENTER_ROW - (trigger_value)*y_product/y_divide; if (trigger_row >= ROW_SCOPE_AREA_STOP) pixel_row = ROW_SCOPE_AREA_STOP; if (trigger_row <= ROW_SCOPE_AREA_START) pixel_row = ROW_SCOPE_AREA_START; // Writing scope image to screen // For each pixel of width of scope area, loop for (i = start_buffer_index; i <= end_buffer_index; i++) { // Calculate row of scope trace pixel (and the next one) // Clip waveform at top and bottom of scope display // Scale Y values by constant pixel_row = CENTER_ROW - (trigger_buffer[i])*y_product/y_divide; pixel_row_next = CENTER_ROW - (trigger_buffer[i+1])*y_product/y_divide; if (pixel_row >= ROW_SCOPE_AREA_STOP) pixel_row = ROW_SCOPE_AREA_STOP; if (pixel_row <= ROW_SCOPE_AREA_START) pixel_row = ROW_SCOPE_AREA_START; if (pixel_row_next >= ROW_SCOPE_AREA_STOP) pixel_row_next = ROW_SCOPE_AREA_STOP; if (pixel_row_next <= ROW_SCOPE_AREA_START) pixel_row_next = ROW_SCOPE_AREA_START; pixel_column = COLUMN_SCOPE_AREA_START + i - start_buffer_index; // Run loop through scope area rows to erase all pixels in this column for(j = ROW_SCOPE_AREA_START; j < ROW_SCOPE_AREA_STOP; j++) { writePixel(j, pixel_column, 0b000); } // Loop to use vectors to connect sample values on screen if (pixel_row_next >= pixel_row) { for(k = pixel_row; k <= pixel_row_next; k++) { writePixel(k, pixel_column, 0b001); } } else if (pixel_row_next < pixel_row) { for(k = pixel_row; k >= pixel_row_next; k--) { writePixel(k, pixel_column, 0b001); } } // Add trigger line pixel writePixel(trigger_row, pixel_column, 0b100); // Draw triangle on left of screen to show "gnd" level // Triangle should be drawn in first 6 pixels of the viewing area if( (pixel_column >= COLUMN_SCOPE_AREA_START) && ( pixel_column <= (COLUMN_SCOPE_AREA_START+5)) ) { switch (pixel_column) { case (COLUMN_SCOPE_AREA_START): writePixel(CENTER_ROW+5, pixel_column, 0b010); writePixel(CENTER_ROW-5, pixel_column, 0b010); case (COLUMN_SCOPE_AREA_START+1): writePixel(CENTER_ROW+4, pixel_column, 0b010); writePixel(CENTER_ROW-4, pixel_column, 0b010); case (COLUMN_SCOPE_AREA_START+2): writePixel(CENTER_ROW+3, pixel_column, 0b010); writePixel(CENTER_ROW-3, pixel_column, 0b010); case (COLUMN_SCOPE_AREA_START+3): writePixel(CENTER_ROW+2, pixel_column, 0b010); writePixel(CENTER_ROW-2, pixel_column, 0b010); case (COLUMN_SCOPE_AREA_START+4): writePixel(CENTER_ROW+1, pixel_column, 0b010); writePixel(CENTER_ROW-1, pixel_column, 0b010); case (COLUMN_SCOPE_AREA_START+5): writePixel(CENTER_ROW, pixel_column, 0b010); } } } write_trigger_rst( 1 ); // Reset trigger to start again write_trigger_rst( 0 ); } /* Don't expect to reach here. */ return 0; } /*-----------------------------------------------------------*/ 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. */ /* Initialise the LED port. */ //vParTestInitialise(); /* 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 vApplicationMallocFailedHook( void ) { /* Called if a call to pvPortMalloc() fails because there is insufficient free memory available in the FreeRTOS heap. pvPortMalloc() is called internally by FreeRTOS API functions that create tasks, queues, software timers, and semaphores. The size of the FreeRTOS heap is set by the configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */ taskDISABLE_INTERRUPTS(); for( ;; ); } /*-----------------------------------------------------------*/ 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 vApplicationIdleHook( void ) { volatile size_t xFreeHeapSpace; /* 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(); /* Remove compiler warning about xFreeHeapSpace being set but never used. */ ( void ) xFreeHeapSpace; } /*-----------------------------------------------------------*/ void vAssertCalled( const char * pcFile, unsigned long ulLine ) { volatile unsigned long ul = 0; ( void ) pcFile; ( void ) ulLine; taskENTER_CRITICAL(); { /* Set ul to a non-zero value using the debugger to step out of this function. */ while( ul == 0 ) { portNOP(); } } taskEXIT_CRITICAL(); } /*-----------------------------------------------------------*/ 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(); } #endif } /*-----------------------------------------------------------*/ void *memcpy( void *pvDest, const void *pvSource, size_t ulBytes ) { unsigned char *pcDest = ( unsigned char * ) pvDest, *pcSource = ( unsigned char * ) pvSource; size_t x; for( x = 0; x < ulBytes; x++ ) { *pcDest = *pcSource; pcDest++; pcSource++; } return pvDest; } /*-----------------------------------------------------------*/ void *memset( void *pvDest, int iValue, size_t ulBytes ) { unsigned char *pcDest = ( unsigned char * ) pvDest; size_t x; for( x = 0; x < ulBytes; x++ ) { *pcDest = ( unsigned char ) iValue; pcDest++; } return pvDest; } /*-----------------------------------------------------------*/ int memcmp( const void *pvMem1, const void *pvMem2, size_t ulBytes ) { const unsigned char *pucMem1 = pvMem1, *pucMem2 = pvMem2; size_t x; for( x = 0; x < ulBytes; x++ ) { if( pucMem1[ x ] != pucMem2[ x ] ) { break; } } return ulBytes - x; } /*-----------------------------------------------------------*/ 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 ); }