#include "main.h"
#include "diskio.h"
typedef struct RTCdata {
unsigned char hours;
unsigned char minutes;
unsigned char seconds;
unsigned char day;
unsigned char month;
int year;
unsigned char daysPerMonth[12];
}RTCdata;
typedef unsigned short int U16;
typedef unsigned int U32;
typedef float real;
RTCdata rtc;
FATFS FatFs;
bool signal;
bool isLeapYear(int yearT)
{
if(((yearT % 4) == 0) && ((yearT % 100) != 0) || ((yearT % 400) == 0))
return true;
return false;
}
void rtcInit(RTCdata * t)
{
t->daysPerMonth[0] = 31;
t->daysPerMonth[1] = 28;
t->daysPerMonth[2] = 31;
t->daysPerMonth[3] = 30;
t->daysPerMonth[4] = 31;
t->daysPerMonth[5] = 30;
t->daysPerMonth[6] = 31;
t->daysPerMonth[7] = 31;
t->daysPerMonth[8] = 30;
t->daysPerMonth[9] = 31;
t->daysPerMonth[10] = 30;
t->daysPerMonth[11] = 31;
t->hours = 0;
t->minutes = 0;
t->seconds = 0;
t->day = 1;
t->month = 1;
t->year = 2000;
}
void RTC(RTCdata * t)
{
unsigned char dayT;
t->seconds++;
if(rtc.seconds > 59)
{
t->seconds = 0;
t->minutes++;
if(t->minutes > 59)
{
t->minutes = 0;
t->hours++;
if(t->hours > 23)
{
t->hours = 0;
t->day++;
if(t->month == 2)
{
if(isLeapYear(t->year))
dayT = 29;
else
dayT = 28;
}
else
dayT = t->daysPerMonth[t->month - 1];
if(t->day > dayT)
{
t->day = 1;
t->month++;
if(t->month > 12)
{
t->month = 1;
t->year++;
}
}
}
}
}
}
void diskCB(BYTE drv, DWORD sector, BYTE count)
{
if((sector % 1024) == 0)
printf("Drive: %d, Sector: %8d, Count: %3d\n", drv, sector, count);
}
void DiskFormat()
{
DRESULT dr;
FRESULT fr;
dr = disk_initialize(0);
if(dr == RES_OK)
{
fr = f_mount(&FatFs, "", 0);
if(fr == RES_OK)
{
printf("FAT on SD ready\n");
disk_setCallback(diskCB);
fr = f_mkfs("", 0, 4096);
disk_setCallback(0);
printf("SD Format done!\n");
fr = f_mount(0, "", 0);
disk_ioctl(0, CTRL_POWER_OFF, 0);
printf("FAT unmounted, SD power OFF\n");
}
else
{
printf("%s\n", disk_errString(dr));
disk_ioctl(0, CTRL_POWER_OFF, 0);
}
}
}
void Sampling(unsigned numSamples, unsigned chans, unsigned delay, unsigned average, const char * fileName)
{
FRESULT fr;
DRESULT dr;
FIL fil;
char buff[128], ch;
unsigned dly = delay, i, avg, ns = numSamples;
U32 ad[8];
sprintf(buff, "%s.txt", fileName);
printf("Init SD and open %s\n", fileName);
dr = disk_initialize(0);
if(dr == RES_OK)
{
fr = f_mount(&FatFs, "", 0);
if(fr == RES_OK)
{
fr = f_open(&fil, buff, FA_CREATE_ALWAYS | FA_WRITE);
if(fr)
{
printf("Can't open [%s]\n", buff);
return;
}
}
else
{
printf("%s\n", disk_errString(dr));
disk_ioctl(0, CTRL_POWER_OFF, 0);
return;
}
}
signal = false;
ch = 0;
printf("Acquisition started: numSamples = %d, chans = %d, delay = %d, average = %d\n", numSamples, chans, delay, average);
while(ns && ch != 'X')
{
if(signal)
{
signal = false;
dly--;
if(dly == 0)
{
dly = delay;
Set_SPI(SPI_AD);
for(i = 0; i < chans; i++)
{
ad[i] = 0;
for(avg = 0; avg < average; avg++)
ad[i] += Get_AD(1, i);
ad[i] /= average;
}
Set_SPI(SPI_SD);
for(i = 0; i < chans; i++)
{
sprintf(buff, "%08d.%d = %04d, %4d/%02d/%02d - %02d:%02d:%02d\n", numSamples - ns, i, ad[i], rtc.year, rtc.month, rtc.day, rtc.hours, rtc.minutes, rtc.seconds);
f_puts(buff, &fil);
printf("%s", buff);
}
ns--;
}
}
if(Signal_Uart0())
ch = GetChar_Uart0();
}
f_close(&fil);
fr = f_mount(0, "", 0);
disk_ioctl(0, CTRL_POWER_OFF, 0);
}
void BackgroundProcess()
{
RTC(&rtc);
signal = true;
}
int GetInt(const char * label, int low, int high)
{
int ret;
printf("\n%s ", label);
_scanf("%d", &ret);
if(ret < low) ret = low;
if(ret > high) ret = high;
printf("\n");
return ret;
}
int Menu()
{
printf("%4d/%02d/%02d - %02d:%02d:%02d\n", rtc.year, rtc.month, rtc.day, rtc.hours, rtc.minutes, rtc.seconds);
printf("----- M E N U ---------\n");
printf("D - Set date\n");
printf("T - Set time\n");
printf("F - SD Format\n");
printf("S - Start sampling\n");
printf("\n> ");
return (GetChar_Uart0());
}
int main(void)
{
int com;
char fileName[12];
Set_PLL(16, 4);
Set_Port();
Set_Uart0(115200);
Set_TimerInterruptService(TIMER_0, &BackgroundProcess, 1000000);
rtcInit(&rtc);
rtc.year = 2015;
rtc.month = 3;
rtc.day = 21;
rtc.hours = 12;
rtc.minutes = 0;
rtc.seconds = 0;
Set_TimerEnable(TIMER_0, true);
while(1)
{
com = Menu();
printf("\n");
switch(com)
{
case 'D': rtc.year = GetInt("Current year [2015] =", 2015, 2099);
rtc.month = GetInt("Current month [1:12] =", 1, 12);
rtc.day = GetInt("Current day [1:31] =", 1, 31);
break;
case 'T': rtc.hours = GetInt("Hour [0:23] =", 0, 23);
rtc.minutes = GetInt("Minute [0:59] =", 0, 59);
rtc.seconds = GetInt("Second [0:59] =", 0, 59);
break;
case 'F': DiskFormat();
break;
case 'S': printf("File name without extension (max 8 chars): ");
_scanf("%s", fileName);
if(_strlen(fileName) > 8)
fileName[8] = 0;
Sampling(
GetInt("Number of samples [1:1000000] =", 1, 1000000),
GetInt("Number of channels [1:8] =", 1, 8),
GetInt("Interval delay (s) [1:3600] =", 1, 3600),
GetInt("Samples average [1:100] =", 1, 1000000),
fileName);
break;
default: printf("\nUnknown command\n");
}
}
return 0;
}