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

✨ Add tla2528 driver #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

MichaelYKersey
Copy link

The tla2528 is an i2c-based GPIO expander and ADC mux. It has 8 pins that can each be independently configured and operated as GPIO or ADC. This is a port of the tla2528 driver and adapters from the SJSU robotics team.

Features

  • ADC readings
  • Digital input readings (bus & individual)
  • Digital output writing (bus & individual) (push-pull & open-drain)
  • Adapters for each

Missing/future/unimplemented features:

  • TLA reset on object creation
  • ADC 16-bit precision (currently 12-bit, but chip allows for more)
  • ADC channel verification (redundancy)

@MichaelYKersey
Copy link
Author

currently, the reset command is declared as a private method but is unimplemented. Should it be removed? or is it better to leave it there for the binary interface consistency (I'm not sure how ABIs work)

@kammce kammce changed the title Tla2528 ✨ Add tla2528 driver Nov 27, 2024
Copy link
Member

@kammce kammce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Much of my comments have to do with formatting and documentation. The rest mostly looks good.

Comment on lines +1 to +12

#include <array>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/input_pin.hpp>
#include <libhal/units.hpp>
#include <libhal-expander/tla2528.hpp>

#include <libhal-expander/tla2528_adapters.hpp>
#include <resource_list.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <array>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/input_pin.hpp>
#include <libhal/units.hpp>
#include <libhal-expander/tla2528.hpp>
#include <libhal-expander/tla2528_adapters.hpp>
#include <resource_list.hpp>
#include <array>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/input_pin.hpp>
#include <libhal/units.hpp>
#include <libhal-expander/tla2528.hpp>
#include <libhal-expander/tla2528_adapters.hpp>
#include <resource_list.hpp>

Suggestion made based on https://libhal.github.io/4.1/contributor_guide/style/#s143-include-order.

}
hal::delay(steady_clock, 500ms);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All files should end with a newline. I'm adding a patch to have CI check demos directory files as well.

Comment on lines +1 to +11
#include <array>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/input_pin.hpp>
#include <libhal/units.hpp>
#include <libhal-expander/tla2528.hpp>
#include <libhal-expander/tla2528_adapters.hpp>
#include <resource_list.hpp>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <array>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/input_pin.hpp>
#include <libhal/units.hpp>
#include <libhal-expander/tla2528.hpp>
#include <libhal-expander/tla2528_adapters.hpp>
#include <resource_list.hpp>
#include <array>
#include <libhal-util/bit.hpp>
#include <libhal-util/i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/input_pin.hpp>
#include <libhal/units.hpp>
#include <libhal-expander/tla2528.hpp>
#include <libhal-expander/tla2528_adapters.hpp>
#include <resource_list.hpp>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this everywhere else as well.

Comment on lines +65 to +78
/**
* @brief create a hal::input_pin driver using the tla2528 driver
*
* @param p_channel pin acting as an input pin
*
* @param p_settings input pin settings, default is no resistor
*
* @throws hal::argument_out_of_domain - if p_channel out of range (>7)
*
* @throws hal::resource_unavailable_try_again - if an adapter has already been
* been made for the pin
*
* @throws hal::no_such_device - no device responded on i2c bus
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @brief create a hal::input_pin driver using the tla2528 driver
*
* @param p_channel pin acting as an input pin
*
* @param p_settings input pin settings, default is no resistor
*
* @throws hal::argument_out_of_domain - if p_channel out of range (>7)
*
* @throws hal::resource_unavailable_try_again - if an adapter has already been
* been made for the pin
*
* @throws hal::no_such_device - no device responded on i2c bus
*/
/**
* @brief create a hal::input_pin driver using the tla2528 driver
*
* @param p_channel pin acting as an input pin
* @param p_settings input pin settings, default is no resistor
* @throws hal::argument_out_of_domain - if p_channel out of range (>7)
* @throws hal::resource_unavailable_try_again - if an adapter has already been
* been made for the pin
* @throws hal::no_such_device - no device responded on i2c bus
*/

I find the spacing harder to read than keeping everything together. Can you make this change to other comments like this as well.

*/
tla2528_input_pin make_input_pin(tla2528& p_tla2528,
hal::byte p_channel,
hal::input_pin::settings const& p_settings);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hal::input_pin::settings const& p_settings);
hal::input_pin::settings const& p_settings = {.resistor = none});

May be a nice QOL feature to have this have a default settings parameter vs having the user have to supply each.

float tla2528::get_analog_in(hal::byte p_channel)
{
set_analog_channel(p_channel);
// TODO: look into averaging & channel validation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: look into averaging & channel validation
// TODO(#issue): look into averaging & channel validation

Make an issue for this.

Comment on lines +22 to +25
if (hal::bit_extract(hal::bit_mask::from(m_channel),
m_tla2528->m_object_created)) {
throw hal::resource_unavailable_try_again(this);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You use this same logic a few times in the adaptors. Would make sense to make a helper function for this.

: m_i2c_bus(p_i2c)
, m_i2c_address(p_i2c_address)
{
// TODO: reset command
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: reset command
// TODO(#issue-number): reset command

Make an technical debt issue for this. Also you might as well call reset() here and define it with the TODO. That way the code is already in place and the empty function has the todo embedded in it.

Comment on lines +39 to +40
hal::byte counter =
0; // output counts in binary to go though all out put combinations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hal::byte counter =
0; // output counts in binary to go though all out put combinations
// output counts in binary to go though all out put combinations
hal::byte counter = 0;

The newline 0 looks really odd. You can put the comment one line above.

tla2528_output_pin::~tla2528_output_pin()
{
hal::bit_modify(m_tla2528->m_object_created)
.set(hal::bit_mask::from(m_channel));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.set(hal::bit_mask::from(m_channel));
.clear(hal::bit_mask::from(m_channel));

Shouldn't this be clear?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the same for the others.

@kammce
Copy link
Member

kammce commented Nov 27, 2024

currently, the reset command is declared as a private method but is unimplemented. Should it be removed? or is it better to leave it there for the binary interface consistency (I'm not sure how ABIs work)

See my comment on what you should do about reset. As for ABI, class functions do not effect the ABI of a function so nothing to worry about from that angle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants