Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- struct StepperInterface {
- uint32_t target_pos;
- uint32_t current_pos;
- uint32_t speed;
- bool halt_requested;
- bool moving;
- };
- far struct StepperInterface shmem __attribute__((location(0x000000))) = {};
- //StepperInterface shmem __attribute__((cregister("LOCAL",near))) = {};
- // define some r30 bits
- #define CW (1 << 3)
- #define CCW (1 << 7)
- volatile register uint32_t __R30;
- #define delay_us(n) __delay_cycles((n) * 200u)
- #define delay_ms(n) delay_us((n) * 1000u)
- int main() {
- uint32_t pos = shmem.current_pos;
- __R30 = 0;
- delay_us(1);
- while ( !shmem.halt_requested ) {
- uint32_t speed = shmem.speed;
- int32_t delta = shmem.target_pos - pos;
- if ( delta == 0 ) {
- shmem.moving = false;
- continue;
- }
- if ( delta < 0 ) {
- --pos;
- __R30 |= CCW;
- delta *= -1;
- shmem.moving = true;
- } else {
- ++pos;
- __R30 |= CW;
- shmem.moving = true;
- }
- shmem.current_pos = pos;
- switch(speed){
- case 1:
- delay_us(50);
- __R30 = 0;
- delay_us(50);
- break;
- case 2:
- delay_us(100);
- __R30 = 0;
- delay_us(100);
- break;
- default:
- delay_us(500);
- __R30 = 0;
- delay_us(500);
- }
- }
- __halt();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement