From ddb3c769f0f5c80d95cd4b51d38e62cdca69d4f2 Mon Sep 17 00:00:00 2001
From: Sam Calisch <s.calisch@gmail.com>
Date: Mon, 25 Sep 2017 14:29:06 -0400
Subject: [PATCH] added microstepping, current limit, and step speed commands

---
 as5013-test/nrf52-as5013/nrf52-as5013.ino   | 34 +++++++-----
 as5013-test/nrf52-drv8825/nrf52-drv8825.ino | 60 ++++++++++++---------
 2 files changed, 57 insertions(+), 37 deletions(-)

diff --git a/as5013-test/nrf52-as5013/nrf52-as5013.ino b/as5013-test/nrf52-as5013/nrf52-as5013.ino
index f8ea518..4c09a76 100644
--- a/as5013-test/nrf52-as5013/nrf52-as5013.ino
+++ b/as5013-test/nrf52-as5013/nrf52-as5013.ino
@@ -16,6 +16,9 @@
 static uint8_t txdata[n_tx_bytes] = {0};
 static uint8_t rxdata[n_rx_bytes] = {0};
 
+//uint16_t step_period = 30000; //microseconds
+
+
 //uarte
 const uint8_t pin_rx = 8;
 const uint8_t pin_tx = 6;
@@ -121,28 +124,33 @@ void as5013_read(){
 //main
 //
 void setup() {
-  Serial.begin(115200);
-  //uarte_setup();
+  //Serial.begin(115200);
+  uarte_setup();
   twi_setup();
   as5013_setup();
   radio_setup();
+
+  //set step speed
+  radio_buffer[0] = 2; //set step speed
+  radio_buffer[1] = 10000; //30ms step period
+  radio_send_redundant();
+  //set full steps
+  radio_buffer[0] = 3; //set step speed
+  radio_buffer[1] = 0; //M1,M0 = 0,1
+  radio_send_redundant();
+  //set current limit
+  radio_buffer[0] = 4; //set step speed
+  radio_buffer[1] = 100; //M1,M0 = 0,1
+  radio_send_redundant();
+  
   while(1){
     if (millis() - last_tx_time > tx_period){
       as5013_read();
       radio_buffer[0] = 1; //send a move command
       radio_buffer[1] = 10; //move 10 steps forward
       radio_send_redundant(); //send command
-      Serial.println("sent command");
-      //int result = radio_recv(); //wait for response
-      //test for false positives:
-      //Serial.print("result: ");
-      //Serial.println(result);
-      //if (radio_buffer[1] != 0){
-      //  Serial.print("XXXXX buffer: ");
-      //  Serial.println(radio_buffer[1]);
-      //}
-      //NRF_UARTE0->TXD.PTR = (uint32_t)(&rxdata);  //reset pointer to start of buffer
-      //NRF_UARTE0->TASKS_STARTTX = 1;  //trigger start task
+      NRF_UARTE0->TXD.PTR = (uint32_t)(&rxdata);  //reset pointer to start of buffer
+      NRF_UARTE0->TASKS_STARTTX = 1;  //trigger start task
       last_tx_time = millis();     
     }
   }
diff --git a/as5013-test/nrf52-drv8825/nrf52-drv8825.ino b/as5013-test/nrf52-drv8825/nrf52-drv8825.ino
index 6f27656..c13df2a 100644
--- a/as5013-test/nrf52-drv8825/nrf52-drv8825.ino
+++ b/as5013-test/nrf52-drv8825/nrf52-drv8825.ino
@@ -1,5 +1,6 @@
 #include "radio.h"
 uint16_t pwms[1] = {0};
+uint16_t step_period = 20000; //microseconds
 
 const uint8_t pin_mode1 = 3; //A1
 const uint8_t pin_mode0 = 0; //XL1
@@ -27,10 +28,10 @@ void stepper_setup(){
   NRF_PWM0->ENABLE = (PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
   NRF_PWM0->MODE = (PWM_MODE_UPDOWN_Up << PWM_MODE_UPDOWN_Pos);
   NRF_PWM0->PRESCALER = (PWM_PRESCALER_PRESCALER_DIV_1 << PWM_PRESCALER_PRESCALER_Pos); //16MHz tick
-  NRF_PWM0->COUNTERTOP = (1600 << PWM_COUNTERTOP_COUNTERTOP_Pos); //1 kHz pwm freq.
+  NRF_PWM0->COUNTERTOP = (1600 << PWM_COUNTERTOP_COUNTERTOP_Pos); //10 kHz pwm freq.
   NRF_PWM0->LOOP = (PWM_LOOP_CNT_Disabled << PWM_LOOP_CNT_Pos);
   NRF_PWM0->DECODER = (PWM_DECODER_LOAD_Common << PWM_DECODER_LOAD_Pos) | (PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos);
-  pwms[0] = 1500; //50% duty cycle to test
+  pwms[0] = 1600-100; //100/1600 * 3.3v = .2V = 200 mA limit
   NRF_PWM0->SEQ[0].PTR = ((uint32_t)(pwms) << PWM_SEQ_PTR_PTR_Pos);
   NRF_PWM0->SEQ[0].CNT = (1 << PWM_SEQ_CNT_CNT_Pos);
   NRF_PWM0->SEQ[0].REFRESH = 0;
@@ -51,27 +52,47 @@ void parse_command(){
     }
     for(i=0; i<radio_buffer[1]; i++){
       NRF_GPIO->OUTSET = (1<<pin_step);
-      delay(10);
+      delayMicroseconds(step_period);
       NRF_GPIO->OUTCLR = (1<<pin_step);
-      delay(1);        
+      delayMicroseconds(10);        
     }
-    
-    //reset radio buffer
-    for(i=0; i<PACKET_LENGTH; i++){
-      radio_buffer[i] = 0;
+  } else if (radio_buffer[0] == 2){
+    //change step speed
+    step_period = radio_buffer[1];
+  } else if (radio_buffer[0] == 3){
+    //change microstepping
+    if (radio_buffer[1] && 1){ 
+      NRF_GPIO->OUTSET = (1 << pin_mode0);
+    } else {
+      NRF_GPIO->OUTCLR = (1 << pin_mode0);      
+    }
+    if (radio_buffer[1] && 2){ 
+      NRF_GPIO->OUTSET = (1 << pin_mode0);
+    } else {
+      NRF_GPIO->OUTCLR = (1 << pin_mode0);      
     }
-  } else{
-    //unrecognized command, set radio buffer to all -1
-    for(int i=0; i<PACKET_LENGTH; i++){
-      radio_buffer[i] = -1;
-    }      
+  } else if (radio_buffer[0] == 4){
+    pwms[0] = 1600-radio_buffer[1]; //100/1600 * 3.3v = .2V = 200 mA limit
+    NRF_PWM0->SEQ[0].REFRESH = 1;
+  }
+  else{
+  }
+
+  //unrecognized command, set radio buffer to all -1
+  //for(int i=0; i<PACKET_LENGTH; i++){
+  //  radio_buffer[i] = -1;
+  //}      
+
+  //reset radio buffer
+  for(i=0; i<PACKET_LENGTH; i++){
+    radio_buffer[i] = 0;
   }
 }
 
 
 void setup() {
 
-  Serial.begin(115200);
+  //Serial.begin(115200);
   
   //Switch to internal LFCLK to disconnect from XL1 and XL2
   NRF_CLOCK->LFCLKSRC = 0; //disconnect XL1 AND XL2 FROM LFCLK?
@@ -86,16 +107,7 @@ void setup() {
 
   while (true) {
     int result = radio_recv(); //wait until recieve
-    //if (result==0){
-      //no crc error
-    parse_command();
-    //} else{
-      //incoming crc error, set radio buffer to all -2
-    //  for(int i=0; i<PACKET_LENGTH; i++){
-    //    radio_buffer[i] = -2;
-    //  }  
-    //}
-    //radio_send(); //send back radio buffer when done
+    parse_command(); //todo: move stepping to non-blocking so can receive commands during moves
   }
 }
 
-- 
GitLab