|
|
@ -18,9 +18,6 @@ |
|
|
|
uint rcChannels[SBUS_CHANNEL_NUMBER]; |
|
|
|
uint32_t sbusTime = 0; |
|
|
|
|
|
|
|
uint32_t counter = 900; |
|
|
|
uint32_t last_count = 0; |
|
|
|
|
|
|
|
SoftwareSerial swSer1; |
|
|
|
|
|
|
|
SBUS::SBUS(void) { |
|
|
@ -34,9 +31,8 @@ void SBUS::begin(void) { |
|
|
|
// swSer1.begin(200000, SWSERIAL_8E2, SBUS_PORT, SBUS_PORT, false, 264);
|
|
|
|
// swSer1.begin(100000, SWSERIAL_8E2, SBUS_PORT, SBUS_PORT);
|
|
|
|
swSer1.begin(200000, SWSERIAL_8E2, SBUS_PORT, SBUS_PORT); |
|
|
|
//// swSer1.enableTx(true);
|
|
|
|
//// swSer1.enableIntTx(true);
|
|
|
|
|
|
|
|
swSer1.enableTx(true); |
|
|
|
swSer1.enableIntTx(true); |
|
|
|
|
|
|
|
// Initialize to 1000
|
|
|
|
for(uint8_t i = 0; i < SBUS_CHANNEL_NUMBER; i++) { |
|
|
@ -48,10 +44,12 @@ void SBUS::run(void) { |
|
|
|
} |
|
|
|
|
|
|
|
uint32_t last_profile = 0; |
|
|
|
uint32_t total_high = 0; |
|
|
|
uint32_t total_low = 0; |
|
|
|
|
|
|
|
// Sending SBUS packets
|
|
|
|
void SBUS::writePackets(void) { |
|
|
|
if((uint32_t)(millis() - sbusTime) > SBUS_UPDATE_RATE) { |
|
|
|
// if((uint32_t)(millis() - sbusTime) > SBUS_UPDATE_RATE) {
|
|
|
|
if(signalNotOK) { |
|
|
|
// DEBUG ONLY...later put back to 900
|
|
|
|
// rcChannels[2] = 900;
|
|
|
@ -70,20 +68,23 @@ void SBUS::writePackets(void) { |
|
|
|
swSer1.write(sbusPacket, SBUS_PACKET_LENGTH); |
|
|
|
|
|
|
|
|
|
|
|
// Looping through the bytes - Generating Parity Bits
|
|
|
|
// Is using SBUS_FAST (200000 Baud), 8E2 (Even Parity and 2 bits stop
|
|
|
|
// For sending at 200000 baud, each bit used about 5uS
|
|
|
|
// Profiling each bits timing before sending
|
|
|
|
// // Looping through the bytes - Generating Parity Bits
|
|
|
|
// // Is using SBUS_FAST (200000 Baud), 8E2 (Even Parity and 2 bits stop
|
|
|
|
// // For sending at 200000 baud, each bit used about 5uS
|
|
|
|
// // Profiling each bits timing before sending
|
|
|
|
// uint32_t clock_cycles = 0;
|
|
|
|
//
|
|
|
|
// if((uint32_t)(millis() - last_profile) > 500) {
|
|
|
|
// Serial.printf("Cmd: %d Signal: %d\r\n", rcCommand[2], signalNotOK);
|
|
|
|
// last_profile = millis();
|
|
|
|
// }
|
|
|
|
if((uint32_t)(millis() - last_profile) > 500) { |
|
|
|
Serial.printf("Cmd: %d Signal: %d Sent: %d NRF: %d\r\n", rcCommand[2], signalNotOK, sbus_packets_sent, nrf_packets_received); |
|
|
|
last_profile = millis(); |
|
|
|
} |
|
|
|
//
|
|
|
|
// total_high = 0;
|
|
|
|
// total_low = 0;
|
|
|
|
//
|
|
|
|
// // bool tx_state = true;
|
|
|
|
// uint8_t parity_count = 0;
|
|
|
|
// uint8_t data_bits = 0;
|
|
|
|
// int8_t data_bits = 0;
|
|
|
|
// bool current_bit = 0;
|
|
|
|
//
|
|
|
|
// // Now we roughly know how much per clock cycles per microseconds
|
|
|
@ -94,105 +95,99 @@ void SBUS::writePackets(void) { |
|
|
|
//
|
|
|
|
// // START BIT - For 1 clock cycle = 5uS = clock_per_micro
|
|
|
|
// // Set to LOW as start bit
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
//
|
|
|
|
// // Low for 50% duty cycle
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_ON_TIME; clock_cycles++);
|
|
|
|
//
|
|
|
|
// GPOC = (1 << SBUS_PORT);
|
|
|
|
// delay_cycles(PULSE_BIT_TIME);
|
|
|
|
//
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// delay_cycles(PULSE_GAP);
|
|
|
|
//
|
|
|
|
// // High for 50% duty cycle
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_OFF_TIME; clock_cycles++);
|
|
|
|
//
|
|
|
|
// // DATA FRAME
|
|
|
|
// // Reading the data sending it byte by byte
|
|
|
|
// ///// for(data_bits = 7; data_bits >= 0; data_bits--) {
|
|
|
|
// for(data_bits = 0; data_bits < 8; data_bits++) {
|
|
|
|
// current_bit = (sbusPacket[pos] >> data_bits) & 0x01;
|
|
|
|
//
|
|
|
|
// // Sending it out...
|
|
|
|
// if(current_bit) {
|
|
|
|
// if(current_bit == 0x01) {
|
|
|
|
// parity_count++;
|
|
|
|
//
|
|
|
|
// total_high++;
|
|
|
|
//
|
|
|
|
// // HIGH
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_HIGH_ON_TIME; clock_cycles++);
|
|
|
|
//
|
|
|
|
// // LOW
|
|
|
|
// GPOC = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_HIGH_OFF_TIME; clock_cycles++);
|
|
|
|
//
|
|
|
|
// delay_cycles(PULSE_BIT_TIME);
|
|
|
|
//
|
|
|
|
// } else {
|
|
|
|
// // HIGH
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_ON_TIME; clock_cycles++);
|
|
|
|
//
|
|
|
|
// // LOW
|
|
|
|
// total_low++;
|
|
|
|
//
|
|
|
|
// GPOC = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_OFF_TIME; clock_cycles++);
|
|
|
|
// delay_cycles(PULSE_BIT_TIME);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // GAP
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// delay_cycles(PULSE_GAP);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // PARITY BITS
|
|
|
|
// // Parity Calculation
|
|
|
|
// if(parity_count & 0x01) {
|
|
|
|
// if((parity_count & 0x01) == 0x01) {
|
|
|
|
// // ODD
|
|
|
|
// // Need to make it EVEN....
|
|
|
|
// // HIGH
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_HIGH_ON_TIME; clock_cycles++);
|
|
|
|
// delay_cycles(PULSE_BIT_TIME);
|
|
|
|
//
|
|
|
|
// // LOW
|
|
|
|
// GPOC = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_HIGH_OFF_TIME; clock_cycles++);
|
|
|
|
// } else {
|
|
|
|
// // EVEN
|
|
|
|
// // Nothing needs to be set
|
|
|
|
// // HIGH
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_ON_TIME; clock_cycles++);
|
|
|
|
//
|
|
|
|
// // LOW
|
|
|
|
// GPOC = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_OFF_TIME; clock_cycles++);
|
|
|
|
// delay_cycles(PULSE_BIT_TIME);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // STOP BITS
|
|
|
|
// // HIGH
|
|
|
|
// // GAP
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_ON_TIME; clock_cycles++);
|
|
|
|
// delay_cycles(PULSE_GAP);
|
|
|
|
//
|
|
|
|
// // STOP BITS
|
|
|
|
// // LOW
|
|
|
|
// GPOC = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_OFF_TIME; clock_cycles++);
|
|
|
|
// delay_cycles(PULSE_BIT_TIME);
|
|
|
|
//
|
|
|
|
// // HIGH
|
|
|
|
// // GAP
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_ON_TIME; clock_cycles++);
|
|
|
|
// delay_cycles(PULSE_GAP);
|
|
|
|
//
|
|
|
|
// // LOW
|
|
|
|
// GPOC = (1 << SBUS_PORT);
|
|
|
|
// for(clock_cycles = 0; clock_cycles < PULSE_LOW_OFF_TIME; clock_cycles++);
|
|
|
|
// delay_cycles(PULSE_BIT_TIME);
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// // GAP
|
|
|
|
// GPOS = (1 << SBUS_PORT);
|
|
|
|
// delay_cycles(PULSE_GAP);
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Wait for next round
|
|
|
|
sbusTime = millis(); |
|
|
|
} |
|
|
|
// sbusTime = millis();
|
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
// Preparing SBUS packets
|
|
|
|
void SBUS::sbusPreparePacket(uint16_t channels[], bool isSignalLoss, bool isFailsafe){ |
|
|
|
if(millis() - last_count > 2000) { |
|
|
|
counter+= 100; |
|
|
|
|
|
|
|
if(counter > 2000) { |
|
|
|
counter = 900; |
|
|
|
} |
|
|
|
// Delay CPU Cycles
|
|
|
|
void SBUS::delay_cycles(uint16_t cpucycles) { |
|
|
|
uint32_t cpucycle_start = ESP.getCycleCount(); |
|
|
|
while((uint32_t)(ESP.getCycleCount() - cpucycle_start) < cpucycles) {} |
|
|
|
} |
|
|
|
|
|
|
|
last_count = millis(); |
|
|
|
} |
|
|
|
|
|
|
|
// Preparing SBUS packets
|
|
|
|
void SBUS::sbusPreparePacket(uint16_t channels[], bool isSignalLoss, bool isFailsafe){ |
|
|
|
static int output[SBUS_CHANNEL_NUMBER] = {0}; |
|
|
|
|
|
|
|
/*
|
|
|
@ -201,9 +196,6 @@ void SBUS::sbusPreparePacket(uint16_t channels[], bool isSignalLoss, bool isFail |
|
|
|
*/ |
|
|
|
for(uint8_t i = 0; i < SBUS_CHANNEL_NUMBER; i++) { |
|
|
|
output[i] = map(channels[i], RC_CHANNEL_MIN, RC_CHANNEL_MAX, SBUS_MIN_OFFSET, SBUS_MAX_OFFSET); |
|
|
|
// DEBUG oNly
|
|
|
|
// output[i] = 1200;
|
|
|
|
// output[i] = counter;
|
|
|
|
} |
|
|
|
|
|
|
|
uint8_t stateByte = 0x00; |
|
|
|