BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Data Structures | Macros | Typedefs | Functions
m25p.h File Reference

Support for M25P-compatible serial SPI flash memory devices. More...

#include <bsp430/core.h>
#include <bsp430/serial.h>
#include <bsp430/periph/port.h>

Go to the source code of this file.

Data Structures

struct  sBSP430m25p
 

Macros

#define configBSP430_PLATFORM_M25P   0
 
#define BSP430_PLATFORM_M25P   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_SPI_CTL0_BYTE   BSP430_SERIAL_ADJUST_CTL0_INITIALIZER(UCCKPL | UCMSB | UCMST)
 
#define BSP430_PLATFORM_M25P_SPI_PERIPH_HANDLE   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_PWR_PORT_PERIPH_HANDLE   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_PWR_PORT_BIT   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_RSTn_PORT_PERIPH_HANDLE   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_RSTn_PORT_BIT   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_CSn_PORT_PERIPH_HANDLE   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_CSn_PORT_BIT   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_SUPPORTS_PE   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_SUPPORTS_PW   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_SECTOR_SIZE   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_SUBSECTOR_SIZE   include <bsp430/platform.h>
 
#define BSP430_PLATFORM_M25P_SECTOR_COUNT   include <bsp430/platform.h>
 
#define BSP430_M25P_CMD_WREN   0x06
 
#define BSP430_M25P_CMD_WRDI   0x04
 
#define BSP430_M25P_CMD_RDID   0x9f
 
#define BSP430_M25P_CMD_RDSR   0x05
 
#define BSP430_M25P_CMD_WRSR   0x01
 
#define BSP430_M25P_CMD_WRLR   0xe5
 
#define BSP430_M25P_CMD_RDLR   0xe8
 
#define BSP430_M25P_CMD_READ   0x03
 
#define BSP430_M25P_CMD_FAST_READ   0x0b
 
#define BSP430_M25P_CMD_PW   0x0a
 
#define BSP430_M25P_CMD_PP   0x02
 
#define BSP430_M25P_CMD_PE   0xdb
 
#define BSP430_M25P_CMD_SSE   0x20
 
#define BSP430_M25P_CMD_SE   0xd8
 
#define BSP430_M25P_CMD_BE   0xc7
 
#define BSP430_M25P_CMD_DP   0xb9
 
#define BSP430_M25P_CMD_RELDP   0xab
 
#define BSP430_M25P_CMD_RES   0xab
 
#define BSP430_M25P_SR_WIP   0x01
 
#define BSP430_M25P_SR_WEL   0x02
 
#define BSP430_M25P_SR_BP0   0x04
 
#define BSP430_M25P_SR_BP1   0x08
 
#define BSP430_M25P_SR_SRWD   0x80
 
#define BSP430_M25P_RESET_SET(dev_)
 
#define BSP430_M25P_RESET_CLEAR(dev_)
 
#define BSP430_M25P_CS_ASSERT(dev_)
 
#define BSP430_M25P_CS_DEASSERT(dev_)
 

Typedefs

typedef struct sBSP430m25p sBSP430m25p
 
typedef sBSP430m25phBSP430m25p
 

Functions

hBSP430m25p hBSP430m25pInitialize (hBSP430m25p dev, unsigned char ctl0_byte, unsigned char ctl1_byte, unsigned int prescaler)
 
int iBSP430m25pStatus_rh (hBSP430m25p dev)
 
static BSP430_CORE_INLINE int iBSP430m25pStatus (hBSP430m25p dev)
 
int iBSP430m25pStrobeCommand_rh (hBSP430m25p dev, uint8_t cmd)
 
int iBSP430m25pStrobeAddressCommand_rh (hBSP430m25p dev, uint8_t cmd, unsigned long addr)
 
int iBSP430m25pInitiateCommand_rh (hBSP430m25p dev, uint8_t cmd)
 
int iBSP430m25pInitiateAddressCommand_rh (hBSP430m25p dev, uint8_t cmd, unsigned long addr)
 
int iBSP430m25pCompleteTxRx_rh (hBSP430m25p dev, const uint8_t *tx_data, size_t tx_len, size_t rx_len, uint8_t *rx_data)
 

Detailed Description

Support for M25P-compatible serial SPI flash memory devices.

Micron/Numonyx serial SPI flash devices such as the M25P16 and M25PE20 are commonly used on MSP430-based wireless sensor platforms. This module defines the command and status constants for the M25P interface; a structure holding the required SPI, CS#, and RESET# information to interact with the device, and declarations for module functions that encode and transmit commands.

The module also supports configBSP430_PLATFORM_M25P and BSP430_PLATFORM_M25P to allow applications to request access to a platform-supplied flash memory chip without encoding platform dependencies in the source code.

As with other parts of BSP430, this is a low-level interface that does not provide convenience wrappers for functions like erasing a sector or chip, or reading and writing the device contents. It does provide enough to allow the application to do this, including the ability to initiate a read or write operation and complete it using programmed I/O (iBSP430m25pCompleteTxRx_rh()), interrupt-driven SPI transactions, or DMA, based on the application's needs.

Note
The commands constants defined in this module (e.g., BSP430_M25P_CMD_PW) cover all known M25P implementations. Not all commands are supported on all devices.

M25PE20 SPI flash memory on TrxEB or SuRF shows how to use this interface.

Homepage
http://github.com/pabigot/bsp430

Macro Definition Documentation

#define BSP430_M25P_CMD_BE   0xc7

BULK ERASE command

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_DP   0xb9

DEEP POWER-DOWN command

#define BSP430_M25P_CMD_FAST_READ   0x0b

READ DATA BYTES at HIGHER SPEED command

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_PE   0xdb

PAGE ERASE command. Not available on all devices. See BSP430_PLATFORM_M25P_SUPPORTS_PE.

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_PP   0x02

PAGE PROGRAM command.

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_PW   0x0a

PAGE WRITE command. Not available on all devices. See BSP430_PLATFORM_M25P_SUPPORTS_PW.

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_RDID   0x9f

READ IDENTIFICATION command.

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_RDLR   0xe8

READ LOCK REGISTER command

#define BSP430_M25P_CMD_RDSR   0x05

READ STATUS REGISTER command

#define BSP430_M25P_CMD_READ   0x03

READ DATA BYTES command

#define BSP430_M25P_CMD_RELDP   0xab

RELEASE from DEEP POWER-DOWN command

#define BSP430_M25P_CMD_RES   0xab

READ ELECTRONIC SIGNATURE command. Overloads BSP430_M25P_CMD_RELDP on some devices.

#define BSP430_M25P_CMD_SE   0xd8

SECTOR ERASE command

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_SSE   0x20

SUBSECTOR ERASE command. Not available on all devices. BSP430_PLATFORM_M25P_SUBSECTOR_SIZE may indicate that support is available.

#define BSP430_M25P_CMD_WRDI   0x04

WRITE DISABLE command

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_WREN   0x06

WRITE ENABLE command

Examples:
utility/m25p/main.c.
#define BSP430_M25P_CMD_WRLR   0xe5

WRITE to LOCK REGISTER command

#define BSP430_M25P_CMD_WRSR   0x01

WRITE STATUS REGISTER command.

#define BSP430_M25P_CS_ASSERT (   dev_)
Value:
do { \
(dev_)->csn_port->out &= ~(dev_)->csn_bit; \
} while (0)

Assert the CS# signal in preparation for interacting with the device.

#define BSP430_M25P_CS_DEASSERT (   dev_)
Value:
do { \
(dev_)->csn_port->out |= (dev_)->csn_bit; \
} while (0)

De-assert the CS# signal after interacting with the device.

#define BSP430_M25P_RESET_CLEAR (   dev_)
Value:
do { \
if (NULL != (dev_)->rstn_port) { \
(dev_)->rstn_port->out |= (dev_)->rstn_bit; \
} \
} while (0)

Clear the RESET# signal to take the device out of reset

Examples:
utility/m25p/main.c.
#define BSP430_M25P_RESET_SET (   dev_)
Value:
do { \
if (NULL != (dev_)->rstn_port) { \
(dev_)->rstn_port->out &= ~(dev_)->rstn_bit; \
} \
} while (0)

Set the RESET# signal to place the device into reset

#define BSP430_M25P_SR_BP0   0x04

Block protect bit BP0 within M25P status register. Not available on all devices.

#define BSP430_M25P_SR_BP1   0x08

Block protect bit BP1 within M25P status register. Not available on all devices.

#define BSP430_M25P_SR_SRWD   0x80

Status-register-write-disable bit within M25P status register. Not available on all devices.

#define BSP430_M25P_SR_WEL   0x02

Write-enable-latch bit within M25P status register.

#define BSP430_M25P_SR_WIP   0x01

Write-in-progress bit within M25P status register. Bit is read-only.

Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P   include <bsp430/platform.h>

Indicate that an M25P serial flash device is available on the platform. This is set by the platform-specific header when configBSP430_PLATFORM_M25P is true and the platform supports an M25P device.

See M25PE20 SPI flash memory on TrxEB or SuRF for how to use the following identifiers to configure and use the platform device:

C Preprocessor Only:
This macro may have a value that restricts its use to C preprocessor conditional directives.
Dependency:
configBSP430_PLATFORM_M25P
Defaulted:
The value here is superseded by previously encountered definitions.
#define BSP430_PLATFORM_M25P_CSn_PORT_BIT   include <bsp430/platform.h>

Port bit on BSP430_PLATFORM_M25P_CSn_PORT_PERIPH_HANDLE for SPI flash CSn

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_CSn_PORT_PERIPH_HANDLE   include <bsp430/platform.h>

BSP430 peripheral handle for port on which SPI flash chip-select (inverted) is placed.

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_PWR_PORT_BIT   include <bsp430/platform.h>

Port bit on BSP430_PLATFORM_M25P_PWR_PORT_PERIPH_HANDLE for SPI flash power

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_PWR_PORT_PERIPH_HANDLE   include <bsp430/platform.h>

BSP430 peripheral handle for port on which SPI flash power is placed.

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_RSTn_PORT_BIT   include <bsp430/platform.h>

Port bit on BSP430_PLATFORM_M25P_RSTn_PORT_PERIPH_HANDLE for SPI flash power

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_RSTn_PORT_PERIPH_HANDLE   include <bsp430/platform.h>

BSP430 peripheral handle for port on which SPI flash power is placed.

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_SECTOR_COUNT   include <bsp430/platform.h>

The number of sectors in the platform M25P device.

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_SECTOR_SIZE   include <bsp430/platform.h>

The number of bytes in each sector of the platform M25P device.

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_SPI_CTL0_BYTE   BSP430_SERIAL_ADJUST_CTL0_INITIALIZER(UCCKPL | UCMSB | UCMST)

SPI initialization constant required for M25P serial flash.

This value may be used by applications as the argument for ctl0_byte to hBSP430serialOpenSPI() when configuring SPI for an M25P serial flash memory.

Note
The M25P serial flash operates correctly in either of two modes.
  • Mode 0 (CPOL=CPHA=0, or UCCKPH) in which clock is low when inactive
  • Mode 3 (CPOL=CPHA=1, or UCCKPL) in which clock is high when inactive
The default value for this constant provides the inactive-high configuration ("mode 3").
Defaulted:
The value here is superseded by previously encountered definitions.
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_SPI_PERIPH_HANDLE   include <bsp430/platform.h>

Peripheral handle for SPI access to platform-provided M25P serial flash

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_SUBSECTOR_SIZE   include <bsp430/platform.h>

The number of bytes in each subsector of the platform M25P device.

Undefined if the device does not support subsector operations.

Dependency:
BSP430_PLATFORM_M25P
Examples:
utility/m25p/main.c.
#define BSP430_PLATFORM_M25P_SUPPORTS_PE   include <bsp430/platform.h>

Defined to a true value iff the M25P flash on the platform supports the BSP430_M25P_CMD_PE PAGE ERASE command. If false or absent, assume the device may be erased only by using BSP430_M25P_CMD_SE or BSP430_M25P_CMD_BE.

#define BSP430_PLATFORM_M25P_SUPPORTS_PW   include <bsp430/platform.h>

Defined to a true value iff the M25P flash on the platform supports the BSP430_M25P_CMD_PW PAGE WRITE command. If false or absent, assume the device may be written only by following a page erase, subsector erase, sector erase, or bulk erase with BSP430_M25P_CMD_PP.

#define configBSP430_PLATFORM_M25P   0

Define to request that platform enable its M25P flash.

Some platforms have an external serial flash memory suitable for control by the <bsp430/utility/m25p.h> interface. Defining this macro in your <bsp430_config.h> will register a request for that device. Define to a true value to enable any platform-provided M25P serial flash memory.

C Preprocessor Only:
This macro may have a value that restricts its use to C preprocessor conditional directives.
Affects:
BSP430_PLATFORM_M25P
Defaulted:
The value here is superseded by previously encountered definitions.

Typedef Documentation

Handle used to access an M25P-based serial SPI flash device.

typedef struct sBSP430m25p sBSP430m25p

Information required to access an M25P-based serial SPI flash device. Boards that allow control of power to the device must do so externally from this module.

Function Documentation

hBSP430m25p hBSP430m25pInitialize ( hBSP430m25p  dev,
unsigned char  ctl0_byte,
unsigned char  ctl1_byte,
unsigned int  prescaler 
)

Initialize an M25P device.

This function initializes the SPI bus and peripheral ports to interact with the device. The SPI bus is configured as specified by the parameters. The CS# and RESET# ports are configured to output mode, with CS# de-asserted (not selected) and RESET# asserted (held in reset mode).

To comply with device power-up requirements, power should not be supplied to the device until this function has completed. Further interaction with the device should be delayed for a device-specific period following application of power.

Parameters
devthe M25P device handle
ctl0_byteas with hBSP430serialOpenSPI()
ctl1_byteas with hBSP430serialOpenSPI()
prescaleras with hBSP430serialOpenSPI()
Returns
dev if the initialization was successful, or a null handle if it failed.
Examples:
utility/m25p/main.c.
int iBSP430m25pCompleteTxRx_rh ( hBSP430m25p  dev,
const uint8_t *  tx_data,
size_t  tx_len,
size_t  rx_len,
uint8_t *  rx_data 
)

Complete an initiated command using programmed I/O.

This simply wraps iBSP430spiTxRx_rh() to transmit data to the device and/or receive data from the device, ensuring that BSP430_M25P_CS_DEASSERT() is invoked to complete the transaction initiated by iBSP430m25pInitiateCommand_rh() or iBSP430m25pInitiateAddressCommand_rh().

Parameters
devthe M25P device handle
tx_dataas with iBSP430spiTxRx_rh()
tx_lenas with iBSP430spiTxRx_rh()
rx_lenas with iBSP430spiTxRx_rh()
rx_dataas with iBSP430spiTxRx_rh()
Returns
as with iBSP430spiTxRx_rh()
Examples:
utility/m25p/main.c.
int iBSP430m25pInitiateAddressCommand_rh ( hBSP430m25p  dev,
uint8_t  cmd,
unsigned long  addr 
)

Initiate an M25P address-based command.

The CS# signal is asserted, the cmd transmitted, and the address transmitted. CS# is de-asserted if an error occurs; otherwise CS# is left asserted and the call returns allowing the caller to write additional data to the device or to receive data from the device through programmed I/O, interrupt-driven I/O, or DMA.

To use programmed I/O to complete a read or write operation, see iBSP430m25pCompleteTxRx_rh().

Note
CS# is left asserted after a successful call to this function.
Parameters
devthe M25P device handle
cmdthe command to be executed, e.g. BSP430_M25P_CMD_RDSR
addrthe address at which the command applies.
Returns
0 if successful, -1 on an error. If successful CS# is left asserted and the caller should invoke BSP430_M25P_CS_DEASSERT() after writing and reading command-specific data.
Examples:
utility/m25p/main.c.
int iBSP430m25pInitiateCommand_rh ( hBSP430m25p  dev,
uint8_t  cmd 
)

Initiate an M25P device command.

The CS# signal is asserted and the cmd transmitted. CS# is de-asserted if an error occurs; otherwise CS# is left asserted and the call returns allowing the caller to write additional data to the device or to receive data from the device through programmed I/O, interrupt-driven I/O, or DMA.

To use programmed I/O to complete a read or write operation, see iBSP430m25pCompleteTxRx_rh().

Note
CS# is left asserted after a successful call to this function.
Parameters
devthe M25P device handle
cmdthe command to be executed, e.g. BSP430_M25P_CMD_RDSR
Returns
0 if successful, -1 on an error. If successful CS# is left asserted and the caller should invoke BSP430_M25P_CS_DEASSERT() after writing and reading command-specific data.
Examples:
utility/m25p/main.c.
static BSP430_CORE_INLINE int iBSP430m25pStatus ( hBSP430m25p  dev)
static

Read the M25P device status register regardless of interrupt state.

This wraps iBSP430m25pStatus_rh() with code that saves the interrupt state, reads the status register, then restores the interrupt state prior to returning the result.

Examples:
utility/m25p/main.c.
int iBSP430m25pStatus_rh ( hBSP430m25p  dev)

Read the M25P device status register assuming interrupts are disabled.

The status register can be polled to detect completion of long-running operations such as BSP430_M25P_CMD_BE. Generally you will need to wait until BSP430_M25P_SR_WIP is clear before executing a new command.

Parameters
devthe M25P device handle
Returns
the non-negative value of the status register, or -1 if an error occurred.
Examples:
utility/m25p/main.c.
int iBSP430m25pStrobeAddressCommand_rh ( hBSP430m25p  dev,
uint8_t  cmd,
unsigned long  addr 
)

Strobe an M25P address-based command.

This interface is appropriate for commands like BSP430_M25P_CMD_PE which affect an address but require no data be written or read. It is functionally equivalent to iBSP430m25pInitiateAddressCommand_rh() followed immediately by BSP430_M25P_CS_DEASSERT().

Note that for many of these commands BSP430_M25P_SR_WIP will remain set for a device/command-specific period until the requested operation has completed.

Parameters
devthe M25P device handle
cmdthe command to be executed, e.g. BSP430_M25P_CMD_PE
addrthe address at which the command applies.
Returns
0 if successful, -1 on an error.
int iBSP430m25pStrobeCommand_rh ( hBSP430m25p  dev,
uint8_t  cmd 
)

Strobe an M25P device command.

The CS# signal is asserted, the cmd transmitted, and CS# is de-asserted. This interface is used for commands like BSP430_M25P_CMD_WREN and BSP430_M25P_CMD_BE that neither provide nor receive data and do not use an address.

Note that for many of these commands BSP430_M25P_SR_WIP will remain set for a device/command-specific period until the requested operation has completed.

Parameters
devthe M25P device handle
cmdthe command to be executed, e.g. BSP430_M25P_CMD_RDSR
Returns
0 on success, or a negative value to indicate an error.
Examples:
utility/m25p/main.c.