hw_i2cmd.c 11.5 KB
Newer Older
hillcode's avatar
hillcode 已提交
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/*
 * @brief I2C master 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_i2cmd.h"

#define DRVVERSION 0x0100

/* Private data structure used for the I2C master 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 */
	i2cMasterCompleteCB         pXferCompCB;	/*!< Transfer complete callback */
	i2cMasterTransmitStartCB    pTranStartCb;	/*!< Transmit data start callback */
	i2cMasterReceiveStartCB     pTranRecvCb;	/*!< Receive data start callback */
	ROM_I2CM_XFER_T             *pXfer;			/*!< Pointer to current transfer */
	ErrorCode_t                 pendingStatus;	/*!< Pending master transfer status before clocking transfer */
	uint16_t                    sendIdx;
	uint16_t                    recvIdx;
} I2CM_DATACONTEXT_T;

#define _rom_i2cmEnable(pI2C)           (pI2C->CFG |= I2C_CFG_MSTEN);
#define _rom_i2cmGetMasterState(pI2C)   ((pI2C->STAT & I2C_STAT_MSTSTATE) >> 1)

/* Sets I2C Clock Divider registers */
static void _rom_i2cmSetClockDiv(LPC_I2C_T *pI2C, uint32_t clkdiv)
{
	if ((clkdiv >= 1) && (clkdiv <= 65536)) {
		pI2C->CLKDIV = clkdiv - 1;
	}
	else {
		pI2C->CLKDIV = 0;
	}
}

/* Sets HIGH and LOW duty cycle registers */
static void _rom_i2cmSetDutyCycle(LPC_I2C_T *pI2C, uint16_t sclH, uint16_t sclL)
{
	/* Limit to usable range of timing values */
	if (sclH < 2) {
		sclH = 2;
	}
	else if (sclH > 9) {
		sclH = 9;
	}
	if (sclL < 2) {
		sclL = 2;
	}
	else if (sclL > 9) {
		sclL = 9;
	}

	pI2C->MSTTIME = (((sclH - 2) & 0x07) << 4) | ((sclL - 2) & 0x07);
}

// **********************************************************
uint32_t i2cm_get_mem_size(void)
{
	return sizeof(I2CM_DATACONTEXT_T);
}

ROM_I2CM_HANDLE_T i2cm_init(void *mem, const ROM_I2CM_INIT_T *pInit)
{
	I2CM_DATACONTEXT_T *pDrv;

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

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

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

	/* Pick a safe clock divider until clock rate is setup */
	_rom_i2cmSetClockDiv(pDrv->base, 8);

	/* Clear pending master statuses */
	pDrv->base->STAT = (I2C_STAT_MSTRARBLOSS | I2C_STAT_MSTSTSTPERR);

	/* Enable I2C master interface */
	_rom_i2cmEnable(pDrv->base);

	return pDrv;
}

uint32_t i2cm_set_clock_rate(ROM_I2CM_HANDLE_T pHandle, uint32_t inRate, uint32_t i2cRate)
{
	uint32_t scl, div;
	I2CM_DATACONTEXT_T *pDrv = (I2CM_DATACONTEXT_T *) pHandle;

	/* Determine the best I2C clock dividers to generate the target I2C master clock */
	/* The maximum SCL and SCH dividers are 7, for a maximum divider set of 14 */
	/* The I2C master divider is between 1 and 65536. */

	/* Pick a main I2C divider that allows centered SCL/SCH dividers */
	div = inRate / (i2cRate << 3);
	if (div == 0) {
		div = 1;
	}
	_rom_i2cmSetClockDiv(pDrv->base, div);

	/* Determine SCL/SCH dividers */
	scl = inRate / (div * i2cRate);
	_rom_i2cmSetDutyCycle(pDrv->base, (scl >> 1), (scl - (scl >> 1)));

	return inRate / (div * scl);
}

void i2cm_register_callback(ROM_I2CM_HANDLE_T pHandle, uint32_t cbIndex, void *pCB)
{
	I2CM_DATACONTEXT_T *pDrv = (I2CM_DATACONTEXT_T *) pHandle;

	if (cbIndex == ROM_I2CM_DATACOMPLETE_CB) {
		pDrv->pXferCompCB = (i2cMasterCompleteCB) pCB;
	}
	else if (cbIndex == ROM_I2CM_DATATRANSMITSTART_CB) {
		pDrv->pTranStartCb = (i2cMasterTransmitStartCB) pCB;
	}
	else if (cbIndex == ROM_I2CM_DATATRECEIVESTART_CB) {
		pDrv->pTranRecvCb = (i2cMasterReceiveStartCB) pCB;
	}
}

ErrorCode_t i2cm_transfer(ROM_I2CM_HANDLE_T pHandle, ROM_I2CM_XFER_T *pXfer)
{
	I2CM_DATACONTEXT_T *pDrv = (I2CM_DATACONTEXT_T *) pHandle;

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

	/* I2C master controller should be pending and idle */
	if ((pDrv->base->STAT & I2C_STAT_MSTPENDING) == 0) {
		pXfer->status = ERR_I2C_GENERAL_FAILURE;
		return ERR_I2C_GENERAL_FAILURE;
	}
	if (_rom_i2cmGetMasterState(pDrv->base) != I2C_STAT_MSTCODE_IDLE) {
		pXfer->status = ERR_I2C_GENERAL_FAILURE;
		return ERR_I2C_GENERAL_FAILURE;
	}

	/* Save transfer descriptor */
	pDrv->pXfer = pXfer;
	pXfer->status = ERR_I2C_BUSY;
	pDrv->sendIdx = 0;
	pDrv->recvIdx = 0;

	/* Pending status for completion of trasnfer */
	pDrv->pendingStatus = ERR_I2C_GENERAL_FAILURE;

	/* Clear controller state */
	pDrv->base->STAT = (I2C_STAT_MSTRARBLOSS | I2C_STAT_MSTSTSTPERR);

	/* Will always transisiton to idle at start or end of transfer */
	if (pXfer->txSz) {
		/* Call transmit start callback to setup TX DMA if needed */
		if (pDrv->pTranStartCb) {
			pDrv->pTranStartCb(pHandle, pXfer);
		}

		/* Start transmit state */
		pDrv->base->MSTDAT = (uint32_t) (pXfer->slaveAddr << 1);
		pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTART;
	}
	else if (pXfer->rxSz) {
		/* Start receive state with start ot repeat start */
		pDrv->base->MSTDAT = (uint32_t) (pXfer->slaveAddr << 1) | 0x1;
		pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTART;

		/* Call receive start callback to setup RX DMA if needed */
		if (pDrv->pTranRecvCb) {
			pDrv->pTranRecvCb(pHandle, pXfer);
		}
	}
	else {
		/* No data - either via data callbacks or a slave query only */
		pDrv->base->MSTDAT = (uint32_t) (pXfer->slaveAddr << 1);
		pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTART;
	}

	/* Enable supported master interrupts */
	pDrv->base->INTENSET = (I2C_INTENSET_MSTPENDING | I2C_INTENSET_MSTRARBLOSS |
							I2C_INTENSET_MSTSTSTPERR);

	/* Does the driver need to block? */
	if ((pXfer->flags & ROM_I2CM_FLAG_BLOCKING) != 0) {
		while (pXfer->status == ERR_I2C_BUSY) {
			i2cm_transfer_handler(pHandle);
		}
	}

	return pXfer->status;
}

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

	uint32_t status = pDrv->base->STAT;

	if (status & I2C_STAT_MSTRARBLOSS) {
		/* Master Lost Arbitration */
		/* Set transfer status as Arbitration Lost */
		pDrv->pendingStatus = ERR_I2C_LOSS_OF_ARBRITRATION;

		/* Clear Status Flags */
		pDrv->base->STAT = I2C_STAT_MSTRARBLOSS;

		pDrv->base->INTENCLR = (I2C_INTENSET_MSTPENDING | I2C_INTENSET_MSTRARBLOSS |
								I2C_INTENSET_MSTSTSTPERR);
		pXfer->status = pDrv->pendingStatus;
		if (pDrv->pXferCompCB != NULL) {
			pDrv->pXferCompCB(pHandle, pXfer);
		}
	}
	else if (status & I2C_STAT_MSTSTSTPERR) {
		/* Master Start Stop Error */
		/* Set transfer status as Bus Error */
		pDrv->pendingStatus = ERR_I2C_GENERAL_FAILURE;

		/* Clear Status Flags */
		pDrv->base->STAT = I2C_STAT_MSTSTSTPERR;

		pDrv->base->INTENCLR = (I2C_INTENSET_MSTPENDING | I2C_INTENSET_MSTRARBLOSS |
								I2C_INTENSET_MSTSTSTPERR);
		pXfer->status = pDrv->pendingStatus;
		if (pDrv->pXferCompCB != NULL) {
			pDrv->pXferCompCB(pHandle, pXfer);
		}
	}
	else if (status & I2C_STAT_MSTPENDING) {
		/* Master is Pending */
		/* Branch based on Master State Code */
		switch (_rom_i2cmGetMasterState(pDrv->base)) {
		case I2C_STAT_MSTCODE_IDLE:	/* Master idle */
			/* Idle state is only called on completion of transfer */
			/* Disable interrupts */
			pDrv->base->INTENCLR = (I2C_INTENSET_MSTPENDING | I2C_INTENSET_MSTRARBLOSS |
									I2C_INTENSET_MSTSTSTPERR);

			/* Update status and call transfer completion callback */
			pXfer->status = pDrv->pendingStatus;
			if (pDrv->pXferCompCB != NULL) {
				pDrv->pXferCompCB(pHandle, pXfer);
			}
			break;

		case I2C_STAT_MSTCODE_RXREADY:	/* Receive data is available */
			if (((pXfer->flags & ROM_I2CM_FLAG_DMARX) != 0) && (pXfer->rxSz > 0)) {
				/* Use DMA for receive */
				pDrv->base->MSTCTL = I2C_MSTCTL_MSTDMA;
				pXfer->flags &= ~ROM_I2CM_FLAG_DMARX;
				pXfer->rxSz = 0;
				return;
			}
			else if (pXfer->rxSz) {
				uint8_t *p8 = pXfer->rxBuff;
				p8[pDrv->recvIdx] = (uint8_t) pDrv->base->MSTDAT & 0xFF;
				pDrv->recvIdx++;
				pXfer->rxSz--;
			}

			if (pXfer->rxSz) {
				pDrv->base->MSTCTL = I2C_MSTCTL_MSTCONTINUE;
			}
			else {
				/* Last byte to receive, send stop after byte received */
				pDrv->base->MSTCTL = I2C_MSTCTL_MSTCONTINUE | I2C_MSTCTL_MSTSTOP;
				pDrv->pendingStatus = LPC_OK;
			}
			break;

		case I2C_STAT_MSTCODE_TXREADY:	/* Master Transmit available */
			if (((pXfer->flags & ROM_I2CM_FLAG_DMATX) != 0) && (pXfer->txSz > 0)) {
				/* Use DMA for transmit */
				pDrv->base->MSTCTL = I2C_MSTCTL_MSTDMA;
				pXfer->flags &= ~ROM_I2CM_FLAG_DMATX;
				pXfer->txSz = 0;
				return;
			}
			else if (pXfer->txSz) {
				uint8_t *p8 = (uint8_t *) pXfer->txBuff;
				/* If Tx data available transmit data and continue */
				pDrv->base->MSTDAT = (uint32_t) p8[pDrv->sendIdx];
				pDrv->base->MSTCTL = I2C_MSTCTL_MSTCONTINUE;
				pDrv->sendIdx++;
				pXfer->txSz--;
			}
			else if (pXfer->rxSz == 0) {
				pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTOP;
				pDrv->pendingStatus = LPC_OK;
			}
			else {
				/* Start receive state with repeat start */
				pDrv->base->MSTDAT = (uint32_t) (pXfer->slaveAddr << 1) | 0x1;
				pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTART;

				/* Call receive start callback to setup RX DMA if needed */
				if (pDrv->pTranRecvCb) {
					pDrv->pTranRecvCb(pHandle, pXfer);
				}
			}
			break;

		case I2C_STAT_MSTCODE_NACKADR:	/* Slave address was NACK'ed */
			/* Set transfer status as NACK on address */
			pDrv->pendingStatus = ERR_I2C_SLAVE_NOT_ADDRESSED;
			pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTOP;
			break;

		case I2C_STAT_MSTCODE_NACKDAT:	/* Slave data was NACK'ed */
			/* Set transfer status as NACK on data */
			pDrv->pendingStatus = ERR_I2C_NAK;
			pDrv->base->MSTCTL = I2C_MSTCTL_MSTSTOP;
			break;

		default:
			/* Illegal I2C master state machine case. This should never happen.
			   Disable and re-enable controller to clear state machine */
			pDrv->pendingStatus = ERR_I2C_GENERAL_FAILURE;
			break;
		}
	}
}

uint32_t i2cm_get_driver_version(void)
{
	return DRVVERSION;
}

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