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 
9 #include "fsl_dspi_freertos.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.dspi_freertos"
14 #endif
15 
DSPI_RTOS_Callback(SPI_Type * base,dspi_master_handle_t * drv_handle,status_t status,void * userData)16 static void DSPI_RTOS_Callback(SPI_Type *base, dspi_master_handle_t *drv_handle, status_t status, void *userData)
17 {
18     dspi_rtos_handle_t *handle = (dspi_rtos_handle_t *)userData;
19     BaseType_t reschedule = pdFALSE;
20     handle->async_status = status;
21     (void)xSemaphoreGiveFromISR(handle->event, &reschedule);
22     portYIELD_FROM_ISR(reschedule);
23 }
24 
25 /*!
26  * brief Initializes the DSPI.
27  *
28  * This function initializes the DSPI module and the related RTOS context.
29  *
30  * param handle The RTOS DSPI handle, the pointer to an allocated space for RTOS context.
31  * param base The pointer base address of the DSPI instance to initialize.
32  * param masterConfig A configuration structure to set-up the DSPI in master mode.
33  * param srcClock_Hz A frequency of the input clock of the DSPI module.
34  * return status of the operation.
35  */
DSPI_RTOS_Init(dspi_rtos_handle_t * handle,SPI_Type * base,const dspi_master_config_t * masterConfig,uint32_t srcClock_Hz)36 status_t DSPI_RTOS_Init(dspi_rtos_handle_t *handle,
37                         SPI_Type *base,
38                         const dspi_master_config_t *masterConfig,
39                         uint32_t srcClock_Hz)
40 {
41     if (handle == NULL)
42     {
43         return kStatus_InvalidArgument;
44     }
45 
46     if (base == NULL)
47     {
48         return kStatus_InvalidArgument;
49     }
50 
51     (void)memset(handle, 0, sizeof(dspi_rtos_handle_t));
52 
53 #if (configSUPPORT_STATIC_ALLOCATION == 1)
54     handle->mutex = xSemaphoreCreateMutexStatic(&handle->mutexBuffer);
55 #else
56     handle->mutex = xSemaphoreCreateMutex();
57 #endif
58     if (handle->mutex == NULL)
59     {
60         return kStatus_Fail;
61     }
62 #if (configSUPPORT_STATIC_ALLOCATION == 1)
63     handle->event = xSemaphoreCreateBinaryStatic(&handle->semaphoreBuffer);
64 #else
65     handle->event = xSemaphoreCreateBinary();
66 #endif
67     if (handle->event == NULL)
68     {
69         vSemaphoreDelete(handle->mutex);
70         return kStatus_Fail;
71     }
72 
73     handle->base = base;
74 
75     DSPI_MasterInit(handle->base, masterConfig, srcClock_Hz);
76     DSPI_MasterTransferCreateHandle(handle->base, &handle->drv_handle, DSPI_RTOS_Callback, (void *)handle);
77 
78     return kStatus_Success;
79 }
80 
81 /*!
82  * brief Deinitializes the DSPI.
83  *
84  * This function deinitializes the DSPI module and the related RTOS context.
85  *
86  * param handle The RTOS DSPI handle.
87  */
DSPI_RTOS_Deinit(dspi_rtos_handle_t * handle)88 status_t DSPI_RTOS_Deinit(dspi_rtos_handle_t *handle)
89 {
90     DSPI_Deinit(handle->base);
91     vSemaphoreDelete(handle->event);
92     vSemaphoreDelete(handle->mutex);
93 
94     return kStatus_Success;
95 }
96 
97 /*!
98  * brief Performs the SPI transfer.
99  *
100  * This function performs the SPI transfer according to the data given in the transfer structure.
101  *
102  * param handle The RTOS DSPI handle.
103  * param transfer A structure specifying the transfer parameters.
104  * return status of the operation.
105  */
DSPI_RTOS_Transfer(dspi_rtos_handle_t * handle,dspi_transfer_t * transfer)106 status_t DSPI_RTOS_Transfer(dspi_rtos_handle_t *handle, dspi_transfer_t *transfer)
107 {
108     status_t status;
109 
110     /* Lock resource mutex */
111     if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
112     {
113         return kStatus_DSPI_Busy;
114     }
115 
116     status = DSPI_MasterTransferNonBlocking(handle->base, &handle->drv_handle, transfer);
117     if (status != kStatus_Success)
118     {
119         (void)xSemaphoreGive(handle->mutex);
120         return status;
121     }
122 
123     /* Wait for transfer to finish */
124     if (xSemaphoreTake(handle->event, portMAX_DELAY) != pdTRUE)
125     {
126         return kStatus_DSPI_Error;
127     }
128 
129     /* Unlock resource mutex */
130     (void)xSemaphoreGive(handle->mutex);
131 
132     /* Return status captured by callback function */
133     return handle->async_status;
134 }
135