-
Notifications
You must be signed in to change notification settings - Fork 16
[I2C, hal, SW] Restructured and refactored the functions #535
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
Open
KinzaQamar
wants to merge
5
commits into
lowRISC:main
Choose a base branch
from
KinzaQamar:i2c_sw_modif
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5dcd728
[I2C, hal, SW] Restructured and refactored the functions
KinzaQamar 3303352
[I2C, SW] Place the resetting of FMT fifo step when halt occurs
KinzaQamar ab608e2
[I2C, sw] Added a HAL function to return I2C RData
KinzaQamar dbf6a8d
[I2C, SW, hal] Extended read / write functions to R/W N bytes
KinzaQamar ed795b3
[i2c, sw] Added an overflow check when the byte is equal to FMT FIFO …
KinzaQamar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,9 +60,10 @@ void i2c_init(i2c_t i2c) | |
| VOLATILE_WRITE(i2c->timing4, t4_reg); | ||
| } | ||
|
|
||
| bool i2c_write_byte(i2c_t i2c, uint8_t addr, uint8_t data) | ||
| void i2c_write_bytes(i2c_t i2c, uint8_t addr, const uint8_t *data, uint8_t len_bytes) | ||
| { | ||
| // Reset FMT FIFO (because we currently don't clean-up after errors) | ||
| // Reset the FMT FIFO as a precautionary step in case something goes wrong when controller's FSM | ||
| // is halted and the SW didn't manage to clear the FIFO during that scenario. | ||
| i2c_fifo_ctrl fifo_ctrl_reg = { .fmtrst = 1u }; | ||
| VOLATILE_WRITE(i2c->fifo_ctrl, fifo_ctrl_reg); | ||
|
|
||
|
|
@@ -74,31 +75,30 @@ bool i2c_write_byte(i2c_t i2c, uint8_t addr, uint8_t data) | |
| fdata_reg.start = 1u; | ||
| VOLATILE_WRITE(i2c->fdata, fdata_reg); | ||
|
|
||
| // Send stop bit and data | ||
| fdata_reg.fbyte = data; | ||
| fdata_reg.start = 0; | ||
| fdata_reg.stop = 1u; | ||
| VOLATILE_WRITE(i2c->fdata, fdata_reg); | ||
|
|
||
| // Wait for transaction to complete and report simple succeed/fail | ||
| for (uint32_t ii = 0; ii < 10000000ul /*arbitrary number*/; ii++) { | ||
| i2c_intr i2c_intr_state_reg = VOLATILE_READ(i2c->intr_state); | ||
| if (i2c_intr_state_reg & i2c_intr_controller_halt) { | ||
| return false; // transaction failed | ||
| for (uint8_t i = 0; i < len_bytes; i++) { | ||
| // Check the overflow condition before writing to the FMT FIFO. | ||
| i2c_status i2c_status_reg = VOLATILE_READ(i2c->status); | ||
|
|
||
| // Wait until FMT FIFO has some space | ||
| while (i2c_status_reg & i2c_status_fmtfull) { | ||
| i2c_status_reg = VOLATILE_READ(i2c->status); | ||
| } | ||
| if (i2c_intr_state_reg & i2c_intr_cmd_complete) { | ||
| i2c_status i2c_status_reg = VOLATILE_READ(i2c->status); | ||
| if (i2c_status_reg & i2c_status_fmtempty) { | ||
| return true; // transaction succeeded | ||
| } | ||
|
|
||
| // Send all data bytes; assert STOP only on the last byte | ||
| fdata_reg.fbyte = data[i]; | ||
| if (i == (len_bytes - 1u)) { | ||
| fdata_reg.stop = 1u; | ||
| } | ||
| VOLATILE_WRITE(i2c->fdata, fdata_reg); | ||
| } | ||
| return false; // timeout | ||
| } | ||
|
|
||
| uint8_t i2c_read_byte(i2c_t i2c, uint8_t addr) | ||
| void i2c_read_bytes(i2c_t i2c, uint8_t addr, uint8_t len_bytes) | ||
| { | ||
| // Reset FMT FIFO (because we currently don't clean-up after errors) | ||
| // Reset the FMT FIFO as a precautionary step in case something goes wrong when controller's FSM | ||
| // is halted and the SW didn't manage to clear the FIFO during that scenario. | ||
| i2c_fifo_ctrl fifo_ctrl_reg = { .fmtrst = 1u }; | ||
| VOLATILE_WRITE(i2c->fifo_ctrl, fifo_ctrl_reg); | ||
|
Comment on lines
+100
to
103
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elliotb-lowrisc Do we still need to keep this precautionary step before the start of every read and write no that we are clearing on errors? |
||
|
|
||
|
|
@@ -112,28 +112,67 @@ uint8_t i2c_read_byte(i2c_t i2c, uint8_t addr) | |
|
|
||
| // Send stop bit, read bit and number of bytes to read | ||
| fdata_reg.readb = 1u; | ||
| fdata_reg.fbyte = 1u; // If readb = 1 then fbyte contains the number of bytes to read | ||
| fdata_reg.fbyte = len_bytes; // If readb = 1 then fbyte contains the number of bytes to read | ||
| fdata_reg.start = 0; | ||
| fdata_reg.stop = 1u; | ||
| VOLATILE_WRITE(i2c->fdata, fdata_reg); | ||
| } | ||
|
|
||
| bool i2c_wait_write_finish(i2c_t i2c) | ||
| { | ||
| // Wait for transaction to complete and report simple succeed / fail | ||
| while (true) { | ||
| i2c_intr i2c_intr_state_reg = VOLATILE_READ(i2c->intr_state); | ||
| if (i2c_intr_state_reg & i2c_intr_controller_halt) { | ||
| // Reset FMT FIFO as controller's FSM is in halt | ||
| i2c_fifo_ctrl fifo_ctrl_reg = { .fmtrst = 1u }; | ||
| VOLATILE_WRITE(i2c->fifo_ctrl, fifo_ctrl_reg); | ||
|
|
||
| // Wait for transaction to complete and return either read data or 0xFF | ||
| for (uint32_t ii = 0; ii < 10000000ul /*arbitrary number*/; ii++) { | ||
| // According to programmer's guide, the CONTROLLER_EVENTS register would be cleared | ||
| // here to acknowledge the controller halt interrupt. However, since we want to | ||
| // treat a halt event as a failure, we intentionally skip clearing it. | ||
| return false; // Transaction failed | ||
| } | ||
| if (i2c_intr_state_reg & i2c_intr_cmd_complete) { | ||
| i2c_status i2c_status_reg = VOLATILE_READ(i2c->status); | ||
| if (i2c_status_reg & i2c_status_fmtempty) { | ||
| return true; // Transaction succeeded | ||
| } | ||
| } | ||
| } | ||
| return false; // Timeout | ||
| } | ||
|
|
||
| bool i2c_wait_read_finish(i2c_t i2c) | ||
| { | ||
| // Wait for transaction to complete and report simple succeed / fail | ||
| while (true) { | ||
| i2c_intr i2c_intr_state_reg = VOLATILE_READ(i2c->intr_state); | ||
| if (i2c_intr_state_reg & i2c_intr_controller_halt) { | ||
| return 0xFF; // transaction failed | ||
| // Reset FMT FIFO as controller's FSM is in halt | ||
| i2c_fifo_ctrl fifo_ctrl_reg = { .fmtrst = 1u }; | ||
| VOLATILE_WRITE(i2c->fifo_ctrl, fifo_ctrl_reg); | ||
|
|
||
| // According to programmer's guide, the CONTROLLER_EVENTS register would be cleared | ||
| // here to acknowledge the controller halt interrupt. However, since we want to | ||
| // treat a halt event as a failure, we intentionally skip clearing it. | ||
| return false; // Transaction failed | ||
| } | ||
| i2c_status i2c_status_reg = VOLATILE_READ(i2c->status); | ||
| if (i2c_status_reg & i2c_status_fmtempty) { | ||
| // transaction succeeded, return read data | ||
| i2c_rdata rdata_reg = VOLATILE_READ(i2c->rdata); | ||
| return rdata_reg.rdata; | ||
| return true; | ||
| } | ||
| } | ||
| return 0xFF; // timeout | ||
| return false; // Timeout | ||
| } | ||
|
|
||
| void enable_controller_mode(i2c_t i2c) | ||
| { | ||
| VOLATILE_WRITE(i2c->ctrl, i2c_ctrl_enablehost); | ||
| } | ||
|
|
||
| uint8_t i2c_rdata_byte(i2c_t i2c) | ||
| { | ||
| i2c_rdata rdata_reg = VOLATILE_READ(i2c->rdata); | ||
| return rdata_reg.rdata; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.