2010-03-10 22:05:19 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file openpilot.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Sets up ans runs main OpenPilot tasks.
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I2C Test: communicate with external PCF8570 ram chip
|
|
|
|
* For this test to function, PCF8570 chips need to be attached to the I2C port
|
|
|
|
*
|
|
|
|
* Blinking Blue LED: No errors detected, test continues
|
|
|
|
* Blinking Red LED: Error detected, test stopped
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* OpenPilot Includes */
|
|
|
|
#include "openpilot.h"
|
|
|
|
|
2010-05-22 08:38:05 +02:00
|
|
|
//#define USE_DEBUG_PINS
|
2010-03-12 08:36:27 +01:00
|
|
|
|
2010-05-01 16:57:26 +02:00
|
|
|
#define DEVICE_1_ADDRESS 0x50
|
|
|
|
#define DEVICE_2_ADDRESS 0x51
|
|
|
|
|
2010-03-12 08:36:27 +01:00
|
|
|
#ifdef USE_DEBUG_PINS
|
2010-05-16 20:07:41 +02:00
|
|
|
#define DEBUG_PIN_TASK1_WAIT 0
|
|
|
|
#define DEBUG_PIN_TASK1_LOCKED 1
|
|
|
|
#define DEBUG_PIN_TASK2_WAIT 2
|
|
|
|
#define DEBUG_PIN_TASK2_LOCKED 3
|
|
|
|
#define DEBUG_PIN_TRANSFER 4
|
|
|
|
#define DEBUG_PIN_IDLE 6
|
|
|
|
#define DEBUG_PIN_ERROR 7
|
2010-03-12 08:36:27 +01:00
|
|
|
#define DebugPinHigh(x) PIOS_DEBUG_PinHigh(x)
|
|
|
|
#define DebugPinLow(x) PIOS_DEBUG_PinLow(x)
|
|
|
|
#else
|
|
|
|
#define DebugPinHigh(x)
|
|
|
|
#define DebugPinLow(x)
|
|
|
|
#endif
|
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define MAX_LOCK_WAIT 2 // Time in ms that a thread can normally block I2C
|
|
|
|
|
|
|
|
/* Task Priorities */
|
|
|
|
#define PRIORITY_TASK_HOOKS (tskIDLE_PRIORITY + 3)
|
|
|
|
|
|
|
|
|
2010-03-10 22:05:19 +01:00
|
|
|
/* Global Variables */
|
|
|
|
|
|
|
|
/* Local Variables */
|
|
|
|
|
|
|
|
/* Function Prototypes */
|
2010-05-01 16:57:26 +02:00
|
|
|
static void Task1(void *pvParameters);
|
|
|
|
static void Task2(void *pvParameters);
|
2010-03-10 22:05:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenPilot Main function
|
|
|
|
*/
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Init
|
|
|
|
PIOS_SYS_Init();
|
|
|
|
PIOS_DEBUG_Init();
|
|
|
|
PIOS_DELAY_Init();
|
|
|
|
PIOS_COM_Init();
|
|
|
|
PIOS_USB_Init(0);
|
|
|
|
PIOS_I2C_Init();
|
|
|
|
|
2010-05-01 16:57:26 +02:00
|
|
|
// Both leds off
|
|
|
|
PIOS_LED_Off(LED1);
|
|
|
|
PIOS_LED_Off(LED2);
|
|
|
|
|
2010-03-10 22:05:19 +01:00
|
|
|
// Create task
|
2010-05-22 08:38:05 +02:00
|
|
|
xTaskCreate(Task1, (signed portCHAR *)"Task1", 1024 , NULL, 1, NULL);
|
|
|
|
xTaskCreate(Task2, (signed portCHAR *)"Task2", 1024 , NULL, 2, NULL);
|
2010-05-01 16:57:26 +02:00
|
|
|
|
2010-03-10 22:05:19 +01:00
|
|
|
|
|
|
|
// Start the FreeRTOS scheduler
|
|
|
|
vTaskStartScheduler();
|
|
|
|
|
|
|
|
/* If all is well we will never reach here as the scheduler will now be running. */
|
|
|
|
/* If we do get here, it will most likely be because we ran out of heap space. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnError(void)
|
|
|
|
{
|
|
|
|
PIOS_LED_Off(LED1);
|
|
|
|
while(1)
|
|
|
|
{
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_ERROR);
|
2010-03-10 22:05:19 +01:00
|
|
|
PIOS_LED_Toggle(LED2);
|
|
|
|
vTaskDelay(50 / portTICK_RATE_MS);
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_ERROR);
|
2010-03-10 22:05:19 +01:00
|
|
|
}
|
|
|
|
}
|
2010-05-22 08:38:05 +02:00
|
|
|
//
|
|
|
|
// This is a low priority task that will continuously access one I2C device
|
|
|
|
// Frequently it will release the I2C device so that others can also use I2C
|
|
|
|
//
|
2010-05-01 16:57:26 +02:00
|
|
|
static void Task1(void *pvParameters)
|
2010-03-10 22:05:19 +01:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
if (PIOS_I2C_LockDevice(MAX_LOCK_WAIT / portTICK_RATE_MS))
|
2010-05-03 20:18:43 +02:00
|
|
|
{
|
|
|
|
if (PIOS_I2C_Transfer(I2C_Write, DEVICE_1_ADDRESS<<1, (uint8_t*)"\x20\xB0\xB1\xB2", 4) != 0)
|
|
|
|
OnError();
|
|
|
|
PIOS_I2C_UnlockDevice();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OnError();
|
|
|
|
}
|
2010-03-10 22:05:19 +01:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
i++;
|
2010-05-01 16:57:26 +02:00
|
|
|
if (i==100)
|
2010-03-10 22:05:19 +01:00
|
|
|
{
|
|
|
|
PIOS_LED_Toggle(LED1);
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_TASK1_WAIT);
|
|
|
|
if (PIOS_I2C_LockDevice(MAX_LOCK_WAIT / portTICK_RATE_MS))
|
2010-03-10 22:05:19 +01:00
|
|
|
{
|
|
|
|
uint8_t buf[20];
|
2010-05-16 20:07:41 +02:00
|
|
|
|
|
|
|
DebugPinLow(DEBUG_PIN_TASK1_WAIT);
|
|
|
|
DebugPinHigh(DEBUG_PIN_TASK1_LOCKED);
|
|
|
|
|
|
|
|
// Write A0 A1 A2 at address 0x10
|
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-01 16:57:26 +02:00
|
|
|
if (PIOS_I2C_Transfer(I2C_Write, DEVICE_1_ADDRESS<<1, (uint8_t*)"\x10\xA0\xA1\xA2", 4) != 0)
|
2010-03-13 13:23:55 +01:00
|
|
|
OnError();
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
|
|
|
|
2010-03-13 13:23:55 +01:00
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
// Read 3 bytes at address 0x20 and check
|
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-01 16:57:26 +02:00
|
|
|
if (PIOS_I2C_Transfer(I2C_Write_WithoutStop, DEVICE_1_ADDRESS<<1, (uint8_t*)"\x20", 1) != 0)
|
2010-03-13 13:23:55 +01:00
|
|
|
OnError();
|
2010-05-22 08:38:05 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
2010-03-13 13:23:55 +01:00
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-01 16:57:26 +02:00
|
|
|
if (PIOS_I2C_Transfer(I2C_Read, DEVICE_1_ADDRESS<<1, buf, 3) != 0)
|
2010-03-13 13:23:55 +01:00
|
|
|
OnError();
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
2010-03-10 22:05:19 +01:00
|
|
|
|
|
|
|
if (memcmp(buf, "\xB0\xB1\xB2",3) != 0)
|
|
|
|
OnError();
|
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
// Read 3 bytes at address 0x10 and check
|
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-01 16:57:26 +02:00
|
|
|
if (PIOS_I2C_Transfer(I2C_Write_WithoutStop, DEVICE_1_ADDRESS<<1, (uint8_t*)"\x10", 1) != 0)
|
2010-03-13 13:23:55 +01:00
|
|
|
OnError();
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
2010-03-13 13:23:55 +01:00
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-01 16:57:26 +02:00
|
|
|
if (PIOS_I2C_Transfer(I2C_Read, DEVICE_1_ADDRESS<<1, buf, 3) != 0)
|
2010-03-13 13:23:55 +01:00
|
|
|
OnError();
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
2010-03-13 13:23:55 +01:00
|
|
|
|
2010-03-10 22:05:19 +01:00
|
|
|
if (memcmp(buf, "\xA0\xA1\xA2",3) != 0)
|
|
|
|
OnError();
|
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TASK1_LOCKED);
|
2010-05-01 16:54:35 +02:00
|
|
|
PIOS_I2C_UnlockDevice();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-01 16:57:26 +02:00
|
|
|
OnError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-22 08:38:05 +02:00
|
|
|
// This is a high priority task that will periodically perform some actions on the second I2C device
|
|
|
|
// Most of the time it will have to wait for the other task task to release I2C
|
2010-05-01 16:57:26 +02:00
|
|
|
static void Task2(void *pvParameters)
|
|
|
|
{
|
|
|
|
portTickType xLastExecutionTime;
|
|
|
|
|
|
|
|
xLastExecutionTime = xTaskGetTickCount();
|
2010-05-03 20:31:34 +02:00
|
|
|
uint32_t count = 0;
|
2010-05-01 16:57:26 +02:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
uint8_t buf[20];
|
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_TASK2_WAIT);
|
|
|
|
if (PIOS_I2C_LockDevice(MAX_LOCK_WAIT / portTICK_RATE_MS))
|
2010-05-01 16:57:26 +02:00
|
|
|
{
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TASK2_WAIT);
|
|
|
|
DebugPinHigh(DEBUG_PIN_TASK2_LOCKED);
|
|
|
|
|
|
|
|
// Write value of count to address 0x10
|
2010-05-03 20:31:34 +02:00
|
|
|
buf[0] = 0x10; // The address
|
|
|
|
memcpy(&buf[1], &count, 4); // The data to write
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-03 20:31:34 +02:00
|
|
|
if (PIOS_I2C_Transfer(I2C_Write, DEVICE_2_ADDRESS<<1, buf, 5) != 0)
|
2010-05-01 16:57:26 +02:00
|
|
|
OnError();
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
|
|
|
|
|
|
|
DebugPinLow(DEBUG_PIN_TASK2_LOCKED);
|
2010-05-01 16:57:26 +02:00
|
|
|
PIOS_I2C_UnlockDevice();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OnError();
|
|
|
|
}
|
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
vTaskDelay(2 / portTICK_RATE_MS);
|
2010-05-01 16:57:26 +02:00
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_TASK2_WAIT);
|
|
|
|
if (PIOS_I2C_LockDevice(1 / portTICK_RATE_MS))
|
2010-05-01 16:57:26 +02:00
|
|
|
{
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TASK2_WAIT);
|
|
|
|
DebugPinHigh(DEBUG_PIN_TASK2_LOCKED);
|
|
|
|
|
|
|
|
// Read at address 0x10 and check
|
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-03 20:18:43 +02:00
|
|
|
if (PIOS_I2C_Transfer(I2C_Write_WithoutStop, DEVICE_2_ADDRESS<<1, (uint8_t*)"\x10", 1) != 0)
|
2010-05-01 16:57:26 +02:00
|
|
|
OnError();
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
2010-05-01 16:57:26 +02:00
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-03 20:31:34 +02:00
|
|
|
if (PIOS_I2C_Transfer(I2C_Read, DEVICE_2_ADDRESS<<1, buf, 4) != 0)
|
2010-05-01 16:57:26 +02:00
|
|
|
OnError();
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
2010-05-01 16:57:26 +02:00
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinHigh(DEBUG_PIN_TRANSFER);
|
2010-05-03 20:31:34 +02:00
|
|
|
if (memcmp(buf, &count, 4) != 0)
|
2010-05-01 16:57:26 +02:00
|
|
|
OnError();
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TRANSFER);
|
|
|
|
|
2010-05-03 20:31:34 +02:00
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
DebugPinLow(DEBUG_PIN_TASK2_LOCKED);
|
2010-05-01 16:57:26 +02:00
|
|
|
PIOS_I2C_UnlockDevice();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-01 16:54:35 +02:00
|
|
|
OnError();
|
2010-03-10 22:05:19 +01:00
|
|
|
}
|
|
|
|
|
2010-05-16 20:07:41 +02:00
|
|
|
vTaskDelay(5 / portTICK_RATE_MS);
|
2010-05-03 20:31:34 +02:00
|
|
|
|
|
|
|
count++;
|
2010-03-10 22:05:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Idle hook function
|
|
|
|
*/
|
|
|
|
void vApplicationIdleHook(void)
|
|
|
|
{
|
|
|
|
/* Called when the scheduler has no tasks to run */
|
2010-03-12 08:36:27 +01:00
|
|
|
DebugPinHigh(DEBUG_PIN_IDLE);
|
|
|
|
DebugPinLow(DEBUG_PIN_IDLE);
|
2010-03-10 22:05:19 +01:00
|
|
|
}
|
2010-05-01 16:45:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the RTOS when a stack overflow is detected.
|
|
|
|
*/
|
|
|
|
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )
|
|
|
|
{
|
|
|
|
OnError();
|
|
|
|
}
|