Skip to content

Commit 83e5d0a

Browse files
committed
locking: use local lock key instead of global
Make sure all peripherals in this PR use local irq_lock and irq_unlock instead of common nested reference. Signed-off-by: Sylvio Alves <[email protected]>
1 parent c009176 commit 83e5d0a

19 files changed

+217
-316
lines changed

components/driver/deprecated/adc_legacy.c

+40-49
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ static const char *ADC_TAG = "ADC";
4545
//////////////////////// Locks ///////////////////////////////////////////
4646
LOG_MODULE_REGISTER(adc_legacy, CONFIG_ADC_LOG_LEVEL);
4747

48-
extern int rtc_spinlock;
49-
50-
#define RTC_ENTER_CRITICAL() do { rtc_spinlock = irq_lock(); } while(0)
51-
#define RTC_EXIT_CRITICAL() irq_unlock(rtc_spinlock);
52-
5348
#define DIGI_ENTER_CRITICAL()
5449
#define DIGI_EXIT_CRITICAL()
5550

@@ -142,9 +137,9 @@ esp_err_t adc2_pad_get_io_num(adc2_channel_t channel, gpio_num_t *gpio_num)
142137
#if SOC_ADC_RTC_CTRL_SUPPORTED
143138
esp_err_t adc_set_clk_div(uint8_t clk_div)
144139
{
145-
DIGI_CONTROLLER_ENTER();
140+
unsigned int key = irq_lock();
146141
adc_ll_digi_set_clk_div(clk_div);
147-
DIGI_CONTROLLER_EXIT();
142+
irq_unlock(key);
148143
return ESP_OK;
149144
}
150145

@@ -196,14 +191,14 @@ esp_err_t adc_common_gpio_init(adc_unit_t adc_unit, adc_channel_t channel)
196191
esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en)
197192
{
198193
if (adc_unit == ADC_UNIT_1) {
199-
SARADC1_ENTER();
194+
unsigned int key = irq_lock();
200195
adc_oneshot_ll_output_invert(ADC_UNIT_1, inv_en);
201-
SARADC1_EXIT();
196+
irq_unlock(key);
202197
}
203198
if (adc_unit == ADC_UNIT_2) {
204-
SARADC2_ENTER();
199+
unsigned int key = irq_lock();
205200
adc_oneshot_ll_output_invert(ADC_UNIT_2, inv_en);
206-
SARADC2_EXIT();
201+
irq_unlock(key);
207202
}
208203

209204
return ESP_OK;
@@ -241,14 +236,14 @@ esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t width_bit)
241236
#endif
242237

243238
if (adc_unit == ADC_UNIT_1) {
244-
SARADC1_ENTER();
239+
unsigned int key = irq_lock();
245240
adc_oneshot_ll_set_output_bits(ADC_UNIT_1, bitwidth);
246-
SARADC1_EXIT();
241+
irq_unlock(key);
247242
}
248243
if (adc_unit == ADC_UNIT_2) {
249-
SARADC2_ENTER();
244+
unsigned int key = irq_lock();
250245
adc_oneshot_ll_set_output_bits(ADC_UNIT_2, bitwidth);
251-
SARADC2_EXIT();
246+
irq_unlock(key);
252247
}
253248

254249
return ESP_OK;
@@ -263,9 +258,9 @@ esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t width_bit)
263258
#if !CONFIG_IDF_TARGET_ESP32
264259
esp_err_t adc_rtc_reset(void)
265260
{
266-
FSM_ENTER();
261+
unsigned int key = irq_lock();
267262
adc_ll_rtc_reset();
268-
FSM_EXIT();
263+
irq_unlock(key);
269264
return ESP_OK;
270265
}
271266
#endif
@@ -286,10 +281,10 @@ esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
286281
#endif //#if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
287282

288283
adc_common_gpio_init(ADC_UNIT_1, channel);
289-
SARADC1_ENTER();
284+
unsigned int key = irq_lock();
290285
adc_rtc_chan_init(ADC_UNIT_1);
291286
adc_oneshot_ll_set_atten(ADC_UNIT_1, channel, atten);
292-
SARADC1_EXIT();
287+
irq_unlock(key);
293288

294289
return ESP_OK;
295290
}
@@ -325,9 +320,9 @@ esp_err_t adc1_config_width(adc_bits_width_t width_bit)
325320
bitwidth = ADC_BITWIDTH_12;
326321
#endif
327322

328-
SARADC1_ENTER();
323+
unsigned int key = irq_lock();
329324
adc_oneshot_ll_set_output_bits(ADC_UNIT_1, bitwidth);
330-
SARADC1_EXIT();
325+
irq_unlock(key);
331326

332327
return ESP_OK;
333328
}
@@ -341,10 +336,10 @@ esp_err_t adc1_dma_mode_acquire(void)
341336

342337
sar_periph_ctrl_adc_continuous_power_acquire();
343338

344-
SARADC1_ENTER();
339+
unsigned int key = irq_lock();
345340
/* switch SARADC into DIG channel */
346341
adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_DIG);
347-
SARADC1_EXIT();
342+
irq_unlock(key);
348343

349344
return ESP_OK;
350345
}
@@ -356,10 +351,10 @@ esp_err_t adc1_rtc_mode_acquire(void)
356351
SARADC1_ACQUIRE();
357352
sar_periph_ctrl_adc_oneshot_power_acquire();
358353

359-
SARADC1_ENTER();
354+
unsigned int key = irq_lock();
360355
/* switch SARADC into RTC channel. */
361356
adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_RTC);
362-
SARADC1_EXIT();
357+
irq_unlock(key);
363358

364359
return ESP_OK;
365360
}
@@ -386,7 +381,7 @@ int adc1_get_raw(adc1_channel_t channel)
386381
adc_set_hw_calibration_code(ADC_UNIT_1, atten);
387382
#endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
388383

389-
SARADC1_ENTER();
384+
unsigned int key = irq_lock();
390385
#ifdef CONFIG_IDF_TARGET_ESP32
391386
adc_ll_hall_disable(); //Disable other peripherals.
392387
adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
@@ -397,7 +392,7 @@ int adc1_get_raw(adc1_channel_t channel)
397392
#if !CONFIG_IDF_TARGET_ESP32
398393
adc_ll_rtc_reset(); //Reset FSM of rtc controller
399394
#endif
400-
SARADC1_EXIT();
395+
irq_unlock(key);
401396

402397
adc1_lock_release();
403398
return adc_value;
@@ -413,7 +408,7 @@ void adc1_ulp_enable(void)
413408
{
414409
sar_periph_ctrl_adc_oneshot_power_acquire();
415410

416-
SARADC1_ENTER();
411+
unsigned int key = irq_lock();
417412
adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_ULP);
418413
/* since most users do not need LNA and HALL with uLP, we disable them here
419414
open them in the uLP if needed. */
@@ -422,7 +417,7 @@ void adc1_ulp_enable(void)
422417
adc_ll_hall_disable();
423418
adc_ll_amp_disable();
424419
#endif
425-
SARADC1_EXIT();
420+
irq_unlock(key);
426421
}
427422
#endif
428423

@@ -452,10 +447,10 @@ esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
452447
#endif
453448

454449
//avoid collision with other tasks
455-
SARADC2_ENTER();
450+
unsigned int key = irq_lock();
456451
adc_rtc_chan_init(ADC_UNIT_2);
457452
adc_oneshot_ll_set_atten(ADC_UNIT_2, channel, atten);
458-
SARADC2_EXIT();
453+
irq_unlock(key);
459454

460455
#if CONFIG_IDF_TARGET_ESP32
461456
adc_lock_release(ADC_UNIT_2);
@@ -553,7 +548,7 @@ esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *
553548

554549
//avoid collision with other tasks
555550
adc2_init(); // in critical section with whole rtc module. because the PWDET use the same registers, place it here.
556-
SARADC2_ENTER();
551+
unsigned int key = irq_lock();
557552

558553
#if SOC_ADC_ARBITER_SUPPORTED
559554
adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
@@ -593,7 +588,7 @@ esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *
593588
}
594589
#endif //CONFIG_PM_ENABLE
595590
#endif //CONFIG_IDF_TARGET_ESP32
596-
SARADC2_EXIT();
591+
irq_unlock(key);
597592

598593
sar_periph_ctrl_adc_oneshot_power_release();
599594
#if CONFIG_IDF_TARGET_ESP32
@@ -625,13 +620,13 @@ esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio)
625620

626621
sar_periph_ctrl_adc_oneshot_power_acquire();
627622
if (adc_unit == ADC_UNIT_1) {
628-
VREF_ENTER(1);
623+
unsigned int key = irq_lock();
629624
adc_ll_vref_output(ADC_UNIT_1, ch, true);
630-
VREF_EXIT(1);
625+
irq_unlock(key);
631626
} else if (adc_unit == ADC_UNIT_2) {
632-
VREF_ENTER(2);
627+
unsigned int key = irq_lock();
633628
adc_ll_vref_output(ADC_UNIT_2, ch, true);
634-
VREF_EXIT(2);
629+
irq_unlock(key);
635630
}
636631

637632
//Configure RTC gpio, Only ADC2's channels IO are supported to output reference voltage.
@@ -650,10 +645,6 @@ esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio)
650645
---------------------------------------------------------------*/
651646
#include "esp_check.h"
652647

653-
int adc_reg_lock;
654-
#define ADC_REG_LOCK_ENTER() do { adc_reg_lock = irq_lock(); } while(0)
655-
#define ADC_REG_LOCK_EXIT() irq_unlock(adc_reg_lock);
656-
657648
static adc_atten_t s_atten1_single[ADC1_CHANNEL_MAX]; //Array saving attenuate of each channel of ADC1, used by single read API
658649
#if (SOC_ADC_PERIPH_NUM >= 2)
659650
static adc_atten_t s_atten2_single[ADC2_CHANNEL_MAX]; //Array saving attenuate of each channel of ADC2, used by single read API
@@ -718,13 +709,13 @@ esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio)
718709

719710
sar_periph_ctrl_adc_oneshot_power_acquire();
720711
if (adc_unit == ADC_UNIT_1) {
721-
RTC_ENTER_CRITICAL();
712+
unsigned int key = irq_lock();
722713
adc_ll_vref_output(ADC_UNIT_1, channel, true);
723-
RTC_EXIT_CRITICAL();
714+
irq_unlock(key);
724715
} else { //ADC_UNIT_2
725-
RTC_ENTER_CRITICAL();
716+
unsigned int key = irq_lock();
726717
adc_ll_vref_output(ADC_UNIT_2, channel, true);
727-
RTC_EXIT_CRITICAL();
718+
irq_unlock(key);
728719
}
729720

730721
ret = adc_digi_gpio_init(ADC_UNIT_2, BIT(channel));
@@ -778,10 +769,10 @@ int adc1_get_raw(adc1_channel_t channel)
778769
adc_set_hw_calibration_code(ADC_UNIT_1, atten);
779770
#endif
780771

781-
ADC_REG_LOCK_ENTER();
772+
unsigned int key = irq_lock();
782773
adc_oneshot_ll_set_atten(ADC_UNIT_1, channel, atten);
783774
adc_hal_convert(ADC_UNIT_1, channel, clk_src_freq_hz, &raw_out);
784-
ADC_REG_LOCK_EXIT();
775+
irq_unlock(key);
785776

786777
sar_periph_ctrl_adc_oneshot_power_release();
787778
periph_module_disable(PERIPH_SARADC_MODULE);
@@ -834,10 +825,10 @@ esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *
834825
adc_set_hw_calibration_code(ADC_UNIT_2, atten);
835826
#endif
836827

837-
ADC_REG_LOCK_ENTER();
828+
unsigned int key = irq_lock();
838829
adc_oneshot_ll_set_atten(ADC_UNIT_2, channel, atten);
839830
ret = adc_hal_convert(ADC_UNIT_2, channel, clk_src_freq_hz, raw_out);
840-
ADC_REG_LOCK_EXIT();
831+
irq_unlock(key);
841832

842833
sar_periph_ctrl_adc_oneshot_power_release();
843834
periph_module_disable(PERIPH_SARADC_MODULE);

components/driver/deprecated/dac_common_legacy.c

+14-19
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
#include "hal/dac_ll.h"
1717
#include "clk_ctrl_os.h"
1818

19-
extern int rtc_spinlock;
20-
21-
#define RTC_ENTER_CRITICAL() do { rtc_spinlock = irq_lock(); } while(0)
22-
#define RTC_EXIT_CRITICAL() irq_unlock(rtc_spinlock);
23-
2419
static __attribute__((unused)) const char *TAG = "DAC";
2520

2621
/*---------------------------------------------------------------
@@ -54,10 +49,10 @@ esp_err_t dac_output_enable(dac_channel_t channel)
5449
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
5550

5651
dac_rtc_pad_init(channel);
57-
RTC_ENTER_CRITICAL();
52+
unsigned int key = irq_lock();
5853
dac_ll_power_on(channel);
5954
dac_ll_rtc_sync_by_adc(false);
60-
RTC_EXIT_CRITICAL();
55+
irq_unlock(key);
6156

6257
return ESP_OK;
6358
}
@@ -66,9 +61,9 @@ esp_err_t dac_output_disable(dac_channel_t channel)
6661
{
6762
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
6863

69-
RTC_ENTER_CRITICAL();
64+
unsigned int key = irq_lock();
7065
dac_ll_power_down(channel);
71-
RTC_EXIT_CRITICAL();
66+
irq_unlock(key);
7267

7368
return ESP_OK;
7469
}
@@ -77,9 +72,9 @@ esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)
7772
{
7873
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
7974

80-
RTC_ENTER_CRITICAL();
75+
unsigned int key = irq_lock();
8176
dac_ll_update_output_value(channel, dac_value);
82-
RTC_EXIT_CRITICAL();
77+
irq_unlock(key);
8378

8479
return ESP_OK;
8580
}
@@ -88,37 +83,37 @@ esp_err_t dac_out_voltage(dac_channel_t channel, uint8_t dac_value)
8883
{
8984
ESP_RETURN_ON_FALSE(channel < SOC_DAC_CHAN_NUM, ESP_ERR_INVALID_ARG, TAG, "DAC channel error");
9085

91-
RTC_ENTER_CRITICAL();
86+
unsigned int key = irq_lock();
9287
dac_ll_update_output_value(channel, dac_value);
93-
RTC_EXIT_CRITICAL();
88+
irq_unlock(key);
9489

9590
return ESP_OK;
9691
}
9792

9893
esp_err_t dac_cw_generator_enable(void)
9994
{
100-
RTC_ENTER_CRITICAL();
95+
unsigned int key = irq_lock();
10196
periph_rtc_dig_clk8m_enable();
10297
dac_ll_cw_generator_enable();
103-
RTC_EXIT_CRITICAL();
98+
irq_unlock(key);
10499

105100
return ESP_OK;
106101
}
107102

108103
esp_err_t dac_cw_generator_disable(void)
109104
{
110-
RTC_ENTER_CRITICAL();
105+
unsigned int key = irq_lock();
111106
dac_ll_cw_generator_disable();
112107
periph_rtc_dig_clk8m_disable();
113-
RTC_EXIT_CRITICAL();
108+
irq_unlock(key);
114109

115110
return ESP_OK;
116111
}
117112

118113
esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)
119114
{
120115
ESP_RETURN_ON_FALSE(cw, ESP_ERR_INVALID_ARG, TAG, "invalid clock configuration");
121-
RTC_ENTER_CRITICAL();
116+
unsigned int key = irq_lock();
122117
/* Enable the rtc8m clock temporary to get the correct frequency */
123118
periph_rtc_dig_clk8m_enable();
124119
uint32_t rtc_freq = periph_rtc_dig_clk8m_get_freq();
@@ -128,7 +123,7 @@ esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)
128123
dac_ll_cw_set_phase(cw->en_ch, (dac_cosine_phase_t)cw->phase);
129124
dac_ll_cw_set_dc_offset(cw->en_ch, cw->offset);
130125
dac_ll_cw_enable_channel(cw->en_ch, true);
131-
RTC_EXIT_CRITICAL();
126+
irq_unlock(key);
132127

133128
return ESP_OK;
134129
}

0 commit comments

Comments
 (0)