-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHardware.cpp
More file actions
178 lines (154 loc) · 4.78 KB
/
Copy pathHardware.cpp
File metadata and controls
178 lines (154 loc) · 4.78 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Copyright © 2019, 2020, 2021, 2022 HackEDA, Inc.
Licensed under the WiPhone Public License v.1.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at
https://wiphone.io/WiPhone_Public_License_v1.0.txt.
Unless required by applicable law or agreed to in writing, software,
hardware or documentation 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 "Hardware.h"
#include "driver/rmt.h"
#if GPIO_EXTENDER == 1509
// GPIO extender used from WiPhone ver.1.4 onward
rmt_item32_t rmtTxItems[] = {
// Pull down for >500 us to turn OFF
{{{ 1, 1, 550, 0 }}},
// Three pulses of: 5 us ON, 5 us OFF
{{{ 5, 1, 5, 0 }}},
//amplifier will run in mode 1 with NCN-ON
// {{{ 5, 1, 5, 0 }}},
// {{{ 5, 1, 5, 0 }}},
// End marker
{{{ 0, 1, 0, 0 }}}
};
SX1509 gpioExtender;
void allPinMode(int16_t pin, int16_t mode) {
if (!(pin & EXTENDER_FLAG) && pin >= 0) {
pinMode(pin, mode);
} else {
gpioExtender.pinMode(pin ^ EXTENDER_FLAG, mode);
}
}
bool allDigitalWrite(int16_t pin, int16_t val) {
if (pin < 0) {
return true;
}
if (pin & EXTENDER_FLAG) {
gpioExtender.digitalWrite(pin ^ EXTENDER_FLAG, val);
} else {
digitalWrite(pin, val);
}
return true;
}
int allDigitalRead(uint8_t pin) {
if (pin & EXTENDER_FLAG) {
return gpioExtender.digitalRead(pin ^ EXTENDER_FLAG);
} else {
return digitalRead(pin);
}
}
/* analogWrite values from 0 to 255 */
void allAnalogWrite(byte pin, byte val) {
if (pin & EXTENDER_FLAG) {
return gpioExtender.analogWrite(pin ^ EXTENDER_FLAG, val);
} else {
log_e("not implemented");
// TODO: do this for ESP32
//return analogWrite(pin, val);
}
}
void lcdLedOnOff(bool turnOn) {
#if LCD_LED_PIN >= 0
#ifdef LCD_INVERTED_LED
byte val = turnOn ? 255 : 0; // It's unclear why doesn't it require inversion; TODO
#else
byte val = turnOn ? 0 : 255;
#endif
allAnalogWrite(LCD_LED_PIN, val);
#endif
}
void lcdLedOnOff(bool turnOn, uint8_t value) {
#if LCD_LED_PIN >= 0
#ifdef LCD_INVERTED_LED
byte val = turnOn ? value : 0; // It's unclear why doesn't it require inversion; TODO
#else
byte val = turnOn ? 0 : value;
#endif
log_d("LCD LED = %d", val);
allAnalogWrite(LCD_LED_PIN, val);
#endif
}
#else
#if GPIO_EXTENDER == 7325
SN7325 gpioExtender(SN7325_I2C_ADDR_BASE + 1, I2C_SDA_PIN, I2C_SCK_PIN);
void allPinMode(int16_t pin, int16_t mode) {
if (!(pin & EXTENDER_FLAG) && pin >= 0) {
pinMode(pin, mode);
}
// TODO: modify extender registers here
}
bool allDigitalWrite(int16_t pin, int16_t val) {
if (pin < 0) {
return true;
}
if (pin & EXTENDER_FLAG) {
return gpioExtender.digitalWrite(pin, val) == SN7325_ERROR_OK;
} else {
digitalWrite(pin, val);
return true;
}
}
void lcdLedOnOff(bool turnOn) {
#if LCD_LED_PIN >= 0
#ifdef LCD_INVERTED_LED
int val = turnOn ? LOW : HIGH;
#else
int val = turnOn ? HIGH : LOW;
#endif
if (!allDigitalWrite(LCD_LED_PIN, val)) {
log_e("backlight error");
}
#endif
}
#endif // GPIO_EXTENDER == 7325
#endif // GPIO_EXTENDER == 1509
/*
* Description:
* Initializes the RMT peripheral transmit channel.
* Based on the example:
* https://github.com/espressif/esp-idf/blob/6fe853a2c73437f74c0e6e79f9b15db68b231d32/examples/peripherals/rmt_tx/main/rmt_tx_main.c
*/
void rmtTxInit(int16_t rmtPin, bool idleHigh) {
log_v("pin = %d, idle_level = %d", rmtPin, idleHigh);
rmt_config_t config;
// Common parameters
config.rmt_mode = RMT_MODE_TX;
config.channel = RMT_TX_CHANNEL;
config.gpio_num = (gpio_num_t)rmtPin;
config.mem_block_num = 1;
config.clk_div = 80; // 80 MHz / 80 = 1 MHz (tick = 1 us)
// TX-specific parameters
config.tx_config.loop_en = 0; // disable looping
config.tx_config.carrier_en = 0; // disable carrier
config.tx_config.idle_output_en = 1; // enable idle output
config.tx_config.idle_level = idleHigh ? RMT_IDLE_LEVEL_HIGH : RMT_IDLE_LEVEL_LOW;
config.tx_config.carrier_duty_percent = 50;
config.tx_config.carrier_freq_hz = 1000;
config.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
}
void amplifierEnable(int level) {
#if GPIO_EXTENDER == 1509
if (level) { // level from 1 (12 dB) to 4 (27.5 dB)
rmt_set_idle_level(RMT_TX_CHANNEL, 1, RMT_IDLE_LEVEL_HIGH);
rmt_write_items(RMT_TX_CHANNEL, rmtTxItems, sizeof(rmtTxItems)/sizeof(*rmtTxItems), true);
} else {
rmt_set_idle_level(RMT_TX_CHANNEL, 1, RMT_IDLE_LEVEL_LOW);
}
#endif
}