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