Page 1 of 1

chSequentialStreamWrite Time for transmission

Posted: Fri Mar 14, 2014 2:36 am
by adnil
Hi,

I'm writting a program for read analog and digital inputs and write digital outpus. I'm sending a string of 10 bytes using chSequentialStreamWrite with the state of the inputs in static msg_t Thread1(void *arg). I have a problem, if I put the chThdSleepMilliseconds time less than 600ms I receive in the serial port only 8 bytes. I want to reduce this time and receive 10 bytes, because 600ms for sending the data is affecting the performance of the application.

Thanks a lot,


Code: Select all

#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include <stdio.h>
#include <string.h>
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;
static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n);

/*Data to Send*/

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


/* 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);
   //chSysUnlockFromIsr();
  }
}



/*
 * This is a periodic thread that does absolutely nothing except flashing
 * a LED.
 */

/*static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (TRUE) {
    palSetPad(GPIOA, GPIO_LED);       
    chThdSleepMilliseconds(500);
    palClearPad(GPIOA, GPIO_LED);     
    chThdSleepMilliseconds(500);
  }
}*/


static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {

    (void)arg;
   //int i=0;
    while (TRUE) {
   values.AInput1 = AI1;
   values.AInput2 = AI2;
   values.AInput3 = AI3;
   values.AInput4 = AI4;
   values.DInput = DI;
   values.state = DOE;
   memcpy(message, &values, sizeof (values));
   //chprintf((BaseSequentialStream *)&SD1,"values: %s \n",message);
        [color=#FF4000]chSequentialStreamWrite((BaseSequentialStream *)&SD1,  (const uint8_t *)&message, sizeof (values));[/color]
   //chSequentialStreamWrite((BaseSequentialStream *)&SD1,message,10);
   adcStartConversionI(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
   DO = palReadPort(GPIOE);
   DOE = (DO & 0xF00)>>4;
   [color=#FF0000]chThdSleepMilliseconds(600)[/color];  // si bajo este tiempo tengo error de comunicación
   }
   return(0);
}

static WORKING_AREA(waThread2, 128);
static msg_t Thread2(void *arg) {

    (void)arg;
   //int i=0;
    while (TRUE) {
   if(chSequentialStreamRead((BaseSequentialStream *)&SD1, (uint8_t *)&receive,1))
   {
   DO1 = receive;

   if (DO1 == 10)
      palClearPad(GPIOE, GPIOE_DO1);
   else if   (DO1 == 20)
           palSetPad(GPIOE, GPIOE_DO1);

   else if   (DO1 == 30)
      palClearPad(GPIOE, GPIOE_DO2);
   else if   (DO1 == 40)
           palSetPad(GPIOE, GPIOE_DO2);

   else if   (DO1 == 50)
      palClearPad(GPIOE, GPIOE_DO3);
   else if   (DO1 == 60)
           palSetPad(GPIOE, GPIOE_DO3);

   else if   (DO1 == 70)
      palClearPad(GPIOE, GPIOE_DO4);
   else if   (DO1 == 80)
           palSetPad(GPIOE, GPIOE_DO4);
   }

   //chprintf((BaseSequentialStream *)&SD1,"DOE: %d  \n",DOE);
   //chThdSleepMilliseconds(800);
   }
   return(0);
}


/* Digital Inputs*/


static void extcb1(EXTDriver *extp, expchannel_t channel) {
  static VirtualTimer vt4;
  (void)extp;
  (void)channel;
  chSysLockFromIsr();
  DI1E = palReadPort(GPIOE);
  DI  = ((DI1E & 0x7C)>>2) | (DI1E & 0x80) | ((DI1E & 0x3000)>>7);
  //DOE = (DI1E & 0xF00)>>4;
 

 
  //palSetPad(GPIOA, GPIO_LED);
   ///

    //

  chSysUnlockFromIsr();
}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},
  }
};




/*
 * 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);
  /*
   * 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);
 
 


/*
   * Creates the example thread.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
    while (TRUE) {
   
    }
}

Re: chSequentialStreamWrite Time for transmission

Posted: Fri Mar 14, 2014 8:59 am
by Giovanni
Hi,

You have several problems there:
1) Functions called out of proper context, please read the chapter "Kernel Concepts" in the documentation. Enabling the state checker in chconf.h would help you finding all the protocol violations.
2) The main thread has an empty loop at the end while(1);. This means that the main never releases the CPU for the other threads at the same priority level until a round robin tick happens. You should put a chThdSleep() there, see all the provided demos for example.

Giovanni

Re: chSequentialStreamWrite Time for transmission

Posted: Tue Mar 18, 2014 4:08 am
by adnil
Thank you for your reply,

Maybe can you show me where in the code the functions are called out of proper context please?

Re: chSequentialStreamWrite Time for transmission

Posted: Tue May 06, 2014 9:18 pm
by colin
adnil wrote:Thank you for your reply,

Maybe can you show me where in the code the functions are called out of proper context please?


First spend some minutes reading the Kernel Concepts document. Pay attention to the "Serving Regular Interrupt" and "I-Locked" states.

Problem (1) you call Normal-class API function adcStopConversion() from the ADC callback.

When your ADC callback is invoked, it's in the "serving regular interrupt" state. That means you must call chSysLockFromIsr() to enter the I-Locked state and then you can call I-Class API functions. But not Normal-class API functions like you are calling adcStopConversion() -- they can only be called from user code normally. But notice that there is an adcStopConversionI() function (note the "I" suffix meaning I-class). Look at the ChibiOS/RT source code demos/ARMCM3-STM32F100-DISCOVERY/main.c function adccb() for instance:

Code: Select all

void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {
...  // We're initially in Serving Regular Interrupt state
    chSysLockFromIsr();
...   // OK, now we can call I-class API functions
    pwmEnableChannelI(&PWMD3, 2, PWM_FRACTION_TO_WIDTH(&PWMD3, 4096, avg_ch1));  // I-Class
    pwmEnableChannelI(&PWMD3, 3, PWM_FRACTION_TO_WIDTH(&PWMD3, 4096, avg_ch2));  // I-Class

    /* SPI slave selection and transmission start.*/
    spiSelectI(&SPID1);  // I-Class
    spiStartSendI(&SPID1, ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH, samples);  // I-Class

    // Unlock and return to Serving Regular Interrupt state
    chSysUnlockFromIsr();
...
}


Problem (2) you call I-Class API function adcStartConversionI() from user code in Thread1().

The API class design is really quite elegant and well documented. I think if you read the Kernel Concepts page linked above you'll find it extremely helpful.

Re: chSequentialStreamWrite Time for transmission

Posted: Sun Jun 22, 2014 1:02 pm
by adnil
Thanks a lot