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_lpspi_freertos.h"
10
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.lpspi_freertos"
14 #endif
15
LPSPI_RTOS_Callback(LPSPI_Type * base,lpspi_master_handle_t * drv_handle,status_t status,void * userData)16 static void LPSPI_RTOS_Callback(LPSPI_Type *base, lpspi_master_handle_t *drv_handle, status_t status, void *userData)
17 {
18 lpspi_rtos_handle_t *handle = (lpspi_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 LPSPI.
27 *
28 * This function initializes the LPSPI module and related RTOS context.
29 *
30 * param handle The RTOS LPSPI handle, the pointer to an allocated space for RTOS context.
31 * param base The pointer base address of the LPSPI instance to initialize.
32 * param masterConfig Configuration structure to set-up LPSPI in master mode.
33 * param srcClock_Hz Frequency of input clock of the LPSPI module.
34 * return status of the operation.
35 */
LPSPI_RTOS_Init(lpspi_rtos_handle_t * handle,LPSPI_Type * base,const lpspi_master_config_t * masterConfig,uint32_t srcClock_Hz)36 status_t LPSPI_RTOS_Init(lpspi_rtos_handle_t *handle,
37 LPSPI_Type *base,
38 const lpspi_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(lpspi_rtos_handle_t));
52
53 handle->mutex = xSemaphoreCreateMutex();
54 if (handle->mutex == NULL)
55 {
56 return kStatus_Fail;
57 }
58
59 handle->event = xSemaphoreCreateBinary();
60 if (handle->event == NULL)
61 {
62 vSemaphoreDelete(handle->mutex);
63 return kStatus_Fail;
64 }
65
66 handle->base = base;
67
68 LPSPI_MasterInit(handle->base, masterConfig, srcClock_Hz);
69 LPSPI_MasterTransferCreateHandle(handle->base, &handle->drv_handle, LPSPI_RTOS_Callback, (void *)handle);
70
71 return kStatus_Success;
72 }
73
74 /*!
75 * brief Deinitializes the LPSPI.
76 *
77 * This function deinitializes the LPSPI module and related RTOS context.
78 *
79 * param handle The RTOS LPSPI handle.
80 */
LPSPI_RTOS_Deinit(lpspi_rtos_handle_t * handle)81 status_t LPSPI_RTOS_Deinit(lpspi_rtos_handle_t *handle)
82 {
83 LPSPI_Deinit(handle->base);
84 vSemaphoreDelete(handle->event);
85 vSemaphoreDelete(handle->mutex);
86
87 return kStatus_Success;
88 }
89
90 /*!
91 * brief Performs SPI transfer.
92 *
93 * This function performs an SPI transfer according to data given in the transfer structure.
94 *
95 * param handle The RTOS LPSPI handle.
96 * param transfer Structure specifying the transfer parameters.
97 * return status of the operation.
98 */
LPSPI_RTOS_Transfer(lpspi_rtos_handle_t * handle,lpspi_transfer_t * transfer)99 status_t LPSPI_RTOS_Transfer(lpspi_rtos_handle_t *handle, lpspi_transfer_t *transfer)
100 {
101 status_t status;
102
103 /* Lock resource mutex */
104 if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
105 {
106 return kStatus_LPSPI_Busy;
107 }
108
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 /* Unlock resource mutex */
123 (void)xSemaphoreGive(handle->mutex);
124
125 /* Return status captured by callback function */
126 return handle->async_status;
127 }
128