1212// See the License for the specific language governing permissions and 
1313// limitations under the License. 
1414
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" 
2215
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 
3716
17+ #include  "esp32-hal.h" 
18+ #include  "soc/soc_caps.h" 
19+ #include  "driver/sigmadelta.h" 
3820
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 };
4723
4824static  void  _on_apb_change (void  *  arg , apb_change_ev_t  ev_type , uint32_t  old_apb , uint32_t  new_apb ){
4925 if (old_apb  ==  new_apb ){
5026 return ;
5127 }
5228 uint32_t  iarg  =  (uint32_t )arg ;
5329 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 ){
5731 old_apb  /= 1000000 ;
5832 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 ;
6537 }
6638}
6739
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 
6941{
70-  if (channel  >   7 )  {
42+  if (channel  >=  SOC_SIGMADELTA_CHANNEL_NUM ) {
7143 return  0 ;
7244 }
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+  
8046 uint32_t  apb_freq  =  getApbFrequency ();
8147 uint32_t  prescale  =  (apb_freq /(freq * 256 )) -  1 ;
8248 if (prescale  >  0xFF ) {
8349 prescale  =  0xFF ;
8450 }
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 ;
9361 uint32_t  iarg  =  channel ;
9462 addApbChangeCallback ((void * )iarg , _on_apb_change );
63+ 
9564 return  apb_freq /((prescale  +  1 ) *  256 );
9665}
9766
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 
9968{
100-  if (channel  >   7 )  {
69+  if (channel  >=  SOC_SIGMADELTA_CHANNEL_NUM ) {
10170 return ;
10271 }
103-  duty  -=  128 ;
104-  SD_MUTEX_LOCK ();
105-  SIGMADELTA .channel [channel ].duty  =  duty ;
106-  SD_MUTEX_UNLOCK ();
107- }
72+  duty  -=  128 ; 
10873
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 ;
11876}
11977
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  
12179{
122-  if (channel  >   7 )  {
123-  return ;
80+  if (channel  >=  SOC_SIGMADELTA_CHANNEL_NUM ) {
81+  return   0 ;
12482 }
125-  pinMode (pin , OUTPUT );
126-  pinMatrixOutAttach (pin , GPIO_SD0_OUT_IDX  +  channel , false, false);
83+  return  duty_set [channel ]+ 128 ;
12784}
12885
12986void  sigmaDeltaDetachPin (uint8_t  pin )
13087{
13188 pinMatrixOutDetach (pin , false, false);
132- }
89+ }
0 commit comments