/*
FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
Changes from V4.0.5
+ Modified to demonstrate the use of co-routines.
*/
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
/* EEPROM routines used only with the WinAVR compiler. */
#include <avr/eeprom.h>
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
//#include "croutine.h"
#include "semphr.h"
/* My file headers. */
#include "systemtasks.h"
#include "sensors.h"
#include "init.h"
#include "buffer.h"
#include "datablock.h"
#include "spi_lib.h"
#include "bma020.h"
/* Priority definitions for most of the tasks in the application. Some
tasks just use the idle priority. The higher the Value, the higher is the actual priority.*/
#define mainLEDTASKPRIORITY ( tskIDLEPRIORITY )
#define mainTIMETASKPRIORITY ( tskIDLEPRIORITY + 2)
#define mainREADCTASKPRIORITY ( tskIDLEPRIORITY + 1 )
#define mainHALLTASKPRIORITY ( tskIDLEPRIORITY + 1 )
#define mainREADTEMPADCPRIORITY ( tskIDLEPRIORITY )
#define mainREADSPITEMPPRIORITY ( tskIDLEPRIORITY )
#define mainREADSPI3DBSPRIORITY ( tskIDLEPRIORITY ) //die SPI-Tasks muessen gleiche Prio haben, sonst muss ein Mutex für SPI eingefuert werden!
/* Baud rate used by the serial port tasks. */
#define mainCOMTESTBAUD_RATE ( ( unsigned long ) 38400 )
/* Scheduler TickPeriod in ms*/
#define mainTICKRATEMS ( 1000/configTICKRATEHZ )
/* LED used by the serial port tasks. This is toggled on each character Tx,
and mainCOMTESTLED + 1 is toggles on each character Rx. */
#define mainCOMTESTLED ( 4 )
//Idle Hook Task
//void vApplicationIdleHook( void );
//Flashing of LEDs Task
//void vLEDFlashTask( void *pvParameters );
//Flashing of LEDs Co-Routine
//void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
//Read Port C Task
void vReadPortCTask( void *pvParameters );
//ADC Sample Battery Voltage Task
void vReadBattVolt(void * pvParameters);
//ADC Sample Intake Temperature Task
void vReadIntakeTemp(void * pvParameters);
//Incrementing of Systemtime Task
void vRefreshSystemTime( void *pvParameters );
//Read Counter 3 counting Result Task
void vReadHallPeriod( void *pvParameters );
//Read MAX6675 Temperature over SPI
void vReadSPITemp (void * pvParameters);
//Read BMA020 Acceleration
void vRead3DBS (void * pvParameters);
/-----------------------------------------------------------/
//Systemzeit
struct time_s SysTime;
//Der Datenpuffer für CAN und sein Mutex
struct Buffer CAN_FIFO = {{}, 0, 0};
xSemaphoreHandle xFIFOmutex;
//letztes Zaehlergebniss des Timer2 fuer eine Periode des Hall-Signals
volatile uint16_t HallPeriod;
/-----------------------------------------------------------/
//tritt auf, wenn Int0 ausgeloest wird
ISR(INT0_vect){
if(TOV3 == 0){ //kein Overflow aufgetreten
//Zwischenspeichern vom Timer3 Zaehlregister (=Zaehlergebnis=>Periodendauer der Radumdrehung)
HallPeriod = TCNT3;
//Reset Timer3 Zaehlregister
}
else{ //Overflow aufgetreten
HallPeriod = 0xFFFF; //Ergebnis soll Maximalwert sein =>langsamste messbare Raddrehzahl
TIFR3 &= ~(1 << TOV3); //Overflow-Flag loeschen
}
TCNT3 = 0; //Zaehlergebnis zuruecksetzen
}
/-----------------------------------------------------------/
int main( void )
{
//Test für Debugging
CANFIFO.data[0]=11;
CANFIFO.data[1]=11;
CANFIFO.data[2]=11;
CANFIFO.data[3]=31;
CANFIFO.data[4]=111;
CANFIFO.data[5]=11;
CANFIFO.data[10]=12;
CANFIFO.data[14]=13;
CANFIFO.data[15]=34;
CANFIFO.data[20]=99;
//Mutex für CANFIFO erstellen, damit nicht mehrere Tasks gleichzeitig auf CANFIFO zugreifen können
xFIFOmutex = xSemaphoreCreateMutex();
<span style="color: #408080; font-style: italic">//Initialisierungen</span>
vIO_Init();
vInitExtInt0();
vInitTimer3();
<span style="color: #408080; font-style: italic">//Reihenfolge wichtig!</span>
vInitSPI_SS ();
spi_init((<span style="color: #B00040">uint8_t</span>)(SPI_MASTER<span style="color: #666666">|</span>SPI_MSB_FIRST<span style="color: #666666">|</span>SPI_DATA_MODE_0<span style="color: #666666">|</span>SPI_CLKIO_BY_16));
vInit3DBS();
<span style="color: #408080; font-style: italic">//Reihenfolge wichtig!</span>
<span style="color: #408080; font-style: italic">//Create Tasks</span>
<span style="color: #408080; font-style: italic">// xTaskCreate (task function name, task name (choose freely), stack size, pointer to task parameters, task priority, pass back handle by reference)</span>
<span style="color: #408080; font-style: italic">//xTaskCreate( vLEDFlashTask, ( signed char * ) "LED", configMINIMAL_STACK_SIZE, NULL, mainLED_TASK_PRIORITY, NULL );</span>
xTaskCreate ( vRefreshSystemTime, ( <span style="color: #B00040">signed</span> <span style="color: #B00040">char</span> <span style="color: #666666">*</span> ) <span style="color: #BA2121">"SYSTIME"</span>, configMINIMAL_STACK_SIZE, ( <span style="color: #B00040">void</span> <span style="color: #666666">*</span> ) <span style="color: #666666">&</span>SysTime, mainTIME_TASK_PRIORITY, <span style="color: #008000">NULL</span>);
xTaskCreate ( vReadPortCTask, ( <span style="color: #B00040">signed</span> <span style="color: #B00040">char</span> <span style="color: #666666">*</span> ) <span style="color: #BA2121">"READC"</span>, configMINIMAL_STACK_SIZE, <span style="color: #008000">NULL</span>, mainREADC_TASK_PRIORITY, <span style="color: #008000">NULL</span>);
<span style="color: #408080; font-style: italic">//xTaskCreate ( vReadBattVolt, ( signed char * ) "ADCBATT", configMINIMAL_STACK_SIZE, NULL, mainREAD_TEMP_ADC_PRIORITY, NULL);</span>
<span style="color: #408080; font-style: italic">//xTaskCreate ( vReadIntakeTemp, ( signed char * ) "INTEMP", configMINIMAL_STACK_SIZE, NULL, mainREAD_TEMP_ADC_PRIORITY, NULL);</span>
<span style="color: #408080; font-style: italic">//xTaskCreate ( vReadHallPeriod, ( signed char * ) "HALL", configMINIMAL_STACK_SIZE, NULL, mainHALL_TASK_PRIORITY, NULL);</span>
<span style="color: #408080; font-style: italic">//xTaskCreate ( vReadSPITemp, ( signed char * ) "ET1", configMINIMAL_STACK_SIZE, ( void * ) SS0, mainREAD_SPI_TEMP_PRIORITY, NULL);</span>
<span style="color: #408080; font-style: italic">//xTaskCreate ( vReadSPITemp, ( signed char * ) "ET2", configMINIMAL_STACK_SIZE, ( void * ) SS1, mainREAD_SPI_TEMP_PRIORITY, NULL);</span>
<span style="color: #408080; font-style: italic">//xTaskCreate ( vReadSPITemp, ( signed char * ) "ET3", configMINIMAL_STACK_SIZE, ( void * ) SS2, mainREAD_SPI_TEMP_PRIORITY, NULL);</span>
<span style="color: #408080; font-style: italic">//xTaskCreate ( vReadSPITemp, ( signed char * ) "ET4", configMINIMAL_STACK_SIZE, ( void * ) SS3, mainREAD_SPI_TEMP_PRIORITY, NULL);</span>
<span style="color: #408080; font-style: italic">//xTaskCreate ( vRead3DBS, ( signed char * ) "3DBS", configMINIMAL_STACK_SIZE, NULL, mainREAD_SPI_3DBS_PRIORITY, NULL);</span>
<span style="color: #408080; font-style: italic">//Create Co-Routines</span>
<span style="color: #408080; font-style: italic">//xCoRoutineCreate (co-routine name, co-routine priority, index)</span>
<span style="color: #408080; font-style: italic">//xCoRoutineCreate( vFlashCoRoutine, 0, 0 );</span>
HallPeriod <span style="color: #666666">=</span> <span style="color: #666666">0xFFFF</span>; <span style="color: #408080; font-style: italic">//Maximalwert =>langsamste messbare Raddrehzahl</span>
sei();
vTaskStartScheduler();
<span style="color: #008000; font-weight: bold">while</span> (<span style="color: #666666">1</span>){};
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #666666">0</span>;
}//int main( void )
/-----------------------------------------------------------/
//Implementation of tasks
//...