No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

132 líneas
3.6 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2019 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_i2c_freertos.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.i2c_freertos"
  12. #endif
  13. static void I2C_RTOS_Callback(I2C_Type *base, i2c_master_handle_t *drv_handle, status_t status, void *userData)
  14. {
  15. i2c_rtos_handle_t *handle = (i2c_rtos_handle_t *)userData;
  16. BaseType_t reschedule;
  17. handle->async_status = status;
  18. xSemaphoreGiveFromISR(handle->semaphore, &reschedule);
  19. portYIELD_FROM_ISR(reschedule);
  20. }
  21. /*!
  22. * brief Initializes I2C.
  23. *
  24. * This function initializes the I2C module and the related RTOS context.
  25. *
  26. * param handle The RTOS I2C handle, the pointer to an allocated space for RTOS context.
  27. * param base The pointer base address of the I2C instance to initialize.
  28. * param masterConfig The configuration structure to set-up I2C in master mode.
  29. * param srcClock_Hz The frequency of an input clock of the I2C module.
  30. * return status of the operation.
  31. */
  32. status_t I2C_RTOS_Init(i2c_rtos_handle_t *handle,
  33. I2C_Type *base,
  34. const i2c_master_config_t *masterConfig,
  35. uint32_t srcClock_Hz)
  36. {
  37. if (handle == NULL)
  38. {
  39. return kStatus_InvalidArgument;
  40. }
  41. if (base == NULL)
  42. {
  43. return kStatus_InvalidArgument;
  44. }
  45. memset(handle, 0, sizeof(i2c_rtos_handle_t));
  46. #if (configSUPPORT_STATIC_ALLOCATION == 1)
  47. handle->mutex = xSemaphoreCreateMutexStatic(&handle->mutexBuffer);
  48. #else
  49. handle->mutex = xSemaphoreCreateMutex();
  50. #endif
  51. if (handle->mutex == NULL)
  52. {
  53. return kStatus_Fail;
  54. }
  55. #if (configSUPPORT_STATIC_ALLOCATION == 1)
  56. handle->semaphore = xSemaphoreCreateBinaryStatic(&handle->semaphoreBuffer);
  57. #else
  58. handle->semaphore = xSemaphoreCreateBinary();
  59. #endif
  60. if (handle->semaphore == NULL)
  61. {
  62. vSemaphoreDelete(handle->mutex);
  63. return kStatus_Fail;
  64. }
  65. handle->base = base;
  66. I2C_MasterInit(handle->base, masterConfig, srcClock_Hz);
  67. I2C_MasterTransferCreateHandle(base, &handle->drv_handle, I2C_RTOS_Callback, (void *)handle);
  68. return kStatus_Success;
  69. }
  70. /*!
  71. * brief Deinitializes the I2C.
  72. *
  73. * This function deinitializes the I2C module and the related RTOS context.
  74. *
  75. * param handle The RTOS I2C handle.
  76. */
  77. status_t I2C_RTOS_Deinit(i2c_rtos_handle_t *handle)
  78. {
  79. I2C_MasterDeinit(handle->base);
  80. vSemaphoreDelete(handle->semaphore);
  81. vSemaphoreDelete(handle->mutex);
  82. return kStatus_Success;
  83. }
  84. /*!
  85. * brief Performs the I2C transfer.
  86. *
  87. * This function performs the I2C transfer according to the data given in the transfer structure.
  88. *
  89. * param handle The RTOS I2C handle.
  90. * param transfer A structure specifying the transfer parameters.
  91. * return status of the operation.
  92. */
  93. status_t I2C_RTOS_Transfer(i2c_rtos_handle_t *handle, i2c_master_transfer_t *transfer)
  94. {
  95. status_t status;
  96. /* Lock resource mutex */
  97. if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
  98. {
  99. return kStatus_I2C_Busy;
  100. }
  101. status = I2C_MasterTransferNonBlocking(handle->base, &handle->drv_handle, transfer);
  102. if (status != kStatus_Success)
  103. {
  104. xSemaphoreGive(handle->mutex);
  105. return status;
  106. }
  107. /* Wait for transfer to finish */
  108. (void)xSemaphoreTake(handle->semaphore, portMAX_DELAY);
  109. /* Unlock resource mutex */
  110. xSemaphoreGive(handle->mutex);
  111. /* Return status captured by callback function */
  112. return handle->async_status;
  113. }