Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 15 → Rev 16

/branches/V0.1 killagreg/GPS.c
54,8 → 54,13
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include "uart.h"
#include "GPS.h"
#include "fat16.h"
#include "timer.h"
 
u8 OsdBar; // Direction home for OSD
s16 OsdDistance; // Distance home
62,8 → 67,9
s16 GPS_Pitch;
s16 GPS_Roll;
s16 GPS_Yaw;
 
File_t *GPSLogFile = 0;
GPSParameter_t GPSParameter;
u8 logtext[100];
 
typedef enum
{
84,89 → 90,149
// Init variables or send configuration to GPS module
void GPS_Init(void)
{
GPS_Pitch = 0;
GPS_Roll = 0;
GPS_Yaw = 0;
GPS_Pitch = 0;
GPS_Roll = 0;
GPS_Yaw = 0;
 
OsdDistance = 0;
OsdBar = 0;
OsdDistance = 0;
OsdBar = 0;
 
GPSParameter.P = 100;
GPSParameter.I = 100;
GPSParameter.D = 100;
GPSParameter.ACC = 100;
GPSParameter.ModeSwitch = 100;
GPSParameter.Amplification = 100;
GPSParameter.P = 100;
GPSParameter.I = 100;
GPSParameter.D = 100;
GPSParameter.ACC = 100;
GPSParameter.ModeSwitch = 100;
GPSParameter.Amplification = 100;
GpsFlightMode = GPS_MODE_FREE;
 
GpsFlightMode = GPS_MODE_FREE;
}
//-----------------------------------------------------------------------
void LogGPSData(void)
{
static u32 Flushtime = 0;
s16 i1, i2, i3;
 
if((GPS_Data.Status != INVALID) && (GPS_Data.SatFix == SATFIX_3D))
{
// if logfile is not opened
if(!GPSLogFile)
{
GPSLogFile = fopen_("/LOG/GPSLOG.TXT", 'a');
if(GPSLogFile) SerialPutString("GPSLogCreated\r\n");
}
// if logfile
if(GPSLogFile)
{
i1 = (s16)(GPS_Data.Longitude/10000000L);
i2 = abs((s16)((GPS_Data.Longitude%10000000L)/10000L));
i3 = abs((s16)(((GPS_Data.Longitude%10000000L)%10000L)/10L));
sprintf(logtext,"%03d.%.3d%.3d\t",i1, i2, i3);
fputs_(logtext, GPSLogFile);
i1 = (s16)(GPS_Data.Latitude/10000000L);
i2 = abs((s16)((GPS_Data.Latitude%10000000L)/10000L));
i3 = abs((s16)(((GPS_Data.Latitude%10000000L)%10000L)/10L));
sprintf(logtext,"%03d.%.3d%.3d\t",i1, i2, i3);
fputs_(logtext, GPSLogFile);
i1 = (s16)(GPS_Data.Altitude/1000L);
i2 = abs((s16)(GPS_Data.Altitude%1000L));
sprintf(logtext,"%d.%.3d\r\n",i1, i2);
fputs_(logtext, GPSLogFile);
if(CheckDelay(Flushtime))
{
fflush_(GPSLogFile);
Flushtime = SetDelay(10000L); // every 10 seconds
}
}
}
}
 
//------------------------------------------------------------
 
u8 Navigation(void)
{
static s8 GpsFix = 0, NewGpsMode = 0;
static GpsFlightMode_t oldGpsFlightMode = GPS_MODE_FREE;
static u32 beep_rythm;
static s8 GpsFix = 0, NewGpsMode = 0;
static GpsFlightMode_t oldGpsFlightMode = GPS_MODE_FREE;
static u32 beep_rythm;
static u32 GPSTimeout = 0;
 
if(GPS_Data.Status == NEWDATA) // there are new data from gps module
{
GPS_Data.Status = PROCESSED;
beep_rythm++;
switch(GPS_Data.Status)
{
case INVALID:
// no gps data available
break;
case NEWDATA:
// handle new gps data
GPSTimeout = SetDelay(2*GPS_Data.UpdateTime);
//LogGPSData();
GPS_Data.Status = PROCESSED; // mark as processed
break;
case PROCESSED:
if(CheckDelay(GPSTimeout)) GPS_Data.Status = INVALID;
break;
}
 
GPSParameter.ModeSwitch = Parameter.User1;
GPSParameter.Amplification = (float) Parameter.User2 / (100.0);
GPSParameter.P = (float) Parameter.User3;
GPSParameter.I = (float) Parameter.User4;
GPSParameter.D = (float) Parameter.User5;
GPSParameter.ACC = (float) Parameter.User6;
if(RC_Quality < 100) // RC-Signal lost
{
GPSParameter.ModeSwitch = 0;
GPSParameter.Amplification = 100;
GPSParameter.P = (float) 90;
GPSParameter.I = (float) 90;
GPSParameter.D = (float) 90;
GPSParameter.ACC = (float) 90;
}
 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ GPS-Mode
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
oldGpsFlightMode = GpsFlightMode;
if(GPSParameter.ModeSwitch < 50) GpsFlightMode = GPS_MODE_AID;
else if(GPSParameter.ModeSwitch < 180) GpsFlightMode = GPS_MODE_FREE;
else GpsFlightMode = GPS_MODE_HOME;
beep_rythm++;
 
GPSParameter.ModeSwitch = Parameter.User1;
GPSParameter.Amplification = (float) Parameter.User2 / (100.0);
GPSParameter.P = (float) Parameter.User3;
GPSParameter.I = (float) Parameter.User4;
GPSParameter.D = (float) Parameter.User5;
GPSParameter.ACC = (float) Parameter.User6;
 
if(RC_Quality < 100) // RC-Signal lost
{
GPSParameter.ModeSwitch = 0;
GPSParameter.Amplification = 100;
GPSParameter.P = (float) 90;
GPSParameter.I = (float) 90;
GPSParameter.D = (float) 90;
GPSParameter.ACC = (float) 90;
}
 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ GPS-Mode
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
oldGpsFlightMode = GpsFlightMode;
if(GPSParameter.ModeSwitch < 50) GpsFlightMode = GPS_MODE_AID;
else if(GPSParameter.ModeSwitch < 180) GpsFlightMode = GPS_MODE_FREE;
else GpsFlightMode = GPS_MODE_HOME;
 
if(GpsFlightMode != oldGpsFlightMode) // Mode changed
{
BeepTime = 100;
NewGpsMode = 1;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
BeepTime = 100;
NewGpsMode = 1;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ Fix okay
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ Fix okay
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if((GPS_Data.Flags & FLAG_GPSFIXOK) && ((GPS_Data.NumOfSats >= GPS_SAT_MIN) || GpsFix))
{
GpsFix = 1; // hysteresis
// here is a good place to put your GPS code...
GpsFix = 1; // hysteresis
// here is a good place to put your GPS code...
GPS_Pitch = 0; // do nothing
GPS_Roll = 0; // do nothing
}
else
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ No Fix
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ No Fix
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
{
GPS_Pitch = 0; // reset value
GPS_Roll = 0; // reset value
if(!(GPS_Data.Flags & 0x01) && !(beep_rythm % 5)) BeepTime = 100;
/*
if(!(GPS_Data.Flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100;
else if (GPS_Data.NumOfSats < GPS_SAT_MIN && !(beep_rythm % 5)) BeepTime = 10;
*/
}
}
return (0);
return (0);
}
 
/branches/V0.1 killagreg/GPSUart.c
54,7 → 54,7
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
#include <stdio.h>
#include "91x_lib.h"
#include "uart.h"
#include "GPSUart.h"
72,7 → 72,6
// message id
#define UBX_ID_POSLLH 0x02
#define UBX_ID_SOL 0x06
#define UBX_ID_POSUTM 0x08
#define UBX_ID_VELNED 0x12
 
// ------------------------------------------------------------------------------------------------
295,8 → 294,7
/********************************************************/
void Update_GPS_Data (void)
{
static u32 lasttime;
 
static u32 lasttime = 0;
LED_RED_TOGGLE;
// update GPS data only if the status is INVALID or PROCESSED
if(GPS_Data.Status != NEWDATA)
323,7 → 321,6
GPS_Data.Speed_Top = -UbxVelNed.VEL_D;
GPS_Data.Speed_Ground = UbxVelNed.GSpeed;
UbxVelNed.Status = PROCESSED; // ready for new data
 
GPS_Data.Status = NEWDATA; // new data available
}
}
340,7 → 337,7
static u16 msglen;
static u8 cka, ckb;
static u8 *ubxP, *ubxEp, *ubxSp; // pointers to data currently transfered
 
// if receive irq or receive timeout irq has occured
if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) )
{
402,7 → 399,7
break;
 
case UBXSTATE_LEN1: // 1st message length byte
msglen = c; // lowbyte first
msglen = (u16)c; // lowbyte first
cka += c;
ckb += cka;
ubxState = UBXSTATE_LEN2;
414,7 → 411,11
ckb += cka;
// if the old data are not processed so far then break parsing now
// to avoid writing new data in ISR during reading by another function
if ( *ubxSp == NEWDATA ) ubxState = UBXSTATE_IDLE;
if ( *ubxSp == NEWDATA )
{
ubxState = UBXSTATE_IDLE;
Update_GPS_Data(); //update GPS info respectively
}
else // data invalid or allready processd
{
*ubxSp = INVALID; // mark invalid during buffer filling
/branches/V0.1 killagreg/fat16.c
298,6 → 298,64
 
 
/****************************************************************************************************************************************/
/* Function: LockFilePointer(); */
/* */
/* Description: This function trys to lock a free file pointer. */
/* */
/* Returnvalue: Returns the Filepointer on success or 0. */
/****************************************************************************************************************************************/
File_t * LockFilePointer(void)
{
u8 i;
File_t * File = 0;
for(i = 0; i < FILE_MAX_OPEN; i++)
{
if(FilePointer[i].State == FSTATE_UNUSED) // found an unused one
{
File = &FilePointer[i]; // set pointer to that entry
FilePointer[i].State = FSTATE_USED; // mark it as used
break;
}
}
return(File);
}
 
/****************************************************************************************************************************************/
/* Function: UnlockFilePointer(File_t *); */
/* */
/* Description: This function trys to unlock a file pointer. */
/* */
/* Returnvalue: Returns 1 if file pointer was freed else 0. */
/****************************************************************************************************************************************/
u8 UnlockFilePointer(File_t * File)
{
u8 cnt;
if(File == 0) return(0);
for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++)
{
if(&FilePointer[cnt] == File) // filepointer to be freed found?
{
File->State = FSTATE_UNUSED;
File->FirstSectorOfFirstCluster = 0; // Sectorpointer to the first sector of the first datacluster of the file.
File->FirstSectorOfCurrCluster = 0;
File->SectorOfCurrCluster = 0; // Pointer to the cluster which is edited at the moment.
File->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index).
File->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index).
File->Mode = 0; // mode of fileoperation (read,write)
File->Size = 0; // the size of the opend file in bytes.
File->Position = 0; // pointer to a character within the file 0 < fileposition < filesize
File->SectorInCache = 0; // the last sector read, wich is still in the sectorbuffer.
File->DirectorySector = 0; // the sectorposition where the directoryentry has been made.
File->DirectoryIndex = 0; // the index to the directoryentry within the specified sector.
File->Attribute = 0; // the attribute of the file opened.
File = 0;
return(1);
}
}
return(0);
}
 
/****************************************************************************************************************************************/
/* Function: SeperateDirName(u8*, u8*, u8*); */
/* */
/* Description: This function seperates the first dirname from filepath and brings them */
641,6 → 699,7
s32 fposition = 0;
s16 retvalue = 1;
if(!Partition.IsValid) return(0);
switch(origin)
{
case SEEK_SET: // Fileposition relative to the beginning of the file.
1019,7 → 1078,7
u8 dirname[11];
 
// if incomming pointers are useless return immediatly
if ((filename == 0) || (File == 0)) return 0;
if ((filename == 0) || (File == 0) || (!Partition.IsValid)) return 0;
 
// trace along the filepath
path = (u8*)filename; // start a the beginning of the filename string
1080,7 → 1139,7
u8 dirname[11];
 
// if incomming pointers are useless return immediatly
if ((filename == 0) || (File == 0)) return 0;
if ((filename == 0) || (File == 0) || (!Partition.IsValid)) return 0;
 
// trace along the filepath
path = (u8*)filename; // start a the beginning of the filename string
1133,18 → 1192,11
File_t * fopen_(const u8 *filename, const s8 mode)
{
File_t *File = 0;
u8 i;
if((!Partition.IsValid) || (filename == 0)) return(File);
 
// Look for an unused filepointer in the file pointer list?
for(i = 0; i < FILE_MAX_OPEN; i++)
{
if(FilePointer[i].State == FSTATE_UNUSED) // found an unused one
{
File = &FilePointer[i]; // set pointer to that entry
FilePointer[i].State = FSTATE_USED; // mark it as used
break;
}
}
File = LockFilePointer();
// if no unused file pointer was found return 0
if(File == 0) return(File);
 
1245,7 → 1297,7
date = ((((rtctime.year)-1980) <<9) | ((rtctime.month) <<5) | rtctime.day);
#endif
if(File == 0) return (EOF);
if((File == 0) || (!Partition.IsValid)) return (EOF);
if(File->Mode == 'a')
{
1284,30 → 1336,11
/****************************************************************************************************************************************/
s16 fclose_(File_t *File)
{
u8 cnt = 0;
s16 returnvalue = EOF;
 
if(File == 0) return(returnvalue);
for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++)
{
if(&FilePointer[cnt] == File) // filepointer to be freed found?
{
returnvalue = fflush_(File); // save cache content to the sdcard
File->State = FSTATE_UNUSED;
File->FirstSectorOfFirstCluster = 0; // Sectorpointer to the first sector of the first datacluster of the file.
File->FirstSectorOfCurrCluster = 0;
File->SectorOfCurrCluster = 0; // Pointer to the cluster which is edited at the moment.
File->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index).
File->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index).
File->Mode = 0; // mode of fileoperation (read,write)
File->Size = 0; // the size of the opend file in bytes.
File->Position = 0; // pointer to a character within the file 0 < fileposition < filesize
File->SectorInCache = 0; // the last sector read, wich is still in the sectorbuffer.
File->DirectorySector = 0; // the sectorposition where the directoryentry has been made.
File->DirectoryIndex = 0; // the index to the directoryentry within the specified sector.
File->Attribute = 0; // the attribute of the file opened.
}
}
returnvalue = fflush_(File);
UnlockFilePointer(File);
return(returnvalue);
}
 
1324,7 → 1357,7
s16 c = EOF;
u32 curr_sector;
if(File == 0) return(c);
if( (!Partition.IsValid) || (File == 0)) return(c);
// if the end of the file is not reached, get the next character.
if((0 < File->Size) && (File->Position < File->Size) )
{
1373,6 → 1406,8
{
u32 curr_sector = 0;
if((!Partition.IsValid) || (File == 0)) return(EOF);
 
// If file position equals to file size, then the end of file has reached.
// In this chase it has to be checked that the ByteOfCurrSector is BYTES_PER_SECTOR
// and a new cluster should be appended.
1441,6 → 1476,8
u8 success = 1; // no error occured during read operation to the file.
s16 c;
 
if((!Partition.IsValid) || (File == 0) || (buffer == 0)) return(0);
 
pbuff = (u8 *) buffer; // cast the void pointer to an u8 *
while((object_cnt < count) && success)
1482,6 → 1519,8
u8 success = 1; // no error occured during write operation to the file.
s16 c;
 
if((!Partition.IsValid) || (File == 0) || (buffer == 0)) return(0);
 
pbuff = (u8 *) buffer; // cast the void pointer to an u8 *
while((object_cnt < count) && success)
1519,6 → 1558,8
u8 i=0;
s16 c = 0;
if((!Partition.IsValid) || (File == 0) || (string == 0)) return(0);
 
while((string[i] != 0)&& (c != EOF))
{
c = fputc_(string[i], File);
1539,7 → 1580,7
u8 *pbuff;
s16 c = 0;
if((File == 0) || (string == 0) || (length = 0)) return (0);
if((!Partition.IsValid) || (File == 0) || (string == 0) || (length = 0)) return (0);
pbuff = string;
while(length > 1) // read the count-1 characters from the file to the string.
{
1563,4 → 1604,20
return(string);
}
 
/****************************************************************************************************************************************/
/* Function: fexit_(const u8*); */
/* */
/* Description: This function cheacks if a file already exist. */
/* */
/* Returnvalue: 1 if the file exist else 0. */
/****************************************************************************************************************************************/
u8 fexit_(const u8* filename)
{
u8 exist = 0;
File_t *File = 0;
File = LockFilePointer();
exist = FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, File);
UnlockFilePointer(File);
return(exist);
}
 
/branches/V0.1 killagreg/fat16.h
9,7 → 9,7
//________________________________________________________________________________________________________________________________________
 
//#define __USE_TIME_DATE_ATTRIBUTE
#define FILE_MAX_OPEN 2 // The number of files that can accessed simultaneously.
#define FILE_MAX_OPEN 3 // The number of files that can accessed simultaneously.
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
49,6 → 49,7
extern File_t * fopen_(const u8 *filename, const s8 mode);
extern s16 fclose_(File_t *File);
extern u8 fexist_(const u8 *filename);
extern s16 fflush_(File_t *File);
extern s16 fseek_(File_t *File, s32 offset, s16 origin);
extern s16 fgetc_(File_t *File);
/branches/V0.1 killagreg/main.c
138,28 → 138,27
// initialize SPI0 to FC
SPI0_Init();
// initialize i2c bus to MK3MAG
I2C1_Init();
// try to initialize the sd card
//SDC_Init();
 
File_t *File = 0;
s16 c = 0;
I2C1_Init();
// initialize fat16 partition on sd card
Fat16_Init();
/*
File_t * F = 0;
u8 txt[20];
u16 i;
if (0 == Fat16_Init())
F = fopen_("/test.txt",'a');
if(F)
{
File = fopen_("/dir/subdir/test.txt", 'a');
if(File != 0)
{
SerialPutString("File was opened.\r\n");
for(i=0;i<1000; i++)
{
fputs_("Das ist ein Test.\r\n", File);// 19 bytes
}
c = fclose_(File);
if (c != EOF) SerialPutString("File is closed.\r\n");
}
}
for(i=0;i<1000;i++)
{
sprintf(txt, "%04i\r\n",i);
fputs_(txt, F);
}
fclose_(F);
}
*/
 
 
 
// get version from MK3MAG
I2C_Version.Major = 0xFF;
SendI2C_Command(I2C_CMD_VERSION);
232,7 → 231,7
//________________________________________________________________________________________________________________________________________
// Funtion: EXTIT1_IRQHandler(void);
//
// Description: This function handles the extnernal interrupts from port 5.3 (SD_SWITCH)
// Description: This function handles the external interrupts from port 5.3 (SD_SWITCH)
//
//
// Returnvalue: none
/branches/V0.1 killagreg/menu.c
132,7 → 132,10
case 1:
if (GPS_Data.Status == INVALID)
{
LCD_printfxy(0,0,"No GPS data!");
LCD_printfxy(0,0,"No GPS data! ");
LCD_printfxy(0,1," ");
LCD_printfxy(0,2," ");
LCD_printfxy(0,3," ");
}
else // newdata or processed
{
139,16 → 142,16
switch (GPS_Data.SatFix)
{
case SATFIX_NONE:
LCD_printfxy(0,0,"Sats: %d Fix: None", GPS_Data.NumOfSats);
LCD_printfxy(0,0,"Sats:%02d Fix:None", GPS_Data.NumOfSats);
break;
case SATFIX_2D:
LCD_printfxy(0,0,"Sats: %d Fix: 2D", GPS_Data.NumOfSats);
LCD_printfxy(0,0,"Sats:%02d Fix:2D ", GPS_Data.NumOfSats);
break;
case SATFIX_3D:
LCD_printfxy(0,0,"Sats: %d Fix: 3D", GPS_Data.NumOfSats);
LCD_printfxy(0,0,"Sats:%02d Fix:3D ", GPS_Data.NumOfSats);
break;
default:
LCD_printfxy(0,0,"Sats: %d Fix: ??", GPS_Data.NumOfSats);
LCD_printfxy(0,0,"Sats:%02d Fix:?? ", GPS_Data.NumOfSats);
break;
}
s16 i1,i2,i3;
155,14 → 158,14
i1 = (s16)(GPS_Data.Longitude/10000000L);
i2 = abs((s16)((GPS_Data.Longitude%10000000L)/10000L));
i3 = abs((s16)(((GPS_Data.Longitude%10000000L)%10000L)/10L));
LCD_printfxy(0,1,"Lon: %d.%.3d%.3d deg",i1, i2, i3);
LCD_printfxy(0,1,"Lon: %03d.%.3d%.3d deg",i1, i2, i3);
i1 = (s16)(GPS_Data.Latitude/10000000L);
i2 = abs((s16)((GPS_Data.Latitude%10000000L)/10000L));
i3 = abs((s16)(((GPS_Data.Latitude%10000000L)%10000L)/10L));
LCD_printfxy(0,2,"Lat: %d.%.3d%.3d deg",i1, i2, i3);
LCD_printfxy(0,2,"Lat: %03d.%.3d%.3d deg",i1, i2, i3);
i1 = (s16)(GPS_Data.Altitude/1000L);
i2 = abs((s16)(GPS_Data.Altitude%1000L));
LCD_printfxy(0,3,"Alt: %d.%.3d m",i1, i2);
LCD_printfxy(0,3,"Alt: %03d.%.3d m",i1, i2);
}
break;
case 2: // RC stick controls from FC
/branches/V0.1 killagreg/sdc.c
58,9 → 58,9
#include <string.h>
#include "91x_lib.h"
#include "uart.h"
#include "timer.h"
#include "sdc.h"
#include "ssc.h"
#include "timer.h"
#include "main.h"
#include "crc16.h"
 
181,23 → 181,8
return(crc);
}
 
u8 SDC_WaitForBusy()
{
u8 rsp = 0;
 
SSC_ClearRxFifo();
SSC_Enable(); // enable chipselect.
do
{
rsp = SSC_GetChar();
}while(rsp != 0xFF); // wait while card is busy (data out low)
return(rsp);
}
 
 
 
//________________________________________________________________________________________________________________________________________
// Function: SDC_SendCMDR1(u8 CmdNo, u32 arg);
// Function: SDC_SendCMD(u8 CmdNo, u32 arg);
//
// Description: This function send a command frame to the SD-Card in spi-mode.
//
204,7 → 189,7
//
// Returnvalue: The function returns the first response byte like for R1 commands
//________________________________________________________________________________________________________________________________________
u8 SDC_SendCMDR1(u8 CmdNo, u32 arg)
u8 SDC_SendCMD(u8 CmdNo, u32 arg)
{
u8 r1;
u16 Timeout = 0;
213,11 → 198,8
 
SSC_ClearRxFifo(); // clear the rx fifo
SSC_Enable(); // enable chipselect.
SDC_WaitForBusy();
SSC_ClearRxFifo(); // clear the rx fifo
SSC_GetChar(); // dummy to sync
 
/* Send cmd10 (SEND_CID) */;
cmd[0] = 0x40|CmdNo; // set command index
cmd[1] = (arg & 0xFF000000)>>24;
cmd[2] = (arg & 0x00FF0000)>>16;
229,10 → 211,11
SSC_PutChar(cmd[a]);
}
SSC_ClearRxFifo(); // clear the rx fifo to discard the bytes received during the transmission of the 6 command bytes
Timeout = SetDelay(50);
do
{
r1 = SSC_GetChar(); // get byte from sd-card
if (Timeout++ > 500) return(r1);
if (CheckDelay(Timeout)) break;
}while(r1 == 0xFF); // wait for the response byte from sd-card.
return(r1);
}
248,10 → 231,10
//________________________________________________________________________________________________________________________________________
u8 SDC_SendACMDR1(u8 CmdNo, u32 arg)
{
u8 r1 = 0xFF;
r1 = SDC_SendCMDR1(CMD_APP_CMD, 0UL);
u8 r1;
r1 = SDC_SendCMD(CMD_APP_CMD, 0UL);
if(r1 & R1_BAD_RESPONSE) return(r1);
r1 = SDC_SendCMDR1(CmdNo, arg);
r1 = SDC_SendCMD(CmdNo, arg);
return(r1);
}
 
268,12 → 251,12
SD_Result_t SDC_GetData(u8 CmdNo, u32 addr, u8 *Buffer, u32 len)
{
u8 rsp;
//u32 Timeout;
u32 Timeout;
u16 a, Crc16;
SD_Result_t result = SD_ERROR_UNKNOWN;
 
// send the command
rsp = SDC_SendCMDR1(CmdNo, addr);
rsp = SDC_SendCMD(CmdNo, addr);
if (rsp != R1_NO_ERR)
{
result = SD_ERROR_BAD_RESPONSE;
280,7 → 263,7
goto end;
}
SSC_ClearRxFifo();
//Timeout = SetDelay(400); // wait alt least 0.2 seconds for data ready
Timeout = SetDelay(200);
do
{
rsp = SSC_GetChar();
289,11 → 272,11
result = SD_ERROR_READ_DATA;
goto end;
}
/*if(CheckDelay(Timeout))
if (CheckDelay(Timeout))
{
result = SD_ERROR_TIMEOUT;
goto end;
}*/
result = SD_ERROR_TIMEOUT;
goto end;
}
}while(rsp != DATA_START_TOKEN);
// data start token received
for (a = 0; a < len; a++) // read the block from the SSC
307,7 → 290,6
else result = SD_SUCCESS;
end:
//SSC_Disable(); // disable sdcard.
if(result != SD_SUCCESS)
{
sprintf(text,"Error %02X reading data from sd card (R1=%02X).\r\n", result, rsp);
420,9 → 402,10
}
// switch to idle state
while(SDC_SendCMDR1(CMD_GO_IDLE_STATE, 0UL) != R1_IDLE_STATE)
Timeout = SetDelay(200);
while(SDC_SendCMD(CMD_GO_IDLE_STATE, 0UL) != R1_IDLE_STATE)
{
if (Timeout++ > 200)
if (CheckDelay(Timeout))
{
SerialPutString("reset timeout.\r\n");
result = SD_ERROR_RESET;
430,7 → 413,7
}
}
// enable crc feature
if(SDC_SendCMDR1(CMD_CRC_ON_OFF, 1UL) != R1_IDLE_STATE)
if(SDC_SendCMD(CMD_CRC_ON_OFF, 1UL) != R1_IDLE_STATE)
{
sprintf(text,"Bad cmd59 R1=%02X.\r\n", rsp[0]);
SerialPutString(text);
439,7 → 422,7
}
// check for card hw version
// 2.7-3.6V Range = 0x01, check pattern 0xAA
rsp[0] = SDC_SendCMDR1(CMD_SEND_IF_COND, 0x000001AA);
rsp[0] = SDC_SendCMD(CMD_SEND_IF_COND, 0x000001AA);
// answer to cmd58 is an R7 response (R1+ 4Byte IFCond)
if(rsp[0] & R1_BAD_RESPONSE)
{
478,7 → 461,7
}
}
rsp[0] = SDC_SendCMDR1(CMD_READ_OCR, 0UL);
rsp[0] = SDC_SendCMD(CMD_READ_OCR, 0UL);
// answer to cmd58 is an R3 response (R1 + 4Byte OCR)
if(rsp[0] & R1_BAD_RESPONSE)
{
508,7 → 491,7
}
// Initialize the sd-card sending continously ACMD_SEND_OP_COND (only supported by SD cards)
Timeout = SetDelay(2000); // set timeout to 2000 ms (large cards tend to longer)
Timeout = SetDelay(1000);
do
{
rsp[0] = SDC_SendACMDR1(ACMD_SEND_OP_COND, 0UL);
534,17 → 517,14
goto end;
}
/* set block size to 512 bytes */
if(SDC_SendCMDR1(CMD_SET_BLOCKLEN, 512UL) != R1_NO_ERR)
if(SDC_SendCMD(CMD_SET_BLOCKLEN, 512UL) != R1_NO_ERR)
{
SerialPutString("Error setting block length to 512.\r\n");
result = SD_ERROR_SET_BLOCKLEN;
goto end;
}
 
 
//SSC_Disable(); // set SD_CS high
// here is the right place to inrease the SPI boud rate to maximum
//SSC_Enable(); // set SD_CS high
 
// read CID register
result = SDC_GetCID((u8 *)&SDCardInfo.CID);
625,10 → 605,6
SDC_PrintCID((u8 *)&SDCardInfo.CID);
SDCardInfo.Valid = 1;
// jump point for error condition before
end:
SSC_Disable();
}
else
{
637,6 → 613,8
result = SD_ERROR_NOCARD;
SerialPutString("No Card in Slot.\r\n");
}
// jump point for error condition before
end:
return(result);
}
 
677,11 → 655,11
{
u8 rsp;
u16 a, Crc16;
//u32 Timeout = 0;
u32 Timeout = 0;
SD_Result_t result = SD_ERROR_UNKNOWN;
 
addr = addr << 9; // convert sectoradress to byteadress
rsp = SDC_SendCMDR1(CMD_WRITE_SINGLE_BLOCK, addr);
rsp = SDC_SendCMD(CMD_WRITE_SINGLE_BLOCK, addr);
if (rsp != R1_NO_ERR)
{
result = SD_ERROR_BAD_RESPONSE;
688,7 → 666,7
goto end;
}
SSC_ClearRxFifo();
for (a=0;a<20;a++) // at least one byte
for (a=0;a<100;a++) // at least one byte
{
SSC_GetChar();
}
703,15 → 681,15
SSC_PutChar((u8)(Crc16>>8)); // write high byte first
SSC_PutChar((u8)(0x00FF&Crc16)); // lowbyte last
SSC_ClearRxFifo();
//Timeout = SetDelay(200);
Timeout = SetDelay(500);
do // wait for data response token
{
rsp = SSC_GetChar();
/*if(CheckDelay(Timeout))
if(CheckDelay(Timeout))
{
result = SD_ERROR_TIMEOUT;
goto end;
}*/
}
}while((rsp & 0x11) != 0x01 );
// analyse data response token
switch(rsp & DATA_RESPONSE_MASK)
734,18 → 712,23
 
}
// wait until the sdcard is busy.
rsp = SDC_WaitForBusy();
if(rsp != 0xFF)
Timeout = SetDelay(1000);
do
{
result = SD_ERROR_TIMEOUT;
goto end;
}
rsp = SSC_GetChar();
if (CheckDelay(Timeout))
{
result = SD_ERROR_TIMEOUT;
goto end;
}
}while(rsp != 0xFF);
// check card status
rsp = SDC_SendCMDR1(CMD_SEND_STATUS, 0);
rsp = SDC_SendCMD(CMD_SEND_STATUS, 0);
// first byte of R2 response is like R1 response
if(rsp != R1_NO_ERR)
{
result = SD_ERROR_BAD_RESPONSE;
SSC_GetChar();
SSC_GetChar(); // read out 2nd byte
goto end;
}
// 2nd byte of r2 response
757,7 → 740,6
goto end;
}
end:
//SSC_Disable(); // disable sdcard.
if(result != SD_SUCCESS)
{
sprintf(text,"Error %02X writing data to sd card (R=%02X).\r\n", result, rsp);
/branches/V0.1 killagreg/uart.c
178,7 → 178,7
UART_InitStructure.UART_WordLength = UART_WordLength_8D;
UART_InitStructure.UART_StopBits = UART_StopBits_1;
UART_InitStructure.UART_Parity = UART_Parity_No ;
UART_InitStructure.UART_BaudRate = BAUD_RATE ;
UART_InitStructure.UART_BaudRate = BAUD_RATE;
UART_InitStructure. UART_HardwareFlowControl = UART_HardwareFlowControl_None;
UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;
UART_InitStructure.UART_FIFO = UART_FIFO_Enable;
188,7 → 188,7
UART_DeInit(UART1); // reset uart 1 to default
UART_Init(UART1, &UART_InitStructure); // initialize uart 1
// enable uart 1 interrupts selective
UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut /*| UART_IT_Transmit */, ENABLE);
UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
UART_Cmd(UART1, ENABLE); // enable uart 1
// configure the uart 1 interupt line as an IRQ with priority 4 (0 is highest)
VIC_Config(UART1_ITLine, VIC_IRQ, 4);