Added banded contention window allocation
This commit is contained in:
		@@ -1349,18 +1349,30 @@ void validate_status() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
 | 
			
		||||
  #define _e 2.71828183
 | 
			
		||||
  #define _S 12.5
 | 
			
		||||
  float csma_slope(float u) { return (pow(_e,_S*u-_S/2.0))/(pow(_e,_S*u-_S/2.0)+1.0); }
 | 
			
		||||
  void update_csma_parameters() {
 | 
			
		||||
    // TODO: Implement
 | 
			
		||||
    int airtime_pct = (int)(airtime*100);
 | 
			
		||||
    int new_cw_band = cw_band;
 | 
			
		||||
 | 
			
		||||
    if (airtime_pct <= CSMA_BAND_1_MAX_AIRTIME) { new_cw_band = 1; }
 | 
			
		||||
    else {
 | 
			
		||||
      int at = airtime_pct + CSMA_BAND_1_MAX_AIRTIME;
 | 
			
		||||
      new_cw_band = map(at, CSMA_BAND_1_MAX_AIRTIME, CSMA_BAND_N_MIN_AIRTIME, 2, CSMA_CW_BANDS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (new_cw_band > CSMA_CW_BANDS) { new_cw_band = CSMA_CW_BANDS; }
 | 
			
		||||
    if (new_cw_band != cw_band) { 
 | 
			
		||||
      cw_band = (uint8_t)(new_cw_band);
 | 
			
		||||
      cw_min  = (cw_band-1) * CSMA_CW_PER_BAND_WINDOWS;
 | 
			
		||||
      cw_max  = (cw_band) * CSMA_CW_PER_BAND_WINDOWS - 1;
 | 
			
		||||
      kiss_indicate_csma_stats();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void tx_queue_handler() {
 | 
			
		||||
  if (!airtime_lock && queue_height > 0) {
 | 
			
		||||
    if (csma_cw == -1) {
 | 
			
		||||
      csma_cw = random(CSMA_CW_MIN, CSMA_CW_MAX);
 | 
			
		||||
      csma_cw = random(cw_min, cw_max);
 | 
			
		||||
      cw_wait_target = csma_cw * csma_slot_ms;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1400,8 +1412,6 @@ void tx_queue_handler() {
 | 
			
		||||
 | 
			
		||||
void loop() {
 | 
			
		||||
  if (radio_online) {
 | 
			
		||||
    update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
    #if MCU_VARIANT == MCU_ESP32
 | 
			
		||||
      modem_packet_t *modem_packet = NULL;
 | 
			
		||||
      if(modem_packet_queue && xQueueReceive(modem_packet_queue, &modem_packet, 0) == pdTRUE && modem_packet) {
 | 
			
		||||
@@ -1444,16 +1454,9 @@ void loop() {
 | 
			
		||||
 | 
			
		||||
    #endif
 | 
			
		||||
 | 
			
		||||
    update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
    tx_queue_handler();
 | 
			
		||||
 | 
			
		||||
    update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
    check_modem_status();
 | 
			
		||||
 | 
			
		||||
    update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
      // if (queue_height > 0) {
 | 
			
		||||
      //   #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
 | 
			
		||||
 | 
			
		||||
@@ -1514,41 +1517,27 @@ void loop() {
 | 
			
		||||
 | 
			
		||||
  #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
 | 
			
		||||
      buffer_serial();
 | 
			
		||||
      update_modem_status(); // TODO: Remove debug
 | 
			
		||||
      
 | 
			
		||||
      if (!fifo_isempty(&serialFIFO)) serial_poll();
 | 
			
		||||
      
 | 
			
		||||
      update_modem_status(); // TODO: Remove debug
 | 
			
		||||
  #else
 | 
			
		||||
    if (!fifo_isempty_locked(&serialFIFO)) serial_poll();
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
  #if HAS_DISPLAY
 | 
			
		||||
    if (disp_ready) update_display();
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
  #if HAS_PMU
 | 
			
		||||
    if (pmu_ready) update_pmu();
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
  #if HAS_BLUETOOTH || HAS_BLE == true
 | 
			
		||||
    if (!console_active && bt_ready) update_bt();
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
  #if HAS_INPUT
 | 
			
		||||
    input_read();
 | 
			
		||||
  #endif
 | 
			
		||||
 | 
			
		||||
  update_modem_status(); // TODO: Remove debug
 | 
			
		||||
 | 
			
		||||
  if (memory_low) {
 | 
			
		||||
    #if PLATFORM == PLATFORM_ESP32
 | 
			
		||||
      if (esp_get_free_heap_size() < 8192) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										37
									
								
								Utilities.h
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								Utilities.h
									
									
									
									
									
								
							@@ -859,6 +859,17 @@ void kiss_indicate_channel_stats() {
 | 
			
		||||
	#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kiss_indicate_csma_stats() {
 | 
			
		||||
	#if MCU_VARIANT == MCU_ESP32
 | 
			
		||||
		serial_write(FEND);
 | 
			
		||||
		serial_write(CMD_STAT_CSMA);
 | 
			
		||||
		escaped_serial_write(cw_band);
 | 
			
		||||
		escaped_serial_write(cw_min);
 | 
			
		||||
		escaped_serial_write(cw_max);
 | 
			
		||||
		serial_write(FEND);
 | 
			
		||||
	#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kiss_indicate_phy_stats() {
 | 
			
		||||
	#if MCU_VARIANT == MCU_ESP32
 | 
			
		||||
		uint16_t lst = (uint16_t)(lora_symbol_time_ms*1000);
 | 
			
		||||
@@ -866,18 +877,15 @@ void kiss_indicate_phy_stats() {
 | 
			
		||||
		uint16_t prs = (uint16_t)(lora_preamble_symbols);
 | 
			
		||||
		uint16_t prt = (uint16_t)(lora_preamble_time_ms);
 | 
			
		||||
		uint16_t cst = (uint16_t)(csma_slot_ms);
 | 
			
		||||
		uint16_t dft = (uint16_t)(difs_ms);
 | 
			
		||||
		serial_write(FEND);
 | 
			
		||||
		serial_write(CMD_STAT_PHYPRM);
 | 
			
		||||
		escaped_serial_write(lst>>8);
 | 
			
		||||
		escaped_serial_write(lst);
 | 
			
		||||
		escaped_serial_write(lsr>>8);
 | 
			
		||||
		escaped_serial_write(lsr);
 | 
			
		||||
		escaped_serial_write(prs>>8);
 | 
			
		||||
		escaped_serial_write(prs);
 | 
			
		||||
		escaped_serial_write(prt>>8);
 | 
			
		||||
		escaped_serial_write(prt);
 | 
			
		||||
		escaped_serial_write(cst>>8);
 | 
			
		||||
		escaped_serial_write(cst);
 | 
			
		||||
		escaped_serial_write(lst>>8);	escaped_serial_write(lst);
 | 
			
		||||
		escaped_serial_write(lsr>>8);	escaped_serial_write(lsr);
 | 
			
		||||
		escaped_serial_write(prs>>8);	escaped_serial_write(prs);
 | 
			
		||||
		escaped_serial_write(prt>>8);	escaped_serial_write(prt);
 | 
			
		||||
		escaped_serial_write(cst>>8);	escaped_serial_write(cst);
 | 
			
		||||
		escaped_serial_write(dft>>8); escaped_serial_write(dft);
 | 
			
		||||
		serial_write(FEND);
 | 
			
		||||
	#endif
 | 
			
		||||
}
 | 
			
		||||
@@ -1086,7 +1094,8 @@ void setPreamble() {
 | 
			
		||||
 | 
			
		||||
void updateBitrate() {
 | 
			
		||||
	#if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
 | 
			
		||||
		if (radio_online) {
 | 
			
		||||
		if (!radio_online) { lora_bitrate = 0; }
 | 
			
		||||
		else {
 | 
			
		||||
			lora_symbol_rate = (float)lora_bw/(float)(pow(2, lora_sf));
 | 
			
		||||
			lora_symbol_time_ms = (1.0/lora_symbol_rate)*1000.0;
 | 
			
		||||
			lora_bitrate = (uint32_t)(lora_sf * ( (4.0/(float)lora_cr) / ((float)(pow(2, lora_sf))/((float)lora_bw/1000.0)) ) * 1000.0);
 | 
			
		||||
@@ -1095,17 +1104,15 @@ void updateBitrate() {
 | 
			
		||||
			csma_slot_ms = lora_symbol_time_ms*CSMA_SLOT_SYMBOLS;
 | 
			
		||||
			if (csma_slot_ms > CSMA_SLOT_MAX_MS) { csma_slot_ms = CSMA_SLOT_MAX_MS; }
 | 
			
		||||
			if (csma_slot_ms < CSMA_SLOT_MIN_MS) { csma_slot_ms = CSMA_SLOT_MIN_MS; }
 | 
			
		||||
			difs_ms = CSMA_SIFS_MS + 2*csma_slot_ms;
 | 
			
		||||
			
 | 
			
		||||
			float target_preamble_symbols = LORA_PREAMBLE_TARGET_MS/lora_symbol_time_ms;
 | 
			
		||||
			if (target_preamble_symbols < LORA_PREAMBLE_SYMBOLS_MIN) { target_preamble_symbols = LORA_PREAMBLE_SYMBOLS_MIN; }
 | 
			
		||||
			else { target_preamble_symbols = (ceil)(target_preamble_symbols); }
 | 
			
		||||
			
 | 
			
		||||
			lora_preamble_symbols = (long)target_preamble_symbols;
 | 
			
		||||
			setPreamble();
 | 
			
		||||
			lora_preamble_symbols = (long)target_preamble_symbols; setPreamble();
 | 
			
		||||
			lora_preamble_time_ms = (ceil)(lora_preamble_symbols * lora_symbol_time_ms);
 | 
			
		||||
			lora_header_time_ms   = (ceil)(PHY_HEADER_LORA_SYMBOLS * lora_symbol_time_ms);
 | 
			
		||||
		} else {
 | 
			
		||||
			lora_bitrate = 0;
 | 
			
		||||
		}
 | 
			
		||||
	#endif
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user