/ MCUME_esp32 / espboot / components / Wire / esp32-hal-i2c.h
esp32-hal-i2c.h
 1  // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
 2  //
 3  // Licensed under the Apache License, Version 2.0 (the "License");
 4  // you may not use this file except in compliance with the License.
 5  // You may obtain a copy of the License at
 6  
 7  //     http://www.apache.org/licenses/LICENSE-2.0
 8  //
 9  // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  // modified Nov 2017 by Chuck Todd <StickBreaker> to support Interrupt Driven I/O
15  
16  #ifndef _ESP32_HAL_I2C_H_
17  #define _ESP32_HAL_I2C_H_
18  
19  #ifdef __cplusplus
20  extern "C" {
21  #endif
22  
23  #include <stdint.h>
24  #include <stdbool.h>
25  #include "freertos/FreeRTOS.h"
26  #include "freertos/event_groups.h"
27  
28  // External Wire.h equivalent error Codes
29  typedef enum {
30      I2C_ERROR_OK=0,
31      I2C_ERROR_DEV,
32      I2C_ERROR_ACK,
33      I2C_ERROR_TIMEOUT,
34      I2C_ERROR_BUS,
35      I2C_ERROR_BUSY,
36      I2C_ERROR_MEMORY,
37      I2C_ERROR_CONTINUE,
38      I2C_ERROR_NO_BEGIN
39  } i2c_err_t;
40  
41  struct i2c_struct_t;
42  typedef struct i2c_struct_t i2c_t;
43  
44  i2c_t * i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t clk_speed);
45  void i2cRelease(i2c_t *i2c); // free ISR, Free DQ, Power off peripheral clock.  Must call i2cInit() to recover
46  i2c_err_t i2cWrite(i2c_t * i2c, uint16_t address, uint8_t* buff, uint16_t size, bool sendStop, uint16_t timeOutMillis);
47  i2c_err_t i2cRead(i2c_t * i2c, uint16_t address, uint8_t* buff, uint16_t size, bool sendStop, uint16_t timeOutMillis, uint32_t *readCount);
48  i2c_err_t i2cFlush(i2c_t *i2c);
49  i2c_err_t i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed);
50  uint32_t i2cGetFrequency(i2c_t * i2c);
51  uint32_t i2cGetStatus(i2c_t * i2c); // Status register of peripheral
52  
53  //Functions below should be used only if well understood
54  //Might be deprecated and removed in future
55  i2c_err_t i2cAttachSCL(i2c_t * i2c, int8_t scl);
56  i2c_err_t i2cDetachSCL(i2c_t * i2c, int8_t scl);
57  i2c_err_t i2cAttachSDA(i2c_t * i2c, int8_t sda);
58  i2c_err_t i2cDetachSDA(i2c_t * i2c, int8_t sda);
59  
60  //Stickbreakers ISR Support
61  i2c_err_t i2cProcQueue(i2c_t *i2c, uint32_t *readCount, uint16_t timeOutMillis);
62  i2c_err_t i2cAddQueueWrite(i2c_t *i2c, uint16_t i2cDeviceAddr, uint8_t *dataPtr, uint16_t dataLen, bool SendStop, EventGroupHandle_t event);
63  i2c_err_t i2cAddQueueRead(i2c_t *i2c, uint16_t i2cDeviceAddr, uint8_t *dataPtr, uint16_t dataLen, bool SendStop, EventGroupHandle_t event);
64  
65  //stickbreaker debug support
66  uint32_t i2cDebug(i2c_t *, uint32_t setBits, uint32_t resetBits);
67  //  Debug actions have 3 currently defined locus 
68  // 0xXX------ : at entry of ProcQueue 
69  // 0x--XX---- : at exit of ProcQueue
70  // 0x------XX : at entry of Flush
71  // 
72  // bit 0 causes DumpI2c to execute 
73  // bit 1 causes DumpInts to execute
74  // bit 2 causes DumpCmdqueue to execute
75  // bit 3 causes DumpStatus to execute
76  // bit 4 causes DumpFifo to execute
77  
78  #ifdef __cplusplus
79  }
80  #endif
81  
82  #endif /* _ESP32_HAL_I2C_H_ */