canid

beagle-nrf905

Dec 21st, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. from time import sleep
  5. import spi
  6. import Adafruit_BBIO.GPIO as GPIO
  7.  
  8. myDev = "/dev/spidev1.0"
  9.  
  10. def main():
  11.     init()
  12.     try:
  13. #        print_config()
  14.         for n in range (0, 200):
  15.             test_tx("test single...", n)
  16.             sleep(.01)
  17.         spi.closeSPI()
  18. #        print_config()
  19.     finally:
  20.         spi.closeSPI()
  21.         set_power_down()
  22.         GPIO.cleanup()
  23.  
  24. ## Functions:
  25. def init():
  26.     sys.stdout.write("[*] Initializing radio... ")
  27.     GPIO.setup("P9_11", GPIO.OUT) # PWR  - Power up chip. Active high.
  28.     GPIO.setup("P9_13", GPIO.OUT) # TXEN - Transmit mode. Low for Recieve mode.
  29.     GPIO.setup("P9_15", GPIO.OUT) # CE   - Enable transciever. Active high.
  30.     GPIO.setup("P9_12", GPIO.IN)  # CD   - Carrier detect. High when carrier present on tuned freq at greater than -95dBm.
  31.     GPIO.setup("P9_14", GPIO.IN)  # AM   - Address match. High when packet header address field matches transciever address.
  32.     GPIO.setup("P9_16", GPIO.IN)  # DR   - Data ready. High when data packet is recieved
  33.     GPIO.output("P9_11", GPIO.LOW)
  34.     GPIO.output("P9_13", GPIO.LOW)
  35.     GPIO.output("P9_15", GPIO.LOW)
  36.  
  37.     set_stand_by()
  38.     try:
  39.         spi.openSPI(device=myDev, speed=10000000, mode=0)
  40.         spi.transfer((0b00000000, 0b00100000, 0b00001011, 0b01000100, 0b01000100, 0b00000100, 0xff, 0xff, 0xff, 0xff, 0b11100111))
  41.         print "done.\n"
  42.     except:
  43.         print "Failsauce! ", sys.exc_info()[0]
  44.     finally:
  45.         spi.closeSPI()
  46.  
  47. def set_config(addr, val1, val2, desc):
  48.     try:
  49.         sys.stdout.write("[*] Changing config register[s]... ")
  50.         set_stand_by()
  51.         spi.openSPI(device=myDev, speed=10000, mode=0)
  52.         spi.transfer((0b00000000^addr, val1, val2))
  53.         print "done.\n"
  54.     except:
  55.         print "Failmobile! Could not "+desc, sys.exc_info()[0]
  56.     finally:
  57.         spi.closeSPI()
  58.  
  59. def test_tx(desc, iter):
  60.     try:
  61. #        sys.stdout.write("["+str(iter)+"] Trying send payload...")
  62.         set_stand_by()
  63.  
  64.         # Writing address, payload and config:
  65.         spi.openSPI(device=myDev, speed=10000, mode=0)
  66.         spi.transfer((0b00100010, 0b11011110, 0b10101101, 0b10111110, 0b11101111))
  67.         spi.closeSPI()
  68.  
  69.         spi.openSPI(device=myDev, speed=10000, mode=0)
  70. #        spi.transfer((0b00100000, 0b10111010, 0b10101101, 0b11110000, 0b00001101))
  71.         spi.transfer((0b00100000, iter, iter, iter, iter))
  72.         spi.closeSPI()
  73.  
  74.         spi.openSPI(device=myDev, speed=10000, mode=0)
  75.         spi.transfer((0b00000000, 0b00011111, 0b00111111, 0b01000100, 0b01000100, 0b00000100, 0xca, 0xfe, 0xca, 0xfe, 0b11011111))
  76.         spi.closeSPI()
  77.  
  78.         set_tx()
  79.         while (not GPIO.input("P9_16")):
  80.             continue
  81.  
  82.     except:
  83.         print "Failmobile! Could not "+desc, sys.exc_info()[0]
  84. #    finally:
  85. #        print " done.\n"
  86.  
  87. def print_config():
  88.     try:
  89.         set_stand_by()
  90.         spi.openSPI(device=myDev, speed=10000, mode=0)
  91.         print "[*] Reading config registers:"
  92.  
  93.         for offset in range (0b0000, 0b1010):
  94.                 bytes = spi.transfer((0b00010000^offset, 0b00010000^offset))
  95.                 sys.stdout.write(str("\t"+hex(offset)+": "+ '{:08b}'.format(bytes[1]))+": "+hex(bytes[1])+" \n")
  96.     except:
  97.                 print "Could not read byte"
  98.     finally:
  99.         spi.closeSPI()
  100.         print "done.\n"
  101.  
  102. def set_power_down():
  103.     GPIO.setup("P9_11", GPIO.OUT)
  104.     GPIO.output("P9_11", GPIO.LOW)
  105.  
  106.     GPIO.setup("P9_13", GPIO.IN)
  107.     GPIO.setup("P9_15", GPIO.IN)
  108.  
  109. def set_stand_by():
  110.     GPIO.setup("P9_11", GPIO.OUT)
  111.     GPIO.output("P9_11", GPIO.HIGH)
  112.  
  113.     GPIO.setup("P9_13", GPIO.IN)
  114.  
  115.     GPIO.setup("P9_15", GPIO.OUT)
  116.     GPIO.output("P9_15", GPIO.LOW)
  117.  
  118. def set_read_rx():
  119.     GPIO.setup("P9_11", GPIO.OUT)
  120.     GPIO.output("P9_11", GPIO.HIGH)
  121.  
  122.     GPIO.setup("P9_13", GPIO.OUT)
  123.     GPIO.output("P9_13", GPIO.LOW)
  124.  
  125.     GPIO.setup("P9_15", GPIO.IN)
  126.  
  127. def set_rx():
  128.     GPIO.setup("P9_11", GPIO.OUT)
  129.     GPIO.output("P9_11", GPIO.HIGH)
  130.  
  131.     GPIO.setup("P9_13", GPIO.OUT)
  132.     GPIO.output("P9_13", GPIO.LOW)
  133.  
  134.     GPIO.setup("P9_15", GPIO.OUT)
  135.     GPIO.output("P9_15", GPIO.HIGH)
  136.  
  137. def set_tx():
  138.    
  139.     GPIO.setup("P9_11", GPIO.OUT)
  140.     GPIO.output("P9_11", GPIO.HIGH)
  141.    
  142.     GPIO.setup("P9_13", GPIO.OUT)
  143.     GPIO.output("P9_13", GPIO.HIGH)
  144.    
  145.     GPIO.setup("P9_15", GPIO.OUT)
  146.     GPIO.output("P9_15", GPIO.HIGH)
  147.  
  148. ############################
  149. if __name__ == "__main__":
  150.     main()
Advertisement
Add Comment
Please, Sign In to add comment