2020-04-13 11:26 PM
Hello all,
I am trying to configure an ADC in injected mode via a trigger from TIMER 1. I want to trigger ADC whenever the timer RESETS. For this, I have set the TIM1's master mode to generate ADC trigger signal during timer RESET. I have configured ADC in injected mode to trigger for the same and have also set the ADC interrupt in trigger mode.
However, I found that it doesnot trigger for whatever reason. I have attached my ADC initialization code for you guys to take a look. Could you tell me where I'm going wrong?
#define ADCbase_ADDR (uint32_t)0x40012000
#define ADC1 0x00
#define ADC2 0x100
#define ADC3 0x200
#define ADC1base_ADDR (ADCbase_ADDR + ADC1)
#define ADC2base_ADDR (ADCbase_ADDR + ADC2)
#define ADC3base_ADDR (ADCbase_ADDR + ADC3)
//Register Offsets
#define ADC_SR 0x00
#define ADC_CR1 0x04
#define ADC_CR2 0x08
#define ADC_SMPR1 0x0C
#define ADC_SMPR2 0x10
#define ADC_JOFR1 0x14
#define ADC_JOFR2 0x18
#define ADC_JOFR3 0x1C
#define ADC_JOFR4 0x20
#define ADC_HTR 0x24
#define ADC_LTR 0x28
#define ADC_SQR1 0x2C
#define ADC_SQR2 0x30
#define ADC_SQR3 0x34
#define ADC_JSQR 0x38
#define ADC_JDR1 0x3C
#define ADC_JDR2 0x40
#define ADC_JDR3 0x44
#define ADC_JDR4 0x48
#define ADC_DR 0x4C
//RCC Registers
#define RCC_Base_Address (uint32_t)0x40023800
#define RCC_APB2ENR 0x44
#define AHB1_ENR 0x30
#define ADC1Clock_Enable_BIT (uint32_t)(1<<8)
#define ADC_ENABLE_BIT 0x00000001
#define GPIOA_RCC_Enable_Bit 0x00000001
#define GPIOA_Base_ADDR 0x40020000
#define GPIOB_Base_ADDR 0x40020400
#define GPIOC_Base_ADDR 0x40020800
#define GPIOA_MODER 0x00
//GPIO Registers
#define AN0 (0x00000003)
#define Timer1_TRGO_bits 0x00010000 //bits 16 through 19
#define Trigger_Rising 0x00300000
//Timer Registers
#define TMR1_BASE_ADDR 0x40010000
#define TIMx_CR2 0x04
#define MMS_BITS_RESET_MODE 0x00000070
uint32_t *p; //pointer pointing to the address of registers
//configure
void ADC_Init(void)
{
//enable GPIO port A's peripheral clock on AHB1
p = (uint32_t)(RCC_Base_Address + AHB1_ENR);
*p = (*p | GPIOA_RCC_Enable_Bit);
//enable ADC1's peripheral clock on APB2
p = (uint32_t)(RCC_Base_Address + RCC_APB2ENR);
*p = (*p | ADC1Clock_Enable_BIT);
//configure PA0 as analog input
p = (uint32_t)(GPIOA_Base_ADDR + GPIOA_MODER);
*p = (*p | AN0);
//Enable interrupt for injected channels
p = (uint32_t)(ADC1base_ADDR + ADC_CR1);
*p = 0x00000000;
*p = *p |((uint32_t)(1<<7));
//point to CR2
p = (uint32_t)(ADC1base_ADDR + ADC_CR2);
*p = 0x00000000;
//External event select for injected group for rising edge
*p = *p |(Trigger_Rising);
//Select Timer 1 TRGO as the SOC trigger source
*p = *p |(Timer1_TRGO_bits);
//Implied Settings by CR2 Reset
//DMA disabled
//The EOC bit is set at the end of each sequence of regular conversions. Overrun detection is enabled only if DMA=1.
//Data Right alignment
//SIngle Conversion Mode
//ADC conversion disabled
//Timer1 Master Mode selection for configuring TRGO conditions
p = (uint32_t)(TMR1_BASE_ADDR + TIMx_CR2);
*p = *p & ~(MMS_BITS_RESET_MODE);
//switch to injected sequence register
p = (uint32_t)(ADC1base_ADDR + ADC_JSQR);
*p = 0x00000000;
//implied changes:
//one conversion
//connect first conversion in injected sequence as channel 1
*p = *p|(0x00000000); //selecting channel 0 as the analog input
//turn on ADC1
p = (uint32_t)(ADC1base_ADDR + ADC_CR2);
*p = *p |((uint32_t)(ADC_ENABLE_BIT));
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
//UNUSED(hadc);
HAL_GPIO_TogglePin(GPIOB, test_Pin);
//This is not getting triggered!!
}