Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register comments #3

Merged
merged 1 commit into from
Mar 1, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions hardware/compass/hmc5883l.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,32 @@ void task_hmc5883l(void *ignore) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_WRITE, 1);
i2c_master_write_byte(cmd, 0x02, 1); // Mode register
i2c_master_write_byte(cmd, 0x00, 1); // value 0
i2c_master_write_byte(cmd, 0x02, 1); // 0x02 = "Mode register"
i2c_master_write_byte(cmd, 0x00, 1); // 0x00 = "Continuous-Measurement Mode"
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);

//Set value in "Configuration Register B"
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_WRITE, 1);
i2c_master_write_byte(cmd, 0x01, 1); // Mode register
i2c_master_write_byte(cmd, 0x20, 1); // value 0
i2c_master_write_byte(cmd, 0x01, 1); // 0x01 = "Configuration Register B"
i2c_master_write_byte(cmd, 0x20, 1); // 0x20 = default Gain setting
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);

//Set active register to "Identification Register A"
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_WRITE, 1));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 10, 1)); // Data registers
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 10, 1)); //10 = 0x0A = "Identification Register A"
ESP_ERROR_CHECK(i2c_master_stop(cmd));
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 100/portTICK_PERIOD_MS));
i2c_cmd_link_delete(cmd);

//Get data from Identification Register A, B and C
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_READ, 1));
Expand All @@ -66,23 +69,25 @@ void task_hmc5883l(void *ignore) {
i2c_cmd_link_delete(cmd);

while(1) {
//Set active registert to "Data Output X MSB Register"
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_WRITE, 1);
i2c_master_write_byte(cmd, 0x03, 1); // Data registers
i2c_master_write_byte(cmd, 0x03, 1); //0x03 = "Data Output X MSB Register"
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);

//Read values for X, Y and Z
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_READ, 1);
i2c_master_read_byte(cmd, data, 0);
i2c_master_read_byte(cmd, data+1, 0);
i2c_master_read_byte(cmd, data+2, 0);
i2c_master_read_byte(cmd, data+3, 0);
i2c_master_read_byte(cmd, data+4, 0);
i2c_master_read_byte(cmd, data+5, 1);
i2c_master_read_byte(cmd, data, 0); //"Data Output X MSB Register"
i2c_master_read_byte(cmd, data+1, 0); //"Data Output X LSB Register"
i2c_master_read_byte(cmd, data+2, 0); //"Data Output Z MSB Register"
i2c_master_read_byte(cmd, data+3, 0); //"Data Output Z LSB Register"
i2c_master_read_byte(cmd, data+4, 0); //"Data Output Y MSB Register"
i2c_master_read_byte(cmd, data+5, 1); //"Data Output Y LSB Register "
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
Expand Down