fsl_lpspi_freertos.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_lpspi_freertos.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.lpspi_freertos"
  12. #endif
  13. static void LPSPI_RTOS_Callback(LPSPI_Type *base, lpspi_master_handle_t *drv_handle, status_t status, void *userData)
  14. {
  15. lpspi_rtos_handle_t *handle = (lpspi_rtos_handle_t *)userData;
  16. BaseType_t reschedule;
  17. handle->async_status = status;
  18. (void)xSemaphoreGiveFromISR(handle->event, &reschedule);
  19. portYIELD_FROM_ISR(reschedule);
  20. }
  21. /*!
  22. * brief Initializes LPSPI.
  23. *
  24. * This function initializes the LPSPI module and related RTOS context.
  25. *
  26. * param handle The RTOS LPSPI handle, the pointer to an allocated space for RTOS context.
  27. * param base The pointer base address of the LPSPI instance to initialize.
  28. * param masterConfig Configuration structure to set-up LPSPI in master mode.
  29. * param srcClock_Hz Frequency of input clock of the LPSPI module.
  30. * return status of the operation.
  31. */
  32. status_t LPSPI_RTOS_Init(lpspi_rtos_handle_t *handle,
  33. LPSPI_Type *base,
  34. const lpspi_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(lpspi_rtos_handle_t));
  46. handle->mutex = xSemaphoreCreateMutex();
  47. if (handle->mutex == NULL)
  48. {
  49. return kStatus_Fail;
  50. }
  51. handle->event = xSemaphoreCreateBinary();
  52. if (handle->event == NULL)
  53. {
  54. vSemaphoreDelete(handle->mutex);
  55. return kStatus_Fail;
  56. }
  57. handle->base = base;
  58. LPSPI_MasterInit(handle->base, masterConfig, srcClock_Hz);
  59. LPSPI_MasterTransferCreateHandle(handle->base, &handle->drv_handle, LPSPI_RTOS_Callback, (void *)handle);
  60. return kStatus_Success;
  61. }
  62. /*!
  63. * brief Deinitializes the LPSPI.
  64. *
  65. * This function deinitializes the LPSPI module and related RTOS context.
  66. *
  67. * param handle The RTOS LPSPI handle.
  68. */
  69. status_t LPSPI_RTOS_Deinit(lpspi_rtos_handle_t *handle)
  70. {
  71. LPSPI_Deinit(handle->base);
  72. vSemaphoreDelete(handle->event);
  73. vSemaphoreDelete(handle->mutex);
  74. return kStatus_Success;
  75. }
  76. /*!
  77. * brief Performs SPI transfer.
  78. *
  79. * This function performs an SPI transfer according to data given in the transfer structure.
  80. *
  81. * param handle The RTOS LPSPI handle.
  82. * param transfer Structure specifying the transfer parameters.
  83. * return status of the operation.
  84. */
  85. status_t LPSPI_RTOS_Transfer(lpspi_rtos_handle_t *handle, lpspi_transfer_t *transfer)
  86. {
  87. status_t status;
  88. /* Lock resource mutex */
  89. if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
  90. {
  91. return kStatus_LPSPI_Busy;
  92. }
  93. status = LPSPI_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. if (xSemaphoreTake(handle->event, portMAX_DELAY) != pdTRUE)
  101. {
  102. return kStatus_LPSPI_Error;
  103. }
  104. /* Unlock resource mutex */
  105. (void)xSemaphoreGive(handle->mutex);
  106. /* Return status captured by callback function */
  107. return handle->async_status;
  108. }