Advertisement
Guest User

Untitled

a guest
Jun 13th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from uio.utils import fix_ctypes_struct
  4. from uio.ti.icss import Icss
  5. import ctypes
  6. from ctypes import c_uint32 as uint32, c_uint16 as uint16, c_int32 as int32, c_uint8 as uint8
  7. from time import sleep
  8. from sys import exit
  9.  
  10. pruss = Icss( "/dev/uio/pruss/module" )
  11. pruss.initialize()
  12. ddr = pruss.ddr
  13.  
  14. core = pruss.core0
  15. core.load( 'fw-c/stream.out' )
  16.  
  17. class Message( ctypes.Structure ):
  18. _fields_ = [
  19. ("id",uint32), # id
  20. ("time", uint32), # time in cycles
  21. ("position", uint32), # in encoder pulsest 1140 per Revo
  22. ("force", int32), # in grams i
  23. ("duty_cycle", uint8), # duty cycle 0%-100%
  24. ("wave_shape",uint8), # wave shape input
  25.  
  26. ]
  27.  
  28. # used to communicate ddr layout to C program
  29. class DDRLayout( ctypes.Structure ):
  30. _fields_ = [
  31. ( 'msgbuf', uint32 ),
  32. ( 'num_msgs', uint16 ),
  33. ( 'msg_size', uint16 ),
  34. ]
  35.  
  36. # volatile variables in pruss shared memory
  37. class SharedVars( ctypes.Structure ):
  38. _fields_ = [
  39. ( 'abort_file', uint32 ),
  40. ( 'abort_line', uint32 ),
  41.  
  42. ( 'ridx', uint16 ),
  43. ]
  44.  
  45. # if you don't want the ringbuffer at the start of the ddr region, specify offset here
  46. MSGBUF_OFFSET = 0
  47.  
  48. # you can use a fixed ringbuffer size:
  49. #NUM_MSGS = 1024
  50. # or you can scale the ringbuffer to fit the size of the ddr region:
  51. NUM_MSGS = ( ddr.size - MSGBUF_OFFSET - ctypes.sizeof( u16 ) ) // ctypes.sizeof( Message )
  52. NUM_MSGS = min( NUM_MSGS, 65535 ) # make sure number of messages fits in u16
  53.  
  54. # map shared memory variables
  55. ddr_layout = core.map( DDRLayout, 0x10000 )
  56. shmem = core.map( SharedVars, 0x10100 )
  57. msgbuf = ddr.map( Message * NUM_MSGS, MSGBUF_OFFSET )
  58. ddr_widx = ddr.map( u16, MSGBUF_OFFSET + ctypes.sizeof( msgbuf ) )
  59.  
  60. # inform pru about layout of shared ddr memory region
  61. ddr_layout.msgbuf = ddr.address + MSGBUF_OFFSET
  62. ddr_layout.num_msgs = NUM_MSGS
  63. ddr_layout.msg_size = ctypes.sizeof( Message )
  64.  
  65. # local copies of read-pointer and write-pointer
  66. ridx = 0
  67. widx = 0
  68.  
  69. # initialize pointers in shared memory
  70. shmem.ridx = ridx
  71. ddr_widx.value = widx
  72.  
  73. # ready, set, go!
  74. core.run()
  75.  
  76. def check_core():
  77. if not core.halted:
  78. return
  79.  
  80. if core.state.crashed:
  81. msg = f'core crashed at pc={core.pc}'
  82. elif shmem.abort_file == 0:
  83. msg = f'core halted at pc={core.pc}'
  84. else:
  85. # FIXME figure out better way to read C-string from PRU memory
  86. abort_file = core.read( ctypes.c_char * 32, shmem.abort_file ).value
  87. abort_file = abort_file.decode("ascii")
  88. msg = f'core aborted at pc={core.pc} ({abort_file}:{shmem.abort_line})'
  89.  
  90. # dump some potentially interesting information:
  91. msg += f'\n ridx = {ridx}'
  92. msg += f'\n shmem.ridx = {shmem.ridx}'
  93. msg += f'\n ddr_widx = {ddr_widx.value}'
  94.  
  95. exit( msg )
  96.  
  97. lastid = 0
  98.  
  99. def recv_messages():
  100. global ridx, widx, lastid
  101.  
  102. # read updated write-pointer
  103. widx = ddr_widx.value
  104. assert widx < NUM_MSGS # sanity-check
  105.  
  106. # process messages
  107. while ridx != widx:
  108. # note: it may be faster to copy a batch of messages from shared memory
  109. # instead of directly accessing individual messages and their fields.
  110. msg = msgbuf[ ridx ]
  111.  
  112. # sanity-check that message id increments monotonically
  113. lastid = ( lastid + 1 ) & 0xffffffff
  114. assert msg.id == lastid
  115.  
  116. #checking to see if I got message
  117. print('Message contents position {} and force {}.format(msg.position,msg.force))
  118. # consume message and update read pointer
  119. del msg # direct access to message forbidden beyond this point
  120. ridx += 1
  121. if ridx == NUM_MSGS:
  122. ridx = 0
  123. shmem.ridx = ridx
  124.  
  125. # update user interface
  126. print( f'\ridx=0x{ridx:04x} id=0x{lastid:08x} ', end='', flush=True )
  127.  
  128.  
  129. try:
  130. while True:
  131. recv_messages()
  132. check_core()
  133.  
  134. sleep( 0.01 )
  135.  
  136. except KeyboardInterrupt:
  137. pass
  138.  
  139. finally:
  140. print( '', flush=True )
  141. core.halt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement