This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

  • Resolved

CC1310: Sensor Controller Studio / Code Generation

Prodigy 130 points

Replies: 10

Views: 102

Part Number: CC1310

In my project i use i2c for accessing an acceleration sensor. A quick look to the generated code (lst-file) shows horrible code: Many redundant load and stores.

I have a marked all redundant load/store in yellow, and other options for better code in orange.

SCS-Source-Code:

macro setMpuReg( reg, val) {
    i2cStart();
    i2cTx( MPU_ADDR | I2C_OP_WRITE);
    if (state.i2cStatus == 0x0000) {
        i2cTx( reg );
    }
    if (state.i2cStatus == 0x0000) {
        i2cTx( val );
    }
    i2cStop();
}
// LowPower-Mode with ACC only
setMpuReg( MPU_REG_PM1, MPU_CFG_PM1);           // cycle+dis-temp+best-osc
setMpuReg( MPU_REG_PM2, MPU_CFG_PM2);           // WupFreq=40Hz, Acc:on, Gyro:off
... and more settings of registers

Generated Code :

               ;?     // LowPower-Mode with ACC only
               ;?     setMpuReg( MPU_REG_PM1, MPU_CFG_PM1);           // cycle+dis-temp+best-osc
               ;?     >   i2cStart();
014e ---- 6000                             ld          R6, #0x0000
014f ---- 16a6                             jsr         I2cStart
0150 ---- 6c9a                             st          R6, [#accsampling/state/i2cStatus]           // no further useof state.i2cStatus
               ;?     >   i2cTx( MPU_ADDR | I2C_OP_WRITE);
0151 ---- 70d0                             ld          R7, #208
0152 ---- 689a                             ld          R6, [#accsampling/state/i2cStatus]          // value already in R6 
0153 ---- 16bd                             jsr         I2cTxByte
0154 ---- 6c9a                             st          R6, [#accsampling/state/i2cStatus]
               ;?     >   if (state.i2cStatus == 0x0000) {
0155 ---- 089a                             ld          R0, [#accsampling/state/i2cStatus]         // use value in R6 for compare
0156 ---- 8a00                             cmp         R0, #0
0157 ---- be04                             bneq        /id0257
               ;?     >       i2cTx( reg );
0158 ---- 706b                                 ld          R7, #107
0159 ---- 689a                                 ld          R6, [#accsampling/state/i2cStatus]     // value already in R6
015a ---- 16bd                                 jsr         I2cTxByte
015b ---- 6c9a                                 st          R6, [#accsampling/state/i2cStatus]
               ;?     >   }
               /id0257:
               ;?     >   if (state.i2cStatus == 0x0000) {
015c ---- 089a                             ld          R0, [#accsampling/state/i2cStatus]        // use value in R6 for compare
015d ---- 8a00                             cmp         R0, #0
015e ---- be04                             bneq        /id0263
               ;?     >       i2cTx( val );
015f ---- 7029                                 ld          R7, #41
0160 ---- 689a                                 ld          R6, [#accsampling/state/i2cStatus]    // value already in R6
0161 ---- 16bd                                 jsr         I2cTxByte
0162 ---- 6c9a                                 st          R6, [#accsampling/state/i2cStatus]   // no further use of state.i2cStatus
               ;?     >   }
               /id0263:
               ;?     >   i2cStop();
0163 ---- 689a                             ld          R6, [#accsampling/state/i2cStatus]       // value already in R6
0164 ---- 16db                             jsr         I2cStop
0165 ---- 6c9a                             st          R6, [#accsampling/state/i2cStatus]
               ;?     setMpuReg( MPU_REG_PM2, MPU_CFG_PM2);           // WupFreq=40Hz, Acc:on, Gyro:off
... same as above

With some soft optimization rom 24 instructions 6 could simply removed and 2 instructions could removed with changing the compares -> Code-size reduced by 1/3
All this could be done with simple peep-hole optimisation.

Are there any plans to improve the SCS codegenerator with a simple peep-hole optimizer?

To generate better code i can create special procedures in asm, better i can't find any documentation about the code-generation templates.

My wishes:

  • Better codegenerator with optimizer
  • Better documentation of CG (.prd-files and .asm-files)
  • Integration of SCS into CCS

  • Guru 227890 points
    The compiler currently optimizes:
    - Register copying
    - Dead code

    There has been more focus on generating correct code than on generating highly optimized code. Also, since many applications use self-timed code, we want to keep execution time for existing code as constant as possible.

    Note that your code can be optimized as follows (since i2cTx() does nothing if one or more status bits are set):
    macro setMpuReg( reg, val) {
    i2cStart();
    i2cTx( MPU_ADDR | I2C_OP_WRITE);
    i2cTx( reg );
    i2cTx( val );
    i2cStop();
    }

    There are plans to create a help document on resource and procedure definitions (that is, how to add your own or modify existing). The current documentation consist of DTD files, and the existing definition files as examples. So far we have not "encouraged" users to write their own definitions, as there is a significant support effort from our side.

    The are many reasons why Sensor Controller Studio is not integrated into CCS, most importantly that it also needs to work with other IDEs such as IAR. There may be better integration with CCS in the future through SysConfig, but there are limitations in the current SysConfig framework that prevent this.

     

  • In reply to TER:

    Thank you for your fast reply.
    That i can remove the checks for i2cStatus is a usefull hint.

    After removing the checks for i2cStatus the assembler code for my (simple) task is excessive large.

    In Initialization i setup some registers of the Acc-Sensor.
    In Execution i read 3 or 7 16-bit values from device.
    In Termination i write 2 regsiters in device to activate standby/sleep
    In summary the Task-Code is 447 bytes. Almost half of the SC-memory in CC1310

    For codesize reduction 2 additional i2c-functions would be usefull:
    - i2cSetRegister( i2cAddr, regAddr, regValue)
    - i2cRxWord( value )

    Can you give some advise how to implement this functions via .prd-/.asm-file ?

    I think at least the function i2cSetRegister() would be usefull for many people, because this is wide spread standard pattern for I2C peripherals.
  • Guru 227890 points

    In reply to Jan Ingwer Baer:

    Is the main objective here to be sure that your code fits in the available code space?

     

  • In reply to TER:

    Yes, i have problems to fit the code+data in the available space. With the big code there is not enough room for a second task and the data.

  • Guru 227890 points

    In reply to Jan Ingwer Baer:

    Would running the two tasks from a separate RAM image solve the problem?

     

  • In reply to TER:

    A better solution would be to reduce the code size.
    In my application using functions for the most common cases (set register in i2c-device and reading a 16-bit word) is the best solution. That is why i asked for support in creating this functions.
  • Guru 227890 points

    In reply to Jan Ingwer Baer:

    There are limitations in hardware that makes this slightly more difficult than it sounds. The Sensor Controller CPU core has a program counter stack in hardware that allows for three levels of subroutine calls. For the I2C case, the framework uses one level to get to the task code, the i2cXxxxx() procedure calls use another level, and then the I2C procedures use the final level internally. This means that you cannot add another level of subroutine calls using the "jsr" instruction.

    It is possible to use the indirect jump "jmp R0" instruction to get around this limitation, with some added complexity (= overhead) for each procedure call.

    It sounds like a good idea to provide more complex operations such as i2cWriteReg and i2cReadReg, with support for 8- and 16-bit values, so we will look into it.

    For now, the best way to optimize your code, I think, is to put your address/value pairs in data structure array members, and do the register accesses in loop(s). If you do sequences of many writes or many reads this method should give the smallest code size. It also enables you to setup the register accesses from the System CPU application.

     

  • In reply to TER:

    Ok, i understand the problems with the limited stack space.

    As a quick solution to reduce code size i have created a prd-file that put all the code inline. With this i have reduced the code for simple 8-bit register set from 18 instructions to 10 instructions. The i2c address of the device has always to be immediate, but register address and value can be immediate or register.

    Together with your suggestion to use a loop for initialisation it will reduce the code size enough.

    Here is my .prd-file:

    i2c_write_register_byte.prd.txt

    Next step is to create a function i2_rx_word(). If it works i will post my code for use by other.

  • Guru 227890 points

    In reply to Jan Ingwer Baer:

    Thanks. I will mark the thread as resolved for now but feel free to post code if you get it up and running.

     

  • In reply to Jan Ingwer Baer:

    With some custom .prd-files, a custom asm procedure and some looping i have reduced my memory usage.

    Task Description: MPU-6050 init, get values triggered by RTC-ticks, deinit

    Original Task: 482 words (445 Code+37 Data) + 107 words procedure library

    Task with custom prd+asm : 305 words (268 Code + 37 Data) + 128 words procedure library

    Task with custom prd+asm and loops : 200 words (138 Code + 62 Data) + 128 words procedure library

    For information (and possible help for other users) i attache my custom files.

    7343.i2c_write_register_byte.prd.txt

    i2c_rx_word_nack.prd.txt

    i2c_rx_word_ack.prd.txt

    i2c_rx_word.asm

    i2c_read_register_start.prd.txt

This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.