serial::serial(GPIO_TypeDef* bankPinTx, uint16_t pinTx, GPIO_TypeDef* bankPinRx, uint16_t pinRx, USART_TypeDef* USARTx, uint32_t baudrate, uint8_t interruptFlag) { thisUsart = USARTx; GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; usartToClock(USARTx); //Initialize the clock for the given USART bankToClock(bankPinRx); //Initiates the clock for the bank of the Rx pin bankToClock(bankPinTx); //Initiates the clock for the bank of the Tx pin //Initialize the Rx pin GPIO_InitStructure.GPIO_Pin = pinRx; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(bankPinRx, &GPIO_InitStructure); //Initialize the Tx pin GPIO_InitStructure.GPIO_Pin = pinTx; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(bankPinTx, &GPIO_InitStructure); uint16_t pinSourceRx = getPinSource(pinRx); uint16_t pinSourceTx = getPinSource(pinTx); uint8_t altFuncUsart = getUsartAltFunc(USARTx); /* Connect the given USART to the given pins */ GPIO_PinAFConfig(bankPinRx, pinSourceRx, altFuncUsart); //Rx GPIO_PinAFConfig(bankPinTx, pinSourceTx, altFuncUsart); //Tx USART_InitStructure.USART_BaudRate = baudrate; 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(USARTx, &USART_InitStructure); USART_Cmd(USARTx, ENABLE); //Enable USARTx if(interruptFlag) { USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); // Enables Serial Interrupt when data is received } }
i2c::i2c(GPIO_TypeDef* sclBank, uint16_t scl_pin, GPIO_TypeDef* sdaBank, uint16_t sda_pin, I2C_TypeDef* selected_I2C) { GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStruct; I2Cx = selected_I2C; bankToClock(sclBank); //sets the pins clock bankToClock(sdaBank); //sets the pins clock i2c_bankToClcok(I2Cx); //initializes the scl pin GPIO_InitStruct.GPIO_Pin = scl_pin; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(sclBank, &GPIO_InitStruct); GPIO_PinAFConfig(sclBank, scl_pin, getAlternateFunction(I2Cx)); //initializes the sda pin GPIO_InitStruct.GPIO_Pin = sda_pin; GPIO_Init(sdaBank, &GPIO_InitStruct); GPIO_PinAFConfig(sdaBank, sda_pin, getAlternateFunction(I2Cx)); // configures I2C I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on) I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses I2C_Init(I2Cx, &I2C_InitStruct); // init I2C // enable I2C1 I2C_Cmd(I2Cx, ENABLE); }
pwm::pwm(GPIO_TypeDef* bank, uint16_t pin, TIM_TypeDef* timer, uint8_t controlRegister, uint32_t inputFrequency) { frequency = inputFrequency; uint8_t prescaler = 1; //scales the timer clock to be the same as the internal clock timerNum = timer; //Saves the value of the timer inputted by the user controlRegisterNum = controlRegister; //Saves the value of the control register number inputted by the user timerFactor(timer); //gets the correct pulse length for the timer timerToClock(timer); //Initiates the clock for the given timer bankToClock(bank); //Initiates the clock for a given bank GPIO_InitTypeDef GPIO_InitStructure; //structure used by stm in initializing pins. // Initialize the given pin for pwm GPIO_InitStructure.GPIO_Pin = pin; //specifies which pins are used GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //assigns the pins to use their alternate functions GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(bank, &GPIO_InitStructure); //initializes the structure GPIO_PinAFConfig(bank, getPinSource(pin), gpioAltFunc); uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / (84000000 / prescaler)) - 1; uint16_t PreCalPeriod = ((84000000 / prescaler) / frequency) - 1; period = PreCalPeriod; //assigns the calculated period to the global instance variable that will store the period TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //structure used by stm in initializing the pwm // Setup timebase for the given timer TIM_TimeBaseStructure.TIM_Period = PreCalPeriod; //sets the period of the timer TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; //sets the pre scaler which is divided into the cpu clock to get a clock speed that is small enough to use for timers TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(timer, &TIM_TimeBaseStructure); //initializes this part of the code setControlRegister(controlRegister, timer); // Enable the given time peripheral Preload register on ARR. TIM_ARRPreloadConfig(timer, ENABLE); TIM_Cmd(timer, ENABLE); //Enable the timer given }