Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions firmware/common/max2831.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "delay.h"
#include "fixed_point.h"
#include "max2831_regs.def" // private register def macros
#include "selftest.h"
Expand Down Expand Up @@ -425,3 +426,23 @@ void max2831_set_rx_hpf_frequency(max2831_driver_t* const drv, const max2831_rx_
break;
}
}

int8_t max2831_temperature(max2831_driver_t* const drv)
{
/* Switch to temperature sensor. */
set_MAX2831_RSSI_MUX(drv, MAX2831_RSSI_MUX_TEMP);
max2831_regs_commit(drv);

/* Wait for output to settle. */
delay_us(100);

/* Read temperature. */
uint16_t value = adc_read(1);

/* Convert to degrees C, using:
* ADC 0 = 0V, ADC 1023 = 3.3V
* Analog 0.35V = -40C, 1.6V = +85C */
const int32_t prescale = 3332, offset = 750104, divisor = 10000;

return ((value * prescale) - offset) / divisor;
}
2 changes: 2 additions & 0 deletions firmware/common/max2831.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ extern void max2831_tx(max2831_driver_t* const drv);
extern void max2831_rx(max2831_driver_t* const drv);
extern void max2831_tx_calibration(max2831_driver_t* const drv);
extern void max2831_rx_calibration(max2831_driver_t* const drv);

int8_t max2831_temperature(max2831_driver_t* const drv);
21 changes: 21 additions & 0 deletions firmware/common/max2837.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "delay.h"
#include "fixed_point.h"
#include "max2837_regs.def" // private register def macros
#include "selftest.h"
Expand Down Expand Up @@ -393,3 +394,23 @@ bool max2837_set_txvga_gain(max2837_driver_t* const drv, const uint32_t gain_db)
max2837_reg_commit(drv, 29);
return true;
}

int8_t max2837_temperature(max2837_driver_t* const drv)
{
/* Enable temperature sensing, if not already enabled. */
if (!get_MAX2837_TEMP_ENABLE(drv)) {
set_MAX2837_TEMP_ENABLE(drv, true);
max2837_reg_commit(drv, 9);
delay_ms(1);
}

/* Trigger ADC */
set_MAX2837_TEMP_TRIGGER(drv, true);
max2837_reg_commit(drv, 9);

/* Empirical testing indicates about 25us is necessary to get a valid
* temperature sense conversion from the ADC. */
delay_us(25);

return max2837_read(drv, 7) & 0x1F;
}
2 changes: 2 additions & 0 deletions firmware/common/max2837.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ bool max2837_set_txvga_gain(max2837_driver_t* const drv, const uint32_t gain_db)

extern void max2837_tx(max2837_driver_t* const drv);
extern void max2837_rx(max2837_driver_t* const drv);

int8_t max2837_temperature(max2837_driver_t* const drv);
6 changes: 3 additions & 3 deletions firmware/common/max2837_regs.def
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ __MREG__(MAX2837_RSSI_MODE,6,8,1) // set to override RXHP pin
__MREG__(MAX2837_LPF_MODE_SEL,6,9,1) // set to enable mode in reg 2 ModeCtrl

/* REG 7 is R/O */
// D4:0 ts_adc (temp sensor)
__MREG__(MAX2837_TEMP_VALUE,0,4,5) // temp sensor value
// D9:5 zeros or test outputs

/* REG 8 */
Expand All @@ -162,8 +162,8 @@ __MREG__(MAX2837_BIAS_TRIM_CNTRL,8,8,1) // enable BIAS_TRIM_SPI value
__MREG__(MAX2837_RX_IQERR_SPI_EN,8,9,1) // ???

/* REG 9 */
__MREG__(MAX2837_ts_adc_trigger,9,0,1) // temp sensor trigger (one shot)
__MREG__(MAX2837_ts_en,9,1,1) // temp sensor enable (before trigger)
__MREG__(MAX2837_TEMP_TRIGGER,9,0,1) // temp sensor trigger (one shot)
__MREG__(MAX2837_TEMP_ENABLE,9,1,1) // temp sensor enable (before trigger)
__MREG__(MAX2837_LPFtrim_SPI_EN,9,2,1)
__MREG__(MAX2837_DOUT_DRVH,9,3,1)
#define MAX2837_DOUT_DRVH_1X 0
Expand Down
21 changes: 21 additions & 0 deletions firmware/common/max2839.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "delay.h"
#include "fixed_point.h"
#include "max2839_regs.def" // private register def macros
#include "selftest.h"
Expand Down Expand Up @@ -456,3 +457,23 @@ bool max2839_set_txvga_gain(max2839_driver_t* const drv, const uint32_t gain_db)
max2839_reg_commit(drv, 29);
return true;
}

int8_t max2839_temperature(max2839_driver_t* const drv)
{
/* Enable temperature sensing, if not already enabled. */
if (!get_MAX2839_TEMP_ENABLE(drv)) {
set_MAX2839_TEMP_ENABLE(drv, true);
max2839_reg_commit(drv, 9);
delay_ms(1);
}

/* Trigger ADC */
set_MAX2839_TEMP_TRIGGER(drv, true);
max2839_reg_commit(drv, 9);

/* Empirical testing indicates about 25us is necessary to get a valid
* temperature sense conversion from the ADC. */
delay_us(25);

return max2839_read(drv, 11) & 0x1F;
}
2 changes: 2 additions & 0 deletions firmware/common/max2839.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ bool max2839_set_txvga_gain(max2839_driver_t* const drv, const uint32_t gain_db)

extern void max2839_tx(max2839_driver_t* const drv);
extern void max2839_rx(max2839_driver_t* const drv);

int8_t max2839_temperature(max2839_driver_t* const drv);
4 changes: 2 additions & 2 deletions firmware/common/max2839_regs.def
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ __MREG__(MAX2839_LPFmode_SPI,8,2,1)
__MREG__(MAX2839_RESERVED_8_9,8,9,7)

/* REG 9 */
__MREG__(MAX2839_Temperature_ADC,9,0,1)
__MREG__(MAX2839_Temperature_Clk_En,9,1,1)
__MREG__(MAX2839_TEMP_TRIGGER,9,0,1)
__MREG__(MAX2839_TEMP_ENABLE,9,1,1)
__MREG__(MAX2839_RESERVED_9_2,9,2,1)
__MREG__(MAX2839_DOUT_Drive_Sel,9,3,1)
__MREG__(MAX2839_DOUT_3state_Ctrl,9,4,1)
Expand Down
6 changes: 6 additions & 0 deletions firmware/common/max283x.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,9 @@ void max283x_rx_calibration(max283x_driver_t* const drv)
{
PRALINE_ONLY(drv, max2831_rx_calibration(&drv->drv.max2831));
}

/* Get chip temperature. */
uint8_t max283x_temperature(max283x_driver_t* const drv)
{
return RESULT(drv, uint8_t, temperature);
}
3 changes: 3 additions & 0 deletions firmware/common/max283x.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ void max283x_set_rx_hpf_frequency(
void max283x_tx_calibration(max283x_driver_t* const drv);
void max283x_rx_calibration(max283x_driver_t* const drv);

/* Get chip temperature. */
uint8_t max283x_temperature(max283x_driver_t* const drv);

/* Driver instance. */
extern ssp_config_t ssp_config_max283x;
extern max283x_driver_t max283x;
1 change: 1 addition & 0 deletions firmware/hackrf_usb/hackrf_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ static usb_request_handler_fn vendor_request_handler[] = {
usb_vendor_request_write_radio_reg,
usb_vendor_request_read_radio_reg,
usb_vendor_request_get_buffer_size,
usb_vendor_request_read_temperature,
};

static const uint32_t vendor_request_handler_count =
Expand Down
18 changes: 18 additions & 0 deletions firmware/hackrf_usb/usb_api_selftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <libopencm3/lpc43xx/cgu.h>
#include <libopencm3/lpc43xx/creg.h>

#include <max283x.h>
#include <platform_detect.h>
#include <selftest.h>
#include <usb_queue.h>
Expand Down Expand Up @@ -266,3 +267,20 @@ usb_request_status_t usb_vendor_request_test_rtc_osc(
return USB_REQUEST_STATUS_OK;
}
}

usb_request_status_t usb_vendor_request_read_temperature(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage)
{
if (stage == USB_TRANSFER_STAGE_SETUP) {
endpoint->buffer[0] = max283x_temperature(&max283x);
usb_transfer_schedule_block(
endpoint->in,
&endpoint->buffer,
1,
NULL,
NULL);
usb_transfer_schedule_ack(endpoint->out);
}
return USB_REQUEST_STATUS_OK;
}
4 changes: 4 additions & 0 deletions firmware/hackrf_usb/usb_api_selftest.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ usb_request_status_t usb_vendor_request_read_selftest(
usb_request_status_t usb_vendor_request_test_rtc_osc(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage);

usb_request_status_t usb_vendor_request_read_temperature(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage);
2 changes: 1 addition & 1 deletion firmware/hackrf_usb/usb_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#define USB_VENDOR_ID (0x1D50)

#define USB_API_VERSION (0x0112)
#define USB_API_VERSION (0x0113)

#define USB_WORD(x) (x & 0xFF), ((x >> 8) & 0xFF)

Expand Down
12 changes: 12 additions & 0 deletions host/hackrf-tools/src/hackrf_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ int main(void)
}
#endif /* HACKRF_ISSUE_609_IS_FIXED */

if (usb_version >= 0x0113) {
int8_t temperature;
result = hackrf_read_temperature(device, &temperature);
if (result != HACKRF_SUCCESS) {
printf("hackrf_read_temperature() failed: %s (%d)\n",
hackrf_error_name(result),
result);
return EXIT_FAILURE;
}
printf("Transceiver temperature: %-d°C\n", temperature);
}

if (usb_version >= 0x0109) {
hackrf_selftest selftest;
result = hackrf_read_selftest(device, &selftest);
Expand Down
23 changes: 23 additions & 0 deletions host/libhackrf/src/hackrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ typedef enum {
HACKRF_VENDOR_REQUEST_RADIO_WRITE_REG = 59,
HACKRF_VENDOR_REQUEST_RADIO_READ_REG = 60,
HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE = 61,
HACKRF_VENDOR_REQUEST_READ_TEMPERATURE = 62,
} hackrf_vendor_request;

#define USB_CONFIG_STANDARD 0x1
Expand Down Expand Up @@ -1415,6 +1416,28 @@ int ADDCALL hackrf_test_rtc_osc(hackrf_device* device, bool* pass)
return HACKRF_SUCCESS;
}

int ADDCALL hackrf_read_temperature(hackrf_device* device, int8_t* temp)
{
USB_API_REQUIRED(device, 0x0113);

int result = libusb_control_transfer(
device->usb_device,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
HACKRF_VENDOR_REQUEST_READ_TEMPERATURE,
0,
0,
(unsigned char*) temp,
1,
DEFAULT_REQUEST_TIMEOUT);

if (result < 1) {
last_libusb_error = result;
return HACKRF_ERROR_LIBUSB;
} else {
return HACKRF_SUCCESS;
}
}

int ADDCALL hackrf_read_adc(hackrf_device* device, uint8_t adc_channel, uint16_t* value)
{
USB_API_REQUIRED(device, 0x0109);
Expand Down
10 changes: 10 additions & 0 deletions host/libhackrf/src/hackrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,16 @@ extern ADDAPI int ADDCALL hackrf_read_selftest(
*/
extern ADDAPI int ADDCALL hackrf_test_rtc_osc(hackrf_device* device, bool* pass);

/**
* Read the temperature of the device
*
* @param[in] device device to query
* @param[out] temp Temperature in degrees C
* @return @ref HACKRF_SUCCESS on success or @ref hackrf_error variant
* @ingroup debug
*/
extern ADDAPI int ADDCALL hackrf_read_temperature(hackrf_device* device, int8_t* temp);

/**
* Read a value from an ADC channel
*
Expand Down
Loading