Reading Sensor Data Over I²C Using DTC on the Renesas RA6M4

by Samba Siva in Circuits > Electronics

30 Views, 0 Favorites, 0 Comments

Reading Sensor Data Over I²C Using DTC on the Renesas RA6M4

Hey everyone! In this Instructable, I'll walk you through how to read a temperature sensor (the HS4001) data on the Renesas RA6M4 using the IIC master driver together with the DTC (Data Transfer Controller). The DTC moves the I2C bytes between the peripheral and RAM by itself, so the CPU is not interrupted for every byte.

Supplies

- Renesas EK-RA6M4 evaluation kit (or any RA6M4 board)

- HS4001 temperature/humidity sensor

- USB cable (micro-USB)

- PC with e² studio and the Renesas FSP (Flexible Software Package)


Understand the Idea

I2C normally interrupts the CPU for every byte sent or received. With the DTC attached to the IIC driver:


1. The CPU only starts the transfer (`R_IIC_MASTER_Write` / `R_IIC_MASTER_Read`).

2. The DTC moves each byte between the IIC data register and RAM.

3. The CPU is notified only when the whole transfer finishes (through a callback).


Flow of this project: **send measure command -> wait for conversion -> read 2 bytes -> convert to °C**.

Wire It Up

| HS4001 | |RA6M4 board| | Notes |

|---|---|---|

| VDD | | 3.3 V | | Do not use 5 V |

| GND | GND | Common ground |

| SCL | IIC SCL pin -

| SDA | IIC SDA pin -


Check your board's user manual for the exact SCL/SDA pins

Create the Project

1. Open e² studio -> File -> New -> Renesas C/C++ Project -> Renesas RA.

2. Choose Renesas RA C Executable Project**.

3. Select the board EK-RA6M4 (or device R7FA6M4AF3CFB).

4. Toolchain: GCC. RTOS: **No RTOS**.

5. Finish and open configuration.xml.

Configure the Pins

In the **Pins** tab, enable the IIC channel that matches your wiring (for example IIC1) and assign:

- SCL -> your board's SCL pin

- SDA -> your board's SDA pin

In the **Stacks** Tab:

1. **New Stack -> Connectivity -> I2C Master (r_iic_master)**.

2. Set these properties:

- Name: `g_i2c_master0`

- Channel: the same channel as your pins

- Rate: Standard (100 kHz) to start

- Slave Address: the sensor's 7-bit address (0x54)

- Address Mode: 7-Bit

- Callback: `i2c_master_callback`

3. Under the stack, add the transfer instances:

- **Transfer Instance Tx** -> `r_dtc`

- **Transfer Instance Rx** -> `r_dtc`

FSP configures the DTC activation sources (IIC TXI / RXI) for you.

4. Click **Generate Project Content**.

Add the Code

Open `src/hal_entry.c` and replace its contents with:


#include "hal_data.h"


#define CMD_MEASURE_TEMP (0xF3U) /* measurement command - check HS4001 datasheet */

#define MEASURE_DELAY_MS (20U)

#define LOOP_DELAY_MS (1000U)

#define I2C_TIMEOUT_LOOPS (1000000UL)


static volatile bool i2c_done = false;

static volatile i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;


volatile int g_temperature_c = 0; /* watch this in the debugger */


void i2c_master_callback(i2c_master_callback_args_t * p_args)

{

i2c_event = p_args->event;

i2c_done = true;

}


static fsp_err_t i2c_wait(i2c_master_event_t expected)

{

uint32_t timeout = I2C_TIMEOUT_LOOPS;

while ((!i2c_done) && (timeout > 0U)) { timeout--; }


if ((!i2c_done) || (i2c_event != expected))

{

i2c_done = false;

return FSP_ERR_ABORTED;

}

i2c_done = false;

return FSP_SUCCESS;

}


void hal_entry(void)

{

fsp_err_t err;

uint8_t cmd = CMD_MEASURE_TEMP;

uint8_t rx_buffer[2];


err = R_IIC_MASTER_Open(&g_i2c_master0_ctrl, &g_i2c_master0_cfg);

if (FSP_SUCCESS != err) { __BKPT(0); }


while (1)

{

/* 1. Send the measurement command (DTC feeds the TX data) */

i2c_done = false;

err = R_IIC_MASTER_Write(&g_i2c_master0_ctrl, &cmd, 1U, false);

if ((FSP_SUCCESS != err) || (FSP_SUCCESS != i2c_wait(I2C_MASTER_EVENT_TX_COMPLETE)))

{

R_IIC_MASTER_Abort(&g_i2c_master0_ctrl);

R_BSP_SoftwareDelay(LOOP_DELAY_MS, BSP_DELAY_UNITS_MILLISECONDS);

continue;

}


/* 2. Wait for the sensor to finish converting */

R_BSP_SoftwareDelay(MEASURE_DELAY_MS, BSP_DELAY_UNITS_MILLISECONDS);


/* 3. Read 2 bytes (DTC stores them into rx_buffer) */

i2c_done = false;

err = R_IIC_MASTER_Read(&g_i2c_master0_ctrl, rx_buffer, 2U, false);

if ((FSP_SUCCESS != err) || (FSP_SUCCESS != i2c_wait(I2C_MASTER_EVENT_RX_COMPLETE)))

{

R_IIC_MASTER_Abort(&g_i2c_master0_ctrl);

R_BSP_SoftwareDelay(LOOP_DELAY_MS, BSP_DELAY_UNITS_MILLISECONDS);

continue;

}


/* 4. Convert the raw 14-bit value to degrees Celsius */

uint16_t raw = (uint16_t)((((uint16_t) rx_buffer[0] << 8) | rx_buffer[1]) & 0x3FFFU);

g_temperature_c = (int)(((uint32_t) raw * 165U) / 16383U) - 40;


R_BSP_SoftwareDelay(LOOP_DELAY_MS, BSP_DELAY_UNITS_MILLISECONDS);

}

}


How the code works

- `R_IIC_MASTER_Open` opens the I2C driver, and FSP opens the linked DTC instances with it.

- `R_IIC_MASTER_Write` and `R_IIC_MASTER_Read` only start the transfer. The DTC does the byte moving.

- The callback sets a flag when the transfer completes, and the main loop waits for that flag (with a timeout, so a missing sensor can never freeze the program).

- The raw reading is masked to 14 bits and scaled to degrees Celsius.

Build, Flash and Debug

1. Click the Build icon and fix any errors.

2. Connect the board to the PC with the debug USB cable.

3. Click Debug, then Resume.

4. Add `g_temperature_c` to the **Expressions** view to see the live temperature. Warm the sensor with your finger and watch the value rise.

Troubleshooting

- No ACK / transfer aborted: check the pull-ups, the sensor's 7-bit address, the 3.3 V supply and the SCL/SDA wiring.

- Garbage values: increase `MEASURE_DELAY_MS` to give the sensor more conversion time.

- Wrong readings: compare the command, byte count and conversion formula with the HS4001 datasheet.

- Bus stuck: make sure grounds are shared, and keep the rate at 100 kHz.

- Linker or build errors:confirm both DTC transfer instances are added, then regenerate project content.

Conclusion

You now have an I2C sensor read on the RA6M4 where the DTC handles the data movement and the CPU is only involved at the start and end of each transfer. Thanks for reading, and if you build this, I'd love to see your results in the comments!