libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pruss_toggle.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
16 
17 from __future__ import print_function
18 from libpruio import *
19 
20 
48 def load_firmware(IRam):
49  PRUcode = (c_uint32*13)(
50  0x240000e0,
51  0xf100a081,
52  0x240001e0,
53  0x0801e0e1,
54  0x30820006,
55  0x14e1fefe,
56  0x240000e0,
57  0x240000e0,
58  0x240000e0,
59  0x240000e0,
60  0x1003031f,
61  0x2a000000,
62  0x21000400
63  )
64  if(0 >= prussdrv_pru_write_memory(IRam, 0, PRUcode, sizeof(PRUcode))):
65  raise AssertionError("failed loading instructions")
66  return 0
67 
68 
69 io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0)
70 #io = pruio_new(PRUIO_DEF_ACTIVE | PRUIO_ACT_FREMUX, 4, 0x98, 0)
71 try:
72 
73  IO = io.contents
74  if IO.Errr: raise AssertionError("pruio_new failed (%s)" % IO.Errr)
75 #
76 # Check init success
77 #
78  if(IO.PruNo): # we use the other PRU
79 
80  pru_num = 0
81 
82  pru_iram = PRUSS0_PRU0_IRAM
83 
84  pru_dram = PRUSS0_PRU0_DRAM
85 
86  pru_intr = PRU0_ARM_INTERRUPT
87  else:
88  pru_num = 1
89  pru_iram = PRUSS0_PRU1_IRAM
90  pru_dram = PRUSS0_PRU1_DRAM
91  pru_intr = PRU0_ARM_INTERRUPT # libpruio uses PRU1_ARM_INTERRUPT!
92 #
93 # Now prepare the other PRU
94 #
95  if(prussdrv_open(PRU_EVTOUT_0)): # note: libpruio uses PRU_EVTOUT_5
96  raise AssertionError("prussdrv_open failed (%s)" % IO.Errr)
97  # Note: no prussdrv_pruintc_init(), libpruio did it already
98 
99  load_firmware(pru_iram)
100 #
101 # Pinmuxing (some examples first)
102 #
103  # # set PRU-0-r31 bit 15 input with pull up resistor
104  #if (IO.setPin(io, P8_15, 6 | PRUIO_RX_ACTIV | PRUIO_PULL_UP)) {
105  #raise AssertionError("P8_15 configuration failed (%s)" % IO.Errr)
106 
107  # # set PRU-0-r31 bit 10 input (mode 6), pull down resistor
108  #if (IO.setPin(io, SD_08, 6 | PRUIO_RX_ACTIV | PRUIO_PULL_DOWN)) {
109  #raise AssertionError("SD_08 configuration failed (%s)" % IO.Errr)
110 
111  # # set PRU-0-r30 bit 10 output (mode 5) no resitor
112  #if (IO.setPin(io, SD_08, 5 | PRUIO_NO_PULL)) {
113  #raise AssertionError("SD_08 configuration failed (%s)" % IO.Errr)
114 
115  # set PRU-0-r30 bit 15 output (mode 6), pull down resistor
116  if (IO.setPin(io, P8_11, 6)):
117  raise AssertionError("P8_11 configuration failed (%s)" % IO.Errr)
118 #
119 # Prepare libpruio measurement
120 #
121  if (pruio_cap_config(io, P9_42, 2.)): # configure CAP input pin
122  raise AssertionError("failed setting input P9_42 (%s)" % IO.Errr)
123  if (pruio_config(io, 1, 0, 0, 0)):
124  raise AssertionError("config failed (%s)" % IO.Errr)
125 
126  f = c_float(0)
127 
128  d = c_float(0)
129 #
130 # Pass parameters to PRU
131 #
132 
133  dram = pointer(c_uint32(0))
134  prussdrv_map_prumem(pru_dram, byref(dram)) # get dram pointer
135  dram[0] = 15 # bit number, must match configured pin (P8_11)
136  dram[1] = 16 # loop count (max 16 bit = 65535)
137  dram[2] = pru_intr + 16 # the interrupt we're waiting for
138 #
139 # Execute 20 times
140 #
141  print("instructions loaded, starting PRU-%d" % pru_num)
142  prussdrv_pru_enable(pru_num, 0) # start @ address 0
143  for i in range(0, 20):
144 
145  prussdrv_pru_wait_event(PRU_EVTOUT_0) # wait until PRU finished
146  prussdrv_pru_clear_event(PRU_EVTOUT_0, pru_intr) # clr interrupt
147 
148  if(pruio_cap_Value(io, P9_42, byref(f), byref(d))): # get last measurement
149  raise AssertionError("failed reading input P9_42 (%s)" % IO.Errr)
150 
151  print("--> Frequency: %3.0f MHz, Duty:%3.0f %c" %
152  ((f.value * .000001), (d.value * 100), '%')) # results
153 
154  prussdrv_pru_resume(pru_num) # continue after HALT
155 
156  prussdrv_pru_disable(pru_num) # disable PRU
157  # note: no prussdrv_exit(), libpruio does it in the destructor
158 
159 finally:
160 
161  pruio_destroy(io)
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
char * pruio_cap_Value(pruIo *Io, uint8 Ball, float_t *Hz, float_t *Du)
Wrapper function for CapMod::Value().
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
char * pruio_cap_config(pruIo *Io, uint8 Ball, float_t FLow)
Wrapper function for CapMod::config().
void pruio_destroy(pruIo *Io)
Wrapper function for the destructor PruIo::~PruIo().
int32 prussdrv_pru_disable(uint32 prunum)
See prussdrv_pru_disable(BYVAL_AS_UInt32 PruId)
char * prussdrv_pru_resume(uint32 prunum)
See prussdrv_pru_resume(BYVAL_AS_UInt32 PruId)
#define prussdrv_pru_enable(N)
Macro for variadic parameter list.
void prussdrv_pru_clear_event(uint32 host_interrupt, uint32 sysevent)
See prussdrv_pru_clear_event(BYVAL_AS_UInt32 Irq, BYVAL_AS_UInt32 Event)
uint32 prussdrv_pru_wait_event(uint32 host_interrupt)
See prussdrv_pru_wait_event(BYVAL_AS_UInt32 Irq)
int32 prussdrv_open(uint32 host_interrupt)
See prussdrv_open(BYVAL_AS_UInt32 Irq)
int32 prussdrv_pru_write_memory(uint32 pru_ram_id, uint32 wordoffset, const uint32 *memarea, uint32 bytelength)
See prussdrv_pru_write_memory(BYVAL_AS_UInt32 RamId, BYVAL_AS_UInt32 Offs, BYVAL_AS_CONST_UInt32_PTR ...
int32 prussdrv_map_prumem(uint32 pru_ram_id, void **address)
See prussdrv_map_prumem(BYVAL_AS_UInt32 RamId, BYVAL_AS_ANY_PTR_PTR Addr)