Hello all,
I am trying to change to given logomatic code to use the rtc, put a date and time stamp on a text file, and give that file the date as its name. The original code comes with three modes of operation, and I am trying to just use mode 1(which is the second mode). I suck at programming, and I have taken a basic introductory course in c. Now, with that being said I have figured out most of what I want to do with this thing, but perhaps somebody can give me a nudge in the right direction. Here is the code
/*********************************************************************************
* Logomatic V2 Firmware
* Sparkfun Electronics 2008
* ******************************************************************************/
/*******************************************************
* Header Files
******************************************************/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "LPC21xx.h"
//UART0 Debugging
#include "serial.h"
#include "rprintf.h"
//Needed for main function calls
#include "main_msc.h"
#include "fat.h"
#include "armVIC.h"
#include "itoa.h"
#include "rootdir.h"
#include "sd_raw.h"
/*******************************************************
* Global Variables
******************************************************/
#define ON 1
#define OFF 0
char RX_array1[512];
char RX_array2[512];
char log_array1 = 0;
char log_array2 = 0;
short RX_in = 0;
char get_frame = 0;
unsigned char flag = 0;
signed int stringSize;
struct fat_file_struct* handle;
struct fat_file_struct * fd;
char stringBuf[256];
// Default Settings
static char mode = 1;
static char asc = 'Y';
static int baud = 9600;
static char trig = '
I am having a hard time getting it to open the same file for mulitple logs. I want to create a new file at say midnight every night, so that it why the file names will be date stamped. I tried some things in that while loop in main, but I was unsuccessful. Keep in mind that I lack experience so most of this is the original just hacked on by me.
Thank you;
static short frame = 100;
/*******************************************************
-
Function Declarations
******************************************************/
void set_time(void);
void Initialize(void);
void setup_uart0(int newbaud, char want_ints);
void mode_0(void);
void mode_1(void);
void mode_2(void);
void mode_action(void);
void Log_init(void);
void test(void);
void stat(int statnum, int onoff);
void AD_conversion(int regbank);
void feed(void);
static void IRQ_Routine(void) attribute ((interrupt(“IRQ”)));
static void UART0ISR(void); //attribute ((interrupt(“IRQ”)));
static void UART0ISR_2(void); //attribute ((interrupt(“IRQ”)));
static void MODE2ISR(void); //attribute ((interrupt(“IRQ”)));
void rtc_int(void) attribute ((interrupt(“IRQ”)));
void FIQ_Routine(void) attribute ((interrupt(“FIQ”)));
void SWI_Routine(void) attribute ((interrupt(“SWI”)));
void UNDEF_Routine(void) attribute ((interrupt(“UNDEF”)));
void fat_initialize(void);
void delay_ms(int count);
/*******************************************************
-
MAIN
******************************************************/
int main (void)
{
int i;
char name[32];
int count = 0;
set_time ();
enableFIQ();
Initialize();
setup_uart0(9600, 0);
fat_initialize();
// Flash Status Lights
for(i = 0; i < 5; i++)
{
stat(0,ON);
delay_ms(50);
stat(0,OFF);
stat(1,ON);
delay_ms(50);
stat(1,OFF);
}
Log_init();
count++;
string_printf(name,"%04d-%02d-%02d.txt", YEAR, MONTH, DOM);
while(root_file_exists(name))
{
count++;
if(count == 250)
{
rprintf("Too Many Logs!\n\r");
while(1)
{
stat(0,ON);
stat(1,ON);
delay_ms(1000);
stat(0,OFF);
stat(1,OFF);
delay_ms(1000);
}
}
string_printf(name,"LOG%02d.txt",count);
}
handle = root_open_new(name);
sd_raw_sync();
if(mode == 0){ mode_0(); }
else if(mode == 1){ mode_1(); }
return 0;
}
/*******************************************************
-
Initialize
******************************************************/
#define PLOCK 0x400
void set_time(void)
{
PCONP |= (1<<9); //Ensure RTC is powered
CCR = 0x13; //Turn off RTC
YEAR = 2015;
MONTH = 06;
DOM = 15; //Day of Month
HOUR = 8;
MIN = 52;
SEC = 0;
CCR = 0x11; //Turn RTC back on
}
void Initialize(void)
{
rprintf_devopen(putc_serial0);
PINSEL0 = 0xCF351505;
PINSEL1 = 0x15441801;
IODIR0 |= 0x00000884;
IOSET0 = 0x00000080;
S0SPCR = 0x08; // SPI clk to be pclk/8
S0SPCR = 0x30; // master, msb, first clk edge, active high, no ints
}
void feed(void)
{
PLLFEED=0xAA;
PLLFEED=0x55;
}
static void UART0ISR(void)
{
char temp;
if(RX_in < 512)
{
RX_array1[RX_in] = U0RBR;
RX_in++;
if(RX_in == 512) log_array1 = 1;
}
else if(RX_in >= 512)
{
RX_array2[RX_in-512] = U0RBR;
RX_in++;
if(RX_in == 1024)
{
log_array2 = 1;
RX_in = 0;
}
}
temp = U0IIR; // Have to read this to clear the interrupt
VICVectAddr = 0;
}
static void UART0ISR_2(void)
{
char temp;
temp = U0RBR;
if(RX_in == 0)
{
memset (RX_array1, 0, 512); // This clears the RX_array to make way for new data
}
if(RX_in < 512)
{
RX_array1[RX_in] = temp;
RX_in++;
if(temp == trig)
{
RX_array1[RX_in] = 10; // delimiters
RX_array1[RX_in + 1] = 13;
log_array1 = 1;
RX_in = 0;
}
}
else if(RX_in >= 512)
{
RX_array2[RX_in - frame] = temp;
RX_in++;
if(RX_in == 2*frame)
{
RX_array2[RX_in - frame] = 10; // delimiters
RX_array2[RX_in + 1 - frame] = 13;
log_array2 = 1;
RX_in = 0;
}
}
temp = U0IIR; // have to read this to clear the interrupt
VICVectAddr = 0;
}
void FIQ_Routine(void)
{
char a;
int j;
stat(0,ON);
for(j = 0; j < 5000000; j++);
stat(0,OFF);
a = U0RBR;
a = U0IIR; // have to read this to clear the interrupt
}
void SWI_Routine(void)
{
while(1);
}
void UNDEF_Routine(void)
{
stat(0,ON);
}
void setup_uart0(int newbaud, char want_ints)
{
baud = newbaud;
U0LCR = 0x83; // 8 bits, no parity, 1 stop bit, DLAB = 1
if(baud == 1200)
{
U0DLM = 0x0C;
U0DLL = 0x00;
}
else if(baud == 2400)
{
U0DLM = 0x06;
U0DLL = 0x00;
}
else if(baud == 4800)
{
U0DLM = 0x03;
U0DLL = 0x00;
}
else if(baud == 9600)
{
U0DLM = 0x01;
U0DLL = 0x80;
}
else if(baud == 19200)
{
U0DLM = 0x00;
U0DLL = 0xC0;
}
else if(baud == 38400)
{
U0DLM = 0x00;
U0DLL = 0x60;
}
else if(baud == 57600)
{
U0DLM = 0x00;
U0DLL = 0x40;
}
else if(baud == 115200)
{
U0DLM = 0x00;
U0DLL = 0x20;
}
U0FCR = 0x01;
U0LCR = 0x03;
if(want_ints == 1)
{
enableIRQ();
VICIntSelect &= ~0x00000040;
VICIntEnable |= 0x00000040;
VICVectCntl1 = 0x26;
VICVectAddr1 = (unsigned int)UART0ISR;
U0IER = 0x01;
}
else if(want_ints == 2)
{
enableIRQ();
VICIntSelect &= ~0x00000040;
VICIntEnable |= 0x00000040;
VICVectCntl2 = 0x26;
VICVectAddr2 = (unsigned int)UART0ISR_2;
U0IER = 0X01;
}
else if(want_ints == 0)
{
VICIntEnClr = 0x00000040;
U0IER = 0x00;
}
}
void stat(int statnum, int onoff)
{
if(statnum) // Stat 1
{
if(onoff){ IOCLR0 = 0x00000800; } // On
else { IOSET0 = 0x00000800; } // Off
}
else // Stat 0
{
if(onoff){ IOCLR0 = 0x00000004; } // On
else { IOSET0 = 0x00000004; } // Off
}
}
void Log_init(void)
{
int x, mark = 0, ind = 0;
char temp;
// signed char handle;
if(root_file_exists("LOGCON.txt"))
{
//rprintf("\n\rFound LOGcon.txt\n");
fd = root_open("LOGCON.txt");
stringSize = fat_read_file(fd, (unsigned char *)stringBuf, 512);
stringBuf[stringSize] = '\0';
fat_close_file(fd);
}
else
{
//rprintf("Couldn't find LOGcon.txt, creating...\n");
fd = root_open_new("LOGCON.txt");
if(fd == NULL)
{
rprintf("Error creating LOGCON.txt, locking up...\n\r");
while(1)
{
stat(0,ON);
delay_ms(50);
stat(0,OFF);
stat(1,ON);
delay_ms(50);
stat(1,OFF);
}
}
strcpy(stringBuf, "MODE = 1\r\nASCII = Y\r\nBaud = 4\r\nTrigger Character = $\r\nText Frame = 100\r\n");
stringSize = strlen(stringBuf);
fat_write_file(fd, (unsigned char*)stringBuf, stringSize);
sd_raw_sync();
}
for(x = 0; x < stringSize; x++)
{
temp = stringBuf[x];
if(temp == 10)
{
mark = x;
ind++;
if(ind == 1)
{
mode = stringBuf[mark-2]-48; // 0 = auto uart, 1 = trigger uart
rprintf("mode = %d\n\r",mode);
}
else if(ind == 2)
{
asc = stringBuf[mark-2]; // default is 'N'
rprintf("asc = %c\n\r",asc);
}
else if(ind == 3)
{
if(stringBuf[mark-2] == '1'){ baud = 1200; }
else if(stringBuf[mark-2] == '2'){ baud = 2400; }
else if(stringBuf[mark-2] == '3'){ baud = 4800; }
else if(stringBuf[mark-2] == '4'){ baud = 9600; }
else if(stringBuf[mark-2] == '5'){ baud = 19200; }
else if(stringBuf[mark-2] == '6'){ baud = 38400; }
else if(stringBuf[mark-2] == '7'){ baud = 57600; }
else if(stringBuf[mark-2] == '8'){ baud = 115200; }
rprintf("baud = %d\n\r",baud);
}
else if(ind == 4)
{
trig = stringBuf[mark-2]; // default is $
rprintf("trig = %c\n\r",trig);
}
else if(ind == 5)
{
frame = (stringBuf[mark-2]-48) + (stringBuf[mark-3]-48) * 10 + (stringBuf[mark-4]-48)*100;
if(frame > 510){ frame = 510; } // up to 510 characters
rprintf("frame = %d\n\r",frame);
}
}
}
}
void mode_0(void) // Auto UART mode
{
rprintf(“MODE 0\n\r”);
setup_uart0(baud,1);
stringSize = 512;
mode_action();
rprintf(“Exit mode 0\n\r”);
}
void mode_1(void)
{
rprintf(“MODE 1\n\r”);
setup_uart0(baud,2);
stringSize = frame + 2;
mode_action();
}
void mode_action(void)
{
int j;
char output[1024];
while(1)
{
uint32_t ctime0_val = CTIME0;
uint32_t ctime1_val = CTIME1;
uint32_t ctime2_val = CTIME2;
unsigned int seconds = (0x0000003f & ctime0_val); // Bit masking to read the time values from the appropriate
unsigned int minutes = (0x00003f00 & ctime0_val) >> 8; // registers, and also placing those values in the correct
unsigned int hours = (0x001f0000 & ctime0_val) >> 16; // slot to display a readable time and date
unsigned int dom = (0x0000001f & ctime1_val);
unsigned int month = (0x00000f00 & ctime1_val) >> 8;
unsigned int year = (0x0fff0000 & ctime1_val) >> 16;
if(log_array1 == 1)
{
stringSize = sprintf(output, "%04d-%02d-%02d %02d:%02d:%02d, %s\n\r", year, month, dom, hours, minutes, seconds, (unsigned char *)RX_array1);
stat(0,ON);
if(fat_write_file(handle, output, stringSize) < 0)
{
while(1)
{
stat(0,ON);
for(j = 0; j < 500000; j++)
stat(0,OFF);
stat(1,ON);
for(j = 0; j < 500000; j++)
stat(1,OFF);
}
}
sd_raw_sync();
stat(0,OFF);
log_array1 = 0;
}
if(log_array2 == 1)
{
stat(1,ON);
if(fat_write_file(handle,(unsigned char *)RX_array2, stringSize) < 0)
{
while(1)
{
stat(0,ON);
for(j = 0; j < 500000; j++)
stat(0,OFF);
stat(1,ON);
for(j = 0; j < 500000; j++)
stat(1,OFF);
}
}
sd_raw_sync();
stat(1,OFF);
log_array2 = 0;
}
if((IOPIN0 & 0x00000008) == 0) // if button pushed, log file & quit
{
VICIntEnClr = 0xFFFFFFFF;
if(RX_in < 512)
{
stringSize = sprintf(output, "%04d-%02d-%02d %02d:%02d:%02d, %s\n\r", year, month, dom, hours, minutes, seconds, (unsigned char *)RX_array1);
fat_write_file(handle, output, stringSize);
sd_raw_sync();
}
else if(RX_in >= 512)
{
fat_write_file(handle, (unsigned char *)RX_array2, RX_in - 512);
sd_raw_sync();
}
while(1)
{
stat(0,ON);
for(j = 0; j < 500000; j++);
stat(0,OFF);
stat(1,ON);
for(j = 0; j < 500000; j++);
stat(1,OFF);
}
}
}
}
void test(void)
{
rprintf("\n\rLogomatic V2 Test Code:\n\r");
rprintf("ADC Test will begin in 5 seconds, hit stop button to terminate the test.\r\n\n");
delay_ms(5000);
while((IOPIN0 & 0x00000008) == 0x00000008)
{
// Get AD1.3
AD1CR = 0x0020FF08;
AD_conversion(1);
// Get AD0.3
AD0CR = 0x0020FF08;
AD_conversion(0);
// Get AD0.2
AD0CR = 0x0020FF04;
AD_conversion(0);
// Get AD0.1
AD0CR = 0x0020FF02;
AD_conversion(0);
// Get AD1.2
AD1CR = 0x0020FF04;
AD_conversion(1);
// Get AD0.4
AD0CR = 0x0020FF10;
AD_conversion(0);
// Get AD1.7
AD1CR = 0x0020FF80;
AD_conversion(1);
// Get AD1.6
AD1CR = 0x0020FF40;
AD_conversion(1);
delay_ms(1000);
rprintf("\n\r");
}
rprintf("\n\rTest complete, locking up...\n\r");
while(1);
}
void AD_conversion(int regbank)
{
int temp = 0, temp2;
if(!regbank) // bank 0
{
AD0CR |= 0x01000000; // start conversion
while((temp & 0x80000000) == 0)
{
temp = AD0DR;
}
temp &= 0x0000FFC0;
temp2 = temp / 0x00000040;
AD0CR = 0x00000000;
}
else // bank 1
{
AD1CR |= 0x01000000; // start conversion
while((temp & 0x80000000) == 0)
{
temp = AD1DR;
}
temp &= 0x0000FFC0;
temp2 = temp / 0x00000040;
AD1CR = 0x00000000;
}
rprintf("%d", temp2);
rprintf(" ");
}
void fat_initialize(void)
{
if(!sd_raw_init())
{
rprintf(“SD Init Error\n\r”);
while(1);
}
if(openroot())
{
rprintf("SD OpenRoot Error\n\r");
}
}
void delay_ms(int count)
{
int i;
count *= 10000;
for(i = 0; i < count; i++)
asm volatile (“nop”);
}
I am having a hard time getting it to open the same file for mulitple logs. I want to create a new file at say midnight every night, so that it why the file names will be date stamped. I tried some things in that while loop in main, but I was unsuccessful. Keep in mind that I lack experience so most of this is the original just hacked on by me.
Thank you