Make common_arm to hardware abstraction layer.
This commit is contained in:
1 parent
51d0ff260a
commit
36f1c7e44c
61 files changed
+8050
-1670
No files matched your search
@@ -0,0 +1,40 @@
|
||||
#ifndef RSSI_APIS_H_
|
||||
#define RSSI_APIS_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
typedef enum {
|
||||
ADC_RSSI_CH_HF,
|
||||
ADC_RSSI_CH_LF,
|
||||
} adc_rssi_ch_t;
|
||||
|
||||
void AdcSetupRssiChannel(adc_rssi_ch_t ch);
|
||||
uint32_t AdcRssiAvg(adc_rssi_ch_t ch);
|
||||
uint32_t AdcRssiSum(adc_rssi_ch_t ch, uint8_t NbSamples);
|
||||
|
||||
STATIC_FORCE_INLINE void AdcRssiConversionStart(void);
|
||||
STATIC_FORCE_INLINE bool AdcRssiDataReady(adc_rssi_ch_t ch);
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiDataRead(adc_rssi_ch_t ch);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Function for converting ADC values to millivolt units(cross platforms)
|
||||
// The ADC sampling results for each platform have different values in millivolts.
|
||||
// Warn: please use this function for cross platform compatibility.
|
||||
//-----------------------------------------------------------------------------
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiDataToMilliVolt(uint16_t data, adc_rssi_ch_t ch);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// After collecting N times, calculate the average value and convert it to millivolts.
|
||||
// This function calls the AdcRssiSum() function internally, so you don't need to call the setup function in advance.
|
||||
// And the conversion result is the RSSI value in millivolts, dont need considering compatibility issues for cross platform conversion.
|
||||
// Warn: please try to call this function as much as possible!
|
||||
//-----------------------------------------------------------------------------
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiAvgToMilliVolt(adc_rssi_ch_t ch);
|
||||
|
||||
#ifdef PM5
|
||||
#include "rssi_hw_at32.h"
|
||||
#else
|
||||
#include "rssi_hw_at91.h"
|
||||
#endif
|
||||
|
||||
#endif // RSSI_APIS_H_
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "rssi_apis.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Read an ADC channel and block till it completes, then return the result
|
||||
// in ADC units (0 to 1023). Also a routine to sum up a number of samples and
|
||||
// return that.
|
||||
//-----------------------------------------------------------------------------
|
||||
static uint32_t ReadAdc(adc_rssi_ch_t ch) {
|
||||
AdcSetupRssiChannel(ch);
|
||||
while (!AdcRssiDataReady(ch)) {};
|
||||
return AdcRssiDataRead(ch);
|
||||
}
|
||||
|
||||
// Collect 32 times and calculate the average value
|
||||
uint32_t AdcRssiAvg(adc_rssi_ch_t ch) {
|
||||
return AdcRssiSum(ch, 32) >> 5; // == /32
|
||||
}
|
||||
|
||||
// Sample the specified RF field voltage N times, note that it cannot exceed 255 times.
|
||||
uint32_t AdcRssiSum(adc_rssi_ch_t ch, uint8_t NbSamples) {
|
||||
uint32_t a = 0;
|
||||
for (uint8_t i = 0; i < NbSamples; i++)
|
||||
a += ReadAdc(ch);
|
||||
return (a + (NbSamples >> 1) - 1);
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
#include "rssi_apis.h"
|
||||
#include "config_gpio.h"
|
||||
|
||||
uint16_t g_adc_vref_value;
|
||||
|
||||
/**
|
||||
* @brief gpio configuration.
|
||||
* Note: view the 'Datasheet' not 'Reference Manual' for pin maping get.
|
||||
*/
|
||||
static void gpio_config(void) {
|
||||
gpio_init_type gpio_initstructure;
|
||||
gpio_default_para_init(&gpio_initstructure);
|
||||
crm_periph_clock_enable(AT32_GPIO_ADC_RSSI_CLK, TRUE);
|
||||
// config adc pin as analog input mode
|
||||
gpio_initstructure.gpio_mode = GPIO_MODE_ANALOG;
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_ADC_RSSI_LF_PIN | AT32_GPIO_ADC_RSSI_HF_PIN;
|
||||
gpio_init(AT32_GPIO_ADC_RSSI, &gpio_initstructure);
|
||||
}
|
||||
|
||||
void AdcSetupRssiChannel(adc_rssi_ch_t ch) {
|
||||
adc_common_config_type adc_common_struct;
|
||||
adc_base_config_type adc_base_struct;
|
||||
|
||||
adc_common_default_para_init(&adc_common_struct);
|
||||
crm_periph_clock_enable(AT32_RSSI_ADC_PERIPH_CLK, TRUE);
|
||||
gpio_config();
|
||||
adc_reset();
|
||||
|
||||
adc_common_struct.combine_mode = ADC_INDEPENDENT_MODE; // config combine mode
|
||||
adc_common_struct.div = ADC_HCLK_DIV_10; // config division,adcclk is division by hclk
|
||||
adc_common_struct.common_dma_mode = ADC_COMMON_DMAMODE_DISABLE; // config common dma mode,it's not useful in independent mode
|
||||
adc_common_struct.common_dma_request_repeat_state = FALSE; // config common dma request repeat
|
||||
adc_common_struct.sampling_interval = ADC_SAMPLING_INTERVAL_5CYCLES; // config adjacent adc sampling interval,it's useful for ordinary shifting mode
|
||||
adc_common_struct.tempervintrv_state = TRUE; // config inner temperature sensor and vintrv, connect to ADC1_IN16 & ADC1_IN17, we need to detect vref
|
||||
|
||||
/* config voltage battery */
|
||||
adc_common_struct.vbat_state = FALSE;
|
||||
adc_common_config(&adc_common_struct);
|
||||
|
||||
adc_base_default_para_init(&adc_base_struct);
|
||||
adc_base_struct.sequence_mode = FALSE; // Disable sequence mode, acquire a channel once.
|
||||
adc_base_struct.repeat_mode = FALSE;
|
||||
adc_base_struct.data_align = ADC_RIGHT_ALIGNMENT;
|
||||
adc_base_struct.ordinary_channel_length = 1;
|
||||
adc_base_config(AT32_RSSI_ADC, &adc_base_struct);
|
||||
adc_resolution_set(AT32_RSSI_ADC, ADC_RESOLUTION_12B);
|
||||
|
||||
// adc_ordinary_conversion_trigger_set(AT32_RSSI_RSSI_ADC, ADC_ORDINARY_TRIG_TMR1CH1, ADC_ORDINARY_TRIG_EDGE_NONE); // config ordinary trigger source and trigger edge
|
||||
adc_dma_mode_enable(AT32_RSSI_ADC, FALSE); // config dma mode,it's not useful when common dma mode is use
|
||||
adc_dma_request_repeat_enable(AT32_RSSI_ADC, FALSE); // config dma request repeat,it's not useful when common dma mode is use
|
||||
adc_occe_each_conversion_enable(AT32_RSSI_ADC, TRUE); // each ordinary channel conversion set occe flag
|
||||
adc_interrupt_enable(AT32_RSSI_ADC, ADC_OCCO_INT, FALSE); // disable adc overflow interrupt
|
||||
|
||||
// adc enable and wait ready
|
||||
adc_enable(AT32_RSSI_ADC, TRUE);
|
||||
while (adc_flag_get(AT32_RSSI_ADC, ADC_RDY_FLAG) == RESET);
|
||||
|
||||
// adc calibration and wait finish
|
||||
adc_calibration_init(AT32_RSSI_ADC);
|
||||
while (adc_calibration_init_status_get(AT32_RSSI_ADC));
|
||||
adc_calibration_start(AT32_RSSI_ADC);
|
||||
while (adc_calibration_status_get(AT32_RSSI_ADC));
|
||||
|
||||
// get vref value, ADC_CHANNEL_17 is fixed, don't change!!!
|
||||
adc_ordinary_channel_set(AT32_RSSI_ADC, ADC_CHANNEL_17, 1, ADC_SAMPLETIME_640_5);
|
||||
AdcRssiConversionStart();
|
||||
while(adc_flag_get(AT32_RSSI_ADC, ADC_OCCE_FLAG) == RESET); // Waiting for adc conversion done.
|
||||
// printf("vref_value = %f V\r\n", ((double)1.2 * 4095) / adc1_ordinary_value);
|
||||
g_adc_vref_value = adc_ordinary_conversion_data_get(AT32_RSSI_ADC);
|
||||
|
||||
// config ordinary channel and start first time conversion.
|
||||
if (ch == ADC_RSSI_CH_HF) {
|
||||
adc_ordinary_channel_set(AT32_RSSI_ADC, AT32_RSSI_ADC_HF_CHANNEL, 1, ADC_SAMPLETIME_640_5);
|
||||
} else {
|
||||
adc_ordinary_channel_set(AT32_RSSI_ADC, AT32_RSSI_ADC_LF_CHANNEL, 1, ADC_SAMPLETIME_640_5);
|
||||
}
|
||||
AdcRssiConversionStart();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef ADC_RSSI_HW_AT32_H
|
||||
#define ADC_RSSI_HW_AT32_H
|
||||
|
||||
#include "at32f435_437_adc.h"
|
||||
#include "at32f435_437_crm.h"
|
||||
|
||||
#define AT32_RSSI_ADC_PERIPH_CLK CRM_ADC1_PERIPH_CLOCK
|
||||
#define AT32_RSSI_ADC ADC1
|
||||
#define AT32_RSSI_ADC_LF_CHANNEL ADC_CHANNEL_10 // ADC123_IN10
|
||||
#define AT32_RSSI_ADC_HF_CHANNEL ADC_CHANNEL_11 // ADC123_IN11
|
||||
|
||||
/**
|
||||
* Save the reference voltage values collected each time the AdcSetupRssiChannel() function is called.
|
||||
*/
|
||||
extern uint16_t g_adc_vref_value;
|
||||
|
||||
STATIC_FORCE_INLINE void AdcRssiConversionStart(void) {
|
||||
adc_ordinary_software_trigger_enable(AT32_RSSI_ADC, TRUE);
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE bool AdcRssiDataReady(adc_rssi_ch_t ch) {
|
||||
return adc_flag_get(AT32_RSSI_ADC, ADC_OCCE_FLAG);
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiDataRead(adc_rssi_ch_t ch) {
|
||||
return adc_ordinary_conversion_data_get(AT32_RSSI_ADC);
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiDataToMilliVolt(uint16_t data, adc_rssi_ch_t ch) {
|
||||
// Analog input voltage (Vin) = (ADC digital value × reference voltage) / full-scale digital value
|
||||
// Voltage division ratio: LF = 46.45,HF = 31.3
|
||||
if (ch == ADC_RSSI_CH_HF) {
|
||||
// VIN = DATA * 1200 / g_adc_vref_value * 31.3
|
||||
return ((data * 1200) / g_adc_vref_value) * 313 / 10; // = *31.3
|
||||
}
|
||||
// VIN = DATA * 1200 / g_adc_vref_value * 46.45
|
||||
return ((data * 1200) / g_adc_vref_value) * 4645 / 100; // = *46.45
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiAvgToMilliVolt(adc_rssi_ch_t ch) {
|
||||
return AdcRssiDataToMilliVolt(AdcRssiAvg(ch), ch);
|
||||
}
|
||||
|
||||
#endif // ADC_RSSI_HW_AT32_H
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "rssi_apis.h"
|
||||
#include "at91sam7s512.h"
|
||||
#include "proxmark3_arm.h"
|
||||
|
||||
void AdcSetupRssiChannel(adc_rssi_ch_t ch) {
|
||||
|
||||
// Note: ADC_MODE_PRESCALE and ADC_MODE_SAMPLE_HOLD_TIME are set to the maximum allowed value.
|
||||
// AMPL_HI is are high impedance (10MOhm || 1MOhm) output, the input capacitance of the ADC is 12pF (typical). This results in a time constant
|
||||
// of RC = (0.91MOhm) * 12pF = 10.9us. Even after the maximum configurable sample&hold time of 40us the input capacitor will not be fully charged.
|
||||
//
|
||||
// The maths are:
|
||||
// If there is a voltage v_in at the input, the voltage v_cap at the capacitor (this is what we are measuring) will be
|
||||
//
|
||||
// v_cap = v_in * (1 - exp(-SHTIM/RC)) = v_in * (1 - exp(-40us/10.9us)) = v_in * 0,97 (i.e. an error of 3%)
|
||||
|
||||
AT91C_BASE_ADC->ADC_CR = AT91C_ADC_SWRST;
|
||||
AT91C_BASE_ADC->ADC_MR =
|
||||
ADC_MODE_PRESCALE(63) // ADC_CLK = MCK / ((63+1) * 2) = 48MHz / 128 = 375kHz
|
||||
| ADC_MODE_STARTUP_TIME(1) // Startup Time = (1+1) * 8 / ADC_CLK = 16 / 375kHz = 42,7us Note: must be > 20us
|
||||
| ADC_MODE_SAMPLE_HOLD_TIME(15); // Sample & Hold Time SHTIM = 15 / ADC_CLK = 15 / 375kHz = 40us
|
||||
|
||||
if (ch == ADC_RSSI_CH_HF) {
|
||||
AT91C_BASE_ADC->ADC_CHER = ADC_CHANNEL(ADC_CHAN_HF);
|
||||
} else {
|
||||
AT91C_BASE_ADC->ADC_CHER = ADC_CHANNEL(ADC_CHAN_LF);
|
||||
}
|
||||
|
||||
AdcRssiConversionStart();
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#ifndef _ADC_RSSI_HW_AT91_H
|
||||
#define _ADC_RSSI_HW_AT91_H
|
||||
|
||||
#include "at91sam7s512.h"
|
||||
#include "proxmark3_arm.h"
|
||||
|
||||
#if defined RDV4 || defined ICOPYX
|
||||
// ADC Vref = 3300mV, and an (10000k+240k):240k voltage divider on the LF input can measure voltages up to 140800 mV
|
||||
#define MAX_ADC_HF_VOLTAGE 140800
|
||||
#else
|
||||
// ADC Vref = 3300mV, and an (10M+1M):1M voltage divider on the HF input can measure voltages up to 36300 mV
|
||||
#define MAX_ADC_HF_VOLTAGE 36300
|
||||
#endif
|
||||
// ADC Vref = 3300mV, (240k-10M):240k voltage divider, 140800 mV
|
||||
#define MAX_ADC_LF_VOLTAGE 140800
|
||||
|
||||
STATIC_FORCE_INLINE void AdcRssiConversionStart(void) {
|
||||
AT91C_BASE_ADC->ADC_CR = AT91C_ADC_START;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE bool AdcRssiDataReady(adc_rssi_ch_t ch) {
|
||||
if (ch == ADC_RSSI_CH_HF) {
|
||||
return AT91C_BASE_ADC->ADC_SR & ADC_END_OF_CONVERSION(ADC_CHAN_HF);
|
||||
}
|
||||
return AT91C_BASE_ADC->ADC_SR & ADC_END_OF_CONVERSION(ADC_CHAN_LF);
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiDataRead(adc_rssi_ch_t ch) {
|
||||
if (ch == ADC_RSSI_CH_HF) {
|
||||
return AT91C_BASE_ADC->ADC_CDR[ADC_CHAN_HF] & 0x3FF;
|
||||
}
|
||||
return AT91C_BASE_ADC->ADC_CDR[ADC_CHAN_LF] & 0x3FF;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiDataToMilliVolt(uint16_t data, adc_rssi_ch_t ch) {
|
||||
if (ch == ADC_RSSI_CH_HF) {
|
||||
return ((uint32_t)data * MAX_ADC_HF_VOLTAGE) >> 10;
|
||||
}
|
||||
return ((uint32_t)data * MAX_ADC_LF_VOLTAGE) >> 10;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t AdcRssiAvgToMilliVolt(adc_rssi_ch_t ch) {
|
||||
/*
|
||||
* voltage = (sum_32 / 32) * (MAX_ADC_HF_VOLTAGE / 1024)
|
||||
* = (sum_32 * MAX_ADC_HF_VOLTAGE) / (32 * 1024)
|
||||
* = (sum_32 * MAX_ADC_HF_VOLTAGE) / 32768
|
||||
* = (sum_32 * MAX_ADC_HF_VOLTAGE) >> 15
|
||||
*/
|
||||
if (ch == ADC_RSSI_CH_HF) {
|
||||
return (MAX_ADC_HF_VOLTAGE * AdcRssiSum(ADC_RSSI_CH_HF, 32)) >> 15;
|
||||
}
|
||||
// Moving one bit to the right in advance is to avoid the risk of multiplication overflow.
|
||||
return (MAX_ADC_LF_VOLTAGE * (AdcRssiSum(ADC_RSSI_CH_LF, 32) >> 1)) >> 14;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user