Skip to content

Commit 080b345

Browse files
author
Jakub Sarzyński
committed
ad7779: apply a sync pulse after configuration
JIRA: DTR-342
1 parent c346606 commit 080b345

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

adc/ad7779/ad7779-driver.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ static int dev_ctl(msg_t *msg)
136136
res = ad7779_set_enabled_channels(dev_ctl.config.enabled_ch);
137137
if (res == AD7779_ARG_ERROR)
138138
return -EINVAL;
139+
if (res != AD7779_OK)
140+
return -EIO;
141+
/* NOTE: Here SYNC_IN is not required by the docs, but sometimes after
142+
* turning on/off channels we encounter unexpected ADC hang-ups (SPI
143+
* works but we don't get DRDY). Applying SYNC solves the issue. */
144+
res = ad7779_pulse_sync();
139145
if (res != AD7779_OK)
140146
return -EIO;
141147
return EOK;
@@ -175,6 +181,9 @@ static int dev_ctl(msg_t *msg)
175181
res = ad7779_set_channel_mode(dev_ctl.ch_config.channel, mode);
176182
if (res == AD7779_ARG_ERROR)
177183
return -EINVAL;
184+
if (res != AD7779_OK)
185+
return -EIO;
186+
res = ad7779_pulse_sync();
178187
if (res != AD7779_OK)
179188
return -EIO;
180189
return EOK;
@@ -213,6 +222,9 @@ static int dev_ctl(msg_t *msg)
213222
res = ad7779_set_channel_gain(dev_ctl.gain.channel, dev_ctl.gain.val);
214223
if (res == AD7779_ARG_ERROR)
215224
return -EINVAL;
225+
if (res != AD7779_OK)
226+
return -EIO;
227+
res = ad7779_pulse_sync();
216228
if (res != AD7779_OK)
217229
return -EIO;
218230
return EOK;
@@ -235,6 +247,9 @@ static int dev_ctl(msg_t *msg)
235247
res = ad7779_set_channel_gain_correction(dev_ctl.calib.channel, dev_ctl.calib.gain);
236248
if (res == AD7779_ARG_ERROR)
237249
return -EINVAL;
250+
if (res != AD7779_OK)
251+
return -EIO;
252+
res = ad7779_pulse_sync();
238253
if (res != AD7779_OK)
239254
return -EIO;
240255
return EOK;

adc/ad7779/ad7779.c

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@
7676

7777
#define AD7779_READ_BIT (0x80)
7878

79+
/* AD7779_STATUS_REG_3 */
80+
#define INIT_COMPLETE_BIT (1 << 4)
81+
82+
/* AD7779_GEN_ERR_REG_2 */
83+
#define RESET_DETECTED_BIT (1 << 5)
84+
85+
7986
static int ad7779_read(uint8_t addr, uint8_t *data, uint8_t len)
8087
{
8188
uint8_t buff[len + 1];
@@ -181,6 +188,27 @@ int ad7779_get_mode(ad7779_mode_t *mode)
181188
return AD7779_OK;
182189
}
183190

191+
int ad7779_pulse_sync(void)
192+
{
193+
int res;
194+
195+
log_debug("resetting internal logic");
196+
res = ad7779_gpio(start, 0);
197+
if (res != 0) {
198+
log_error("failed to set start GPIO to 0");
199+
return AD7779_GPIO_IO_ERROR;
200+
}
201+
202+
usleep(2);
203+
res = ad7779_gpio(start, 1);
204+
if (res != 0) {
205+
log_error("failed to set start GPIO to 1");
206+
return AD7779_GPIO_IO_ERROR;
207+
}
208+
209+
return AD7779_OK;
210+
}
211+
184212
int ad7779_set_mode(ad7779_mode_t mode)
185213
{
186214
if (mode == ad7779_mode__high_resolution)
@@ -267,13 +295,6 @@ int ad7779_set_sampling_rate(uint32_t fs)
267295
if ((res = ad7779_set_clear_bits(AD7779_SRC_UPDATE, SRC_LOAD_UPDATE_BIT, 0)) < 0)
268296
return res;
269297

270-
/* Reset internal logic */
271-
log_debug("reseting internal logic");
272-
if ((res = ad7779_set_clear_bits(AD7779_GENERAL_USER_CONFIG_2, 0, SPI_SYNC)) < 0)
273-
return res;
274-
if ((res = ad7779_set_clear_bits(AD7779_GENERAL_USER_CONFIG_2, SPI_SYNC, 0)) < 0)
275-
return res;
276-
277298
return AD7779_OK;
278299
}
279300

@@ -543,25 +564,35 @@ static int ad7779_reset(int hard)
543564
}
544565

545566
/* Software reset */
546-
ad7779_gpio(start, 0);
547-
usleep(10000);
548567
ad7779_gpio(reset, 0);
549-
usleep(200000);
550-
ad7779_gpio(start, 1);
551-
usleep(100000);
568+
usleep(2);
552569
ad7779_gpio(reset, 1);
570+
usleep(300);
553571

554572
memset(status, 0, sizeof(status));
555573

556574
for (i = 0; i < 4; ++i) {
557-
if (!ad7779_get_status(status)) {
558-
if (status[16] & 0x10)
559-
return AD7779_OK;
575+
if (ad7779_get_status(status)) {
576+
continue;
577+
}
578+
579+
if ((status[16] & INIT_COMPLETE_BIT) && (status[13] & RESET_DETECTED_BIT)) {
580+
return AD7779_OK;
581+
}
582+
else {
583+
if (!(status[16] & INIT_COMPLETE_BIT)) {
584+
log_error("init bit not detected");
585+
}
586+
if (!(status[13] & RESET_DETECTED_BIT)) {
587+
log_error("reset bit not detected");
588+
}
589+
break;
560590
}
561591

562592
usleep(100000);
563593
}
564594

595+
log_error("failed to read status after the device restart");
565596
return AD7779_CTRL_IO_ERROR;
566597
}
567598

@@ -598,6 +629,10 @@ int ad7779_init(int hard)
598629
if ((res = ad7779_set_mode(ad7779_mode__high_resolution)) < 0)
599630
return res;
600631

632+
/* Toggle START (to generate SYNC_IN) after mode change */
633+
if ((res = ad7779_pulse_sync()) < 0)
634+
return res;
635+
601636
/* Use one DOUTx line; DCLK_CLK_DIV = 1 */
602637
log_debug("setting DOUT_FORMAT");
603638
if ((res = ad7779_write_reg(AD7779_DOUT_FORMAT, 0xe0)) < 0)

adc/ad7779/ad7779.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ int ad7779_set_channel_gain_correction(uint8_t channel, uint32_t gain);
8989

9090
int ad7779_get_status(uint8_t *status_buf);
9191

92+
int ad7779_pulse_sync(void);
93+
9294
/* For debugging purposes */
9395
int ad7779_print_status(void);
9496

0 commit comments

Comments
 (0)