Skip to content

Instantly share code, notes, and snippets.

@EmbeddedSam
Last active December 9, 2015 15:46
Show Gist options
  • Save EmbeddedSam/8bca0b63c212f62c6e37 to your computer and use it in GitHub Desktop.
Save EmbeddedSam/8bca0b63c212f62c6e37 to your computer and use it in GitHub Desktop.
/*----------------------------------------------------------------------------
* Name: GPIO_Toggle_No_Delay
* Purpose: Toggles a single GPIO pin continuously
* Author: Samuel Walsh <samuel.walsh@manchester.ac.uk>
*----------------------------------------------------------------------------
* This code configures GPIOA5 as an output and then continously toggles it
*----------------------------------------------------------------------------*/
#include <stdio.h>
#include "stm32f4xx.h" // Device header
/*----------------------------------------------------------------------------
* main
*----------------------------------------------------------------------------*/
int main (void) {
/*This code below just sets up the LED pin (GPIOA5) as an output with no clock setup
Instead of a TRIS,PORT and LAT we have multiple configuration registers, (MODE, Speed, Output Type etc)
First you configure the pin the same as the PIC, then you output data on the ODR (Output Data Register) and read data
from the IDR (Input Data Register). There are also Bit set registers, called BSRR's which can read and write but aren't used here.
For more information on GPIO see page 140 in Reference manual.
*/
RCC->AHB1ENR |= (1ul << 0);
/*Enables GPIOA clock, 0 = disable. See page 114 in Reference manual
Even though 1ul<<0 is just the same as 1, it is nice to see we are working on bit zero at a glance.*/
GPIOA->MODER &= ~((3ul << 2*5));
/* Each GPIO pin has 2 config bits in the MODE REGISTER (MODER) which is the equivelant of a TRIS register on a PIC
This line is just a bitmask which keeps everything in the register same and just zeroes the 2 GPIO5 config bits.
&= ~((3ul << 2*5)) is equivalent to 11111111111111111111001111111111 in binary. Notice how only the GPIO5 bits are zeroed.
Also we could have just wrote &= ~((3ul << 10)) but 2*5 allows us to see which GPIO pin we are working on at a glance(5) */
GPIOA->MODER |= ((1ul << 2*5));
/* Each GPIO Pin has 4 possible configurations which are as follows
00: Input (reset state)
01: General purpose output mode
10: Alternate function mode
11: Analog mode - see page 152 in the Reference manual.
Looking at the combinations above this line then just sets GPIO5 to 01 = Output.*/
GPIOA->OTYPER &= ~((1ul << 5));
/* This sets the type of output
0: Push-Pull
1: Open Drain
See page 152 of the reference manual */
GPIOA->OSPEEDR &= ~((3ul << 2*5));
//This is just doing the same masking that we did at the beginning with MODER
GPIOA->OSPEEDR |= ((1ul << 2*5));
/*These bits configure the I/O output speed.
00: Low speed
01: Medium speed
10: Fast speed
11: High speed*/
GPIOA->PUPDR &= ~((3ul << 2*5));
/*These bits configure the I/O pull-up or pull-down
00: No pull-up, pull-down
01: Pull-up
10: Pull-down
11: Reserved
See page 154 of the reference manual */
/* The pin is now configured as medium speed, push pull output */
while (1)
{
GPIOA->ODR = 32; //turn LED on, LED = GPIOA5 = (in binary) 100000 = 32; ODR = output data register
GPIOA->ODR = 0; // Turn LED off
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment