[SOLVED] JLINK_SWD_ReadWriteBits often crashes jlink and doesn't send the right data

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] JLINK_SWD_ReadWriteBits often crashes jlink and doesn't send the right data

    I have a Jlinkscript:

    C Source Code

    1. int InitTarget(void) {
    2. U8 pDataIn[2];
    3. const U8 pDataOut[2];
    4. const U8 pDirection[2];
    5. pDirection[0] = 0;
    6. pDataOut[0] = 1;
    7. pDirection[1] = 1;
    8. pDataOut[1] = 1;
    9. JLINK_SWD_ReadWriteBits(pDataOut, pDirection, pDataIn, 2);
    10. return 1;
    11. }
    Display All

    When adding this to jlink via the commands line:

    Source Code

    1. jlink -JLinkScriptFile .\minimal.JLinkScript -device cortex-M0 -if SWD -speed 1000
    jlink will often crash back to the terminal when I enter the "connect" command which triggers InitTarget to be run. I also checked this with ResetTarget and it also crashed.
    Depending on the data specified in pDataOut, jlink may succeed in executing the command but when I check the data with a logic analyser, it does not match the pDataOut array.

    What is the proper usage of this function? It would be good if there was any example code for this function in the docs.
  • Hi,
    There are two issues with the code you linked,
    one which is causing the crash and the other, will result in behavior that you might not intent.

    1) Reason for the crash:
    The language of the J-Link Script files is not 100% compatible to C, but only "C like".
    Passing pointers of an array to a function does not work the way you do it.

    Pointers of arrays have to be passed as follows:

    C Source Code

    1. JLINK_SWD_ReadWriteBits(&pDataOut[0], &pDirection[0], &pDataIn[0], 2);

    Could you please check if this works in your case?

    We will adjust the documentation so that this becomes more clear.

    2) Usage of JLINK_SWD_ReadWriteBits():
    In your code snipped, JLINK_SWD_ReadWriteBits is not used correctly.
    I added an example to our wiki which should help you out:
    wiki.segger.com/J-Link_script_…ing_data_via_SWD_manually

    BR
    Fabian
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Thanks Fabian. That has solved everything.

    Since you can edit the wiki... ideally the function's description would fully define the operation. It would include details such as:
    1. Are 8 bits packed into each byte of the input/output/direction arrays, or it is 1 bit per index?
    2. What direction value corresponds to input/output?
    3. Is the data in the byte sent LSB first or MSB first?
    Of course I know these answers now. They would be helpful for someone who is new to using this function.

    Thanks again.