- Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathesp32-hal-sigmadelta.c
115 lines (105 loc) · 3.19 KB
/
esp32-hal-sigmadelta.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include"esp32-hal.h"
#include"freertos/FreeRTOS.h"
#include"freertos/task.h"
#include"freertos/semphr.h"
#include"rom/ets_sys.h"
#include"esp32-hal-matrix.h"
#include"soc/gpio_sd_reg.h"
#include"soc/gpio_sd_struct.h"
#ifCONFIG_DISABLE_HAL_LOCKS
#defineSD_MUTEX_LOCK()
#defineSD_MUTEX_UNLOCK()
#else
#defineSD_MUTEX_LOCK() do {} while (xSemaphoreTake(_sd_sys_lock, portMAX_DELAY) != pdPASS)
#defineSD_MUTEX_UNLOCK() xSemaphoreGive(_sd_sys_lock)
xSemaphoreHandle_sd_sys_lock;
#endif
staticvoid_on_apb_change(void*arg, apb_change_ev_tev_type, uint32_told_apb, uint32_tnew_apb){
if(old_apb==new_apb){
return;
}
uint32_tiarg= (uint32_t)arg;
uint8_tchannel=iarg;
if(ev_type==APB_BEFORE_CHANGE){
SIGMADELTA.cg.clk_en=0;
} else {
old_apb /= 1000000;
new_apb /= 1000000;
SD_MUTEX_LOCK();
uint32_told_prescale=SIGMADELTA.channel[channel].prescale+1;
SIGMADELTA.channel[channel].prescale= ((new_apb*old_prescale) / old_apb) -1;
SIGMADELTA.cg.clk_en=0;
SIGMADELTA.cg.clk_en=1;
SD_MUTEX_UNLOCK();
}
}
uint32_tsigmaDeltaSetup(uint8_tchannel, uint32_tfreq) //chan 0-7 freq 1220-312500
{
if(channel>7) {
return0;
}
#if !CONFIG_DISABLE_HAL_LOCKS
staticbooltHasStarted= false;
if(!tHasStarted) {
tHasStarted= true;
_sd_sys_lock=xSemaphoreCreateMutex();
}
#endif
uint32_tapb_freq=getApbFrequency();
uint32_tprescale= (apb_freq/(freq*256)) -1;
if(prescale>0xFF) {
prescale=0xFF;
}
SD_MUTEX_LOCK();
SIGMADELTA.channel[channel].prescale=prescale;
SIGMADELTA.cg.clk_en=0;
SIGMADELTA.cg.clk_en=1;
SD_MUTEX_UNLOCK();
uint32_tiarg=channel;
addApbChangeCallback((void*)iarg, _on_apb_change);
returnapb_freq/((prescale+1) *256);
}
voidsigmaDeltaWrite(uint8_tchannel, uint8_tduty) //chan 0-7 duty 8 bit
{
if(channel>7) {
return;
}
duty-=128;
SD_MUTEX_LOCK();
SIGMADELTA.channel[channel].duty=duty;
SD_MUTEX_UNLOCK();
}
uint8_tsigmaDeltaRead(uint8_tchannel) //chan 0-7
{
if(channel>7) {
return0;
}
SD_MUTEX_LOCK();
uint8_tduty=SIGMADELTA.channel[channel].duty+128;
SD_MUTEX_UNLOCK();
returnduty;
}
voidsigmaDeltaAttachPin(uint8_tpin, uint8_tchannel) //channel 0-7
{
if(channel>7) {
return;
}
pinMode(pin, OUTPUT);
pinMatrixOutAttach(pin, GPIO_SD0_OUT_IDX+channel, false, false);
}
voidsigmaDeltaDetachPin(uint8_tpin)
{
pinMatrixOutDetach(pin, false, false);
}