This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-09-10

mwf_periph_common - Peripheral common

mwf_periph_common - Peripheral common
    Defines common peripheral definitions and pin operations.

    mwf_periph_common - Peripheral common

    include

    #include "mwf_periph_common.hpp"
    

    GPIO Operation Functions

    These are static (inline) functions defined within struct pin that you can call.

    set_pin_as_input()

    static void set_pin_as_input(uint8_t pin, uint32_t param = 0)

    Sets pin to an input state.

    • param is unused.

    set_pin_as_output()

    static void set_pin_as_output(uint8_t pin, uint32_t param = PORTOUT_INITSTATE_HIGH)

    Sets pin to an output state.

    • If PORTOUT_INITSTATE_HIGH is specified for param, the pin will be set to a HIGH state when this function is called.

    set_output()

     void set_output(uint8_t pin, uint8_t value)

    Changes the output state of pin. If value is PORT_HIGH (1), it sets the pin to HIGH; if PORT_LOW (0), it sets it to LOW.

    get_input()

     static uint8_t get_input(uint8_t pin)

    Reads the state of pin when it is in an input state. The return value is PORT_HIGH (1) for HIGH and PORT_LOW (0) for LOW.

    get_input_bm()

    static uint32_t get_input_bm(uint32_t u32mask = 0x3FFFFF)
    
    // Example
      uint32_t bm = get_input_bm((1ul << 0) | (1ul << 3));
      if (bm & (1ul << 3)) { ... } // If PIO3 is HIGH
      else { ... }                 // If PIO3 is LOW
    

    Reads the input state of all pins as a bitmap.

    • The value of PIOn corresponds to the bit (1UL << n).
    • The values of pins not in an input state are undefined.

    In the example, you specify a pin bitmap (u32mask) to get the state of input pins at once. For example, if you need the values for PIO0 and PIO3, you would specify (1ul << 0) | (1ul << 3).

    The return value is also a bitmap of the input states. A HIGH level is represented by 1, and a LOW level by 0. For example, if PIO3 is HIGH, the 4th bit from the LSB will be 1.

    set_output_bm()

    static void set_output_bm(uint32_t u32mask, uint8_t value)
    
    // Example
      set_output_bm((1ul << 0) | (1ul << 3), 0); // Sets PIO0 and 3 to LOW
    

    Changes the output state for the pins corresponding to the bitmap specified by u32mask.

    • The value of PIOn corresponds to the bit (1UL << n).
    • If value is PORT_HIGH (1), it sets the output to HIGH; if PORT_LOW (0), it sets it to LOW.

    In the example, you specify a pin bitmap (u32mask) to set the output state of multiple pins at once. For example, to set PIO0 and PIO3, you would specify (1ul << 0) | (1ul << 3). The value is 1 for a HIGH level and 0 for a LOW level.

    PIN Operation Functions

    These are static (inline) functions defined within struct pin that you can call.

    static void __conf_digital()

    static void __conf_digital(uint8_t pin, uint8_t func)

    This function sets IOCON_PIO_FUNC(func) for the PIO register of the specified pin pin.

    			IOCON->PIO[0][pin] =
    							( 0
    							| IOCON_PIO_FUNC(func) // SETFUNC (e.g. PWM is 0x04)
    							| IOCON_PIO_MODE(0x00u) // 0x00:pullup
    							| IOCON_PIO_DIGIMODE(0x01u)
    							| IOCON_PIO_INPFILT_OFF
    							);

    static void conf_default()

    static void conf_default(uint8_t pin)

    This function reverts the pin to its default definition, as set in board/pin_mux.c within the TWENETmcu library.

    static void __conf_gpio_input()

    static void __conf_gpio_input(uint8_t pin)

    This is for internal use. It sets pin pin as a GPIO input.

    The user program should use set_pin_as_input().

    static void __conf_gpio_output()

    static void __conf_gpio_output(uint8_t pin, bool b_init_high = true)

    This is for internal use. It sets pin pin as a GPIO output. If b_init_high is true, the initial output is a HIGH (Vcc) level; if false, it’s a LOW (GND) level.

    The user program should use set_pin_as_output().

    static void set_pullup()

    static void set_pullup(uint8_t pin, uint8_t mode)

    This function sets IOCON_PIO_MODE(mode) for the PIO register of the specified pin pin. This bit controls the pull-up behavior.

    static void conf_pwmout()

    static void conf_pwmout(uint8_t pin, bool b_enable)

    This function uses __conf_digital() to set IOCON_PIO_FUNC(0x04) for the PIO register of the specified pin pin. This typically configures the pin for PWM output.

    static void conf_adc_input()

    static void conf_adc_input(uint8_t pin)

    For pins PIO14..19, this function makes the following settings to configure them for ADC:

    IOCON->PIO[0][pin] =
    ( 0
    | IOCON_PIO_FUNC(0x00u) // FUNC_ALT0
    | IOCON_PIO_MODE(0x00u) // 0x00:pullup
    | IOCON_PIO_DIGIMODE(0x00u) // ANALOGUE
    | IOCON_PIO_INPFILT_OFF
    );

    static void conf_sclN_pioM()

    		static void conf_scl0_pio10() {
    		    __conf_digital(10, 0x05);
    		}
    
    		static void conf_sda0_pio11() {
    		    __conf_digital(11, 0x05);
    		}
    
    		static void conf_scl0_pio15() {
    		    __conf_digital(15, 0x05);
    		}
    
    		static void conf_sda0_pio16() {
    		    __conf_digital(11, 0x05);
    		}
    
    		static void conf_scl1_pio06() {
    		    __conf_digital(06, 0x05);
    		}
    
    		static void conf_sda1_pio07() {
    		    __conf_digital(07, 0x05);
    		}
    
    		static void conf_scl1_pio12() {
    		    __conf_digital(12, 0x05);
    		}
    
    		static void conf_sda1_pio13() {
    		    __conf_digital(13, 0x05);
    		}

    These are configuration functions for using the pins for I2C.

    static void conf_uart1(uint8_t tx, uint8_t rx)

    static void conf_uart1(uint8_t tx, uint8_t rx)

    This function specifies the TXD pin of UART1 as tx and the RXD pin as rx.

    • You cannot set only one of the TXD or RXD pins with this function.

    Others

    struct ts_retention_context

    struct ts_retention_context {
        uint32_t u32_bm_io;
        uint32_t u32_bm_set;
        void save();
        void restore();
    };
    static ts_retention_context s_retention_context;
    
    static void retention_on_sleep()
    static void retention_on_wake()

    This is for internal use. It manages the processes and necessary data for retaining the GPIO output state during sleep.

    static bool __b_check_swdbg_port(uint8_t pin)

    static bool __b_check_swdbg_port(uint8_t pin)

    This is for internal use. It determines whether a pin is used by the debugger during a debug session.

    Behavior during Sleep

    • For pins whose GPIO output state has been set using set_pin_as_output(), the output state is maintained even during sleep. However, retention_on_sleep() and retention_on_wake() must be called appropriately. In TWENET, these functions are called during the pre-sleep and wake-from-sleep processes handled within the TWENETmcu and TWENETcmpt libraries.