Skip to content
Snippets Groups Projects
Commit db2900cd authored by Sam Calisch's avatar Sam Calisch
Browse files

used lazy redundant packet checking to get stepper control working

parent e94ec788
Branches
No related tags found
No related merge requests found
Pipeline #
...@@ -19,8 +19,8 @@ static uint8_t rxdata[n_rx_bytes] = {0}; ...@@ -19,8 +19,8 @@ static uint8_t rxdata[n_rx_bytes] = {0};
//uarte //uarte
const uint8_t pin_rx = 8; const uint8_t pin_rx = 8;
const uint8_t pin_tx = 6; const uint8_t pin_tx = 6;
uint16_t last_tx_time = 0; uint32_t last_tx_time = 0;
uint16_t tx_period = 100; //ms uint32_t tx_period = 1000; //ms
// //
//UARTE //UARTE
...@@ -131,14 +131,16 @@ void setup() { ...@@ -131,14 +131,16 @@ void setup() {
as5013_read(); as5013_read();
radio_buffer[0] = 1; //send a move command radio_buffer[0] = 1; //send a move command
radio_buffer[1] = 10; //move 10 steps forward radio_buffer[1] = 10; //move 10 steps forward
radio_send(); //send command radio_send_redundant(); //send command
int result = radio_recv(); //wait for response Serial.println("sent command");
//getting false positives... //int result = radio_recv(); //wait for response
Serial.print("buffer: "); //test for false positives:
Serial.println(radio_buffer[1]); //Serial.print("result: ");
Serial.print("result: "); //Serial.println(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->TXD.PTR = (uint32_t)(&rxdata); //reset pointer to start of buffer
//NRF_UARTE0->TASKS_STARTTX = 1; //trigger start task //NRF_UARTE0->TASKS_STARTTX = 1; //trigger start task
last_tx_time = millis(); last_tx_time = millis();
......
#define PACKET_BASE_ADDRESS_LENGTH (2UL) //Packet base address length field size in bytes #define PACKET_BASE_ADDRESS_LENGTH (2UL) //Packet base address length field size in bytes
#define PACKET_LENGTH 4 #define PACKET_LENGTH 4
#define REDUNDANCY_COUNT 10 //number of transmissions to ensure delivery... hack.
static int16_t radio_buffer[PACKET_LENGTH] = {0}; static int16_t radio_buffer[PACKET_LENGTH] = {0};
//static int16_t reference_buffer[PACKET_LENGTH] = {0}; //for checking against receipt
// //
//RADIO //RADIO
...@@ -13,8 +16,8 @@ void radio_setup(){ ...@@ -13,8 +16,8 @@ void radio_setup(){
delay(10); delay(10);
NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_Pos3dBm << RADIO_TXPOWER_TXPOWER_Pos); NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_Pos3dBm << RADIO_TXPOWER_TXPOWER_Pos);
NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz NRF_RADIO->FREQUENCY = 11UL; // 2400 + X MHz
NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos); NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_2Mbit << RADIO_MODE_MODE_Pos);
NRF_RADIO->PREFIX0 = ((uint32_t)0xC0 << 0); // Prefix byte of address 0 NRF_RADIO->PREFIX0 = ((uint32_t)0xC0 << 0); // Prefix byte of address 0
NRF_RADIO->BASE0 = 0x01234567UL; // Base address for prefix 0 NRF_RADIO->BASE0 = 0x01234567UL; // Base address for prefix 0
...@@ -53,13 +56,18 @@ void radio_send(){ ...@@ -53,13 +56,18 @@ void radio_send(){
//radio_wait_for_ready(); //only generated when actually switching to tx mode //radio_wait_for_ready(); //only generated when actually switching to tx mode
NRF_RADIO->TASKS_START=1; //start NRF_RADIO->TASKS_START=1; //start
radio_wait_for_end(); radio_wait_for_end();
//add wait for ack?
} }
void radio_send_redundant(){
for(int i=0; i<REDUNDANCY_COUNT; i++){
radio_send();
}
}
int radio_recv(){ int radio_recv(){
//return 0 if success //return number of packets before CRC match
NRF_RADIO->EVENTS_CRCOK = 0; NRF_RADIO->EVENTS_CRCOK = 0;
NRF_RADIO->EVENTS_CRCERROR = 0; //NRF_RADIO->EVENTS_CRCERROR = 0;
NRF_RADIO->EVENTS_DEVMATCH = 0; int i=1;
while(true){ while(true){
NRF_RADIO->EVENTS_READY = 0; //clear ready event NRF_RADIO->EVENTS_READY = 0; //clear ready event
NRF_RADIO->TASKS_RXEN=1; //trigger rx enable task NRF_RADIO->TASKS_RXEN=1; //trigger rx enable task
...@@ -67,10 +75,38 @@ int radio_recv(){ ...@@ -67,10 +75,38 @@ int radio_recv(){
//radio_wait_for_ready(); //only generated when actually switching to rx mode //radio_wait_for_ready(); //only generated when actually switching to rx mode
NRF_RADIO->TASKS_START=1; NRF_RADIO->TASKS_START=1;
radio_wait_for_end(); radio_wait_for_end();
Serial.println("got packet"); if (NRF_RADIO->EVENTS_CRCOK == 1){ break;}
Serial.println(NRF_RADIO->EVENTS_CRCOK); i++;
if ((NRF_RADIO->EVENTS_CRCOK == 1) && (NRF_RADIO->EVENTS_DEVMATCH == 1)){ break;} }
delay(10); return i;
}
/*
// start of 3 way handshake implementation
void copy_buffer_to_reference(){
for(int i=0; i++; i<PACKET_LENGTH){
reference_buffer[i] = radio_buffer[i];
}
}
bool buffer_matches_reference(){
bool match = true;
for(int i=0; i++; i<PACKET_LENGTH){
if (reference_buffer[i] != radio_buffer[i]){
match = false;
break;
}
} }
return 0; return match;
} }
void radio_send_with_handshake(){
copy_buffer_to_reference();
radio_send(); //send packet
int crc_match = radio_recv(); //receive ack
if crc_match && buffer_matches_reference(){
radio_send(); //send ack
}
}
*/
...@@ -84,24 +84,18 @@ void setup() { ...@@ -84,24 +84,18 @@ void setup() {
NRF_GPIO->OUTSET = (1<<pin_nrst); //set nrst/nslp high to enable drv8825 NRF_GPIO->OUTSET = (1<<pin_nrst); //set nrst/nslp high to enable drv8825
//debug transmit loop
while(true){
radio_send();
delay(10);
}
while (true) { while (true) {
int result = radio_recv(); //wait until recieve int result = radio_recv(); //wait until recieve
if (result==0){ //if (result==0){
//no crc error //no crc error
parse_command(); parse_command();
} else{ //} else{
//incoming crc error, set radio buffer to all -2 //incoming crc error, set radio buffer to all -2
for(int i=0; i<PACKET_LENGTH; i++){ // for(int i=0; i<PACKET_LENGTH; i++){
radio_buffer[i] = -2; // radio_buffer[i] = -2;
} // }
} //}
radio_send(); //send back radio buffer when done //radio_send(); //send back radio buffer when done
} }
} }
......
#define PACKET_BASE_ADDRESS_LENGTH (2UL) //Packet base address length field size in bytes #define PACKET_BASE_ADDRESS_LENGTH (2UL) //Packet base address length field size in bytes
#define PACKET_LENGTH 4 #define PACKET_LENGTH 4
#define REDUNDANCY_COUNT 10 //number of transmissions to ensure delivery... hack.
static int16_t radio_buffer[PACKET_LENGTH] = {0}; static int16_t radio_buffer[PACKET_LENGTH] = {0};
//static int16_t reference_buffer[PACKET_LENGTH] = {0}; //for checking against receipt
// //
//RADIO //RADIO
...@@ -13,8 +16,8 @@ void radio_setup(){ ...@@ -13,8 +16,8 @@ void radio_setup(){
delay(10); delay(10);
NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_Pos3dBm << RADIO_TXPOWER_TXPOWER_Pos); NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_Pos3dBm << RADIO_TXPOWER_TXPOWER_Pos);
NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz NRF_RADIO->FREQUENCY = 11UL; // 2400 + X MHz
NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos); NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_2Mbit << RADIO_MODE_MODE_Pos);
NRF_RADIO->PREFIX0 = ((uint32_t)0xC0 << 0); // Prefix byte of address 0 NRF_RADIO->PREFIX0 = ((uint32_t)0xC0 << 0); // Prefix byte of address 0
NRF_RADIO->BASE0 = 0x01234567UL; // Base address for prefix 0 NRF_RADIO->BASE0 = 0x01234567UL; // Base address for prefix 0
...@@ -28,6 +31,7 @@ void radio_setup(){ ...@@ -28,6 +31,7 @@ void radio_setup(){
(RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) | (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) |
(2 << RADIO_PCNF1_BALEN_Pos); (2 << RADIO_PCNF1_BALEN_Pos);
NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Three << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Three << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
NRF_RADIO->CRCPOLY = 0x18D; //x8 + x7 + x3 + x2 + 1 = 110001101 NRF_RADIO->CRCPOLY = 0x18D; //x8 + x7 + x3 + x2 + 1 = 110001101
NRF_RADIO->MODECNF0 |= RADIO_MODECNF0_RU_Fast << RADIO_MODECNF0_RU_Pos; //turn on fast ramp up NRF_RADIO->MODECNF0 |= RADIO_MODECNF0_RU_Fast << RADIO_MODECNF0_RU_Pos; //turn on fast ramp up
NRF_RADIO->SHORTS = 0; //turn off all shortcuts, for debug NRF_RADIO->SHORTS = 0; //turn off all shortcuts, for debug
...@@ -48,24 +52,61 @@ void radio_disable(){ ...@@ -48,24 +52,61 @@ void radio_disable(){
void radio_send(){ void radio_send(){
NRF_RADIO->EVENTS_READY = 0; //clear ready event NRF_RADIO->EVENTS_READY = 0; //clear ready event
NRF_RADIO->TASKS_TXEN=1; //trigger tx enable task NRF_RADIO->TASKS_TXEN=1; //trigger tx enable task
delayMicroseconds(15); delayMicroseconds(20);
//radio_wait_for_ready(); //only generated when actually switching to tx mode //radio_wait_for_ready(); //only generated when actually switching to tx mode
NRF_RADIO->TASKS_START=1; //start NRF_RADIO->TASKS_START=1; //start
radio_wait_for_end(); radio_wait_for_end();
//add wait for ack?
} }
void radio_send_redundant(){
for(int i=0; i<REDUNDANCY_COUNT; i++){
radio_send();
}
}
int radio_recv(){ int radio_recv(){
//return 0 if success //return number of packets before CRC match
NRF_RADIO->EVENTS_CRCOK = 0; NRF_RADIO->EVENTS_CRCOK = 0;
NRF_RADIO->EVENTS_CRCERROR = 0; //NRF_RADIO->EVENTS_CRCERROR = 0;
int i=1;
while(true){ while(true){
NRF_RADIO->EVENTS_READY = 0; //clear ready event NRF_RADIO->EVENTS_READY = 0; //clear ready event
NRF_RADIO->TASKS_RXEN=1; //trigger rx enable task NRF_RADIO->TASKS_RXEN=1; //trigger rx enable task
delayMicroseconds(15); delayMicroseconds(20);
//radio_wait_for_ready(); //only generated when actually switching to rx mode //radio_wait_for_ready(); //only generated when actually switching to rx mode
NRF_RADIO->TASKS_START=1; NRF_RADIO->TASKS_START=1;
radio_wait_for_end(); radio_wait_for_end();
if (NRF_RADIO->EVENTS_CRCOK == 1){ break;} if (NRF_RADIO->EVENTS_CRCOK == 1){ break;}
i++;
}
return i;
}
/*
// start of 3 way handshake implementation
void copy_buffer_to_reference(){
for(int i=0; i++; i<PACKET_LENGTH){
reference_buffer[i] = radio_buffer[i];
} }
return 0;
} }
bool buffer_matches_reference(){
bool match = true;
for(int i=0; i++; i<PACKET_LENGTH){
if (reference_buffer[i] != radio_buffer[i]){
match = false;
break;
}
}
return match;
}
void radio_send_with_handshake(){
copy_buffer_to_reference();
radio_send(); //send packet
int crc_match = radio_recv(); //receive ack
if crc_match && buffer_matches_reference(){
radio_send(); //send ack
}
}
*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment