Page 1 of 2
STM32 sd Spurious Character
Posted: Tue Jan 25, 2011 3:17 am
by brian360
Hi,
I happened to notice the "bug" regarding a spurious character on the STM32 serial driver and was wondering how to properly implement the workaround. In my case, I'm communicating with a device that may change baud, and I currently call "sdStart()" at various points while running with updated parameters to do so. Should I be reconfiguring the TX pin as PP output before every call to sdStart(), or just the very first call after the OS starts?
I've got a bit more debugging to do before I make any conclusions, but for some reason after calling sdStart(), my next call to sdWrite() transmits an undesired 0x00 character before my desired character, which seems to cause problems with my device.
Thanks,
Brian
Re: STM32 sd Spurious Character
Posted: Tue Jan 25, 2011 6:58 am
by mculover
Hi Brian,
Are you use USART 1 ?.
I have temporary solution for that. I edit pal_lld_init () from
to
Code: Select all
#ifdef USE_STM32_USART1
RCC->APB2ENR |= (APB2_EN_MASK | RCC_APB2ENR_USART1EN);
#else
RCC->APB2ENR |= APB2_EN_MASK;
#endif
and comment in sd_lld_start ()
Code: Select all
/* RCC->APB2ENR |= RCC_APB2ENR_USART1EN; */
It's work. !!!
You must be enable clock for AFIO and USART1 at same time.
But I don't know why STM32 not allow to seperate enable clock.
HTH
mculover
Re: STM32 sd Spurious Character
Posted: Tue Jan 25, 2011 9:09 am
by Giovanni
Hi Brian,
The solution from mculover works but it leaves the USART clock enabled and you would still get the spurious character the next time you do sdStop() and then sdStart() again.
The problem is not in the Serial driver but in interaction between USART and AFIO/GPIO. Programming the TX pin as "alternate output" when the USART clock is not yet enabled drives the TX pin low until you enable the clock using sdStart(), that is interpreted as a start bit, thus the spurious character.
The correct start operations sequence is:
- Program the TX pin as push-pull output in board.h driving a logical 1.
- Enable the Serial driver using sdStart from the main().
- Reprogram the TX pin as alternate output using palSetPadMode() just after the sdStart().
- Use the serial driver I/O functions.
The correct stop sequence is:
- Reprogram the TX pin as push pull output using palSetPadMode(), this will mask the glitch.
- Disable the serial driver using sdStop().
The STM32 demos will be modified to do exactly what I described above in some next release.
Giovanni
Re: STM32 sd Spurious Character
Posted: Tue Jan 25, 2011 6:51 pm
by brian360
mculover and Giovanni,
Thanks for your help! Disabling the pin with palSetPadMode() between sdStarts() and stStops() definitely fixed my issue.
I do have one question regarding the serial buffers. It seems that sdWriteTimeout returns before the bytes are actually transmitted on the serial line. That's ok, but does the sd driver have any mechanism to block until the bytes are physically transmitted?
Re: STM32 sd Spurious Character
Posted: Tue Jan 25, 2011 7:16 pm
by Giovanni
Just specify TIME_INFINITE as timeout or use the macro sdWrite().
Note that this only ensures that the buffer has been emptied but the USART may still be transmitting the last frames, in the STM32 there can be up two frames buffered in HW.
Giovanni
Re: STM32 sd Spurious Character
Posted: Tue Jan 25, 2011 9:22 pm
by Giovanni
I just remembered, the UART driver does have a callback for the physical transmission end, you may consider using that one instead of the buffered Serial driver, it also allows efficient DMA transfers.
Giovanni
Re: STM32 sd Spurious Character
Posted: Wed Jan 26, 2011 6:38 am
by brian360
I had forgotten about the new UART drivers, too. Looks like they're only in the unstable branch, though. For this project, I have to stick with stable releases. But I've got the buffered serial driver working perfectly now for my application.
Thanks again.
Re: STM32 sd Spurious Character
Posted: Wed Jan 26, 2011 8:48 am
by Giovanni
Ok, anyway 2.2.0 should be released during next weekend.
Probably it would be a good idea to add an "end of transmission" event to the buffered serial driver too.
Giovanni
Re: STM32 sd Spurious Character
Posted: Wed Jan 26, 2011 1:54 pm
by mculover
Giovanni wrote:Ok, anyway 2.2.0 should be released during next weekend.
It's good news. I waiting for that.
mculover
Re: STM32 sd Spurious Character
Posted: Wed Jan 26, 2011 9:20 pm
by joaquin
Just if it helps..
I needed it for a rs485 driver, to switch the TX/RX pin. It could be done better with a chibios event, anyway
In the serve_interrupt function, I modified it as:
static void serve_interruptRs485(SerialDriver *sdp, int16_t TXRX_PIN) {
USART_TypeDef *u = sdp->usart;
uint16_t cr1 = u->CR1;
uint16_t sr = u->SR; /* SR reset step 1.*/
uint16_t dr = u->DR; /* SR reset step 2.*/
if (sr & (USART_SR_LBD | USART_SR_ORE | USART_SR_NE |
USART_SR_FE | USART_SR_PE)) {
set_error(sdp, sr);
u->SR = 0; /* Clears the LBD bit in the SR.*/
}
if (sr & USART_SR_RXNE) {
chSysLockFromIsr();
sdIncomingDataI(sdp, (uint8_t)dr);
chSysUnlockFromIsr();
}
if ((cr1 & USART_CR1_TXEIE) && (sr & USART_SR_TXE)) {
msg_t b;
chSysLockFromIsr();
b = chOQGetI(&sdp->oqueue);
if (b < Q_OK) {
chEvtBroadcastI(&sdp->oevent);
u->CR1 = cr1 & ~USART_CR1_TXEIE;
u->CR1 |= USART_CR1_TCIE; /* enable the transmision complete interrupt */
}
else
u->DR = b;
chSysUnlockFromIsr();
}
if (sr & USART_SR_TC) { /* if it's a transmission complete interrupt... */
palClearPad(IOPORT1, TXRX_PIN); /* switch TX/RX pin */
u->CR1 &= ~USART_CR1_TCIE; /* disable transmission complete interrupt */
u->SR &= ~USART_SR_TC; /* clear flag */
}
}