CC2538 ZigBee I2C Sample code read-write at24c02d eeprom_ embedded hardware

Source: Internet
Author: User
at24c02d

at24c02d because it is not a standard I2C bus, the I2C 8-byte address bus select、read is not fully used, but the 5+3 external pin setting (A2,A1,A0) is used to determine which device to communicate with, and a I2C bus can mount up to 8 at24c02d, just 2 three times.

Do not understand why at24c02d I2C inside the sub address is called word address, is very strange, I am here to express all according to sub address to express, the same below.

at24c02d as CC2538 plug-in EEPROM, through the I2C bus and CC2538 communication, to achieve reading and writing, spent a long time has been uncertain, on the internet to find the nine of the Ten is what Gpio simulation I2C, my CC2538 itself has I2C interface, but also simulate what simulation.

began not to understand read and write at24c02d EEPROM, think and ordinary I2C device, write words, send slave address+ sub address+data, you can write, read words, send slave address+sub address, You can read the data of the specified address, but it is wrong.

As we all know, I2C has a starting bit, a start, a stop bit, a stop. Write, according to the EEPROM chip manual to the sequence diagram, that is, after the start, sent from the machine address slave addresses, send the address from the machine, you can not have stop, you must send sub addresses, and then can not have a stop, and then send data, Is the need to write the data, and finally send the stop, only to complete a write operation, really troublesome.

Read, there are three ways to read in the chip manual, which are current address read, Random address read, sequential read,

We first discuss random reading, I think that is random reading is not too accurate, should be read the specified address of the data to be accurate. A random read is actually to change the value of the data word address in the EEPROM by writing a location, and then to read the current address. is not directly sent slave address+sub address can be read. Before you read, you must first write the slave address+sub address, delay 20ms, and then start reading, reading, only need to give slave address can, do not need sub address, you can read the data. CC2538 I2C function Search the Internet, also can not find the CC2538 I2C related sample code, in addition to a master_slave_loopback.c example loop read and write I2C, anything else gpio analog I2C, or what firmware Foundation provides the sample code, none of which is really successful to use. It seems that the number of people using CC2538 is very small, most people use CC2530 with more than 10 20 nodes to meet the home application is finished.

Give the CC2538 I2C read-write EEPROM header file:

/*
  Atmel AT24C02 EEPROM Read and write
*/

#ifndef hal_eeprom_h
#define HAL_EEPROM_H

#include < stdint.h>

#ifdef __cplusplus
extern "C"
{
#endif/*
* PC.6->I2C_SCL *
pc.7- >I2C_SDA
* *
#define I2C_SCL             gpio_pin_6
#define I2C_SDA             gpio_pin_7
#define Gpio_ I2c_base       gpio_c_base

void haleeprominit (void);
uint32_t haleepromwritebyte (uint8 slaveaddr,uint32_t subaddr,uint32_t data);
uint32_t haleepromreadbyte (uint8 slaveaddr,uint32_t subaddr);
uint32_t Haleepromreadbyteex (uint8 slaveaddr,uint32_t subaddr);

#ifdef __cplusplus
}
#endif

#endif

Give the CC2538 I2C read-write EEPROM source files, you can refer to the use of I2C functions and EEPROM function

/**************************************************************************************************
 * INCLUDES * * Description:atmel INC at24c02d EEPROM chip write and read. For CC2538 CORTEX-M3 Core * * ************************************************************************************** /#include <string.h> #include "hal_types.h" #include "hal_eeprom.h" #include "hw_ioc.h" #include "hal _mcu.h "void Haleeprominit (void) {#if (Hal_eeprom = = TRUE)////Set the clocking to run directly from the Exter
    NAL Crystal/oscillator.
    (No ext 32k OSC, no internal OSC)//Sysctrlclockset (False, False, Sys_ctrl_sysdiv_32mhz);  
    Set IO clock to the same as system clock//Sysctrlioclockset (Sys_ctrl_sysdiv_32mhz);
    The I2C peripheral must is enabled before use.

    Sysctrlperipheralenable (SYS_CTRL_PERIPH_I2C); Do reset of I2C module//SYSCTRLPERIPHEralreset (SYS_CTRL_PERIPH_I2C);
    Configure I2C Physical Pins//GPIOPINTYPEI2C (Gpio_i2c_base, I2C_SCL);

    GPIOPINTYPEI2C (Gpio_i2c_base, I2C_SDA);
    Configure pins as peripheral input and output//iocpinconfigperiphinput (Gpio_i2c_base, I2C_SCL,IOC_I2CMSSCL);    
    Iocpinconfigperiphinput (Gpio_i2c_base, I2C_SDA,IOC_I2CMSSDA);
    Iocpinconfigperiphoutput (Gpio_i2c_base, I2C_SCL,IOC_MUX_OUT_SEL_I2C_CMSSCL);

    Iocpinconfigperiphoutput (Gpio_i2c_base, I2C_SDA,IOC_MUX_OUT_SEL_I2C_CMSSDA);

Data rate setting//fase:100kbps//true:400kbps I2CMASTERINITEXPCLK (Sysctrlclockget (), false); #endif}/* *write data to external EEPROM (at24c02d) * @param 7-bit the slave address @param 8-bit of sub-address @param b Yte to is write @param index of total EEPROM (0~255)/uint32_t haleepromwritebyte (uint8 slaveaddr,uint32_t Subaddr,uint32 _t data) {#if (Hal_eeprom = = TRUE) if (Subaddr < 0xFF) {//set slave address (Device address) i2cmAsterslaveaddrset (Slaveaddr,false);
    False:write//set Sub Address (Word address) i2cmasterdataput (SUBADDR);     I2cmastercontrol (I2c_master_cmd_burst_send_start);
    Star+run//wait until Master module was done transferring.
    while (I2cmasterbusy ()) {}//place the data of the "sent" in the data register i2cmasterdataput (data);    I2cmastercontrol (I2c_master_cmd_burst_send_finish);
    Run+stop//wait until Master module was done transferring.
while (I2cmasterbusy ()) {}} #endif return 0; } * * Dummy write for read Purpose @param index to total EEPROM (0~255)/uint32_t haleepromreadbyte (uint8 Slaveaddr,uin t32_t subaddr) {uint32_t uread=0; #if (hal_eeprom = TRUE) if (Subaddr < 0xFF) {//1. Dummy write//set s       Lave address (Device address) i2cmasterslaveaddrset (slaveaddr,false);
    False:write//set Sub Address (Word address) i2cmasterdataput (SUBADDR);

  I2cmastercontrol (I2c_master_cmd_burst_send_start);  while (I2cmasterbusy ()) {}} #endif return uread; * * * Radom read (at24c02d chip), read the Specify Word address * @param index to Total EEPROM (0~255)/uint32_t Haleepro
    Mreadbyteex (uint8 slaveaddr,uint32_t subaddr) {uint32_t uread=0; #if (hal_eeprom = TRUE) if (Subaddr < 0xFF) { 2.
    Actual read i2cmasterslaveaddrset (slaveaddr,true);

    I2cmastercontrol (i2c_master_cmd_single_receive);
    Read the data from the master.
    Uread = I2cmasterdataget ();
while (I2cmasterbusy ()) {}} #endif return uread;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.