-
-
Notifications
You must be signed in to change notification settings - Fork 782
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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); | ||
|
@@ -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) */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 |
||
|
||
|
||
|
||
} | ||
|
||
|
||
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;; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the extra |
||
//return val; | ||
|
||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unreachable, please remove it, along with the commented out |
||
} | ||
|
||
|
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.