forked from gschorcht/lis3mdl-esp-idf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266_wrapper.c
194 lines (157 loc) · 5.23 KB
/
esp8266_wrapper.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Wrapper module for source code compatibility with esp-open-rtos.
*/
#ifdef ESP_PLATFORM // ESP32 (ESP-IDF)
#include <sys/time.h>
#include <string.h>
#include "driver/spi_master.h"
#include "driver/spi_common.h"
#include "driver/i2c.h"
#include "driver/gpio.h"
#include "esp8266_wrapper.h"
// esp-open-rtos SDK function wrapper
uint32_t sdk_system_get_time ()
{
struct timeval time;
gettimeofday(&time,0);
return time.tv_sec*1e6 + time.tv_usec;
}
bool gpio_isr_service_installed = false;
bool auto_pull_up = false;
bool auto_pull_down = true;
esp_err_t gpio_set_interrupt(gpio_num_t gpio,
gpio_int_type_t type,
gpio_interrupt_handler_t handler)
{
if (!gpio_isr_service_installed)
gpio_isr_service_installed = (gpio_install_isr_service(0) == ESP_OK);
gpio_config_t gpio_cfg = {
.pin_bit_mask = ((uint64_t)(((uint64_t)1)<< gpio)),
.mode = GPIO_MODE_INPUT,
.pull_up_en = auto_pull_up,
.pull_down_en = auto_pull_down,
.intr_type = type
};
gpio_config(&gpio_cfg);
// set interrupt handler
gpio_isr_handler_add(gpio, (gpio_isr_t)handler, (void*)gpio);
return ESP_OK;
}
void gpio_enable (gpio_num_t gpio, const gpio_mode_t mode)
{
gpio_config_t gpio_cfg = {
.pin_bit_mask = ((uint64_t)(((uint64_t)1)<< gpio)),
.mode = mode,
.pull_up_en = auto_pull_up,
.pull_down_en = auto_pull_down,
};
gpio_config(&gpio_cfg);
}
// esp-open-rtos I2C interface wrapper
#define I2C_ACK_VAL 0x0
#define I2C_NACK_VAL 0x1
void i2c_init (int bus, gpio_num_t scl, gpio_num_t sda, uint32_t freq)
{
i2c_config_t conf;
memset(&conf, 0, sizeof(conf));
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = sda;
conf.scl_io_num = scl;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = freq;
i2c_param_config(bus, &conf);
i2c_driver_install(bus, I2C_MODE_MASTER, 0, 0, 0);
}
int i2c_slave_write (uint8_t bus, uint8_t addr, const uint8_t *reg,
uint8_t *data, uint32_t len)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, addr << 1 | I2C_MASTER_WRITE, true);
if (reg)
i2c_master_write_byte(cmd, *reg, true);
if (data)
i2c_master_write(cmd, data, len, true);
i2c_master_stop(cmd);
esp_err_t err = i2c_master_cmd_begin(bus, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return err;
}
int i2c_slave_read (uint8_t bus, uint8_t addr, const uint8_t *reg,
uint8_t *data, uint32_t len)
{
if (len == 0) return true;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
if (reg)
{
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( addr << 1 ) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, *reg, true);
if (!data)
i2c_master_stop(cmd);
}
if (data)
{
i2c_master_start(cmd);
i2c_master_write_byte(cmd, ( addr << 1 ) | I2C_MASTER_READ, true);
if (len > 1) i2c_master_read(cmd, data, len-1, I2C_ACK_VAL);
i2c_master_read_byte(cmd, data + len-1, I2C_NACK_VAL);
i2c_master_stop(cmd);
}
esp_err_t err = i2c_master_cmd_begin(bus, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
return err;
}
// esp-open-rtos SPI interface wrapper
#define SPI_MAX_BUS 3 // ESP32 features three SPIs (SPI_HOST, HSPI_HOST and VSPI_HOST)
#define SPI_MAX_CS 34 // GPIO 33 is the last port that can be used as output
spi_device_handle_t spi_handles[SPI_MAX_CS] = { 0 };
bool spi_bus_init (spi_host_device_t host, uint8_t sclk , uint8_t miso, uint8_t mosi)
{
spi_bus_config_t spi_bus_cfg = {
.miso_io_num=miso,
.mosi_io_num=mosi,
.sclk_io_num=sclk,
.quadwp_io_num=-1,
.quadhd_io_num=-1
};
return (spi_bus_initialize(host, &spi_bus_cfg, 1) == ESP_OK);
}
bool spi_device_init (uint8_t bus, uint8_t cs)
{
if (bus >= SPI_MAX_BUS || cs >= SPI_MAX_CS)
return false;
if ((spi_handles[cs] = malloc (sizeof(spi_device_handle_t))) == 0)
return false;
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 1e6, // 1 MHz clock
.mode = 0, // SPI mode 0
.spics_io_num = cs, // CS GPIO
.queue_size = 1,
.flags = 0, // no flags set
.command_bits = 0, // no command bits used
.address_bits = 0, // register address is first byte in MOSI
.dummy_bits = 0 // no dummy bits used
};
if (spi_bus_add_device(bus, &dev_cfg, &(spi_handles[cs])) != ESP_OK)
{
free (spi_handles[cs]);
return false;
}
return true;
}
size_t spi_transfer_pf (uint8_t bus, uint8_t cs, const uint8_t *mosi, uint8_t *miso, uint16_t len)
{
spi_transaction_t spi_trans;
if (cs >= SPI_MAX_CS)
return 0;
memset(&spi_trans, 0, sizeof(spi_trans)); // zero out spi_trans;
spi_trans.tx_buffer = mosi;
spi_trans.rx_buffer = miso;
spi_trans.length=len*8;
if (spi_device_transmit(spi_handles[cs], &spi_trans) != ESP_OK)
return 0;
return len;
}
#endif // ESP32 (ESP-IDF)