Page 1 of 2
STM32 ADC and exttrig event problem
Posted: Mon Sep 24, 2012 4:21 pm
by alexblack
CPU: STM32F103, OS version 2.4.2, compiler: IAR
I wanted to use TIM3 timer to get values from ADC on a fixed time rate (100 ms). To do this I setup TIM3 peripheral generate TRGO event:
Code: Select all
TIM3->PSC = (STM32_TIMCLK1/1000)-1; /* prescalser to get 1 ms on each timer tick */
TIM3->ARR = 100; /* 100 ms period */
TIM3->CR2 = TIM_CR2_MMS_1; /* TRGO event is timer update event, e.g. overflow */
TIM3->CR1 = TIM_CR1_CEN; /* enable the timer */
ADC config is :
Code: Select all
static const ADCConversionGroup adcgrpcfg =
{
TRUE,
ADC_NUM_CHANNELS,
adccallback,
adcerrorcallback,
/* CR1 */
ADC_CR1_DISCNUM_2 | ADC_CR1_DISCNUM_1, /* 7 channels*/
/* CR2 */
ADC_CR2_EXTTRIG /* use ext event */
| ADC_CR2_EXTSEL_2, /* select Timer3 TRGO event */
/* SMPR1 */
ADC_SMPR1_SMP_AN15(ADC_SAMPLE_239P5) |
ADC_SMPR1_SMP_AN14(ADC_SAMPLE_239P5) |
ADC_SMPR1_SMP_AN13(ADC_SAMPLE_239P5) |
ADC_SMPR1_SMP_AN12(ADC_SAMPLE_239P5) |
ADC_SMPR1_SMP_AN11(ADC_SAMPLE_239P5) |
ADC_SMPR1_SMP_AN10(ADC_SAMPLE_239P5),
/* SMPR2 */
ADC_SMPR2_SMP_AN3(ADC_SAMPLE_239P5) |
ADC_SMPR2_SMP_AN2(ADC_SAMPLE_239P5) |
ADC_SMPR2_SMP_AN1(ADC_SAMPLE_239P5),
/* SQR1 */
ADC_SQR1_NUM_CH(ADC_NUM_CHANNELS),
/* SQR2 */
ADC_SQR2_SQ7_N(ADC_KEY_CHNL),
/* SQR3 */
ADC_SQR3_SQ6_N(ADC_T_BAT_CHNL) |
ADC_SQR3_SQ5_N(ADC_U_BAT_CHNL) |
ADC_SQR3_SQ4_N(ADC_PGM4_CHNL) |
ADC_SQR3_SQ3_N(ADC_PGM3_CHNL) |
ADC_SQR3_SQ2_N(ADC_PGM2_CHNL) |
ADC_SQR3_SQ1_N(ADC_PGM1_CHNL)
};
ADC works, but with wrong time rate (not 100 ms, much faster), because in adc_lld.c driver file there was set bit 'ADC_CR2_CONT' on line 213 & 221, which must be cleared in this mode. After patching driver code to disable setup this bit, ADC works fine.
Re: STM32 ADC and exttrig event problem
Posted: Tue Sep 25, 2012 6:31 am
by Giovanni
Hi,
The problem is that you specified a "continuous" conversion in you conversion group, that "TRUE", in the first field, the ADC driver is just doing what you specified. Change that field to false if you need a one-shot conversion.
If you need to make conversions at intervals you don't even need to touch a timer, there are several other ways:
1) A periodic thread.
2) A virtual timer callback.
3) A GPT driver callback.
Also make sure to write your callback code correctly, I-class APIs and locks, if not sure post your code.
Giovanni
Re: STM32 ADC and exttrig event problem
Posted: Tue Sep 25, 2012 5:54 pm
by alexblack
Giovanni wrote:The problem is that you specified a "continuous" conversion in you conversion group, that "TRUE", in the first field, the ADC driver is just doing what you specified.
Yes, I wanted
continuous mode with specified interval between conversions - 100 ms and wanted to use hardware events without addition software overheads.
Giovanni wrote: Change that field to false if you need a one-shot conversion.
If I change this field to FALSE, the conversion is executed one time and then the driver isr routine disable the ADC module. In that case I must start conversion anew. It is not "external triggered ADC conversion by timer event" what I want.
Giovanni wrote:1) A periodic thread.
2) A virtual timer callback.
3) A GPT driver callback.
Yes it's true but I want to use hardware events ...
I ran into another problem which allowed to find out wrong work of ADC driver which consists in that DMA does not have time to process SPI DATA, when another module use DMA for example ADC working in "continuous" mode. SPI driver blocked through indefinite time. After a few experiments I saw that WRITE DMA channel ended write data to SPI peripheral while READ DMA channel steel wait some data from it - it is nonsense because number of writes to SPI must be equal to number of reads from it in fact. I decided it is hardware problem with speed of DMA transfers on the CPU STM32 because I solved problem by increasing speed of AHB bus from 36Mhz to 72Mhz in which CPU is run (appeared that STM32_HPRE value was STM32_HPRE_DIV2) and slow down ADC conversions but not sure on 100% that will help me (but already device works 4 days without locks). I tried to find such problems in Internet but nothing found. If your can help with this I can get additional information.
Re: STM32 ADC and exttrig event problem
Posted: Tue Sep 25, 2012 6:15 pm
by Giovanni
Of course you cannot ignore bandwidth issues when using DMAs, you could also try to change priorities however I never had problems with DMAs, if you look into ./testhal/STM32F4xx/DMA_STORM, there is a DMA stress test application using 9 channels together.
About the ADC, if you create a small application to test I could give it a try.
Giovanni
Re: STM32 ADC and exttrig event problem
Posted: Wed Sep 26, 2012 6:46 am
by alexblack
I tried only two test treads with SPI1 and SPI2 working on max frequencies (SPI1 on 36MHz , SPI2 on 18MHz, CPU and AHB run at 72MHz) and there is a problem on STM32F103 device! It locks in watchdog 'two' callback in few seconds! If change settings of SPIs to work with DIV4 CLK option (SPI1 on 18MHz, SPI2 on 9MHz) than test passed! I tried to change priorities of the used DMA channels - nothing can help. May be I have wrong hardware settings? (I start work with STM32 devices recently and never used any OS before).
About ADC. I think ADC low level driver must test 'ADC_CR2_EXTTRIG' or 'ADC_CR2_JEXTTRIG' bits of 'ADCConversionGroup.cr2' field and do not set the 'ADC_CR2_CONT' bit if any of them is set to work with external triggered continuous conversions.
Re: STM32 ADC and exttrig event problem
Posted: Wed Sep 26, 2012 9:12 am
by Giovanni
It is possible we are hitting some HW problem, I will port that DMA stress test application to the F1 over the weekend. This will give us the opportunity to asses the optimal settings.
We should also do some bandwidth calculations. Considering that the RAM runs at 72MHz with no wait states on a 32bits bus we should have 288 MBytes/sec of peak bandwidth using aligned words accesses.
Considering that the SPI DMA works one byte at time the bandwidth available to SPIs should be 72MBytes/sec (shared with CPU). There should be enough for two SPIs.
Did you port just parts of it or the whole DMA test application?
Giovanni
Re: STM32 ADC and exttrig event problem
Posted: Wed Sep 26, 2012 3:17 pm
by alexblack
I do not understood how you got 72Mbytes/sec bandwidth of SPI peripheral. If it works on 36MHz it send one bit at one clock, not byte isn't it? Ie bandwidth is 36/8 = 4,5Mbytes/sec mult by 2 for transmitter + receiver working simultaneously, ie 9MBytes/sec maximum. Yes, I agree that it is quite small compared to the bandwidth of the processor, but do not understand why it does not have time to do it. Maybe there are any restrictions or additional delays, which I do not know. If this proves true, then I will be very angry, as was hoping to get a much greater bandwidth.
Did you port just parts of it or the whole DMA test application?
Before I wrote same test routines and than tried part of the DMA stress test application:
Code: Select all
// SPI configuration (Bus CLK/2, CPHA=0, CPOL=0, MSb first).
static const SPIConfig hs_spicfg = {
NULL,
MMC_CS_PORT,
MMC_CS_BIT,
SPI_CR1_MSTR
};
static void tmo(void *p) {
(void)p;
chSysHalt();
}
/*
* SPI thread.
*/
static WORKING_AREA(waSPI1, 1024);
static WORKING_AREA(waSPI2, 1024);
static msg_t spi_thread(void *p) {
unsigned i;
SPIDriver *spip = (SPIDriver *)p;
VirtualTimer vt;
uint8_t txbuf[256];
uint8_t rxbuf[256];
/* Prepare transmit pattern.*/
for (i = 0; i < sizeof(txbuf); i++)
txbuf[i] = (uint8_t)i;
/* Continuous transmission.*/
while (TRUE) {
/* Starts a VT working as watchdog to catch a malfunction in the SPI
driver.*/
chSysLock();
chVTSetI(&vt, MS2ST(100), tmo, NULL);
chSysUnlock();
spiExchange(spip, sizeof(txbuf), txbuf, rxbuf);
/* Stops the watchdog.*/
chSysLock();
if (chVTIsArmedI(&vt))
chVTResetI(&vt);
chSysUnlock();
}
}
__task void 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();
// Blinking leds working on GPT callback
static const GPTConfig GPT1conf = {2000, gpt_callback};
gptStart(&GPTD1, &GPT1conf);
gptStartContinuous(&GPTD1, 250);
/* Activating SPI drivers.*/
spiStart(&SPID1, &hs_spicfg);
spiStart(&SPID2, &hs_spicfg);
/* Starting SPI threads instances.*/
chThdCreateStatic(waSPI1, sizeof(waSPI1), NORMALPRIO + 1, spi_thread, &SPID1);
chThdCreateStatic(waSPI2, sizeof(waSPI2), NORMALPRIO + 1, spi_thread, &SPID2);
for (;;)
{
chThdSleepMilliseconds(MS2ST(1000));
}
}
Re: STM32 ADC and exttrig event problem
Posted: Wed Sep 26, 2012 4:38 pm
by Giovanni
That would be the RAM bandwidth not the SPI bandwidth.
I will try this myself over the weekend, the whole thing is very suspect. It is not a bandwidth issue of course, there is an order of magnitude difference between the required bandwidth and available bandwidth.
Giovanni
Re: STM32 ADC and exttrig event problem
Posted: Wed Sep 26, 2012 5:56 pm
by alexblack
I found description of DMA bandwidth on STM:
http://www.st.com/internet/com/TECHNICA ... 160362.pdfBut it is complex information and I not able to quickly understand how it can help me.
Re: STM32 ADC and exttrig event problem
Posted: Wed Sep 26, 2012 7:00 pm
by Giovanni
I think that the relevant part is just section 3.4 at page 8, it could explain your findings.
Giovanni