Dear all,
Does anyone have interfaced SHT20 sensor with R5F10268 with IIC00??
Thank you..
Hi, Thank you for posting on the Renesas community.I'll provide you the example code for sht20 interfacing with RL78/G14 MCU, There might not be a big difference between IIC in the G14 and the G12 series. so I'll share this as soon as possible. Thanks for your patience.
Best regards, Hossein.
If this or any other user's response answers your concern, kindly verify the answer. Thank you!
Renesas Engineering Community Moderatorhttps://community.renesas.com/https://academy.renesas.com/en-support.renesas.com/.../
Hello,
Thank you for your reply.
it will be big help if i will get it.
Thank you
Here's the sample project for SHT20, utilizing IICA0 and the code generator tool in e2studio IDE.
G14_IICA_SHT20.rarThe "temp" and "humidity" variables obtain the sensor's values.I hope you find it useful, although there's room to enhance the code.
Feel free to ask questions if you need to. Regards, Hossein.
Hi hossein,
can i use this code with IIC00 simplified mode too???
Hi, yes, there might not be a problem. It simply sends a command to the sensor, then waits for its response and gets the data. check it and modify based on your hardware.
If you run into any trouble, feel free to reach out to me
Hi,
when i am trying to read multiple data it gives only 0x00 every time. i am using below given code for given sequence.
uint16_t readValue(unsigned char cmd) { uint16_t rawValue; uint8_t rx_data[3],i=0; uint8_t tx_data[2];//msb, lsb, checksum; i2c_flag = 0; tx_data[0] = cmd; R_IIC00_Master_Send(SLAVE_ADDRESS,&tx_data[0],1); if(i2c_flag == I2C_TX_END){ delay(20); R_IIC00_StopCondition(); for(i=0;i<20;i++){ delay_ms(20); R_IIC00_Master_Receive(SLAVE_ADDRESS,rx_data,3); if(i2c_flag == I2C_RX_END){ i=20; } } } rawValue = ((uint16_t) rx_data[0] << 8) | (uint16_t) rx_data[1]; if(checkCRC(rawValue, rx_data[2]) != 0){ return (ERROR_BAD_CRC); } return rawValue; //& 0xFFFC; }
If i am reading only one register then i am able to get data of that register.
unsigned char readUserRegister(void) { unsigned char rx_data[2]; unsigned char tx_data[2];//msb, lsb, checksum; i2c_flag = 0; tx_data[0] = READ_USER_REG; R_IIC00_Master_Send(SLAVE_ADDRESS,&tx_data[0],1); R_IIC00_Master_Receive(SLAVE_ADDRESS,&rx_data[0],1); R_IIC00_StopCondition(); return (rx_data[0]); }
Thank you for you great. help.
Regarding your issue, If I get it right, you're able to get the 'User register' data from the sensor, but you cannot get the 2-byte, or 3-byte long data which includes temp and humidity data.
As the datasheet of the SHT20 says:
For getting the temp and humidity values, the sensor needs a period to process the values and it takes about 85ms (maximum). after you send the command to get the T or RH value, you need to consider that. although reading from UserRegister doesn't need any delay and works instantly. On the other hand, if you're sending commands 0xE3 and 0xE5, after requesting T or RH data, the sensor will pull down the SCL line (clock stretch) until the data is ready to be read. The I2C unit will that delay into account and you'll have no trouble getting the data.The figure below shows how the sensor stretches the SCL line to indicate that it's not ready to communicate during data processing: then after the waiting period, the sensor will send 3 consecutive bytes of data.But, if you're sending the commands or getting data in a while loop, the transmission will be restarted, and you'll not get the data.
To redeem that, you can set a flag in the "r_iic00_callback_master_receiveend", then check it to make sure, you're not overwriting the bus before the data is ready. Here's my code:
main.c:
/*********************************************************************************************************************** * DISCLAIMER * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. * No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all * applicable laws, including copyright laws. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED * OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY * LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT, * INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR * ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability * of this software. By using this software, you agree to the additional terms and conditions found by accessing the * following link: * http://www.renesas.com/disclaimer * * Copyright (C) 2011, 2021 Renesas Electronics Corporation. All rights reserved. ***********************************************************************************************************************/ /*********************************************************************************************************************** * File Name : r_main.c * Version : CodeGenerator for RL78/G14 V2.05.06.02 [08 Nov 2021] * Device(s) : R5F104ML * Tool-Chain : CCRL * Description : This file implements main function. * Creation Date: 3/28/2024 ***********************************************************************************************************************/ /*********************************************************************************************************************** Includes ***********************************************************************************************************************/ #include "r_cg_macrodriver.h" #include "r_cg_cgc.h" #include "r_cg_port.h" #include "r_cg_serial.h" /* Start user code for include. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ #include "r_cg_userdefine.h" /*********************************************************************************************************************** Pragma directive ***********************************************************************************************************************/ /* Start user code for pragma. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ /*********************************************************************************************************************** Global variables and functions ***********************************************************************************************************************/ /* Start user code for global. Do not edit comment generated here */ uint8_t SHT_command[] = {0xE3,0xE5};//T Measurement command hold-master,Relative Humidity Measurement command hold-master #define SLAVE_ADDRESS 0x80 //SHT20 I2C address volatile _Bool g_data_Ready; //End of reception indication uint16_t temp_data; uint16_t readValue(uint8_t* const cmd); /* End user code. Do not edit comment generated here */ void R_MAIN_UserInit(void); /*********************************************************************************************************************** * Function Name: main * Description : This function implements main function. * Arguments : None * Return Value : None ***********************************************************************************************************************/ void main(void) { R_MAIN_UserInit(); /* Start user code. Do not edit comment generated here */ while (1U) { temp_data = readValue(&SHT_command[0]); } /* End user code. Do not edit comment generated here */ } /*********************************************************************************************************************** * Function Name: R_MAIN_UserInit * Description : This function adds user code before implementing main function. * Arguments : None * Return Value : None ***********************************************************************************************************************/ void R_MAIN_UserInit(void) { /* Start user code. Do not edit comment generated here */ EI(); /* End user code. Do not edit comment generated here */ } /* Start user code for adding. Do not edit comment generated here */ uint16_t readValue(uint8_t* const cmd) { uint16_t rawValue; uint8_t rx_data[3];//msb, lsb, checksum; g_data_Ready = 0;//Reset the data reception flag R_IIC00_Master_Send(SLAVE_ADDRESS,cmd,1); //Receive the data R_IIC00_Master_Receive(SLAVE_ADDRESS,rx_data,3); //Wait for SCL line to be released from the sensor while(g_data_Ready == 0); //Wait for setting g_data_Ready from receive end interrupt rawValue = ((uint16_t) rx_data[0] << 8) | (uint16_t) rx_data[1]; /* if(checkCRC(rawValue, rx_data[2]) != 0){ return (ERROR_BAD_CRC); } */ return rawValue; //& 0xFFFC; } /* End user code. Do not edit comment generated here */
r_cg_serial_user.c :
/*********************************************************************************************************************** * DISCLAIMER * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. * No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all * applicable laws, including copyright laws. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED * OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY * LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT, * INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR * ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability * of this software. By using this software, you agree to the additional terms and conditions found by accessing the * following link: * http://www.renesas.com/disclaimer * * Copyright (C) 2011, 2021 Renesas Electronics Corporation. All rights reserved. ***********************************************************************************************************************/ /*********************************************************************************************************************** * File Name : r_cg_serial_user.c * Version : CodeGenerator for RL78/G12 V2.04.06.02 [08 Nov 2021] * Device(s) : R5F10268 * Tool-Chain : CCRL * Description : This file implements device driver for Serial module. * Creation Date: 3/28/2024 ***********************************************************************************************************************/ /*********************************************************************************************************************** Includes ***********************************************************************************************************************/ #include "r_cg_macrodriver.h" #include "r_cg_serial.h" /* Start user code for include. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ #include "r_cg_userdefine.h" /*********************************************************************************************************************** Pragma directive ***********************************************************************************************************************/ #pragma interrupt r_iic00_interrupt(vect=INTIIC00) /* Start user code for pragma. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ /*********************************************************************************************************************** Global variables and functions ***********************************************************************************************************************/ extern volatile uint8_t g_iic00_master_status_flag; /* iic00 start flag for send address check by master mode */ extern volatile uint8_t * gp_iic00_tx_address; /* iic00 send data pointer by master mode */ extern volatile uint16_t g_iic00_tx_count; /* iic00 send data size by master mode */ extern volatile uint8_t * gp_iic00_rx_address; /* iic00 receive data pointer by master mode */ extern volatile uint16_t g_iic00_rx_count; /* iic00 receive data size by master mode */ extern volatile uint16_t g_iic00_rx_length; /* iic00 receive data length by master mode */ /* Start user code for global. Do not edit comment generated here */ extern volatile _Bool g_data_Ready; //Indication of end of reception from the sensor /* End user code. Do not edit comment generated here */ /*********************************************************************************************************************** * Function Name: r_iic00_interrupt * Description : This function is INTIIC00 interrupt service routine. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void __near r_iic00_interrupt(void) { volatile uint16_t w_count; /* Set delay to start next transmission. The delay time depend on slave device. Here set 20us as default base on current clock */ for (w_count = 0U; w_count <= IIC00_WAITTIME_2; w_count++) { NOP(); } if (((SSR00 & _0002_SAU_PARITY_ERROR) == 0x0002U) && (g_iic00_tx_count != 0U)) { SIR00 |= _0002_SAU_SIRMN_PECTMN; //clear ACK error detection flag R_IIC00_StopCondition(); r_iic00_callback_master_error(MD_NACK); } else if(((SSR00 & _0001_SAU_OVERRUN_ERROR) == 0x0001U) && (g_iic00_tx_count != 0U)) { SIR00 |= _0001_SAU_SIRMN_OVCTMN; //clear overrun error detection flag R_IIC00_StopCondition(); r_iic00_callback_master_error(MD_OVERRUN); } else { /* Control for master send */ if ((g_iic00_master_status_flag & _01_SAU_IIC_SEND_FLAG) == 1U) { if (g_iic00_tx_count > 0U) { SIO00 = *gp_iic00_tx_address; gp_iic00_tx_address++; g_iic00_tx_count--; } else { /* IIC master transmission finishes and a callback function can be called here. */ r_iic00_callback_master_sendend(); } } /* Control for master receive */ else { if ((g_iic00_master_status_flag & _04_SAU_IIC_SENDED_ADDRESS_FLAG) == 0U) { ST0 |= _0001_SAU_CH0_STOP_TRG_ON; SCR00 &= ~_C000_SAU_RECEPTION_TRANSMISSION; SCR00 |= _4000_SAU_RECEPTION; SS0 |= _0001_SAU_CH0_START_TRG_ON; g_iic00_master_status_flag |= _04_SAU_IIC_SENDED_ADDRESS_FLAG; if (g_iic00_rx_length == 1U) { SOE0 &= ~_0001_SAU_CH0_OUTPUT_ENABLE; /* disable IIC00 out */ } SIO00 = 0xFFU; } else { if (g_iic00_rx_count < g_iic00_rx_length) { *gp_iic00_rx_address = SIO00; gp_iic00_rx_address++; g_iic00_rx_count++; if (g_iic00_rx_count == (g_iic00_rx_length - 1U)) { SOE0 &= ~_0001_SAU_CH0_OUTPUT_ENABLE; /* disable IIC00 out */ SIO00 = 0xFFU; } else if (g_iic00_rx_count == g_iic00_rx_length) { /* IIC master reception finishes and a callback function can be called here. */ r_iic00_callback_master_receiveend(); } else { SIO00 = 0xFFU; } } } } } } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_error * Description : This function is a callback function when IIC00 master error occurs. * Arguments : flag - * status flag * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_error(MD_STATUS flag) { /* Start user code. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_receiveend * Description : This function is a callback function when IIC00 finishes master reception. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_receiveend(void) { /* Start user code. Do not edit comment generated here */ g_data_Ready = 1; //Setting receive end flag /* End user code. Do not edit comment generated here */ } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_sendend * Description : This function is a callback function when IIC00 finishes master transmission. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_sendend(void) { /* Start user code. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ } /* Start user code for adding. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */
I hope you find it helpful. Best regards,Hossein.
Thank you for your response,
I have tried this code but not working. after R_IIC00_Master_Receive(SLAVE_ADDRESS,rx_data,3);
is executed, it goes into while loop forever.
Is it anything to do with clock times ?
Thanks, in advance.
Hi, I hope you're doing well, sorry for that, I just realized that the method that I was using to obtain the sensor's value (holding master, clock stretching) needs repeated Start conditions, which cannot be implemented by the SAU0. Serial array unit can only perform a 'start condition' if we had a stop condition before that. so that was the main problem of the above code, but if it's possible to use the IICA unit, don't hesitate to use this example which I shared in the first reply. by the way, if you're going to continue using IIC00, we'll need to switch to no-hold master mode, which doesn't need repeated start conditions. (Make sure you're using no-hold commands)Here's my code for no-hold-master mode.
main.c :
/*********************************************************************************************************************** * DISCLAIMER * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. * No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all * applicable laws, including copyright laws. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED * OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY * LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT, * INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR * ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability * of this software. By using this software, you agree to the additional terms and conditions found by accessing the * following link: * http://www.renesas.com/disclaimer * * Copyright (C) 2011, 2021 Renesas Electronics Corporation. All rights reserved. ***********************************************************************************************************************/ /*********************************************************************************************************************** * File Name : r_main.c * Version : CodeGenerator for RL78/G14 V2.05.06.02 [08 Nov 2021] * Device(s) : R5F104ML * Tool-Chain : CCRL * Description : This file implements main function. * Creation Date: 3/28/2024 ***********************************************************************************************************************/ /*********************************************************************************************************************** Includes ***********************************************************************************************************************/ #include "r_cg_macrodriver.h" #include "r_cg_cgc.h" #include "r_cg_port.h" #include "r_cg_serial.h" /* Start user code for include. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ #include "r_cg_userdefine.h" /*********************************************************************************************************************** Pragma directive ***********************************************************************************************************************/ /* Start user code for pragma. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ /*********************************************************************************************************************** Global variables and functions ***********************************************************************************************************************/ /* Start user code for global. Do not edit comment generated here */ uint8_t SHT_command[] = {0xF3,0xF5};//T Measurement command no-hold-master,Relative Humidity Measurement command no-hold-master #define SLAVE_ADDRESS 0x80 //SHT20 I2C address volatile _Bool g_transmission_ended; //End of reception indication uint16_t temp_data,humidity_data; uint16_t readValue(uint8_t* const cmd); void delay(uint32_t t); /* End user code. Do not edit comment generated here */ void R_MAIN_UserInit(void); /*********************************************************************************************************************** * Function Name: main * Description : This function implements main function. * Arguments : None * Return Value : None ***********************************************************************************************************************/ void main(void) { R_MAIN_UserInit(); /* Start user code. Do not edit comment generated here */ while (1U) { temp_data = readValue(&SHT_command[0]); humidity_data = readValue(&SHT_command[1]); } /* End user code. Do not edit comment generated here */ } /*********************************************************************************************************************** * Function Name: R_MAIN_UserInit * Description : This function adds user code before implementing main function. * Arguments : None * Return Value : None ***********************************************************************************************************************/ void R_MAIN_UserInit(void) { /* Start user code. Do not edit comment generated here */ EI(); /* End user code. Do not edit comment generated here */ } /* Start user code for adding. Do not edit comment generated here */ uint16_t readValue(uint8_t* const cmd) { uint16_t rawValue; uint8_t rx_data[3];//msb, lsb, checksum; g_transmission_ended = 0;//Reset flag R_IIC00_Master_Send(SLAVE_ADDRESS,cmd,1);//Send command to device while(g_transmission_ended == 0); //Wait for data to be send g_transmission_ended = 0;//Reset the flag delay(20);//30us delay R_IIC00_StopCondition(); delay(250000);//100ms delay for data to be ready //Receive the data R_IIC00_Master_Receive(SLAVE_ADDRESS,rx_data,3); //Wait for SCL line to be released from the sensor while(g_transmission_ended == 0); //Wait for data to be received R_IIC00_StopCondition(); rawValue = ((uint16_t) rx_data[0] << 8) | (uint16_t) rx_data[1]; /* if(checkCRC(rawValue, rx_data[2]) != 0){ return (ERROR_BAD_CRC); } */ return rawValue; //& 0xFFFC; } void delay(uint32_t t){ for(uint32_t i = 0;i < t;i++){ NOP(); } } /* End user code. Do not edit comment generated here */
r_cg_serial_user.c :/*********************************************************************************************************************** * DISCLAIMER * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. * No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all * applicable laws, including copyright laws. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED * OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY * LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT, * INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR * ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability * of this software. By using this software, you agree to the additional terms and conditions found by accessing the * following link: * http://www.renesas.com/disclaimer * * Copyright (C) 2011, 2021 Renesas Electronics Corporation. All rights reserved. ***********************************************************************************************************************/ /*********************************************************************************************************************** * File Name : r_cg_serial_user.c * Version : CodeGenerator for RL78/G14 V2.05.06.02 [08 Nov 2021] * Device(s) : R5F104ML * Tool-Chain : CCRL * Description : This file implements device driver for Serial module. * Creation Date: 3/30/2024 ***********************************************************************************************************************/ /*********************************************************************************************************************** Includes ***********************************************************************************************************************/ #include "r_cg_macrodriver.h" #include "r_cg_serial.h" /* Start user code for include. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ #include "r_cg_userdefine.h" /*********************************************************************************************************************** Pragma directive ***********************************************************************************************************************/ #pragma interrupt r_iic00_interrupt(vect=INTIIC00) /* Start user code for pragma. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ /*********************************************************************************************************************** Global variables and functions ***********************************************************************************************************************/ extern volatile uint8_t g_iic00_master_status_flag; /* iic00 start flag for send address check by master mode */ extern volatile uint8_t * gp_iic00_tx_address; /* iic00 send data pointer by master mode */ extern volatile uint16_t g_iic00_tx_count; /* iic00 send data size by master mode */ extern volatile uint8_t * gp_iic00_rx_address; /* iic00 receive data pointer by master mode */ extern volatile uint16_t g_iic00_rx_count; /* iic00 receive data size by master mode */ extern volatile uint16_t g_iic00_rx_length; /* iic00 receive data length by master mode */ /* Start user code for global. Do not edit comment generated here */ extern volatile _Bool g_transmission_ended; //Indication of end of send or receive /* End user code. Do not edit comment generated here */ /*********************************************************************************************************************** * Function Name: r_iic00_interrupt * Description : This function is INTIIC00 interrupt service routine. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void __near r_iic00_interrupt(void) { volatile uint16_t w_count; /* Set delay to start next transmission. The delay time depend on slave device. Here set 20us as default base on current clock */ for (w_count = 0U; w_count <= IIC00_WAITTIME_2; w_count++) { NOP(); } if (((SSR00 & _0002_SAU_PARITY_ERROR) == 0x0002U) && (g_iic00_tx_count != 0U)) { SIR00 |= _0002_SAU_SIRMN_PECTMN; //clear ACK error detection flag R_IIC00_StopCondition(); r_iic00_callback_master_error(MD_NACK); } else if(((SSR00 & _0001_SAU_OVERRUN_ERROR) == 0x0001U) && (g_iic00_tx_count != 0U)) { SIR00 |= _0001_SAU_SIRMN_OVCTMN; //clear overrun error detection flag R_IIC00_StopCondition(); r_iic00_callback_master_error(MD_OVERRUN); } else { /* Control for master send */ if ((g_iic00_master_status_flag & _01_SAU_IIC_SEND_FLAG) == 1U) { if (g_iic00_tx_count > 0U) { SIO00 = *gp_iic00_tx_address; gp_iic00_tx_address++; g_iic00_tx_count--; } else { /* IIC master transmission finishes and a callback function can be called here. */ r_iic00_callback_master_sendend(); } } /* Control for master receive */ else { if ((g_iic00_master_status_flag & _04_SAU_IIC_SENDED_ADDRESS_FLAG) == 0U) { ST0 |= _0001_SAU_CH0_STOP_TRG_ON; SCR00 &= ~_C000_SAU_RECEPTION_TRANSMISSION; SCR00 |= _4000_SAU_RECEPTION; SS0 |= _0001_SAU_CH0_START_TRG_ON; g_iic00_master_status_flag |= _04_SAU_IIC_SENDED_ADDRESS_FLAG; if (g_iic00_rx_length == 1U) { SOE0 &= ~_0001_SAU_CH0_OUTPUT_ENABLE; /* disable IIC00 out */ } SIO00 = 0xFFU; } else { if (g_iic00_rx_count < g_iic00_rx_length) { *gp_iic00_rx_address = SIO00; gp_iic00_rx_address++; g_iic00_rx_count++; if (g_iic00_rx_count == (g_iic00_rx_length - 1U)) { SOE0 &= ~_0001_SAU_CH0_OUTPUT_ENABLE; /* disable IIC00 out */ SIO00 = 0xFFU; } else if (g_iic00_rx_count == g_iic00_rx_length) { /* IIC master reception finishes and a callback function can be called here. */ r_iic00_callback_master_receiveend(); } else { SIO00 = 0xFFU; } } } } } } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_error * Description : This function is a callback function when IIC00 master error occurs. * Arguments : flag - * status flag * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_error(MD_STATUS flag) { /* Start user code. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_receiveend * Description : This function is a callback function when IIC00 finishes master reception. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_receiveend(void) { /* Start user code. Do not edit comment generated here */ g_transmission_ended = 1; /* End user code. Do not edit comment generated here */ } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_sendend * Description : This function is a callback function when IIC00 finishes master transmission. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_sendend(void) { /* Start user code. Do not edit comment generated here */ g_transmission_ended = 1; /* End user code. Do not edit comment generated here */ } /* Start user code for adding. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */
/*********************************************************************************************************************** * DISCLAIMER * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. * No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all * applicable laws, including copyright laws. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED * OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY * LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT, * INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR * ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability * of this software. By using this software, you agree to the additional terms and conditions found by accessing the * following link: * http://www.renesas.com/disclaimer * * Copyright (C) 2011, 2021 Renesas Electronics Corporation. All rights reserved. ***********************************************************************************************************************/ /*********************************************************************************************************************** * File Name : r_cg_serial_user.c * Version : CodeGenerator for RL78/G14 V2.05.06.02 [08 Nov 2021] * Device(s) : R5F104ML * Tool-Chain : CCRL * Description : This file implements device driver for Serial module. * Creation Date: 3/30/2024 ***********************************************************************************************************************/ /*********************************************************************************************************************** Includes ***********************************************************************************************************************/ #include "r_cg_macrodriver.h" #include "r_cg_serial.h" /* Start user code for include. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ #include "r_cg_userdefine.h" /*********************************************************************************************************************** Pragma directive ***********************************************************************************************************************/ #pragma interrupt r_iic00_interrupt(vect=INTIIC00) /* Start user code for pragma. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ /*********************************************************************************************************************** Global variables and functions ***********************************************************************************************************************/ extern volatile uint8_t g_iic00_master_status_flag; /* iic00 start flag for send address check by master mode */ extern volatile uint8_t * gp_iic00_tx_address; /* iic00 send data pointer by master mode */ extern volatile uint16_t g_iic00_tx_count; /* iic00 send data size by master mode */ extern volatile uint8_t * gp_iic00_rx_address; /* iic00 receive data pointer by master mode */ extern volatile uint16_t g_iic00_rx_count; /* iic00 receive data size by master mode */ extern volatile uint16_t g_iic00_rx_length; /* iic00 receive data length by master mode */ /* Start user code for global. Do not edit comment generated here */ extern volatile _Bool g_transmission_ended; //Indication of end of send or receive /* End user code. Do not edit comment generated here */ /*********************************************************************************************************************** * Function Name: r_iic00_interrupt * Description : This function is INTIIC00 interrupt service routine. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void __near r_iic00_interrupt(void) { volatile uint16_t w_count; /* Set delay to start next transmission. The delay time depend on slave device. Here set 20us as default base on current clock */ for (w_count = 0U; w_count <= IIC00_WAITTIME_2; w_count++) { NOP(); } if (((SSR00 & _0002_SAU_PARITY_ERROR) == 0x0002U) && (g_iic00_tx_count != 0U)) { SIR00 |= _0002_SAU_SIRMN_PECTMN; //clear ACK error detection flag R_IIC00_StopCondition(); r_iic00_callback_master_error(MD_NACK); } else if(((SSR00 & _0001_SAU_OVERRUN_ERROR) == 0x0001U) && (g_iic00_tx_count != 0U)) { SIR00 |= _0001_SAU_SIRMN_OVCTMN; //clear overrun error detection flag R_IIC00_StopCondition(); r_iic00_callback_master_error(MD_OVERRUN); } else { /* Control for master send */ if ((g_iic00_master_status_flag & _01_SAU_IIC_SEND_FLAG) == 1U) { if (g_iic00_tx_count > 0U) { SIO00 = *gp_iic00_tx_address; gp_iic00_tx_address++; g_iic00_tx_count--; } else { /* IIC master transmission finishes and a callback function can be called here. */ r_iic00_callback_master_sendend(); } } /* Control for master receive */ else { if ((g_iic00_master_status_flag & _04_SAU_IIC_SENDED_ADDRESS_FLAG) == 0U) { ST0 |= _0001_SAU_CH0_STOP_TRG_ON; SCR00 &= ~_C000_SAU_RECEPTION_TRANSMISSION; SCR00 |= _4000_SAU_RECEPTION; SS0 |= _0001_SAU_CH0_START_TRG_ON; g_iic00_master_status_flag |= _04_SAU_IIC_SENDED_ADDRESS_FLAG; if (g_iic00_rx_length == 1U) { SOE0 &= ~_0001_SAU_CH0_OUTPUT_ENABLE; /* disable IIC00 out */ } SIO00 = 0xFFU; } else { if (g_iic00_rx_count < g_iic00_rx_length) { *gp_iic00_rx_address = SIO00; gp_iic00_rx_address++; g_iic00_rx_count++; if (g_iic00_rx_count == (g_iic00_rx_length - 1U)) { SOE0 &= ~_0001_SAU_CH0_OUTPUT_ENABLE; /* disable IIC00 out */ SIO00 = 0xFFU; } else if (g_iic00_rx_count == g_iic00_rx_length) { /* IIC master reception finishes and a callback function can be called here. */ r_iic00_callback_master_receiveend(); } else { SIO00 = 0xFFU; } } } } } } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_error * Description : This function is a callback function when IIC00 master error occurs. * Arguments : flag - * status flag * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_error(MD_STATUS flag) { /* Start user code. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */ } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_receiveend * Description : This function is a callback function when IIC00 finishes master reception. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_receiveend(void) { /* Start user code. Do not edit comment generated here */ g_transmission_ended = 1; /* End user code. Do not edit comment generated here */ } /*********************************************************************************************************************** * Function Name: r_iic00_callback_master_sendend * Description : This function is a callback function when IIC00 finishes master transmission. * Arguments : None * Return Value : None ***********************************************************************************************************************/ static void r_iic00_callback_master_sendend(void) { /* Start user code. Do not edit comment generated here */ g_transmission_ended = 1; /* End user code. Do not edit comment generated here */ } /* Start user code for adding. Do not edit comment generated here */ /* End user code. Do not edit comment generated here */
Although I took a poor approach to making a delay, that's the way it works without using clock stretching. and you might need to modify it, it should work now for IIC00, I've tested it on my hardware. but I think it's more convenient to use the IICA unit which can handle that delay considering clock stretching.
Regards.
Thak you for you reply.
at which speed you have tried this code??
my main clock is 12Mhz and I2C speed is 100000 bps.
Thank you.