Author: Anonymous Language: python
Description: No description Timestamp: 2011-03-25 18:43:42 -0400
View raw paste Reply
  1. #!/usr/bin/env python
  2. #
  3. # Vaisala RS92-SGP Frequency Reprogramming Script
  4. # By Mark Jessop, 24-03-2011
  5. # mjessop@eleceng.adelaide.edu.au
  6. #
  7. # To use this script, you will need a Bus Pirate, information is available at:
  8. # http://code.google.com/p/the-bus-pirate/
  9. #
  10. # The pyBusPirate libraries are available from:
  11. # https://github.com/audiohacked/pyBusPirate
  12. #
  13. # You will also need pySerial: http://pyserial.sourceforge.net/
  14. #
  15. # You will also need a cable to connect the bus pirate to the Sonde. The pin
  16. # connections for my cable is as follows:
  17. #
  18. # Bus Pirate    SONDE16-pin
  19. # ---------------------------
  20. # 1 = GND               3 = GND
  21. # 2 = 3V3               16 = VCC
  22. # 3 = 5V
  23. # 4 = ADC      
  24. # 5 = Vpu
  25. # 6 = AUX
  26. # 7 = CLK               12 = EEPROM CLK
  27. # 8 = MOSI      11 = EEPROM D (in)
  28. # 9 = CS                10 = EEPROM CS
  29. # 10 = MISO     13 = EEPROM Q (out)
  30. #
  31. # The 16-pin connector is the un-populated pin-header at the bottom-right of
  32. # the board, not the edge connector.
  33. #
  34.  
  35. from pyBusPirate.BinaryMode.SPI import *
  36. import math,sys,time
  37.  
  38. # Enter your serial port information here
  39. SERIAL_PORT = "/dev/tty.usbserial-A7004E8c"
  40. SERIAL_BAUD = 115200
  41.  
  42. MIN_FREQ = 400.0
  43. MAX_FREQ = 423.0
  44.  
  45. F1_ADDR = [0x70,0x02]
  46. F2_ADDR = [0x70,0x03]
  47.  
  48. EEPROM_WRITE_ENABLE = 0x06
  49. EEPROM_WRITE_BYTE = 0x02
  50. EEPROM_READ_BYTE = 0x03
  51.  
  52. def freq_to_bytes(freq):
  53.     freq = float(freq)
  54.    
  55.     if(freq>MAX_FREQ or freq<MIN_FREQ):
  56.         print "Frequency out of PLL lock range"
  57.         return [0]
  58.        
  59.     freq = freq - MIN_FREQ
  60.    
  61.     f2 = int(math.floor(freq/2.560))
  62.     f1 = int(math.floor((freq-(f2*2.560))/0.01))
  63.    
  64.     return [f1,f2]
  65.    
  66. def bytes_to_freq(f1,f2):
  67.     f1 = int(f1)
  68.     f2 = int(f2)
  69.    
  70.     freq = (((f1<<8)/256.0)*10 + 400000 + f2*2560.0)/1000.0
  71.     return "%.3f" % freq
  72.    
  73.  
  74. def prog_freq(freq):
  75.     freq_bytes = freq_to_bytes(freq)
  76.     if(len(freq_bytes)==1):
  77.         print "Bad Frequency. Halting."
  78.         return 0
  79.    
  80.     print "Chosen Frequency = " + str(freq) + "MHz"
  81.     print "Hex values are " + hex(freq_bytes[0]) + "," + hex(freq_bytes[1])
  82.    
  83.    
  84.     print "Opening serial connection to bus pirate..."
  85.     spi = SPI(SERIAL_PORT,SERIAL_BAUD)
  86.    
  87.     print "Entering binmode: ",
  88.     if spi.BBmode():
  89.         print "OK."
  90.     else:
  91.         print "Failed."
  92.         return -1
  93.    
  94.     print "Entering raw SPI mode: ",
  95.     if spi.enter_SPI():
  96.         print "OK."
  97.     else:
  98.         print "Failed."
  99.         return -1
  100.    
  101.     print "Configuring SPI:"
  102.     if spi.cfg_pins(PinCfg.POWER | PinCfg.CS):
  103.         print "3V3 Power On."
  104.     else:
  105.         print "Pin Configuration Failed?"
  106.    
  107.     if spi.set_speed(SPISpeed._125KHZ):
  108.         print "SPI Speed set to 125KHz."
  109.    
  110.     if spi.cfg_spi(SPICfg.CLK_EDGE | SPICfg.OUT_TYPE):
  111.         print "SPI Configuration Finished."
  112.     spi.timeout(0.2)
  113.        
  114.     print ""
  115.     print "Reading current frequency: ",
  116.    
  117.     spi.CS_Low()
  118.     f1 = spi.bulk_trans(4,[EEPROM_READ_BYTE,F1_ADDR[0],F1_ADDR[1],0])[-1]
  119.     spi.CS_High()
  120.     time.sleep(0.05)
  121.     spi.CS_Low()
  122.     f2 = spi.bulk_trans(4,[EEPROM_READ_BYTE,F2_ADDR[0],F2_ADDR[1],0])[-1]
  123.     spi.CS_High()
  124.     print bytes_to_freq(ord(f1),ord(f2)) + "MHz"
  125.    
  126.     response = str(raw_input("Confirm Programming of new frequency ("+str(freq)+"MHz)? "))
  127.     if(response != "y"):
  128.         print "Not programming."
  129.         return -1
  130.        
  131.     print "Clearing Status Bits."
  132.     spi.CS_Low()
  133.     spi.bulk_trans(1,[EEPROM_WRITE_ENABLE])
  134.     spi.CS_High()
  135.     time.sleep(0.05)
  136.    
  137.     spi.CS_Low()
  138.     spi.bulk_trans(2,[0x01,0x00])
  139.     spi.CS_High()
  140.     time.sleep(0.2)
  141.    
  142.     print "Status Register: ",
  143.     spi.CS_Low()
  144.     status_register = ord(spi.bulk_trans(2,[0x05,0,00])[-1])
  145.     spi.CS_High()
  146.    
  147.     print hex(status_register)
  148.     if(status_register != 0):
  149.         print "Status register not clear, cannot write!"
  150.         return -1
  151.    
  152.     print "Writing f1. ",
  153.     spi.CS_Low()
  154.     spi.bulk_trans(1,[EEPROM_WRITE_ENABLE])
  155.     spi.CS_High()
  156.     time.sleep(0.2)
  157.    
  158.     spi.CS_Low()
  159.     spi.bulk_trans(4,[EEPROM_WRITE_BYTE,F1_ADDR[0],F1_ADDR[1],freq_bytes[0]])
  160.     spi.CS_High()
  161.     time.sleep(0.2)
  162.  
  163.     print "Writing f2."
  164.     spi.CS_Low()
  165.     spi.bulk_trans(1,[EEPROM_WRITE_ENABLE])
  166.     spi.CS_High()
  167.     time.sleep(0.2)
  168.    
  169.     spi.CS_Low()
  170.     spi.bulk_trans(4,[EEPROM_WRITE_BYTE,F2_ADDR[0],F2_ADDR[1],freq_bytes[1]])
  171.     spi.CS_High()
  172.     time.sleep(0.2)
  173.    
  174.     print "Confirming Write: ",
  175.     spi.CS_Low()
  176.     f1 = spi.bulk_trans(4,[EEPROM_READ_BYTE,F1_ADDR[0],F1_ADDR[1],0])[-1]
  177.     spi.CS_High()
  178.     time.sleep(0.05)
  179.     spi.CS_Low()
  180.     f2 = spi.bulk_trans(4,[EEPROM_READ_BYTE,F2_ADDR[0],F2_ADDR[1],0])[-1]
  181.     spi.CS_High()
  182.     new_freq = bytes_to_freq(ord(f1),ord(f2))
  183.    
  184.     if(new_freq == bytes_to_freq(freq_bytes[0],freq_bytes[1])):
  185.         print "OK - Frequency set to " + new_freq + "MHz."
  186.     else:
  187.         print "Error: Freq = " + new_freq
  188.        
  189.     spi.resetBP()
  190.    
  191. freq = float(raw_input("Enter new frequency (MHz): "))
  192. prog_freq(freq)
  193.    
View raw paste Reply