Arduino Core for STM32  1.0
HardwareI2C.h
Go to the documentation of this file.
1 
14 #ifndef HARDWAREI2C_h
15 #define HARDWAREI2C_h
16 
17 //Includes
18 
19 #if defined(STM32F103xB)
20 //#include "stm32f1xx_hal.h"
21 
22 #define IS_I2C_INSTANCE_ON_APB1(_instance_) ((_instance_) == I2C2)
23 #define IS_I2C_INSTANCE_ON_APB2(_instance_) ((_instance_) == I2C1)
24 
25 #elif defined(STM32F031x6) || defined(STM32F070xB) || defined(STM32F070x6) || defined(STM32F030x6)
26 //#include "stm32f0xx_hal.h"
27 
28 //#define IS_I2C_INSTANCE_ON_APB2(_instance_)
29 #define IS_I2C_INSTANCE_ON_APB1(_instance_) ((_instance_) == I2C1)
30 
31 #elif defined(STM32L031xx)
32 //#include "stm32l0xx_hal.h"
33 
34 //#define IS_I2C_INSTANCE_ON_APB1(_instance_)
35 #define IS_I2C_INSTANCE_ON_APB2(_instance_) ((_instance_) == I2C1)
36 
37 #elif defined(STM32L432xx)
38 //#include "stm32l4xx_hal.h"
39 
40 
41 #define IS_I2C_INSTANCE_ON_APB1(_instance_) ((_instance_) == I2C3)
42 #define IS_I2C_INSTANCE_ON_APB2(_instance_) ((_instance_) == I2C1)
43 
44 #else
45 #error "Definitions for your MCU are not defined."
46 #endif
47 
48 #include "wirish.h"
49 #include "Stream.h"
50 
51 
52 #ifndef I2C_BUFFER_LENGTH
53 // DEPRECATED: Do not use BUFFER_LENGTH, prefer I2C_BUFFER_LENGTH
54 #define BUFFER_LENGTH 128
55 #define I2C_BUFFER_LENGTH BUFFER_LENGTH
56 #endif
57 
58 
59 #if defined(HAL_I2C_MODULE_ENABLED) || defined(DOXYGEN_FORCED)
60 
61 class HardwareI2C : public Stream
62 {
63 private:
64  #define MAX_NBYTE_SIZE 255U
65  #define RX_BUFF_SIZE 64
66  #define I2C_TIMEOUT_STOPF (25U)
67  #define I2C_TIMEOUT_BUSY (25U)
68  //#define I2C_FLAG_BUSY I2C_ISR_BUSY
69 
70  I2C_HandleTypeDef *hi2c;
71 
72  uint8_t rxBuffer[RX_BUFF_SIZE];
73  size_t rxBufferIndex = 0;
74  size_t rxBufferLength = 0;
75  uint32_t timeout= 50;
76  HAL_I2C_ModeTypeDef I2CMode = HAL_I2C_MODE_NONE;
77 
78  uint8_t _status = 0;
79  uint16_t txAddress = 0;
80  bool isBusReleased = true;
81  uint8_t transmitting = 0;
82  bool anyByteSent= false;
83 
84  void (*user_onRequest)(void) = NULL;
85  void (*user_onReceive)(size_t) = NULL;
86  void onRequestService(void);
87  void onReceiveService(uint8_t*, size_t);
88 
89  size_t writePriv(uint8_t data);
90 
91  HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout,uint32_t Tickstart);
92  HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
93 
94  /* Private function to handle start, restart or stop a transfer */
95  void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode,uint32_t Request);
96 
97  /* Private functions to handle flags during polling transfer */
98  HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
99 
100  HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
101 
102  HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
103 
104  void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c);
105 public:
106  // Constructors ////////////////////////////////////////////////////////////////
107  HardwareI2C(I2C_HandleTypeDef& hi2c_) : hi2c(&hi2c_){}
108 
109 
114  inline bool instanceExists(void){
115  return (hi2c != NULL) && (IS_I2C_ALL_INSTANCE(hi2c->Instance));
116  }
117 
118  inline bool isMaster(void){
119  return hi2c->Mode == HAL_I2C_MODE_MASTER || hi2c->Mode == HAL_I2C_MODE_MEM;
120  }
121 
122  inline void setTimeout(uint32_t timeout_ ){
123  timeout = timeout_;
124  }
125 
126  inline uint32_t errorCode(void){
127  return hi2c->ErrorCode;
128  }
129 
130  bool begin(uint8_t adress1,uint8_t adress2 = 0);
131  bool begin();
132  bool end();
133 
134 
135  void setClock(uint32_t);
136  void setClockStretchLimit(uint32_t);
137  bool beginTransmission(uint16_t address, uint32_t bytesCountToSend);
138  bool beginTransmission(int address, uint32_t bytesCountToSend);
139  uint8_t endTransmission(void);
140  uint8_t endTransmission(uint8_t);
141  uint8_t requestFrom(uint8_t address, size_t size, bool sendStop);
142  uint8_t status();
143 
144  uint8_t requestFrom(uint8_t, uint8_t);
145  uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
146  uint8_t requestFrom(int, int);
147  uint8_t requestFrom(int, int, int);
148 
149 
150  size_t write(uint8_t)override;
151  size_t write(const uint8_t *, size_t)override;
152 
153  int available(void)override;
154  int read(void)override;
155  int peek(void)override;
156 
157  void flush(void);
158  void onReceive(void (*function)(int));
159  void onReceive(void (*function)(size_t));
160 
161  void onRequest(void (*)(void));
162 
163  //using Print::write;
164 
165 
166 };
167 
168 #endif // HAL_I2C_MODULE_ENABLED || DOXYGEN_FORCED
169 #endif // HARDWAREI2C_h
HardwareI2C::onReceive
void onReceive(void(*function)(int))
Definition: HardwareI2C.cpp:539
HardwareI2C::onRequest
void onRequest(void(*)(void))
Definition: HardwareI2C.cpp:547
HardwareI2C::available
int available(void) override
Checks how many bytes are available in receiving buffer.
Definition: HardwareI2C.cpp:507
HardwareI2C::endTransmission
uint8_t endTransmission(void)
Definition: HardwareI2C.cpp:411
RX_BUFF_SIZE
#define RX_BUFF_SIZE
Definition: HardwareI2C.h:65
HardwareI2C::errorCode
uint32_t errorCode(void)
Definition: HardwareI2C.h:126
HardwareI2C::setTimeout
void setTimeout(uint32_t timeout_)
Definition: HardwareI2C.h:122
HardwareI2C::peek
int peek(void) override
Reads one byte from receiving buffer and without removing it from buffer.
Definition: HardwareI2C.cpp:523
Stream
This class is for receiving and transmitting data.
Definition: Stream.h:56
HardwareI2C::begin
bool begin()
Definition: HardwareI2C.cpp:13
HardwareI2C::requestFrom
uint8_t requestFrom(uint8_t address, size_t size, bool sendStop)
Definition: HardwareI2C.cpp:69
HardwareI2C
Definition: HardwareI2C.h:61
HardwareI2C::write
size_t write(uint8_t) override
Writes one unsigned byte (character).
Definition: HardwareI2C.cpp:469
HardwareI2C::isMaster
bool isMaster(void)
Definition: HardwareI2C.h:118
wirish.h
This file contains basic (Arduino like) GPIO manipulation functions, delay, random,...
HardwareI2C::setClock
void setClock(uint32_t)
Definition: HardwareI2C.cpp:59
HardwareI2C::instanceExists
bool instanceExists(void)
Checks if I2C instance exists.
Definition: HardwareI2C.h:114
HardwareI2C::status
uint8_t status()
Definition: HardwareI2C.cpp:50
HardwareI2C::HardwareI2C
HardwareI2C(I2C_HandleTypeDef &hi2c_)
Definition: HardwareI2C.h:107
HardwareI2C::flush
void flush(void)
Definition: HardwareI2C.cpp:533
HardwareI2C::setClockStretchLimit
void setClockStretchLimit(uint32_t)
Definition: HardwareI2C.cpp:64
HardwareI2C::beginTransmission
bool beginTransmission(uint16_t address, uint32_t bytesCountToSend)
Definition: HardwareI2C.cpp:261
HardwareI2C::read
int read(void) override
Reads one byte from receiving buffer and removes it from buffer.
Definition: HardwareI2C.cpp:512
HardwareI2C::end
bool end()
Definition: HardwareI2C.cpp:41
Stream.h
Base class for character-based streams.