/* FreeRTOS V9.0.0 - Copyright (C) 2016 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 file has been modified from the original FreeRTOS demo code * Author: Joseph Coplon * Class: CPE 439 Fall 2016 * * Description: Contains the required functions to initialize GPIO and UART, * set the power relay output, communicate with the PID module, and read the * temperature from the digital thermometer. * * The onewire functions are adapted from Electric Imp, using code * written by Theo Habers: * https://electricimp.com/docs/resources/onewire/ ****************************************************************************** */ /* Scheduler includes. */ #include "FreeRTOS.h" #include "task.h" /* Demo includes. */ #include "partest.h" /* Xilinx includes. */ #include "xgpiops.h" #include "xgpio.h" #include "xuartps.h" #define partstNUM_LEDS ( 1 ) #define partstDIRECTION_OUTPUT ( 1 ) #define partstDIRECTION_INPUT ( 0 ) #define partstOUTPUT_ENABLED ( 1 ) #define partstTEMP_OUT_CHANNEL ( 1 ) #define partstTEMP_ERR_CHANNEL ( 2 ) #define partstLEDS_GPIO_CHANNEL ( 1 ) #define partstSWITCHES_CHANNEL ( 2 ) #define partstPID_CHANNEL ( 1 ) #define partstLED_OUTPUT ( 7 ) #define partst_TPO ( 9 ) #define partst_RX ( 10 ) #define partst_TX ( 11 ) /* Modify these values to adjust PID coefficients */ #define kp 1; #define kd 0; #define ki 0; /*-----------------------------------------------------------*/ static XGpioPs xGpio; static XGpio xGpio0; static XGpio xGpio1; static XGpio xGpio2; static XUartPs xUart; static uint8_t sendBuffer; static uint8_t recvBuffer; static uint8_t tempGoal = 25; /*-----------------------------------------------------------*/ void vParTestInitialise( void ) { XGpioPs_Config *pxConfigPtr; XGpio_Config *xConfigPtr; BaseType_t xStatus; BaseType_t xxStatus; XUartPs_Config *uart; /* Initialise the GPIOPs driver. */ pxConfigPtr = XGpioPs_LookupConfig( XPAR_XGPIOPS_0_DEVICE_ID ); xStatus = XGpioPs_CfgInitialize( &xGpio, pxConfigPtr, pxConfigPtr->BaseAddr ); configASSERT( xStatus == XST_SUCCESS ); ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */ /* Enable outputs and set low. */ XGpioPs_SetDirectionPin( &xGpio, partstLED_OUTPUT, partstDIRECTION_OUTPUT ); XGpioPs_SetDirectionPin( &xGpio, partst_TPO, partstDIRECTION_OUTPUT ); XGpioPs_SetOutputEnablePin( &xGpio, partstLED_OUTPUT, partstOUTPUT_ENABLED ); XGpioPs_SetOutputEnablePin( &xGpio, partst_TPO, partstOUTPUT_ENABLED ); XGpioPs_WritePin( &xGpio, partstLED_OUTPUT, 0 ); XGpioPs_WritePin( &xGpio, partst_TPO, 0 ); /* Enable UART pins */ XGpioPs_SetDirectionPin( &xGpio, partst_RX, partstDIRECTION_INPUT ); XGpioPs_SetDirectionPin( &xGpio, partst_TX, partstDIRECTION_OUTPUT ); XGpioPs_SetOutputEnablePin( &xGpio, partst_TX, partstOUTPUT_ENABLED ); /* Initialize GPIO 0 Driver */ xConfigPtr = XGpio_LookupConfig( XPAR_GPIO_0_DEVICE_ID ); xStatus = XGpio_CfgInitialize( &xGpio0, xConfigPtr, xConfigPtr->BaseAddress ); configASSERT( xStatus == XST_SUCCESS ); ( void ) xStatus; XGpio_SetDataDirection( &xGpio0, partstTEMP_OUT_CHANNEL, 0xFF ); XGpio_SetDataDirection( &xGpio0, partstTEMP_ERR_CHANNEL, 0x00 ); XGpio_DiscreteWrite( &xGpio0, partstTEMP_ERR_CHANNEL, 0x7F ); /* Initialize GPIO 1 Driver */ xConfigPtr = XGpio_LookupConfig( XPAR_GPIO_1_DEVICE_ID ); xStatus = XGpio_CfgInitialize( &xGpio1, xConfigPtr, xConfigPtr->BaseAddress ); configASSERT( xStatus == XST_SUCCESS ); ( void ) xStatus; XGpio_SetDataDirection( &xGpio1, partstPID_CHANNEL, 0x1FFFFFF ); XGpio_DiscreteWrite( &xGpio1, partstPID_CHANNEL, (kp << 16) + (kd << 8) + ki ); /* Initialize GPIO 2 Driver */ xConfigPtr = XGpio_LookupConfig( XPAR_GPIO_2_DEVICE_ID ); xStatus = XGpio_CfgInitialize( &xGpio2, xConfigPtr, xConfigPtr->BaseAddress ); configASSERT( xStatus == XST_SUCCESS ); ( void ) xStatus; XGpio_SetDataDirection( &xGpio2, partstLEDS_GPIO_CHANNEL, 0xF ); XGpio_SetDataDirection( &xGpio2, partstSWITCHES_CHANNEL, 0x0 ); XGpio_DiscreteWrite( &xGpio2, partstLEDS_GPIO_CHANNEL, 0x0 ); /* Initialize UART */ uart = XUartPs_LookupConfig( XPAR_PS7_UART_0_DEVICE_ID ); xxStatus = XUartPs_CfgInitialize( &xUart, uart, uart->BaseAddress ); configASSERT( xxStatus == XST_SUCCESS ); ( void ) xxStatus; XUartPs_SetBaudRate( &xUart, 115200 ); } // Set the value for the power relay output pin void vParTestSetTPOutput( BaseType_t xState) { XGpioPs_WritePin( &xGpio, partst_TPO, xState); } // Get the output pulsewidth from the PID module unsigned long vParTestGetPW() { // Read value from bits [7:0] of the temp channel unsigned long iPW = XGpio_DiscreteRead( &xGpio0, partstTEMP_OUT_CHANNEL); // convert to value in ms with max being 10,000 ms iPW -= 25; if (iPW > 65) iPW = 65; iPW *= (10000 / 65); return iPW; } // Read the temperature from the digital thermometer void vParTestReadTemp() { uint8_t tempLSB = 0; uint8_t tempMSB = 0; uint8_t tempCelsius = 0; int8_t tempError = 0; uint8_t switches = 0; XGpio_DiscreteWrite( &xGpio0, partstTEMP_ERR_CHANNEL, 0xFF ); if (onewireReset()) { onewireWriteByte(0xCC); onewireWriteByte(0x44); vTaskDelay( 800 / portTICK_PERIOD_MS ); onewireReset(); onewireWriteByte(0xCC); onewireWriteByte(0xBE); tempLSB = onewireReadByte(); tempMSB = onewireReadByte(); onewireReset(); tempCelsius = ((tempMSB * 256) + tempLSB) / 16; } switches = XGpio_DiscreteRead( &xGpio2, partstSWITCHES_CHANNEL); tempGoal = (switches*4) + 25; XGpio_DiscreteWrite( &xGpio2, partstLEDS_GPIO_CHANNEL, switches ); tempError = tempGoal - tempCelsius; if ((tempError <= 2) && (tempError >= -2)) { XGpioPs_WritePin( &xGpio, partstLED_OUTPUT, 1); } else { XGpioPs_WritePin( &xGpio, partstLED_OUTPUT, 0); } XGpio_DiscreteWrite( &xGpio0, partstTEMP_ERR_CHANNEL, tempError ); } int onewireReset() { XUartPs_SetBaudRate( &xUart, 9600 ); sendBuffer = 0xF0; XUartPs_Send( &xUart, &sendBuffer, 1); // Allow time for message to send vTaskDelay( 5 / portTICK_PERIOD_MS ); recvBuffer = 0; XUartPs_Recv( &xUart, &recvBuffer, 1); if ((recvBuffer == 0xF0) || (!recvBuffer)) { return 0; } else { XUartPs_SetBaudRate ( &xUart, 115200); return 1; } } void onewireWriteByte(uint8_t xByte) { uint8_t i; for (i=0; i<8; i++) { onewireBit(xByte & 0x01); xByte = xByte >> 1; } } uint8_t onewireReadByte() { uint8_t xByte = 0; uint8_t i; for (i=0; i<8; i++) { xByte = (xByte >> 1) + 0x80 * onewireBit(1); } return xByte; } uint8_t onewireBit(uint8_t xBit) { uint8_t returnVal; xBit = xBit ? 0xFF : 0x00; sendBuffer = xBit; XUartPs_Send( &xUart, &sendBuffer, 1); // Allow time for message to send vTaskDelay( 5 / portTICK_PERIOD_MS ); recvBuffer = 0; XUartPs_Recv( &xUart, &recvBuffer, 1); returnVal = (recvBuffer == 0xFF ? 1 : 0); return returnVal; }