Advertisement
Guest User

Untitled

a guest
May 19th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdint.h>
  2.  
  3.  
  4. struct StepperInterface {
  5.     uint32_t target_pos;
  6.     uint32_t current_pos;
  7.     bool halt_requested;
  8. };
  9.  
  10. far struct StepperInterface shmem __attribute__((location(0x10100))) = {};
  11. //StepperInterface shmem __attribute__((cregister("LOCAL",near))) = {};
  12.  
  13.  
  14. // define some r30 bits
  15. #define CW  (1u << 0)
  16. #define CCW (1u << 1)
  17.  
  18. volatile register uint32_t __R30;
  19.  
  20.  
  21. #define delay_us(n) __delay_cycles((n) * 200u)
  22. #define delay_ms(n) delay_us((n) * 1000u)
  23.  
  24. static inline void deassert_CW_CCW() { __R30 = 0; }
  25. static inline void assert_CW() { __R30 = CW; }
  26. static inline void assert_CCW() { __R30 = CCW; }
  27.  
  28. int main() {
  29.     uint32_t pos = shmem.current_pos;
  30.     deassert_CW_CCW();
  31.     delay_us(1);
  32.    
  33.     while ( !shmem.halt_requested ) {
  34.         int32_t delta = shmem.target_pos - pos;
  35.         if ( delta == 0 )
  36.             continue;
  37.         if ( delta < 0 ) {
  38.             --pos;
  39.             assert_CCW();
  40.         } else {
  41.             ++pos;
  42.             assert_CW();
  43.         }
  44.  
  45.         shmem.current_pos = pos;
  46.         delay_us(1000);
  47.         deassert_CW_CCW();
  48.         delay_us(1000);
  49.     }
  50.     __halt();
  51. }
  52.  
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement