Download presentation
Presentation is loading. Please wait.
1
PRJ2UC: Microcontrollers
workshop 3 Std. Periph. Driver Lib.
2
Today Standard Peripheral Driver Library Blinky with STM32F0-Discovery LED & PB functions USART with Std. Periph. Driver Lib.
3
Standard Peripheral Drivers Library
Today Standard Peripheral Drivers Library Blinky with STM32F0-Discovery LED & PB functions USART with Std. Periph. Driver Lib.
4
Cortex Microcontroller Software Interface Standard (CMSIS)
SRAM main application USART GPIO ??? drivers CMSIS STM32F05x Processor & Peripherals
5
Standard Peripheral Drivers Library
SRAM main application STM32F0-Discovery LED & PB functions STM Std Periph Lib GPIO STM Std Periph Lib MISC STM Std Periph Lib RCC STM Std Periph Lib SYSCFG STM Std Periph Lib EXTI CMSIS STM32F05x Processor & Peripherals
6
Today Standard Peripheral Drivers Library Blinky with STM32F0-Discovery LED & PB functions USART with Std. Periph. Driver Lib.
7
Two files: STM32F0 LED & PB functions stm32f0-discovery.c
stm32f0-discovery.h
8
STM32F0 LED & PB functions typedef enum { LED3 = 0, LED4 = 1
STM32F0_DISCOVERY_LOW_LEVEL_Exported_Types */ typedef enum { LED3 = 0, LED4 = 1 } Led_TypeDef; STM32F0_DISCOVERY_LOW_LEVEL_Exported_Functions void STM_EVAL_LEDInit(Led_TypeDef Led); void STM_EVAL_LEDOn(Led_TypeDef Led); void STM_EVAL_LEDOff(Led_TypeDef Led); void STM_EVAL_LEDToggle(Led_TypeDef Led); void STM_EVAL_PBInit(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode); uint32_t STM_EVAL_PBGetState(Button_TypeDef Button);
9
STM32F0 LED & PB functions - example
#include "stm32f0xx.h" #include "stm32f0_discovery.h" int main(void) { // Configure LED3 on STM32F0-Discovery STM_EVAL_LEDInit(LED3); while(1) STM_EVAL_LEDToggle(LED3); delay(SystemCoreClock/8); // Delay 1 sec. }
10
Standard Peripheral Drivers Library
SRAM main application STM32F0 LED & PB functions STM Std Periph Lib GPIO STM Std Periph Lib MISC STM Std Periph Lib RCC STM Std Periph Lib SYSCFG STM Std Periph Lib EXTI CMSIS STM32F05x Processor & Peripherals
11
StdLib. is used in stm32f0_discovery.c
/* Configures LED GPIO */ void STM_EVAL_LEDInit( Led_TypeDef Led ) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */ RCC_AHBPeriphClockCmd( GPIO_CLK[Led], ENABLE ); /* Configure the GPIO_LED pin */ GPIO_InitStructure.GPIO_Pin = GPIO_PIN[Led]; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIO_PORT[Led], &GPIO_InitStructure); } General use of StdLib.: 1. Declare peripheral specific struct(s) 2. Fill the struct(s) with parameters 3. Call function(s) with struct(s) as a parameter
12
GPIO stm32f0xx_gpio.c stm32f0xx_gpio.c ================================================================= ##### How to use this driver ##### ================================================================= [..] (#) Enable the GPIO AHB clock using RCC_AHBPeriphClockCmd() (#) Configure the GPIO pin(s) using GPIO_Init() Four possible configuration are available for each pin: (++) Input: Floating, Pull-up, Pull-down. (++) Output: Push-Pull (Pull-up, Pull-down or no Pull) Open Drain (Pull-up, Pull-down or no Pull). In output mode, the speed is configurable: Low, Medium, Fast orHigh. (++) Alternate Function: Push-Pull (Pull-up, Pull-down or no Pull) (++) Analog: required mode when a pin is to be used as ADC channel, DAC output or comparator input. (#) Peripherals alternate function: …
13
Assignment Use STM_EVAL_PBGetState() to toggle the blue LED each time the blue Push Button is pressed. Remember to first initialize the pushbutton. Change the 'STM32F0Discovery LED & PB functions' so that besides using LED3 and LED4 (see Led_TypeDef in stm32f0_discovery.h) to select a LED, you can also use LED_GREEN and LED_BLUE. E.g.: STM_EVAL_LEDToggle(LED_GREEN);
14
Assignment – extra Add a new red LED to the 'STM32F0Discovery LED & PB functions' (see files stm32f0_discovery.h and stm32f0_discovery.c): The LED is connected to PB0. The LED is defined as LED5 (and/or as LED_RED). The LED is on with a logic 1. Test your implementation (remember to use a resistor in series) using the following function calls: STM_EVAL_LEDInit(LED5); STM_EVAL_LEDOn(LED5); STM_EVAL_LEDOff(LED5); STM_EVAL_LEDToggle(LED5);
15
Today Standard Peripheral Drivers Library Blinky with STM32F0-Discovery LED & PB functions USART with Std. Periph. Driver Lib.
16
USART stm32f0xx_usart.c stm32f0xx_usart.c ================================================================= ##### How to use this driver ##### ================================================================= [..] (#) Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE) function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) function for USART2 and USART3. (#) According to the USART mode, enable the GPIO clocks using RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS, or and SCLK). (#) Peripheral's alternate function: (++) Connect the pin to the desired peripherals' Alternate Function (AF) using GPIO_PinAFConfig() function. (++) Configure the desired pin in alternate function by: GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF. (++) Select the type, pull-up/pull-down and output speed via GPIO_PuPd, GPIO_OType and GPIO_Speed members. (++) Call GPIO_Init() function. (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware flow control and Mode(Receiver/Transmitter) using the SPI_Init() function. (#) For synchronous mode, enable the clock and program the polarity, phase and last bit using the USART_ClockInit() function. (#) Enable the NVIC and the corresponding interrupt using the function USART_ITConfig() if you need to use interrupt mode. (#) When using the DMA mode: (++) Configure the DMA using DMA_Init() function. (++) Active the needed channel Request using USART_DMACmd() function. (#) Enable the USART using the USART_Cmd() function. (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
17
USART (I) int main(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; uint16_t c; // Initialize USART1 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Setup Tx- and Rx pin GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); 1. 2. 3.
18
USART (II) //fill with default values USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate = ; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); while(1) { // Transmit information: "Press a key: " while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){} USART_SendData(USART1, 'P'); // Etc. ... // Receive data while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){} c = USART_ReceiveData(USART1); 2. 3.
19
Use the example project to: Implement the following functions:
Assignment Use the example project to: Implement the following functions: void USART_SendString(USART_TypeDef* USARTx, char *p); void USART_ReceiveString(USART_TypeDef* USARTx, char *p); Extra Implement auto baud rate detection. Check the peripheral driver library for functions that can be used with respect to this feature.
20
Assignment: Use Std. Periph. Driver Library
Demonstrate next week per team: TIP: Use interrupts for pushbutton (and USART?) STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); Putty STM32F0-Discovery “blue on” Blue LED on “blue off” Blue LED off “blue toggle” Blue LED toggle “green n” with n=1..9 Green LED blink with n Hz STM32F0-Discovery Putty Blue pushbutton pressed “Blue pushbutton pressed”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.