diff --git a/as5013-test/nrf52-as5013/nrf52-as5013.ino b/as5013-test/nrf52-as5013/nrf52-as5013.ino index fef0576be41957cc5fb1888b90bbd68a9ac2a72f..f8ea518e49bcaac3daa63f66dd3df42fc80c3499 100644 --- a/as5013-test/nrf52-as5013/nrf52-as5013.ino +++ b/as5013-test/nrf52-as5013/nrf52-as5013.ino @@ -19,8 +19,8 @@ static uint8_t rxdata[n_rx_bytes] = {0}; //uarte const uint8_t pin_rx = 8; const uint8_t pin_tx = 6; -uint16_t last_tx_time = 0; -uint16_t tx_period = 100; //ms +uint32_t last_tx_time = 0; +uint32_t tx_period = 1000; //ms // //UARTE @@ -131,14 +131,16 @@ void setup() { as5013_read(); radio_buffer[0] = 1; //send a move command radio_buffer[1] = 10; //move 10 steps forward - radio_send(); //send command - int result = radio_recv(); //wait for response - //getting false positives... - Serial.print("buffer: "); - Serial.println(radio_buffer[1]); - Serial.print("result: "); - Serial.println(result); - + 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 last_tx_time = millis(); diff --git a/as5013-test/nrf52-as5013/radio.h b/as5013-test/nrf52-as5013/radio.h index ee6b0059b76006ed538f696067528b002f982c01..89b5972f14ffde0034a4e49cb5a929b7e25dfaad 100644 --- a/as5013-test/nrf52-as5013/radio.h +++ b/as5013-test/nrf52-as5013/radio.h @@ -1,7 +1,10 @@ #define PACKET_BASE_ADDRESS_LENGTH (2UL) //Packet base address length field size in bytes #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 reference_buffer[PACKET_LENGTH] = {0}; //for checking against receipt + // //RADIO @@ -13,8 +16,8 @@ void radio_setup(){ delay(10); NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_Pos3dBm << RADIO_TXPOWER_TXPOWER_Pos); - NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz - NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos); + NRF_RADIO->FREQUENCY = 11UL; // 2400 + X MHz + 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->BASE0 = 0x01234567UL; // Base address for prefix 0 @@ -53,13 +56,18 @@ void radio_send(){ //radio_wait_for_ready(); //only generated when actually switching to tx mode NRF_RADIO->TASKS_START=1; //start 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(){ - //return 0 if success + //return number of packets before CRC match NRF_RADIO->EVENTS_CRCOK = 0; - NRF_RADIO->EVENTS_CRCERROR = 0; - NRF_RADIO->EVENTS_DEVMATCH = 0; + //NRF_RADIO->EVENTS_CRCERROR = 0; + int i=1; while(true){ NRF_RADIO->EVENTS_READY = 0; //clear ready event NRF_RADIO->TASKS_RXEN=1; //trigger rx enable task @@ -67,10 +75,38 @@ int radio_recv(){ //radio_wait_for_ready(); //only generated when actually switching to rx mode NRF_RADIO->TASKS_START=1; radio_wait_for_end(); - Serial.println("got packet"); - Serial.println(NRF_RADIO->EVENTS_CRCOK); - if ((NRF_RADIO->EVENTS_CRCOK == 1) && (NRF_RADIO->EVENTS_DEVMATCH == 1)){ break;} - delay(10); + 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]; + } +} +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 + } +} +*/ + + diff --git a/as5013-test/nrf52-drv8825/nrf52-drv8825.ino b/as5013-test/nrf52-drv8825/nrf52-drv8825.ino index 8e5b659789ee5589bb312eff6ee694a18f35ce37..6f276566d39544758a25ec9e85fcf766f4624ed4 100644 --- a/as5013-test/nrf52-drv8825/nrf52-drv8825.ino +++ b/as5013-test/nrf52-drv8825/nrf52-drv8825.ino @@ -84,24 +84,18 @@ void setup() { NRF_GPIO->OUTSET = (1<<pin_nrst); //set nrst/nslp high to enable drv8825 - //debug transmit loop - while(true){ - radio_send(); - delay(10); - } - while (true) { int result = radio_recv(); //wait until recieve - if (result==0){ + //if (result==0){ //no crc error - parse_command(); - } else{ + 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 + // for(int i=0; i<PACKET_LENGTH; i++){ + // radio_buffer[i] = -2; + // } + //} + //radio_send(); //send back radio buffer when done } } diff --git a/as5013-test/nrf52-drv8825/radio.h b/as5013-test/nrf52-drv8825/radio.h index 040f155262395cf7127bd483f812cf6956474450..89b5972f14ffde0034a4e49cb5a929b7e25dfaad 100644 --- a/as5013-test/nrf52-drv8825/radio.h +++ b/as5013-test/nrf52-drv8825/radio.h @@ -1,7 +1,10 @@ #define PACKET_BASE_ADDRESS_LENGTH (2UL) //Packet base address length field size in bytes #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 reference_buffer[PACKET_LENGTH] = {0}; //for checking against receipt + // //RADIO @@ -13,8 +16,8 @@ void radio_setup(){ delay(10); NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_Pos3dBm << RADIO_TXPOWER_TXPOWER_Pos); - NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz - NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos); + NRF_RADIO->FREQUENCY = 11UL; // 2400 + X MHz + 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->BASE0 = 0x01234567UL; // Base address for prefix 0 @@ -28,6 +31,7 @@ void radio_setup(){ (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) | (2 << RADIO_PCNF1_BALEN_Pos); 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->MODECNF0 |= RADIO_MODECNF0_RU_Fast << RADIO_MODECNF0_RU_Pos; //turn on fast ramp up NRF_RADIO->SHORTS = 0; //turn off all shortcuts, for debug @@ -48,24 +52,61 @@ void radio_disable(){ void radio_send(){ NRF_RADIO->EVENTS_READY = 0; //clear ready event 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 NRF_RADIO->TASKS_START=1; //start 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(){ - //return 0 if success + //return number of packets before CRC match NRF_RADIO->EVENTS_CRCOK = 0; - NRF_RADIO->EVENTS_CRCERROR = 0; + //NRF_RADIO->EVENTS_CRCERROR = 0; + int i=1; while(true){ NRF_RADIO->EVENTS_READY = 0; //clear ready event 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 NRF_RADIO->TASKS_START=1; radio_wait_for_end(); 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]; + } +} +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 + } +} +*/ + +