Skip to content

Commit

Permalink
Merge pull request #34 from melexis/addressed_reset_fix
Browse files Browse the repository at this point in the history
Addressed reset fix
  • Loading branch information
Letme authored Jun 28, 2021
2 parents d894f28 + 7f98a58 commit 16caccb
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/mlx90632.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,28 @@ int32_t mlx90632_init(void)
int32_t mlx90632_addressed_reset(void)
{
int32_t ret;
uint16_t reg_ctrl;
uint16_t reg_value;

ret = mlx90632_i2c_read(MLX90632_REG_CTRL, &reg_value);
if (ret < 0)
return ret;

reg_ctrl = reg_value & ~MLX90632_CFG_PWR_MASK;
reg_ctrl |= MLX90632_PWR_STATUS_STEP;
ret = mlx90632_i2c_write(MLX90632_REG_CTRL, reg_ctrl);
if (ret < 0)
return ret;

ret = mlx90632_i2c_write(0x3005, MLX90632_RESET_CMD);
if (ret < 0)
return ret;

usleep(150, 200);

return 0;
ret = mlx90632_i2c_write(MLX90632_REG_CTRL, reg_value);

return ret;
}

int32_t mlx90632_get_measurement_time(uint16_t meas)
Expand Down
103 changes: 99 additions & 4 deletions test/TestRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -1642,14 +1642,24 @@ void test_set_meas_type_success(void)
uint16_t reg_ctrl_mock_med = 0xFE0F;
uint16_t reg_ctrl_mock_med1 = 0xFE09;
uint16_t reg_ctrl_mock_med2 = 0xFE0B;
uint16_t reg_ctrl_mock_med3 = 0xFE0D;
uint16_t reg_ctrl_mock_ext = 0xFF1F;
uint16_t reg_ctrl_mock_ext1 = 0xFF19;
uint16_t reg_ctrl_mock_ext2 = 0xFF1B;
uint16_t reg_ctrl_mock_ext3 = 0xFF1D;

// Switch from medical to extended measurement type
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med3, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, 0);
usleep_Expect(150, 200);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med, 0);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);
Expand All @@ -1665,12 +1675,20 @@ void test_set_meas_type_success(void)
TEST_ASSERT_EQUAL_INT(0, mlx90632_set_meas_type(MLX90632_MTYP_EXTENDED));

// Switch from extended to medical measurement type
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_ext, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_ext);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_ext3, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, 0);
usleep_Expect(150, 200);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_ext, 0);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_ext, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_ext);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med1, 0);

Expand All @@ -1683,9 +1701,17 @@ void test_set_meas_type_success(void)
TEST_ASSERT_EQUAL_INT(0, mlx90632_set_meas_type(MLX90632_MTYP_MEDICAL));

// Switch from medical to sleeping step medical measurement type
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med3, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, 0);
usleep_Expect(150, 200);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med, 0);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);
Expand All @@ -1701,12 +1727,20 @@ void test_set_meas_type_success(void)
TEST_ASSERT_EQUAL_INT(0, mlx90632_set_meas_type(MLX90632_MTYP_MEDICAL_BURST));

// Switch from medical sleeping step to extended sleeping step measurement type
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med2, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med2);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med3, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, 0);
usleep_Expect(150, 200);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med2, 0);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med2, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med2);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_ext1, 0);

Expand All @@ -1723,29 +1757,82 @@ void test_set_meas_type_errors(void)
{
uint16_t reg_ctrl_mock_med = 0xFE0F;
uint16_t reg_ctrl_mock_ext1 = 0xFF19;
uint16_t reg_ctrl_mock_step = 0xFE0D;

// Invalid input parameter
TEST_ASSERT_EQUAL_INT(-EINVAL, mlx90632_set_meas_type(9));

// Addressed reset read current reg value error
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, -EPERM);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

TEST_ASSERT_EQUAL_INT(-EPERM, mlx90632_set_meas_type(MLX90632_MTYP_EXTENDED));

// Addressed reset write step value error
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_step, -EPERM);

TEST_ASSERT_EQUAL_INT(-EPERM, mlx90632_set_meas_type(MLX90632_MTYP_EXTENDED));

// Addressed reset command error
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_step, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, -EPERM);

TEST_ASSERT_EQUAL_INT(-EPERM, mlx90632_set_meas_type(MLX90632_MTYP_EXTENDED));

// Addressed reset restore value error
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_step, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, 0);
usleep_Expect(150, 200);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med, -EPERM);

TEST_ASSERT_EQUAL_INT(-EPERM, mlx90632_set_meas_type(MLX90632_MTYP_EXTENDED));

// First read fail
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_step, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, 0);
usleep_Expect(150, 200);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med, 0);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, -EPERM);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

TEST_ASSERT_EQUAL_INT(-EPERM, mlx90632_set_meas_type(MLX90632_MTYP_EXTENDED));

// First write fail
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_step, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, 0);
usleep_Expect(150, 200);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med, 0);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);
Expand All @@ -1755,9 +1842,17 @@ void test_set_meas_type_errors(void)
TEST_ASSERT_EQUAL_INT(-EPERM, mlx90632_set_meas_type(MLX90632_MTYP_EXTENDED));

// Second read fail
mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_step, 0);

mlx90632_i2c_write_ExpectAndReturn(0x3005, MLX90632_RESET_CMD, 0);
usleep_Expect(150, 200);

mlx90632_i2c_write_ExpectAndReturn(MLX90632_REG_CTRL, reg_ctrl_mock_med, 0);

mlx90632_i2c_read_ExpectAndReturn(MLX90632_REG_CTRL, &reg_ctrl_mock_med, 0);
mlx90632_i2c_read_IgnoreArg_value(); // Ignore input of mock since we use it as output
mlx90632_i2c_read_ReturnThruPtr_value(&reg_ctrl_mock_med);
Expand Down

0 comments on commit 16caccb

Please sign in to comment.