i2c_controller_interface.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * This file is an interface file for I2C communication between MCU (Controller) and other peripherals (Target): i2c_hal.c
  3. * Context:
  4. * Our current functions in ADC and DAC are tightly coupled with I2C communication.
  5. * Need a communication mechanism to isolate the flow of the ADC function from Asynchronous calls.
  6. * Decision:
  7. * i2c_controller_interface is a header file which defines what the I2C peripheral does:
  8. * - i2c READ
  9. * - i2c WRITE
  10. * - i2c start controller transfer
  11. * - i2c enable interrupt
  12. * - i2c bus wait
  13. * Consequences:
  14. * Cleaner platform independent file
  15. Reference:
  16. - https://www.embeddedrelated.com/showarticle/1596.php
  17. - https://www.beningo.com/5-tips-for-designing-an-interface-in-c/
  18. - https://iot.sites.arm.com/open-iot-sdk/libraries/mcu-driver-hal/mcu-driver-hal/pwmout__api_8h_source.html
  19. */
  20. #ifndef I2C_INTERFACE_H_
  21. #define I2C_INTERFACE_H_
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. //Maximum packet sizes
  25. #define I2C_TX_MAX_PACKET_SIZE (4)
  26. #define I2C_RX_MAX_PACKET_SIZE (4)
  27. typedef struct {
  28. volatile bool complete;
  29. uint8_t packet[I2C_RX_MAX_PACKET_SIZE];
  30. uint8_t count;
  31. uint8_t len;
  32. } I2CRxPackage;
  33. extern I2CRxPackage controllerRxPackage;
  34. typedef struct {
  35. volatile bool complete;
  36. uint8_t packet[I2C_TX_MAX_PACKET_SIZE];
  37. uint8_t count;
  38. uint8_t len;
  39. } I2CTxPackage;
  40. extern I2CTxPackage controllerTxPackage;
  41. /*
  42. * Since C does not allows to add functions in typedef struct, however a function pointer can be included in Structure in C. This interface provides a standard features of i2c_hal
  43. */
  44. typedef struct{
  45. bool (*write)(uint8_t const TARGET_ADDRESS);
  46. bool (*read) (uint8_t const TARGET_ADDRESS);
  47. } I2C_Interface;
  48. extern I2C_Interface i2c_hal;
  49. #endif