Differences in LED Control: 51, STM32, and Linux

Follow+Star Public Account Number, don’t miss wonderful content

Differences in LED Control: 51, STM32, and Linux

Compiled by | strongerHuang

WeChat Public Account | Embedded Column

The first “project” for embedded beginners is LED control. This article will show you the differences between 51, STM32, and Linux LED control.

51 LED Control

51 LED control is the first choice for many microcontroller beginners, and the difficulty is relatively low.
Preparation:
  • 51 Development Board (taking STC51 microcontroller as an example)

  • Keil C51, STC-ISP Download Software

For 51 microcontroller development, operations are usually performed directly on registers, for example, P1_0 corresponds to the LED IO port.
Source Code:
#include <reg51.h>
sbit LED = P1^0;
void main(){    LED = 0;
    while(1);}

STM32 LED Control

Compared to 51 LED control, STM32 LED control is slightly more difficult due to more peripheral resources and more complex startup files, which often causes many beginners to give up at first sight.
However, it is quite simple; below, we will show LED control using registers and standard peripheral libraries, and you will understand it.
Preparation:
  • STM32 Development Board

  • Keil MDK, ST-LINK Utility Download Software

1. Register Version
Directly operating registers requires a deep understanding of the meaning of each bit of each register (not recommended for beginners to start with registers), and the source code appears to be relatively lengthy:
#include "stm32f4xx.h"
/* Main function*/int main(void){    /* Enable GPIOH clock, always enable the clock first when using peripherals*/    RCC_AHB1ENR |= (1<<7);
    /* LED port initialization */    /*GPIOH MODER10 clear*/    GPIOH_MODER &= ~( 0x03<< (2*10));
    /*PH10 MODER10 = 01b output mode*/    GPIOH_MODER |= (1<<2*10);
    /*GPIOH OTYPER10 clear*/    GPIOH_OTYPER &= ~(1<<1*10);
    /*PH10 OTYPER10 = 0b push-pull mode*/    GPIOH_OTYPER |= (0<<1*10);
    /*GPIOH OSPEEDR10 clear*/    GPIOH_OSPEEDR &= ~(0x03<<2*10);
    /*PH10 OSPEEDR10 = 0b speed 2MHz*/    GPIOH_OSPEEDR |= (0<<2*10);
    /*GPIOH PUPDR10 clear*/    GPIOH_PUPDR &= ~(0x03<<2*10);
    /*PH10 PUPDR10 = 01b pull-up mode*/    GPIOH_PUPDR |= (1<<2*10);
    /*PH10 BSRR register's BR10 set to 1 to output low level*/    GPIOH_BSRR |= (1<<16<<10);  // LED on
    while (1);}
2. Standard Peripheral Library Version
The standard peripheral library is a wrapper around the registers provided by ST, allowing you to call function interfaces directly.
#include "stm32f10x.h"
/*  LED clock port, pin definitions*/#define LED_PORT       GPIOC   #define LED_PIN       GPIO_Pin_0#define LED_PORT_RCC            RCC_APB2Periph_GPIOC
void LED_Init(){    GPIO_InitTypeDef GPIO_InitStructure;        // Define structure variable
    RCC_APB2PeriphClockCmd(LED_PORT_RCC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = LED_PIN;                // Select the IO port you want to set    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      // Set push-pull output mode    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;       // Set transmission speed    GPIO_Init(LED_PORT,&GPIO_InitStructure);              // Initialize GPIO 
    GPIO_SetBits(LED_PORT, LED_PIN);                       // Set LED port high, turn off LED}
int main(){    LED_Init();  GPIO_ResetBits(LED_PORT,GPIO_Pin_0);// LED on
    while(1);}

Linux LED Control

Linux LED control is relatively more complex. Of course, if you have a pre-configured environment, it becomes simpler and easier. If you step by step port the system and write drivers, it will be very complex.
1. Raspberry Pi
We will take the wiringPi open-source library as an example:
  1. Download U-boot source code, configure, and compile;

  2. Download Linux kernel, configure, and compile (generally, development boards will have ready-made configuration files);

  3. Create a root file system; (for the above three steps, if you lack a certain Linux foundation, you can use one-click burning)

  4. Port the open-source library WiringPi;

  5. Check the circuit diagram to find the pin corresponding to the LED; the program needs to use the pin number;

  6. Code and cross-compile;

  7. Download and run.

After preparation, the source code for LED control is relatively simple:
#include <wiringPi.h>
int main(void){    wiringPiSetup() ;    pinMode (7, OUTPUT);    digitalWrite(7, HIGH);    while(1);}
2. Linux Driver LED Control
Among all LED control methods, this method is the most difficult, covering embedded development from upper-level applications to lower-level drivers. The steps involve driver code writing, Linux kernel module addition, operating system porting, and Linux application programming.
Here is a classic LED driver source code for mini2440:
#include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/ioctl.h>#include <linux/gpio.h> #include <mach/regs-gpio.h> #include "led.h" 
static int led_open(struct inode *inode, struct file *file){      s3c2410_gpio_cfgpin(S3C2410_GPB(5), S3C2410_GPIO_OUTPUT);    s3c2410_gpio_setpin(S3C2410_GPB(5), 1);       return 0;} 
static int led_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){      switch (cmd) {          case LED_ON:                s3c2410_gpio_setpin(S3C2410_GPB(5), 0);                return 0;          case LED_OFF:                s3c2410_gpio_setpin(S3C2410_GPB(5), 1);                return 0;          default:                return -EINVAL;          }} 
static struct file_operations led_fops = {      .owner = THIS_MODULE,      .open = led_open,      .ioctl = led_ioctl,}; 
static struct miscdevice led_misc = {      .minor = MISC_DYNAMIC_MINOR,      .name = "led",      .fops = &led_fops,}; 
static int led_init(void){      return misc_register(&led_misc);} 
static void led_exit(void){      misc_deregister(&led_misc);} 
MODULE_LICENSE("Dual BSD/GPL");module_init(led_init);module_exit(led_exit);
After writing the driver, here is the application layer code:
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/ioctl.h>#include <unistd.h>#include <stdio.h>#include "led.h" 
int main(void){            int fd;             fd = open("/dev/led", O_RDWR);             if (fd < 0) {                        printf("No such device!\n");                        return -1;            }             while (1) {                        ioctl(fd, LED_ON);                        sleep(1);                        ioctl(fd, LED_OFF);                        sleep(1);                }         
    close(fd);             return 0;}

Finally

LED control is fundamental; if you are engaged in embedded development, I believe these LED controls are the most basic first step.
The first step may be difficult, but once you take it, I believe the next steps will become easier.
———— END ————
Follow the public account and reply with Embedded DevelopmentMicrocontroller to read more related articles.
Reply “Add Group” to join the technical exchange group as per the rules, reply “1024” to see more content.

Differences in LED Control: 51, STM32, and Linux

Differences in LED Control: 51, STM32, and Linux

Click “Read Original” for more shares.

Leave a Comment