summaryrefslogtreecommitdiff
path: root/software/mpu/src/Kopie von main.c
blob: bb1399b13935eee5fa3703bef89a65fa36521bb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
===============================================================================
 Name        : main.c
 Author      : 
 Version     :
 Copyright   : Copyright (C)
 Description : main definition
===============================================================================
*/

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

/* SSP includes */
#include "ssp.h"

/* Priorities at which the tasks are created. */
#define mainQUEUE_RECEIVE_TASK_PRIORITY		( tskIDLE_PRIORITY + 2 )
#define	mainQUEUE_SEND_TASK_PRIORITY		( tskIDLE_PRIORITY + 1 )
#define	mainQUEUE_SSP_TASK_PRIORITY		    ( tskIDLE_PRIORITY + 2 )

#define configSND_TASK_STACK_SIZE			( ( unsigned short ) 32 )
#define configRCV_TASK_STACK_SIZE			( ( unsigned short ) 32 )
#define configSPI_LOOP_TASK_STACK_SIZE		( ( unsigned short ) 32 )

/* The bit of port 0 that the LPCXpresso LPC13xx LED is connected. */
#define mainLED_BIT 						( 7 )

/* The rate at which data is sent to the queue, specified in milliseconds. */
#define mainQUEUE_SEND_FREQUENCY_MS			( 500 / portTICK_RATE_MS )

/* The number of items the queue can hold.  This is 1 as the receive task
will remove items as they are added, meaning the send task should always find
the queue empty. */
#define mainQUEUE_LENGTH					( 1 )

/*
 * The tasks.
 */
static void prvQueueReceiveTask( void *pvParameters );
static void prvQueueSendTask( void *pvParameters );
static void prvLoopBackTestTask( void *pvParameters );

/*
 * Simple function to toggle the LED on the LPCXpresso LPC13xx board.
 */
static void prvToggleLED( void );

/* The queue used by both tasks. */
static xQueueHandle xQueue = NULL;

/* SSP buffer */
 uint8_t src_addr[SSP_BUFSIZE];
 uint8_t dest_addr[SSP_BUFSIZE];
/*-----------------------------------------------------------*/

int main(void) {
	
	/* Set the LPCXpresso LPC13xx LED port to output. */
	LPC_GPIO0->DIR |= ( 0x1 << mainLED_BIT );

	/* Create the queue. */
	xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );

	if( xQueue != NULL )
	{
		/* Start the two tasks as described in the accompanying application
		note. */
		xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configRCV_TASK_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
		xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configSND_TASK_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
		xTaskCreate( prvLoopBackTestTask, ( signed char * ) "LoopBckTst", configSPI_LOOP_TASK_STACK_SIZE, NULL, mainQUEUE_SSP_TASK_PRIORITY, NULL );

		/* Start the tasks running. */
		vTaskStartScheduler();
	}

	/* If all is well we will never reach here as the scheduler will now be
	running.  If we do reach here then it is likely that there was insufficient
	heap available for the idle task to be created. */
	for( ;; );
/*
	// Enter an infinite loop, just incrementing a counter
	volatile static int i = 0 ;
	while(1) {
		i++ ;
	}
*/
	return 0 ;
}


/*
 *  -------------------------------------------------------------
 */
static void prvQueueSendTask( void *pvParameters )
{
	portTickType xNextWakeTime;
	unsigned long ulValueToSend = 100UL;

	/* Initialise xNextWakeTime - this only needs to be done once. */
	xNextWakeTime = xTaskGetTickCount();

	for( ;; )
	{
		/* Place this task in the blocked state until it is time to run again.
		The block state is specified in ticks, the constant used converts ticks
		to ms.  While in the blocked state this task will not consume any CPU
		time. */
		vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );

		/* Send to the queue - causing the queue receive task to flash its LED.
		0 is used as the block time so the sending operation will not block -
		it shouldn't need to block as the queue should always be empty at this
		point in the code. */
		if(ulValueToSend == 200UL)
			ulValueToSend = 100UL;
		else
			ulValueToSend = 200UL;
		xQueueSend( xQueue, &ulValueToSend, 0 );

	}
}
/*-----------------------------------------------------------*/

static void prvQueueReceiveTask( void *pvParameters )
{
unsigned long ulReceivedValue;

	for( ;; )
	{
		/* Wait until something arrives in the queue - this task will block
		indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
		FreeRTOSConfig.h. */
		xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );

		/*  To get here something must have been received from the queue, but
		is it the expected value?  If it is, toggle the LED. */
		if( ulReceivedValue == 100UL )
		{
			prvToggleLED();
		}
	}
}
/*-----------------------------------------------------------*/
static void prvLoopBackTestTask( void *pvParameters )
{
	uint8_t u;
	unsigned long ulReceivedValue;
	portTickType xNextWakeTime;

	xNextWakeTime = xTaskGetTickCount();
	u = 3;
	SSPInit();

	for(;;)
	{
		xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
		if(ulReceivedValue == 200UL)
		{
			prvToggleLED();
			vTaskDelayUntil( &xNextWakeTime, (500 / portTICK_RATE_MS) );
			prvToggleLED();
			vTaskDelayUntil( &xNextWakeTime, (500 / portTICK_RATE_MS) );
			prvToggleLED();
		}
		vTaskDelayUntil( &xNextWakeTime, (100 / portTICK_RATE_MS) );
		u++;
		LoopbackTest();
	}
}
/*-----------------------------------------------------------*/

static void prvToggleLED( void )
{
/* This function is only called from one task so does not have to be reentrant,
so the static qualifier is acceptable. */
static unsigned long ulLEDState = 1;

	/* Turn the LED off if it was on, and on if it was off. */
	ulLEDState = !ulLEDState;
	LPC_GPIO0->MASKED_ACCESS[ ( 1 << mainLED_BIT) ] = ( ulLEDState << mainLED_BIT );
}
/*-----------------------------------------------------------*/