fsl_lpi2c_freertos.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_lpi2c_freertos.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.lpi2c_freertos"
  12. #endif
  13. static void LPI2C_RTOS_Callback(LPI2C_Type *base, lpi2c_master_handle_t *drv_handle, status_t status, void *userData)
  14. {
  15. lpi2c_rtos_handle_t *handle = (lpi2c_rtos_handle_t *)userData;
  16. BaseType_t reschedule;
  17. handle->async_status = status;
  18. (void)xSemaphoreGiveFromISR(handle->semaphore, &reschedule);
  19. portYIELD_FROM_ISR(reschedule);
  20. }
  21. /*!
  22. * brief Initializes LPI2C.
  23. *
  24. * This function initializes the LPI2C module and related RTOS context.
  25. *
  26. * param handle The RTOS LPI2C handle, the pointer to an allocated space for RTOS context.
  27. * param base The pointer base address of the LPI2C instance to initialize.
  28. * param masterConfig Configuration structure to set-up LPI2C in master mode.
  29. * param srcClock_Hz Frequency of input clock of the LPI2C module.
  30. * return status of the operation.
  31. */
  32. status_t LPI2C_RTOS_Init(lpi2c_rtos_handle_t *handle,
  33. LPI2C_Type *base,
  34. const lpi2c_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. (void)memset(handle, 0, sizeof(lpi2c_rtos_handle_t));
  46. handle->mutex = xSemaphoreCreateMutex();
  47. if (handle->mutex == NULL)
  48. {
  49. return kStatus_Fail;
  50. }
  51. handle->semaphore = xSemaphoreCreateBinary();
  52. if (handle->semaphore == NULL)
  53. {
  54. vSemaphoreDelete(handle->mutex);
  55. return kStatus_Fail;
  56. }
  57. handle->base = base;
  58. LPI2C_MasterInit(handle->base, masterConfig, srcClock_Hz);
  59. LPI2C_MasterTransferCreateHandle(base, &handle->drv_handle, LPI2C_RTOS_Callback, (void *)handle);
  60. return kStatus_Success;
  61. }
  62. /*!
  63. * brief Deinitializes the LPI2C.
  64. *
  65. * This function deinitializes the LPI2C module and related RTOS context.
  66. *
  67. * param handle The RTOS LPI2C handle.
  68. */
  69. status_t LPI2C_RTOS_Deinit(lpi2c_rtos_handle_t *handle)
  70. {
  71. LPI2C_MasterDeinit(handle->base);
  72. vSemaphoreDelete(handle->semaphore);
  73. vSemaphoreDelete(handle->mutex);
  74. return kStatus_Success;
  75. }
  76. /*!
  77. * brief Performs I2C transfer.
  78. *
  79. * This function performs an I2C transfer using LPI2C module according to data given in the transfer structure.
  80. *
  81. * param handle The RTOS LPI2C handle.
  82. * param transfer Structure specifying the transfer parameters.
  83. * return status of the operation.
  84. */
  85. status_t LPI2C_RTOS_Transfer(lpi2c_rtos_handle_t *handle, lpi2c_master_transfer_t *transfer)
  86. {
  87. status_t status;
  88. /* Lock resource mutex */
  89. if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
  90. {
  91. return kStatus_LPI2C_Busy;
  92. }
  93. status = LPI2C_MasterTransferNonBlocking(handle->base, &handle->drv_handle, transfer);
  94. if (status != kStatus_Success)
  95. {
  96. (void)xSemaphoreGive(handle->mutex);
  97. return status;
  98. }
  99. /* Wait for transfer to finish */
  100. (void)xSemaphoreTake(handle->semaphore, portMAX_DELAY);
  101. /* Unlock resource mutex */
  102. (void)xSemaphoreGive(handle->mutex);
  103. /* Return status captured by callback function */
  104. return handle->async_status;
  105. }