Wuxi Motian Signage Co., Ltd , https://www.makesignage.com
The fifth chapter dives deep into explaining AMetal. The content of this section is 5.1 Interface and Implementation.
This chapter guides: For beginners, it can be challenging to achieve temperature acquisition, but AMetal makes it possible. AMetal builds a more abstract and standardized interface, encapsulating the underlying changes of various MCUs, providing a more stable abstraction service for applications, and extending the lifecycle of software systems. No matter which MCU you choose, as long as it supports AMetal, developers don't need to read the user manual or even know what AMetal is—they can reuse their original code effortlessly.
Even if you have written many programs using AMetal, you might still want to learn how more interfaces are implemented. Let's embark on the magical journey of AMetal together!
**5.1 Interface and Implementation**
> > >
**5.1.1 GPIO Interface Functions**
AMetal provides standard interface functions for manipulating GPIO. The standard interface function prototypes for all GPIOs are located in the ametal_am824_core_1.00\ametal\common\interface\am_gpio.h file, which includes macro definitions and declarations for function prototypes provided to users for GPIO operations. These include:
- Configure pin functions and modes: `int am_gpio_pin_cfg(int pin, uint32_t flags)`
- Get the pin level: `int am_gpio_get(int pin)`
- Set the pin level: `int am_gpio_set(int pin, int value)`
- Flip pin level: `int am_gpio_toggle(int pin)`
**1. Configuring Pin Functions and Modes**
The pin is specified in the format `PIOx_x`, such as `PIO0_0` for a specific pin. The `flags` parameter consists of "General Functions | Common Mode | Platform Features | Platform Mode" (bitwise OR in C). Common functions and modes are defined in `am_gpio.h`, while platform-specific features are defined in files like `lpc8xx_pin.h`.
If `AM_OK` is returned, the configuration is successful; otherwise, it fails. For example, see Listing 5.1 for configuring GPIO functions.
**2. Getting the Pin Level**
Use `am_gpio_get(int pin)` to get the level state of a pin. For example, `PIO0_0` can be used to check its status. See Appendix 5.4 for an example program.
**3. Setting the Pin Level**
Use `am_gpio_set(int pin, int value)` to set the pin level. A value of 0 sets it low, and 1 sets it high. If `AM_OK` is returned, the operation is successful. See Listing 5.5 for an example.
**4. Flipping the Pin Level**
Use `am_gpio_toggle(int pin)` to flip the output level of a GPIO pin. If the current output is low, it will become high, and vice versa. See Listing 5.6 for an example.
**5. Example**
To control LED0, configure the GPIO to output mode and initialize it to a high level. Then use `am_gpio_set()` to turn it off. See Listing 5.7 for the code.
Flashing LEDs involves toggling the pin continuously. See Listings 5.8 and 5.9 for examples.
For a buzzer that sounds at 1KHz, the pin must toggle every 500us. See Listing 5.10 for an example.
> > >
**5.1.2 LED Interface and Implementation**
Implementing a universal LED function involves creating a `.h` and `.c` file. The `.h` file defines the interface, while the `.c` file implements the functions. For example, define the GPIO port for each LED in an array.
Check if `led_id` is within bounds to prevent array overflow. Initialize the GPIO as an output with a high level to ensure the LED starts off. Encapsulate the interface in `led.h` for easy access.
In practical use, macros can simplify calling LED functions. Define `LED_ID` in `led.h` for convenience. Use a structure to handle different data types, making the code more maintainable.
Define a macro `USE_MINIPORT_LED` in `led.h` to switch between different LED configurations. This reduces dependencies and promotes reusable programming.
> > >
**5.1.3 I/O Interfaces and Interrupts**
GPIO triggers involve interrupt-related functions. Table 5.5 shows the relevant interface functions.
**1. Configuring Pin Trigger Conditions**
Use `am_gpio_trigger_cfg(int pin, uint32_t flag)` to set the trigger condition. Possible flags are listed in Table 5.6. Check the return value to ensure the condition is supported.
**2. Connecting a Trigger Callback Function**
Use `am_gpio_trigger_connect(int pin, am_pfnvoid_t pfn_callback, void *p_arg)` to connect a callback function. The callback is called when the pin triggers.
**3. Disconnecting a Trigger Callback Function**
Use `am_gpio_trigger_disconnect(int pin, am_pfnvoid_t pfn_callback, void *p_arg)` to disconnect the callback function. This allows reconfiguration or switching to a new function.
**4. Enabling a Pin Trigger**
Use `am_gpio_trigger_on(int pin)` to enable the pin trigger. Ensure the callback and conditions are set before enabling.
**5. Disabling a Pin Trigger**
Use `am_gpio_trigger_off(int pin)` to disable the pin trigger. You can re-enable it later using `am_gpio_trigger_on()`.