RX231のコード生成を用いた簡易IIC通信について

電流・電圧測定デバイスであるINA233を用いて電圧値を測定し,測定値を簡易IIC通信でRX231に送るプログラムを作成しておりますが上手くいきません.

RX231シリーズ R5F52316ADFM 

ROM:256Kbyte RAM 32Kbyte  外部発振20MHzを使用しています.

コード生成はSCI12を簡易IICバスに設定し,生成しました.

void main(void)
{
    R_MAIN_UserInit();
    /* Start user code. Do not edit comment generated here */
   {
    uint8_t     adr         =0      ;//7bit address
 uint8_t     dir         =0      ;//0:Write 1:Read
 
 uint8_t  tx_buf[255] = {0};//send data
     uint8_t  rx_buf[255] = {0};//receive data
 
 while(1){
  adr = 0x41;
  dir = 0x00;
  adr = (adr<<1) | (dir & 0x00);
  tx_buf[0] = 0x88; //READ_VIN PMBusコード ina233
  tx_buf[1] = 0x89; //READ_IN PMBusコード ina233
    
  //PMBusコマンド ina233に送信
  R_SCI12_Create();
  R_SCI12_Start();
  R_SCI12_IIC_Master_Send(adr,&tx_buf[0],3);
  R_SCI12_Stop();
 
  adr = 0x41;
  dir = 0x01;
  adr = (adr<<1) | (dir & 0x00);
 
  //ina233からデータ取得
  R_SCI12_Start();
  R_SCI12_IIC_Master_Receive(adr,rx_buf,3);
  R_SCI12_Stop();
 }
   }
  
    /* End user code. Do not edit comment generated here */
}

CS+の設定等もよく分からないのでその辺りも教えていただけるとありがたいです.

初歩的な質問ですいませんが,よろしくお願いいたします.

//INA233のデータシート

www.ti.com/.../ina233.pdf

Parents
  • fiReさん、こんにちは。NoMaYです。

    チョコさんが仰っていたこと(以下)をコードにしてr_cg_sci_user.cのユーザ記述部に書くと以下のようになります。これを使ってr_cg_main.cを以下のように書き換えたらどうなりますでしょうか?(参考用に当方で試しに書き換えたプロジェクトのファイル一式を添付します。ただ、過去のプロジェクトのファイルを流用したのでresetprg.cが最新版では無かったりするので、参考にするぐらいにして下さい。また、相手側デバイスを持ち合わせていません(実はI2C全般で持ち合わせていません)ので、動作は未確認です。すみません。)


    SCI12の割り込み処理のコードを参照して,指定したデータ数の転送が完了したところで,何かコールバック処理をやっていると思うので,そこで通信完了フラグをセットして,そのフラグを確認するような通信待ちを行ってみてください。


    プロジェクトのファイル一式
    issue_20200214.zip    293KB

    今回、送信完了/受信完了を待つ送信関数名/受信関数名の末尾に _UWT を付けてみました。(unlimited wait timeの意。また、先頭を R_ ではなくて U_ にしてあります。ちなみに、STM32CubeMXでは送信完了/受信完了を待たない関数の名前の末尾に _IT を付けるようでしたので、逆向きに(?)真似てみました。) なお、スレーブアドレスは7ビットアドレスを受け取るようにしました。

    cg_src/r_cg_sci_user.c

    /******************************************************************************
    Global variables and functions
    ******************************************************************************/
    extern uint8_t   g_sci12_iic_transmit_receive_flag;  /* SCI12 transmit receive flag for I2C */
    extern uint8_t   g_sci12_iic_cycle_flag;             /* SCI12 start stop flag for I2C */
    extern uint8_t   g_sci12_slave_address;              /* SCI12 target slave address */
    extern uint8_t * gp_sci12_tx_address;                /* SCI12 send buffer address */
    extern uint16_t  g_sci12_tx_count;                   /* SCI12 send data number */
    extern uint8_t * gp_sci12_rx_address;                /* SCI12 receive buffer address */
    extern uint16_t  g_sci12_rx_count;                   /* SCI12 receive data number */
    extern uint16_t  g_sci12_rx_length;                  /* SCI12 receive data length */
    /* Start user code for global. Do not edit comment generated here */
    volatile uint8_t g_sci12_tx_ready_flag;              /* SCI12 send end flag */
    volatile uint8_t g_sci12_rx_ready_flag;              /* SCI12 receive end flag */
    /* End user code. Do not edit comment generated here */
    static void r_sci12_callback_transmitend(void)
    {
        /* Start user code. Do not edit comment generated here */

        g_sci12_tx_ready_flag = 1;

        /* End user code. Do not edit comment generated here */
    }
    static void r_sci12_callback_receiveend(void)
    {
        /* Start user code. Do not edit comment generated here */

        g_sci12_rx_ready_flag = 1;

        /* End user code. Do not edit comment generated here */
    }
    /* Start user code for adding. Do not edit comment generated here */

    /******************************************************************************
    * Function Name: U_SCI12_IIC_Master_Send
    * Description  : This function sends IIC12 data to slave device. (unlimited wait time)
    * Arguments    : same as R_SCI12_IIC_Master_Send except for adr
    *                adr -
    *                    slave device '7-bit' address (Not '8-bit' address)
    * Return Value : same as R_SCI12_IIC_Master_Send
    ******************************************************************************/
    void U_SCI12_IIC_Master_Send_UWT(uint8_t adr, uint8_t * const tx_buf, uint16_t tx_num)
    {
        R_SCI12_IIC_Master_Send( (uint8_t)(adr << 1), tx_buf, tx_num );
        do{}while (g_sci12_tx_ready_flag != 1);
        g_sci12_tx_ready_flag = 0;
    }

    /******************************************************************************
    * Function Name: R_SCI12_IIC_Master_Receive
    * Description  : This function receives IIC12 data from slave device. (unlimited wait time)
    * Arguments    : same as R_SCI12_IIC_Master_Receive except for adr
    *                adr -
    *                    slave device '7-bit' address (Not '8-bit' address)
    * Return Value : same as R_SCI12_IIC_Master_Receive
    ******************************************************************************/
    void U_SCI12_IIC_Master_Receive_UWT(uint8_t adr, uint8_t * const rx_buf, uint16_t rx_num)
    {
        R_SCI12_IIC_Master_Send( (uint8_t)((adr << 1) | 1), rx_buf, rx_num );
        do{}while (g_sci12_rx_ready_flag != 1);
        g_sci12_rx_ready_flag = 0;

    }

    /* End user code. Do not edit comment generated here */

    cg_src/r_cg_sci.h

    /* Start user code for function. Do not edit comment generated here */

    void U_SCI12_IIC_Master_Send_UWT(uint8_t adr, uint8_t * const tx_buf, uint16_t tx_num);
    void U_SCI12_IIC_Master_Receive_UWT(uint8_t adr, uint8_t * const rx_buf, uint16_t rx_num);

    /* End user code. Do not edit comment generated here */

    cg_src/r_cg_main.c

    void main(void)
    {
        R_MAIN_UserInit();
        /* Start user code. Do not edit comment generated here */
        {
            uint8_t adr = 0;//7bit address

            uint8_t  tx_buf[255] = {0};//send data
            uint8_t  rx_buf[255] = {0};//receive data

            R_SCI12_Start();

            while(1)
            {
                adr = 0x41;
                tx_buf[0] = 0xE0; //TI_MFR_ID PMBusコード ina233

                //PMBusコマンド ina233に送信
                U_SCI12_IIC_Master_Send_UWT( adr, tx_buf, 1 );

                //ina233からデータ取得
                U_SCI12_IIC_Master_Receive_UWT( adr, rx_buf, 2 );

                //デバッグ時にブレークポイントやアクションイベントを設定する箇所
                //ここまで来たら rx_buf[0] = 'T', rx_buf[1] = 'I' となっている筈
                nop();
            }
        }
        /* End user code. Do not edit comment generated here */
    }

    以下、作成した時の資料です。(自分自身への備忘録としてでもあります。)

    INA233データシートの画面コピー
    www.ti.com/jp/lit/ds/symlink/ina233.pdf



    CS+のコード生成機能の設定画面




    CS+のコード生成機能のヘルプのSCIのAPIの使用例の画面コピー


     

Reply
  • fiReさん、こんにちは。NoMaYです。

    チョコさんが仰っていたこと(以下)をコードにしてr_cg_sci_user.cのユーザ記述部に書くと以下のようになります。これを使ってr_cg_main.cを以下のように書き換えたらどうなりますでしょうか?(参考用に当方で試しに書き換えたプロジェクトのファイル一式を添付します。ただ、過去のプロジェクトのファイルを流用したのでresetprg.cが最新版では無かったりするので、参考にするぐらいにして下さい。また、相手側デバイスを持ち合わせていません(実はI2C全般で持ち合わせていません)ので、動作は未確認です。すみません。)


    SCI12の割り込み処理のコードを参照して,指定したデータ数の転送が完了したところで,何かコールバック処理をやっていると思うので,そこで通信完了フラグをセットして,そのフラグを確認するような通信待ちを行ってみてください。


    プロジェクトのファイル一式
    issue_20200214.zip    293KB

    今回、送信完了/受信完了を待つ送信関数名/受信関数名の末尾に _UWT を付けてみました。(unlimited wait timeの意。また、先頭を R_ ではなくて U_ にしてあります。ちなみに、STM32CubeMXでは送信完了/受信完了を待たない関数の名前の末尾に _IT を付けるようでしたので、逆向きに(?)真似てみました。) なお、スレーブアドレスは7ビットアドレスを受け取るようにしました。

    cg_src/r_cg_sci_user.c

    /******************************************************************************
    Global variables and functions
    ******************************************************************************/
    extern uint8_t   g_sci12_iic_transmit_receive_flag;  /* SCI12 transmit receive flag for I2C */
    extern uint8_t   g_sci12_iic_cycle_flag;             /* SCI12 start stop flag for I2C */
    extern uint8_t   g_sci12_slave_address;              /* SCI12 target slave address */
    extern uint8_t * gp_sci12_tx_address;                /* SCI12 send buffer address */
    extern uint16_t  g_sci12_tx_count;                   /* SCI12 send data number */
    extern uint8_t * gp_sci12_rx_address;                /* SCI12 receive buffer address */
    extern uint16_t  g_sci12_rx_count;                   /* SCI12 receive data number */
    extern uint16_t  g_sci12_rx_length;                  /* SCI12 receive data length */
    /* Start user code for global. Do not edit comment generated here */
    volatile uint8_t g_sci12_tx_ready_flag;              /* SCI12 send end flag */
    volatile uint8_t g_sci12_rx_ready_flag;              /* SCI12 receive end flag */
    /* End user code. Do not edit comment generated here */
    static void r_sci12_callback_transmitend(void)
    {
        /* Start user code. Do not edit comment generated here */

        g_sci12_tx_ready_flag = 1;

        /* End user code. Do not edit comment generated here */
    }
    static void r_sci12_callback_receiveend(void)
    {
        /* Start user code. Do not edit comment generated here */

        g_sci12_rx_ready_flag = 1;

        /* End user code. Do not edit comment generated here */
    }
    /* Start user code for adding. Do not edit comment generated here */

    /******************************************************************************
    * Function Name: U_SCI12_IIC_Master_Send
    * Description  : This function sends IIC12 data to slave device. (unlimited wait time)
    * Arguments    : same as R_SCI12_IIC_Master_Send except for adr
    *                adr -
    *                    slave device '7-bit' address (Not '8-bit' address)
    * Return Value : same as R_SCI12_IIC_Master_Send
    ******************************************************************************/
    void U_SCI12_IIC_Master_Send_UWT(uint8_t adr, uint8_t * const tx_buf, uint16_t tx_num)
    {
        R_SCI12_IIC_Master_Send( (uint8_t)(adr << 1), tx_buf, tx_num );
        do{}while (g_sci12_tx_ready_flag != 1);
        g_sci12_tx_ready_flag = 0;
    }

    /******************************************************************************
    * Function Name: R_SCI12_IIC_Master_Receive
    * Description  : This function receives IIC12 data from slave device. (unlimited wait time)
    * Arguments    : same as R_SCI12_IIC_Master_Receive except for adr
    *                adr -
    *                    slave device '7-bit' address (Not '8-bit' address)
    * Return Value : same as R_SCI12_IIC_Master_Receive
    ******************************************************************************/
    void U_SCI12_IIC_Master_Receive_UWT(uint8_t adr, uint8_t * const rx_buf, uint16_t rx_num)
    {
        R_SCI12_IIC_Master_Send( (uint8_t)((adr << 1) | 1), rx_buf, rx_num );
        do{}while (g_sci12_rx_ready_flag != 1);
        g_sci12_rx_ready_flag = 0;

    }

    /* End user code. Do not edit comment generated here */

    cg_src/r_cg_sci.h

    /* Start user code for function. Do not edit comment generated here */

    void U_SCI12_IIC_Master_Send_UWT(uint8_t adr, uint8_t * const tx_buf, uint16_t tx_num);
    void U_SCI12_IIC_Master_Receive_UWT(uint8_t adr, uint8_t * const rx_buf, uint16_t rx_num);

    /* End user code. Do not edit comment generated here */

    cg_src/r_cg_main.c

    void main(void)
    {
        R_MAIN_UserInit();
        /* Start user code. Do not edit comment generated here */
        {
            uint8_t adr = 0;//7bit address

            uint8_t  tx_buf[255] = {0};//send data
            uint8_t  rx_buf[255] = {0};//receive data

            R_SCI12_Start();

            while(1)
            {
                adr = 0x41;
                tx_buf[0] = 0xE0; //TI_MFR_ID PMBusコード ina233

                //PMBusコマンド ina233に送信
                U_SCI12_IIC_Master_Send_UWT( adr, tx_buf, 1 );

                //ina233からデータ取得
                U_SCI12_IIC_Master_Receive_UWT( adr, rx_buf, 2 );

                //デバッグ時にブレークポイントやアクションイベントを設定する箇所
                //ここまで来たら rx_buf[0] = 'T', rx_buf[1] = 'I' となっている筈
                nop();
            }
        }
        /* End user code. Do not edit comment generated here */
    }

    以下、作成した時の資料です。(自分自身への備忘録としてでもあります。)

    INA233データシートの画面コピー
    www.ti.com/jp/lit/ds/symlink/ina233.pdf



    CS+のコード生成機能の設定画面




    CS+のコード生成機能のヘルプのSCIのAPIの使用例の画面コピー


     

Children
No Data