The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
<>
Date:
Tue Mar 14 16:20:51 2017 +0000
Revision:
138:093f2bd7b9eb
Parent:
128:9bcdf88f62b0
Child:
145:64910690c574
Release 138 of the mbed library

Ports for Upcoming Targets


Fixes and Changes

3716: fix for issue #3715: correction in startup files for ARM and IAR, alignment of system_stm32f429xx.c files https://github.com/ARMmbed/mbed-os/pull/3716
3741: STM32 remove warning in hal_tick_32b.c file https://github.com/ARMmbed/mbed-os/pull/3741
3780: STM32L4 : Fix GPIO G port compatibility https://github.com/ARMmbed/mbed-os/pull/3780
3831: NCS36510: SPISLAVE enabled (Conflict resolved) https://github.com/ARMmbed/mbed-os/pull/3831
3836: Allow to redefine nRF's PSTORAGE_NUM_OF_PAGES outside of the mbed-os https://github.com/ARMmbed/mbed-os/pull/3836
3840: STM32: gpio SPEED - always set High Speed by default https://github.com/ARMmbed/mbed-os/pull/3840
3844: STM32 GPIO: Typo correction. Update comment (GPIO_IP_WITHOUT_BRR) https://github.com/ARMmbed/mbed-os/pull/3844
3850: STM32: change spi error to debug warning https://github.com/ARMmbed/mbed-os/pull/3850
3860: Define GPIO_IP_WITHOUT_BRR for xDot platform https://github.com/ARMmbed/mbed-os/pull/3860
3880: DISCO_F469NI: allow the use of CAN2 instance when CAN1 is not activated https://github.com/ARMmbed/mbed-os/pull/3880
3795: Fix pwm period calc https://github.com/ARMmbed/mbed-os/pull/3795
3828: STM32 CAN API: correct format and type https://github.com/ARMmbed/mbed-os/pull/3828
3842: TARGET_NRF: corrected spi_init() to properly handle re-initialization https://github.com/ARMmbed/mbed-os/pull/3842
3843: STM32L476xG: set APB2 clock to 80MHz (instead of 40MHz) https://github.com/ARMmbed/mbed-os/pull/3843
3879: NUCLEO_F446ZE: Add missing AnalogIn pins on PF_3, PF_5 and PF_10. https://github.com/ARMmbed/mbed-os/pull/3879
3902: Fix heap and stack size for NUCLEO_F746ZG https://github.com/ARMmbed/mbed-os/pull/3902
3829: can_write(): return error code when no tx mailboxes are available https://github.com/ARMmbed/mbed-os/pull/3829

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 128:9bcdf88f62b0 1
<> 128:9bcdf88f62b0 2 /** \addtogroup hal */
<> 128:9bcdf88f62b0 3 /** @{*/
<> 128:9bcdf88f62b0 4 /* mbed Microcontroller Library
<> 128:9bcdf88f62b0 5 * Copyright (c) 2006-2013 ARM Limited
<> 128:9bcdf88f62b0 6 *
<> 128:9bcdf88f62b0 7 * Licensed under the Apache License, Version 2.0 (the "License");
<> 128:9bcdf88f62b0 8 * you may not use this file except in compliance with the License.
<> 128:9bcdf88f62b0 9 * You may obtain a copy of the License at
<> 128:9bcdf88f62b0 10 *
<> 128:9bcdf88f62b0 11 * http://www.apache.org/licenses/LICENSE-2.0
<> 128:9bcdf88f62b0 12 *
<> 128:9bcdf88f62b0 13 * Unless required by applicable law or agreed to in writing, software
<> 128:9bcdf88f62b0 14 * distributed under the License is distributed on an "AS IS" BASIS,
<> 128:9bcdf88f62b0 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 128:9bcdf88f62b0 16 * See the License for the specific language governing permissions and
<> 128:9bcdf88f62b0 17 * limitations under the License.
<> 128:9bcdf88f62b0 18 */
<> 128:9bcdf88f62b0 19 #ifndef MBED_SPI_API_H
<> 128:9bcdf88f62b0 20 #define MBED_SPI_API_H
<> 128:9bcdf88f62b0 21
<> 128:9bcdf88f62b0 22 #include "device.h"
<> 128:9bcdf88f62b0 23 #include "hal/dma_api.h"
<> 128:9bcdf88f62b0 24 #include "hal/buffer.h"
<> 128:9bcdf88f62b0 25
<> 128:9bcdf88f62b0 26 #if DEVICE_SPI
<> 128:9bcdf88f62b0 27
<> 128:9bcdf88f62b0 28 #define SPI_EVENT_ERROR (1 << 1)
<> 128:9bcdf88f62b0 29 #define SPI_EVENT_COMPLETE (1 << 2)
<> 128:9bcdf88f62b0 30 #define SPI_EVENT_RX_OVERFLOW (1 << 3)
<> 128:9bcdf88f62b0 31 #define SPI_EVENT_ALL (SPI_EVENT_ERROR | SPI_EVENT_COMPLETE | SPI_EVENT_RX_OVERFLOW)
<> 128:9bcdf88f62b0 32
<> 128:9bcdf88f62b0 33 #define SPI_EVENT_INTERNAL_TRANSFER_COMPLETE (1 << 30) // Internal flag to report that an event occurred
<> 128:9bcdf88f62b0 34
<> 128:9bcdf88f62b0 35 #define SPI_FILL_WORD (0xFFFF)
<> 128:9bcdf88f62b0 36
<> 128:9bcdf88f62b0 37 #if DEVICE_SPI_ASYNCH
<> 128:9bcdf88f62b0 38 /** Asynch SPI HAL structure
<> 128:9bcdf88f62b0 39 */
<> 128:9bcdf88f62b0 40 typedef struct {
<> 128:9bcdf88f62b0 41 struct spi_s spi; /**< Target specific SPI structure */
<> 128:9bcdf88f62b0 42 struct buffer_s tx_buff; /**< Tx buffer */
<> 128:9bcdf88f62b0 43 struct buffer_s rx_buff; /**< Rx buffer */
<> 128:9bcdf88f62b0 44 } spi_t;
<> 128:9bcdf88f62b0 45
<> 128:9bcdf88f62b0 46 #else
<> 128:9bcdf88f62b0 47 /** Non-asynch SPI HAL structure
<> 128:9bcdf88f62b0 48 */
<> 128:9bcdf88f62b0 49 typedef struct spi_s spi_t;
<> 128:9bcdf88f62b0 50
<> 128:9bcdf88f62b0 51 #endif
<> 128:9bcdf88f62b0 52
<> 128:9bcdf88f62b0 53 #ifdef __cplusplus
<> 128:9bcdf88f62b0 54 extern "C" {
<> 128:9bcdf88f62b0 55 #endif
<> 128:9bcdf88f62b0 56
<> 128:9bcdf88f62b0 57 /**
<> 128:9bcdf88f62b0 58 * \defgroup hal_GeneralSPI SPI Configuration Functions
<> 128:9bcdf88f62b0 59 * @{
<> 128:9bcdf88f62b0 60 */
<> 128:9bcdf88f62b0 61
<> 128:9bcdf88f62b0 62 /** Initialize the SPI peripheral
<> 128:9bcdf88f62b0 63 *
<> 128:9bcdf88f62b0 64 * Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral
<> 128:9bcdf88f62b0 65 * @param[out] obj The SPI object to initialize
<> 128:9bcdf88f62b0 66 * @param[in] mosi The pin to use for MOSI
<> 128:9bcdf88f62b0 67 * @param[in] miso The pin to use for MISO
<> 128:9bcdf88f62b0 68 * @param[in] sclk The pin to use for SCLK
<> 128:9bcdf88f62b0 69 * @param[in] ssel The pin to use for SSEL
<> 128:9bcdf88f62b0 70 */
<> 128:9bcdf88f62b0 71 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel);
<> 128:9bcdf88f62b0 72
<> 128:9bcdf88f62b0 73 /** Release a SPI object
<> 128:9bcdf88f62b0 74 *
<> 128:9bcdf88f62b0 75 * TODO: spi_free is currently unimplemented
<> 128:9bcdf88f62b0 76 * This will require reference counting at the C++ level to be safe
<> 128:9bcdf88f62b0 77 *
<> 128:9bcdf88f62b0 78 * Return the pins owned by the SPI object to their reset state
<> 128:9bcdf88f62b0 79 * Disable the SPI peripheral
<> 128:9bcdf88f62b0 80 * Disable the SPI clock
<> 128:9bcdf88f62b0 81 * @param[in] obj The SPI object to deinitialize
<> 128:9bcdf88f62b0 82 */
<> 128:9bcdf88f62b0 83 void spi_free(spi_t *obj);
<> 128:9bcdf88f62b0 84
<> 128:9bcdf88f62b0 85 /** Configure the SPI format
<> 128:9bcdf88f62b0 86 *
<> 128:9bcdf88f62b0 87 * Set the number of bits per frame, configure clock polarity and phase, shift order and master/slave mode.
<> 128:9bcdf88f62b0 88 * The default bit order is MSB.
<> 128:9bcdf88f62b0 89 * @param[in,out] obj The SPI object to configure
<> 128:9bcdf88f62b0 90 * @param[in] bits The number of bits per frame
<> 128:9bcdf88f62b0 91 * @param[in] mode The SPI mode (clock polarity, phase, and shift direction)
<> 128:9bcdf88f62b0 92 * @param[in] slave Zero for master mode or non-zero for slave mode
<> 128:9bcdf88f62b0 93 */
<> 128:9bcdf88f62b0 94 void spi_format(spi_t *obj, int bits, int mode, int slave);
<> 128:9bcdf88f62b0 95
<> 128:9bcdf88f62b0 96 /** Set the SPI baud rate
<> 128:9bcdf88f62b0 97 *
<> 128:9bcdf88f62b0 98 * Actual frequency may differ from the desired frequency due to available dividers and bus clock
<> 128:9bcdf88f62b0 99 * Configures the SPI peripheral's baud rate
<> 128:9bcdf88f62b0 100 * @param[in,out] obj The SPI object to configure
<> 128:9bcdf88f62b0 101 * @param[in] hz The baud rate in Hz
<> 128:9bcdf88f62b0 102 */
<> 128:9bcdf88f62b0 103 void spi_frequency(spi_t *obj, int hz);
<> 128:9bcdf88f62b0 104
<> 128:9bcdf88f62b0 105 /**@}*/
<> 128:9bcdf88f62b0 106 /**
<> 128:9bcdf88f62b0 107 * \defgroup SynchSPI Synchronous SPI Hardware Abstraction Layer
<> 128:9bcdf88f62b0 108 * @{
<> 128:9bcdf88f62b0 109 */
<> 128:9bcdf88f62b0 110
<> 128:9bcdf88f62b0 111 /** Write a byte out in master mode and receive a value
<> 128:9bcdf88f62b0 112 *
<> 128:9bcdf88f62b0 113 * @param[in] obj The SPI peripheral to use for sending
<> 128:9bcdf88f62b0 114 * @param[in] value The value to send
<> 128:9bcdf88f62b0 115 * @return Returns the value received during send
<> 128:9bcdf88f62b0 116 */
<> 128:9bcdf88f62b0 117 int spi_master_write(spi_t *obj, int value);
<> 128:9bcdf88f62b0 118
<> 128:9bcdf88f62b0 119 /** Check if a value is available to read
<> 128:9bcdf88f62b0 120 *
<> 128:9bcdf88f62b0 121 * @param[in] obj The SPI peripheral to check
<> 128:9bcdf88f62b0 122 * @return non-zero if a value is available
<> 128:9bcdf88f62b0 123 */
<> 128:9bcdf88f62b0 124 int spi_slave_receive(spi_t *obj);
<> 128:9bcdf88f62b0 125
<> 128:9bcdf88f62b0 126 /** Get a received value out of the SPI receive buffer in slave mode
<> 128:9bcdf88f62b0 127 *
<> 128:9bcdf88f62b0 128 * Blocks until a value is available
<> 128:9bcdf88f62b0 129 * @param[in] obj The SPI peripheral to read
<> 128:9bcdf88f62b0 130 * @return The value received
<> 128:9bcdf88f62b0 131 */
<> 128:9bcdf88f62b0 132 int spi_slave_read(spi_t *obj);
<> 128:9bcdf88f62b0 133
<> 128:9bcdf88f62b0 134 /** Write a value to the SPI peripheral in slave mode
<> 128:9bcdf88f62b0 135 *
<> 128:9bcdf88f62b0 136 * Blocks until the SPI peripheral can be written to
<> 128:9bcdf88f62b0 137 * @param[in] obj The SPI peripheral to write
<> 128:9bcdf88f62b0 138 * @param[in] value The value to write
<> 128:9bcdf88f62b0 139 */
<> 128:9bcdf88f62b0 140 void spi_slave_write(spi_t *obj, int value);
<> 128:9bcdf88f62b0 141
<> 128:9bcdf88f62b0 142 /** Checks if the specified SPI peripheral is in use
<> 128:9bcdf88f62b0 143 *
<> 128:9bcdf88f62b0 144 * @param[in] obj The SPI peripheral to check
<> 128:9bcdf88f62b0 145 * @return non-zero if the peripheral is currently transmitting
<> 128:9bcdf88f62b0 146 */
<> 128:9bcdf88f62b0 147 int spi_busy(spi_t *obj);
<> 128:9bcdf88f62b0 148
<> 128:9bcdf88f62b0 149 /** Get the module number
<> 128:9bcdf88f62b0 150 *
<> 128:9bcdf88f62b0 151 * @param[in] obj The SPI peripheral to check
<> 128:9bcdf88f62b0 152 * @return The module number
<> 128:9bcdf88f62b0 153 */
<> 128:9bcdf88f62b0 154 uint8_t spi_get_module(spi_t *obj);
<> 128:9bcdf88f62b0 155
<> 128:9bcdf88f62b0 156 /**@}*/
<> 128:9bcdf88f62b0 157
<> 128:9bcdf88f62b0 158 #if DEVICE_SPI_ASYNCH
<> 128:9bcdf88f62b0 159 /**
<> 128:9bcdf88f62b0 160 * \defgroup AsynchSPI Asynchronous SPI Hardware Abstraction Layer
<> 128:9bcdf88f62b0 161 * @{
<> 128:9bcdf88f62b0 162 */
<> 128:9bcdf88f62b0 163
<> 128:9bcdf88f62b0 164 /** Begin the SPI transfer. Buffer pointers and lengths are specified in tx_buff and rx_buff
<> 128:9bcdf88f62b0 165 *
<> 128:9bcdf88f62b0 166 * @param[in] obj The SPI object that holds the transfer information
<> 128:9bcdf88f62b0 167 * @param[in] tx The transmit buffer
<> 128:9bcdf88f62b0 168 * @param[in] tx_length The number of bytes to transmit
<> 128:9bcdf88f62b0 169 * @param[in] rx The receive buffer
<> 128:9bcdf88f62b0 170 * @param[in] rx_length The number of bytes to receive
<> 128:9bcdf88f62b0 171 * @param[in] bit_width The bit width of buffer words
<> 128:9bcdf88f62b0 172 * @param[in] event The logical OR of events to be registered
<> 128:9bcdf88f62b0 173 * @param[in] handler SPI interrupt handler
<> 128:9bcdf88f62b0 174 * @param[in] hint A suggestion for how to use DMA with this transfer
<> 128:9bcdf88f62b0 175 */
<> 128:9bcdf88f62b0 176 void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint8_t bit_width, uint32_t handler, uint32_t event, DMAUsage hint);
<> 128:9bcdf88f62b0 177
<> 128:9bcdf88f62b0 178 /** The asynchronous IRQ handler
<> 128:9bcdf88f62b0 179 *
<> 128:9bcdf88f62b0 180 * Reads the received values out of the RX FIFO, writes values into the TX FIFO and checks for transfer termination
<> 128:9bcdf88f62b0 181 * conditions, such as buffer overflows or transfer complete.
<> 128:9bcdf88f62b0 182 * @param[in] obj The SPI object that holds the transfer information
<> 128:9bcdf88f62b0 183 * @return Event flags if a transfer termination condition was met; otherwise 0.
<> 128:9bcdf88f62b0 184 */
<> 128:9bcdf88f62b0 185 uint32_t spi_irq_handler_asynch(spi_t *obj);
<> 128:9bcdf88f62b0 186
<> 128:9bcdf88f62b0 187 /** Attempts to determine if the SPI peripheral is already in use
<> 128:9bcdf88f62b0 188 *
<> 128:9bcdf88f62b0 189 * If a temporary DMA channel has been allocated, peripheral is in use.
<> 128:9bcdf88f62b0 190 * If a permanent DMA channel has been allocated, check if the DMA channel is in use. If not, proceed as though no DMA
<> 128:9bcdf88f62b0 191 * channel were allocated.
<> 128:9bcdf88f62b0 192 * If no DMA channel is allocated, check whether tx and rx buffers have been assigned. For each assigned buffer, check
<> 128:9bcdf88f62b0 193 * if the corresponding buffer position is less than the buffer length. If buffers do not indicate activity, check if
<> 128:9bcdf88f62b0 194 * there are any bytes in the FIFOs.
<> 128:9bcdf88f62b0 195 * @param[in] obj The SPI object to check for activity
<> 128:9bcdf88f62b0 196 * @return Non-zero if the SPI port is active or zero if it is not.
<> 128:9bcdf88f62b0 197 */
<> 128:9bcdf88f62b0 198 uint8_t spi_active(spi_t *obj);
<> 128:9bcdf88f62b0 199
<> 128:9bcdf88f62b0 200 /** Abort an SPI transfer
<> 128:9bcdf88f62b0 201 *
<> 128:9bcdf88f62b0 202 * @param obj The SPI peripheral to stop
<> 128:9bcdf88f62b0 203 */
<> 128:9bcdf88f62b0 204 void spi_abort_asynch(spi_t *obj);
<> 128:9bcdf88f62b0 205
<> 128:9bcdf88f62b0 206
<> 128:9bcdf88f62b0 207 #endif
<> 128:9bcdf88f62b0 208
<> 128:9bcdf88f62b0 209 /**@}*/
<> 128:9bcdf88f62b0 210
<> 128:9bcdf88f62b0 211 #ifdef __cplusplus
<> 128:9bcdf88f62b0 212 }
<> 128:9bcdf88f62b0 213 #endif // __cplusplus
<> 128:9bcdf88f62b0 214
<> 128:9bcdf88f62b0 215 #endif // SPI_DEVICE
<> 128:9bcdf88f62b0 216
<> 128:9bcdf88f62b0 217 #endif // MBED_SPI_API_H
<> 128:9bcdf88f62b0 218
<> 128:9bcdf88f62b0 219 /** @}*/