1 /*
2  * Copyright (c) 2016, 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_spi_freertos.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.flexcomm_spi_freertos"
14 #endif
15 
SPI_RTOS_Callback(SPI_Type * base,spi_master_handle_t * drv_handle,status_t status,void * userData)16 static void SPI_RTOS_Callback(SPI_Type *base, spi_master_handle_t *drv_handle, status_t status, void *userData)
17 {
18     spi_rtos_handle_t *handle = (spi_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 SPI.
27  *
28  * This function initializes the SPI module and related RTOS context.
29  *
30  * param handle The RTOS SPI handle, the pointer to an allocated space for RTOS context.
31  * param base The pointer base address of the SPI instance to initialize.
32  * param masterConfig Configuration structure to set-up SPI in master mode.
33  * param srcClock_Hz Frequency of input clock of the SPI module.
34  * return status of the operation.
35  */
SPI_RTOS_Init(spi_rtos_handle_t * handle,SPI_Type * base,const spi_master_config_t * masterConfig,uint32_t srcClock_Hz)36 status_t SPI_RTOS_Init(spi_rtos_handle_t *handle,
37                        SPI_Type *base,
38                        const spi_master_config_t *masterConfig,
39                        uint32_t srcClock_Hz)
40 {
41     status_t status;
42 
43     if (handle == NULL)
44     {
45         return kStatus_InvalidArgument;
46     }
47 
48     if (base == NULL)
49     {
50         return kStatus_InvalidArgument;
51     }
52 
53     (void)memset(handle, 0, sizeof(spi_rtos_handle_t));
54 
55     handle->mutex = xSemaphoreCreateMutex();
56     if (handle->mutex == NULL)
57     {
58         return kStatus_Fail;
59     }
60 
61     handle->event = xSemaphoreCreateBinary();
62     if (handle->event == NULL)
63     {
64         vSemaphoreDelete(handle->mutex);
65         return kStatus_Fail;
66     }
67 
68     handle->base = base;
69 
70     (void)SPI_MasterInit(handle->base, masterConfig, srcClock_Hz);
71     status = SPI_MasterTransferCreateHandle(handle->base, &handle->drv_handle, SPI_RTOS_Callback, (void *)handle);
72 
73     return status;
74 }
75 
76 /*!
77  * brief Deinitializes the SPI.
78  *
79  * This function deinitializes the SPI module and related RTOS context.
80  *
81  * param handle The RTOS SPI handle.
82  */
SPI_RTOS_Deinit(spi_rtos_handle_t * handle)83 status_t SPI_RTOS_Deinit(spi_rtos_handle_t *handle)
84 {
85     SPI_Deinit(handle->base);
86     vSemaphoreDelete(handle->event);
87     vSemaphoreDelete(handle->mutex);
88 
89     return kStatus_Success;
90 }
91 
92 /*!
93  * brief Performs SPI transfer.
94  *
95  * This function performs an SPI transfer according to data given in the transfer structure.
96  *
97  * param handle The RTOS SPI handle.
98  * param transfer Structure specifying the transfer parameters.
99  * return status of the operation.
100  */
SPI_RTOS_Transfer(spi_rtos_handle_t * handle,spi_transfer_t * transfer)101 status_t SPI_RTOS_Transfer(spi_rtos_handle_t *handle, spi_transfer_t *transfer)
102 {
103     status_t status;
104 
105     /* Lock resource mutex */
106     if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
107     {
108         return kStatus_SPI_Busy;
109     }
110 
111     /* Initiate transfer */
112     status = SPI_MasterTransferNonBlocking(handle->base, &handle->drv_handle, transfer);
113     if (status != kStatus_Success)
114     {
115         (void)xSemaphoreGive(handle->mutex);
116         return status;
117     }
118 
119     /* Wait for transfer to finish */
120     if (xSemaphoreTake(handle->event, portMAX_DELAY) != pdTRUE)
121     {
122         return kStatus_SPI_Error;
123     }
124 
125     /* Retrieve status before releasing mutex */
126     status = handle->async_status;
127 
128     /* Unlock resource mutex */
129     (void)xSemaphoreGive(handle->mutex);
130 
131     /* Translate status of underlying driver */
132     if (status == kStatus_SPI_Idle)
133     {
134         status = kStatus_Success;
135     }
136 
137     return status;
138 }
139