<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
    <title>nostd</title>
    <subtitle>Code bas niveau, systèmes et embarqué</subtitle>
    <link rel="self" type="application/atom+xml" href="https://nostd.fr/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://nostd.fr"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-11-30T00:00:00+00:00</updated>
    <id>https://nostd.fr/atom.xml</id>
    <entry xml:lang="fr">
        <title>Du C aux transistors : jusqu&#39;où peut-on descendre ?</title>
        <published>2026-11-30T00:00:00+00:00</published>
        <updated>2026-11-30T00:00:00+00:00</updated>
        <author>
          <name>nostd</name>
        </author>
        <link rel="alternate" type="text/html" href="https://nostd.fr/articles/du-c-aux-transistors/"/>
        <id>https://nostd.fr/articles/du-c-aux-transistors/</id>
        <summary type="text">Une exploration du C, des appels système, du démarrage bare-metal et des circuits logiques, à partir d&#39;une lettre A.</summary>
        <content type="html" xml:base="https://nostd.fr/articles/du-c-aux-transistors/">&lt;p&gt;L’informatique est un domaine tellement vaste et passionnant qu’il faudrait probablement plusieurs vies pour en maîtriser toutes les disciplines. D’ailleurs, à l’image des mathématiques, on serait presque tenté de parler &lt;em&gt;des informatiques&lt;/em&gt; tant les domaines qu’elle recouvre sont différents.&lt;/p&gt;
&lt;p&gt;On y retrouve le développement logiciel, l’algorithmique, les réseaux, l’administration système, les systèmes embarqués, la cybersécurité, la cryptographie, les bases de données ou encore l’intelligence artificielle. Et ces branches sont loin d’être indépendantes : elles se nourrissent de disciplines voisines. Les mathématiques donnent des outils pour raisonner sur les algorithmes et la cryptographie ; la physique et l’électronique permettent de construire les machines ; le droit encadre l’usage des données et des logiciels ; l’ergonomie et les sciences cognitives éclairent nos interactions avec les systèmes.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Panorama de quelques branches et disciplines connexes de l’informatique : algorithmique, réseaux, systèmes embarqués, cryptographie, bases de données, génie logiciel, avec en dessous les mathématiques, la physique et l’électronique, le droit et les sciences humaines.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 680 390&quot; width=&quot;680&quot; height=&quot;390&quot;&gt;   &lt;rect class=&quot;fig-box fig-box-strong&quot; x=&quot;205&quot; y=&quot;24&quot; width=&quot;270&quot; height=&quot;54&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;340.0&quot; y=&quot;45.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot; font-weight=&quot;650&quot;&gt;L’informatique&lt;/text&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;340.0&quot; y=&quot;67.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Science, techniques et usages&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M340 78 L340 104 M336.0 97.1 L340 104 L344.0 97.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;45&quot; y=&quot;105&quot; width=&quot;590&quot; height=&quot;44&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340.0&quot; y=&quot;132.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Quelques branches&lt;/text&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;20&quot; y=&quot;162&quot; width=&quot;204&quot; height=&quot;53&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;122.0&quot; y=&quot;182.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Algorithmique&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;122.0&quot; y=&quot;204.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Développement logiciel&lt;/text&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;238&quot; y=&quot;162&quot; width=&quot;204&quot; height=&quot;53&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340.0&quot; y=&quot;182.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Réseaux&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;340.0&quot; y=&quot;204.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Administration système&lt;/text&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;456&quot; y=&quot;162&quot; width=&quot;204&quot; height=&quot;53&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;558.0&quot; y=&quot;182.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Systèmes embarqués&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;558.0&quot; y=&quot;204.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Architecture matérielle&lt;/text&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;20&quot; y=&quot;226&quot; width=&quot;204&quot; height=&quot;53&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;122.0&quot; y=&quot;246.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Cryptographie&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;122.0&quot; y=&quot;268.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Cybersécurité&lt;/text&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;238&quot; y=&quot;226&quot; width=&quot;204&quot; height=&quot;53&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340.0&quot; y=&quot;246.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Bases de données&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;340.0&quot; y=&quot;268.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Intelligence artificielle&lt;/text&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;456&quot; y=&quot;226&quot; width=&quot;204&quot; height=&quot;53&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;558.0&quot; y=&quot;246.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Génie logiciel&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;558.0&quot; y=&quot;268.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Interfaces homme-machine&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;45&quot; y=&quot;299&quot; width=&quot;590&quot; height=&quot;38&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340.0&quot; y=&quot;323.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Disciplines connexes et apports transversaux&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;20&quot; y=&quot;345&quot; width=&quot;204&quot; height=&quot;32&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;122.0&quot; y=&quot;366.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;15&quot;&gt;Mathématiques&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;238&quot; y=&quot;345&quot; width=&quot;204&quot; height=&quot;32&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340.0&quot; y=&quot;366.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;15&quot;&gt;Physique / électronique&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;456&quot; y=&quot;345&quot; width=&quot;204&quot; height=&quot;32&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;558.0&quot; y=&quot;366.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;15&quot;&gt;Droit / sciences humaines&lt;/text&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 1 — Une carte volontairement non exhaustive : ces domaines se croisent plutôt qu’ils ne forment une hiérarchie stricte.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Étant développeur de métier, et curieux de comprendre comment les choses fonctionnent, je me suis lancé un petit défi : &lt;strong&gt;aller le plus bas niveau possible lorsque j’écris du code.&lt;/strong&gt; Non pas pour réinventer toutes les bibliothèques que j’utilise au quotidien, mais pour comprendre ce qu’elles cachent.&lt;/p&gt;
&lt;p&gt;Nous n’allons pas retracer ici toute l’histoire de l’informatique, de la Pascaline de Blaise Pascal aux travaux d’Ada Lovelace sur la machine analytique, en passant par ceux d’&lt;strong&gt;Al-Khwârizmî&lt;/strong&gt;, dont le nom a donné le mot « algorithme ». Nous allons plutôt suivre le chemin qui sépare une ligne de code des fameux 0 et 1… et même descendre encore en dessous.&lt;/p&gt;
&lt;p&gt;Vous allez voir : c’est un sac de nœuds. Mais un sac de nœuds plutôt fascinant.&lt;/p&gt;
&lt;h2 id=&quot;tout-commence-avec-la-lettre-a&quot;&gt;Tout commence avec la lettre A&lt;/h2&gt;
&lt;p&gt;Mon point de départ est ridiculement simple. Je déclare un caractère en C et je lui attribue la valeur 65.&lt;/p&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;c&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-10&quot;&gt;#include&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;lt;unistd.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;    unsigned char&lt;/span&gt;&lt;span&gt; c &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 65&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-8&quot;&gt;    write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;, &amp;amp;&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Résultat : un &lt;strong&gt;A&lt;/strong&gt; apparaît dans le terminal. Pourtant, je n’ai jamais écrit la lettre A dans mon programme. J’ai seulement manipulé un nombre. La table ASCII associe par convention la valeur 65 au caractère A. En UTF-8, les caractères ASCII conservent leur représentation sur un seul octet.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Le nombre 65 en décimal, en hexadécimal (0x41) et comme caractère ASCII A, puis ses huit bits : 01000001, avec les poids 128, 64, 32, 16, 8, 4, 2 et 1. Seuls les bits de poids 64 et 1 valent 1.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 680 215&quot; width=&quot;680&quot; height=&quot;215&quot;&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;24&quot; y=&quot;22&quot; width=&quot;182&quot; height=&quot;55&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;115.0&quot; y=&quot;43.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;65&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;115.0&quot; y=&quot;65.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Décimal&lt;/text&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;249&quot; y=&quot;22&quot; width=&quot;182&quot; height=&quot;55&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340.0&quot; y=&quot;43.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0x41&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;340.0&quot; y=&quot;65.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Hexadécimal&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;474&quot; y=&quot;22&quot; width=&quot;182&quot; height=&quot;55&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;565.0&quot; y=&quot;43.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;A&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;565.0&quot; y=&quot;65.5&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Caractère ASCII&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;36&quot; y=&quot;111&quot; width=&quot;66&quot; height=&quot;56&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;69.0&quot; y=&quot;144.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;69&quot; y=&quot;189&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;128&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-strong&quot; x=&quot;113&quot; y=&quot;111&quot; width=&quot;66&quot; height=&quot;56&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;146.0&quot; y=&quot;144.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot; font-weight=&quot;650&quot;&gt;1&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;146&quot; y=&quot;189&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;64&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;190&quot; y=&quot;111&quot; width=&quot;66&quot; height=&quot;56&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;223.0&quot; y=&quot;144.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;223&quot; y=&quot;189&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;32&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;267&quot; y=&quot;111&quot; width=&quot;66&quot; height=&quot;56&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;144.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300&quot; y=&quot;189&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;16&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;344&quot; y=&quot;111&quot; width=&quot;66&quot; height=&quot;56&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;377.0&quot; y=&quot;144.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;377&quot; y=&quot;189&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;8&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;421&quot; y=&quot;111&quot; width=&quot;66&quot; height=&quot;56&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;454.0&quot; y=&quot;144.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;454&quot; y=&quot;189&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;4&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;498&quot; y=&quot;111&quot; width=&quot;66&quot; height=&quot;56&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;531.0&quot; y=&quot;144.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;531&quot; y=&quot;189&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;2&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-strong&quot; x=&quot;575&quot; y=&quot;111&quot; width=&quot;66&quot; height=&quot;56&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;608.0&quot; y=&quot;144.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot; font-weight=&quot;650&quot;&gt;1&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;608&quot; y=&quot;189&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;1&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M340 77 L340 108 M336.0 101.1 L340 108 L344.0 101.1&quot;/&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 2 — 65 = 64 + 1. Les huit bits sont une représentation ; le caractère A est une interprétation.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Cette distinction est essentielle : &lt;code&gt;01000001&lt;/code&gt; n’est pas intrinsèquement une lettre. Les mêmes bits peuvent représenter un entier, une instruction machine, une partie d’une image ou une donnée quelconque. C’est le contexte qui leur donne un sens.&lt;/p&gt;
&lt;h3 id=&quot;un-premier-piege-la-representation-des-nombres&quot;&gt;Un premier piège : la représentation des nombres&lt;/h3&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;c&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;unsigned char&lt;/span&gt;&lt;span&gt; a &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 65&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;          // décimal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;unsigned char&lt;/span&gt;&lt;span&gt; b &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 0101&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;        // octal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;unsigned char&lt;/span&gt;&lt;span&gt; c &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 0x41&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;        // hexadécimal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;unsigned char&lt;/span&gt;&lt;span&gt; d &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 0b01000001&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;  // binaire (C23)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Les quatre valeurs sont identiques. Le préfixe &lt;code&gt;0b&lt;/code&gt; appartient au C23 (et était déjà disponible comme extension de certains compilateurs). Un simple zéro initial, en revanche, introduit de l’octal, pas du binaire.&lt;/p&gt;
&lt;p&gt;Autre piège rencontré au passage : si &lt;code&gt;char&lt;/code&gt; est signé sur notre machine et possède huit bits, il ne peut représenter que les valeurs de −128 à 127. Une condition &lt;code&gt;c &amp;lt;= 150&lt;/code&gt; reste donc toujours vraie. Sur les implémentations courantes, la conversion lors de &lt;code&gt;c++&lt;/code&gt; fait revenir la valeur de 127 à −128. Pour parcourir une plage entière d’octets, mieux vaut utiliser un compteur de type &lt;code&gt;int&lt;/code&gt; et convertir chaque valeur en &lt;code&gt;unsigned char&lt;/code&gt;. Et au-delà de 127, un octet isolé n’est plus forcément un caractère valide en UTF-8.&lt;/p&gt;
&lt;h2 id=&quot;write-du-bas-niveau-vraiment&quot;&gt;write() : du bas niveau, vraiment ?&lt;/h2&gt;
&lt;p&gt;Lorsqu’on apprend le C, on commence souvent par &lt;code&gt;printf()&lt;/code&gt;. Dans des cursus comme celui de l’école 42, on rencontre aussi très tôt &lt;code&gt;write()&lt;/code&gt;, ce qui nous rapproche des buffers, des pointeurs et des descripteurs de fichiers. Mais cela ne signifie pas que nous parlons directement au matériel.&lt;/p&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;c&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-8&quot;&gt;write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;, &amp;amp;&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le premier argument désigne le descripteur de fichier 1, le deuxième l’adresse de l’octet à lire et le troisième le nombre d’octets à écrire. Dans un environnement Unix classique, les descripteurs 0, 1 et 2 sont normalement associés respectivement à l’entrée standard, à la sortie standard et à la sortie d’erreur.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Chemin typique d’un octet sur un système Unix graphique, de haut en bas : printf (formatage, flux stdio, buffering), write (interface POSIX, généralement via la libc), appel système (passage en mode noyau), noyau et pseudo-terminal, émulateur de terminal (décodage UTF-8, choix du glyphe), puis rendu graphique à l’écran.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 600 500&quot; width=&quot;600&quot; height=&quot;500&quot;&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;80&quot; y=&quot;14&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;37.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;printf()&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;59.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Formatage, flux stdio, buffering&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 72 L300 91 M296.0 84.1 L300 91 L304.0 84.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;80&quot; y=&quot;93&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;116.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;write()&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;138.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Interface POSIX, généralement via la libc&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 151 L300 170 M296.0 163.1 L300 170 L304.0 163.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;80&quot; y=&quot;172&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;195.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Appel système&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;217.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Passage en mode noyau&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 230 L300 249 M296.0 242.1 L300 249 L304.0 242.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;80&quot; y=&quot;251&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;274.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Noyau / pseudo-terminal&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;296.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Routage des octets vers leur destination&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 309 L300 328 M296.0 321.1 L300 328 L304.0 321.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;80&quot; y=&quot;330&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;353.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Émulateur de terminal&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;375.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Décodage UTF-8, sélection du glyphe&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 388 L300 407 M296.0 400.1 L300 407 L304.0 400.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;80&quot; y=&quot;409&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;432.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Rendu graphique / écran&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;454.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Pixels puis émission ou modulation de lumière&lt;/text&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 3 — Chemin typique sur un système Unix graphique. &lt;code&gt;printf&lt;/code&gt; peut emprunter ce chemin après mise en tampon ; les détails dépendent de l’implémentation.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Le terminal est généralement une application graphique. Le shell (Bash, Zsh, etc.) est un autre programme qui interprète nos commandes et lance nos exécutables. Le noyau, lui, gère notamment les processus, les ressources et les interfaces d’entrée-sortie.&lt;/p&gt;
&lt;p&gt;Le terminal reçoit l’octet, le décode et sélectionne un glyphe dans une police. Le système graphique transforme ensuite sa forme en pixels. À aucun moment le processeur n’a besoin de savoir qu’il s’agit de la lettre A au sens où un humain l’entend.&lt;/p&gt;
&lt;h3 id=&quot;la-preuve-que-stdout-n-est-pas-l-ecran&quot;&gt;La preuve que stdout n’est pas l’écran&lt;/h3&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Rediriger la sortie standard&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Rediriger la sortie standard&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;./a.out &amp;gt; fichier.txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cat fichier.txt&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;Sans modifier le programme, on a redirigé sa sortie vers un fichier. Le shell a changé la destination du descripteur 1 avant de lancer le programme. &lt;code&gt;write()&lt;/code&gt; demande seulement une écriture ; il ne demande pas « dessine un A sur l’écran ».&lt;/p&gt;
&lt;aside class=&quot;callout callout-note&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;POSIX n’est ni un langage ni un système d’exploitation&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;C’est une famille de standards qui spécifie des interfaces et leur comportement. &lt;code&gt;write()&lt;/code&gt; et &lt;code&gt;&amp;lt;unistd.h&amp;gt;&lt;/code&gt; en font partie. Linux et macOS proposent ces interfaces ; Windows natif dispose notamment des API Win32 et de sa propre infrastructure. Le C, en tant que langage, n’impose pas POSIX.&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;et-si-on-supprimait-les-include&quot;&gt;Et si on supprimait les #include ?&lt;/h2&gt;
&lt;p&gt;Un fichier d’en-tête ne fait pas apparaître magiquement une fonction. Il fournit notamment au compilateur les déclarations, types et macros dont il a besoin. L’implémentation peut être ailleurs, dans une bibliothèque par exemple.&lt;/p&gt;
&lt;p&gt;Sur Linux x86-64, où &lt;code&gt;ssize_t&lt;/code&gt; et &lt;code&gt;size_t&lt;/code&gt; correspondent ici respectivement à &lt;code&gt;long&lt;/code&gt; et &lt;code&gt;unsigned long&lt;/code&gt;, on peut déclarer nous-mêmes la fonction :&lt;/p&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;c&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;extern&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; long&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;int&lt;/span&gt;&lt;span&gt; fd&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt; const&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; *&lt;/span&gt;&lt;span&gt;buf&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; unsigned long&lt;/span&gt;&lt;span&gt; count)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;    char&lt;/span&gt;&lt;span&gt; c &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;#39;A&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-8&quot;&gt;    write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;, &amp;amp;&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pas un seul &lt;code&gt;#include&lt;/code&gt; et pourtant le programme fonctionne avec la chaîne de compilation habituelle. Il dépend toujours de la libc, qui fournit &lt;code&gt;write()&lt;/code&gt;, et du système. La déclaration ci-dessus n’est pas portable vers toutes les architectures : dans un vrai programme, il vaut mieux utiliser l’en-tête officiel.&lt;/p&gt;
&lt;p&gt;À l’inverse, un programme peut contenir des &lt;code&gt;#include&lt;/code&gt; et ne dépendre d’aucun OS : par exemple en utilisant des types ou des constantes fournis par des en-têtes adaptés à une cible embarquée.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Retirer un header ne supprime pas une dépendance. Pour supprimer une couche, il faut comprendre le service qu’elle rend et décider comment le remplacer.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;gcc-assembleur-linker-qui-fait-quoi&quot;&gt;GCC, assembleur, linker : qui fait quoi ?&lt;/h2&gt;
&lt;p&gt;Une autre question m’a longtemps embrouillé : comment peut-on créer un système d’exploitation si le compilateur qui le construit fonctionne lui-même sous un système d’exploitation ? Et si NASM est écrit dans un autre langage, comment peut-il être à l’origine du code machine ?&lt;/p&gt;
&lt;p&gt;La réponse tient dans une distinction : &lt;strong&gt;la machine sur laquelle on construit un programme n’est pas nécessairement celle sur laquelle il s’exécute.&lt;/strong&gt; Un compilateur peut s’exécuter sous Linux et produire du code pour un microcontrôleur ARM dépourvu de système d’exploitation. C’est la compilation croisée.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Chaîne de construction simplifiée de GCC, de haut en bas : source C, préprocesseur (gcc -E), compilateur (gcc -S) qui produit de l’assembleur, assembleur (gcc -c) qui produit un fichier objet, éditeur de liens (gcc ou ld), puis exécutable ou image firmware.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 600 500&quot; width=&quot;600&quot; height=&quot;500&quot;&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;80&quot; y=&quot;14&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;37.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Source C : fichier.c&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;59.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Texte destiné aux humains&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 72 L300 91 M296.0 84.1 L300 91 L304.0 84.1&quot;/&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;80&quot; y=&quot;93&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;116.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Préprocesseur : gcc -E&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;138.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Développe notamment les #include et macros&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 151 L300 170 M296.0 163.1 L300 170 L304.0 163.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;80&quot; y=&quot;172&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;195.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Compilateur : gcc -S&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;217.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Produit généralement de l’assembleur&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 230 L300 249 M296.0 242.1 L300 249 L304.0 242.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;80&quot; y=&quot;251&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;274.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Assembleur : gcc -c&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;296.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Produit un fichier objet, contenant du code machine&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 309 L300 328 M296.0 321.1 L300 328 L304.0 321.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;80&quot; y=&quot;330&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;353.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Éditeur de liens : gcc / ld&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;375.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Résout les symboles, organise les sections&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 388 L300 407 M296.0 400.1 L300 407 L304.0 400.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;80&quot; y=&quot;409&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;432.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Exécutable ou image firmware&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;454.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Format et conventions dépendant de la cible&lt;/text&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 4 — Chaîne de construction simplifiée de GCC. Les étapes sont ici séparées pour rendre leur rôle visible.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;NASM est un assembleur : il traduit du texte assembleur x86 en code objet ou en binaire brut selon le format demandé. Le linker n’interprète pas ce code à l’exécution ; il résout les références et construit le résultat. Et quand le processeur exécute ce résultat, ni NASM ni GCC ne sont nécessaires.&lt;/p&gt;
&lt;h3 id=&quot;observer-les-couches-soi-meme&quot;&gt;Observer les couches soi-même&lt;/h3&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;c&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; addition&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;int&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; int&lt;/span&gt;&lt;span&gt; b)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    return&lt;/span&gt;&lt;span&gt; a &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;+&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sous Linux, avec GCC et binutils :&lt;/p&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Observer chaque étape&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Observer chaque étape&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;gcc -E addition.c -o addition.i&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;gcc -S -O2 addition.c -o addition.s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;gcc -c -O2 addition.c -o addition.o&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;objdump -d addition.o&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;Sur x86-64 avec la convention d’appel System V, une traduction optimisée possible est (syntaxe Intel) :&lt;/p&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;asm&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;lea&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt; eax&lt;/span&gt;&lt;span&gt;, [&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;rdi&lt;/span&gt;&lt;span&gt; + &lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;rsi&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;ret&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;lea&lt;/code&gt; peut ici servir à calculer la somme sans accès mémoire. Ce sont des instructions machine, pas des appels à Linux. La convention d’appel précise comment les arguments et le résultat sont transmis, tandis que le jeu d’instructions définit ce que le processeur sait exécuter. D’autres options ou architectures peuvent donner un code différent.&lt;/p&gt;
&lt;h2 id=&quot;afficher-a-sans-libc-sans-header-et-sans-main&quot;&gt;Afficher A sans libc, sans header et sans main()&lt;/h2&gt;
&lt;p&gt;Descendons encore. Sous Linux x86-64, on peut demander directement une écriture au noyau avec l’instruction &lt;code&gt;syscall&lt;/code&gt;. Le numéro d’appel système de &lt;code&gt;write&lt;/code&gt; est 1 ; celui de &lt;code&gt;exit&lt;/code&gt; est 60. Les arguments sont placés dans les registres définis par l’ABI Linux x86-64.&lt;/p&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;a.s&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;asm&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;# GNU assembler, syntaxe AT&amp;amp;T&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;.global&lt;/span&gt;&lt;span&gt; _start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;section .text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-8&quot;&gt;_start&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    mov&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; $1&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;       # syscall write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    mov&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; $1&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;edi&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;       # descripteur stdout&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    lea&lt;/span&gt;&lt;span&gt; msg(%&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;rip&lt;/span&gt;&lt;span&gt;), %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;rsi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    mov&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; $1&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;       # un octet&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    syscall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    mov&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; $60&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;      # syscall exit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    xor&lt;/span&gt;&lt;span&gt; %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;edi&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;edi&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;     # code de retour 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    syscall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;section .rodata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-8&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 65&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.section .note.GNU-stack,&amp;quot;&amp;quot;,@progbits&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Assembler, lier et exécuter&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Assembler, lier et exécuter&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;gcc -nostdlib -static -no-pie a.s -o a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;./a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;objdump -d a&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;Le résultat est encore &lt;strong&gt;A&lt;/strong&gt;. Nous n’utilisons ni &lt;code&gt;main()&lt;/code&gt;, ni &lt;code&gt;write()&lt;/code&gt; de la libc, ni &lt;code&gt;printf()&lt;/code&gt;. Pourtant nous dépendons toujours de Linux : c’est lui qui comprend ces numéros d’appels système, fournit les descripteurs et termine notre processus.&lt;/p&gt;
&lt;p&gt;On peut également intégrer l’instruction dans du C avec l’assembleur inline de GCC (extension GNU, Linux x86-64) :&lt;/p&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;c&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;    char&lt;/span&gt;&lt;span&gt; c &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;#39;A&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;    long&lt;/span&gt;&lt;span&gt; r &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    __asm__ volatile&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;syscall&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-3&quot;&gt;        :&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;+a&amp;quot;&lt;/span&gt;&lt;span&gt;(r)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-3&quot;&gt;        :&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;D&amp;quot;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;1L&lt;/span&gt;&lt;span&gt;), &lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;S&amp;quot;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;c), &lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;d&amp;quot;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;1L&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-3&quot;&gt;        :&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;rcx&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;r11&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;cc&amp;quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;memory&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    return&lt;/span&gt;&lt;span&gt; r &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce deuxième exemple, compilé normalement, évite la fonction &lt;code&gt;write()&lt;/code&gt; de la libc pour l’écriture, mais utilise encore le démarrage et la terminaison habituels du programme C. Il ne faut donc pas le confondre avec l’exécutable précédent. Les numéros et conventions ne sont pas ceux de macOS ou de Windows, ni même ceux de toutes les architectures Linux. Les exemples sont volontairement minimalistes et ne traitent pas toutes les erreurs d’écriture.&lt;/p&gt;
&lt;h2 id=&quot;mais-qui-appelle-main&quot;&gt;Mais qui appelle main() ?&lt;/h2&gt;
&lt;p&gt;Dans un programme C classique, on présente souvent &lt;code&gt;main()&lt;/code&gt; comme le point de départ. Pourtant, lorsque notre programme s’exécute sous Linux, le noyau charge son image ELF et transfère le contrôle au point d’entrée prévu (éventuellement via le chargeur dynamique). Le code de démarrage de l’environnement C appelle ensuite &lt;code&gt;main()&lt;/code&gt;. Ce code prépare notamment ce dont la bibliothèque et le programme ont besoin.&lt;/p&gt;
&lt;p&gt;Et si on retire Linux ? Sur une cible bare-metal, le processeur commence son exécution selon des règles de démarrage définies par son architecture. Sur un PC x86 typique, le firmware de la plateforme prend en charge les premières étapes. Sur un microcontrôleur, le mécanisme peut être différent, par exemple une table de vecteurs contenant l’adresse de la routine de reset.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Exemple de démarrage bare-metal, de haut en bas : mise sous tension ou reset, firmware ou routine de reset, code de démarrage (initialisation de la pile et de la mémoire), initialisation de .data et .bss, puis main() ou autre point d’entrée.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 600 421&quot; width=&quot;600&quot; height=&quot;421&quot;&gt;   &lt;rect class=&quot;fig-box fig-box-strong&quot; x=&quot;80&quot; y=&quot;14&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;300.0&quot; y=&quot;37.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot; font-weight=&quot;650&quot;&gt;Mise sous tension / reset&lt;/text&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;300.0&quot; y=&quot;59.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;État initial défini par le matériel&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 72 L300 91 M296.0 84.1 L300 91 L304.0 84.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;80&quot; y=&quot;93&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;116.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Firmware ou routine de reset&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;138.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Premières instructions machine&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 151 L300 170 M296.0 163.1 L300 170 L304.0 163.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;80&quot; y=&quot;172&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;195.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Code de démarrage&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;217.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Initialisation de la pile et de la mémoire&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 230 L300 249 M296.0 242.1 L300 249 L304.0 242.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;80&quot; y=&quot;251&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;274.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Initialisation de .data et .bss&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;296.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Selon la disposition mémoire du firmware&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 309 L300 328 M296.0 321.1 L300 328 L304.0 321.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;80&quot; y=&quot;330&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;353.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;main() ou autre point d’entrée&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;375.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Le code applicatif, enfin&lt;/text&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 5 — Exemple de démarrage bare-metal. Sur un PC, un chargeur et un noyau peuvent s’intercaler ; sur un microcontrôleur, le chemin dépend de l’architecture.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Le C distingue les environnements &lt;em&gt;hosted&lt;/em&gt; (avec l’environnement standard prévu) et &lt;em&gt;freestanding&lt;/em&gt;, dans lesquels le démarrage et la bibliothèque disponibles sont définis par l’implémentation. GCC propose notamment l’option &lt;code&gt;-ffreestanding&lt;/code&gt;.&lt;/p&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Compiler pour une cible bare-metal ARM&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Compiler pour une cible bare-metal ARM&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;arm-none-eabi-gcc -ffreestanding -c firmware.c -o firmware.o&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;Cette commande produit un fichier objet, pas encore un firmware démarrable. Il faut généralement ajouter le code de démarrage, décrire la disposition mémoire dans un script de linkage, lier les objets et fournir les éventuelles routines dont le compilateur a besoin. &lt;code&gt;-ffreestanding&lt;/code&gt; ne fait pas tout cela automatiquement.&lt;/p&gt;
&lt;p&gt;Par exemple, pour &lt;code&gt;int x = 42;&lt;/code&gt; et &lt;code&gt;int y;&lt;/code&gt; définis globalement, une organisation bare-metal typique conserve les valeurs initiales de &lt;code&gt;.data&lt;/code&gt; en flash, les copie en RAM au démarrage et remet &lt;code&gt;.bss&lt;/code&gt; à zéro. Le processeur ne connaît pas les règles du C : c’est l’environnement que nous construisons qui les rend vraies.&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Section typique&lt;/th&gt;&lt;th&gt;Contenu&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.text&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Instructions machine&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.rodata&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Données en lecture seule&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.data&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Données statiques initialisées non nulles, généralement&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.bss&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Données statiques à initialiser à zéro, généralement&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Et si &lt;code&gt;main()&lt;/code&gt; retourne ? Sans OS, il n’y a pas forcément de processus à terminer. Le code de démarrage peut prévoir une boucle infinie, une mise en veille ou un redémarrage. C’est à nous de définir la suite.&lt;/p&gt;
&lt;h2 id=&quot;un-a-sans-systeme-d-exploitation&quot;&gt;Un A sans système d’exploitation&lt;/h2&gt;
&lt;p&gt;Cette fois, supprimons le noyau. Il n’existe plus de descripteur 1 ni de sortie standard à moins de les implémenter nous-mêmes. Pour afficher quelque chose, il faut exploiter une interface matérielle : un port série, un contrôleur vidéo ou un framebuffer, par exemple.&lt;/p&gt;
&lt;h3 id=&quot;un-secteur-de-demarrage-x86-minimal&quot;&gt;Un secteur de démarrage x86 minimal&lt;/h3&gt;
&lt;p&gt;Sur un PC compatible BIOS traditionnel, en mode texte VGA, la mémoire vidéo peut être accessible à l’adresse physique &lt;code&gt;0xB8000&lt;/code&gt;. Un mot de 16 bits contient alors le code du caractère (octet de poids faible) et son attribut de couleur (octet de poids fort). Voici un exemple concret, destiné à une machine virtuelle de type PC avec BIOS, en supposant le mode texte couleur déjà actif :&lt;/p&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;boot.s&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;asm&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;# GNU assembler, x86 en mode réel 16 bits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.code16&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;.global&lt;/span&gt;&lt;span&gt; _start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;section .text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-8&quot;&gt;_start&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    cli&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    mov&lt;/span&gt;&lt;span&gt; $0xb800, %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;ax&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    mov&lt;/span&gt;&lt;span&gt; %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;ax&lt;/span&gt;&lt;span&gt;, %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;es&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    movw $0x0741, %&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;es&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;  # A, gris clair sur fond noir&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-6&quot;&gt;1&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    hlt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;    jmp&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 1b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.org &lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;510&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 0xaa55&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Assembler et lancer sous QEMU&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Assembler et lancer sous QEMU&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;as --32 boot.s -o boot.o&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;ld -m elf_i386 -Ttext 0x7c00 --oformat binary boot.o -o boot.bin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;qemu-system-i386 -drive file=boot.bin,format=raw,if=floppy -boot a&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;Le BIOS de la machine virtuelle charge le secteur de démarrage et lui transfère le contrôle. Notre code écrit directement dans la mémoire vidéo émulée. Le A devrait apparaître dans la première case de l’écran VGA. Il n’y a ni DOS ni Linux dans la machine virtuelle. Il reste du firmware, du matériel (ici émulé) et un environnement hôte qui fait tourner QEMU ; ce n’est donc pas un démarrage à partir de transistors vierges. Cet exemple n’est pas compatible tel quel avec un PC UEFI moderne sans mode de compatibilité BIOS. Ne pas écrire cette image sur un disque physique.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Le mot de 16 bits 0x0741 est écrit à l’adresse 0xB8000 de la mémoire vidéo VGA, ce qui affiche un caractère à l’écran. L’octet de poids faible 0x41 est le caractère A, l’octet de poids fort 0x07 est l’attribut de couleur.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 680 244&quot; width=&quot;680&quot; height=&quot;244&quot;&gt;   &lt;rect class=&quot;fig-box fig-box-strong&quot; x=&quot;20&quot; y=&quot;25&quot; width=&quot;204&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;122.0&quot; y=&quot;50.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot; font-weight=&quot;650&quot;&gt;0x0741&lt;/text&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;122.0&quot; y=&quot;72.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Mot de 16 bits&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M224 56 L247 56 M240.1 60.0 L247 56 L240.1 52.0&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;250&quot; y=&quot;25&quot; width=&quot;190&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;345.0&quot; y=&quot;50.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0xB8000&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;345.0&quot; y=&quot;72.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Mémoire vidéo VGA&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M440 56 L462 56 M455.1 60.0 L462 56 L455.1 52.0&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;465&quot; y=&quot;25&quot; width=&quot;195&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;562.5&quot; y=&quot;50.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Affichage texte&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;562.5&quot; y=&quot;72.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Contrôleur VGA&lt;/text&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;20&quot; y=&quot;125&quot; width=&quot;204&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;122.0&quot; y=&quot;148.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0x07&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;122.0&quot; y=&quot;170.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Attribut de couleur&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;250&quot; y=&quot;125&quot; width=&quot;190&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;345.0&quot; y=&quot;148.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;0x41&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;345.0&quot; y=&quot;170.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Caractère A&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M122 88 L122 123 M118.0 116.1 L122 123 L126.0 116.1&quot;/&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M345 88 L345 123 M341.0 116.1 L345 123 L349.0 116.1&quot;/&gt;   &lt;rect class=&quot;fig-screen&quot; x=&quot;490&quot; y=&quot;112&quot; width=&quot;145&quot; height=&quot;99&quot; rx=&quot;8&quot;/&gt;   &lt;text class=&quot;fig-screen-text&quot; x=&quot;511&quot; y=&quot;153&quot; font-size=&quot;30&quot;&gt;A&lt;/text&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 6 — Sur cette architecture historique, écrire un mot à une adresse associée au périphérique vidéo suffit à modifier une cellule de texte.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Sur un microcontrôleur, une opération analogue en C peut consister à écrire dans un registre matériel mappé en mémoire :&lt;/p&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;c&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;/* Exemple conceptuel : adresse volontairement fictive. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;volatile&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; unsigned int&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; *&lt;/span&gt;&lt;span&gt;uart &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;volatile&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; unsigned int&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;0x12340000u&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-3&quot;&gt;*&lt;/span&gt;&lt;span&gt;uart &lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 65&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Le mot-clé &lt;code&gt;volatile&lt;/code&gt; signale au compilateur que l’accès doit être conservé selon les règles de l’implémentation. Il ne configure pas le périphérique et ne garantit ni l’ordre matériel de toutes les opérations ni leur atomicité. Il faut suivre la documentation du composant : adresse réelle, largeur du registre, initialisation, état prêt à transmettre, etc. &lt;strong&gt;Le C peut donc servir à piloter le matériel sans qu’un OS ne fournisse le service.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;sous-les-0-et-les-1-il-y-a-quoi&quot;&gt;Sous les 0 et les 1, il y a quoi ?&lt;/h2&gt;
&lt;p&gt;Jusqu’ici, nous avons surtout supprimé des couches logicielles. Mais même le code machine et les octets sont encore des abstractions. Dans un circuit numérique, un 0 et un 1 sont représentés par des états physiques, souvent des plages de tension. Des transistors contrôlent le passage du courant, et leur combinaison réalise des fonctions logiques.&lt;/p&gt;
&lt;p&gt;Prenons un additionneur sur un bit. Deux portes suffisent à illustrer le principe : XOR produit le bit de somme, AND produit la retenue. Pour &lt;code&gt;1 + 1&lt;/code&gt;, XOR donne 0 et AND donne 1. Nous obtenons donc &lt;code&gt;10&lt;/code&gt; en binaire.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Demi-additionneur : les entrées A = 1 et B = 1 alimentent une porte XOR, qui donne la somme S = 0, et une porte AND, qui donne la retenue C = 1. Résultat : 1 + 1 = 10 en binaire.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 680 276&quot; width=&quot;680&quot; height=&quot;276&quot;&gt;   &lt;rect class=&quot;fig-box fig-box-strong&quot; x=&quot;26&quot; y=&quot;35&quot; width=&quot;108&quot; height=&quot;48&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;80.0&quot; y=&quot;64.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot; font-weight=&quot;650&quot;&gt;A = 1&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-strong&quot; x=&quot;26&quot; y=&quot;139&quot; width=&quot;108&quot; height=&quot;48&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;80.0&quot; y=&quot;168.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot; font-weight=&quot;650&quot;&gt;B = 1&lt;/text&gt;   &lt;g class=&quot;fig-arrow fig-wire&quot;&gt;   &lt;path d=&quot;M134 59 H175 V45 H270 M175 59 V141 H270 M134 163 H213 V77 H270 M213 163 V173 H270&quot;/&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-dot&quot;&gt;   &lt;circle cx=&quot;175&quot; cy=&quot;59&quot; r=&quot;4&quot;/&gt;   &lt;circle cx=&quot;213&quot; cy=&quot;163&quot; r=&quot;4&quot;/&gt;   &lt;/g&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;270&quot; y=&quot;30&quot; width=&quot;155&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;347.5&quot; y=&quot;55.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;XOR&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;347.5&quot; y=&quot;77.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Somme&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;270&quot; y=&quot;126&quot; width=&quot;155&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;347.5&quot; y=&quot;151.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;AND&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;347.5&quot; y=&quot;173.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Retenue&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M425 61 L478 61 M471.1 65.0 L478 61 L471.1 57.0&quot;/&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M425 157 L478 157 M471.1 161.0 L478 157 L471.1 153.0&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;480&quot; y=&quot;35&quot; width=&quot;170&quot; height=&quot;52&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;565.0&quot; y=&quot;66.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;S = 0&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;480&quot; y=&quot;131&quot; width=&quot;170&quot; height=&quot;52&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;565.0&quot; y=&quot;162.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;C = 1&lt;/text&gt;   &lt;text class=&quot;fig-accent-text&quot; x=&quot;340&quot; y=&quot;238&quot; text-anchor=&quot;middle&quot; font-size=&quot;22&quot; font-weight=&quot;750&quot;&gt;1 + 1 = 10₂&lt;/text&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 7 — Demi-additionneur : la somme et la retenue résultent du comportement des portes. Les blocs sont des symboles fonctionnels, pas un plan de câblage.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;À partir de circuits logiques, on peut fabriquer des éléments capables de mémoriser des bits, les assembler en registres, relier ces registres à un bus et réaliser une unité arithmétique et logique (ALU). On obtient ensuite un processeur en ajoutant un mécanisme de lecture des instructions et une logique de contrôle.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Architecture simplifiée d’un processeur élémentaire : le compteur d’instructions (PC) donne l’adresse à la mémoire, qui fournit l’instruction au registre d’instruction (IR). L’unité de contrôle, en aval de l’IR, envoie des signaux de commande (en pointillés) au registre A, à l’ALU et au registre B. Ces trois éléments sont reliés à un bus de données.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 680 360&quot; width=&quot;680&quot; height=&quot;360&quot;&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;20&quot; y=&quot;20&quot; width=&quot;182&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;111.0&quot; y=&quot;45.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Compteur (PC)&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;111.0&quot; y=&quot;67.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Adresse suivante&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M202 51 L246 51 M239.1 55.0 L246 51 L239.1 47.0&quot;/&gt;   &lt;rect class=&quot;fig-box&quot; x=&quot;249&quot; y=&quot;20&quot; width=&quot;182&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340.0&quot; y=&quot;45.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Mémoire&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;340.0&quot; y=&quot;67.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Instructions / données&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M431 51 L475 51 M468.1 55.0 L475 51 L468.1 47.0&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;478&quot; y=&quot;20&quot; width=&quot;182&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;569.0&quot; y=&quot;45.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Registre IR&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;569.0&quot; y=&quot;67.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Instruction courante&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M569 82 L569 118 M565.0 111.1 L569 118 L573.0 111.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;478&quot; y=&quot;120&quot; width=&quot;182&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;569.0&quot; y=&quot;143.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Contrôle&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;569.0&quot; y=&quot;165.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Signaux de commande&lt;/text&gt;   &lt;path class=&quot;fig-arrow fig-dashed&quot; d=&quot;M569 178 V202 H111 M340 202 V218 M111 202 V218 M569 202 V218&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;20&quot; y=&quot;220&quot; width=&quot;182&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;111.0&quot; y=&quot;256.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Registre A&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;249&quot; y=&quot;220&quot; width=&quot;182&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340.0&quot; y=&quot;245.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;ALU&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;340.0&quot; y=&quot;267.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Addition / logique&lt;/text&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;478&quot; y=&quot;220&quot; width=&quot;182&quot; height=&quot;62&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;569.0&quot; y=&quot;256.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Registre B&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M202 251 L246 251 M239.1 255.0 L246 251 L239.1 247.0&quot;/&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M478 251 L434 251 M440.9 247.0 L434 251 L440.9 255.0&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;28&quot; y=&quot;309&quot; width=&quot;624&quot; height=&quot;22&quot; rx=&quot;7&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;340&quot; y=&quot;325&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Bus de données (liaisons simplifiées)&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M111 282 L111 307 M107.0 300.1 L111 307 L115.0 300.1&quot;/&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M340 282 L340 307 M336.0 300.1 L340 307 L344.0 300.1&quot;/&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M569 282 L569 307 M565.0 300.1 L569 307 L573.0 300.1&quot;/&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 8 — Modèle pédagogique : mémoire, compteur d’instructions, registre d’instruction, unité de contrôle, registres et ALU. Les signaux de commande sont en pointillés.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Une horloge cadence les opérations dans de nombreux circuits. Le compteur d’instructions indique où chercher la prochaine instruction ; le registre d’instruction conserve celle en cours ; l’unité de contrôle active les circuits appropriés. Dans certains processeurs, une partie de cette logique s’appuie sur du microcode.&lt;/p&gt;
&lt;p&gt;Quand on attribue à une combinaison de bits le rôle d’une instruction &lt;code&gt;ADD&lt;/code&gt;, il ne suffit pas d’écrire le mot ADD dans un tableau. Il faut concevoir le circuit de contrôle pour que cette combinaison provoque effectivement l’addition et le stockage du résultat. Le mot assembleur n’est qu’une notation humaine.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Au bout de la chaîne, aucun compilateur caché ne traduit les instructions pour les transistors. Le circuit physique réalise le comportement que ses concepteurs ont prévu.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;alors-comment-a-t-on-cree-les-premiers-systemes&quot;&gt;Alors, comment a-t-on créé les premiers systèmes ?&lt;/h2&gt;
&lt;p&gt;Il reste le paradoxe apparent : si les outils modernes sont construits avec d’autres outils, comment a-t-on fabriqué les premiers ? C’est ce qu’on appelle le &lt;em&gt;bootstrapping&lt;/em&gt;, ou amorçage au sens large.&lt;/p&gt;
&lt;p&gt;Les premières machines ont été programmées de différentes manières selon leur architecture : câblage, interrupteurs, cartes ou rubans perforés, saisie de code machine, puis outils de traduction rudimentaires. Il était possible de construire un petit programme avec les moyens disponibles et de l’utiliser pour en produire un plus complexe. L’outil créé hier sert à construire celui de demain. On peut ainsi parvenir à un compilateur capable de compiler son propre code source, avec une version antérieure ou une autre chaîne de compilation comme point de départ.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;L’amorçage des outils, de haut en bas : matériel conçu par des humains, code machine et outils rudimentaires, assembleurs et outils plus élaborés, compilateurs et systèmes, puis outils modernes qui peuvent construire les générations suivantes.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 600 421&quot; width=&quot;600&quot; height=&quot;421&quot;&gt;   &lt;rect class=&quot;fig-box fig-box-strong&quot; x=&quot;80&quot; y=&quot;14&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;300.0&quot; y=&quot;37.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot; font-weight=&quot;650&quot;&gt;Matériel conçu par des humains&lt;/text&gt;   &lt;text class=&quot;fig-on-strong&quot; x=&quot;300.0&quot; y=&quot;59.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Règles électriques et jeu d’instructions&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 72 L300 91 M296.0 84.1 L300 91 L304.0 84.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-amber&quot; x=&quot;80&quot; y=&quot;93&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;116.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Code machine / outils rudimentaires&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;138.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Saisie ou chargement selon la machine&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 151 L300 170 M296.0 163.1 L300 170 L304.0 163.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;80&quot; y=&quot;172&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;195.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Assembleurs et outils plus élaborés&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;217.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Traduction de représentations symboliques&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 230 L300 249 M296.0 242.1 L300 249 L304.0 242.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-blue&quot; x=&quot;80&quot; y=&quot;251&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;274.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Compilateurs et systèmes&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;296.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Construction de nouvelles abstractions&lt;/text&gt;   &lt;path class=&quot;fig-arrow&quot; d=&quot;M300 309 L300 328 M296.0 321.1 L300 328 L304.0 321.1&quot;/&gt;   &lt;rect class=&quot;fig-box fig-box-mint&quot; x=&quot;80&quot; y=&quot;330&quot; width=&quot;440&quot; height=&quot;58&quot; rx=&quot;11&quot;/&gt;   &lt;text class=&quot;fig-text&quot; x=&quot;300.0&quot; y=&quot;353.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;17&quot;&gt;Outils modernes&lt;/text&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;300.0&quot; y=&quot;375.0&quot; text-anchor=&quot;middle&quot; font-size=&quot;13&quot;&gt;Peuvent construire les générations suivantes&lt;/text&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Figure 9 — L’amorçage des outils, représenté comme une progression conceptuelle et non comme une chronologie universelle.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;DOS illustre bien ce principe. Microsoft est parti d’un système antérieur, 86-DOS (également appelé QDOS à ses débuts), développé chez Seattle Computer Products par Tim Paterson. Les premières versions de MS-DOS étaient écrites en assembleur 8086. Leurs sources sont aujourd’hui consultables. Elles n’avaient évidemment pas besoin de &lt;code&gt;windows.h&lt;/code&gt; ou de POSIX pour définir leurs propres interfaces. Les premières versions de Windows se sont ensuite appuyées sur DOS ; la lignée Windows NT, à l’origine des Windows modernes, repose sur une architecture différente.&lt;/p&gt;
&lt;p&gt;Il n’y a donc aucune dépendance circulaire incontournable. Un OS est un ensemble de programmes que le matériel peut exécuter. Un autre OS peut servir à le construire, sans devenir une dépendance de son exécution. Le démarrage d’une machine et l’amorçage historique des outils sont deux problèmes distincts, même s’ils partagent l’idée de commencer avec quelque chose de plus élémentaire.&lt;/p&gt;
&lt;h2 id=&quot;et-si-on-construisait-vraiment-un-ordinateur&quot;&gt;Et si on construisait vraiment un ordinateur ?&lt;/h2&gt;
&lt;p&gt;À ce stade, continuer à lire des documentations Linux ne suffit peut-être plus à satisfaire ma curiosité. J’ai envie de voir les bits changer physiquement, de comprendre comment un compteur avance, pourquoi un registre retient une valeur et comment un opcode déclenche une addition.&lt;/p&gt;
&lt;p&gt;C’est tout l’intérêt du projet &lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://eater.net/8bit&quot;&gt;Build an 8-bit computer from scratch&lt;/a&gt; de Ben Eater. Il construit un ordinateur programmable à huit bits sur des plaques de prototypage, avec des circuits logiques, des mémoires, des LED et des fils. On peut avancer module par module et observer le fonctionnement de chacun.&lt;/p&gt;
&lt;p&gt;Je commencerais par l’horloge, puis les registres, l’ALU, la RAM, le compteur d’instructions, le bus et la logique de contrôle. Son ordinateur possède notamment une mémoire de 16 octets dans sa configuration de base et une sortie numérique sur afficheurs à sept segments. Ce n’est pas un terminal ASCII moderne, et c’est précisément ce qui rend le projet intéressant : chaque capacité doit être construite explicitement.&lt;/p&gt;
&lt;p&gt;Programme symbolique pour l’ordinateur 8 bits de Ben Eater :&lt;/p&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;plain&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Adresse   Instruction / donnée&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0         LDA 14&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1         ADD 15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;2         OUT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;3         HLT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;14        28&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;15        14&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ce petit programme charge 28, ajoute 14, affiche 42 et s’arrête. Le programme peut être saisi directement en mémoire sous forme d’octets correspondant aux opcodes et aux opérandes. Aucun noyau, aucun appel POSIX, aucun compilateur n’est requis pour son exécution.&lt;/p&gt;
&lt;p&gt;Une fois l’architecture comprise, pourquoi ne pas écrire notre propre mini-assembleur en C ? Il convertirait les mnémoniques &lt;code&gt;LDA&lt;/code&gt;, &lt;code&gt;ADD&lt;/code&gt;, &lt;code&gt;OUT&lt;/code&gt; et &lt;code&gt;HLT&lt;/code&gt; en octets adaptés à notre machine. Nous aurions alors reconstruit, à une échelle minuscule, la chaîne qui nous paraissait si obscure au départ.&lt;/p&gt;
&lt;h3 id=&quot;le-prochain-defi&quot;&gt;Le prochain défi&lt;/h3&gt;
&lt;p&gt;Je ne cherche pas à bannir les abstractions. Elles sont indispensables pour écrire du logiciel moderne. Mais j’aimerais pouvoir les traverser mentalement : comprendre ce que fait un appel système, ce qu’est une instruction, comment elle arrive au processeur et ce qui se passe lorsqu’elle rencontre des circuits.&lt;/p&gt;
&lt;p&gt;Partis d’un simple &lt;code&gt;char c = 65;&lt;/code&gt;, nous avons rencontré des conventions d’encodage, des bibliothèques, le noyau, les outils de compilation, le firmware, les registres matériels, les portes logiques et les transistors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Finalement, afficher la lettre A n’était peut-être pas si simple. Et c’est exactement pour ça que l’informatique est passionnante.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;pour-creuser-et-reproduire&quot;&gt;Pour creuser et reproduire&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://www.rfc-editor.org/rfc/rfc20&quot;&gt;RFC 20 — ASCII format for network interchange&lt;/a&gt; et &lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://www.rfc-editor.org/rfc/rfc3629&quot;&gt;RFC 3629 — UTF-8&lt;/a&gt; : conventions de représentation des caractères.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://man7.org/linux/man-pages/man2/write.2.html&quot;&gt;Linux man-pages : write(2)&lt;/a&gt; et &lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://man7.org/linux/man-pages/man2/syscall.2.html&quot;&gt;syscall(2)&lt;/a&gt; : interfaces, descripteurs et conventions d’appel.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/torvalds/linux/blob/master/arch/x86/entry/syscalls/syscall_64.tbl&quot;&gt;Noyau Linux — table des appels système x86-64&lt;/a&gt; : numéros des appels.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html&quot;&gt;GCC — options de dialecte C&lt;/a&gt; et &lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html&quot;&gt;options de linkage&lt;/a&gt; : environnements freestanding et construction des binaires.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://computerhistory.org/blog/microsoft-ms-dos-early-source-code/&quot;&gt;Computer History Museum — Early MS-DOS source code&lt;/a&gt; et &lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://opensource.microsoft.com/blog/2024/04/25/open-sourcing-ms-dos-4-0/&quot;&gt;Microsoft — publication des sources de MS-DOS&lt;/a&gt; : contexte historique.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://eater.net/8bit&quot;&gt;Ben Eater — Build an 8-bit computer from scratch&lt;/a&gt; et &lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://eater.net/8bit/schematics&quot;&gt;schémas du projet&lt;/a&gt; : construction pas à pas.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Les neuf schémas de cet article sont des illustrations pédagogiques originales et autonomes. Ils simplifient certaines architectures et ne constituent pas des plans de câblage. Les exemples exécutables ciblent explicitement les environnements indiqués.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="fr">
        <title>STM32F407 Rust Embedded Lab</title>
        <published>2026-11-19T00:00:00+00:00</published>
        <updated>2026-11-19T00:00:00+00:00</updated>
        <author>
          <name>nostd</name>
        </author>
        <link rel="alternate" type="text/html" href="https://nostd.fr/projets/stm32-rust-playground/"/>
        <id>https://nostd.fr/projets/stm32-rust-playground/</id>
        <summary type="text">Un laboratoire Rust no_std sur STM32F407 pour mesurer ce que la simulation Renode peut valider à la place de la carte, et ce qu&#39;elle ne peut pas.</summary>
        <content type="html" xml:base="https://nostd.fr/projets/stm32-rust-playground/">&lt;h2 id=&quot;le-probleme&quot;&gt;Le problème&lt;/h2&gt;
&lt;p&gt;Un firmware se teste mal. La carte est sur un bureau, pas dans l’intégration continue, et
un test « à la main » ne se rejoue pas à chaque commit. Un simulateur comme
&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https://renode.io/&quot;&gt;Renode&lt;/a&gt; promet de faire tourner le même binaire sans matériel.&lt;/p&gt;
&lt;p&gt;Reste à savoir jusqu’où on peut le croire. Ce dépôt sert à ça : écrire de petits firmwares
Rust pour le STM32F407, les lancer dans Renode &lt;strong&gt;et&lt;/strong&gt; sur la STM32F407G-DISC1, puis noter
précisément où les deux divergent. L’objectif final est une chaîne CI/CD qui automatise
tout ce que la simulation sait valider, et qui réserve la carte au reste.&lt;/p&gt;
&lt;h2 id=&quot;l-architecture&quot;&gt;L’architecture&lt;/h2&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Les sources de apps/ passent par cargo build pour la cible thumbv7em et donnent un fichier ELF. Ce même fichier part dans deux directions : la simulation Renode, et la carte Discovery par probe-rs. Les deux résultats sont comparés et les écarts relevés. La compilation et la simulation sont destinées à tourner dans GitHub Actions.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 760 236&quot; width=&quot;760&quot; height=&quot;236&quot;&gt;   &lt;rect class=&quot;fig-line fig-dashed&quot; x=&quot;150&quot; y=&quot;8&quot; width=&quot;406&quot; height=&quot;130&quot;/&gt;   &lt;text class=&quot;fig-dim&quot; x=&quot;162&quot; y=&quot;28&quot;&gt;objectif : GitHub Actions&lt;/text&gt;   &lt;g class=&quot;fig-fill fig-line&quot;&gt;     &lt;rect x=&quot;4&quot; y=&quot;56&quot; width=&quot;124&quot; height=&quot;60&quot;/&gt;     &lt;rect x=&quot;166&quot; y=&quot;56&quot; width=&quot;150&quot; height=&quot;60&quot;/&gt;     &lt;rect x=&quot;390&quot; y=&quot;56&quot; width=&quot;150&quot; height=&quot;60&quot;/&gt;     &lt;rect x=&quot;390&quot; y=&quot;164&quot; width=&quot;150&quot; height=&quot;60&quot;/&gt;     &lt;rect x=&quot;612&quot; y=&quot;110&quot; width=&quot;144&quot; height=&quot;60&quot;/&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-accent&quot;&gt;     &lt;path d=&quot;M128 86h36M158 81l6 5-6 5&quot;/&gt;     &lt;path d=&quot;M316 86h72M382 81l6 5-6 5&quot;/&gt;     &lt;path d=&quot;M352 86v108h36M382 189l6 5-6 5&quot;/&gt;     &lt;path d=&quot;M540 86h36v54h34M604 135l6 5-6 5&quot;/&gt;     &lt;path d=&quot;M540 194h36v-54&quot;/&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-text&quot; text-anchor=&quot;middle&quot;&gt;     &lt;text x=&quot;66&quot; y=&quot;82&quot;&gt;apps/&lt;/text&gt;     &lt;text x=&quot;241&quot; y=&quot;82&quot;&gt;cargo build&lt;/text&gt;     &lt;text x=&quot;465&quot; y=&quot;82&quot;&gt;Renode&lt;/text&gt;     &lt;text x=&quot;465&quot; y=&quot;190&quot;&gt;probe-rs&lt;/text&gt;     &lt;text x=&quot;684&quot; y=&quot;136&quot;&gt;comparaison&lt;/text&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-dim&quot; text-anchor=&quot;middle&quot;&gt;     &lt;text x=&quot;66&quot; y=&quot;102&quot;&gt;Rust no_std&lt;/text&gt;     &lt;text x=&quot;241&quot; y=&quot;102&quot;&gt;thumbv7em, ELF&lt;/text&gt;     &lt;text x=&quot;465&quot; y=&quot;102&quot;&gt;simulation&lt;/text&gt;     &lt;text x=&quot;465&quot; y=&quot;210&quot;&gt;carte Discovery&lt;/text&gt;     &lt;text x=&quot;684&quot; y=&quot;156&quot;&gt;écarts relevés&lt;/text&gt;   &lt;/g&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;Un seul binaire, deux exécutions. La partie encadrée est celle visée pour l’intégration continue.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Le dépôt est un espace de travail Cargo, un paquet par expérience.&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dossier&lt;/th&gt;&lt;th&gt;Contenu&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;apps/00-smoke&lt;/code&gt;&lt;/td&gt;&lt;td&gt;firmware minimal : démarre et boucle&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;apps/01-gpio-pac&lt;/code&gt;&lt;/td&gt;&lt;td&gt;LED sur PD12, registre par registre avec le PAC&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;apps/01-gpio-hal&lt;/code&gt;&lt;/td&gt;&lt;td&gt;la même LED avec &lt;code&gt;stm32f4xx-hal&lt;/code&gt;, horloge à 48 MHz&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;apps/02-exti-pac&lt;/code&gt;&lt;/td&gt;&lt;td&gt;bouton sur PA0, interruption EXTI0, avec le PAC&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;renode/scripts&lt;/code&gt;&lt;/td&gt;&lt;td&gt;un script &lt;code&gt;.resc&lt;/code&gt; par firmware : carte, firmware, démarrage&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;renode/platforms&lt;/code&gt;&lt;/td&gt;&lt;td&gt;surcouches &lt;code&gt;.repl&lt;/code&gt; : LED virtuelle, correction du câblage simulé&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;memory.x&lt;/code&gt;&lt;/td&gt;&lt;td&gt;1 Mo de flash, 128 Ko de RAM&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;les-choix-techniques&quot;&gt;Les choix techniques&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Du PAC vers le HAL, dans cet ordre.&lt;/strong&gt; Chaque périphérique est d’abord piloté par ses
registres, puis avec le HAL. On sait ainsi ce que l’abstraction fait à notre place, et
on peut le vérifier dans la simulation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Le même ELF partout.&lt;/strong&gt; &lt;code&gt;cargo run&lt;/code&gt; flashe la carte avec &lt;code&gt;probe-rs&lt;/code&gt; ; le script Renode
charge le fichier produit par &lt;code&gt;cargo build&lt;/code&gt;. Rien n’est recompilé pour la simulation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Profil &lt;code&gt;release&lt;/code&gt; orienté taille&lt;/strong&gt; : &lt;code&gt;opt-level = &quot;s&quot;&lt;/code&gt;, LTO, une seule unité de
génération de code, &lt;code&gt;panic = &quot;abort&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Les écarts de simulation sont corrigés dans le dépôt, pas contournés dans le
firmware.&lt;/strong&gt; Quand Renode câble mal une broche, c’est une surcouche &lt;code&gt;.repl&lt;/code&gt; qui le répare.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;L’exemple le plus parlant est le bouton. Dans la description &lt;code&gt;stm32f4.repl&lt;/code&gt; de Renode, la
broche PA0 est reliée à l’EXTI, puis redéfinie plus bas pour deux temporisateurs, ce qui
efface la première liaison. Le firmware est correct, mais l’appui n’arrive jamais.&lt;/p&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;renode/platforms/exti-pac.repl&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;plain&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;gpioPortA:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    0 -&amp;gt; exti@0 | timer2@00 | timer5@00&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;gpioPortD:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    12 -&amp;gt; UserLED@0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;h2 id=&quot;les-mesures&quot;&gt;Les mesures&lt;/h2&gt;
&lt;p&gt;Relevées le 19 novembre 2026 avec rustc 1.99.1 et Renode 1.17.0.&lt;/p&gt;
&lt;h3 id=&quot;taille-des-firmwares&quot;&gt;Taille des firmwares&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;cargo size --release&lt;/code&gt;, en octets.&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Firmware&lt;/th&gt;&lt;th&gt;&lt;code&gt;text&lt;/code&gt;&lt;/th&gt;&lt;th&gt;&lt;code&gt;data&lt;/code&gt;&lt;/th&gt;&lt;th&gt;&lt;code&gt;bss&lt;/code&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;smoke&lt;/code&gt;&lt;/td&gt;&lt;td&gt;1140&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;gpio-pac&lt;/code&gt;&lt;/td&gt;&lt;td&gt;796&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;gpio-hal&lt;/code&gt;&lt;/td&gt;&lt;td&gt;852&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;exti-pac&lt;/code&gt;&lt;/td&gt;&lt;td&gt;864&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Le HAL ne coûte que 56 octets de plus que le PAC, alors qu’il configure en plus la PLL.&lt;/p&gt;
&lt;h3 id=&quot;ecarts-entre-renode-et-le-comportement-attendu-sur-la-carte&quot;&gt;Écarts entre Renode et le comportement attendu sur la carte&lt;/h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Firmware&lt;/th&gt;&lt;th&gt;Attendu sur la carte&lt;/th&gt;&lt;th&gt;Observé dans Renode&lt;/th&gt;&lt;th&gt;Cause&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;gpio-pac&lt;/code&gt;&lt;/td&gt;&lt;td&gt;la LED change d’état toutes les 2 s&lt;/td&gt;&lt;td&gt;toutes les 0,44 s&lt;/td&gt;&lt;td&gt;SysTick simulé à 72 MHz, firmware réglé pour 16 MHz&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;gpio-hal&lt;/code&gt;&lt;/td&gt;&lt;td&gt;toutes les 1 s&lt;/td&gt;&lt;td&gt;toutes les 83 ms&lt;/td&gt;&lt;td&gt;SysTick simulé à 72 MHz, HAL réglé pour 6 MHz&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;exti-pac&lt;/code&gt;&lt;/td&gt;&lt;td&gt;l’appui allume la LED&lt;/td&gt;&lt;td&gt;rien sans la surcouche, &lt;code&gt;True&lt;/code&gt; avec&lt;/td&gt;&lt;td&gt;liaison PA0 vers EXTI écrasée dans &lt;code&gt;stm32f4.repl&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;exti-pac&lt;/code&gt;&lt;/td&gt;&lt;td&gt;l’écriture dans SYSCFG est nécessaire&lt;/td&gt;&lt;td&gt;registre ignoré, avec un avertissement&lt;/td&gt;&lt;td&gt;&lt;code&gt;SYSCFG_EXTICR1&lt;/code&gt; non implémenté&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Les deux premières lignes se corrigent d’une commande dans le script Renode,
&lt;code&gt;sysbus.nvic Frequency 16000000&lt;/code&gt; pour &lt;code&gt;gpio-pac&lt;/code&gt; et &lt;code&gt;sysbus.nvic Frequency 6000000&lt;/code&gt; pour
&lt;code&gt;gpio-hal&lt;/code&gt; : la LED simulée retrouve alors le rythme prévu.&lt;/p&gt;
&lt;p&gt;Ce que ces mesures disent déjà :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;un test &lt;strong&gt;d’état&lt;/strong&gt; (« la LED est allumée après l’appui ») se valide en simulation ;&lt;/li&gt;
&lt;li&gt;un test &lt;strong&gt;de durée&lt;/strong&gt; n’a de sens que si la fréquence simulée est fixée à la main, firmware par firmware ;&lt;/li&gt;
&lt;li&gt;une &lt;strong&gt;configuration oubliée&lt;/strong&gt; (horloge de SYSCFG, routage EXTI) passe inaperçue dans Renode et ne se voit que sur la carte.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;la-suite&quot;&gt;La suite&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Brancher la compilation et les scripts Renode dans GitHub Actions.&lt;/li&gt;
&lt;li&gt;Écrire les vérifications sous forme de tests automatiques plutôt que de commandes tapées dans la console.&lt;/li&gt;
&lt;li&gt;Mesurer les mêmes durées sur la carte, à l’analyseur logique, pour remplacer la colonne « attendu » par des valeurs relevées.&lt;/li&gt;
&lt;li&gt;Ajouter des tests avec le matériel dans la boucle pour ce que la simulation ne voit pas.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Les trois premiers firmwares sont expliqués pas à pas dans l’exploration
&lt;a href=&quot;https://nostd.fr/explorations/stm32-rust/&quot;&gt;Rust embarqué sur STM32 : des registres au HAL&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="fr">
        <title>EXTI avec le PAC</title>
        <published>2026-11-19T00:00:00+00:00</published>
        <updated>2026-11-19T00:00:00+00:00</updated>
        <author>
          <name>nostd</name>
        </author>
        <link rel="alternate" type="text/html" href="https://nostd.fr/articles/rust-stm32f407/03-exti-pac/"/>
        <id>https://nostd.fr/articles/rust-stm32f407/03-exti-pac/</id>
        <summary type="text">Déclencher une interruption en appuyant sur le bouton de la carte : horloges, SYSCFG, EXTI, NVIC, puis le gestionnaire.</summary>
        <content type="html" xml:base="https://nostd.fr/articles/rust-stm32f407/03-exti-pac/">&lt;h2 id=&quot;objectif&quot;&gt;Objectif&lt;/h2&gt;
&lt;p&gt;Un firmware qui ne fait rien tant qu’on ne le dérange pas. Un appui sur le bouton
utilisateur B1 déclenche l’interruption &lt;code&gt;EXTI0&lt;/code&gt;, et le gestionnaire allume la LED verte.
La boucle principale, elle, reste vide.&lt;/p&gt;
&lt;p&gt;Sur la STM32F407G-DISC1, B1 est relié à &lt;strong&gt;PA0&lt;/strong&gt; et la LED verte à &lt;strong&gt;PD12&lt;/strong&gt; (paquet de
support de carte de ST : &lt;code&gt;KEY_BUTTON_PIN&lt;/code&gt; = &lt;code&gt;GPIO_PIN_0&lt;/code&gt; sur &lt;code&gt;GPIOA&lt;/code&gt;, interruption
&lt;code&gt;EXTI0_IRQn&lt;/code&gt;). ST configure ce bouton sans résistance interne et sur front montant :
la broche est au repos à 0 et passe à 1 quand on appuie.&lt;/p&gt;
&lt;h2 id=&quot;le-chemin-d-une-interruption-externe&quot;&gt;Le chemin d’une interruption externe&lt;/h2&gt;
&lt;p&gt;Entre la broche et le code, le signal traverse quatre blocs. Chacun doit être configuré,
et un seul oubli suffit pour que rien ne se passe.&lt;/p&gt;
&lt;figure class=&quot;figure&quot;&gt;
&lt;div class=&quot;figure-media&quot; role=&quot;img&quot; aria-label=&quot;Le signal part de la broche PA0, traverse SYSCFG qui choisit le port, puis la ligne EXTI 0 qui détecte le front et applique le masque, puis le NVIC qui autorise l&amp;#39;interruption numéro 6, et arrive à la fonction EXTI0. Le port GPIO dépend de l&amp;#39;horloge AHB1, SYSCFG de l&amp;#39;horloge APB2.&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 760 132&quot; width=&quot;760&quot; height=&quot;132&quot;&gt;   &lt;g class=&quot;fig-fill fig-line&quot;&gt;     &lt;rect x=&quot;4&quot; y=&quot;34&quot; width=&quot;132&quot; height=&quot;60&quot;/&gt;     &lt;rect x=&quot;156&quot; y=&quot;34&quot; width=&quot;132&quot; height=&quot;60&quot;/&gt;     &lt;rect x=&quot;308&quot; y=&quot;34&quot; width=&quot;132&quot; height=&quot;60&quot;/&gt;     &lt;rect x=&quot;460&quot; y=&quot;34&quot; width=&quot;132&quot; height=&quot;60&quot;/&gt;     &lt;rect x=&quot;612&quot; y=&quot;34&quot; width=&quot;144&quot; height=&quot;60&quot;/&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-accent&quot;&gt;     &lt;path d=&quot;M136 64h18M148 59l6 5-6 5&quot;/&gt;     &lt;path d=&quot;M288 64h18M300 59l6 5-6 5&quot;/&gt;     &lt;path d=&quot;M440 64h18M452 59l6 5-6 5&quot;/&gt;     &lt;path d=&quot;M592 64h18M604 59l6 5-6 5&quot;/&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-text&quot; text-anchor=&quot;middle&quot;&gt;     &lt;text x=&quot;70&quot; y=&quot;60&quot;&gt;PA0&lt;/text&gt;     &lt;text x=&quot;222&quot; y=&quot;60&quot;&gt;SYSCFG&lt;/text&gt;     &lt;text x=&quot;374&quot; y=&quot;60&quot;&gt;EXTI ligne 0&lt;/text&gt;     &lt;text x=&quot;526&quot; y=&quot;60&quot;&gt;NVIC&lt;/text&gt;     &lt;text x=&quot;684&quot; y=&quot;60&quot;&gt;fn EXTI0()&lt;/text&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-dim&quot; text-anchor=&quot;middle&quot;&gt;     &lt;text x=&quot;70&quot; y=&quot;80&quot;&gt;bouton B1&lt;/text&gt;     &lt;text x=&quot;222&quot; y=&quot;80&quot;&gt;EXTICR1&lt;/text&gt;     &lt;text x=&quot;374&quot; y=&quot;80&quot;&gt;RTSR, IMR, PR&lt;/text&gt;     &lt;text x=&quot;526&quot; y=&quot;80&quot;&gt;IRQ 6&lt;/text&gt;     &lt;text x=&quot;684&quot; y=&quot;80&quot;&gt;gestionnaire&lt;/text&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-dim&quot; text-anchor=&quot;middle&quot;&gt;     &lt;text x=&quot;70&quot; y=&quot;20&quot;&gt;la broche&lt;/text&gt;     &lt;text x=&quot;222&quot; y=&quot;20&quot;&gt;choisit le port&lt;/text&gt;     &lt;text x=&quot;374&quot; y=&quot;20&quot;&gt;front et masque&lt;/text&gt;     &lt;text x=&quot;526&quot; y=&quot;20&quot;&gt;autorise&lt;/text&gt;     &lt;text x=&quot;684&quot; y=&quot;20&quot;&gt;ton code&lt;/text&gt;   &lt;/g&gt;   &lt;g class=&quot;fig-dim&quot; text-anchor=&quot;middle&quot;&gt;     &lt;text x=&quot;222&quot; y=&quot;118&quot;&gt;horloge : APB2&lt;/text&gt;     &lt;text x=&quot;70&quot; y=&quot;118&quot;&gt;horloge : AHB1&lt;/text&gt;   &lt;/g&gt; &lt;/svg&gt;&lt;/div&gt;
&lt;figcaption&gt;De la broche au gestionnaire. Chaque bloc a son registre.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;SYSCFG&lt;/strong&gt; choisit quel port alimente la ligne. La ligne EXTI 0 peut venir de PA0, PB0,
PC0… mais d’un seul à la fois.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;EXTI&lt;/strong&gt; détecte le front, applique le masque et mémorise la demande dans un drapeau.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;NVIC&lt;/strong&gt;, dans le cœur Cortex-M4, autorise ou non l’interruption à interrompre le programme.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;etape-1-les-registres&quot;&gt;Étape 1 : les registres&lt;/h2&gt;
&lt;p&gt;Les adresses viennent de la carte mémoire du STM32F407 (RM0090, en-tête CMSIS
&lt;code&gt;stm32f407xx.h&lt;/code&gt;) : &lt;code&gt;SYSCFG&lt;/code&gt; commence à &lt;code&gt;0x4001 3800&lt;/code&gt;, &lt;code&gt;EXTI&lt;/code&gt; à &lt;code&gt;0x4001 3C00&lt;/code&gt;.&lt;/p&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
&lt;figcaption&gt;&lt;span class=&quot;bitfield-name&quot;&gt;RCC_APB2ENR&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;adresse &lt;code&gt;0x4002 3844&lt;/code&gt;&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;source : RM0090, registre RCC_APB2ENR ; CMSIS RCC_APB2ENR_SYSCFGEN&lt;/span&gt;&lt;/figcaption&gt;
&lt;div class=&quot;table-scroll&quot; role=&quot;region&quot; aria-label=&quot;Champs du registre RCC_APB2ENR&quot; tabindex=&quot;0&quot;&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Bits&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Nom&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Accès&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;31:15&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;autres&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Horloges d’autres périphériques du bus APB2&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;14&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;SYSCFGEN&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Horloge de SYSCFG : &lt;code&gt;1&lt;/code&gt; = activée&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;13:0&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;autres&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Horloges d’autres périphériques du bus APB2&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/figure&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
&lt;figcaption&gt;&lt;span class=&quot;bitfield-name&quot;&gt;SYSCFG_EXTICR1&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;adresse &lt;code&gt;0x4001 3808&lt;/code&gt;&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;source : RM0090, registre SYSCFG_EXTICR1 ; CMSIS SYSCFG_EXTICR1_EXTI0&lt;/span&gt;&lt;/figcaption&gt;
&lt;div class=&quot;table-scroll&quot; role=&quot;region&quot; aria-label=&quot;Champs du registre SYSCFG_EXTICR1&quot; tabindex=&quot;0&quot;&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Bits&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Nom&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Accès&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;15:4&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;EXTI3 à EXTI1&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Port source des lignes 3 à 1, quatre bits par ligne&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;3:0&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;EXTI0&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Port source de la ligne 0 : &lt;code&gt;0000&lt;/code&gt; = port A, &lt;code&gt;0001&lt;/code&gt; = port B, &lt;code&gt;0010&lt;/code&gt; = port C…&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/figure&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
&lt;figcaption&gt;&lt;span class=&quot;bitfield-name&quot;&gt;EXTI_RTSR&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;adresse &lt;code&gt;0x4001 3C08&lt;/code&gt;&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;source : RM0090, registre EXTI_RTSR ; CMSIS EXTI_RTSR_TR0&lt;/span&gt;&lt;/figcaption&gt;
&lt;div class=&quot;table-scroll&quot; role=&quot;region&quot; aria-label=&quot;Champs du registre EXTI_RTSR&quot; tabindex=&quot;0&quot;&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Bits&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Nom&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Accès&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;0&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;TR0&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;&lt;code&gt;1&lt;/code&gt; = la ligne 0 réagit au front montant&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/figure&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
&lt;figcaption&gt;&lt;span class=&quot;bitfield-name&quot;&gt;EXTI_IMR&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;adresse &lt;code&gt;0x4001 3C00&lt;/code&gt;&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;source : RM0090, registre EXTI_IMR ; CMSIS EXTI_IMR_MR0&lt;/span&gt;&lt;/figcaption&gt;
&lt;div class=&quot;table-scroll&quot; role=&quot;region&quot; aria-label=&quot;Champs du registre EXTI_IMR&quot; tabindex=&quot;0&quot;&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Bits&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Nom&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Accès&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;0&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;MR0&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;&lt;code&gt;1&lt;/code&gt; = la demande d’interruption de la ligne 0 n’est pas masquée&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/figure&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
&lt;figcaption&gt;&lt;span class=&quot;bitfield-name&quot;&gt;EXTI_PR&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;adresse &lt;code&gt;0x4001 3C14&lt;/code&gt;&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;source : RM0090, registre EXTI_PR ; CMSIS EXTI_PR_PR0&lt;/span&gt;&lt;/figcaption&gt;
&lt;div class=&quot;table-scroll&quot; role=&quot;region&quot; aria-label=&quot;Champs du registre EXTI_PR&quot; tabindex=&quot;0&quot;&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Bits&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Nom&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Accès&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;0&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;PR0&lt;/td&gt;&lt;td&gt;rc_w1&lt;/td&gt;&lt;td&gt;&lt;code&gt;1&lt;/code&gt; = une demande est en attente sur la ligne 0. Se remet à &lt;code&gt;0&lt;/code&gt; en y &lt;strong&gt;écrivant &lt;code&gt;1&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/figure&gt;
&lt;h2 id=&quot;etape-2-partager-les-peripheriques-avec-le-gestionnaire&quot;&gt;Étape 2 : partager les périphériques avec le gestionnaire&lt;/h2&gt;
&lt;p&gt;Le gestionnaire d’interruption est une fonction sans paramètre : on ne peut pas lui passer
&lt;code&gt;dp&lt;/code&gt;. Il faut une variable globale, et Rust n’en accepte une modifiable que si son accès
est protégé.&lt;/p&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;apps/02-exti-pac/src/main.rs (début)&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;rust&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-10&quot;&gt;#![no_std]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-10&quot;&gt;#![no_main]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; core&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;RefCell&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; cortex_m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;interrupt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Mutex&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; cortex_m_rt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;entry&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; pac&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;{interrupt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; Peripherals&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span&gt; panic_halt &lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;as&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; stm32f4&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;stm32f407 &lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;as&lt;/span&gt;&lt;span&gt; pac&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;// Peripherals shared between main and the interrupt handler.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;// Périphériques partagés entre main et le handler d&amp;#39;interruption.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; G_PER&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; Mutex&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;RefCell&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Option&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Peripherals&lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; Mutex&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;RefCell&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;None&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;Le type se lit de l’intérieur vers l’extérieur :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Option&amp;lt;Peripherals&amp;gt;&lt;/code&gt; : la variable est vide au démarrage, remplie par &lt;code&gt;main&lt;/code&gt; ;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;RefCell&amp;lt;…&amp;gt;&lt;/code&gt; : permet de modifier le contenu à travers une référence partagée, avec une vérification à l’exécution ;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Mutex&amp;lt;…&amp;gt;&lt;/code&gt; : celui de &lt;code&gt;cortex-m&lt;/code&gt;. Il ne donne accès au contenu que dans une &lt;strong&gt;section critique&lt;/strong&gt;, interruptions masquées.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;etape-3-la-configuration&quot;&gt;Étape 3 : la configuration&lt;/h2&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;apps/02-exti-pac/src/main.rs (main)&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;rust&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-10&quot;&gt;#[entry]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; main&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; -&amp;gt; !&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; pac&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Peripherals&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;take&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;Périphériques STM32 déjà récupérés&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 1. Enable GPIOA, GPIOD and SYSCFG clocks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    //    Activer les horloges GPIOA, GPIOD et SYSCFG&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 7&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;RCC&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;ahb1enr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;gpioaen&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;gpioden&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 8&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;RCC&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;apb2enr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;syscfgen&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 9&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 2. Configure PA0 as input (0b00) and PD12 as output&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    //    Configurer PA0 en entrée (0b00) et PD12 en sortie&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;12&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;GPIOA&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;moder&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;moder0&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;input&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;13&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;GPIOD&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;moder&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;moder12&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;output&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;14&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 3. SYSCFG routing: 0 selects Port A for EXTI line 0 (PA0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    //    Routage dans le SYSCFG : 0 pour le Port A (PA0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;17&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;SYSCFG&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;exticr1&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt; unsafe&lt;/span&gt;&lt;span&gt; { w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;exti0&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;bits&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;0&lt;/span&gt;&lt;span&gt;) })&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;18&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 4. EXTI configuration: rising edge + unmask the line (IMR)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    //    Configuration de l&amp;#39;EXTI : front montant + démasquage (IMR)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;21&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;EXTI&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;rtsr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;tr0&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;22&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;EXTI&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;imr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;mr0&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;23&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 5. Share the peripherals with the handler BEFORE enabling the IRQ&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    //    Partager les périphériques avec le handler AVANT d&amp;#39;activer l&amp;#39;IRQ&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;    cortex_m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;interrupt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;cs&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;        G_PER&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;borrow&lt;/span&gt;&lt;span&gt;(cs)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;replace&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(dp))&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;28&lt;/span&gt;&lt;span&gt;    })&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;29&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 6. Enable the interrupt in the NVIC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    //    Activer l&amp;#39;interruption dans le NVIC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    unsafe&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;        cortex_m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;peripheral&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;NVIC&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;unmask&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;pac&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Interrupt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;EXTI0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;34&lt;/span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;35&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;37&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;        // Wait for interrupt (saves power) - currently disabled&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;38&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;        // Attente d&amp;#39;interruption (économise l&amp;#39;énergie) - actuellement désactivé&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;39&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;        //cortex_m::asm::wfi();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;40&lt;/span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;41&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;Les lignes surlignées sont celles qu’on oublie ou qu’on place mal.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ligne 8, l’horloge de SYSCFG.&lt;/strong&gt; Elle est sur le bus APB2, pas sur AHB1 comme les ports.
Sans elle, l’écriture de la ligne 17 est perdue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ligne 17, le routage.&lt;/strong&gt; &lt;code&gt;0&lt;/code&gt; est la valeur de reset : sur cette carte, la ligne passerait
même sans cette écriture. La garder rend le programme correct quel que soit l’état laissé
par un chargeur d’amorçage, et elle devient indispensable dès que le bouton est sur un
autre port.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lignes 26 à 28, puis le NVIC.&lt;/strong&gt; L’ordre compte : on dépose &lt;code&gt;dp&lt;/code&gt; dans &lt;code&gt;G_PER&lt;/code&gt; &lt;strong&gt;avant&lt;/strong&gt;
d’autoriser l’interruption. Dans l’autre sens, un appui au mauvais moment lancerait le
gestionnaire sur une variable encore vide.&lt;/p&gt;
&lt;aside class=&quot;callout callout-danger&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Danger&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;Deux blocs unsafe, deux raisons différentes&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;w.exti0().bits(0)&lt;/code&gt; est &lt;code&gt;unsafe&lt;/code&gt; parce que le PAC ne connaît pas les valeurs autorisées de
ce champ : c’est au développeur de garantir, reference manual à l’appui, que &lt;code&gt;0&lt;/code&gt; en est une.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;NVIC::unmask&lt;/code&gt; est &lt;code&gt;unsafe&lt;/code&gt; parce qu’autoriser une interruption peut casser une section
critique en cours. Ici il n’y en a pas : l’appel est fait une fois, à la fin de
l’initialisation.&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;etape-4-le-gestionnaire&quot;&gt;Étape 4 : le gestionnaire&lt;/h2&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;apps/02-exti-pac/src/main.rs (fin)&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;rust&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-10&quot;&gt;#[interrupt]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; EXTI0&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-9&quot;&gt;    cortex_m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;interrupt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;free&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;cs&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;        // Get access to the global peripherals&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;        // Obtenir l&amp;#39;accès aux périphériques globaux&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;        let mut&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; G_PER&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;borrow&lt;/span&gt;&lt;span&gt;(cs)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;borrow_mut&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-2&quot;&gt;        if&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;as_mut&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;EXTI&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;pr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;read&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;pr0&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;bit&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;            // Clear the button interrupt flag (write 1 to clear)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;            // Effacer le flag d&amp;#39;interruption du bouton (écrire 1 pour effacer)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;as_mut&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;EXTI&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;pr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;pr0&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;            // Turn the LED on (PD12)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;            // Allumer la LED (PD12)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;as_mut&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-3&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-3&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;GPIOD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-3&quot;&gt;                .&lt;/span&gt;&lt;span&gt;bsrr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-3&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;bs12&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    })&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;L’attribut &lt;code&gt;#[interrupt]&lt;/code&gt; et le nom &lt;code&gt;EXTI0&lt;/code&gt; suffisent : &lt;code&gt;cortex-m-rt&lt;/code&gt; place l’adresse de la
fonction dans la table des vecteurs, à l’entrée de l’interruption numéro 6.&lt;/p&gt;
&lt;p&gt;Le gestionnaire fait trois choses : il vérifie que le drapeau &lt;code&gt;PR0&lt;/code&gt; est bien levé, il
l’efface, puis il allume la LED. &lt;strong&gt;Effacer le drapeau est obligatoire.&lt;/strong&gt; Tant qu’il vaut 1,
la demande reste active, et le gestionnaire est rappelé dès qu’il se termine.&lt;/p&gt;
&lt;aside class=&quot;callout callout-attention&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Attention&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;modify sur un registre « écrire 1 pour effacer »&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXTI_PR&lt;/code&gt; s’efface en y écrivant &lt;code&gt;1&lt;/code&gt;. Or &lt;code&gt;modify&lt;/code&gt; relit d’abord le registre : tous les
drapeaux levés sont relus à &lt;code&gt;1&lt;/code&gt;, puis réécrits à &lt;code&gt;1&lt;/code&gt;, donc &lt;strong&gt;tous&lt;/strong&gt; effacés, pas seulement
&lt;code&gt;PR0&lt;/code&gt;. Dans ce programme une seule ligne est active et le résultat est le même. Dès que plusieurs
lignes sont utilisées, la bonne écriture est &lt;code&gt;dp.EXTI.pr.write(|w| w.pr0().set_bit())&lt;/code&gt; : &lt;code&gt;write&lt;/code&gt; part de
zéro et n’efface que le drapeau nommé.&lt;/p&gt;
&lt;p&gt;Le HAL &lt;code&gt;stm32f4xx-hal&lt;/code&gt; a fait la même correction dans sa version 0.23 (« Use &lt;code&gt;write&lt;/code&gt;
instead of &lt;code&gt;modify&lt;/code&gt; to clear flags »).&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class=&quot;callout callout-note&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;La boucle vide et wfi&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;cortex_m::asm::wfi()&lt;/code&gt; endort le cœur jusqu’à la prochaine interruption. L’appel est en
commentaire dans le dépôt : la boucle tourne donc à vide. Le programme fonctionne dans les
deux cas ; seule la consommation change.&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;etape-5-essayer&quot;&gt;Étape 5 : essayer&lt;/h2&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Depuis la racine du dépôt&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Depuis la racine du dépôt&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo build --release -p exti-pac&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo size --release -p exti-pac&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;   text	   data	    bss	    dec	    hex	filename&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;    864	      0	      8	    872	    368	exti-pac&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo run --release -p exti-pac&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;Vérification sur la carte.&lt;/strong&gt; Au démarrage, la LED verte est éteinte. Un appui sur le
bouton utilisateur B1, le bleu, l’allume, et elle reste allumée. Le bouton noir, lui, redémarre la carte.&lt;/p&gt;
&lt;p&gt;Dans Renode, après &lt;code&gt;renode renode/scripts/exti-pac.resc&lt;/code&gt; :&lt;/p&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Console Renode&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Console Renode&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;(stm32f407-exti-pac) &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;sysbus.gpioPortD.UserLED State&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;(stm32f407-exti-pac) &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;sysbus.gpioPortA.UserButton PressAndRelease&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;(stm32f407-exti-pac) &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;sysbus.gpioPortD.UserLED State&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;Vérification dans Renode.&lt;/strong&gt; &lt;code&gt;False&lt;/code&gt; avant l’appui, &lt;code&gt;True&lt;/code&gt; après.&lt;/p&gt;
&lt;h2 id=&quot;ce-que-la-simulation-ne-voit-pas&quot;&gt;Ce que la simulation ne voit pas&lt;/h2&gt;
&lt;p&gt;Deux écarts avec la carte, relevés avec Renode 1.17.0.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Le bouton n’atteint pas l’EXTI sans retouche.&lt;/strong&gt; La description &lt;code&gt;stm32f4.repl&lt;/code&gt; de Renode
relie d’abord les seize broches du port A à l’EXTI, puis un bloc plus bas redéfinit la
broche 0 pour deux temporisateurs, ce qui remplace la première liaison. Avec la carte
simulée seule, la LED reste à &lt;code&gt;False&lt;/code&gt; après l’appui. Le dépôt rétablit la liaison dans une
surcouche :&lt;/p&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;renode/platforms/exti-pac.repl&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;plain&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;gpioPortA:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    0 -&amp;gt; exti@0 | timer2@00 | timer5@00&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;gpioPortD:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    12 -&amp;gt; UserLED@0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;SYSCFG n’est pas simulé.&lt;/strong&gt; À l’écriture de la ligne 17, Renode prévient :
&lt;code&gt;WriteDoubleWord of value 0x0 to an unimplemented register SYSCFG:EXTICR1 (0x40013808)&lt;/code&gt;.
Le routage est fait par le fichier &lt;code&gt;.repl&lt;/code&gt;, pas par le firmware. Avec l’horloge de
SYSCFG oubliée ou un mauvais port, la simulation passe quand même, la carte non. Ce test-là
ne se valide que sur le matériel.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="fr">
        <title>GPIO avec le HAL</title>
        <published>2026-11-19T00:00:00+00:00</published>
        <updated>2026-11-19T00:00:00+00:00</updated>
        <author>
          <name>nostd</name>
        </author>
        <link rel="alternate" type="text/html" href="https://nostd.fr/articles/rust-stm32f407/02-gpio-hal/"/>
        <id>https://nostd.fr/articles/rust-stm32f407/02-gpio-hal/</id>
        <summary type="text">La même LED, sans écrire dans un seul registre : horloges, broches typées et délai avec stm32f4xx-hal.</summary>
        <content type="html" xml:base="https://nostd.fr/articles/rust-stm32f407/02-gpio-hal/">&lt;h2 id=&quot;objectif&quot;&gt;Objectif&lt;/h2&gt;
&lt;p&gt;La LED verte de la &lt;a href=&quot;https://nostd.fr/articles/rust-stm32f407/01-gpio-pac/&quot;&gt;partie précédente&lt;/a&gt;, qui
change d’état chaque seconde. Cette fois avec le &lt;strong&gt;HAL&lt;/strong&gt; (&lt;em&gt;Hardware Abstraction Layer&lt;/em&gt;) :
le crate &lt;code&gt;stm32f4xx-hal&lt;/code&gt;, construit au-dessus du PAC. On ne nomme plus aucun registre, et
on en profite pour faire tourner la puce à 48 MHz.&lt;/p&gt;
&lt;h2 id=&quot;etape-1-les-dependances&quot;&gt;Étape 1 : les dépendances&lt;/h2&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;apps/01-gpio-hal/Cargo.toml&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;toml&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;package&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;gpio-hal&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.1.0&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;edition&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;2021&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;publish&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;dependencies&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;embedded-hal&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;nb&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;1&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;cortex-m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.7&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;cortex-m-rt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.7&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-1&quot;&gt;# Panic behaviour, see https://crates.io/keywords/panic-impl for alternatives&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;panic-halt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;dependencies&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;stm32f4xx-hal&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.23.0&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;stm32f407&amp;quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;Le PAC n’apparaît plus dans la liste : le HAL le réexporte sous le nom &lt;code&gt;pac&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;etape-2-le-programme&quot;&gt;Étape 2 : le programme&lt;/h2&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;apps/01-gpio-hal/src/main.rs&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;rust&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-10&quot;&gt;#![no_std]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-10&quot;&gt;#![no_main]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; cortex_m_rt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;entry&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span&gt; panic_halt &lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;as&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; stm32f4xx_hal&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 7&lt;/span&gt;&lt;span&gt;    pac&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;    prelude&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::*,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;    rcc&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Config&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;10&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;11&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;z-10&quot;&gt;#[entry]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; main&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; -&amp;gt; !&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;14&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; pac&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Peripherals&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;take&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;Périphériques STM32 déjà récupérés&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let&lt;/span&gt;&lt;span&gt; cp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; cortex_m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;peripheral&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Peripherals&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;take&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;Périphériques Cortex-M déjà récupérés&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;19&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let mut&lt;/span&gt;&lt;span&gt; rcc&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;RCC&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;freeze&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Config&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;hsi&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;sysclk&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;48.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;MHz&lt;/span&gt;&lt;span&gt;()))&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;21&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let&lt;/span&gt;&lt;span&gt; gpiod&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;GPIOD&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;split&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;mut&lt;/span&gt;&lt;span&gt; rcc)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let mut&lt;/span&gt;&lt;span&gt; led&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; gpiod&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;pd12&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;into_push_pull_output&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;24&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let mut&lt;/span&gt;&lt;span&gt; delay&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; cp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;SYST&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;delay&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;rcc&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;clocks)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;26&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;28&lt;/span&gt;&lt;span&gt;        led&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;toggle&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;29&lt;/span&gt;&lt;span&gt;        delay&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;delay_ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;1000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;30&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;31&lt;/span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;32&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;Quatre lignes font le travail, et chacune remplace un passage de la partie précédente.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ligne 20, les horloges.&lt;/strong&gt; &lt;code&gt;Config::hsi().sysclk(48.MHz())&lt;/code&gt; demande une horloge système de
48 MHz à partir de l’oscillateur interne. &lt;code&gt;freeze&lt;/code&gt; démarre la PLL, règle la mémoire flash
pour cette fréquence, bascule l’horloge, et renvoie un objet &lt;code&gt;rcc&lt;/code&gt; qui &lt;strong&gt;connaît&lt;/strong&gt; les
fréquences obtenues. Plus de &lt;code&gt;16_000_000&lt;/code&gt; écrit à la main.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ligne 22, le port.&lt;/strong&gt; &lt;code&gt;split&lt;/code&gt; active l’horloge de GPIOD (le bit &lt;code&gt;GPIODEN&lt;/code&gt; de la partie 1)
et découpe le port en seize broches indépendantes, &lt;code&gt;pd0&lt;/code&gt; à &lt;code&gt;pd15&lt;/code&gt;. Il lui faut &lt;code&gt;&amp;amp;mut rcc&lt;/code&gt;
pour toucher au registre d’horloge.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ligne 23, la broche.&lt;/strong&gt; &lt;code&gt;into_push_pull_output()&lt;/code&gt; écrit &lt;code&gt;MODER12&lt;/code&gt; et, surtout, &lt;strong&gt;change le
type&lt;/strong&gt; de la broche. &lt;code&gt;gpiod.pd12&lt;/code&gt; était une entrée ; &lt;code&gt;led&lt;/code&gt; est une sortie. Les méthodes
&lt;code&gt;toggle&lt;/code&gt;, &lt;code&gt;set_high&lt;/code&gt; et &lt;code&gt;set_low&lt;/code&gt; n’existent que sur ce second type.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ligne 25, le délai.&lt;/strong&gt; Le délai reçoit les fréquences de &lt;code&gt;rcc&lt;/code&gt; et calcule lui-même son
rechargement.&lt;/p&gt;
&lt;aside class=&quot;callout callout-note&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;Le type de la broche est sa configuration&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Appeler &lt;code&gt;toggle()&lt;/code&gt; sur une broche restée en entrée ne compile pas : la méthode n’existe pas
pour ce type. Et comme &lt;code&gt;into_push_pull_output()&lt;/code&gt; consomme la broche, on ne peut pas la
reconfigurer ailleurs par mégarde. L’état du matériel est écrit dans les types, et le
compilateur en assure le suivi.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class=&quot;callout callout-attention&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Attention&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;Cette écriture est celle de la version 0.23&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Depuis &lt;code&gt;stm32f4xx-hal&lt;/code&gt; 0.23, les fonctions qui activent un périphérique reçoivent
&lt;code&gt;&amp;amp;mut rcc&lt;/code&gt; (« Use &lt;code&gt;&amp;amp;mut RCC&lt;/code&gt; for &lt;code&gt;PER::enable/reset&lt;/code&gt; » dans le journal des modifications).
Un exemple plus ancien trouvé en ligne ne compilera donc pas tel quel avec cette version :
la version du HAL est donc à contrôler avant de reprendre un exemple.&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;etape-3-ce-que-l-abstraction-coute&quot;&gt;Étape 3 : ce que l’abstraction coûte&lt;/h2&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Depuis la racine du dépôt&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Depuis la racine du dépôt&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo build --release -p gpio-hal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo size --release -p gpio-hal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;   text	   data	    bss	    dec	    hex	filename&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;    852	      0	      4	    856	    358	gpio-hal&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo run --release -p gpio-hal&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Firmware&lt;/th&gt;&lt;th&gt;&lt;code&gt;text&lt;/code&gt;&lt;/th&gt;&lt;th&gt;&lt;code&gt;data&lt;/code&gt;&lt;/th&gt;&lt;th&gt;&lt;code&gt;bss&lt;/code&gt;&lt;/th&gt;&lt;th&gt;Ce qu’il fait en plus&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;gpio-pac&lt;/code&gt;&lt;/td&gt;&lt;td&gt;796&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;rien : horloge par défaut à 16 MHz&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;gpio-hal&lt;/code&gt;&lt;/td&gt;&lt;td&gt;852&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;configure la PLL et passe à 48 MHz&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;56 octets de plus, et ils servent à configurer la PLL, ce que la version PAC ne fait pas.
Les types de broches, eux, ne coûtent rien : ils disparaissent à la compilation
(profil &lt;code&gt;release&lt;/code&gt; du dépôt : &lt;code&gt;opt-level = &quot;s&quot;&lt;/code&gt;, LTO activé).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Vérification sur la carte.&lt;/strong&gt; La LED verte LD4 change d’état chaque seconde : une seconde
allumée, une seconde éteinte.&lt;/p&gt;
&lt;h2 id=&quot;etape-4-dans-renode&quot;&gt;Étape 4 : dans Renode&lt;/h2&gt;
&lt;p&gt;La simulation se lance avec &lt;code&gt;renode renode/scripts/gpio-hal.resc&lt;/code&gt; depuis la racine du dépôt.
Au démarrage, Renode signale ce que le HAL a écrit pour la mémoire flash :&lt;/p&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Journal de Renode&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Journal de Renode&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-out&quot;&gt;[WARNING] flash_controller: Unhandled write to offset 0x0. Unhandled bits: [8-10] when writing value 0x701. Tags: PRFTEN (0x1), ICEN (0x1), DCEN (0x1).&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;(stm32f407-gpio-hal) &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;sysbus.gpioPortD.UserLED State&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;&lt;code&gt;0x701&lt;/code&gt; : un cycle d’attente, la lecture anticipée et les deux caches. C’est le réglage de
la flash que &lt;code&gt;freeze&lt;/code&gt; a choisi pour 48 MHz, sans qu’il ait fallu le chercher.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Vérification dans Renode.&lt;/strong&gt; La LED simulée change d’état, mais là encore pas au bon rythme.&lt;/p&gt;
&lt;aside class=&quot;callout callout-attention&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Attention&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;Dans Renode, la LED clignote 12 fois trop vite&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Mesuré avec Renode 1.17.0 en échantillonnant la LED toutes les 20 ms de temps simulé :
elle change d’état toutes les 83 ms environ, au lieu d’une seconde.&lt;/p&gt;
&lt;p&gt;Le délai du HAL cadence le SysTick sur l’horloge du cœur divisée par 8
(&lt;code&gt;Timer::syst_external&lt;/code&gt; dans le code du HAL) : 6 MHz ici, donc un rechargement de
6 000 000 pour une seconde. Renode, lui, fait toujours tourner son SysTick à 72 MHz :
6 000 000 de tops y durent 83 ms.&lt;/p&gt;
&lt;p&gt;Pour retrouver le rythme de la carte, il suffit de régler la fréquence simulée :
&lt;code&gt;sysbus.nvic Frequency 6000000&lt;/code&gt;.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;La valeur à donner dépend donc du firmware : 16 MHz pour la partie 1, 6 MHz ici. La
simulation ne déduit pas la fréquence de la configuration d’horloge que le firmware vient
d’écrire.&lt;/p&gt;
&lt;h2 id=&quot;pac-ou-hal&quot;&gt;PAC ou HAL ?&lt;/h2&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;PAC&lt;/th&gt;&lt;th&gt;HAL&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Ce qu’on écrit&lt;/td&gt;&lt;td&gt;des registres et des champs&lt;/td&gt;&lt;td&gt;des intentions : sortie, délai, fréquence&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Ce qu’il faut ouvrir&lt;/td&gt;&lt;td&gt;le reference manual&lt;/td&gt;&lt;td&gt;la documentation du crate&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Erreurs détectées à la compilation&lt;/td&gt;&lt;td&gt;registre en lecture ou écriture seule, champ inconnu&lt;/td&gt;&lt;td&gt;en plus : broche mal configurée, périphérique sans horloge&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Portabilité&lt;/td&gt;&lt;td&gt;une famille de puces&lt;/td&gt;&lt;td&gt;les traits &lt;code&gt;embedded-hal&lt;/code&gt;, communs à d’autres puces&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Le HAL ne remplace pas le PAC : il s’en sert. Quand il ne couvre pas un cas, on redescend
d’un étage, comme dans la &lt;a href=&quot;https://nostd.fr/articles/rust-stm32f407/03-exti-pac/&quot;&gt;partie suivante&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    <entry xml:lang="fr">
        <title>GPIO avec le PAC</title>
        <published>2026-11-19T00:00:00+00:00</published>
        <updated>2026-11-19T00:00:00+00:00</updated>
        <author>
          <name>nostd</name>
        </author>
        <link rel="alternate" type="text/html" href="https://nostd.fr/articles/rust-stm32f407/01-gpio-pac/"/>
        <id>https://nostd.fr/articles/rust-stm32f407/01-gpio-pac/</id>
        <summary type="text">Faire clignoter la LED verte de la Discovery en écrivant dans trois registres, à travers le crate stm32f4.</summary>
        <content type="html" xml:base="https://nostd.fr/articles/rust-stm32f407/01-gpio-pac/">&lt;h2 id=&quot;objectif&quot;&gt;Objectif&lt;/h2&gt;
&lt;p&gt;Un firmware qui fait clignoter la LED verte LD4 de la STM32F407G-DISC1 : deux secondes
allumée, deux secondes éteinte. Sur cette carte, LD4 est reliée à la broche &lt;strong&gt;PD12&lt;/strong&gt;
(c’est ce que définit le paquet de support de carte de ST : &lt;code&gt;LED4_PIN&lt;/code&gt; = &lt;code&gt;GPIO_PIN_12&lt;/code&gt;,
&lt;code&gt;LED4_GPIO_PORT&lt;/code&gt; = &lt;code&gt;GPIOD&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;On passe par le &lt;strong&gt;PAC&lt;/strong&gt; (&lt;em&gt;Peripheral Access Crate&lt;/em&gt;) : le crate &lt;code&gt;stm32f4&lt;/code&gt;, généré à partir
de la description SVD de la puce. Il n’automatise rien. Il donne seulement un nom et
un type à chaque registre et à chaque champ, et le compilateur vérifie le reste.&lt;/p&gt;
&lt;h2 id=&quot;etape-1-les-dependances&quot;&gt;Étape 1 : les dépendances&lt;/h2&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;apps/01-gpio-pac/Cargo.toml&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;toml&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;package&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;gpio-pac&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.1.0&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;edition&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;2021&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;publish&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt;dependencies&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;cortex-m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.7.7&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;cortex-m-rt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.7.5&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;panic-halt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.2&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-12&quot;&gt;stm32f4&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt; version&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;0.15.1&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-12&quot;&gt; features&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;stm32f407&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt; &amp;quot;rt&amp;quot;&lt;/span&gt;&lt;span&gt;] }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cortex-m-rt&lt;/code&gt; fournit le démarrage : table des vecteurs, initialisation de la RAM, point d’entrée.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cortex-m&lt;/code&gt; donne accès au cœur, ici au compteur SysTick pour les délais.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;panic-halt&lt;/code&gt; arrête le programme en cas de panique.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;stm32f4&lt;/code&gt; est le PAC. La fonctionnalité &lt;code&gt;stm32f407&lt;/code&gt; choisit la puce, &lt;code&gt;rt&lt;/code&gt; ajoute sa table d’interruptions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;etape-2-trois-registres&quot;&gt;Étape 2 : trois registres&lt;/h2&gt;
&lt;p&gt;Un périphérique STM32 ne répond pas tant que son horloge est coupée. Il faut donc, dans
l’ordre : activer l’horloge du port D, passer PD12 en sortie, puis écrire son niveau.
Les adresses viennent de la carte mémoire du STM32F407 (RM0090, et l’en-tête CMSIS
&lt;code&gt;stm32f407xx.h&lt;/code&gt;) : &lt;code&gt;RCC&lt;/code&gt; commence à &lt;code&gt;0x4002 3800&lt;/code&gt;, &lt;code&gt;GPIOD&lt;/code&gt; à &lt;code&gt;0x4002 0C00&lt;/code&gt;.&lt;/p&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
&lt;figcaption&gt;&lt;span class=&quot;bitfield-name&quot;&gt;RCC_AHB1ENR&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;adresse &lt;code&gt;0x4002 3830&lt;/code&gt;&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;source : RM0090, registre RCC_AHB1ENR ; CMSIS RCC_AHB1ENR_GPIODEN&lt;/span&gt;&lt;/figcaption&gt;
&lt;div class=&quot;table-scroll&quot; role=&quot;region&quot; aria-label=&quot;Champs du registre RCC_AHB1ENR&quot; tabindex=&quot;0&quot;&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Bits&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Nom&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Accès&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;31:4&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;autres&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Horloges des autres périphériques du bus AHB1, inchangées ici&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;3&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;GPIODEN&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Horloge du port D : &lt;code&gt;1&lt;/code&gt; = activée (masque &lt;code&gt;0x0000 0008&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;2:0&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;GPIOCEN, GPIOBEN, GPIOAEN&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Horloges des ports C, B et A&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/figure&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
&lt;figcaption&gt;&lt;span class=&quot;bitfield-name&quot;&gt;GPIOD_MODER&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;adresse &lt;code&gt;0x4002 0C00&lt;/code&gt;&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;source : RM0090, registre GPIOx_MODER ; CMSIS GPIO_MODER_MODER12&lt;/span&gt;&lt;/figcaption&gt;
&lt;div class=&quot;table-scroll&quot; role=&quot;region&quot; aria-label=&quot;Champs du registre GPIOD_MODER&quot; tabindex=&quot;0&quot;&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Bits&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Nom&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Accès&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;31:26&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;MODER15 à MODER13&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Mode des broches 15 à 13, deux bits par broche&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;25:24&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;MODER12&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Mode de PD12 : &lt;code&gt;00&lt;/code&gt; entrée, &lt;code&gt;01&lt;/code&gt; sortie, &lt;code&gt;10&lt;/code&gt; fonction alternative, &lt;code&gt;11&lt;/code&gt; analogique&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;23:0&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;MODER11 à MODER0&lt;/td&gt;&lt;td&gt;rw&lt;/td&gt;&lt;td&gt;Mode des broches 11 à 0&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/figure&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
&lt;figcaption&gt;&lt;span class=&quot;bitfield-name&quot;&gt;GPIOD_BSRR&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;adresse &lt;code&gt;0x4002 0C18&lt;/code&gt;&lt;/span&gt; &lt;span class=&quot;bitfield-info&quot;&gt;source : RM0090, registre GPIOx_BSRR ; CMSIS GPIO_BSRR_BS12, GPIO_BSRR_BR12&lt;/span&gt;&lt;/figcaption&gt;
&lt;div class=&quot;table-scroll&quot; role=&quot;region&quot; aria-label=&quot;Champs du registre GPIOD_BSRR&quot; tabindex=&quot;0&quot;&gt;
&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;Bits&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Nom&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Accès&lt;/th&gt;&lt;th scope=&quot;col&quot;&gt;Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;31:29&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;BR15 à BR13&lt;/td&gt;&lt;td&gt;w&lt;/td&gt;&lt;td&gt;Écrire &lt;code&gt;1&lt;/code&gt; met la broche correspondante à 0&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;28&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;BR12&lt;/td&gt;&lt;td&gt;w&lt;/td&gt;&lt;td&gt;Écrire &lt;code&gt;1&lt;/code&gt; met PD12 à 0 : LED éteinte (valeur &lt;code&gt;0x1000 0000&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;27:13&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;BR11 à BR0, BS15 à BS13&lt;/td&gt;&lt;td&gt;w&lt;/td&gt;&lt;td&gt;Mêmes rôles pour les autres broches&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;12&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;BS12&lt;/td&gt;&lt;td&gt;w&lt;/td&gt;&lt;td&gt;Écrire &lt;code&gt;1&lt;/code&gt; met PD12 à 1 : LED allumée (valeur &lt;code&gt;0x0000 1000&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope=&quot;row&quot;&gt;11:0&lt;/th&gt;&lt;td class=&quot;bitfield-field&quot;&gt;BS11 à BS0&lt;/td&gt;&lt;td&gt;w&lt;/td&gt;&lt;td&gt;Écrire &lt;code&gt;1&lt;/code&gt; met la broche correspondante à 1&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/figure&gt;
&lt;h2 id=&quot;etape-3-le-programme&quot;&gt;Étape 3 : le programme&lt;/h2&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;apps/01-gpio-pac/src/main.rs&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;rust&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-10&quot;&gt;#![no_std]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-10&quot;&gt;#![no_main]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; cortex_m_rt&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;entry&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span&gt; panic_halt &lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;as&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; stm32f4&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span&gt;stm32f407&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;z-10&quot;&gt;#[entry]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt; main&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; -&amp;gt; !&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 1. Récupération des périphériques STM32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; stm32f407&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Peripherals&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;take&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;Périphériques STM32 déjà récupérés&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;13&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 2. Récupération des périphériques du cœur Cortex-M4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let&lt;/span&gt;&lt;span&gt; cp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; cortex_m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Peripherals&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;take&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;expect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-4&quot;&gt;&amp;quot;Périphériques Cortex-M déjà récupérés&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;17&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 3. Configuration du SysTick&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    let mut&lt;/span&gt;&lt;span&gt; delay&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt; cortex_m&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;Delay&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;new&lt;/span&gt;&lt;span&gt;(cp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;SYST&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt; 16_000_000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 4. Activation de l&amp;#39;horloge de GPIOD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;22&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;RCC&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;ahb1enr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;gpioden&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;23&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;    // 5. Configuration de PD12 en sortie&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l z-hl&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;25&lt;/span&gt;&lt;span&gt;    dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;GPIOD&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;moder&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;modify&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;,&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;moder12&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;output&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;26&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;z-2&quot;&gt;    loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;        // LED verte allumée&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;29&lt;/span&gt;&lt;span&gt;        dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;GPIOD&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;bsrr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;bs12&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;30&lt;/span&gt;&lt;span&gt;        delay&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;delay_ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;2000_&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;u32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;31&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;z-1&quot;&gt;        // LED verte éteinte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;33&lt;/span&gt;&lt;span&gt;        dp&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-7&quot;&gt;GPIOD&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span&gt;bsrr&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;write&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt;w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;|&lt;/span&gt;&lt;span&gt; w&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;br12&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;set_bit&lt;/span&gt;&lt;span&gt;())&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;34&lt;/span&gt;&lt;span&gt;        delay&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-8&quot;&gt;delay_ms&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-6&quot;&gt;2000_&lt;/span&gt;&lt;span class=&quot;z-9&quot;&gt;u32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-3&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;35&lt;/span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;giallo-ln&quot;&gt;36&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;Les deux lignes surlignées sont toute la configuration. Chaque écriture de l’étape 2 se
retrouve mot pour mot :&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dans le reference manual&lt;/th&gt;&lt;th&gt;Avec le PAC&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;mettre le bit GPIODEN de RCC_AHB1ENR&lt;/td&gt;&lt;td&gt;&lt;code&gt;dp.RCC.ahb1enr.modify(|_, w| w.gpioden().set_bit())&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;écrire &lt;code&gt;01&lt;/code&gt; dans MODER12&lt;/td&gt;&lt;td&gt;&lt;code&gt;dp.GPIOD.moder.modify(|_, w| w.moder12().output())&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;écrire &lt;code&gt;1&lt;/code&gt; dans BS12&lt;/td&gt;&lt;td&gt;&lt;code&gt;dp.GPIOD.bsrr.write(|w| w.bs12().set_bit())&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;écrire &lt;code&gt;1&lt;/code&gt; dans BR12&lt;/td&gt;&lt;td&gt;&lt;code&gt;dp.GPIOD.bsrr.write(|w| w.br12().set_bit())&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Trois choses à comprendre avant d’aller plus loin.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;Peripherals::take()&lt;/code&gt; ne réussit qu’une fois.&lt;/strong&gt; Il renvoie &lt;code&gt;Some&lt;/code&gt; au premier appel, &lt;code&gt;None&lt;/code&gt;
ensuite. Le programme ne peut donc pas se retrouver avec deux accès concurrents au même
registre : c’est la propriété de Rust appliquée au matériel.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;modify&lt;/code&gt; lit, modifie, réécrit.&lt;/strong&gt; La closure reçoit la valeur lue (ignorée ici, d’où le
&lt;code&gt;_&lt;/code&gt;) et un écrivain &lt;code&gt;w&lt;/code&gt; déjà rempli avec cette valeur. Seul le champ nommé change : les
autres horloges de &lt;code&gt;RCC_AHB1ENR&lt;/code&gt; et les autres broches de &lt;code&gt;GPIOD_MODER&lt;/code&gt; restent comme
elles étaient.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;write&lt;/code&gt; part de la valeur de reset.&lt;/strong&gt; C’est ce qu’il faut pour &lt;code&gt;BSRR&lt;/code&gt; : écrire &lt;code&gt;0&lt;/code&gt; dans
un de ses bits ne fait rien, seuls les &lt;code&gt;1&lt;/code&gt; agissent. Une seule écriture change donc une
seule broche, sans lecture préalable.&lt;/p&gt;
&lt;aside class=&quot;callout callout-note&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;Le compilateur connaît le sens de chaque registre&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BSRR&lt;/code&gt; est en écriture seule. Si &lt;code&gt;write&lt;/code&gt; est remplacé par &lt;code&gt;modify&lt;/code&gt; sur ce registre, la
compilation échoue : le type &lt;code&gt;BSRR_SPEC&lt;/code&gt; n’implémente pas &lt;code&gt;Readable&lt;/code&gt;. Une erreur qu’on
découvre d’habitude à l’oscilloscope est ici refusée avant le flashage.&lt;/p&gt;
&lt;/aside&gt;
&lt;aside class=&quot;callout callout-attention&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Attention&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;Le délai suppose une horloge à 16 MHz&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Delay::new(cp.SYST, 16_000_000)&lt;/code&gt; dit au SysTick que le cœur tourne à 16 MHz. C’est vrai
après un reset : le STM32F407 démarre sur son oscillateur interne HSI à 16 MHz
(&lt;code&gt;HSI_VALUE&lt;/code&gt; dans &lt;code&gt;system_stm32f4xx.c&lt;/code&gt;). Le programme ne touche pas aux horloges, donc ce
nombre reste juste. Dès que la PLL est configurée, cette valeur doit être adaptée.&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;etape-4-compiler-et-flasher&quot;&gt;Étape 4 : compiler et flasher&lt;/h2&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Depuis la racine du dépôt&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Depuis la racine du dépôt&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo build --release -p gpio-pac&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo size --release -p gpio-pac&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;   text	   data	    bss	    dec	    hex	filename&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;    796	      0	      4	    800	    320	gpio-pac&lt;/span&gt;
&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;cargo run --release -p gpio-pac&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;Le firmware complet tient en 796 octets de flash : table des vecteurs, démarrage et boucle
compris.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Vérification sur la carte.&lt;/strong&gt; Après &lt;code&gt;cargo run&lt;/code&gt;, la LED verte LD4 s’allume deux
secondes, s’éteint deux secondes, et recommence. Si elle reste éteinte, la ligne 22 est la
première à contrôler : sans l’horloge du port D, les écritures suivantes n’ont aucun effet.&lt;/p&gt;
&lt;h2 id=&quot;etape-5-la-meme-chose-dans-renode&quot;&gt;Étape 5 : la même chose dans Renode&lt;/h2&gt;
&lt;p&gt;Le dépôt contient un script qui charge une carte Discovery simulée et le firmware.&lt;/p&gt;
&lt;figure class=&quot;code-file&quot;&gt;
&lt;figcaption class=&quot;code-file-name&quot;&gt;renode/scripts/gpio-pac.resc&lt;/figcaption&gt;
&lt;pre class=&quot;giallo z-code&quot; data-lang=&quot;plain&quot;&gt;&lt;code &gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mach create &amp;quot;stm32f407-gpio-pac&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Charger la carte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;machine LoadPlatformDescription @platforms/boards/stm32f4_discovery.repl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Ajouter la LED virtuelle sur PD12&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;machine LoadPlatformDescription @renode/platforms/gpio-pac.repl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Charger le firmware Rust&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sysbus LoadELF @target/thumbv7em-none-eabihf/release/gpio-pac&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Démarrer la simulation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;start&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;La simulation se lance avec &lt;code&gt;renode renode/scripts/gpio-pac.resc&lt;/code&gt; depuis la racine du
dépôt. L’état de la LED s’interroge ensuite dans la console de Renode :&lt;/p&gt;
&lt;figure class=&quot;term&quot;&gt;
&lt;figcaption&gt;Console Renode&lt;/figcaption&gt;
&lt;pre class=&quot;term-body&quot; role=&quot;region&quot; aria-label=&quot;Session de terminal : Console Renode&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;term-line&quot;&gt;&lt;span class=&quot;term-prompt&quot; aria-hidden=&quot;true&quot;&gt;(stm32f407-gpio-pac) &lt;/span&gt;&lt;span class=&quot;term-cmd&quot;&gt;sysbus.gpioPortD.UserLED State&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;term-out&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;Vérification dans Renode.&lt;/strong&gt; La commande renvoie &lt;code&gt;True&lt;/code&gt; ou &lt;code&gt;False&lt;/code&gt; selon le moment : la
LED simulée change bien d’état. Mais pas au bon rythme.&lt;/p&gt;
&lt;aside class=&quot;callout callout-attention&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;callout-title&quot;&gt;&lt;span class=&quot;callout-kind&quot;&gt;Attention&lt;/span&gt; &lt;span class=&quot;callout-what&quot;&gt;Dans Renode, la LED clignote 4,5 fois trop vite&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Mesuré avec Renode 1.17.0 en échantillonnant la LED tous les dixièmes de seconde de temps
simulé : elle change d’état toutes les 0,44 s environ, et non toutes les 2 s. La
plateforme &lt;code&gt;stm32f4.repl&lt;/code&gt; de Renode fixe la fréquence du SysTick à 72 MHz
(&lt;code&gt;systickFrequency: 72000000&lt;/code&gt;), alors que le firmware compte sur 16 MHz :
32 000 000 de tops durent 2 s à 16 MHz, mais 0,44 s à 72 MHz.&lt;/p&gt;
&lt;p&gt;Une ligne dans la console, ou dans le script après le chargement de la carte, remet les
choses d’accord : &lt;code&gt;sysbus.nvic Frequency 16000000&lt;/code&gt;. Avec elle, la LED simulée suit bien
le rythme de 2 s.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;C’est exactement le genre d’écart que ce dépôt cherche à recenser : le test « la LED
change d’état » passe en simulation, mais un test sur la &lt;strong&gt;durée&lt;/strong&gt; aurait donné un
résultat faux sans que le firmware soit en cause.&lt;/p&gt;
</content>
    </entry>
</feed>
