Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

function, pointer to an instance

Status
Not open for further replies.

nmem

Newbie level 4
Joined
May 10, 2016
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
53
Hello. I am confused in c programming when a function is called by (pointer to an instance). For example in the code below:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * Set the I2C bus speed in conjunction with the clock frequency.
 * param p_twi Pointer to a TWI instance.
 * ul_speed: The desired I2C bus speed (in Hz) (ul_speed to DesiredClock)
 * ul_mck: Main clock of the device (in Hz). (ul_mck to MainClock)
 * return value PASS\Fail New speed setting is accepted\rejected
 */
uint32_t twi_set_speed(Twi *p_twi, uint32_t ul_speed, uint32_t ul_mck)
{
    uint32_t ckdiv = 0;
    uint32_t c_lh_div;
 
    if (ul_speed > I2C_FAST_MODE_SPEED) {
        return FAIL;
    }
 
    c_lh_div = ul_mck / (ul_speed * TWI_CLK_DIVIDER) - TWI_CLK_CALC_ARGU;
 
    /* cldiv must fit in 8 bits, ckdiv must fit in 3 bits */
    while ((c_lh_div > TWI_CLK_DIV_MAX) && (ckdiv < TWI_CLK_DIV_MIN)) {
        /* Increase clock divider */
        ckdiv++;
        /* Divide cldiv value */
        c_lh_div /= TWI_CLK_DIVIDER;
    }
 
    /* set clock waveform generator register */
    p_twi->TWI_CWGR =
            TWI_CWGR_CLDIV(c_lh_div) | TWI_CWGR_CHDIV(c_lh_div) |
            TWI_CWGR_CKDIV(ckdiv);
 
    return PASS;
}



what could be the other method to re-write the code.?
mem
 
Last edited by a moderator:

I am interfacing 24AA64F with Atmel SAM4E, the eeprom speed is upto 400KHz and the processor speed is 120MHz. I wanted to downconvert it. Its my first time working with I2C so having a lot of confusions. there is a register TWI_CWGR and I need a value to be loaded in it. First option may be I load 0x0C in TWI_CWGR or use this instance and pointer. It would be nice if I get some guidance.
mem
 

You didn't tell which problems you have with the code. In any case, you would inspect the definition of Twi to understand it's function.

- - - Updated - - -

I presume setting I2C registers using pointers is part of an existing library. It's most likely used to keep the register access generic, independent of the actually used I2C interface. If it's all over your head, you can of course write to explicit register addresses in your code.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top