当前位置:网站首页>Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
2022-07-29 06:14:00 【Q-Stark】
Catalog
Preface
I learned before STM32 when , Have studied liteOS, Have a simple understanding of the kernel . After learning the kernel , This time, learn some drive subsystems ,GPIO,I2C, A serial port ,ADC Data collection, etc . Mainly understand the encapsulated interface function and how to call .
Related concepts are stm32 I summarized it when I studied .
GPIO
ADC And DAC
I2C
A serial port
More can go to HAL Library column . This paper mainly summarizes Hongmeng's API Interface
Chip pin diagram
First put a chip schematic , Easy to view pins 
File structure chart
The following referenced header files are in the folder under this path bearpi-hm_nano/ base / iot_hardware
frameworks Inside the folder is c Source file , It is the implementation file of interface function ;hals Define header files for hardware abstraction layer functions in ; We mainly quote interfaces/kits The header file under , This folder provides a more convenient interface .
One 、GPIO
API Interface
The file path is base/iot_hardware/interfaces/kits/wifiiot_lite
stay wifiiot_gpio.h The statement is made. GPIO function , For initialization GPIO. Interrupt related functions are also declared .
stay wifiiot_gpio_ex.h The statement is made. GPIO spread function , Used for setting up GPIO Some properties of .
stay wifiiot_pwm.h The statement is made. PWM Related interface functions .
| The interface name | Function description |
|---|---|
| GpioInit | initialization GPIO |
| GpioDeinit | Uninitialize GPIO |
| GpioSetDir | Set up GPIO Pin direction |
| GpioGetDir | obtain GPIO Pin direction |
| GpioSetOutputVal | Set up GPIO Pin output level value |
| GpioGetOutputVal | obtain GPIO Pin output level value |
| IoSetPull | Set up GPIO Pin pull-up |
| IoGetPull | obtain GPIO Pin pull-up |
| IoSetFunc | Set up GPIO Pin function |
| IoGetFunc | obtain GPIO Pin function |
| IoSetDriverStrength | Set up GPIO Driving ability |
| IoGetDriverStrength | obtain GPIO Driving ability |
Find corresponding GPIO Pin
When we use the development board as the driver , You need to look at the circuit diagram to find the corresponding pin , Configure the code accordingly .
GPIO Basic case introduction

LedTask() by LED Main task of lamp test , This task calls GpioInit() initialization GPIO, because LED The control pin of the lamp is connected to GPIO_2 On , So by calling IoSetFunc() and GpioSetDir() take GPIO_2 Set to normal GPIO The output mode of . Finally, interval in the dead cycle 1s Output GPIO_2 High and low levels of , Realization LED The phenomenon of light flashing
static void LedTask(void)
{
GpioInit();// initialization GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);// Set up GPIO_2 The reuse function of is common GPIO
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);// Set up GPIO_2 For output mode
while (1)
{
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);// Set up GPIO_2 The output high level is on LED The lamp
usleep(1000000);// Time delay 1s
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);// Set up GPIO_2 The output low level is off LED The lamp
usleep(1000000);// Time delay 1s
}
}
GPIO interrupt
| The interface name | Function description |
|---|---|
| GpioRegisterIsrFunc | Set up GPIO Pin interrupt function |
| GpioUnregisterIsrFunc | Cancel GPIO Pin interrupt function |
| GpioSetIsrMask | shielding GPIO Pin interrupt function |
| GpioSetIsrMode | Set up GPIO Pin interrupt trigger mode |
GPIO Introduction to interruption case

Through the key control LED The light is on and off . Here, press the key F1 For example , Key F1 The detection pin is connected with the main control chip GPIO_11 Connect , First by calling IoSetFunc() and GpioSetDir() take GPIO_11 Set to normal GPIO Input mode of . As can be seen from the previous schematic diagram , When the key is pressed ,GPIO_11 Will be pulled down to the ground , So we need to use IoSetPull() take GPIO_11 The pin is set to pull-up, i.e. high level , In this way, the level jump can be generated . Finally through GpioRegisterIsrFunc() Set the interrupt type to edge trigger , And it is triggered by the falling edge , When the key is pressed ,GPIO_11 Will change from high level to low level , Produce a drop , This will trigger the interrupt and call back F1_Pressed function . stay F1_Pressed Function LED Light operation .
static void F1_Pressed(char *arg)
{
(void) arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);
}
static void F2_Pressed(char *arg)
{
(void) arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);
}
static void ButtonExampleEntry(void)
{
GpioInit();
/***** initialization LED The lamp *****/
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);
/***** initialization F1 Key , Set as falling edge trigger interrupt *****/
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,F1_Pressed, NULL);
/***** initialization F2 Key , Set as falling edge trigger interrupt *****/
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,F2_Pressed, NULL);
}
PWM Output
| The interface name | Function description |
|---|---|
| PwmInit | initialization PWM |
| PwmDeinit | Uninitialize PWM |
| PwmStart | Output according to input parameters PWM |
| PwmStop | stop it PWM Output |
PWM Case introduction
This case will use onboard LED To verify GPIO Of PWM function , stay BearPi-HM_Nano Development board LED The connection circuit diagram of is shown in the basic part ,LED The control pin is connected with the main control chip GPIO_2 Connect , So you need to write software to control GPIO_2 Output PWM Wave to achieve the effect of breathing lamp . When the power is high, it is on , Low power usually goes out .
PWMTask() by PWM Test the main task , This task calls GpioInit() initialization GPIO, because LED The control pin of the lamp is connected to GPIO_2 On , So by calling IoSetFunc() take GPIO_2 Reuse as PWM function , And pass PwmInit() initialization PWM2 port , Finally, interval in the dead cycle 10us Output signals with different duty cycles PWM wave , To achieve the effect of breathing lamp .
static void PWMTask(void)
{
unsigned int i;
GpioInit();// initialization GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_PWM2_OUT);// Set up GPIO_2 The pin multiplexing function is PWM
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);// Set up GPIO_2 Pin is output mode
PwmInit(WIFI_IOT_PWM_PORT_PWM2);// initialization PWM2 port
while (1)
{
for (i = 0; i < 40000; i += 100)
{
PwmStart(WIFI_IOT_PWM_PORT_PWM2, i, 40000); // Output signals with different duty cycles PWM wave
usleep(10);
}
i = 0;
}
}
Two 、ADC sampling
ADC The principle of is summarized in the link of the preface is ok , You can check it out , Is to sample analog quantity , Convert to digital quantity .
API function
AdcRead(): From the specified... According to the input parameters ADC The channel reads a section of sampling data unsigned int AdcRead (WifiIotAdcChannelIndex channel, unsigned short * data, WifiIotAdcEquModelSel equModel, WifiIotAdcCurBais curBais, unsigned short rstCnt )
| Parameters | explain |
|---|---|
| channel | ADC passageway |
| data | Address pointer for storing read data |
| equModel | Represents the number of average algorithms |
| curBais | Indicates analog power control mode |
| rstCnt | Indicates the time count from reset to the beginning of the conversion |
ADC Case introduction
This case will use onboard user buttons F1 To simulate the GPIO Change of port voltage . See the chip manual GPIO_11 The corresponding is ADC Channel 5 , So you need to write software to read ADC Channel 5 The voltage of , When programming, first GPIO_11 Pull up , send GPIO_11 The voltage of is always at a high level , When the key is pressed GPIO_11 Grounding , here GPIO_11 The voltage of becomes 0 V.

Software function design
This function uses AdcRead() Function to read ADC_CHANNEL_5 The value of is stored in data in , WIFI_IOT_ADC_EQU_MODEL_8 Express 8 Sub average algorithm mode ,WIFI_IOT_ADC_CUR_BAIS_DEFAULT Indicates the default automatic recognition mode , Finally through data * 1.8 * 4 / 4096.0 Calculate the actual voltage value .
static float GetVoltage(void)
{
unsigned int ret;
unsigned short data;
ret = AdcRead(WIFI_IOT_ADC_CHANNEL_5, &data, WIFI_IOT_ADC_EQU_MODEL_8, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0xff);
if (ret != WIFI_IOT_SUCCESS)
{
printf("ADC Read Fail\n");
}
return (float)data * 1.8 * 4 / 4096.0; /* data * 1.8 * 4 / 4096.0: Convert code into voltage */
}
After compiling and burning the sample code , Press... On the development board RESET Key , View the log through the serial port assistant , When F1 The voltage collected when the key is not pressed is 3.3V about , When the key is pressed , The voltage becomes 0.2V about .
3、 ... and 、I2C Reading and writing NFC chip
API
| The interface name | Function description |
|---|---|
| I2cInit | initialization I2C |
| I2cDeinit | Uninitialize I2C |
| I2cWrite | Writes data to I2C equipment |
| I2cRead | Read data from the device |
| I2cWriteread | Composite communication , Send data to the device and receive the data response |
| I2cSetBaudrate | Set up I2C frequency |
I2C Case introduction
NFC The chip uses I2C agreement ,I2C_SCL And GPIO_0 Connect ,I2C_SDA And GPIO_1 Connect , So you need to write software to use GPIO_0 and GPIO_1 produce I2C Signal to control NFC chip .
Software function design
I2C Initialized code , First use IoSetFunc() Function will GPIO_0 Reuse as I2C1_SDA,GPIO_1 Reuse as I2C1_SCL. And then call I2cInit() Function initialization I2C1 port , Finally using I2cSetBaudrate() Function settings I2C1 The frequency of is 400kbps.
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA); // GPIO_0 Reuse as I2C1_SDA
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL); // GPIO_1 Reuse as I2C1_SCL
I2cInit(WIFI_IOT_I2C_IDX_1, 400000); /* baudrate: 400kbps */
I2cSetBaudrate(WIFI_IOT_I2C_IDX_1, 400000);
towards NFC The chip writes data , But you need to write 2 When it's a record , The first 2 The location of a record needs to be NDEFLastPos To define ; When writing is required 3 When it's a record , The first 2 And the first 3 The location of each record needs to be NDEFMiddlePos and NDEFLastPos To define .
ret=storeText(NDEFFirstPos, (uint8_t *)TEXT);
if(ret != 1)
{
printf("NFC Write Data Falied :%d ",ret);
}
ret=storeUrihttp(NDEFLastPos, (uint8_t *)WEB);
if(ret != 1)
{
printf("NFC Write Data Falied :%d ",ret);
}
Four 、UART Reading and writing
API
UartInit()unsigned int UartInit (WifiIotUartIdx id, const WifiIotUartAttribute * param, const WifiIotUartExtraAttr * extraAttr )
| Parameters | explain |
|---|---|
| id | UART Port number |
| param | Represent basic UART attribute |
| extraAttr | Represents an extension UART attribute |
UartWrite()int UartWrite (WifiIotUartIdx id, const unsigned char * data, unsigned int dataLen )
UartRead()int UartRead (WifiIotUartIdx id, unsigned char * data, unsigned int dataLen )
| Parameters | explain |
|---|---|
| id | UART Port number . |
| data | A pointer to the starting address of the data to be read and written |
| dataLen | Represents the length of the data |
UART Case introduction
This case will use BearPi-HM_Nano Development board E53 Interface UART As a test , As shown in the schematic diagram, section 18 and 19 The feet are TXD and RXD , Connected to the main control chip GPIO_6 and GPIO_5 , So when writing software, you need to GPIO_6 and GPIO_5 Reuse as TXD and RXD .
Software function design
UART Initialized code , In the first uart_attr This structure configures the baud rate 、 Data bits 、 Stop bit 、 Parity check bit , And then through UartInit() Function to serial port 1 To configure .
WifiIotUartAttribute uart_attr = {
.baudRate = 9600, /* baud_rate: 9600 */
.dataBits = 8, /* data_bits: 8bits */
.stopBits = 1,
.parity = 0,
};
/* Initialize uart driver */
ret = UartInit(WIFI_IOT_UART_IDX_1, &uart_attr, NULL);
if (ret != WIFI_IOT_SUCCESS) {
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
adopt UartWrite() Function in serial port 1 Send a string of data , And then through UartRead() Function returns all the data , And pass debug Serial port print out .
UartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data)); // Through serial port 1 send data
UartRead(WIFI_IOT_UART_IDX_1,uart_buff_ptr,UART_BUFF_SIZE); // Through serial port 1 receive data
printf("%s",uart_buff_ptr);
summary
The course is very simple , It's how to use it , The principles still need to be learned .
边栏推荐
- 扬尘噪声监控系统
- Design and implementation of QT learning notes data management system
- Hal library learning notes-10 overview of Hal library peripheral driver framework
- Tf.get in tensorflow_ Detailed explanation of variable() function
- Transfer joint matching for unsupervised domain adaptation
- 京微齐力:基于HMEP060的心率血氧模块开发(1:FPGA发送多位指令)
- Huawei cloud 14 day Hongmeng device development -day1 source code acquisition
- HAL库学习笔记- 8 串口通信之使用
- 基于msp430f2491的proteus仿真(实现流水灯)
- 物联网倾斜监测解决方案
猜你喜欢

HAL库学习笔记- 8 串口通信之概念

CNOOC, desktop cloud & network disk storage system application case

HAL学习笔记 - 7 定时器之高级定时器

Wechat built-in browser prohibits caching

Jianzhi core taocloud full flash SDS helps build high-performance cloud services

Reading papers on fake news detection (2): semi supervised learning and graph neural networks for fake news detection

Hal library learning notes-10 overview of Hal library peripheral driver framework

Chongqing Avenue cloud bank, as a representative of the software industry, was invited to participate in the signing ceremony of key projects in Yuzhong District

Huawei cloud 14 days Hongmeng device development -day1 environment construction

基于FPGA:运动目标检测(补充仿真结果,可用毕设)
随机推荐
ML自学笔记5
Transfer feature learning with joint distribution adaptation
CNOOC, desktop cloud & network disk storage system application case
HAL库学习笔记-14 ADC和DAC
Reading papers on false news detection (I): fake news detection using semi supervised graph revolutionary network
基于AD9850的多功能信号发生器
DP4301—SUB-1G高集成度无线收发芯片
Pytorch Basics (Introductory)
京微齐力:基于HMEP060的OLED字符显示(及FUXI工程建立演示)
ML17-神经网络实战
Beijing Baode & taocloud jointly build the road of information innovation
基于STM32开源:磁流体蓝牙音箱(包含源码+PCB)
Hal library learning notes-11 I2C
2021-06-10
1、 Pytorch Cookbook (common code Collection)
新能源充电桩后台管理系统平台
HAL学习笔记 - 7 定时器之基本定时器
HAL库学习笔记-11 I2C
1、 Usage of common loss function
2022春招——禾赛科技FPGA技术岗(一、二面,收集于:数字IC打工人及FPGA探索者)