libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
IIC library

Enumerations

enum  iic_index_t { IIC0 = 0, IIC1 = 1, NUM_IICS = 2 }
 

Functions

void iic_init (const iic_index_t iic)
 
void iic_destroy (const iic_index_t iic)
 
bool iic_read_register (const iic_index_t iic, const uint8_t addr, const uint8_t reg, uint8_t *data, uint16_t length)
 
bool iic_write_register (const iic_index_t iic, const uint8_t addr, const uint8_t reg, uint8_t *data, uint16_t length)
 
bool iic_set_slave_mode (const iic_index_t iic, const uint8_t addr, uint32_t *register_map, const uint32_t rm_length)
 
void iic_slave_mode_handler (const iic_index_t iic)
 
void iic_reset (const iic_index_t iic)
 

Detailed Description

Functions to use the Inter-Integrated Circuit (IIC).

High-level functions to read/write to clients connected to the two integrated IIC modules. Before sending and receiving bytes the IIC2 must be connect to some I/O pins through the switchbox (see switchbox.h), e.g. Pmod A:

or the SCL and SDA Arduino IIC pins:

The Pmod A pins (see pinmap.h) are good because they have 2K2 pull-up resistors built in. If you want to use more than three IICs then you can use different pins with an external 2K2 pull-up resistor, which should work for more than three boards.

After that, an example of how to use this library for the MASTER.

#include <libpynq.h>
int main (void)
{
// initialise all I/O
uint32_t i;
// you can use multiple slaves, here only one is shown
uint32_t slave_address = 0x70;
// read out all registers of the slave
for (int reg=0; reg < 32; reg++) {
if (iic_read_register(IIC0, slave_address, reg, (uint8_t *) &i, 4)) {
// 4 means 4 bytes, do not change
printf("register[%d]=error\n",reg); } else {
printf("register[%d]=%d\n",reg,i);
}
}
// clean up after use
return EXIT_SUCCESS;
}

An example of how to use this library for the SLAVE.

int main(void)
{
// this is the address by which this slave is reached by the master
// different slaves must have different addresses
const uint32_t my_slave_address = 0x70;
// array contains 32 registers that can be written & read by the master
// the slave can of course modify the values of the registers
uint32_t my_register_map[32] = {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};
const uint32_t my_register_map_length =
sizeof(my_register_mmap)/sizeof(uint32_t);
iic_set_slave_mode(IIC0, my_slave_address,
&(my_register_map[0]), my_register_map_length);
while (1) {
// the slave mode handler must be run regularly to react to the master
// insert your own code here, to do whatever the slave needs to do;
// but ensure that you execute the slave mode handler regularly enough
}
return EXIT_SUCCESS;
}

Enumeration Type Documentation

◆ iic_index_t

Enum of IICs. Functions use a switch numbered from 0..NUM_IICS-1.

Enumerator
IIC0 
IIC1 
NUM_IICS 

Definition at line 114 of file iic.h.

Function Documentation

◆ iic_destroy()

void iic_destroy ( const iic_index_t  iic)

Close the shared memory handle for the specified IIC index.

Parameters
uartThe IIC index to remove from the shared memory space.
Warning
Fails with program exit if the IIC channel is outside valid range.

Definition at line 3 of file iic.c.

◆ iic_init()

void iic_init ( const iic_index_t  iic)

Initialize the IIC specified by the index with a shared memory handle and a buffer size of 4096 bytes.

Parameters
uartThe IIC index to initialize.
Warning
Fails with program exit if the IIC channel is outside valid range or when the shared memory system has not been instantiated.

Definition at line 2 of file iic.c.

◆ iic_read_register()

bool iic_read_register ( const iic_index_t  iic,
const uint8_t  addr,
const uint8_t  reg,
uint8_t *  data,
uint16_t  length 
)
Parameters
iicThe IIC index to initialize.
addrThe IIC address of the client to access.
regThe clients register address.
dataBuffer where the register content is stored. [out]
lengthThe amount of data to read.

Reads the content of the register into data.

Returns
0 if successful, 1 on error

Definition at line 7 of file iic.c.

◆ iic_reset()

void iic_reset ( const iic_index_t  iic)
Parameters
iicThe IIC index of the hardware to use. Return the IIC module into its default mode. This way it can be used as master.

Definition at line 6 of file iic.c.

◆ iic_set_slave_mode()

bool iic_set_slave_mode ( const iic_index_t  iic,
const uint8_t  addr,
uint32_t *  register_map,
const uint32_t  rm_length 
)

Definition at line 4 of file iic.c.

◆ iic_slave_mode_handler()

void iic_slave_mode_handler ( const iic_index_t  iic)
Parameters
iicThe IIC index of the hardware to use.

This handles requests that came in to the IIC unit when it is in slave mode.

Definition at line 302 of file iic.c.

◆ iic_write_register()

bool iic_write_register ( const iic_index_t  iic,
const uint8_t  addr,
const uint8_t  reg,
uint8_t *  data,
uint16_t  length 
)
Parameters
iicThe IIC index to initialize.
addrThe IIC address of the client to access.
regThe clients register address.
dataBuffer where new the register content is stored.
lengthThe amount of data to write.

Writes data to register.

Returns
0 if successful, 1 on error

Definition at line 10 of file iic.c.

iic_init
void iic_init(const iic_index_t iic)
Initialize the IIC specified by the index with a shared memory handle and a buffer size of 4096 bytes...
Definition: iic.c:2
pynq_destroy
void pynq_destroy(void)
Reset and destroy the switchbox and GPIO of the PYNQ.
Definition: libpynq.c:3
switchbox_set_pin
void switchbox_set_pin(const pin_t pin_number, const uint8_t pin_type)
Definition: switchbox.c:4
sleep_msec
void sleep_msec(int msec)
Wait for msec milliseconds.
Definition: util.c:2
iic_set_slave_mode
bool iic_set_slave_mode(const iic_index_t iic, const uint8_t addr, uint32_t *register_map, const uint32_t rm_length)
Definition: iic.c:4
IO_PMODA4
#define IO_PMODA4
Definition: pinmap.h:153
SWB_IIC0_SDA
@ SWB_IIC0_SDA
Definition: switchbox.h:88
SWB_IIC0_SCL
@ SWB_IIC0_SCL
Definition: switchbox.h:90
IO_AR_SDA
@ IO_AR_SDA
Definition: pinmap.h:100
iic_destroy
void iic_destroy(const iic_index_t iic)
Close the shared memory handle for the specified IIC index.
Definition: iic.c:3
pynq_init
void pynq_init(void)
Initialise the switchbox and GPIO of the PYNQ.
Definition: libpynq.c:2
iic_reset
void iic_reset(const iic_index_t iic)
Definition: iic.c:6
IO_AR_SCL
@ IO_AR_SCL
I2C pins.
Definition: pinmap.h:99
IO_PMODA3
#define IO_PMODA3
Definition: pinmap.h:152
iic_slave_mode_handler
void iic_slave_mode_handler(const iic_index_t iic)
Definition: iic.c:302
IIC0
@ IIC0
Definition: iic.h:114
iic_read_register
bool iic_read_register(const iic_index_t iic, const uint8_t addr, const uint8_t reg, uint8_t *data, uint16_t length)
Definition: iic.c:7
libpynq.h