i2c_slave_simulate.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef __I2C_SLAVE_SIMULATE_H__
  2. #define __I2C_SLAVE_SIMULATE_H__
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include "define.h"
  6. #include "nrf_drv_gpiote.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. //IO操作函数
  11. #define I2C_SDA_HIGH nrf_gpio_pin_write(I2C_SLAVE_SDA, 1);
  12. #define I2C_SDA_LOW nrf_gpio_pin_write(I2C_SLAVE_SDA, 0);
  13. #define READ_I2C_SDA nrf_gpio_pin_read(I2C_SLAVE_SDA) //输入SDA
  14. #define READ_I2C_SCL nrf_gpio_pin_read(I2C_SLAVE_SCL) //输入SCL
  15. typedef enum{
  16. I2C_SLAVE_START,
  17. I2C_SLAVE_ADDR,
  18. I2C_SLAVE_ACK,
  19. I2C_SLAVE_NACK,
  20. I2C_SLAVE_WRITE,
  21. I2C_SLAVE_READ,
  22. I2C_SLAVE_STOP
  23. }i2c_slave_stat;
  24. typedef struct{
  25. i2c_slave_stat i2c_stat;
  26. int bit_idx;
  27. uint8_t addr;
  28. uint8_t reg_addr;
  29. bool is_wr;
  30. int wr_len;
  31. uint8_t *wr_data;
  32. int rd_len;
  33. uint8_t *rd_data;
  34. int idx;
  35. bool scl_stat;
  36. bool sda_start;
  37. bool sda_end;
  38. }i2c_slave_work;
  39. #define I2C_SLAVE_WORK_DEFAULT_CONFIG \
  40. { \
  41. .i2c_stat=I2C_SLAVE_STOP, \
  42. .bit_idx=0, \
  43. .addr=0, \
  44. .reg_addr=0, \
  45. .is_wr=true, \
  46. .wr_len=0, \
  47. .wr_data=NULL, \
  48. .rd_len=0, \
  49. .rd_data=NULL, \
  50. .idx=0, \
  51. .scl_stat=true, \
  52. .sda_start=true, \
  53. .sda_end=true, \
  54. }
  55. typedef void (*i2c_data_func)(i2c_slave_work *slave_work);
  56. void set_i2c_slave_addr(uint8_t addr);
  57. void set_i2c_slave_data_func(i2c_data_func func);
  58. #ifdef __cplusplus
  59. }
  60. #endif
  61. #endif //__I2C_SLAVE_SIMULATE_H__