2009-11-28 10:28:11 +01:00
|
|
|
/**
|
2009-11-29 11:03:08 +01:00
|
|
|
******************************************************************************
|
2009-11-28 12:07:38 +01:00
|
|
|
*
|
2009-11-29 11:03:08 +01:00
|
|
|
* @file pios.c
|
2010-01-31 18:56:54 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2009-11-29 11:03:08 +01:00
|
|
|
* @brief Sets up main tasks, tickhook, and contains the Main function.
|
|
|
|
* - It all starts from here!
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2009-11-29 13:38:42 +01:00
|
|
|
*
|
2009-11-29 11:03:08 +01:00
|
|
|
*****************************************************************************/
|
2009-11-28 10:28:11 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Project Includes */
|
|
|
|
#include "pios.h"
|
|
|
|
|
2009-12-02 12:07:56 +01:00
|
|
|
/* OpenPilot Includes */
|
2009-12-11 19:48:48 +01:00
|
|
|
#include <openpilot.h>
|
2009-11-28 10:28:11 +01:00
|
|
|
|
2009-12-02 12:07:56 +01:00
|
|
|
/* Task Priorities */
|
2010-01-25 16:23:55 +01:00
|
|
|
#define PRIORITY_TASK_HOOKS (tskIDLE_PRIORITY + 3)
|
2009-11-28 10:28:11 +01:00
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
/* Global Variables */
|
2009-12-02 22:49:31 +01:00
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
/* Local Variables */
|
|
|
|
#define STRING_MAX 1024
|
|
|
|
static uint8_t line_buffer[STRING_MAX];
|
|
|
|
static uint16_t line_ix;
|
2010-01-31 17:48:23 +01:00
|
|
|
static uint8_t sdcard_available;
|
2009-12-04 21:33:18 +01:00
|
|
|
|
2009-12-02 07:44:53 +01:00
|
|
|
/* Function Prototypes */
|
2010-01-25 16:23:55 +01:00
|
|
|
static void TaskTick(void *pvParameters);
|
|
|
|
static void TaskHooks(void *pvParameters);
|
|
|
|
int32_t CONSOLE_Parse(COMPortTypeDef port, char c);
|
|
|
|
void OP_ADC_NotifyChange(uint32_t pin, uint32_t pin_value);
|
2010-01-31 17:48:23 +01:00
|
|
|
static void TaskSDCard(void *pvParameters);
|
2009-12-23 08:39:34 +01:00
|
|
|
|
2009-12-02 07:44:53 +01:00
|
|
|
/**
|
|
|
|
* Main function
|
|
|
|
*/
|
2009-11-28 10:28:11 +01:00
|
|
|
int main()
|
|
|
|
{
|
2010-01-23 18:31:14 +01:00
|
|
|
/* Brings up System using CMSIS functions, enables the LEDs. */
|
2009-12-08 03:13:21 +01:00
|
|
|
PIOS_SYS_Init();
|
2009-12-22 20:32:47 +01:00
|
|
|
|
2009-12-24 03:51:20 +01:00
|
|
|
/* Delay system */
|
|
|
|
PIOS_DELAY_Init();
|
2009-12-23 11:34:26 +01:00
|
|
|
|
2010-01-23 18:31:14 +01:00
|
|
|
/* SPI Init */
|
|
|
|
PIOS_SPI_Init();
|
|
|
|
|
2009-12-22 20:32:47 +01:00
|
|
|
/* Enables the SDCard */
|
2010-01-21 00:48:04 +01:00
|
|
|
PIOS_SDCARD_Init();
|
|
|
|
|
2010-01-24 09:13:48 +01:00
|
|
|
/* Wait for SD card for ever */
|
|
|
|
for(;;)
|
|
|
|
{
|
2010-01-25 17:30:31 +01:00
|
|
|
/* Check if we have an SD Card with the correct settings files on it */
|
|
|
|
if(!PIOS_SDCARD_MountFS(STARTUP_LOG_ENABLED) && !PIOS_Settings_CheckForFiles()) {
|
2010-01-24 09:13:48 +01:00
|
|
|
/* Found one without errors */
|
|
|
|
break;
|
|
|
|
}
|
2010-01-23 18:31:14 +01:00
|
|
|
|
2010-01-24 09:13:48 +01:00
|
|
|
/* SD Card not found, flash for 1 second */
|
|
|
|
PIOS_LED_On(LED1);
|
|
|
|
PIOS_LED_On(LED2);
|
|
|
|
for(uint32_t i = 0; i < 10; i++) {
|
|
|
|
PIOS_LED_Toggle(LED2);
|
2010-01-31 17:48:23 +01:00
|
|
|
PIOS_DELAY_WaitmS(100);
|
2010-01-24 09:13:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call LoadSettings which populates global variables so the rest of the hardware can be configured. */
|
2010-01-21 03:04:15 +01:00
|
|
|
PIOS_Settings_Load();
|
2010-01-21 00:48:04 +01:00
|
|
|
|
2009-12-21 04:57:30 +01:00
|
|
|
/* Com ports init */
|
2010-01-24 14:03:13 +01:00
|
|
|
PIOS_COM_Init();
|
|
|
|
|
|
|
|
/* Initialise servo outputs */
|
|
|
|
PIOS_Servo_Init();
|
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
/* Analog to digital converter initialise */
|
2010-01-31 17:48:23 +01:00
|
|
|
//PIOS_ADC_Init();
|
2010-01-25 16:23:55 +01:00
|
|
|
|
|
|
|
//PIOS_PWM_Init();
|
|
|
|
|
2010-01-31 17:48:23 +01:00
|
|
|
PIOS_USB_Init(0);
|
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
PIOS_COM_ReceiveCallbackInit(CONSOLE_Parse);
|
|
|
|
|
|
|
|
/* Initialise OpenPilot application */
|
|
|
|
// OpenPilotInit();
|
|
|
|
|
|
|
|
/* Create a FreeRTOS task */
|
2010-01-31 17:48:23 +01:00
|
|
|
//xTaskCreate(TaskTick, (signed portCHAR *)"Test", configMINIMAL_STACK_SIZE , NULL, 1, NULL);
|
|
|
|
//xTaskCreate(TaskHooks, (signed portCHAR *)"Hooks", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_HOOKS, NULL);
|
|
|
|
xTaskCreate(TaskSDCard, (signed portCHAR *)"SDCard", configMINIMAL_STACK_SIZE, NULL, (tskIDLE_PRIORITY + 2), NULL);
|
2010-01-25 16:23:55 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CONSOLE_Parse(COMPortTypeDef port, char c)
|
|
|
|
{
|
|
|
|
if(c == '\r') {
|
|
|
|
/* Ignore */
|
|
|
|
} else if(c == '\n') {
|
|
|
|
PIOS_COM_SendFormattedString(GPS, "String: %s\n", line_buffer);
|
|
|
|
line_ix = 0;
|
|
|
|
} else if(line_ix < (STRING_MAX - 1)) {
|
|
|
|
line_buffer[line_ix++] = c;
|
|
|
|
line_buffer[line_ix] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No error */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OP_ADC_NotifyChange(uint32_t pin, uint32_t pin_value)
|
|
|
|
{
|
|
|
|
/* HW v1.0 GPS/IR Connector
|
|
|
|
0000000
|
|
|
|
|||||||-- 5V
|
|
|
|
||||||--- TX (RXD on FDTI)
|
|
|
|
|||||---- RX (TXD on FDTI)
|
|
|
|
||||----- ADC PIN 3 (PC0)
|
|
|
|
|||------ ADC PIN 0 (PC1)
|
|
|
|
||------- ADC PIN 1 (PC2)
|
|
|
|
|-------- GND
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskTick(void *pvParameters)
|
|
|
|
{
|
2010-01-31 17:48:23 +01:00
|
|
|
portTickType xLastExecutionTime;
|
2010-01-25 16:23:55 +01:00
|
|
|
|
|
|
|
/* Setup the LEDs to Alternate */
|
|
|
|
PIOS_LED_On(LED1);
|
|
|
|
PIOS_LED_Off(LED2);
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
PIOS_LED_Toggle(LED1);
|
2010-01-31 17:48:23 +01:00
|
|
|
vTaskDelayUntil(&xLastExecutionTime, 500 / portTICK_RATE_MS);
|
2010-01-25 16:23:55 +01:00
|
|
|
}
|
|
|
|
|
2010-01-31 17:48:23 +01:00
|
|
|
#if 0
|
|
|
|
/* For testing servo outputs */
|
2010-01-25 16:23:55 +01:00
|
|
|
const portTickType xDelay = 1 / portTICK_RATE_MS;
|
|
|
|
|
|
|
|
Used to test servos, cycles all servos from one side to the other
|
2010-01-24 14:03:13 +01:00
|
|
|
for(;;) {
|
|
|
|
for(int i = 1000; i < 2000; i++) {
|
|
|
|
PIOS_Servo_Set(0, i);
|
|
|
|
PIOS_Servo_Set(1, i);
|
|
|
|
PIOS_Servo_Set(2, i);
|
|
|
|
PIOS_Servo_Set(3, i);
|
|
|
|
PIOS_Servo_Set(4, i);
|
|
|
|
PIOS_Servo_Set(5, i);
|
|
|
|
PIOS_Servo_Set(6, i);
|
|
|
|
PIOS_Servo_Set(7, i);
|
2010-01-25 16:23:55 +01:00
|
|
|
vTaskDelay(xDelay);
|
2010-01-24 14:03:13 +01:00
|
|
|
}
|
|
|
|
for(int i = 2000; i > 1000; i--) {
|
|
|
|
PIOS_Servo_Set(0, i);
|
|
|
|
PIOS_Servo_Set(1, i);
|
|
|
|
PIOS_Servo_Set(2, i);
|
|
|
|
PIOS_Servo_Set(3, i);
|
|
|
|
PIOS_Servo_Set(4, i);
|
|
|
|
PIOS_Servo_Set(5, i);
|
|
|
|
PIOS_Servo_Set(6, i);
|
|
|
|
PIOS_Servo_Set(7, i);
|
2010-01-25 16:23:55 +01:00
|
|
|
vTaskDelay(xDelay);
|
2010-01-24 14:03:13 +01:00
|
|
|
}
|
2010-01-25 16:23:55 +01:00
|
|
|
}
|
2010-01-31 17:48:23 +01:00
|
|
|
#endif
|
2010-01-25 16:23:55 +01:00
|
|
|
}
|
2010-01-23 18:31:14 +01:00
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
static void TaskHooks(void *pvParameters)
|
|
|
|
{
|
|
|
|
portTickType xLastExecutionTime;
|
2009-12-24 03:51:20 +01:00
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
// Initialise the xLastExecutionTime variable on task entry
|
|
|
|
xLastExecutionTime = xTaskGetTickCount();
|
2009-11-28 10:28:11 +01:00
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
for(;;) {
|
|
|
|
vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS);
|
2009-11-28 10:28:11 +01:00
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
/* Skip delay gap if we had to wait for more than 5 ticks to avoid */
|
|
|
|
/* unnecessary repeats until xLastExecutionTime reached xTaskGetTickCount() again */
|
|
|
|
portTickType xCurrentTickCount = xTaskGetTickCount();
|
|
|
|
if(xLastExecutionTime < (xCurrentTickCount - 5))
|
|
|
|
xLastExecutionTime = xCurrentTickCount;
|
2009-12-24 03:51:20 +01:00
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
/* Check for incoming COM messages */
|
|
|
|
PIOS_COM_ReceiveHandler();
|
2009-12-23 08:14:52 +01:00
|
|
|
|
2010-01-25 16:23:55 +01:00
|
|
|
/* Check for incoming ADC notifications */
|
|
|
|
PIOS_ADC_Handler(OP_ADC_NotifyChange);
|
2010-01-21 00:48:04 +01:00
|
|
|
}
|
2009-12-02 22:49:31 +01:00
|
|
|
}
|
|
|
|
|
2010-01-31 17:48:23 +01:00
|
|
|
static void TaskSDCard(void *pvParameters)
|
|
|
|
{
|
|
|
|
uint16_t second_delay_ctr = 0;
|
|
|
|
portTickType xLastExecutionTime;
|
|
|
|
|
|
|
|
/* Initialise the xLastExecutionTime variable on task entry */
|
|
|
|
xLastExecutionTime = xTaskGetTickCount();
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS);
|
|
|
|
|
|
|
|
/* Each second: */
|
|
|
|
/* Check if SD card is available */
|
|
|
|
/* High-speed access if SD card was previously available */
|
|
|
|
if(++second_delay_ctr >= 1000) {
|
|
|
|
second_delay_ctr = 0;
|
|
|
|
|
|
|
|
uint8_t prev_sdcard_available = sdcard_available;
|
|
|
|
sdcard_available = PIOS_SDCARD_CheckAvailable(prev_sdcard_available);
|
|
|
|
|
|
|
|
if(sdcard_available && !prev_sdcard_available) {
|
|
|
|
/* SD Card has been connected! */
|
|
|
|
/* Switch to mass storage device */
|
|
|
|
MSD_Init(0);
|
|
|
|
} else if(!sdcard_available && prev_sdcard_available) {
|
2010-01-31 18:56:54 +01:00
|
|
|
/* Re-init USB for HID */
|
2010-01-31 17:48:23 +01:00
|
|
|
PIOS_USB_Init(1);
|
|
|
|
/* SD Card disconnected! */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Each millisecond: */
|
|
|
|
/* Handle USB access if device is available */
|
|
|
|
if(sdcard_available) {
|
|
|
|
MSD_Periodic_mS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-02 22:49:31 +01:00
|
|
|
/**
|
|
|
|
* Idle hook function
|
|
|
|
*/
|
|
|
|
void vApplicationIdleHook(void)
|
|
|
|
{
|
|
|
|
/* Called when the scheduler has no tasks to run */
|
2009-12-15 13:17:11 +01:00
|
|
|
|
2009-12-02 22:49:31 +01:00
|
|
|
|
|
|
|
}
|
2009-12-15 13:17:11 +01:00
|
|
|
|