12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- #include "esp32-hal.h"
16
- #include "freertos/FreeRTOS.h"
17
- #include "freertos/task.h"
18
- #include "freertos/semphr.h"
19
- #include "esp32-hal-matrix.h"
20
- #include "soc/gpio_sd_reg.h"
21
- #include "soc/gpio_sd_struct.h"
22
15
23
- #include "esp_system.h"
24
- #ifdef ESP_IDF_VERSION_MAJOR // IDF 4+
25
- #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
26
- #include "esp32/rom/ets_sys.h"
27
- #elif CONFIG_IDF_TARGET_ESP32S2
28
- #include "esp32s2/rom/ets_sys.h"
29
- #elif CONFIG_IDF_TARGET_ESP32C3
30
- #include "esp32c3/rom/ets_sys.h"
31
- #else
32
- #error Target CONFIG_IDF_TARGET is not supported
33
- #endif
34
- #else // ESP32 Before IDF 4.0
35
- #include "rom/ets_sys.h"
36
- #endif
37
16
17
+ #include "esp32-hal.h"
18
+ #include "soc/soc_caps.h"
19
+ #include "driver/sigmadelta.h"
38
20
39
- #if CONFIG_DISABLE_HAL_LOCKS
40
- #define SD_MUTEX_LOCK ()
41
- #define SD_MUTEX_UNLOCK ()
42
- #else
43
- #define SD_MUTEX_LOCK () do {} while (xSemaphoreTake(_sd_sys_lock, portMAX_DELAY) != pdPASS)
44
- #define SD_MUTEX_UNLOCK () xSemaphoreGive(_sd_sys_lock)
45
- xSemaphoreHandle _sd_sys_lock ;
46
- #endif
21
+ static uint8_t duty_set [SOC_SIGMADELTA_CHANNEL_NUM ] = {0 };
22
+ static uint32_t prescaler_set [SOC_SIGMADELTA_CHANNEL_NUM ] = {0 };
47
23
48
24
static void _on_apb_change (void * arg , apb_change_ev_t ev_type , uint32_t old_apb , uint32_t new_apb ){
49
25
if (old_apb == new_apb ){
50
26
return ;
51
27
}
52
28
uint32_t iarg = (uint32_t )arg ;
53
29
uint8_t channel = iarg ;
54
- if (ev_type == APB_BEFORE_CHANGE ){
55
- SIGMADELTA .cg .clk_en = 0 ;
56
- } else {
30
+ if (ev_type == APB_AFTER_CHANGE ){
57
31
old_apb /= 1000000 ;
58
32
new_apb /= 1000000 ;
59
- SD_MUTEX_LOCK ();
60
- uint32_t old_prescale = SIGMADELTA .channel [channel ].prescale + 1 ;
61
- SIGMADELTA .channel [channel ].prescale = ((new_apb * old_prescale ) / old_apb ) - 1 ;
62
- SIGMADELTA .cg .clk_en = 0 ;
63
- SIGMADELTA .cg .clk_en = 1 ;
64
- SD_MUTEX_UNLOCK ();
33
+ uint32_t old_prescale = prescaler_set [channel ] + 1 ;
34
+ uint32_t new_prescale = ((new_apb * old_prescale ) / old_apb ) - 1 ;
35
+ sigmadelta_set_prescale (channel ,new_prescale );
36
+ prescaler_set [channel ] = new_prescale ;
65
37
}
66
38
}
67
39
68
- uint32_t sigmaDeltaSetup (uint8_t channel , uint32_t freq ) //chan 0-7 freq 1220-312500
40
+ uint32_t sigmaDeltaSetup (uint8_t pin , uint8_t channel , uint32_t freq ) //chan 0-x according to SOC, freq 1220-312500
69
41
{
70
- if (channel > 7 ) {
42
+ if (channel >= SOC_SIGMADELTA_CHANNEL_NUM ) {
71
43
return 0 ;
72
44
}
73
- #if !CONFIG_DISABLE_HAL_LOCKS
74
- static bool tHasStarted = false;
75
- if (!tHasStarted ) {
76
- tHasStarted = true;
77
- _sd_sys_lock = xSemaphoreCreateMutex ();
78
- }
79
- #endif
45
+
80
46
uint32_t apb_freq = getApbFrequency ();
81
47
uint32_t prescale = (apb_freq /(freq * 256 )) - 1 ;
82
48
if (prescale > 0xFF ) {
83
49
prescale = 0xFF ;
84
50
}
85
- SD_MUTEX_LOCK ();
86
- #ifndef CONFIG_IDF_TARGET_ESP32
87
- SIGMADELTA .misc .function_clk_en = 1 ;
88
- #endif
89
- SIGMADELTA .channel [channel ].prescale = prescale ;
90
- SIGMADELTA .cg .clk_en = 0 ;
91
- SIGMADELTA .cg .clk_en = 1 ;
92
- SD_MUTEX_UNLOCK ();
51
+
52
+ sigmadelta_config_t sigmadelta_cfg = {
53
+ .channel = channel ,
54
+ .sigmadelta_prescale = prescale ,
55
+ .sigmadelta_duty = 0 ,
56
+ .sigmadelta_gpio = pin ,
57
+ };
58
+ sigmadelta_config (& sigmadelta_cfg );
59
+
60
+ prescaler_set [channel ] = prescale ;
93
61
uint32_t iarg = channel ;
94
62
addApbChangeCallback ((void * )iarg , _on_apb_change );
63
+
95
64
return apb_freq /((prescale + 1 ) * 256 );
96
65
}
97
66
98
- void sigmaDeltaWrite (uint8_t channel , uint8_t duty ) //chan 0-7 duty 8 bit
67
+ void sigmaDeltaWrite (uint8_t channel , uint8_t duty ) //chan 0-x according to SOC duty 8 bit
99
68
{
100
- if (channel > 7 ) {
69
+ if (channel >= SOC_SIGMADELTA_CHANNEL_NUM ) {
101
70
return ;
102
71
}
103
- duty -= 128 ;
104
- SD_MUTEX_LOCK ();
105
- SIGMADELTA .channel [channel ].duty = duty ;
106
- SD_MUTEX_UNLOCK ();
107
- }
72
+ duty -= 128 ;
108
73
109
- uint8_t sigmaDeltaRead (uint8_t channel ) //chan 0-7
110
- {
111
- if (channel > 7 ) {
112
- return 0 ;
113
- }
114
- SD_MUTEX_LOCK ();
115
- uint8_t duty = SIGMADELTA .channel [channel ].duty + 128 ;
116
- SD_MUTEX_UNLOCK ();
117
- return duty ;
74
+ sigmadelta_set_duty (channel ,duty );
75
+ duty_set [channel ] = duty ;
118
76
}
119
77
120
- void sigmaDeltaAttachPin (uint8_t pin , uint8_t channel ) //channel 0-7
78
+ uint8_t sigmaDeltaRead (uint8_t channel ) //chan 0-x according to SOC
121
79
{
122
- if (channel > 7 ) {
123
- return ;
80
+ if (channel >= SOC_SIGMADELTA_CHANNEL_NUM ) {
81
+ return 0 ;
124
82
}
125
- pinMode (pin , OUTPUT );
126
- pinMatrixOutAttach (pin , GPIO_SD0_OUT_IDX + channel , false, false);
83
+ return duty_set [channel ]+ 128 ;
127
84
}
128
85
129
86
void sigmaDeltaDetachPin (uint8_t pin )
130
87
{
131
88
pinMatrixOutDetach (pin , false, false);
132
- }
89
+ }
0 commit comments