You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

135 lines
3.6 KiB

  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_dspi_freertos.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.dspi_freertos"
  12. #endif
  13. static void DSPI_RTOS_Callback(SPI_Type *base, dspi_master_handle_t *drv_handle, status_t status, void *userData)
  14. {
  15. dspi_rtos_handle_t *handle = (dspi_rtos_handle_t *)userData;
  16. BaseType_t reschedule;
  17. handle->async_status = status;
  18. xSemaphoreGiveFromISR(handle->event, &reschedule);
  19. portYIELD_FROM_ISR(reschedule);
  20. }
  21. /*!
  22. * brief Initializes the DSPI.
  23. *
  24. * This function initializes the DSPI module and the related RTOS context.
  25. *
  26. * param handle The RTOS DSPI handle, the pointer to an allocated space for RTOS context.
  27. * param base The pointer base address of the DSPI instance to initialize.
  28. * param masterConfig A configuration structure to set-up the DSPI in master mode.
  29. * param srcClock_Hz A frequency of the input clock of the DSPI module.
  30. * return status of the operation.
  31. */
  32. status_t DSPI_RTOS_Init(dspi_rtos_handle_t *handle,
  33. SPI_Type *base,
  34. const dspi_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(dspi_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->event = xSemaphoreCreateBinaryStatic(&handle->semaphoreBuffer);
  57. #else
  58. handle->event = xSemaphoreCreateBinary();
  59. #endif
  60. if (handle->event == NULL)
  61. {
  62. vSemaphoreDelete(handle->mutex);
  63. return kStatus_Fail;
  64. }
  65. handle->base = base;
  66. DSPI_MasterInit(handle->base, masterConfig, srcClock_Hz);
  67. DSPI_MasterTransferCreateHandle(handle->base, &handle->drv_handle, DSPI_RTOS_Callback, (void *)handle);
  68. return kStatus_Success;
  69. }
  70. /*!
  71. * brief Deinitializes the DSPI.
  72. *
  73. * This function deinitializes the DSPI module and the related RTOS context.
  74. *
  75. * param handle The RTOS DSPI handle.
  76. */
  77. status_t DSPI_RTOS_Deinit(dspi_rtos_handle_t *handle)
  78. {
  79. DSPI_Deinit(handle->base);
  80. vSemaphoreDelete(handle->event);
  81. vSemaphoreDelete(handle->mutex);
  82. return kStatus_Success;
  83. }
  84. /*!
  85. * brief Performs the SPI transfer.
  86. *
  87. * This function performs the SPI transfer according to the data given in the transfer structure.
  88. *
  89. * param handle The RTOS DSPI handle.
  90. * param transfer A structure specifying the transfer parameters.
  91. * return status of the operation.
  92. */
  93. status_t DSPI_RTOS_Transfer(dspi_rtos_handle_t *handle, dspi_transfer_t *transfer)
  94. {
  95. status_t status;
  96. /* Lock resource mutex */
  97. if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
  98. {
  99. return kStatus_DSPI_Busy;
  100. }
  101. status = DSPI_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. if (xSemaphoreTake(handle->event, portMAX_DELAY) != pdTRUE)
  109. {
  110. return kStatus_DSPI_Error;
  111. }
  112. /* Unlock resource mutex */
  113. xSemaphoreGive(handle->mutex);
  114. /* Return status captured by callback function */
  115. return handle->async_status;
  116. }