Library to access LPC17xx peripherals. It uses static inline functions, constant propagation and dead code elimination to be as fast as possible.

Dependents:   Chua-VGA Wolfram-1D-VGA WolframRnd-1D-VGA Basin-VGA ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2s.h Source File

i2s.h

00001 /* Copyright (C) 2010, 2011 by Ivo van Poorten <ivop@euronet.nl>
00002  * This file is licensed under the terms of the GNU Lesser
00003  * General Public License, version 3.
00004  */
00005 
00006 #ifndef FASTLIB_I2S_H
00007 #define FASTLIB_I2S_H
00008 
00009 #include "fastlib/common.h"
00010 
00011 /* important: in mono mode, each word is transmitted twice(!) */
00012 
00013 #define FL_I2SDAO       ((volatile uint32_t *) 0x400A8000)
00014 #define FL_I2SDAI       ((volatile uint32_t *) 0x400A8004)
00015 #define FL_I2STXFIFO    ((volatile uint32_t *) 0x400A8008)
00016 #define FL_I2SRXFIFO    ((volatile uint32_t *) 0x400A800C)
00017 #define FL_I2SSTATE     ((volatile uint32_t *) 0x400A8010)
00018 #define FL_I2SDMA1      ((volatile uint32_t *) 0x400A8014)
00019 #define FL_I2SDMA2      ((volatile uint32_t *) 0x400A8018)
00020 #define FL_I2SIRQ       ((volatile uint32_t *) 0x400A801C)
00021 #define FL_I2STXRATE    ((volatile uint32_t *) 0x400A8020)
00022 #define FL_I2SRXRATE    ((volatile uint32_t *) 0x400A8024)
00023 #define FL_I2STXBITRATE ((volatile uint32_t *) 0x400A8028)
00024 #define FL_I2SRXBITRATE ((volatile uint32_t *) 0x400A802C)
00025 #define FL_I2STXMODE    ((volatile uint32_t *) 0x400A8030)
00026 #define FL_I2SRXMODE    ((volatile uint32_t *) 0x400A8034)
00027 
00028 static inline void fl_i2s_output_set_config(const unsigned width, const unsigned mono, const unsigned halfperiod,
00029                                             const unsigned stop, const unsigned reset, const unsigned mute, const unsigned slave) {
00030     *FL_I2SDAO = width | (mono<<2) | ((halfperiod-1)<<6) | (stop<<3) | (reset<<4) | (mute<<15) | (slave<<5);
00031 }
00032 
00033 static inline unsigned fl_i2s_output_get_config(void) {
00034     return *FL_I2SDAO;
00035 }
00036 
00037 static inline void fl_i2s_input_set_config(const unsigned width, const unsigned mono, const unsigned halfperiod,
00038                                             const unsigned stop, const unsigned reset, const unsigned mute, const unsigned slave) {
00039     *FL_I2SDAI = width | (mono<<2) | ((halfperiod-1)<<6) | (stop<<3) | (reset<<4) | (mute<<15) | (slave<<5);
00040 }
00041 
00042 static inline unsigned fl_i2s_input_get_config(void) {
00043     return *FL_I2SDAI;
00044 }
00045 
00046 static inline void fl_i2s_write_tx_fifo(const unsigned value) {
00047     *FL_I2STXFIFO = value;
00048 }
00049 
00050 static inline unsigned fl_i2s_read_rx_fifo(void) {
00051     return *FL_I2SRXFIFO;
00052 }
00053 
00054 static inline unsigned fl_i2s_status_irq(void) {
00055     return *FL_I2SSTATE & (1U<<0);
00056 }
00057 
00058 static inline unsigned fl_i2s_status_dmareq1(void) {
00059     return *FL_I2SSTATE & (1U<<1);
00060 }
00061 
00062 static inline unsigned fl_i2s_status_dmareq2(void) {
00063     return *FL_I2SSTATE & (1U<<2);
00064 }
00065 
00066 static inline unsigned fl_i2s_get_rx_level(void) {
00067     return (*FL_I2SSTATE >> 8) & 15;
00068 }
00069 
00070 static inline unsigned fl_i2s_get_tx_level(void) {
00071     return (*FL_I2SSTATE >> 16) & 15;
00072 }
00073 
00074 /* rx/tx_state: 0-1     rx/tx-level: 0-15 */
00075 static inline void fl_i2s_config_dma1(const unsigned rx_state, const unsigned tx_state, const unsigned rx_level, const unsigned tx_level) {
00076     *FL_I2SDMA1 = rx_state | (tx_state<<1) | (rx_level<<8) | (tx_level<<16);
00077 }
00078 static inline void fl_i2s_config_dma2(const unsigned rx_state, const unsigned tx_state, const unsigned rx_level, const unsigned tx_level) {
00079     *FL_I2SDMA2 = rx_state | (tx_state<<1) | (rx_level<<8) | (tx_level<<16);
00080 }
00081 
00082 /* rx/tx_irq: 0-1 */
00083 static inline void fl_i2s_config_irq(const unsigned rx_irq, const unsigned tx_irq, const unsigned rx_level, const unsigned tx_level) {
00084     *FL_I2SIRQ = rx_irq | (tx_irq<<1) | (rx_level<<8) | (tx_level<<16);
00085 }
00086 
00087 /* tx rate = PCLK_I2S * (X/Y) / 2       X and Y: 1-255 (0=no clock) */
00088 static inline void fl_i2s_set_tx_rate(const unsigned X, const unsigned Y) {
00089     *FL_I2STXRATE = Y | (X<<8);
00090 }
00091 static inline void fl_i2s_set_rx_rate(const unsigned X, const unsigned Y) {
00092     *FL_I2SRXRATE = Y | (X<<8);
00093 }
00094 
00095 /* bitrate: 1-32 */
00096 static inline void fl_i2s_set_tx_bitrate(const unsigned bitrate) {
00097     *FL_I2STXBITRATE = bitrate-1;
00098 }
00099 static inline void fl_i2s_set_rx_bitrate(const unsigned bitrate) {
00100     *FL_I2SRXBITRATE = bitrate-1;
00101 }
00102 
00103 #define FL_TX_FRACTIONAL_RATE_DIVIDER   0
00104 #define FL_TX_USE_RX_CLOCK              1
00105 
00106 static inline void fl_i2s_tx_mode_clock(const unsigned tx_clock) {
00107     if (tx_clock) *FL_I2STXMODE |=   1U<<1;
00108     else          *FL_I2STXMODE &= ~(1U<<1);
00109 }
00110 
00111 #define FL_RX_FRACTIONAL_RATE_DIVIDER   0
00112 #define FL_RX_USE_TX_CLOCK              1
00113 
00114 static inline void fl_i2s_rx_mode_clock(const unsigned rx_clock) {
00115     if (rx_clock) *FL_I2SRXMODE |=   1U<<1;
00116     else          *FL_I2SRXMODE &= ~(1U<<1);
00117 }
00118 
00119 static inline void fl_i2s_tx_mode_4pin(const unsigned state) {
00120     if (state) *FL_I2STXMODE |=   1U<<2;
00121     else       *FL_I2STXMODE &= ~(1U<<2);
00122 }
00123 
00124 static inline void fl_i2s_rx_mode_4pin(const unsigned state) {
00125     if (state) *FL_I2SRXMODE |=   1U<<2;
00126     else       *FL_I2SRXMODE &= ~(1U<<2);
00127 }
00128 
00129 static inline void fl_i2s_tx_clock_output(const unsigned state) {
00130     if (state) *FL_I2STXMODE |=   1U<<3;
00131     else       *FL_I2STXMODE &= ~(1U<<3);
00132 }
00133 
00134 static inline void fl_i2s_rx_clock_output(const unsigned state) {
00135     if (state) *FL_I2SRXMODE |=   1U<<3;
00136     else       *FL_I2SRXMODE &= ~(1U<<3);
00137 }
00138 
00139 #endif