Help wit serial read and write data

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
adnil
Posts: 16
Joined: Fri Aug 09, 2013 5:12 am

Help wit serial read and write data

Post by adnil »

Hello,

I want to ask you wich is the best way to read and write data with SD1 constantly in the main thread.

Thank you very much
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Help wit serial read and write data

Post by Giovanni »

Hi,

You may use chprintf() or chnWriteTimeout(), example:

Code: Select all

chprintf(&SD1, "Hello World!");

chnWriteTimeout(&SD1, "Hello World!", 12, TIME_INFINITE);


Giovanni
adnil
Posts: 16
Joined: Fri Aug 09, 2013 5:12 am

Re: Help wit serial read and write data

Post by adnil »

Thank you for your reply,

Maybe can you help me, I made this code and I don't understand why it did not work, thanks a lot and sorry about this kind of questions.

Code: Select all

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

    /*
     * System initializations.
     * - HAL initialization, this also initializes the configured device drivers
     *   and performs the board-specific initializations.
     * - Kernel initialization, the main() function becomes a thread and the
     *   RTOS is active.
     */
    halInit();
    chSysInit();

  /*
   * Activates the serial driver 1 using the driver default configuration.
   */
  sdStart(&SD1, NULL);
  extStart(&EXTD1, &extcfg);
  adcStart(&ADCD1, NULL);
  /*
   * Initializes the ADC driver 1 and enable the thermal sensor.
   * The pin PC1 on the port GPIOC is programmed as analog input.
   */
  //adcStart(&ADCD1, NULL);
  //adcSTM32EnableTSVREFE();
  palSetGroupMode(GPIOC, PAL_PORT_BIT(0) | PAL_PORT_BIT(1) | PAL_PORT_BIT(2) | PAL_PORT_BIT(3),0,PAL_MODE_INPUT_ANALOG);
  //palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG);

  palClearPad(GPIOE, GPIOE_DO1);
  palClearPad(GPIOE, GPIOE_DO2);
  palClearPad(GPIOE, GPIOE_DO3);
  palClearPad(GPIOE, GPIOE_DO4);
 

    while (TRUE) {    

   if(chSequentialStreamRead((BaseSequentialStream *)&SD1, (uint8_t *)&receive,1)==0)
   
   chprintf((BaseSequentialStream *)&SD1,"values: %d \n",flag);

   chThdSleepMilliseconds(100);
    }
}
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Help wit serial read and write data

Post by Giovanni »

Are the serial TX and RX pins programmed as appropriate alternate functions? see the data sheet for details.

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Help wit serial read and write data

Post by steved »

Your test on chSequentialStreamRead() is the wrong way round - it returns the number of characters received.
adnil
Posts: 16
Joined: Fri Aug 09, 2013 5:12 am

Re: Help wit serial read and write data

Post by adnil »

Thanks for your replies,

The pins are ok because with the code of the post viewtopic.php?f=2&t=1770 they work, but I have a lot of mistakes as you told me. I'm trying to correct it and now it looks like this, but I have a problem with the if sentence when i only write on the port it works fine but if I try to read the port the code does not work. I suppose that if the port is empty the chSequentialStreamRead() functions returns "0" and then write on the port.

Code: Select all

#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include <stdio.h>
#include <string.h>
#include "chconf.h"
static int8_t flag = 1;
uint8_t  DI = 0, DO1 = 0, DOE=0;
uint16_t DI1E=0,DO=0;
uint16_t AI1=0, AI2=0, AI3=0, AI4=0;
char message[10];
char receive = 0;
static int8_t flagTx = 0, flagRv=0;
static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n);

struct frame {
    uint16_t AInput1;
    uint16_t AInput2;
    uint16_t AInput3;
    uint16_t AInput4;
    uint8_t DInput;
    uint8_t  state;
} values;



/* Digital Inputs*/


static void extcb1(EXTDriver *extp, expchannel_t channel) {
  (void)extp;
  (void)channel;
       flag = 1;
}

static const EXTConfig extcfg = {
  {
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
  }
};


/* Total number of channels to be sampled by a single ADC operation.*/
#define ADC_GRP1_NUM_CHANNELS   4
/* Depth of the conversion buffer, channels are sampled four times each.*/
#define ADC_GRP1_BUF_DEPTH      32  // Número de muestras
#define N         5  // Corrimientos para la división 2^N =  define ADC_GRP1_BUF_DEPTH

/*
 * ADC samples buffer.
 */
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];


/*
 * ADC conversion group.
 * Mode:        Linear buffer, ADC_GRP1_BUF_DEPTH samples of 4 channels, SW triggered.
 * Channels:    IN10   (48 cycles sample time)
 * Channels:    IN11   (48 cycles sample time)
 * Channels:    IN12   (48 cycles sample time)
 * Channels:    IN13   (48 cycles sample time)
 */
static const ADCConversionGroup adcgrpcfg = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  adccb,
  NULL,
  /* HW dependent part.*/
  0,
  ADC_CR2_SWSTART,
  ADC_SMPR1_SMP_AN13(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN12(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN11(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN10(ADC_SAMPLE_56),
  0,
  ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
  0,
  ADC_SQR3_SQ4_N(ADC_CHANNEL_IN13) | ADC_SQR3_SQ3_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ2_N(ADC_CHANNEL_IN11) | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN10)
};



/*
 * ADC end conversion callback.
 * The PWM channels are reprogrammed using the latest ADC samples.
 * The latest samples are transmitted into a single SPI transaction.
 */
void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {

  (void) buffer; (void) n;
  /* Note, only in the ADC_COMPLETE state because the ADC driver fires an
     intermediate callback when the buffer is half full.*/
  if (adcp->state == ADC_COMPLETE) {
   int i;
   uint32_t  avg1=0,avg2=0,avg3=0,avg4=0;
    /* Calculates the average values from the ADC samples.*/
     for (i=0;i<ADC_GRP1_BUF_DEPTH;i++){
   avg1 += samples[4*i];
   avg2 += samples[4*i+1];
   avg3 += samples[4*i+2];
   avg4 += samples[4*i+3];
    }
   AI1 = avg1 >> N;
   AI2 = avg2 >> N;
   AI3 = avg3 >> N;
   AI4 = avg4 >> N;
   adcStopConversion(&ADCD1);
  }
}


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

    /*
     * System initializations.
     * - HAL initialization, this also initializes the configured device drivers
     *   and performs the board-specific initializations.
     * - Kernel initialization, the main() function becomes a thread and the
     *   RTOS is active.
     */
    halInit();
    chSysInit();

  /*
   * Activates the serial driver 1 using the driver default configuration.
   */
  sdStart(&SD1, NULL);
  extStart(&EXTD1, &extcfg);
  adcStart(&ADCD1, NULL);
  /*
   * Initializes the ADC driver 1 and enable the thermal sensor.
   * The pin PC1 on the port GPIOC is programmed as analog input.
   */
  //adcStart(&ADCD1, NULL);
  //adcSTM32EnableTSVREFE();
  palSetGroupMode(GPIOC, PAL_PORT_BIT(0) | PAL_PORT_BIT(1) | PAL_PORT_BIT(2) | PAL_PORT_BIT(3),0,PAL_MODE_INPUT_ANALOG);
  //palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG);

  palClearPad(GPIOE, GPIOE_DO1);
  palClearPad(GPIOE, GPIOE_DO2);
  palClearPad(GPIOE, GPIOE_DO3);
  palClearPad(GPIOE, GPIOE_DO4);
 

    while (TRUE) {
   if (flag) {
     DI1E = palReadPort(GPIOE);
     DI  = ((DI1E & 0x7C)>>2) | (DI1E & 0x80) | ((DI1E & 0x3000)>>7);
          flag = 0;
     }
     values.DInput = DI;
     values.state = DOE;
     values.AInput1 = AI1;
     values.AInput2 = AI2;
     values.AInput3 = AI3;
     values.AInput4 = AI4;
     adcStartConversion(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
    

   if(chSequentialStreamRead((BaseSequentialStream *)&SD1, (uint8_t *)&receive,1)==0)
   {
      
      DO = palReadPort(GPIOE);
      DOE = (DO & 0xF00)>>4;
      memcpy(message, &values, sizeof (values));
      chnWriteTimeout(&SD1,  (const uint8_t *)&message, sizeof (values), TIME_INFINITE);
      }
   else
      {
      DO1 = receive;
      if (DO1 == 10 || DO1 == 30|| DO1 == 50|| DO1 == 70){
         palClearPad(GPIOE, GPIOE_DO1);
         palClearPad(GPIOE, GPIOE_DO2);
         palClearPad(GPIOE, GPIOE_DO3);
         palClearPad(GPIOE, GPIOE_DO4);}
      else if   (DO1 == 20)
         palSetPad(GPIOE, GPIOE_DO1);
      else if   (DO1 == 40)
         palSetPad(GPIOE, GPIOE_DO2);
      else if   (DO1 == 60)
         palSetPad(GPIOE, GPIOE_DO3);
      else if   (DO1 == 80)
         palSetPad(GPIOE, GPIOE_DO4);
      }
   //chSequentialStreamWrite((BaseSequentialStream *)&SD1,  (const uint8_t *)&message, sizeof (values));

   chThdSleepMilliseconds(100);
    }
}

Thank you very much again,

Adnil
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Help wit serial read and write data

Post by Giovanni »

Nope, it waits until the specified amount of data is received.

Giovanni
adnil
Posts: 16
Joined: Fri Aug 09, 2013 5:12 am

Re: Help wit serial read and write data

Post by adnil »

Hi,
When I changed the functions of write and read the port and it works but i need to use a funtion like chSequentialStreamWrite for sending my data of 10 bytes. How can I make to work it. Thanks a lot.

Code: Select all

#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include <stdio.h>
#include <string.h>
#include "chconf.h"
static int8_t flag = 1;
uint8_t  DI = 0, DO1 = 0, DOE=0;
uint16_t DI1E=0,DO=0;
uint16_t AI1=0, AI2=0, AI3=0, AI4=0;
char message[10];
char receive = 0;
static int8_t flagTx = 0, flagRv=0;
static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n);

struct frame {
    uint16_t AInput1;
    uint16_t AInput2;
    uint16_t AInput3;
    uint16_t AInput4;
    uint8_t DInput;
    uint8_t  state;
} values;



/* Digital Inputs*/


static void extcb1(EXTDriver *extp, expchannel_t channel) {
  (void)extp;
  (void)channel;
       flag = 1;
}

static const EXTConfig extcfg = {
  {
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOE, extcb1},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
    {EXT_CH_MODE_DISABLED, NULL},
  }
};


/* Total number of channels to be sampled by a single ADC operation.*/
#define ADC_GRP1_NUM_CHANNELS   4
/* Depth of the conversion buffer, channels are sampled four times each.*/
#define ADC_GRP1_BUF_DEPTH      32  // Número de muestras
#define N         5  // Corrimientos para la división 2^N =  define ADC_GRP1_BUF_DEPTH

/*
* ADC samples buffer.
*/
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];


/*
* ADC conversion group.
* Mode:        Linear buffer, ADC_GRP1_BUF_DEPTH samples of 4 channels, SW triggered.
* Channels:    IN10   (48 cycles sample time)
* Channels:    IN11   (48 cycles sample time)
* Channels:    IN12   (48 cycles sample time)
* Channels:    IN13   (48 cycles sample time)
*/
static const ADCConversionGroup adcgrpcfg = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  adccb,
  NULL,
  /* HW dependent part.*/
  0,
  ADC_CR2_SWSTART,
  ADC_SMPR1_SMP_AN13(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN12(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN11(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN10(ADC_SAMPLE_56),
  0,
  ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
  0,
  ADC_SQR3_SQ4_N(ADC_CHANNEL_IN13) | ADC_SQR3_SQ3_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ2_N(ADC_CHANNEL_IN11) | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN10)
};



/*
* ADC end conversion callback.
* The PWM channels are reprogrammed using the latest ADC samples.
* The latest samples are transmitted into a single SPI transaction.
*/
void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {

  (void) buffer; (void) n;
  /* Note, only in the ADC_COMPLETE state because the ADC driver fires an
     intermediate callback when the buffer is half full.*/
  if (adcp->state == ADC_COMPLETE) {
   int i;
   uint32_t  avg1=0,avg2=0,avg3=0,avg4=0;
    /* Calculates the average values from the ADC samples.*/
     for (i=0;i<ADC_GRP1_BUF_DEPTH;i++){
   avg1 += samples[4*i];
   avg2 += samples[4*i+1];
   avg3 += samples[4*i+2];
   avg4 += samples[4*i+3];
    }
   AI1 = avg1 >> N;
   AI2 = avg2 >> N;
   AI3 = avg3 >> N;
   AI4 = avg4 >> N;
   adcStopConversion(&ADCD1);
  }
}


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

    /*
     * System initializations.
     * - HAL initialization, this also initializes the configured device drivers
     *   and performs the board-specific initializations.
     * - Kernel initialization, the main() function becomes a thread and the
     *   RTOS is active.
     */
    halInit();
    chSysInit();

  /*
   * Activates the serial driver 1 using the driver default configuration.
   */
  sdStart(&SD1, NULL);
  extStart(&EXTD1, &extcfg);
  adcStart(&ADCD1, NULL);
  /*
   * Initializes the ADC driver 1 and enable the thermal sensor.
   * The pin PC1 on the port GPIOC is programmed as analog input.
   */
  //adcStart(&ADCD1, NULL);
  //adcSTM32EnableTSVREFE();
  palSetGroupMode(GPIOC, PAL_PORT_BIT(0) | PAL_PORT_BIT(1) | PAL_PORT_BIT(2) | PAL_PORT_BIT(3),0,PAL_MODE_INPUT_ANALOG);
  //palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG);

  palClearPad(GPIOE, GPIOE_DO1);
  palClearPad(GPIOE, GPIOE_DO2);
  palClearPad(GPIOE, GPIOE_DO3);
  palClearPad(GPIOE, GPIOE_DO4);
 

    while (TRUE) {
   if (flag) {
     DI1E = palReadPort(GPIOE);
     DI  = ((DI1E & 0x7C)>>2) | (DI1E & 0x80) | ((DI1E & 0x3000)>>7);
          flag = 0;
     }
     values.DInput = DI;
     values.state = DOE;
     values.AInput1 = AI1;
     values.AInput2 = AI2;
     values.AInput3 = AI3;
     values.AInput4 = AI4;
     adcStartConversion(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
     

   if(chnWriteTimeout(&SD1,  (const uint8_t *)&receive, 1, 50))
   {
     
      DO = palReadPort(GPIOE);
      DOE = (DO & 0xF00)>>4;
      memcpy(message, &values, sizeof (values));
      chnWriteTimeout(&SD1,  (const uint8_t *)&message, sizeof (values), TIME_INFINITE);
      }
   else
      {
      DO1 = receive;
      if (DO1 == 10 || DO1 == 30|| DO1 == 50|| DO1 == 70){
         palClearPad(GPIOE, GPIOE_DO1);
         palClearPad(GPIOE, GPIOE_DO2);
         palClearPad(GPIOE, GPIOE_DO3);
         palClearPad(GPIOE, GPIOE_DO4);}
      else if   (DO1 == 20)
         palSetPad(GPIOE, GPIOE_DO1);
      else if   (DO1 == 40)
         palSetPad(GPIOE, GPIOE_DO2);
      else if   (DO1 == 60)
         palSetPad(GPIOE, GPIOE_DO3);
      else if   (DO1 == 80)
         palSetPad(GPIOE, GPIOE_DO4);
      }
   //chSequentialStreamWrite((BaseSequentialStream *)&SD1,  (const uint8_t *)&message, sizeof (values));
   chprintf((BaseSequentialStream *)&SD1,"values: %d \n",flag);
   chThdSleepMilliseconds(100);
    }
}
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Help wit serial read and write data

Post by Giovanni »

I don't understand the question.

Giovanni
Post Reply