now my code has another bug..,
when i set the depth=8,and the callback return the n =4,
depth=16,n=8;
this is my test code ,it work like pwmstart->pwmcallback->adcStartConversion->adccallback->Bsem->thread
then the thread print the samples buffer and n.
Code: Select all
#include <stdio.h>
#include <string.h>
#include "ch.h"
#include "hal.h"
#include "ch_test.h"
#include "myshell.h"
#define CHP ((BaseSequentialStream *)&SD2)
#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
#define ADC_GRP_NUM_CHANNELS 3 //adc 通道数
#define ADC_GRP_BUF_DEPTH 16 //数据深度
int samplessize = 0;
binary_semaphore_t bsem, errsem;
adcsample_t samples[ADC_GRP_NUM_CHANNELS * ADC_GRP_BUF_DEPTH]; //buffer
/**
* @brief ADC
*
*/
static void adccallback(ADCDriver *adcp, adcsample_t *buffer, size_t n)
{
(void)adcp;
(void)buffer;
(void)n;
//锁定进入isr
chSysLockFromISR();
samplessize = n;
chBSemSignalI(&bsem); //发送信号
adcStopConversionI(&ADCD1); //停止adc转换
//解锁
chSysUnlockFromISR();
}
static void adcerrorcallback(ADCDriver *adcp, adcerror_t err)
{
(void)adcp;
(void)err;
chSysLockFromISR();
// chBSemSignalI(&errsem); //发送信号
chSysUnlockFromISR();
palClearPad(GPIOB, GPIOB_LED_1);
}
const ADCConversionGroup adcgrpcfg = {
TRUE,
ADC_GRP_NUM_CHANNELS,
adccallback,
adcerrorcallback,
0,
ADC_CR2_TSVREFE, /* CR2 */
ADC_SMPR1_SMP_VREF(ADC_SAMPLE_239P5) | ADC_SMPR1_SMP_AN11(ADC_SAMPLE_239P5) | ADC_SMPR1_SMP_AN10(ADC_SAMPLE_239P5),
0, /* SMPR2 */
ADC_SQR1_NUM_CH(ADC_GRP_NUM_CHANNELS),
0,
ADC_SQR3_SQ1_N(ADC_CHANNEL_VREFINT) | ADC_SQR3_SQ2_N(ADC_CHANNEL_IN10) | ADC_SQR3_SQ3_N(ADC_CHANNEL_IN11)};
/**
* @brief PWM
*
*/
static void pwmpcb(PWMDriver *pwmp)
{
(void)pwmp;
}
static void pwmc1cb(PWMDriver *pwmp)
{
(void)pwmp;
chSysLockFromISR();
// chBSemSignalI(&bsem);
adcStartConversion(&ADCD1, &adcgrpcfg, samples, ADC_GRP_BUF_DEPTH);
chSysUnlockFromISR();
}
static PWMConfig pwmcfg = {
100000,
1000, //pwm 10s
pwmpcb,
{{PWM_OUTPUT_ACTIVE_HIGH, pwmc1cb},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL}},
0,
0,
#if STM32_PWM_USE_ADVANCED
0
#endif
};
/*
* blinker
*/
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg)
{
(void)arg;
chRegSetThreadName("blinker");
while (true)
{
msg_t msg = chBSemWaitTimeout(&bsem, MS2ST(1000));
/* time out*/
if (msg == MSG_TIMEOUT)
{
chprintf((BaseSequentialStream *)&SD2, "\r\ncurrent thread timeout\r\n");
continue;
}
chprintf((BaseSequentialStream *)&SD2, "samples size=%d\r\n", samplessize);
for (int i = 0; i < ADC_GRP_NUM_CHANNELS; i++)
{
for (int j = 0; j < ADC_GRP_BUF_DEPTH; j++)
chprintf((BaseSequentialStream *)&SD2, "%d\t", samples[i + j * ADC_GRP_NUM_CHANNELS]);
chprintf((BaseSequentialStream *)&SD2, "\r\n");
}
chprintf((BaseSequentialStream *)&SD2, "\r\n\r\n");
}
}
int main(void)
{
halInit();
chSysInit();
shellInit();
sdStart(&SD2, NULL);
//Bsem init
chBSemObjectInit(&bsem, TRUE);
// chBSemObjectInit(&errsem, TRUE);
/*
* pwm init
*/
pwmStart(&PWMD1, &pwmcfg);
pwmEnablePeriodicNotification(&PWMD1);
palSetPadMode(IOPORT1, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL); /*PA8*/
/*
* adc init
*/
//1.初始化GPIO
palSetGroupMode(GPIOC, PAL_PORT_BIT(0) | PAL_PORT_BIT(1),
0, PAL_MODE_INPUT_ANALOG);
//2.开启adc
adcStart(&ADCD1, NULL);
/*
* 75%
*/
pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
pwmEnableChannelNotification(&PWMD1, 0);
// chThdSleepMilliseconds(500000);
/*
* stop pwm
*/
// pwmDisableChannel(&PWMD1, 0);
// pwmStop(&PWMD1);
/*
* creat blinker thread
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
while (true)
{
thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
"shell", NORMALPRIO + 1,
shellThread, (void *)&shell_cfg1);
chThdWait(shelltp); /* Waiting termination. */
chThdSleepMilliseconds(1000);
}
}
this is print data
Code: Select all
//when adc depth=8
samples size=4
1507 1507 1509 1508 0 0 0 0
0 0 0 7 0 0 0 0
0 0 5 8 0 0 0 0
//when adc depth=16
samples size=8
1508 1508 1509 1509 1509 1509 1509 1510 0 0 0 0 0 0 0 0
3 0 6 53 0 0 0 0 0 0 0 0 0 0 0 0
7 4 0 6 0 0 0 0 0 0 0 0 0 0 0 0
i read the doc again ,in the Driver State Machine, maybe the callback will be called twice:once is half and another is full?