DenseBrainMatrix

Untitled

Dec 27th, 2023
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.90 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include "pru_uart.h"
  5. #include "pru_ecap.h"
  6. #include "sys_gpio.h"
  7.  
  8. // define some r30 bits
  9. #define CW  (1 << 3)
  10. #define CCW (1 << 7)
  11. #define delay_us(n) __delay_cycles((n) * 200u)
  12. #define delay_ms(n) delay_us((n) * 1000u)
  13.  
  14. struct StepperInterface {
  15.       uint32_t target_pos;
  16.       uint32_t current_pos;
  17.       uint32_t speed;
  18.       bool halt_requested;
  19.       bool moving;
  20. };
  21.  
  22. struct Message {
  23.       uint32_t id;
  24.       uint32_t timestamp;
  25.       int32_t control_signal;
  26.       int32_t error_signal;
  27.       int16_t force;
  28.       int16_t cycle_count;
  29.       uint16_t counter;
  30.       uint16_t signal_length;
  31. };
  32.  
  33. // layout of shared ddr3 memory (filled in by loader)
  34. struct DDRLayout {
  35.       Message volatile *msgbuf;
  36.       uint16_t num_msgs;
  37.       uint16_t msg_size;
  38. };
  39.  
  40. struct UserControlVars {
  41.       // set by pru before halting
  42.       char const *abort_file;
  43.       int abort_line;
  44.       uint32_t time;
  45.       // read-pointer updated by python
  46.       uint16_t ridx;
  47.       uint16_t cycle_limit;
  48.       uint16_t time_limit;
  49.       uint16_t signal_length;
  50.       uint16_t signal[2000];
  51. };
  52.  
  53. far struct DDRLayout ddr __attribute__((location(0x10000))) = {};
  54. far struct UserControlVars volatile shmem __attribute__((location(0x10100))) = {};
  55. far struct StepperInterface volatile stepper __attribute__ ((location(0x20000))) = {};
  56. volatile register uint32_t __R30;
  57.  
  58. static inline uint32_t timestamp_cycles()
  59. {
  60.     return CT_ECAP.TSCTR;
  61. }
  62.  
  63. // for easier debugging, record where in the source code we halted
  64. __attribute__((noreturn))
  65. void abort_at( char const *file, int line )
  66. {
  67.       shmem.abort_file = file;
  68.       shmem.abort_line = line;
  69.       for(;;) __halt();
  70. }
  71.  
  72. static inline void assert_at( bool cond, char const *file, int line )
  73. {
  74.       if( ! cond )
  75.             abort_at( file, line );
  76. }
  77.  
  78. #define abort() abort_at( __FILE__, __LINE__ )
  79. #define assert(cond) assert_at( (cond), __FILE__, __LINE__ )
  80.  
  81. // local copy of write-pointer
  82. static uint16_t widx = 0;
  83.  
  84. // global var for write-pointer is located right after message ringbuffer
  85. #define ddr_msgbuf_end  ( ddr.msgbuf + ddr.num_msgs )
  86. #define ddr_widx  ( *(uint16_t volatile *)ddr_msgbuf_end )
  87.  
  88. // receive byte from uart
  89. static inline char uart_recv_byte()
  90. {
  91.     for(;;) {
  92.         uint8_t lsr = CT_UART.LSR;
  93.         if( lsr & 0x1e )
  94.             abort();  // receive-error occurred
  95.         if( lsr & 0x01 )
  96.             return (char) CT_UART.RBR;
  97.     }
  98. }
  99.  
  100. // receive CR-terminated line from uart
  101. static inline uint8_t uart_recv_line( char volatile msg[], uint8_t maxlen )
  102. {
  103.     uint8_t len = 0;
  104.  
  105.     for(;;) {
  106.         char c = uart_recv_byte();
  107.         if( c == '\r' )
  108.             break;  // found end of line
  109.         if( len == maxlen )
  110.             abort();  // line does not fit in buffer
  111.         msg[ len++ ] = c;
  112.     }
  113.     return len;
  114. }
  115.  
  116.  
  117. int16_t receive_measurement()
  118. {
  119.     // allocate buffer at fixed address for debugging convenience
  120.     static char volatile msg[8] __attribute__((location(0x1f00)));
  121.  
  122.     // receive line from uart
  123.     uint8_t len = uart_recv_line( msg, sizeof msg );
  124.  
  125.     // verify length and prefix
  126.     if( len != 8 || msg[0] != 'N' || (msg[1] != '+' && msg[1] != '-'))
  127.         abort();
  128.  
  129.     // parse the remainder as integer
  130.     int16_t value = 0;
  131.     uint8_t i;
  132.     for( i = 2; i < len; i++ ) {
  133.         if( msg[i] < '0' || msg[i] > '9' )
  134.             abort();
  135.         value = value * 10 + ( msg[i] - '0' );
  136.     }
  137.     if (msg[1] == '-')
  138.             value *= -1;
  139.     return value;
  140. }
  141.  
  142.  
  143. void initialize()
  144. {
  145.       // perform sanity-checking
  146.       assert( 0x80000000 <= (uint32_t)ddr.msgbuf );
  147.       assert( ddr.msgbuf < ddr_msgbuf_end );
  148.       assert( ddr.msg_size == sizeof(Message) );
  149.       assert( ddr_widx == widx );
  150.       assert( shmem.ridx == widx );
  151. }
  152.  
  153.  
  154. static inline uint16_t next_idx( uint16_t idx )
  155. {
  156.       if( ++idx == ddr.num_msgs )
  157.             idx = 0;
  158.       return idx;
  159. }
  160.  
  161.  
  162. void send_message( uint32_t id, int16_t force, int32_t input, int32_t output, uint16_t cycle,uint16_t counter, uint16_t signal_length)
  163. {
  164.       uint16_t next_widx = next_idx( widx );
  165.  
  166.       if( next_widx == shmem.ridx ) {
  167.             // can't send message, ringbuffer is full
  168.             abort();
  169.       }
  170.  
  171.       Message volatile *msg = &ddr.msgbuf[ widx ];
  172.  
  173.       // fill in contents of message
  174.       msg->id = id;
  175.       msg->force = force;
  176.       msg->timestamp = timestamp_cycles();
  177.       msg->control_signal = input;
  178.       msg->error_signal = output;
  179.       msg->cycle_count = cycle;
  180.       msg->counter = counter;
  181.       msg->signal_length = signal_length;
  182.       // update write-pointer
  183.       ddr_widx = widx = next_widx;
  184. }
  185.  
  186. void main() {
  187.  
  188.       const int32_t pulses_per_revolutions = 800;
  189.       int32_t max_diff = pulses_per_revolutions/2;
  190.       uint32_t pos = stepper.current_pos;
  191.       int32_t control_signal = 0;
  192.       initialize();
  193.       
  194.       uint32_t id =0;
  195.  
  196.       uint16_t counter = 0;
  197.       gpio_set_one_low(&GPIO0,31);
  198.  
  199.       uint16_t cycle_count = 0;
  200.       bool running = true;
  201.       uint16_t last_cycle = shmem.cycle_limit;
  202.       uint16_t time_limit = shmem.time_limit;
  203.       uint16_t signal_length = shmem.signal_length;
  204.       while (running) {
  205.             if (cycle_count <= last_cycle)
  206.             {
  207.                   int32_t input_signal = shmem.signal[counter];   
  208.                   int16_t force = 0;
  209.                   control_signal = input_signal - 0;
  210.  
  211.                   // check if motor direction must change  
  212.                   if (control_signal == 0) {
  213.                         //stepper.moving = false;
  214.                   } else if (control_signal < 0) {
  215.                         --pos;
  216.                         __R30 |= CCW;
  217.                         //stepper.moving = true;
  218.                         // place lower bound on control signal
  219.                         if (control_signal < -(max_diff << 16))
  220.                               control_signal = -(max_diff << 16);
  221.                   } else {
  222.                         ++pos;
  223.                         __R30 |= CW;
  224.                         //stepper.moving = true;
  225.                         //place upper bound on control signal
  226.                         if (control_signal > (max_diff  << 16))
  227.                               control_signal = (max_diff  << 16);
  228.                   }     
  229.                   //report to python
  230.                   //send_message( ++id,force,input_signal,control_signal,cycle_count,counter,signal_length);
  231.                   
  232.                   delay_us(100);
  233.                   __R30 = 0;
  234.                   delay_us(100);
  235.                   stepper.current_pos = pos;
  236.                   stepper.target_pos = pos;
  237.                   
  238.                   ++counter;
  239.                   
  240.                   // reset counter to enforce periodicy
  241.                   if (counter >= signal_length){
  242.                         counter = 0;
  243.                         ++cycle_count;
  244.                   }
  245.             } else {
  246.                   int16_t force = 0; //receive_measurement();
  247.                   int32_t input_signal = shmem.signal[counter];   
  248.                   int32_t output_signal = 0;    
  249.                   send_message( ++id, force, input_signal,output_signal,cycle_count,counter,signal_length);
  250.             }
  251.       }    
  252. }
Advertisement
Add Comment
Please, Sign In to add comment