Page 1 of 1

Serial Driver and IO_TRANSMISSION_END Event

Posted: Wed Aug 28, 2013 1:28 am
by GregB
Hello,

I'm completely new to ChibiOS, and I just inherited a ChibiOS 2.4.0 project. The board with an STM32F407 CPU has both and RS-232 and RS-485 transceiver ICs that are muxed together on USART1. Up until this time, only the RS-232 portion has been used, along with the HAL serial driver (not the UART). Now I want to enable the RS-485 section. This is a half-duplex setup, and I have to enable the transmit driver when sending data, and disable the transmit driver when the transmission is done. I can set the enable bit right before I begin the transmission, but I need a way to know when the last bit has gone out the wire, so I can disable the driver, and listen for more commands. From the searching I've done on the 'net and this forum, it appears that there is an event called "IO_TRANSMISSION_END" that sounds like it would be just what I'm looking for. It looks like that event was added in ChibiOS 2.3.0. But I'm not quite sure about the details. Could someone point me in the direction of the documentation or examples that could help me understand what events are possible from the Serial driver, and how they get used? Am I getting close with the code snippet below?

Code: Select all

/*
 * Thread for putting RS-485 driver back into Hi-Z state after transmission.
 */
static WORKING_AREA(waThread4, 128);

static msg_t Thread4(void *arg) {
    EventListener el1;

    (void)arg;
    chRegSetThreadName("RS-485 Enable");
    chEvtRegisterMask(chnGetEventSource(&SD1), &el1, IO_TRANSMISSION_END);

    while (TRUE) {
        chEvtWaitOne(IO_TRANSMISSION_END); 
        chEvtGetAndClearFlags(&el1); //is this necessary?
        palClearPad(GPIOA, RS485MYLINE); //Disable TX driver.
    }
}


...fwiw, I've found these messages with some related and similar content.

serial write completion and documentation mismatch
sdWrite only two bytes
Issues with Serial and Event
STM32 sd Spurious Character

Thanks,

Greg

Re: Serial Driver and IO_TRANSMISSION_END Event

Posted: Wed Aug 28, 2013 8:22 am
by Giovanni
Hi,

For RS485 I recommend the use of the UART driver instead of serial driver, using UART you have a callback for the end of transmission.

The serial driver has just one event source, all events are added as "flags", the end of transmission is one of those flags. You have to wait for the event then perform a chEvtGetAndClearFlags() on the listener and process the flags in the returned work, at least those who interest you.

Code: Select all

/**
 * @name    I/O status flags added to the event listener
 * @{
 */
/** @brief No pending conditions.*/
#define CHN_NO_ERROR            0
/** @brief Connection happened.*/
#define CHN_CONNECTED           1
/** @brief Disconnection happened.*/
#define CHN_DISCONNECTED        2
/** @brief Data available in the input queue.*/
#define CHN_INPUT_AVAILABLE     4
/** @brief Output queue empty.*/
#define CHN_OUTPUT_EMPTY        8
/** @brief Transmission end.*/
#define CHN_TRANSMISSION_END    16
/** @} */


and also

Code: Select all

/**
 * @name    Serial status flags
 * @{
 */
#define SD_PARITY_ERROR         32  /**< @brief Parity error happened.      */
#define SD_FRAMING_ERROR        64  /**< @brief Framing error happened.     */
#define SD_OVERRUN_ERROR        128 /**< @brief Overflow happened.          */
#define SD_NOISE_ERROR          256 /**< @brief Noise on the line.          */
#define SD_BREAK_DETECTED       512 /**< @brief Break detected.             */
/** @} */


Giovanni

Re: Serial Driver and IO_TRANSMISSION_END Event

Posted: Thu Aug 29, 2013 5:42 am
by jcbarlow
Don't the F4 USARTS have the hardware RS-485 driver enable feature like the F3 and F0?

Re: Serial Driver and IO_TRANSMISSION_END Event

Posted: Thu Aug 29, 2013 7:54 am
by Giovanni
Not sure, F0 and F3 have a new UART with significant differences.

Giovanni

Re: Serial Driver and IO_TRANSMISSION_END Event

Posted: Thu Aug 29, 2013 6:30 pm
by GregB
I want to revisit this an do it the right way eventually, but for the meantime, I've patched serve_interrupt() in serial_lld.c...

Code: Select all

  /* Physical transmission end.*/
  if (sr & USART_SR_TC) {
    chSysLockFromIsr();
    chIOAddFlagsI(sdp, IO_TRANSMISSION_END);
    chSysUnlockFromIsr();
    u->CR1 = cr1 & ~USART_CR1_TCIE;
    u->SR &= ~USART_SR_TC;
    palClearPad(GPIOA, 13); //Adding for RS-485.
  }
}


..I'm not 100% sure if that's OK to do in this context, or if I need to move it up to the locked portion.

Thanks,

Greg

Re: Serial Driver and IO_TRANSMISSION_END Event

Posted: Thu Aug 29, 2013 6:36 pm
by Giovanni
It is not very clean but correct. PAL macros can be used in any context.

Note that ANY UART will clear that pad, that function is shared.

Giovanni

Re: Serial Driver and IO_TRANSMISSION_END Event

Posted: Thu Aug 29, 2013 7:47 pm
by GregB
Yeah, there the Right Way(TM), and then there's the other way ;-) Thanks for your input.

Greg