summaryrefslogtreecommitdiff
path: root/bsp/lpc5410x/Libraries/lpc_chip/chip_5410x/hw_i2csd.c
blob: edab3a9cdfc2d22c59f272173303e53173f8fbc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * @brief I2C slave ROM API declarations and functions
 *
 * @note
 * Copyright(C) NXP Semiconductors, 2014
 * All rights reserved.
 *
 * @par
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * LPC products.  This software is supplied "AS IS" without any warranties of
 * any kind, and NXP Semiconductors and its licensor disclaim any and
 * all warranties, express or implied, including all implied warranties of
 * merchantability, fitness for a particular purpose and non-infringement of
 * intellectual property rights.  NXP Semiconductors assumes no responsibility
 * or liability for the use of the software, conveys no license or rights under any
 * patent, copyright, mask work right, or any other intellectual property rights in
 * or to any products. NXP Semiconductors reserves the right to make changes
 * in the software without notification. NXP Semiconductors also makes no
 * representation or warranty that such application will be suitable for the
 * specified use without further testing or modification.
 *
 * @par
 * Permission to use, copy, modify, and distribute this software and its
 * documentation is hereby granted, under NXP Semiconductors' and its
 * licensor's relevant copyrights in the software, without fee, provided that it
 * is used in conjunction with NXP Semiconductors microcontrollers.  This
 * copyright, permission, and disclaimer notice must appear in all copies of
 * this code.
 */

#include <stdint.h>
#include <string.h>
#include "hw_i2csd.h"

#define DRVVERSION 0x0100

/* Private data structure used for the I2C slave driver, holds the driver and
   peripheral context */
typedef struct {
	void                    *pUserData;		/*!< Pointer to user data used by driver instance, use NULL if not used */
	LPC_I2C_T               *base;			/*!< Base address of I2C peripheral to use */
	i2cSlaveStartCB         pXferStartCB;	/*!< Transfer start callback */
	i2cSlaveTransmitCB      pTranTranCb;	/*!< Data transmit callback */
	i2cSlaveReceiveCB       pTranRecvCb;	/*!< Data Receive callback */
	i2cSlaveCompleteCB      pXferCompCB;	/*!< Transfer complete callback */
	ROM_I2CS_XFER_T         *pXfer;			/*!< Pointer to current transfer */
	ErrorCode_t             pendingStatus;	/*!< Pending transfer status */
} I2CS_DATACONTEXT_T;

#define _rom_i2csEnable(pI2C)                   (pI2C->CFG |= I2C_CFG_SLVEN);
#define _rom_i2csGetSlaveAddr(pI2C, slvNum)     ((pI2C->SLVADR[slvNum] >> 1) & 0x7F)
#define _rom_i2csGetSlaveMatchIndex(pI2C)       ((pI2C->STAT & I2C_STAT_SLVIDX) >> 12)
#define _rom_i2csGetSlaveState(pI2C)            (((pI2C->STAT & I2C_STAT_SLVSTATE) >> 9) & 0x3)

// **********************************************************
uint32_t i2cs_get_mem_size(void)
{
	return sizeof(I2CS_DATACONTEXT_T);
}

ROM_I2CS_HANDLE_T i2cs_init(void *mem, const ROM_I2CS_INIT_T *pInit)
{
	I2CS_DATACONTEXT_T *pDrv;

	/* Verify alignment is at least 4 bytes */
	if (((uint32_t) mem & 0x3) != 0) {
		return NULL;
	}

	pDrv = (I2CS_DATACONTEXT_T *) mem;
	memset(pDrv, 0, sizeof(I2CS_DATACONTEXT_T));

	/* Save base of peripheral and pointer to user data */
	pDrv->pUserData = pInit->pUserData;
	pDrv->base = (LPC_I2C_T *) pInit->base;

	/* If this needs to be changed, it should be done in the app after
	   this call. */
	pDrv->base->CLKDIV = 2;

	/* Clear controller state */
	pDrv->base->STAT = (I2C_STAT_SLVSEL | I2C_STAT_SLVDESEL);

	/* Enable I2C slave interface */
	_rom_i2csEnable(pDrv->base);

	return pDrv;
}

void i2cs_setup_slave(ROM_I2CS_HANDLE_T pHandle, ROM_I2CS_SLAVE_T *pSlaveSetup)
{
	uint32_t sa, idx;
	I2CS_DATACONTEXT_T *pDrv = (I2CS_DATACONTEXT_T *) pHandle;

	/* Limit usable slave address indexes to the maximum the controller can support */
	if (pSlaveSetup->SlaveIndex <= 3) {
		sa = (uint32_t) (pSlaveSetup->slaveAddr & 0x7F) << 1;
		if (pSlaveSetup->EnableSlave == 0) {
			sa |= I2C_SLVADR_SADISABLE;	/* Disable slave address */
		}

		/* Setup slave address at index */
		pDrv->base->SLVADR[pSlaveSetup->SlaveIndex] = sa;
	}

	/* Check all slave indexes. If any are enabled, then enable the slave interrupts,
	   else disable the slave interrupts. */
	sa = 0;
	for (idx = 0; ((idx <= 3) && (sa == 0)); idx++) {
		if ((pDrv->base->SLVADR[idx] & I2C_SLVADR_SADISABLE) == 0) {
			/* Slave is enabled */
			sa = 1;
		}
	}
	if (sa) {
		pDrv->base->INTENSET = I2C_INTENSET_SLVPENDING;

	}
	else {
		pDrv->base->INTENCLR = (I2C_INTENSET_SLVPENDING | I2C_INTENSET_SLVDESEL);
	}
}

void i2cs_register_callback(ROM_I2CS_HANDLE_T pHandle, uint32_t cbIndex, void *pCB)
{
	I2CS_DATACONTEXT_T *pDrv = (I2CS_DATACONTEXT_T *) pHandle;

	if (cbIndex == ROM_I2CS_START_CB) {
		pDrv->pXferStartCB = (i2cSlaveStartCB) pCB;
	}
	else if (cbIndex == ROM_I2CS_XFERSEND_CB) {
		pDrv->pTranTranCb = (i2cSlaveTransmitCB) pCB;
	}
	else if (cbIndex == ROM_I2CS_XFERRECV_CB) {
		pDrv->pTranRecvCb = (i2cSlaveReceiveCB) pCB;
	}
	else if (cbIndex == ROM_I2CS_DONE_CB) {
		pDrv->pXferCompCB = (i2cSlaveCompleteCB) pCB;
	}
}

ErrorCode_t i2cs_transfer(ROM_I2CS_HANDLE_T pHandle, ROM_I2CS_XFER_T *pXfer)
{
	I2CS_DATACONTEXT_T *pDrv = (I2CS_DATACONTEXT_T *) pHandle;

	/* Is transfer NULL? */
	if (pXfer == NULL) {
		return ERR_I2C_PARAM;
	}

	/* Save transfer descriptor */
	pDrv->pXfer = pXfer;
	pXfer->status = ERR_I2C_BUSY;
	pDrv->pendingStatus = LPC_OK;
	pXfer->bytesSent = 0;
	pXfer->bytesRecv = 0;

	return pXfer->status;
}

// Otime = "optimize for speed of code execution"
// ...add this pragma 1 line above the interrupt service routine function.
void i2cs_transfer_handler(ROM_I2CS_HANDLE_T pHandle)
{
	I2CS_DATACONTEXT_T *pDrv = (I2CS_DATACONTEXT_T *) pHandle;
	ROM_I2CS_XFER_T *pXfer = pDrv->pXfer;

	uint32_t done = 0;
	uint16_t data = 0;

	uint32_t status = pDrv->base->INTSTAT;

	/* Transfer complete? */
	if ((status & I2C_INTENSET_SLVDESEL) != 0) {
		pDrv->base->INTENCLR = I2C_INTENSET_SLVDESEL;
		pDrv->base->STAT = I2C_STAT_SLVDESEL;
		if (pXfer) {
			pXfer->status = pDrv->pendingStatus;
			pDrv->pXfer = NULL;
		}
		if (pDrv->pXferCompCB) {
			pDrv->pXferCompCB(pHandle, pXfer);
		}
		return;
	}
	else if ((status & I2C_INTENSET_SLVPENDING) != 0) {
		/* Determine the current I2C slave state */
		switch (_rom_i2csGetSlaveState(pDrv->base)) {
		case I2C_STAT_SLVCODE_ADDR:
			/* Get slave address that needs servicing */
			data = _rom_i2csGetSlaveAddr(pDrv->base, _rom_i2csGetSlaveMatchIndex(pDrv->base));

			/* Call address callback */
			if (pDrv->pXferStartCB) {
				pDrv->pXferStartCB(pHandle, data);

				/* Update transfer descriptor */
				pXfer = pDrv->pXfer;
			}
			pDrv->base->INTENSET = I2C_INTENSET_SLVDESEL;
			break;

		case I2C_STAT_SLVCODE_RX:
			/* Receive from master */
			/* A byte has been received in thee receive FIFO */
			if ((pXfer == NULL) || (pXfer->bytesRecv >= pXfer->rxSz)) {
				/* No more data, call receive data callback */
				if (pDrv->pTranRecvCb) {
					done = pDrv->pTranRecvCb(pHandle, pXfer);
					if (pDrv->pXfer) {
						pXfer = pDrv->pXfer;
						pXfer->bytesRecv = 0;
					}
				}
			}

			/* Not using DMA */
			if (!(done == ROM_I2CS_DMA)) {
				data = (uint8_t) pDrv->base->SLVDAT;
				if (pXfer == NULL) {
					/* Toss data and NAK, no buffer space */
					done = ROM_I2CS_NAK;
					pDrv->pendingStatus = ERR_I2C_BUFFER_OVERFLOW;
				}
				else {
					uint8_t *p8 = pXfer->rxBuff;
					if ((p8 == NULL) || (pXfer->bytesRecv >= pXfer->rxSz)) {
						/* Toss data and NAK, no buffer space */
						done = ROM_I2CS_NAK;
						pDrv->pendingStatus = ERR_I2C_BUFFER_OVERFLOW;
					}
					else {
						p8[pXfer->bytesRecv] = (uint8_t) data;
						pDrv->pXfer->bytesRecv++;
					}
				}
			}
			break;

		case I2C_STAT_SLVCODE_TX:
			/* Send to master */
			/* A byte needs to be placed into the transmit FIFO */
			if ((pXfer == NULL) || (pXfer->bytesSent >= pXfer->txSz)) {
				/* Does callback exist? */
				if (pDrv->pTranTranCb) {
					done = pDrv->pTranTranCb(pHandle, pXfer);
					/* Can't really NAK on read, so switch to continue */
					if (pDrv->pXfer) {
						pXfer = pDrv->pXfer;
						pXfer->bytesSent = 0;
					}
					if (done == ROM_I2CS_NAK) {
						pDrv->base->SLVDAT = 0;
					}
				}
			}

			/* Continue if not DMA or NAK */
			if (!((done == ROM_I2CS_NAK) || (done == ROM_I2CS_DMA))) {
				if (pXfer == NULL) {
					pDrv->base->SLVDAT = 0;
					pDrv->pendingStatus = ERR_I2C_BUFFER_UNDERFLOW;
					done = 0;
				}
				else {
					uint8_t *p8 = (uint8_t *) pXfer->txBuff;
					/* Not using DMA, so this is a normal transfer */
					if ((p8 == NULL) || (pXfer->bytesSent >= pXfer->txSz)) {
						/* Have to send something, so NAK with 0 */
						pDrv->base->SLVDAT = 0;
						pDrv->pendingStatus = ERR_I2C_BUFFER_UNDERFLOW;
						done = 0;
					}
					else {
						pDrv->base->SLVDAT = (uint32_t) p8[pXfer->bytesSent];
						pDrv->pXfer->bytesSent++;
					}
				}
			}
			break;
		}

		if (done == ROM_I2CS_NAK) {
			pDrv->base->SLVCTL = I2C_SLVCTL_SLVNACK;
		}
		else if (done == ROM_I2CS_DMA) {
			pDrv->base->SLVCTL = I2C_SLVCTL_SLVDMA;
		}
		else {
			pDrv->base->SLVCTL = I2C_SLVCTL_SLVCONTINUE;
		}
	}
}

uint32_t i2cs_get_driver_version(void)
{
	return DRVVERSION;
}

// *********************************************************