@@ -14,12 +14,15 @@ Purpose: configure I2C2 and implement read and write functions
1414#include  "nvic.h" 
1515#include  "tcnt.h" 
1616
17+ volatile  uint8_t  g_i2c2_rx_count  =  0 ;
18+ extern  volatile  uint32_t  g_read_buffer ;
19+ 1720//initialize I2C2 registers: 100KHz SCL Frequency, 7-bit addressing mode 
1821void  i2c2_init (void ) {
1922	gpio_i2c2_init (); 	//set up gpio for i2c2 
2023
2124	I2C2_CR1  &= ~1 ; //disable I2C2 Peripheral 
22- 	I2C2_CR1  |= (1  << 4 ); //enable NACK  interrupt 
25+ 	I2C2_CR1  |= (1  << 2 ); //enable receive  interrupt 
2326
2427	I2C2_TIMINGR  |= I2C2_TIMING_VALS ;
2528
@@ -28,75 +31,49 @@ void i2c2_init(void) {
2831}
2932
3033//write to the target; this function only allows up to 2 bytes of data transmission at once 
31- void  i2c2_write (uint8_t  NBYTES , const  uint16_t  w_buffer ) {
34+ void  i2c2_write (const uint8_t  NBYTES , const  uint16_t  w_buffer ) {
3235	I2C2_CR2  &= ~(1  << 10 ); //set to Write 
3336	I2C2_CR2  &= ~(0xFF  << 16 ); //clear NBYTES; if this is not done then sometimes no stop condition 
3437	I2C2_CR2  |= (NBYTES  << 16 ); //NBYTES = NBYTES parameter 
3538
36- 	//loop until bus is idle 
3739	volatile  int  count  =  0 ;
3840	uint8_t  timeout  =  0xFF ;
39- 	while  (timeout  !=  1 ) timeout  =  i2c2_check_bus (); 
41+ 	while  (timeout  !=  1 ) { //loop until bus is idle 
42+ 		timeout  =  i2c2_check_bus (); 
43+ 	}
4044
4145	I2C2_CR2  |= (1  << 13 ); //send start condition 
4246
4347	//loop until all bytes are sent 
4448	for  (count  =  NBYTES - 1 ; count  >  -1 ; count -- ) {
4549		while  (!((I2C2_ISR  >> 1 ) &  1 )); //loop until TXIS flag is set 
46- 		I2C2_TXDR  =  (uint8_t )(( w_buffer  >> (8 * count ))  & 0xFF ); 
50+ 		I2C2_TXDR  =  (uint8_t ) ( w_buffer  >> (8 * count )); 
4751	}
4852
49- 	//delay for target device to process 2nd data byte 
50- 	timer3_delay_us (500 );
53+ 	if  (NBYTES  >  1 ) {
54+ 		timer3_delay_us (30 );	//delay for target device to process 2nd data byte 
55+ 	}
5156
5257	I2C2_CR2  |= (1  << 14 ); //send STOP condition 
5358	I2C2_ICR  |= (1  << 5 ); //clear stop flag 
5459}
5560
56- //Write to a register in the target device then read and store 4 bytes to the r_buffer 
57- void  i2c2_write_read (uint8_t  NBYTES , const  uint16_t  target_reg , uint32_t  * r_buffer ) {
58- 	* r_buffer  =  0 ; //clear r_buffer 
59- 60- 	//complete write section first: 
61- 	I2C2_CR2  &= ~(1  << 10 ); //set to Write 
62- 	I2C2_CR2  &= ~(0xFF  << 16 ); //clear NBYTES; if this is not done then sometimes no stop condition 
63- 	I2C2_CR2  |= (1  << 16 ); //NBYTES = 1 
64- 65- 	//loop until bus is idle 
66- 	volatile  int  count  =  0 ;
67- 	uint8_t  timeout  =  0xFF ;
68- 	while  (timeout  !=  1 ) timeout  =  i2c2_check_bus (); 
69- 70- 	I2C2_CR2  |= (1  << 13 ); //send start condition 
71- 72- 	while  (!((I2C2_ISR  >> 1 ) &  1 )); //loop until TXIS flag is set 
73- 	I2C2_TXDR  =  target_reg  &  0xFF ; //only use first byte  
74- 75- 	//repeated START to do read section: 
76- 	while  ((I2C2_CR2  >> 13 ) &  1 ); //loop until START bit is not set 
77- 61+ //read from the i2c bus 
62+ void  i2c2_read (const  uint8_t  NBYTES ) {
7863	I2C2_CR2  |= (1  << 10 ); //set to Read  
7964	I2C2_CR2  &= ~(0xFF  << 16 ); //clear NBYTES; if this is not done then sometimes no stop condition 
8065	I2C2_CR2  |= (NBYTES  << 16 ); //NBYTES = 4 for the TSL2591 data registers 
8166
82- 	//loop until bus is idle 
83- 	count  =  0 ;
84- 	timeout  =  0xFF ;
85- 	while  (timeout  !=  1 ) timeout  =  i2c2_check_bus (); 
67+ 	volatile  int  count  =  0 ;
68+ 	uint8_t  timeout  =  0xFF ;
69+ 	while  (timeout  !=  1 ) { //loop until bus is idle 
70+ 		timeout  =  i2c2_check_bus (); 
71+ 	}
8672
8773	I2C2_CR2  |= (1  << 13 ); //send start condition 
8874
89- 	//loop until all bytes are read  
90- 	for  (count  =  0 ; count  <  NBYTES ; count ++ ) {
91- 		while  (!((I2C2_ISR  >> 2 ) &  1 )); //loop until RXNE flag is set 
92- 		* r_buffer  |= (I2C2_RXDR  &  0xFF ) << (count * 8 ); 
93- 	}
94- 95- 	//NOTE: NACK is automatically sent after the last byte reception.  
96- 97- 	I2C2_CR2  |= (1  << 14 ); //send STOP condition 
98- 	I2C2_ICR  |= (1  << 5 ); //clear stop flag 
99- }	
75+ 	g_read_buffer  =  0 ;
76+ }
10077
10178//Use timers to check if the bus goes idle; returns 1 if idle and 0 if not idle 
10279uint8_t  i2c2_check_bus (void ) {
@@ -118,14 +95,36 @@ uint8_t i2c2_check_bus(void) {
11895	}
11996}
12097
121- //Function to resolve I2C deadlocks - not completed 
122- void  i2c2_resolve_deadlock (void ) {
123- }
124- 125- //IRQ handler for I2C2 event interrupts; for now for NACKs 
98+ //IRQ handler for I2C2 event interrupts - for receives! 
12699void  I2C2_EV_IRQHandler (void ) {
127100	nvic_disable ();
128- 	//do something 
101+ 102+ 	static  uint8_t  bytes_rx ;	//holds number of bytes received 
103+ 104+ 	//if this is the first received byte, init a count var to 1 
105+ 	if  (g_i2c2_rx_count  ==  0 ) {
106+ 		///* 
107+ 		bytes_rx  =  0 ;
108+ 		g_read_buffer  |= ((uint8_t ) I2C2_RXDR ) << (g_i2c2_rx_count * 8 );
109+ 		//*/ 
110+ 111+ 		bytes_rx  =  1 ;
112+ 		g_i2c2_rx_count  =  1 ;
113+ 	}
114+ 	else  {
115+ 		///* 
116+ 		g_read_buffer  |= ((uint8_t ) I2C2_RXDR ) << (bytes_rx * 8 );
117+ 		//*/ 
118+ 119+ 		bytes_rx ++ ;
120+ 	}
121+ 122+ 	if  (bytes_rx  ==  I2C2_NBYTES ) {
123+ 		I2C2_CR2  |= (1  << 14 ); //send STOP condition 
124+ 		I2C2_ICR  |= (1  << 5 ); //clear stop flag 
125+ 		bytes_rx  =  0 ;
126+ 		g_i2c2_rx_count  =  0 ;
127+ 	}
129128	nvic_enable ();
130129}
131130
0 commit comments