void WRITE(PinName pin, bool value) { __NOP(); __NOP(); if (value) { GPIO_ADDR[STM_PORT(pin)]->BSRR = (uint32_t)(1 << STM_PIN(pin)); } else { GPIO_ADDR[STM_PORT(pin)]->BSRR = (uint32_t)(0x00010000 << STM_PIN(pin)); } }
/** * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t pupd = STM_PIN_PUPD(data); uint32_t afnum = STM_PIN_AFNUM(data); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Configure GPIO GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index); GPIO_InitStructure.Mode = gpio_mode[mode]; GPIO_InitStructure.Pull = pupd; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Alternate = afnum; HAL_GPIO_Init(gpio, &GPIO_InitStructure); // [TODO] Disconnect JTAG-DP + SW-DP signals. // Warning: Need to reconnect under reset //if ((pin == PA_13) || (pin == PA_14)) { // //} //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { // //} }
/** * @brief TIM MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param htim: TIM handle pointer * @retval None */ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) { GPIO_InitTypeDef GPIO_InitStruct; GPIO_TypeDef *port; uint32_t function = pinmap_function(g_current_pin, PinMap_PWM); /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* TIMx Peripheral clock enable */ timer_enable_clock(htim); /* Enable GPIO Channels Clock */ /* Enable GPIO clock ****************************************/ port = set_GPIO_Port_Clock(STM_PORT(g_current_pin)); /* Common configuration for all channels */ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; #ifdef STM32F1xx pin_SetF1AFPin(STM_PIN_AFNUM(function)); #else GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function); #endif /* STM32F1xx */ GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin); HAL_GPIO_Init(port, &GPIO_InitStruct); }
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) { IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; uint32_t irq_index; if (pin == NC) return -1; uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Select irq number and interrupt routine if ((pin_index == 0) || (pin_index == 1)) { irq_n = EXTI0_1_IRQn; vector = (uint32_t)&gpio_irq0; irq_index = 0; } else if ((pin_index == 2) || (pin_index == 3)) { irq_n = EXTI2_3_IRQn; vector = (uint32_t)&gpio_irq1; irq_index = 1; } else if ((pin_index > 3) && (pin_index < 16)) { irq_n = EXTI4_15_IRQn; vector = (uint32_t)&gpio_irq2; irq_index = 2; } else { error("InterruptIn error: pin not supported.\n"); return -1; } // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); // Configure GPIO pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0)); // Enable EXTI interrupt NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); // Save informations for future use obj->irq_n = irq_n; obj->irq_index = irq_index; obj->event = EDGE_NONE; obj->pin = pin; channel_ids[irq_index] = id; channel_gpio[irq_index] = gpio_add; channel_pin[irq_index] = pin_index; irq_handler = handler; return 0; }
/** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Configure open-drain and pull-up/down switch (mode) { case PullNone: break; case PullUp: case PullDown: // Set pull-up / pull-down for Input mode if (pin_index < 8) { if ((gpio->CRL & (0x03 << (pin_index * 4))) == 0) { // MODE bits = Input mode gpio->CRL |= (0x08 << (pin_index * 4)); // Set pull-up / pull-down gpio->CRL &= ~(0x08 << ((pin_index * 4)-1)); // ENSURES GPIOx_CRL.CNFx.bit0 = 0 } } else { if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) == 0) { // MODE bits = Input mode gpio->CRH |= (0x08 << ((pin_index % 8) * 4)); // Set pull-up / pull-down gpio->CRH &= ~(0x08 << (((pin_index % 8) * 4)-1)); // ENSURES GPIOx_CRH.CNFx.bit0 = 0 } } // ENSURES GPIOx_ODR pull up/down is properly setup if(mode == PullUp) gpio->ODR |= (0x01 << (pin_index)); // Set pull-up else gpio->ODR &= ~(0x01 << (pin_index)); // Set pull-down break; case OpenDrain: // Set open-drain for Output mode (General Purpose or Alternate Function) if (pin_index < 8) { if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain } } else { if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain } } break; default: break; } }
/** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; __IO uint32_t* gpio_reg_hl;//gpio register depends on bit index (high or low) uint32_t shift; if (pin_index < 8) { shift = (pin_index * 4); gpio_reg_hl = &(gpio->CRL); } else { shift = (pin_index % 8) * 4; gpio_reg_hl = &(gpio->CRH); } // Configure open-drain and pull-up/down switch (mode) { case PullNone: break; case PullUp: case PullDown: // Set pull-up / pull-down for Input mode if ((*gpio_reg_hl & (0x03 << shift)) == 0) { // MODE bits = Input mode *gpio_reg_hl |= (0x08 << shift); // Set pull-up / pull-down *gpio_reg_hl &= ~(0x04 << shift); // ENSURES GPIOx_CRL.CNFx.bit0 = 0 } // Now it's time to setup properly if pullup or pulldown. This is done in ODR register: // set pull-up => bit=1, set pull-down => bit = 0 if (mode == PullUp) { gpio->ODR |= (0x01 << (pin_index)); // Set pull-up } else{ gpio->ODR &= ~(0x01 << (pin_index)); // Set pull-down } break; case OpenDrain: // Set open-drain for Output mode (General Purpose or Alternate Function) if ((*gpio_reg_hl & (0x03 << shift)) > 0) { // MODE bits = Output mode *gpio_reg_hl |= (0x04 << shift); // Set open-drain } break; default: break; } }
/** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { #if 0 ASF_assert(pin != (PinName)NC); GPIO_InitTypeDef GPIO_InitStructure; uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Configure open-drain and pull-up/down switch (mode) { case AnalogInput: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; break; case Floating: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; break; case PullUp: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; break; case PullDown: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; break; case OpenDrain: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; break; case PushPullOutput: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; break; case OpenDrainOutputAF: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; break; case PushPullOutputAF: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; break; default: D1_printf("PINMAP: Bad GPIO Mode: %d\r\n", mode); break; } // Configure GPIO GPIO_InitStructure.GPIO_Pin = (uint16_t)pin_index; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(gpio, &GPIO_InitStructure); #endif }
void gpio_init(gpio_t *obj, PinName pin) { if (pin == NC) return; uint32_t port_index = STM_PORT(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; obj->reg_clr = &gpio->BRR; }
/** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2)); }
void SET_OUTPUT(PinName pin) { if (pin == (PinName)NC) return; uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); Set_GPIO_Clock((PortName)port_index); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index); GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIO_ADDR[port_index], &GPIO_InitStructure); }
/** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { MBED_ASSERT(pin != (PinName)NC); GPIO_InitTypeDef GPIO_InitStructure; uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Configure open-drain and pull-up/down switch (mode) { case PullNone: return; case PullUp: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; break; case PullDown: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; break; case OpenDrain: if (pin_index < 8) { if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain } } else { if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain } } return; default: break; } // Configure GPIO GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(gpio, &GPIO_InitStructure); }
/** * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { if (pin == NC) return; // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t otype = STM_PIN_OTYPE(data); uint32_t pupd = STM_PIN_PUPD(data); uint32_t afnum = STM_PIN_AFNUM(data); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Configure Alternate Function // Warning: Must be done before the GPIO is initialized if (afnum != 0xFF) { GPIO_PinAFConfig(gpio, (uint16_t)pin_index, afnum); } // Configure GPIO GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); GPIO_InitStructure.GPIO_Mode = (GPIOMode_TypeDef)mode; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStructure.GPIO_OType = (GPIOOType_TypeDef)otype; GPIO_InitStructure.GPIO_PuPd = (GPIOPuPd_TypeDef)pupd; GPIO_Init(gpio, &GPIO_InitStructure); // *** TODO *** // Disconnect JTAG-DP + SW-DP signals. // Warning: Need to reconnect under reset //if ((pin == PA_13) || (pin == PA_14)) { // //} //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { // //} }
/** * Configure pin (input, output, alternate function or analog) + output speed + AF */ void pin_function(PinName pin, int data) { MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t afnum = STM_PIN_AFNUM(data); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Enable AFIO clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // Configure Alternate Function // Warning: Must be done before the GPIO is initialized if ((afnum > 0) && (afnum < AF_NUM)) { GPIO_PinRemapConfig(AF_mapping[afnum], ENABLE); } // Configure GPIO GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); GPIO_InitStructure.GPIO_Mode = (GPIOMode_TypeDef)mode; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(gpio, &GPIO_InitStructure); // Disconnect JTAG-DP + SW-DP signals. // Warning: Need to reconnect under reset if ((pin == PA_13) || (pin == PA_14)) { GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); } if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); } }
void gpio_init(gpio_t *obj, PinName pin) { obj->pin = pin; if (pin == (PinName)NC) { return; } uint32_t port_index = STM_PORT(pin); // Enable GPIO clock GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index); // Fill GPIO object structure for future use obj->mask = gpio_set(pin); obj->gpio = gpio; obj->ll_pin = ll_pin_defines[STM_PIN(obj->pin)]; obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; #ifdef GPIO_IP_WITHOUT_BRR obj->reg_clr = &gpio->BSRR; #else obj->reg_clr = &gpio->BRR; #endif }
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) { if (pin == NC) return; uint32_t port_index = STM_PORT(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRRL; obj->reg_clr = &gpio->BSRRH; // Configure GPIO if (direction == PIN_OUTPUT) { pin_function(pin, STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); } else { // PIN_INPUT pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); } }
/* Internal function for setting the gpiomode/function * without changing Pull mode */ void pin_function_gpiomode(PinName pin, uint32_t gpiomode) { /* Read current pull state from HW to avoid over-write*/ uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index); uint32_t pull = PullNone; __IO uint32_t* gpio_reg_hl;//gpio register depends on bit index (high or low) uint32_t shift; if (pin_index < 8) { shift = (pin_index * 4); gpio_reg_hl = &(gpio->CRL); } else { shift = (pin_index % 8) * 4; gpio_reg_hl = &(gpio->CRH); } /* Check if pull/pull down is active */ if ((!(*gpio_reg_hl & (0x03 << shift))) // input && (!!(*gpio_reg_hl & (0x08 << shift))) // pull-up / down && (!(*gpio_reg_hl & (0x04 << shift)))) { // GPIOx_CRL.CNFx.bit0 = 0 if (!!(gpio->ODR & (0x01 << pin_index))) { pull = PullUp; } else { pull = PullDown; } } else { //output if (!!(*gpio_reg_hl & (0x04 << shift))) { //open drain pull = OpenDrain; } } /* Then re-use global function for updating the mode part*/ pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0)); }
/** * @brief DAC MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param hdac: DAC handle pointer * @retval None */ void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac) { GPIO_InitTypeDef GPIO_InitStruct; GPIO_TypeDef *port; UNUSED(hdac); /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO clock ****************************************/ port = set_GPIO_Port_Clock(STM_PORT(g_current_pin)); /* DAC Periph clock enable */ #ifdef __HAL_RCC_DAC1_CLK_ENABLE __HAL_RCC_DAC1_CLK_ENABLE(); #endif #ifdef __HAL_RCC_DAC_CLK_ENABLE __HAL_RCC_DAC_CLK_ENABLE(); #endif /*##-2- Configure peripheral GPIO ##########################################*/ /* DAC Channel1 GPIO pin configuration */ GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin); GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(port, &GPIO_InitStruct); }
/** * @brief SPI initialization function * @param obj : pointer to spi_t structure * @param speed : spi output speed * @param mode : one of the spi modes * @param msb : set to 1 in msb first * @retval None */ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb) { if(obj == NULL) return; SPI_HandleTypeDef *handle = &(obj->handle); GPIO_InitTypeDef GPIO_InitStruct; GPIO_TypeDef *port; uint32_t spi_freq = 0; // Determine the SPI to use SPI_TypeDef *spi_mosi = pinmap_peripheral(obj->pin_mosi, PinMap_SPI_MOSI); SPI_TypeDef *spi_miso = pinmap_peripheral(obj->pin_miso, PinMap_SPI_MISO); SPI_TypeDef *spi_sclk = pinmap_peripheral(obj->pin_sclk, PinMap_SPI_SCLK); SPI_TypeDef *spi_ssel = pinmap_peripheral(obj->pin_ssel, PinMap_SPI_SSEL); /* Pins MOSI/MISO/SCLK must not be NP. ssel can be NP. */ if(spi_mosi == NP || spi_miso == NP || spi_sclk == NP) { printf("ERROR: at least one SPI pin has no peripheral\n"); return; } SPI_TypeDef *spi_data = pinmap_merge_peripheral(spi_mosi, spi_miso); SPI_TypeDef *spi_cntl = pinmap_merge_peripheral(spi_sclk, spi_ssel); obj->spi = pinmap_merge_peripheral(spi_data, spi_cntl); // Are all pins connected to the same SPI instance? if(obj->spi == NP) { printf("ERROR: SPI pins mismatch\n"); return; } // Configure the SPI pins if (obj->pin_ssel != NC) { handle->Init.NSS = SPI_NSS_HARD_OUTPUT; } else { handle->Init.NSS = SPI_NSS_SOFT; } /* Fill default value */ handle->Instance = obj->spi; handle->Init.Mode = SPI_MODE_MASTER; spi_freq = spi_getClkFreqInst(obj->spi); if(speed >= (spi_freq/SPI_SPEED_CLOCK_DIV2_MHZ)) { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; } else if(speed >= (spi_freq/SPI_SPEED_CLOCK_DIV4_MHZ)) { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; } else if (speed >= (spi_freq/SPI_SPEED_CLOCK_DIV8_MHZ)) { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; } else if (speed >= (spi_freq/SPI_SPEED_CLOCK_DIV16_MHZ)) { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; } else if (speed >= (spi_freq/SPI_SPEED_CLOCK_DIV32_MHZ)) { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; } else if (speed >= (spi_freq/SPI_SPEED_CLOCK_DIV64_MHZ)) { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; } else if (speed >= (spi_freq/SPI_SPEED_CLOCK_DIV128_MHZ)) { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; } else if (speed >= (spi_freq/SPI_SPEED_CLOCK_DIV256_MHZ)) { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; } else { handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; } handle->Init.Direction = SPI_DIRECTION_2LINES; if((mode == SPI_MODE_0)||(mode == SPI_MODE_2)) { handle->Init.CLKPhase = SPI_PHASE_1EDGE; } else { handle->Init.CLKPhase = SPI_PHASE_2EDGE; } if((mode == SPI_MODE_0)||(mode == SPI_MODE_1)) { handle->Init.CLKPolarity = SPI_POLARITY_LOW; } else { handle->Init.CLKPolarity = SPI_POLARITY_HIGH; } handle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; handle->Init.CRCPolynomial = 7; handle->Init.DataSize = SPI_DATASIZE_8BIT; if(msb == 0) { handle->Init.FirstBit = SPI_FIRSTBIT_LSB; } else { handle->Init.FirstBit = SPI_FIRSTBIT_MSB; } handle->Init.TIMode = SPI_TIMODE_DISABLE; #if defined(STM32F0xx) || defined(STM32F3xx) || defined(STM32F7xx) || defined(STM32L4xx) handle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE; #endif if(obj->pin_mosi != NC) { port = set_GPIO_Port_Clock(STM_PORT(obj->pin_mosi)); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_mosi); GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI)); GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI)); GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; #ifdef STM32F1xx pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI))); #else GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI)); #endif /* STM32F1xx */ HAL_GPIO_Init(port, &GPIO_InitStruct); } if(obj->pin_miso != NC) { port = set_GPIO_Port_Clock(STM_PORT(obj->pin_miso)); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_miso); GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_miso,PinMap_SPI_MISO)); GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_miso,PinMap_SPI_MISO)); GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; #ifdef STM32F1xx pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_miso,PinMap_SPI_MISO))); #else GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_miso,PinMap_SPI_MISO)); #endif /* STM32F1xx */ HAL_GPIO_Init(port, &GPIO_InitStruct); } if(obj->pin_sclk != NC) { port = set_GPIO_Port_Clock(STM_PORT(obj->pin_sclk)); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_sclk); GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK)); /* * According the STM32 Datasheet for SPI peripheral we need to PULLDOWN * or PULLUP the SCK pin according the polarity used. */ if(handle->Init.CLKPolarity == SPI_POLARITY_LOW) { GPIO_InitStruct.Pull = GPIO_PULLDOWN; } else { GPIO_InitStruct.Pull = GPIO_PULLUP; } GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; #ifdef STM32F1xx pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK))); #else GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK)); #endif /* STM32F1xx */ HAL_GPIO_Init(port, &GPIO_InitStruct); } if(obj->pin_ssel != NC) { port = set_GPIO_Port_Clock(STM_PORT(obj->pin_ssel)); GPIO_InitStruct.Pin = STM_GPIO_PIN(obj->pin_ssel); GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL)); GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL)); GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; #ifdef STM32F1xx pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL))); #else GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL)); #endif /* STM32F1xx */ HAL_GPIO_Init(port, &GPIO_InitStruct); } #if defined SPI1_BASE // Enable SPI clock if (handle->Instance == SPI1) { __HAL_RCC_SPI1_CLK_ENABLE(); } #endif #if defined SPI2_BASE if (handle->Instance == SPI2) { __HAL_RCC_SPI2_CLK_ENABLE(); } #endif #if defined SPI3_BASE if (handle->Instance == SPI3) { __HAL_RCC_SPI3_CLK_ENABLE(); } #endif #if defined SPI4_BASE if (handle->Instance == SPI4) { __HAL_RCC_SPI4_CLK_ENABLE(); } #endif #if defined SPI5_BASE if (handle->Instance == SPI5) { __HAL_RCC_SPI5_CLK_ENABLE(); } #endif #if defined SPI6_BASE if (handle->Instance == SPI6) { __HAL_RCC_SPI6_CLK_ENABLE(); } #endif HAL_SPI_Init(handle); /* In order to set correctly the SPI polarity we need to enable the peripheral */ __HAL_SPI_ENABLE(handle); }
bool READ(PinName pin) { return ((GPIO_ADDR[STM_PORT(pin)]->IDR & (1 << STM_PIN(pin))) != (uint32_t)GPIO_PIN_RESET)? true : false; }
/** * Configure pin (input, output, alternate function or analog) + output speed + AF */ void pin_function(PinName pin, int data) { MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t pupd = STM_PIN_PUPD(data); uint32_t afnum = STM_PIN_AFNUM(data); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Enable AFIO clock __HAL_RCC_AFIO_CLK_ENABLE(); // Configure Alternate Function // Warning: Must be done before the GPIO is initialized if (afnum > 0) { switch (afnum) { case 1: // Remap SPI1 __HAL_AFIO_REMAP_SPI1_ENABLE(); break; case 2: // Remap I2C1 __HAL_AFIO_REMAP_I2C1_ENABLE(); break; case 3: // Remap USART1 __HAL_AFIO_REMAP_USART1_ENABLE(); break; case 4: // Remap USART2 __HAL_AFIO_REMAP_USART2_ENABLE(); break; case 5: // Partial Remap USART3 __HAL_AFIO_REMAP_USART3_PARTIAL(); break; case 6: // Partial Remap TIM1 __HAL_AFIO_REMAP_TIM1_PARTIAL(); break; case 7: // Partial Remap TIM3 __HAL_AFIO_REMAP_TIM3_PARTIAL(); break; case 8: // Full Remap TIM2 __HAL_AFIO_REMAP_TIM2_ENABLE(); break; case 9: // Full Remap TIM3 __HAL_AFIO_REMAP_TIM3_ENABLE(); break; default: break; } } // Configure GPIO GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index); GPIO_InitStructure.Mode = gpio_mode[mode]; GPIO_InitStructure.Pull = pupd; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(gpio, &GPIO_InitStructure); // Disconnect JTAG-DP + SW-DP signals. // Warning: Need to reconnect under reset if ((pin == PA_13) || (pin == PA_14)) { __HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled } if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { __HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled } }
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) { IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; uint32_t irq_index; if (pin == NC) return -1; uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Select irq number and interrupt routine switch (pin_index) { case 0: irq_n = EXTI0_IRQn; vector = (uint32_t)&gpio_irq0; irq_index = 0; break; case 1: irq_n = EXTI1_IRQn; vector = (uint32_t)&gpio_irq1; irq_index = 1; break; case 2: irq_n = EXTI2_IRQn; vector = (uint32_t)&gpio_irq2; irq_index = 2; break; case 3: irq_n = EXTI3_IRQn; vector = (uint32_t)&gpio_irq3; irq_index = 3; break; case 4: irq_n = EXTI4_IRQn; vector = (uint32_t)&gpio_irq4; irq_index = 4; break; case 5: case 6: case 7: case 8: case 9: irq_n = EXTI9_5_IRQn; vector = (uint32_t)&gpio_irq5; irq_index = 5; break; case 10: case 11: case 12: case 13: case 14: case 15: irq_n = EXTI15_10_IRQn; vector = (uint32_t)&gpio_irq6; irq_index = 6; break; default: error("InterruptIn error: pin not supported.\n"); return -1; } // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); // Configure GPIO pin_function(pin, STM_PIN_DATA(STM_MODE_IT_FALLING, GPIO_NOPULL, 0)); // Enable EXTI interrupt NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); // Save informations for future use obj->irq_n = irq_n; obj->irq_index = irq_index; obj->event = EDGE_NONE; obj->pin = pin; channel_ids[irq_index] = id; channel_gpio[irq_index] = gpio_add; channel_pin[irq_index] = pin_index; irq_handler = handler; return 0; }
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) { IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; uint32_t irq_index; if (pin == NC) return -1; uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Select irq number and interrupt routine if ((pin_index == 0) || (pin_index == 1)) { irq_n = EXTI0_1_IRQn; vector = (uint32_t)&gpio_irq0; irq_index = 0; } else if ((pin_index == 2) || (pin_index == 3)) { irq_n = EXTI2_3_IRQn; vector = (uint32_t)&gpio_irq1; irq_index = 1; } else if ((pin_index > 3) && (pin_index < 16)) { irq_n = EXTI4_15_IRQn; vector = (uint32_t)&gpio_irq2; irq_index = 2; } else { error("InterruptIn error: pin not supported.\n"); return -1; } // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); // Enable SYSCFG clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Connect EXTI line to pin SYSCFG_EXTILineConfig(port_index, pin_index); // Configure EXTI line EXTI_InitTypeDef EXTI_InitStructure; EXTI_InitStructure.EXTI_Line = (uint32_t)(1 << pin_index); EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); // Enable and set EXTI interrupt to the lowest priority NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = irq_n; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); // Save informations for future use obj->irq_n = irq_n; obj->irq_index = irq_index; obj->event = EDGE_NONE; channel_ids[irq_index] = id; channel_gpio[irq_index] = gpio_add; channel_pin[irq_index] = pin_index; irq_handler = handler; return 0; }
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) { IRQn_Type irq_n = (IRQn_Type)0; uint32_t vector = 0; uint32_t irq_index; if (pin == NC) return -1; uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); // Select irq number and interrupt routine switch (pin_index) { case 0: irq_n = EXTI0_IRQn; vector = (uint32_t)&gpio_irq0; irq_index = 0; break; case 1: irq_n = EXTI1_IRQn; vector = (uint32_t)&gpio_irq1; irq_index = 1; break; case 2: irq_n = EXTI2_TS_IRQn; vector = (uint32_t)&gpio_irq2; irq_index = 2; break; case 3: irq_n = EXTI3_IRQn; vector = (uint32_t)&gpio_irq3; irq_index = 3; break; case 4: irq_n = EXTI4_IRQn; vector = (uint32_t)&gpio_irq4; irq_index = 4; break; case 5: case 6: case 7: case 8: case 9: irq_n = EXTI9_5_IRQn; vector = (uint32_t)&gpio_irq5; irq_index = 5; break; case 10: case 11: case 12: case 13: case 14: case 15: irq_n = EXTI15_10_IRQn; vector = (uint32_t)&gpio_irq6; irq_index = 6; break; default: error("This pin is not supported with InterruptIn.\n"); return -1; } // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); // Enable SYSCFG clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Connect EXTI line to pin SYSCFG_EXTILineConfig(port_index, pin_index); // Configure EXTI line EXTI_InitTypeDef EXTI_InitStructure; EXTI_InitStructure.EXTI_Line = pin_index; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); // Enable and set EXTI interrupt to the lowest priority NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = irq_n; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); // Save informations for future use obj->irq_n = irq_n; obj->irq_index = irq_index; obj->event = EDGE_NONE; channel_ids[irq_index] = id; channel_gpio[irq_index] = gpio_add; channel_pin[irq_index] = pin_index; irq_handler = handler; return 0; }
/** * @brief ADC MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param hadc: ADC handle pointer * @retval None */ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) { GPIO_InitTypeDef GPIO_InitStruct; GPIO_TypeDef *port; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC Periph clock enable */ if(hadc->Instance == ADC1) { #ifdef __HAL_RCC_ADC1_CLK_ENABLE __HAL_RCC_ADC1_CLK_ENABLE(); #endif #ifdef __HAL_RCC_ADC12_CLK_ENABLE __HAL_RCC_ADC12_CLK_ENABLE(); #endif } #ifdef ADC2 else if(hadc->Instance == ADC2) { #ifdef __HAL_RCC_ADC2_CLK_ENABLE __HAL_RCC_ADC2_CLK_ENABLE(); #endif #ifdef __HAL_RCC_ADC12_CLK_ENABLE __HAL_RCC_ADC12_CLK_ENABLE(); #endif } #endif #ifdef ADC3 else if(hadc->Instance == ADC3) { #ifdef __HAL_RCC_ADC3_CLK_ENABLE __HAL_RCC_ADC3_CLK_ENABLE(); #endif #ifdef __HAL_RCC_ADC34_CLK_ENABLE __HAL_RCC_ADC34_CLK_ENABLE(); #endif } #endif #ifdef ADC4 else if(hadc->Instance == ADC4) { __HAL_RCC_ADC34_CLK_ENABLE(); } #endif #ifdef __HAL_RCC_ADC_CLK_ENABLE __HAL_RCC_ADC_CLK_ENABLE(); #endif /* For STM32F1xx, ADC prescaler is confgured in SystemClock_Config (variant.cpp) */ #if defined(__HAL_RCC_ADC_CONFIG) && !defined(STM32F1xx) /* ADC Periph interface clock configuration */ __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); #endif /* Enable GPIO clock ****************************************/ port = set_GPIO_Port_Clock(STM_PORT(g_current_pin)); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC Channel GPIO pin configuration */ GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin); #ifdef GPIO_MODE_ANALOG_ADC_CONTROL GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL; #else GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; #endif GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(port, &GPIO_InitStruct); }