/*
 * File:   main.c
 * Author: michal
 *
 * Created on 2 listopada 2017, 20:18
 */



// PIC32MM0064GPL028 Configuration Bit Settings

// 'C' source line config statements

// FDEVOPT
#pragma config SOSCHP = OFF             // Secondary Oscillator High Power Enable bit (SOSC oprerates in normal power mode.)
#pragma config USERID = 0xFFFF          // User ID bits (User ID bits)

// FICD
#pragma config JTAGEN = OFF             // JTAG Enable bit (JTAG is disabled)
#pragma config ICS = PGx1               // ICE/ICD Communication Channel Selection bits (Communicate on PGEC1/PGED1)

// FPOR
#pragma config BOREN = BOR3             // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware; SBOREN bit disabled)
#pragma config RETVR = OFF              // Retention Voltage Regulator Enable bit (Retention regulator is disabled)
#pragma config LPBOREN = ON             // Low Power Brown-out Enable bit (Low power BOR is enabled, when main BOR is disabled)

// FWDT
#pragma config SWDTPS = PS1048576       // Sleep Mode Watchdog Timer Postscale Selection bits (1:1048576)
#pragma config FWDTWINSZ = PS25_0       // Watchdog Timer Window Size bits (Watchdog timer window size is 25%)
#pragma config WINDIS = OFF             // Windowed Watchdog Timer Disable bit (Watchdog timer is in non-window mode)
#pragma config RWDTPS = PS1048576       // Run Mode Watchdog Timer Postscale Selection bits (1:1048576)
#pragma config RCLKSEL = LPRC           // Run Mode Watchdog Timer Clock Source Selection bits (Clock source is LPRC (same as for sleep mode))
#pragma config FWDTEN = OFF             // Watchdog Timer Enable bit (WDT is disabled)

// FOSCSEL
#pragma config FNOSC = PRI              // Oscillator Selection bits (Primary oscillator (XT, HS, EC))
#pragma config PLLSRC = PRI             // System PLL Input Clock Selection bit (Primary oscillator is selected as PLL reference input on device reset)
#pragma config SOSCEN = OFF             // Secondary Oscillator Enable bit (Secondary oscillator (SOSC) is disabled)
#pragma config IESO = OFF               // Two Speed Startup Enable bit (Two speed startup is disabled)
#pragma config POSCMOD = HS             // Primary Oscillator Selection bit (HS oscillator mode is selected)
#pragma config OSCIOFNC = ON            // System Clock on CLKO Pin Enable bit (System clock is connected to CLKO/OSC2 pin)
#pragma config SOSCSEL = ON             // Secondary Oscillator External Clock Enable bit (External clock is connected to SOSCO pin (RA4 and RB4 are controlled by I/O port registers))
#pragma config FCKSM = CSECME           // Clock Switching and Fail-Safe Clock Monitor Enable bits (Clock switching is enabled; Fail-safe clock monitor is enabled)

// FSEC
#pragma config CP = OFF                 // Code Protection Enable bit (Code protection is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.




#include <xc.h>
#include "structs.h"
#include "processing.h"
#include "ui.h"
#include "hardware.h"

unsigned long CALIBRATIONSPACE[512] __attribute__((space(prog), address(0x9D00D000)));
//global variables
int new_samp_ena = 0;
int iv_sel = 0;
struct results_struct results;
int g_wave_step = 1;
uint32_t wav_len = 25;
uint32_t wav_periods = 250;
int g_cal_autorange_mode = 0;

void main(void)
{
    struct config_struct config;
    struct cal_struct cal = {1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0};
    struct cal_struct *cal_flash_pt = (struct cal_struct*)(0x9D00D000);
    volatile int32_t del;
    
    for(del = 1000000; del > 0; --del);
    PWR_EN_LAT = 0x0;
    init(&config, &cal, cal_flash_pt);
    while(!INT_PORT);
    PWR_EN_LAT = 0x1;

    while(1)
    {
        while(!results.new_data);       //wait for waveform
        config_hardware(&config);       //configure hardware here, within critical section (which ends inside data_acq) - this is to ensure that reconfiguration of the signal path for voltage and current alternately is done while break between samplings
        data_acq(config, &cal);         //here sampled waveform is saved (as voltage or current alternately), then critical section ends (new sampling can start) and data is processed
        send_ui_message();              //communication with user interface
        get_ui_message(&config);        //communication with user interface
        if(config.do_the_cal)           
            calibration(&config, &cal, cal_flash_pt);
        if(config.turn_on_off)
        {
            __pic32_software_reset();
            del++;
        }
    }
    
    return;
}
