#include "SPI.h"
#include "define.h"
#include "BoardCfg.h"
#include "DACInterface.h"

unsigned char mDACSPIBaudrate;
void DACInit()
{
    mDACSPIBaudrate = SPICalculateBRG(PERIPHERAL_FREQ, 20000000);
}

int DACSetValue(int Channel, int Value)
{
    unsigned short Buf;
    unsigned char LSByte, MSByte;

    Buf = Value << 2;
    Buf &= (0x0FFF);
      
    LSByte = (unsigned char)(Buf & 0xFF);
    Buf >>= 8;
    MSByte = (Buf & 0xFF);
            
    switch(Channel)
    {
        case BUREAU_DAC_CHANNEL:
        {
            SPI2CONbits.CKE = 0;
            VU1_DAC_SYNC_PIN = 0;
            SPITransaction(MSByte,mDACSPIBaudrate);
            SPITransaction(LSByte,mDACSPIBaudrate);
            VU1_DAC_SYNC_PIN = 1;
            SPI2CONbits.CKE = 1;
            break;
        }
        case CUISINE_DAC_CHANNEL:
        {
            SPI2CONbits.CKE = 0;
            VU2_DAC_SYNC_PIN = 0;
            SPITransaction(MSByte,mDACSPIBaudrate);
            SPITransaction(LSByte,mDACSPIBaudrate);
            VU2_DAC_SYNC_PIN = 1;
            SPI2CONbits.CKE = 1;
            break;
        }
        default:
        {
            break;
        }
    }
}
