UART learning summary: how to judge the completion of one frame of data

**UART (Universal Asynchronous Receiver/Transmitter)** The Universal Asynchronous Receiver/Transmitter, commonly known as UART, is a type of asynchronous serial communication interface that converts parallel data into serial format and vice versa. It is an essential component in many embedded systems, allowing devices to communicate with each other over a serial link. Typically, the UART is integrated into microcontrollers or used as a standalone chip, often paired with level-shifting circuits like the MAX232 to ensure compatibility with standard RS-232 signals. A variant of the UART, called USART (Universal Synchronous Asynchronous Receiver Transmitter), includes additional hardware for synchronous communication. However, most applications still use the basic UART for asynchronous communication, which allows full-duplex data transfer—meaning it can send and receive data simultaneously. In embedded systems, UART is widely used for communication between a host device and peripheral modules. For example, it connects a car audio system to an external AP, or facilitates communication between a microcontroller and a PC, such as with a debugger or EEPROM. When receiving data via UART, the system receives one byte at a time. The underlying hardware simply stores the received byte in a buffer but does not know when a complete frame has been received. Determining the end of a frame depends on the protocol being used, which can vary from one application to another. There are several methods to detect the end of a frame: **Method One:** This approach involves using an interrupt-driven receiver function. When a byte is received, it is stored in a buffer, and once the expected header length is reached, the code checks for the frame header and proceeds to parse the rest of the data. This method is efficient and allows for real-time processing. **Method Two:** Some protocols include a length field before the actual data. The receiver uses this length to determine how many bytes to expect. This method is simple and reliable if all devices follow the same protocol. **Method Three:** This method uses a timeout-based approach. If no new data arrives within a certain period (e.g., 20ms), the system assumes the frame has ended. While this method is straightforward, it may reduce overall throughput due to the delay introduced by the timeout. **Method Four:** The most flexible and commonly used approach today is the circular buffer. Data is continuously written to a ring buffer, and the application or protocol parser reads from it as needed. This method avoids the need for timeouts and allows for efficient handling of continuous data streams. However, managing the buffer properly requires careful design to prevent overflow and ensure thread safety. To implement a robust UART driver, I defined several functions and interfaces, including initialization, configuration, sending, and receiving functions. These functions allow for modular and reusable code, making it easier to integrate UART communication into different projects. One key challenge was designing a decoupled ring buffer. Initially, I struggled with how to separate the buffer from the protocol parsing logic. After learning more about object-oriented programming and data structures, I realized that defining a queue class with enqueue and dequeue operations could simplify the process. I created a `byte_queue_t` structure with four main operations: `init_byte_queue`, `is_queue_empty`, `enqueue_byte`, and `dequeue_byte`. This allowed me to manage the buffer independently of the UART driver, improving modularity and maintainability. However, I also learned that in a multi-threaded environment, atomic operations are crucial. Since both the UART interrupt and the main application may access the buffer, I had to add critical section locks to prevent race conditions. Depending on the priority of the threads involved, locking might be necessary to ensure data integrity. Overall, implementing a UART driver with a circular buffer and proper synchronization mechanisms provides a powerful and flexible solution for serial communication in embedded systems. It allows for efficient, real-time data handling while maintaining clean and modular code.

Siren and Alarm

A siren is a loud noise-making device. Civil defense sirens are mounted in fixed locations and used to warn of natural disasters or attacks. Sirens are used on emergency service vehicles such as ambulances, police cars, and fire trucks. There are two general types: pneumatic and electronic.

Many fire sirens (used for calling the volunteer fire fighters) serve double duty as tornado or civil defense sirens, alerting an entire community of impending danger. Most fire sirens are either mounted on the roof of a fire station or on a pole next to the fire station. Fire sirens can also be mounted on or near government buildings, on tall structures such as water towers, as well as in systems where several sirens are distributed around a town for better sound coverage. Most fire sirens are single tone and mechanically driven by electric motors with a rotor attached to the shaft. Some newer sirens are electronically driven speakers.

Fire sirens are often called "fire whistles", "fire alarms", or "fire horns". Although there is no standard signaling of fire sirens, some utilize codes to inform firefighters of the location of the fire. Civil defense sirens also used as fire sirens often can produce an alternating "hi-lo" signal (similar to emergency vehicles in many European countries) as the fire signal, or a slow wail (typically 3x) as to not confuse the public with the standard civil defense signals of alert (steady tone) and attack (fast wavering tone). Fire sirens are often tested once a day at noon and are also called "noon sirens" or "noon whistles".

The first emergency vehicles relied on a bell. Then in the 70s, they switched to a duotone airhorn. Then in the 80s, that was overtaken by an electronic wail.

Piezo Alarm,Siren And Alarm,Piezo Buzzer Siren,Piezo Buzzer Alarm Siren

Jiangsu Huawha Electronices Co.,Ltd , https://www.hnbuzzer.com