Read the learning points of the ametal_hc595 interface

The eighth chapter is dedicated to understanding AMetal in depth. The content of this section focuses on 8.2, the HC595 interface. **8.2 HC595 Interface** The HC595 is a "serial-to-parallel" peripheral that allows data to be output through GPIO control of data and clock pins. However, in some MCUs with SPI peripherals, the SPI is often used to control the HC595 output, making the driver simpler. To abstract the differences in the underlying data output method, the relevant HC595 interface can be defined using a design approach similar to the LED universal interface. **8.2.1 Defining Interfaces** **1. Interface Naming** Since the object of operation is the HC595, the interface name is prefixed with "am_hc595_". The basic operation of the HC595 is to output parallel data, so the corresponding interface name is: `Am_hc595_send` In addition, the HC595 outputs in a three-state mode, controlled by the OE pin. Therefore, interfaces for enabling (normal output) and disabling (high-impedance state) the output can be defined as: `Am_hc595_enable` `Am_hc595_disable` **2. Interface Parameters** In the design of the LED universal interface, multiple LEDs are often involved, so a unique ID number `led_id` is used to distinguish them. Following this logic, should we also use `hc595_id` to differentiate between various HC595s? While using an ID to identify different LEDs is simple and easy to understand, it may not be efficient when the number of LEDs increases, as searching for the corresponding device by ID becomes time-consuming. For LEDs, which are usually operated at slower rates, this impact is negligible. However, for general-purpose devices like the HC595, where efficiency is crucial, such a search process could affect performance. To achieve faster output, it's best to avoid any search process and directly operate the corresponding HC595 object. Using a pointer to the object is an effective solution. Since the user does not need to care about the internal details, a "handle" concept is introduced, which is essentially a pointer to the HC595 object. This handle is used as the first parameter in all interfaces: `Am_hc595_enable(am_hc595_handle_t handle);` `Am_hc595_disable(am_hc595_handle_t handle);` `Am_hc595_send(am_hc595_handle_t handle, const void *p_data, size_t nbytes);` Here, `p_data` points to the first address of the data being sent, and `nbytes` specifies the number of bytes to send. If only one HC595 is used, it sends one byte (8 bits). If multiple HC595s are cascaded, it sends `n` bytes (totaling 8 × n bits). **3. Return Value** All interfaces return an integer representing the standard error code. The complete definition of the HC595 interface is detailed in Table 8.4, and the corresponding class diagram is shown in Figure 8.5. **8.2.2 Implementing the Interface** **1. Abstract HC595 Device Class** Similar to the LED universal interface, the underlying hardware differences are abstracted. Based on the three interfaces, three abstract methods are defined and stored in a virtual function table. These methods are part of the abstract HC595 device class. The specific HC595 device is derived from this abstract class and implements the three abstract methods based on actual hardware. The method for defining the abstract device is identical to the LED device, and it can be used as a template for other devices. The handle used in the interface is essentially a pointer to the device. All interfaces call the abstract method implementation, and the specific implementation is done by the concrete HC595 device. The implementation of each interface is detailed in Listing 8.19. **2. Specific HC595 Device Class** Taking the SPI-controlled HC595 as an example, a concrete device class is derived from the abstract device class. The class diagram shows the structure of the specific HC595 device. The specific HC595 device class includes information such as the latch pin, output enable pin, and SPI clock frequency. It also stores the device information provided by the user, including the bit order (MSB or LSB first), which affects the output behavior. A new device information structure type is defined to store this information. When using the MiniPort-595 with the AM824-Core, the device instance information is defined accordingly. Before using the SPI to control the HC595, the initialization function must assign values to each member of the device. The initialization function returns the HC595 handle, which is used as the argument for the general interface functions. An example of the initialization function is shown in Listing 8.21, where a standard SPI slave device is created, the device information is initialized, and the abstract methods are assigned to the device's function pointers. Finally, the HC595 handle is returned for use in subsequent operations. The complete implementation of the abstract methods is shown in Listing 8.22, where GPIO and SPI functions are used to implement the enable/disable and send operations. The contents of the specific HC595 device interface file (am_hc595_spi.h) are shown in Listing 8.23, providing further details on the interface definitions.

Grain Bin Full Sensor

Grain Bin Full Sensor,Grain Auger Full Bin Alarm,Grain Bin Full Alarm,Grain Bin Full Indicator

Changzhou Youeryou Trading Co., Ltd. , https://www.farmpartssupplier.com