#!/usr/bin/python
#coding: utf8

# Copyright Seven Solutions S.L., 2012
# Author: Tomasz?
# Author: Richard Carrillo <rcarrillo(AT)sevensols.com>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
# Website: http://www.sevensols.com
# Version: 0.1 (Last modifications: 1/4/2012)

from ptsexcept import *
from dio_fmc import *

import rr
import os


"""
test00: Check fmc-dio-5chttl-board EEPROM and DAC presence and temperature-sensor operation
Conditions: I2C bus and OneWire bus work
User intervention required: No
Procedure details:
- Load firmware
- Test mezzanine presence line.
- Initialize the board and its peripherals
- Check that I2C devices (EEPROM (24AA64T) and DAC (DAC5578)) are present
- Check that OneWire device (temperature sensor (DS18B20)) is present and responds
- Check temperature sensor ID
- Check temperature acquisition.
"""

class test00:
   def __init__(self, spec):
      """ Initialize the board, check PRESENT pin state and look for I2C devices """
      try:
         self.dio = CFmcDio(spec, 0x80000, True)
      except CFmcDioError as e:
         err_msg="While fmc-dio-5chttla initialization: "+str(e)
         if e.bus_name == "FMC":
            raise PtsCritical(err_msg) # Critical error: the fmc-dio-5chttl-board is apparently not present
         else:
            raise PtsError(err_msg) # Non-critical error: further tests could be performed successfully

   def test_temp(self):
      """ Check OneWire device response correctly and check temperature acquisition """
      try:
         uid = self.dio.get_unique_id()
      except CFmcDioError as e:
         print str(e)
         emsg="While getting DS18B20 chip ID: "+str(e)
      else:
         try:
            print ("FMC temperature: %3.3f°C" % self.dio.get_temp())
         except CFmcDioError as e:
            print str(e)
            emsg="While getting temperature with DS18B20 chip: "+str(e)
         else:
            emsg=None # test completed successfully
      return emsg

def main(default_directory="."):
   # Configure the FPGA using the program fpga_loader
   path_fpga_loader = '../firmwares/fpga_loader'
   path_firmware = '../firmwares/spec_top.bin'
   firmware_loader = os.path.join(default_directory, path_fpga_loader)
   bitstream = os.path.join(default_directory, path_firmware)
   print "Loading firmware: %s" % (firmware_loader + ' ' + bitstream)
   os.system( firmware_loader + ' ' + bitstream )

   # Load board library and open the corresponding device
   print "Loading hardware access library and opening device\n"
   spec = rr.Gennum()

   print "Test Start"

   init_test_time = time.time()
   print "Configuring and checking fmc-dio-5chttla devices"
   test=test00(spec)
   print "\nChecking temperature acquisition"
   emsg=test.test_temp()

   end_test_time = time.time()
   print "\nEnd of Test"
   print "RESULT: [{}]".format("FAIL" if emsg else "OK")
   print 'Test03 elapsed time: {:.2f} seconds'.format(end_test_time-init_test_time)
   if emsg:
      raise PtsError(emsg)

if __name__ == "__main__":
    main(".")

