Advertisement
Guest User

Untitled

a guest
Jun 14th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stddef.h>
  3. #include "pru_uart.h"
  4. #include "pru_ecap.h"
  5.  
  6.  
  7. struct Message {
  8. uint32_t id;
  9. uint32_t position;
  10. };
  11.  
  12.  
  13. // encoder position in other PRU core's memory
  14. #define position_var ((uint32_t const volatile *)0x00002000)
  15.  
  16.  
  17. // layout of shared ddr3 memory (filled in by loader)
  18. struct DDRLayout {
  19. Message volatile *msgbuf;
  20. uint16_t num_msgs;
  21. uint16_t msg_size;
  22. };
  23.  
  24. struct SharedVars {
  25. // set by pru before halting
  26. char const *abort_file;
  27. int abort_line;
  28.  
  29. // read-pointer updated by python
  30. uint16_t ridx;
  31. };
  32.  
  33. far struct DDRLayout ddr __attribute__((location(0x10000))) = {};
  34. far struct SharedVars volatile shmem __attribute__((location(0x10100))) = {};
  35.  
  36. // for easier debugging, record where in the source code we halted
  37. __attribute__((noreturn))
  38. void abort_at( char const *file, int line )
  39. {
  40. shmem.abort_file = file;
  41. shmem.abort_line = line;
  42. for(;;) __halt();
  43. }
  44.  
  45. static inline void assert_at( bool cond, char const *file, int line )
  46. {
  47. if( ! cond )
  48. abort_at( file, line );
  49. }
  50.  
  51. #define abort() abort_at( __FILE__, __LINE__ )
  52. #define assert(cond) assert_at( (cond), __FILE__, __LINE__ )
  53.  
  54. // local copy of write-pointer
  55. static uint16_t widx = 0;
  56.  
  57. // global var for write-pointer is located right after message ringbuffer
  58. #define ddr_msgbuf_end ( ddr.msgbuf + ddr.num_msgs )
  59. #define ddr_widx ( *(uint16_t volatile *)ddr_msgbuf_end )
  60.  
  61. void initialize()
  62. {
  63. // perform sanity-checking
  64. assert( 0x80000000 <= (uint32_t)ddr.msgbuf );
  65. assert( ddr.msgbuf < ddr_msgbuf_end );
  66. assert( ddr.msg_size == sizeof(Message) );
  67.  
  68. assert( ddr_widx == widx );
  69. assert( shmem.ridx == widx );
  70. }
  71.  
  72. static inline uint16_t next_idx( uint16_t idx )
  73. {
  74. if( ++idx == ddr.num_msgs )
  75. idx = 0;
  76. return idx;
  77. }
  78.  
  79. void send_message( uint32_t id, uint32_t position)
  80. {
  81. uint16_t next_widx = next_idx( widx );
  82.  
  83. if( next_widx == shmem.ridx ) {
  84. // can't send message, ringbuffer is full
  85. abort();
  86. }
  87.  
  88. Message volatile *msg = &ddr.msgbuf[ widx ];
  89.  
  90. // fill in contents of message
  91. msg->id = id;
  92. // msg->position = *position_var;
  93.  
  94. // update write-pointer
  95. ddr_widx = widx = next_widx;
  96. }
  97.  
  98. void main() {
  99. initialize();
  100.  
  101. // CT_UART.THR = 'S';
  102. // CT_UART.THR = 'N';
  103. // CT_UART.THR = '\r';
  104.  
  105. uint32_t position = *position_var;
  106. uint32_t id = 0;
  107. for(;;) {
  108. // force = receive_measurement();
  109. send_message( ++id,position );
  110. __delay_cycles( 10000 );
  111. }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement