From a75feca479f85caae2bec3433dacc37c98beb6d7 Mon Sep 17 00:00:00 2001 From: Shinichiro Nakamura Date: Fri, 10 Aug 2012 03:35:19 +0900 Subject: [PATCH] Added the mute function for SSM2603. --- firm/bare_metal/main.c | 430 +++++++++++++++++++++++----------------------- firm/bare_metal/ssm2603.c | 29 +++- firm/bare_metal/ssm2603.h | 3 + 3 files changed, 240 insertions(+), 222 deletions(-) diff --git a/firm/bare_metal/main.c b/firm/bare_metal/main.c index e2868b1..73a4b6d 100644 --- a/firm/bare_metal/main.c +++ b/firm/bare_metal/main.c @@ -1,217 +1,213 @@ -/** - * @file main.c - * @brief BlueTank ACB-BF592 Application Sample Codes. - * @author Copyright(C) 2012 Shinichiro Nakamura - */ - -#include -#include -#include -#include "lcd.h" -#include "led.h" -#include "pff.h" -#include "ssm2603.h" - -#define SCLOCK_HZ (100000000) /**< システムクロック(100MHz) */ -#define DMA_SAMPLE_SIZE (256) /**< 1回のサンプルサイズ */ - -void sport_rx_isr() __attribute__((interrupt_handler)); -void audio_effect(const int32_t* src, int32_t* des, int32_t count); - -static int32_t sport_buffer_rx[2][DMA_SAMPLE_SIZE]; /**< SPORT受信バッファ(ダブルバッファ) */ -static int32_t sport_buffer_tx[2][DMA_SAMPLE_SIZE]; /**< SPORT受信バッファ(ダブルバッファ) */ -static volatile int32_t bufidx_dma_target; /**< DMAがどのバッファ使用をしているか */ -static volatile int32_t data_ready; /**< データ転送完了フラグ */ - -int iii; - -static void setup_pll(uint8_t mul_val, uint8_t div_val) -{ - *pSIC_IWR = IWR_ENABLE(0); /* PLLのみIWRを許す */ - *pPLL_DIV = div_val; - *pPLL_CTL = (mul_val) << 9; - asm("cli r0; csync; idle; sti r0;": : :"R0"); - *pSIC_IWR = IWR_ENABLE_ALL; -} - -__attribute__((interrupt_handler)) void sport_rx_isr() -{ - *pDMA1_IRQ_STATUS = DMA_DONE; - asm("ssync;"); - /* オーディオコーデックからデータ到着 */ - data_ready = 1; - /* DMAが使用するバッファを変更 */ - bufidx_dma_target = (bufidx_dma_target ^ 1) & 1; -} - -int main(void) -{ - int32_t bufidx_dma_done; - FATFS fatfs; - DIR dir; - FILINFO finfo; - - /* - * PLLを設定する。 - */ - setup_pll(16, 4); - - /* - * LEDを初期化する。 - */ - led_init(); - led_write(LedTargetR, 1); - led_write(LedTargetG, 0); - - /* - * LCDを初期化する。 - */ - lcd_init(); - lcd_clear(); - lcd_goto(0, 0); - lcd_puts("BlueTank"); - lcd_goto(0, 1); - lcd_puts("Init...."); - - /* - * オーディオコーデックを初期化する。 - */ - ssm2603_init(); - - /* - * SDカードをマウントする。 - */ - if (pf_mount(&fatfs) == FR_OK) { - lcd_goto(0, 0); - lcd_puts("MT: OK "); - } else { - lcd_goto(0, 0); - lcd_puts("MT: FAIL"); - } - - /* - * バッファをクリアする。 - */ - memset(sport_buffer_rx, 0 , sizeof(sport_buffer_rx)); - memset(sport_buffer_tx, 0 , sizeof(sport_buffer_tx)); - bufidx_dma_target = 0; - data_ready = 0; - - /* - * ポートの設定 - */ - *pPORTG_MUX &= ~(PG1 | PG2 | PG3 | PG5 | PG6 | PG7); - *pPORTG_FER |= (PG1 | PG2 | PG3 | PG5 | PG6 | PG7); - - /* - * 割り込みハンドラをIVG9に登録 - */ - *pEVT9 = sport_rx_isr; - asm("ssync;"); - asm volatile ("cli %0; bitset (%0, 9); sti %0; csync;": "+d"(iii)); // set IMASK bit - asm("ssync;"); - *pSIC_IMASK |= IRQ_DMA1; - asm("ssync;"); - - /* - * SPORT RXの設定 - * 外部クロック、外部SYNC、MSB First - * 24ビットデータ、 ステレオ - */ - *pSPORT0_RCR1 = RFSR | RCKFE; - *pSPORT0_RCR2 = SLEN(31) | RSFSE; - - /* - * SPORT TXの設定 - * 外部クロック、外部SYNC、MSB First - * 24ビットデータ、 ステレオ - */ - *pSPORT0_TCR1 = TFSR | TCKFE; - *pSPORT0_TCR2 = SLEN(31) | TSFSE; - - /* - * DMA1(SPORT0 RX)の設定 - */ - *pDMA1_PERIPHERAL_MAP = 0x1000; - *pDMA1_CONFIG = FLOW_AUTO | DI_EN | DI_SEL | DMA2D | WDSIZE_32 | WNR; - *pDMA1_START_ADDR = sport_buffer_rx; - *pDMA1_X_COUNT = DMA_SAMPLE_SIZE; - *pDMA1_X_MODIFY = sizeof(int32_t); - *pDMA1_Y_COUNT = 2; - *pDMA1_Y_MODIFY = sizeof(int32_t); - - /* - * DMA2(SPORT0 TX)の設定 - */ - *pDMA2_PERIPHERAL_MAP = 0x2000; - *pDMA2_CONFIG = FLOW_AUTO | DMA2D | WDSIZE_32; - *pDMA2_START_ADDR = sport_buffer_tx; - *pDMA2_X_COUNT = DMA_SAMPLE_SIZE; - *pDMA2_X_MODIFY = sizeof(int32_t); - *pDMA2_Y_COUNT = 2; - *pDMA2_Y_MODIFY = sizeof(int32_t); - asm("ssync;"); - - /* - * DMAを有効 - */ - *pDMA1_IRQ_STATUS = DMA_DONE; - *pDMA1_CONFIG |= DMAEN; - *pDMA2_CONFIG |= DMAEN; - asm("ssync;"); - - /* - * SPORT0を有効 - */ - *pSPORT0_TCR1 |= TSPEN; - *pSPORT0_RCR1 |= RSPEN; - asm("ssync;"); - - /* - * PG13(MUTE#)を出力ピンに設定する。 - */ - *pPORTGIO_DIR |= PG13; - - /* - * PG13(MUTE#)を'H'出力に設定する。 - */ - *pPORTGIO_SET = PG13; - - /* - * 初期化の完了をユーザに通知する。 - */ - led_write(LedTargetR, 0); - led_write(LedTargetG, 1); - lcd_goto(0, 1); - lcd_puts("InitDone"); - - while (1) { - asm("idle;"); - if (0 != data_ready) { - /* - * フラグをクリアする。 - */ - data_ready = 0; - - /* - * DMAが完了したバッファを使用してオーディオ処理を行なう。 - */ - bufidx_dma_done = bufidx_dma_target ^ 1; - audio_effect(sport_buffer_rx[bufidx_dma_done], sport_buffer_tx[bufidx_dma_done], DMA_SAMPLE_SIZE); - } - } - - return 0; -} - -/** - * @brief オーディオ処理を実行する。 - * - * @param src 処理元バッファ。 - * @param des 処理後バッファ。 - */ -void audio_effect(const int32_t *src, int32_t *des, int32_t count) -{ - memcpy(des, src, sizeof(int32_t) * count); -} - +/** + * @file main.c + * @brief BlueTank ACB-BF592 Application Sample Codes. + * @author Copyright(C) 2012 Shinichiro Nakamura + */ + +#include +#include +#include +#include "lcd.h" +#include "led.h" +#include "pff.h" +#include "ssm2603.h" + +#define SCLOCK_HZ (100000000) /**< システムクロック(100MHz) */ +#define DMA_SAMPLE_SIZE (256) /**< 1回のサンプルサイズ */ + +void sport_rx_isr() __attribute__((interrupt_handler)); +void audio_effect(const int32_t* src, int32_t* des, int32_t count); + +static int32_t sport_buffer_rx[2][DMA_SAMPLE_SIZE]; /**< SPORT受信バッファ(ダブルバッファ) */ +static int32_t sport_buffer_tx[2][DMA_SAMPLE_SIZE]; /**< SPORT受信バッファ(ダブルバッファ) */ +static volatile int32_t bufidx_dma_target; /**< DMAがどのバッファ使用をしているか */ +static volatile int32_t data_ready; /**< データ転送完了フラグ */ + +int iii; + +static void setup_pll(uint8_t mul_val, uint8_t div_val) +{ + *pSIC_IWR = IWR_ENABLE(0); /* PLLのみIWRを許す */ + *pPLL_DIV = div_val; + *pPLL_CTL = (mul_val) << 9; + asm("cli r0; csync; idle; sti r0;": : :"R0"); + *pSIC_IWR = IWR_ENABLE_ALL; +} + +__attribute__((interrupt_handler)) void sport_rx_isr() +{ + *pDMA1_IRQ_STATUS = DMA_DONE; + asm("ssync;"); + /* オーディオコーデックからデータ到着 */ + data_ready = 1; + /* DMAが使用するバッファを変更 */ + bufidx_dma_target = (bufidx_dma_target ^ 1) & 1; +} + +int main(void) +{ + int32_t bufidx_dma_done; + FATFS fatfs; + DIR dir; + FILINFO finfo; + + /* + * PLLを設定する。 + */ + setup_pll(16, 4); + + /* + * LEDを初期化する。 + */ + led_init(); + led_write(LedTargetR, 1); + led_write(LedTargetG, 0); + + /* + * LCDを初期化する。 + */ + lcd_init(); + lcd_clear(); + lcd_goto(0, 0); + lcd_puts("BlueTank"); + lcd_goto(0, 1); + lcd_puts("Init...."); + + /* + * オーディオコーデックを初期化する。 + * 初期化された時点ではミュートされている。 + */ + ssm2603_init(); + + /* + * SDカードをマウントする。 + */ + if (pf_mount(&fatfs) == FR_OK) { + lcd_goto(0, 0); + lcd_puts("MT: OK "); + } else { + lcd_goto(0, 0); + lcd_puts("MT: FAIL"); + } + + /* + * バッファをクリアする。 + */ + memset(sport_buffer_rx, 0 , sizeof(sport_buffer_rx)); + memset(sport_buffer_tx, 0 , sizeof(sport_buffer_tx)); + bufidx_dma_target = 0; + data_ready = 0; + + /* + * ポートの設定 + */ + *pPORTG_MUX &= ~(PG1 | PG2 | PG3 | PG5 | PG6 | PG7); + *pPORTG_FER |= (PG1 | PG2 | PG3 | PG5 | PG6 | PG7); + + /* + * 割り込みハンドラをIVG9に登録 + */ + *pEVT9 = sport_rx_isr; + asm("ssync;"); + asm volatile ("cli %0; bitset (%0, 9); sti %0; csync;": "+d"(iii)); // set IMASK bit + asm("ssync;"); + *pSIC_IMASK |= IRQ_DMA1; + asm("ssync;"); + + /* + * SPORT RXの設定 + * 外部クロック、外部SYNC、MSB First + * 24ビットデータ、 ステレオ + */ + *pSPORT0_RCR1 = RFSR | RCKFE; + *pSPORT0_RCR2 = SLEN(31) | RSFSE; + + /* + * SPORT TXの設定 + * 外部クロック、外部SYNC、MSB First + * 24ビットデータ、 ステレオ + */ + *pSPORT0_TCR1 = TFSR | TCKFE; + *pSPORT0_TCR2 = SLEN(31) | TSFSE; + + /* + * DMA1(SPORT0 RX)の設定 + */ + *pDMA1_PERIPHERAL_MAP = 0x1000; + *pDMA1_CONFIG = FLOW_AUTO | DI_EN | DI_SEL | DMA2D | WDSIZE_32 | WNR; + *pDMA1_START_ADDR = sport_buffer_rx; + *pDMA1_X_COUNT = DMA_SAMPLE_SIZE; + *pDMA1_X_MODIFY = sizeof(int32_t); + *pDMA1_Y_COUNT = 2; + *pDMA1_Y_MODIFY = sizeof(int32_t); + + /* + * DMA2(SPORT0 TX)の設定 + */ + *pDMA2_PERIPHERAL_MAP = 0x2000; + *pDMA2_CONFIG = FLOW_AUTO | DMA2D | WDSIZE_32; + *pDMA2_START_ADDR = sport_buffer_tx; + *pDMA2_X_COUNT = DMA_SAMPLE_SIZE; + *pDMA2_X_MODIFY = sizeof(int32_t); + *pDMA2_Y_COUNT = 2; + *pDMA2_Y_MODIFY = sizeof(int32_t); + asm("ssync;"); + + /* + * DMAを有効 + */ + *pDMA1_IRQ_STATUS = DMA_DONE; + *pDMA1_CONFIG |= DMAEN; + *pDMA2_CONFIG |= DMAEN; + asm("ssync;"); + + /* + * SPORT0を有効 + */ + *pSPORT0_TCR1 |= TSPEN; + *pSPORT0_RCR1 |= RSPEN; + asm("ssync;"); + + /* + * 初期化の完了をユーザに通知する。 + */ + led_write(LedTargetR, 0); + led_write(LedTargetG, 1); + lcd_goto(0, 1); + lcd_puts("InitDone"); + + /* + * ミュートを解除する。 + */ + ssm2603_mute(false); + + while (1) { + asm("idle;"); + if (0 != data_ready) { + /* + * フラグをクリアする。 + */ + data_ready = 0; + + /* + * DMAが完了したバッファを使用してオーディオ処理を行なう。 + */ + bufidx_dma_done = bufidx_dma_target ^ 1; + audio_effect(sport_buffer_rx[bufidx_dma_done], sport_buffer_tx[bufidx_dma_done], DMA_SAMPLE_SIZE); + } + } + + return 0; +} + +/** + * @brief オーディオ処理を実行する。 + * + * @param src 処理元バッファ。 + * @param des 処理後バッファ。 + */ +void audio_effect(const int32_t *src, int32_t *des, int32_t count) +{ + memcpy(des, src, sizeof(int32_t) * count); +} + diff --git a/firm/bare_metal/ssm2603.c b/firm/bare_metal/ssm2603.c index 51140e8..19258a9 100644 --- a/firm/bare_metal/ssm2603.c +++ b/firm/bare_metal/ssm2603.c @@ -3,10 +3,10 @@ #include "twi.h" #include "ssm2603.h" -/* SSM2603$B$N(BI2C$B%G%P%$%9%"%I%l%9(B */ +/* SSM2603のI2Cデバイスアドレス */ #define TWI_DEVICE_ADDR (0x1A) -/* SSM2603 TWI$B=i4|2=%G!<%?Ns(B */ +/* SSM2603 TWI初期化データ列 */ static const uint8_t ssm2603_initdata[] = { (0x0F << 1) | 0, 0x00, @@ -27,17 +27,27 @@ void ssm2603_init(void) uint32_t i, j; /* - * TWI$B=i4|2=(B + * TWI初期化 */ twi_init(); /* - * SSM2603$B=i4|2=(B + * PG13(MUTE#)を出力ピンに設定する。 + */ + *pPORTGIO_DIR |= PG13; + + /* + * ミュートする。 + */ + ssm2603_mute(true); + + /* + * SSM2603初期化 */ for (i = 0; i < sizeof(ssm2603_initdata); i+=2) { twi_master_write(TWI_DEVICE_ADDR, TWI_STOP, &ssm2603_initdata[i], 2); /* - * STOP CONDITION$B$+$i(B600ns$B0J>e6u$1$k(B + * STOP CONDITIONから600ns以上空ける */ for (j = 0; j < 300; j++) { asm("ssync;"); @@ -45,3 +55,12 @@ void ssm2603_init(void) } } +void ssm2603_mute(bool mute) +{ + if (mute) { + *pPORTGIO_CLEAR = PG13; + } else { + *pPORTGIO_SET = PG13; + } +} + diff --git a/firm/bare_metal/ssm2603.h b/firm/bare_metal/ssm2603.h index 7b673ea..904c56f 100644 --- a/firm/bare_metal/ssm2603.h +++ b/firm/bare_metal/ssm2603.h @@ -2,7 +2,10 @@ #ifndef SSM2603_H #define SSM2603_H +#include + void ssm2603_init(void); +void ssm2603_mute(bool mute); #endif -- 2.11.0