RT-Thread trip: AT24C02 IIC equipment

Project Creation

Development board I am using STM32F1 elite board punctual atoms, so when building the project configuration is as follows (I'm using pirated elite version, so the choice debugging tool when you want to select SWO mode, because the board did not even JTAG mode line):

Load the driver

Load simulation I2C driver

Because we want to test a device IIC, here we use the STM32 board of AT24C02 chip as the chip under test. STM32 hardware IIC because there are some flaws in the design of the beginning, so in general we are using software simulation IIC, where you need to load RTThread package supports soft IIC drive. Methods as below:

Double-click the Project Explorer "RT-thread Setting" title, as shown:

 Click "software simulation I2C" icon at the right end in the Software Center to add IIC drive.

Then select "Software simulation I2C" right "Detailed Configuration" option, select the option box on the right side of the pop-up "component" option box and "Using the I2C device drivers" options bar and its sub-Options bar "use GPIO simulation I2C "tick the options bar, as shown:

Download the driver code:

The section just opened the I2C driver software framework, the specific software I2C chip driver source code need to manually add their own path to drive the project file is located, you need to download the STM32 family of I2C driver software. Download .

After the download is complete, " drv_soft_i2c.c" and "  drv_soft_i2c.h to lower" These two files are placed in the project of "drivers" directory.

Load AT24CXX drive

Click the center of the package "Add it now" button will pop up online RTThread package center (as shown below). Which contains many of the components RTThread officially supported, you can download load for its own programs.

Because AT24CXX series is affiliated with the peripheral portion of the 24C02 we want to use, click the "peripheral" icon in the search bar and find AT24CXX, after finding can click Add, as shown:

添加完成之后IDE并不会立即就下载驱动代码,需要关闭或者“Ctrl+S”保存一次“RT-Thread settings”这个界面IDE才会下载并且将代码载入到工程。代码成功载入工程后会分别在drivers目录下生成“drv_soft_i2c.h”、“drv_soft_i2c.c”和packages目录下生成一个“AT24cxx-latest”文件夹,如下图所示:

这样整个代码环境就搭建完成了。


代码编写

在编写代码的时候需要注意两点:

  • I2C的设备地址都是7位,所以精英板的设备地址应该设置为0x50而不是0xA0。
  • 在使用AT24cxx驱动之前需要在“at24cxx.h”中设置EEPROM的型号,如下所示:

代码: 

/*
 * Copyright (c) 2006-2019, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-09-09     RT-Thread    first version
 */

#include <rtthread.h>
#include <board.h>
#include <rtdevice.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include "drv_soft_i2c.h"
#include "at24cxx.h"

const char *i2c_bus_name = "i2c1";

struct at24cxx_device *i2c_bus = RT_NULL;

/*
 * @discription:AT24Cxx设备的初始化
 * @param:无
 * @return:无
 */
void eeprom_init()
{
    /*初始化IIC设备*/
    int iRet = RT_NULL;
    iRet = rt_hw_i2c_init(i2c_bus_name,GET_PIN(B,6),GET_PIN(B,7));
    if(iRet == RT_EOK)
    {
        rt_kprintf("i2c设备初始化成功\r\n");
    }
    else
    {
        rt_kprintf("i2c设备初始化失败\r\n");
        return RT_ERROR;
    }
    i2c_bus = at24cxx_init(i2c_bus_name, 0x50);
    rt_kprintf("i2c_address:%x\r\n",i2c_bus->AddrInput);
    if (i2c_bus != RT_NULL)
    {
        rt_kprintf("成功读取设备\r\n");
    }
    else
    {
        rt_kprintf("获取设备失败\r\n");
        return RT_ERROR;
    }
}

/*
 * @discription:GPIO的初始化
 * @param:无
 * @return:无
 */
void gpio_init()
{
    /* set LED0 pin mode to output */
    rt_pin_mode(GET_PIN(B, 5), PIN_MODE_OUTPUT);
}

/*
 * @discription:主函数
 * @param:无
 * @return:无
 */
int main(void)
{
    static unsigned int flag = 0;
    gpio_init();
    eeprom_init();
    while (1)
    {
        flag = ~flag;
        rt_pin_write(GET_PIN(B,5), flag&0x01);
        rt_thread_mdelay(1000);
    }
    return RT_EOK;
}

 

发布了21 篇原创文章 · 获赞 12 · 访问量 1万+

Guess you like

Origin blog.csdn.net/Argon_Ghost/article/details/104161889
IIC