mirror of https://github.com/ARMmbed/mbed-os.git
STM32: Analogin - define handle in object not as a global
In this commit, the analogin_s structure is moved to commonn_objects.h file to limit the duplicaion. The ADC handle is moved from a global variable to a struct member of the analogin object. This allows multiple ADC instances to work correctly. Note that State needs to be explicitely set to HAL_ADC_STATE_RESET because the object is not zero initialized.pull/4889/head
parent
4c256f0459
commit
d79014f35a
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -36,15 +36,13 @@
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
int adc_inited = 0;
|
int adc_inited = 0;
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin) {
|
void analogin_init(analogin_t *obj, PinName pin) {
|
||||||
// Get the peripheral name from the pin and assign it to the object
|
// Get the peripheral name from the pin and assign it to the object
|
||||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||||
MBED_ASSERT(obj->adc != (ADCName)NC);
|
MBED_ASSERT(obj->adc != (ADCName)NC);
|
||||||
|
|
||||||
// Get the functions (adc channel) from the pin and assign it to the object
|
// Get the functions (adc channel) from the pin and assign it to the object
|
||||||
uint32_t function = pinmap_function(pin, PinMap_ADC);
|
uint32_t function = pinmap_function(pin, PinMap_ADC);
|
||||||
MBED_ASSERT(function != (uint32_t)NC);
|
MBED_ASSERT(function != (uint32_t)NC);
|
||||||
|
@ -67,25 +65,26 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
||||||
__ADC1_CLK_ENABLE();
|
__ADC1_CLK_ENABLE();
|
||||||
|
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
|
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
|
||||||
AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
|
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||||
AdcHandle.Init.LowPowerAutoWait = DISABLE;
|
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
|
||||||
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
|
obj->handle.Init.LowPowerAutoWait = DISABLE;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE;
|
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||||
AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
|
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
||||||
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC");
|
error("Cannot initialize ADC");
|
||||||
}
|
}
|
||||||
// Run the ADC calibration
|
// Run the ADC calibration
|
||||||
if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK) {
|
if (HAL_ADCEx_Calibration_Start(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot Start ADC_Calibration");
|
error("Cannot Start ADC_Calibration");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +93,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
||||||
static inline uint16_t adc_read(analogin_t *obj) {
|
static inline uint16_t adc_read(analogin_t *obj) {
|
||||||
ADC_ChannelConfTypeDef sConfig;
|
ADC_ChannelConfTypeDef sConfig;
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
||||||
|
@ -169,15 +168,15 @@ static inline uint16_t adc_read(analogin_t *obj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear all channels as it is not done in HAL_ADC_ConfigChannel()
|
// Clear all channels as it is not done in HAL_ADC_ConfigChannel()
|
||||||
AdcHandle.Instance->CHSELR = 0;
|
obj->handle.Instance->CHSELR = 0;
|
||||||
|
|
||||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
return (HAL_ADC_GetValue(&AdcHandle));
|
return (HAL_ADC_GetValue(&obj->handle));
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,6 +111,13 @@ struct i2c_s {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct analogin_s {
|
||||||
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
|
PinName pin;
|
||||||
|
uint8_t channel;
|
||||||
|
};
|
||||||
|
|
||||||
#include "gpio_object.h"
|
#include "gpio_object.h"
|
||||||
|
|
||||||
#if DEVICE_ANALOGOUT
|
#if DEVICE_ANALOGOUT
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -35,8 +35,6 @@
|
||||||
#include "pinmap.h"
|
#include "pinmap.h"
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
int adc_inited = 0;
|
int adc_inited = 0;
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
|
@ -79,15 +77,16 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
|
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
|
||||||
|
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
AdcHandle.Init.ScanConvMode = DISABLE;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE;
|
obj->handle.Init.ScanConvMode = DISABLE;
|
||||||
AdcHandle.Init.NbrOfConversion = 1;
|
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
obj->handle.Init.NbrOfConversion = 1;
|
||||||
AdcHandle.Init.NbrOfDiscConversion = 0;
|
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
obj->handle.Init.NbrOfDiscConversion = 0;
|
||||||
HAL_ADC_Init(&AdcHandle);
|
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||||
|
HAL_ADC_Init(&obj->handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +94,7 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
{
|
{
|
||||||
ADC_ChannelConfTypeDef sConfig;
|
ADC_ChannelConfTypeDef sConfig;
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
sConfig.Rank = 1;
|
sConfig.Rank = 1;
|
||||||
|
@ -160,13 +159,13 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
return (HAL_ADC_GetValue(&AdcHandle));
|
return (HAL_ADC_GetValue(&obj->handle));
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,6 +110,13 @@ struct i2c_s {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct analogin_s {
|
||||||
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
|
PinName pin;
|
||||||
|
uint8_t channel;
|
||||||
|
};
|
||||||
|
|
||||||
#include "gpio_object.h"
|
#include "gpio_object.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -36,8 +36,6 @@
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
{
|
{
|
||||||
uint32_t function = (uint32_t)NC;
|
uint32_t function = (uint32_t)NC;
|
||||||
|
@ -101,20 +99,21 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
|
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
|
||||||
AdcHandle.Init.ScanConvMode = DISABLE;
|
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE;
|
obj->handle.Init.ScanConvMode = DISABLE;
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.NbrOfDiscConversion = 0;
|
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
obj->handle.Init.NbrOfDiscConversion = 0;
|
||||||
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
||||||
AdcHandle.Init.NbrOfConversion = 1;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.NbrOfConversion = 1;
|
||||||
AdcHandle.Init.EOCSelection = DISABLE;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
|
obj->handle.Init.EOCSelection = DISABLE;
|
||||||
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC\n");
|
error("Cannot initialize ADC\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,8 +122,6 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
{
|
{
|
||||||
ADC_ChannelConfTypeDef sConfig = {0};
|
ADC_ChannelConfTypeDef sConfig = {0};
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
sConfig.Rank = 1;
|
sConfig.Rank = 1;
|
||||||
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
||||||
|
@ -192,13 +189,13 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
return (HAL_ADC_GetValue(&AdcHandle));
|
return (HAL_ADC_GetValue(&obj->handle));
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,7 @@ struct port_s {
|
||||||
|
|
||||||
struct analogin_s {
|
struct analogin_s {
|
||||||
ADCName adc;
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
PinName pin;
|
PinName pin;
|
||||||
uint8_t channel;
|
uint8_t channel;
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,13 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
DAC_HandleTypeDef handle;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#if defined (DEVICE_CAN)
|
#if defined (DEVICE_CAN)
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
|
|
|
@ -36,7 +36,6 @@
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
{
|
{
|
||||||
|
@ -76,7 +75,6 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
#if defined(ADC1)
|
#if defined(ADC1)
|
||||||
if ((obj->adc == ADC_1) && adc1_inited) return;
|
if ((obj->adc == ADC_1) && adc1_inited) return;
|
||||||
if (obj->adc == ADC_1) {
|
if (obj->adc == ADC_1) {
|
||||||
AdcHandle.State = HAL_ADC_STATE_RESET;
|
|
||||||
__ADC1_CLK_ENABLE();
|
__ADC1_CLK_ENABLE();
|
||||||
adc1_inited = 1;
|
adc1_inited = 1;
|
||||||
}
|
}
|
||||||
|
@ -84,7 +82,6 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
#if defined(ADC2)
|
#if defined(ADC2)
|
||||||
if ((obj->adc == ADC_2) && adc2_inited) return;
|
if ((obj->adc == ADC_2) && adc2_inited) return;
|
||||||
if (obj->adc == ADC_2) {
|
if (obj->adc == ADC_2) {
|
||||||
AdcHandle.State = HAL_ADC_STATE_RESET;
|
|
||||||
__ADC2_CLK_ENABLE();
|
__ADC2_CLK_ENABLE();
|
||||||
adc2_inited = 1;
|
adc2_inited = 1;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +89,6 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
#if defined(ADC3)
|
#if defined(ADC3)
|
||||||
if ((obj->adc == ADC_3) && adc3_inited) return;
|
if ((obj->adc == ADC_3) && adc3_inited) return;
|
||||||
if (obj->adc == ADC_3) {
|
if (obj->adc == ADC_3) {
|
||||||
AdcHandle.State = HAL_ADC_STATE_RESET;
|
|
||||||
__ADC34_CLK_ENABLE();
|
__ADC34_CLK_ENABLE();
|
||||||
adc3_inited = 1;
|
adc3_inited = 1;
|
||||||
}
|
}
|
||||||
|
@ -100,30 +96,30 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
#if defined(ADC4)
|
#if defined(ADC4)
|
||||||
if ((obj->adc == ADC_4) && adc4_inited) return;
|
if ((obj->adc == ADC_4) && adc4_inited) return;
|
||||||
if (obj->adc == ADC_4) {
|
if (obj->adc == ADC_4) {
|
||||||
AdcHandle.State = HAL_ADC_STATE_RESET;
|
|
||||||
__ADC34_CLK_ENABLE();
|
__ADC34_CLK_ENABLE();
|
||||||
adc4_inited = 1;
|
adc4_inited = 1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
|
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
|
||||||
AdcHandle.Init.ScanConvMode = DISABLE;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
|
obj->handle.Init.ScanConvMode = DISABLE;
|
||||||
AdcHandle.Init.LowPowerAutoWait = DISABLE;
|
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE;
|
obj->handle.Init.LowPowerAutoWait = DISABLE;
|
||||||
AdcHandle.Init.NbrOfConversion = 1;
|
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
obj->handle.Init.NbrOfConversion = 1;
|
||||||
AdcHandle.Init.NbrOfDiscConversion = 0;
|
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
obj->handle.Init.NbrOfDiscConversion = 0;
|
||||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
||||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||||
AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
|
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
||||||
|
|
||||||
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC");
|
error("Cannot initialize ADC");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,7 +128,7 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
{
|
{
|
||||||
ADC_ChannelConfTypeDef sConfig = {0};
|
ADC_ChannelConfTypeDef sConfig = {0};
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
sConfig.Rank = ADC_REGULAR_RANK_1;
|
sConfig.Rank = ADC_REGULAR_RANK_1;
|
||||||
|
@ -200,13 +196,13 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
return (HAL_ADC_GetValue(&AdcHandle));
|
return (HAL_ADC_GetValue(&obj->handle));
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,6 +118,13 @@ struct dac_s {
|
||||||
DAC_HandleTypeDef handle;
|
DAC_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct analogin_s {
|
||||||
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
|
PinName pin;
|
||||||
|
uint8_t channel;
|
||||||
|
};
|
||||||
|
|
||||||
#include "gpio_object.h"
|
#include "gpio_object.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct trng_s {
|
struct trng_s {
|
||||||
RNG_HandleTypeDef handle;
|
RNG_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -40,12 +40,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -40,12 +40,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct trng_s {
|
struct trng_s {
|
||||||
RNG_HandleTypeDef handle;
|
RNG_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -36,8 +36,6 @@
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
{
|
{
|
||||||
uint32_t function = (uint32_t)NC;
|
uint32_t function = (uint32_t)NC;
|
||||||
|
@ -101,21 +99,22 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
|
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
||||||
AdcHandle.Init.ScanConvMode = DISABLE;
|
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE;
|
obj->handle.Init.ScanConvMode = DISABLE;
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.NbrOfDiscConversion = 0;
|
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
obj->handle.Init.NbrOfDiscConversion = 0;
|
||||||
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
||||||
AdcHandle.Init.NbrOfConversion = 1;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.NbrOfConversion = 1;
|
||||||
AdcHandle.Init.EOCSelection = DISABLE;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
|
obj->handle.Init.EOCSelection = DISABLE;
|
||||||
|
|
||||||
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC\n");
|
error("Cannot initialize ADC\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,7 +123,7 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
{
|
{
|
||||||
ADC_ChannelConfTypeDef sConfig = {0};
|
ADC_ChannelConfTypeDef sConfig = {0};
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
sConfig.Rank = 1;
|
sConfig.Rank = 1;
|
||||||
|
@ -202,13 +201,13 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
// so VBAT readings are returned in place of temperature.
|
// so VBAT readings are returned in place of temperature.
|
||||||
ADC->CCR &= ~(ADC_CCR_VBATE | ADC_CCR_TSVREFE);
|
ADC->CCR &= ~(ADC_CCR_VBATE | ADC_CCR_TSVREFE);
|
||||||
|
|
||||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
return (uint16_t)HAL_ADC_GetValue(&AdcHandle);
|
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,11 +109,20 @@ struct i2c_s {
|
||||||
uint8_t available_events;
|
uint8_t available_events;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#if DEVICE_FLASH
|
#if DEVICE_FLASH
|
||||||
struct flash_s {
|
struct flash_s {
|
||||||
uint32_t dummy;
|
uint32_t dummy;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct analogin_s {
|
||||||
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
|
PinName pin;
|
||||||
|
uint8_t channel;
|
||||||
|
};
|
||||||
|
|
||||||
#define GPIO_IP_WITHOUT_BRR
|
#define GPIO_IP_WITHOUT_BRR
|
||||||
#include "gpio_object.h"
|
#include "gpio_object.h"
|
||||||
|
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint8_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -36,8 +36,6 @@
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
{
|
{
|
||||||
uint32_t function = (uint32_t)NC;
|
uint32_t function = (uint32_t)NC;
|
||||||
|
@ -102,21 +100,22 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
|
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
|
||||||
AdcHandle.Init.ScanConvMode = DISABLE;
|
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE;
|
obj->handle.Init.ScanConvMode = DISABLE;
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.NbrOfDiscConversion = 0;
|
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
obj->handle.Init.NbrOfDiscConversion = 0;
|
||||||
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
|
||||||
AdcHandle.Init.NbrOfConversion = 1;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.NbrOfConversion = 1;
|
||||||
AdcHandle.Init.EOCSelection = DISABLE;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
|
obj->handle.Init.EOCSelection = DISABLE;
|
||||||
|
|
||||||
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC");
|
error("Cannot initialize ADC");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +124,7 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
{
|
{
|
||||||
ADC_ChannelConfTypeDef sConfig = {0};
|
ADC_ChannelConfTypeDef sConfig = {0};
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
sConfig.Rank = 1;
|
sConfig.Rank = 1;
|
||||||
|
@ -194,16 +193,16 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) {
|
if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
|
||||||
error("Cannot configure ADC channel");
|
error("Cannot configure ADC channel");
|
||||||
}
|
}
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
HAL_ADC_PollForConversion(&AdcHandle, 10);
|
HAL_ADC_PollForConversion(&obj->handle, 10);
|
||||||
if (HAL_ADC_GetState(&AdcHandle) & HAL_ADC_STATE_EOC_REG) {
|
if (HAL_ADC_GetState(&obj->handle) & HAL_ADC_STATE_EOC_REG) {
|
||||||
return (HAL_ADC_GetValue(&AdcHandle));
|
return (HAL_ADC_GetValue(&obj->handle));
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,6 +111,13 @@ struct i2c_s {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct analogin_s {
|
||||||
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
|
PinName pin;
|
||||||
|
uint8_t channel;
|
||||||
|
};
|
||||||
|
|
||||||
#define GPIO_IP_WITHOUT_BRR
|
#define GPIO_IP_WITHOUT_BRR
|
||||||
#include "gpio_object.h"
|
#include "gpio_object.h"
|
||||||
|
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct trng_s {
|
struct trng_s {
|
||||||
RNG_HandleTypeDef handle;
|
RNG_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct trng_s {
|
struct trng_s {
|
||||||
RNG_HandleTypeDef handle;
|
RNG_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct trng_s {
|
struct trng_s {
|
||||||
RNG_HandleTypeDef handle;
|
RNG_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,8 +36,6 @@
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
int adc_inited = 0;
|
int adc_inited = 0;
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
|
@ -74,37 +72,38 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
if (adc_inited == 0) {
|
if (adc_inited == 0) {
|
||||||
adc_inited = 1;
|
adc_inited = 1;
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
|
|
||||||
// Enable ADC clock
|
// Enable ADC clock
|
||||||
__ADC1_CLK_ENABLE();
|
__ADC1_CLK_ENABLE();
|
||||||
|
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Init.OversamplingMode = DISABLE;
|
obj->handle.Init.OversamplingMode = DISABLE;
|
||||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
|
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
|
||||||
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
|
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
|
||||||
AdcHandle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
|
obj->handle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
|
||||||
AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE;
|
obj->handle.Init.ContinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
obj->handle.Init.DiscontinuousConvMode = DISABLE;
|
||||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
|
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
|
||||||
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
|
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
|
||||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
|
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
|
||||||
AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
||||||
AdcHandle.Init.LowPowerAutoWait = ENABLE;
|
obj->handle.Init.LowPowerAutoWait = ENABLE;
|
||||||
AdcHandle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
|
obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
|
||||||
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
|
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
|
||||||
|
|
||||||
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC");
|
error("Cannot initialize ADC");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calibration
|
// Calibration
|
||||||
HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED);
|
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
|
||||||
|
|
||||||
__HAL_ADC_ENABLE(&AdcHandle);
|
__HAL_ADC_ENABLE(&obj->handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +111,7 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
{
|
{
|
||||||
ADC_ChannelConfTypeDef sConfig = {0};
|
ADC_ChannelConfTypeDef sConfig = {0};
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
||||||
|
@ -181,13 +180,13 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
ADC1->CHSELR = 0; // [TODO] Workaround. To be removed after Cube driver is corrected.
|
ADC1->CHSELR = 0; // [TODO] Workaround. To be removed after Cube driver is corrected.
|
||||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
return (HAL_ADC_GetValue(&AdcHandle));
|
return (HAL_ADC_GetValue(&obj->handle));
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,6 +116,13 @@ struct flash_s {
|
||||||
uint32_t dummy;
|
uint32_t dummy;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct analogin_s {
|
||||||
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
|
PinName pin;
|
||||||
|
uint8_t channel;
|
||||||
|
};
|
||||||
|
|
||||||
#include "gpio_object.h"
|
#include "gpio_object.h"
|
||||||
|
|
||||||
#if DEVICE_ANALOGOUT
|
#if DEVICE_ANALOGOUT
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define GPIO_IP_WITHOUT_BRR
|
#define GPIO_IP_WITHOUT_BRR
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define GPIO_IP_WITHOUT_BRR
|
#define GPIO_IP_WITHOUT_BRR
|
||||||
#include "common_objects.h"
|
#include "common_objects.h"
|
||||||
|
|
||||||
|
|
|
@ -36,8 +36,6 @@
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
int adc_inited = 0;
|
int adc_inited = 0;
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
|
@ -73,29 +71,30 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||||
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
|
|
||||||
// Enable ADC clock
|
// Enable ADC clock
|
||||||
__ADC1_CLK_ENABLE();
|
__ADC1_CLK_ENABLE();
|
||||||
|
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
|
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
|
||||||
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
|
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
|
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
|
||||||
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
|
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
|
||||||
AdcHandle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software.
|
obj->handle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software.
|
||||||
AdcHandle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling).
|
obj->handle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling).
|
||||||
AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
|
obj->handle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
|
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
|
||||||
AdcHandle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
|
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
|
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
|
||||||
AdcHandle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
|
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
|
||||||
AdcHandle.Init.ExternalTrigConv = 0; // Not used
|
obj->handle.Init.ExternalTrigConv = 0; // Not used
|
||||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
|
|
||||||
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC");
|
error("Cannot initialize ADC");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,7 +104,7 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
{
|
{
|
||||||
ADC_ChannelConfTypeDef sConfig = {0};
|
ADC_ChannelConfTypeDef sConfig = {0};
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
switch (obj->channel) {
|
switch (obj->channel) {
|
||||||
|
@ -222,13 +221,13 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
sConfig.Rank = ADC_REGULAR_RANK_1;
|
sConfig.Rank = ADC_REGULAR_RANK_1;
|
||||||
sConfig.SamplingTime = ADC_SAMPLETIME_16CYCLES;
|
sConfig.SamplingTime = ADC_SAMPLETIME_16CYCLES;
|
||||||
|
|
||||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
return (HAL_ADC_GetValue(&AdcHandle));
|
return (HAL_ADC_GetValue(&obj->handle));
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,6 +122,13 @@ struct dac_s {
|
||||||
DAC_HandleTypeDef handle;
|
DAC_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct analogin_s {
|
||||||
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
|
PinName pin;
|
||||||
|
uint8_t channel;
|
||||||
|
};
|
||||||
|
|
||||||
#include "gpio_object.h"
|
#include "gpio_object.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -54,12 +54,6 @@ struct port_s {
|
||||||
__IO uint32_t *reg_out;
|
__IO uint32_t *reg_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct analogin_s {
|
|
||||||
ADCName adc;
|
|
||||||
PinName pin;
|
|
||||||
uint32_t channel;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
CANName can;
|
CANName can;
|
||||||
int index;
|
int index;
|
||||||
|
|
|
@ -36,8 +36,6 @@
|
||||||
#include "mbed_error.h"
|
#include "mbed_error.h"
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
ADC_HandleTypeDef AdcHandle;
|
|
||||||
|
|
||||||
int adc_inited = 0;
|
int adc_inited = 0;
|
||||||
|
|
||||||
void analogin_init(analogin_t *obj, PinName pin)
|
void analogin_init(analogin_t *obj, PinName pin)
|
||||||
|
@ -78,26 +76,27 @@ void analogin_init(analogin_t *obj, PinName pin)
|
||||||
__HAL_RCC_ADC_CLK_ENABLE();
|
__HAL_RCC_ADC_CLK_ENABLE();
|
||||||
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
|
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||||
|
|
||||||
// Configure ADC
|
// Configure ADC
|
||||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
|
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
|
||||||
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
|
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
|
||||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
AdcHandle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
|
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
|
||||||
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
|
obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
|
||||||
AdcHandle.Init.LowPowerAutoWait = DISABLE;
|
obj->handle.Init.LowPowerAutoWait = DISABLE;
|
||||||
AdcHandle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
|
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
|
||||||
AdcHandle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
|
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
|
||||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
|
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
|
||||||
AdcHandle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
|
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
|
||||||
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
|
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
|
||||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||||
AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
|
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
|
||||||
AdcHandle.Init.OversamplingMode = DISABLE; // No oversampling
|
obj->handle.Init.OversamplingMode = DISABLE; // No oversampling
|
||||||
|
|
||||||
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
|
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||||
error("Cannot initialize ADC\n");
|
error("Cannot initialize ADC\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,7 +106,7 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
{
|
{
|
||||||
ADC_ChannelConfTypeDef sConfig = {0};
|
ADC_ChannelConfTypeDef sConfig = {0};
|
||||||
|
|
||||||
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
|
obj->handle.Instance = (ADC_TypeDef *)(obj->adc);
|
||||||
|
|
||||||
// Configure ADC channel
|
// Configure ADC channel
|
||||||
switch (obj->channel) {
|
switch (obj->channel) {
|
||||||
|
@ -178,13 +177,13 @@ static inline uint16_t adc_read(analogin_t *obj)
|
||||||
sConfig.OffsetNumber = ADC_OFFSET_NONE;
|
sConfig.OffsetNumber = ADC_OFFSET_NONE;
|
||||||
sConfig.Offset = 0;
|
sConfig.Offset = 0;
|
||||||
|
|
||||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
|
||||||
|
|
||||||
HAL_ADC_Start(&AdcHandle); // Start conversion
|
HAL_ADC_Start(&obj->handle); // Start conversion
|
||||||
|
|
||||||
// Wait end of conversion and get value
|
// Wait end of conversion and get value
|
||||||
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
|
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||||
return (HAL_ADC_GetValue(&AdcHandle));
|
return (HAL_ADC_GetValue(&obj->handle));
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,6 +116,13 @@ struct flash_s {
|
||||||
uint32_t dummy;
|
uint32_t dummy;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct analogin_s {
|
||||||
|
ADCName adc;
|
||||||
|
ADC_HandleTypeDef handle;
|
||||||
|
PinName pin;
|
||||||
|
uint8_t channel;
|
||||||
|
};
|
||||||
|
|
||||||
#include "gpio_object.h"
|
#include "gpio_object.h"
|
||||||
|
|
||||||
struct dac_s {
|
struct dac_s {
|
||||||
|
|
Loading…
Reference in New Issue