Skip to content
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

Fixed I2C contention & timeout bugs #118

Merged
merged 1 commit into from
Jun 1, 2016
Merged

Fixed I2C contention & timeout bugs #118

merged 1 commit into from
Jun 1, 2016

Conversation

mikehamer
Copy link
Contributor

This pull request fixes two related bugs, which resulted in high CPU load from a high-priority task causing I2C timeouts, which led to tasks spinning endlessly in the (now removed) read/write spin-locks, which finally resulted in an assertFail on reenabling a FreeRTOS timer. This issue was exacerbated by the recent size increase in gyro bias calibration (more calculations from a high priority task), particularly if a radio connection is also established during the calibration routine.

The bugs fixed by this pull request are:

  1. Improper semaphore usage:
    • Previously the I2C semaphores were initialised as unlocked (the subsequent xSemaphoreTake in the initialization was forgotten), and as such that they could be taken, before first being given.
    • This caused the (intended-to-be-blocking) xSemaphoreTakes to always pass immediately, which is the reason for the spin-locks (since the code did not wait for the completion interrupts).
    • This commit changes from the deprecated vSemaphoreCreateBinary (which creates the semaphore in an unlocked state, and would require a subsequent xSemaphoreTake) to the more current xSemaphoreCreateBinary (which creates the semaphore in the locked state).
  2. I2C Timeouts:
    • The calls to CPAL_I2C_Read and CPAL_I2C_Write contain a spin-lock to detect timeout.
    • If a context change occurs during this function (ie. if the task is interrupted by a higher priority task), then I2C will timeout.
    • This pull request disables context switching for the duration of these calls.

This pull request fixes two related bugs, which resulted in high CPU load from a high-priority task causing I2C timeouts, which led to tasks spinning endlessly in the (now removed) read/write spin-locks, which finally resulted in an assertFail on reenabling a FreeRTOS timer. This issue was exacerbated by the recent size increase in gyro bias calibration (more calculations from a high priority task), particularly if a radio connection is also established during the calibration routine.

The bugs fixed by this pull request are:

1. Improper semaphore usage.
 * Previously the I2C semaphores were initialised as unlocked (the subsequent xSemaphoreTake in the initialization was forgotten), and as such that they could be taken, before first being given.
 * This caused the (intended-to-be-blocking) xSemaphoreTakes to always pass immediately, which is the reason for the spin-locks (since the code did not wait for the completion interrupts).
 * This commit changes from the deprecated vSemaphoreCreateBinary (which creates the semaphore in an unlocked state, and would require a subsequent xSemaphoreTake) to the more current xSemaphoreCreateBinary (which creates the semaphore in the locked state).

2. I2C Timeouts
 * The calls to CPAL_I2C_Read and CPAL_I2C_Write contain a spin-lock to detect timeout.
 * If a context switch occurs during this function (ie. if the task is interrupted by a higher priority task), then I2C will timeout.
 * This pull request disables context switching for the duration of these calls.

Signed-off-by: Mike Hamer <[email protected]>
@mikehamer
Copy link
Contributor Author

This should fix your problem, @ataffanel. Note that I only fixed this yesterday and thus it has only been running for a few hours, more testing is probably required.

@ataffanel
Copy link
Member

Thanks! I will merge it and so we have much more tester! :) This bug has be haunting us for a while so it is really exciting to see a potential fix.

@ataffanel ataffanel merged commit c2a8821 into bitcraze:master Jun 1, 2016
@ataffanel ataffanel modified the milestone: next release Jun 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants