@@ -14,14 +14,12 @@ Purpose: configure I2C2 and implement read and write functions
1414#include  "nvic.h" 
1515#include  "tcnt.h" 
1616
17- volatile  uint8_t  g_i2c2_stage  =  I2C2_POST_DISPLAY ;
18- extern  volatile  uint32_t  g_read_buffer ;
19- 2017//initialize I2C2 registers: 100KHz SCL Frequency, 7-bit addressing mode 
2118void  i2c2_init (void ) {
2219	gpio_i2c2_init (); 	//set up gpio for i2c2 
2320
2421	I2C2_CR1  &= ~1 ; //disable I2C2 Peripheral 
22+ 	I2C2_CR1  |= (1  << 15 ); //enable receive DMA 
2523	I2C2_CR1  |= (1  << 2 ); //enable receive interrupt 
2624
2725	I2C2_TIMINGR  |= I2C2_TIMING_VALS ;
@@ -31,7 +29,7 @@ void i2c2_init(void) {
3129}
3230
3331//write to the target; this function only allows up to 2 bytes of data transmission at once 
34- void  i2c2_write (const  uint8_t  NBYTES , const  uint16_t  w_buffer ) {
32+ uint8_t  i2c2_write (const  uint8_t  NBYTES , const  uint16_t  w_buffer ) {
3533	I2C2_CR2  &= ~(1  << 10 ); //set to Write 
3634	I2C2_CR2  &= ~(0xFF  << 16 ); //clear NBYTES; if this is not done then sometimes no stop condition 
3735	I2C2_CR2  |= (NBYTES  << 16 ); //NBYTES = NBYTES parameter 
@@ -57,11 +55,11 @@ void i2c2_write(const uint8_t NBYTES, const uint16_t w_buffer) {
5755	I2C2_CR2  |= (1  << 14 ); //send STOP condition 
5856	I2C2_ICR  |= (1  << 5 ); //clear stop flag 
5957
60- 	g_i2c2_stage =  I2C2_POST_WRITE ;
58+ 	return  I2C2_POST_WRITE ;
6159}
6260
6361//read from the i2c bus 
64- void  i2c2_read (const  uint8_t  NBYTES ) {
62+ uint8_t  i2c2_read (const  uint8_t  NBYTES ) {
6563	I2C2_CR2  |= (1  << 10 ); //set to Read  
6664	I2C2_CR2  &= ~(0xFF  << 16 ); //clear NBYTES; if this is not done then sometimes no stop condition 
6765	I2C2_CR2  |= (NBYTES  << 16 ); //NBYTES = 4 for the TSL2591 data registers 
@@ -73,8 +71,7 @@ void i2c2_read(const uint8_t NBYTES) {
7371
7472	I2C2_CR2  |= (1  << 13 ); //send start condition 
7573
76- 	g_read_buffer  =  0 ;
77- 	g_i2c2_stage  =  I2C2_POST_READ ;
74+ 	return  I2C2_POST_READ ;
7875}
7976
8077//Use timers to check if the bus goes idle; returns 1 if idle and 0 if not idle 
@@ -97,36 +94,16 @@ uint8_t i2c2_check_bus(void) {
9794	}
9895}
9996
97+ //PROBABLY DON'T NEED THIS!!! 
10098//IRQ handler for I2C2 event interrupts - for receives! 
10199void  I2C2_EV_IRQHandler (void ) {
102- 	nvic_disable ();
103- 104- 	static  uint8_t  bytes_rx ;	//holds number of bytes received 
105- 106- 	//if this is the first received byte, init a count var to 1 
107- 	if  (g_i2c2_stage  ==  I2C2_POST_READ ) {
108- 		///* BEFORE DMA 
109- 		bytes_rx  =  0 ;
110- 		g_read_buffer  |= ((uint8_t ) I2C2_RXDR );
111- 		//*/ 
112- 113- 		bytes_rx  =  1 ;
114- 		g_i2c2_stage  =  I2C2_RECEIVING ;
115- 	}
116- 	else  {
117- 		///* BEFORE DMA 
118- 		g_read_buffer  |= ((uint8_t ) I2C2_RXDR ) << (bytes_rx * 8 );
119- 		//*/ 
120- 121- 		bytes_rx ++ ;
122- 	}
100+ 	static  uint8_t  bytes_rx  =  0 ;	//holds number of bytes received 
101+ 	bytes_rx ++ ;
123102
124103	if  (bytes_rx  ==  I2C2_NBYTES ) {
125104		I2C2_CR2  |= (1  << 14 ); //send STOP condition 
126105		I2C2_ICR  |= (1  << 5 ); //clear stop flag 
127106		bytes_rx  =  0 ;
128- 		g_i2c2_stage  =  I2C2_POST_RECEIVE ;
129107	}
130- 	nvic_enable ();
131108}
132109
0 commit comments