-
Notifications
You must be signed in to change notification settings - Fork 23
SPI bus driver #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: prototype_1
Are you sure you want to change the base?
SPI bus driver #128
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @Tejasgarg, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Reviewer's GuideThis PR introduces a basic SPI bus driver by adding low-level STM32H5 SPI code, exposing a high-level SPI_Handle API, configuring clocks and build files, and integrating an example usage in the application. Sequence diagram for SPI transmit and receive in main applicationsequenceDiagram
participant App as Application
participant SPI as SPI_Handle
participant LL as SPI_LL
participant HW as STM32 SPI Peripheral
App->>SPI: SPI_init(0)
SPI->>LL: SPI_LL_init(bus)
LL->>HW: Configure SPI peripheral
App->>SPI: SPI_transmit_receive(spi_handle, &tx_data, rx_data, size)
SPI->>LL: SPI_LL_transmit_receive(bus, tx_data, rx_data, size)
LL->>HW: HAL_SPI_TransmitReceive(...)
HW-->>LL: Data received
LL-->>SPI: Return received data
SPI-->>App: Return received data
Class diagram for new SPI bus driver APIclassDiagram
class SPI_Handle {
+SPI_Bus bus_id
+bool initialized
}
class SPI_Bus {
}
SPI_Handle -- SPI_Bus
SPI_Handle <|.. SPI_init
SPI_Handle <|.. SPI_deinit
SPI_Handle <|.. SPI_transmit
SPI_Handle <|.. SPI_receive
SPI_Handle <|.. SPI_transmit_receive
class SPI_init {
+SPI_Handle *SPI_init(size_t bus)
}
class SPI_deinit {
+void SPI_deinit(SPI_Handle *handle)
}
class SPI_transmit {
+void SPI_transmit(SPI_Handle *handle, uint8_t const *txbuf, size_t sz)
}
class SPI_receive {
+void SPI_receive(SPI_Handle *handle, uint8_t *rxbuf, size_t sz)
}
class SPI_transmit_receive {
+void SPI_transmit_receive(SPI_Handle *handle, uint8_t const *tx_data, uint8_t *rx_data, size_t size)
}
Class diagram for SPI low-level hardware interfaceclassDiagram
class SPIInstance {
+SPI_HandleTypeDef *hspi
+bool initialized
}
class SPI_Bus {
}
SPIInstance -- SPI_Bus
SPIInstance <|.. SPI_LL_init
SPIInstance <|.. SPI_LL_deinit
SPIInstance <|.. SPI_LL_transmit
SPIInstance <|.. SPI_LL_receive
SPIInstance <|.. SPI_LL_transmit_receive
class SPI_LL_init {
+void SPI_LL_init(SPI_Bus bus)
}
class SPI_LL_deinit {
+void SPI_LL_deinit(SPI_Bus bus)
}
class SPI_LL_transmit {
+void SPI_LL_transmit(SPI_Bus bus, uint8_t const *txdata, size_t size)
}
class SPI_LL_receive {
+void SPI_LL_receive(SPI_Bus bus, uint8_t *rxdata, size_t size)
}
class SPI_LL_transmit_receive {
+void SPI_LL_transmit_receive(SPI_Bus bus, uint8_t const *tx_data, uint8_t *rx_data, size_t size)
}
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- In SPI_init you dereference g_spi_instances[bus_id] without checking for NULL (it starts out as nullptr), so the first init will crash—update the logic to check the pointer before deref and reset the slot on deinit.
- The bus enum and HAL_SPI_MspInit comment refer to SPI1, but the code enables/configures SPI3—align the instance mapping, names, and comments to the same peripheral to avoid confusion.
- It looks like src/system/bus/spi.c isn’t added to your CMakeLists.target.txt, so the new high-level SPI module won’t be built—please include it in the build configuration.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In SPI_init you dereference g_spi_instances[bus_id] without checking for NULL (it starts out as nullptr), so the first init will crash—update the logic to check the pointer before deref and reset the slot on deinit.
- The bus enum and HAL_SPI_MspInit comment refer to SPI1, but the code enables/configures SPI3—align the instance mapping, names, and comments to the same peripheral to avoid confusion.
- It looks like src/system/bus/spi.c isn’t added to your CMakeLists.target.txt, so the new high-level SPI module won’t be built—please include it in the build configuration.
## Individual Comments
### Comment 1
<location> `src/system/bus/spi.c:25` </location>
<code_context>
+ bool initialized;
+};
+
+static SPI_Handle *g_spi_instances[SPI_BUS_COUNT] = { nullptr };
+
+/**
</code_context>
<issue_to_address>
**issue (bug_risk):** Potential null pointer dereference in SPI_init.
g_spi_instances[bus_id] may be nullptr on first use, leading to a crash when accessing its members. Add a nullptr check before referencing g_spi_instances[bus_id]->initialized.
</issue_to_address>
### Comment 2
<location> `src/platform/h563xx/spi_ll.c:35` </location>
<code_context>
+} SPIInstance;
+
+/* HAL SPI handle */
+static SPI_HandleTypeDef g_hspi1 = { nullptr };
+
+/* SPI instance configuration */
</code_context>
<issue_to_address>
**issue (bug_risk):** Use of 'nullptr' in C source file.
Replace 'nullptr' with 'NULL' to ensure compatibility with C compilers.
</issue_to_address>
### Comment 3
<location> `src/platform/h563xx/spi_ll.c:53` </location>
<code_context>
+{
+ GPIO_InitTypeDef gpio_init = { 0 };
+
+ if (hspi->Instance == SPI3) {
+ /* SPI3 clock enable */
+ __HAL_RCC_SPI3_CLK_ENABLE();
</code_context>
<issue_to_address>
**nitpick (typo):** GPIO configuration comment refers to SPI1 instead of SPI3.
Please change the comment to 'SPI3 GPIO Configuration' to match the code.
```suggestion
/* SPI3 GPIO Configuration: PC10=SCK, PC11=MISO, PC12=MOSI */
```
</issue_to_address>
### Comment 4
<location> `src/application/main.c:22-19` </location>
<code_context>
}
+ // Initialize the SPI bus
+ SPI_Handle *spi_handle = SPI_init(0);
+ if (spi_handle == NULL) {
+ LOG_ERROR("Failed to initialize SPI bus");
+ }
+
// Main application loop
</code_context>
<issue_to_address>
**issue (bug_risk):** SPI bus initialization error is logged but not handled.
Please ensure the function exits or handles the error when SPI_init returns NULL to prevent invalid SPI operations.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if (!protocol_init()) { | ||
| LOG_ERROR("Failed to initialize protocol"); | ||
| return -1; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): SPI bus initialization error is logged but not handled.
Please ensure the function exits or handles the error when SPI_init returns NULL to prevent invalid SPI operations.
This is a work in progress.
Currently, I have implemented the basic low-level driver for SPI bus, that implements two SPI buses (SPI1 and SPI2)
I will continue working on this to implement the complete working of the SPI bus module
Summary by Sourcery
Add basic SPI bus driver support by providing hardware abstraction, system-level APIs, platform configuration, and example usage in main
New Features:
Enhancements:
Build: