#include "stm32f4xx.h" #include "stm32f4xx_rcc.c" #include "stm32f4xx_adc.c" #include "stm32f4xx_gpio.c" #include "stm32f4xx_usart.c" #include "stm32f4xx_tim.c" #include "dd.h" char Results[65536]; int ResultsPointer = 0; // initialize port E (switches), Gpio_Pin_3 to GPIO_Pin_6 as inputs void SWITCHinit (void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOE, &GPIO_InitStructure); } // initialize port D (LEDS), Gpio_Pin_12 to GPIO_Pin_15 as outputs void LEDinit (void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure); } //USART3 initialization, GPIOD 8:9, TX&RX, IRQ on RX&TX void USART3_init(int BaudRate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); USART_DeInit(USART3); USART_InitStructure.USART_BaudRate = BaudRate; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(USART3, &USART_InitStructure); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // enable IRQ on RX USART_Cmd(USART3, ENABLE); NVIC_EnableIRQ(USART3_IRQn); // Enable IRQ for USART3 in NVIC } // ADC init function void ADCinit_T5_CC1_IRQ (void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInit(&ADC_CommonInitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T5_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles); ADC_Init(ADC2, &ADC_InitStructure); ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 1, ADC_SampleTime_3Cycles); ADC_Cmd(ADC1, ENABLE); ADC_Cmd(ADC2, ENABLE); NVIC_EnableIRQ(ADC_IRQn); // Enable IRQ for ADC in NVIC ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); // Enable IRQ generation in ADC } // Timer 5 init function - time base void TIM5init_TimeBase_CC1 (int interval) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 840; // .1ms TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable; TIM_OCInitStructure.TIM_Pulse = 1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set; TIM_OC1Init(TIM5, &TIM_OCInitStructure); TIM_Cmd(TIM5, ENABLE); } // NVIC - interrupt setup function for ADC //void NVICinit_ADC_EOC(void) { // NVIC_EnableIRQ(ADC_IRQn); // Enable IRQ for ADC in NVIC // ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); // Enable IRQ generation in ADC //} void main(void) { LEDinit(); USART3_init(921600); ADCinit_T5_CC1_IRQ(); TIM5init_TimeBase_CC1(840); // 840 == 100us while (1) { // endless loop }; } // USART IRQ function // caution: placing breakpoints in IRQ corrupts the execution! // this happens due to the reading of the DR by the IAR and changes IRQ flags! // I wasted sevaral hours to find this out. Do not step into the same trap. void USART3_IRQHandler(void) { // TX IRQ part if (USART3->SR & USART_SR_TXE) { // If TXE flag in SR is on then if (ResultsPointer < 65536) // if not end of string USART3->DR = Results[ResultsPointer++]; // send next byte & pointer++ else { // else USART_ITConfig(USART3, USART_IT_TXE, DISABLE); LED_RD_OFF; // red OFF }; }; // RX IRQ part if (USART3->SR & USART_SR_RXNE) { // if RXNE flag in SR is on then int RXch = USART3->DR; // save received character & clear flag if (RXch == 'a') { LED_BL_ON; // blue ON ResultsPointer = 0; // init pointer to results TIM_Cmd(TIM5, ENABLE); }; if (RXch == 'b') { USART_ITConfig(USART3, USART_IT_TXE, ENABLE); ResultsPointer = 0; LED_RD_ON; // red ON USART3->DR = Results[ResultsPointer++]; // Send first character }; }; } // ADC IRQ function void ADC_IRQHandler(void) // PASS takes approx 300ns of CPU time! { int Ra = ADC1->DR; int Rb = ADC2->DR; if (ResultsPointer < 65535) { Results[ResultsPointer++] = (Rb & 0x0f00) >> 8; Results[ResultsPointer++] = (Rb & 0x00ff); } else { LED_BL_OFF; // blue OFF TIM_Cmd(TIM5, DISABLE); }; }