Advertisement
Guest User

Untitled

a guest
May 18th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <stdint.h>
  2.  
  3. struct StepperInterface {
  4.     uint32_t target_pos;
  5.     uint32_t current_pos;
  6.     bool halt_requested;
  7. };
  8. StepperInterface shmem __attribute__((cregister("LOCAL", near))) = {};
  9. //StepperInterface shmem __attribute__((location(0))) = {};
  10.  
  11.  
  12. // define some r30 bits
  13. #define CW  (1u << 0)
  14. #define CCW (1u << 1)
  15.  
  16. uint32_t volatile register __R30;
  17.  
  18. #define delay_us(n) __delay_cycles((n) * 200u)
  19. #define delay_ms(n) delay_us((n) * 1000u)
  20.  
  21. static inline void enable()
  22. {
  23.     // this could either control the ENA signal or perhaps disable the
  24.     // control signals.  either way, probably want to use normal GPIO for
  25.     // it since the PRU direct gpio (via R30) have random values at boot.
  26. }
  27.  
  28. static inline void disable()
  29. {
  30. }
  31.  
  32. static inline void deassert_CW_CCW() {  __R30 = 0;  }
  33. static inline void assert_CW() {  __R30 = CW;  }
  34. static inline void assert_CCW() {  __R30 = CCW;  }
  35.  
  36. // CW/CCW version
  37. int main()
  38. {
  39.     uint32_t pos = shmem.current_pos;
  40.     deassert_CW_CCW();
  41.     delay_us(1);  // just to be paranoid
  42.     enable();
  43.     delay_ms(200);
  44.     while( ! shmem.halt_requested ) {
  45.         int32_t delta = shmem.target_pos - pos;
  46.         if( delta == 0 )
  47.             continue;
  48.         // XXX optional: check for excessive delta
  49.         if( delta < 0 ) {
  50.             --pos;
  51.             assert_CCW();
  52.         } else {
  53.             ++pos;
  54.             assert_CW();
  55.         }
  56.         shmem.current_pos = pos;  // for diagnostic purposes
  57.         delay_us(1);
  58.         deassert_CW_CCW();
  59.         delay_us(1);
  60.     }
  61.     disable();
  62.     __halt();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement