TINY841 (TWI Slave) Running WS2812 LEDs

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I'm writing firmware for the ATTINY841, it's designed to receive data from the TWI master, store the data in SRAM buffer and then display it on a string of 16 WS2812 LEDs. My problem is that due to timing requirements, I cannot receive data from the TWI bus while updating the LEDs. (Interrupts are temporarily disabled)

 

I could try...

 

A, clock stretching

B, disabling TWI interface while updating LEDs (no acknowledge)

C, somehow abort LED refreshing upon twi interrupt?

D, limit the communication on the master side, update LED upon STOP condition.

E, some sort of slave ready signal to the master

 

I would prefer the C solution, any ideas are welcome!

 

Here is the TWI slave interrupt service routine I'm using, it's pretty standard stuff!

Thanks!


ISR(TWI_SLAVE_vect){
	
	
	uint8_t status = TWSSRA & 0xC0;
	
	if (status & I2C_DATA_INTERRUPT) // Received data from master
	{

		if (TWSSRA & (1 << TWDIR)) // Send data to the master
		{
			if(tx_buf_index >= sizeof(tx_buf))
			{
				tx_buf_index=0;
			}
			
			TWSD = tx_buf[tx_buf_index];
			tx_buf_index++;
			
			TWSCRB = (uint8_t) ((1<<TWCMD1)|(1<<TWCMD0));
		}
		else // Receive data from the master
		{
			
			
			TWSCRB |= (uint8_t) ((1<<TWCMD1)|(1<<TWCMD0));
			if(rx_buf_index >= sizeof(rx_buf))
			{
				rx_buf_index=0;
			}

			uint8_t temp = TWSD;
			rx_buf[rx_buf_index] = temp;
			rx_buf_index++;
			
			
		}
	}
	else if (status & I2C_ADDRESS_STOP_MATCH)
	{
		if (TWSSRA & I2C_BUS_COLLISION)
		{
			TWSCRB = (uint8_t) (1<<TWCMD1);
		}
		else
		{
			if (TWSSRA & (1<<TWAS))
			{
				// ACK
				TWSCRB = (uint8_t) ((1<<TWCMD1)|(1<<TWCMD0));
			}
			else
			{
				// Stop Condition
				TWSCRB = (uint8_t) (1<<TWCMD1);
				tx_buf_index = 0;
				rx_buf_index = 0; 
				
				rx_ready = 1; // decode message!
			}
		}
	}
	
	
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would think that considering how fast the leds update this would not be a problem.. are you manually disabling the interrupt while updating the leds? Reason I ask is that if choice C is your preferred option, leave the interrupt enabled and when the TWI interrupt fires it will stop the led update.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes, I am manually disabling the interrupt!

 

if i don't disable it, then after serving the interrupt the program will return to where it left off (to the middle of updating the LEDs), how can I detect from 'there' that there was and interrupt?

 

Maybe the interrupt could disable the driver pin for the LED signal but that is a little brave to just stop the signal wherever....

Last Edited: Mon. Feb 19, 2018 - 09:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The thing about those leds is that they rely on timing, so if a TWI interrupt comes in during the update one, or more may update while some may not. In the case of the ones that do not as you probably have found it's anyone's guess what color they will end up.

Since I don't know if you are disabling the twi interrupt or the global interrupt I will suggest that you only disable the global during the update of the leds. This way if a twi message comes in, the flag is set, and when you re enable the global it will service the interrupt right away.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The WS2812 timing requirements can be relaxed slightly (specifically, the T0L and T1L intervals).  You could interleave TWI handling code into the WS2812 code, but this is not for the faint of heart ;-)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Tue. Feb 20, 2018 - 01:07 AM