ADC unit select, reset bugfix

This commit is contained in:
Hierophect 2019-09-12 13:47:01 -04:00
parent 57ce381bcd
commit 55eb8dcfa0
4 changed files with 52 additions and 19 deletions

View File

@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -47,8 +48,16 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
//TODO: add ADC traits to structure?
LL_GPIO_SetPinMode(pin_port(pin->port), pin_mask(pin->number), LL_GPIO_MODE_ANALOG);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1); //TODO: conditional on ADC unit
//claim_pin(pin);
if (pin->adc_unit & 0x01) {
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
} else if (pin->adc_unit == 0x04) {
#ifdef LL_APB2_GRP1_PERIPH_ADC3
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC3);
#endif
} else {
mp_raise_ValueError(translate("Invalid ADC Unit value"));
}
claim_pin(pin);
self->pin = pin;
}
@ -67,6 +76,17 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
// Something else might have used the ADC in a different way,
// so we completely re-initialize it.
ADC_TypeDef * ADCx;
if(self->pin->adc_unit & 0x01) {
ADCx = ADC1;
} else if (self->pin->adc_unit == 0x04) {
#ifdef ADC3
ADCx = ADC3;
#endif
} else {
mp_raise_ValueError(translate("Invalid ADC Unit value"));
}
//HAL Implementation
// ADC_HandleTypeDef AdcHandle;
@ -99,24 +119,24 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
// HAL_ADC_Stop(&AdcHandle);
//LL Implementation
if (LL_ADC_IsEnabled(ADC1) == 0)
if (LL_ADC_IsEnabled(ADCx) == 0)
{
LL_ADC_REG_SetTriggerSource(ADC1, LL_ADC_REG_TRIG_SOFTWARE);
LL_ADC_REG_SetContinuousMode(ADC1, LL_ADC_REG_CONV_SINGLE);
LL_ADC_REG_SetSequencerLength(ADC1, LL_ADC_REG_SEQ_SCAN_DISABLE);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, self->pin->adc_channel);
LL_ADC_REG_SetTriggerSource(ADCx, LL_ADC_REG_TRIG_SOFTWARE);
LL_ADC_REG_SetContinuousMode(ADCx, LL_ADC_REG_CONV_SINGLE);
LL_ADC_REG_SetSequencerLength(ADCx, LL_ADC_REG_SEQ_SCAN_DISABLE);
LL_ADC_REG_SetSequencerRanks(ADCx, LL_ADC_REG_RANK_1, self->pin->adc_channel);
//LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_4);
LL_ADC_SetChannelSamplingTime(ADC1, self->pin->adc_channel, LL_ADC_SAMPLINGTIME_56CYCLES);
LL_ADC_EnableIT_OVR(ADC1);
LL_ADC_SetChannelSamplingTime(ADCx, self->pin->adc_channel, LL_ADC_SAMPLINGTIME_56CYCLES);
LL_ADC_EnableIT_OVR(ADCx);
}
LL_ADC_Enable(ADC1);
LL_ADC_Enable(ADCx);
uint16_t uhADCxConvertedData = (__LL_ADC_DIGITAL_SCALE(LL_ADC_RESOLUTION_12B) + 1);
LL_ADC_REG_StartConversionSWStart(ADC1);
while (LL_ADC_IsActiveFlag_EOCS(ADC1) == 0) {}
LL_ADC_REG_StartConversionSWStart(ADCx);
while (LL_ADC_IsActiveFlag_EOCS(ADCx) == 0) {}
/* Retrieve ADC conversion data */
/* (data scale corresponds to ADC resolution: 12 bits) */
uhADCxConvertedData = LL_ADC_REG_ReadConversionData12(ADC1);
uhADCxConvertedData = LL_ADC_REG_ReadConversionData12(ADCx);
// // Shift the value to be 16 bit.
return uhADCxConvertedData << 4;

View File

@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -24,8 +25,8 @@
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H
#ifndef MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGIN_H
#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGIN_H
#include "common-hal/microcontroller/Pin.h"
@ -44,4 +45,4 @@ static inline uint8_t stm32_adc_channel(uint8_t adc_packed) {
return adc_packed & 0x1f;
}
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGIN_H

View File

@ -30,6 +30,8 @@
#include "boards/board.h"
#include "tick.h"
#include "common-hal/microcontroller/Pin.h"
#include "stm32f4/clocks.h"
#include "stm32f4/gpio.h"
@ -48,7 +50,7 @@ safe_mode_t port_init(void) {
}
void reset_port(void) {
reset_all_pins();
}
void reset_to_bootloader(void) {
@ -56,7 +58,7 @@ void reset_to_bootloader(void) {
}
void reset_cpu(void) {
NVIC_SystemReset();
}
extern uint32_t _ebss;
@ -70,5 +72,8 @@ uint32_t port_get_saved_word(void) {
}
void HardFault_Handler(void) {
while(1) {}
reset_into_safe_mode(HARD_CRASH);
while (true) {
asm("nop;");
}
}

View File

@ -32,6 +32,8 @@
#include "lib/mp-readline/readline.h"
#include "stm32f4xx_hal.h"
#include "common-hal/microcontroller/Pin.h"
void init_usb_hardware(void) {
//TODO: if future chips overload this with options, move to peripherals management.
@ -50,12 +52,15 @@ void init_usb_hardware(void) {
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
never_reset_pin_number(0, 11);
never_reset_pin_number(0, 12);
/* Configure VBUS Pin */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
never_reset_pin_number(0, 9);
/* This for ID line debug */
GPIO_InitStruct.Pin = GPIO_PIN_10;
@ -64,6 +69,7 @@ void init_usb_hardware(void) {
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
never_reset_pin_number(0, 10);
#ifdef STM32F412Zx
/* Configure POWER_SWITCH IO pin (F412 ONLY)*/
@ -71,6 +77,7 @@ void init_usb_hardware(void) {
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
never_reset_pin_number(0, 8);
#endif
/* Peripheral clock enable */