BSP430
20141115
Board Support Package for MSP430 microcontrollers
|
Declarations for abstracted serial interface. More...
Go to the source code of this file.
Macros | |
#define | configBSP430_SERIAL_USE_USCI defined(__MSP430_HAS_USCI__) |
#define | configBSP430_SERIAL_USE_USCI5 defined(__MSP430_HAS_USCI_A0__) |
#define | configBSP430_SERIAL_USE_EUSCI (defined(__MSP430_HAS_EUSCI_A0__) || defined(__MSP430_HAS_EUSCI_B0__)) |
#define | configBSP430_SERIAL_ENABLE_UART (configBSP430_CONSOLE - 0) |
#define | configBSP430_SERIAL_ENABLE_SPI 0 |
#define | BSP430_SERIAL_SPI_BUS_SPEED_HZ 4000000UL |
#define | configBSP430_SERIAL_ENABLE_I2C 0 |
#define | BSP430_SERIAL_I2C_BUS_SPEED_HZ 400000UL |
#define | BSP430_SERIAL |
#define | BSP430_SERIAL_ADJUST_CTL0_INITIALIZER(_i) (((_i) >= 0x100) ? ((_i) >> 8) : (_i)) |
#define | BSP430_I2C_SPIN_LIMIT 65536 |
#define | BSP430_I2C_ERRFLAG_PROTOCOL 0x4000 |
#define | BSP430_I2C_ERRFLAG_SPINLIMIT 0x2000 |
Declarations for abstracted serial interface.
The MSP430 line has a variety of peripherals that support serial communications, including the origin 2xx/4xx family USCI, 5xx USCI, and 5xx eUSCI. These are sufficiently similar in function that it is useful to have a hardware abstraction layer that delegates to the peripherals available on the current platform.
#define BSP430_I2C_ERRFLAG_PROTOCOL 0x4000 |
Bit set in absolute value of I2C error codes to indicate a protocol error.
I2C error codes are negative values. The absolute value may encode information on the cause of an error.
If BSP430_I2C_ERRFLAG_PROTOCOL is set in the absolute value, the low 8 bits encode peripheral-specific error data, taken from (for example) UCBxIFG. Generally protocol errors include receipt of a NACK or loss of arbitration.
#define BSP430_I2C_ERRFLAG_SPINLIMIT 0x2000 |
Bit set in absolute value of I2C error codes to indicate spin limit exceeded.
I2C error codes are negative values. The absolute value may encode information on the cause of an error.
If BSP430_I2C_ERRFLAG_SPINLIMIT is set in the absolute value then a loop awaiting an expected condition executed for BSP430_I2C_SPIN_LIMIT times without detecting the desired condition. The I2C operation aborts immediately, but no restorative action is taken by the infrastructure.
#define BSP430_I2C_SPIN_LIMIT 65536 |
Control the duration of I2C loops waiting for bus conditions.
A positive value enables a loop limit; the number of loops attempted ranges from 1 to 65536. A zero or negative value disables the loop limit.
#define BSP430_SERIAL |
Defined by the infrastructure to a true expression in the case where at least one protocol is to be supported by the serial abstraction. If it evaluates to false, the serial abstraction will be absent from the compiled infrastructure code.
#define BSP430_SERIAL_ADJUST_CTL0_INITIALIZER | ( | _i | ) | (((_i) >= 0x100) ? ((_i) >> 8) : (_i)) |
When the underlying implementation is an EUSCI device (as on FR5xx chips), the header defines used to construct the value ctl0_byte
are specified for a 16-bit access. The ctl0 byte is the upper byte of the ctlw0 word that comprises ctl0 and ctl1 on these MCUs. On those devices you must use the _H
suffix to select the high-byte version of these constants or divide your configured value by 256 to place it in the low byte of the argument. This applies to configuration values UCPEN, UCPAR, UCMSB, UC7BIT, UCSPB, UCCKPH, UCCKPL, UCMST, and UCMODE_0 or UCMODE_1 or UCMODE_2, and perhaps others.
Given a ctl0_byte
expression comprised of these constants, this macro will adjust it for you when the value appears to be outside the expected range (i.e., greater than 0xFF
) and will leave the values alone when it is not, so you can use the standard header constant names without having to worry about the underlying implementation.
#define BSP430_SERIAL_I2C_BUS_SPEED_HZ 400000UL |
Default speed for I2C bus transactions when application does not specify divisor.
This is referenced by hBSP430serialOpenI2C() when the prescaler
parameter is zero.
As most I2C devices accept the V1.0 specification maximum, the default value should be 400kHz.
#define BSP430_SERIAL_SPI_BUS_SPEED_HZ 4000000UL |
Default speed for SPI bus transactions when application does not specify divisor.
This is referenced by hBSP430serialOpenSPI() when the prescaler
parameter is zero.
SPI flash can often work at 20MHz; the ChipCon radios are good to 6MHz generally. The default will be 4MHz although there are chips that will not operate at that speed (e.g., the CMA3000 accelerometer).
#define configBSP430_SERIAL_ENABLE_I2C 0 |
Define to a true value to allow the general serial layer to recognize and dispatch I2C-related functions. If left false, I2C-related functions will not be added to the serial abstraction dispatch table.
#define configBSP430_SERIAL_ENABLE_SPI 0 |
Define to a true value to allow the general serial layer to recognize and dispatch SPI-related functions. If left false, SPI-related functions will not be added to the serial abstraction dispatch table.
#define configBSP430_SERIAL_ENABLE_UART (configBSP430_CONSOLE - 0) |
Define to a true value to allow the general serial layer to recognize and dispatch UART-related functions. If left false, UART-related functions will not be added to the serial abstraction dispatch table.
#define configBSP430_SERIAL_USE_EUSCI (defined(__MSP430_HAS_EUSCI_A0__) || defined(__MSP430_HAS_EUSCI_B0__)) |
Define to true value to allow the generic serial dispatches to recognize sBSP430hplEUSCIA and sBSP430hplEUSCIB as underlying serial implementation. This defaults to true iff the MCU supports the eUSCI device as implemented on 5xx MCUs.
#define configBSP430_SERIAL_USE_USCI defined(__MSP430_HAS_USCI__) |
Define to true value to allow the generic serial dispatches to recognize sBSP430hplUSCI as an underlying serial implementation. This defaults to true iff the MCU supports the USCI device as implemented on 2xx/4xx MCUs.
#define configBSP430_SERIAL_USE_USCI5 defined(__MSP430_HAS_USCI_A0__) |
Define to true value to allow the generic serial dispatches to recognize sBSP430hplUSCI5 as an underlying serial implementation. This defaults to true iff the MCU supports the USCI device as implemented on 5xx MCUs.
|
static |
Get the HAL handle for a specific serial peripheral.
periph | The handle identifier, such as BSP430_PERIPH_USCI_A0. |
|
static |
Request and configure a serial device in I2C mode.
hal | the handle for the HAL interface for the serial device to be configured. These are found from the peripheral handle using hBSP430serialLookup(). |
ctl0_byte | The configuration to be written to the device's ctl0 byte. For I2C mode, potential values specified in the <msp430.h> header include UCA10, UCSLA10, UCMM, and UCMST (but see the warning above). The UCSYNC field is cleared and the UCMODE field is configured for I2C before being written. |
ctl0_byte
header constants.ctl1_byte | The configuration to be written to the device's ctl1 byte. For I2C mode, potential values specified in the <msp430.h> header are configuration of the clock (UCSSEL_0, UCSSEL_1, UCSSEL_2, or UCSSEL_3) and perhaps UCTR. The UCSWRST field is controlled by the function. If prescaler is zero, the clock configuration in this will be overridden to select SMCLK. |
prescaler | The value by which the clock selected in ctl1_byte is divided to produce the I2C clock. A value of zero will select the minimum of 1 and ceil(ulBSP430clockSMCLK_Hz()/BSP430_SERIAL_I2C_BUS_SPEED_HZ), resulting in the highest speed that does not exceed BSP430_SERIAL_I2C_BUS_SPEED_HZ. Use of this feature will override the clock selection in ctl1_byte . |
prescaler
).
|
static |
Request and configure a serial device in SPI mode.
hal | the handle for the HAL interface for the serial device to be configured. These are found from the peripheral handle using hBSP430serialLookup(). |
ctl0_byte | The configuration to be written to the device's ctl0 byte. For SPI mode, potential values specified in the <msp430.h> header include UCCKPH, UCCKPL, UCMSB, UC7BIT, UCMST, and UCMODE_0 or UCMODE_1 or UCMODE_2. The UCSYNC field is cleared before being written. Selection of UCMODE_3 (I2C mode) will result in this function returning an error. |
ctl0_byte
header constants.Mode | CPOL | CPHA | ctl0_byte |
---|---|---|---|
0 | 0 | 0 | UCCKPH |
1 | 0 | 1 | 0 |
2 | 1 | 0 | UCCKPH | UCCKPL |
3 | 1 | 1 | UCCKPL |
ctl1_byte | The configuration to be written to the device's ctl1 byte. For SPI mode, potential values specified in the <msp430.h> header are only configuration of the clock: UCSSEL_0, UCSSEL_1, UCSSEL_2, UCSSEL_3. The UCSWRST field is controlled by the function. If prescaler is zero, the clock configuration in this will be overridden to select SMCLK. |
prescaler | The value by which the clock selected in ctl1_byte is divided to produce the SPI clock. A value of zero will select the minimum of 1 and ceil(ulBSP430clockSMCLK_Hz()/BSP430_SERIAL_SPI_BUS_SPEED_HZ), resulting in the highest speed that does not exceed BSP430_SERIAL_SPI_BUS_SPEED_HZ. Use of this feature will override the clock selection in ctl1_byte . |
ctl0_byte
or a zero-valued prescaler
).
|
static |
Request and configure a serial device in UART mode.
hal | the handle for the HAL interface for the serial device to be configured. These are found from the peripheral handle using hBSP430serialLookup(). |
ctl0_byte | The configuration to be written to the device's ctl0 byte. For UART mode, potential values specified in the <msp430.h> header include UCPEN, UCPAR, UCMSB, UC7BIT, UCSPB. The UCMODE and UCSYNC elements of the byte are ignored. In most cases for UART mode a value of 0 producing 8N1 serial communications is appropriate. |
ctl0_byte
header constants.ctl1_byte | The configuration to be written to the device's ctl1 byte. For UART mode, potential values specified in the <msp430.h> header include UCRXEIE, UCBRKIE, and UCDORM. The UCSSEL and UCSWRST elements of the byte are cleared as the function configures those. In most cases a value of 0 is appropriate. |
baud | The desired baud rate. This will be configured automatically. ACLK will be used as the baud rate clock if ACLK is both at least 20 kHz and at least three times faster than the requested baud rate; otherwise SMCLK will be used. The function invokes ulBSP430clockSMCLK_Hz() and ulBSP430clockACLK_Hz() as necessary to determine the actual speed of the baud rate clock. |
|
static |
Receive using a master I2C-configured device
This routine receives rx_len
octets into rx_data
, storing the octets received in response into rx_data
.
This routine should only be invoked when hal->tx_cbchain_ni is null. If a callback is present, it is expected to be used to provide data for transmission. Note that such a callback must handle I2C start and stop conditions, which are peripheral-specific.
hal | the serial device from which the data is received |
rx_data | where to store the data. The space available must be at least rx_len octets. |
rx_len | the number of bytes expected in response. A transaction reading more than 255 bytes may be rejected. |
rx_data
, or a negative error code (see BSP430_I2C_ERRFLAG_PROTOCOL and BSP430_I2C_ERRFLAG_SPINLIMIT). This function will not return -1, reserving that as a generic error code for higher-level functions.
|
static |
Configure I2C addresses
This routine sets the own-address and slave-address registers of an I2C peripheral. The device should have been opened as an I2C device prior to invoking this function.
own_address
is to be set.hal | the serial device to be configured |
own_address | the value to use as this device's address. A negative value leaves the current configured own address unchanged. |
slave_address | the value to use as the slave address. A negative value leaves the current configured slave address unchanged. |
|
static |
Transmit using a master I2C-configured device
This routine transmits tx_len
octets from tx_data
.
This routine should only be invoked when hal->tx_cbchain_ni is null. If a callback is present, it is expected to be used to provide data for transmission. Note that such a callback must handle I2C start and stop conditions, which are peripheral-specific.
hal | the serial device over which the data is transmitted and received |
tx_data | the data to be transmitted |
tx_len | the number of bytes to transmit. A transaction writing more than 255 bytes may be rejected. |
|
static |
Release a serial device.
This places the device into reset mode and resets the peripheral pins to port function. It does not release or disassociate any callbacks.
hal | a serial HAL handle previously obtained through hBSP430serialOpenUART(), hBSP430serialOpenSPI(), or hBSP430serialOpenI2C(). |
|
static |
Control serial device hold mode
When a serial peripheral is placed in hold mode the peripheral is reset per iBSP430serialSetReset_rh() with resetp set to a negative value to force a wait for pending activity to complete. In addition, the function reconfigures the associated port pins to their digital I/O function per iBSP430platformConfigurePeripheralPins_ni().
When the hold is released, the port pins are reconfigured to their peripheral function per iBSP430platformConfigurePeripheralPins_ni(), and the device is taken out of reset mode.
hal | a serial HAL handle to a peripheral that has been opened |
holdp | a nonzero value if the peripheral is to be placed into hold mode, and a zero value to release it from hold mode |
|
static |
Control serial device reset mode.
When placed into reset mode, the UCSWRST bit (or peripheral-specific analog) is set, clearing all errors and stopping all activity but retaining all configuration information. Interrupts are disabled. When the hold is released the UCSWRST bit is cleared, and any interrupts for which callbacks are registered are re-enabled. (Note that enabling the interrupts at the peripheral level does not change the GIE state, which should be cleared while this function is executing.)
When the peripheral is in I2C mode, any pending UCTXNACK, UCTXSTP, UCTXSTT, or similar family-specific requests will be cleared before the reset is released.
Placing a serial peripheral into reset mode prior to entering a low power mode will often reduce current consumption. Entering and leaving reset mode is also the simplest way to clear peripheral errors and flush pending data.
hal | a serial HAL handle to a peripheral that has been opened |
resetp | A positive value places peripheral into reset mode. A negative value places peripheral into reset mode after blocking until peripheral is no longer busy. A zero value releases peripheral from reset mode. |
|
static |
Transmit and receive using a SPI-configured device
This routine transmits tx_len
octets from tx_data
, storing the octets received in response into rx_data
. It then transmits rx_len
dummy bytes, appending the resulting response into rx_data
.
This routine should only be invoked when hal->tx_cbchain_ni
and hal->rx_cbchain_ni
are null. If callbacks are present, they are expected to be used to provide data for transmission and to process received data.
hal | the serial device over which the data is transmitted and received |
tx_data | the data to be transmitted (generally, a command). The pointer may be null only if tx_len is zero. |
tx_len | the number of bytes to transmit as the command. The value may be zero if this call is reading additional data resulting from a previous command. |
rx_len | the number of additional bytes expected in response, exclusive of the synchronous responses to bytes transmitted from tx_data . See BSP430_SERIAL_SPI_READ_TX_BYTE regarding the content of the dummy bytes that are transmitted to trigger reception. |
rx_data | where to store the responses received during the transmit and receive phases. A null pointer may be passed if the incoming data is not of interest. If the pointer is not null, the space available must be at least tx_len + rx_len . |
rx_data
(or that would have been stored if rx_data
were not null), or -1 if an error occcured.
|
static |
Receive a byte from a UART-configured device.
This routine should only be invoked when hal->rx_cbchain_ni is null. If a callback is present, it is expected to be used to accept data on reception.
hal | the serial device over which the data should be transmitted |
|
static |
Transmit a sequence of characters over a UART-configured device.
This routine should only be invoked when hal->tx_cbchain_ni is null. If a callback is present, it is expected to be used to provide data for transmission.
hal | the serial device over which the data should be transmitted |
str | a NUL-terminated sequence of character data to be transmitted. The NUL serves only as a termination marker, and is not transmitted. |
|
static |
Transmit a byte over a UART-configured device.
This routine should only be invoked when hal->tx_cbchain_ni is null. If a callback is present, it is expected to be used to provide data for transmission.
hal | the serial device over which the data should be transmitted |
c | a data byte to be transmitted |
c
if transmitted, or -1 if an error occurred.
|
static |
Transmit a block of data over a UART-configured device.
This routine should only be invoked when hal->tx_cbchain_ni is null. If a callback is present, it is expected to be used to provide data for transmission.
hal | the serial device over which the data should be transmitted |
data | pointer to the start of a sequence of data to transmit |
len | the number of octets to be transmitted |
unsigned int uiBSP430serialSMCLKPrescaler | ( | unsigned long | freq_Hz | ) |
Calculate prescaler to obtain desired clock from SMCLK.
The prescaler returned is designed to produce the fastest clock based on an integer division of ulBSP430clockSMCLK_Hz() that does not exceed freq_Hz
, except that if freq_Hz
is too slow to be produced by dividing SMCLK by a 16-bit prescaler then 65535 will be returned.
freq_Hz | Desired clock frequency in Hz. |
|
static |
Return the frequency of the serial interface.
If hal
is configured in UART mode, this is the baud rate. In all other modes it's the bus frequency.
hal | the serial device for which frequency is desired |
|
static |
Spin until any in-progress transmission or reception is complete.
This is used to ensure the device is idle prior to reconfiguring it.
hal | a serial HAL handle |
|
static |
Wake up the interrupt-driven transmission if necessary.
Normally the transmission infrastructure transmits data as soon as space is available in the transmission buffer. The infrastructure is disabled when the sBSP430halSERIAL.tx_cbchain_ni indicates that no more data is available. When this has happened, it must be told that more data has been added and the infrastructure re-enabled.
For efficiency, this should only be called if it is believed that data is ready to be presented in a transmission callback, but that the transmission infrastructure may be idle.
hal | a serial HAL handle |
const char* xBSP430serialName | ( | tBSP430periphHandle | periph | ) |
Get a human-readable identifier for the serial peripheral
periph | The handle identifier, such as BSP430_PERIPH_USCI_A0. |