tca9555_driver.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "tca9555_driver.h"
  2. #include "esp_log.h"
  3. #include "bsp_board.h"
  4. esp_io_expander_handle_t io_expander = NULL;
  5. #define AUDIO_POWER_MASK (IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_5 | IO_EXPANDER_PIN_NUM_6 | IO_EXPANDER_PIN_NUM_8)
  6. void tca9555_driver_init(void)
  7. {
  8. i2c_master_bus_handle_t i2c_bus = esp_ret_i2c_handle();
  9. esp_err_t ret = esp_io_expander_new_i2c_tca95xx_16bit(i2c_bus, ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_000, &io_expander);
  10. /* Test output level function */
  11. ret = esp_io_expander_set_dir(io_expander, (IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_5 | IO_EXPANDER_PIN_NUM_6 | IO_EXPANDER_PIN_NUM_8), IO_EXPANDER_OUTPUT);
  12. if(ret != ESP_OK){
  13. printf("EXIO Mode setting failure!!\r\n");
  14. }
  15. ret = esp_io_expander_set_dir(io_expander, (IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_9 | IO_EXPANDER_PIN_NUM_10 | IO_EXPANDER_PIN_NUM_11), IO_EXPANDER_INPUT);
  16. if(ret != ESP_OK){
  17. printf("EXIO Mode setting failure!!\r\n");
  18. }
  19. }
  20. void tca9555_enable_audio_power(void)
  21. {
  22. if (io_expander == NULL) {
  23. ESP_LOGW("TCA9555", "IO expander not initialized, cannot enable audio power");
  24. return;
  25. }
  26. esp_err_t ret = esp_io_expander_set_level(io_expander, AUDIO_POWER_MASK, 1);
  27. if (ret != ESP_OK) {
  28. ESP_LOGW("TCA9555", "Failed to enable audio power rails: %s", esp_err_to_name(ret));
  29. } else {
  30. ESP_LOGI("TCA9555", "Audio power rails enabled via IO expander");
  31. }
  32. }
  33. /********************************************************** Set the EXIO output status **********************************************************/
  34. void Set_EXIO(uint32_t Pin,uint8_t State) // Sets the level state of the Pin without affecting the other pins(PIN:1~8)
  35. {
  36. esp_err_t ret;
  37. ret = esp_io_expander_set_level(io_expander, Pin, State);
  38. if(ret != ESP_OK){
  39. printf("EXIO level setting failure!!\r\n");
  40. }
  41. }
  42. /********************************************************** Read EXIO status **********************************************************/
  43. bool Read_EXIO(uint32_t Pin) // Read the level of the TCA9555PWR Pin
  44. {
  45. esp_err_t ret;
  46. uint32_t input_level_mask = 0;
  47. ret = esp_io_expander_get_level(io_expander, Pin, &input_level_mask);
  48. if(ret != ESP_OK){
  49. printf("EXIO level reading failure!!\r\n");
  50. }
  51. bool bitStatus = input_level_mask & Pin;
  52. return bitStatus;
  53. }