Overriding yield() prevents sketch from starting

Post Reply
doominick
Posts: 2
Joined: Tue Dec 22, 2020 3:26 am

Overriding yield() prevents sketch from starting

Post by doominick »

Hello everyone!

I'm using a generic Blue Pill with STM32F103C8, flashed with a generic PC13 maple dfu bootloader (not sure if fast or not).
My problem in short is that when trying to override yield() inside my sketch (no matter in which place regarding setup() and loop()), my sketch won't start. The setup() function doesn't start, and even USB CDC serial port doesn't show up (unknown device instead). This happens using both PlatformIO and Arduino Studio using the official stm32duino core. As soon as I upload the same code without yield() (even as an empty function), everythng starts working. I have checked it even on a simple blink sketch, so the surrounding code seems to make no difference.

Longer story about why I want to override yield() is, that I want to use a TM1637 display simultaneously with Accelstepper library. The display library uses delay function that looks like this:

Code: Select all

void TM1637Display::bitDelay()
{
	delayMicroseconds(m_bitDelay);
}
where m_bitDelay equals 100.
This causes problems for Accelstepper, as run() is not called for at least 1ms, as usually more than 8 bits are transferred in a row.
I thought of using a similar concept as for delay(), and change the function to:

Code: Select all

void TM1637Display::bitDelay()
{
  uint32_t start = micros();
  do {
      yield();
  } while (micros() - start < m_bitDelay);
}
and override yield() with:

Code: Select all

void yield(void){
  stepper_x.run();
  stepper_y.run();
}
so that my steppers would be called in between the bits sent to the display (with a possible small disruption of display timing, but possibly acceptable).

Do you have any thoughts on why would overriding yield() cause the whole program fail to start? I have checked my way of overriding yield() on Arduino Nano and it works OK. I have searched through the stm32duino core repository for any implementation of yield(), but again nothing except of a standard weak empty implementation. Can it be that it interferes with the bootloader?
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: Overriding yield() prevents sketch from starting

Post by fredbox »

TM1637 devices have a maximum clock frequency of 250 kHz. A delay time of 4-5 microseconds should be plenty. Also, these displays are simple enough that you really don't need a dedicated library. See https://github.com/moozzyk/TM1637Clock for an example of communications without a library.
doominick
Posts: 2
Joined: Tue Dec 22, 2020 3:26 am

Re: Overriding yield() prevents sketch from starting

Post by doominick »

Thanks @fredbox, that's an interesting resource I didn't stumble upon earlier. I'll try to use it as a workaround and write my own display driving functions, with timers or using a state machine.

However, that still doesn't answer my question from the title ;) . And as I can go on with the project using different solutions, it's interesting why would yield() behave in such a way for me.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Overriding yield() prevents sketch from starting

Post by fpiSTM »

Well hard to tell without debugging nor the full code.

yield() is also called by delay() so pay attention to not block.
another point is that if you defined it at sketch level I would prefix it by extern "C"
Post Reply

Return to “STM32F1 based boards”