2021-07-29 04:35 AM
Hello folks,
Today, I am trying to use SPI communication on my board. I saw that there is a gyroscope on my board. So, I thought it's a good idea to use this device as an example. Since it's already routed it up, I cannot mess up with connections, etc.
Here is the thing, I don't want to install additional libraries. Instead, I want to create/program this example via what everybody starts with. I use CubeIDE. CubeIDE already gives me a bunch of predefined functions with HAL and I assume that function is more than enough to complete this project.
I guess I am able to use SPI since the function returns HAL_OK. However, I cannot read any value from either x, y, or z.
These are the pins according to the datasheet.
This is an SPI configuration made by Cube automatically. I didn't change anything from here.
I upload the program in case someone wants to look at it.
I found these links useful for understanding how they made the chip select SPI, transmit-receive kinda stuff.
https://github.com/MaJerle/stm32f429/tree/master/28-STM32F429_L3GD20
-> This guy uses SPI to communicate
https://www.instructables.com/Learning-About-the-L3GD20-Breakout-Board/
-> This guy uses I2C to communicate
I use SPI but, I'm guessing Majerle doesn't have the SPI_Receive function in DATA_Read.
anyway, that info is a little irrelevant.
I want to learn from my mistakes and get better at this stuff.
THANKS for your attention,
Kind Regards.
*I set pin A1 for interrupt, but I didn't use it for interrupt. Is that the problem? If it's the case then, that might be a very rookie mistake.:grimacing_face:
2021-07-29 05:14 AM
I checked and accidentally, I made PC1 as input. I change that value with GPIO_Output with default HIGH.
I put a control array to see if I could write anything on-chip. Sadly, the HAL_I2C_Receive function turns nothing (0).
2021-07-29 05:34 AM
I realize that I cannot always send data through TX or RX, people make chip select high and low every time. So I change my initialize part like this
// Manually Create L3GD20 via SPI!
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x20;
gyroBufferTx[1] = 0x0F;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 2, 100); // L3GD20 PowerBit
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x20 | 0x80;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 1, 100);
HAL_SPI_Receive(&hspi5, *gyroBufferRx, 1, 100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
val_Ctrl[0] = gyroBufferRx[0];
/////////
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x23;
gyroBufferTx[1] = 0x00; // 0x00 for scale 250, 0x10 for scale 500, 0x20 for scale 2000
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 2, 100); // Scaling
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x23 | 0x80;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 1, 100);
HAL_SPI_Receive(&hspi5, *gyroBufferRx, 1, 100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
val_Ctrl[4] = gyroBufferRx[0];
/////////
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x21;
gyroBufferTx[1] = 0x00;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 2, 100); // Set high-pass filter settings.
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x21 | 0x80;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 1, 100);
HAL_SPI_Receive(&hspi5, *gyroBufferRx, 1, 100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
val_Ctrl[1] = gyroBufferRx[0];
/////////
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x22;
gyroBufferTx[1] = 0x08;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 2, 100); // Set high-pass filter settings.
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x22 | 0x80;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 1, 100);
HAL_SPI_Receive(&hspi5, *gyroBufferRx, 1, 100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
val_Ctrl[2] = gyroBufferRx[0];
/////////
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x22;
gyroBufferTx[1] = 0x10;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 2, 100); // Enable high-pass filter settings.
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, RESET); // Chip Select LOW!
gyroBufferTx[0] = 0x22 | 0x80;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 1, 100);
HAL_SPI_Receive(&hspi5, *gyroBufferRx, 1, 100);
val_Ctrl[3] = gyroBufferRx[0];
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); // Chip Select HIGH!
// Manually we should create L3GD20 SPI communication
2021-07-29 05:53 AM
gyroBufferTx[0] = 0x20;
gyroBufferTx[1] = 0x0F;
HAL_SPI_Transmit(&hspi5, *gyroBufferTx, 2, 100);
This seems wrong. How is gyroBufferTx defined? Probably want to pass gyroBufferTx and not *gyroBufferTx. It should be giving you a warning during compiling.
2021-07-29 06:08 AM
/* USER CODE BEGIN PV */
uint8_t gyroBufferTx[8];
uint8_t gyroBufferRx[8];
/* USER CODE END PV */
under private variable section
2021-07-29 06:11 AM
When I seeking around,
https://github.com/MaJerle this guy set his board Prescaler to 32. Mine was initially set to 128, I changed as 32 too.
2021-07-29 06:32 AM
2021-07-29 06:44 AM
WONDERFUL!!
I cannot thanks enough, thanks to you (and all the community) my learning process got speed!
THANKS AGAIN!