Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add voltage sensor on stm32f3 and correct the readme #1794

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is not being used, and likewise with the gpio-mode_setup() above, please remove them.

adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_601DOT5CYC);
uint8_t channel_array[] = { 1 }; /* ADC1_IN1 (PA0) */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than defining an array, please define just a channel value and take its address below as in:

uint8_t channel = 1U;
adc_set_regular_sequence(ADC1, 1U, &channel);

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");
Comment on lines +145 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i cannot go negative, so to avoid signed overflow issues and other such unnecessary badness, please use unsigned types here. size_t is ideal.

Note that the preferred style for this sort of busy loop is the one used in the native platform for this:

	for (volatile size_t i = 0U; i < 800000U; ++i)
		continue;

The U suffix on the constants are not optional and makes them unsigned, preventing a signed-unsigned conversion that can drastically change the meaning of the code.




}


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;;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the extra ;.

//return val;

return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unreachable, please remove it, along with the commented out return val;

}



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;
}*/
Comment on lines +178 to +183
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this if you aren't including it as active code.


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
Loading