#include "stm32f10x.h" // STM32F10x Library Definitions #include "hardware.h" // defining hardware access #include "usb_lib.h" #include "usbutilities.h" #include "usb_istr.h" /* Prototypes*/ static void RCC_Configuration(void); static void IWDG_Configuration(void); static void GPIO_Configuration(void); static void LED_Configuration(void); static void USB_Configuration(void); static void USART_Configuration(void); //static void TIM2_Configuration(void); void USB_LP_CAN1_RX0_IRQHandler(void); void USART1_IRQHandler(void); void TIM2_IRQHandler(void); void Ptr_Init(void); /* Private Define */ #define Q_ASSERT(value) if (!(value)) {while(1);} #define Q_REQUIRE(value) Q_ASSERT(value) #define VIRTUAL_COM_PORT_DATA_SIZE 64 /* Byte */ #define RxBufferSize VIRTUAL_COM_PORT_DATA_SIZE #define POLLFIFOTIME 10000 /* µs */ /* Private variables */ USART_InitTypeDef USART_InitStructure; unsigned char FIFORxBuffer1[RxBufferSize]; /* 1st buffer for transmit data; one is filled, the other is transmitted. */ unsigned char FIFORxBuffer2[RxBufferSize]; /* 2st buffer for transmit data; one is filled, the other is transmitted. */ unsigned char *FifoBufferPtr; /* Pointer to buffer to be filled by firmware. */ unsigned char *USBBufferPtr; /* Pointer to buffer currently transmitted by USART. */ volatile unsigned char TransmitUSBFlag; /* Flag is set to 1 when transmission is started. */ volatile unsigned char TransmitTIMFlag; /* Flag is set to 1 when transmission is started, due to elapsed Timer */ unsigned short int FIFOBufferCount = 0; /* Number of valid bytes in FifoBufferPtr. */ /* void SysTick_Handler*/ /* This function handles SysTick Handler SysTick handles QF_tick as timer for QF*/ void SysTick_Handler(void) { } /* void Ptr_Init */ /* Initialize Pointer on Buffer */ void Ptr_Init(void) { /* Initialize module variables. */ FifoBufferPtr = FIFORxBuffer1; USBBufferPtr = FIFORxBuffer2; return; } /* void USART1_IRQHandler*/ /* This function handles USART1 global interrupt request */ void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { FifoBufferPtr[FIFOBufferCount++] = USART_ReceiveData(USART1); if (FIFOBufferCount >= VIRTUAL_COM_PORT_DATA_SIZE) { unsigned char *TempPtr; /* Temporary Pointer for swapping. */ /* buffer is full now. Switch buffers and transmit full one. */ TempPtr = USBBufferPtr; USBBufferPtr = FifoBufferPtr; FifoBufferPtr = TempPtr; FIFOBufferCount = 0; /* mark new FifoBufferPtr as empty. */ TransmitUSBFlag = 1; /* Set Flag for Buffer transmission via USB */ } } return; } /* static void RCC_Configuration*/ /* Configures the different system clocks.*/ static void RCC_Configuration(void) { unsigned long int StartUpCounter; // used as timeout for PLL startup to prevent unbounded loops. ErrorStatus HSEStartUpStatus; /* RCC system reset (for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* ADCCLK = PCLK2/2 */ RCC_ADCCLKConfig(RCC_PCLK2_Div2); /*!< PLLCLK = 8MHz/1 * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); } else { /*HSE setup failed.*/ Q_ASSERT(0); } /*Wait till PLL is ready*/ StartUpCounter = HSEStartUp_TimeOut; while((StartUpCounter > 0) && (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)) { StartUpCounter--; } Q_REQUIRE(StartUpCounter != 0); // make sure loop exits with 2nd condition. /*Select PLL as system clock source*/ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); StartUpCounter = HSEStartUp_TimeOut; /*Wait till PLL is used as system clock source*/ while((StartUpCounter > 0) && (RCC_GetSYSCLKSource() != 0x08)) { StartUpCounter--; } Q_REQUIRE(StartUpCounter != 0); // make sure loop exits with 2nd condition. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* GPIOA Periph clock enable i.e. for trigger pins, UART1 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); /* GPIOC Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable USART1 clock */ //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* DMA1 Periph clock enable */ return; } /*IWDG_Configuration*/ /* Set IWDG configuration for watchdog */ static void IWDG_Configuration(void) { /* Enable write access to IWDG_PR and IWDG_RLR registers*/ IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); /* IWDG counter clock: 40kHz(LSI) / 32 = 1.25 KHz*/ IWDG_SetPrescaler(IWDG_Prescaler_32); IWDG_SetReload(20); /* Set counter reload value to 20 = 16ms */ IWDG_ReloadCounter(); /* Reload IWDG counter */ IWDG_Enable(); /* Enable IWDG (the LSI oscillator will be enabled by hardware) */ } /* static void GPIO_Configuration*/ /* - Configure trigger port pins*/ /* - Set unused ports to floating input to reduce EMI/EMC */ static void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure USART1 Rx as input */ GPIO_InitStructure.GPIO_Pin = UART1_RxPin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); //Debug-Pin (14), for time measurement /*GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure);*/ } /* static void LED_Configuration*/ /* Configure the used I/O ports pin for LED*/ static void LED_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Init LED pins as open drain ports*/ GPIO_InitStructure.GPIO_Pin = LED_Pins; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(LED_Port, &GPIO_InitStructure); LED_GREEN(); } /* void USB_configuration*/ /* Initialize USB configuration. */ static void USB_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /*Set clock*/ RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); /* Select USBCLK source */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); /* Enable the USB clock */ /* Init USB-EN pins as push-pull ports*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); /*Set interrupt priority*/ NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USB_IRQChannelPreemptionPriority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = USB_IRQChannelSubPriority; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*Initialize USB and set up USB_enable Pin */ USB_Init(); GPIO_SetBits(GPIOC, GPIO_Pin_6); // enable USB return; } /* static void USART_Configuration*/ /*Configure the used parameters for USART*/ static void USART_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; USART_Cmd(USART1, DISABLE); /* disable the USART */ USART_DeInit(USART1); /* Enable DMA for DEBUGUART Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = USART1_IRQChannelPreemptionPriority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = USART1_IRQChannelSubPriority; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* USART1 configured as follow: - BaudRate = 4500000 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive enabled */ USART_InitStructure.USART_BaudRate = 4500000; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx; /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); /* Enable USART1 Receive interrupts */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* Enable USART1 */ USART_Cmd(USART1, ENABLE); } /*static void Initialize*/ /* Starts Initialize routine*/ static void Initialize(void) { DBGMCU_Config(DBGMCU_IWDG_STOP, ENABLE); DBGMCU_Config(DBGMCU_TIM4_STOP, ENABLE); /* brief Initialization of Hardware*/ USART_DeInit(USART1); RCC_Configuration(); USART_Configuration(); /* Set the Vector Table base location at 0x2000 for cooperation with boot loader */ //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000); NVIC_PriorityGroupConfig(NVIC_PriorityGroup); IWDG_Configuration(); GPIO_Configuration(); LED_Configuration(); USB_Configuration(); // TIM2_Configuration(); return; } /*void USB_LP_CAN1_RX0_IRQHandler*/ /* function handles USB Low Priority */ void USB_LP_CAN1_RX0_IRQHandler(void) { USB_Istr(); return; } /*main*/ /* int main()*/ int main(void) { __disable_irq(); Initialize(); Ptr_Init(); /* SysTick end of count event each 1ms.*/ /* SystemFrequency is defined in "system_stm32f10x.h" and equal to HCLK frequency.*/ /* Enable SysTick interrupt.*/ SysTick_Config(SystemFrequency / 1000); NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup, SYSTICK_IRQChannelPreemptionPriority, SYSTICK_IRQChannelSubPriority)); /* enable interrupts*/ __enable_irq(); for (;;) { if(TransmitUSBFlag == 1) { USBTransmitBuffer(&USBBufferPtr[0], VIRTUAL_COM_PORT_DATA_SIZE); TransmitUSBFlag = 0; } TRIG_WATCHDOG(); } }