1 /*
2  * Copyright 2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_lpspi_freertos.h"
9 
10 /* Component ID definition, used by tools. */
11 #ifndef FSL_COMPONENT_ID
12 #define FSL_COMPONENT_ID "platform.drivers.lpflexcomm_lpspi_freertos"
13 #endif
14 
LPSPI_RTOS_Callback(LPSPI_Type * base,lpspi_master_handle_t * drv_handle,status_t status,void * userData)15 static void LPSPI_RTOS_Callback(LPSPI_Type *base, lpspi_master_handle_t *drv_handle, status_t status, void *userData)
16 {
17     lpspi_rtos_handle_t *handle = (lpspi_rtos_handle_t *)userData;
18     BaseType_t reschedule = pdFALSE;
19     handle->async_status = status;
20     (void)xSemaphoreGiveFromISR(handle->event, &reschedule);
21     portYIELD_FROM_ISR(reschedule);
22 }
23 
24 /*!
25  * brief Initializes LPSPI.
26  *
27  * This function initializes the LPSPI module and related RTOS context.
28  *
29  * param handle The RTOS LPSPI handle, the pointer to an allocated space for RTOS context.
30  * param base The pointer base address of the LPSPI instance to initialize.
31  * param masterConfig Configuration structure to set-up LPSPI in master mode.
32  * param srcClock_Hz Frequency of input clock of the LPSPI module.
33  * return status of the operation.
34  */
LPSPI_RTOS_Init(lpspi_rtos_handle_t * handle,LPSPI_Type * base,const lpspi_master_config_t * masterConfig,uint32_t srcClock_Hz)35 status_t LPSPI_RTOS_Init(lpspi_rtos_handle_t *handle,
36                          LPSPI_Type *base,
37                          const lpspi_master_config_t *masterConfig,
38                          uint32_t srcClock_Hz)
39 {
40     if (handle == NULL)
41     {
42         return kStatus_InvalidArgument;
43     }
44 
45     if (base == NULL)
46     {
47         return kStatus_InvalidArgument;
48     }
49 
50     (void)memset(handle, 0, sizeof(lpspi_rtos_handle_t));
51 
52     handle->mutex = xSemaphoreCreateMutex();
53     if (handle->mutex == NULL)
54     {
55         return kStatus_Fail;
56     }
57 
58     handle->event = xSemaphoreCreateBinary();
59     if (handle->event == NULL)
60     {
61         vSemaphoreDelete(handle->mutex);
62         return kStatus_Fail;
63     }
64 
65     handle->base = base;
66 
67     (void)LPSPI_MasterInit(handle->base, masterConfig, srcClock_Hz);
68     LPSPI_MasterTransferCreateHandle(handle->base, &handle->drv_handle, LPSPI_RTOS_Callback, (void *)handle);
69 
70     return kStatus_Success;
71 }
72 
73 /*!
74  * brief Deinitializes the LPSPI.
75  *
76  * This function deinitializes the LPSPI module and related RTOS context.
77  *
78  * param handle The RTOS LPSPI handle.
79  */
LPSPI_RTOS_Deinit(lpspi_rtos_handle_t * handle)80 status_t LPSPI_RTOS_Deinit(lpspi_rtos_handle_t *handle)
81 {
82     LPSPI_Deinit(handle->base);
83     vSemaphoreDelete(handle->event);
84     vSemaphoreDelete(handle->mutex);
85 
86     return kStatus_Success;
87 }
88 
89 /*!
90  * brief Performs LPSPI transfer.
91  *
92  * This function performs an LPSPI transfer according to data given in the transfer structure.
93  *
94  * param handle The RTOS LPSPI handle.
95  * param transfer Structure specifying the transfer parameters.
96  * return status of the operation.
97  */
LPSPI_RTOS_Transfer(lpspi_rtos_handle_t * handle,lpspi_transfer_t * transfer)98 status_t LPSPI_RTOS_Transfer(lpspi_rtos_handle_t *handle, lpspi_transfer_t *transfer)
99 {
100     status_t status;
101 
102     /* Lock resource mutex */
103     if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
104     {
105         return kStatus_LPSPI_Busy;
106     }
107 
108     /* Initiate transfer */
109     status = LPSPI_MasterTransferNonBlocking(handle->base, &handle->drv_handle, transfer);
110     if (status != kStatus_Success)
111     {
112         (void)xSemaphoreGive(handle->mutex);
113         return status;
114     }
115 
116     /* Wait for transfer to finish */
117     if (xSemaphoreTake(handle->event, portMAX_DELAY) != pdTRUE)
118     {
119         return kStatus_LPSPI_Error;
120     }
121 
122     /* Retrieve status before releasing mutex */
123     status = handle->async_status;
124 
125     /* Unlock resource mutex */
126     (void)xSemaphoreGive(handle->mutex);
127 
128     /* Translate status of underlying driver */
129     if (status == kStatus_LPSPI_Idle)
130     {
131         status = kStatus_Success;
132     }
133 
134     return status;
135 }
136