Exemplo n.º 1
0
void i2c1_isr(void) {
	//WARNING: prints in here are a bad idea
	static int i=0;

	uint32_t isr = I2C_ISR(I2C_BUS);


	if (isr & I2C_ISR_RXNE) {
		//do this first so we don't lose a char if other events are happening
		volatile uint8_t byte = I2C_RXDR(I2C_BUS);
		i2c_rx_buf[i2c_rx_bytes++] = byte;
// 		printf("rx 0x%02x\n", byte);
	}

	if (isr & I2C_ISR_ADDR) {
// 		printf("\n");
// 		printf("\naddress match 0x%04lx dir%d\n", I2C_ISR_ADDCODE_VALG(isr), !!(isr & I2C_ISR_DIR));
		I2C_ICR(I2C_BUS) = I2C_ICR_ADDRCF;
		i2c_rx_bytes = 0;
		i2c_tx_bytes = 0;
		if (isr & I2C_ISR_DIR) {
			I2C_ISR(I2C_BUS) |= I2C_ISR_TXE;
		}
	}

	if ((isr & (I2C_ISR_TXIS | I2C_ISR_DIR)) == (I2C_ISR_TXIS | I2C_ISR_DIR)) {
		if(!(isr & I2C_ISR_STOPF)) {
			volatile uint8_t byte = i2c_tx_buf[i2c_tx_bytes++];
			I2C_TXDR(I2C_BUS) = byte;
// 			printf("t");
		}
// 		printf("i0x%08lx\n", isr);
	}

	if (isr & I2C_ISR_STOPF) {
// 		printf("stop dir%d\n", !!(isr & I2C_ISR_DIR));
		if(!(isr & I2C_ISR_DIR)) {
			handle_i2c_bufs();
		}

		I2C_ICR(I2C_BUS) = I2C_ICR_STOPCF;
		I2C_TXDR(I2C_BUS) = 0; //hack to avoid another txis interrupt at the end
	}

	if (isr & I2C_ISR_NACKF) {
// 		printf("nack\n");
		I2C_ICR(I2C_BUS) = I2C_ICR_NACKCF;
	}

	if (isr & 0x3f00) {
		i++;
		if(i>10) {
			while(1) {
// 				printf("i2c error 0x%08lx\n", isr);
			}
		}
	}

}
Exemplo n.º 2
0
void i2c_send_data(uint32_t i2c, uint8_t data)
{
	I2C_TXDR(i2c) = data;
}