diff --git a/src/ad5933.c b/src/ad5933.c index 50a3b42b4f4ca28a4c02887d4a2704078d009970..5ce592206448e6973017f1c2b3596611b40e220e 100644 --- a/src/ad5933.c +++ b/src/ad5933.c @@ -92,32 +92,30 @@ #define CTRL_BLK_WRITE 0b10100000 #define CTRL_BLK_READ 0b10100001 // Status register options -#define STATUS_TEMP_VALID 0x01 -#define STATUS_DATA_VALID 0x02 -#define STATUS_SWEEP_DONE 0x04 +#define STATUS_TEMP_VALID 0b00000001 +#define STATUS_DATA_VALID 0b00000010 +#define STATUS_SWEEP_DONE 0b00000100 #define STATUS_ERROR 0xFF // Frequency sweep parameters #define SWEEP_DELAY 1 - // this was calculated per the data sheet -#define clockSpeed 16776000 +#define INTERNAL_CLK_HZ 16776000 uint8_t read_byte(uint8_t address); void read_block(uint8_t address, uint8_t length, uint8_t* buffer); void write_byte(uint8_t address, uint8_t value); void write_block(uint8_t address, uint8_t length, uint8_t* buffer); -void ad_reset(void); +void reset(void); void internal_clock_set(bool); -bool ad_set_start_frequency(uint32_t); -bool ad_set_increment_frequency(uint32_t); -bool ad_set_num_increments(uint16_t); -void ad_set_gain(uint8_t pga_gain, uint8_t o_range); -void ad_set_control_mode(uint8_t); -uint8_t ad_read_register(uint8_t); -void ad_set_power_mode(uint8_t); -cuint16_t ad_get_complex_data(void); +bool set_start_frequency(uint32_t); +bool set_increment_frequency(uint32_t); +bool set_num_increments(uint16_t); +bool set_settling_cycles(uint16_t cycles); +void set_gain(uint8_t pga_gain, uint8_t o_range); +void set_control_mode(uint8_t); + void eis_init(void){ @@ -140,8 +138,9 @@ uint8_t read_byte(uint8_t address) { * @return Value read. */ twi_package_t twi_pack; - uint8_t value; + uint8_t value; + twi_pack.addr[0] = address; twi_pack.addr_length = 1; twi_pack.buffer = &value; @@ -149,10 +148,10 @@ uint8_t read_byte(uint8_t address) { twi_pack.no_wait = false; twi_pack.length = 1; - //read the data to *value to access twi_master_read(&EIS_TWI, &twi_pack); return value; - + read_block(address, 1, &value); + return value; } void read_block(uint8_t address, uint8_t length, uint8_t* buffer) { @@ -236,7 +235,8 @@ void write_block(uint8_t address, uint8_t length, uint8_t* buffer) { twi_master_write(&EIS_TWI, &twi_pack); } -void ad5933_set_params(uint32_t start, uint32_t increment, uint16_t n_increments, uint16_t settle_cycles, uint8_t gain){ +void ad5933_set_params(uint32_t start, uint32_t increment, uint16_t n_increments, + uint16_t settle_cycles, uint8_t gain) { /** * Set EIS sweep parameters. * @@ -247,12 +247,13 @@ void ad5933_set_params(uint32_t start, uint32_t increment, uint16_t n_increments * @param gain PGA gain */ internal_clock_set(1); - ad_reset(); + reset(); - ad_set_start_frequency(start); - ad_set_increment_frequency(increment); - ad_set_num_increments(n_increments); - ad_set_gain(gain, 1); + set_start_frequency(start); + set_increment_frequency(increment); + set_num_increments(n_increments); + set_gain(gain, 1); + set_settling_cycles(settle_cycles); printf("Initializing of AD5933 Complete\r\n"); @@ -260,57 +261,50 @@ void ad5933_set_params(uint32_t start, uint32_t increment, uint16_t n_increments } -void ad_reset(void) { - //resets the AD5933 - - uint8_t checkbit; - - // get the CTRL_REG2 (contains D7-D0) - checkbit = read_byte(CTRL_REG2); - - // reset the reset bit (D4) - checkbit |= CTRL_RESET; - - //write the data to the AD5933 - write_byte(CTRL_REG2, checkbit); +void reset(void) { + uint8_t current = read_byte(CTRL_REG2); + write_byte(CTRL_REG2, current | CTRL_RESET); } void internal_clock_set(bool internal) { - //set the control register to internal or external clock + uint8_t current = read_byte(CTRL_REG2); if (internal) - write_byte(CTRL_REG2, CTRL_CLOCK_INTERNAL); + current &= ~CTRL_CLOCK_INTERNAL; else - write_byte(CTRL_REG2, CTRL_CLOCK_EXTERNAL); + current |= CTRL_CLOCK_EXTERNAL; + write_byte(CTRL_REG2, current); } void test(uint32_t freq){ - ad_set_start_frequency(freq); -// write_block(START_FREQ_1, 3, &freq); - uint32_t buffer1 = 0; - read_block(START_FREQ_1, 3, (uint8_t*)&buffer1+1); - printf("%lx\r\n", swap32(buffer1)); + ad5933_set_params(0x0f5c28, 0x14f, 10, 100, 1); + + cuint16_t result[10]; + ad5933_sweep(result, 10); + + for (uint8_t n = 0; n < 10; n++) { + printf("%u + %ui\r\n", result[n].real, result[n].imag); + } } -bool ad_set_start_frequency(uint32_t start) { +bool set_start_frequency(uint32_t start) { if (start > 0xFFFFFF) { printf("#ERR: AD5933: Start frequency too high.\r\n"); - return false; // overflow + return false; } - uint32_t buffer = swap32(start); write_block(START_FREQ_1, 3, (uint8_t*)&buffer+1); return true; } -bool ad_set_increment_frequency(uint32_t increment) { +bool set_increment_frequency(uint32_t increment) { if (increment > 0xFFFFFF) { - printf("#ERR: AD5933: Increment too high. \r\n"); - return false; // overflow + printf("#ERR: AD5933: Increment too high.\r\n"); + return false; } uint32_t buffer = swap32(increment); @@ -319,20 +313,41 @@ bool ad_set_increment_frequency(uint32_t increment) { return true; } -bool ad_set_num_increments(uint16_t num) { +bool set_num_increments(uint16_t num) { if (num > 511){ - printf("#ERR: AD5933: Increment number too high. \r\n"); - return false; // overflow + printf("#ERR: AD5933: Increment number too high.\r\n"); + return false; } uint16_t buffer = swap16(num); - // attempt to write write_block(NUM_INC_1, 2, (uint8_t*)&buffer); return true; } -void ad_set_gain(uint8_t pga_gain, uint8_t o_range) { +bool set_settling_cycles(uint16_t cycles) { + uint16_t output = 0; + + if (cycles <= 0x1ff) { + output = swap16(cycles); + } + else if (cycles <= 0x3fe) { + output = swap16((cycles >> 1) | 0b0000001000000000); + } + else if (cycles <= 0x7fc) { + output = swap16((cycles >> 2) | 0b0000011000000000); + } + else { + printf("#ERR: AD5933: Settling cycles too high.\r\n"); + return false; + } + + write_block(NUM_SCYCLES_1, 2, (uint8_t*)&output); + + return true; +} + +void set_gain(uint8_t pga_gain, uint8_t o_range) { uint8_t val = 0; switch (pga_gain) { @@ -369,99 +384,49 @@ void ad_set_gain(uint8_t pga_gain, uint8_t o_range) { return; } -void ad_set_control_mode(uint8_t mode) { +void set_control_mode(uint8_t mode) { uint8_t val = read_byte(CTRL_REG1); - // clear out the top 4 bits - val &= 0x00001111; - - val |= mode; - write_byte(CTRL_REG1, val); + val &= 0b00001111; // clear out the top 4 bits + write_byte(CTRL_REG1, val | mode); return; } -uint8_t ad_read_register(uint8_t reg) { - // short script to read registers - uint8_t val = read_byte(reg); - return val; -} - -uint8_t ad_read_status(void) { - //short script to read register - return ad_read_register(STATUS_REG); -} - -void ad_set_power_mode(uint8_t level) { - - switch (level) { - case POWER_ON: - ad_set_control_mode(CTRL_NO_OPERATION); - return; - case POWER_STANDBY: - ad_set_control_mode(CTRL_STANDBY_MODE); - return; - case POWER_DOWN: - ad_set_control_mode(CTRL_POWER_DOWN_MODE); - return; - - } -} - void ad5933_sweep(cuint16_t imp[], int n) { - - // issue needed commands to start sweep - ad_set_power_mode(POWER_STANDBY); // place in standby - ad_set_control_mode(CTRL_INIT_START_FREQ); // init start freq - ad_set_control_mode(CTRL_START_FREQ_SWEEP); + reset(); + set_control_mode(CTRL_STANDBY_MODE); + set_control_mode(CTRL_INIT_START_FREQ); + delay_ms(400); + set_control_mode(CTRL_START_FREQ_SWEEP); for (uint16_t i = 0; i < n; i++) { - if ((ad_read_status() & STATUS_SWEEP_DONE) != STATUS_SWEEP_DONE) { /////////////!! + if ((read_byte(STATUS_REG) & STATUS_SWEEP_DONE) == STATUS_SWEEP_DONE) { break; } else { - imp[i] = ad_get_complex_data(); + imp[i] = ad5993_get_complex_data(); delay_ms(100); - ad_set_control_mode(CTRL_INCREMENT_FREQ); + set_control_mode(CTRL_INCREMENT_FREQ); } } - ad_set_power_mode(POWER_STANDBY); + set_control_mode(CTRL_POWER_DOWN_MODE); return; - -// int i = 0; -// -// //check the register for sweep completion as loop condition -// while ((ad_read_status() & STATUS_SWEEP_DONE) != STATUS_SWEEP_DONE) { -// // Make sure we aren't exceeding the bounds of our buffer -// if (i >= n) { -// return; -// } -// -// // Get the data for this frequency point and store it in the array -// ad_get_complex_data(&real[i], &imag[i]); -// -// i++; -// -// delay_ms(100); -// ad_set_control_mode(CTRL_INCREMENT_FREQ); -// } -// -// // change power mode to standy -// ad_set_power_mode(POWER_STANDBY); -// return; } -cuint16_t ad_get_complex_data(void) { +cuint16_t ad5993_get_complex_data(void) { // poll the status register to check for data to receive - while ((ad_read_status() & STATUS_DATA_VALID) != STATUS_DATA_VALID); + while ((read_byte(STATUS_REG) & STATUS_DATA_VALID) != STATUS_DATA_VALID){ + delay_ms(30); + } uint16_t real; uint16_t imag; - read_block(REAL_DATA_1, 2, &real); - read_block(IMAG_DATA_1, 2, &imag); + read_block(REAL_DATA_1, 2, (uint8_t*)&real); + read_block(IMAG_DATA_1, 2, (uint8_t*)&imag); cuint16_t result; diff --git a/src/ad5933.h b/src/ad5933.h index 0acca6d19370c09727c803436558abfaae445e0d..96c96a9f8d70f22f74df6cf3df8bda531a06e799 100644 --- a/src/ad5933.h +++ b/src/ad5933.h @@ -13,16 +13,17 @@ #include #include -void eis_init(void); -uint8_t ad_read_status(void); -void ad5933_set_params(uint32_t start, uint32_t increment, uint16_t n_increments, uint16_t settle_cycles, uint8_t gain); -void test(uint32_t freq); - typedef struct c_uint16_t { uint16_t real; uint16_t imag; } cuint16_t; +void eis_init(void); +void ad5933_set_params(uint32_t start, uint32_t increment, uint16_t n_increments, + uint16_t settle_cycles, uint8_t gain); void ad5933_sweep(cuint16_t imp[], int n); +cuint16_t ad5993_get_complex_data(void); + +void test(uint32_t freq); #endif /* ad5933_h */