赞
踩
我之前写过一篇基于 stm32cubemx 快速使用 iic 接口 oled 的过程,里面有现成的 oled 接口,我们只要直接拿过来替换掉底层接口就行。
替换的接口位置如下位置:
SDK提供了两层接口封装,一层是芯片级的 driver 驱动,由原厂sdk提供:
另一层 open Harmony 级的驱动,是操作系统对原厂sdk的封装,主要为了兼容不同芯片开发:
原厂的 driver 接口更加细致,系统层的driver其实就是对原厂的一层封装,简单开发可以直接使用系统提供的driver接口,复杂开发则推荐研究一下原厂的接口,因为sdk不是很完善,不符合需求的可以直接改原厂的sdk。
我们简单驱动个i2c,使用系统封装的接口:
unsigned int IoTI2cInit(unsigned int id, unsigned int baudrate); unsigned int IoTI2cDeinit(unsigned int id); unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, const unsigned char *data, unsigned int dataLen); unsigned int IoTI2cRead(unsigned int id, unsigned short deviceAddr, unsigned char *data, unsigned int dataLen); unsigned int IoTI2cSetBaudrate(unsigned int id, unsigned int baudrate); |
用上面的读写接口替换掉 oled 轮子里面的 i2c 接口
在主函数中创建一个线程,调用 i2c、oled初始化接口,将初始化代码注册到系统运行回调中
#include <unistd.h> #include "stdio.h" #include "ohos_init.h" #include "cmsis_os2.h" #include "hi_io.h" #include "hi_i2c.h" #include "iot_errno.h" #include "iot_i2c.h" #include "iot_gpio.h" #define OLED_I2C_BAUDRATE 400000 static void iot_i2c_init(void) { IoTGpioInit(HI_IO_NAME_GPIO_0); IoTGpioInit(HI_IO_NAME_GPIO_1); //将引脚功能设置为 I2C 引脚 hi_io_set_func(HI_IO_NAME_GPIO_0, HI_IO_FUNC_GPIO_0_I2C1_SDA); hi_io_set_func(HI_IO_NAME_GPIO_1, HI_IO_FUNC_GPIO_1_I2C1_SCL); //初始化 I2C0 uint32_t ret = IoTI2cInit(HI_I2C_IDX_1, OLED_I2C_BAUDRATE); if (ret != IOT_SUCCESS) { printf("[OLED_TASK] Init i2c Fail. ret = %d\n", ret); } printf("[OLED_TASK] Init i2c succ.\n"); } void oled_i2c_init(void) { iot_i2c_init(); OLED_Init(); OLED_Clear(); } void *oled_demo(const char *arg) { (void)arg; char i = 0; char dat[10] = { 0 }; oled_i2c_init(); while (1) { printf("[OLED_TASK] i = %d!\n", i); OLED_ShowNum(10, 10, i++, 1, 8); osDelay(100); } } void oled_demo_init(void) { osThreadAttr_t attr; attr.name = "OLED_Task"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 2048; attr.priority = 26; if (osThreadNew((osThreadFunc_t)oled_demo, NULL, &attr) == NULL) { printf("[OLED_TASK] Falied to create OLED_TASK!\n"); } } SYS_RUN(oled_demo_init); |
编译后通过 uart 下载代码
hb build -f
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。