/* rts.c hardware initialization for Real-Time Systems lab */ #include "md407.h" /* #include "stm32f4xx.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_dac.h" #include "stm32f4xx_can.h" #include "stm32f4xx_rcc.h" #include "stm32f4xx_syscfg.h" #include "stm32f4xx_exti.h" */ static void __can_init() { /* Connect CAN1 pins to AF */ /* PB9 - CAN1 TX */ /* PB8 - CAN1 RX */ // GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1); // GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1); /* Connect CAN1 pins to AF */ GPIOB->afrh |= 0x0099; GPIOB->moder |= 0x000C0000; /* Pins 8 (RX) and 9 (TX) are AF */ // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Init(GPIOB, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_InitStructure.GPIO_OType = GPIO_Mode_IN; // should really be GPIO_OType_PP? // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Init(GPIOB, &GPIO_InitStructure); GPIOB->pupdr |= 0x00010000; /* Pin 8 pull up */ /* Pin 9 push/pull default after reset */ GPIOB->ospeedr |= 0x00080000; /* Pins 9 (TX) 50 MHz */ /* Connect CAN2 pins to AF */ /* PB6 - CAN2 TX */ /* PB5 - CAN2 RX */ // GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_CAN2); // GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_CAN2); GPIOB->afrl |= 0x09900000; GPIOB->moder |= 0x00002800; /* Pins 5(RX) and 6 (TX) are AF */ // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Init(GPIOB, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_InitStructure.GPIO_OType = GPIO_Mode_IN; // should really be GPIO_OType_PP? // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Init(GPIOB, &GPIO_InitStructure); GPIOB->pupdr |= 0x0000400; /* Pin 5 pull up */ /* Pin 6 push/pull default after reset */ GPIOB->ospeedr |= 0x0002000; /* Pins 6 (TX) 50 MHz */ /* CAN filter init */ // CAN_FilterInitStructure.CAN_FilterNumber = 0; // CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; // CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; // CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; // CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; // 0 in a position means ignore that bit // CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; // CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; // CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; // CAN_FilterInit(&CAN_FilterInitStructure); #define filter_number_bit_pos 1 #define FMR_FINIT ((uint32_t)0x00000001) /* Filter init mode */ CAN1->FMR |= FMR_FINIT; /* Initialisation mode for the filter */ CAN1->FA1R &= ~filter_number_bit_pos; /* Filter Deactivation */ CAN1->FS1R |= filter_number_bit_pos; /* 32-bit scale for the filter */ CAN1->sFilterRegister[0].FR1 = 0; /* 32-bit identifier or First 32-bit identifier */ CAN1->sFilterRegister[0].FR2 = 0; /* 32-bit mask or Second 32-bit identifier */ CAN1->FM1R &= ~(uint32_t)filter_number_bit_pos; /*Id/Mask mode for the filter*/ CAN1->FFA1R &= ~(uint32_t)filter_number_bit_pos; /* FIFO 0 assignation for the filter */ CAN1->FA1R |= filter_number_bit_pos; /* Filter activation */ CAN1->FMR &= ~FMR_FINIT; /* Leave the initialisation mode for the filter */ } static void __dac_init() { /* DAC channel 2 (DAC_OUT2 = PA.5) configuration */ // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // GPIO_Init(GPIOA, &GPIO_InitStructure); /* GPIO A, DAC */ if( (RCC->ahb1enr & (1<<0)) == 0 ) { /* if no previous init, start clocks and reset */ RCC->ahb1enr |= (1<<0); RCC->ahb1rstr |= (1<<0); RCC->ahb1rstr &= ~(1<<0); } GPIOA->moder |= 0x00000C000; /* Pin 5 is analog */ // DAC_StructInit( &DAC_InitStructure ); // DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; // DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; // DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; // DAC_Init(DAC_Channel_2, &DAC_InitStructure); #define DAC_Channel_1 ((uint32_t)0x00000000) #define DAC_Channel_2 ((uint32_t)0x00000010) #define CR_CLEAR_MASK ((uint32_t)0x00000FFE) #define DAC_Trigger_None ((uint32_t)0x00000000) /*!< Conversion is automatic once the DAC1_DHRxxxx register #define DAC_WaveGeneration_None ((uint32_t)0x00000000) #define DAC_LFSRUnmask_Bit0 ((uint32_t)0x00000000) /*!< Unmask DAC channel LFSR bit0 for noise wave generation */ #define DAC_OutputBuffer_Enable ((uint32_t)0x00000000) /* uint32_t tmpreg; tmpreg = DAC->cr; tmpreg &= ~(CR_CLEAR_MASK << DAC_Channel_2); DAC->CR = tmpreg; * */ DAC->cr &= ~(CR_CLEAR_MASK<<16); /*---------------------------- DAC CR Configuration -------------------------- // Get the DAC CR value tmpreg1 = DAC->CR; // Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits tmpreg1 &= ~(CR_CLEAR_MASK << DAC_Channel); // Configure for the selected DAC channel: buffer output, trigger, // wave generation, mask/amplitude for wave generation // Set TSELx and TENx bits according to DAC_Trigger value // Set WAVEx bits according to DAC_WaveGeneration value // Set MAMPx bits according to DAC_LFSRUnmask_TriangleAmplitude value // Set BOFFx bit according to DAC_OutputBuffer value tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration | DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | \ DAC_InitStruct->DAC_OutputBuffer); // Calculate CR register value depending on DAC_Channel tmpreg1 |= tmpreg2 << DAC_Channel; // Write to DAC CR DAC->CR = tmpreg1; */ #define DAC_CR_EN1 ((uint32_t)0x00000001) /*!cr |= (DAC_CR_EN1 << DAC_Channel_2); // DAC_SetChannel2Data(DAC_Align_8b_R, 0); DAC->dhr8r2 = 0; } void rtsInit( void ) { /* Todo, sense, is this session through GDB or debugger ? */ /* GPIO B, pushbutton and LEDS */ RCC->ahb1enr |= (1<<1); RCC->ahb1rstr |= (1<<1); RCC->ahb1rstr &= ~(1<<1); RCC->apb2enr |= (1<<14); /* SYSCONFIG, Already in main do it again if we are debugging through USBDM */ SYSCFG->exticr2 &= 0x0FFF; SYSCFG->exticr2 |= 0x1000; EXTI->imr |= (1<<7); EXTI->emr &= ~(1<<7); EXTI->ftsr &= ~(1<<7); EXTI->rtsr |= (1<<7); GPIOB->moder |= 0x00000005; /* Pins 0 and 1 are outputs */ GPIOB->ospeedr |= 0x00000005; /* Pins 0 and 1, 25 MHz */ GPIOB->bsrl |= 0x0003; /* Pins 0 and 1, set bit to turn LED off */ GPIOB->moder &= 0xFFFF3FFF; /* Pin 7 is input */ /* Enable clocks DAC, CAN1, CAN2, TIM5 */ // RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); // RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN1, ENABLE ); // RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN2, ENABLE ); // RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM5, ENABLE); RCC->apb1enr |= ( (1<<29) | (1<<26) | (1<<25) | (1<<3)); // DAC_DeInit(); // CAN_DeInit( CAN1 ); // CAN_DeInit( CAN2 ); RCC->apb1rstr |= ( (1<<29) | (1<<26) | (1<<25) | (1<<3) ); /* Reset DAC, CAN1,CAN2 and TIM5 */ RCC->apb1rstr &= ~( (1<<29) | (1<<26) | (1<<25) | (1<<3) ); /* Take DAC, CAN1,CAN2 and TIM5 out of Reset */ __can_init(); __dac_init(); }