UART and buffer

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: barthess, RoccoMarco

User avatar
fifa
Posts: 4
Joined: Thu Oct 15, 2015 7:49 pm

UART and buffer

Postby fifa » Thu Oct 22, 2015 2:06 pm

hi to all, i a newbie and i write this easy program for control data stored in the Uart buffer

Code: Select all

#include "ch.h"
#include "hal.h"

static virtual_timer_t vt3, vt4, vt5;
static uint8_t message[] = "CIAO\r";
static uint8_t buffer[16];


static void led3off(void *p) {

  (void)p;
  palClearPad(GPIOA, GPIOA_LED_GREEN);
}

static void led4off(void *p) {

  (void)p;
  palClearPad(GPIOA, GPIOA_LED_GREEN);
}

static void led5off(void *p) {

  (void)p;
  palClearPad(GPIOA, GPIOA_LED_GREEN);
}

/*
 * This callback is invoked when a transmission buffer has been completely
 * read by the driver.
 */
static void txend1(UARTDriver *uartp) {

  (void)uartp;
}

/*
 * This callback is invoked when a transmission has physically completed.
 */
static void txend2(UARTDriver *uartp) {

  (void)uartp;
  palSetPad(GPIOA, GPIOA_LED_GREEN);
  chSysLockFromISR();
  chVTResetI(&vt5);
  chVTSetI(&vt5, MS2ST(200), led5off, NULL);
  chSysUnlockFromISR();
}

/*
 * This callback is invoked on a receive error, the errors mask is passed
 * as parameter.
 */
static void rxerr(UARTDriver *uartp, uartflags_t e) {

  (void)uartp;
  (void)e;
}

/*
 * This callback is invoked when a character is received but the application
 * was not ready to receive it, the character is passed as parameter.
 */
static void rxchar(UARTDriver *uartp, uint16_t c) {

  (void)uartp;
  (void)c;
  /* Flashing the LED each time a character is received.*/
  palSetPad(GPIOA, GPIOA_LED_GREEN);
  chSysLockFromISR();
  chVTResetI(&vt4);
  chVTSetI(&vt4, MS2ST(200), led4off, NULL);
  chSysUnlockFromISR();
}

/*
 * This callback is invoked when a receive buffer has been completely written.
 */
static void rxend(UARTDriver *uartp) {

  (void)uartp;

  /* Flashing the LED each time a character is received.*/
  palSetPad(GPIOA, GPIOA_LED_GREEN);
  chSysLockFromISR();
  chVTResetI(&vt3);
  chVTSetI(&vt3, MS2ST(200), led3off, NULL);
  chSysUnlockFromISR();
}

/*
 * UART driver configuration structure.
 */
static UARTConfig uart_cfg_2 = {
  txend1,
  txend2,
  rxend,
  rxchar,
  rxerr,
  9600,
  0,
  USART_CR2_LINEN,
  0
};



void controllo(uint8_t *m)

{

while(*m!=0x0D)  {
if(*m==0x43)  //C 0x43
{
  chSysLockFromISR();
  uartStartSendI(&UARTD2, 2, "O1");
  chSysUnlockFromISR();
}
chThdSleepMilliseconds(50);

if(*m==0x41)  //A 0x41
{
  chSysLockFromISR();
  uartStartSendI(&UARTD2, 2, "O2");
  chSysUnlockFromISR();
}

*m++;

 }
}

/*
 * Application entry point.
 */
int main(void) {

  halInit();
  chSysInit();


     uartStart(&UARTD2, &uart_cfg_2);
     palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
     palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));


  while (true) {

    uartStopReceive(&UARTD2);
    uartStopSend(&UARTD2);
    uartStartReceive(&UARTD2, 5, buffer);
    uartStartSend(&UARTD2, 4, message);
    if(UARTD2.rxstate==2)
    {
    chThdSleepMilliseconds(50);
controllo(&buffer);
    chThdSleepMilliseconds(200);
  }
    chThdSleepMilliseconds(500);

  }
}


i see the string ciao but i don't have nothing in the buffer, can you help me?
best regards
F.

User avatar
Giovanni
Site Admin
Posts: 13085
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 759 times
Been thanked: 637 times
Contact:

Re: UART and buffer

Postby Giovanni » Thu Oct 22, 2015 2:36 pm

Please make very specific questions if you want meaningful answers, posting random code will likely not get attention.

This forum is not a remote debugging service.

Giovanni

antoniolinux
Posts: 38
Joined: Thu Dec 19, 2013 12:59 pm

Re: UART and buffer

Postby antoniolinux » Mon Oct 26, 2015 5:07 pm

hi, in this period i have a similar problem, you can use Serial Driver in this metodology you can use
1) Event you can see this viewtopic.php?f=16&t=2944
or
2) use chnRead(&SD1,&bufferMsg,64); if you want use uart directly, you know is very hard to learn


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 4 guests