24.06.2013 Views

Programmation proche du hardware avec le kit de développement ...

Programmation proche du hardware avec le kit de développement ...

Programmation proche du hardware avec le kit de développement ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Hardwarenahe Programmierung in C<br />

void TimerInterruptHand<strong>le</strong>r (void) interrupt 5<br />

{<br />

}<br />

Timeout--;<br />

void Troub<strong>le</strong> (void)<br />

{<br />

Disab<strong>le</strong>Interrupts ();<br />

if (Timeout > 0) {<br />

Magic = 1.0 / (float) Timeout;<br />

}<br />

Enab<strong>le</strong>Interrupts ();<br />

}<br />

Ce qui est beaucoup meil<strong>le</strong>ur. Mais que se passe-t-il lorsque <strong>le</strong>s interruptions sont préalab<strong>le</strong>ment<br />

bloquées et que ces <strong>de</strong>rniers sont libérés. Il existe pour cela <strong>de</strong>s fonctions, qui permettent <strong>de</strong><br />

déterminer si <strong>le</strong>s interruptions sont préalab<strong>le</strong>ment bloquées ou pas. De co<strong>de</strong> <strong>de</strong>vrait être définie <strong>de</strong><br />

façon optima<strong>le</strong> <strong>de</strong> la manière suivante :<br />

void TimerInterruptHand<strong>le</strong>r (void) interrupt 5<br />

{<br />

Timeout--;<br />

}<br />

void Troub<strong>le</strong> (void)<br />

{<br />

int OldInterruptState;<br />

OldInterruptState = GetInterruptState();<br />

Disab<strong>le</strong>Interrupts();<br />

}<br />

Attention<br />

if (Timeout > 0) {<br />

Magic = 1.0 / (float) Timeout;<br />

}<br />

/* Enab<strong>le</strong> Interrupts only if they were enab<strong>le</strong>d before */<br />

if (OldInterruptState == 1) {<br />

Enab<strong>le</strong>Interrupts ();<br />

}<br />

Même <strong>le</strong>s morceaux <strong>de</strong> co<strong>de</strong> qui peuvent paraître comme étant apparemment sur, comme par exemp<strong>le</strong><br />

a++ ou a = a + 1, doivent être protéges <strong>de</strong>s interruptions. Car ces instructions haut niveau sont<br />

souvent tra<strong>du</strong>ites en une suite d’instructions assemb<strong>le</strong>ur (<strong>le</strong>cture, incrémentation et écriture), qui ne<br />

doivent éga<strong>le</strong>ment pas être interrompues. Avec certain processeur, il existe <strong>de</strong>s instructions dites<br />

« atomiques », qui ne peuvent pas être interrompues. Mais pour pouvoir utiliser ces instructions, il faut<br />

activer certaines options <strong>du</strong> compilateur.<br />

38 / 41

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!