//Initialization. Will set all motors ratio to 0% void motorsInit() { if (isInit) return; //Init structures GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; //Enable gpio and the timer RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO | MOTOR1_GPIO_PERIF | MOTOR2_GPIO_PERIF | MOTOR3_GPIO_PERIF | MOTOR4_GPIO_PERIF, ENABLE); RCC_APB1PeriphClockCmd( MOTOR1_TIM_PERIF | MOTOR2_TIM_PERIF | MOTOR3_TIM_PERIF | MOTOR4_TIM_PERIF, ENABLE); GPIO_AFIODeInit(); //Remap MOTO3 TIM GPIO_PinRemapConfig(MOTOR3_TIM_REMAP, ENABLE); //Remap MOTO4 TIM GPIO_PinRemapConfig(MOTOR4_TIM_REMAP, ENABLE); // Configure the GPIO for the MOTO1 output GPIO_InitStructure.GPIO_Pin = MOTOR1_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(MOTOR1_GPIO_PORT, &GPIO_InitStructure); // Configure the GPIO for the MOTO2 output GPIO_InitStructure.GPIO_Pin = MOTOR2_GPIO_PIN; GPIO_Init(MOTOR2_GPIO_PORT, &GPIO_InitStructure); // Configure the GPIO for the MOTO3 output GPIO_InitStructure.GPIO_Pin = MOTOR3_GPIO_PIN; GPIO_Init(MOTOR3_GPIO_PORT, &GPIO_InitStructure); // Configure the GPIO for the MOTO2 output GPIO_InitStructure.GPIO_Pin = MOTOR4_GPIO_PIN; GPIO_Init(MOTOR4_GPIO_PORT, &GPIO_InitStructure); //Timer configuration TIM_TimeBaseStructure.TIM_Period = MOTORS_PWM_PERIOD; TIM_TimeBaseStructure.TIM_Prescaler = MOTORS_PWM_PRESCALE; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(MOTOR1_TIM, &TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = MOTORS_PWM_PERIOD; TIM_TimeBaseStructure.TIM_Prescaler = MOTORS_PWM_PRESCALE; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(MOTOR2_TIM, &TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = MOTORS_PWM_PERIOD; TIM_TimeBaseStructure.TIM_Prescaler = MOTORS_PWM_PRESCALE; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(MOTOR3_TIM, &TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = MOTORS_PWM_PERIOD; TIM_TimeBaseStructure.TIM_Prescaler = MOTORS_PWM_PRESCALE; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(MOTOR4_TIM, &TIM_TimeBaseStructure); //PWM channels configuration (All identical!) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = MOTORS_POLARITY; TIM_OC4Init(MOTOR1_TIM, &TIM_OCInitStructure); TIM_OC4PreloadConfig(MOTOR1_TIM, TIM_OCPreload_Enable); TIM_OC4Init(MOTOR2_TIM, &TIM_OCInitStructure); TIM_OC4PreloadConfig(MOTOR2_TIM, TIM_OCPreload_Enable); TIM_OC4Init(MOTOR3_TIM, &TIM_OCInitStructure); TIM_OC4PreloadConfig(MOTOR3_TIM, TIM_OCPreload_Enable); TIM_OC3Init(MOTOR4_TIM, &TIM_OCInitStructure); TIM_OC3PreloadConfig(MOTOR4_TIM, TIM_OCPreload_Enable); //Enable the timer TIM_Cmd(MOTOR1_TIM, ENABLE); TIM_Cmd(MOTOR2_TIM, ENABLE); TIM_Cmd(MOTOR3_TIM, ENABLE); TIM_Cmd(MOTOR4_TIM, ENABLE); //Enable the timer PWM outputs //TIM_CtrlPWMOutputs(MOTOR1_TIM, ENABLE); //TIM_CtrlPWMOutputs(MOTOR2_TIM, ENABLE); //TIM_CtrlPWMOutputs(MOTOR3_TIM, ENABLE); //TIM_CtrlPWMOutputs(MOTOR4_TIM, ENABLE); TIM_ARRPreloadConfig(MOTOR1_TIM, ENABLE); TIM_ARRPreloadConfig(MOTOR2_TIM, ENABLE); TIM_ARRPreloadConfig(MOTOR3_TIM, ENABLE); TIM_ARRPreloadConfig(MOTOR4_TIM, ENABLE); // Halt timer during debug halt. DBGMCU_Config(MOTOR1_TIM_DBG, ENABLE); DBGMCU_Config(MOTOR2_TIM_DBG, ENABLE); DBGMCU_Config(MOTOR3_TIM_DBG, ENABLE); DBGMCU_Config(MOTOR4_TIM_DBG, ENABLE); isInit = RT_TRUE; }
void systemInit(void) { GPIO_InitTypeDef GPIO_InitStructure; //uint32_t i; //GPIO_InitTypeDef GPIO_InitStructure; /*RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = LED0_PIN | LED1_PIN | LED2_PIN| LED3_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOE, &GPIO_InitStructure);*/ /* Configure the GPIO_LED pins */ RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = LED0_PIN | LED1_PIN | LED2_PIN| LED3_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); #ifdef BUZZER { RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = BEEP_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOD, &GPIO_InitStructure); }; #endif // uint8_t gpio_count = sizeof(gpio_cfg) / sizeof(gpio_cfg[0]); // This is needed because some shit inside Keil startup f***s with SystemCoreClock, setting it back to 72MHz even on HSI. //SystemCoreClockUpdate(); SystemInit(); // Turn on clocks for stuff we use RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_TIM5 | RCC_APB1Periph_I2C2 | RCC_APB1Periph_SPI2 | RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3 , ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1 | RCC_APB2Periph_TIM8 | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1 | RCC_APB2Periph_SPI1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_DMA2, ENABLE); RCC_ClearFlag(); /*/ Make all GPIO in by default to save power and reduce noise GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOC, &GPIO_InitStructure); // Turn off JTAG port 'cause we're using the GPIO for leds GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // Configure gpio for (i = 0; i < gpio_count; i++) { GPIO_InitStructure.GPIO_Pin = gpio_cfg[i].pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = gpio_cfg[i].mode; GPIO_Init(gpio_cfg[i].gpio, &GPIO_InitStructure); }*/ LED0_ON; LED1_ON; //LED2_OFF; //LED3_OFF; BEEP_OFF; // Init cycle counter cycleCounterInit(); // SysTick SysTick_Config(SystemCoreClock / 1000); // Configure the rest of the stuff #ifndef FY90Q i2cInit(I2C2); #endif //spiInit(); // sleep for 100ms delay(100); }
/** * @brief Initializes peripherals: I2Cx, GPIO, DMA channels . * @param None * @retval None */ void I2C_LowLevel_Init(I2C_TypeDef* I2Cx) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; /* GPIOB clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* Enable the DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); if (I2Cx == I2C1) { /* I2C1 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* I2C1 SDA and SCL configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Enable I2C1 reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); /* Release I2C1 from reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); } else /* I2Cx = I2C2 */ { /* I2C2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); /* I2C1 SDA and SCL configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Enable I2C2 reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); /* Release I2C2 from reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); } /* I2C1 and I2C2 configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = OwnAddress1; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = ClockSpeed; I2C_Init(I2C1, &I2C_InitStructure); I2C_InitStructure.I2C_OwnAddress1 = OwnAddress2; I2C_Init(I2C2, &I2C_InitStructure); if (I2Cx == I2C1) { /* I2C1 TX DMA Channel configuration */ DMA_DeInit(I2C1_DMA_CHANNEL_TX); I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address; I2CDMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)0; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; I2CDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; I2CDMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte; I2CDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; I2CDMA_InitStructure.DMA_Mode = DMA_Mode_Normal; I2CDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; I2CDMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(I2C1_DMA_CHANNEL_TX, &I2CDMA_InitStructure); /* I2C1 RX DMA Channel configuration */ DMA_DeInit(I2C1_DMA_CHANNEL_RX); DMA_Init(I2C1_DMA_CHANNEL_RX, &I2CDMA_InitStructure); } else /* I2Cx = I2C2 */ { /* I2C2 TX DMA Channel configuration */ DMA_DeInit(I2C2_DMA_CHANNEL_TX); I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C2_DR_Address; I2CDMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)0; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; I2CDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; I2CDMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte; I2CDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; I2CDMA_InitStructure.DMA_Mode = DMA_Mode_Normal; I2CDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; I2CDMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(I2C2_DMA_CHANNEL_TX, &I2CDMA_InitStructure); /* I2C2 RX DMA Channel configuration */ DMA_DeInit(I2C2_DMA_CHANNEL_RX); DMA_Init(I2C2_DMA_CHANNEL_RX, &I2CDMA_InitStructure); } }
/* This funcion initializes the USART1 peripheral * * Arguments: baudrate --> the baudrate at which the USART is * supposed to operate */ void init_USART1(uint32_t baudrate){ /* This is a concept that has to do with the libraries provided by ST * to make development easier the have made up something similar to * classes, called TypeDefs, which actually just define the common * parameters that every peripheral needs to work correctly * * They make our life easier because we don't have to mess around with * the low level stuff of setting bits in the correct registers */ GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller) /* enable APB2 peripheral clock for USART1 * note that only USART1 and USART6 are connected to APB2 * the other USARTs are connected to APB1 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* enable the peripheral clock for the pins used by * USART1, PB6 for TX and PB7 for RX */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* This sequence sets up the TX and RX pins * so they work correctly with the USART1 peripheral */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are used GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this defines the IO speed and has nothing to do with the baudrate! GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this defines the output type as push pull mode (as opposed to open drain) GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // this activates the pullup resistors on the IO pins GPIO_Init(GPIOB, &GPIO_InitStruct); // now all the values are passed to the GPIO_Init() function which sets the GPIO registers /* The RX and TX pins are now connected to their AF * so that the USART1 can take over control of the * pins */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); // GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); /* Now the USART_InitStruct is used to define the * properties of USART1 */ USART_InitStruct.USART_BaudRate = baudrate; // the baudrate is set to the value we passed into this init function USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard) USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard) USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard) USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard) USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver USART_Init(USART1, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting /* Here the USART1 receive interrupt is enabled * and the interrupt controller is configured * to jump to the USART1_IRQHandler() function * if the USART1 receive interrupt occurs */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // we want to configure the USART1 interrupts NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART1 interrupts are globally enabled NVIC_Init(&NVIC_InitStructure); // the properties are passed to the NVIC_Init function which takes care of the low level stuff // finally this enables the complete USART1 peripheral USART_Cmd(USART1, ENABLE); }
static void SPI_RCC_Configuration(void) { RCC_APB1PeriphClockCmd(SD_SPI_CLK, ENABLE); RCC_APB2PeriphClockCmd(SD_SPI_GPIO_CLK, ENABLE); }
int main() { RCC_ClocksTypeDef RCC_Clocks; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); int rf_len = 0; int rs485_len = 0; #if DEBUG // int usart_len = 0; // char buff_usart[BUFFER_SIZE]; #endif char buff_rf[BUFFER_SIZE]; char buff_rs485[BUFFER_SIZE]; unsigned int sensors_time_poll = 0; // int temp_time_poll = 0; // int sms_test_time = 0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET); // off Delay_Init(); Enrf24_init(CE_PIN, CSN_PIN, IRQ_PIN); Enrf24_begin(1000000, 0); // Defaults 1Mbps, channel 0, max TX power Enrf24_setTXaddress((void*)enrf24_addr); Enrf24_setRXaddress((void*)enrf24_addr); Enrf24_enableRX(); // Start listening #if DEBUG USART1_Init(115200); #endif DS1307_Init(); Sensors_Init(); RS485_Init(115200); sim_hal_init(115200); OutputInit(); if (ThesisInit() == THESIS_FLASH_ERROR) { #if DEBUG USART1_SendStr("\nFlash write error.\n"); #endif TurnBuzzerOn(); Delay(1000); } #if DEBUG RCC_GetClocksFreq(&RCC_Clocks); USART1_SendStr("System Clock: "); USART1_SendNum(RCC_Clocks.SYSCLK_Frequency); USART1_SendStr("\r\n"); USART1_SendStr("Device ID: "); USART1_SendByte(__flash_data.id, HEX); USART1_SendStr("\r\n"); USART1_SendStr("Device Unique Number: "); USART1_SendByte(__flash_data.unique_number[0], HEX); USART1_SendByte(__flash_data.unique_number[1], HEX); USART1_SendByte(__flash_data.unique_number[2], HEX); USART1_SendByte(__flash_data.unique_number[3], HEX); USART1_SendStr("\r\n"); #endif OneWire_Init(); for EVER { if (millis() - sensors_time_poll > 100) { led_toggle(); Sensors_Poll(); sensors_time_poll = millis(); // buzzer_toggle(); // output_toggle(); if (millis() > 10000) { if (sensors.Gas >= GAS_LIMIT) { #if DEBUG USART1_SendStr("Gas detected.\r\n"); USART1_SendStr("Current Gas: "); USART1_SendFloat(sensors.Gas); USART1_SendStr("Limited Gas: "); USART1_SendFloat(GAS_LIMIT); USART1_SendStr("\r\n"); #endif TurnBuzzerOn(); TurnSpeakerOn(); TurnRelayOn(); } else if (sensors.Lighting >= LIGHT_LIMIT) { #if DEBUG USART1_SendStr("Light detected.\r\n"); USART1_SendStr("Current Light: "); USART1_SendFloat(sensors.Lighting); USART1_SendStr("Limited Light: "); USART1_SendFloat(LIGHT_LIMIT); USART1_SendStr("\r\n"); #endif TurnBuzzerOn(); TurnSpeakerOn(); TurnRelayOn(); } else if (sensors.TempC >= TEMPC_LIMIT) { #if DEBUG USART1_SendStr("Tempc detected.\r\n"); USART1_SendStr("Current Tempc: "); USART1_SendFloat(sensors.TempC); USART1_SendStr("Limited Tempc: "); USART1_SendFloat(TEMPC_LIMIT); USART1_SendStr("\r\n"); #endif TurnBuzzerOn(); TurnSpeakerOn(); TurnRelayOn(); } else { TurnBuzzerOff(); TurnSpeakerOff(); TurnRelayOff(); } } // Sim900_Process(); } // if (millis() - sms_test_time > 10000) // { // Sim900_SendSMS("Hi kieu", "01677880531"); // sms_test_time = millis(); // } // usart_len = USART1_Available(); // // // usart: for test // if (usart_len > 4) // { // int i; // USART1_SendStr("\nUSART1 received packet: \n"); // USART1_GetData(buff_usart, usart_len); // for (i = 0; i < usart_len; i++) // USART1_SendByte(buff_usart[i], HEX); // USART1_SendChar('\n'); // if (ThesisProcess(buff_usart, usart_len) == THESIS_OK) // { // memset(buff_usart, 0, usart_len); // USART1_Flush(); // if (thesis_need_to_send) // { // int i; // USART1_SendStr("\nNeed to send packet: "); // for (i = 0; i < thesis_msg_len; i++) // { // USART1_SendByte(thesis_sent_msg[i], HEX); // } // USART1_SendStr("\nNeed to send packet length: "); // USART1_SendNum(thesis_msg_len); // USART1_SendStr("\n"); // thesis_msg_len = 0; // thesis_need_to_send = 0; // } // USART1_SendStr("\nPacket processed.\n"); // } // else if (thesis_errn == THESIS_FLASH_ERROR) // { // USART1_SendStr("\n"); // USART1_SendStr(thesis_err_msg); // USART1_SendStr("\n"); // led_toggle(); // for(;;); // } // else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH) // { // memset(buff_usart, 0, usart_len); // USART1_Flush(); // USART1_SendStr("Packet processing fail.\n"); // } // // USART1_SendStr("\n"); // USART1_SendStr(thesis_err_msg); // USART1_SendStr("\n"); // } // rf if (Enrf24_available(1)) { int i; rf_len = Enrf24_read(buff_rf + rf_len, BUFFER_SIZE - 1 - rf_len); #if DEBUG USART1_SendStr("\nRF received packet.\n"); for (i = 0; i < rf_len; i++) USART1_SendByte(buff_rf[i], HEX); USART1_SendChar('\n'); #endif if (ThesisProcess(buff_rf, rf_len) == THESIS_OK) { memset(buff_rf, 0, rf_len); rf_len = 0; if (thesis_need_to_send) { int i; #if DEBUG USART1_SendStr("\nNeed to send packet: "); for (i = 0; i < thesis_msg_len; i++) { // Enrf24_write(thesis_sent_msg[i]); USART1_SendByte(thesis_sent_msg[i], HEX); } #endif Enrf24_write_buff(thesis_sent_msg, thesis_msg_len); Enrf24_flush(); #if DEBUG USART1_SendStr("\nNeed to send packet length: "); USART1_SendNum(thesis_msg_len); USART1_SendStr("\n"); #endif thesis_msg_len = 0; thesis_need_to_send = 0; } #if DEBUG USART1_SendStr("\nPacket processed.\n"); #endif } else if (thesis_errn == THESIS_FLASH_ERROR) { #if DEBUG USART1_SendStr("\n"); USART1_SendStr(thesis_err_msg); USART1_SendStr("\n"); #endif led_toggle(); for(;;); } else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH) { memset(buff_rf, 0, rf_len); // RF_Flush(); rf_len = 0; #if DEBUG USART1_SendStr("Packet processing fail.\n"); #endif } #if DEBUG USART1_SendStr("\n"); USART1_SendStr(thesis_err_msg); USART1_SendStr("\n"); #endif } // rs485 rs485_len = RS485_Available(); if (rs485_len > 4) { int i; RS485_GetData(buff_rs485, rs485_len); #if DEBUG USART1_SendStr("\nUSART1 received packet: \n"); for (i = 0; i < rs485_len; i++) USART1_SendByte(buff_rs485[i], HEX); USART1_SendChar('\n'); #endif if (ThesisProcess(buff_rs485, rs485_len) == THESIS_OK) { memset(buff_rs485, 0, rs485_len); RS485_Flush(); if (thesis_need_to_send) { int i; #if DEBUG USART1_SendStr("\nNeed to send packet: "); #endif RS485_DIR_Output(); for (i = 0; i < thesis_msg_len; i++) { RS485_SendChar(thesis_sent_msg[i]); #if DEBUG USART1_SendByte(thesis_sent_msg[i], HEX); #endif } RS485_DIR_Input(); #if DEBUG USART1_SendStr("\nNeed to send packet length: "); USART1_SendNum(thesis_msg_len); USART1_SendStr("\n"); #endif thesis_msg_len = 0; thesis_need_to_send = 0; } #if DEBUG USART1_SendStr("\nPacket processed.\n"); #endif } else if (thesis_errn == THESIS_FLASH_ERROR) { #if DEBUG USART1_SendStr("\n"); USART1_SendStr(thesis_err_msg); USART1_SendStr("\n"); #endif led_toggle(); for(;;); } else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH) { memset(buff_rs485, 0, rs485_len); RS485_Flush(); #if DEBUG USART1_SendStr("Packet processing fail.\n"); #endif } #if DEBUG USART1_SendStr("\n"); USART1_SendStr(thesis_err_msg); USART1_SendStr("\n"); #endif } } }
/* ****************************************************************************** *函数:void LCD_GPIOInit(void) *输入:void *输出:void *描述:LCD初始化 ****************************************************************************** */ void LCD_Init(void) { LTDC_InitTypeDef LTDC_InitStruct; LTDC_Layer_InitTypeDef LTDC_Layer_InitStruct; LTDC_Layer_TypeDef LTDC_Layerx; /* IO口初始化 */ LCD_GPIOInit(); LCD_DisplayOff(); /* 使能LCD时钟 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_LTDC, ENABLE); /* 使能DMA失踪*/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2D, ENABLE); /* 水平同步信号---低电平有效 */ LTDC_InitStruct.LTDC_HSPolarity = LTDC_HSPolarity_AL; /* 垂直同步信号---低电平有效 */ LTDC_InitStruct.LTDC_VSPolarity = LTDC_VSPolarity_AL; /* 数据使能信号---低电平有效 */ LTDC_InitStruct.LTDC_DEPolarity = LTDC_DEPolarity_AL; /* 像素时钟配置--- */ LTDC_InitStruct.LTDC_PCPolarity = LTDC_DEPolarity_AL; /* LCD背光设置 */ LTDC_InitStruct.LTDC_BackgroundRedValue = 0; LTDC_InitStruct.LTDC_BackgroundGreenValue = 0; LTDC_InitStruct.LTDC_BackgroundBlueValue = 0; /* **************************************************************************** *PLLSAI_VCO = HSE*PLLSAI_N / PLL_M = 8 * 192 / 8 = 192MHz *PLLLCDCLK = PLLSAI_VCO / PLLSAI_R = 192 / 3 = 64 Mhz *LTDC clock frequency = PLLLCDCLK / RCC_PLLSAIDivR = 64 / 8 = 8 Mhz **************************************************************************** */ RCC_PLLSAIConfig(192, 7, 3); RCC_LTDCCLKDivConfig(RCC_PLLSAIDivR_Div4); /* 使能PLLSAI时钟 */ RCC_PLLSAICmd(ENABLE); /* 等待PLLSAI时钟 */ while(RCC_GetFlagStatus(RCC_FLAG_PLLSAIRDY) == RESET){} /* */ LTDC_InitStruct.LTDC_HorizontalSync = 40; /* */ LTDC_InitStruct.LTDC_VerticalSync = 9; /* */ LTDC_InitStruct.LTDC_AccumulatedHBP = 42; /* */ LTDC_InitStruct.LTDC_AccumulatedVBP = 11; /* */ LTDC_InitStruct.LTDC_AccumulatedActiveW = 522; /* */ LTDC_InitStruct.LTDC_AccumulatedActiveH = 283; /* */ LTDC_InitStruct.LTDC_TotalWidth = 524; /* */ LTDC_InitStruct.LTDC_TotalHeigh = 285; LTDC_Init(<DC_InitStruct); LTDC_Layer_InitStruct.LTDC_HorizontalStart = 43; LTDC_Layer_InitStruct.LTDC_HorizontalStop = (480 + 43 - 1); LTDC_Layer_InitStruct.LTDC_VarticalStart = 12; LTDC_Layer_InitStruct.LTDC_VerticalStop = (272 + 12 - 1); /* Pixel Format configuration*/ LTDC_Layer_InitStruct.LTDC_PixelFormat = LTDC_Pixelformat_RGB565; /* Alpha constant (255 totally opaque) */ LTDC_Layer_InitStruct.LTDC_ConstantAlpha = 255; /* Default Color configuration (configure A,R,G,B component values) */ LTDC_Layer_InitStruct.LTDC_DefaultColorBlue = 0; LTDC_Layer_InitStruct.LTDC_DefaultColorGreen = 0; LTDC_Layer_InitStruct.LTDC_DefaultColorRed = 0; LTDC_Layer_InitStruct.LTDC_DefaultColorAlpha = 0; /* Configure blending factors */ LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_CA; LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_CA; /* the length of one line of pixels in bytes + 3 then : Line Lenth = Active high width x number of bytes per pixel + 3 Active high width = LCD_PIXEL_WIDTH number of bytes per pixel = 2 (pixel_format : RGB565) */ LTDC_Layer_InitStruct.LTDC_CFBLineLength = ((480 * 2) + 3); /* the pitch is the increment from the start of one line of pixels to the start of the next line in bytes, then : Pitch = Active high width x number of bytes per pixel */ LTDC_Layer_InitStruct.LTDC_CFBPitch = (480 * 2); /* configure the number of lines */ LTDC_Layer_InitStruct.LTDC_CFBLineNumber = 272; /* Input Address configuration */ LTDC_Layer_InitStruct.LTDC_CFBStartAdress = LCD_FRAME_BUFFER; LTDC_LayerInit(LTDC_Layer1, <DC_Layer_InitStruct); /* Configure Layer2 */ LTDC_Layer_InitStruct.LTDC_CFBStartAdress = LCD_FRAME_BUFFER + BUFFER_OFFSET; LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_PAxCA; LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_PAxCA; LTDC_LayerInit(LTDC_Layer2, <DC_Layer_InitStruct); LTDC_ReloadConfig(LTDC_IMReload); /* Enable foreground & background Layers */ LTDC_LayerCmd(LTDC_Layer1, ENABLE); // LTDC_LayerCmd(LTDC_Layer2, ENABLE); LTDC_ReloadConfig(LTDC_IMReload); LCD_DisplayOn(); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* Configure all unused GPIO port pins in Analog Input mode (floating input trigger OFF), this will reduce the power consumption and increase the device immunity against EMI/EMC *************************************************/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_Init(GPIOE, &GPIO_InitStructure); a = 0; while (1) { a++; a--; a *= 2; a -= 3; } RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, DISABLE); #ifdef USE_STM3210E_EVAL RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE); GPIO_Init(GPIOF, &GPIO_InitStructure); GPIO_Init(GPIOG, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, DISABLE); #endif /* USE_STM3210E_EVAL */ /* Initialize Leds mounted on STM3210X-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); while (1) { /* Turn on LD1 */ STM_EVAL_LEDOn(LED1); /* Insert delay */ Delay(0xAFFFF); /* Turn on LD2 and LD3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn off LD1 */ STM_EVAL_LEDOff(LED1); /* Insert delay */ Delay(0xAFFFF); /* Turn on LD4 */ STM_EVAL_LEDOn(LED4); /* Turn off LD2 and LD3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); /* Insert delay */ Delay(0xAFFFF); /* Turn off LD4 */ STM_EVAL_LEDOff(LED4); } }
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) { GPIO_InitTypeDef GPIO_InitStructure; #ifdef USE_USB_OTG_FS RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); /* Configure SOF ID DM DP Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ; /* Configure VBUS Pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure ID pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; #else // USE_USB_OTG_HS RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0 GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1 GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2 GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3 GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4 GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5 GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6 GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7 GPIO_PinAFConfig(GPIOC,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // NXT GPIO_PinAFConfig(GPIOC,GPIO_PinSource2, GPIO_AF_OTG2_HS) ; // DIR GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP // CLK GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStructure); // D0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); // D1 D2 D3 D4 D5 D6 D7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); // STP NXT DIR GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure pin for OTG_HOST RST */ RCC_AHB1PeriphClockCmd(OTG_HS_RST_PORT_RCC, ENABLE); //RST GPIO_InitStructure.GPIO_Pin = OTG_HS_RST_PIN ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(OTG_HS_RST_PORT, &GPIO_InitStructure); /* reset the ULPI_PHY */ GPIO_ResetBits(OTG_HS_RST_PORT, OTG_HS_RST_PIN); _uDelay(2000); GPIO_SetBits(OTG_HS_RST_PORT, OTG_HS_RST_PIN); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | RCC_AHB1Periph_OTG_HS_ULPI, ENABLE); #endif //USB_OTG_HS #ifdef USE_USB_OTG_HS /* Enable the OTG ID Detect Fuciton */ /* Configure pin for OTG_HOST detection */ RCC_AHB1PeriphClockCmd(OTG_HS_ID_PORT_RCC, ENABLE); /* Reset the GPIO_InitStructure variable */ /* Configure ID Pin */ GPIO_InitStructure.GPIO_Pin = OTG_HS_ID_LINE ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(OTG_HS_ID_PORT, &GPIO_InitStructure); /* Configure pin for OTG Power Manager */ RCC_AHB1PeriphClockCmd(OTG_HS_PWR_PORT_RCC, ENABLE); /* Configure Power Pin */ GPIO_InitStructure.GPIO_Pin = OTG_HS_PWR_PIN ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(OTG_HS_PWR_PORT, &GPIO_InitStructure); #endif //USB_OTG_HS /* Intialize Timer for delay function */ USB_OTG_BSP_TimeInit(); }
int32_t PIOS_TIM_InitClock(const struct pios_tim_clock_cfg *cfg) { PIOS_DEBUG_Assert(cfg); /* Enable appropriate clock to timer module */ switch ((uint32_t)cfg->timer) { case (uint32_t)TIM1: RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); break; case (uint32_t)TIM2: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); break; case (uint32_t)TIM3: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); break; case (uint32_t)TIM4: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); break; case (uint32_t)TIM5: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); break; case (uint32_t)TIM6: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); break; case (uint32_t)TIM7: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); break; case (uint32_t)TIM8: RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); break; case (uint32_t)TIM9: RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE); break; case (uint32_t)TIM10: RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE); break; case (uint32_t)TIM11: RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, ENABLE); break; case (uint32_t)TIM12: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12, ENABLE); break; case (uint32_t)TIM13: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE); break; case (uint32_t)TIM14: RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE); break; } /* Configure the dividers for this timer */ TIM_TimeBaseInit(cfg->timer, cfg->time_base_init); /* Configure internal timer clocks */ TIM_InternalClockConfig(cfg->timer); /* Enable timers */ TIM_Cmd(cfg->timer, ENABLE); /* Enable Interrupts */ NVIC_Init(&cfg->irq.init); return 0; }
/******************************************************************************* * Function Name : SPI_LCD_Init * Description : Initializes the peripherals used by the SPI FLASH driver. * Input : None * Output : None * Return : None *******************************************************************************/ void SPI_LCD_Init(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; #if USE_SPI /* Enable LCD_SPIx and GPIO clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); #endif RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIO_RS |RCC_APB2Periph_GPIO_REST | RCC_APB2Periph_GPIO_CS, ENABLE); #if USE_SPI /* Configure LCD_SPIx pins: SCK, MISO and MOSI */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 /*| GPIO_Pin_14 miso*/ | GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); #else /* Configure LCD_SPIx pins: SCK, MISO and MOSI */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 /*| GPIO_Pin_14 miso*/ | GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); SPI_LCD_CLK(1); #endif //REST GPIO_InitStructure.GPIO_Pin = GPIO_Pin_REST; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIO_REST, &GPIO_InitStructure); // rs pin config GPIO_InitStructure.GPIO_Pin = GPIO_Pin_RS; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIO_RS, &GPIO_InitStructure); /* Configure I/O for Flash Chip select */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CS; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIO_CS, &GPIO_InitStructure); /* Deselect the FLASH: Chip Select high */ SPI_LCD_CS_HIGH(); #if USE_SPI /* LCD_SPIx configuration */ SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(LCD_SPIx, &SPI_InitStructure); /* Enable LCD_SPIx */ SPI_Cmd(LCD_SPIx, ENABLE); #endif }
void ADC3_CH13_Config(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable ADC3, DMA2 and GPIO clocks ****************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); /* DMA2 Stream0 channel2 configuration **************************************/ DMA_InitStructure.DMA_Channel = DMA_Channel_2; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&base; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); /* Configure ADC3 Channel13 pin as analog input ******************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC3 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC3, &ADC_InitStructure); /* ADC3 regular channel13 configuration *************************************/ ADC_RegularChannelConfig(ADC3, ADC_Channel_13, 1, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE); /* Enable ADC3 DMA */ ADC_DMACmd(ADC3, ENABLE); /* Enable ADC3 */ ADC_Cmd(ADC3, ENABLE); }
/** * @brief Initializes the SD Card and put it into StandBy State (Ready for * data transfer). * @param None * @retval None */ void SD_LowLevel_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /*!< SD_SPI_CS_GPIO, SD_SPI_MOSI_GPIO, SD_SPI_MISO_GPIO, SD_SPI_DETECT_GPIO and SD_SPI_SCK_GPIO Periph clock enable */ RCC_AHBPeriphClockCmd(SD_CS_GPIO_CLK | SD_SPI_MOSI_GPIO_CLK | SD_SPI_MISO_GPIO_CLK | SD_SPI_SCK_GPIO_CLK | SD_DETECT_GPIO_CLK, ENABLE); /*!< SD_SPI Periph clock enable */ RCC_APB2PeriphClockCmd(SD_SPI_CLK, ENABLE); /*!< Configure SD_SPI pins: SCK */ GPIO_InitStructure.GPIO_Pin = SD_SPI_SCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI pins: MISO */ GPIO_InitStructure.GPIO_Pin = SD_SPI_MISO_PIN; GPIO_Init(SD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI pins: MOSI */ GPIO_InitStructure.GPIO_Pin = SD_SPI_MOSI_PIN; GPIO_Init(SD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI_CS_PIN pin: SD Card CS pin */ GPIO_InitStructure.GPIO_Pin = SD_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */ GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure); /* Connect PXx to SD_SPI_SCK */ GPIO_PinAFConfig(SD_SPI_SCK_GPIO_PORT, SD_SPI_SCK_SOURCE, SD_SPI_SCK_AF); /* Connect PXx to SD_SPI_MISO */ GPIO_PinAFConfig(SD_SPI_MISO_GPIO_PORT, SD_SPI_MISO_SOURCE, SD_SPI_MISO_AF); /* Connect PXx to SD_SPI_MOSI */ GPIO_PinAFConfig(SD_SPI_MOSI_GPIO_PORT, SD_SPI_MOSI_SOURCE, SD_SPI_MOSI_AF); /*!< SD_SPI Config */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SD_SPI, &SPI_InitStructure); SPI_RxFIFOThresholdConfig(SD_SPI, SPI_RxFIFOThreshold_QF); SPI_Cmd(SD_SPI, ENABLE); /*!< SD_SPI enable */ }
// Setup Resolver Interface // TIM8 triggers ADC1 and 2 at 20kHz // TIM8 OC1 generates resolver reference signal at 10kHz // DMA2 moves 4 samples to memory, generates transfer complete interrupt at 5kHz void setup_res(){ //resolver timer RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 420*2;//20kHz TIM_TimeBaseStructure.TIM_Prescaler = 9; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); TIM_ITConfig(TIM8, TIM_IT_Update, DISABLE); TIM_SelectOutputTrigger(TIM8, TIM_TRGOSource_Update);//trigger ADC //resolver ref signal generation RCC_AHB1PeriphClockCmd(RES_IO_RCC, ENABLE); GPIO_InitStructure.GPIO_Pin = RES_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(RES_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(RES_PORT, GPIO_PinSource5, GPIO_AF_TIM8); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = 300; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC1Init(TIM8, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); TIM_CtrlPWMOutputs(TIM8, ENABLE); RCC_AHB1PeriphClockCmd(SIN_IO_RCC, ENABLE); RCC_AHB1PeriphClockCmd(COS_IO_RCC, ENABLE); /* ADC clock enable */ RCC_APB2PeriphClockCmd(SIN_ADC_RCC | COS_ADC_RCC, ENABLE); //Analog pin configuration GPIO_InitStructure.GPIO_Pin = SIN_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(SIN_PORT,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = COS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(COS_PORT,&GPIO_InitStructure); //ADC structure configuration ADC_DeInit(); ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T8_TRGO;//trigger on rising edge of TIM8 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_NbrOfConversion = ADC_ANZ;//I think this one is clear :p ADC_InitStructure.ADC_ScanConvMode = ENABLE;//The scan is configured in one channel ADC_Init(SIN_ADC, &ADC_InitStructure);//Initialize ADC with the previous configuration ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_Init(COS_ADC, &ADC_InitStructure);//Initialize ADC with the previous configuration ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); for(int i = 1;i<=ADC_ANZ;i++){ ADC_RegularChannelConfig(SIN_ADC, SIN_ADC_CHAN, i, RES_SampleTime); ADC_RegularChannelConfig(COS_ADC, COS_ADC_CHAN, i, RES_SampleTime); } ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE); //Enable ADC conversion ADC_Cmd(SIN_ADC,ENABLE); ADC_Cmd(COS_ADC,ENABLE); // Clock Enable RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA-Disable DMA_Cmd(DMA2_Stream0, DISABLE); DMA_DeInit(DMA2_Stream0); // DMA2-Config DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC->CDR; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_DMA_Buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = ADC_ANZ*PID_WAVES; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE); }
/** * @brief Initializes the low level interface used to drive the LIS302DL * @param None * @retval None */ static void LIS302DL_LowLevel_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /* Enable the SPI periph */ RCC_APB2PeriphClockCmd(LIS302DL_SPI_CLK, ENABLE); /* Enable SCK, MOSI and MISO GPIO clocks */ RCC_AHB1PeriphClockCmd(LIS302DL_SPI_SCK_GPIO_CLK | LIS302DL_SPI_MISO_GPIO_CLK | LIS302DL_SPI_MOSI_GPIO_CLK, ENABLE); /* Enable CS GPIO clock */ RCC_AHB1PeriphClockCmd(LIS302DL_SPI_CS_GPIO_CLK, ENABLE); /* Enable INT1 GPIO clock */ RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT1_GPIO_CLK, ENABLE); /* Enable INT2 GPIO clock */ RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT2_GPIO_CLK, ENABLE); GPIO_PinAFConfig(LIS302DL_SPI_SCK_GPIO_PORT, LIS302DL_SPI_SCK_SOURCE, LIS302DL_SPI_SCK_AF); GPIO_PinAFConfig(LIS302DL_SPI_MISO_GPIO_PORT, LIS302DL_SPI_MISO_SOURCE, LIS302DL_SPI_MISO_AF); GPIO_PinAFConfig(LIS302DL_SPI_MOSI_GPIO_PORT, LIS302DL_SPI_MOSI_SOURCE, LIS302DL_SPI_MOSI_AF); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_SCK_PIN; GPIO_Init(LIS302DL_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_MOSI_PIN; GPIO_Init(LIS302DL_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_MISO_PIN; GPIO_Init(LIS302DL_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); /* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(LIS302DL_SPI); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(LIS302DL_SPI, &SPI_InitStructure); /* Enable SPI1 */ SPI_Cmd(LIS302DL_SPI, ENABLE); /* Configure GPIO PIN for Lis Chip select */ GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(LIS302DL_SPI_CS_GPIO_PORT, &GPIO_InitStructure); /* Deselect : Chip Select high */ GPIO_SetBits(LIS302DL_SPI_CS_GPIO_PORT, LIS302DL_SPI_CS_PIN); /* Configure GPIO PINs to detect Interrupts */ GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_INT1_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(LIS302DL_SPI_INT1_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_INT2_PIN; GPIO_Init(LIS302DL_SPI_INT2_GPIO_PORT, &GPIO_InitStructure); }
static void prvSetupHardware( void ) { /* Start with the clocks in their expected state. */ RCC_DeInit(); /* Enable HSE (high speed external clock). */ RCC_HSEConfig( RCC_HSE_ON ); /* Wait till HSE is ready. */ while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET ) { } /* 2 wait states required on the flash. */ *( ( unsigned portLONG * ) 0x40022000 ) = 0x02; /* HCLK = SYSCLK */ RCC_HCLKConfig( RCC_SYSCLK_Div1 ); /* PCLK2 = HCLK */ RCC_PCLK2Config( RCC_HCLK_Div1 ); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config( RCC_HCLK_Div2 ); /* PLLCLK = 12MHz * 6 = 72 MHz. */ RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 ); /* Enable PLL. */ RCC_PLLCmd( ENABLE ); /* Wait till PLL is ready. */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source. */ RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK ); /* Wait till PLL is used as system clock source. */ while( RCC_GetSYSCLKSource() != 0x08 ) { } /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */ RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE ); /* SPI2 Periph clock enable */ RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE ); /* Set the Vector Table base address at 0x08000000 */ NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 ); NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ); /* Configure HCLK clock as SysTick clock source. */ SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK ); /* Misc initialisation, including some of the CircleOS features. Note that CircleOS itself is not used. */ vParTestInitialise(); MEMS_Init(); POINTER_Init(); POINTER_SetMode( POINTER_RESTORE_LESS ); }
void FSMC_LCD_Init(void) { FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; FSMC_NORSRAMTimingInitTypeDef p; GPIO_InitTypeDef GPIO_InitStructure; //使能FSMC外设时钟 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //LCD背光控制 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; //LCD复位 GPIO_Init(GPIOE, &GPIO_InitStructure); // 复用端口为FSMC接口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10), PE.14(D11), PE.15(D12) as alternate function push pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOE, &GPIO_InitStructure); //FSMC NE1 LCD片选 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOD, &GPIO_InitStructure); //FSMC RS---LCD指令 指令/数据 切换 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_SetBits(GPIOD, GPIO_Pin_13); //LCD背光打开 //FSMC接口特性配置 p.FSMC_AddressSetupTime = 0x02; p.FSMC_AddressHoldTime = 0x00; p.FSMC_DataSetupTime = 0x05; p.FSMC_BusTurnAroundDuration = 0x00; p.FSMC_CLKDivision = 0x00; p.FSMC_DataLatency = 0x00; p.FSMC_AccessMode = FSMC_AccessMode_B; FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR; FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); /* Enable FSMC Bank1_SRAM Bank */ FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); }
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) { // EXTI_InitTypeDef EXTI_InitStructure; #ifdef USE_STM3210C_EVAL RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE) ; #else // USE_STM322xG_EVAL GPIO_InitTypeDef GPIO_InitStructure; #ifdef USE_USB_OTG_FS RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); /* Configure SOF VBUS ID DM DP Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ; /* this for ID line debug */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; #else // USE_USB_OTG_HS #ifdef USE_ULPI_PHY // ULPI RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0 GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1 GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2 GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7 GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3 GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4 GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5 GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6 GPIO_PinAFConfig(GPIOH,GPIO_PinSource4, GPIO_AF_OTG2_HS) ; // NXT GPIO_PinAFConfig(GPIOI,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // DIR GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP // CLK GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStructure); // D0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); // D1 D2 D3 D4 D5 D6 D7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); // STP GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOC, &GPIO_InitStructure); //NXT GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOH, &GPIO_InitStructure); //DIR GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOI, &GPIO_InitStructure); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | RCC_AHB1Periph_OTG_HS_ULPI, ENABLE) ; #else #ifdef USE_I2C_PHY RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB , ENABLE); /* Configure RESET INTN SCL SDA (Phy/I2C) Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_FS); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; #else RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; #endif #endif #endif //USB_OTG_HS #endif //USE_STM322xG_EVAL /* Intialize Timer for delay function */ USB_OTG_BSP_TimeInit(); }
/******************************************************************************* * Function Name : Demo_Init * Description : Initializes the demonstration application. * Input : None * Output : None * Return : None *******************************************************************************/ void Demo_Init(void) { /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/ /* 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); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_16); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } /* Enable USART1 and GPIOA clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); } /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO, ENABLE); /*------------------- Resources Initialization -----------------------------*/ /* ADCCLK = PCLK2/8 */ RCC_ADCCLKConfig(RCC_PCLK2_Div8); /* Enable DMA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable ADC1 and GPIOC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE); /* If HSE is not detected at program startup */ if(HSEStartUpStatus == ERROR) { /* Generate NMI exception */ NVIC_SetSystemHandlerPendingBit(SystemHandler_NMI); } }
void sys_usartInit(USART_TypeDef *USART_x, u32 Boud) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; if(USART_x == USART1) { #if (!USART1_REMAP) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); #else RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStructure); #endif RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); } else if(USART_x == USART2) { #if (!USART2_REMAP) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); #else RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOD, &GPIO_InitStructure); #endif RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_SetBits(GPIOC, GPIO_Pin_8); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } else if(USART_x == USART3) { #if (!USART3_PARTIALREMAP)&&(!USART3_FULLREMAP) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_SetBits(GPIOD, GPIO_Pin_8); #elif (USART3_PARTIALREMAP)&&(USART3_FULLREMAP) #err "USART3_PARTIALREMAP and USART3_FULLREMAP == 1" #elif (USART3_PARTIALREMAP) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOC, &GPIO_InitStructure); #elif (USART3_FULLREMAP) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOD, &GPIO_InitStructure); #endif RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); } USART_InitStructure.USART_BaudRate = Boud; 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_Tx | USART_Mode_Rx; USART_Init(USART_x, &USART_InitStructure); USART_Cmd(USART_x, ENABLE); if(USART_x == USART1) { USART1_NVIC_Init(); USART1_DMA_Init(); } }
void inputInit() { RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitTypeDef init; EXTI_InitTypeDef exti; NVIC_InitTypeDef nvic; //D24 - (PA11) init.GPIO_Pin = GPIO_Pin_11; init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &init); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource11); exti.EXTI_Line = EXTI_Line11; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Rising_Falling; exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI15_10_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0x0F; nvic.NVIC_IRQChannelSubPriority = 0x0F; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); //D27 - (PA8)/////////////////////////////// init.GPIO_Pin = GPIO_Pin_8; init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &init); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8); exti.EXTI_Line = EXTI_Line8; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Rising_Falling; exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI9_5_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0x0F; nvic.NVIC_IRQChannelSubPriority = 0x0F; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); //D28 - (PB15)///////////////////////////////// init.GPIO_Pin = GPIO_Pin_15; init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &init); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource15); exti.EXTI_Line = EXTI_Line15; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Rising_Falling; exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI15_10_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0x0F; nvic.NVIC_IRQChannelSubPriority = 0x0F; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); //D29 - (PB14)//////////////////////////////////// init.GPIO_Pin = GPIO_Pin_14; init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &init); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14); exti.EXTI_Line = EXTI_Line14; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Rising_Falling; exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI15_10_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0x0F; nvic.NVIC_IRQChannelSubPriority = 0x0F; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); //D30 - (PB13)///////////////////////////////////// init.GPIO_Pin = GPIO_Pin_13; init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &init); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource13); exti.EXTI_Line = EXTI_Line13; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Rising_Falling; exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI15_10_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0x0F; nvic.NVIC_IRQChannelSubPriority = 0x0F; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); //D31 - (PB12)/////////////////////////////////////// init.GPIO_Pin = GPIO_Pin_12; init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &init); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12); exti.EXTI_Line = EXTI_Line12; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Rising_Falling; exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI15_10_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0x0F; nvic.NVIC_IRQChannelSubPriority = 0x0F; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); }
uint8_t MPU_Init(void) { GPIO_InitTypeDef gpio; NVIC_InitTypeDef nvic; EXTI_InitTypeDef exti; int res=0; IIC_Init(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); gpio.GPIO_Pin = GPIO_Pin_5; gpio.GPIO_Mode = GPIO_Mode_IN; gpio.GPIO_OType = GPIO_OType_PP; gpio.GPIO_PuPd = GPIO_PuPd_UP; gpio.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOB, &gpio); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB,GPIO_PinSource5); exti.EXTI_Line = EXTI_Line5; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿中断 exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI9_5_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = ITP_MPU_EXTI9_5_PREEMPTION; nvic.NVIC_IRQChannelSubPriority = ITP_MPU_EXTI9_5_SUB; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); if(mpu_init()==0) //初始化MPU6050 { res=mpu_set_sensors(INV_XYZ_GYRO|INV_XYZ_ACCEL);//设置所需要的传感器 if(res)return 1; res=mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL);//设置FIFO if(res)return 2; res=mpu_set_sample_rate(DEFAULT_MPU_HZ); //设置采样率 if(res)return 3; res=dmp_load_motion_driver_firmware(); //加载dmp固件 if(res)return 4; res=dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation));//设置陀螺仪方向 if(res)return 5; res=dmp_enable_feature(DMP_FEATURE_6X_LP_QUAT|DMP_FEATURE_TAP| //设置dmp功能 DMP_FEATURE_ANDROID_ORIENT|DMP_FEATURE_SEND_RAW_ACCEL|DMP_FEATURE_SEND_CAL_GYRO| DMP_FEATURE_GYRO_CAL); if(res)return 6; res=dmp_set_fifo_rate(DEFAULT_MPU_HZ); //设置DMP输出速率(最大不超过200Hz) if(res)return 7; res=mpu_set_int_level(1); if(res)return 8; res=dmp_set_interrupt_mode(DMP_INT_CONTINUOUS); if(res)return 9; res=run_self_test(); //自检 if(res)return 10; res=mpu_set_dmp_state(1); //使能DMP if(res)return 11; } else return 12; return 0; }
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) { GPIO_InitTypeDef GPIO_InitStructure; #ifndef USE_ULPI_PHY #ifdef USB_OTG_FS_LOW_PWR_MGMT_SUPPORT EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; #endif #endif #ifdef USE_USB_OTG_FS RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); /* Configure SOF VBUS ID DM DP Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ; /* this for ID line debug */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; #else // USE_USB_OTG_HS #ifdef USE_ULPI_PHY // ULPI RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0 GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1 GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2 GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7 GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3 GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4 GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5 GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6 GPIO_PinAFConfig(GPIOH,GPIO_PinSource4, GPIO_AF_OTG2_HS) ; // NXT GPIO_PinAFConfig(GPIOI,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // DIR GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP // CLK GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStructure); // D0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); // D1 D2 D3 D4 D5 D6 D7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); // STP GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOC, &GPIO_InitStructure); //NXT GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOH, &GPIO_InitStructure); //DIR GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOI, &GPIO_InitStructure); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | RCC_AHB1Periph_OTG_HS_ULPI, ENABLE) ; #else #ifdef USE_I2C_PHY RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB , ENABLE); /* Configure RESET INTN SCL SDA (Phy/I2C) Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_FS); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; #else RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ; #endif #endif // USE_ULPI_PHY #endif //USB_OTG_HS /* enable the PWR clock */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE); /* Configure the Key button in EXTI mode */ //STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); #ifdef USB_OTG_FS_LOW_PWR_MGMT_SUPPORT EXTI_ClearITPendingBit(EXTI_Line18); EXTI_InitStructure.EXTI_Line = EXTI_Line18; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); EXTI_ClearITPendingBit(EXTI_Line18); NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); EXTI_ClearITPendingBit(EXTI_Line18); #endif #ifdef USB_OTG_HS_LOW_PWR_MGMT_SUPPORT EXTI_ClearITPendingBit(EXTI_Line20); EXTI_InitStructure.EXTI_Line = EXTI_Line20; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); EXTI_ClearITPendingBit(EXTI_Line20); NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); EXTI_ClearITPendingBit(EXTI_Line20); #endif EXTI_ClearITPendingBit(EXTI_Line0); }
//默认一个从机 串口1 波特率可设置 奇偶检验可设置 BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //======================时钟初始化======================================= RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_USART1, ENABLE); //======================IO初始化======================================= //USART1_TX GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOA, &GPIO_InitStructure); //USART1_RX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); //配置485发送和接收模式 // TODO 暂时先写B13 等之后组网测试时再修改 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOC, &GPIO_InitStructure); //======================串口初始化======================================= USART_InitStructure.USART_BaudRate = ulBaudRate; //设置校验模式 switch (eParity) { case MB_PAR_NONE: //无校验 USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_WordLength = USART_WordLength_8b; break; case MB_PAR_ODD: //奇校验 USART_InitStructure.USART_Parity = USART_Parity_Odd; USART_InitStructure.USART_WordLength = USART_WordLength_9b; break; case MB_PAR_EVEN: //偶校验 USART_InitStructure.USART_Parity = USART_Parity_Even; USART_InitStructure.USART_WordLength = USART_WordLength_9b; break; default: return FALSE; } USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; if (ucPORT != 1) return FALSE; ENTER_CRITICAL_SECTION(); //关全局中断 USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); //=====================中断初始化====================================== //设置NVIC优先级分组为Group2:0-3抢占式优先级,0-3的响应式优先级 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); EXIT_CRITICAL_SECTION(); //开全局中断 return TRUE; }
/** * AD转换器配置函数 * @param None * @return None */ void ad_config(void) { GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; RCC_AHBPeriphClockCmd(AD_Sencer_1_RCC | AD_Sencer_2_RCC | AD_Sencer_3_RCC | AD_Sencer_Power_RCC, ENABLE); GPIO_InitStructure.GPIO_Pin = AD_Sencer_Power_PIN; /**< AD供电开关 */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_Init(AD_Sencer_Power_PORT, &GPIO_InitStructure); ad_power_switch(ENABLE); /**< AD供电使能 */ GPIO_InitStructure.GPIO_Pin = AD_Sencer_1_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(AD_Sencer_1_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = AD_Sencer_2_PIN; GPIO_Init(AD_Sencer_2_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = AD_Sencer_3_PIN; GPIO_Init(AD_Sencer_3_PORT, &GPIO_InitStructure); RCC_HSICmd(ENABLE); /**< 注意:此芯片的ADC时钟由HSI提供 */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA1_Channel1); /**< 配置通道 */ DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; /**< "桥梁"的一端,AD采样值 */ DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&adc_buffer; /**< "桥梁"的另一端,内存存储基地址 */ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /**< 单向传输 */ DMA_InitStructure.DMA_BufferSize = AD_BUFFER_SIZE * 3; /**< 缓存大小 */ DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; /**< 关闭外设指针的自动递增 */ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /**< 开启内存指针的自动递增 */ DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; /**< 外设数据宽度 */ DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; /**< 内存数据宽度 */ DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /**< 循环缓存 */ DMA_InitStructure.DMA_Priority = DMA_Priority_High; /**< 优先级 */ DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; /**< 禁用memory to memory */ DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_Cmd(DMA1_Channel1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; /**< 分辨率 12位 */ ADC_InitStructure.ADC_ScanConvMode = ENABLE; /**< 关闭扫描模式,只有1个通道时,一般关闭 */ ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; /**< 开启连续转换模式 */ ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; /**< 关闭外部触发,使用软件触发(ADC_SoftwareStartConvCmd) */ ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /**< 12位数据右对齐 */ ADC_InitStructure.ADC_NbrOfConversion = 3; /**< 开启通道数,3个 */ ADC_Init(ADC1, &ADC_InitStructure); /** 采样周期设为:采样时间:384cycles, 转化时间:12cycles, 总时间:(12+384)/16M = 30us; */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_384Cycles); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 2, ADC_SampleTime_384Cycles); ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 3, ADC_SampleTime_384Cycles); ADC_DMACmd(ADC1, ENABLE); ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_RCNR) == SET); /**< Regular channel not ready */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET); /**< ADC ON status */ ADC_SoftwareStartConv(ADC1); /**< 软件触发 */ }
void hw_usart_init(hw_usart_e usart, uint32_t baudrate, uint32_t rx_buf_size, uint32_t tx_buf_size, bool flowControl) { // init the hardware pins hw_usart_init_pins(usart); // init the buffers for the interrupt hw_usart_init_buffers(usart, rx_buf_size, tx_buf_size); // enable the peripheral clock switch (usart) { #if HW_USART_COUNT >= 1 case HW_USART1: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); break; #endif #if HW_USART_COUNT >=2 case HW_USART2: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); break; #endif #if HW_USART_COUNT >=3 case HW_USART3: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); break; #endif #if HW_USART_COUNT >=4 case HW_USART4: RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); break; #endif #if HW_USART_COUNT >=5 case HW_USART5: RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); break; #endif #if HW_USART_COUNT >=6 case HW_USART6: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); break; #endif } // init and enable the usart USART_InitTypeDef usartCfg; usartCfg.USART_BaudRate = baudrate; if (flowControl) { usartCfg.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; } else { usartCfg.USART_HardwareFlowControl = USART_HardwareFlowControl_None; } usartCfg.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usartCfg.USART_Parity = USART_Parity_No; usartCfg.USART_StopBits = USART_StopBits_1; usartCfg.USART_WordLength = USART_WordLength_8b; USART_Init(usarts[usart], &usartCfg); USART_Cmd(usarts[usart], ENABLE); // enable the usart irq NVIC_EnableIRQ(irqns[usart]); USART_ITConfig(usarts[usart], USART_IT_RXNE, ENABLE); // set enabled flag hw_usart_enabled[usart] = true; }
void CC2520_ACTIVATE(void) { //int i; // enable the SPI module which is used for communication between MCU and cc2520. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // configure Port B Pin 14 // Port B Pin 14 is used for SPI's MISO (IPD means Input Pull Down). GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOB,&GPIO_InitStructure); // Port B Pin 1 is used for cc2520 RST // Port B Pin 5 is used for VREG_EN // Port B Pin 12 is used for NSS // GPIO_Mode_Out_PP here means Push Pull(推挽输出) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); // reset the cc2520 nRST GPIO_ResetBits(GPIOB, GPIO_Pin_1); // set VREG_EN which will enable the cc2520's internal voltage regulator GPIO_SetBits( GPIOB,GPIO_Pin_5); // wait for the regulator to be stabe. // @todo //for ( i=0;i<13500;i++); // hal_delayus(?) hal_delayms(2); // set the cc2520 nRST GPIO_SetBits(GPIOB,GPIO_Pin_1); hal_delayus(1); // reset the cc2520 CSn GPIO_ResetBits( GPIOB,GPIO_Pin_12); // @todo: shall we need to wait a little while after CS and then RST for stable? // @todo repalce with hal_delayus(?) hal_delayms(2); // todo //for ( i=0;i<13500;i++);//wait for the output of SO to be 1//todo for testing hal_assert(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14));//todo该语句报错,可能是因为SO引脚的 输出模式改变的原 // set the cc2520 CSn GPIO_SetBits(GPIOB, GPIO_Pin_12); hal_delayus(1); // configure Port B Pin 13 for SCK // configure Port B Pin 15 for SPI's MOSI GPIO_InitStructure.GPIO_Pin = SPI_pin_MOSI | SPI_pin_SCK; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init( GPIO_SPI, &GPIO_InitStructure); // configure Port B Pin 14 for MISO GPIO_InitStructure.GPIO_Pin = SPI_pin_MISO; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIO_SPI, &GPIO_InitStructure); // configure Port B Pin 12 for NSS GPIO_InitStructure.GPIO_Pin = SPI_pin_SS; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIO_SPI,&GPIO_InitStructure); }
/** * @brief Configure the USART to Wake up from STOP mode by Start bit Method * @param None * @retval None */ static void USART_Config(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable GPIOD clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOE , ENABLE); /* Enable USART1 APB clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Configure the HSI as USART clock */ RCC_USARTCLKConfig(RCC_USART1CLK_HSI); /* USART1 Pins configuration ***********************************************/ /* Connect pin to Periph */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_7); GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_7); /* Configure pins as AF pushpull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOE, &GPIO_InitStructure); /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - Stop Bit = 1 Stop Bit - Parity = No Parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_DeInit(USART1); USART_InitStructure.USART_BaudRate = 115200; 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 | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); /* USART2 IRQ Channel configuration */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure the wake up Method = Start bit */ USART_StopModeWakeUpSourceConfig(USART1, USART_WakeUpSource_StartBit); /* Enable USART1 */ USART_Cmd(USART1, ENABLE); /* Before entering the USART in STOP mode the REACK flag must be checked to ensure the USART RX is ready */ while(USART_GetFlagStatus(USART1, USART_FLAG_REACK) == RESET) {} /* Enable USART STOP mode by setting the UESM bit in the CR1 register.*/ USART_STOPModeCmd(USART1, ENABLE); /* Enable the wake up from stop Interrupt */ USART_ITConfig(USART1, USART_IT_WU, ENABLE); /* Enter USART in STOP mode with regulator in low power mode */ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); }
int spi_init(SPI_TypeDef* SPIx) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // USART use DMA1 -> RM0008 Table 78 if (SPIx == SPI1) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); GPIO_StructInit(&GPIO_InitStructure); // SPI1_NCC Pin PA4 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // SPI1_SCK Pin PA5 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // SPI1_MISO Pin PA6 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // Input pull-up GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // SPI1_MOSI Pin PA7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } else if (SPIx == SPI2) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); GPIO_StructInit(&GPIO_InitStructure); // SPI2_NCC Pin PB12 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); // SPI2_SCK Pin PB13 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); // SPI2_MISO Pin PB14 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // Input pull-up GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); // SPI2_MOSI Pin PB15 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); } SPI_InitTypeDef SPI_InitStructure; SPI_StructInit(&SPI_InitStructure); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPIx, &SPI_InitStructure); SPI_Cmd(SPIx, ENABLE); }
void RCC_Config_ADC1(void) { //for ADC1 on PC0 using IN10 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); }