-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathads_hal_i2c.c
281 lines (243 loc) · 6.71 KB
/
ads_hal_i2c.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @file ads_hal_i2c.c
* @author Yazan Abbas (mhd.yazan.abbas3@gmail.com)
* @brief Angular Displacement Sensor
* @version 0.1
* @date 2022-12-09
*
* @copyright Copyright (c) 2022
*
*/
#include "ads_hal.h"
#include <stdint.h>
/* Hardware Specific Includes */
#include <stdio.h>
#include <driver/i2c.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <math.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#define PIN_SDA 18
#define PIN_CLK 19
#define I2C_RX_BUF_DISABLE 0
#define I2C_TX_BUF_DISABLE 0
#define I2C_PORT I2C_NUM_1
#define ACK 0
#define NAK 1
#define ENABLE ACK
static char * TAG = {"ADS"};
static void (*ads_read_callback)(uint8_t *);
static uint8_t read_buffer[ADS_TRANSFER_SIZE];
#define ADS_DEFAULT_ADDR (0x12) // Default I2C address of the ADS one axis sensor
static uint32_t ADS_RESET_PIN = 0;
static uint32_t ADS_INTERRUPT_PIN = 0;
static uint8_t _address = ADS_DEFAULT_ADDR;
volatile bool _ads_int_enabled = false;
/************************************************************************/
/* HAL Stub Functions */
/************************************************************************/
static inline void ads_hal_gpio_pin_write(uint8_t pin, uint8_t val);
static void ads_hal_pin_int_init(void);
static void ads_hal_i2c_init(void);
/**
* @brief ADS data ready interrupt. Reads out packet from ADS and fires
* callback in ads.c
*/
void ads_hal_interrupt(void* arg)
{
if(ads_hal_read_buffer(read_buffer, ADS_TRANSFER_SIZE) == ADS_OK)
ads_read_callback(read_buffer);
}
/**
* @brief Initializes the pin ADS_INTERRUPT_PIN as a falling edge pin change interrupt(1).
* Assign the interrupt service routine as ads_hal_interrupt(2). Enable pullup(3)
* Enable interrupt(4)
*/
static void ads_hal_pin_int_init(void)
{
const gpio_config_t GPIOConfig = {
.pin_bit_mask = ADS_INTERRUPT_PIN,
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,//(3)
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_NEGEDGE,//(1)
};
ESP_ERROR_CHECK( gpio_config(&GPIOConfig) );
ESP_ERROR_CHECK( gpio_intr_enable(ADS_INTERRUPT_PIN) );//(4)
ESP_ERROR_CHECK( gpio_install_isr_service(ESP_INTR_FLAG_LEVEL3) );//(2)
ESP_ERROR_CHECK( gpio_isr_handler_add(ADS_INTERRUPT_PIN, ads_hal_interrupt, (void*) ADS_INTERRUPT_PIN) );//(2)
}
/**
* @brief Write pin to level of val
*/
static inline void ads_hal_gpio_pin_write(uint8_t pin, uint8_t val)
{
ESP_ERROR_CHECK( gpio_set_level(pin, val) );
}
/**
* @brief Millisecond delay routine.
*/
void ads_hal_delay(uint16_t delay_ms)
{
vTaskDelay(delay_ms / portTICK_PERIOD_MS);
}
/**
* @brief Enable/Disable the pin change data ready interrupt
*
* @param enable true = enable, false = disable
*/
void ads_hal_pin_int_enable(bool enable)
{
_ads_int_enabled = enable;
if(enable)
{
ESP_ERROR_CHECK( gpio_intr_enable(ADS_INTERRUPT_PIN) );
ESP_ERROR_CHECK( gpio_isr_handler_add(ADS_INTERRUPT_PIN, ads_hal_interrupt, (void*) ADS_INTERRUPT_PIN) );
}
else
{
ESP_ERROR_CHECK( gpio_isr_handler_remove(ADS_INTERRUPT_PIN) );
ESP_ERROR_CHECK( gpio_intr_disable(ADS_INTERRUPT_PIN) );
}
}
/**
* @brief Configure I2C bus, 7 bit address, 100kHz frequency enable clock stretching
* if available.
*/
static void ads_hal_i2c_init(void)
{
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = PIN_SDA,
.scl_io_num = PIN_CLK,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000,
};
esp_err_t ret = i2c_param_config(I2C_PORT, &conf);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "i2c_param_config failed: %d", ret);
return;
}
ret = i2c_driver_install(
I2C_PORT, //I2C interface number
conf.mode, // device mode (Master / Slave)
I2C_RX_BUF_DISABLE, // Read buffer state (not needed in master mode)
I2C_TX_BUF_DISABLE, // Transmitte buffer state (not needed in master mode)
ESP_INTR_FLAG_LOWMED // Interrupt flags.
);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "i2c_driver_install failed: %d", ret);
return;
}
}
/**
* @brief Write buffer of data to the Angular Displacement Sensor
*
* @param buffer[in] Write buffer
* @param len Length of buffer.
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_hal_write_buffer(uint8_t * buffer, uint8_t len)
{
// Disable the interrupt, if interrupt is enabled
if(_ads_int_enabled)
ads_hal_pin_int_enable(false);
esp_err_t ret = i2c_master_write_to_device(
I2C_PORT,
ADS_DEFAULT_ADDR,
buffer,
len,
1000 / portTICK_PERIOD_MS
);
ESP_ERROR_CHECK(ret);
if(_ads_int_enabled)
{
ads_hal_pin_int_enable(true);
// Read data packet if interrupt was missed
if( gpio_get_level(ADS_INTERRUPT_PIN) == 0)
{
if(ads_hal_read_buffer(read_buffer, ADS_TRANSFER_SIZE) == ADS_OK)
{
ads_read_callback(read_buffer);
}
}
}
return ret;
}
/**
* @brief Read buffer of data from the Angular Displacement Sensor
*
* @param buffer[out] Read buffer
* @param len Length of data to read in number of bytes.
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
esp_err_t ads_hal_read_buffer(uint8_t * buffer, uint8_t len)
{
esp_err_t ret = i2c_master_read_from_device(
I2C_PORT,
ADS_DEFAULT_ADDR,
buffer,
len,
1000 / portTICK_PERIOD_MS
);
ESP_ERROR_CHECK(ret);
return ret;
}
/**
* @brief Reset the Angular Displacement Sensor
*/
void ads_hal_reset(void)
{
// Configure reset line as an output
ESP_ERROR_CHECK( gpio_set_direction(ADS_RESET_PIN, GPIO_MODE_OUTPUT) );
// Bring reset low for 10ms then release
ads_hal_gpio_pin_write(ADS_RESET_PIN, 0);
ads_hal_delay(10);
ads_hal_gpio_pin_write(ADS_RESET_PIN, 1);
}
/**
* @brief Initializes the hardware abstraction layer
*
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_hal_init(void (*callback)(uint8_t*), uint32_t reset_pin, uint32_t datardy_pin)
{
// Copy pin numbers for reset and data ready to local variables
ADS_RESET_PIN = reset_pin;
ADS_INTERRUPT_PIN = datardy_pin;
// Set callback pointer
ads_read_callback = callback;
// Reset the ads
ads_hal_reset();
// Wait for ads to initialize
ads_hal_delay(2000);
// Configure and enable interrupt pin
ads_hal_pin_int_init();
// Initialize the I2C bus
ads_hal_i2c_init();
return ADS_OK;
}
/**
* @brief Gets the current i2c address that the hal layer is addressing.
* Used by device firmware update (dfu)
* @return uint8_t _address
*/
uint8_t ads_hal_get_address(void)
{
return _address;
}
/**
* @brief Sets the i2c address that the hal layer is addressing *
* Used by device firmware update (dfu)
*
* @param address i2c address hal to communicate with
*/
void ads_hal_set_address(uint8_t address)
{
_address = address;
}