1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-17 02:52:12 +01:00

Added variety of helper functions to PIOS_SDCARD.

Made minGlue even more robust and simpler.
Fixed a small bug with the servo output code, which is now working 100%.
Enabled USART, which can now be used for debugging.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@138 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
gussy 2010-01-24 13:03:13 +00:00 committed by gussy
parent 39403ee20d
commit c1ab22f595
7 changed files with 214 additions and 105 deletions

View File

@ -43,15 +43,14 @@ int dosfs_ini_close(PFILEINFO file)
int dosfs_ini_read(char *buffer, int size, PFILEINFO file)
{
DFS_ReadFile(file, Sector, (uint8_t *)buffer, &SuccessCount, size);
if(SuccessCount == size) {
/* No errors */
return 1;
} else {
/* Reached EOF */
if(PIOS_SDCARD_ReadLine(file, (uint8_t *)buffer, size) < 0)
{
/* Error reading line */
return 0;
}
/* No errors */
return 1;
}
int dosfs_ini_write(char *buffer, PFILEINFO file)
@ -65,63 +64,22 @@ int dosfs_ini_write(char *buffer, PFILEINFO file)
int dosfs_ini_rename(const char *source, const char *dest)
{
/* TODO: Check this works */
FILEINFO SourceFile, DestFile;
/* Disable caching to avoid file inconsistencies while using different sector buffers! */
DFS_CachingEnabledSet(0);
if(DFS_OpenFile(&VolInfo, (uint8_t *)source, DFS_READ, Sector, &SourceFile)) {
/* Source file doesn't exist */
return 1;
} else {
/* Delete destination file if it already exists - ignore errors */
DFS_UnlinkFile(&VolInfo, (uint8_t *)dest, Sector);
if(DFS_OpenFile(&VolInfo, (uint8_t *)dest, DFS_WRITE, Sector, &DestFile)) {
/* Failed to create destination file */
return 1;
}
if(PIOS_SDCARD_FileCopy((char *)source, (char *)dest)) {
/* Error renaming file */
return 0;
}
/* Copy operation */
uint8_t WriteBuffer[SECTOR_SIZE];
uint32_t SuccessCountRead;
uint32_t SuccessCountWrite;
do {
if(DFS_ReadFile(&SourceFile, Sector, WriteBuffer, &SuccessCountRead, SECTOR_SIZE)) {
/* DFS_ReadFile failed */
return 1;
} else if(DFS_WriteFile(&DestFile, Sector, WriteBuffer, &SuccessCountWrite, SuccessCountRead)) {
/* DFS_WriteFile failed */
return 1;
}
} while(SuccessCountRead > 0);
/* No errors */
return 1;
}
int dosfs_ini_remove(const char *filename)
{
/* TODO: Check this works */
VOLINFO vi;
uint32_t pstart, psize;
uint8_t pactive, ptype;
pstart = DFS_GetPtnStart(0, Sector, 0, &pactive, &ptype, &psize);
if (pstart == 0xffffffff) {
/* Cannot find first partition */
return 0;
}
if(DFS_GetVolInfo(0, Sector, pstart, &vi) != DFS_OK) {
/* No volume information */
return 0;
}
/* Remove the file */
DFS_UnlinkFile(&vi, (uint8_t *)filename, Sector);
if(PIOS_SDCARD_FileDelete((char *)filename)) {
/* Error deleting file */
return 0;
}
/* No errors */
return 1;

View File

@ -769,11 +769,8 @@ int32_t PIOS_SDCARD_StartupLog(void)
char Buffer[1024];
uint32_t Cache;
/* Delete the file if it exists */
if(DFS_UnlinkFile(&VolInfo, (uint8_t *)LOG_FILENAME, Sector)) {
/* Error deleting file */
return -1;
}
/* Delete the file if it exists - ignore errors */
DFS_UnlinkFile(&VolInfo, (uint8_t *)LOG_FILENAME, Sector);
if(DFS_OpenFile(&VolInfo, (uint8_t *)LOG_FILENAME, DFS_WRITE, Sector, &File)) {
/* Error opening file */
@ -805,7 +802,6 @@ int32_t PIOS_SDCARD_StartupLog(void)
return 0;
}
/**
* Mounts the file system
* param[in] CreateStartupLog 1 = True, 0 = False
@ -825,8 +821,6 @@ int32_t PIOS_SDCARD_MountFS(uint32_t CreateStartupLog)
return -1;
}
/* Connected */
pstart = DFS_GetPtnStart(0, Sector, 0, &pactive, &ptype, &psize);
if (pstart == 0xffffffff) {
/* Cannot find first partition */
@ -871,3 +865,125 @@ int32_t PIOS_SDCARD_GetFree(void)
return VolFreeBytes;
}
/**
* Read from file
* return 0 No error
* return -1 DFS_ReadFile failed
* return -2 Less bytes read than expected
*/
int32_t PIOS_SDCARD_ReadBuffer(PFILEINFO fileinfo, uint8_t *buffer, uint32_t len)
{
uint32_t SuccessCount;
if(DFS_ReadFile(fileinfo, Sector, buffer, &SuccessCount, len)) {
/* DFS_ReadFile failed */
return -1;
}
if(SuccessCount != len) {
/* Less bytes read than expected */
return -2;
}
/* No error */
return 0;
}
/**
* Read a line from file
* returns Number of bytes read
*/
int32_t PIOS_SDCARD_ReadLine(PFILEINFO fileinfo, uint8_t *buffer, uint32_t max_len)
{
int32_t status;
uint32_t num_read = 0;
while(fileinfo->pointer < fileinfo->filelen) {
status = PIOS_SDCARD_ReadBuffer(fileinfo, buffer, 1);
if(status < 0) {
return status;
}
++num_read;
if(*buffer == '\n' || *buffer == '\r') {
break;
}
if(num_read < max_len) {
++buffer;
}
}
/* Replace newline by terminator */
*buffer = 0;
return num_read;
}
/**
* Copy a file
* WARNING: This will overwrite the destination file even if it exists
* param[in] *Source Path to file to copy
* param[in] *Destination Path to destination file
* return 0 No errors
* return -1 Source file doesn't exist
* return -2 Failed to create destination file
* return -3 DFS_ReadFile failed
* return -4 DFS_WriteFile failed
*/
int32_t PIOS_SDCARD_FileCopy(char *Source, char *Destination)
{
FILEINFO SourceFile, DestFile;
/* Disable caching to avoid file inconsistencies while using different sector buffers! */
DFS_CachingEnabledSet(0);
if(DFS_OpenFile(&VolInfo, (uint8_t *)Source, DFS_READ, Sector, &SourceFile)) {
/* Source file doesn't exist */
return -1;
} else {
/* Delete destination file if it already exists - ignore errors */
DFS_UnlinkFile(&VolInfo, (uint8_t *)Destination, Sector);
if(DFS_OpenFile(&VolInfo, (uint8_t *)Destination, DFS_WRITE, Sector, &DestFile)) {
/* Failed to create destination file */
return -2;
}
}
/* Copy operation */
uint8_t WriteBuffer[SECTOR_SIZE];
uint32_t SuccessCountRead;
uint32_t SuccessCountWrite;
do {
if(DFS_ReadFile(&SourceFile, Sector, WriteBuffer, &SuccessCountRead, SECTOR_SIZE)) {
/* DFS_ReadFile failed */
return -3;
} else if(DFS_WriteFile(&DestFile, Sector, WriteBuffer, &SuccessCountWrite, SuccessCountRead)) {
/* DFS_WriteFile failed */
return -4;
}
} while(SuccessCountRead > 0);
/* No errors */
return 0;
}
/**
* Delete a file
* param[in] *Filename File to delete
* return 0 No errors
* return -1 Error deleting file
*/
int32_t PIOS_SDCARD_FileDelete(char *Filename)
{
if(DFS_UnlinkFile(&VolInfo, (uint8_t *)Filename, Sector)) {
/* Error deleting file */
return -1;
}
/* No errors */
return 0;
}

View File

@ -46,7 +46,7 @@ void PIOS_Servo_Init(void)
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Pin = SERVO1_PIN | SERVO2_PIN | SERVO3_PIN | SERVO4_PIN;
GPIO_Init(SERVO1TO4_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SERVO5_PIN | SERVO6_PIN | SERVO7_PIN | SERVO8_PIN;
@ -55,17 +55,17 @@ void PIOS_Servo_Init(void)
/* Initialise RCC Clocks (TIM4 and TIM8) */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
/* Initialise Timers TIM4 and TIM8 */
/* With a resolution of 1uS, period of 20mS (50Hz) */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = (PERIPHERAL_CLOCK / 1000000) - 1;
TIM_TimeBaseStructure.TIM_Prescaler = (MASTER_CLOCK / 1000000) - 1;
TIM_TimeBaseStructure.TIM_Period = (20000 - 1);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* Setup each timer seperatly */
/* Setup each timer separately */
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM4 */
@ -84,6 +84,7 @@ void PIOS_Servo_Init(void)
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_CtrlPWMOutputs(TIM4, ENABLE);
TIM_Cmd(TIM4, ENABLE);
/* TIM8 */
@ -102,7 +103,8 @@ void PIOS_Servo_Init(void)
TIM_OC4Init(TIM8, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_Cmd(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
TIM_Cmd(TIM8, ENABLE);
}
/**
@ -118,40 +120,41 @@ void PIOS_Servo_Set(uint8_t Servo, uint16_t Position)
/* Clip servo position */
if(Position < Settings.Servos.PositionMin) {
Position = Settings.Servos.PositionMin;
}
}
if(Position > Settings.Servos.PositionMax) {
Position = Settings.Servos.PositionMax;
}
}
/* Update the position */
ServoPosition[Servo] = Position;
switch(Servo)
{
case 0:
TIM_SetCompare1(TIM4, Position);
break;
case 1:
TIM_SetCompare2(TIM4, Position);
break;
case 2:
TIM_SetCompare3(TIM4, Position);
break;
case 3:
TIM_SetCompare4(TIM4, Position);
break;
case 4:
TIM_SetCompare1(TIM8, Position);
break;
case 5:
TIM_SetCompare2(TIM8, Position);
break;
case 6:
TIM_SetCompare3(TIM8, Position);
break;
case 7:
TIM_SetCompare4(TIM8, Position);
break;
case 0:
TIM_SetCompare1(TIM4, Position);
break;
case 1:
TIM_SetCompare2(TIM4, Position);
break;
case 2:
TIM_SetCompare3(TIM4, Position);
break;
case 3:
TIM_SetCompare4(TIM4, Position);
break;
case 4:
TIM_SetCompare1(TIM8, Position);
break;
case 5:
TIM_SetCompare2(TIM8, Position);
break;
case 6:
TIM_SetCompare3(TIM8, Position);
break;
case 7:
TIM_SetCompare4(TIM8, Position);
break;
}
}
}

View File

@ -30,8 +30,8 @@
#include "pios.h"
/* Private Function Prototypes */
/* Global Variables */
extern SettingsTypeDef Settings;
/* Local Variables */
static uint8_t rx_buffer[USART_NUM][USART_RX_BUFFER_SIZE];
@ -578,4 +578,4 @@ AUX_USART_IRQHANDLER_FUNC
AUX_USART_USART->CR1 &= ~(1 << 7);
}
}
}
}

View File

@ -87,7 +87,6 @@
// GPS USART
//-------------------------
#define GPS_USART USART2
#define GPS_BAUD 57600
#define GPS_GPIO_PORT GPIOA
#define GPS_RX_PIN GPIO_Pin_3
#define GPS_TX_PIN GPIO_Pin_2
@ -101,7 +100,6 @@
// Telemetry radio USART
//-------------------------
#define TELEM_USART USART3
#define TELEM_BAUD 115200
#define TELEM_GPIO_PORT GPIOC
#define TELEM_RX_PIN GPIO_Pin_11
#define TELEM_TX_PIN GPIO_Pin_10
@ -114,7 +112,6 @@
//-------------------------
// AUXSER USART (available instead of RX5/RX6)
//-------------------------
#define AUX_USART_BAUD 19200
#define AUX_USART_USART USART1
#define AUX_USART_GPIO_PORT GPIOA
#define AUX_USART_RX_PIN GPIO_Pin_10
@ -131,7 +128,7 @@
#define USART_NUM 3
#define USART_RX_BUFFER_SIZE 1024
#define USART_TX_BUFFER_SIZE 256
#define COM_DEBUG_PORT TELEM
#define COM_DEBUG_PORT GPS
//-------------------------
// SPI

View File

@ -96,8 +96,14 @@ extern int32_t PIOS_SDCARD_SectorRead(uint32_t sector, uint8_t *buffer);
extern int32_t PIOS_SDCARD_SectorWrite(uint32_t sector, uint8_t *buffer);
extern int32_t PIOS_SDCARD_CIDRead(SDCARDCidTypeDef *cid);
extern int32_t PIOS_SDCARD_CSDRead(SDCARDCsdTypeDef *csd);
extern int32_t PIOS_SDCARD_StartupLog(void);
extern int32_t PIOS_SDCARD_MountFS(uint32_t StartupLog);
extern int32_t PIOS_SDCARD_GetFree(void);
extern int32_t PIOS_SDCARD_ReadBuffer(PFILEINFO fileinfo, uint8_t *buffer, uint32_t len);
extern int32_t PIOS_SDCARD_ReadLine(PFILEINFO fileinfo, uint8_t *buffer, uint32_t max_len);
extern int32_t PIOS_SDCARD_FileCopy(char *Source, char *Destination);
extern int32_t PIOS_SDCARD_FileDelete(char *Filename);
#endif /* PIOS_SDCARD_H */

View File

@ -84,11 +84,40 @@ int main()
/* Call LoadSettings which populates global variables so the rest of the hardware can be configured. */
PIOS_Settings_Load();
Flashy();
/* Com ports init */
// PIOS_COM_Init();
PIOS_COM_Init();
/* Initialise servo outputs */
PIOS_Servo_Init();
/* Used to test servos, cycles all servos from one side to the other
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);
PIOS_DELAY_Wait_uS(500);
}
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);
PIOS_DELAY_Wait_uS(500);
}
}
*/
Flashy();
/* Analog to digi init */
// PIOS_ADC_Init();
@ -116,7 +145,7 @@ void Flashy(void)
PIOS_LED_Off(LED2);
/* Infinite loop */
while(1)
for(;;)
{
PIOS_LED_Toggle(LED1);
//PIOS_LED_Toggle(LED2);