1 /*
2 * Copyright 2022 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 #include "fsl_lpi2c_freertos.h"
9
10 /* Component ID definition, used by tools. */
11 #ifndef FSL_COMPONENT_ID
12 #define FSL_COMPONENT_ID "platform.drivers.lpflexcomm_lpi2c_freertos"
13 #endif
14
LPI2C_RTOS_Callback(LPI2C_Type * base,lpi2c_master_handle_t * drv_handle,status_t status,void * userData)15 static void LPI2C_RTOS_Callback(LPI2C_Type *base, lpi2c_master_handle_t *drv_handle, status_t status, void *userData)
16 {
17 lpi2c_rtos_handle_t *handle = (lpi2c_rtos_handle_t *)userData;
18 BaseType_t reschedule = pdFALSE;
19 handle->async_status = status;
20 (void)xSemaphoreGiveFromISR(handle->semaphore, &reschedule);
21 portYIELD_FROM_ISR(reschedule);
22 }
23
24 /*!
25 * brief Initializes LPI2C.
26 *
27 * This function initializes the LPI2C module and the related RTOS context.
28 *
29 * param handle The RTOS LPI2C handle, the pointer to an allocated space for RTOS context.
30 * param base The pointer base address of the LPI2C instance to initialize.
31 * param masterConfig Configuration structure to set-up LPI2C in master mode.
32 * param srcClock_Hz Frequency of input clock of the LPI2C module.
33 * return status of the operation.
34 */
LPI2C_RTOS_Init(lpi2c_rtos_handle_t * handle,LPI2C_Type * base,const lpi2c_master_config_t * masterConfig,uint32_t srcClock_Hz)35 status_t LPI2C_RTOS_Init(lpi2c_rtos_handle_t *handle,
36 LPI2C_Type *base,
37 const lpi2c_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(lpi2c_rtos_handle_t));
51
52 handle->mutex = xSemaphoreCreateMutex();
53 if (handle->mutex == NULL)
54 {
55 return kStatus_Fail;
56 }
57
58 handle->semaphore = xSemaphoreCreateBinary();
59 if (handle->semaphore == NULL)
60 {
61 vSemaphoreDelete(handle->mutex);
62 return kStatus_Fail;
63 }
64
65 handle->base = base;
66
67 LPI2C_MasterInit(handle->base, masterConfig, srcClock_Hz);
68 LPI2C_MasterTransferCreateHandle(base, &handle->drv_handle, LPI2C_RTOS_Callback, (void *)handle);
69
70 return kStatus_Success;
71 }
72
73 /*!
74 * brief Deinitializes the LPI2C.
75 *
76 * This function deinitializes the LPI2C module and the related RTOS context.
77 *
78 * param handle The RTOS LPI2C handle.
79 */
LPI2C_RTOS_Deinit(lpi2c_rtos_handle_t * handle)80 status_t LPI2C_RTOS_Deinit(lpi2c_rtos_handle_t *handle)
81 {
82 LPI2C_MasterDeinit(handle->base);
83 vSemaphoreDelete(handle->semaphore);
84 vSemaphoreDelete(handle->mutex);
85 return kStatus_Success;
86 }
87
88 /*!
89 * brief Performs LPI2C transfer.
90 *
91 * This function performs an LPI2C transfer according to data given in the transfer structure.
92 *
93 * param handle The RTOS LPI2C handle.
94 * param transfer Structure specifying the transfer parameters.
95 * return status of the operation.
96 */
LPI2C_RTOS_Transfer(lpi2c_rtos_handle_t * handle,lpi2c_master_transfer_t * transfer)97 status_t LPI2C_RTOS_Transfer(lpi2c_rtos_handle_t *handle, lpi2c_master_transfer_t *transfer)
98 {
99 status_t status;
100
101 /* Lock resource mutex */
102 if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
103 {
104 return kStatus_LPI2C_Busy;
105 }
106
107 status = LPI2C_MasterTransferNonBlocking(handle->base, &handle->drv_handle, transfer);
108 if (status != kStatus_Success)
109 {
110 (void)xSemaphoreGive(handle->mutex);
111 return status;
112 }
113
114 /* Wait for transfer to finish */
115 (void)xSemaphoreTake(handle->semaphore, portMAX_DELAY);
116
117 /* Unlock resource mutex */
118 (void)xSemaphoreGive(handle->mutex);
119
120 /* Return status captured by callback function */
121 return handle->async_status;
122 }
123