MMEx with SPI Slave to allow legacy devices to communicate with modern media such as USB, SD cards, the internet and all of the mbed\'s other interfaces

Dependencies:   NetServices MSCUsbHost mbed TMP102 SDFileSystem

Committer:
DeMein
Date:
Sun Feb 27 18:54:40 2011 +0000
Revision:
0:67a55a82ce06
Version as submitted to the NXP Design Challenge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DeMein 0:67a55a82ce06 1 /* MMEx for MBED - SPI interfacing and ringbuffer functions
DeMein 0:67a55a82ce06 2 * Copyright (c) 2011 MK
DeMein 0:67a55a82ce06 3 *
DeMein 0:67a55a82ce06 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
DeMein 0:67a55a82ce06 5 * of this software and associated documentation files (the "Software"), to deal
DeMein 0:67a55a82ce06 6 * in the Software without restriction, including without limitation the rights
DeMein 0:67a55a82ce06 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
DeMein 0:67a55a82ce06 8 * copies of the Software, and to permit persons to whom the Software is
DeMein 0:67a55a82ce06 9 * furnished to do so, subject to the following conditions:
DeMein 0:67a55a82ce06 10 *
DeMein 0:67a55a82ce06 11 * The above copyright notice and this permission notice shall be included in
DeMein 0:67a55a82ce06 12 * all copies or substantial portions of the Software.
DeMein 0:67a55a82ce06 13 *
DeMein 0:67a55a82ce06 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
DeMein 0:67a55a82ce06 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
DeMein 0:67a55a82ce06 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
DeMein 0:67a55a82ce06 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
DeMein 0:67a55a82ce06 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DeMein 0:67a55a82ce06 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
DeMein 0:67a55a82ce06 20 * THE SOFTWARE.
DeMein 0:67a55a82ce06 21 */
DeMein 0:67a55a82ce06 22
DeMein 0:67a55a82ce06 23 /**
DeMein 0:67a55a82ce06 24 \file spissp.h
DeMein 0:67a55a82ce06 25 \brief SPI interfacing and ringbuffer functions
DeMein 0:67a55a82ce06 26 */
DeMein 0:67a55a82ce06 27
DeMein 0:67a55a82ce06 28 #ifndef SPISSP_H
DeMein 0:67a55a82ce06 29 #define SPISSP_H
DeMein 0:67a55a82ce06 30
DeMein 0:67a55a82ce06 31 #include "mbed.h"
DeMein 0:67a55a82ce06 32 #include "lpc17xx_ssp.h"
DeMein 0:67a55a82ce06 33 #include "globaldefs.h"
DeMein 0:67a55a82ce06 34
DeMein 0:67a55a82ce06 35 // macros for managing the SSP interface SSP0 only!
DeMein 0:67a55a82ce06 36 #define tx_fifo_notfull (LPC_SSP0->SR & SSP_STAT_TXFIFO_NOTFULL)
DeMein 0:67a55a82ce06 37 #define tx_fifo_empty (LPC_SSP0->SR & SSP_STAT_TXFIFO_EMPTY)
DeMein 0:67a55a82ce06 38 #define rx_fifo_notempty (LPC_SSP0->SR & SSP_STAT_RXFIFO_NOTEMPTY)
DeMein 0:67a55a82ce06 39 #define rx_fifo_full (LPC_SSP0->SR & SSP_STAT_RXFIFO_FULL)
DeMein 0:67a55a82ce06 40
DeMein 0:67a55a82ce06 41 #define irq_RTMIS (LPC_SSP0->MIS & SSP_MIS_RT) // RX FIFO timeout
DeMein 0:67a55a82ce06 42 #define irq_RXMIS (LPC_SSP0->MIS & SSP_MIS_RX) // RX FIFO half full
DeMein 0:67a55a82ce06 43 #define irq_RISOVR (LPC_SSP0->RIS & SSP_RIS_ROR) // receive overrun
DeMein 0:67a55a82ce06 44
DeMein 0:67a55a82ce06 45 #define DBG_OFF 0
DeMein 0:67a55a82ce06 46 #define DBG_ON 1
DeMein 0:67a55a82ce06 47 #define DBG_FULL 2
DeMein 0:67a55a82ce06 48
DeMein 0:67a55a82ce06 49
DeMein 0:67a55a82ce06 50 /** Library for the interrupt driven SPI SLave
DeMein 0:67a55a82ce06 51 *
DeMein 0:67a55a82ce06 52 */
DeMein 0:67a55a82ce06 53 class spislirq {
DeMein 0:67a55a82ce06 54
DeMein 0:67a55a82ce06 55 public:
DeMein 0:67a55a82ce06 56
DeMein 0:67a55a82ce06 57 /** initialize the SPI interface
DeMein 0:67a55a82ce06 58 *
DeMein 0:67a55a82ce06 59 */
DeMein 0:67a55a82ce06 60 spislirq(int PortNum);
DeMein 0:67a55a82ce06 61
DeMein 0:67a55a82ce06 62 /** initialization of SPI Slave and buffers
DeMein 0:67a55a82ce06 63 *
DeMein 0:67a55a82ce06 64 */
DeMein 0:67a55a82ce06 65 void init();
DeMein 0:67a55a82ce06 66
DeMein 0:67a55a82ce06 67 /** attach callback for network (or other?) polling
DeMein 0:67a55a82ce06 68 *
DeMein 0:67a55a82ce06 69 */
DeMein 0:67a55a82ce06 70 void attach(void (*function)(void)) {
DeMein 0:67a55a82ce06 71 _callback.attach(function);
DeMein 0:67a55a82ce06 72 }
DeMein 0:67a55a82ce06 73 /** attach callback for network (or other?) polling
DeMein 0:67a55a82ce06 74 *
DeMein 0:67a55a82ce06 75 */
DeMein 0:67a55a82ce06 76
DeMein 0:67a55a82ce06 77 /** disable SSP0 interrupts
DeMein 0:67a55a82ce06 78 * will discard the byte if buffer full
DeMein 0:67a55a82ce06 79 */
DeMein 0:67a55a82ce06 80 void disable_irq(void);
DeMein 0:67a55a82ce06 81
DeMein 0:67a55a82ce06 82 /** enable SSP0 interrupts
DeMein 0:67a55a82ce06 83 * will discard the byte if buffer full
DeMein 0:67a55a82ce06 84 */
DeMein 0:67a55a82ce06 85 void enable_irq(void);
DeMein 0:67a55a82ce06 86
DeMein 0:67a55a82ce06 87 /** add one byte to the transmit buffer, no blocking, no protocol
DeMein 0:67a55a82ce06 88 * will discard the byte if buffer full
DeMein 0:67a55a82ce06 89 *
DeMein 0:67a55a82ce06 90 * @param c byte to send
DeMein 0:67a55a82ce06 91 */
DeMein 0:67a55a82ce06 92 void txx_add(unsigned char c);
DeMein 0:67a55a82ce06 93
DeMein 0:67a55a82ce06 94 /** returns the number of bytes available in the transmit buffer
DeMein 0:67a55a82ce06 95 *
DeMein 0:67a55a82ce06 96 * @return available space in transmit buffer
DeMein 0:67a55a82ce06 97 */
DeMein 0:67a55a82ce06 98 int tx_room();
DeMein 0:67a55a82ce06 99
DeMein 0:67a55a82ce06 100 /** returns the amount af actual bytes used in the transmit buffer
DeMein 0:67a55a82ce06 101 *
DeMein 0:67a55a82ce06 102 * @return bytes used in transmit buffer
DeMein 0:67a55a82ce06 103 */
DeMein 0:67a55a82ce06 104 int tx_use();
DeMein 0:67a55a82ce06 105
DeMein 0:67a55a82ce06 106 /** returns the number of bytes available in the receive buffer
DeMein 0:67a55a82ce06 107 *
DeMein 0:67a55a82ce06 108 * @return available space in transmit buffer
DeMein 0:67a55a82ce06 109 */
DeMein 0:67a55a82ce06 110 int rx_room();
DeMein 0:67a55a82ce06 111
DeMein 0:67a55a82ce06 112 /** returns the percentage used in the transmit buffer
DeMein 0:67a55a82ce06 113 *
DeMein 0:67a55a82ce06 114 * @return percentage from 0 to 100
DeMein 0:67a55a82ce06 115 */
DeMein 0:67a55a82ce06 116 int tx_perc();
DeMein 0:67a55a82ce06 117
DeMein 0:67a55a82ce06 118 /** returns the percentage used in the receive buffer
DeMein 0:67a55a82ce06 119 *
DeMein 0:67a55a82ce06 120 * @return percentage from 0 to 100
DeMein 0:67a55a82ce06 121 */
DeMein 0:67a55a82ce06 122 int rx_perc();
DeMein 0:67a55a82ce06 123
DeMein 0:67a55a82ce06 124 /** returns the amount af actual bytes used in the receive buffer
DeMein 0:67a55a82ce06 125 *
DeMein 0:67a55a82ce06 126 * @return bytes used in receive buffer
DeMein 0:67a55a82ce06 127 */
DeMein 0:67a55a82ce06 128 int rx_use();
DeMein 0:67a55a82ce06 129
DeMein 0:67a55a82ce06 130 /** empties the transmit buffer
DeMein 0:67a55a82ce06 131 */
DeMein 0:67a55a82ce06 132 void flush_tx();
DeMein 0:67a55a82ce06 133
DeMein 0:67a55a82ce06 134 /** empties the receive buffer
DeMein 0:67a55a82ce06 135 */
DeMein 0:67a55a82ce06 136 void flush_rx();
DeMein 0:67a55a82ce06 137
DeMein 0:67a55a82ce06 138 /** add one byte to the transmit buffer, no protocol handling is done
DeMein 0:67a55a82ce06 139 *
DeMein 0:67a55a82ce06 140 * @param Bt byte to send
DeMein 0:67a55a82ce06 141 * @return 0 when succesfull, will block until there is room in the buffer
DeMein 0:67a55a82ce06 142 *
DeMein 0:67a55a82ce06 143 */
DeMein 0:67a55a82ce06 144 int tx_add(unsigned char Bt);
DeMein 0:67a55a82ce06 145
DeMein 0:67a55a82ce06 146 /** add one byte to the transmit buffer with protocol handling,
DeMein 0:67a55a82ce06 147 * function will block until there is room in the transmit buffer
DeMein 0:67a55a82ce06 148 *
DeMein 0:67a55a82ce06 149 * @param Bt byte to send
DeMein 0:67a55a82ce06 150 */
DeMein 0:67a55a82ce06 151 void tx_addp(unsigned char Bt);
DeMein 0:67a55a82ce06 152
DeMein 0:67a55a82ce06 153 /** adds a complete string to the transmit buffer, with protocol processing.
DeMein 0:67a55a82ce06 154 * function will block until there is room in the transmit buffer
DeMein 0:67a55a82ce06 155 *
DeMein 0:67a55a82ce06 156 * @param S string to be transmitted
DeMein 0:67a55a82ce06 157 * @return always returns 0
DeMein 0:67a55a82ce06 158 */
DeMein 0:67a55a82ce06 159 int tx_string(char S[]);
DeMein 0:67a55a82ce06 160
DeMein 0:67a55a82ce06 161 /** read one byte from the receive buffer without protocol processing
DeMein 0:67a55a82ce06 162 *
DeMein 0:67a55a82ce06 163 * @return the character read, blocks if RX buffer is empty!
DeMein 0:67a55a82ce06 164 *
DeMein 0:67a55a82ce06 165 */
DeMein 0:67a55a82ce06 166 int rx_read();
DeMein 0:67a55a82ce06 167
DeMein 0:67a55a82ce06 168 /** inserts one byte in the receive buffer without protocol processing
DeMein 0:67a55a82ce06 169 * used for command batch processing
DeMein 0:67a55a82ce06 170 *
DeMein 0:67a55a82ce06 171 * @return the character read, blocks if RX buffer is empty!
DeMein 0:67a55a82ce06 172 *
DeMein 0:67a55a82ce06 173 */
DeMein 0:67a55a82ce06 174 int rx_add(unsigned char c);
DeMein 0:67a55a82ce06 175
DeMein 0:67a55a82ce06 176 /** peeks in receive buffer, reads next pending input char without advancing buffer pointer
DeMein 0:67a55a82ce06 177 *
DeMein 0:67a55a82ce06 178 * @return the character read, blocks if RX buffer is empty!
DeMein 0:67a55a82ce06 179 *
DeMein 0:67a55a82ce06 180 */
DeMein 0:67a55a82ce06 181 int rx_peek();
DeMein 0:67a55a82ce06 182
DeMein 0:67a55a82ce06 183 /** read one byte, will wait until a valid char is read,
DeMein 0:67a55a82ce06 184 * this function is blocking and will process escape characters,
DeMein 0:67a55a82ce06 185 * when an escape is seen, will try to read the next char
DeMein 0:67a55a82ce06 186 *
DeMein 0:67a55a82ce06 187 * @param mode command line processing or data read (ignored)
DeMein 0:67a55a82ce06 188 * @return character read, or -1 when >F seen, -2 when >I seen
DeMein 0:67a55a82ce06 189 *
DeMein 0:67a55a82ce06 190 */
DeMein 0:67a55a82ce06 191 int rxx_read(bool mode);
DeMein 0:67a55a82ce06 192 void DBG_set(int l);
DeMein 0:67a55a82ce06 193
DeMein 0:67a55a82ce06 194 /** check if the TX buffer is empty
DeMein 0:67a55a82ce06 195 * @return true if the TX buffer is empty
DeMein 0:67a55a82ce06 196 */
DeMein 0:67a55a82ce06 197 bool tx_empty();
DeMein 0:67a55a82ce06 198
DeMein 0:67a55a82ce06 199 /** check if the TX buffer is full
DeMein 0:67a55a82ce06 200 * @return true if the TX buffer is full
DeMein 0:67a55a82ce06 201 */
DeMein 0:67a55a82ce06 202 bool tx_full();
DeMein 0:67a55a82ce06 203
DeMein 0:67a55a82ce06 204 /** check if the RX buffer is empty
DeMein 0:67a55a82ce06 205 * @return true if the RX buffer is empty
DeMein 0:67a55a82ce06 206 */
DeMein 0:67a55a82ce06 207 bool rx_empty();
DeMein 0:67a55a82ce06 208
DeMein 0:67a55a82ce06 209 /** check if the RX buffer is full
DeMein 0:67a55a82ce06 210 * @return true if the RX buffer is full
DeMein 0:67a55a82ce06 211 */
DeMein 0:67a55a82ce06 212 bool rx_full();
DeMein 0:67a55a82ce06 213
DeMein 0:67a55a82ce06 214
DeMein 0:67a55a82ce06 215 private:
DeMein 0:67a55a82ce06 216
DeMein 0:67a55a82ce06 217 SPISlave smldl; // uses SPI Class from MBED library
DeMein 0:67a55a82ce06 218 Serial pc;
DeMein 0:67a55a82ce06 219 DigitalOut _led4;
DeMein 0:67a55a82ce06 220 FunctionPointer _callback;
DeMein 0:67a55a82ce06 221
DeMein 0:67a55a82ce06 222 int hex2int(char C);
DeMein 0:67a55a82ce06 223 void spi_isr(); // interrupt service routine
DeMein 0:67a55a82ce06 224 static void _spi_isr(void);
DeMein 0:67a55a82ce06 225 static spislirq *instance;
DeMein 0:67a55a82ce06 226 void DBG_outchar(char C);
DeMein 0:67a55a82ce06 227 void DBG_inchar(char C);
DeMein 0:67a55a82ce06 228 void DBG_chr(char* src, char c);
DeMein 0:67a55a82ce06 229 void do_callback(void) { _callback.call(); };
DeMein 0:67a55a82ce06 230
DeMein 0:67a55a82ce06 231
DeMein 0:67a55a82ce06 232 // buffers and pointers in buffers
DeMein 0:67a55a82ce06 233 #define bufsize 255
DeMein 0:67a55a82ce06 234 unsigned char rxxbuf[bufsize]; // receive buffer
DeMein 0:67a55a82ce06 235 unsigned char txxbuf[bufsize]; // transmit buffer
DeMein 0:67a55a82ce06 236 int tx_in;
DeMein 0:67a55a82ce06 237 int tx_out;
DeMein 0:67a55a82ce06 238 int rx_in;
DeMein 0:67a55a82ce06 239 int rx_out;
DeMein 0:67a55a82ce06 240
DeMein 0:67a55a82ce06 241 int DBG_level;
DeMein 0:67a55a82ce06 242
DeMein 0:67a55a82ce06 243 };
DeMein 0:67a55a82ce06 244
DeMein 0:67a55a82ce06 245 #endif