<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>firmware robusto - MCU &amp; FPGA</title>
	<atom:link href="https://mcu.tec.br/tags/firmware-robusto/feed/" rel="self" type="application/rss+xml" />
	<link>https://mcu.tec.br</link>
	<description>Microcontroladores &#38; FPGA</description>
	<lastBuildDate>Fri, 30 Jan 2026 14:56:23 +0000</lastBuildDate>
	<language>pt-BR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://mcu.tec.br/wp-content/uploads/2025/02/Robo-para-o-site-MCU.tec_.br-512x512-1-150x150.png</url>
	<title>firmware robusto - MCU &amp; FPGA</title>
	<link>https://mcu.tec.br</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Zephyr e Integração Avançada: Interrupções + Soft Timers + Workqueues</title>
		<link>https://mcu.tec.br/geral/zephyr-e-integracao-avancada-interrupcoes-soft-timers-workqueues/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=zephyr-e-integracao-avancada-interrupcoes-soft-timers-workqueues</link>
					<comments>https://mcu.tec.br/geral/zephyr-e-integracao-avancada-interrupcoes-soft-timers-workqueues/#respond</comments>
		
		<dc:creator><![CDATA[Carlos Delfino]]></dc:creator>
		<pubDate>Fri, 10 Apr 2026 14:49:36 +0000</pubDate>
				<category><![CDATA[geral]]></category>
		<category><![CDATA[RTOS]]></category>
		<category><![CDATA[arquitetura de firmware]]></category>
		<category><![CDATA[boas práticas Zephyr]]></category>
		<category><![CDATA[event driven firmware]]></category>
		<category><![CDATA[firmware escalável]]></category>
		<category><![CDATA[firmware industrial]]></category>
		<category><![CDATA[firmware robusto]]></category>
		<category><![CDATA[interrupções Zephyr]]></category>
		<category><![CDATA[IoT de produção]]></category>
		<category><![CDATA[pipelines de eventos]]></category>
		<category><![CDATA[rtos para iot]]></category>
		<category><![CDATA[sistemas embarcados industriais]]></category>
		<category><![CDATA[soft timers zephyr]]></category>
		<category><![CDATA[workqueues Zephyr]]></category>
		<category><![CDATA[Zephyr k_timer]]></category>
		<category><![CDATA[Zephyr k_work]]></category>
		<category><![CDATA[Zephyr RTOS]]></category>
		<guid isPermaLink="false">https://mcu.tec.br/?p=1247</guid>

					<description><![CDATA[<p>O Zephyr RTOS oferece mecanismos poderosos para o desenvolvimento de firmware industrial e IoT de produção. Neste artigo, apresentamos de forma didática e aprofundada como integrar interrupções, soft timers (k_timer) e workqueues (k_work) para projetar pipelines de eventos robustos, escaláveis e previsíveis. O conteúdo explora boas práticas arquiteturais, anti-padrões comuns, controle de prioridade, backpressure e estratégias de recuperação de falhas, com exemplos reais em C. Ideal para engenheiros de sistemas embarcados que desejam evoluir de firmwares reativos simples para arquiteturas industriais confiáveis baseadas em eventos.</p>
<p>The post <a href="https://mcu.tec.br/geral/zephyr-e-integracao-avancada-interrupcoes-soft-timers-workqueues/">Zephyr e Integração Avançada: Interrupções + Soft Timers + Workqueues</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></description>
										<content:encoded><![CDATA[<div class="root-eb-toc-f87wq wp-block-essential-blocks-table-of-contents"><div class="eb-parent-wrapper eb-parent-eb-toc-f87wq "><div class="eb-toc-container eb-toc-f87wq  eb-toc-is-not-sticky eb-toc-not-collapsible eb-toc-initially-not-collapsed eb-toc-scrollToTop style-1 list-style-none" data-scroll-top="false" data-scroll-top-icon="fas fa-angle-up" data-collapsible="false" data-sticky-hide-mobile="false" data-sticky="false" data-scroll-target="scroll_to_toc" data-copy-link="false" data-editor-type="" data-hide-desktop="false" data-hide-tab="false" data-hide-mobile="false" data-itemCollapsed="false" data-highlight-scroll="false"><div class="eb-toc-header"><h2 class="eb-toc-title">Table of Contents</h2></div><div class="eb-toc-wrapper " data-headers="[{&quot;level&quot;:2,&quot;content&quot;:&quot;2 \u2014 Interrup\u00e7\u00f5es no Zephyr&quot;,&quot;text&quot;:&quot;2 \u2014 Interrup\u00e7\u00f5es no Zephyr&quot;,&quot;link&quot;:&quot;eb-table-content-0&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;O papel correto das ISRs em pipelines de eventos robustos&quot;,&quot;text&quot;:&quot;O papel correto das ISRs em pipelines de eventos robustos&quot;,&quot;link&quot;:&quot;o-papel-correto-das-isrs-em-pipelines-de-eventos-robustos&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.1 O erro cl\u00e1ssico: ISR como \u201cmini-thread\u201d&quot;,&quot;text&quot;:&quot;2.1 O erro cl\u00e1ssico: ISR como \u201cmini-thread\u201d&quot;,&quot;link&quot;:&quot;eb-table-content-2&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.2 O que uma ISR pode fazer no Zephyr&quot;,&quot;text&quot;:&quot;2.2 O que uma ISR pode fazer no Zephyr&quot;,&quot;link&quot;:&quot;22-o-que-uma-isr-pode-fazer-no-zephyr&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.3 Exemplo 1 \u2014 Interrup\u00e7\u00e3o de GPIO bem projetada&quot;,&quot;text&quot;:&quot;2.3 Exemplo 1 \u2014 Interrup\u00e7\u00e3o de GPIO bem projetada&quot;,&quot;link&quot;:&quot;eb-table-content-4&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Configura\u00e7\u00e3o do callback de interrup\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;Configura\u00e7\u00e3o do callback de interrup\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-5&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Inicializa\u00e7\u00e3o do GPIO&quot;,&quot;text&quot;:&quot;Inicializa\u00e7\u00e3o do GPIO&quot;,&quot;link&quot;:&quot;eb-table-content-6&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.4 A thread consumidora do evento&quot;,&quot;text&quot;:&quot;2.4 A thread consumidora do evento&quot;,&quot;link&quot;:&quot;24-a-thread-consumidora-do-evento&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.5 Regra de ouro para ISRs em firmware industrial&quot;,&quot;text&quot;:&quot;2.5 Regra de ouro para ISRs em firmware industrial&quot;,&quot;link&quot;:&quot;25-regra-de-ouro-para-isrs-em-firmware-industrial&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;3 \u2014 Soft Timers no Zephyr&quot;,&quot;text&quot;:&quot;3 \u2014 Soft Timers no Zephyr&quot;,&quot;link&quot;:&quot;3-soft-timers-no-zephyr&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Tempo como evento, n\u00e3o como delay&quot;,&quot;text&quot;:&quot;Tempo como evento, n\u00e3o como delay&quot;,&quot;link&quot;:&quot;eb-table-content-10&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.1 O erro comum: k_sleep() como l\u00f3gica&quot;,&quot;text&quot;:&quot;3.1 O erro comum: k_sleep() como l\u00f3gica&quot;,&quot;link&quot;:&quot;eb-table-content-11&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.2 Conceito de Soft Timer no Zephyr&quot;,&quot;text&quot;:&quot;3.2 Conceito de Soft Timer no Zephyr&quot;,&quot;link&quot;:&quot;32-conceito-de-soft-timer-no-zephyr&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.3 Exemplo 2 \u2014 Timer como fonte de eventos&quot;,&quot;text&quot;:&quot;3.3 Exemplo 2 \u2014 Timer como fonte de eventos&quot;,&quot;link&quot;:&quot;33-exemplo-2-timer-como-fonte-de-eventos&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Defini\u00e7\u00e3o do timer&quot;,&quot;text&quot;:&quot;Defini\u00e7\u00e3o do timer&quot;,&quot;link&quot;:&quot;eb-table-content-14&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Inicializa\u00e7\u00e3o do timer&quot;,&quot;text&quot;:&quot;Inicializa\u00e7\u00e3o do timer&quot;,&quot;link&quot;:&quot;eb-table-content-15&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.4 Consumindo eventos temporais&quot;,&quot;text&quot;:&quot;3.4 Consumindo eventos temporais&quot;,&quot;link&quot;:&quot;34-consumindo-eventos-temporais&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.5 Soft Timer como etapa do pipeline&quot;,&quot;text&quot;:&quot;3.5 Soft Timer como etapa do pipeline&quot;,&quot;link&quot;:&quot;35-soft-timer-como-etapa-do-pipeline&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.6 Regra de ouro dos Soft Timers&quot;,&quot;text&quot;:&quot;3.6 Regra de ouro dos Soft Timers&quot;,&quot;link&quot;:&quot;36-regra-de-ouro-dos-soft-timers&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;4 \u2014 Workqueues no Zephyr&quot;,&quot;text&quot;:&quot;4 \u2014 Workqueues no Zephyr&quot;,&quot;link&quot;:&quot;4-workqueues-no-zephyr&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Processamento desacoplado, escal\u00e1vel e seguro&quot;,&quot;text&quot;:&quot;Processamento desacoplado, escal\u00e1vel e seguro&quot;,&quot;link&quot;:&quot;eb-table-content-20&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.1 O que \u00e9 uma Workqueue, de fato&quot;,&quot;text&quot;:&quot;4.1 O que \u00e9 uma Workqueue, de fato&quot;,&quot;link&quot;:&quot;eb-table-content-21&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.2 O erro comum: jogar tudo na system workqueue&quot;,&quot;text&quot;:&quot;4.2 O erro comum: jogar tudo na system workqueue&quot;,&quot;link&quot;:&quot;42-o-erro-comum-jogar-tudo-na-system-workqueue&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.3 Criando uma Workqueue dedicada&quot;,&quot;text&quot;:&quot;4.3 Criando uma Workqueue dedicada&quot;,&quot;link&quot;:&quot;43-criando-uma-workqueue-dedicada&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Defini\u00e7\u00e3o da workqueue&quot;,&quot;text&quot;:&quot;Defini\u00e7\u00e3o da workqueue&quot;,&quot;link&quot;:&quot;eb-table-content-24&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Inicializa\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;Inicializa\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-25&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.4 Criando um work item&quot;,&quot;text&quot;:&quot;4.4 Criando um work item&quot;,&quot;link&quot;:&quot;44-criando-um-work-item&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.5 Integrando eventos \u2192 workqueues&quot;,&quot;text&quot;:&quot;4.5 Integrando eventos \u2192 workqueues&quot;,&quot;link&quot;:&quot;eb-table-content-27&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;ISR \u2192 Timer \u2192 Workqueue&quot;,&quot;text&quot;:&quot;ISR \u2192 Timer \u2192 Workqueue&quot;,&quot;link&quot;:&quot;eb-table-content-28&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.6 Workqueues como est\u00e1gios de pipeline&quot;,&quot;text&quot;:&quot;4.6 Workqueues como est\u00e1gios de pipeline&quot;,&quot;link&quot;:&quot;eb-table-content-29&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.7 Regra de ouro das Workqueues&quot;,&quot;text&quot;:&quot;4.7 Regra de ouro das Workqueues&quot;,&quot;link&quot;:&quot;47-regra-de-ouro-das-workqueues&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;5 \u2014 Desenhando pipelines completos de eventos no Zephyr&quot;,&quot;text&quot;:&quot;5 \u2014 Desenhando pipelines completos de eventos no Zephyr&quot;,&quot;link&quot;:&quot;5-desenhando-pipelines-completos-de-eventos-no-zephyr&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Do evento f\u00edsico ao processamento industrial robusto&quot;,&quot;text&quot;:&quot;Do evento f\u00edsico ao processamento industrial robusto&quot;,&quot;link&quot;:&quot;eb-table-content-32&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.1 O modelo mental correto: firmware como fluxo de eventos&quot;,&quot;text&quot;:&quot;5.1 O modelo mental correto: firmware como fluxo de eventos&quot;,&quot;link&quot;:&quot;51-o-modelo-mental-correto-firmware-como-fluxo-de-eventos&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.2 Pipeline can\u00f4nico no Zephyr&quot;,&quot;text&quot;:&quot;5.2 Pipeline can\u00f4nico no Zephyr&quot;,&quot;link&quot;:&quot;eb-table-content-34&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.3 Implementa\u00e7\u00e3o passo a passo&quot;,&quot;text&quot;:&quot;5.3 Implementa\u00e7\u00e3o passo a passo&quot;,&quot;link&quot;:&quot;eb-table-content-35&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;1\ufe0f\u20e3 Interrup\u00e7\u00e3o: entrada do pipeline&quot;,&quot;text&quot;:&quot;1\ufe0f\u20e3 Interrup\u00e7\u00e3o: entrada do pipeline&quot;,&quot;link&quot;:&quot;eb-table-content-36&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;2\ufe0f\u20e3 Soft Timer: debounce e normaliza\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;2\ufe0f\u20e3 Soft Timer: debounce e normaliza\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-37&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;3\ufe0f\u20e3 Workqueue: processamento real&quot;,&quot;text&quot;:&quot;3\ufe0f\u20e3 Workqueue: processamento real&quot;,&quot;link&quot;:&quot;eb-table-content-38&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;4\ufe0f\u20e3 Workqueue de comunica\u00e7\u00e3o (opcional, mas comum)&quot;,&quot;text&quot;:&quot;4\ufe0f\u20e3 Workqueue de comunica\u00e7\u00e3o (opcional, mas comum)&quot;,&quot;link&quot;:&quot;eb-table-content-39&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.4 Por que esse pipeline \u00e9 robusto&quot;,&quot;text&quot;:&quot;5.4 Por que esse pipeline \u00e9 robusto&quot;,&quot;link&quot;:&quot;eb-table-content-40&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.5 Anti-padr\u00f5es comuns (e perigosos)&quot;,&quot;text&quot;:&quot;5.5 Anti-padr\u00f5es comuns (e perigosos)&quot;,&quot;link&quot;:&quot;eb-table-content-41&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.6 Regra de ouro do pipeline no Zephyr&quot;,&quot;text&quot;:&quot;5.6 Regra de ouro do pipeline no Zephyr&quot;,&quot;link&quot;:&quot;56-regra-de-ouro-do-pipeline-no-zephyr&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;6 \u2014 Boas pr\u00e1ticas industriais&quot;,&quot;text&quot;:&quot;6 \u2014 Boas pr\u00e1ticas industriais&quot;,&quot;link&quot;:&quot;eb-table-content-43&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Prioridades, backpressure e recupera\u00e7\u00e3o de falhas&quot;,&quot;text&quot;:&quot;Prioridades, backpressure e recupera\u00e7\u00e3o de falhas&quot;,&quot;link&quot;:&quot;eb-table-content-44&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.1 Prioriza\u00e7\u00e3o correta: tempo \u00e9 um recurso finito&quot;,&quot;text&quot;:&quot;6.1 Prioriza\u00e7\u00e3o correta: tempo \u00e9 um recurso finito&quot;,&quot;link&quot;:&quot;eb-table-content-45&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Regra pr\u00e1tica de prioridade&quot;,&quot;text&quot;:&quot;Regra pr\u00e1tica de prioridade&quot;,&quot;link&quot;:&quot;eb-table-content-46&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.2 Exemplo \u2014 Workqueues com prioridades distintas&quot;,&quot;text&quot;:&quot;6.2 Exemplo \u2014 Workqueues com prioridades distintas&quot;,&quot;link&quot;:&quot;62-exemplo-workqueues-com-prioridades-distintas&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.3 Backpressure: quando eventos chegam r\u00e1pido demais&quot;,&quot;text&quot;:&quot;6.3 Backpressure: quando eventos chegam r\u00e1pido demais&quot;,&quot;link&quot;:&quot;eb-table-content-48&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Estrat\u00e9gias de backpressure no Zephyr&quot;,&quot;text&quot;:&quot;Estrat\u00e9gias de backpressure no Zephyr&quot;,&quot;link&quot;:&quot;eb-table-content-49&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.4 Exemplo \u2014 Limitando eventos com k_msgq&quot;,&quot;text&quot;:&quot;6.4 Exemplo \u2014 Limitando eventos com k_msgq&quot;,&quot;link&quot;:&quot;64-exemplo-limitando-eventos-com-k_msgq&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.5 Eventos colaps\u00e1veis (coalescing)&quot;,&quot;text&quot;:&quot;6.5 Eventos colaps\u00e1veis (coalescing)&quot;,&quot;link&quot;:&quot;eb-table-content-51&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.6 Recupera\u00e7\u00e3o de falhas no pipeline&quot;,&quot;text&quot;:&quot;6.6 Recupera\u00e7\u00e3o de falhas no pipeline&quot;,&quot;link&quot;:&quot;eb-table-content-52&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Exemplo \u2014 Timeout l\u00f3gico com timer&quot;,&quot;text&quot;:&quot;Exemplo \u2014 Timeout l\u00f3gico com timer&quot;,&quot;link&quot;:&quot;eb-table-content-53&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.7 Checklist industrial de robustez&quot;,&quot;text&quot;:&quot;6.7 Checklist industrial de robustez&quot;,&quot;link&quot;:&quot;67-checklist-industrial-de-robustez&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;7 \u2014 Estudo de caso completo&quot;,&quot;text&quot;:&quot;7 \u2014 Estudo de caso completo&quot;,&quot;link&quot;:&quot;7-estudo-de-caso-completo&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Pipeline Zephyr em um dispositivo IoT industrial (sensor \u2192 processamento \u2192 nuvem)&quot;,&quot;text&quot;:&quot;Pipeline Zephyr em um dispositivo IoT industrial (sensor \u2192 processamento \u2192 nuvem)&quot;,&quot;link&quot;:&quot;eb-table-content-56&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.1 Cen\u00e1rio do sistema&quot;,&quot;text&quot;:&quot;7.1 Cen\u00e1rio do sistema&quot;,&quot;link&quot;:&quot;eb-table-content-57&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.2 Arquitetura do pipeline&quot;,&quot;text&quot;:&quot;7.2 Arquitetura do pipeline&quot;,&quot;link&quot;:&quot;72-arquitetura-do-pipeline&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.3 Est\u00e1gio 1 \u2014 ISR (evento f\u00edsico)&quot;,&quot;text&quot;:&quot;7.3 Est\u00e1gio 1 \u2014 ISR (evento f\u00edsico)&quot;,&quot;link&quot;:&quot;eb-table-content-59&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.4 Est\u00e1gio 2 \u2014 Soft Timer (normaliza\u00e7\u00e3o temporal)&quot;,&quot;text&quot;:&quot;7.4 Est\u00e1gio 2 \u2014 Soft Timer (normaliza\u00e7\u00e3o temporal)&quot;,&quot;link&quot;:&quot;eb-table-content-60&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.5 Est\u00e1gio 3 \u2014 Workqueue de aquisi\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;7.5 Est\u00e1gio 3 \u2014 Workqueue de aquisi\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-61&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.6 Est\u00e1gio 4 \u2014 Workqueue de processamento&quot;,&quot;text&quot;:&quot;7.6 Est\u00e1gio 4 \u2014 Workqueue de processamento&quot;,&quot;link&quot;:&quot;eb-table-content-62&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.7 Est\u00e1gio 5 \u2014 Workqueue de comunica\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;7.7 Est\u00e1gio 5 \u2014 Workqueue de comunica\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-63&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.8 Controle de carga (backpressure)&quot;,&quot;text&quot;:&quot;7.8 Controle de carga (backpressure)&quot;,&quot;link&quot;:&quot;78-controle-de-carga-backpressure&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.9 Recupera\u00e7\u00e3o e robustez&quot;,&quot;text&quot;:&quot;7.9 Recupera\u00e7\u00e3o e robustez&quot;,&quot;link&quot;:&quot;eb-table-content-65&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.10 O que esse estudo de caso demonstra&quot;,&quot;text&quot;:&quot;7.10 O que esse estudo de caso demonstra&quot;,&quot;link&quot;:&quot;710-o-que-esse-estudo-de-caso-demonstra&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;8 \u2014 Conclus\u00e3o&quot;,&quot;text&quot;:&quot;8 \u2014 Conclus\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-67&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Padr\u00f5es mentais e checklist para firmware industrial com Zephyr&quot;,&quot;text&quot;:&quot;Padr\u00f5es mentais e checklist para firmware industrial com Zephyr&quot;,&quot;link&quot;:&quot;eb-table-content-68&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.1 O modelo mental correto (o que levar deste artigo)&quot;,&quot;text&quot;:&quot;8.1 O modelo mental correto (o que levar deste artigo)&quot;,&quot;link&quot;:&quot;81-o-modelo-mental-correto-o-que-levar-deste-artigo&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.2 Anti-padr\u00f5es finais a evitar&quot;,&quot;text&quot;:&quot;8.2 Anti-padr\u00f5es finais a evitar&quot;,&quot;link&quot;:&quot;eb-table-content-70&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.3 Checklist pr\u00e1tico de revis\u00e3o arquitetural&quot;,&quot;text&quot;:&quot;8.3 Checklist pr\u00e1tico de revis\u00e3o arquitetural&quot;,&quot;link&quot;:&quot;eb-table-content-71&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.4 Onde esse padr\u00e3o \u00e9 usado na pr\u00e1tica&quot;,&quot;text&quot;:&quot;8.4 Onde esse padr\u00e3o \u00e9 usado na pr\u00e1tica&quot;,&quot;link&quot;:&quot;eb-table-content-72&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.5 Encerramento&quot;,&quot;text&quot;:&quot;8.5 Encerramento&quot;,&quot;link&quot;:&quot;85-encerramento&quot;}]" data-visible="[true,true,true,true,true,true]" data-delete-headers="[{&quot;label&quot;:&quot;2 \u2014 Interrup\u00e7\u00f5es no Zephyr&quot;,&quot;value&quot;:&quot;2-interrup\u00e7\u00f5es-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;O papel correto das ISRs em pipelines de eventos robustos&quot;,&quot;value&quot;:&quot;o-papel-correto-das-isrs-em-pipelines-de-eventos-robustos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.1 O erro cl\u00e1ssico: ISR como \u201cmini-thread\u201d&quot;,&quot;value&quot;:&quot;21-o-erro-cl\u00e1ssico-isr-como-mini-thread&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.2 O que uma ISR pode fazer no Zephyr&quot;,&quot;value&quot;:&quot;22-o-que-uma-isr-pode-fazer-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.3 Exemplo 1 \u2014 Interrup\u00e7\u00e3o de GPIO bem projetada&quot;,&quot;value&quot;:&quot;23-exemplo-1-interrup\u00e7\u00e3o-de-gpio-bem-projetada&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Configura\u00e7\u00e3o do callback de interrup\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;configura\u00e7\u00e3o-do-callback-de-interrup\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Inicializa\u00e7\u00e3o do GPIO&quot;,&quot;value&quot;:&quot;inicializa\u00e7\u00e3o-do-gpio&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.4 A thread consumidora do evento&quot;,&quot;value&quot;:&quot;24-a-thread-consumidora-do-evento&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.5 Regra de ouro para ISRs em firmware industrial&quot;,&quot;value&quot;:&quot;25-regra-de-ouro-para-isrs-em-firmware-industrial&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3 \u2014 Soft Timers no Zephyr&quot;,&quot;value&quot;:&quot;3-soft-timers-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Tempo como evento, n\u00e3o como delay&quot;,&quot;value&quot;:&quot;tempo-como-evento-n\u00e3o-como-delay&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.1 O erro comum: k_sleep() como l\u00f3gica&quot;,&quot;value&quot;:&quot;31-o-erro-comum-k_sleep-como-l\u00f3gica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.2 Conceito de Soft Timer no Zephyr&quot;,&quot;value&quot;:&quot;32-conceito-de-soft-timer-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.3 Exemplo 2 \u2014 Timer como fonte de eventos&quot;,&quot;value&quot;:&quot;33-exemplo-2-timer-como-fonte-de-eventos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Defini\u00e7\u00e3o do timer&quot;,&quot;value&quot;:&quot;defini\u00e7\u00e3o-do-timer&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Inicializa\u00e7\u00e3o do timer&quot;,&quot;value&quot;:&quot;inicializa\u00e7\u00e3o-do-timer&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.4 Consumindo eventos temporais&quot;,&quot;value&quot;:&quot;34-consumindo-eventos-temporais&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.5 Soft Timer como etapa do pipeline&quot;,&quot;value&quot;:&quot;35-soft-timer-como-etapa-do-pipeline&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.6 Regra de ouro dos Soft Timers&quot;,&quot;value&quot;:&quot;36-regra-de-ouro-dos-soft-timers&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4 \u2014 Workqueues no Zephyr&quot;,&quot;value&quot;:&quot;4-workqueues-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Processamento desacoplado, escal\u00e1vel e seguro&quot;,&quot;value&quot;:&quot;processamento-desacoplado-escal\u00e1vel-e-seguro&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.1 O que \u00e9 uma Workqueue, de fato&quot;,&quot;value&quot;:&quot;41-o-que-\u00e9-uma-workqueue-de-fato&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.2 O erro comum: jogar tudo na system workqueue&quot;,&quot;value&quot;:&quot;42-o-erro-comum-jogar-tudo-na-system-workqueue&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.3 Criando uma Workqueue dedicada&quot;,&quot;value&quot;:&quot;43-criando-uma-workqueue-dedicada&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Defini\u00e7\u00e3o da workqueue&quot;,&quot;value&quot;:&quot;defini\u00e7\u00e3o-da-workqueue&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Inicializa\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;inicializa\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.4 Criando um work item&quot;,&quot;value&quot;:&quot;44-criando-um-work-item&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.5 Integrando eventos \u2192 workqueues&quot;,&quot;value&quot;:&quot;45-integrando-eventos-\u2192-workqueues&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;ISR \u2192 Timer \u2192 Workqueue&quot;,&quot;value&quot;:&quot;isr-\u2192-timer-\u2192-workqueue&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.6 Workqueues como est\u00e1gios de pipeline&quot;,&quot;value&quot;:&quot;46-workqueues-como-est\u00e1gios-de-pipeline&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.7 Regra de ouro das Workqueues&quot;,&quot;value&quot;:&quot;47-regra-de-ouro-das-workqueues&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5 \u2014 Desenhando pipelines completos de eventos no Zephyr&quot;,&quot;value&quot;:&quot;5-desenhando-pipelines-completos-de-eventos-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Do evento f\u00edsico ao processamento industrial robusto&quot;,&quot;value&quot;:&quot;do-evento-f\u00edsico-ao-processamento-industrial-robusto&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.1 O modelo mental correto: firmware como fluxo de eventos&quot;,&quot;value&quot;:&quot;51-o-modelo-mental-correto-firmware-como-fluxo-de-eventos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.2 Pipeline can\u00f4nico no Zephyr&quot;,&quot;value&quot;:&quot;52-pipeline-can\u00f4nico-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.3 Implementa\u00e7\u00e3o passo a passo&quot;,&quot;value&quot;:&quot;53-implementa\u00e7\u00e3o-passo-a-passo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1\ufe0f\u20e3 Interrup\u00e7\u00e3o: entrada do pipeline&quot;,&quot;value&quot;:&quot;1\ufe0f\u20e3-interrup\u00e7\u00e3o-entrada-do-pipeline&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2\ufe0f\u20e3 Soft Timer: debounce e normaliza\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;2\ufe0f\u20e3-soft-timer-debounce-e-normaliza\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3\ufe0f\u20e3 Workqueue: processamento real&quot;,&quot;value&quot;:&quot;3\ufe0f\u20e3-workqueue-processamento-real&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4\ufe0f\u20e3 Workqueue de comunica\u00e7\u00e3o (opcional, mas comum)&quot;,&quot;value&quot;:&quot;4\ufe0f\u20e3-workqueue-de-comunica\u00e7\u00e3o-opcional-mas-comum&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.4 Por que esse pipeline \u00e9 robusto&quot;,&quot;value&quot;:&quot;54-por-que-esse-pipeline-\u00e9-robusto&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.5 Anti-padr\u00f5es comuns (e perigosos)&quot;,&quot;value&quot;:&quot;55-anti-padr\u00f5es-comuns-e-perigosos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.6 Regra de ouro do pipeline no Zephyr&quot;,&quot;value&quot;:&quot;56-regra-de-ouro-do-pipeline-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6 \u2014 Boas pr\u00e1ticas industriais&quot;,&quot;value&quot;:&quot;6-boas-pr\u00e1ticas-industriais&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Prioridades, backpressure e recupera\u00e7\u00e3o de falhas&quot;,&quot;value&quot;:&quot;prioridades-backpressure-e-recupera\u00e7\u00e3o-de-falhas&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.1 Prioriza\u00e7\u00e3o correta: tempo \u00e9 um recurso finito&quot;,&quot;value&quot;:&quot;61-prioriza\u00e7\u00e3o-correta-tempo-\u00e9-um-recurso-finito&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Regra pr\u00e1tica de prioridade&quot;,&quot;value&quot;:&quot;regra-pr\u00e1tica-de-prioridade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.2 Exemplo \u2014 Workqueues com prioridades distintas&quot;,&quot;value&quot;:&quot;62-exemplo-workqueues-com-prioridades-distintas&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.3 Backpressure: quando eventos chegam r\u00e1pido demais&quot;,&quot;value&quot;:&quot;63-backpressure-quando-eventos-chegam-r\u00e1pido-demais&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Estrat\u00e9gias de backpressure no Zephyr&quot;,&quot;value&quot;:&quot;estrat\u00e9gias-de-backpressure-no-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.4 Exemplo \u2014 Limitando eventos com k_msgq&quot;,&quot;value&quot;:&quot;64-exemplo-limitando-eventos-com-k_msgq&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.5 Eventos colaps\u00e1veis (coalescing)&quot;,&quot;value&quot;:&quot;65-eventos-colaps\u00e1veis-coalescing&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.6 Recupera\u00e7\u00e3o de falhas no pipeline&quot;,&quot;value&quot;:&quot;66-recupera\u00e7\u00e3o-de-falhas-no-pipeline&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo \u2014 Timeout l\u00f3gico com timer&quot;,&quot;value&quot;:&quot;exemplo-timeout-l\u00f3gico-com-timer&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.7 Checklist industrial de robustez&quot;,&quot;value&quot;:&quot;67-checklist-industrial-de-robustez&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7 \u2014 Estudo de caso completo&quot;,&quot;value&quot;:&quot;7-estudo-de-caso-completo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Pipeline Zephyr em um dispositivo IoT industrial (sensor \u2192 processamento \u2192 nuvem)&quot;,&quot;value&quot;:&quot;pipeline-zephyr-em-um-dispositivo-iot-industrial-sensor-\u2192-processamento-\u2192-nuvem&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.1 Cen\u00e1rio do sistema&quot;,&quot;value&quot;:&quot;71-cen\u00e1rio-do-sistema&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.2 Arquitetura do pipeline&quot;,&quot;value&quot;:&quot;72-arquitetura-do-pipeline&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.3 Est\u00e1gio 1 \u2014 ISR (evento f\u00edsico)&quot;,&quot;value&quot;:&quot;73-est\u00e1gio-1-isr-evento-f\u00edsico&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.4 Est\u00e1gio 2 \u2014 Soft Timer (normaliza\u00e7\u00e3o temporal)&quot;,&quot;value&quot;:&quot;74-est\u00e1gio-2-soft-timer-normaliza\u00e7\u00e3o-temporal&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.5 Est\u00e1gio 3 \u2014 Workqueue de aquisi\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;75-est\u00e1gio-3-workqueue-de-aquisi\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.6 Est\u00e1gio 4 \u2014 Workqueue de processamento&quot;,&quot;value&quot;:&quot;76-est\u00e1gio-4-workqueue-de-processamento&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.7 Est\u00e1gio 5 \u2014 Workqueue de comunica\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;77-est\u00e1gio-5-workqueue-de-comunica\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.8 Controle de carga (backpressure)&quot;,&quot;value&quot;:&quot;78-controle-de-carga-backpressure&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.9 Recupera\u00e7\u00e3o e robustez&quot;,&quot;value&quot;:&quot;79-recupera\u00e7\u00e3o-e-robustez&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.10 O que esse estudo de caso demonstra&quot;,&quot;value&quot;:&quot;710-o-que-esse-estudo-de-caso-demonstra&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8 \u2014 Conclus\u00e3o&quot;,&quot;value&quot;:&quot;8-conclus\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Padr\u00f5es mentais e checklist para firmware industrial com Zephyr&quot;,&quot;value&quot;:&quot;padr\u00f5es-mentais-e-checklist-para-firmware-industrial-com-zephyr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.1 O modelo mental correto (o que levar deste artigo)&quot;,&quot;value&quot;:&quot;81-o-modelo-mental-correto-o-que-levar-deste-artigo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.2 Anti-padr\u00f5es finais a evitar&quot;,&quot;value&quot;:&quot;82-anti-padr\u00f5es-finais-a-evitar&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.3 Checklist pr\u00e1tico de revis\u00e3o arquitetural&quot;,&quot;value&quot;:&quot;83-checklist-pr\u00e1tico-de-revis\u00e3o-arquitetural&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.4 Onde esse padr\u00e3o \u00e9 usado na pr\u00e1tica&quot;,&quot;value&quot;:&quot;84-onde-esse-padr\u00e3o-\u00e9-usado-na-pr\u00e1tica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.5 Encerramento&quot;,&quot;value&quot;:&quot;85-encerramento&quot;,&quot;isDelete&quot;:false}]" data-smooth="true" data-top-offset=""><div class="eb-toc__list-wrap"><ul class='eb-toc__list'><li><a href="#eb-table-content-0">2 — Interrupções no Zephyr</a><ul class='eb-toc__list'><li><a href="#o-papel-correto-das-isrs-em-pipelines-de-eventos-robustos">O papel correto das ISRs em pipelines de eventos robustos</a><li><a href="#eb-table-content-2">2.1 O erro clássico: ISR como “mini-thread”</a><li><a href="#22-o-que-uma-isr-pode-fazer-no-zephyr">2.2 O que uma ISR pode fazer no Zephyr</a><li><a href="#eb-table-content-4">2.3 Exemplo 1 — Interrupção de GPIO bem projetada</a><ul class='eb-toc__list'><li><a href="#eb-table-content-5">Configuração do callback de interrupção</a><li><a href="#eb-table-content-6">Inicialização do GPIO</a></li></ul><li><a href="#24-a-thread-consumidora-do-evento">2.4 A thread consumidora do evento</a><li><a href="#25-regra-de-ouro-para-isrs-em-firmware-industrial">2.5 Regra de ouro para ISRs em firmware industrial</a></li></ul><li><a href="#3-soft-timers-no-zephyr">3 — Soft Timers no Zephyr</a><ul class='eb-toc__list'><li><a href="#eb-table-content-10">Tempo como evento, não como delay</a><li><a href="#eb-table-content-11">3.1 O erro comum: k_sleep() como lógica</a><li><a href="#32-conceito-de-soft-timer-no-zephyr">3.2 Conceito de Soft Timer no Zephyr</a><li><a href="#33-exemplo-2-timer-como-fonte-de-eventos">3.3 Exemplo 2 — Timer como fonte de eventos</a><ul class='eb-toc__list'><li><a href="#eb-table-content-14">Definição do timer</a><li><a href="#eb-table-content-15">Inicialização do timer</a></li></ul><li><a href="#34-consumindo-eventos-temporais">3.4 Consumindo eventos temporais</a><li><a href="#35-soft-timer-como-etapa-do-pipeline">3.5 Soft Timer como etapa do pipeline</a><li><a href="#36-regra-de-ouro-dos-soft-timers">3.6 Regra de ouro dos Soft Timers</a></li></ul><li><a href="#4-workqueues-no-zephyr">4 — Workqueues no Zephyr</a><ul class='eb-toc__list'><li><a href="#eb-table-content-20">Processamento desacoplado, escalável e seguro</a><li><a href="#eb-table-content-21">4.1 O que é uma Workqueue, de fato</a><li><a href="#42-o-erro-comum-jogar-tudo-na-system-workqueue">4.2 O erro comum: jogar tudo na system workqueue</a><li><a href="#43-criando-uma-workqueue-dedicada">4.3 Criando uma Workqueue dedicada</a><ul class='eb-toc__list'><li><a href="#eb-table-content-24">Definição da workqueue</a><li><a href="#eb-table-content-25">Inicialização</a></li></ul><li><a href="#44-criando-um-work-item">4.4 Criando um work item</a><li><a href="#eb-table-content-27">4.5 Integrando eventos → workqueues</a><ul class='eb-toc__list'><li><a href="#eb-table-content-28">ISR → Timer → Workqueue</a></li></ul><li><a href="#eb-table-content-29">4.6 Workqueues como estágios de pipeline</a><li><a href="#47-regra-de-ouro-das-workqueues">4.7 Regra de ouro das Workqueues</a></li></ul><li><a href="#5-desenhando-pipelines-completos-de-eventos-no-zephyr">5 — Desenhando pipelines completos de eventos no Zephyr</a><ul class='eb-toc__list'><li><a href="#eb-table-content-32">Do evento físico ao processamento industrial robusto</a><li><a href="#51-o-modelo-mental-correto-firmware-como-fluxo-de-eventos">5.1 O modelo mental correto: firmware como fluxo de eventos</a><li><a href="#eb-table-content-34">5.2 Pipeline canônico no Zephyr</a><li><a href="#eb-table-content-35">5.3 Implementação passo a passo</a><ul class='eb-toc__list'><li><a href="#eb-table-content-36">1&#xfe0f;&#x20e3; Interrupção: entrada do pipeline</a><li><a href="#eb-table-content-37">2&#xfe0f;&#x20e3; Soft Timer: debounce e normalização</a><li><a href="#eb-table-content-38">3&#xfe0f;&#x20e3; Workqueue: processamento real</a><li><a href="#eb-table-content-39">4&#xfe0f;&#x20e3; Workqueue de comunicação (opcional, mas comum)</a></li></ul><li><a href="#eb-table-content-40">5.4 Por que esse pipeline é robusto</a><li><a href="#eb-table-content-41">5.5 Anti-padrões comuns (e perigosos)</a><li><a href="#56-regra-de-ouro-do-pipeline-no-zephyr">5.6 Regra de ouro do pipeline no Zephyr</a></li></ul><li><a href="#eb-table-content-43">6 — Boas práticas industriais</a><ul class='eb-toc__list'><li><a href="#eb-table-content-44">Prioridades, backpressure e recuperação de falhas</a><li><a href="#eb-table-content-45">6.1 Priorização correta: tempo é um recurso finito</a><ul class='eb-toc__list'><li><a href="#eb-table-content-46">Regra prática de prioridade</a></li></ul><li><a href="#62-exemplo-workqueues-com-prioridades-distintas">6.2 Exemplo — Workqueues com prioridades distintas</a><li><a href="#eb-table-content-48">6.3 Backpressure: quando eventos chegam rápido demais</a><ul class='eb-toc__list'><li><a href="#eb-table-content-49">Estratégias de backpressure no Zephyr</a></li></ul><li><a href="#64-exemplo-limitando-eventos-com-k_msgq">6.4 Exemplo — Limitando eventos com k_msgq</a><li><a href="#eb-table-content-51">6.5 Eventos colapsáveis (coalescing)</a><li><a href="#eb-table-content-52">6.6 Recuperação de falhas no pipeline</a><ul class='eb-toc__list'><li><a href="#eb-table-content-53">Exemplo — Timeout lógico com timer</a></li></ul><li><a href="#67-checklist-industrial-de-robustez">6.7 Checklist industrial de robustez</a></li></ul><li><a href="#7-estudo-de-caso-completo">7 — Estudo de caso completo</a><ul class='eb-toc__list'><li><a href="#eb-table-content-56">Pipeline Zephyr em um dispositivo IoT industrial (sensor → processamento → nuvem)</a><li><a href="#eb-table-content-57">7.1 Cenário do sistema</a><li><a href="#72-arquitetura-do-pipeline">7.2 Arquitetura do pipeline</a><li><a href="#eb-table-content-59">7.3 Estágio 1 — ISR (evento físico)</a><li><a href="#eb-table-content-60">7.4 Estágio 2 — Soft Timer (normalização temporal)</a><li><a href="#eb-table-content-61">7.5 Estágio 3 — Workqueue de aquisição</a><li><a href="#eb-table-content-62">7.6 Estágio 4 — Workqueue de processamento</a><li><a href="#eb-table-content-63">7.7 Estágio 5 — Workqueue de comunicação</a><li><a href="#78-controle-de-carga-backpressure">7.8 Controle de carga (backpressure)</a><li><a href="#eb-table-content-65">7.9 Recuperação e robustez</a><li><a href="#710-o-que-esse-estudo-de-caso-demonstra">7.10 O que esse estudo de caso demonstra</a></li></ul><li><a href="#eb-table-content-67">8 — Conclusão</a><ul class='eb-toc__list'><li><a href="#eb-table-content-68">Padrões mentais e checklist para firmware industrial com Zephyr</a><li><a href="#81-o-modelo-mental-correto-o-que-levar-deste-artigo">8.1 O modelo mental correto (o que levar deste artigo)</a><li><a href="#eb-table-content-70">8.2 Anti-padrões finais a evitar</a><li><a href="#eb-table-content-71">8.3 Checklist prático de revisão arquitetural</a><li><a href="#eb-table-content-72">8.4 Onde esse padrão é usado na prática</a><li><a href="#85-encerramento">8.5 Encerramento</a></li></ul></ul></div></div></div></div></div>


<p>Em firmware <strong>industrial</strong> e <strong>IoT de produção</strong>, o problema raramente é “como ler um sensor” ou “como reagir a uma interrupção”. O desafio real está em <strong>como transformar eventos assíncronos e imprevisíveis</strong> (interrupções de hardware, timeouts, pacotes de rede, watchdogs) em <strong>fluxos determinísticos, observáveis e recuperáveis</strong> — sem violar latência, sem bloquear ISRs, sem explodir prioridade e sem criar acoplamentos frágeis entre camadas.</p>



<p>O <strong>Zephyr RTOS</strong> se destaca exatamente por oferecer <strong>primitivas complementares</strong> — <em>Interrupções</em>, <em>Soft Timers</em> (<code>k_timer</code>) e <em>Workqueues</em> (<code>k_work</code>) — que, quando <strong>combinadas corretamente</strong>, permitem desenhar <strong>pipelines de eventos robustos</strong>, usados em produtos reais: gateways industriais, sensores remotos, equipamentos médicos, automação predial e dispositivos conectados operando por anos no campo.</p>



<p>Este artigo não trata essas primitivas de forma isolada. O foco é <strong>arquitetural</strong>:<br>como <strong>compor</strong> essas ferramentas para criar <strong>fluxos de processamento desacoplados</strong>, previsíveis e testáveis, respeitando as regras fundamentais de sistemas embarcados modernos:</p>



<ul class="wp-block-list">
<li><strong>ISR mínima</strong>: interrupção apenas sinaliza, nunca processa.</li>



<li><strong>Tempo como evento</strong>: temporização explícita, não “sleep espalhado”.</li>



<li><strong>Processamento fora de ISR</strong>: trabalho pesado sempre em contexto de thread.</li>



<li><strong>Backpressure e ordenação</strong>: eventos fluem por etapas bem definidas.</li>



<li><strong>Escalabilidade</strong>: o desenho deve sobreviver ao crescimento do sistema.</li>
</ul>



<p>Ao longo das próximas seções, vamos evoluir de um <strong>modelo mental</strong> até <strong>códigos concretos em C</strong>, mostrando:</p>



<ol class="wp-block-list">
<li>O papel correto de <strong>Interrupções</strong> no Zephyr.</li>



<li>Como <strong>Soft Timers</strong> viram fontes de eventos temporais confiáveis.</li>



<li>Como <strong>Workqueues</strong> funcionam como estágios de processamento.</li>



<li>Como integrar tudo isso em <strong>pipelines de eventos industriais</strong>, com exemplos reais de firmware.</li>
</ol>



<p>A ideia é que, ao final, você não apenas “saiba usar” essas APIs, mas <strong>consiga enxergar a arquitetura</strong> por trás de um firmware profissional — aquele que não depende de sorte, nem de atrasos mágicos, nem de lógica escondida em ISR.</p><p>The post <a href="https://mcu.tec.br/geral/zephyr-e-integracao-avancada-interrupcoes-soft-timers-workqueues/">Zephyr e Integração Avançada: Interrupções + Soft Timers + Workqueues</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://mcu.tec.br/geral/zephyr-e-integracao-avancada-interrupcoes-soft-timers-workqueues/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1247</post-id>	</item>
		<item>
		<title>Padrões de Projeto Aplicados a RTOS (FreeRTOS)</title>
		<link>https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=padroes-de-projeto-aplicados-a-rtos-freertos-2</link>
					<comments>https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos-2/#respond</comments>
		
		<dc:creator><![CDATA[Carlos Delfino]]></dc:creator>
		<pubDate>Fri, 20 Mar 2026 08:29:27 +0000</pubDate>
				<category><![CDATA[RTOS]]></category>
		<category><![CDATA[arquitetura de firmware]]></category>
		<category><![CDATA[comunicação entre tarefas FreeRTOS]]></category>
		<category><![CDATA[firmware robusto]]></category>
		<category><![CDATA[freertos]]></category>
		<category><![CDATA[inicialização em RTOS]]></category>
		<category><![CDATA[máquinas de estado em FreeRTOS]]></category>
		<category><![CDATA[mutex e gatekeeper FreeRTOS]]></category>
		<category><![CDATA[padrões de projeto embarcados]]></category>
		<category><![CDATA[RTOS em sistemas embarcados]]></category>
		<category><![CDATA[sistemas de tempo real]]></category>
		<guid isPermaLink="false">https://mcu.tec.br/?p=1206</guid>

					<description><![CDATA[<p>Neste artigo apresentamos, de forma didática e aprofundada, os principais padrões de projeto aplicados a sistemas embarcados com FreeRTOS. O conteúdo aborda desde a estruturação de tarefas, comunicação e sincronização, proteção de recursos e controle de estados, até arquitetura em camadas, inicialização segura e monitoramento de saúde do sistema. Com exemplos práticos em C e foco em aplicações reais, o artigo mostra como usar padrões para construir firmware robusto, previsível e escalável, adequado a ambientes industriais, IoT e sistemas críticos de tempo real.</p>
<p>The post <a href="https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos-2/">Padrões de Projeto Aplicados a RTOS (FreeRTOS)</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">Introdução geral</h2>



<p>Em sistemas embarcados com RTOS, especialmente em aplicações industriais, automotivas e IoT crítico, o maior desafio não é apenas “fazer funcionar”, mas <strong>manter previsibilidade temporal, escalabilidade e manutenibilidade</strong>. É nesse ponto que os <strong>padrões de projeto aplicados a RTOS</strong> se tornam fundamentais.</p>



<p>Diferente de aplicações desktop, padrões em RTOS precisam respeitar <strong>restrições de tempo real</strong>, <strong>uso determinístico de memória</strong>, <strong>prioridades</strong>, <strong>latência de interrupções</strong> e <strong>sincronização segura entre contexto de ISR e tarefas</strong>. Muitos padrões clássicos de software são adaptados ou reinterpretados nesse contexto.</p>



<p>Neste artigo, começaremos pelos <strong>padrões estruturais básicos de tarefas</strong>, que formam a fundação de praticamente qualquer sistema com FreeRTOS.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">1 – Padrões Fundamentais de Estruturação de Tarefas</h2>



<h3 class="wp-block-heading">1.1 Superloop + Tasks (Incremental RTOS Adoption)</h3>



<p><strong>Problema resolvido:</strong><br>Projetos legados em superloop (<code>while(1)</code>) tornam-se difíceis de manter à medida que crescem. Migrar tudo de uma vez para RTOS é arriscado.</p>



<p><strong>Ideia do padrão:</strong><br>Manter o superloop como uma <strong>tarefa principal</strong>, introduzindo gradualmente novas tarefas RTOS.</p>



<p><strong>Quando usar:</strong></p>



<ul class="wp-block-list">
<li>Migração de firmware bare-metal para FreeRTOS</li>



<li>Sistemas simples que estão crescendo</li>



<li>Prototipação controlada</li>
</ul>



<p><strong>Estrutura típica:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void LegacySuperloopTask(void *pvParameters)
{
    for (;;)
    {
        ReadSensors();
        ProcessData();
        UpdateOutputs();

        vTaskDelay(pdMS_TO_TICKS(10));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">LegacySuperloopTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ReadSensors</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ProcessData</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">UpdateOutputs</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelay</span><span style="color: #D8DEE9FF">(</span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">10</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>xTaskCreate(
    LegacySuperloopTask,
    "Legacy",
    1024,
    NULL,
    tskIDLE_PRIORITY + 1,
    NULL
);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">xTaskCreate</span><span style="color: #D8DEE9FF">(</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">LegacySuperloopTask</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">Legacy</span><span style="color: #ECEFF4">&quot;</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #B48EAD">1024</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">NULL</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">tskIDLE_PRIORITY</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">NULL</span></span>
<span class="line"><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Baixo risco na migração</li>



<li>Preserva código validado</li>



<li>Facilita testes incrementais</li>
</ul>



<p><strong>Risco comum:</strong><br>Transformar essa tarefa em um “monstro” que ignora o espírito do RTOS. Este padrão <strong>deve ser transitório</strong>, não permanente.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">1.2 One Task Per Responsibility (Uma tarefa por responsabilidade)</h3>



<p><strong>Problema resolvido:</strong><br>Tarefas que fazem “de tudo” dificultam análise temporal, debugging e escalonamento.</p>



<p><strong>Ideia do padrão:</strong><br>Cada tarefa possui <strong>uma única responsabilidade funcional bem definida</strong>.</p>



<p><strong>Exemplo típico em FreeRTOS:</strong></p>



<ul class="wp-block-list">
<li>Task de aquisição</li>



<li>Task de processamento</li>



<li>Task de comunicação</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void SensorTask(void *pvParameters)
{
    for (;;)
    {
        ReadADC();
        xTaskNotify(ProcessTaskHandle, 0, eNoAction);
        vTaskDelay(pdMS_TO_TICKS(5));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">SensorTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ReadADC</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">xTaskNotify</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">ProcessTaskHandle</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">eNoAction</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelay</span><span style="color: #D8DEE9FF">(</span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">5</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void ProcessTask(void *pvParameters)
{
    for (;;)
    {
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
        FilterData();
        ComputeResults();
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">ProcessTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ulTaskNotifyTake</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">pdTRUE</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">FilterData</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ComputeResults</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Facilita análise de prioridades</li>



<li>Reduz acoplamento</li>



<li>Favorece paralelismo real</li>
</ul>



<p><strong>Boas práticas:</strong></p>



<ul class="wp-block-list">
<li>Nomear tarefas claramente</li>



<li>Documentar WCET (Worst Case Execution Time)</li>



<li>Evitar bloqueios longos</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">1.3 Cyclic Executive com RTOS</h3>



<p><strong>Problema resolvido:</strong><br>Algumas aplicações precisam de <strong>periodicidade rígida</strong>, mesmo usando RTOS.</p>



<p><strong>Ideia do padrão:</strong><br>Uma tarefa de alta prioridade atua como <strong>orquestrador temporal</strong>, liberando outras tarefas de forma cíclica.</p>



<p><strong>Estrutura conceitual:</strong></p>



<ul class="wp-block-list">
<li>Task cíclica principal</li>



<li>Subtarefas acionadas por notificações</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void CyclicTask(void *pvParameters)
{
    TickType_t xLastWakeTime = xTaskGetTickCount();

    for (;;)
    {
        xTaskNotify(TaskAHandle, 0, eNoAction);
        xTaskNotify(TaskBHandle, 0, eNoAction);

        vTaskDelayUntil(&amp;xLastWakeTime, pdMS_TO_TICKS(10));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">CyclicTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">TickType_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">xLastWakeTime</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">xTaskGetTickCount</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">xTaskNotify</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">TaskAHandle</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">eNoAction</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">xTaskNotify</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">TaskBHandle</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">eNoAction</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelayUntil</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">xLastWakeTime</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">10</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Controle temporal explícito</li>



<li>Previsibilidade elevada</li>



<li>Útil em controle e automação</li>
</ul>



<p><strong>Limitação:</strong><br>Menos flexível que arquiteturas puramente orientadas a eventos.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">1.4 Idle Task como Padrão Arquitetural</h3>



<p><strong>Problema resolvido:</strong><br>Desperdício de CPU e energia quando o sistema está ocioso.</p>



<p><strong>Ideia do padrão:</strong><br>Usar a <strong>Idle Task</strong> como ponto central para:</p>



<ul class="wp-block-list">
<li>Economia de energia</li>



<li>Limpeza de recursos</li>



<li>Instrumentação</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void vApplicationIdleHook(void)
{
    EnterLowPowerMode();
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">vApplicationIdleHook</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">EnterLowPowerMode</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Uso típico:</strong></p>



<ul class="wp-block-list">
<li><code>__WFI()</code> em Cortex-M</li>



<li>Estatísticas de CPU</li>



<li>Monitoramento de heap</li>
</ul>



<p><strong>Importante:</strong><br>Nunca bloquear, nunca usar delays e nunca acessar recursos não protegidos.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">2 – Padrões de Comunicação e Sincronização em RTOS</h2>



<p>Em FreeRTOS, <strong>concorrência mal projetada é a principal fonte de bugs</strong>: deadlocks, starvation, jitter temporal e corrupção de dados. Os padrões desta seção existem para <strong>substituir variáveis globais, flags soltas e <code>volatile</code> mal utilizados</strong> por mecanismos determinísticos e auditáveis.</p>



<p>Aqui falamos de <strong>comunicação</strong>, não apenas de sincronização.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2.1 Event Queue (Fila de Eventos)</h3>



<p><strong>Problema resolvido:</strong><br>Múltiplos produtores gerando eventos assíncronos para um único consumidor.</p>



<p><strong>Ideia do padrão:</strong><br>Eventos são encapsulados em estruturas e enviados por uma fila RTOS. O consumidor processa eventos <strong>em ordem temporal</strong>.</p>



<p><strong>Quando usar:</strong></p>



<ul class="wp-block-list">
<li>Interface homem-máquina</li>



<li>Eventos de sensores</li>



<li>Drivers desacoplados da lógica de aplicação</li>
</ul>



<p><strong>Estrutura típica do evento:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>typedef enum {
    EVT_BUTTON,
    EVT_SENSOR,
    EVT_TIMEOUT
} EventType;

typedef struct {
    EventType type;
    uint32_t  data;
} AppEvent;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">typedef</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">enum</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">EVT_BUTTON</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">EVT_SENSOR</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">EVT_TIMEOUT</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">EventType</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9">typedef</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">struct</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">EventType</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">type</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">uint32_t</span><span style="color: #D8DEE9FF">  </span><span style="color: #D8DEE9">data</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">AppEvent</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Fila global (criada na inicialização):</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>QueueHandle_t EventQueue;

EventQueue = xQueueCreate(10, sizeof(AppEvent));
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">QueueHandle_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">EventQueue</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9">EventQueue</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">xQueueCreate</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">10</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">sizeof</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">AppEvent</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Produtor:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void ButtonISR(void)
{
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;

    AppEvent evt = {
        .type = EVT_BUTTON,
        .data = 1
    };

    xQueueSendFromISR(EventQueue, &amp;evt, &amp;xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">ButtonISR</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">BaseType_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">xHigherPriorityTaskWoken</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">pdFALSE</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">AppEvent</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">evt</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        .</span><span style="color: #D8DEE9">type</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">EVT_BUTTON</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">        .</span><span style="color: #D8DEE9">data</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">xQueueSendFromISR</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">EventQueue</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">evt</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">xHigherPriorityTaskWoken</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">portYIELD_FROM_ISR</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">xHigherPriorityTaskWoken</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Consumidor:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void EventTask(void *pvParameters)
{
    AppEvent evt;

    for (;;)
    {
        if (xQueueReceive(EventQueue, &amp;evt, portMAX_DELAY))
        {
            switch (evt.type)
            {
                case EVT_BUTTON:
                    HandleButton(evt.data);
                    break;

                case EVT_SENSOR:
                    HandleSensor(evt.data);
                    break;

                default:
                    break;
            }
        }
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">EventTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">AppEvent</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">evt</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">xQueueReceive</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">EventQueue</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">evt</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">))</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">switch</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">evt</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">type</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">EVT_BUTTON</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">                    </span><span style="color: #88C0D0">HandleButton</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">evt</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">data</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                    </span><span style="color: #81A1C1">break;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">EVT_SENSOR</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">                    </span><span style="color: #88C0D0">HandleSensor</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">evt</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">data</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                    </span><span style="color: #81A1C1">break;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">default</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">                    </span><span style="color: #81A1C1">break;</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Desacoplamento total</li>



<li>Ordem garantida</li>



<li>Fácil instrumentação</li>
</ul>



<p><strong>Erro comum:</strong><br>Usar fila para <strong>stream contínuo de dados</strong> (para isso existem buffers).</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2.2 Mailbox (Fila de Mensagem Única)</h3>



<p><strong>Problema resolvido:</strong><br>Apenas <strong>o último valor importa</strong>, não o histórico.</p>



<p><strong>Ideia do padrão:</strong><br>Uma fila de tamanho 1 funciona como <strong>mailbox sobrescrevível</strong>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>QueueHandle_t Mailbox;

Mailbox = xQueueCreate(1, sizeof(uint32_t));
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">QueueHandle_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Mailbox</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9">Mailbox</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">xQueueCreate</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">1</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">sizeof</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">uint32_t</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Produtor:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>uint32_t temperature = ReadTemperature();
xQueueOverwrite(Mailbox, &amp;temperature);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">uint32_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">temperature</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">ReadTemperature</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #88C0D0">xQueueOverwrite</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">Mailbox</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">temperature</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Consumidor:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>uint32_t temp;

if (xQueuePeek(Mailbox, &amp;temp, 0))
{
    UseTemperature(temp);
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">uint32_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">temp</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">xQueuePeek</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">Mailbox</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">temp</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">))</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">UseTemperature</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">temp</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Quando usar:</strong></p>



<ul class="wp-block-list">
<li>Telemetria</li>



<li>Estados globais observáveis</li>



<li>Última leitura válida</li>
</ul>



<p><strong>Vantagem-chave:</strong><br>Evita backlog artificial.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2.3 Publish–Subscribe (Pub-Sub) com FreeRTOS</h3>



<p><strong>Problema resolvido:</strong><br>Múltiplos consumidores interessados no mesmo evento.</p>



<p><strong>Ideia do padrão:</strong><br>Uma tarefa “dispatcher” recebe eventos e os redistribui para múltiplas filas.</p>



<p><strong>Arquitetura:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>ISR / Producers
      ↓
  Event Queue
      ↓
  Dispatcher Task
     ↓          ↓           ↓
 Task A Task B Task C
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">ISR</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">/</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Producers</span></span>
<span class="line"><span style="color: #D8DEE9FF">      ↓</span></span>
<span class="line"><span style="color: #D8DEE9FF">  </span><span style="color: #D8DEE9">Event</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Queue</span></span>
<span class="line"><span style="color: #D8DEE9FF">      ↓</span></span>
<span class="line"><span style="color: #D8DEE9FF">  </span><span style="color: #D8DEE9">Dispatcher</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Task</span></span>
<span class="line"><span style="color: #D8DEE9FF">     ↓          ↓           ↓</span></span>
<span class="line"><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Task</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">A</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Task</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">B</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Task</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">C</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Dispatcher:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void DispatcherTask(void *pvParameters)
{
    AppEvent evt;

    for (;;)
    {
        if (xQueueReceive(EventQueue, &amp;evt, portMAX_DELAY))
        {
            xQueueSend(QueueA, &amp;evt, 0);
            xQueueSend(QueueB, &amp;evt, 0);
        }
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">DispatcherTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">AppEvent</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">evt</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">xQueueReceive</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">EventQueue</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">evt</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">))</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #88C0D0">xQueueSend</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">QueueA</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">evt</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #88C0D0">xQueueSend</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">QueueB</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">evt</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Extensível</li>



<li>Isola produtores de consumidores</li>



<li>Facilita testes</li>
</ul>



<p><strong>Custo:</strong><br>Mais RAM e latência — aceitável em troca de clareza.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2.4 Task Notification como Padrão de Sinalização Rápida</h3>



<p><strong>Problema resolvido:</strong><br>Overhead de filas quando apenas <strong>sinalização simples</strong> é necessária.</p>



<p><strong>Ideia do padrão:</strong><br>Usar <strong>Task Notifications</strong> como semáforos ultraleves.</p>



<p><strong>Produtor:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>xTaskNotify(TaskHandle, 0x01, eSetBits);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">xTaskNotify</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">TaskHandle</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0x01</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">eSetBits</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Consumidor:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>uint32_t flags;

xTaskNotifyWait(0, 0xFFFFFFFF, &amp;flags, portMAX_DELAY);

if (flags &amp; 0x01)
{
    ProcessEvent();
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">uint32_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">flags</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #88C0D0">xTaskNotifyWait</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">0</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0xFFFFFFFF</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">flags</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">flags</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0x01</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">ProcessEvent</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Zero alocação dinâmica</li>



<li>Extremamente rápido</li>



<li>Ideal para ISR → Task</li>
</ul>



<p><strong>Limitação:</strong><br>Apenas 32 bits por tarefa → não serve para dados complexos.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2.5 Stream Buffer e Message Buffer</h3>



<p><strong>Problema resolvido:</strong><br>Troca eficiente de <strong>fluxos contínuos</strong> de dados.</p>



<h4 class="wp-block-heading">Stream Buffer</h4>



<ul class="wp-block-list">
<li>Dados binários contínuos</li>



<li>Sem fronteira de mensagem</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>StreamBufferHandle_t sb;
sb = xStreamBufferCreate(128, 1);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">StreamBufferHandle_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">sb</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9">sb</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">xStreamBufferCreate</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">128</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<h4 class="wp-block-heading">Message Buffer</h4>



<ul class="wp-block-list">
<li>Mensagens delimitadas</li>



<li>Ideal para pacotes</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>MessageBufferHandle_t mb;
mb = xMessageBufferCreate(128);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">MessageBufferHandle_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">mb</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9">mb</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">xMessageBufferCreate</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">128</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Uso típico:</strong></p>



<ul class="wp-block-list">
<li>UART</li>



<li>SPI</li>



<li>Áudio</li>



<li>Protocolos binários</li>
</ul>



<p><strong>Boa prática:</strong><br>Preferir buffers a filas para dados “em volume”.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">3 – Padrões de Exclusão Mútua, Proteção de Recursos e Prevenção de Deadlock</h2>



<p>Em FreeRTOS, <strong>concorrência não controlada não falha sempre — falha quando você menos espera</strong>. Esta seção trata de padrões que <strong>evitam corrupção de dados, inversão de prioridade e deadlocks</strong>, sem sacrificar o determinismo.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3.1 Mutex com Priority Inheritance</h3>



<p><strong>Problema resolvido:</strong><br>Duas ou mais tarefas acessam o mesmo recurso (UART, I²C, SPI, memória compartilhada).</p>



<p><strong>Risco clássico:</strong><br><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f534.png" alt="🔴" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <em>Priority Inversion</em> — tarefa de baixa prioridade bloqueia uma de alta.</p>



<p><strong>Ideia do padrão:</strong><br>Usar <strong>Mutex (não semáforo)</strong> para herança de prioridade automática.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>SemaphoreHandle_t uartMutex;

uartMutex = xSemaphoreCreateMutex();
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">SemaphoreHandle_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">uartMutex</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9">uartMutex</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">xSemaphoreCreateMutex</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Uso correto:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void UartTask(void *pvParameters)
{
    for (;;)
    {
        if (xSemaphoreTake(uartMutex, portMAX_DELAY))
        {
            UART_Write("Hello RTOS\n");
            xSemaphoreGive(uartMutex);
        }
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">UartTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">xSemaphoreTake</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">uartMutex</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">))</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #88C0D0">UART_Write</span><span style="color: #D8DEE9FF">(</span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">Hello RTOS</span><span style="color: #EBCB8B">\n</span><span style="color: #ECEFF4">&quot;</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #88C0D0">xSemaphoreGive</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">uartMutex</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Por que não semáforo binário?</strong></p>



<ul class="wp-block-list">
<li>Semáforos <strong>não fazem herança de prioridade</strong></li>



<li>Mutexes são projetados para exclusão mútua real</li>
</ul>



<p><strong>Regra de ouro:</strong><br><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f512.png" alt="🔒" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <em>Mutex protege recurso, não evento.</em></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3.2 Gatekeeper Task (Padrão Fundamental em FreeRTOS)</h3>



<p><strong>Problema resolvido:</strong><br>Múltiplas tarefas acessando um <strong>único periférico</strong> (UART, Flash, Display).</p>



<p><strong>Ideia do padrão:</strong><br>Apenas <strong>uma tarefa é dona do recurso</strong>. As demais <strong>pedem serviço</strong> via fila.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Este é um dos padrões mais importantes para firmware de produção.</p>
</blockquote>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Estrutura:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Tasks ──► Request Queue ──► Gatekeeper Task ──► Hardware
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">Tasks</span><span style="color: #D8DEE9FF"> ──► </span><span style="color: #D8DEE9">Request</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Queue</span><span style="color: #D8DEE9FF"> ──► </span><span style="color: #D8DEE9">Gatekeeper</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">Task</span><span style="color: #D8DEE9FF"> ──► </span><span style="color: #D8DEE9">Hardware</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Mensagem de requisição:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>typedef struct {
    char message&#91;64&#93;;
} UartRequest;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">typedef</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">struct</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">char</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">message</span><span style="color: #D8DEE9FF">&#91;</span><span style="color: #B48EAD">64</span><span style="color: #D8DEE9FF">&#93;</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">UartRequest</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Gatekeeper:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void UartGatekeeperTask(void *pvParameters)
{
    UartRequest req;

    for (;;)
    {
        if (xQueueReceive(UartQueue, &amp;req, portMAX_DELAY))
        {
            UART_Write(req.message);
        }
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">UartGatekeeperTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">UartRequest</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">req</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">xQueueReceive</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">UartQueue</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">req</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">))</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #88C0D0">UART_Write</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">req</span><span style="color: #ECEFF4">.</span><span style="color: #D8DEE9">message</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Cliente:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>UartRequest req = { .message = "Log message\n" };
xQueueSend(UartQueue, &amp;req, portMAX_DELAY);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">UartRequest</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">req</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span><span style="color: #D8DEE9FF"> .</span><span style="color: #D8DEE9">message</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">Log message</span><span style="color: #EBCB8B">\n</span><span style="color: #ECEFF4">&quot;</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">}</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #88C0D0">xQueueSend</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">UartQueue</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">req</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Elimina mutexes</li>



<li>Elimina deadlocks</li>



<li>Simplifica drivers</li>
</ul>



<p><strong>Trade-off:</strong><br>Leve aumento de latência → ganho enorme em robustez.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3.3 Critical Section (Uso cirúrgico)</h3>



<p><strong>Problema resolvido:</strong><br>Trechos <strong>extremamente curtos</strong> que não podem ser interrompidos.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>taskENTER_CRITICAL();
sharedCounter++;
taskEXIT_CRITICAL();
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">taskENTER_CRITICAL</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9">sharedCounter</span><span style="color: #81A1C1">++;</span></span>
<span class="line"><span style="color: #88C0D0">taskEXIT_CRITICAL</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>O que acontece:</strong></p>



<ul class="wp-block-list">
<li>Desabilita interrupções (localmente)</li>



<li>Bloqueia escalonamento</li>
</ul>



<p><strong>Quando usar:</strong></p>



<ul class="wp-block-list">
<li>Incrementos atômicos</li>



<li>Flags simples</li>



<li>Estruturas lock-free auxiliares</li>
</ul>



<p><strong>Quando NÃO usar:</strong><br><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f6ab.png" alt="🚫" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Código longo<br><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f6ab.png" alt="🚫" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Acesso a drivers<br><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f6ab.png" alt="🚫" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Comunicação entre tarefas</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Se você precisa de <code>printf()</code> dentro de critical section, algo está errado.</p>
</blockquote>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3.4 Read–Modify–Write protegido (Anti-pattern clássico)</h3>



<p><strong>Problema comum (ERRADO):</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>if (flag == 0)
{
    flag = 1;
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">flag</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">==</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">flag</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Correção com mutex:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>xSemaphoreTake(flagMutex, portMAX_DELAY);
if (flag == 0)
{
    flag = 1;
}
xSemaphoreGive(flagMutex);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">xSemaphoreTake</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">flagMutex</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">flag</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">==</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">flag</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #88C0D0">xSemaphoreGive</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">flagMutex</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Ou melhor ainda:</strong><br>Substituir <code>flag</code> por <strong>task notification</strong> ou <strong>event group</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3.5 Event Groups como Padrão de Coordenação</h3>



<p><strong>Problema resolvido:</strong><br>Sincronizar múltiplas tarefas com <strong>condições compostas</strong>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>EventGroupHandle_t systemEvents;

#define EVT_NET_READY   (1 &lt;&lt; 0)
#define EVT_SENSOR_OK   (1 &lt;&lt; 1)
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">EventGroupHandle_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">systemEvents</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">#</span><span style="color: #D8DEE9">define</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">EVT_NET_READY</span><span style="color: #D8DEE9FF">   (</span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;&lt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">#</span><span style="color: #D8DEE9">define</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">EVT_SENSOR_OK</span><span style="color: #D8DEE9FF">   (</span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&lt;&lt;</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Sinalização:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>xEventGroupSetBits(systemEvents, EVT_NET_READY);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">xEventGroupSetBits</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">systemEvents</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">EVT_NET_READY</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Aguardar múltiplas condições:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>xEventGroupWaitBits(
    systemEvents,
    EVT_NET_READY | EVT_SENSOR_OK,
    pdFALSE,
    pdTRUE,
    portMAX_DELAY
);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">xEventGroupWaitBits</span><span style="color: #D8DEE9FF">(</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">systemEvents</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">EVT_NET_READY</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">|</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">EVT_SENSOR_OK</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">pdFALSE</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">pdTRUE</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">portMAX_DELAY</span></span>
<span class="line"><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Uso típico:</strong></p>



<ul class="wp-block-list">
<li>Startup sequencial</li>



<li>Estados globais do sistema</li>



<li>Dependências entre subsistemas</li>
</ul>



<p><strong>Vantagem:</strong><br>Muito mais expressivo que flags globais.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3.6 Deadlock Avoidance (Padrões de Prevenção)</h3>



<p><strong>Causa clássica de deadlock:</strong></p>



<ul class="wp-block-list">
<li>Ordem inconsistente de aquisição de mutexes</li>
</ul>



<p><strong>Padrão de prevenção:</strong><br><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4d0.png" alt="📐" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <em>Sempre adquirir recursos na mesma ordem</em></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>xSemaphoreTake(mutexA, portMAX_DELAY);
xSemaphoreTake(mutexB, portMAX_DELAY);

/* uso */

xSemaphoreGive(mutexB);
xSemaphoreGive(mutexA);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">xSemaphoreTake</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">mutexA</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #88C0D0">xSemaphoreTake</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">mutexB</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #616E88">/* uso */</span></span>
<span class="line"></span>
<span class="line"><span style="color: #88C0D0">xSemaphoreGive</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">mutexB</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #88C0D0">xSemaphoreGive</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">mutexA</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Alternativa mais segura:</strong><br>Eliminar múltiplos mutexes → usar <strong>Gatekeeper</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">4 – Padrões de Controle de Fluxo, Estados e Recuperação em RTOS</h2>



<p>Em sistemas com FreeRTOS, <strong>fluxo implícito é inimigo do determinismo</strong>. <code>if</code> encadeado, flags globais e dependências ocultas tornam o comportamento do sistema impossível de prever sob carga, falhas ou eventos raros.</p>



<p>Os padrões desta seção tornam o <strong>comportamento do sistema explícito, rastreável e recuperável</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">4.1 State Machine (Máquina de Estados Finita – FSM)</h3>



<p><strong>Problema resolvido:</strong><br>Fluxo de controle espalhado por múltiplas tarefas e condicionais.</p>



<p><strong>Ideia do padrão:</strong><br>Representar o comportamento como <strong>estados explícitos</strong> e <strong>transições bem definidas</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Estrutura básica</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>typedef enum {
    STATE_INIT,
    STATE_IDLE,
    STATE_ACTIVE,
    STATE_ERROR
} SystemState;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">typedef</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">enum</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">STATE_INIT</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">STATE_IDLE</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">STATE_ACTIVE</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">STATE_ERROR</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SystemState</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>static SystemState currentState = STATE_INIT;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">static</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SystemState</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">currentState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_INIT</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Execução da máquina de estados (task dedicada)</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void StateMachineTask(void *pvParameters)
{
    for (;;)
    {
        switch (currentState)
        {
            case STATE_INIT:
                InitHardware();
                currentState = STATE_IDLE;
                break;

            case STATE_IDLE:
                if (StartCondition())
                    currentState = STATE_ACTIVE;
                break;

            case STATE_ACTIVE:
                RunControlLoop();
                if (FaultDetected())
                    currentState = STATE_ERROR;
                break;

            case STATE_ERROR:
                HandleFault();
                currentState = STATE_IDLE;
                break;
        }

        vTaskDelay(pdMS_TO_TICKS(10));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">StateMachineTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">switch</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">currentState</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_INIT</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #88C0D0">InitHardware</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">currentState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_IDLE</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">break;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_IDLE</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">StartCondition</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">                    </span><span style="color: #D8DEE9">currentState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_ACTIVE</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">break;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_ACTIVE</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #88C0D0">RunControlLoop</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">FaultDetected</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">                    </span><span style="color: #D8DEE9">currentState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_ERROR</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">break;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_ERROR</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #88C0D0">HandleFault</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">currentState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_IDLE</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">break;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #ECEFF4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelay</span><span style="color: #D8DEE9FF">(</span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">10</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Fluxo claro</li>



<li>Fácil depuração</li>



<li>Excelente para controle e sequenciamento</li>
</ul>



<p><strong>Limitação:</strong><br>Cresce mal quando há muitos estados e eventos → entra a HSM.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">4.2 Hierarchical State Machine (HSM)</h3>



<p><strong>Problema resolvido:</strong><br>Explosão combinatória de estados.</p>



<p><strong>Ideia do padrão:</strong><br>Estados possuem <strong>comportamento comum herdado</strong> de estados “pais”.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Exemplo conceitual</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>SYSTEM
 ├── OPERATIONAL
 │    ├── IDLE
 │    └── ACTIVE
 └── FAULT
      ├── WARNING
      └── CRITICAL
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">SYSTEM</span></span>
<span class="line"><span style="color: #D8DEE9FF"> ├── </span><span style="color: #D8DEE9">OPERATIONAL</span></span>
<span class="line"><span style="color: #D8DEE9FF"> │    ├── </span><span style="color: #D8DEE9">IDLE</span></span>
<span class="line"><span style="color: #D8DEE9FF"> │    └── </span><span style="color: #D8DEE9">ACTIVE</span></span>
<span class="line"><span style="color: #D8DEE9FF"> └── </span><span style="color: #D8DEE9">FAULT</span></span>
<span class="line"><span style="color: #D8DEE9FF">      ├── </span><span style="color: #D8DEE9">WARNING</span></span>
<span class="line"><span style="color: #D8DEE9FF">      └── </span><span style="color: #D8DEE9">CRITICAL</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Implementação simplificada em C</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>typedef enum {
    SUPER_OPERATIONAL,
    SUPER_FAULT
} SuperState;

typedef enum {
    SUB_IDLE,
    SUB_ACTIVE,
    SUB_WARNING,
    SUB_CRITICAL
} SubState;

static SuperState superState;
static SubState   subState;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">typedef</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">enum</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">SUPER_OPERATIONAL</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">SUPER_FAULT</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SuperState</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9">typedef</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">enum</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">SUB_IDLE</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">SUB_ACTIVE</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">SUB_WARNING</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">SUB_CRITICAL</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SubState</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9">static</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SuperState</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">superState</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9">static</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SubState</span><span style="color: #D8DEE9FF">   </span><span style="color: #D8DEE9">subState</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void StateMachineStep(void)
{
    switch (superState)
    {
        case SUPER_OPERATIONAL:
            if (FaultDetected())
            {
                superState = SUPER_FAULT;
                subState = SUB_WARNING;
                break;
            }

            if (subState == SUB_IDLE &amp;&amp; StartCondition())
                subState = SUB_ACTIVE;

            break;

        case SUPER_FAULT:
            HandleFaultCommon();

            if (subState == SUB_WARNING &amp;&amp; EscalateFault())
                subState = SUB_CRITICAL;

            if (FaultCleared())
            {
                superState = SUPER_OPERATIONAL;
                subState = SUB_IDLE;
            }
            break;
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">StateMachineStep</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">switch</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">superState</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUPER_OPERATIONAL</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">FaultDetected</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">superState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUPER_FAULT</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">subState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUB_WARNING</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #81A1C1">break;</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #ECEFF4">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">subState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">==</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUB_IDLE</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;&amp;</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">StartCondition</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">subState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUB_ACTIVE</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">break;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUPER_FAULT</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #88C0D0">HandleFaultCommon</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">subState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">==</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUB_WARNING</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">&amp;&amp;</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">EscalateFault</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">subState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUB_CRITICAL</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">FaultCleared</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">superState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUPER_OPERATIONAL</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">                </span><span style="color: #D8DEE9">subState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SUB_IDLE</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #81A1C1">break;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Redução de duplicação</li>



<li>Escala bem</li>



<li>Muito usada em automotivo e aeroespacial</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">4.3 Mode Manager (Gerenciador de Modos)</h3>



<p><strong>Problema resolvido:</strong><br>Múltiplas tarefas precisam reagir a <strong>mudanças globais de modo</strong>.</p>



<p><strong>Ideia do padrão:</strong><br>Uma tarefa central gerencia o <strong>modo do sistema</strong>, e os demais módulos <strong>se adaptam</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Modos típicos</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>typedef enum {
    MODE_STARTUP,
    MODE_NORMAL,
    MODE_LOW_POWER,
    MODE_MAINTENANCE
} SystemMode;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">typedef</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">enum</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">MODE_STARTUP</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">MODE_NORMAL</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">MODE_LOW_POWER</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">MODE_MAINTENANCE</span></span>
<span class="line"><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SystemMode</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Mode Manager</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>static SystemMode currentMode;

void ModeManagerTask(void *pvParameters)
{
    for (;;)
    {
        if (LowPowerRequested())
            currentMode = MODE_LOW_POWER;

        if (MaintenanceRequested())
            currentMode = MODE_MAINTENANCE;

        vTaskDelay(pdMS_TO_TICKS(100));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9">static</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">SystemMode</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">currentMode</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">ModeManagerTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">LowPowerRequested</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #D8DEE9">currentMode</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">MODE_LOW_POWER</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">MaintenanceRequested</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #D8DEE9">currentMode</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">MODE_MAINTENANCE</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelay</span><span style="color: #D8DEE9FF">(</span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">100</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Uso pelas tarefas</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void SensorTask(void *pvParameters)
{
    for (;;)
    {
        if (currentMode == MODE_NORMAL)
            ReadSensors();

        vTaskDelay(pdMS_TO_TICKS(50));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">SensorTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #D8DEE9">currentMode</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">==</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">MODE_NORMAL</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #88C0D0">ReadSensors</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelay</span><span style="color: #D8DEE9FF">(</span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">50</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Boa prática:</strong><br>Substituir variável global por:</p>



<ul class="wp-block-list">
<li>Event Groups</li>



<li>Message Queue</li>



<li>Broadcast via Pub-Sub</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">4.4 Recovery Pattern (Padrão de Recuperação)</h3>



<p><strong>Problema resolvido:</strong><br>Falhas transitórias travam o sistema.</p>



<p><strong>Ideia do padrão:</strong><br>Falhas são tratadas como <strong>estados</strong>, não exceções implícitas.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Exemplo de recuperação controlada</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>case STATE_ERROR:
    LogFault();
    ResetSubsystem();
    if (RetryAllowed())
        currentState = STATE_INIT;
    else
        EnterSafeState();
    break;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">case</span><span style="color: #D8DEE9FF"> STATE_ERROR</span><span style="color: #ECEFF4">:</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">LogFault</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">ResetSubsystem</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">RetryAllowed</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #D8DEE9">currentState</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">STATE_INIT</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">else</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">EnterSafeState</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">break;</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Evita reboot desnecessário</li>



<li>Permite retry controlado</li>



<li>Essencial para sistemas remotos</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">4.5 Watchdog como Padrão Arquitetural</h3>



<p><strong>Problema resolvido:</strong><br>Deadlocks silenciosos e travamentos não detectados.</p>



<p><strong>Ideia do padrão:</strong><br>Cada tarefa <strong>sinaliza vida</strong> periodicamente para um supervisor.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void SupervisorTask(void *pvParameters)
{
    for (;;)
    {
        if (!AllTasksAlive())
            SystemReset();

        FeedWatchdog();
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">SupervisorTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">!</span><span style="color: #88C0D0">AllTasksAlive</span><span style="color: #D8DEE9FF">())</span></span>
<span class="line"><span style="color: #D8DEE9FF">            </span><span style="color: #88C0D0">SystemReset</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">FeedWatchdog</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelay</span><span style="color: #D8DEE9FF">(</span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">1000</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Importante:</strong><br>O watchdog <strong>não substitui</strong> arquitetura correta — ele a complementa.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>Excelente. Agora avançamos para a <strong>arquitetura interna do firmware</strong>, onde decisões erradas costumam gerar <strong>acoplamento irreversível</strong>, e decisões corretas permitem <strong>portabilidade, testes e evolução por anos</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">5 – Padrões de Arquitetura em Camadas, Drivers e Serviços em RTOS</h2>



<p>Em sistemas com FreeRTOS, <strong>misturar driver, lógica de negócio e política de tempo real na mesma tarefa é um erro estrutural</strong>. Os padrões desta seção tratam de <strong>separação de responsabilidades</strong>, <strong>isolamento de hardware</strong> e <strong>execução previsível</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">5.1 Layered Architecture (Arquitetura em Camadas)</h3>



<p><strong>Problema resolvido:</strong><br>Firmware “espaguete”, onde qualquer módulo acessa diretamente registradores, RTOS e aplicação.</p>



<p><strong>Ideia do padrão:</strong><br>Organizar o sistema em <strong>camadas bem definidas</strong>, com dependências unidirecionais.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Camadas típicas em FreeRTOS</h4>



<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td>Application / State Machines</td></tr><tr><td>Services (Comms, Storage)</td></tr><tr><td>Drivers / HAL</td></tr><tr><td>RTOS (FreeRTOS Kernel)</td></tr><tr><td>Hardware</td></tr></tbody></table></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Exemplo de separação correta</h4>



<p><strong>Driver (HAL):</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void UartHw_SendByte(uint8_t b);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void UartHw_SendByte(uint8_t b);</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Serviço:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void UartService_Send(const uint8_t *data, size_t len);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void UartService_Send(const uint8_t *data, size_t len);</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Aplicação:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>UartService_Send((uint8_t*)"OK\n", 3);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">UartService_Send((uint8_t*)&quot;OK\n&quot;, 3);</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Portabilidade de MCU</li>



<li>Testes unitários viáveis</li>



<li>Evolução controlada</li>
</ul>



<p><strong>Regra prática:</strong><br><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2b06.png" alt="⬆" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Camadas superiores <strong>não conhecem</strong> detalhes das inferiores.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">5.2 Hardware Abstraction Layer (HAL) como Padrão</h3>



<p><strong>Problema resolvido:</strong><br>Dependência direta de registradores ou SDK do fabricante.</p>



<p><strong>Ideia do padrão:</strong><br>Isolar acesso ao hardware atrás de uma <strong>API mínima e estável</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>typedef struct {
    void (*init)(void);
    void (*write)(const uint8_t *buf, size_t len);
} UartDriver;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">typedef struct {</span></span>
<span class="line"><span style="color: #D8DEE9FF">    void (*init)(void);</span></span>
<span class="line"><span style="color: #D8DEE9FF">    void (*write)(const uint8_t *buf, size_t len);</span></span>
<span class="line"><span style="color: #D8DEE9FF">} UartDriver;</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>static UartDriver uart0 = {
    .init  = STM32_UartInit,
    .write = STM32_UartWrite
};
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">static UartDriver uart0 = {</span></span>
<span class="line"><span style="color: #D8DEE9FF">    .init  = STM32_UartInit,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    .write = STM32_UartWrite</span></span>
<span class="line"><span style="color: #D8DEE9FF">};</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Benefício crítico:</strong><br>Permite trocar STM32 → NXP → RP2040 <strong>sem reescrever aplicação</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">5.3 Service Layer (Camada de Serviços)</h3>



<p><strong>Problema resolvido:</strong><br>Drivers não devem conter lógica de negócio nem política de concorrência.</p>



<p><strong>Ideia do padrão:</strong><br>Serviços encapsulam:</p>



<ul class="wp-block-list">
<li>Sincronização</li>



<li>Filas</li>



<li>Retries</li>



<li>Timeout</li>



<li>Políticas de acesso</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Exemplo: Serviço de UART com Gatekeeper</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void UartService_Send(const uint8_t *data, size_t len)
{
    UartRequest req = {
        .data = data,
        .len  = len
    };

    xQueueSend(UartQueue, &amp;req, portMAX_DELAY);
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void UartService_Send(const uint8_t *data, size_t len)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    UartRequest req = {</span></span>
<span class="line"><span style="color: #D8DEE9FF">        .data = data,</span></span>
<span class="line"><span style="color: #D8DEE9FF">        .len  = len</span></span>
<span class="line"><span style="color: #D8DEE9FF">    };</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    xQueueSend(UartQueue, &amp;req, portMAX_DELAY);</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Aplicação fica limpa</li>



<li>Política centralizada</li>



<li>Drivers permanecem simples</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">5.4 Active Object Pattern</h3>



<p><strong>Problema resolvido:</strong><br>Misturar lógica de controle com concorrência explícita.</p>



<p><strong>Ideia do padrão:</strong><br>Cada objeto ativo possui:</p>



<ul class="wp-block-list">
<li>Estado interno</li>



<li>Fila própria</li>



<li>Tarefa dedicada</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Estrutura conceitual</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>Active Object
 ├── Queue
 ├── Task
 └── State Machine
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">Active Object</span></span>
<span class="line"><span style="color: #D8DEE9FF"> ├── Queue</span></span>
<span class="line"><span style="color: #D8DEE9FF"> ├── Task</span></span>
<span class="line"><span style="color: #D8DEE9FF"> └── State Machine</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Exemplo simplificado</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>typedef struct {
    QueueHandle_t queue;
} MotorController;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">typedef struct {</span></span>
<span class="line"><span style="color: #D8DEE9FF">    QueueHandle_t queue;</span></span>
<span class="line"><span style="color: #D8DEE9FF">} MotorController;</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void MotorTask(void *pvParameters)
{
    MotorCommand cmd;

    for (;;)
    {
        xQueueReceive(motor.queue, &amp;cmd, portMAX_DELAY);
        ExecuteMotorCommand(&amp;cmd);
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void MotorTask(void *pvParameters)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    MotorCommand cmd;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    for (;;)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    {</span></span>
<span class="line"><span style="color: #D8DEE9FF">        xQueueReceive(motor.queue, &amp;cmd, portMAX_DELAY);</span></span>
<span class="line"><span style="color: #D8DEE9FF">        ExecuteMotorCommand(&amp;cmd);</span></span>
<span class="line"><span style="color: #D8DEE9FF">    }</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Forte encapsulamento</li>



<li>Ideal para controle, comunicação e I/O</li>



<li>Elimina mutexes internos</li>
</ul>



<p><strong>Custo:</strong><br>Uso maior de RAM (1 task + 1 fila por objeto)</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">5.5 Deferred Interrupt Processing</h3>



<p><strong>Problema resolvido:</strong><br>ISR longa causa jitter e perda de deadlines.</p>



<p><strong>Ideia do padrão:</strong><br>ISR apenas <strong>sinaliza</strong> → processamento pesado ocorre em task.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void ADC_IRQHandler(void)
{
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    xTaskNotifyFromISR(AdcTaskHandle, 0, eNoAction, &amp;xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void ADC_IRQHandler(void)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    BaseType_t xHigherPriorityTaskWoken = pdFALSE;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    xTaskNotifyFromISR(AdcTaskHandle, 0, eNoAction, &amp;xHigherPriorityTaskWoken);</span></span>
<span class="line"><span style="color: #D8DEE9FF">    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void AdcTask(void *pvParameters)
{
    for (;;)
    {
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
        ProcessAdcData();
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void AdcTask(void *pvParameters)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    for (;;)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    {</span></span>
<span class="line"><span style="color: #D8DEE9FF">        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);</span></span>
<span class="line"><span style="color: #D8DEE9FF">        ProcessAdcData();</span></span>
<span class="line"><span style="color: #D8DEE9FF">    }</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>ISR mínima</li>



<li>Melhor escalonamento</li>



<li>Sistema mais previsível</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"> 6 – Padrões de Inicialização, Startup Sequencial e Monitoramento de Saúde</h2>



<p>Em sistemas com FreeRTOS, <strong>o boot é o momento mais frágil do sistema</strong>. Muitos firmwares falham não durante a operação normal, mas <strong>durante inicialização parcial, falhas de periféricos, ordem incorreta de dependências ou estados indefinidos</strong>.</p>



<p>Os padrões desta seção tornam o startup <strong>determinístico, auditável e recuperável</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">6.1 Startup Task (Task de Inicialização)</h3>



<p><strong>Problema resolvido:</strong><br>Inicialização espalhada entre <code>main()</code>, ISRs e tarefas diversas.</p>



<p><strong>Ideia do padrão:</strong><br>Criar uma <strong>tarefa exclusiva de startup</strong>, responsável por inicializar tudo <strong>em ordem controlada</strong>, antes do sistema entrar em operação normal.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Estrutura típica</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>int main(void)
{
    HardwareInit();

    xTaskCreate(StartupTask, "Startup", 1024, NULL, tskIDLE_PRIORITY + 3, NULL);

    vTaskStartScheduler();
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">int main(void)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    HardwareInit();</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    xTaskCreate(StartupTask, &quot;Startup&quot;, 1024, NULL, tskIDLE_PRIORITY + 3, NULL);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    vTaskStartScheduler();</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void StartupTask(void *pvParameters)
{
    InitDrivers();
    InitServices();
    InitApplication();

    xTaskCreate(AppTask, "App", 1024, NULL, tskIDLE_PRIORITY + 1, NULL);

    vTaskDelete(NULL);
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void StartupTask(void *pvParameters)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    InitDrivers();</span></span>
<span class="line"><span style="color: #D8DEE9FF">    InitServices();</span></span>
<span class="line"><span style="color: #D8DEE9FF">    InitApplication();</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    xTaskCreate(AppTask, &quot;App&quot;, 1024, NULL, tskIDLE_PRIORITY + 1, NULL);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    vTaskDelete(NULL);</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Ordem explícita</li>



<li>Inicialização rastreável</li>



<li>Elimina “efeitos colaterais” no <code>main()</code></li>
</ul>



<p><strong>Boa prática:</strong><br>Startup task deve <strong>se autodestruir</strong> após cumprir sua função.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">6.2 Init Sequencing (Sequenciamento de Inicialização)</h3>



<p><strong>Problema resolvido:</strong><br>Tarefas começam a rodar antes de suas dependências estarem prontas.</p>



<p><strong>Ideia do padrão:</strong><br>Inicialização ocorre em <strong>fases bem definidas</strong>, com sincronização explícita.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Exemplo com Event Groups</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>#define EVT_DRIVERS_READY   (1 &lt;&lt; 0)
#define EVT_SERVICES_READY  (1 &lt;&lt; 1)
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">#define EVT_DRIVERS_READY   (1 &lt;&lt; 0)</span></span>
<span class="line"><span style="color: #D8DEE9FF">#define EVT_SERVICES_READY  (1 &lt;&lt; 1)</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void StartupTask(void *pvParameters)
{
    InitDrivers();
    xEventGroupSetBits(systemEvents, EVT_DRIVERS_READY);

    InitServices();
    xEventGroupSetBits(systemEvents, EVT_SERVICES_READY);

    vTaskDelete(NULL);
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void StartupTask(void *pvParameters)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    InitDrivers();</span></span>
<span class="line"><span style="color: #D8DEE9FF">    xEventGroupSetBits(systemEvents, EVT_DRIVERS_READY);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    InitServices();</span></span>
<span class="line"><span style="color: #D8DEE9FF">    xEventGroupSetBits(systemEvents, EVT_SERVICES_READY);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    vTaskDelete(NULL);</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void AppTask(void *pvParameters)
{
    xEventGroupWaitBits(
        systemEvents,
        EVT_DRIVERS_READY | EVT_SERVICES_READY,
        pdFALSE,
        pdTRUE,
        portMAX_DELAY
    );

    RunApplication();
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void AppTask(void *pvParameters)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    xEventGroupWaitBits(</span></span>
<span class="line"><span style="color: #D8DEE9FF">        systemEvents,</span></span>
<span class="line"><span style="color: #D8DEE9FF">        EVT_DRIVERS_READY | EVT_SERVICES_READY,</span></span>
<span class="line"><span style="color: #D8DEE9FF">        pdFALSE,</span></span>
<span class="line"><span style="color: #D8DEE9FF">        pdTRUE,</span></span>
<span class="line"><span style="color: #D8DEE9FF">        portMAX_DELAY</span></span>
<span class="line"><span style="color: #D8DEE9FF">    );</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    RunApplication();</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagem:</strong><br>Dependências explícitas → menos bugs “fantasma”.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">6.3 Dependency Ordering Pattern</h3>



<p><strong>Problema resolvido:</strong><br>Serviços que dependem implicitamente uns dos outros.</p>



<p><strong>Ideia do padrão:</strong><br>Cada serviço <strong>declara o que precisa</strong> antes de operar.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Exemplo conceitual</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>bool Network_IsReady(void);
bool Storage_IsReady(void);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">bool Network_IsReady(void);</span></span>
<span class="line"><span style="color: #D8DEE9FF">bool Storage_IsReady(void);</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void TelemetryTask(void *pvParameters)
{
    while (!Network_IsReady() || !Storage_IsReady())
    {
        vTaskDelay(pdMS_TO_TICKS(100));
    }

    StartTelemetry();
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void TelemetryTask(void *pvParameters)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    while (!Network_IsReady() || !Storage_IsReady())</span></span>
<span class="line"><span style="color: #D8DEE9FF">    {</span></span>
<span class="line"><span style="color: #D8DEE9FF">        vTaskDelay(pdMS_TO_TICKS(100));</span></span>
<span class="line"><span style="color: #D8DEE9FF">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    StartTelemetry();</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Melhoria recomendada:</strong><br>Substituir polling por <strong>eventos ou notificações</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">6.4 Health Monitoring Pattern (Monitoramento de Saúde)</h3>



<p><strong>Problema resolvido:</strong><br>Tarefas travam silenciosamente.</p>



<p><strong>Ideia do padrão:</strong><br>Cada tarefa reporta periodicamente seu estado para um <strong>supervisor central</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">Estrutura básica</h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>typedef struct {
    uint32_t taskId;
    TickType_t lastAlive;
} TaskHealth;
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">typedef struct {</span></span>
<span class="line"><span style="color: #D8DEE9FF">    uint32_t taskId;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    TickType_t lastAlive;</span></span>
<span class="line"><span style="color: #D8DEE9FF">} TaskHealth;</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void HealthReport(uint32_t id)
{
    healthTable&#91;id&#93;.lastAlive = xTaskGetTickCount();
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void HealthReport(uint32_t id)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    healthTable</span><span style="color: #ECEFF4">&#91;</span><span style="color: #88C0D0">id</span><span style="color: #ECEFF4">&#93;</span><span style="color: #D8DEE9FF">.lastAlive = xTaskGetTickCount();</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void SupervisorTask(void *pvParameters)
{
    for (;;)
    {
        if (TaskTimeoutDetected())
            EnterRecoveryMode();

        vTaskDelay(pdMS_TO_TICKS(500));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void SupervisorTask(void *pvParameters)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    for (;;)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    {</span></span>
<span class="line"><span style="color: #D8DEE9FF">        if (TaskTimeoutDetected())</span></span>
<span class="line"><span style="color: #D8DEE9FF">            EnterRecoveryMode();</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        vTaskDelay(pdMS_TO_TICKS(500));</span></span>
<span class="line"><span style="color: #D8DEE9FF">    }</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Detecção precoce de falhas</li>



<li>Integração natural com watchdog</li>



<li>Essencial em sistemas remotos</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">6.5 Fail-Safe State Pattern</h3>



<p><strong>Problema resolvido:</strong><br>Sistema entra em estado indefinido após falha grave.</p>



<p><strong>Ideia do padrão:</strong><br>Definir explicitamente um <strong>estado seguro</strong>, conhecido e controlado.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void EnterSafeState(void)
{
    DisableOutputs();
    StopMotors();
    SignalErrorLED();
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">void EnterSafeState(void)</span></span>
<span class="line"><span style="color: #D8DEE9FF">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    DisableOutputs();</span></span>
<span class="line"><span style="color: #D8DEE9FF">    StopMotors();</span></span>
<span class="line"><span style="color: #D8DEE9FF">    SignalErrorLED();</span></span>
<span class="line"><span style="color: #D8DEE9FF">}</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>Importante:</strong><br>Fail-safe <strong>não é reset</strong>. É sobrevivência controlada.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">6.6 Warm Restart vs Cold Restart</h3>



<p><strong>Problema resolvido:</strong><br>Reset total desnecessário causa perda de estado e latência.</p>



<p><strong>Ideia do padrão:</strong><br>Diferenciar:</p>



<ul class="wp-block-list">
<li><strong>Cold restart</strong> → reboot completo</li>



<li><strong>Warm restart</strong> → reinicialização parcial de serviços</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>if (RecoverableFault())
    RestartServices();
else
    SystemReset();
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #D8DEE9FF">if (RecoverableFault())</span></span>
<span class="line"><span style="color: #D8DEE9FF">    RestartServices();</span></span>
<span class="line"><span style="color: #D8DEE9FF">else</span></span>
<span class="line"><span style="color: #D8DEE9FF">    SystemReset();</span></span>
<span class="line"></span></code></pre></div>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">7 – Síntese Geral e Checklist Arquitetural de Padrões para FreeRTOS</h2>



<p>Após percorrer os principais grupos de padrões aplicáveis a sistemas com RTOS, fica claro que <strong>FreeRTOS não é apenas um escalonador</strong>, mas uma <strong>plataforma arquitetural</strong> que exige decisões conscientes desde o primeiro <code>xTaskCreate()</code>.</p>



<p>Esta seção final transforma o conteúdo técnico em <strong>instrumento de uso real</strong>, algo que pode ser consultado durante o desenho ou revisão de um firmware.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">7.1 Mapa Conceitual dos Padrões em RTOS</h3>



<p>Podemos organizar os padrões apresentados em <strong>camadas de decisão</strong>, do mais fundamental ao mais sistêmico:</p>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Estrutura Básica</h3>



<ul class="wp-block-list">
<li>Superloop + Tasks</li>



<li>One Task per Responsibility</li>



<li>Cyclic Executive</li>



<li>Idle Task Pattern</li>
</ul>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/27a1.png" alt="➡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Define <strong>como o sistema respira</strong></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Comunicação e Sincronização</h3>



<ul class="wp-block-list">
<li>Event Queue</li>



<li>Mailbox</li>



<li>Publish–Subscribe</li>



<li>Task Notifications</li>



<li>Stream Buffer / Message Buffer</li>
</ul>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/27a1.png" alt="➡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Define <strong>como a informação flui</strong></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Proteção de Recursos</h3>



<ul class="wp-block-list">
<li>Mutex com Priority Inheritance</li>



<li>Gatekeeper Task</li>



<li>Critical Sections</li>



<li>Event Groups</li>



<li>Deadlock Avoidance</li>
</ul>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/27a1.png" alt="➡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Define <strong>como o sistema se protege</strong></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Controle de Comportamento</h3>



<ul class="wp-block-list">
<li>Finite State Machine (FSM)</li>



<li>Hierarchical State Machine (HSM)</li>



<li>Mode Manager</li>



<li>Recovery Pattern</li>



<li>Watchdog Pattern</li>
</ul>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/27a1.png" alt="➡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Define <strong>como o sistema se comporta e reage</strong></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Arquitetura Interna</h3>



<ul class="wp-block-list">
<li>Layered Architecture</li>



<li>HAL</li>



<li>Service Layer</li>



<li>Active Object</li>



<li>Deferred Interrupt Processing</li>
</ul>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/27a1.png" alt="➡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Define <strong>como o sistema é construído</strong></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Inicialização e Robustez</h3>



<ul class="wp-block-list">
<li>Startup Task</li>



<li>Init Sequencing</li>



<li>Dependency Ordering</li>



<li>Health Monitoring</li>



<li>Fail-Safe State</li>



<li>Warm vs Cold Restart</li>
</ul>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/27a1.png" alt="➡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Define <strong>como o sistema nasce e sobrevive</strong></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">7.2 Checklist Arquitetural para Projetos com FreeRTOS</h3>



<p>Use este checklist como <strong>ferramenta de revisão técnica</strong>:</p>



<h4 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Estrutura</h4>



<ul class="wp-block-list">
<li>Cada tarefa tem uma responsabilidade clara?</li>



<li>Existe uma política clara de prioridades?</li>



<li>Tarefas críticas têm WCET conhecido?</li>
</ul>



<h4 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Comunicação</h4>



<ul class="wp-block-list">
<li>Variáveis globais foram substituídas por filas/eventos?</li>



<li>ISR apenas sinalizam, nunca processam?</li>



<li>Fluxos contínuos usam buffers, não filas?</li>
</ul>



<h4 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Proteção</h4>



<ul class="wp-block-list">
<li>Mutexes são usados apenas para recursos?</li>



<li>Existe Gatekeeper para periféricos compartilhados?</li>



<li>Ordem de aquisição de recursos é consistente?</li>
</ul>



<h4 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Estados e Modos</h4>



<ul class="wp-block-list">
<li>O comportamento está modelado em FSM ou HSM?</li>



<li>Modos globais são explícitos?</li>



<li>Falhas são estados, não exceções implícitas?</li>
</ul>



<h4 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Arquitetura</h4>



<ul class="wp-block-list">
<li>Aplicação não conhece registradores?</li>



<li>Drivers não conhecem RTOS?</li>



<li>Serviços encapsulam política e sincronização?</li>
</ul>



<h4 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Inicialização e Saúde</h4>



<ul class="wp-block-list">
<li>Startup ocorre em task dedicada?</li>



<li>Dependências são explícitas?</li>



<li>Existe supervisão de tarefas?</li>



<li>Fail-safe é definido?</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">7.3 Erros Clássicos que Esses Padrões Evitam</h3>



<ul class="wp-block-list">
<li><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> “Só mais um <code>volatile</code> resolve”</li>



<li><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> “Vamos proteger tudo com mutex”</li>



<li><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> “ISR rápida não precisa de cuidado”</li>



<li><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> “Reset resolve qualquer coisa”</li>



<li><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> “Depois a gente organiza a arquitetura”</li>
</ul>



<p>Todos esses erros são <strong>sintomas da ausência de padrões</strong>, não de falta de RTOS.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Conclusão Geral do Artigo</h3>



<p>A maturidade em sistemas embarcados com FreeRTOS <strong>não está em quantas tasks você cria</strong>, mas <strong>em como você estrutura comportamento, comunicação e recuperação</strong>.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>RTOS sem padrões é apenas concorrência.<br>RTOS com padrões é engenharia.</strong></p>
</blockquote>



<p>Os padrões apresentados aqui não são teóricos: eles surgiram de <strong>sistemas industriais, médicos, automotivos e aeroespaciais</strong>, e quando aplicados corretamente, reduzem drasticamente:</p>



<ul class="wp-block-list">
<li>Bugs intermitentes</li>



<li>Tempo de depuração</li>



<li>Retrabalho arquitetural</li>



<li>Risco em produção</li>
</ul><p>The post <a href="https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos-2/">Padrões de Projeto Aplicados a RTOS (FreeRTOS)</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1206</post-id>	</item>
		<item>
		<title>Padrões de Projeto Aplicados a RTOS (FreeRTOS)</title>
		<link>https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=padroes-de-projeto-aplicados-a-rtos-freertos</link>
					<comments>https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos/#respond</comments>
		
		<dc:creator><![CDATA[Carlos Delfino]]></dc:creator>
		<pubDate>Fri, 13 Mar 2026 15:48:51 +0000</pubDate>
				<category><![CDATA[RTOS]]></category>
		<category><![CDATA[arquitetura de firmware]]></category>
		<category><![CDATA[comunicação entre tarefas FreeRTOS]]></category>
		<category><![CDATA[firmware robusto]]></category>
		<category><![CDATA[freertos]]></category>
		<category><![CDATA[inicialização em RTOS]]></category>
		<category><![CDATA[máquinas de estado em FreeRTOS]]></category>
		<category><![CDATA[mutex e gatekeeper FreeRTOS]]></category>
		<category><![CDATA[padrões de projeto embarcados]]></category>
		<category><![CDATA[RTOS em sistemas embarcados]]></category>
		<category><![CDATA[sistemas de tempo real]]></category>
		<guid isPermaLink="false">https://mcu.tec.br/?p=1198</guid>

					<description><![CDATA[<p>Neste artigo apresentamos, de forma didática e aprofundada, os principais padrões de projeto aplicados a sistemas embarcados com FreeRTOS. O conteúdo aborda desde a estruturação de tarefas, comunicação e sincronização, proteção de recursos e controle de estados, até arquitetura em camadas, inicialização segura e monitoramento de saúde do sistema. Com exemplos práticos em C e foco em aplicações reais, o artigo mostra como usar padrões para construir firmware robusto, previsível e escalável, adequado a ambientes industriais, IoT e sistemas críticos de tempo real.</p>
<p>The post <a href="https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos/">Padrões de Projeto Aplicados a RTOS (FreeRTOS)</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></description>
										<content:encoded><![CDATA[<div class="root-eb-toc-n0zji wp-block-essential-blocks-table-of-contents"><div class="eb-parent-wrapper eb-parent-eb-toc-n0zji "><div class="eb-toc-container eb-toc-n0zji  eb-toc-is-not-sticky eb-toc-not-collapsible eb-toc-initially-not-collapsed eb-toc-scrollToTop style-1 list-style-none" data-scroll-top="false" data-scroll-top-icon="fas fa-angle-up" data-collapsible="false" data-sticky-hide-mobile="false" data-sticky="false" data-scroll-target="scroll_to_toc" data-copy-link="false" data-editor-type="" data-hide-desktop="false" data-hide-tab="false" data-hide-mobile="false" data-itemCollapsed="false" data-highlight-scroll="false"><div class="eb-toc-header"><h2 class="eb-toc-title">Table of Contents</h2></div><div class="eb-toc-wrapper " data-headers="[{&quot;level&quot;:2,&quot;content&quot;:&quot;Introdu\u00e7\u00e3o geral&quot;,&quot;text&quot;:&quot;Introdu\u00e7\u00e3o geral&quot;,&quot;link&quot;:&quot;eb-table-content-0&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Padr\u00f5es Fundamentais de Estrutura\u00e7\u00e3o de Tarefas&quot;,&quot;text&quot;:&quot;Padr\u00f5es Fundamentais de Estrutura\u00e7\u00e3o de Tarefas&quot;,&quot;link&quot;:&quot;eb-table-content-1&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;1.1 Superloop + Tasks (Incremental RTOS Adoption)&quot;,&quot;text&quot;:&quot;1.1 Superloop + Tasks (Incremental RTOS Adoption)&quot;,&quot;link&quot;:&quot;11-superloop-tasks-incremental-rtos-adoption&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;1.2 One Task Per Responsibility (Uma tarefa por responsabilidade)&quot;,&quot;text&quot;:&quot;1.2 One Task Per Responsibility (Uma tarefa por responsabilidade)&quot;,&quot;link&quot;:&quot;12-one-task-per-responsibility-uma-tarefa-por-responsabilidade&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;1.3 Cyclic Executive com RTOS&quot;,&quot;text&quot;:&quot;1.3 Cyclic Executive com RTOS&quot;,&quot;link&quot;:&quot;13-cyclic-executive-com-rtos&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;1.4 Idle Task como Padr\u00e3o Arquitetural&quot;,&quot;text&quot;:&quot;1.4 Idle Task como Padr\u00e3o Arquitetural&quot;,&quot;link&quot;:&quot;eb-table-content-5&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Padr\u00f5es de Comunica\u00e7\u00e3o e Sincroniza\u00e7\u00e3o em RTOS&quot;,&quot;text&quot;:&quot;Padr\u00f5es de Comunica\u00e7\u00e3o e Sincroniza\u00e7\u00e3o em RTOS&quot;,&quot;link&quot;:&quot;eb-table-content-6&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.1 Event Queue (Fila de Eventos)&quot;,&quot;text&quot;:&quot;2.1 Event Queue (Fila de Eventos)&quot;,&quot;link&quot;:&quot;21-event-queue-fila-de-eventos&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.2 Mailbox (Fila de Mensagem \u00danica)&quot;,&quot;text&quot;:&quot;2.2 Mailbox (Fila de Mensagem \u00danica)&quot;,&quot;link&quot;:&quot;eb-table-content-8&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.3 Publish\u2013Subscribe (Pub-Sub) com FreeRTOS&quot;,&quot;text&quot;:&quot;2.3 Publish\u2013Subscribe (Pub-Sub) com FreeRTOS&quot;,&quot;link&quot;:&quot;23-publishsubscribe-pub-sub-com-freertos&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.4 Task Notification como Padr\u00e3o de Sinaliza\u00e7\u00e3o R\u00e1pida&quot;,&quot;text&quot;:&quot;2.4 Task Notification como Padr\u00e3o de Sinaliza\u00e7\u00e3o R\u00e1pida&quot;,&quot;link&quot;:&quot;eb-table-content-10&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;2.5 Stream Buffer e Message Buffer&quot;,&quot;text&quot;:&quot;2.5 Stream Buffer e Message Buffer&quot;,&quot;link&quot;:&quot;25-stream-buffer-e-message-buffer&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Stream Buffer&quot;,&quot;text&quot;:&quot;Stream Buffer&quot;,&quot;link&quot;:&quot;stream-buffer&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Message Buffer&quot;,&quot;text&quot;:&quot;Message Buffer&quot;,&quot;link&quot;:&quot;message-buffer&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Padr\u00f5es de Exclus\u00e3o M\u00fatua, Prote\u00e7\u00e3o de Recursos e Preven\u00e7\u00e3o de Deadlock&quot;,&quot;text&quot;:&quot;Padr\u00f5es de Exclus\u00e3o M\u00fatua, Prote\u00e7\u00e3o de Recursos e Preven\u00e7\u00e3o de Deadlock&quot;,&quot;link&quot;:&quot;eb-table-content-14&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.1 Mutex com Priority Inheritance&quot;,&quot;text&quot;:&quot;3.1 Mutex com Priority Inheritance&quot;,&quot;link&quot;:&quot;31-mutex-com-priority-inheritance&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.2 Gatekeeper Task (Padr\u00e3o Fundamental em FreeRTOS)&quot;,&quot;text&quot;:&quot;3.2 Gatekeeper Task (Padr\u00e3o Fundamental em FreeRTOS)&quot;,&quot;link&quot;:&quot;eb-table-content-16&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.3 Critical Section (Uso cir\u00fargico)&quot;,&quot;text&quot;:&quot;3.3 Critical Section (Uso cir\u00fargico)&quot;,&quot;link&quot;:&quot;eb-table-content-17&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.4 Read\u2013Modify\u2013Write protegido (Anti-pattern cl\u00e1ssico)&quot;,&quot;text&quot;:&quot;3.4 Read\u2013Modify\u2013Write protegido (Anti-pattern cl\u00e1ssico)&quot;,&quot;link&quot;:&quot;eb-table-content-18&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.5 Event Groups como Padr\u00e3o de Coordena\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;3.5 Event Groups como Padr\u00e3o de Coordena\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-19&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;3.6 Deadlock Avoidance (Padr\u00f5es de Preven\u00e7\u00e3o)&quot;,&quot;text&quot;:&quot;3.6 Deadlock Avoidance (Padr\u00f5es de Preven\u00e7\u00e3o)&quot;,&quot;link&quot;:&quot;eb-table-content-20&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Padr\u00f5es de Controle de Fluxo, Estados e Recupera\u00e7\u00e3o em RTOS&quot;,&quot;text&quot;:&quot;Padr\u00f5es de Controle de Fluxo, Estados e Recupera\u00e7\u00e3o em RTOS&quot;,&quot;link&quot;:&quot;eb-table-content-21&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.1 State Machine (M\u00e1quina de Estados Finita \u2013 FSM)&quot;,&quot;text&quot;:&quot;4.1 State Machine (M\u00e1quina de Estados Finita \u2013 FSM)&quot;,&quot;link&quot;:&quot;eb-table-content-22&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Estrutura b\u00e1sica&quot;,&quot;text&quot;:&quot;Estrutura b\u00e1sica&quot;,&quot;link&quot;:&quot;eb-table-content-23&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Execu\u00e7\u00e3o da m\u00e1quina de estados (task dedicada)&quot;,&quot;text&quot;:&quot;Execu\u00e7\u00e3o da m\u00e1quina de estados (task dedicada)&quot;,&quot;link&quot;:&quot;eb-table-content-24&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.2 Hierarchical State Machine (HSM)&quot;,&quot;text&quot;:&quot;4.2 Hierarchical State Machine (HSM)&quot;,&quot;link&quot;:&quot;42-hierarchical-state-machine-hsm&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Exemplo conceitual&quot;,&quot;text&quot;:&quot;Exemplo conceitual&quot;,&quot;link&quot;:&quot;exemplo-conceitual&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Implementa\u00e7\u00e3o simplificada em C&quot;,&quot;text&quot;:&quot;Implementa\u00e7\u00e3o simplificada em C&quot;,&quot;link&quot;:&quot;eb-table-content-27&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.3 Mode Manager (Gerenciador de Modos)&quot;,&quot;text&quot;:&quot;4.3 Mode Manager (Gerenciador de Modos)&quot;,&quot;link&quot;:&quot;43-mode-manager-gerenciador-de-modos&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Modos t\u00edpicos&quot;,&quot;text&quot;:&quot;Modos t\u00edpicos&quot;,&quot;link&quot;:&quot;eb-table-content-29&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Mode Manager&quot;,&quot;text&quot;:&quot;Mode Manager&quot;,&quot;link&quot;:&quot;mode-manager&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Uso pelas tarefas&quot;,&quot;text&quot;:&quot;Uso pelas tarefas&quot;,&quot;link&quot;:&quot;uso-pelas-tarefas&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.4 Recovery Pattern (Padr\u00e3o de Recupera\u00e7\u00e3o)&quot;,&quot;text&quot;:&quot;4.4 Recovery Pattern (Padr\u00e3o de Recupera\u00e7\u00e3o)&quot;,&quot;link&quot;:&quot;eb-table-content-32&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Exemplo de recupera\u00e7\u00e3o controlada&quot;,&quot;text&quot;:&quot;Exemplo de recupera\u00e7\u00e3o controlada&quot;,&quot;link&quot;:&quot;eb-table-content-33&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;4.5 Watchdog como Padr\u00e3o Arquitetural&quot;,&quot;text&quot;:&quot;4.5 Watchdog como Padr\u00e3o Arquitetural&quot;,&quot;link&quot;:&quot;eb-table-content-34&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Padr\u00f5es de Arquitetura em Camadas, Drivers e Servi\u00e7os em RTOS&quot;,&quot;text&quot;:&quot;Padr\u00f5es de Arquitetura em Camadas, Drivers e Servi\u00e7os em RTOS&quot;,&quot;link&quot;:&quot;eb-table-content-35&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.1 Layered Architecture (Arquitetura em Camadas)&quot;,&quot;text&quot;:&quot;5.1 Layered Architecture (Arquitetura em Camadas)&quot;,&quot;link&quot;:&quot;51-layered-architecture-arquitetura-em-camadas&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Camadas t\u00edpicas em FreeRTOS&quot;,&quot;text&quot;:&quot;Camadas t\u00edpicas em FreeRTOS&quot;,&quot;link&quot;:&quot;eb-table-content-37&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Exemplo de separa\u00e7\u00e3o correta&quot;,&quot;text&quot;:&quot;Exemplo de separa\u00e7\u00e3o correta&quot;,&quot;link&quot;:&quot;eb-table-content-38&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.2 Hardware Abstraction Layer (HAL) como Padr\u00e3o&quot;,&quot;text&quot;:&quot;5.2 Hardware Abstraction Layer (HAL) como Padr\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-39&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.3 Service Layer (Camada de Servi\u00e7os)&quot;,&quot;text&quot;:&quot;5.3 Service Layer (Camada de Servi\u00e7os)&quot;,&quot;link&quot;:&quot;eb-table-content-40&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Exemplo: Servi\u00e7o de UART com Gatekeeper&quot;,&quot;text&quot;:&quot;Exemplo: Servi\u00e7o de UART com Gatekeeper&quot;,&quot;link&quot;:&quot;eb-table-content-41&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.4 Active Object Pattern&quot;,&quot;text&quot;:&quot;5.4 Active Object Pattern&quot;,&quot;link&quot;:&quot;54-active-object-pattern&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Estrutura conceitual&quot;,&quot;text&quot;:&quot;Estrutura conceitual&quot;,&quot;link&quot;:&quot;estrutura-conceitual&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Exemplo simplificado&quot;,&quot;text&quot;:&quot;Exemplo simplificado&quot;,&quot;link&quot;:&quot;exemplo-simplificado&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.5 Deferred Interrupt Processing&quot;,&quot;text&quot;:&quot;5.5 Deferred Interrupt Processing&quot;,&quot;link&quot;:&quot;55-deferred-interrupt-processing&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Padr\u00f5es de Inicializa\u00e7\u00e3o, Startup Sequencial e Monitoramento de Sa\u00fade&quot;,&quot;text&quot;:&quot;Padr\u00f5es de Inicializa\u00e7\u00e3o, Startup Sequencial e Monitoramento de Sa\u00fade&quot;,&quot;link&quot;:&quot;eb-table-content-46&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.1 Startup Task (Task de Inicializa\u00e7\u00e3o)&quot;,&quot;text&quot;:&quot;6.1 Startup Task (Task de Inicializa\u00e7\u00e3o)&quot;,&quot;link&quot;:&quot;eb-table-content-47&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Estrutura t\u00edpica&quot;,&quot;text&quot;:&quot;Estrutura t\u00edpica&quot;,&quot;link&quot;:&quot;eb-table-content-48&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.2 Init Sequencing (Sequenciamento de Inicializa\u00e7\u00e3o)&quot;,&quot;text&quot;:&quot;6.2 Init Sequencing (Sequenciamento de Inicializa\u00e7\u00e3o)&quot;,&quot;link&quot;:&quot;eb-table-content-49&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Exemplo com Event Groups&quot;,&quot;text&quot;:&quot;Exemplo com Event Groups&quot;,&quot;link&quot;:&quot;exemplo-com-event-groups&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.3 Dependency Ordering Pattern&quot;,&quot;text&quot;:&quot;6.3 Dependency Ordering Pattern&quot;,&quot;link&quot;:&quot;63-dependency-ordering-pattern&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Exemplo conceitual&quot;,&quot;text&quot;:&quot;Exemplo conceitual&quot;,&quot;link&quot;:&quot;exemplo-conceitual&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.4 Health Monitoring Pattern (Monitoramento de Sa\u00fade)&quot;,&quot;text&quot;:&quot;6.4 Health Monitoring Pattern (Monitoramento de Sa\u00fade)&quot;,&quot;link&quot;:&quot;eb-table-content-53&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;Estrutura b\u00e1sica&quot;,&quot;text&quot;:&quot;Estrutura b\u00e1sica&quot;,&quot;link&quot;:&quot;eb-table-content-54&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.5 Fail-Safe State Pattern&quot;,&quot;text&quot;:&quot;6.5 Fail-Safe State Pattern&quot;,&quot;link&quot;:&quot;65-fail-safe-state-pattern&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;6.6 Warm Restart vs Cold Restart&quot;,&quot;text&quot;:&quot;6.6 Warm Restart vs Cold Restart&quot;,&quot;link&quot;:&quot;66-warm-restart-vs-cold-restart&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;S\u00edntese Geral e Checklist Arquitetural de Padr\u00f5es para FreeRTOS&quot;,&quot;text&quot;:&quot;S\u00edntese Geral e Checklist Arquitetural de Padr\u00f5es para FreeRTOS&quot;,&quot;link&quot;:&quot;eb-table-content-57&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.1 Mapa Conceitual dos Padr\u00f5es em RTOS&quot;,&quot;text&quot;:&quot;7.1 Mapa Conceitual dos Padr\u00f5es em RTOS&quot;,&quot;link&quot;:&quot;eb-table-content-58&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;\ud83d\udd39 Estrutura B\u00e1sica&quot;,&quot;text&quot;:&quot;\ud83d\udd39 Estrutura B\u00e1sica&quot;,&quot;link&quot;:&quot;eb-table-content-59&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;\ud83d\udd39 Comunica\u00e7\u00e3o e Sincroniza\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;\ud83d\udd39 Comunica\u00e7\u00e3o e Sincroniza\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-60&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;\ud83d\udd39 Prote\u00e7\u00e3o de Recursos&quot;,&quot;text&quot;:&quot;\ud83d\udd39 Prote\u00e7\u00e3o de Recursos&quot;,&quot;link&quot;:&quot;eb-table-content-61&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;\ud83d\udd39 Controle de Comportamento&quot;,&quot;text&quot;:&quot;\ud83d\udd39 Controle de Comportamento&quot;,&quot;link&quot;:&quot;eb-table-content-62&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;\ud83d\udd39 Arquitetura Interna&quot;,&quot;text&quot;:&quot;\ud83d\udd39 Arquitetura Interna&quot;,&quot;link&quot;:&quot;eb-table-content-63&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;\ud83d\udd39 Inicializa\u00e7\u00e3o e Robustez&quot;,&quot;text&quot;:&quot;\ud83d\udd39 Inicializa\u00e7\u00e3o e Robustez&quot;,&quot;link&quot;:&quot;eb-table-content-64&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.2 Checklist Arquitetural para Projetos com FreeRTOS&quot;,&quot;text&quot;:&quot;7.2 Checklist Arquitetural para Projetos com FreeRTOS&quot;,&quot;link&quot;:&quot;72-checklist-arquitetural-para-projetos-com-freertos&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;\u2714\ufe0f Estrutura&quot;,&quot;text&quot;:&quot;\u2714\ufe0f Estrutura&quot;,&quot;link&quot;:&quot;eb-table-content-66&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;\u2714\ufe0f Comunica\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;\u2714\ufe0f Comunica\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-67&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;\u2714\ufe0f Prote\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;\u2714\ufe0f Prote\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-68&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;\u2714\ufe0f Estados e Modos&quot;,&quot;text&quot;:&quot;\u2714\ufe0f Estados e Modos&quot;,&quot;link&quot;:&quot;eb-table-content-69&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;\u2714\ufe0f Arquitetura&quot;,&quot;text&quot;:&quot;\u2714\ufe0f Arquitetura&quot;,&quot;link&quot;:&quot;eb-table-content-70&quot;},{&quot;level&quot;:4,&quot;content&quot;:&quot;\u2714\ufe0f Inicializa\u00e7\u00e3o e Sa\u00fade&quot;,&quot;text&quot;:&quot;\u2714\ufe0f Inicializa\u00e7\u00e3o e Sa\u00fade&quot;,&quot;link&quot;:&quot;eb-table-content-71&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;7.3 Erros Cl\u00e1ssicos que Esses Padr\u00f5es Evitam&quot;,&quot;text&quot;:&quot;7.3 Erros Cl\u00e1ssicos que Esses Padr\u00f5es Evitam&quot;,&quot;link&quot;:&quot;eb-table-content-72&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Conclus\u00e3o&quot;,&quot;text&quot;:&quot;Conclus\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-73&quot;}]" data-visible="[true,true,true,true,true,true]" data-delete-headers="[{&quot;label&quot;:&quot;Introdu\u00e7\u00e3o geral&quot;,&quot;value&quot;:&quot;introdu\u00e7\u00e3o-geral&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Padr\u00f5es Fundamentais de Estrutura\u00e7\u00e3o de Tarefas&quot;,&quot;value&quot;:&quot;padr\u00f5es-fundamentais-de-estrutura\u00e7\u00e3o-de-tarefas&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1.1 Superloop + Tasks (Incremental RTOS Adoption)&quot;,&quot;value&quot;:&quot;11-superloop-tasks-incremental-rtos-adoption&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1.2 One Task Per Responsibility (Uma tarefa por responsabilidade)&quot;,&quot;value&quot;:&quot;12-one-task-per-responsibility-uma-tarefa-por-responsabilidade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1.3 Cyclic Executive com RTOS&quot;,&quot;value&quot;:&quot;13-cyclic-executive-com-rtos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1.4 Idle Task como Padr\u00e3o Arquitetural&quot;,&quot;value&quot;:&quot;14-idle-task-como-padr\u00e3o-arquitetural&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Padr\u00f5es de Comunica\u00e7\u00e3o e Sincroniza\u00e7\u00e3o em RTOS&quot;,&quot;value&quot;:&quot;padr\u00f5es-de-comunica\u00e7\u00e3o-e-sincroniza\u00e7\u00e3o-em-rtos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.1 Event Queue (Fila de Eventos)&quot;,&quot;value&quot;:&quot;21-event-queue-fila-de-eventos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.2 Mailbox (Fila de Mensagem \u00danica)&quot;,&quot;value&quot;:&quot;22-mailbox-fila-de-mensagem-\u00fanica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.3 Publish\u2013Subscribe (Pub-Sub) com FreeRTOS&quot;,&quot;value&quot;:&quot;23-publishsubscribe-pub-sub-com-freertos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.4 Task Notification como Padr\u00e3o de Sinaliza\u00e7\u00e3o R\u00e1pida&quot;,&quot;value&quot;:&quot;24-task-notification-como-padr\u00e3o-de-sinaliza\u00e7\u00e3o-r\u00e1pida&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2.5 Stream Buffer e Message Buffer&quot;,&quot;value&quot;:&quot;25-stream-buffer-e-message-buffer&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Stream Buffer&quot;,&quot;value&quot;:&quot;stream-buffer&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Message Buffer&quot;,&quot;value&quot;:&quot;message-buffer&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Padr\u00f5es de Exclus\u00e3o M\u00fatua, Prote\u00e7\u00e3o de Recursos e Preven\u00e7\u00e3o de Deadlock&quot;,&quot;value&quot;:&quot;padr\u00f5es-de-exclus\u00e3o-m\u00fatua-prote\u00e7\u00e3o-de-recursos-e-preven\u00e7\u00e3o-de-deadlock&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.1 Mutex com Priority Inheritance&quot;,&quot;value&quot;:&quot;31-mutex-com-priority-inheritance&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.2 Gatekeeper Task (Padr\u00e3o Fundamental em FreeRTOS)&quot;,&quot;value&quot;:&quot;32-gatekeeper-task-padr\u00e3o-fundamental-em-freertos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.3 Critical Section (Uso cir\u00fargico)&quot;,&quot;value&quot;:&quot;33-critical-section-uso-cir\u00fargico&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.4 Read\u2013Modify\u2013Write protegido (Anti-pattern cl\u00e1ssico)&quot;,&quot;value&quot;:&quot;34-readmodifywrite-protegido-anti-pattern-cl\u00e1ssico&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.5 Event Groups como Padr\u00e3o de Coordena\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;35-event-groups-como-padr\u00e3o-de-coordena\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3.6 Deadlock Avoidance (Padr\u00f5es de Preven\u00e7\u00e3o)&quot;,&quot;value&quot;:&quot;36-deadlock-avoidance-padr\u00f5es-de-preven\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Padr\u00f5es de Controle de Fluxo, Estados e Recupera\u00e7\u00e3o em RTOS&quot;,&quot;value&quot;:&quot;padr\u00f5es-de-controle-de-fluxo-estados-e-recupera\u00e7\u00e3o-em-rtos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.1 State Machine (M\u00e1quina de Estados Finita \u2013 FSM)&quot;,&quot;value&quot;:&quot;41-state-machine-m\u00e1quina-de-estados-finita-fsm&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Estrutura b\u00e1sica&quot;,&quot;value&quot;:&quot;estrutura-b\u00e1sica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Execu\u00e7\u00e3o da m\u00e1quina de estados (task dedicada)&quot;,&quot;value&quot;:&quot;execu\u00e7\u00e3o-da-m\u00e1quina-de-estados-task-dedicada&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.2 Hierarchical State Machine (HSM)&quot;,&quot;value&quot;:&quot;42-hierarchical-state-machine-hsm&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo conceitual&quot;,&quot;value&quot;:&quot;exemplo-conceitual&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Implementa\u00e7\u00e3o simplificada em C&quot;,&quot;value&quot;:&quot;implementa\u00e7\u00e3o-simplificada-em-c&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.3 Mode Manager (Gerenciador de Modos)&quot;,&quot;value&quot;:&quot;43-mode-manager-gerenciador-de-modos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Modos t\u00edpicos&quot;,&quot;value&quot;:&quot;modos-t\u00edpicos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Mode Manager&quot;,&quot;value&quot;:&quot;mode-manager&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Uso pelas tarefas&quot;,&quot;value&quot;:&quot;uso-pelas-tarefas&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.4 Recovery Pattern (Padr\u00e3o de Recupera\u00e7\u00e3o)&quot;,&quot;value&quot;:&quot;44-recovery-pattern-padr\u00e3o-de-recupera\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo de recupera\u00e7\u00e3o controlada&quot;,&quot;value&quot;:&quot;exemplo-de-recupera\u00e7\u00e3o-controlada&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4.5 Watchdog como Padr\u00e3o Arquitetural&quot;,&quot;value&quot;:&quot;45-watchdog-como-padr\u00e3o-arquitetural&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Padr\u00f5es de Arquitetura em Camadas, Drivers e Servi\u00e7os em RTOS&quot;,&quot;value&quot;:&quot;padr\u00f5es-de-arquitetura-em-camadas-drivers-e-servi\u00e7os-em-rtos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.1 Layered Architecture (Arquitetura em Camadas)&quot;,&quot;value&quot;:&quot;51-layered-architecture-arquitetura-em-camadas&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Camadas t\u00edpicas em FreeRTOS&quot;,&quot;value&quot;:&quot;camadas-t\u00edpicas-em-freertos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo de separa\u00e7\u00e3o correta&quot;,&quot;value&quot;:&quot;exemplo-de-separa\u00e7\u00e3o-correta&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.2 Hardware Abstraction Layer (HAL) como Padr\u00e3o&quot;,&quot;value&quot;:&quot;52-hardware-abstraction-layer-hal-como-padr\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.3 Service Layer (Camada de Servi\u00e7os)&quot;,&quot;value&quot;:&quot;53-service-layer-camada-de-servi\u00e7os&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo: Servi\u00e7o de UART com Gatekeeper&quot;,&quot;value&quot;:&quot;exemplo-servi\u00e7o-de-uart-com-gatekeeper&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.4 Active Object Pattern&quot;,&quot;value&quot;:&quot;54-active-object-pattern&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Estrutura conceitual&quot;,&quot;value&quot;:&quot;estrutura-conceitual&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo simplificado&quot;,&quot;value&quot;:&quot;exemplo-simplificado&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.5 Deferred Interrupt Processing&quot;,&quot;value&quot;:&quot;55-deferred-interrupt-processing&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Padr\u00f5es de Inicializa\u00e7\u00e3o, Startup Sequencial e Monitoramento de Sa\u00fade&quot;,&quot;value&quot;:&quot;padr\u00f5es-de-inicializa\u00e7\u00e3o-startup-sequencial-e-monitoramento-de-sa\u00fade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.1 Startup Task (Task de Inicializa\u00e7\u00e3o)&quot;,&quot;value&quot;:&quot;61-startup-task-task-de-inicializa\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Estrutura t\u00edpica&quot;,&quot;value&quot;:&quot;estrutura-t\u00edpica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.2 Init Sequencing (Sequenciamento de Inicializa\u00e7\u00e3o)&quot;,&quot;value&quot;:&quot;62-init-sequencing-sequenciamento-de-inicializa\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo com Event Groups&quot;,&quot;value&quot;:&quot;exemplo-com-event-groups&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.3 Dependency Ordering Pattern&quot;,&quot;value&quot;:&quot;63-dependency-ordering-pattern&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo conceitual&quot;,&quot;value&quot;:&quot;exemplo-conceitual&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.4 Health Monitoring Pattern (Monitoramento de Sa\u00fade)&quot;,&quot;value&quot;:&quot;64-health-monitoring-pattern-monitoramento-de-sa\u00fade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Estrutura b\u00e1sica&quot;,&quot;value&quot;:&quot;estrutura-b\u00e1sica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.5 Fail-Safe State Pattern&quot;,&quot;value&quot;:&quot;65-fail-safe-state-pattern&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6.6 Warm Restart vs Cold Restart&quot;,&quot;value&quot;:&quot;66-warm-restart-vs-cold-restart&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;S\u00edntese Geral e Checklist Arquitetural de Padr\u00f5es para FreeRTOS&quot;,&quot;value&quot;:&quot;s\u00edntese-geral-e-checklist-arquitetural-de-padr\u00f5es-para-freertos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.1 Mapa Conceitual dos Padr\u00f5es em RTOS&quot;,&quot;value&quot;:&quot;71-mapa-conceitual-dos-padr\u00f5es-em-rtos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\ud83d\udd39 Estrutura B\u00e1sica&quot;,&quot;value&quot;:&quot;\ud83d\udd39-estrutura-b\u00e1sica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\ud83d\udd39 Comunica\u00e7\u00e3o e Sincroniza\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;\ud83d\udd39-comunica\u00e7\u00e3o-e-sincroniza\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\ud83d\udd39 Prote\u00e7\u00e3o de Recursos&quot;,&quot;value&quot;:&quot;\ud83d\udd39-prote\u00e7\u00e3o-de-recursos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\ud83d\udd39 Controle de Comportamento&quot;,&quot;value&quot;:&quot;\ud83d\udd39-controle-de-comportamento&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\ud83d\udd39 Arquitetura Interna&quot;,&quot;value&quot;:&quot;\ud83d\udd39-arquitetura-interna&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\ud83d\udd39 Inicializa\u00e7\u00e3o e Robustez&quot;,&quot;value&quot;:&quot;\ud83d\udd39-inicializa\u00e7\u00e3o-e-robustez&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.2 Checklist Arquitetural para Projetos com FreeRTOS&quot;,&quot;value&quot;:&quot;72-checklist-arquitetural-para-projetos-com-freertos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\u2714\ufe0f Estrutura&quot;,&quot;value&quot;:&quot;\u2714\ufe0f-estrutura&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\u2714\ufe0f Comunica\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;\u2714\ufe0f-comunica\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\u2714\ufe0f Prote\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;\u2714\ufe0f-prote\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\u2714\ufe0f Estados e Modos&quot;,&quot;value&quot;:&quot;\u2714\ufe0f-estados-e-modos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\u2714\ufe0f Arquitetura&quot;,&quot;value&quot;:&quot;\u2714\ufe0f-arquitetura&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;\u2714\ufe0f Inicializa\u00e7\u00e3o e Sa\u00fade&quot;,&quot;value&quot;:&quot;\u2714\ufe0f-inicializa\u00e7\u00e3o-e-sa\u00fade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7.3 Erros Cl\u00e1ssicos que Esses Padr\u00f5es Evitam&quot;,&quot;value&quot;:&quot;73-erros-cl\u00e1ssicos-que-esses-padr\u00f5es-evitam&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Conclus\u00e3o&quot;,&quot;value&quot;:&quot;conclus\u00e3o&quot;,&quot;isDelete&quot;:false}]" data-smooth="true" data-top-offset=""><div class="eb-toc__list-wrap"><ul class='eb-toc__list'><li><a href="#eb-table-content-0">Introdução geral</a><li><a href="#eb-table-content-1">Padrões Fundamentais de Estruturação de Tarefas</a><ul class='eb-toc__list'><li><a href="#11-superloop-tasks-incremental-rtos-adoption">1.1 Superloop + Tasks (Incremental RTOS Adoption)</a><li><a href="#12-one-task-per-responsibility-uma-tarefa-por-responsabilidade">1.2 One Task Per Responsibility (Uma tarefa por responsabilidade)</a><li><a href="#13-cyclic-executive-com-rtos">1.3 Cyclic Executive com RTOS</a><li><a href="#eb-table-content-5">1.4 Idle Task como Padrão Arquitetural</a></li></ul><li><a href="#eb-table-content-6">Padrões de Comunicação e Sincronização em RTOS</a><ul class='eb-toc__list'><li><a href="#21-event-queue-fila-de-eventos">2.1 Event Queue (Fila de Eventos)</a><li><a href="#eb-table-content-8">2.2 Mailbox (Fila de Mensagem Única)</a><li><a href="#23-publishsubscribe-pub-sub-com-freertos">2.3 Publish–Subscribe (Pub-Sub) com FreeRTOS</a><li><a href="#eb-table-content-10">2.4 Task Notification como Padrão de Sinalização Rápida</a><li><a href="#25-stream-buffer-e-message-buffer">2.5 Stream Buffer e Message Buffer</a><ul class='eb-toc__list'><li><a href="#stream-buffer">Stream Buffer</a><li><a href="#message-buffer">Message Buffer</a></li></ul></li></ul><li><a href="#eb-table-content-14">Padrões de Exclusão Mútua, Proteção de Recursos e Prevenção de Deadlock</a><ul class='eb-toc__list'><li><a href="#31-mutex-com-priority-inheritance">3.1 Mutex com Priority Inheritance</a><li><a href="#eb-table-content-16">3.2 Gatekeeper Task (Padrão Fundamental em FreeRTOS)</a><li><a href="#eb-table-content-17">3.3 Critical Section (Uso cirúrgico)</a><li><a href="#eb-table-content-18">3.4 Read–Modify–Write protegido (Anti-pattern clássico)</a><li><a href="#eb-table-content-19">3.5 Event Groups como Padrão de Coordenação</a><li><a href="#eb-table-content-20">3.6 Deadlock Avoidance (Padrões de Prevenção)</a></li></ul><li><a href="#eb-table-content-21">Padrões de Controle de Fluxo, Estados e Recuperação em RTOS</a><ul class='eb-toc__list'><li><a href="#eb-table-content-22">4.1 State Machine (Máquina de Estados Finita – FSM)</a><ul class='eb-toc__list'><li><a href="#eb-table-content-23">Estrutura básica</a><li><a href="#eb-table-content-24">Execução da máquina de estados (task dedicada)</a></li></ul><li><a href="#42-hierarchical-state-machine-hsm">4.2 Hierarchical State Machine (HSM)</a><ul class='eb-toc__list'><li><a href="#exemplo-conceitual">Exemplo conceitual</a><li><a href="#eb-table-content-27">Implementação simplificada em C</a></li></ul><li><a href="#43-mode-manager-gerenciador-de-modos">4.3 Mode Manager (Gerenciador de Modos)</a><ul class='eb-toc__list'><li><a href="#eb-table-content-29">Modos típicos</a><li><a href="#mode-manager">Mode Manager</a><li><a href="#uso-pelas-tarefas">Uso pelas tarefas</a></li></ul><li><a href="#eb-table-content-32">4.4 Recovery Pattern (Padrão de Recuperação)</a><ul class='eb-toc__list'><li><a href="#eb-table-content-33">Exemplo de recuperação controlada</a></li></ul><li><a href="#eb-table-content-34">4.5 Watchdog como Padrão Arquitetural</a></li></ul><li><a href="#eb-table-content-35">Padrões de Arquitetura em Camadas, Drivers e Serviços em RTOS</a><ul class='eb-toc__list'><li><a href="#51-layered-architecture-arquitetura-em-camadas">5.1 Layered Architecture (Arquitetura em Camadas)</a><ul class='eb-toc__list'><li><a href="#eb-table-content-37">Camadas típicas em FreeRTOS</a><li><a href="#eb-table-content-38">Exemplo de separação correta</a></li></ul><li><a href="#eb-table-content-39">5.2 Hardware Abstraction Layer (HAL) como Padrão</a><li><a href="#eb-table-content-40">5.3 Service Layer (Camada de Serviços)</a><ul class='eb-toc__list'><li><a href="#eb-table-content-41">Exemplo: Serviço de UART com Gatekeeper</a></li></ul><li><a href="#54-active-object-pattern">5.4 Active Object Pattern</a><ul class='eb-toc__list'><li><a href="#estrutura-conceitual">Estrutura conceitual</a><li><a href="#exemplo-simplificado">Exemplo simplificado</a></li></ul><li><a href="#55-deferred-interrupt-processing">5.5 Deferred Interrupt Processing</a></li></ul><li><a href="#eb-table-content-46">Padrões de Inicialização, Startup Sequencial e Monitoramento de Saúde</a><ul class='eb-toc__list'><li><a href="#eb-table-content-47">6.1 Startup Task (Task de Inicialização)</a><ul class='eb-toc__list'><li><a href="#eb-table-content-48">Estrutura típica</a></li></ul><li><a href="#eb-table-content-49">6.2 Init Sequencing (Sequenciamento de Inicialização)</a><ul class='eb-toc__list'><li><a href="#exemplo-com-event-groups">Exemplo com Event Groups</a></li></ul><li><a href="#63-dependency-ordering-pattern">6.3 Dependency Ordering Pattern</a><ul class='eb-toc__list'><li><a href="#exemplo-conceitual">Exemplo conceitual</a></li></ul><li><a href="#eb-table-content-53">6.4 Health Monitoring Pattern (Monitoramento de Saúde)</a><ul class='eb-toc__list'><li><a href="#eb-table-content-54">Estrutura básica</a></li></ul><li><a href="#65-fail-safe-state-pattern">6.5 Fail-Safe State Pattern</a><li><a href="#66-warm-restart-vs-cold-restart">6.6 Warm Restart vs Cold Restart</a></li></ul><li><a href="#eb-table-content-57">Síntese Geral e Checklist Arquitetural de Padrões para FreeRTOS</a><ul class='eb-toc__list'><li><a href="#eb-table-content-58">7.1 Mapa Conceitual dos Padrões em RTOS</a><li><a href="#eb-table-content-59"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Estrutura Básica</a><li><a href="#eb-table-content-60"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Comunicação e Sincronização</a><li><a href="#eb-table-content-61"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Proteção de Recursos</a><li><a href="#eb-table-content-62"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Controle de Comportamento</a><li><a href="#eb-table-content-63"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Arquitetura Interna</a><li><a href="#eb-table-content-64"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f539.png" alt="🔹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Inicialização e Robustez</a><li><a href="#72-checklist-arquitetural-para-projetos-com-freertos">7.2 Checklist Arquitetural para Projetos com FreeRTOS</a><ul class='eb-toc__list'><li><a href="#eb-table-content-66"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Estrutura</a><li><a href="#eb-table-content-67"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Comunicação</a><li><a href="#eb-table-content-68"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Proteção</a><li><a href="#eb-table-content-69"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Estados e Modos</a><li><a href="#eb-table-content-70"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Arquitetura</a><li><a href="#eb-table-content-71"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Inicialização e Saúde</a></li></ul><li><a href="#eb-table-content-72">7.3 Erros Clássicos que Esses Padrões Evitam</a><li><a href="#eb-table-content-73">Conclusão</a></li></ul></ul></div></div></div></div></div>


<h2 class="wp-block-heading">Introdução geral</h2>



<p>Em sistemas embarcados com RTOS, especialmente em aplicações industriais, automotivas e IoT crítico, o maior desafio não é apenas “fazer funcionar”, mas <strong>manter previsibilidade temporal, escalabilidade e manutenibilidade</strong>. É nesse ponto que os <strong>padrões de projeto aplicados a RTOS</strong> se tornam fundamentais.</p>



<p>Diferente de aplicações desktop, padrões em RTOS precisam respeitar <strong>restrições de tempo real</strong>, <strong>uso determinístico de memória</strong>, <strong>prioridades</strong>, <strong>latência de interrupções</strong> e <strong>sincronização segura entre contexto de ISR e tarefas</strong>. Muitos padrões clássicos de software são adaptados ou reinterpretados nesse contexto.</p>



<p>Neste artigo, começaremos pelos <strong>padrões estruturais básicos de tarefas</strong>, que formam a fundação de praticamente qualquer sistema com FreeRTOS.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Padrões Fundamentais de Estruturação de Tarefas</h2>



<h3 class="wp-block-heading">1.1 Superloop + Tasks (Incremental RTOS Adoption)</h3>



<p><strong>Problema resolvido:</strong><br>Projetos legados em superloop (<code>while(1)</code>) tornam-se difíceis de manter à medida que crescem. Migrar tudo de uma vez para RTOS é arriscado.</p>



<p><strong>Ideia do padrão:</strong><br>Manter o superloop como uma <strong>tarefa principal</strong>, introduzindo gradualmente novas tarefas RTOS.</p>



<p><strong>Quando usar:</strong></p>



<ul class="wp-block-list">
<li>Migração de firmware bare-metal para FreeRTOS</li>



<li>Sistemas simples que estão crescendo</li>



<li>Prototipação controlada</li>
</ul>



<p><strong>Estrutura típica:</strong></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void LegacySuperloopTask(void *pvParameters)
{
    for (;;)
    {
        ReadSensors();
        ProcessData();
        UpdateOutputs();

        vTaskDelay(pdMS_TO_TICKS(10));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">LegacySuperloopTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ReadSensors</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ProcessData</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">UpdateOutputs</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelay</span><span style="color: #D8DEE9FF">(</span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">10</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>xTaskCreate(
    LegacySuperloopTask,
    "Legacy",
    1024,
    NULL,
    tskIDLE_PRIORITY + 1,
    NULL
);
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #88C0D0">xTaskCreate</span><span style="color: #D8DEE9FF">(</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">LegacySuperloopTask</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">Legacy</span><span style="color: #ECEFF4">&quot;</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #B48EAD">1024</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">NULL</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">tskIDLE_PRIORITY</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">+</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">1</span><span style="color: #ECEFF4">,</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">NULL</span></span>
<span class="line"><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Baixo risco na migração</li>



<li>Preserva código validado</li>



<li>Facilita testes incrementais</li>
</ul>



<p><strong>Risco comum:</strong><br>Transformar essa tarefa em um “monstro” que ignora o espírito do RTOS. Este padrão <strong>deve ser transitório</strong>, não permanente.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">1.2 One Task Per Responsibility (Uma tarefa por responsabilidade)</h3>



<p><strong>Problema resolvido:</strong><br>Tarefas que fazem “de tudo” dificultam análise temporal, debugging e escalonamento.</p>



<p><strong>Ideia do padrão:</strong><br>Cada tarefa possui <strong>uma única responsabilidade funcional bem definida</strong>.</p>



<p><strong>Exemplo típico em FreeRTOS:</strong></p>



<ul class="wp-block-list">
<li>Task de aquisição</li>



<li>Task de processamento</li>



<li>Task de comunicação</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void SensorTask(void *pvParameters)
{
    for (;;)
    {
        ReadADC();
        xTaskNotify(ProcessTaskHandle, 0, eNoAction);
        vTaskDelay(pdMS_TO_TICKS(5));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">SensorTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ReadADC</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">xTaskNotify</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">ProcessTaskHandle</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">eNoAction</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelay</span><span style="color: #D8DEE9FF">(</span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">5</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void ProcessTask(void *pvParameters)
{
    for (;;)
    {
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
        FilterData();
        ComputeResults();
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">ProcessTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ulTaskNotifyTake</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">pdTRUE</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">portMAX_DELAY</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">FilterData</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">ComputeResults</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Facilita análise de prioridades</li>



<li>Reduz acoplamento</li>



<li>Favorece paralelismo real</li>
</ul>



<p><strong>Boas práticas:</strong></p>



<ul class="wp-block-list">
<li>Nomear tarefas claramente</li>



<li>Documentar WCET (Worst Case Execution Time)</li>



<li>Evitar bloqueios longos</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">1.3 Cyclic Executive com RTOS</h3>



<p><strong>Problema resolvido:</strong><br>Algumas aplicações precisam de <strong>periodicidade rígida</strong>, mesmo usando RTOS.</p>



<p><strong>Ideia do padrão:</strong><br>Uma tarefa de alta prioridade atua como <strong>orquestrador temporal</strong>, liberando outras tarefas de forma cíclica.</p>



<p><strong>Estrutura conceitual:</strong></p>



<ul class="wp-block-list">
<li>Task cíclica principal</li>



<li>Subtarefas acionadas por notificações</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void CyclicTask(void *pvParameters)
{
    TickType_t xLastWakeTime = xTaskGetTickCount();

    for (;;)
    {
        xTaskNotify(TaskAHandle, 0, eNoAction);
        xTaskNotify(TaskBHandle, 0, eNoAction);

        vTaskDelayUntil(&amp;xLastWakeTime, pdMS_TO_TICKS(10));
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">CyclicTask</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">*</span><span style="color: #D8DEE9">pvParameters</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">TickType_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">xLastWakeTime</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">xTaskGetTickCount</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">for</span><span style="color: #D8DEE9FF"> (</span><span style="color: #81A1C1">;;</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">xTaskNotify</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">TaskAHandle</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">eNoAction</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">xTaskNotify</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">TaskBHandle</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">eNoAction</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">vTaskDelayUntil</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">&amp;</span><span style="color: #D8DEE9">xLastWakeTime</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">pdMS_TO_TICKS</span><span style="color: #D8DEE9FF">(</span><span style="color: #B48EAD">10</span><span style="color: #D8DEE9FF">))</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Vantagens:</strong></p>



<ul class="wp-block-list">
<li>Controle temporal explícito</li>



<li>Previsibilidade elevada</li>



<li>Útil em controle e automação</li>
</ul>



<p><strong>Limitação:</strong><br>Menos flexível que arquiteturas puramente orientadas a eventos.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">1.4 Idle Task como Padrão Arquitetural</h3>



<p><strong>Problema resolvido:</strong><br>Desperdício de CPU e energia quando o sistema está ocioso.</p>



<p><strong>Ideia do padrão:</strong><br>Usar a <strong>Idle Task</strong> como ponto central para:</p>



<ul class="wp-block-list">
<li>Economia de energia</li>



<li>Limpeza de recursos</li>



<li>Instrumentação</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>void vApplicationIdleHook(void)
{
    EnterLowPowerMode();
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">vApplicationIdleHook</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF">)</span></span>
<span class="line"><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">EnterLowPowerMode</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p><strong>Uso típico:</strong></p>



<ul class="wp-block-list">
<li><code>__WFI()</code> em Cortex-M</li>



<li>Estatísticas de CPU</li>



<li>Monitoramento de heap</li>
</ul>



<p><strong>Importante:</strong><br>Nunca bloquear, nunca usar delays e nunca acessar recursos não protegidos.</p><p>The post <a href="https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos/">Padrões de Projeto Aplicados a RTOS (FreeRTOS)</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://mcu.tec.br/rtos/padroes-de-projeto-aplicados-a-rtos-freertos/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1198</post-id>	</item>
		<item>
		<title>Máquina de Estados em Sistemas Embarcados com FreeRTOS: Projeto Cíclico, Recuperável e Orientado a Eventos</title>
		<link>https://mcu.tec.br/algoritimos/maquina-de-estados-em-sistemas-embarcados-com-freertos-projeto-ciclico-recuperavel-e-orientado-a-eventos/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=maquina-de-estados-em-sistemas-embarcados-com-freertos-projeto-ciclico-recuperavel-e-orientado-a-eventos</link>
					<comments>https://mcu.tec.br/algoritimos/maquina-de-estados-em-sistemas-embarcados-com-freertos-projeto-ciclico-recuperavel-e-orientado-a-eventos/#respond</comments>
		
		<dc:creator><![CDATA[Carlos Delfino]]></dc:creator>
		<pubDate>Wed, 14 Jan 2026 13:51:20 +0000</pubDate>
				<category><![CDATA[Algoritimos]]></category>
		<category><![CDATA[Active Object]]></category>
		<category><![CDATA[arquitetura de firmware]]></category>
		<category><![CDATA[firmware robusto]]></category>
		<category><![CDATA[freertos]]></category>
		<category><![CDATA[FSM embarcado]]></category>
		<category><![CDATA[Gatekeeper]]></category>
		<category><![CDATA[máquina de estados]]></category>
		<category><![CDATA[padrões de projeto embarcados]]></category>
		<category><![CDATA[programação em C embarcado]]></category>
		<category><![CDATA[rtos]]></category>
		<category><![CDATA[sistemas embarcados]]></category>
		<guid isPermaLink="false">https://mcu.tec.br/?p=1194</guid>

					<description><![CDATA[<p>Aprenda como projetar Máquinas de Estados robustas para sistemas embarcados usando FreeRTOS. Este artigo apresenta uma abordagem prática e profissional para construir firmware cíclico e recuperável, utilizando duas threads cooperativas, filas de mensagens e eventos tipados. São explorados padrões de projeto clássicos para sistemas embarcados, como Active Object, Gatekeeper, State Pattern, Supervisor e Error Containment, com exemplos completos em C, incluindo tratamento de timeouts, retentativas com backoff, correlação de requisições e estados seguros. O conteúdo é voltado a desenvolvedores que desejam sair do superloop tradicional e construir sistemas previsíveis, escaláveis e fáceis de manter em ambientes reais de produção.</p>
<p>The post <a href="https://mcu.tec.br/algoritimos/maquina-de-estados-em-sistemas-embarcados-com-freertos-projeto-ciclico-recuperavel-e-orientado-a-eventos/">Máquina de Estados em Sistemas Embarcados com FreeRTOS: Projeto Cíclico, Recuperável e Orientado a Eventos</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></description>
										<content:encoded><![CDATA[<div class="root-eb-toc-y4egp wp-block-essential-blocks-table-of-contents"><div class="eb-parent-wrapper eb-parent-eb-toc-y4egp "><div class="eb-toc-container eb-toc-y4egp  eb-toc-is-not-sticky eb-toc-not-collapsible eb-toc-initially-not-collapsed eb-toc-scrollToTop style-1 list-style-none" data-scroll-top="false" data-scroll-top-icon="fas fa-angle-up" data-collapsible="false" data-sticky-hide-mobile="false" data-sticky="false" data-scroll-target="scroll_to_toc" data-copy-link="false" data-editor-type="" data-hide-desktop="false" data-hide-tab="false" data-hide-mobile="false" data-itemCollapsed="false" data-highlight-scroll="false"><div class="eb-toc-header"><h2 class="eb-toc-title">Table of Contents</h2></div><div class="eb-toc-wrapper " data-headers="[{&quot;level&quot;:2,&quot;content&quot;:&quot;Vis\u00e3o Geral: por que M\u00e1quina de Estados em Firmware \u201cde verdade\u201d&quot;,&quot;text&quot;:&quot;Vis\u00e3o Geral: por que M\u00e1quina de Estados em Firmware \u201cde verdade\u201d&quot;,&quot;link&quot;:&quot;eb-table-content-0&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Estrutura do projeto FreeRTOS: tipos, filas e as duas tasks&quot;,&quot;text&quot;:&quot;Estrutura do projeto FreeRTOS: tipos, filas e as duas tasks&quot;,&quot;link&quot;:&quot;estrutura-do-projeto-freertos-tipos-filas-e-as-duas-tasks&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Motor da FSM: transi\u00e7\u00f5es, deadlines, retentativas e estado seguro&quot;,&quot;text&quot;:&quot;Motor da FSM: transi\u00e7\u00f5es, deadlines, retentativas e estado seguro&quot;,&quot;link&quot;:&quot;eb-table-content-2&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;O que foi garantido aqui (na pr\u00e1tica)&quot;,&quot;text&quot;:&quot;O que foi garantido aqui (na pr\u00e1tica)&quot;,&quot;link&quot;:&quot;eb-table-content-3&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Tornando \u201cprodu\u00e7\u00e3o\u201d: entry-actions, correla\u00e7\u00e3o request\/response e boas vs m\u00e1s escolhas&quot;,&quot;text&quot;:&quot;Tornando \u201cprodu\u00e7\u00e3o\u201d: entry-actions, correla\u00e7\u00e3o request\/response e boas vs m\u00e1s escolhas&quot;,&quot;link&quot;:&quot;eb-table-content-4&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.1 Entry-actions: rodar \u201cuma vez\u201d ao entrar no estado&quot;,&quot;text&quot;:&quot;5.1 Entry-actions: rodar \u201cuma vez\u201d ao entrar no estado&quot;,&quot;link&quot;:&quot;51-entry-actions-rodar-uma-vez-ao-entrar-no-estado&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.2 Correla\u00e7\u00e3o por req_id: evitando consumir resposta errada&quot;,&quot;text&quot;:&quot;5.2 Correla\u00e7\u00e3o por req_id: evitando consumir resposta errada&quot;,&quot;link&quot;:&quot;eb-table-content-6&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.3 Boa escolha vs m\u00e1 escolha (na pr\u00e1tica)&quot;,&quot;text&quot;:&quot;5.3 Boa escolha vs m\u00e1 escolha (na pr\u00e1tica)&quot;,&quot;link&quot;:&quot;eb-table-content-7&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;5.4 Padr\u00f5es expl\u00edcitos que estamos aplicando aqui&quot;,&quot;text&quot;:&quot;5.4 Padr\u00f5es expl\u00edcitos que estamos aplicando aqui&quot;,&quot;link&quot;:&quot;eb-table-content-8&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Exemplo consolidado (mais \u201creal\u201d): correla\u00e7\u00e3o req_id, entry-actions, e ISR \u2192 fila&quot;,&quot;text&quot;:&quot;Exemplo consolidado (mais \u201creal\u201d): correla\u00e7\u00e3o req_id, entry-actions, e ISR \u2192 fila&quot;,&quot;link&quot;:&quot;eb-table-content-9&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;O que esse \u201cconsolidado\u201d resolve que o exemplo anterior n\u00e3o garantia t\u00e3o bem&quot;,&quot;text&quot;:&quot;O que esse \u201cconsolidado\u201d resolve que o exemplo anterior n\u00e3o garantia t\u00e3o bem&quot;,&quot;link&quot;:&quot;eb-table-content-10&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Padr\u00f5es de projeto embarcados aplicados: nomes, pap\u00e9is e como encaixar no seu firmware&quot;,&quot;text&quot;:&quot;Padr\u00f5es de projeto embarcados aplicados: nomes, pap\u00e9is e como encaixar no seu firmware&quot;,&quot;link&quot;:&quot;eb-table-content-11&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Active Object (Objeto Ativo)&quot;,&quot;text&quot;:&quot;Active Object (Objeto Ativo)&quot;,&quot;link&quot;:&quot;active-object-objeto-ativo&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Message Queue \/ Event-Driven Boundary (Fronteira por Mensagens)&quot;,&quot;text&quot;:&quot;Message Queue \/ Event-Driven Boundary (Fronteira por Mensagens)&quot;,&quot;link&quot;:&quot;message-queue-event-driven-boundary-fronteira-por-mensagens&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Gatekeeper (Dono do Perif\u00e9rico)&quot;,&quot;text&quot;:&quot;Gatekeeper (Dono do Perif\u00e9rico)&quot;,&quot;link&quot;:&quot;eb-table-content-14&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;State Pattern (varia\u00e7\u00e3o em C) e disciplina de entry\/actions&quot;,&quot;text&quot;:&quot;State Pattern (varia\u00e7\u00e3o em C) e disciplina de entry\/actions&quot;,&quot;link&quot;:&quot;eb-table-content-15&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Supervisor (FSM acima de FSMs)&quot;,&quot;text&quot;:&quot;Supervisor (FSM acima de FSMs)&quot;,&quot;link&quot;:&quot;supervisor-fsm-acima-de-fsms&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Watchdog l\u00f3gico (mais \u00fatil que s\u00f3 \u201cchutar o c\u00e3o\u201d)&quot;,&quot;text&quot;:&quot;Watchdog l\u00f3gico (mais \u00fatil que s\u00f3 \u201cchutar o c\u00e3o\u201d)&quot;,&quot;link&quot;:&quot;eb-table-content-17&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Error Containment \/ Recovery Block&quot;,&quot;text&quot;:&quot;Error Containment \/ Recovery Block&quot;,&quot;link&quot;:&quot;error-containment-recovery-block&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Como isso vira uma arquitetura real (sem \u201cexplodir\u201d em complexidade)&quot;,&quot;text&quot;:&quot;Como isso vira uma arquitetura real (sem \u201cexplodir\u201d em complexidade)&quot;,&quot;link&quot;:&quot;como-isso-vira-uma-arquitetura-real-sem-explodir-em-complexidade&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Checklist pr\u00e1tico, m\u00e9tricas de campo e extens\u00f5es naturais da FSM&quot;,&quot;text&quot;:&quot;Checklist pr\u00e1tico, m\u00e9tricas de campo e extens\u00f5es naturais da FSM&quot;,&quot;link&quot;:&quot;eb-table-content-20&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.1 Checklist de boas pr\u00e1ticas (FSM + FreeRTOS)&quot;,&quot;text&quot;:&quot;8.1 Checklist de boas pr\u00e1ticas (FSM + FreeRTOS)&quot;,&quot;link&quot;:&quot;eb-table-content-21&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.2 O que medir e logar (para depura\u00e7\u00e3o em campo)&quot;,&quot;text&quot;:&quot;8.2 O que medir e logar (para depura\u00e7\u00e3o em campo)&quot;,&quot;link&quot;:&quot;eb-table-content-22&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.3 Watchdog l\u00f3gico: como integrar de forma correta&quot;,&quot;text&quot;:&quot;8.3 Watchdog l\u00f3gico: como integrar de forma correta&quot;,&quot;link&quot;:&quot;eb-table-content-23&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.4 Extens\u00f5es naturais (quando o sistema crescer)&quot;,&quot;text&quot;:&quot;8.4 Extens\u00f5es naturais (quando o sistema crescer)&quot;,&quot;link&quot;:&quot;eb-table-content-24&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;8.5 Ideia-chave para levar consigo&quot;,&quot;text&quot;:&quot;8.5 Ideia-chave para levar consigo&quot;,&quot;link&quot;:&quot;85-ideia-chave-para-levar-consigo&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;S\u00edntese t\u00e9cnica do artigo&quot;,&quot;text&quot;:&quot;S\u00edntese t\u00e9cnica do artigo&quot;,&quot;link&quot;:&quot;eb-table-content-26&quot;}]" data-visible="[true,true,true,true,true,true]" data-delete-headers="[{&quot;label&quot;:&quot;Vis\u00e3o Geral: por que M\u00e1quina de Estados em Firmware \u201cde verdade\u201d&quot;,&quot;value&quot;:&quot;vis\u00e3o-geral-por-que-m\u00e1quina-de-estados-em-firmware-de-verdade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Estrutura do projeto FreeRTOS: tipos, filas e as duas tasks&quot;,&quot;value&quot;:&quot;estrutura-do-projeto-freertos-tipos-filas-e-as-duas-tasks&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Motor da FSM: transi\u00e7\u00f5es, deadlines, retentativas e estado seguro&quot;,&quot;value&quot;:&quot;motor-da-fsm-transi\u00e7\u00f5es-deadlines-retentativas-e-estado-seguro&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;O que foi garantido aqui (na pr\u00e1tica)&quot;,&quot;value&quot;:&quot;o-que-foi-garantido-aqui-na-pr\u00e1tica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Tornando \u201cprodu\u00e7\u00e3o\u201d: entry-actions, correla\u00e7\u00e3o request\/response e boas vs m\u00e1s escolhas&quot;,&quot;value&quot;:&quot;tornando-produ\u00e7\u00e3o-entry-actions-correla\u00e7\u00e3o-requestresponse-e-boas-vs-m\u00e1s-escolhas&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.1 Entry-actions: rodar \u201cuma vez\u201d ao entrar no estado&quot;,&quot;value&quot;:&quot;51-entry-actions-rodar-uma-vez-ao-entrar-no-estado&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.2 Correla\u00e7\u00e3o por req_id: evitando consumir resposta errada&quot;,&quot;value&quot;:&quot;52-correla\u00e7\u00e3o-por-req_id-evitando-consumir-resposta-errada&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.3 Boa escolha vs m\u00e1 escolha (na pr\u00e1tica)&quot;,&quot;value&quot;:&quot;53-boa-escolha-vs-m\u00e1-escolha-na-pr\u00e1tica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5.4 Padr\u00f5es expl\u00edcitos que estamos aplicando aqui&quot;,&quot;value&quot;:&quot;54-padr\u00f5es-expl\u00edcitos-que-estamos-aplicando-aqui&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo consolidado (mais \u201creal\u201d): correla\u00e7\u00e3o req_id, entry-actions, e ISR \u2192 fila&quot;,&quot;value&quot;:&quot;exemplo-consolidado-mais-real-correla\u00e7\u00e3o-req_id-entry-actions-e-isr-\u2192-fila&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;O que esse \u201cconsolidado\u201d resolve que o exemplo anterior n\u00e3o garantia t\u00e3o bem&quot;,&quot;value&quot;:&quot;o-que-esse-consolidado-resolve-que-o-exemplo-anterior-n\u00e3o-garantia-t\u00e3o-bem&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Padr\u00f5es de projeto embarcados aplicados: nomes, pap\u00e9is e como encaixar no seu firmware&quot;,&quot;value&quot;:&quot;padr\u00f5es-de-projeto-embarcados-aplicados-nomes-pap\u00e9is-e-como-encaixar-no-seu-firmware&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Active Object (Objeto Ativo)&quot;,&quot;value&quot;:&quot;active-object-objeto-ativo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Message Queue \/ Event-Driven Boundary (Fronteira por Mensagens)&quot;,&quot;value&quot;:&quot;message-queue-event-driven-boundary-fronteira-por-mensagens&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Gatekeeper (Dono do Perif\u00e9rico)&quot;,&quot;value&quot;:&quot;gatekeeper-dono-do-perif\u00e9rico&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;State Pattern (varia\u00e7\u00e3o em C) e disciplina de entry\/actions&quot;,&quot;value&quot;:&quot;state-pattern-varia\u00e7\u00e3o-em-c-e-disciplina-de-entryactions&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Supervisor (FSM acima de FSMs)&quot;,&quot;value&quot;:&quot;supervisor-fsm-acima-de-fsms&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Watchdog l\u00f3gico (mais \u00fatil que s\u00f3 \u201cchutar o c\u00e3o\u201d)&quot;,&quot;value&quot;:&quot;watchdog-l\u00f3gico-mais-\u00fatil-que-s\u00f3-chutar-o-c\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Error Containment \/ Recovery Block&quot;,&quot;value&quot;:&quot;error-containment-recovery-block&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Como isso vira uma arquitetura real (sem \u201cexplodir\u201d em complexidade)&quot;,&quot;value&quot;:&quot;como-isso-vira-uma-arquitetura-real-sem-explodir-em-complexidade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Checklist pr\u00e1tico, m\u00e9tricas de campo e extens\u00f5es naturais da FSM&quot;,&quot;value&quot;:&quot;checklist-pr\u00e1tico-m\u00e9tricas-de-campo-e-extens\u00f5es-naturais-da-fsm&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.1 Checklist de boas pr\u00e1ticas (FSM + FreeRTOS)&quot;,&quot;value&quot;:&quot;81-checklist-de-boas-pr\u00e1ticas-fsm-freertos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.2 O que medir e logar (para depura\u00e7\u00e3o em campo)&quot;,&quot;value&quot;:&quot;82-o-que-medir-e-logar-para-depura\u00e7\u00e3o-em-campo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.3 Watchdog l\u00f3gico: como integrar de forma correta&quot;,&quot;value&quot;:&quot;83-watchdog-l\u00f3gico-como-integrar-de-forma-correta&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.4 Extens\u00f5es naturais (quando o sistema crescer)&quot;,&quot;value&quot;:&quot;84-extens\u00f5es-naturais-quando-o-sistema-crescer&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8.5 Ideia-chave para levar consigo&quot;,&quot;value&quot;:&quot;85-ideia-chave-para-levar-consigo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;S\u00edntese t\u00e9cnica do artigo&quot;,&quot;value&quot;:&quot;s\u00edntese-t\u00e9cnica-do-artigo&quot;,&quot;isDelete&quot;:false}]" data-smooth="true" data-top-offset=""><div class="eb-toc__list-wrap"><ul class='eb-toc__list'><li><a href="#eb-table-content-0">Visão Geral: por que Máquina de Estados em Firmware “de verdade”</a><li><a href="#estrutura-do-projeto-freertos-tipos-filas-e-as-duas-tasks">Estrutura do projeto FreeRTOS: tipos, filas e as duas tasks</a><li><a href="#eb-table-content-2">Motor da FSM: transições, deadlines, retentativas e estado seguro</a><ul class='eb-toc__list'><li><a href="#eb-table-content-3">O que foi garantido aqui (na prática)</a></li></ul><li><a href="#eb-table-content-4">Tornando “produção”: entry-actions, correlação request/response e boas vs más escolhas</a><ul class='eb-toc__list'><li><a href="#51-entry-actions-rodar-uma-vez-ao-entrar-no-estado">5.1 Entry-actions: rodar “uma vez” ao entrar no estado</a><li><a href="#eb-table-content-6">5.2 Correlação por req_id: evitando consumir resposta errada</a><li><a href="#eb-table-content-7">5.3 Boa escolha vs má escolha (na prática)</a><li><a href="#eb-table-content-8">5.4 Padrões explícitos que estamos aplicando aqui</a></li></ul><li><a href="#eb-table-content-9">Exemplo consolidado (mais “real”): correlação req_id, entry-actions, e ISR → fila</a><ul class='eb-toc__list'><li><a href="#eb-table-content-10">O que esse “consolidado” resolve que o exemplo anterior não garantia tão bem</a></li></ul><li><a href="#eb-table-content-11">Padrões de projeto embarcados aplicados: nomes, papéis e como encaixar no seu firmware</a><ul class='eb-toc__list'><li><a href="#active-object-objeto-ativo">Active Object (Objeto Ativo)</a><li><a href="#message-queue-event-driven-boundary-fronteira-por-mensagens">Message Queue / Event-Driven Boundary (Fronteira por Mensagens)</a><li><a href="#eb-table-content-14">Gatekeeper (Dono do Periférico)</a><li><a href="#eb-table-content-15">State Pattern (variação em C) e disciplina de entry/actions</a><li><a href="#supervisor-fsm-acima-de-fsms">Supervisor (FSM acima de FSMs)</a><li><a href="#eb-table-content-17">Watchdog lógico (mais útil que só “chutar o cão”)</a><li><a href="#error-containment-recovery-block">Error Containment / Recovery Block</a><li><a href="#como-isso-vira-uma-arquitetura-real-sem-explodir-em-complexidade">Como isso vira uma arquitetura real (sem “explodir” em complexidade)</a></li></ul><li><a href="#eb-table-content-20">Checklist prático, métricas de campo e extensões naturais da FSM</a><ul class='eb-toc__list'><li><a href="#eb-table-content-21">8.1 Checklist de boas práticas (FSM + FreeRTOS)</a><li><a href="#eb-table-content-22">8.2 O que medir e logar (para depuração em campo)</a><li><a href="#eb-table-content-23">8.3 Watchdog lógico: como integrar de forma correta</a><li><a href="#eb-table-content-24">8.4 Extensões naturais (quando o sistema crescer)</a><li><a href="#85-ideia-chave-para-levar-consigo">8.5 Ideia-chave para levar consigo</a><li><a href="#eb-table-content-26">Síntese técnica do artigo</a></li></ul></ul></div></div></div></div></div>


<h2 class="wp-block-heading">Visão Geral: por que Máquina de Estados em Firmware “de verdade”</h2>



<p>Em sistemas embarcados, a Máquina de Estados (Finite State Machine, FSM) não é só uma técnica de organização: ela é uma forma de <strong>tornar o comportamento previsível</strong>, rastreável e recuperável. Quando você está lidando com periféricos reais (UART, ADC, rádio, motores), o sistema não é “sequencial”; ele é <strong>reativo</strong>. Eventos chegam fora de hora, dados vêm incompletos, sensores falham, o usuário aperta botão no pior momento possível. A FSM é o mecanismo mais direto para modelar isso sem cair em “if-else infinito” ou em código que parece funcionar só em bancada.</p>



<p>O problema típico que vamos resolver neste artigo é este: <strong>como projetar um fluxo de controle cíclico</strong> (rodando continuamente) <strong>e recuperável</strong> (capaz de detectar falhas e voltar a um estado seguro) <strong>usando FreeRTOS</strong>, com <strong>duas threads</strong> cooperando: uma thread dona da lógica (controle por estados) e outra thread provedora de eventos/dados (E/S, aquisição, comunicação). A ideia é evitar dois extremos comuns: (1) uma thread monolítica que faz tudo e trava o sistema; (2) várias threads competindo por dados sem governança clara.</p>



<p>Para construir isso com rigor arquitetural, vamos “encaixar” a FSM dentro de padrões de projeto clássicos de tempo real: <strong>Active Object</strong> (um objeto/tarefa dona do seu próprio loop e fila), <strong>Message Queuing</strong> (fila de mensagens como fronteira limpa entre threads), e uma estratégia de <strong>ciclo + timeouts</strong> inspirada no raciocínio de padrões de concorrência e arquitetura para sistemas de tempo real (como discutido em catálogos de padrões para RTE systems). Na parte de robustez, vamos adicionar mecanismos de “recuperação” que não são mágicos: <strong>Watchdog lógico</strong>, <strong>Sanity Check</strong> (checagens de coerência), e uma política de <strong>degradação controlada</strong> (fallback para estado seguro).</p>



<p>No final, você terá um exemplo que dá para copiar para um projeto real: duas tasks do FreeRTOS, uma FSM explícita com transições bem definidas, eventos chegando por fila, timeouts, retentativas, e recuperação após erro — sem “gambiarras” como delays espalhados ou variáveis globais sem proteção.</p><p>The post <a href="https://mcu.tec.br/algoritimos/maquina-de-estados-em-sistemas-embarcados-com-freertos-projeto-ciclico-recuperavel-e-orientado-a-eventos/">Máquina de Estados em Sistemas Embarcados com FreeRTOS: Projeto Cíclico, Recuperável e Orientado a Eventos</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://mcu.tec.br/algoritimos/maquina-de-estados-em-sistemas-embarcados-com-freertos-projeto-ciclico-recuperavel-e-orientado-a-eventos/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1194</post-id>	</item>
		<item>
		<title>UART não é Porta Serial: Como Projetar Protocolos Robustos em Sistemas Embarcados</title>
		<link>https://mcu.tec.br/protoclolos/uart-serial/uart-nao-e-porta-serial-como-projetar-protocolos-robustos-em-sistemas-embarcados/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=uart-nao-e-porta-serial-como-projetar-protocolos-robustos-em-sistemas-embarcados</link>
					<comments>https://mcu.tec.br/protoclolos/uart-serial/uart-nao-e-porta-serial-como-projetar-protocolos-robustos-em-sistemas-embarcados/#respond</comments>
		
		<dc:creator><![CDATA[Carlos Delfino]]></dc:creator>
		<pubDate>Sun, 04 Jan 2026 00:42:48 +0000</pubDate>
				<category><![CDATA[UART (Serial)]]></category>
		<category><![CDATA[buffer UART]]></category>
		<category><![CDATA[comunicação embarcada]]></category>
		<category><![CDATA[comunicação serial]]></category>
		<category><![CDATA[CRC em UART]]></category>
		<category><![CDATA[engenharia de firmware]]></category>
		<category><![CDATA[firmware robusto]]></category>
		<category><![CDATA[framing UART]]></category>
		<category><![CDATA[FreeRTOS UART]]></category>
		<category><![CDATA[parsing defensivo]]></category>
		<category><![CDATA[protocolo UART]]></category>
		<category><![CDATA[protocolos binários]]></category>
		<category><![CDATA[ring buffer UART]]></category>
		<category><![CDATA[sistemas distribuídos embarcados]]></category>
		<category><![CDATA[sistemas embarcados]]></category>
		<category><![CDATA[UART]]></category>
		<category><![CDATA[USART]]></category>
		<category><![CDATA[Zephyr UART]]></category>
		<guid isPermaLink="false">https://mcu.tec.br/?p=1102</guid>

					<description><![CDATA[<p>UART é frequentemente tratada como uma simples porta serial para uso com printf e terminais, mas essa abordagem falha rapidamente em sistemas embarcados reais. Este artigo apresenta uma visão técnica e madura sobre UART como uma decisão de arquitetura, não como um periférico trivial. Ao longo do texto, discutimos bufferização correta, framing explícito, detecção de início de quadro, validação de integridade com CRC, parsing defensivo, recuperação de erros, perda de pacotes e re-sincronização. Com exemplos práticos em C comparando abordagens erradas e corretas, o artigo mostra como transformar UART em uma verdadeira camada de transporte para sistemas distribuídos embarcados, usados em ambientes industriais, robóticos e médicos. Ideal para engenheiros que desejam sair do nível de demos e construir firmware robusto, previsível e confiável para operação contínua em campo.</p>
<p>The post <a href="https://mcu.tec.br/protoclolos/uart-serial/uart-nao-e-porta-serial-como-projetar-protocolos-robustos-em-sistemas-embarcados/">UART não é Porta Serial: Como Projetar Protocolos Robustos em Sistemas Embarcados</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></description>
										<content:encoded><![CDATA[<div class="root-eb-toc-p8biy wp-block-essential-blocks-table-of-contents"><div class="eb-parent-wrapper eb-parent-eb-toc-p8biy "><div class="eb-toc-container eb-toc-p8biy  eb-toc-is-not-sticky eb-toc-not-collapsible eb-toc-initially-not-collapsed eb-toc-scrollToTop style-1 list-style-none" data-scroll-top="false" data-scroll-top-icon="fas fa-angle-up" data-collapsible="false" data-sticky-hide-mobile="false" data-sticky="false" data-scroll-target="scroll_to_toc" data-copy-link="false" data-editor-type="" data-hide-desktop="false" data-hide-tab="false" data-hide-mobile="false" data-itemCollapsed="false" data-highlight-scroll="false"><div class="eb-toc-header"><h2 class="eb-toc-title">Table of Contents</h2></div><div class="eb-toc-wrapper " data-headers="[{&quot;level&quot;:2,&quot;content&quot;:&quot;UART n\u00e3o \u00e9 uma porta: \u00e9 uma decis\u00e3o de arquitetura&quot;,&quot;text&quot;:&quot;UART n\u00e3o \u00e9 uma porta: \u00e9 uma decis\u00e3o de arquitetura&quot;,&quot;link&quot;:&quot;eb-table-content-0&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;O erro cl\u00e1ssico: UART como \u201clinha de printf\u201d&quot;,&quot;text&quot;:&quot;O erro cl\u00e1ssico: UART como \u201clinha de printf\u201d&quot;,&quot;link&quot;:&quot;eb-table-content-1&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;A mudan\u00e7a de mentalidade: UART como canal bruto&quot;,&quot;text&quot;:&quot;A mudan\u00e7a de mentalidade: UART como canal bruto&quot;,&quot;link&quot;:&quot;eb-table-content-2&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;UART como espelho do engenheiro&quot;,&quot;text&quot;:&quot;UART como espelho do engenheiro&quot;,&quot;link&quot;:&quot;uart-como-espelho-do-engenheiro&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Bufferiza\u00e7\u00e3o n\u00e3o \u00e9 opcional: \u00e9 o primeiro contrato com a realidade&quot;,&quot;text&quot;:&quot;Bufferiza\u00e7\u00e3o n\u00e3o \u00e9 opcional: \u00e9 o primeiro contrato com a realidade&quot;,&quot;link&quot;:&quot;eb-table-content-4&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;O erro cl\u00e1ssico: processar dados no contexto da interrup\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;O erro cl\u00e1ssico: processar dados no contexto da interrup\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-5&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;A abordagem correta: ISR m\u00ednima + buffer expl\u00edcito&quot;,&quot;text&quot;:&quot;A abordagem correta: ISR m\u00ednima + buffer expl\u00edcito&quot;,&quot;link&quot;:&quot;eb-table-content-6&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Consumir bytes \u00e9 diferente de interpretar mensagens&quot;,&quot;text&quot;:&quot;Consumir bytes \u00e9 diferente de interpretar mensagens&quot;,&quot;link&quot;:&quot;eb-table-content-7&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Bufferiza\u00e7\u00e3o como base para escalabilidade&quot;,&quot;text&quot;:&quot;Bufferiza\u00e7\u00e3o como base para escalabilidade&quot;,&quot;link&quot;:&quot;eb-table-content-8&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Um crit\u00e9rio simples de maturidade&quot;,&quot;text&quot;:&quot;Um crit\u00e9rio simples de maturidade&quot;,&quot;link&quot;:&quot;eb-table-content-9&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Framing: bytes sem fronteiras n\u00e3o s\u00e3o mensagens&quot;,&quot;text&quot;:&quot;Framing: bytes sem fronteiras n\u00e3o s\u00e3o mensagens&quot;,&quot;link&quot;:&quot;eb-table-content-10&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;O erro cl\u00e1ssico: confiar em delimitadores \u201chumanos\u201d&quot;,&quot;text&quot;:&quot;O erro cl\u00e1ssico: confiar em delimitadores \u201chumanos\u201d&quot;,&quot;link&quot;:&quot;eb-table-content-11&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Framing \u00e9 uma decis\u00e3o de protocolo, n\u00e3o de conveni\u00eancia&quot;,&quot;text&quot;:&quot;Framing \u00e9 uma decis\u00e3o de protocolo, n\u00e3o de conveni\u00eancia&quot;,&quot;link&quot;:&quot;eb-table-content-12&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Abordagem correta 1: marcador de in\u00edcio + comprimento&quot;,&quot;text&quot;:&quot;Abordagem correta 1: marcador de in\u00edcio + comprimento&quot;,&quot;link&quot;:&quot;eb-table-content-13&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Abordagem correta 2: delimitador com escape (SLIP-like)&quot;,&quot;text&quot;:&quot;Abordagem correta 2: delimitador com escape (SLIP-like)&quot;,&quot;link&quot;:&quot;abordagem-correta-2-delimitador-com-escape-slip-like&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Framing define se o sistema sobrevive ao erro&quot;,&quot;text&quot;:&quot;Framing define se o sistema sobrevive ao erro&quot;,&quot;link&quot;:&quot;framing-define-se-o-sistema-sobrevive-ao-erro&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Uma regra pr\u00e1tica&quot;,&quot;text&quot;:&quot;Uma regra pr\u00e1tica&quot;,&quot;link&quot;:&quot;eb-table-content-16&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Integridade e parsing defensivo: confiar em bytes \u00e9 ingenuidade&quot;,&quot;text&quot;:&quot;Integridade e parsing defensivo: confiar em bytes \u00e9 ingenuidade&quot;,&quot;link&quot;:&quot;eb-table-content-17&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;O erro cl\u00e1ssico: \u201cse chegou, \u00e9 v\u00e1lido\u201d&quot;,&quot;text&quot;:&quot;O erro cl\u00e1ssico: \u201cse chegou, \u00e9 v\u00e1lido\u201d&quot;,&quot;link&quot;:&quot;eb-table-content-18&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Integridade n\u00e3o \u00e9 luxo, \u00e9 requisito&quot;,&quot;text&quot;:&quot;Integridade n\u00e3o \u00e9 luxo, \u00e9 requisito&quot;,&quot;link&quot;:&quot;eb-table-content-19&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Exemplo correto: CRC expl\u00edcito no frame&quot;,&quot;text&quot;:&quot;Exemplo correto: CRC expl\u00edcito no frame&quot;,&quot;link&quot;:&quot;eb-table-content-20&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Parsing defensivo: nunca confie no emissor&quot;,&quot;text&quot;:&quot;Parsing defensivo: nunca confie no emissor&quot;,&quot;link&quot;:&quot;parsing-defensivo-nunca-confie-no-emissor&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;UART como superf\u00edcie de ataque (mesmo sem rede)&quot;,&quot;text&quot;:&quot;UART como superf\u00edcie de ataque (mesmo sem rede)&quot;,&quot;link&quot;:&quot;eb-table-content-22&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Regra de ouro&quot;,&quot;text&quot;:&quot;Regra de ouro&quot;,&quot;link&quot;:&quot;regra-de-ouro&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Recupera\u00e7\u00e3o, perda e por que sistemas reais n\u00e3o assumem perfei\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;Recupera\u00e7\u00e3o, perda e por que sistemas reais n\u00e3o assumem perfei\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-24&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;O erro cl\u00e1ssico: assumir que \u201cn\u00e3o acontece\u201d&quot;,&quot;text&quot;:&quot;O erro cl\u00e1ssico: assumir que \u201cn\u00e3o acontece\u201d&quot;,&quot;link&quot;:&quot;eb-table-content-25&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Sistemas reais assumem falha como condi\u00e7\u00e3o normal&quot;,&quot;text&quot;:&quot;Sistemas reais assumem falha como condi\u00e7\u00e3o normal&quot;,&quot;link&quot;:&quot;eb-table-content-26&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Timeouts: o rel\u00f3gio tamb\u00e9m \u00e9 parte do protocolo&quot;,&quot;text&quot;:&quot;Timeouts: o rel\u00f3gio tamb\u00e9m \u00e9 parte do protocolo&quot;,&quot;link&quot;:&quot;eb-table-content-27&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Re-sincroniza\u00e7\u00e3o expl\u00edcita&quot;,&quot;text&quot;:&quot;Re-sincroniza\u00e7\u00e3o expl\u00edcita&quot;,&quot;link&quot;:&quot;eb-table-content-28&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Perda de pacotes n\u00e3o \u00e9 exce\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;Perda de pacotes n\u00e3o \u00e9 exce\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-29&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;UART como transporte muda o jogo&quot;,&quot;text&quot;:&quot;UART como transporte muda o jogo&quot;,&quot;link&quot;:&quot;uart-como-transporte-muda-o-jogo&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;A frase que resume tudo&quot;,&quot;text&quot;:&quot;A frase que resume tudo&quot;,&quot;link&quot;:&quot;a-frase-que-resume-tudo&quot;},{&quot;level&quot;:1,&quot;content&quot;:&quot;Checklist de Boas Pr\u00e1ticas \u2014 UART como Camada de Transporte&quot;,&quot;text&quot;:&quot;Checklist de Boas Pr\u00e1ticas \u2014 UART como Camada de Transporte&quot;,&quot;link&quot;:&quot;eb-table-content-32&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;1. Modelo Mental e Arquitetura&quot;,&quot;text&quot;:&quot;1. Modelo Mental e Arquitetura&quot;,&quot;link&quot;:&quot;1-modelo-mental-e-arquitetura&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;2. Interrup\u00e7\u00f5es (ISR)&quot;,&quot;text&quot;:&quot;2. Interrup\u00e7\u00f5es (ISR)&quot;,&quot;link&quot;:&quot;eb-table-content-34&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;3. Bufferiza\u00e7\u00e3o&quot;,&quot;text&quot;:&quot;3. Bufferiza\u00e7\u00e3o&quot;,&quot;link&quot;:&quot;eb-table-content-35&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;4. Framing&quot;,&quot;text&quot;:&quot;4. Framing&quot;,&quot;link&quot;:&quot;4-framing&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;5. Integridade&quot;,&quot;text&quot;:&quot;5. Integridade&quot;,&quot;link&quot;:&quot;5-integridade&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;6. Parsing Defensivo&quot;,&quot;text&quot;:&quot;6. Parsing Defensivo&quot;,&quot;link&quot;:&quot;6-parsing-defensivo&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;7. Recupera\u00e7\u00e3o e Robustez&quot;,&quot;text&quot;:&quot;7. Recupera\u00e7\u00e3o e Robustez&quot;,&quot;link&quot;:&quot;eb-table-content-39&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;8. Perda, Repeti\u00e7\u00e3o e Ordem&quot;,&quot;text&quot;:&quot;8. Perda, Repeti\u00e7\u00e3o e Ordem&quot;,&quot;link&quot;:&quot;eb-table-content-40&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;9. Tempo e Determinismo&quot;,&quot;text&quot;:&quot;9. Tempo e Determinismo&quot;,&quot;link&quot;:&quot;9-tempo-e-determinismo&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;10. Seguran\u00e7a e Confiabilidade&quot;,&quot;text&quot;:&quot;10. Seguran\u00e7a e Confiabilidade&quot;,&quot;link&quot;:&quot;eb-table-content-42&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;11. Testabilidade&quot;,&quot;text&quot;:&quot;11. Testabilidade&quot;,&quot;link&quot;:&quot;11-testabilidade&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;12. Maturidade do Sistema&quot;,&quot;text&quot;:&quot;12. Maturidade do Sistema&quot;,&quot;link&quot;:&quot;12-maturidade-do-sistema&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Regra Final&quot;,&quot;text&quot;:&quot;Regra Final&quot;,&quot;link&quot;:&quot;regra-final&quot;}]" data-visible="[true,true,true,true,true,true]" data-delete-headers="[{&quot;label&quot;:&quot;UART n\u00e3o \u00e9 uma porta: \u00e9 uma decis\u00e3o de arquitetura&quot;,&quot;value&quot;:&quot;uart-n\u00e3o-\u00e9-uma-porta-\u00e9-uma-decis\u00e3o-de-arquitetura&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;O erro cl\u00e1ssico: UART como \u201clinha de printf\u201d&quot;,&quot;value&quot;:&quot;o-erro-cl\u00e1ssico-uart-como-linha-de-printf&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;A mudan\u00e7a de mentalidade: UART como canal bruto&quot;,&quot;value&quot;:&quot;a-mudan\u00e7a-de-mentalidade-uart-como-canal-bruto&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;UART como espelho do engenheiro&quot;,&quot;value&quot;:&quot;uart-como-espelho-do-engenheiro&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Bufferiza\u00e7\u00e3o n\u00e3o \u00e9 opcional: \u00e9 o primeiro contrato com a realidade&quot;,&quot;value&quot;:&quot;bufferiza\u00e7\u00e3o-n\u00e3o-\u00e9-opcional-\u00e9-o-primeiro-contrato-com-a-realidade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;O erro cl\u00e1ssico: processar dados no contexto da interrup\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;o-erro-cl\u00e1ssico-processar-dados-no-contexto-da-interrup\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;A abordagem correta: ISR m\u00ednima + buffer expl\u00edcito&quot;,&quot;value&quot;:&quot;a-abordagem-correta-isr-m\u00ednima-buffer-expl\u00edcito&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Consumir bytes \u00e9 diferente de interpretar mensagens&quot;,&quot;value&quot;:&quot;consumir-bytes-\u00e9-diferente-de-interpretar-mensagens&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Bufferiza\u00e7\u00e3o como base para escalabilidade&quot;,&quot;value&quot;:&quot;bufferiza\u00e7\u00e3o-como-base-para-escalabilidade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Um crit\u00e9rio simples de maturidade&quot;,&quot;value&quot;:&quot;um-crit\u00e9rio-simples-de-maturidade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Framing: bytes sem fronteiras n\u00e3o s\u00e3o mensagens&quot;,&quot;value&quot;:&quot;framing-bytes-sem-fronteiras-n\u00e3o-s\u00e3o-mensagens&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;O erro cl\u00e1ssico: confiar em delimitadores \u201chumanos\u201d&quot;,&quot;value&quot;:&quot;o-erro-cl\u00e1ssico-confiar-em-delimitadores-humanos&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Framing \u00e9 uma decis\u00e3o de protocolo, n\u00e3o de conveni\u00eancia&quot;,&quot;value&quot;:&quot;framing-\u00e9-uma-decis\u00e3o-de-protocolo-n\u00e3o-de-conveni\u00eancia&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Abordagem correta 1: marcador de in\u00edcio + comprimento&quot;,&quot;value&quot;:&quot;abordagem-correta-1-marcador-de-in\u00edcio-comprimento&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Abordagem correta 2: delimitador com escape (SLIP-like)&quot;,&quot;value&quot;:&quot;abordagem-correta-2-delimitador-com-escape-slip-like&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Framing define se o sistema sobrevive ao erro&quot;,&quot;value&quot;:&quot;framing-define-se-o-sistema-sobrevive-ao-erro&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Uma regra pr\u00e1tica&quot;,&quot;value&quot;:&quot;uma-regra-pr\u00e1tica&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Integridade e parsing defensivo: confiar em bytes \u00e9 ingenuidade&quot;,&quot;value&quot;:&quot;integridade-e-parsing-defensivo-confiar-em-bytes-\u00e9-ingenuidade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;O erro cl\u00e1ssico: \u201cse chegou, \u00e9 v\u00e1lido\u201d&quot;,&quot;value&quot;:&quot;o-erro-cl\u00e1ssico-se-chegou-\u00e9-v\u00e1lido&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Integridade n\u00e3o \u00e9 luxo, \u00e9 requisito&quot;,&quot;value&quot;:&quot;integridade-n\u00e3o-\u00e9-luxo-\u00e9-requisito&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Exemplo correto: CRC expl\u00edcito no frame&quot;,&quot;value&quot;:&quot;exemplo-correto-crc-expl\u00edcito-no-frame&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Parsing defensivo: nunca confie no emissor&quot;,&quot;value&quot;:&quot;parsing-defensivo-nunca-confie-no-emissor&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;UART como superf\u00edcie de ataque (mesmo sem rede)&quot;,&quot;value&quot;:&quot;uart-como-superf\u00edcie-de-ataque-mesmo-sem-rede&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Regra de ouro&quot;,&quot;value&quot;:&quot;regra-de-ouro&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Recupera\u00e7\u00e3o, perda e por que sistemas reais n\u00e3o assumem perfei\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;recupera\u00e7\u00e3o-perda-e-por-que-sistemas-reais-n\u00e3o-assumem-perfei\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;O erro cl\u00e1ssico: assumir que \u201cn\u00e3o acontece\u201d&quot;,&quot;value&quot;:&quot;o-erro-cl\u00e1ssico-assumir-que-n\u00e3o-acontece&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Sistemas reais assumem falha como condi\u00e7\u00e3o normal&quot;,&quot;value&quot;:&quot;sistemas-reais-assumem-falha-como-condi\u00e7\u00e3o-normal&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Timeouts: o rel\u00f3gio tamb\u00e9m \u00e9 parte do protocolo&quot;,&quot;value&quot;:&quot;timeouts-o-rel\u00f3gio-tamb\u00e9m-\u00e9-parte-do-protocolo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Re-sincroniza\u00e7\u00e3o expl\u00edcita&quot;,&quot;value&quot;:&quot;re-sincroniza\u00e7\u00e3o-expl\u00edcita&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Perda de pacotes n\u00e3o \u00e9 exce\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;perda-de-pacotes-n\u00e3o-\u00e9-exce\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;UART como transporte muda o jogo&quot;,&quot;value&quot;:&quot;uart-como-transporte-muda-o-jogo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;A frase que resume tudo&quot;,&quot;value&quot;:&quot;a-frase-que-resume-tudo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Checklist de Boas Pr\u00e1ticas \u2014 UART como Camada de Transporte&quot;,&quot;value&quot;:&quot;checklist-de-boas-pr\u00e1ticas-uart-como-camada-de-transporte&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;1. Modelo Mental e Arquitetura&quot;,&quot;value&quot;:&quot;1-modelo-mental-e-arquitetura&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;2. Interrup\u00e7\u00f5es (ISR)&quot;,&quot;value&quot;:&quot;2-interrup\u00e7\u00f5es-isr&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;3. Bufferiza\u00e7\u00e3o&quot;,&quot;value&quot;:&quot;3-bufferiza\u00e7\u00e3o&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;4. Framing&quot;,&quot;value&quot;:&quot;4-framing&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;5. Integridade&quot;,&quot;value&quot;:&quot;5-integridade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;6. Parsing Defensivo&quot;,&quot;value&quot;:&quot;6-parsing-defensivo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;7. Recupera\u00e7\u00e3o e Robustez&quot;,&quot;value&quot;:&quot;7-recupera\u00e7\u00e3o-e-robustez&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;8. Perda, Repeti\u00e7\u00e3o e Ordem&quot;,&quot;value&quot;:&quot;8-perda-repeti\u00e7\u00e3o-e-ordem&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;9. Tempo e Determinismo&quot;,&quot;value&quot;:&quot;9-tempo-e-determinismo&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;10. Seguran\u00e7a e Confiabilidade&quot;,&quot;value&quot;:&quot;10-seguran\u00e7a-e-confiabilidade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;11. Testabilidade&quot;,&quot;value&quot;:&quot;11-testabilidade&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;12. Maturidade do Sistema&quot;,&quot;value&quot;:&quot;12-maturidade-do-sistema&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Regra Final&quot;,&quot;value&quot;:&quot;regra-final&quot;,&quot;isDelete&quot;:false}]" data-smooth="true" data-top-offset=""><div class="eb-toc__list-wrap"><ul class='eb-toc__list'><li><a href="#eb-table-content-0">UART não é uma porta: é uma decisão de arquitetura</a><ul class='eb-toc__list'><li><a href="#eb-table-content-1">O erro clássico: UART como “linha de printf”</a><li><a href="#eb-table-content-2">A mudança de mentalidade: UART como canal bruto</a><li><a href="#uart-como-espelho-do-engenheiro">UART como espelho do engenheiro</a></li></ul><li><a href="#eb-table-content-4">Bufferização não é opcional: é o primeiro contrato com a realidade</a><ul class='eb-toc__list'><li><a href="#eb-table-content-5">O erro clássico: processar dados no contexto da interrupção</a><li><a href="#eb-table-content-6">A abordagem correta: ISR mínima + buffer explícito</a><li><a href="#eb-table-content-7">Consumir bytes é diferente de interpretar mensagens</a><li><a href="#eb-table-content-8">Bufferização como base para escalabilidade</a><li><a href="#eb-table-content-9">Um critério simples de maturidade</a></li></ul><li><a href="#eb-table-content-10">Framing: bytes sem fronteiras não são mensagens</a><ul class='eb-toc__list'><li><a href="#eb-table-content-11">O erro clássico: confiar em delimitadores “humanos”</a><li><a href="#eb-table-content-12">Framing é uma decisão de protocolo, não de conveniência</a><li><a href="#eb-table-content-13">Abordagem correta 1: marcador de início + comprimento</a><li><a href="#abordagem-correta-2-delimitador-com-escape-slip-like">Abordagem correta 2: delimitador com escape (SLIP-like)</a><li><a href="#framing-define-se-o-sistema-sobrevive-ao-erro">Framing define se o sistema sobrevive ao erro</a><li><a href="#eb-table-content-16">Uma regra prática</a></li></ul><li><a href="#eb-table-content-17">Integridade e parsing defensivo: confiar em bytes é ingenuidade</a><ul class='eb-toc__list'><li><a href="#eb-table-content-18">O erro clássico: “se chegou, é válido”</a><li><a href="#eb-table-content-19">Integridade não é luxo, é requisito</a><li><a href="#eb-table-content-20">Exemplo correto: CRC explícito no frame</a><li><a href="#parsing-defensivo-nunca-confie-no-emissor">Parsing defensivo: nunca confie no emissor</a><li><a href="#eb-table-content-22">UART como superfície de ataque (mesmo sem rede)</a><li><a href="#regra-de-ouro">Regra de ouro</a></li></ul><li><a href="#eb-table-content-24">Recuperação, perda e por que sistemas reais não assumem perfeição</a><ul class='eb-toc__list'><li><a href="#eb-table-content-25">O erro clássico: assumir que “não acontece”</a><li><a href="#eb-table-content-26">Sistemas reais assumem falha como condição normal</a><li><a href="#eb-table-content-27">Timeouts: o relógio também é parte do protocolo</a><li><a href="#eb-table-content-28">Re-sincronização explícita</a><li><a href="#eb-table-content-29">Perda de pacotes não é exceção</a><li><a href="#uart-como-transporte-muda-o-jogo">UART como transporte muda o jogo</a><li><a href="#a-frase-que-resume-tudo">A frase que resume tudo</a></li></ul><li><a href="#eb-table-content-32">Checklist de Boas Práticas — UART como Camada de Transporte</a><ul class='eb-toc__list'><li><a href="#1-modelo-mental-e-arquitetura">1. Modelo Mental e Arquitetura</a><li><a href="#eb-table-content-34">2. Interrupções (ISR)</a><li><a href="#eb-table-content-35">3. Bufferização</a><li><a href="#4-framing">4. Framing</a><li><a href="#5-integridade">5. Integridade</a><li><a href="#6-parsing-defensivo">6. Parsing Defensivo</a><li><a href="#eb-table-content-39">7. Recuperação e Robustez</a><li><a href="#eb-table-content-40">8. Perda, Repetição e Ordem</a><li><a href="#9-tempo-e-determinismo">9. Tempo e Determinismo</a><li><a href="#eb-table-content-42">10. Segurança e Confiabilidade</a><li><a href="#11-testabilidade">11. Testabilidade</a><li><a href="#12-maturidade-do-sistema">12. Maturidade do Sistema</a><li><a href="#regra-final">Regra Final</a></li></ul></ul></div></div></div></div></div>


<h2 class="wp-block-heading"><strong>UART não é uma porta: é uma decisão de arquitetura</strong></h2>



<p>Para a maioria dos engenheiros, o primeiro contato com um microcontrolador envolve ligar um conversor USB-UART, abrir um terminal serial e enviar caracteres. Essa experiência inicial cria um vício conceitual perigoso: tratar UART como se fosse uma “porta de texto”, algo próximo de um <code>printf()</code> com fio. Esse modelo mental funciona em <em>demos</em>, <em>provas de conceito</em> e exemplos de datasheet — mas ele quebra imediatamente quando o sistema passa a existir no mundo real.</p>



<p>UART não é uma porta, não é um periférico isolado e definitivamente não é um canal confiável por definição. UART é apenas um <strong>mecanismo físico de serialização de bits</strong>, sem noção de mensagens, pacotes, estados, erros ou intenção semântica. Tudo o que faz sentido — início, fim, significado, validade, recuperação — <strong>precisa ser projetado em cima dela</strong>. Quando isso não é feito, o sistema não “falha de vez”; ele falha de forma intermitente, imprevisível e difícil de depurar.</p>



<p>Um teste simples revela rapidamente o nível de maturidade de um firmware: <em>o que acontece quando um byte se perde?</em> Se a resposta for “nunca acontece” ou “o terminal não mostra”, o sistema já está arquiteturalmente comprometido. Em sistemas reais há ruído elétrico, interrupções concorrentes, buffers cheios, clocks ligeiramente desalinhados, resets parciais e firmware sendo atualizado em campo. UART expõe todas essas fragilidades sem piedade.</p>



<h3 class="wp-block-heading">O erro clássico: UART como “linha de printf”</h3>



<p>O padrão mais comum — e mais frágil — de uso de UART é este:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>// <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Abordagem frágil e não determinística
void loop(void) {
    char cmd&#91;32&#93;;
    scanf("%s", cmd);

    if (strcmp(cmd, "ON") == 0) {
        led_on();
    } else if (strcmp(cmd, "OFF") == 0) {
        led_off();
    }
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #616E88">// &#x274c; Abordagem frágil e não determinística</span></span>
<span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">loop</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">char</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">cmd</span><span style="color: #D8DEE9FF">&#91;</span><span style="color: #B48EAD">32</span><span style="color: #D8DEE9FF">&#93;</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #88C0D0">scanf</span><span style="color: #D8DEE9FF">(</span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">%s</span><span style="color: #ECEFF4">&quot;</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">cmd</span><span style="color: #D8DEE9FF">)</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">strcmp</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">cmd</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">ON</span><span style="color: #ECEFF4">&quot;</span><span style="color: #D8DEE9FF">) </span><span style="color: #81A1C1">==</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">led_on</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">else</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">if</span><span style="color: #D8DEE9FF"> (</span><span style="color: #88C0D0">strcmp</span><span style="color: #D8DEE9FF">(</span><span style="color: #D8DEE9">cmd</span><span style="color: #ECEFF4">,</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">OFF</span><span style="color: #ECEFF4">&quot;</span><span style="color: #D8DEE9FF">) </span><span style="color: #81A1C1">==</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">        </span><span style="color: #88C0D0">led_off</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #ECEFF4">}</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p>Esse código pressupõe coisas que <strong>não são garantidas pela UART</strong>:</p>



<ul class="wp-block-list">
<li>Que os dados chegam completos</li>



<li>Que não há bytes extras ou truncados</li>



<li>Que o buffer nunca estoura</li>



<li>Que o alinhamento entre transmissor e receptor é perfeito</li>



<li>Que ruído não existe</li>



<li>Que o tempo não importa</li>
</ul>



<p>Além disso, <code>scanf()</code> bloqueia a execução, mistura parsing com transporte, não detecta framing e não oferece nenhuma estratégia de recuperação. Funciona em bancada, falha em produção.</p>



<h3 class="wp-block-heading">A mudança de mentalidade: UART como <em>canal bruto</em></h3>



<p>Um sistema robusto parte do princípio oposto:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>UART não transporta comandos, transporta bytes.<br>Quem transporta mensagens é o protocolo que você desenha.</strong></p>
</blockquote>



<p>Isso muda completamente a arquitetura. Em vez de “ler comandos”, o firmware passa a:</p>



<ol class="wp-block-list">
<li><strong>Receber bytes de forma assíncrona</strong></li>



<li><strong>Bufferizar dados</strong></li>



<li><strong>Detectar início e fim de quadros</strong></li>



<li><strong>Validar integridade</strong></li>



<li><strong>Interpretar significado</strong></li>



<li><strong>Responder ou recuperar estado</strong></li>
</ol>



<p>Um primeiro passo correto é separar <strong>recepção</strong> de <strong>interpretação</strong>:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" style="color:#d8dee9ff;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>// <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> UART tratada como stream bruto
#define RX_BUFFER_SIZE 128

volatile uint8_t rx_buffer&#91;RX_BUFFER_SIZE&#93;;
volatile uint16_t rx_head = 0;

void USART_IRQHandler(void) {
    uint8_t byte = USART_ReadByte();
    rx_buffer&#91;rx_head++&#93; = byte;
    rx_head %= RX_BUFFER_SIZE;
}
</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki nord" style="background-color: #2e3440ff" tabindex="0"><code><span class="line"><span style="color: #616E88">// &#x2705; UART tratada como stream bruto</span></span>
<span class="line"><span style="color: #D8DEE9FF">#</span><span style="color: #D8DEE9">define</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">RX_BUFFER_SIZE</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">128</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D8DEE9">volatile</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">uint8_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">rx_buffer</span><span style="color: #D8DEE9FF">&#91;</span><span style="color: #D8DEE9">RX_BUFFER_SIZE</span><span style="color: #D8DEE9FF">&#93;</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9">volatile</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">uint16_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">rx_head</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #B48EAD">0</span><span style="color: #81A1C1">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">USART_IRQHandler</span><span style="color: #D8DEE9FF">(</span><span style="color: #81A1C1">void</span><span style="color: #D8DEE9FF">) </span><span style="color: #ECEFF4">{</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">uint8_t</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">byte</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #88C0D0">USART_ReadByte</span><span style="color: #D8DEE9FF">()</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">rx_buffer</span><span style="color: #D8DEE9FF">&#91;</span><span style="color: #D8DEE9">rx_head</span><span style="color: #81A1C1">++</span><span style="color: #D8DEE9FF">&#93; </span><span style="color: #81A1C1">=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">byte</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #D8DEE9FF">    </span><span style="color: #D8DEE9">rx_head</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">%=</span><span style="color: #D8DEE9FF"> </span><span style="color: #D8DEE9">RX_BUFFER_SIZE</span><span style="color: #81A1C1">;</span></span>
<span class="line"><span style="color: #ECEFF4">}</span></span>
<span class="line"></span></code></pre></div>



<p>Aqui não há “comandos”, apenas <strong>dados crus entrando no sistema</strong>. Isso é proposital. O firmware assume que os bytes podem estar incompletos, desalinhados ou corrompidos — e projeta em cima disso.</p>



<p>A partir desse ponto, tudo o que vier depois — framing, parsing, validação — deixa de ser improviso e passa a ser <strong>engenharia deliberada</strong>.</p>



<h3 class="wp-block-heading">UART como espelho do engenheiro</h3>



<p>UART é brutal porque ela não esconde nada. Não há retransmissão automática, não há ordenação garantida, não há controle de fluxo implícito. Se o sistema funciona bem sobre UART, ele provavelmente funcionará bem sobre SPI, CAN, RS-485, TCP ou rádio. Se ele só funciona quando tudo dá certo, então ele não funciona.</p>



<p>Projetar bem sobre UART é um divisor de águas. É o ponto em que o engenheiro deixa de “fazer firmware” e começa a <strong>construir sistemas distribuídos embarcados</strong>, capazes de sobreviver ao mundo real.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/><p>The post <a href="https://mcu.tec.br/protoclolos/uart-serial/uart-nao-e-porta-serial-como-projetar-protocolos-robustos-em-sistemas-embarcados/">UART não é Porta Serial: Como Projetar Protocolos Robustos em Sistemas Embarcados</a> first appeared on <a href="https://mcu.tec.br">MCU & FPGA</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://mcu.tec.br/protoclolos/uart-serial/uart-nao-e-porta-serial-como-projetar-protocolos-robustos-em-sistemas-embarcados/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1102</post-id>	</item>
	</channel>
</rss>
