Skip to content

Commit 6eb0560

Browse files
committed
updated ist8308 driver
1 parent 2bb1e99 commit 6eb0560

File tree

4 files changed

+73
-123
lines changed

4 files changed

+73
-123
lines changed

boards/varmint_h7/common/drivers/Ist8308.cpp

Lines changed: 66 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,24 @@
4141

4242
extern Time64 time64;
4343

44-
#define ROLLOVER 10000
44+
#define ROLLOVER_US 10000
4545

46-
#define IST8308_STATE_READING 0
47-
#define IST8308_STATE_ACQ_CMD 1
48-
#define IST8308_STATE_WAITING 2
46+
//#define IST8308_STATE_READING 0
47+
//#define IST8308_STATE_ACQ_CMD 1
48+
//#define IST8308_STATE_WAITING 2
49+
50+
#define IST8308_STATE_IDLE 0
51+
#define IST8308_STATE_POLLING 1
52+
#define IST8308_STATE_READ_CMD 2
53+
#define IST8308_STATE_DATA_READ 3
54+
#define IST8308_STATE_MEASURE_CMD 4
55+
#define IST8308_STATE_ERROR 5
4956

5057
//#define IST8308_CMD 0
5158
//#define IST8308_TX 83 //89
5259
//#define IST8308_RX 92
53-
#define IST8308_STATE_ERROR 0xFFFF
54-
#define IST8308_IDLE_STATE 0xFFFF
60+
//#define IST8308_STATE_ERROR 0xFFFF
61+
//#define IST8308_IDLE_STATE 0xFFFF
5562

5663
DMA_RAM uint8_t ist8308_i2c_dma_buf[I2C_DMA_MAX_BUFFER_SIZE];
5764
DTCM_RAM uint8_t ist8308_double_buffer[2 * sizeof(MagPacket)];
@@ -124,7 +131,7 @@ uint32_t Ist8308::init(
124131
hi2c_ = hi2c;
125132
address_ = i2c_address << 1;
126133

127-
i2cState_ = IST8308_IDLE_STATE;
134+
i2cState_ = IST8308_STATE_IDLE;
128135
//dmaRunning_ = false;
129136

130137
double_buffer_.init(ist8308_double_buffer, sizeof(ist8308_double_buffer));
@@ -204,53 +211,46 @@ uint32_t Ist8308::init(
204211

205212
bool Ist8308::poll(uint64_t poll_counter)
206213
{
207-
PollingState poll_state = (PollingState) (poll_counter % (ROLLOVER / POLLING_PERIOD_US));
214+
uint16_t poll_state = (poll_counter % (ROLLOVER_US / POLLING_PERIOD_US));
208215

209216
if(poll_state == 0) {
210-
// Read previous data.
211-
startReadData();
217+
i2cState_ = IST8308_STATE_POLLING;
218+
stateMachine();
212219
}
213-
return false;
220+
return true;
214221
}
215222

216-
// if (poll_state == IST8308_CMD) {
217-
//// drdy_ = time64.Us();
218-
// ist8308_i2c_dma_buf[0] = CNTL2_REG;
219-
// ist8308_i2c_dma_buf[1] = CNTL2_VAL_SINGLE_MODE;
220-
//
221-
// if ((dmaRunning_ = (HAL_OK == HAL_I2C_Master_Transmit_DMA(hi2c_, address_, ist8308_i2c_dma_buf, 2))))
222-
// i2cState_ = poll_state;
223-
// else i2cState_ = IST8308_STATE_ERROR;
224-
// }
225-
// else if (poll_state == IST8308_TX) // Write the register we want to read
226-
// {
227-
// drdy_ = time64.Us();
228-
// if (HAL_I2C_Mem_Read_DMA(hi2c_, address_, STAT1_REG, I2C_MEMADD_SIZE_8BIT, ist8308_i2c_dma_buf,7) == HAL_OK)
229-
// {
230-
// i2cState_ = poll_state;
231-
// } else {
232-
// i2cState_ = IST8308_STATE_ERROR;
233-
// }
234-
// }
235-
// return dmaRunning_;
236-
//}
237-
238-
239-
void Ist8308::startReadData(void) {
223+
void Ist8308::stateMachine(void)
224+
{
225+
switch(i2cState_)
226+
{
227+
case IST8308_STATE_IDLE:
228+
{
229+
i2cState_ = IST8308_STATE_ERROR;
230+
break;
231+
}
232+
case IST8308_STATE_POLLING:
233+
{
240234
drdy_ = time64.Us();
241-
if (HAL_I2C_Mem_Read_DMA(hi2c_, address_, STAT1_REG, I2C_MEMADD_SIZE_8BIT, ist8308_i2c_dma_buf,7) == HAL_OK)
235+
ist8308_i2c_dma_buf[0] = STAT1_REG;
236+
if (HAL_I2C_Master_Transmit_DMA(hi2c_, address_, ist8308_i2c_dma_buf, 1)==HAL_OK) {
237+
i2cState_ = IST8308_STATE_READ_CMD;
238+
} else {
239+
i2cState_ = IST8308_STATE_ERROR;
240+
}
241+
break;
242+
}
243+
case IST8308_STATE_READ_CMD:
242244
{
243-
i2cState_ = IST8308_STATE_READING;
245+
if (HAL_I2C_Master_Receive_DMA(hi2c_, address_, ist8308_i2c_dma_buf,7) == HAL_OK) {
246+
i2cState_ = IST8308_STATE_DATA_READ;
244247
} else {
245248
i2cState_ = IST8308_STATE_ERROR;
246249
}
250+
break;
247251
}
248-
249-
250-
void Ist8308::endRxDma(void)
252+
case IST8308_STATE_DATA_READ:
251253
{
252-
if (i2cState_ == IST8308_STATE_READING) {
253-
254254
MagPacket p;
255255
p.header.timestamp = drdy_;
256256
p.header.complete = time64.Us();
@@ -271,86 +271,36 @@ void Ist8308::endRxDma(void)
271271

272272
rotate(p.flux);
273273
write((uint8_t *) &p, sizeof(p));
274-
startAcq();
274+
275+
ist8308_i2c_dma_buf[0] = CNTL2_REG;
276+
ist8308_i2c_dma_buf[1] = CNTL2_VAL_SINGLE_MODE;
277+
278+
if (HAL_OK == HAL_I2C_Master_Transmit_DMA(hi2c_, address_, ist8308_i2c_dma_buf, 2)) {
279+
i2cState_ = IST8308_STATE_MEASURE_CMD;
280+
} else {
281+
i2cState_ = IST8308_STATE_ERROR;
282+
}
275283
return;
276284
}
285+
break;
277286
}
287+
case IST8308_STATE_MEASURE_CMD:
288+
{
289+
i2cState_ = IST8308_STATE_IDLE;
290+
break;
291+
}
292+
case IST8308_STATE_ERROR:
293+
{
294+
break;
295+
}
296+
default:
297+
{
278298
i2cState_ = IST8308_STATE_ERROR;
279-
//dmaRunning_ = false;
280-
}
281-
282-
void Ist8308::startAcq(void) {
283-
ist8308_i2c_dma_buf[0] = CNTL2_REG;
284-
ist8308_i2c_dma_buf[1] = CNTL2_VAL_SINGLE_MODE;
285-
286-
if (HAL_OK == HAL_I2C_Master_Transmit_DMA(hi2c_, address_, ist8308_i2c_dma_buf, 2))
287-
i2cState_ = IST8308_STATE_ACQ_CMD;
288-
else i2cState_ = IST8308_STATE_ERROR;
289-
}
290-
void Ist8308::endTxDma(void) {
291-
i2cState_ = IST8308_STATE_WAITING;
299+
break;
300+
}
301+
}
292302
}
293303

294-
295-
//bool Ist8308::poll(uint64_t poll_counter)
296-
//{
297-
// PollingState poll_state = (PollingState) (poll_counter % (ROLLOVER / POLLING_PERIOD_US));
298-
// if (poll_state == IST8308_CMD) {
299-
//// drdy_ = time64.Us();
300-
// ist8308_i2c_dma_buf[0] = CNTL2_REG;
301-
// ist8308_i2c_dma_buf[1] = CNTL2_VAL_SINGLE_MODE;
302-
//
303-
// if ((dmaRunning_ = (HAL_OK == HAL_I2C_Master_Transmit_DMA(hi2c_, address_, ist8308_i2c_dma_buf, 2))))
304-
// i2cState_ = poll_state;
305-
// else i2cState_ = IST8308_STATE_ERROR;
306-
// } else if (poll_state == IST8308_TX) // Write the register we want to read
307-
// {
308-
// drdy_ = time64.Us();
309-
// ist8308_i2c_dma_buf[0] = STAT1_REG;
310-
// if ((dmaRunning_ = (HAL_OK == HAL_I2C_Master_Transmit_DMA(hi2c_, address_, ist8308_i2c_dma_buf, 1))))
311-
// i2cState_ = poll_state;
312-
// else i2cState_ = IST8308_STATE_ERROR;
313-
// } else if (poll_state == IST8308_RX) {
314-
// if ((dmaRunning_ = (HAL_OK == HAL_I2C_Master_Receive_DMA(hi2c_, address_, ist8308_i2c_dma_buf, 7))))
315-
// i2cState_ = poll_state;
316-
// else i2cState_ = IST8308_STATE_ERROR;
317-
// }
318-
// return dmaRunning_;
319-
//}
320-
321-
//void Ist8308::endDma(void)
322-
//{
323-
// // if(i2cState_ == IST8308_CMD) {} // do nothing
324-
// // if(i2cState_ == IST8308_TX) {} // do nothing
325-
// // else
326-
// if (i2cState_ == IST8308_RX) {
327-
// MagPacket p;
328-
// p.header.timestamp = drdy_;
329-
// p.header.complete = time64.Us();
330-
// p.header.status = ist8308_i2c_dma_buf[0];
331-
//
332-
// if (p.header.status == STAT1_VAL_DRDY)
333-
// {
334-
// p.temperature = 0;
335-
//
336-
// int16_t iflux = ((int16_t) ist8308_i2c_dma_buf[2] << 8) | (int16_t) ist8308_i2c_dma_buf[1];
337-
// p.flux[0] = (double) iflux * 1.515e-7; // Tesla
338-
//
339-
// iflux = ((int16_t) ist8308_i2c_dma_buf[4] << 8) | (int16_t) ist8308_i2c_dma_buf[3];
340-
// p.flux[1] = (double) iflux * 1.1515e-7; // Tesla
341-
//
342-
// iflux = ((int16_t) ist8308_i2c_dma_buf[6] << 8) | (int16_t) ist8308_i2c_dma_buf[5];
343-
// p.flux[2] = -(double) iflux * 1.1515e-7; // Tesla
344-
//
345-
// rotate(p.flux);
346-
// write((uint8_t *) &p, sizeof(p));
347-
// }
348-
// }
349-
// i2cState_ = IST8308_STATE_ERROR;
350-
// dmaRunning_ = false;
351-
//}
352-
353-
354304
bool Ist8308::display()
355305
{
356306
MagPacket p;

boards/varmint_h7/common/drivers/Ist8308.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ class Ist8308 : public Status, public MiscRotatable
5858
const double *rotation);
5959

6060
bool poll(uint64_t poll_counter);
61-
void endTxDma(void);
62-
void endRxDma(void);
61+
62+
void stateMachine(void);
63+
6364
bool display(void);
6465
// I2C_HandleTypeDef* hi2c(void) {return hi2c_;}
6566
bool isMy(I2C_HandleTypeDef * hi2c) { return hi2c_ == hi2c; }
@@ -71,7 +72,6 @@ class Ist8308 : public Status, public MiscRotatable
7172
DoubleBuffer double_buffer_;
7273
uint16_t sampleRateHz_;
7374
uint64_t drdy_;
74-
// bool dmaRunning_;
7575

7676
I2C_HandleTypeDef * hi2c_;
7777
PollingState i2cState_;

boards/varmint_h7/pixracer_pro/specific/Callbacks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef * hi2c)
113113
{
114114
if (varmint.pitot_.isMy(hi2c)) varmint.pitot_.endDma();
115115
if (varmint.range_.isMy(hi2c)) varmint.range_.stateMachine();
116-
if (varmint.mag_.isMy(hi2c)) varmint.mag_.endRxDma();
116+
if (varmint.mag_.isMy(hi2c)) varmint.mag_.stateMachine();
117117
}
118118

119119
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
120120
{
121121
if (varmint.range_.isMy(hi2c)) varmint.range_.stateMachine();
122-
if (varmint.mag_.isMy(hi2c)) varmint.mag_.endTxDma();
122+
if (varmint.mag_.isMy(hi2c)) varmint.mag_.stateMachine();
123123
}
124124

125125
//////////////////////////////////////////////////////////////////////////////////////////

boards/varmint_h7/varmint_10X/specific/BoardConfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040

4141
#include "CommonConfig.h"
4242

43-
#define SANDBOX true
44-
#define BOARD_STATUS_PRINT true
43+
#define SANDBOX false
44+
#define BOARD_STATUS_PRINT false
4545
#define USE_TELEM 0 // 1 = use UART, 0 = use VCP for link to companion computer.
4646

4747
// UART used for printf's

0 commit comments

Comments
 (0)