cancel
Showing results for 
Search instead for 
Did you mean: 

SPI STM32 Master Nano Slave possibly 16-bit -> 8-bit issues

bibble235
Associate III

I am guessing having read forum after forum that this is an old topic

I am trying to get 

https://github.com/rayruu/MasteringMCU-guide/blob/master/Resources/Arduino/spi/002SPISlaveCmdHandling/002SPISlaveCmdHandling.ino

https://github.com/rayruu/MasteringMCU-guide/blob/master/Resources/Source_code/Workspace/stm32f4xx_drivers/src/008spi_cmd_handling.c

to work on my STM32F302R8 and a nano

I have tried,

Changing receive and send to have volitile

pRxBuffer = *(volatile uint8_t *)&pSPIx->DR;
and
*(volatile uint8_t *)&pSPIx->DR = *pTxBuffer;

I have tried the 

SPI_FRXTHConfig(SPI2, ENABLE);
 
But have not managed to get this to work. The send works fine but the receive fails. I am sure it is a mixing of 8-bit and 16-bit somewhere because I remember doing the packing to get the send to work. I wonder if there is something I need to do on the nano end.
 
Sorry to repeat and promise to put in my own github (lab) when working.
 
I will get a scope on it at the weekend but time is not available tonight.
 
Thanks
Iain

 

1 ACCEPTED SOLUTION

Accepted Solutions
bibble235
Associate III

You as promised I have uncovered the problems getting this to work

1/ The STM32F302R8TX_FLASH.ld does indeed send to bytes

To stop it doing this you need to change the write as others have suggested with

*(volatile uint8_t *)&pSPIx->DR = *pTxBuffer;
// pSPIx->DR =   *pTxBuffer;

2/ You need to change the FRXTH in the CR2 register to be enabled

3/ You cannot use the debugger as this reads the register itself and produces different register states as a result

I managed in the end to use semihosting which provides the ability to use printf with OpenOCD and gdb. In order to use this you have to

a) remove syscalls.c from the build
b) update STM32F302R8TX_FLASH.ld to define __end__ (the build will fail with __end__ undefined otherwise)

._user_heap_stack :
  {
    . = ALIGN(8);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    PROVIDE ( __end__ = . ); 
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(8);
  } >RAM

c) make sure STM32F302R8TX_FLASH.ld is linked
Now you should be able to run openocd in vs code with this launch.json

    {
      "name": "OpenOCD",
      "type": "cppdbg",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "program": "${command:cmake.launchTargetPath}",
      "args": [],
      "stopAtEntry": false,
      "environment": [],
      "externalConsole": false,
      "filterStderr": true,
      "filterStdout": false,
      "logging": {
        "moduleLoad": true,
        "trace": true,
        "engineLogging": true,
        "programOutput": true,
        "exceptions": false
      },
      "linux": {
        "MIMode": "gdb",
        "miDebuggerPath": "arm-none-eabi-gdb",
        "debugServerPath": "openocd",
        "debugServerArgs": "-f ${workspaceRoot}/test.cfg -c init -c \"reset init\"",
        "setupCommands": [
          { "text": "-environment-cd ${workspaceRoot}/build" },
          {
            "text": "-target-select remote localhost:3333",
            "description": "connect to target",
            "ignoreFailures": false
          },
          {
            "text": "-file-exec-and-symbols /home/jbond/course01.elf",
            "description": "load file",
            "ignoreFailures": false
          },
          {
            "text": "-interpreter-exec console \"monitor reset\"",
            "ignoreFailures": false
          },
          {
            "text": "-interpreter-exec console \"monitor halt\"",
            "ignoreFailures": false
          },
          {
            "text": "-interpreter-exec console \"monitor arm semihosting enable\"",
            "ignoreFailures": false
          }
        ]
      }
    },

Remember to reverse this out when done. 

The aim is hopefully to help someone, so they can build new great software. I would get this far without people who helped me.

Thanks

View solution in original post

1 REPLY 1
bibble235
Associate III

You as promised I have uncovered the problems getting this to work

1/ The STM32F302R8TX_FLASH.ld does indeed send to bytes

To stop it doing this you need to change the write as others have suggested with

*(volatile uint8_t *)&pSPIx->DR = *pTxBuffer;
// pSPIx->DR =   *pTxBuffer;

2/ You need to change the FRXTH in the CR2 register to be enabled

3/ You cannot use the debugger as this reads the register itself and produces different register states as a result

I managed in the end to use semihosting which provides the ability to use printf with OpenOCD and gdb. In order to use this you have to

a) remove syscalls.c from the build
b) update STM32F302R8TX_FLASH.ld to define __end__ (the build will fail with __end__ undefined otherwise)

._user_heap_stack :
  {
    . = ALIGN(8);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    PROVIDE ( __end__ = . ); 
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(8);
  } >RAM

c) make sure STM32F302R8TX_FLASH.ld is linked
Now you should be able to run openocd in vs code with this launch.json

    {
      "name": "OpenOCD",
      "type": "cppdbg",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "program": "${command:cmake.launchTargetPath}",
      "args": [],
      "stopAtEntry": false,
      "environment": [],
      "externalConsole": false,
      "filterStderr": true,
      "filterStdout": false,
      "logging": {
        "moduleLoad": true,
        "trace": true,
        "engineLogging": true,
        "programOutput": true,
        "exceptions": false
      },
      "linux": {
        "MIMode": "gdb",
        "miDebuggerPath": "arm-none-eabi-gdb",
        "debugServerPath": "openocd",
        "debugServerArgs": "-f ${workspaceRoot}/test.cfg -c init -c \"reset init\"",
        "setupCommands": [
          { "text": "-environment-cd ${workspaceRoot}/build" },
          {
            "text": "-target-select remote localhost:3333",
            "description": "connect to target",
            "ignoreFailures": false
          },
          {
            "text": "-file-exec-and-symbols /home/jbond/course01.elf",
            "description": "load file",
            "ignoreFailures": false
          },
          {
            "text": "-interpreter-exec console \"monitor reset\"",
            "ignoreFailures": false
          },
          {
            "text": "-interpreter-exec console \"monitor halt\"",
            "ignoreFailures": false
          },
          {
            "text": "-interpreter-exec console \"monitor arm semihosting enable\"",
            "ignoreFailures": false
          }
        ]
      }
    },

Remember to reverse this out when done. 

The aim is hopefully to help someone, so they can build new great software. I would get this far without people who helped me.

Thanks