Thanks for your help finally I could use the serial driver. The problem was the interface I was using to see the data "A". It was working all the time, sorry for the mistake.
I would like to ask you about the IRQ to the serial driver and about a problem that I have with EXT driver. I'm reading the serial port into a static Thread but I would like to read the port only when it receives the data using an interruption, but I really don't know how doing it. The other question is about EXT driver, I'm reading 8 Digital inputs but unfortunately two of them are in the same channel 13 I had already seen in the forum that for ST32F4 it is not possible to do that, but I have already the hardware, maybe you could advice to me another way pretty close to an interruption for reading this PIN.
Code: Select all
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include <stdio.h>
#include <string.h>
uint8_t DI = 0, DO1 = 0;
uint16_t DI1E=0,DI2E=0,DIC=0;
uint16_t AI1=0, AI2=0, AI3=0, AI4=0;
char message[11];
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
#define N 5 // 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();
}
}
static WORKING_AREA(waThread2, 128);
static msg_t Thread2(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 = 0;
memcpy(message, &values, sizeof (values));
chSequentialStreamWrite((BaseSequentialStream *)&SD1, (const uint8_t *)&message, 10);
chSequentialStreamRead((BaseSequentialStream *)&SD1, (uint8_t *)&receive,4);
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);
adcStartConversionI(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
chThdSleepMilliseconds(1000);
}
return(0);
}
/* Digital Inputs*/
static void led5off(void *arg) {
(void)arg;
palClearPad(GPIOA, GPIO_LED);
}
static void extcb1(EXTDriver *extp, expchannel_t channel) {
static VirtualTimer vt4;
(void)extp;
(void)channel;
DI1E = palReadPort(GPIOE);
DI2E = ((DI1E & 0x5C)>>2) | (DI1E & 0x80) | ((DI1E & 0x3000)>>7);
DI = DI2E;
//chSysLockFromIsr();
//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_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_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);
adcStart(&ADCD1, NULL);
palSetGroupMode(GPIOC, PAL_PORT_BIT(0) | PAL_PORT_BIT(1) | PAL_PORT_BIT(2) | PAL_PORT_BIT(3),0,PAL_MODE_INPUT_ANALOG);
palClearPad(GPIOE, GPIOE_DO1);
palClearPad(GPIOE, GPIOE_DO2);
palClearPad(GPIOE, GPIOE_DO3);
palClearPad(GPIOE, GPIOE_DO4);
chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
while (TRUE) {
}
}
I hope you could help me, thanks a lot.