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