fsl_codec_i2c.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2019 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_codec_i2c.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /*******************************************************************************
  13. * Variables
  14. ******************************************************************************/
  15. /*******************************************************************************
  16. * Code
  17. ******************************************************************************/
  18. /*!
  19. * brief Codec i2c bus initilization.
  20. *
  21. * param handle i2c master handle.
  22. * param i2CInstance instance number of the i2c bus, such as 0 is corresponding to I2C0.
  23. * param i2cBaudrate i2c baudrate.
  24. * param i2cSourceClockHz i2c source clock frequency.
  25. * return kStatus_HAL_I2cSuccess is success, else initial failed.
  26. */
  27. status_t CODEC_I2C_Init(void *handle, uint32_t i2cInstance, uint32_t i2cBaudrate, uint32_t i2cSourceClockHz)
  28. {
  29. hal_i2c_master_config_t masterConfig;
  30. masterConfig.enableMaster = true;
  31. masterConfig.baudRate_Bps = i2cBaudrate;
  32. masterConfig.srcClock_Hz = i2cSourceClockHz;
  33. masterConfig.instance = i2cInstance;
  34. return HAL_I2cMasterInit((hal_i2c_master_handle_t *)handle, &masterConfig);
  35. }
  36. /*!
  37. * brief Codec i2c de-initilization.
  38. *
  39. * param handle i2c master handle.
  40. * return kStatus_HAL_I2cSuccess is success, else deinitial failed.
  41. */
  42. status_t CODEC_I2C_Deinit(void *handle)
  43. {
  44. return HAL_I2cMasterDeinit((hal_i2c_master_handle_t *)handle);
  45. }
  46. /*!
  47. * brief codec i2c send function.
  48. *
  49. * param handle i2c master handle.
  50. * param deviceAddress codec device address.
  51. * param subAddress register address.
  52. * param subaddressSize register address width.
  53. * param txBuff tx buffer pointer.
  54. * param txBuffSize tx buffer size.
  55. * return kStatus_HAL_I2cSuccess is success, else send failed.
  56. */
  57. status_t CODEC_I2C_Send(void *handle,
  58. uint8_t deviceAddress,
  59. uint32_t subAddress,
  60. uint8_t subaddressSize,
  61. uint8_t *txBuff,
  62. uint8_t txBuffSize)
  63. {
  64. hal_i2c_master_transfer_t masterXfer;
  65. masterXfer.slaveAddress = deviceAddress;
  66. masterXfer.direction = kHAL_I2cWrite;
  67. masterXfer.subaddress = (uint32_t)subAddress;
  68. masterXfer.subaddressSize = subaddressSize;
  69. masterXfer.data = txBuff;
  70. masterXfer.dataSize = txBuffSize;
  71. masterXfer.flags = kHAL_I2cTransferDefaultFlag;
  72. return HAL_I2cMasterTransferBlocking((hal_i2c_master_handle_t *)handle, &masterXfer);
  73. }
  74. /*!
  75. * brief codec i2c receive function.
  76. *
  77. * param handle i2c master handle.
  78. * param deviceAddress codec device address.
  79. * param subAddress register address.
  80. * param subaddressSize register address width.
  81. * param rxBuff rx buffer pointer.
  82. * param rxBuffSize rx buffer size.
  83. * return kStatus_HAL_I2cSuccess is success, else receive failed.
  84. */
  85. status_t CODEC_I2C_Receive(void *handle,
  86. uint8_t deviceAddress,
  87. uint32_t subAddress,
  88. uint8_t subaddressSize,
  89. uint8_t *rxBuff,
  90. uint8_t rxBuffSize)
  91. {
  92. hal_i2c_master_transfer_t masterXfer;
  93. masterXfer.slaveAddress = deviceAddress;
  94. masterXfer.direction = kHAL_I2cRead;
  95. masterXfer.subaddress = (uint32_t)subAddress;
  96. masterXfer.subaddressSize = subaddressSize;
  97. masterXfer.data = rxBuff;
  98. masterXfer.dataSize = rxBuffSize;
  99. masterXfer.flags = kHAL_I2cTransferDefaultFlag;
  100. return HAL_I2cMasterTransferBlocking((hal_i2c_master_handle_t *)handle, &masterXfer);
  101. }