-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI2CInterruptSystem.cpp
More file actions
183 lines (158 loc) · 4.98 KB
/
Copy pathI2CInterruptSystem.cpp
File metadata and controls
183 lines (158 loc) · 4.98 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
179
180
181
182
183
/// @copyright Copyright © 2025 ygdstmidn
/// @license This file is released under the MIT License(https://opensource.org/license/mit)
#include "I2CInterruptSystem.h"
namespace I2CInterruptSystem
{
I2CInterruptSystem::I2CInterruptSystem(I2C_HandleTypeDef *hi2c)
{
_hi2c = hi2c;
}
I2CInterruptSystem::~I2CInterruptSystem(void)
{
}
void I2CInterruptSystem::send(void)
{
if (_DataQueueIndex >= _DataQueue.size())
{
_DataQueueIndex = 0;
}
if (__HAL_I2C_GET_FLAG(_hi2c, I2C_FLAG_BUSY))
{//error 多分断線
_state = 2;//もう一度リセット
return;
}
I2CInterruptSystemData &data = _DataQueue[_DataQueueIndex];
HAL_StatusTypeDef status;
switch (data.Type)
{
case MasterTransmit:
status = HAL_I2C_Master_Transmit_IT(_hi2c, data.DevAddress, data.pData, data.Size);
break;
case MasterReceive:
status = HAL_I2C_Master_Receive_IT(_hi2c, data.DevAddress, data.pData, data.Size);
break;
case MemWrite:
status = HAL_I2C_Mem_Write_IT(_hi2c, data.DevAddress, data.MemAddress, data.MemAddSize, data.pData, data.Size);
break;
case MemRead:
status = HAL_I2C_Mem_Read_IT(_hi2c, data.DevAddress, data.MemAddress, data.MemAddSize, data.pData, data.Size);
break;
default://ありえないはず
break;
}
if (status == HAL_OK)
{
data.sendFlag++;
_DataQueueIndex++;
}
else
{
_state = 2; // エラーが発生したので次はリセット
}
}
void I2CInterruptSystem::reset(void)
{
HAL_I2C_DeInit(_hi2c);
HAL_I2C_Init(_hi2c);
}
void I2CInterruptSystem::start(void)
{
_startFlag = 1; // 開始フラグ
_state = 1;
}
/// @note 現在送信中のデータはキャンセルされません
void I2CInterruptSystem::stop(void)
{
_startFlag = 0; // 停止フラグ
}
void I2CInterruptSystem::loop(void)
{
if (_startFlag && _state == 1)
{
_state = 0;
send();
}
else if (_startFlag && _state == 2)
{
_state = 1; // 次はsend
reset();
}
}
int I2CInterruptSystem::sendCheck(const unsigned int index)
{
if (index < _DataQueue.size())
{
const int sendFlag = _DataQueue[index].sendFlag;
_DataQueue[index].sendFlag = 0; // 送信フラグをリセット
return sendFlag;
}
return -1; // 無効なインデックス
}
unsigned int I2CInterruptSystem::addMasterTransmit(const uint16_t DevAddress, uint8_t *pData, const uint16_t Size)
{
_DataQueue.push_back({MasterTransmit, DevAddress, pData, Size, 0, 0});
return _DataQueue.size() - 1;
}
unsigned int I2CInterruptSystem::addMasterReceive(const uint16_t DevAddress, uint8_t *pData, const uint16_t Size)
{
_DataQueue.push_back({MasterReceive, DevAddress, pData, Size, 0, 0});
return _DataQueue.size() - 1;
}
unsigned int I2CInterruptSystem::addMemWrite(const uint16_t DevAddress, const uint16_t MemAddress, const uint16_t MemAddSize, uint8_t *pData, const uint16_t Size)
{
_DataQueue.push_back({MemWrite, DevAddress, pData, Size, MemAddress, MemAddSize});
return _DataQueue.size() - 1;
}
unsigned int I2CInterruptSystem::addMemRead(const uint16_t DevAddress, const uint16_t MemAddress, const uint16_t MemAddSize, uint8_t *pData, const uint16_t Size)
{
_DataQueue.push_back({MemRead, DevAddress, pData, Size, MemAddress, MemAddSize});
return _DataQueue.size() - 1;
}
void I2CInterruptSystem::MasterTxCpltCallback(const I2C_HandleTypeDef *hi2c)
{
if (hi2c == _hi2c)
{
if (_startFlag)
{
_state = 1; // 次はsend
}
}
}
void I2CInterruptSystem::MasterRxCpltCallback(const I2C_HandleTypeDef *hi2c)
{
if (hi2c == _hi2c)
{
if (_startFlag)
{
_state = 1; // 次はsend
}
}
}
void I2CInterruptSystem::MemTxCpltCallback(const I2C_HandleTypeDef *hi2c)
{
if (hi2c == _hi2c)
{
if (_startFlag)
{
_state = 1; // 次はsend
}
}
}
void I2CInterruptSystem::MemRxCpltCallback(const I2C_HandleTypeDef *hi2c)
{
if (hi2c == _hi2c)
{
if (_startFlag)
{
_state = 1; // 次はsend
}
}
}
void I2CInterruptSystem::ErrorCallback(const I2C_HandleTypeDef *hi2c)
{
if (hi2c == _hi2c)
{
_state = 2; // 次はリセット
}
}
} // namespace I2CInterruptSystem