gpio_exp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* GDS Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_log.h"
  12. #include "driver/gpio.h"
  13. #include "driver/i2c.h"
  14. #include "gpio_exp.h"
  15. static const char TAG[] = "gpio expander";
  16. static void IRAM_ATTR intr_isr_handler(void* arg);
  17. static struct gpio_exp_s* find_expander(struct gpio_exp_s *expander, int *gpio);
  18. static void pca9535_set_direction(union gpio_exp_phy_u *phy, uint32_t r_mask, uint32_t w_mask);
  19. static int pca9535_read(union gpio_exp_phy_u *phy);
  20. static esp_err_t i2c_write_byte(uint8_t i2c_port, uint8_t i2c_addr, uint8_t reg, uint8_t val);
  21. static uint8_t i2c_read_byte(uint8_t i2c_port, uint8_t i2c_addr, uint8_t reg);
  22. static uint16_t i2c_read_word(uint8_t i2c_port, uint8_t i2c_addr, uint8_t reg);
  23. static esp_err_t i2c_write_word(uint8_t i2c_port, uint8_t i2c_addr, uint8_t reg, uint16_t data);
  24. static const struct gpio_exp_model_s {
  25. char *model;
  26. gpio_int_type_t trigger;
  27. void (*set_direction)(union gpio_exp_phy_u *phy, uint32_t r_mask, uint32_t mask);
  28. int (*read)(union gpio_exp_phy_u *phy);
  29. } registered[] = {
  30. { .model = "pca9535",
  31. .trigger = GPIO_INTR_NEGEDGE,
  32. .set_direction = pca9535_set_direction,
  33. .read = pca9535_read, }
  34. };
  35. static uint8_t n_expanders;
  36. static EXT_RAM_ATTR struct gpio_exp_s {
  37. uint32_t first, last;
  38. union gpio_exp_phy_u phy;
  39. uint32_t shadow;
  40. TickType_t age;
  41. uint32_t r_mask, w_mask;
  42. struct {
  43. gpio_exp_isr handler;
  44. void *arg;
  45. } isr[4];
  46. struct gpio_exp_model_s const *model;
  47. } expanders[4];
  48. /******************************************************************************
  49. * Retrieve base from an expander reference
  50. */
  51. uint32_t gpio_exp_base(struct gpio_exp_s *expander) {
  52. return expander->first;
  53. }
  54. /******************************************************************************
  55. * Retrieve reference from a GPIO
  56. */
  57. struct gpio_exp_s *gpio_exp_expander(int gpio) {
  58. int _gpio = gpio;
  59. return find_expander(NULL, &_gpio);
  60. }
  61. /******************************************************************************
  62. * Create an I2C expander
  63. */
  64. struct gpio_exp_s* gpio_exp_create(const gpio_exp_config_t *config) {
  65. struct gpio_exp_s *expander = expanders + n_expanders;
  66. if (config->base < GPIO_EXP_BASE_MIN || n_expanders == sizeof(expanders)/sizeof(struct gpio_exp_s)) {
  67. ESP_LOGE(TAG, "Base %d GPIO must be > %d for %s or too many expanders %d", config->base, GPIO_EXP_BASE_MIN, config->model, n_expanders);
  68. return NULL;
  69. }
  70. // See if we know that model (expanders is zero-initialized)
  71. for (int i = 0; !expander->model && i < sizeof(registered)/sizeof(struct gpio_exp_model_s); i++) {
  72. if (strcasestr(config->model, registered[i].model)) expander->model = registered + i;
  73. }
  74. // well... try again
  75. if (!expander->model) {
  76. ESP_LOGE(TAG,"Unknown GPIO expansion chip %s", config->model);
  77. return NULL;
  78. }
  79. n_expanders++;
  80. expander->first = config->base;
  81. expander->last = config->base + config->count - 1;
  82. memcpy(&expander->phy, &config->phy, sizeof(union gpio_exp_phy_u));
  83. // set interrupt if possible
  84. if (config->intr > 0) {
  85. gpio_pad_select_gpio(config->intr);
  86. gpio_set_direction(config->intr, GPIO_MODE_INPUT);
  87. switch (expander->model->trigger) {
  88. case GPIO_INTR_NEGEDGE:
  89. case GPIO_INTR_LOW_LEVEL:
  90. gpio_set_pull_mode(config->intr, GPIO_PULLUP_ONLY);
  91. break;
  92. case GPIO_INTR_POSEDGE:
  93. case GPIO_INTR_HIGH_LEVEL:
  94. gpio_set_pull_mode(config->intr, GPIO_PULLDOWN_ONLY);
  95. break;
  96. default:
  97. gpio_set_pull_mode(config->intr, GPIO_PULLUP_PULLDOWN);
  98. break;
  99. }
  100. gpio_set_intr_type(config->intr, expander->model->trigger);
  101. gpio_isr_handler_add(config->intr, intr_isr_handler, expander);
  102. gpio_intr_enable(config->intr);
  103. }
  104. ESP_LOGI(TAG, "Create GPIO expander at base %u with INT %u at @%x", config->base, config->intr, config->phy.addr);
  105. return expander;
  106. }
  107. /******************************************************************************
  108. * Add ISR handler
  109. */
  110. bool gpio_exp_add_isr(gpio_exp_isr isr, void *arg, struct gpio_exp_s *expander) {
  111. for (int i = 0; i < sizeof(expander->isr)/sizeof(*expander->isr); i++) {
  112. if (!expander->isr[i].handler) {
  113. expander->isr[i].handler = isr;
  114. expander->isr[i].arg = arg;
  115. ESP_LOGI(TAG, "Added new ISR for expander base %d", expander->first);
  116. return true;
  117. }
  118. }
  119. ESP_LOGE(TAG, "No room left to add new ISR");
  120. return false;
  121. }
  122. /******************************************************************************
  123. * Set GPIO direction
  124. */
  125. struct gpio_exp_s* gpio_exp_set_direction(int gpio, gpio_mode_t mode, struct gpio_exp_s *expander) {
  126. if ((expander = find_expander(expander, &gpio)) == NULL) return NULL;
  127. if (mode == GPIO_MODE_INPUT) {
  128. expander->r_mask |= 1 << gpio;
  129. expander->age = ~xTaskGetTickCount();
  130. } else {
  131. expander->w_mask |= 1 << gpio;
  132. }
  133. if (expander->r_mask & expander->w_mask) {
  134. ESP_LOGE(TAG, "GPIO %d on expander base %u can't be r/w", gpio, expander->first);
  135. return false;
  136. }
  137. // most expanders want unconfigured GPIO to be set to output
  138. if (expander->model->set_direction) expander->model->set_direction(&expander->phy, expander->r_mask, expander->w_mask);
  139. return expander;
  140. }
  141. /******************************************************************************
  142. * Get GPIO level with cache
  143. */
  144. int gpio_exp_get_level(int gpio, uint32_t age, struct gpio_exp_s *expander) {
  145. if ((expander = find_expander(expander, &gpio)) == NULL) return false;
  146. uint32_t now = xTaskGetTickCount();
  147. if (now - expander->age >= pdMS_TO_TICKS(age)) {
  148. expander->shadow = expander->model->read(&expander->phy);
  149. expander->age = now;
  150. }
  151. return expander->shadow & (1 << gpio) ? 1 : 0;
  152. }
  153. /******************************************************************************
  154. * Enumerate modified GPIO
  155. */
  156. void gpio_exp_enumerate(gpio_exp_enumerator enumerator, struct gpio_exp_s *expander) {
  157. uint32_t value = expander->model->read(&expander->phy) ^ expander->shadow;
  158. uint8_t clz;
  159. // memorize newly read value and just update if requested
  160. expander->shadow ^= value;
  161. if (!enumerator) return;
  162. // now we have a bitmap of all modified GPIO since last call
  163. for (int gpio = 0; value; value <<= (clz + 1)) {
  164. clz = __builtin_clz(value);
  165. gpio += clz;
  166. enumerator(expander->first + 31 - gpio, (expander->shadow >> (31 - gpio)) & 0x01, expander);
  167. }
  168. }
  169. /****************************************************************************************
  170. * Find the expander related to base
  171. */
  172. static struct gpio_exp_s* find_expander(struct gpio_exp_s *expander, int *gpio) {
  173. for (int i = 0; !expander && i < n_expanders; i++) {
  174. if (*gpio >= expanders[i].first && *gpio <= expanders[i].last) expander = expanders + i;
  175. }
  176. // normalize GPIO number
  177. if (expander && *gpio >= expanders->first) *gpio -= expanders->first;
  178. return expander;
  179. }
  180. /****************************************************************************************
  181. * Configure unused GPIO to output
  182. */
  183. static void pca9535_set_direction(union gpio_exp_phy_u *phy, uint32_t r_mask, uint32_t w_mask) {
  184. esp_err_t err = i2c_write_word(phy->port, phy->addr, 0x06, r_mask);
  185. ESP_LOGD(TAG, "PCA9535 set direction %x %d", r_mask, err);
  186. }
  187. /****************************************************************************************
  188. * Configure unused GPIO to output
  189. */
  190. static int pca9535_read(union gpio_exp_phy_u *phy) {
  191. ESP_LOGD(TAG, "PCA9535 read @%d", phy->addr);
  192. return i2c_read_word(phy->port, phy->addr, 0x0);
  193. }
  194. /****************************************************************************************
  195. * INTR low-level handler
  196. */
  197. static void IRAM_ATTR intr_isr_handler(void* arg)
  198. {
  199. struct gpio_exp_s *expander = (struct gpio_exp_s*) arg;
  200. BaseType_t woken = pdFALSE;
  201. for (int i = 0; i < sizeof(expander->isr)/sizeof(*expander->isr); i++) {
  202. if (expander->isr[i].handler) woken |= expander->isr[i].handler(expander->isr[i].arg);
  203. }
  204. if (woken) portYIELD_FROM_ISR();
  205. ESP_EARLY_LOGD(TAG, "INTR for expander %u", expander->first);
  206. }
  207. /****************************************************************************************
  208. *
  209. */
  210. static esp_err_t i2c_write_byte(uint8_t i2c_port, uint8_t i2c_addr, uint8_t reg, uint8_t val) {
  211. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  212. i2c_master_start(cmd);
  213. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  214. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  215. i2c_master_write_byte(cmd, val, I2C_MASTER_NACK);
  216. i2c_master_stop(cmd);
  217. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  218. i2c_cmd_link_delete(cmd);
  219. if (ret != ESP_OK) {
  220. ESP_LOGW(TAG, "I2C write failed");
  221. }
  222. return ret;
  223. }
  224. /****************************************************************************************
  225. * I2C read one byte
  226. */
  227. static uint8_t i2c_read_byte(uint8_t i2c_port, uint8_t i2c_addr, uint8_t reg) {
  228. uint8_t data = 0xff;
  229. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  230. i2c_master_start(cmd);
  231. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  232. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  233. i2c_master_start(cmd);
  234. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_READ, I2C_MASTER_NACK);
  235. i2c_master_read_byte(cmd, &data, I2C_MASTER_NACK);
  236. i2c_master_stop(cmd);
  237. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  238. i2c_cmd_link_delete(cmd);
  239. if (ret != ESP_OK) {
  240. ESP_LOGW(TAG, "I2C read failed");
  241. }
  242. return data;
  243. }
  244. /****************************************************************************************
  245. * I2C read 16 bits word
  246. */
  247. static uint16_t i2c_read_word(uint8_t i2c_port, uint8_t i2c_addr, uint8_t reg) {
  248. uint16_t data = 0xffff;
  249. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  250. i2c_master_start(cmd);
  251. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  252. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  253. i2c_master_start(cmd);
  254. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_READ, I2C_MASTER_NACK);
  255. i2c_master_read(cmd, (uint8_t*) &data, 2, I2C_MASTER_NACK);
  256. i2c_master_stop(cmd);
  257. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  258. i2c_cmd_link_delete(cmd);
  259. if (ret != ESP_OK) {
  260. ESP_LOGW(TAG, "I2C read failed");
  261. }
  262. return data;
  263. }
  264. /****************************************************************************************
  265. * I2C write 16 bits word
  266. */
  267. static esp_err_t i2c_write_word(uint8_t i2c_port, uint8_t i2c_addr, uint8_t reg, uint16_t data) {
  268. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  269. i2c_master_start(cmd);
  270. i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  271. i2c_master_write_byte(cmd, reg, I2C_MASTER_NACK);
  272. i2c_master_write(cmd, (uint8_t*) &data, 2, I2C_MASTER_NACK);
  273. i2c_master_stop(cmd);
  274. esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 100 / portTICK_RATE_MS);
  275. i2c_cmd_link_delete(cmd);
  276. if (ret != ESP_OK) {
  277. ESP_LOGW(TAG, "I2C write failed");
  278. }
  279. return ret;
  280. }