예제 #1
0
/**
 * @brief	Main routine for I2C example
 * @return	Function should not exit
 */
int main(void)
{
	/* Generic Initialization */
	SystemCoreClockUpdate();
	Board_Init();

	Board_LED_Set(0, false);

	/* Setup I2C at the board level (usually pin muxing) */
	Init_I2C_PinMux();

	/* Allocate I2C handle, setup I2C rate, and initialize I2C
	   clocking */
	setupI2CSlave();

	/* Enable the interrupt for the I2C */
	NVIC_EnableIRQ(I2C_IRQn);

	/* Setup I2C receive slave mode - this will setup a
	   non-blocking I2C mode which will be handled via the I2C interrupt */
	readI2CSlave();	/* From master first */

	/* I2C slave handler loop - wait for requests from master and
	   receive or send data */
	while (1) {
		/* Sleep while waiting for I2C master requests */
		__WFI();

		/* All I2C slave processing is performed in the I2C IRQ
		   handler, so there is nothing to really do here */
	}

	return 0;
}
예제 #2
0
파일: i2c.c 프로젝트: jmeed/teamRocket
/* Initialize the I2C bus */
static void i2c_app_init(I2C_ID_T id, int speed)
{
	Init_I2C_PinMux();

	/* Initialize I2C */
	Chip_I2C_Init(id);
	Chip_I2C_SetClockRate(id, speed);

	/* Set default mode to interrupt */
	i2c_set_mode(id, 0);
}
	void i2c_setup(void)
{
	/* Setup I2C pin muxing */
	Init_I2C_PinMux();

	/* Setup I2C, master, and slave */
	setupI2CMaster();
  setupI2CSlave();

	/* Enable the interrupt for the I2C */
	NVIC_EnableIRQ(LPC_IRQNUM);
		
	xI2cSemaphore =xSemaphoreCreateBinary();
	

	DEBUGOUT(" I2C slaves are ready\r\n");
}
예제 #4
0
파일: Zegar.c 프로젝트: Sarvaruis/Zegar
/**
 * @brief	Main routine for I2C example
 * @return	Function should not exit
 */
int main(void)
{
	int lastState = -1;

	/* Generic Initialization */
	SystemCoreClockUpdate();
	Board_Init();

	Board_LED_Set(0, false);

	/* Setup I2C pin muxing */
	Init_I2C_PinMux();

	/* Allocate I2C handle, setup I2C rate, and initialize I2C
	   clocking */
	setupI2CMaster();

	/* Enable the interrupt for the I2C */
	NVIC_EnableIRQ(I2C_IRQn);

	/* Enable SysTick Timer */
	SysTick_Config(SystemCoreClock / TICKRATE_HZ);

	//ustawiamy przykładowo godzinę 9:31
	volatile int hourH = 0;
	volatile int hourL = 9;
	volatile int minuteH = 3;
	volatile int minuteL = 1;
	volatile int seconds = 0;
	volatile int tickCounter = 0;

	/* Toggle LED on other board via I2C */
	while (1) {

		//zliczanie ticków
		tickCounter++;

		//kolejno warunki na zliczanie sekund, minut, godzin
		//w przypadku taktowania 400 ticków na sekundę
		if(tickCounter == 400) {
			tickCounter = 0;
			seconds = seconds + 1;
		}
		if(seconds == 60) {
			seconds = 0;
			minuteL = minuteL + 1;
		}
		if(minuteL == 10) {
			minuteL = 0;
			minuteH = minuteH + 1;
		}
		if(minuteH == 6) {
			minuteH = 0;
			hourL = hourL + 1;
		}
		if(hourL == 10) {
			hourL = 0;
			hourH = hourH + 1;
		}
		if(hourH == 2 && hourL == 4) {
			minuteH = 0; minuteL = 0;
			hourL = 0; hourH = 0;
		}

		//wyświetlanie aktualnej godziny
		showNumber(minuteL,LED0);
		showNumber(minuteH,LED1);
		showNumber(hourL,LED2);
		showNumber(hourH,LED3);

		/* Handle states */
		switch (state) {
		case 0:
			/* Toggle LED state value */
			ledState = (ledState == 0);
			break;

		case 1:
		default:
			break;
		}

		lastState = state;

		/* Match this board's LED to other boards state */
		Board_LED_Set(0, ledState);
	}
}
예제 #5
0
파일: i2cs_int.c 프로젝트: JamesHinnant/osp
/**
 * @brief	Main routine for I2C example
 * @return	Function should not exit
 */
int main(void)
{
	uint32_t memSize, *devMem, optimalDev;
	ROM_I2CS_INIT_T i2csInit;
	ROM_I2CS_SLAVE_T slaveSetup;
	int i;

	/* Generic Initialization */
	SystemCoreClockUpdate();
	Board_Init();

	/* Setup I2C pin muxing, enable I2C clock and reset I2C peripheral */
	Init_I2C_PinMux();
	Chip_Clock_EnablePeriphClock(LPC_I2CS_CLOCK);
	Chip_SYSCON_PeriphReset(LPC_I2CS_RESET);

	/* Get needed size for driver context memory */
	memSize = ROM_I2CS_GetMemSize();
	if (memSize > sizeof(drvData)) {
		errorOut("Can't allocate memory for I2C driver context\r\n");
	}
	devMem = drvData;	/* Or just use malloc(memSize) */

	/* Initialize driver */
	i2csInit.pUserData = (void *) &xferDone;
	i2csInit.base = (uint32_t) LPC_I2C_PORT;
	i2csHandle = ROM_I2CS_Init(devMem, &i2csInit);
	if (i2csHandle == NULL) {
		/* Error initializing I2C */
		errorOut("Error initializing ROM\r\n");
	}

	/* Register slave start and completion callbacks */
	ROM_I2CS_RegisterCallback(i2csHandle, ROM_I2CS_DONE_CB, (void *) i2cSlaveCompleteFunc);
	ROM_I2CS_RegisterCallback(i2csHandle, ROM_I2CS_START_CB, (void *) i2cSlaveStartFunc);

	/* Setup slave address to respond to */
	slaveSetup.slaveAddr = I2C_ADDR_7BIT;
	slaveSetup.SlaveIndex = 0;
	slaveSetup.EnableSlave = 1;
	ROM_I2CS_SetupSlave(i2csHandle, &slaveSetup);

	/* Setup clock rate for I2C - this must be done for master or slave modes.
	   This function is not set by the ROM API code. */
	optimalDev = Chip_Clock_GetAsyncSyscon_ClockRate() / 4000000;	/* 250nS */
	LPC_I2C_PORT->CLKDIV = optimalDev;

	/* Enable the interrupt for the I2C */
	NVIC_EnableIRQ(LPC_I2CS_INT);

	while (1) {
		/* Populate some TX data and clear RX data */
		for (i = 0; i < sizeof(slaveTx); i++) {
			slaveTx[i] = 0x70 + i;
			slaveRx[i] = 0;
		}
		sXfer.txBuff = slaveTx;
		sXfer.rxBuff = slaveRx;
		sXfer.txSz = sizeof(slaveTx);
		sXfer.rxSz = sizeof(slaveRx);

		/* Wait for a transfer from the master */
		xferDone = false;
		ROM_I2CS_Transfer(i2csHandle, &sXfer);	/* Never blocks, always returns LPC_OK */
		DEBUGOUT("Waiting for master\r\n");

		/* Wait for transfer to complete. The slave data completion callback
		   will set xferDone when ready via it's completion callback. */
		while (xferDone == false) {
			__WFI();
		}

		/* Check status of the transfer */
		DEBUGOUT("Transfer complete (%x)\r\n", sXfer.status);
		DEBUGOUT("RX [%d]", sXfer.bytesRecv);
		for (i = 0; i < sXfer.bytesRecv; i++) {
			DEBUGOUT(" : %02x", slaveRx[i]);
		}
		DEBUGOUT("\r\n");
		DEBUGOUT("TX [%d]", sXfer.bytesSent);
		for (i = 0; i < sXfer.bytesSent; i++) {
			DEBUGOUT(" : %02x", slaveTx[i]);
		}
		DEBUGOUT("\r\n");
	}

	/* Code never reaches here. Only used to satisfy standard main() */
	return 0;
}
예제 #6
0
파일: i2cmon.c 프로젝트: JamesHinnant/osp
/**
 * @brief	Main routine for I2C monitor example
 * @return	Function should not exit
 */
int main(void)
{
	uint32_t memSize, *devMem;
	ROM_I2CMON_INIT_T i2cmonInit;
	int i, curCapIndex = 0;
	volatile int curCapGetIndex;

	/* Generic Initialization */
	SystemCoreClockUpdate();
	Board_Init();

	/* Setup I2C pin muxing, enable I2C clock and reset I2C peripheral */
	Init_I2C_PinMux();
	Chip_Clock_EnablePeriphClock(LPC_I2C_CLOCK);
	Chip_SYSCON_PeriphReset(LPC_I2C_RESET);

	/* Get needed size for driver context memory */
	memSize = ROM_I2CMON_GetMemSize();
	if (memSize > sizeof(drvData)) {
		errorOut("Can't allocate memory for I2C monitor driver context\r\n");
	}
	devMem = drvData;	/* Or just use malloc(memSize) */

	/* Initialize driver */
	i2cmonInit.pUserData = (void *) &curCapGetIndex;
	i2cmonInit.base = (uint32_t) LPC_I2C_PORT;
	i2cmonInit.stretch = 1;	/* Enable clock stretching */
	i2cmomHandle = ROM_I2CMON_Init(devMem, &i2cmonInit);
	if (i2cmomHandle == NULL) {
		/* Error initializing I2C monitor */
		errorOut("Error initializing ROM\r\n");
	}

	/* Register the capture completion and DMA callbacks */
	ROM_I2CMON_RegisterCallback(i2cmomHandle, ROM_I2CMON_CAPTUREREADY_CB, (void *) i2cMonitorDoneCallback);
	ROM_I2CMON_RegisterCallback(i2cmomHandle, ROM_I2CMON_DMASETUP_CB, (void *) i2cMonSetupDMACallback);

	/* Enable the interrupt for the I2C */
	NVIC_EnableIRQ(LPC_IRQNUM);

	/* Read data as fast as possible in loop */
	while (1) {
		/* Next capture buffer */
		pNextCap = &capQueue[curCapIndex];

		/* Setup capture as non-blocking */
		pNextCap->startBuff = &capBuffer[curCapIndex][0];
		pNextCap->startBuffSz = MAXBUFFCAPLEN;
		pNextCap->flags = 0;

		/* Next expected capture index */
		curCapIndex++;
		if (curCapIndex >= MAXCAPS) {
			curCapIndex = 0;
		}

		/* Queue capture descriptor to start capture */
		ROM_I2CMON_StartLog(i2cmomHandle, pNextCap);

		/* Wait for capture to complete */
		while (pNextCap->status == ERR_I2C_BUSY) {}

		/* Display captured I2C data */
		if (pNextCap->status != LPC_OK) {
			DEBUGOUT("Error with monitor function (%x)\r\n", pNextCap->status);
		}
		else {
			if (curCapGetIndex != curCapIndex) {
				uint16_t data, *p16 = (uint16_t *) pCurrentCap->startBuff;

				/* Show captured data */
				for (i = 0; i < pCurrentCap->capStartBuffSz; i++) {
					data = *p16;
					p16++;
					if ((data & (I2C_MONRXDAT_MONSTART | I2C_MONRXDAT_MONRESTART)) != 0) {
						if ((data & I2C_MONRXDAT_MONSTART) != 0) {
							DEBUGSTR("[S:");
						}
						else {
							DEBUGSTR("[S2:");
						}
						DEBUGOUT("%02x:", ((data & I2C_MONRXDAT_DATA) >> 1));
						if ((data & 1) != 0) {
							DEBUGSTR("R:");
						}
						else {
							DEBUGSTR("W:");
						}
					}
					else {
						DEBUGOUT("[D:%02x:", (data & I2C_MONRXDAT_DATA));
					}
					if ((data & I2C_MONRXDAT_MONNACK) != 0) {
						DEBUGOUT("N] ");
					}
					else {
						DEBUGOUT("A] ");
					}
				}

				DEBUGSTR("\r\n");
			}
		}