PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
Pokitto
Date:
Sat Oct 07 21:31:12 2017 +0000
Revision:
5:7e5c566b1760
mbed-pokitto integrated

Who changed what in which revision?

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