Key Moments from Getting Started to Giving Up Embedded Development

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

Key Moments from Getting Started to Giving Up Embedded Development

Compilation | strongerHuang

WeChat Public Account | Embedded Column

The process from getting started to giving up is a torturous one, once you get started, this torturous process will gradually ease. So, what does it mean to have gotten started?
In embedded development, I believe that being able to independently complete a “light control” project is considered getting started. So, this article will show you the differences between51STM32andLinux light control.

51 Light Control

The 51 light 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, it usually involves directly manipulating 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 Light Control

Compared to 51 light control, STM32 light control is a bit more difficult because STM32 has more peripheral resources, and the startup files are more complex. Many beginners see this and directly give up.
In fact, it’s quite simple. Below, I will show you how to light up using registers and standard peripheral libraries, and you will understand.
Preparation:
  • STM32 Development Board

  • Keil MDKandST-LINK Utility Download Software

1. Register Version
Directly manipulating registers requires a deep understanding of the meaning of each bit in each register (it is not recommended for beginners to start learning registers), and the source code looks quite extensive:
#include "stm32f4xx.h"
/* Main function */
int main(void){
 /* Enable GPIOH clock, always enable its clock before 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);
 /* Set BR10 of GPIOH BSRR register to 1 to output low level */
 GPIOH_BSRR |= (1<<16<<10);  // Light up
 while (1);}
2. Standard Peripheral Library Version
The standard peripheral library is where ST has already encapsulated the registers, and you can directly call the function interface.
#include "stm32f10x.h"
/* LED clock port and 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);                       // Pull the LED port high, turn off LED}
int main(){
 LED_Init();  GPIO_ResetBits(LED_PORT,GPIO_Pin_0);// Light up
 while(1);}

Linux Light Control

Linux light control is relatively more complex. Of course, if there are some pre-built environments, it becomes relatively simpler and easier. If you have to port the system step by step and write drivers, it can be very complicated.
1. Raspberry Pi
Here we take the [open source library wiringPi] as an example:
  1. Download U-boot source code, configure, compile;

  2. Download Linux kernel, configure, 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. View the circuit diagram to find the corresponding pin for the LED, the program needs to use the pin number;

  6. Code, cross-compile;

  7. Download and run.

After the preparation work is done, the source code for light control is quite simple:
#include <wiringPi.h>
int main(void){
 wiringPiSetup();
 pinMode (7, OUTPUT);
 digitalWrite(7, HIGH);
 while(1);}
2. Linux Driver Light Control
This method is the most difficult among all light 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 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, 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;}

Finally

Light control is fundamental. If you are engaged in embedded development, I believe these light controls are the most basic first step.
The first step may be difficult, but once you have taken the first step, the next steps will become easier.
———— END ————

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

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

Key Moments from Getting Started to Giving Up Embedded Development

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

Leave a Comment