diff --git a/flight/Modules/Radio/radio.c b/flight/Modules/Radio/radio.c index 7b61dce34..e1fab89f2 100644 --- a/flight/Modules/Radio/radio.c +++ b/flight/Modules/Radio/radio.c @@ -48,6 +48,7 @@ #define STATS_UPDATE_PERIOD_MS 500 #define RADIOSTATS_UPDATE_PERIOD_MS 250 #define MAX_LOST_CONTACT_TIME 4 +#define PACKET_MAX_DELAY 50 #ifndef LINK_LED_ON #define LINK_LED_ON @@ -73,7 +74,6 @@ typedef struct { // The task handles. xTaskHandle radioReceiveTaskHandle; xTaskHandle radioStatusTaskHandle; - xTaskHandle sendPacketTaskHandle; // Queue handles. xQueueHandle radioPacketQueue; @@ -106,7 +106,6 @@ typedef struct { static void radioReceiveTask(void *parameters); static void radioStatusTask(void *parameters); -static void sendPacketTask(void *parameters); static void StatusHandler(PHStatusPacketHandle p, int8_t rssi, int8_t afc); static int32_t transmitPacket(PHPacketHandle packet); static void PPMHandler(uint16_t *channels); @@ -137,11 +136,9 @@ static int32_t RadioStart(void) // Start the tasks. xTaskCreate(radioReceiveTask, (signed char *)"RadioReceive", STACK_SIZE_BYTES, NULL, TASK_PRIORITY, &(data->radioReceiveTaskHandle)); xTaskCreate(radioStatusTask, (signed char *)"RadioStatus", STACK_SIZE_BYTES * 2, NULL, TASK_PRIORITY, &(data->radioStatusTaskHandle)); - xTaskCreate(sendPacketTask, (signed char *)"SendPacket", STACK_SIZE_BYTES, NULL, TASK_PRIORITY, &(data->sendPacketTaskHandle)); // Install the monitors TaskMonitorAdd(TASKINFO_RUNNING_MODEMRX, data->radioReceiveTaskHandle); - TaskMonitorAdd(TASKINFO_RUNNING_MODEMTX, data->sendPacketTaskHandle); TaskMonitorAdd(TASKINFO_RUNNING_MODEMSTAT, data->radioStatusTaskHandle); // Register the watchdog timers. @@ -289,9 +286,6 @@ static int32_t RadioInitialize(void) PipXSettingsPairIDGet(&(data->pairStats[0].pairID)); data->destination_id = data->pairStats[0].pairID ? data->pairStats[0].pairID : 0xffffffff; - // Create the packet queue. - data->radioPacketQueue = xQueueCreate(PACKET_QUEUE_SIZE, sizeof(PHPacketHandle)); - // Register the callbacks with the packet handler PHRegisterStatusHandler(pios_packet_handler, StatusHandler); PHRegisterOutputStream(pios_packet_handler, transmitPacket); @@ -342,27 +336,6 @@ static void radioReceiveTask(void *parameters) } } -/** - * Send packets to the radio. - */ -static void sendPacketTask(void *parameters) -{ - PHPacketHandle p; - - // Loop forever - while (1) { -#ifdef PIOS_INCLUDE_WDG - // Update the watchdog timer. - //PIOS_WDG_UpdateFlag(PIOS_WDG_SENDPACKET); -#endif /* PIOS_INCLUDE_WDG */ - // Wait for a packet on the queue. - if (xQueueReceive(data->radioPacketQueue, &p, MAX_PORT_DELAY) == pdTRUE) { - PIOS_COM_SendBuffer(PIOS_COM_RADIO, (uint8_t*)p, PH_PACKET_SIZE(p)); - PHReleaseTXPacket(pios_packet_handler, p); - } - } -} - /** * Transmit a packet to the radio port. * \param[in] buf Data buffer to send @@ -374,7 +347,7 @@ static int32_t transmitPacket(PHPacketHandle p) { uint16_t len = PH_PACKET_SIZE(p); data->txBytes += len; - if (xQueueSend(data->radioPacketQueue, &p, portMAX_DELAY) != pdTRUE) + if (!PIOS_RFM22B_Send_Packet(pios_rfm22b_id, p, PACKET_MAX_DELAY)) return -1; return len; } diff --git a/flight/PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h b/flight/PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h index d500c86ea..aaf265c58 100755 --- a/flight/PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h +++ b/flight/PiOS/Boards/STM32103CB_PIPXTREME_Rev1.h @@ -71,13 +71,12 @@ TIM4 | RC In 1 | Servo 3 | Servo 2 | Servo 1 //------------------------ #define PIOS_WATCHDOG_TIMEOUT 500 #define PIOS_WDG_REGISTER BKP_DR4 -#define PIOS_WDG_COMUAVTALK 0x0001 -#define PIOS_WDG_RADIORECEIVE 0x0002 -#define PIOS_WDG_SENDPACKET 0x0004 +#define PIOS_WDG_COMGCS 0x0001 +#define PIOS_WDG_COMUAVTALK 0x0002 +#define PIOS_WDG_RADIORECEIVE 0x0004 #define PIOS_WDG_SENDDATA 0x0008 -#define PIOS_WDG_TRANSCOMM 0x0010 -#define PIOS_WDG_PPMINPUT 0x0020 -#define PIOS_WDG_COMGCS 0x0040 +#define PIOS_WDG_TRANSCOMM 0x0008 +#define PIOS_WDG_PPMINPUT 0x0010 //------------------------ // TELEMETRY diff --git a/flight/PiOS/Common/pios_rfm22b.c b/flight/PiOS/Common/pios_rfm22b.c index 7a0e145dd..e48a97318 100644 --- a/flight/PiOS/Common/pios_rfm22b.c +++ b/flight/PiOS/Common/pios_rfm22b.c @@ -76,13 +76,9 @@ // ************************************ -#define TX_TEST_MODE_TIMELIMIT_MS 30000 // TX test modes time limit (in ms) - #define TX_PREAMBLE_NIBBLES 12 // 7 to 511 (number of nibbles) #define RX_PREAMBLE_NIBBLES 6 // 5 to 31 (number of nibbles) -// the size of the rf modules internal transmit buffers. -#define TX_BUFFER_SIZE 256 // the size of the rf modules internal FIFO buffers #define FIFO_SIZE 64 @@ -163,6 +159,7 @@ enum pios_rfm22b_state { enum pios_rfm22b_event { RFM22B_EVENT_INITIALIZE, + RFM22B_EVENT_INITIALIZED, RFM22B_EVENT_INT_RECEIVED, RFM22B_EVENT_TX_MODE, RFM22B_EVENT_RX_MODE, @@ -171,7 +168,9 @@ enum pios_rfm22b_event { RFM22B_EVENT_RX_COMPLETE, RFM22B_EVENT_SEND_PACKET, RFM22B_EVENT_TX_START, + RFM22B_EVENT_TX_STARTED, RFM22B_EVENT_TX_COMPLETE, + RFM22B_EVENT_TIMEOUT, RFM22B_EVENT_ERROR, RFM22B_EVENT_FATAL_ERROR, @@ -227,6 +226,23 @@ struct pios_rfm22b_dev { // The packet queue handle xQueueHandle packetQueue; + + // The current tx packet + PHPacketHandle tx_packet; + // the tx data read index + uint16_t tx_data_rd; + // the tx data write index + uint16_t tx_data_wr; + + // The frequency hopping step size + float frequency_step_size; + // current frequency hop channel + uint8_t frequency_hop_channel; + // the frequency hop step size + uint8_t frequency_hop_step_size_reg; + // afc correction reading (in Hz) + int32_t afc_correction_Hz; + int8_t rx_packet_start_afc_Hz; }; struct pios_rfm22b_transition { @@ -311,7 +327,7 @@ const static struct pios_rfm22b_transition rfm22b_transitions[RFM22B_STATE_NUM_S [RFM22B_STATE_INITIALIZING] = { .entry_fn = rfm22_init, .next_state = { - [RFM22B_EVENT_RX_MODE] = RFM22B_STATE_RX_MODE, + [RFM22B_EVENT_INITIALIZED] = RFM22B_STATE_TX_START, [RFM22B_EVENT_ERROR] = RFM22B_STATE_ERROR, [RFM22B_EVENT_FATAL_ERROR] = RFM22B_STATE_FATAL_ERROR, }, @@ -327,7 +343,9 @@ const static struct pios_rfm22b_transition rfm22b_transitions[RFM22B_STATE_NUM_S .entry_fn = rfm22_setRxMode, .next_state = { [RFM22B_EVENT_INT_RECEIVED] = RFM22B_STATE_WAIT_PREAMBLE, + [RFM22B_EVENT_SEND_PACKET] = RFM22B_STATE_TX_START, [RFM22B_EVENT_TX_START] = RFM22B_STATE_TX_START, + [RFM22B_EVENT_TIMEOUT] = RFM22B_STATE_TX_START, [RFM22B_EVENT_ERROR] = RFM22B_STATE_ERROR, [RFM22B_EVENT_FATAL_ERROR] = RFM22B_STATE_FATAL_ERROR, }, @@ -337,7 +355,9 @@ const static struct pios_rfm22b_transition rfm22b_transitions[RFM22B_STATE_NUM_S .next_state = { [RFM22B_EVENT_INT_RECEIVED] = RFM22B_STATE_WAIT_PREAMBLE, [RFM22B_EVENT_PREAMBLE_DETECTED] = RFM22B_STATE_WAIT_SYNC, + [RFM22B_EVENT_SEND_PACKET] = RFM22B_STATE_TX_START, [RFM22B_EVENT_TX_START] = RFM22B_STATE_TX_START, + [RFM22B_EVENT_TIMEOUT] = RFM22B_STATE_TX_START, [RFM22B_EVENT_ERROR] = RFM22B_STATE_ERROR, [RFM22B_EVENT_FATAL_ERROR] = RFM22B_STATE_FATAL_ERROR, }, @@ -393,34 +413,34 @@ const static struct pios_rfm22b_transition rfm22b_transitions[RFM22B_STATE_NUM_S // xtal 10 ppm, 434MHz #define LOOKUP_SIZE 14 -const uint32_t data_rate[] = { 500, 1000, 2000, 4000, 8000, 9600, 16000, 19200, 24000, 32000, 64000, 128000, 192000, 256000}; -const uint8_t modulation_index[] = { 16, 8, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; -const uint32_t freq_deviation[] = { 4000, 4000, 4000, 4000, 4000, 4800, 8000, 9600, 12000, 16000, 32000, 64000, 96000, 128000}; -const uint32_t rx_bandwidth[] = { 17500, 17500, 17500, 17500, 17500, 19400, 32200, 38600, 51200, 64100, 137900, 269300, 420200, 518800}; -const int8_t est_rx_sens_dBm[] = { -118, -118, -117, -116, -115, -115, -112, -112, -110, -109, -106, -103, -101, -100}; // estimated receiver sensitivity for BER = 1E-3 +static const uint32_t data_rate[] = { 500, 1000, 2000, 4000, 8000, 9600, 16000, 19200, 24000, 32000, 64000, 128000, 192000, 256000}; +static const uint8_t modulation_index[] = { 16, 8, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; +static const uint32_t freq_deviation[] = { 4000, 4000, 4000, 4000, 4000, 4800, 8000, 9600, 12000, 16000, 32000, 64000, 96000, 128000}; +static const uint32_t rx_bandwidth[] = { 17500, 17500, 17500, 17500, 17500, 19400, 32200, 38600, 51200, 64100, 137900, 269300, 420200, 518800}; +static const int8_t est_rx_sens_dBm[] = { -118, -118, -117, -116, -115, -115, -112, -112, -110, -109, -106, -103, -101, -100}; // estimated receiver sensitivity for BER = 1E-3 -const uint8_t reg_1C[] = { 0x37, 0x37, 0x37, 0x37, 0x3A, 0x3B, 0x26, 0x28, 0x2E, 0x16, 0x07, 0x83, 0x8A, 0x8C}; // rfm22_if_filter_bandwidth +static const uint8_t reg_1C[] = { 0x37, 0x37, 0x37, 0x37, 0x3A, 0x3B, 0x26, 0x28, 0x2E, 0x16, 0x07, 0x83, 0x8A, 0x8C}; // rfm22_if_filter_bandwidth -const uint8_t reg_1D[] = { 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}; // rfm22_afc_loop_gearshift_override -const uint8_t reg_1E[] = { 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x02}; // rfm22_afc_timing_control +static const uint8_t reg_1D[] = { 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}; // rfm22_afc_loop_gearshift_override +static const uint8_t reg_1E[] = { 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x02}; // rfm22_afc_timing_control -const uint8_t reg_1F[] = { 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03}; // rfm22_clk_recovery_gearshift_override -const uint8_t reg_20[] = { 0xE8, 0xF4, 0xFA, 0x70, 0x3F, 0x34, 0x3F, 0x34, 0x2A, 0x3F, 0x3F, 0x5E, 0x3F, 0x2F}; // rfm22_clk_recovery_oversampling_ratio -const uint8_t reg_21[] = { 0x60, 0x20, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x01, 0x02, 0x02}; // rfm22_clk_recovery_offset2 -const uint8_t reg_22[] = { 0x20, 0x41, 0x83, 0x06, 0x0C, 0x75, 0x0C, 0x75, 0x12, 0x0C, 0x0C, 0x5D, 0x0C, 0xBB}; // rfm22_clk_recovery_offset1 -const uint8_t reg_23[] = { 0xC5, 0x89, 0x12, 0x25, 0x4A, 0x25, 0x4A, 0x25, 0x6F, 0x4A, 0x4A, 0x86, 0x4A, 0x0D}; // rfm22_clk_recovery_offset0 -const uint8_t reg_24[] = { 0x00, 0x00, 0x00, 0x02, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x07, 0x07}; // rfm22_clk_recovery_timing_loop_gain1 -const uint8_t reg_25[] = { 0x0A, 0x23, 0x85, 0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x74, 0xFF, 0xFF}; // rfm22_clk_recovery_timing_loop_gain0 +static const uint8_t reg_1F[] = { 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03}; // rfm22_clk_recovery_gearshift_override +static const uint8_t reg_20[] = { 0xE8, 0xF4, 0xFA, 0x70, 0x3F, 0x34, 0x3F, 0x34, 0x2A, 0x3F, 0x3F, 0x5E, 0x3F, 0x2F}; // rfm22_clk_recovery_oversampling_ratio +static const uint8_t reg_21[] = { 0x60, 0x20, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x01, 0x02, 0x02}; // rfm22_clk_recovery_offset2 +static const uint8_t reg_22[] = { 0x20, 0x41, 0x83, 0x06, 0x0C, 0x75, 0x0C, 0x75, 0x12, 0x0C, 0x0C, 0x5D, 0x0C, 0xBB}; // rfm22_clk_recovery_offset1 +static const uint8_t reg_23[] = { 0xC5, 0x89, 0x12, 0x25, 0x4A, 0x25, 0x4A, 0x25, 0x6F, 0x4A, 0x4A, 0x86, 0x4A, 0x0D}; // rfm22_clk_recovery_offset0 +static const uint8_t reg_24[] = { 0x00, 0x00, 0x00, 0x02, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x07, 0x07}; // rfm22_clk_recovery_timing_loop_gain1 +static const uint8_t reg_25[] = { 0x0A, 0x23, 0x85, 0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x74, 0xFF, 0xFF}; // rfm22_clk_recovery_timing_loop_gain0 -const uint8_t reg_2A[] = { 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0D, 0x0D, 0x0E, 0x12, 0x17, 0x31, 0x50, 0x50, 0x50}; // rfm22_afc_limiter .. AFC_pull_in_range = ±AFCLimiter[7:0] x (hbsel+1) x 625 Hz +static const uint8_t reg_2A[] = { 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0D, 0x0D, 0x0E, 0x12, 0x17, 0x31, 0x50, 0x50, 0x50}; // rfm22_afc_limiter .. AFC_pull_in_range = ±AFCLimiter[7:0] x (hbsel+1) x 625 Hz -const uint8_t reg_6E[] = { 0x04, 0x08, 0x10, 0x20, 0x41, 0x4E, 0x83, 0x9D, 0xC4, 0x08, 0x10, 0x20, 0x31, 0x41}; // rfm22_tx_data_rate1 -const uint8_t reg_6F[] = { 0x19, 0x31, 0x62, 0xC5, 0x89, 0xA5, 0x12, 0x49, 0x9C, 0x31, 0x62, 0xC5, 0x27, 0x89}; // rfm22_tx_data_rate0 +static const uint8_t reg_6E[] = { 0x04, 0x08, 0x10, 0x20, 0x41, 0x4E, 0x83, 0x9D, 0xC4, 0x08, 0x10, 0x20, 0x31, 0x41}; // rfm22_tx_data_rate1 +static const uint8_t reg_6F[] = { 0x19, 0x31, 0x62, 0xC5, 0x89, 0xA5, 0x12, 0x49, 0x9C, 0x31, 0x62, 0xC5, 0x27, 0x89}; // rfm22_tx_data_rate0 -const uint8_t reg_70[] = { 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D}; // rfm22_modulation_mode_control1 -const uint8_t reg_71[] = { 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23}; // rfm22_modulation_mode_control2 +static const uint8_t reg_70[] = { 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D}; // rfm22_modulation_mode_control1 +static const uint8_t reg_71[] = { 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23}; // rfm22_modulation_mode_control2 -const uint8_t reg_72[] = { 0x06, 0x06, 0x06, 0x06, 0x06, 0x08, 0x0D, 0x0F, 0x13, 0x1A, 0x33, 0x66, 0x9A, 0xCD}; // rfm22_frequency_deviation +static const uint8_t reg_72[] = { 0x06, 0x06, 0x06, 0x06, 0x06, 0x08, 0x0D, 0x0F, 0x13, 0x1A, 0x33, 0x66, 0x9A, 0xCD}; // rfm22_frequency_deviation // ************************************ // Scan Spectrum settings @@ -433,87 +453,29 @@ const uint8_t reg_72[] = { 0x06, 0x06, 0x06, 0x06, 0x06, 0x #define SS_LOOKUP_SIZE 2 // xtal 1 ppm, 434MHz -const uint32_t ss_rx_bandwidth[] = { 2600, 10600}; +static const uint32_t ss_rx_bandwidth[] = { 2600, 10600}; -const uint8_t ss_reg_1C[] = { 0x51, 0x32}; // rfm22_if_filter_bandwidth -const uint8_t ss_reg_1D[] = { 0x00, 0x00}; // rfm22_afc_loop_gearshift_override +static const uint8_t ss_reg_1C[] = { 0x51, 0x32}; // rfm22_if_filter_bandwidth +static const uint8_t ss_reg_1D[] = { 0x00, 0x00}; // rfm22_afc_loop_gearshift_override -const uint8_t ss_reg_20[] = { 0xE8, 0x38}; // rfm22_clk_recovery_oversampling_ratio -const uint8_t ss_reg_21[] = { 0x60, 0x02}; // rfm22_clk_recovery_offset2 -const uint8_t ss_reg_22[] = { 0x20, 0x4D}; // rfm22_clk_recovery_offset1 -const uint8_t ss_reg_23[] = { 0xC5, 0xD3}; // rfm22_clk_recovery_offset0 -const uint8_t ss_reg_24[] = { 0x00, 0x07}; // rfm22_clk_recovery_timing_loop_gain1 -const uint8_t ss_reg_25[] = { 0x0F, 0xFF}; // rfm22_clk_recovery_timing_loop_gain0 +static const uint8_t ss_reg_20[] = { 0xE8, 0x38}; // rfm22_clk_recovery_oversampling_ratio +static const uint8_t ss_reg_21[] = { 0x60, 0x02}; // rfm22_clk_recovery_offset2 +static const uint8_t ss_reg_22[] = { 0x20, 0x4D}; // rfm22_clk_recovery_offset1 +static const uint8_t ss_reg_23[] = { 0xC5, 0xD3}; // rfm22_clk_recovery_offset0 +static const uint8_t ss_reg_24[] = { 0x00, 0x07}; // rfm22_clk_recovery_timing_loop_gain1 +static const uint8_t ss_reg_25[] = { 0x0F, 0xFF}; // rfm22_clk_recovery_timing_loop_gain0 -const uint8_t ss_reg_2A[] = { 0xFF, 0xFF}; // rfm22_afc_limiter .. AFC_pull_in_range = ±AFCLimiter[7:0] x (hbsel+1) x 625 Hz +static const uint8_t ss_reg_2A[] = { 0xFF, 0xFF}; // rfm22_afc_limiter .. AFC_pull_in_range = ±AFCLimiter[7:0] x (hbsel+1) x 625 Hz -const uint8_t ss_reg_70[] = { 0x24, 0x2D}; // rfm22_modulation_mode_control1 -const uint8_t ss_reg_71[] = { 0x2B, 0x23}; // rfm22_modulation_mode_control2 - -// ************************************ - -volatile bool initialized = false; - -#if defined(RFM22_EXT_INT_USE) -volatile bool exec_using_spi; // set this if you want to access the SPI bus outside of the interrupt -#endif - -uint8_t device_type; // the RF chips device ID number -uint8_t device_version; // the RF chips revision number - -uint32_t lower_carrier_frequency_limit_Hz; // the minimum RF frequency we can use -uint32_t upper_carrier_frequency_limit_Hz; // the maximum RF frequency we can use -uint32_t carrier_frequency_hz; // the current RF frequency we are on - -uint32_t carrier_datarate_bps; // the RF data rate we are using - -uint32_t rf_bandwidth_used; // the RF bandwidth currently used -uint32_t ss_rf_bandwidth_used; // the RF bandwidth currently used - -uint8_t hbsel; // holds the hbsel (1 or 2) -float frequency_step_size; // holds the minimum frequency step size - -uint8_t frequency_hop_channel; // current frequency hop channel - -uint8_t frequency_hop_step_size_reg; // - -uint8_t adc_config; // holds the adc config reg value - -volatile int16_t afc_correction; // afc correction reading -volatile int32_t afc_correction_Hz; // afc correction reading (in Hz) - -volatile int16_t temperature_reg; // the temperature sensor reading - -volatile uint8_t osc_load_cap; // xtal frequency calibration value - -// The transmit buffer. Holds data that is being transmitted. -uint8_t tx_buffer[TX_BUFFER_SIZE] __attribute__ ((aligned(4))); -// The transmit buffer. Hosts data that is wating to be transmitted. -uint8_t tx_pre_buffer[TX_BUFFER_SIZE] __attribute__ ((aligned(4))); -// The tx pre-buffer write index. -uint16_t tx_pre_buffer_size; -// the tx data read index -volatile uint16_t tx_data_rd; -// the tx data write index -volatile uint16_t tx_data_wr; +static const uint8_t ss_reg_70[] = { 0x24, 0x2D}; // rfm22_modulation_mode_control1 +static const uint8_t ss_reg_71[] = { 0x2B, 0x23}; // rfm22_modulation_mode_control2 // the current receive buffer in use (double buffer) volatile uint8_t rx_buffer_current; // the receive buffer .. received packet data is saved here volatile uint8_t rx_buffer[258] __attribute__ ((aligned(4))); // the receive buffer write index -volatile uint16_t rx_buffer_wr; - -// the received packet -volatile int8_t rx_packet_start_afc_Hz; // -volatile int8_t rx_packet_afc_Hz; // the receive packet frequency offset - -int lookup_index; -int ss_lookup_index; - -uint16_t timeout_ms = 20000; // -uint16_t timeout_sync_ms = 3; // -uint16_t timeout_data_ms = 20; // +volatile uint16_t rx_buffer_wr; static bool PIOS_RFM22B_validate(struct pios_rfm22b_dev * rfm22b_dev) @@ -596,9 +558,6 @@ int32_t PIOS_RFM22B_Init(uint32_t *rfm22b_id, uint32_t spi_id, uint32_t slave_nu // Create a semaphore to know if an ISR needs responding to vSemaphoreCreateBinary( rfm22b_dev->isrPending ); - // Initialize the TX pre-buffer pointer. - tx_pre_buffer_size = 0; - // Create the packet queue. rfm22b_dev->packetQueue = xQueueCreate(PACKET_QUEUE_SIZE, sizeof(PHPacketHandle)); @@ -804,6 +763,13 @@ static void PIOS_RFM22B_Task(void *parameters) // Clear the event queue. while (xQueueReceive(rfm22b_dev->eventQueue, &event, 0) == pdTRUE) ; + lastEventTime = xTaskGetTickCount(); + } + else + { + enum pios_rfm22b_event event = RFM22B_EVENT_TIMEOUT; + while(event != RFM22B_EVENT_NUM_EVENTS) + event = rfm22_process_state_transition(rfm22b_dev, event); } } } @@ -815,6 +781,7 @@ static void PIOS_RFM22B_TxStart(uint32_t rfm22b_id, uint16_t tx_bytes_avail) bool valid = PIOS_RFM22B_validate(rfm22b_dev); PIOS_Assert(valid); +#ifdef NEVER // Get some data to send bool need_yield = false; if(tx_pre_buffer_size == 0) @@ -823,6 +790,7 @@ static void PIOS_RFM22B_TxStart(uint32_t rfm22b_id, uint16_t tx_bytes_avail) // Inject a send packet event PIOS_RFM22B_InjectEvent(g_rfm22b_dev, RFM22B_EVENT_TX_START, false); +#endif } /** @@ -1006,27 +974,17 @@ static enum pios_rfm22b_event rfm22_process_state_transition(struct pios_rfm22b_ // ************************************ -uint32_t rfm22_minFrequency(void) +static void rfm22_setNominalCarrierFrequency(struct pios_rfm22b_dev *rfm22b_dev, uint32_t frequency_hz) { - return lower_carrier_frequency_limit_Hz; -} -uint32_t rfm22_maxFrequency(void) -{ - return upper_carrier_frequency_limit_Hz; -} - -void rfm22_setNominalCarrierFrequency(uint32_t frequency_hz) -{ - - exec_using_spi = true; - - // ******* - - if (frequency_hz < lower_carrier_frequency_limit_Hz) - frequency_hz = lower_carrier_frequency_limit_Hz; - else if (frequency_hz > upper_carrier_frequency_limit_Hz) - frequency_hz = upper_carrier_frequency_limit_Hz; + uint32_t min_frequency_hz = rfm22b_dev->cfg.minFrequencyHz; + uint32_t max_frequency_hz = rfm22b_dev->cfg.maxFrequencyHz; + if (frequency_hz < min_frequency_hz) + frequency_hz = min_frequency_hz; + else if (frequency_hz > max_frequency_hz) + frequency_hz = max_frequency_hz; + // holds the hbsel (1 or 2) + uint8_t hbsel; if (frequency_hz < 480000000) hbsel = 1; else @@ -1038,59 +996,37 @@ void rfm22_setNominalCarrierFrequency(uint32_t frequency_hz) fc = (fc * 64u) / (10000ul * hbsel); fb -= 24; - // carrier_frequency_hz = frequency_hz; - carrier_frequency_hz = ((uint32_t)fb + 24 + ((float)fc / 64000)) * 10000000 * hbsel; - if (hbsel > 1) fb |= RFM22_fbs_hbsel; fb |= RFM22_fbs_sbse; // is this the RX LO polarity? - frequency_step_size = 156.25f * hbsel; + // frequency hopping channel (0-255) + rfm22b_dev->frequency_step_size = 156.25f * hbsel; - rfm22_write(RFM22_frequency_hopping_channel_select, frequency_hop_channel); // frequency hopping channel (0-255) + // frequency hopping channel (0-255) + rfm22_write(RFM22_frequency_hopping_channel_select, rfm22b_dev->frequency_hop_channel); - rfm22_write(RFM22_frequency_offset1, 0); // no frequency offset - rfm22_write(RFM22_frequency_offset2, 0); // no frequency offset + // no frequency offset + rfm22_write(RFM22_frequency_offset1, 0); + // no frequency offset + rfm22_write(RFM22_frequency_offset2, 0); - rfm22_write(RFM22_frequency_band_select, fb); // set the carrier frequency - rfm22_write(RFM22_nominal_carrier_frequency1, fc >> 8); // " " - rfm22_write(RFM22_nominal_carrier_frequency0, fc & 0xff); // " " - - // ******* - -#if defined(RFM22_DEBUG) - //DEBUG_PRINTF(2, "rf setFreq: %u\n\r", carrier_frequency_hz); - // DEBUG_PRINTF(2, "rf setFreq frequency_step_size: %0.2f\n\r", frequency_step_size); -#endif - - exec_using_spi = false; -} - -uint32_t rfm22_getNominalCarrierFrequency(void) -{ - return carrier_frequency_hz; -} - -float rfm22_getFrequencyStepSize(void) -{ - return frequency_step_size; + // set the carrier frequency + rfm22_write(RFM22_frequency_band_select, fb); + rfm22_write(RFM22_nominal_carrier_frequency1, fc >> 8); + rfm22_write(RFM22_nominal_carrier_frequency0, fc & 0xff); } void rfm22_setFreqHopChannel(uint8_t channel) { // set the frequency hopping channel - frequency_hop_channel = channel; - rfm22_write(RFM22_frequency_hopping_channel_select, frequency_hop_channel); -} - -uint8_t rfm22_freqHopChannel(void) -{ // return the current frequency hopping channel - return frequency_hop_channel; + g_rfm22b_dev->frequency_hop_channel = channel; + rfm22_write(RFM22_frequency_hopping_channel_select, channel); } uint32_t rfm22_freqHopSize(void) { // return the frequency hopping step size - return ((uint32_t)frequency_hop_step_size_reg * 10000); + return ((uint32_t)g_rfm22b_dev->frequency_hop_step_size_reg * 10000); } // ************************************ @@ -1109,15 +1045,12 @@ uint32_t rfm22_freqHopSize(void) void rfm22_setDatarate(uint32_t datarate_bps, bool data_whitening) { - exec_using_spi = true; - - lookup_index = 0; + // Find the closest data rate that is >= the value passed in + int lookup_index = 0; while (lookup_index < (LOOKUP_SIZE - 1) && data_rate[lookup_index] < datarate_bps) lookup_index++; - carrier_datarate_bps = datarate_bps = data_rate[lookup_index]; - - rf_bandwidth_used = rx_bandwidth[lookup_index]; + datarate_bps = data_rate[lookup_index]; // rfm22_if_filter_bandwidth rfm22_write(0x1C, reg_1C[lookup_index]); @@ -1145,7 +1078,7 @@ void rfm22_setDatarate(uint32_t datarate_bps, bool data_whitening) // rfm22_afc_limiter rfm22_write(0x2A, reg_2A[lookup_index]); - if (carrier_datarate_bps < 100000) + if (datarate_bps < 100000) // rfm22_chargepump_current_trimming_override rfm22_write(0x58, 0x80); else @@ -1176,84 +1109,12 @@ void rfm22_setDatarate(uint32_t datarate_bps, bool data_whitening) rfm22_write(RFM22_ook_counter_value1, 0x00); rfm22_write(RFM22_ook_counter_value2, 0x00); - - // ******************************** - // calculate the TX register values - /* - uint16_t fd = frequency_deviation / 625; - - uint8_t mmc1 = RFM22_mmc1_enphpwdn | RFM22_mmc1_manppol; - uint16_t txdr; - if (datarate_bps < 30000) - { - txdr = (datarate_bps * 20972) / 10000; - mmc1 |= RFM22_mmc1_txdtrtscale; - } - else - txdr = (datarate_bps * 6553) / 100000; - - uint8_t mmc2 = RFM22_mmc2_dtmod_fifo | RFM22_mmc2_modtyp_gfsk; // FIFO mode, GFSK - // uint8_t mmc2 = RFM22_mmc2_dtmod_pn9 | RFM22_mmc2_modtyp_gfsk; // PN9 mode, GFSK .. TX TEST MODE - if (fd & 0x100) mmc2 |= RFM22_mmc2_fd; - - rfm22_write(RFM22_frequency_deviation, fd); // set the TX peak frequency deviation - - rfm22_write(RFM22_modulation_mode_control1, mmc1); - rfm22_write(RFM22_modulation_mode_control2, mmc2); - - rfm22_write(RFM22_tx_data_rate1, txdr >> 8); // set the TX data rate - rfm22_write(RFM22_tx_data_rate0, txdr); // " " - */ - // ******************************** - // determine a clear channel time - - // initialise the stopwatch with a suitable resolution for the datarate - //STOPWATCH_init(4000000ul / carrier_datarate_bps); // set resolution to the time for 1 nibble (4-bits) at rf datarate - - // ******************************** - // determine suitable time-out periods - - // milliseconds - timeout_sync_ms = (8000ul * 16) / carrier_datarate_bps; - if (timeout_sync_ms < 3) - // because out timer resolution is only 1ms - timeout_sync_ms = 3; - - // milliseconds - timeout_data_ms = (8000ul * 100) / carrier_datarate_bps; - if (timeout_data_ms < 3) - // because out timer resolution is only 1ms - timeout_data_ms = 3; - - // ******************************** - -#if defined(RFM22_DEBUG) -/* - DEBUG_PRINTF(2, "rf datarate_bps: %d\n\r", datarate_bps); - DEBUG_PRINTF(2, "rf frequency_deviation: %d\n\r", frequency_deviation); - uint32_t frequency_deviation = freq_deviation[lookup_index]; // Hz - uint32_t modulation_bandwidth = datarate_bps + (2 * frequency_deviation); - DEBUG_PRINTF(2, "rf modulation_bandwidth: %u\n\r", modulation_bandwidth); - DEBUG_PRINTF(2, "rf_rx_bandwidth[%u]: %u\n\r", lookup_index, rx_bandwidth[lookup_index]); - DEBUG_PRINTF(2, "rf est rx sensitivity[%u]: %ddBm\n\r", lookup_index, est_rx_sens_dBm[lookup_index]); -*/ -#endif - - // ******* - - exec_using_spi = false; -} - -uint32_t rfm22_getDatarate(void) -{ - return carrier_datarate_bps; } // ************************************ static enum pios_rfm22b_event rfm22_setRxMode(struct pios_rfm22b_dev *rfm22b_dev) { - exec_using_spi = true; // disable interrupts rfm22_write(RFM22_interrupt_enable1, 0x00); @@ -1269,7 +1130,7 @@ static enum pios_rfm22b_event rfm22_setRxMode(struct pios_rfm22b_dev *rfm22b_dev rx_buffer_wr = 0; // Clear the TX buffer. - tx_data_rd = tx_data_wr = 0; + rfm22b_dev->tx_data_rd = rfm22b_dev->tx_data_wr = 0; // clear FIFOs rfm22_write(RFM22_op_and_func_ctrl2, RFM22_opfc2_ffclrrx | RFM22_opfc2_ffclrtx); @@ -1284,8 +1145,6 @@ static enum pios_rfm22b_event rfm22_setRxMode(struct pios_rfm22b_dev *rfm22b_dev // enable the receiver rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_pllon | RFM22_opfc1_rxon); - exec_using_spi = false; - // No event generated return RFM22B_EVENT_NUM_EVENTS; } @@ -1294,14 +1153,15 @@ static enum pios_rfm22b_event rfm22_setRxMode(struct pios_rfm22b_dev *rfm22b_dev static enum pios_rfm22b_event rfm22_txStart(struct pios_rfm22b_dev *rfm22b_dev) { - if((tx_pre_buffer_size == 0) || (exec_using_spi == true)) + // See if there's a packet on the packet queue. + PHPacketHandle p; + if (xQueueReceive(rfm22b_dev->packetQueue, &p, 0) != pdTRUE) { // Clear the TX buffer. - tx_data_rd = tx_data_wr = 0; + rfm22b_dev->tx_data_rd = rfm22b_dev->tx_data_wr = 0; return RFM22B_EVENT_RX_MODE; } - - exec_using_spi = true; + rfm22b_dev->tx_packet = p; // disable interrupts rfm22_write(RFM22_interrupt_enable1, 0x00); @@ -1311,15 +1171,13 @@ static enum pios_rfm22b_event rfm22_txStart(struct pios_rfm22b_dev *rfm22b_dev) rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_pllon); // Queue the data up for sending - memcpy(tx_buffer, tx_pre_buffer, tx_pre_buffer_size); - tx_data_rd = 0; - tx_data_wr = tx_pre_buffer_size; - tx_pre_buffer_size = 0; + rfm22b_dev->tx_data_wr = PH_PACKET_SIZE(rfm22b_dev->tx_packet); RX_LED_OFF; // Set the destination address in the transmit header. // The destination address is the first 4 bytes of the message. + uint8_t *tx_buffer = (uint8_t*)(rfm22b_dev->tx_packet); rfm22_write(RFM22_transmit_header0, tx_buffer[0]); rfm22_write(RFM22_transmit_header1, tx_buffer[1]); rfm22_write(RFM22_transmit_header2, tx_buffer[2]); @@ -1342,16 +1200,16 @@ static enum pios_rfm22b_event rfm22_txStart(struct pios_rfm22b_dev *rfm22b_dev) // add some data to the chips TX FIFO before enabling the transmitter // set the total number of data bytes we are going to transmit - rfm22_write(RFM22_transmit_packet_length, tx_data_wr); + rfm22_write(RFM22_transmit_packet_length, rfm22b_dev->tx_data_wr); // add some data rfm22_claimBus(); rfm22_assertCs(); PIOS_SPI_TransferByte(g_rfm22b_dev->spi_id, RFM22_fifo_access | 0x80); - int bytes_to_write = (tx_data_wr - tx_data_rd); + int bytes_to_write = (rfm22b_dev->tx_data_wr - rfm22b_dev->tx_data_rd); bytes_to_write = (bytes_to_write > FIFO_SIZE) ? FIFO_SIZE: bytes_to_write; - PIOS_SPI_TransferBlock(g_rfm22b_dev->spi_id, &tx_buffer[tx_data_rd], NULL, bytes_to_write, NULL); - tx_data_rd += bytes_to_write; + PIOS_SPI_TransferBlock(g_rfm22b_dev->spi_id, &tx_buffer[rfm22b_dev->tx_data_rd], NULL, bytes_to_write, NULL); + rfm22b_dev->tx_data_rd += bytes_to_write; rfm22_deassertCs(); rfm22_releaseBus(); @@ -1363,8 +1221,7 @@ static enum pios_rfm22b_event rfm22_txStart(struct pios_rfm22b_dev *rfm22b_dev) TX_LED_ON; - exec_using_spi = false; - return RFM22B_EVENT_NUM_EVENTS; + return RFM22B_EVENT_TX_STARTED; } // ************************************ @@ -1375,7 +1232,6 @@ static enum pios_rfm22b_event rfm22_txStart(struct pios_rfm22b_dev *rfm22b_dev) */ static bool rfm22_readStatus(struct pios_rfm22b_dev *rfm22b_dev) { - exec_using_spi = true; // 1. Read the interrupt statuses with burst read rfm22_claimBus(); // Set RC and the semaphore @@ -1398,12 +1254,8 @@ static bool rfm22_readStatus(struct pios_rfm22b_dev *rfm22b_dev) // the RF module has gone and done a reset - we need to re-initialize the rf module if (rfm22b_dev->int_status2 & RFM22_is2_ipor) - { - exec_using_spi = false; return false; - } - exec_using_spi = false; return true; } @@ -1438,12 +1290,12 @@ static enum pios_rfm22b_event rfm22_detectSync(struct pios_rfm22b_dev *rfm22b_de // read the 10-bit signed afc correction value // bits 9 to 2 - afc_correction = (uint16_t)rfm22_read(RFM22_afc_correction_read) << 8; + uint16_t afc_correction = (uint16_t)rfm22_read(RFM22_afc_correction_read) << 8; // bits 1 & 0 afc_correction |= (uint16_t)rfm22_read(RFM22_ook_counter_value1) & 0x00c0; afc_correction >>= 6; // convert the afc value to Hz - afc_correction_Hz = (int32_t)(frequency_step_size * afc_correction + 0.5f); + rfm22b_dev->afc_correction_Hz = (int32_t)(rfm22b_dev->frequency_step_size * afc_correction + 0.5f); // read rx signal strength .. 45 = -100dBm, 205 = -20dBm rfm22b_dev->rssi = rfm22_read(RFM22_rssi); @@ -1451,7 +1303,7 @@ static enum pios_rfm22b_event rfm22_detectSync(struct pios_rfm22b_dev *rfm22b_de rfm22b_dev->rssi_dBm = (int8_t)(rfm22b_dev->rssi >> 1) - 122; // remember the afc value for this packet - rx_packet_start_afc_Hz = afc_correction_Hz; + rfm22b_dev->rx_packet_start_afc_Hz = rfm22b_dev->afc_correction_Hz; return RFM22B_EVENT_SYNC_DETECTED; } @@ -1470,8 +1322,6 @@ static enum pios_rfm22b_event rfm22_rxData(struct pios_rfm22b_dev *rfm22b_dev) if (rfm22b_dev->device_status & (RFM22_ds_ffunfl | RFM22_ds_ffovfl)) return RFM22B_EVENT_ERROR; - exec_using_spi = true; - // RX FIFO almost full, it needs emptying if (rfm22b_dev->int_status1 & RFM22_is1_irxffafull) { @@ -1525,10 +1375,7 @@ static enum pios_rfm22b_event rfm22_rxData(struct pios_rfm22b_dev *rfm22b_dev) } if (rx_buffer_wr != len) - { - exec_using_spi = false; return RFM22B_EVENT_ERROR; - } // we have a valid received packet @@ -1536,7 +1383,7 @@ static enum pios_rfm22b_event rfm22_rxData(struct pios_rfm22b_dev *rfm22b_dev) { // Add the rssi and afc to the end of the packet. rx_buffer[rx_buffer_wr++] = rfm22b_dev->rssi_dBm; - rx_buffer[rx_buffer_wr++] = rx_packet_start_afc_Hz; + rx_buffer[rx_buffer_wr++] = rfm22b_dev->rx_packet_start_afc_Hz; // Pass this packet on bool need_yield = false; if (rfm22b_dev->rx_in_cb) @@ -1546,11 +1393,9 @@ static enum pios_rfm22b_event rfm22_rxData(struct pios_rfm22b_dev *rfm22b_dev) } // Start a new transaction - exec_using_spi = false; return RFM22B_EVENT_RX_COMPLETE; } - exec_using_spi = false; return RFM22B_EVENT_NUM_EVENTS; } @@ -1559,12 +1404,21 @@ static enum pios_rfm22b_event rfm22_txData(struct pios_rfm22b_dev *rfm22b_dev) // Read the device status registers if (!rfm22_readStatus(rfm22b_dev)) + { + // Free the tx packet + PHReleaseTXPacket(pios_packet_handler, rfm22b_dev->tx_packet); + rfm22b_dev->tx_packet = 0; + rfm22b_dev->tx_data_wr = rfm22b_dev->tx_data_rd = 0; return RFM22B_EVENT_ERROR; + } // FIFO under/over flow error. Back to RX mode. if (rfm22b_dev->device_status & (RFM22_ds_ffunfl | RFM22_ds_ffovfl)) { - exec_using_spi = false; + // Free the tx packet + PHReleaseTXPacket(pios_packet_handler, rfm22b_dev->tx_packet); + rfm22b_dev->tx_packet = 0; + rfm22b_dev->tx_data_wr = rfm22b_dev->tx_data_rd = 0; return RFM22B_EVENT_ERROR; } @@ -1572,14 +1426,15 @@ static enum pios_rfm22b_event rfm22_txData(struct pios_rfm22b_dev *rfm22b_dev) if (rfm22b_dev->int_status1 & RFM22_is1_ixtffaem) { // top-up the rf chips TX FIFO buffer + uint8_t *tx_buffer = (uint8_t*)(rfm22b_dev->tx_packet); uint16_t max_bytes = FIFO_SIZE - TX_FIFO_LO_WATERMARK - 1; rfm22_claimBus(); rfm22_assertCs(); PIOS_SPI_TransferByte(g_rfm22b_dev->spi_id, RFM22_fifo_access | 0x80); - int bytes_to_write = (tx_data_wr - tx_data_rd); + int bytes_to_write = (rfm22b_dev->tx_data_wr - rfm22b_dev->tx_data_rd); bytes_to_write = (bytes_to_write > max_bytes) ? max_bytes: bytes_to_write; - PIOS_SPI_TransferBlock(g_rfm22b_dev->spi_id, &tx_buffer[tx_data_rd], NULL, bytes_to_write, NULL); - tx_data_rd += bytes_to_write; + PIOS_SPI_TransferBlock(g_rfm22b_dev->spi_id, &tx_buffer[rfm22b_dev->tx_data_rd], NULL, bytes_to_write, NULL); + rfm22b_dev->tx_data_rd += bytes_to_write; rfm22_deassertCs(); rfm22_releaseBus(); } @@ -1587,12 +1442,14 @@ static enum pios_rfm22b_event rfm22_txData(struct pios_rfm22b_dev *rfm22b_dev) // Packet has been sent if (rfm22b_dev->int_status1 & RFM22_is1_ipksent) { - exec_using_spi = false; + // Free the tx packet + PHReleaseTXPacket(pios_packet_handler, rfm22b_dev->tx_packet); + rfm22b_dev->tx_packet = 0; + rfm22b_dev->tx_data_wr = rfm22b_dev->tx_data_rd = 0; // Start a new transaction return RFM22B_EVENT_TX_COMPLETE; } - exec_using_spi = false; return RFM22B_EVENT_NUM_EVENTS; } @@ -1613,10 +1470,6 @@ bool rfm22_transmitting(void) // return true if the channel is clear to transmit on bool rfm22_channelIsClear(void) { - if (!initialized) - // we haven't yet been initialized - return false; - if (g_rfm22b_dev->state != RFM22B_STATE_RX_MODE && g_rfm22b_dev->state != RFM22B_STATE_WAIT_PREAMBLE && g_rfm22b_dev->state != RFM22B_STATE_WAIT_SYNC) // we are receiving something or we are transmitting or we are scanning the spectrum return false; @@ -1624,29 +1477,12 @@ bool rfm22_channelIsClear(void) return true; } -// return true if the transmiter is ready for use -bool rfm22_txReady(void) -{ - return (tx_data_rd == 0 && tx_data_wr == 0 && g_rfm22b_dev->state != RFM22B_STATE_TX_DATA); -} - // ************************************ // set/get the frequency calibration value void rfm22_setFreqCalibration(uint8_t value) { - osc_load_cap = value; - - exec_using_spi = true; - - rfm22_write(RFM22_xtal_osc_load_cap, osc_load_cap); - - exec_using_spi = false; -} - -uint8_t rfm22_getFreqCalibration(void) -{ - return osc_load_cap; + rfm22_write(RFM22_xtal_osc_load_cap, value); } // ************************************ @@ -1659,9 +1495,6 @@ static enum pios_rfm22b_event rfm22_init(struct pios_rfm22b_dev *rfm22b_dev) uint32_t max_frequency_hz = rfm22b_dev->cfg.maxFrequencyHz; uint32_t freq_hop_step_size = 50000; - initialized = false; - exec_using_spi = true; - // software reset the RF chip .. following procedure according to Si4x3x Errata (rev. B) rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_swres); @@ -1693,39 +1526,25 @@ static enum pios_rfm22b_event rfm22_init(struct pios_rfm22b_dev *rfm22b_dev) // **************** - exec_using_spi = false; - - // **************** - rfm22b_dev->device_status = rfm22b_dev->int_status1 = rfm22b_dev->int_status2 = rfm22b_dev->ezmac_status = 0; rx_buffer_current = 0; rx_buffer_wr = 0; - rx_packet_afc_Hz = 0; - tx_data_rd = tx_data_wr = 0; + rfm22b_dev->tx_data_rd = rfm22b_dev->tx_data_wr = 0; + rfm22b_dev->tx_packet = NULL; - lookup_index = 0; - ss_lookup_index = 0; + rfm22b_dev->frequency_hop_channel = 0; - rf_bandwidth_used = 0; - ss_rf_bandwidth_used = 0; - - hbsel = 0; - frequency_step_size = 0.0f; - - frequency_hop_channel = 0; - - afc_correction = 0; - afc_correction_Hz = 0; - - temperature_reg = 0; + rfm22b_dev->afc_correction_Hz = 0; // **************** // read the RF chip ID bytes - device_type = rfm22_read(RFM22_DEVICE_TYPE) & RFM22_DT_MASK; // read the device type - device_version = rfm22_read(RFM22_DEVICE_VERSION) & RFM22_DV_MASK; // read the device version + // read the device type + uint8_t device_type = rfm22_read(RFM22_DEVICE_TYPE) & RFM22_DT_MASK; + // read the device version + uint8_t device_version = rfm22_read(RFM22_DEVICE_VERSION) & RFM22_DV_MASK; #if defined(RFM22_DEBUG) DEBUG_PRINTF(2, "rf device type: %d\n\r", device_type); @@ -1767,27 +1586,24 @@ static enum pios_rfm22b_event rfm22_init(struct pios_rfm22b_dev *rfm22b_dev) max_frequency_hz = tmp; } - lower_carrier_frequency_limit_Hz = min_frequency_hz; - upper_carrier_frequency_limit_Hz = max_frequency_hz; - // **************** // calibrate our RF module to be exactly on frequency .. different for every module - - osc_load_cap = OSC_LOAD_CAP; // default - rfm22_write(RFM22_xtal_osc_load_cap, osc_load_cap); + rfm22_write(RFM22_xtal_osc_load_cap, OSC_LOAD_CAP); // **************** // disable Low Duty Cycle Mode rfm22_write(RFM22_op_and_func_ctrl2, 0x00); - rfm22_write(RFM22_cpu_output_clk, RFM22_coc_1MHz); // 1MHz clock output + // 1MHz clock output + rfm22_write(RFM22_cpu_output_clk, RFM22_coc_1MHz); - rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_xton); // READY mode - // rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_pllon); // TUNE mode + // READY mode + rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_xton); // choose the 3 GPIO pin functions - rfm22_write(RFM22_io_port_config, RFM22_io_port_default); // GPIO port use default value + // GPIO port use default value + rfm22_write(RFM22_io_port_config, RFM22_io_port_default); if (rfm22b_dev->cfg.gpio_direction == GPIO0_TX_GPIO1_RX) { rfm22_write(RFM22_gpio0_config, RFM22_gpio0_config_drv3 | RFM22_gpio0_config_txstate); // GPIO0 = TX State (to control RF Switch) rfm22_write(RFM22_gpio1_config, RFM22_gpio1_config_drv3 | RFM22_gpio1_config_rxstate); // GPIO1 = RX State (to control RF Switch) @@ -1802,7 +1618,7 @@ static enum pios_rfm22b_event rfm22_init(struct pios_rfm22b_dev *rfm22b_dev) // initialize the frequency hopping step size freq_hop_step_size /= 10000; // in 10kHz increments if (freq_hop_step_size > 255) freq_hop_step_size = 255; - frequency_hop_step_size_reg = freq_hop_step_size; + rfm22b_dev->frequency_hop_step_size_reg = freq_hop_step_size; // set the RF datarate rfm22_setDatarate(RFM22_DEFAULT_RF_DATARATE, true); @@ -1814,7 +1630,7 @@ static enum pios_rfm22b_event rfm22_init(struct pios_rfm22b_dev *rfm22b_dev) // setup to read the internal temperature sensor // ADC used to sample the temperature sensor - adc_config = RFM22_ac_adcsel_temp_sensor | RFM22_ac_adcref_bg; + uint8_t adc_config = RFM22_ac_adcsel_temp_sensor | RFM22_ac_adcref_bg; rfm22_write(RFM22_adc_config, adc_config); // adc offset @@ -1875,7 +1691,7 @@ static enum pios_rfm22b_event rfm22_init(struct pios_rfm22b_dev *rfm22b_dev) ((TX_PREAMBLE_NIBBLES >> 8) & 0x01)); #endif - // sync word + // sync word rfm22_write(RFM22_sync_word3, SYNC_BYTE_1); rfm22_write(RFM22_sync_word2, SYNC_BYTE_2); rfm22_write(RFM22_sync_word1, SYNC_BYTE_3); @@ -1884,10 +1700,10 @@ static enum pios_rfm22b_event rfm22_init(struct pios_rfm22b_dev *rfm22b_dev) rfm22_write(RFM22_agc_override1, RFM22_agc_ovr1_agcen); // set frequency hopping channel step size (multiples of 10kHz) - rfm22_write(RFM22_frequency_hopping_step_size, frequency_hop_step_size_reg); + rfm22_write(RFM22_frequency_hopping_step_size, rfm22b_dev->frequency_hop_step_size_reg); // set our nominal carrier frequency - rfm22_setNominalCarrierFrequency((min_frequency_hz + max_frequency_hz) / 2); + rfm22_setNominalCarrierFrequency(rfm22b_dev, (min_frequency_hz + max_frequency_hz) / 2); // set the tx power rfm22_write(RFM22_tx_power, RFM22_tx_pwr_papeaken | RFM22_tx_pwr_papeaklvl_0 | RFM22_tx_pwr_lna_sw | rfm22b_dev->tx_power); @@ -1901,21 +1717,11 @@ static enum pios_rfm22b_event rfm22_init(struct pios_rfm22b_dev *rfm22b_dev) // RX FIFO Almost Full Threshold (0 - 63) rfm22_write(RFM22_rx_fifo_control, RX_FIFO_HI_WATERMARK); - //rfm22_setRxMode(rfm22b_dev); - rfm22_setFreqCalibration(rfm22b_dev->cfg.RFXtalCap); - rfm22_setNominalCarrierFrequency(rfm22b_dev->cfg.frequencyHz); + rfm22_setNominalCarrierFrequency(rfm22b_dev, rfm22b_dev->cfg.frequencyHz); rfm22_setDatarate(rfm22b_dev->cfg.maxRFBandwidth, true); - DEBUG_PRINTF(2, "\n\r"); - DEBUG_PRINTF(2, "RF device ID: %x\n\r", rfm22b_dev->deviceID); - DEBUG_PRINTF(2, "RF datarate: %dbps\n\r", rfm22_getDatarate()); - DEBUG_PRINTF(2, "RF frequency: %dHz\n\r", rfm22_getNominalCarrierFrequency()); - DEBUG_PRINTF(2, "RF TX power: %d\n\r", rfm22b_dev->tx_power); - - initialized = true; - - return RFM22B_EVENT_RX_MODE; + return RFM22B_EVENT_INITIALIZED; } static enum pios_rfm22b_event rfm22_error(struct pios_rfm22b_dev *rfm22b_dev) diff --git a/flight/PiOS/inc/pios_rfm22b_priv.h b/flight/PiOS/inc/pios_rfm22b_priv.h index 604efba63..cea382220 100644 --- a/flight/PiOS/inc/pios_rfm22b_priv.h +++ b/flight/PiOS/inc/pios_rfm22b_priv.h @@ -582,44 +582,6 @@ typedef bool ( *t_rfm22_RxDataCallback ) (void *data, uint8_t len); void PIOS_RFM22_EXT_Int(void); -uint32_t rfm22_minFrequency(void); -uint32_t rfm22_maxFrequency(void); - -void rfm22_setNominalCarrierFrequency(uint32_t frequency_hz); -uint32_t rfm22_getNominalCarrierFrequency(void); - -float rfm22_getFrequencyStepSize(void); - -void rfm22_setFreqHopChannel(uint8_t channel); -uint8_t rfm22_freqHopChannel(void); - -uint32_t rfm22_freqHopSize(void); - -void rfm22_setDatarate(uint32_t datarate_bps, bool data_whitening); -uint32_t rfm22_getDatarate(void); - -void rfm22_setFreqCalibration(uint8_t value); -uint8_t rfm22_getFreqCalibration(void); - -void rfm22_setTxPower(uint8_t tx_pwr); -uint8_t rfm22_getTxPower(void); - -void rfm22_setTxStream(void); // TEST ONLY - -void rfm22_setTxNormal(void); -void rfm22_setTxCarrierMode(void); -void rfm22_setTxPNMode(void); - -int8_t rfm22_currentMode(void); -bool rfm22_transmitting(void); - -bool rfm22_channelIsClear(void); - -bool rfm22_txReady(void); - -void rfm22_TxDataByte_SetCallback(t_rfm22_TxDataByteCallback new_function); -void rfm22_RxData_SetCallback(t_rfm22_RxDataCallback new_function); - #endif /* PIOS_RFM22B_PRIV_H */ /**