FireBeetle 2 ESP32 C6开发板 __ Arduino接口声明讲解与GPIO输入DEMO_CDC串口使用
本帖最后由 尹小舟 于 2024-5-1 16:48 编辑<p><span style="font-size:18px;"><strong>1.Arduino接口声明讲解</strong></span></p>
<article data-content="[{"type":"block","id":"tKdn-1714215366374","name":"paragraph","data":{},"nodes":[{"type":"text","id":"hg8R-1714215366373","leaves":[{"text":"Arduino接口声明讲解","marks":[{"type":"color","value":"#4d4d4d"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"SimHei"}]}]}],"state":{}},{"type":"block","id":"Jfnq-1714213012315","name":"code","data":{"version":1,"language":"","theme":"default"},"nodes":[{"type":"block","id":"wyUy-1714213012304","name":"code-line","data":{},"nodes":[{"type":"text","id":"PtNn-1714213012303","leaves":[{"text":"extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias(\"__pinMode\")));","marks":[]}]}]},{"type":"block","id":"eIem-1714213012306","name":"code-line","data":{},"nodes":[{"type":"text","id":"8ayb-1714213012305","leaves":[{"text":"extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias(\"__digitalWrite\")));","marks":[]}]}]},{"type":"block","id":"Fcqa-1714213012308","name":"code-line","data":{},"nodes":[{"type":"text","id":"WxzP-1714213012307","leaves":[{"text":"extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias(\"__digitalRead\")));","marks":[]}]}]},{"type":"block","id":"A2Xv-1714213012310","name":"code-line","data":{},"nodes":[{"type":"text","id":"t4C9-1714213012309","leaves":[{"text":"extern void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) __attribute__ ((weak, alias(\"__attachInterrupt\")));","marks":[]}]}]},{"type":"block","id":"tz7V-1714213012312","name":"code-line","data":{},"nodes":[{"type":"text","id":"QBoG-1714213012311","leaves":[{"text":"extern void attachInterruptArg(uint8_t pin, voidFuncPtrArg handler, void * arg, int mode) __attribute__ ((weak, alias(\"__attachInterruptArg\")));","marks":[]}]}]},{"type":"block","id":"RK92-1714213012314","name":"code-line","data":{},"nodes":[{"type":"text","id":"IKZj-1714213012313","leaves":[{"text":"extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias(\"__detachInterrupt\")));","marks":[]}]}]}],"state":{}},{"type":"block","id":"G3D2-1714213012336","name":"paragraph","data":{},"nodes":[{"type":"text","id":"3ZOC-1714213012333","leaves":[{"text":"","marks":[{"type":"color","value":"#4d4d4d"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16},{"type":"fontFamily","value":"SimHei"}]}]}],"state":{}},{"type":"block","id":"6dpq-1714215979910","name":"paragraph","data":{},"nodes":[{"type":"text","id":"ZU5Q-1714215979909","leaves":[{"text":"这行代码是C语言中的一个声明,主要用在嵌入式编程中,特别是与Arduino兼容的硬件上。我将逐一解释这行代码的各个部分。","marks":[]}]}],"state":{}},{"type":"block","id":"V50M-1714215981373","name":"list-item","data":{"version":1,"listId":"euKb-1714215981390","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"w2hJ-1714215981372","leaves":[{"text":"extern","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"chuA-1714215981375","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"GPz5-1714215981374","leaves":[{"text":": 这是一个关键字,用于声明一个变量或函数,但不在当前源文件中定义它。这通常意味着该函数或变量的定义在另一个源文件中,或者在链接时提供的库中。","marks":[]}]}],"state":{}},{"type":"block","id":"bwf2-1714215981377","name":"list-item","data":{"version":1,"listId":"euKb-1714215981390","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"vsch-1714215981376","leaves":[{"text":"void pinMode(uint8_t pin, uint8_t mode)等函数","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"QfgL-1714215981379","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"hbDl-1714215981378","leaves":[{"text":"Arduino接口函数","marks":[]}]}],"state":{}},{"type":"block","id":"nqNh-1714215981381","name":"list-item","data":{"version":1,"listId":"euKb-1714215981390","listLevel":1,"listType":"ordered"},"nodes":[{"type":"text","id":"LYD3-1714215981380","leaves":[{"text":"__attribute__ ((weak, alias(\"__pinMode\")));","marks":[]}]}],"state":{"index":3}},{"type":"block","id":"Chy4-1714215981383","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"lByF-1714215981382","leaves":[{"text":" ((weak, alias(\"__pinMode\")))","marks":[{"type":"bold"},{"type":"fontFamily","value":"Arial"}]},{"text":": 这是GCC编译器的一个特性,它用于为函数或变量添加特殊的属性。","marks":[]}]}],"state":{}},{"type":"block","id":"ROY8-1714215981385","name":"list-item","data":{"version":1,"listId":"GU73-1714215981391","listLevel":2,"listType":"unordered"},"nodes":[{"type":"text","id":"KVY3-1714215981384","leaves":[{"text":"weak: 这个属性表示这个函数是一个弱定义。如果有其他同名的非弱定义函数存在,那么在链接时,这个弱定义函数会被忽略。这在提供默认实现的同时允许用户或库提供自定义实现时非常有用。","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"PinE-1714215981387","name":"list-item","data":{"version":1,"listId":"GU73-1714215981391","listLevel":2,"listType":"unordered"},"nodes":[{"type":"text","id":"qTZs-1714215981386","leaves":[{"text":"alias(\"__pinMode\"): 这个属性表示","marks":[]},{"text":"pinMode","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":"函数是另一个函数","marks":[]},{"text":"__pinMode","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":"的别名。这意味着在代码中调用","marks":[]},{"text":"pinMode","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":"实际上会调用","marks":[]},{"text":"__pinMode","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":"。这通常用于允许用户通过更友好的名称(在这种情况下是","marks":[]},{"text":"pinMode","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":")来访问底层或内部的函数(","marks":[]},{"text":"__pinMode","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":")。","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"3PzJ-1714215981389","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"u7eE-1714215981388","leaves":[{"text":"综上所述,这些代码声明了适配的Arduino接口,这个适配的接口是弱的,","marks":[]},{"text":"允许用户或特定库提供自定义实现。","marks":[{"type":"color","value":"#05073b"},{"type":"backgroundColor","value":"rgb(253, 253, 254)"},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}}]">
<p>右键选择Go to Definition 可以跳到以下代码</p>
<pre>
<code>extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias("__pinMode")));
extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("__digitalWrite")));
extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias("__digitalRead")));
extern void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) __attribute__ ((weak, alias("__attachInterrupt")));
extern void attachInterruptArg(uint8_t pin, voidFuncPtrArg handler, void * arg, int mode) __attribute__ ((weak, alias("__attachInterruptArg")));
extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias("__detachInterrupt")));</code></pre>
<p>这行代码是C语言中的一个声明,主要用在嵌入式编程中,特别是与Arduino兼容的硬件上。我将逐一解释这行代码的各个部分。</p>
<ol yne-block-type="list">
<li>extern</li>
</ol>
<p>: 这是一个关键字,用于声明一个变量或函数,但不在当前源文件中定义它。这通常意味着该函数或变量的定义在另一个源文件中,或者在链接时提供的库中。</p>
<ol start="2" yne-block-type="list">
<li>void pinMode(uint8_t pin, uint8_t mode)等函数</li>
</ol>
<p>Arduino接口函数</p>
<ol start="3" yne-block-type="list">
<li>__attribute__ ((weak, alias("__pinMode")));</li>
</ol>
<p> ((weak, alias("__pinMode"))): 这是GCC编译器的一个特性,它用于为函数或变量添加特殊的属性。</p>
<p>综上所述,这些代码声明了适配的Arduino接口,这个适配的接口是弱的,允许用户或特定库提供自定义实现。</p>
</article>
<p> </p>
<p><strong>2.GPIO输入DEMO</strong></p>
<p>按下SW1 亮蓝灯,按下SW2亮红灯</p>
<p>d0865db13fb35299419ec7b4e8c3672e<br />
</p>
<pre>
<code>
#define LED_BLUE SCK
#define LED_RED MOSI
#define SW1_PIN_INPUT 17
#define SW2_PIN_INPUT 16
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(SW1_PIN_INPUT, INPUT_PULLUP); /* 设置key引脚为上拉输入模式 */
pinMode(SW2_PIN_INPUT, INPUT_PULLUP); /* 设置key引脚为上拉输入模式 */
}
// the loop function runs over and over again forever
void loop() {
if(digitalRead(SW1_PIN_INPUT) == 0)
{
delay(20); // wait for a second
if(digitalRead(SW1_PIN_INPUT) == 0)
{
digitalWrite(LED_BLUE, HIGH);// turn the LED on (HIGH is the voltage level)
digitalWrite(LED_RED, LOW); // turn the LED off by making the voltage LOW
}
}
if(digitalRead(SW2_PIN_INPUT) == 0)
{
delay(20); // wait for a second
if(digitalRead(SW2_PIN_INPUT) == 0)
{
digitalWrite(LED_BLUE, LOW);// turn the LED on (HIGH is the voltage level)
digitalWrite(LED_RED, HIGH); // turn the LED off by making the voltage LOW
}
}
}</code></pre>
<p> </p>
<p>3.关于上篇串口的问题</p>
<p> </p>
<p> </p>
<p></p>
<p>将USB CDC On BOOT选择"Enabled" </p>
<p>些代码声明了适配的Arduino接口,这个适配的接口是弱的,弱到什么地步,怎么讲</p>
页:
[1]