STM32 sd Spurious Character

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

brian360
Posts: 27
Joined: Wed Dec 08, 2010 5:47 pm

STM32 sd Spurious Character

Post 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
mculover
Posts: 26
Joined: Fri Dec 10, 2010 2:27 pm

Re: STM32 sd Spurious Character

Post by mculover »

Hi Brian,

Are you use USART 1 ?.

I have temporary solution for that. I edit pal_lld_init () from

Code: Select all

RCC->APB2ENR |= APB2_EN_MASK;


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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 sd Spurious Character

Post 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
brian360
Posts: 27
Joined: Wed Dec 08, 2010 5:47 pm

Re: STM32 sd Spurious Character

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 sd Spurious Character

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 sd Spurious Character

Post 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
brian360
Posts: 27
Joined: Wed Dec 08, 2010 5:47 pm

Re: STM32 sd Spurious Character

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 sd Spurious Character

Post 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
mculover
Posts: 26
Joined: Fri Dec 10, 2010 2:27 pm

Re: STM32 sd Spurious Character

Post by mculover »

Giovanni wrote:Ok, anyway 2.2.0 should be released during next weekend.


It's good news. I waiting for that. :lol:
mculover
joaquin
Posts: 50
Joined: Sat Jan 22, 2011 8:44 pm
Has thanked: 1 time
Been thanked: 3 times

Re: STM32 sd Spurious Character

Post 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 */
}
}
Post Reply