Page 1 of 1

IRQ, EVENT and Srial Driver

Posted: Thu Oct 22, 2015 9:56 am
by antoniolinux
hi to all
i write this code

Code: Select all

#define EVT_UART_RX EVENT_MASK(0)
#define EVT_UART_TX EVENT_MASK(1)
#define EVT_UART_ERR EVENT_MASK(2)

/*
 * Pointer to the UART handler thread.
 */
static const uint8_t errore[]="Error uart";
static const uint8_t dati[]="Dati uart";
static const uint8_t letti[]="Read uart";
static const uint8_t hello[]="Hello World";

thread_t *uart_thread;
/*
 * Red LED blinker thread, times are in milliseconds.
 */

CH_IRQ_HANDLER(UART_RX_IRQ) {
  eventmask_t events = 0;

  uint32_t sr;
  USART_TypeDef *u=USART2;
  sr = u->SR;
  u->SR = ~sr;

  CH_IRQ_PROLOGUE();
  /* Events mask initially at zero.*/
  events = 0;

  /* If there are errors.*/
  if ((u->SR & USART_SR_ORE) != 0)
    events |= EVT_UART_ERR;

  /* If there is data available in the UART RX FIFO.*/
    events |= EVT_UART_ERR;

  /* If there is data available in the UART RX FIFO.*/
  if ((u->SR & (USART_SR_RXNE | USART_SR_ORE | USART_SR_NE | USART_SR_FE |
     USART_SR_PE)) != 0)

  if ((u->SR & USART_SR_TXE) != 0) {
    events |= EVT_UART_TX;

  /* Segnaling events to the thread, if any.*/
  if (events) {
    chSysLockFromISR();
    chEvtSignalI(uart_thread, events);
    chSysUnlockFromISR();
  }

  CH_IRQ_EPILOGUE();
}
}

 static THD_WORKING_AREA(waRead1, 128);
    static THD_FUNCTION(Thread1,arg) {
  (void)arg;
    chRegSetThreadName("buffer");
    /* Thread activity.*/
    while (true) {
      /* Waiting for any event.*/
      eventmask_t evt = chEvtWaitAny(ALL_EVENTS);

      /* Serving events.*/
      if (evt & EVT_UART_ERR) {
        /* Error event.*/
        error_handler();
      }
      if (evt & EVT_UART_RX) {
        /* Data available event.*/
        rx_handler();
      }
      if (evt & EVT_UART_TX) {
        /* TX ready event.*/
        tx_handler();
      }
    }
  }


void error_handler(){
  sdWrite(&SD2, errore, 13);
}


void rx_handler(){
  sdWrite(&SD2, dati, 13);
}

void tx_handler(){
  sdWrite(&SD2, letti, 13);
}

when i sand data on SD2 i don't see nothing, i dont' know where is the error
can you help me?
best regards
A.

Re: IRQ, EVENT and Srial Driver

Posted: Thu Oct 22, 2015 11:06 am
by Giovanni
Hi,

Several problems, the main one is that you don't need to write ISRs in order to use the serial driver, the other ones are a consequence of the first one.

Giovanni

Re: IRQ, EVENT and Serial Driver

Posted: Thu Oct 22, 2015 1:35 pm
by antoniolinux
hi rewrite all with event system but don't work......
I don't understand where is the error,

Code: Select all

static uint8_t bufferMsg[BUFFER_SIZE];
/*
 * Pointer to the UART handler thread.
 */
static THD_WORKING_AREA(waRead5, 128);
static THD_FUNCTION(Thread5,arg) {
  chRegSetThreadName("th_Riempio_Buffer");
  msg_t charbuf;
  int count=0;
  uint8_t c;
  event_listener_t Uart2Data;
  eventmask_t flags;
 chEvtRegisterMask((event_source_t *)chnGetEventSource(&SD2), &Uart2Data, EVENT_MASK(1));
  while (TRUE) {
       chEvtWaitOneTimeout(EVENT_MASK(1), MS2ST(10));
       chSysLock();
       flags =chEvtGetAndClearFlags(&Uart2Data);
      chSysUnlock();
       if (flags & CHN_INPUT_AVAILABLE)
                {
              sdWrite(&SD2,"OK\r\n",16);
                }
      }
}

int main(void) {

  halInit();
  chSysInit();
  sdStart(&SD2, NULL);
 chThdCreateStatic(waRead5, sizeof(waRead5),NORMALPRIO, Thread5, NULL);
  while (true) {
    if (!palReadPad(GPIOC, GPIOC_BUTTON))
    {
      sdWrite(&SD2,"Ciao\r\n",16);
    }
    chThdSleepMilliseconds(500);
  }
}


can you help me?

Re: IRQ, EVENT and Srial Driver

Posted: Thu Oct 22, 2015 2:05 pm
by Giovanni
You need to specify what it is supposed to do and what you get instead.

I see several strange things there:
- You wait for the event with a timeout so it exits even if the event has not occurred.
- You wait for data available then you write instead of read.

The debugger is your friend, use it.

Giovanni

Re: IRQ, EVENT and Srial Driver

Posted: Fri Oct 23, 2015 10:19 am
by antoniolinux
i want start a event when on SD1 arrived data, i wired the pin tx and rx for simulate the arrive data on SD1,
i write this code but when i start program the thread go in status WTANDEVT, and don't running,

Code: Select all

event_listener_t Uart1Data;
static THD_WORKING_AREA(waRead5, 128);
static THD_FUNCTION(Thread5,arg) {
  chRegSetThreadName("th_Riempio_Buffer");

  eventmask_t flags;
  while (TRUE) {
       chEvtWaitAll(EVENT_MASK(1));
      chSysLock();
       flags =chEvtGetAndClearFlagsI(&Uart1Data);
      chSysUnlock();
       if (flags & CHN_INPUT_AVAILABLE)
                {
              sdWrite(&SD2,"OK\r\n",4);
                }
      }
}
....
main

 chThdCreateStatic(waRead5, sizeof(waRead5),NORMALPRIO, Thread5, NULL);

 chEvtRegisterMask((event_source_t *)chnGetEventSource(&SD1), &Uart1Data, EVENT_MASK(1));


can you help me?

best regards
A.

Re: IRQ, EVENT and Srial Driver

Posted: Fri Oct 23, 2015 1:57 pm
by antoniolinux
hi to all, i want start a event when on SD1 arrived data, i wired the pin tx and rx for simulate the arrive data on SD1,
i fix some errore in last post, and write this code:

Code: Select all


static THD_WORKING_AREA(waRead5, 128);
static THD_FUNCTION(Thread5,arg) {
  (void)arg;
  chRegSetThreadName("th_Riempio_Buffer");
  palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7));
  palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7));
  sdStart(&SD1, NULL);
  event_listener_t Uart1Data;
  eventmask_t flags;
  chEvtRegisterMask((event_source_t *)chnGetEventSource(&SD1), &Uart1Data, EVENT_MASK(1));
  while (TRUE) {
       chEvtWaitAll(EVENT_MASK(1));
      chSysLock();
       flags =chEvtGetAndClearFlagsI(&Uart1Data);
      chSysUnlock();
       if (flags & CHN_INPUT_AVAILABLE)
                {
              sdWrite(&SD2,"OK\r\n",4);
                }
      }
}


but now i have this error if i reset the my sT_Nucleo_L152RE and later i press the button all work well, but if i press other time i don't see nothing, i see the thread is in in status WTANDEVT, the debug don't give other problem