How to genaraite ppm in stm32f1

Post here first, or if you can't find a relevant section!
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: How to genaraite ppm in stm32f1

Post by dannyf »

ws2812b
I looked it up and it is I think too fast for any timer isr.

but it can be done via other serial peripherals - spi, for example.

bit 1: 0b11100000;
bit 0: 0b11111000;

for a spi with a buffer or dma, that can be done fairly easily. even if we use an isr, if it is fast enough, it is doable too: a bit is 1.2us, or about 80 ticks on a stm32f1. easier with 32-bit transmission.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: How to genaraite ppm in stm32f1

Post by dannyf »

I wrote this generic routine to transmit via SPI.

Code: Select all

//send ws2812 data
//load data into spi1buf
//assuming tx buffer is empty
char ws2812b_send(void) {
	static uint8_t idx0=0;				//counter for ws2812b, indexing through myWS2812:0..WS2812B_No
	static uint32_t idx1=1ul<<23;		//indexing through individual bits of grb
	static char tmp=0;					//next byte to be transmitted
	
	//fill the transmit buffer
	while (!spi1Busy()) {				//if buffer isn't full
		//load into the tx buffer - 
		spi1Write(tmp);					//buffer assumes to have at least 1 spot available
		//figure out which data to send
		if (idx1==0) {					//if a 24-bit data frame has been transmitted
			idx1=1ul<<23;				//reset idx1 advance to the next data frame
			if (++idx0==WS2812B_No) idx0=0;
		} else idx1 = idx1 >> 1;		//shift to the next bit	
		//form the next byte to send
		tmp = (myWS2812[idx0] & idx1)?WS2812B_ONE:WS2812B_ZERO;
	}
	return idx0;
}			
it is fairly hardware independent and can be called directly or via the interrupt. I tested it on a pic24 (with an 8-level buffer) and it can seamlessly transmit.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: How to genaraite ppm in stm32f1

Post by dannyf »

the code is plain. the only "trickery" is that after the transmission buffer is loaded, the code then figures out what to send next -> while the transmission is ongoing. This helps provide a seamless transmission -> continuous serial clock.

I did some googling and it seems that others have thought about using spi as the hardware for such devices. actual implementation varies, obviously.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: How to genaraite ppm in stm32f1

Post by ag123 »

dannyf wrote: Sun Jun 20, 2021 2:42 pm
ws2812b
I looked it up and it is I think too fast for any timer isr.

but it can be done via other serial peripherals - spi, for example.

bit 1: 0b11100000;
bit 0: 0b11111000;

for a spi with a buffer or dma, that can be done fairly easily. even if we use an isr, if it is fast enough, it is doable too: a bit is 1.2us, or about 80 ticks on a stm32f1. easier with 32-bit transmission.
i did a search here, and apparently someone did it with a h/w timer
viewtopic.php?f=10&t=357
i think it may be worth trying that out with a timer, i've had an impression timer irqs are good for below 1 mhz, above that, it is a challenge.
that has been my observations prior. but timer interrupts is 'attractive' as it is possible to state the period and duty cycle directly
my guess is that i could have placed too much codes in a timer isr and hence observed a rather low trigger rate.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: How to genaraite ppm in stm32f1

Post by dannyf »

he is using the pwm generator for that. it has the advantage of higher resolution. but it is tougher to link it to dma.

the isr overhead is likely in the 20 ticks territory, maybe slightly higher. if you look at my code, you probably go through it in 30 - 50 ticks (to be conservative). that means 70 ticks.

on a 72Mhz chip, that's about 1us.

the only way I can see to run it on slower chips is to run this to use fewer bits. rather than 8 bit spi, each "ws2812" bit is represented by 4 spi bits. you double the speed. But will need to work out the timing.
Post Reply

Return to “General discussion”