// PIC32MX530F128H Configuration Bit Settings

// DEVCFG3
#pragma config USERID = 0xFFFF          // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config PMDL1WAY = OFF           // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
#pragma config IOL1WAY = OFF            // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
#pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by Port Function)
#pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by Port Function)

// DEVCFG2
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_16         // PLL Multiplier (16x Multiplier)
#pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider (2x Divider)
#pragma config UPLLEN = ON              // USB PLL Enable (Enabled)
#pragma config FPLLODIV = DIV_8       // System PLL Output Clock Divider (PLL Divide by 8)

// DEVCFG1
#pragma config FNOSC = PRI              // Oscillator Selection Bits (Primary Osc (XT,HS,EC))
#pragma config FSOSCEN = ON             // Secondary Oscillator Enable (Enabled)
#pragma config IESO = ON                // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = HS             // Primary Oscillator Configuration (HS osc mode)
#pragma config OSCIOFNC = OFF           // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_8           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576        // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF             // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25     // Watchdog Timer Window Size (Window Size is 25%)

// DEVCFG0
#pragma config JTAGEN = OFF             // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx2        // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config PWP = OFF                // Program Flash Write Protect (Disable)
#pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF                 // Code Protect (Protection Disabled)

#include <xc.h> //XC32 prekladac
#include "can.h"
#include "delay.h"
#include "lcd.h"
#include <stdio.h>

#define LED1_ANSEL ANSELBbits.ANSB8
#define LED2_ANSEL ANSELBbits.ANSB9
#define LED3_ANSEL ANSELBbits.ANSB10
#define LED4_ANSEL ANSELBbits.ANSB11

#define LED1_TRIS TRISBbits.TRISB8
#define LED2_TRIS TRISBbits.TRISB9
#define LED3_TRIS TRISBbits.TRISB10
#define LED4_TRIS TRISBbits.TRISB11

#define LED1 LATBbits.LATB8
#define LED2 LATBbits.LATB9
#define LED3 LATBbits.LATB10
#define LED4 LATBbits.LATB11

//jednotlive bajty dat
unsigned char data[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};

unsigned char text[16];
unsigned char i;

unsigned int ID = 0x555;
unsigned char length = 8;

void main() {
    LED1_ANSEL = 0; //dig. pin
    LED2_ANSEL = 0;
    LED3_ANSEL = 0;
    LED4_ANSEL = 0;
    
    LED1_TRIS = 0; //vystup
    LED2_TRIS = 0;
    LED3_TRIS = 0;
    LED4_TRIS = 0;
    
    LED1 = (length >> 3) & 0x01; //LED binarne zobrazi pocet prijatych dat
    LED2 = (length >> 2) & 0x01;
    LED3 = (length >> 1) & 0x01;
    LED4 = length & 0x01;
    
    initLcd();
    setAddrLcd(0, 5);
    writeStringLcd("CAN TX", 6);
    setAddrLcd(1, 0); //vypsat ID
    sprintf(text, "%X", ID);
    writeLineLcd(text);
    setAddrLcd(1, 14); //vypsat pocet bajtu dat
    sprintf(text, "%d", length);
    writeStringLcd(text, 1);
    sprintf(text, "B");
    writeStringLcd(text, 1);
    
    setAddrLcd(2, 0);
    for(i = 0; i < length; i++) { //vypsat odesilana data
        sprintf(text, "%2X", data[i]);
        writeStringLcd(text, 2);
    }
    
    initCan();
    
    while(1) {
        //poslat 8 bajtu dat z pole "data", ID je 0x555 = 0b10101010101
        writeCan(ID, length, data);
        delayMs(500); //pockat 1 sec
        delayMs(500);
    }
}