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_i2c_freertos.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.i2c_freertos"
14 #endif
15 
I2C_RTOS_Callback(I2C_Type * base,i2c_master_handle_t * drv_handle,status_t status,void * userData)16 static void I2C_RTOS_Callback(I2C_Type *base, i2c_master_handle_t *drv_handle, status_t status, void *userData)
17 {
18     i2c_rtos_handle_t *handle = (i2c_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 I2C.
27  *
28  * This function initializes the I2C module and the related RTOS context.
29  *
30  * param handle The RTOS I2C handle, the pointer to an allocated space for RTOS context.
31  * param base The pointer base address of the I2C instance to initialize.
32  * param masterConfig The configuration structure to set-up I2C in master mode.
33  * param srcClock_Hz The frequency of an input clock of the I2C module.
34  * return status of the operation.
35  */
I2C_RTOS_Init(i2c_rtos_handle_t * handle,I2C_Type * base,const i2c_master_config_t * masterConfig,uint32_t srcClock_Hz)36 status_t I2C_RTOS_Init(i2c_rtos_handle_t *handle,
37                        I2C_Type *base,
38                        const i2c_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(i2c_rtos_handle_t));
52 #if (configSUPPORT_STATIC_ALLOCATION == 1)
53     handle->mutex = xSemaphoreCreateMutexStatic(&handle->mutexBuffer);
54 #else
55     handle->mutex     = xSemaphoreCreateMutex();
56 #endif
57     if (handle->mutex == NULL)
58     {
59         return kStatus_Fail;
60     }
61 #if (configSUPPORT_STATIC_ALLOCATION == 1)
62     handle->semaphore = xSemaphoreCreateBinaryStatic(&handle->semaphoreBuffer);
63 #else
64     handle->semaphore = xSemaphoreCreateBinary();
65 #endif
66     if (handle->semaphore == NULL)
67     {
68         vSemaphoreDelete(handle->mutex);
69         return kStatus_Fail;
70     }
71 
72     handle->base = base;
73 
74     I2C_MasterInit(handle->base, masterConfig, srcClock_Hz);
75     I2C_MasterTransferCreateHandle(base, &handle->drv_handle, I2C_RTOS_Callback, (void *)handle);
76 
77     return kStatus_Success;
78 }
79 
80 /*!
81  * brief Deinitializes the I2C.
82  *
83  * This function deinitializes the I2C module and the related RTOS context.
84  *
85  * param handle The RTOS I2C handle.
86  */
I2C_RTOS_Deinit(i2c_rtos_handle_t * handle)87 status_t I2C_RTOS_Deinit(i2c_rtos_handle_t *handle)
88 {
89     I2C_MasterDeinit(handle->base);
90 
91     vSemaphoreDelete(handle->semaphore);
92     vSemaphoreDelete(handle->mutex);
93 
94     return kStatus_Success;
95 }
96 
97 /*!
98  * brief Performs the I2C transfer.
99  *
100  * This function performs the I2C transfer according to the data given in the transfer structure.
101  *
102  * param handle The RTOS I2C handle.
103  * param transfer A structure specifying the transfer parameters.
104  * return status of the operation.
105  */
I2C_RTOS_Transfer(i2c_rtos_handle_t * handle,i2c_master_transfer_t * transfer)106 status_t I2C_RTOS_Transfer(i2c_rtos_handle_t *handle, i2c_master_transfer_t *transfer)
107 {
108     status_t status;
109 
110     /* Lock resource mutex */
111     if (xSemaphoreTake(handle->mutex, portMAX_DELAY) != pdTRUE)
112     {
113         return kStatus_I2C_Busy;
114     }
115 
116     status = I2C_MasterTransferNonBlocking(handle->base, &handle->drv_handle, transfer);
117     if (status != kStatus_Success)
118     {
119         (void)xSemaphoreGive(handle->mutex);
120         return status;
121     }
122 
123     /* Wait for transfer to finish */
124     (void)xSemaphoreTake(handle->semaphore, portMAX_DELAY);
125 
126     /* Unlock resource mutex */
127     (void)xSemaphoreGive(handle->mutex);
128 
129     /* Return status captured by callback function */
130     return handle->async_status;
131 }
132