# UART, I2C, SPI Communication via Infineon PSoC™6

## **UART (Universal Asynchronous Receiver-Transmitter)**

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2Fuqh0zR5G5pbrkjH9ZFwG%2Fimage.png?alt=media&#x26;token=3aff9865-dc8c-43f0-9635-2306864f5e40" alt=""><figcaption><p><a href="https://www.analog.com/en/analog-dialogue/articles/uart-a-hardware-communication-protocol.html">Figure 1. Two UARTs directly communicate with each other.</a></p></figcaption></figure>

**Purpose**: Communication between devices.

**How it works**: Sends data one bit at a time, in sequence.

#### Key Features:&#x20;

* Only 2 wires are needed: Transmit (TX) and Receive (RX).&#x20;
* No clock signal (it's asynchronous).&#x20;
* Devices need to agree on a data rate (baud rate) beforehand.&#x20;

> Analogy: Think of UART like two people talking on walkie-talkies. One person speaks (transmits), and the other listens (receives), and vice versa. Both people need to agree on the speed of their conversation (like speaking slowly) to understand each other.

### Printing with retarget io using UART&#x20;

To use the `retarget-io` library to print messages, you can follow these steps:

1. **Add the Library**: Utilize the Library Manager to incorporate the `retarget-io` library to your project. You can find this library under the "Peripherals" category.
2. **Include the Header File**: In your <mark style="color:red;">`main.c`</mark> file, include the header file for the library by adding the line <mark style="color:red;">`#include "cy_retarget_io.h"`</mark> at the beginning of your file.
3. **Initialize the Interface**: In the initialization segment of your firmware, call the function <mark style="color:red;">`cy_retarget_io_init`</mark> to initialize the communication interface. This function utilizes the PSoC™ debug UART pins that are connected to KitProg3, and it initializes the interface with a default baud rate of 115200. Here is how you can call this function:

   ```c
   cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);
   ```
4. **Using printf**: Now, you can use the <mark style="color:red;">`printf`</mark> function in your code just like you normally would. For instance, to print a variable named "myVar", you can use a line of code like this:

   ```c
   printf("The value of myVar is: %d\n", myVar);
   ```

## **I2C (Inter-Integrated Circuit)**

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2Fjm4NmuYhMdQGsBX0Mg5r%2Fimage.png?alt=media&#x26;token=8b474e5c-3107-466b-bb26-a14cd128911e" alt=""><figcaption><p><a href="https://www.analog.com/en/analog-dialogue/articles/i2c-communication-protocol-understanding-i2c-primer-pmbus-and-smbus.html">Figure 1. Integrated circuits directly communicate with each other.</a></p></figcaption></figure>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FU78L95Xy7w64bBIcxKwn%2Fimage.png?alt=media&#x26;token=c30e3fec-1280-42ef-8fa6-61b7de953503" alt=""><figcaption><p><a href="https://www.analog.com/en/analog-dialogue/articles/i2c-communication-protocol-understanding-i2c-primer-pmbus-and-smbus.html">Figure 2. An I2C pull-up resistor connection.</a></p></figcaption></figure>

**Purpose**: Communication between devices, often within the same circuit board.

**How it works**: Multiple devices can be connected in a 'bus' system using only 2 wires.

**Key Features**:

* 2 wires: Serial Data (SDA) and Serial Clock (SCL).
* Devices have unique addresses so they can be individually spoken to.
* One master device controls the communication; others are slaves.

> **Analogy**: Think of I2C like a teacher (master) in a classroom, and the students (slaves) are the devices. The teacher asks a specific student a question by calling their name (address) and waits for an answer.

To facilitate communication between a master device and a slave device, there are several functions available for reading and writing data. Here’s a brief overview:

### Master and Slave Communication Using I2C Functions in Infineon

#### Master-Side Functions

1. **Dedicated Read and Write Functions**:
   * <mark style="color:red;">`cyhal_i2c_master_read`</mark>: A dedicated function to perform read operations from the slave.
   * <mark style="color:red;">`cyhal_i2c_master_write`</mark>: A dedicated function to perform write operations to the slave.
2. **Memory-Specific Read and Write Functions**:
   * <mark style="color:red;">`cyhal_i2c_master_mem_read`</mark>: This function allows the master to read a block of data from a specified memory address in the slave.
   * <mark style="color:red;">`cyhal_i2c_master_mem_write`</mark>: This function enables the master to write a block of data to a specific memory address in the slave.
3. **Asynchronous Transfer Function**:
   * <mark style="color:red;">`cyhal_i2c_master_transfer_async`</mark>: A versatile function that can perform either read, write, or both operations asynchronously.

#### Slave-Side Functions

To configure the read and write buffers of the slave, the following functions are available:

* <mark style="color:red;">`cyhal_i2c_slave_config_read_buffer`</mark>: A function to configure the slave's read buffer.
* <mark style="color:red;">`cyhal_i2c_slave_config_write_buffer`</mark>: A function to configure the slave's write buffer.

#### Documentation and Resources

For more detailed information and usage examples, you can refer to the documentation for the HAL I2C functions, which can be found in the following section of the official documentation:

* **Hardware Abstraction Layer (HAL):** Hardware Abstraction Layer > HAL API Reference > HAL Drivers > I2C.

## **SPI (Serial Peripheral Interface)**

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2Fo9Ya5UiktaYC0DFQ3xvh%2Fimage.png?alt=media&#x26;token=dedfd0d6-3c25-4a7b-aa4a-43c3b554637f" alt=""><figcaption></figcaption></figure>

**Purpose**: Fast communication between devices.

**How it works**: Sends data simultaneously in both directions.

**Key Features**:

* More than 2 wires: at least 4 (Master Out Slave In, Master In Slave Out, Clock, and Chip Select).
* One master, multiple slaves.
* It's synchronous (uses a clock).

> **Analogy**: Imagine a conveyor belt (SPI) with people on either side passing boxes (data) back and forth. There's a rhythm or clock to when they pass and receive boxes, making the transfer efficient.
