How to use STM32 to read signal from the PS2 hand controller through SPI protocol?
Started by 2 years ago●6 replies●latest reply 2 years ago●758 viewsI have a project where I need to connect a PS2 hand controller to my microcontroller (STM32F4 series) to control a robot. I want to use the controller to read the signals from the buttons and joystick on that hand controller via SPI communication using DMA to avoid missing events from PS2. Then, I can control my robot's direction, speed, and switch between several operating modes.
Currently, I am first building a small program to test the signal transmission between the microcontroller and the ps2 controller. I created variables for each button on the handler, and then each time I press a button, the corresponding variable should increase by one. However, it seems that my MCU cannot receive the signal from the ps2 yet because when I connect and press the buttons, the program still cannot run as expected. I have also attached my main code: ps2_controller_polling.txt.
I also attached the ps2's data frame format that i found for anyone who wants to refer: PS2_msg_format.gif.
I'm still fairly new to embedded programming so can someone help me with suggestions on where maybe I'm going wrong, please?
Thank you so much!
Hi, @Namcrazy37!
Using STM32CubeIDE can do all the hardware configurations for you, and, better yet, it can flag hardware configuration errors and even auto-correct them.
Have you verified whether your hardware configuration is all ok?
Besides that, it's worthwhile to verify the circuit itself. SPI uses a separate pin to enable the peripheral. Are you ( or the HAL ) driving it?
Hope to have helped.
Daniel.
Yes, I'm using STM32CUBEMX for the configuration. I set the mode for my SPI as full-duplex master and NSS signal type as Software. Here is my other SPI configuration:
I have also used a randomly separated GPIO pin as the Slave Select pin for my SPI. Can you refer t my main code to know better what I might be going wrong, please?
In the current state, I don't see how you program can even be built. You declare static functions but don't define them. The linker should scream.
Hi, faustoRaicam!
I think the O. P. wanted to write spi_enable;
It should be a type of "finger addressing error" :-)
Daniel.
You need to change spi_disable; to spi_enable.
Also, when reading the values for the button, you should use bit-wise checking not byte-wise. HINT: What happens if you press TWO, THREE or FOUR buttons at the same time?