cancel
Showing results for 
Search instead for 
Did you mean: 

stm32l-discovery mouse demo modification

quazarstar
Associate II
Posted on January 09, 2014 at 14:22

Hello Everybody,

i'm trying to modify joystick demo from STM32_USB-FS-Device_Lib_V3.4.0

I want to control movement by thumb joystick. However, when i initialize ADC (or usart) it won't jump to interrupt routine. At the same the, the debugger is not working.

I have found out that debugger and interrupts start wroking when i comment out:

  _SetCNTR(wInterrupt_Mask);   line 57 in usb_sil.c ( setting wInterrupt_Mask = IMR_MSK; )

  _SetCNTR(wInterrupt_Mask);  line 80 in usb_pwr.c ( setting   wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; )

Any idea what might be the problem ?

#stm32l-discovery-usb-mouse
15 REPLIES 15
quazarstar
Associate II
Posted on January 14, 2014 at 14:23

Ah, sorry.

Here it is:

/*-----------------------------------------------------------------------------*/

/**

* @brief  Initialize ADC interrupt

* @param  None

* @retval None

*/

void ADC_Initialize_IT(void)

{

  NVIC_InitTypeDef NVIC_InitStructure;

  // 2 bit for pre-emption priority, 2 bits for subpriority

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);   // setups NVIC->ISER

 

// setups ADC->CR

ADC_ITConfig(ADC1, ADC_IT_EOC,ENABLE);

ADC1->CR1 |= 0x00000020;

NVIC->ISER[0] |= 0x00040000; /* 18 position, first position is 0 */

}

chen
Associate II
Posted on January 14, 2014 at 15:59

Hi

Sorry but I am not that familiar with the ADC peripheral in this processor, I have worked

on lots of different processor over the years!

''    ADC_InitStruct.ADC_Resolution = ADC_Resolution_8b;

//    ADC_InitStruct.ADC_ScanConvMode = ENABLE;

    ADC_InitStruct.ADC_ScanConvMode = DISABLE;

    ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;

    ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

    //ADC_InitStruct.ADC_ExternalTrigConv;

    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right ;

//    ADC_InitStruct.ADC_NbrOfConversion = 2;

    ADC_InitStruct.ADC_NbrOfConversion = 1;''

This appears to be setting up the ADC to do conversions.

What does Scan mode do?

''  // 2 bit for pre-emption priority, 2 bits for subpriority

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);   // setups NVIC->ISER

 

// setups ADC->CR

ADC_ITConfig(ADC1, ADC_IT_EOC,ENABLE);

ADC1->CR1 |= 0x00000020;

NVIC->ISER[0] |= 0x00040000; /* 18 position, first position is 0 */''

This sets up the IRQ for ADC

BUT

I am not sure what generates the ADC IRQ.

Normally, the ADC can generate an IRQ when it has finished doing a conversion.

(Check the data sheet to see what the ADC is capable of)

Is this what you want to do?

The ADC peripheral will probably have some register to enable IRQ generation on converssion complete.

Since, the ADC is not in continuous mode - you will have to start it again (in the ISR).

(I think if you do this - you may flood the USB HID)

OR do you want to log the ADC value after a fixed period of time?

quazarstar
Associate II
Posted on January 15, 2014 at 12:46

Hello,

You can set it in a register, irq is generated after each conversion or after a group of conversion. I set it to generate irq after each conversion.

I have this lines of code in Initialization routine:

/* Enables or disables the EOC on each regular channel conversion. */

    ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE);

Offtopic optional question: If you have for example 3 channels in group conversion, how do you get data out of it if irq is generated at the end of group conversion ? DMA ?

Yes i have start conversion in main while() loop , there is also a for() loop to slow it down a bit ..

Yes, ADC has EOCIE set

chen
Associate II
Posted on January 15, 2014 at 13:04

Hi

''Yes i have start conversion in main while() loop , there is also a for() loop to slow it down a bit .. ''

Hmm - how is this going to synchronise with the ADC end of convert IRQ?

Have another look at the HID example with the MEMS. How does the example get updates from the MEMS. How does the example decide when to send USB HID update.

(Hint - you may be better off setting up ADC in continuous mode. Use a timer to read ADC value at timed intervals)

''Offtopic optional question: If you have for example 3 channels in group conversion, how do you get data out of it if irq is generated at the end of group conversion ? DMA ?''

Do not know without reading the ADC section in data sheet.

Posted on January 15, 2014 at 14:32

(Hint - you may be better off setting up ADC in continuous mode. Use a timer to read ADC value at timed intervals)

Or more effectively not use continuous mode, and trigger the ADC with a timer periodically, and catching the EOC interrupt when it's actually done the conversion.

Offtopic optional question: If you have for example 3 channels in group conversion, how do you get data out of it if irq is generated at the end of group conversion ? DMA ?

You use DMA to get the results quickly enough, when the DMA is complete you use the DMA TC interrupt as a ''Block EOC'' signal.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
quazarstar
Associate II
Posted on January 19, 2014 at 09:26

Hello guys,

thanks for your replies.

I think i figured out why my adc was not working.

As i wrote, by setting interrupts, adc stops working. I also figured out that HSI also stops working. And i think it's because of suspend home which usb is entering all the time . So by putting routine into main loop which set's up HSI, my ADC interrupts started working .. LEDs blinking ... and i also have HSI 16MHz on MCO :)