#!   /usr/bin/env   python

# Copyright Creotech SA, 2015
# Author: Marek Guminski <marek.guminski@creotech.pl>
# With small modifications from WZab ;-)
# Wojciech M. Zabolotny <wzab@ise.pw.edu.pl>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org


import sys
import time
import os
import csr
import time

from spec import *
from ad9516 import *
from dac import *
from ptsexcept import PtsCritical


"""
test01: 
    DAC tests
"""


FIRMWARE_PATH = '/../syn/spec_top.bin'


def main (card=None, default_directory='.',suite=None):

    delay = 0;

    carrier=spec( FIRMWARE_PATH, 1, True );
    

    sys_pll = ad9516(carrier);
    
    dacinst = dac(carrier);
    
    for i in range(10):
        time.sleep(delay)
            
        dacinst.set_en(1);
        dacinst.set_ampl(1);
        dacinst.set_shape(dacinst.SHAPE_CONST);
        time.sleep(delay)
        
        dacinst.set_en(1);
        dacinst.set_ampl(1/2);
        dacinst.set_shape(dacinst.SHAPE_SQUARE);
        print "amp/2"
        time.sleep(delay)
        
        print "amp/4"
        dacinst.set_ampl(1/4);
        time.sleep(delay);
        
        print "amp/8"
        dacinst.set_ampl(1/8);
        time.sleep(delay);
        
        
        dacinst.set_ampl(1);     
        for j in range(4):
            print "freq:%d" % (j+1)*4
            dacinst.set_frequency((j+1)*4e4);
            
            
            for k in range(14):
                print "Ampl: %d" % pow(2, 14-k )
                print "Bit: %d" % (14-k)
                dacinst.set_ampl_bit( pow(2, 14-k ) );   
                time.sleep(delay);
        
            # przy zapalonym bicie 6 sygnal ma okolo 5mv i malo wystaje z szumu
            # przynajmniej na oscyloskopie
            
        
        dacinst.set_ampl(3.3/2);
        time.sleep(delay);
            
    return 0
        
if __name__ == '__main__' :
    main()

