Last commit 05 Aug 2011
I2SSlave.h@0:e62e06e11575, 04 Aug 2011 (annotated)
- Committer:
- Date:
- Thu Aug 04 14:53:50 2011 +0000
- Revision:
- 0:e62e06e11575
- Child:
- 1:1c6ab181537f
version 2.31
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 0:e62e06e11575 | 1 | /** | |
| 0:e62e06e11575 | 2 | * @author Daniel Worrall | |
| 0:e62e06e11575 | 3 | * | |
| 0:e62e06e11575 | 4 | * @section LICENSE | |
| 0:e62e06e11575 | 5 | * | |
| 0:e62e06e11575 | 6 | * Copyright (c) 2011 mbed | |
| 0:e62e06e11575 | 7 | * | |
| 0:e62e06e11575 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 0:e62e06e11575 | 9 | * of this software and associated documentation files (the "Software"), to deal | |
| 0:e62e06e11575 | 10 | * in the Software without restriction, including without limitation the rights | |
| 0:e62e06e11575 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 0:e62e06e11575 | 12 | * copies of the Software, and to permit persons to whom the Software is | |
| 0:e62e06e11575 | 13 | * furnished to do so, subject to the following conditions: | |
| 0:e62e06e11575 | 14 | * | |
| 0:e62e06e11575 | 15 | * The above copyright notice and this permission notice shall be included in | |
| 0:e62e06e11575 | 16 | * all copies or substantial portions of the Software. | |
| 0:e62e06e11575 | 17 | * | |
| 0:e62e06e11575 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 0:e62e06e11575 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 0:e62e06e11575 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 0:e62e06e11575 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 0:e62e06e11575 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 0:e62e06e11575 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 0:e62e06e11575 | 24 | * THE SOFTWARE. | |
| 0:e62e06e11575 | 25 | * | |
| 0:e62e06e11575 | 26 | * @section DESCRIPTION | |
| 0:e62e06e11575 | 27 | * I2S port abstraction library cpp file for NXP LPC1768 | |
| 0:e62e06e11575 | 28 | * | |
| 0:e62e06e11575 | 29 | */ | |
| 0:e62e06e11575 | 30 | ||
| 0:e62e06e11575 | 31 | #include "mbed.h" | |
| 0:e62e06e11575 | 32 | #include "math.h" | |
| 0:e62e06e11575 | 33 | ||
| 0:e62e06e11575 | 34 | #ifndef __MBED_I2SSLAVE_H__ | |
| 0:e62e06e11575 | 35 | #define __MBED_I2SSLAVE_H__ | |
| 0:e62e06e11575 | 36 | ||
| 0:e62e06e11575 | 37 | /** I2S class defined on the LPC1768 port | |
| 0:e62e06e11575 | 38 | * | |
| 0:e62e06e11575 | 39 | */ | |
| 0:e62e06e11575 | 40 | extern FunctionPointer akjnh3489v8ncv; | |
| 0:e62e06e11575 | 41 | class I2SSlave{ | |
| 0:e62e06e11575 | 42 | ||
| 0:e62e06e11575 | 43 | public: | |
| 0:e62e06e11575 | 44 | //Constructor | |
| 0:e62e06e11575 | 45 | /** Create an I2S object | |
| 0:e62e06e11575 | 46 | * | |
| 0:e62e06e11575 | 47 | * @param tx_sda Transmitter serial data line | |
| 0:e62e06e11575 | 48 | * @param tx_ws Transmitter word select line | |
| 0:e62e06e11575 | 49 | * @param clk Shared transmitter/receiver clock line | |
| 0:e62e06e11575 | 50 | * @param rx_sda Receiver serial data line | |
| 0:e62e06e11575 | 51 | * @param rx_ws Receiver word select line | |
| 0:e62e06e11575 | 52 | */ | |
| 0:e62e06e11575 | 53 | I2SSlave(PinName tx_sda, PinName tx_ws, PinName clk, PinName rx_sda, PinName rx_ws); | |
| 0:e62e06e11575 | 54 | /** Set the data transmission format | |
| 0:e62e06e11575 | 55 | * | |
| 0:e62e06e11575 | 56 | * @param bit Set the number of bits per write | |
| 0:e62e06e11575 | 57 | * @param mode Set STEREO (0) or MONO (1) mode | |
| 0:e62e06e11575 | 58 | */ | |
| 0:e62e06e11575 | 59 | void format(int bit, bool mode); | |
| 0:e62e06e11575 | 60 | /** Write a buffer to the I2S port | |
| 0:e62e06e11575 | 61 | * | |
| 0:e62e06e11575 | 62 | * @param buffer Address of buffer to pass to I2S port | |
| 0:e62e06e11575 | 63 | * @param from Start position in buffer to read from | |
| 0:e62e06e11575 | 64 | * @param length Length of buffer (MUST not exceed 8 words, each 32 bits long) | |
| 0:e62e06e11575 | 65 | * | |
| 0:e62e06e11575 | 66 | * Note: sending 8 words to the TXFIFO will trigger an interrupt! | |
| 0:e62e06e11575 | 67 | */ | |
| 0:e62e06e11575 | 68 | void write(int* buffer, int from, int length); | |
| 0:e62e06e11575 | 69 | /** Activate I2S port for data streaming | |
| 0:e62e06e11575 | 70 | * | |
| 0:e62e06e11575 | 71 | * @param mode Mode to enable - NONE, TRANSMIT only, RECEIVE only, BOTH | |
| 0:e62e06e11575 | 72 | * Enables tx/rx interrupts | |
| 0:e62e06e11575 | 73 | */ | |
| 0:e62e06e11575 | 74 | void start(int mode); | |
| 0:e62e06e11575 | 75 | /** Deactivate I2S port from data streaming | |
| 0:e62e06e11575 | 76 | * | |
| 0:e62e06e11575 | 77 | * Disable all interrupts | |
| 0:e62e06e11575 | 78 | */ | |
| 0:e62e06e11575 | 79 | void stop(void); | |
| 0:e62e06e11575 | 80 | /** Load receive FIFO data into receiver buffer | |
| 0:e62e06e11575 | 81 | * | |
| 0:e62e06e11575 | 82 | */ | |
| 0:e62e06e11575 | 83 | void read(void); | |
| 0:e62e06e11575 | 84 | /** Attach a void/void function or void/void static memeber function to an interrupt generated by the I2SxxFIFOs | |
| 0:e62e06e11575 | 85 | * | |
| 0:e62e06e11575 | 86 | * @param function Function to attach | |
| 0:e62e06e11575 | 87 | * | |
| 0:e62e06e11575 | 88 | * e.g. myI2sObject.attach(&myfunction); | |
| 0:e62e06e11575 | 89 | * OR myI2sObject.attach(&myClass::myStaticMemberFunction); | |
| 0:e62e06e11575 | 90 | */ | |
| 0:e62e06e11575 | 91 | void attach(void(*fptr)(void)){ | |
| 0:e62e06e11575 | 92 | akjnh3489v8ncv.attach(fptr); | |
| 0:e62e06e11575 | 93 | } | |
| 0:e62e06e11575 | 94 | /** Attach a nonstatic void/void member function to an interrupt generated by the I2SxxFIFOs | |
| 0:e62e06e11575 | 95 | * | |
| 0:e62e06e11575 | 96 | * @param tptr Object pointer | |
| 0:e62e06e11575 | 97 | * @param mptr Member function pointer | |
| 0:e62e06e11575 | 98 | * | |
| 0:e62e06e11575 | 99 | * e.g. myI2sObject.attach(&myObject, &myClass::myNonstaticMemberFunction); where myObject is an object of myClass | |
| 0:e62e06e11575 | 100 | */ | |
| 0:e62e06e11575 | 101 | template<typename T> | |
| 0:e62e06e11575 | 102 | void attach(T *tptr, void(T::*mptr)(void)){ | |
| 0:e62e06e11575 | 103 | akjnh3489v8ncv.attach(tptr, mptr); | |
| 0:e62e06e11575 | 104 | } | |
| 0:e62e06e11575 | 105 | /** Return contents of I2S status register | |
| 0:e62e06e11575 | 106 | * | |
| 0:e62e06e11575 | 107 | * @returns Content of I2SSTATE register | |
| 0:e62e06e11575 | 108 | * | |
| 0:e62e06e11575 | 109 | * bit0: receive/transmit interrupt active | |
| 0:e62e06e11575 | 110 | * bit1: receive/transmit DMA request 1 | |
| 0:e62e06e11575 | 111 | * bit2: receive/transmit DMA request 2 | |
| 0:e62e06e11575 | 112 | * bit[11:8]: receive FIFO level | |
| 0:e62e06e11575 | 113 | * bit[19:16]: transmit FIFO level | |
| 0:e62e06e11575 | 114 | */ | |
| 0:e62e06e11575 | 115 | int status(void); | |
| 0:e62e06e11575 | 116 | ||
| 0:e62e06e11575 | 117 | //Receiver buffer | |
| 0:e62e06e11575 | 118 | int rxBuffer[4]; | |
| 0:e62e06e11575 | 119 | //defines | |
| 0:e62e06e11575 | 120 | #define STEREO 0 | |
| 0:e62e06e11575 | 121 | #define MONO 1 | |
| 0:e62e06e11575 | 122 | ||
| 0:e62e06e11575 | 123 | #define I2SFIFO_EMPTY 0 | |
| 0:e62e06e11575 | 124 | #define I2SFIFO_FULL 8 | |
| 0:e62e06e11575 | 125 | ||
| 0:e62e06e11575 | 126 | #define RAM_LENGTH 1024 | |
| 0:e62e06e11575 | 127 | #define RAM_LIMIT (RAM_LENGTH - 1) | |
| 0:e62e06e11575 | 128 | #define PTR_MAX ((RAM_LENGTH / 8) - 1) | |
| 0:e62e06e11575 | 129 | ||
| 0:e62e06e11575 | 130 | #define NONE 0 | |
| 0:e62e06e11575 | 131 | #define TRANSMIT 1 | |
| 0:e62e06e11575 | 132 | #define RECEIVE 2 | |
| 0:e62e06e11575 | 133 | #define BOTH 3 | |
| 0:e62e06e11575 | 134 | ||
| 0:e62e06e11575 | 135 | private: | |
| 0:e62e06e11575 | 136 | /** I2S intitalize function | |
| 0:e62e06e11575 | 137 | * | |
| 0:e62e06e11575 | 138 | * @param tx_sda Transmitter serial data line | |
| 0:e62e06e11575 | 139 | * @param tx_ws Transmitter word select line | |
| 0:e62e06e11575 | 140 | * @param clk Shared transmitter/receiver clock line | |
| 0:e62e06e11575 | 141 | * @param rx_sda Receiver serial data line | |
| 0:e62e06e11575 | 142 | * @param rx_ws Receiver word select line | |
| 0:e62e06e11575 | 143 | * @return Returns 0 for successful initialisation, -1 for an error | |
| 0:e62e06e11575 | 144 | */ | |
| 0:e62e06e11575 | 145 | int initialize_(PinName tx_sda, PinName tx_ws, PinName clk, PinName rx_sda, PinName rx_ws); | |
| 0:e62e06e11575 | 146 | /** Set internal clock divide by rate | |
| 0:e62e06e11575 | 147 | * | |
| 0:e62e06e11575 | 148 | * @param divideBy Divide by 1, 2, 4 or 8 | |
| 0:e62e06e11575 | 149 | */ | |
| 0:e62e06e11575 | 150 | void setClocks_(int divideBy); | |
| 0:e62e06e11575 | 151 | /** Set up the pins on the processor itself | |
| 0:e62e06e11575 | 152 | * | |
| 0:e62e06e11575 | 153 | * @param tx_sda Transmitter serial data line | |
| 0:e62e06e11575 | 154 | * @param tx_ws Transmitter word select line | |
| 0:e62e06e11575 | 155 | * @param clk Shared transmitter/receiver clock line | |
| 0:e62e06e11575 | 156 | * @param rx_sda Receiver serial data line | |
| 0:e62e06e11575 | 157 | * @param rx_ws Receiver word select line | |
| 0:e62e06e11575 | 158 | */ | |
| 0:e62e06e11575 | 159 | void setPins_(PinName tx_sda, PinName tx_ws, PinName clk, PinName rx_sda, PinName rx_ws); | |
| 0:e62e06e11575 | 160 | /** Set the data transmission format | |
| 0:e62e06e11575 | 161 | * | |
| 0:e62e06e11575 | 162 | * @param bit Set the number of bits per write | |
| 0:e62e06e11575 | 163 | * @param mode Set STEREO (0) or MONO (1) mode | |
| 0:e62e06e11575 | 164 | */ | |
| 0:e62e06e11575 | 165 | void format_(int bit, bool mode); | |
| 0:e62e06e11575 | 166 | /** Set slave mode | |
| 0:e62e06e11575 | 167 | * | |
| 0:e62e06e11575 | 168 | */ | |
| 0:e62e06e11575 | 169 | void modeConfig_(void); | |
| 0:e62e06e11575 | 170 | /** Store PinName values | |
| 0:e62e06e11575 | 171 | * | |
| 0:e62e06e11575 | 172 | * @param tx_sda Transmitter serial data line | |
| 0:e62e06e11575 | 173 | * @param tx_ws Transmitter word select line | |
| 0:e62e06e11575 | 174 | * @param clk Shared transmitter/receiver clock line | |
| 0:e62e06e11575 | 175 | * @param rx_sda Receiver serial data line | |
| 0:e62e06e11575 | 176 | * @param rx_ws Receiver word select line | |
| 0:e62e06e11575 | 177 | */ | |
| 0:e62e06e11575 | 178 | void storePins_(PinName tx_sda, PinName tx_ws, PinName clk, PinName rx_sda, PinName rx_ws); | |
| 0:e62e06e11575 | 179 | //variables | |
| 0:e62e06e11575 | 180 | int bit_; | |
| 0:e62e06e11575 | 181 | bool mode_; | |
| 0:e62e06e11575 | 182 | PinName tx_sda_; | |
| 0:e62e06e11575 | 183 | PinName tx_ws_; | |
| 0:e62e06e11575 | 184 | PinName clk_; | |
| 0:e62e06e11575 | 185 | PinName rx_sda_; | |
| 0:e62e06e11575 | 186 | PinName rx_ws_; | |
| 0:e62e06e11575 | 187 | }; | |
| 0:e62e06e11575 | 188 | ||
| 0:e62e06e11575 | 189 | #endif /*__MBED_I2S_H__*/ |

