Skip to content

Commit

Permalink
Merge pull request #109 from mossmann/board-rev
Browse files Browse the repository at this point in the history
Extend Cynthion r1.4 detection voltage
  • Loading branch information
mossmann authored Jul 23, 2024
2 parents d9a5943 + eddf962 commit ec9d7c6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
2 changes: 1 addition & 1 deletion firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ BUILD := _build

# Flashing using Saturn-V.
dfu: _build/$(BOARD)/firmware.bin
dfu-util -a 0 -d 1d50:615c -D $< || dfu-util -a 0 -d 1209:0010 -D $<
fwup-util --device 1d50:615c $< || fwup-util --device 1209:0010 $<


# Flashing using the Black Magic Probe,
Expand Down
8 changes: 8 additions & 0 deletions firmware/src/board_rev.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ __attribute__((weak)) const char *get_product_string(void)
{
return "Apollo Debugger";
}

/**
* Return the raw ADC value.
*/
__attribute__((weak)) uint16_t get_adc_reading(void)
{
return 0;
}
5 changes: 5 additions & 0 deletions firmware/src/board_rev.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ const char *get_manufacturer_string(void);
*/
const char *get_product_string(void);

/**
* Return the raw ADC value.
*/
uint16_t get_adc_reading(void);

#endif
43 changes: 26 additions & 17 deletions firmware/src/boards/cynthion_d11/board_rev.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

static uint16_t revision = CYNTHION_REV_UNKNOWN;
static bool gsg_production = false;
static uint16_t reading = 0xffff;

/**
* Detect hardware revision using Cynthion pin straps.
Expand Down Expand Up @@ -50,20 +51,20 @@ void detect_hardware_revision(void)
// Retrieve a single ADC reading.
hri_adc_set_SWTRIG_START_bit(ADC);
while (!hri_adc_get_interrupt_RESRDY_bit(ADC));
uint16_t reading = hri_adc_read_RESULT_reg(ADC);
reading = hri_adc_read_RESULT_reg(ADC);

// Convert ADC measurement to a percentage of the reference voltage.
uint32_t percentage = (((uint32_t)reading * 100) + 2048) >> 12;
if (percentage > 51) {
percentage = 100 - percentage;
// Convert ADC measurement to per mille of the reference voltage.
uint32_t permille = (((uint32_t)reading * 1000) + 20480) >> 12;
if (permille > 510) {
permille = 1000 - permille;
gsg_production = true;
}

/*
hardware version | percent of +3V3
___________________________________________
0.6 | 0-1
future versions | 2-20
future versions | 2-19
1.4 | 21-22
1.3 | 23-24
1.2 | 25-26
Expand All @@ -79,21 +80,21 @@ void detect_hardware_revision(void)
// Identify the board revision by comparing against expected thresholds.
struct {
uint16_t version;
uint8_t threshold;
uint16_t threshold;
} revisions[] = {
{ CYNTHION_REV_0_6, 1 },
{ CYNTHION_REV_UNKNOWN, 20 },
{ CYNTHION_REV_1_4, 22 },
{ CYNTHION_REV_1_3, 24 },
{ CYNTHION_REV_1_2, 26 },
{ CYNTHION_REV_1_0, 28 },
{ CYNTHION_REV_1_1, 31 },
{ CYNTHION_REV_UNKNOWN, 48 },
{ CYNTHION_REV_0_7, 51 },
{ CYNTHION_REV_0_6, 10 },
{ CYNTHION_REV_UNKNOWN, 195 },
{ CYNTHION_REV_1_4, 220 },
{ CYNTHION_REV_1_3, 240 },
{ CYNTHION_REV_1_2, 260 },
{ CYNTHION_REV_1_0, 280 },
{ CYNTHION_REV_1_1, 310 },
{ CYNTHION_REV_UNKNOWN, 480 },
{ CYNTHION_REV_0_7, 510 },
};

int i = 0;
while (percentage > revisions[i].threshold) { ++i; }
while (permille > revisions[i].threshold) { ++i; }
revision = revisions[i].version;
}

Expand Down Expand Up @@ -121,4 +122,12 @@ const char *get_product_string(void)
return (gsg_production) ? "Cynthion Apollo Debugger" : "Apollo Debugger";
}

/**
* Return the raw ADC value.
*/
uint16_t get_adc_reading(void)
{
return reading;
}

#endif
17 changes: 17 additions & 0 deletions firmware/src/vendor.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "debug_spi.h"
#include "usb_switch.h"
#include "fpga_adv.h"
#include "board_rev.h"

#define USB_API_MAJOR 1
#define USB_API_MINOR 1
Expand All @@ -32,6 +33,7 @@ enum {
VENDOR_REQUEST_SET_LED_PATTERN = 0xa1,
VENDOR_REQUEST_GET_FIRMWARE_VERSION = 0xa2,
VENDOR_REQUEST_GET_USB_API_VERSION = 0xa3,
VENDOR_REQUEST_GET_ADC_READING = 0xa4,

//
// JTAG requests.
Expand Down Expand Up @@ -161,6 +163,19 @@ bool handle_get_usb_api_version_request(uint8_t rhport, tusb_control_request_t c
}


/**
* Request raw ADC reading.
*/
bool handle_get_adc_reading_request(uint8_t rhport, tusb_control_request_t const* request)
{
static char buf[2];
uint16_t reading = get_adc_reading();
buf[0] = reading >> 8;
buf[1] = reading & 0xff;
return tud_control_xfer(rhport, request, buf, sizeof(buf));
}


/**
* Request that changes the active LED pattern.
*/
Expand Down Expand Up @@ -213,6 +228,8 @@ bool handle_allow_fpga_takeover_usb_finish(uint8_t rhport, tusb_control_request_
static bool handle_vendor_request_setup(uint8_t rhport, tusb_control_request_t const* request)
{
switch(request->bRequest) {
case VENDOR_REQUEST_GET_ADC_READING:
return handle_get_adc_reading_request(rhport, request);
case VENDOR_REQUEST_GET_ID:
return handle_get_id_request(rhport, request);
case VENDOR_REQUEST_GET_FIRMWARE_VERSION:
Expand Down

0 comments on commit ec9d7c6

Please sign in to comment.