From 58489cc1bf1a9f48098d59e648b33a38b2d0ac79 Mon Sep 17 00:00:00 2001 From: James Snyder Date: Mon, 29 Jun 2009 16:41:52 +0000 Subject: [PATCH] added platform interface documentation for UART, SPI, timers and PWM --- doc/en/arch_platform.html | 7 +- doc/en/arch_platform_pio.html | 8 ++- doc/en/arch_platform_pwm.html | 45 ++++++++++++ doc/en/arch_platform_spi.html | 52 ++++++++++++++ doc/en/arch_platform_template.html | 2 +- doc/en/arch_platform_timers.html | 111 +++++++++++++++++++++++++++++ doc/en/arch_platform_uart.html | 72 +++++++++++++++++++ src/common.c | 2 +- 8 files changed, 294 insertions(+), 5 deletions(-) create mode 100644 doc/en/arch_platform_pwm.html create mode 100644 doc/en/arch_platform_spi.html create mode 100644 doc/en/arch_platform_timers.html create mode 100644 doc/en/arch_platform_uart.html diff --git a/doc/en/arch_platform.html b/doc/en/arch_platform.html index 46337952..0ccdcf17 100644 --- a/doc/en/arch_platform.html +++ b/doc/en/arch_platform.html @@ -10,5 +10,10 @@ of all platforms supported by eLua in a common interface. For more details about the platform interface and the overall structure of eLua in general, check this link.
The platform interface is defined in the inc/platform.h header file from the eLua source distribution. It is a collection of various - components (UART, SPI, timers ...), each of them is detailed in the next subsections. + components (UART, SPI, timers ...), each of them is detailed in the next subsections. Each such component has an id which is a number that + identifies that component in eLua. Generally, numbers are assigned to components in their "natural" order; for example, PORTA will have the id + 0, PORTB will have 1 and so on. Similarly, the second SPI interface (SPI1) of the MCU will probably have an id equal to 1. However, this is not a strict + rule. The implementation of the platform interface might choose to expose only some of the peripherals (components) of the MCU, thus this rule might be + broken. For example, if a board has 3 UARTs, but for some reason the second UART (UART1) is dedicated and can't be touched by eLua, then UART0 will have the id 0 and UART2 will + have the id 1, so UART1 won't ever be accesible to the code. Such cases are documented in the ##specific usage notes section. diff --git a/doc/en/arch_platform_pio.html b/doc/en/arch_platform_pio.html index 15bda345..8634963f 100644 --- a/doc/en/arch_platform_pio.html +++ b/doc/en/arch_platform_pio.html @@ -29,14 +29,17 @@ This part of the platform interface deals with PIO (Programmable Input Output) o };

These are the operations that can be executed by the PIO subsystem on both ports and pins. They are given as arguments to the platform_pio_op function shown below. ##TODO: document read in/read out if we keep that

+

typedef u32 pio_type;

This is the type used for the actual I/O operations. Currently defined as an unsigned 32-bit type, thus no port can have more than 32 pins. If this happens, it should be possible to split it in two or more parts and adding the new parts as "virtual ports" (logical ports that don't have a direct hardware equivalent). The "virtual port" technique is used in the AVR32 backend.

+

Functions

int platform_pio_has_port( unsigned port );

-

Returns 1 if the platform has the hardware port received as an argument, 0 otherwise. Implemented in src/common.c, it uses the NUM_PIO macro that must be defined in the +

Returns 1 if the platform has the hardware port sepcified as argument, 0 otherwise. Implemented in src/common.c, it uses the NUM_PIO macro that must be defined in the platform's platform_conf.h file (see here for details). For example:

-

#define NUM_PIO 4 // The platform has 4 hardware ports

+

#define NUM_PIO 4 // The platform has 4 hardware PIO ports

+

int platform_pio_has_pin( unsigned port, unsigned pin );

Returns 1 if the platform has the hardware port and pin specified as arguments, 0 otherwise. Implemented in src/common.c, it uses the NUM_PIO macro to check the validity of the port and the PIO_PINS_PER_PORT or PIO_PIN_ARRAY macros to check the validity of the pin. The macros must be defined in the platform's platform_conf.h file @@ -47,6 +50,7 @@ of the port and the PIO_PINS_PER_PORT or PIO_PIN_ARRAY macros to c

  • use PIO_PIN_ARRAY when different ports of the MCU have different number of pins. For example:

    #define PIO_PIN_ARRAY { 4, 4, 2, 6 } // Port 0 has 4 pins, port 1 has 4 pins, port 2 has 2 pins, port 3 has 6 pins

  • +

    const char* platform_pio_get_prefix( unsigned port );

    Get the port prefix. Used to establish if the port notation uses numbers (P0, P1, P2...) or letters (PA, PB, PC...). Implemented in src/common.c is uses the PIO_PREFIX macro that must be defined in the platform's platform_conf.h file (see here for details). The value of this macro can be either '0' (for diff --git a/doc/en/arch_platform_pwm.html b/doc/en/arch_platform_pwm.html new file mode 100644 index 00000000..c05774d5 --- /dev/null +++ b/doc/en/arch_platform_pwm.html @@ -0,0 +1,45 @@ + + + +eLua platform interface - PWM + + + +

    Overview

    +

    +This part of the platform interface groups functions related to the PWM channel(s) of the MCU. +

    +

    Data structures, constants and types

    +

    // PWM operations
    +enum
    +{
    +  PLATFORM_PWM_OP_START,
    +  PLATFORM_PWM_OP_STOP,
    +  PLATFORM_PWM_OP_SET_CLOCK,
    +  PLATFORM_PWM_OP_GET_CLOCK
    +};

    +

    This enum lists all the operations that can be executed on a given PWM channel.

    + +

    +

    Functions

    +

    int platform_pwm_exists( unsigned id );

    +

    Returns 1 if the platform has the PWM channel specified as argument, 0 otherwise. Implemented in src/common.c, it uses the NUM_PWM macro that must be defined in the +platform's platform_conf.h file (see here for details). For example:

    +

    #define NUM_PWM 4 // The platform has 4 PWM channels

    + +

    u32 platform_pwm_setup( unsigned id, u32 frequency, unsigned duty );

    +

    Set up the PWM channel id to run at the given frequency (in hertz) with the specified duty cycle (specified as percent from 0 to 100). The function returns the +actual frequency set on the PWM channel, which might differ from frequency depending on the hardware. Also, some platforms don't allow the full range of 0%-100% duty cycle.

    + +

    u32 platform_pwm_op( unsigned id, int op, u32 data );

    +

    Executes op on the given PWM id. op can take any value from this enum, as follows: +

    + + diff --git a/doc/en/arch_platform_spi.html b/doc/en/arch_platform_spi.html new file mode 100644 index 00000000..3702fd44 --- /dev/null +++ b/doc/en/arch_platform_spi.html @@ -0,0 +1,52 @@ + + + +eLua platform interface - SPI + + + +

    Overview

    +

    +This part of the platform interface groups functions related to the SPI interface(s) of the MCU. +

    +

    Data structures and constants

    +

    // SPI mode
    +#define PLATFORM_SPI_MASTER                   1
    +#define PLATFORM_SPI_SLAVE                    0

    +

    Constants used to specify the SPI mode (master or slave).

    + +

    // SS values
    +#define PLATFORM_SPI_SELECT_ON                1
    +#define PLATFORM_SPI_SELECT_OFF               0

    +

    Constants used to select/deselect the SPI SS pin (if applicable).

    + +

    typedef u32 spi_data_type;

    +

    This is the type of a SPI data word, thus limiting the maximum size of a SPI data work to 32 bits (which should be enough for all practical purposes).

    +

    Functions

    +

    int platform_spi_exists( unsigned id );

    +

    Returns 1 if the platform has the hardware SPI specified as argument, 0 otherwise. Implemented in src/common.c, it uses the NUM_SPI macro that must be defined in the +platform's platform_conf.h file (see here for details). For example:

    +

    #define NUM_SPI 1 // The platform has 1 SPI interface

    + +

    u32 platform_spi_setup( unsigned id, int mode, u32 clock, unsigned cpol, unsigned cpha, unsigned databits );

    +

    This function is used to initialize the parameters of the SPI interface.
    +Arguments: +

    +

    This function returns the actual clock set for the SPI interface. Depending on the hardware, this may have a different value than the clock argument.
    +NOTE: currently, only master SPI mode is implemented in eLua.

    + +

    spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data );

    +

    Executes a SPI read/write cycle by sending the data received in argument data and returning the data read from the SPI bus.

    + +

    void platform_spi_select( unsigned id, int is_select );

    +

    For platforms that have a dedicated SS (Slave Select) pin in master SPI mode that be controlled manually, this function should enable or disable this pin on the SPI interface identified +by id depending on the value of the is_select argument (either PLATFORM_SPI_SELECT_ON or PLATFORM_SPI_SELECT_OFF, see here). If this +functionality does not exist in hardware this function does nothing.

    + diff --git a/doc/en/arch_platform_template.html b/doc/en/arch_platform_template.html index 8045c96b..37eece0f 100644 --- a/doc/en/arch_platform_template.html +++ b/doc/en/arch_platform_template.html @@ -9,7 +9,7 @@

    Insert overview here.

    -

    Data structures and constants

    +

    Data structures, constants and types

    Insert data structures and constants here.

    diff --git a/doc/en/arch_platform_timers.html b/doc/en/arch_platform_timers.html new file mode 100644 index 00000000..735c608b --- /dev/null +++ b/doc/en/arch_platform_timers.html @@ -0,0 +1,111 @@ + + + +eLua platform interface - timers + + + +

    Overview

    +

    +This part of the platform interface groups functions related to the timers of the MCU. It also makes provisions for using virtual timers on any platform, see +this section for details and kKeep in mind that in the following paragraphs an id can reffer to either a hardware timer or a virtual timer. +

    +

    Data structures, constants and types

    +

    typedef u32 timer_data_type;

    +

    This defines the data type used to specify delays and time intervals (which are always specifide in microseconds).

    + +

    // Timer operations
    +enum
    +{
    +  PLATFORM_TIMER_OP_START,
    +  PLATFORM_TIMER_OP_READ,
    +  PLATFORM_TIMER_OP_SET_CLOCK,
    +  PLATFORM_TIMER_OP_GET_CLOCK,
    +  PLATFORM_TIMER_OP_GET_MAX_DELAY,
    +  PLATFORM_TIMER_OP_GET_MIN_DELAY
    +};

    +

    This enum lists all the operations that can be executed on a given timer.

    + +

    Functions

    +

    int platform_timer_exists( unsigned id );

    +

    Returns 1 if the platform has the timer specified as argument, 0 otherwise. Implemented in src/common.c, it uses the NUM_TIMER macro that must be defined in the +platform's platform_conf.h file (see here for details) and the virtual timer configuration (see here for details). +For example:

    +

    #define NUM_TIMER 2 // The platform has 2 hardware timers

    + +

    void platform_timer_delay( unsigned id, u32 delay_us );

    +

    Waits for delay_us microseconds on timer id, then returns. This function is "split" in two parts: a platform-independent part implemented in src/common.c (that +handles virtual timers) and a platform-dependent part that must be implemented by each platform in a function named platform_s_timer_delay (see below). This function handles both +hardware timer IDs and virtual timer IDs.
    +IMPORTANT NOTE: the real delay after executing this is a function of a number of variables, most notably the base clock of the timer and the size of the timer counter register +(32 bits on some platforms, 16 bits on most platforms, other values are less common). To ensure that the delay you're requesting is achievable, use +platform_timer_op with platform_timer_op_get_max_delay and platform_timer_op_get_min_delay to obtain the maximum and the minimum +achievable wait times on your timer, respectively. Even if your delay is within these limits, the precision of this function still varies a lot, mainly as a function of +the timer base clock.

    + +

    void platform_s_timer_delay( unsigned id, u32 delay_us );

    +

    This function is identical in functionality to the one above (platform_timer_delay), but this is the function that must actually be implemented by a platform port, and it must never +handle virtual timer IDs, only hardware timer IDs.

    + +

    u32 platform_timer_op( unsigned id, int op, u32 data );

    +

    Executes operation op on timer id. op can take any value from this enum, as follows: +

    +

    This function is "split" in two parts: a platform-independent part implemented in src/common.c (that handles virtual timers) and a platform-dependent part that must be implemented +by each platform in a function named platform_s_timer_op (see below). This function handles both hardware timer IDs and virtual timer IDs.

    + +

    u32 platform_s_timer_op( unsigned id, int op, u32 data );

    +

    This function is identical in functionality to the one above (platform_timer_op), but this is the function that must actually be implemented by a platform port, and it must never +handle virtual timer IDs, only hardware timer IDs.

    + +

    u32 platform_timer_get_diff_us( unsigned id, timer_data_type end, timer_data_type start );

    +

    Return the time difference (in us) betweeen the end and start timer values of timer id. This function is generic for all platforms, thus it is implemented in +src/common.c.

    + +

    Virtual timers

    +

    Virtual timers were added to eLua to overcome some limitations: +

    +

    In this respect, virtual timers are a set of timers that share a single hardware timer. It is possible, in this way, to have a hardware timer that can implement 4, 8 or more hardware +timers. There are a few drawbacks to this approach: +

    +

    To enable virtual timers: +

      +
    1. edit platform_conf.h (see here for details) and set VTMR_NUM_TIMERS to the number of desired virtual timers and + VTMR_FREQ_HZ to the base frequency of the virtual timers (in hertz).For example: +

      + #define VTMR_NUM_TIMERS 4 // we need 4 virtual timers
      + #define VTMR_FREQ_HZ 4 // the base clock for the virtual timers is 4Hz

    2. +
    3. in your platform port setup a hardware timer to fire an interrupt at VTMR_FREQ_HZ and call the cmn_virtual_timer_cb function (defined in src/common.c) in the + timer interrupt handler. For example, if the the interrupt handler is called timer_int_handler, do this: +

      void timer_int_handler( void )
      + {
      +   // add code to clear the timer interrupt flag here if needed
      +   cmn_virtual_timer_cb();
      + }
      +

    4. +

    +

    Note that because of step 2 above you are limited by practical constraints on the value of VTMR_FREQ_HZ. If set too high, the timer interrupt will fire too often, thus taking too much +CPU time. The maximum value depends largely on the hardware and the desired behaviour of the virtual timers, but in practice values larger than 10 might visibly change the behaviour of your +system.

    +

    To use a virtua timers, identify it with the constant VTMR_FIRST_ID (defined in inc/common.h) plus an offset. For example, VTMR_FIRST_ID+0 (or simply +VTMR_FIRST_ID) is the ID of the first virtual timer in the system, and VTMR_FIRST_ID+2 is the ID of the third virtual timer in the system.

    + + diff --git a/doc/en/arch_platform_uart.html b/doc/en/arch_platform_uart.html new file mode 100644 index 00000000..78ccef9c --- /dev/null +++ b/doc/en/arch_platform_uart.html @@ -0,0 +1,72 @@ + + + +eLua platform interface - UART + + + +

    Overview

    +

    This part of the platform interface groups functions related to the UART interface(s) of the MCU.

    +

    Data structures, constants and types

    +

    // Parity
    +enum
    +{
    +  PLATFORM_UART_PARITY_EVEN,
    +  PLATFORM_UART_PARITY_ODD,
    +  PLATFORM_UART_PARITY_NONE
    +};

    +

    Constants used to specify the UART parity mode.

    + +

    // Stop bits
    +enum
    +{
    +  PLATFORM_UART_STOPBITS_1,
    +  PLATFORM_UART_STOPBITS_1_5,
    +  PLATFORM_UART_STOPBITS_2
    +};

    +

    Constants used to specify the number of UART stop bits.

    + +

    // "Infinite timeout" constant for recv
    +#define PLATFORM_UART_INFINITE_TIMEOUT        (-1)

    +

    This constant is used as a special timeout value (infinite timeout) in the UART functions that expect a timeout as argument.

    + +

    Functions

    +

    int platform_uart_exists( unsigned id );

    +

    Returns 1 if the platform has the hardware UART specified as argument, 0 otherwise. Implemented in src/common.c, it uses the NUM_UART macro that must be defined in the +platform's platform_conf.h file (see here for details). For example:

    +

    #define NUM_UART 2 // The platform has 2 UART interfaces

    + +

    u32 platform_uart_setup( unsigned id, u32 baud, int databits, int parity, int stopbits );

    +

    This function is used to initialize the parameters of the UART interface.
    +Arguments: +

    +

    This function returns the actual baud rate. Depending on the hardware, this may have a different value than the baud argument.

    + +

    void platform_uart_send( unsigned id, u8 data );

    +

    Send the given data to the UART interface with the specified id.

    + +

    int platform_uart_recv( unsigned id, unsigned timer_id, s32 timeout );

    +

    Receive a byte from the UART interface with the specified id. timeout specifies a timeout for the receive operation as follows: +

    +

    This function is "split" in two parts: a platform-independent part that is implemented in src/common.c, and a platform-dependent part that must be implemented by each +platform in a function named platform_s_uart_recv (see below).

    +

    int platform_s_uart_recv( unsigned id, s32 timeout );

    +

    This is the platform-dependent part of the UART receive function platform_uart_recv (see above), and is in fact a "subset" of the full function (thus being easier to implement). +It receives a byte from the UART interface with the specified id. timeout +specifies a timeout for the receive operation that can be either 0 or PLATFORM_UART_INFINITE_TIMEOUT, see the platform_uart_recv function (above) for a full description of the +timeout argument. NOTE: this function doesn't have to deal with the timeout > 0 case, this is handled by the generic implementation in src/common.c.

    + + diff --git a/src/common.c b/src/common.c index 1b7eb957..d46704a7 100644 --- a/src/common.c +++ b/src/common.c @@ -274,7 +274,7 @@ void cmn_virtual_timer_cb() int platform_timer_exists( unsigned id ) { #if VTMR_NUM_TIMERS > 0 - if( TIMER_IS_VIRTUAL( id ) ) + if( id >= VTMR_FIRST_ID ) return TIMER_IS_VIRTUAL( id ); else #endif