Differences Between 51, STM32, and Linux LED Control

Follow+Star Public Number, don’t miss out on exciting content

Differences Between 51, STM32, and Linux LED Control

Arrangement | strongerHuang

WeChat Public Account | Embedded Column

The first “project” for embedded beginners is LED control. In this article, we’ll explore the differences between 51, STM32, and Linux LED control.

51 LED Control

51 LED control is a popular choice for many microcontroller beginners, as it is relatively easy.
Preparation:
  • 51 Development Board (using STC51 microcontroller as an example)

  • Keil C51, STC-ISP Download Software

51 microcontroller development usually involves directly manipulating registers, such as P1_0 corresponding to the LED IO port.
Source Code:
#include <reg51.h>
sbit LED = P1^0;
void main(){ LED = 0;
 while(1);}
C

STM32 LED Control

Compared to 51 LED control, STM32 LED control is slightly more difficult due to the greater number of peripherals and the complexity of the startup files. Many beginners give up when they see it.
However, it is quite simple. Below, we will explore LED control through registers and the standard peripheral library, and you’ll understand.
Preparation:
  • STM32 Development Board

  • Keil MDKST-LINK Utility Download Software

1. Register Version
Directly manipulating registers requires a deep understanding of the meaning of each bit in each register (not recommended for beginners to learn registers initially), and the source code can look quite extensive:
#include "stm32f4xx.h"
/* Main function*/
int main(void){
 /* Enable GPIOH clock; when using peripherals, you must first enable its clock*/
 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, output low level*/
 GPIOH_BSRR |= (1<<16<<10);  // LED On
 while (1);}
C
2. Standard Peripheral Library Version
The standard peripheral library is one where ST has encapsulated the registers, allowing you to directly call function interfaces.
#include "stm32f10x.h"
/* LED clock port, pin definition*/
#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 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);                       // Pull LED port high to turn off LED}
int main(){
 LED_Init();  GPIO_ResetBits(LED_PORT,GPIO_Pin_0);// LED On
 while(1);}
C

Linux LED Control

Linux LED control is relatively more complex. Of course, if some pre-configured environments are available, it becomes simpler and easier. However, if you have to port the system step by step and write drivers, it can get quite complicated.
1. Raspberry Pi
Here, 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 the root file system; (if you lack a certain Linux foundation, you can use one-click burning for the above three steps)

  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, cross-compile;

  7. Download and run.

Once the preparations are done, the source code for LED control is relatively simple:
#include <wiringPi.h>
int main(void){
 wiringPiSetup();
 pinMode(7, OUTPUT);
 digitalWrite(7, HIGH);
 while(1);}
C
2. Linux Driver LED Control
This method has the highest difficulty level among all LED control methods, 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, I will share the 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);
C
After writing the driver, the application layer code is as follows:
#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;}
C

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 challenging, but once you take it, the next step will become easier.
———— END ————

Reply in the background Embedded Software Design and DevelopmentMicrocontroller』『Linux』 to read more related articles.

Welcome to follow my public account, reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Welcome to follow my video account:

Differences Between 51, STM32, and Linux LED Control

Click “Read the Original” to see more shares, feel free to share, bookmark, like, and view.

Leave a Comment