Skip to content

Commit

Permalink
add voltage sensor on stm32f3 and correct the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuFluidy committed Apr 9, 2024
1 parent 8699093 commit 9a0c486
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/platforms/f3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ For simpicity, the ST system bootloader is used. This saves additional 4 kB for
## Connections

* PA2: UART RX
* PS3: UART TX
* PA0: TDI
* PA3: UART TX
* PA0: VTref
* PA1: TMS/SWDIO
* PA4: TDI
* PA7: TCK/SWCLK
* PA6: TDO/TRACESWO
* PA5: TRST
Expand Down
70 changes: 69 additions & 1 deletion src/platforms/f3/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/syscfg.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/stm32/flash.h>

static void adc_init(void);

extern uint32_t _ebss; // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)

int platform_hwversion(void)
Expand Down Expand Up @@ -94,6 +97,9 @@ void platform_init(void)
gpio_mode_setup(NRST_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, NRST_PIN);
gpio_set(NRST_PORT, NRST_PIN);
gpio_set_output_options(NRST_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, NRST_PIN);

adc_init();

platform_timing_init();
/* Set up USB pins and alternate function */
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12);
Expand All @@ -112,9 +118,71 @@ bool platform_nrst_get_val(void)
return (gpio_get(NRST_PORT, NRST_PIN)) ? false : true;
}


static void adc_init(void)
{

//ADC
rcc_periph_clock_enable(RCC_ADC12);
//ADC
gpio_mode_setup(VTREF_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, VTREF_PIN);
//gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1);
adc_power_off(ADC1);
adc_set_clk_prescale(ADC1, ADC_CCR_CKMODE_DIV2);
adc_set_single_conversion_mode(ADC1);
adc_disable_external_trigger_regular(ADC1);
adc_set_right_aligned(ADC1);
/* We want to read the temperature sensor, so we have to enable it. */
//adc_enable_temperature_sensor();
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_601DOT5CYC);
uint8_t channel_array[] = { 1 }; /* ADC1_IN1 (PA0) */
adc_calibrate(ADC1);
adc_set_regular_sequence(ADC1, 1, channel_array);
adc_set_resolution(ADC1, ADC_CFGR1_RES_12_BIT);
adc_power_on(ADC1);

/* Wait for ADC starting up. */
int i;
for (i = 0; i < 800000; i++)
__asm__("nop");



}


uint32_t platform_target_voltage_sense(void)
{
/*
* Returns the voltage in tenths of a volt (so 33 means 3.3V)
*/
adc_start_conversion_regular(ADC1);
while (!(adc_eoc(ADC1)))
continue;
uint32_t val=adc_read_regular(ADC1);
return (val * 99U) / 8191U;;
//return val;

return 0;
}



const char *platform_target_voltage(void)
{
return "ABSENT!";
static char ret[] = "0.0V";
uint32_t val = platform_target_voltage_sense();
ret[0] = '0' + val / 10U;
ret[2] = '0' + val % 10U;

/*static char ret[] = "0000000000";
uint32_t val = platform_target_voltage_sense();
for(uint8_t i = 10; i > 0; i--){
ret[i-1] = '0' + val % 10U;
val = val / 10;
}*/

return ret;
}

#pragma GCC diagnostic push
Expand Down
7 changes: 5 additions & 2 deletions src/platforms/f3/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
* LED1 = PB6 (Orange LED : Idle)
* LED2 = PB7 (Red LED : Error)
*
* TDI = PA0
* VTref = PA0
* TMS = PA1 (input for SWDP)
* TDI = PA4
* TCK = PA7/SWCLK
* TDO = PA6 (input for TRACESWO
* nRST = PA5
Expand All @@ -48,11 +49,13 @@

/* Hardware definitions... */
#define JTAG_PORT GPIOA
#define VTREF_PORT JTAG_PORT
#define TDI_PORT JTAG_PORT
#define TMS_PORT JTAG_PORT
#define TCK_PORT JTAG_PORT
#define TDO_PORT JTAG_PORT
#define TDI_PIN GPIO0
#define VTREF_PIN GPIO0
#define TDI_PIN GPIO4
#define TMS_PIN GPIO1
#define TCK_PIN GPIO7
#define TDO_PIN GPIO6
Expand Down

0 comments on commit 9a0c486

Please sign in to comment.