|
76 | 76 |
|
77 | 77 | #define AD7779_READ_BIT (0x80)
|
78 | 78 |
|
| 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 | + |
79 | 86 | static int ad7779_read(uint8_t addr, uint8_t *data, uint8_t len)
|
80 | 87 | {
|
81 | 88 | uint8_t buff[len + 1];
|
@@ -181,6 +188,27 @@ int ad7779_get_mode(ad7779_mode_t *mode)
|
181 | 188 | return AD7779_OK;
|
182 | 189 | }
|
183 | 190 |
|
| 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 | + |
184 | 212 | int ad7779_set_mode(ad7779_mode_t mode)
|
185 | 213 | {
|
186 | 214 | if (mode == ad7779_mode__high_resolution)
|
@@ -267,13 +295,6 @@ int ad7779_set_sampling_rate(uint32_t fs)
|
267 | 295 | if ((res = ad7779_set_clear_bits(AD7779_SRC_UPDATE, SRC_LOAD_UPDATE_BIT, 0)) < 0)
|
268 | 296 | return res;
|
269 | 297 |
|
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 |
| - |
277 | 298 | return AD7779_OK;
|
278 | 299 | }
|
279 | 300 |
|
@@ -543,25 +564,35 @@ static int ad7779_reset(int hard)
|
543 | 564 | }
|
544 | 565 |
|
545 | 566 | /* Software reset */
|
546 |
| - ad7779_gpio(start, 0); |
547 |
| - usleep(10000); |
548 | 567 | ad7779_gpio(reset, 0);
|
549 |
| - usleep(200000); |
550 |
| - ad7779_gpio(start, 1); |
551 |
| - usleep(100000); |
| 568 | + usleep(2); |
552 | 569 | ad7779_gpio(reset, 1);
|
| 570 | + usleep(300); |
553 | 571 |
|
554 | 572 | memset(status, 0, sizeof(status));
|
555 | 573 |
|
556 | 574 | 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; |
560 | 590 | }
|
561 | 591 |
|
562 | 592 | usleep(100000);
|
563 | 593 | }
|
564 | 594 |
|
| 595 | + log_error("failed to read status after the device restart"); |
565 | 596 | return AD7779_CTRL_IO_ERROR;
|
566 | 597 | }
|
567 | 598 |
|
@@ -598,6 +629,10 @@ int ad7779_init(int hard)
|
598 | 629 | if ((res = ad7779_set_mode(ad7779_mode__high_resolution)) < 0)
|
599 | 630 | return res;
|
600 | 631 |
|
| 632 | + /* Toggle START (to generate SYNC_IN) after mode change */ |
| 633 | + if ((res = ad7779_pulse_sync()) < 0) |
| 634 | + return res; |
| 635 | + |
601 | 636 | /* Use one DOUTx line; DCLK_CLK_DIV = 1 */
|
602 | 637 | log_debug("setting DOUT_FORMAT");
|
603 | 638 | if ((res = ad7779_write_reg(AD7779_DOUT_FORMAT, 0xe0)) < 0)
|
|
0 commit comments