Lancaster University's (short term!) clone of mbed-src for micro:bit. This is a copy of the github branch https://github.com/lancaster-university/mbed-classic

Fork of mbed-src by mbed official

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:52:54 2016 +0100
Revision:
641:be9b2017785a
Parent:
563:536c9fb088a0
Synchronized with git rev 1fb8ab4c
Author: James Devine
mbed-classic: BUGFIX for timer when using wait_ms from interrupt context

Previously if a user used wait[_ms,_us] in interrupt context the device would
hang indefinitely. This was due to incrementing overflowCount from
interrupt context only.

This meant that if a user used wait[_ms,_us] in an ISR with
the same or greater interrupt priority, it would result in an infinite
loop as the overflowCount variable would never be incremented, and
wait[_ms,_us] would never return.

This patch simply applies a better solution for the race condition
mentioned in the previous commit. It instead disables the timer1
interrupt and increments the overflowCount variable, preventing
the race condition whilst supporting wait[_ms,_us] in interrupt
context.

Who changed what in which revision?

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