I2C of mini2440 bare metal

Publisher:滁州鱼儿Latest update time:2021-11-24 Source: eefocusKeywords:mini2440 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

// File Name : IIC.c

// Function  : S3C2440 IIC-bus Master Tx/Rx mode Test Program

//             (Interrupt / Non Interrupt (Polling))

// Program   : Shin, On Pil (SOP)

// Date      : May 21, 2002

// Version   : 0.0

// History

//   0.0 : Programming start (March 11, 2002) -> SOP

//====================================================================

#include //The serial port will be used

#include "2440addr.h" //register macro definition

#include "2440lib.h" //Functions to be used, function declarations

#include "def.h" //Macro definitions of several variable types

#include "IIC.h"

//volatile affects the compiler's compilation results, indicating that volatile variables may be

//Do not compile and optimize operations related to volatile variables that have changed

static U8 _iicData[IICBUFSIZE]; //Define an array with 256 data

static volatile int _iicDataCount; //IIC data count

static volatile int _iicStatus; //IIC status

static volatile int _iicMode; //IIC mode

static int _iicPt; //IIC pointer

//===================================================================

//       SMDK2440 IIC configuration

//  GPE15=IICSDA, GPE14=IICSCL

// "Interrupt mode" for IIC block IIC operation in interrupt mode

//===================================================================

//******************[ Test_Iic ]**************************************

void Test_Iic(void)

{

    unsigned int i,j,save_E,save_PE;

    static U8 data[256]; //define an array of 256 8-bit data

    Uart_Printf("nIIC Test(Interrupt) using AT24C02n");

    save_E = rGPECON; //Save the scene

    save_PE  = rGPEUP;

    rGPEUP |= 0xc000; //1100 0000 0000 0000 disable GPE14, GPE15 pull-up resistor

    rGPECON |= 0xa00000; //GPE15:IICSDA, GPE14:IICSCL (should be 0xa0000000)???

    pISR_IIC = (unsigned)IicInt; //Function assigned to vector address

    rINTMSK &= ~(BIT_IIC); //Enable IIC interrupt

      //Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16

      // If PCLK 50.7MHz, IICCLK=PCLK/16=3.17MHz, Tx Clock(transmit clock)=IICCLK/16=0.198MHz

    rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);

    rIICADD = 0x10; //2440 slave address=[7:1]=0x10 slave address

    rIICSTAT = 0x10; //IIC bus data output enable (Rx/Tx) IIC data output enable

    rIICLC = (1<<2)|(1); //Filter enable, 5 clocks SDA output delay added by junon (I feel it is useless)

   

    Uart_Printf("Write test data into AT24C02n");

    for(i=0;i<256;i++)

    Wr24C080(0xa0,(U8)i,i); //The address of the slave device is 0xa0, starting from the internal address 0 of the slave device and ending at address 256, write 0~256

          

    for(i=0;i<256;i++)

        data[i] = 0; //All elements of the data array are initialized to 0

    Uart_Printf("Read test data from AT24C02n");

   

    for(i=0;i<256;i++)

        Rd24C080(0xa0,(U8)i,&(data[i])); //Read the data from the device and store it in the data array

        //Line changed 0 ~ f

    for(i=0;i<16;i++)

    {

        for(j=0;j<16;j++)

            Uart_Printf("%2x",data[i*16+j]); //Print the data just read, that is, the value in the data array

        Uart_Printf("n"); //Change to a new line every 16 bytes

    }

    rINTMSK |= BIT_IIC; //IIC operation is completed, IIC interrupt is disabled

    rGPEUP = save_PE; //Restore the scene

    rGPECON = save_E;

}


//*************************[ Wr24C080 ]****************************

// Function name Slave address Data written to internal address

//****************************************************************

void Wr24C080(U32 slvAddr,U32 addr,U8 data)

{

//The process is: S (start signal) --- send device address ---- device internal address ---- written data

    _iicMode = WRDATA; //Set to write data mode

    _iicPt = 0; //IIC pointer points to address 0

    _iicData[0] = (U8)addr; //Assign a value to element 0 of the Data array, internal address

    _iicData[1] = data; //Assign a value to element 1 of the Data array, and write the data

    _iicDataCount = 2; //The initial value of the data count is 2

   

    rIICDS = slvAddr; //0xa0 gives the slave device address to IICDS

    rIICSTAT = 0xf0; //MasTx, Start Set to master transmission mode, write IIC to generate start signal, enable RX/TX

      //Clearing the pending bit isn't needed because the pending bit has been cleared.

    //Start IIC writing. After sending the slave device address, an interrupt is generated and the interrupt function is entered.

    while(_iicDataCount!=-1 );//Because count is 2, the condition is not met and then wait!! After the first interrupt is processed, count=1, and then judge that the condition in while is not met, continue to wait, and after sending the internal address of the slave word device, an interrupt is generated and the interrupt function is entered   

//The following while statement is to determine whether the ACK signal is received   

    _iicMode = POLLACK; //Wait for ACK response mode, a response means the slave device has received it

    while(1)

    {

        rIICDS     = slvAddr;           //slvAddr=0xa0

        _iicStatus = 0x100;             //IICSTAT clear??

        rIICSTAT = 0xf0; //MasTx, Start, master transmission mode, generate start signal, enable serial output, start IIC

        rIICCON    = 0xaf;              //Resumes IIC operation.

          

        while(_iicStatus==0x100);       //Wait until IICSTAT change

          

        if(!(_iicStatus&0x1)) // Check if the 0th bit of IICSTAT is 0. If it is 0, it means ACK is received.

            break;                      //When ACK is received

    }

     rIICSTAT=0xd0; //Generate a stop signal to stop IIC

     rIICCON=0xaf; //Reconfigure IICCON.

     Delay(1); //Wait until the stop signal of IICSTAT is valid

       //Write is completed.

}

       

//**********************[ Rd24C080 ] ***********************************

//Read random address data read function

//Process: S---Send device address---Device internal address---Send device address---Read data---NOACK---Abort

void Rd24C080(U32 slvAddr,U32 addr,U8 *data)

{

    _iicMode = SETRDADDR; //Set the read address

    _iicPt        = 0;

    _iicData[0]   = (U8)addr;

    _iicDataCount = 1;  

    rIICDS = slvAddr; //slvAddr=0xa0 first write the slave device address 

    rIICSTAT = 0xf0; //MasTx, Start start signal  

      //Clearing the pending bit isn't needed because the pending bit has been cleared.

    while(_iicDataCount!=-1); //Because count is 1, the condition is not met and then wait!! After the first interrupt is processed, count=0, and then judge that the condition in while is not met, continue to wait, and after sending the internal address of the slave word device, an interrupt is generated and the interrupt function is entered. After the interrupt function is executed, count=-1, so this while loop is exited

    _iicMode = RDDATA; //Read data mode 

    _iicPt        = 0;

    _iicDataCount = 1; //Read a data (address)

   

    rIICDS = slvAddr; //slvAddr=0xa0, send the slave device address again

    rIICSTAT = 0xb0; //1011, 10~MasRx, 1~Start, 1~RxTx enable master receive mode

    rIICCON       = 0xaf;               //Resumes IIC operation.  

    while(_iicDataCount!=-1); //Wait, after sending the device address, an interrupt is generated and the interrupt function is entered to execute the second interrupt processing. Count=-1, the condition is not met and exit while, and continue with the following statement

    *data = _iicData[1]; //Send the data received from IICDS to pointer data,

}


//-------------------------------------------------------------------------

void __irq IicInt(void) //IIC interrupt initialization function 

{

    U32 iicSt,i;

   

    rSRCPND = BIT_IIC; //Clear pending bit so that the next interrupt can be executed

    rINTPND = BIT_IIC; //Clear pending bit so that the next interrupt can be executed

    iicSt = rIICSTAT; //Assign the value of the IICSTAT status register to iicSt

   

    if(iicSt & 0x8){} //When bus arbitration is failed. When bus arbitration fails, perform no operation

    if(iicSt & 0x4){} //When a slave address is matched with. When the received slave address matches the address in IICADD, perform no operation

    if(iicSt & 0x2){} //When a slave address is 0000000b. The slave address received is 0000000b

    if(iicSt & 0x1){} //When ACK isn't received, no operation is performed.

    switch(_iicMode) //Perform different operations according to different modes

    {

       case POLLACK: //ACK mode

           _iicStatus = iicSt; //Let _iicStatus equal the value of IICSTAT

           break;

       case RDDATA: //Read data mode

           if((_iicDataCount--)==0) //count is initially 1, so the if code segment is not executed. After the completion, count=0. When the second interrupt comes, the condition is met and the code inside the if is executed

           {

               _iicData[_iicPt++] = rIICDS; //Re-read the value in IICDS to _iicData[1]

           

               rIICSTAT = 0x90; //Stop MasRx condition 1001 0000 Generates a stop signal

               rIICCON = 0xaf; //Resumes IIC operation. Resume IIC operation

               Delay(1);                        //Wait until stop condtion is in effect.

                                                //Too long time... Wait until the stop signal takes effect

                                                //The pending bit will not be set after issuing stop condition.

               break; //jump back to the read function   

           }   

             //Cannot generate ACK if the last byte has not been read. Read data in IICDS

           _iicData[_iicPt++] = rIICDS;         //The last data has to be read with no ack.

           if((_iicDataCount)==0)

               rIICCON = 0x2f; //Because the condition is met, execute rIICCON = 0x2f; the host generates NOACK and releases the IIC operation

           else

               rIICCON = 0xaf; //Generate ACK, release IIC operation

               break;

        case WRDATA: //write data mode

            if((_iicDataCount--)==0) //Judge whether it is 0 after self-decrement. (2 is 1 after self-decrement, not 0)

[1] [2]
Keywords:mini2440 Reference address:I2C of mini2440 bare metal

Previous article:mini2440 i2c device support
Next article:MINI2440i2c driver learning 2

Recommended ReadingLatest update time:2024-11-23 04:18

linux2.6.32.2 mini2440 platform transplantation--LCD backlight driver
1.3.1 LCD backlight control principle So far, we have been transplanting under the command line to view the results. In the mini2440/micro2440 development board, the LCD backlight is controlled through the LCD_PWR pin of the CPU. As can be seen from the schematic diagram, it corresponds to GPG4. When the LCD_PWR o
[Microcontroller]
Shenzhou IV Study Notes (VI) I2C Interface EEPROM-Software and Hardware Implementation
EEPROM is also called electrically erasable programmable read-only memory. Data is not lost after power failure. It is widely used to save small amounts of data. Some enhanced 51 microcontrollers and AVRs have EEPROM integrated inside the chip. The development board uses the I2C interface ATMEL's 24C02 chip, which has
[Microcontroller]
Shenzhou IV Study Notes (VI) I2C Interface EEPROM-Software and Hardware Implementation
u-boot-2011.03 porting on mini2440/micro2440 runs in RAM
2.1 include/configs/micro2440.h delete #define CONFIG_S3C2410 1 /* specifically a SAMSUNG S3C2410 SoC */ #define CONFIG_SMDK2410 1 /* on a SAMSUNG SMDK2410 Board */ Add #define CONFIG_S3C2440 1 /* specifically a SAMSUNG S3C2440 SoC */ #define CONF
[Microcontroller]
u-boot-2011.03 porting on mini2440/micro2440 runs in RAM
mini2440 nandflash simple read and write debugging successful
After another two days of streaking, I adjusted the simple page read, write and erase operations of the nandflash on the mini2440 (model: K9F1G08U0C 128M*8bit). I recorded the code first. I will talk about the complex nandflash operation when I have time. Now it can be realized. Enough with the code relocation. head
[Microcontroller]
mini2440 nandflash simple read and write debugging successful
STM8L052C6 hardware I2C debugging: PCF8563/BM856 clock chip
The Internet generally advocates using software to simulate I2C. I also used simulation to read temperature and humidity sensors before. This time I have the time and opportunity to use I2C again, so I decided to debug it with hardware I2C. So far, there seems to be no problem, and both reading and writing devices are
[Microcontroller]
Setting the slave address of the I2C device
I just started to use microcontrollers and I don’t understand a lot. Today I saw a sentence: #define WriteDeviceAddress 0xa0           //Write device address #define ReadDviceAddress 0xa1               //Read device address I don't understand how 0xa0 and 0xa1 are determined, so I looked up some information and se
[Microcontroller]
Keil mini2440 bare metal code problem solving notes 1
Warning: L6305W: Image does not have an entry point. (Not specified or not set due to multiple choices.) Add the startup code and comment out main, and you will see solve In the Linker-Misc controls of Options for Target, add the entry address: –entry Reset_Handler In the help manual Controls Specify any directi
[Microcontroller]
Summary and analysis of IIC timing problems in mini2440 debugging
Description: mini2440 platform, wince6.0 system, vs2005 After several months of study and pause, I finally added the IIC bus simulated by GPIO under wince and realized the transplantation of IIC. The chip of IIC is SHT21 temperature and humidity chip. The reason why I haven't figured out IIC for so long is
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号