libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pruss_add.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
14 
15 from __future__ import print_function
16 from libpruio import *
17 
18 
41 def load_firmware(IRam):
42  PRUcode = (c_uint32*8)(
43  0x240000e0,
44  0xf104e081,
45  0x30830002,
46  0x00e2e1e1,
47  0xe1002081,
48  0x1004041f,
49  0x2a000000,
50  0x21000100
51  )
52  if(0 >= prussdrv_pru_write_memory(IRam, 0, PRUcode, sizeof(PRUcode))):
53  raise AssertionError("failed loading instructions")
54  return 0
55 
56 
57 io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0)
58 try:
59 
60  IO = io.contents
61  if IO.Errr: raise AssertionError("pruio_new failed (%s)" % IO.Errr)
62 #
63 # Check init success
64 #
65  if(IO.PruNo): # we use the other PRU
66 
67  pru_num = 0
68 
69  pru_iram = PRUSS0_PRU0_IRAM
70 
71  pru_dram = PRUSS0_PRU0_DRAM
72 
73  pru_intr = PRU0_ARM_INTERRUPT
74  else:
75  pru_num = 1
76  pru_iram = PRUSS0_PRU1_IRAM
77  pru_dram = PRUSS0_PRU1_DRAM
78  pru_intr = PRU0_ARM_INTERRUPT # libpruio uses PRU1_ARM_INTERRUPT!
79 #
80 # Now prepare the other PRU
81 #
82  if(prussdrv_open(PRU_EVTOUT_0)): # note: libpruio uses PRU_EVTOUT_5
83  raise AssertionError("prussdrv_open failed (%s)" % IO.Errr)
84  # Note: no prussdrv_pruintc_init(), libpruio did it already
85 
86  load_firmware(pru_iram)
87 #
88 # Pass parameters to PRU
89 #
90 
91  dram = pointer(c_uint32(0))
92  prussdrv_map_prumem(pru_dram, byref(dram)) # get dram pointer
93  dram[1] = 23 # start value
94  dram[2] = 7 # value to add
95  dram[3] = 67 # loop count (max 16 bit = 65536)
96  dram[4] = pru_intr + 16 # the interrupt we're waiting for
97 #
98 # Execute
99 #
100  print("instructions loaded, starting PRU-%d" % pru_num)
101  prussdrv_pru_enable(pru_num, 0) # start @ address 0
102  prussdrv_pru_wait_event(PRU_EVTOUT_0) # wait until finished
103  prussdrv_pru_clear_event(PRU_EVTOUT_0, pru_intr) # clear interrupt (optional, useful when starting again)
104 #
105 # Check result
106 #
107  if(dram[0] == (dram[1] + (dram[2] * dram[3]))):
108  print("Test OK %d == %d + (%d * %d)\n" % (dram[0], dram[1], dram[2], dram[3]))
109  else:
110  print("Test failed: %d != %d + (%d * %d)\n" % (dram[0], dram[1], dram[2], dram[3]))
111 
112  prussdrv_pru_disable(pru_num) # disable PRU
113  # note: no prussdrv_exit(), libpruio does it in the destructor
114 
115 finally:
116  pruio_destroy(io)
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
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)
#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)