2020-06-06 08:54 PM
@For a few days I'm trying to get the correct answer from the PS2 PAD on the STM32F103 Bluepil. I Used SPI2 with clock frequency is set to 36MHz (APB1) SPI prescaler 256 configuration which gives me 140KHz (or KBits / s). The first bit is taken from LSB, CPOL High, CPHA on 2, NSS software controlled. GPIO output (PB12-CS pin). I use PB13 pins as SCK (clock) PB14-MISO, PB15-MOSI. SPI is set to Full-Duplex Master mode. I would add that the microcontroller operates at std Periph library. In the main.c file :
/*Initial PS2 Controller*/
unsigned char Config_Mode_Digital[9]={0x01,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//function for send data PS2
void PS2_send(unsigned char commands[], unsigned char data[], int length)
{
int i = 0;
GPIO_ResetBits(GPIOB, GPIO_Pin_12); //set CS pin low to select PS2
delay_us(60); //60us delay before sending commands
for(i = 0; i < length; i = i + 1)
{
/* Loop while DR register in not emplty */
while (!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE));
/* Send byte through the SPI2 peripheral */
SPI_I2S_SendData(SPI2, commands[i]);
/* Wait to receive a byte */
while (!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE));
// Wait until SPI is not busy anymore
/* Return the byte read from the SPI bus */
Data[i] = SPI_I2S_ReceiveData(SPI2);
}
delay_us(60); //60us delay before sending next command
/* Return the byte read from the SPI bus */
GPIO_SetBits(GPIOB, GPIO_Pin_12); //set CS pin high to deselect PS2
}
//In Main loop
int main(void)
{
GPIO_Configuration();
delay_init();
SPI_Configuration();
while(1)
{
PS2_send(Config_Mode_Digital, Data, 5);
delay_ms(100);
}
}
After sending the information: 0x01 0x42 0x00 0x00 0x00 0x00 0x00 0x00 0x00 I should get 0xFF 0x41 0x5A 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF if no button is pressed. but I got the error value : 0xFF 0x83 0xFE 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF, and when I press some button the value not changed. Maybe someone comes up with some idea. I'm attach my code here
my reference :
https://store.curiousinventor.com/guides/PS2
http://virtual-shed.blogspot.com/2013/03/playstation-2-controller-interface.html
regards