Driver PL2303HX untuk Windows 10 64bit

Pernah beli alat USB to Serial Converter murah, dengan IC PL2303. Saat dipasang di komputer, ternyata butuh driver. Alhasil menemukan driver di situs produsen IC namun ada catatan :

Windows 8/8.1/10 are NOT supported in PL-2303HXA and PL-2303X EOL chip versions.

Untungnya ada situs yang baik hati (lupa dimana) memberikan file hasil modifikasi driver Windows 7 agar bisa dipasang di Windows 10.

Link: http://latiful.hayat.web.id/file/PL2303HX-edit.zip

Semoga bisa bermanfaat

Contoh kode interupsi I2S pada WAU8822 pada Nuvoton NUC-LB140

Contoh loopback dari dan ke WAU serta pembentukan sinyal sinusoida

#include "NUC100Series.h"
#include "gpio.h"
#include "portinit.h" //lcd
#include "lcd.h"
#include "wau8822n.h"

# define PLLCON_SETTING      CLK_PLLCON_50MHz_HIRC
# define PLL_CLOCK           50000000
# define HCLK_DIV            1

#define MUTE_OFF(x)         PE->DOUT &= ~(1 << 14)
#define MUTE_ON(x)          PE->DOUT |=  (1 << 14)

//#define POOLING
#define LOOPBACK 1
#define MONOTOSTEREO 1
#define GAIN 1//50

#define SIN_SAMPLES     32
int32_t i32SinIdx = 0;
int16_t i16Sin[SIN_SAMPLES] = {
    0,3203,6284,9124,11612,13655,15172,16106,16423,16107,15172,13655,
    11613,9124,6284,3204,1,-3205,-6284,-9124,-11613,-13654,-15173,-16107,
    -16422,-16106,-15172,-13655,-11613,-9123,-6285,-3203
};

uint32_t g_u32TxValue;
uint32_t connect=TRUE;
void displayLCD(){

    //PC15=0;
    print_Line(0,   " Teknik Elektro ");
    printS(2*8+4,1*16,"Universitas"); //printS(x, y, char)
    printS(2*8,2*16,"Muhammadiyah");
    printS(3*8,3*16, "Purwokert0");
    connect=FALSE;

}

uint32_t spectrum[128];
volatile uint8_t ii=0;
extern void wau(void){
    uint8_t i;
    for(i=0;i<128;i++){
        spectrum[i]=0;
    }


    SYS_UnlockReg(); // Unlock protected registers
    CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk ); // Enable HXT external 12MHz cyrstal
    CLK_SetCoreClock(50000000);    //  Set HCLK frequency 50MHz
    SYS_LockReg(); // Lock protected registers

    Port_Init(SPI3PORT);
    init_LCD();
    clear_LCD();

    //I2C - I2S WAU8822 codec
    WAU8822_Config();

    GPIO_SetMode(PE, BIT14, GPIO_PMD_OUTPUT);
    PE14=0;  //PHone OUT Enable (NUC-LB-140)

#ifdef POOLING
    //uint32_t u32Tmp;

    //open I2S function
    //st.u32SampleRate     = 16000;
    //st.u8WordWidth       = I2S_DATABIT_16;
    //st.u8AudioFormat     = I2S_STEREO;
    //st.u8DataFormat      = I2S_FORMAT_I2S;
    //st.u8Mode            = I2S_MODE_SLAVE;
    //st.u8TxFIFOThreshold = I2S_FIFO_TX_LEVEL_WORD_0;
    //st.u8RxFIFOThreshold = I2S_FIFO_RX_LEVEL_WORD_8;//SMP_ONE
    I2S_Open(I2S,I2S_MODE_MASTER,32000,I2S_DATABIT_16,I2S_STEREO,I2S_FORMAT_I2S);

    // Set MCLK and enable MCLK
    I2S_EnableMCLK(I2S,6000000);

    while(1)
    {

            if((I2S->STATUS & I2S_STATUS_TXFULL_Msk) == 0)
            {
                u32Tmp = i16Sini[i32SinIdxi];
                u32Tmp &= 0xFFFFUL;
                //u32Tmp |= u32Tmp << 16;  //duplicate it to stereo to Tx FIFO
                I2S->TXFIFO = u32Tmp;
                i32SinIdxi++;
                if(i32SinIdxi >= SIN_SAMPLESi)
                    i32SinIdxi = 0;

            }

    }
#else

    //interrupt
    I2S_Open(I2S,I2S_MODE_SLAVE,32000,I2S_DATABIT_16,I2S_STEREO,I2S_FORMAT_I2S);
    I2S_EnableMCLK(I2S,12000000);

    I2S_EnableInt(I2S,I2S_IE_RXTHIE_Msk );
    NVIC_EnableIRQ(I2S_IRQn);

    GPIO_SetMode(PC,BIT12,GPIO_PMD_OPEN_DRAIN);PA12=1;
    GPIO_SetMode(PC,BIT14,GPIO_PMD_OPEN_DRAIN);

    if(PE15==1) displayLCD();

    while(1)
    {
        CLK_SysTickDelay(1000000);
        if(PE15==0)
        {
            if(connect==FALSE) {
                clear_LCD();
                print_Line(0,   "Line-In Sound OK");
            }
            PC12 ^= 1;  //in LIN connected
            connect=TRUE;


        }else{
            if(connect){displayLCD();}

        }


    }
#endif
}

void I2S_IRQHandler(void)
{
    uint32_t u32Tmp=0;
    //unit32_t s =

    /* Fill sin samples to I2S until Tx FIFO full */
    while((I2S->STATUS & I2S_STATUS_TXFULL_Msk) == 0)
    {

#if LOOPBACK // Just loop Rx FIFO to Tx FIFO
        u32Tmp = I2S->RXFIFO;
        I2S->TXFIFO = (u32Tmp*GAIN);
#elif MONOTOSTEREO // Mono data in Rx FIFO and duplicate it to stereo to Tx FIFO
        u32Tmp = I2S->RXFIFO*GAIN & 0xFFFF0000UL;
        u32Tmp |= u32Tmp >> 16;
        I2S->TXFIFO = u32Tmp;

#else // Output Sin Wave
        u32Tmp = i16Sin[i32SinIdx];
        u32Tmp &= 0xFFFFUL;
        u32Tmp |= u32Tmp << 16;
        I2S->TXFIFO = u32Tmp;
        i32SinIdx++;
        if(i32SinIdx >= SIN_SAMPLES)
            i32SinIdx = 0;
#endif
    }
    ii = u32Tmp;
    PC->DOUT ^= (1 << 14);
}

Mikrokontroler Nuvoton NUC140 (ARM)

Mikrokontroler Nuvoton NUC140 (ARM) ARM adalah prosesor dengan arsitektur set instruksi 32­bit RISC (Reduced Instruction Set Computer) yang dikembangkan oleh ARM Holdings. ARM merupakan singkatan dari Advanced RISC Machine (sebelumnya lebih dikenal dengan kepanjangan Acorn RISC Machine). Dalam proses perkembangannya, akhirnya kekayaan intelektual tentang ARM dibeli oleh ARM Ltd namun ARM Ltd kemudian memutuskan untuk tidak memproduksi ARM prosesor, tetapi melisensikan desain prosesor tersebut untuk digabungkan dengan ASIC (Application Specific IC) yang membutuhkan kontroler embedded (contoh: kontroler printer, kontroler mesin cuci, kontroler video dekoder, kontroler ethernet hub/router, dan sebagainya). ARM klasik (Classic ARM Processors) adalah keluarga ARM prosesor yang pertama kali dirilis oleh ARM Ltd (sekarang ARM Holdings). Prosesor ARM klasik ideal untuk pengguna yang ingin menggunakan teknologi telah teruji di pasar. Prosesor­prosesor ini telah digunakan untuk berbagai macam produk elektronik selama bertahun­-tahun. Desainer produk elektronik yang memilih prosesor­prosesor ini dijamin mempunyai dukungan ekosistem dan sumber daya yang luas, tingkat kesulitan integrasi yang minimum, dan menurunkan waktu desain.ARM Cortex Embedded (ARM Cortex Embedded Processors)Prosesor­prosesor di keluarga seri Cortex­M telah dikembangkan khusus untuk domain mikrokontroler, dimana permintaan untuk kecepatan, determinasi waktu proses, dan manajemen interrupt bersama dengan jumlah gate silikon minimum (luas silikon yang minimum menentukan harga akhir prosesor) dan konsumsi daya yang minimum sangat diminati. Contoh aplikasi prosesor Cortex­M adalah mikrokontroller dan sensor cerdas.Prosesor­-prosesor di keluarga seri Cortex­R, sebaliknya, dikembangkan khusus untuk keperluan real­time yang mendalam, dimana kebutuhan konsumsi daya minimum dan sifat interrupt yang terprediksi diimbangi dengan performa yang luar biasa dan kompatibilitas yang kuat dengan platform yang telah ada. Contoh aplikasi prosesor Cortex­R adalah ABS (Automotive Braking Systems), kontroler elektronik roda gigi, hidrolik, dan mesin otomotif.ARM Cortex Prosesor (ARM Cortex Application Processors)Prosesor­prosesor di keluarga prosesor aplikasi dikembangkan untuk aplikasi yang membutuhkan daya komputasi yang tinggi (frekuensi prosesing rata­rata 2GHz), seperti netbook, mobile internet devices, smartphone, dan lain-­lain. Mikrokontroler Nuvoton NUC140NUC140 series adalah ARM Cortex mikrokontroler dengan M0. Cortex M0 adalah prosesor ARM terbaru dengan kinerja 32 bit dengan biaya setara dengan mikrokontroler 8 bit. ARM Cortex­M0 mempunyai peripheral­peripheral yang terintegrasi dengan prosesor. Peripheral­peripheral tersebut merupakan bagian dari desain ARM Cortex­M0. Karena itu, peripheral­peripheral tersebut terdapat di semua mikroprosesor yang berbasis ARM Cortex­M0, walaupun dibuat oleh manufaktur yang berbeda. Selain itu, peripheral­peripheral yang terdapat di ARM Cortex­M0, juga terdapat di prosesor ARM Cortex­M yang lain (ARM Cortex­M1, ARM Cortex­M3, ARM Cortex­M4), sehingga semua prosesor di keluarga ARM Cortex­M kompatibel satu sama lainnya.Kompatibilitas Set Instruksi Nuvoton NUC140 ARM Cortex­M0Set instruksi yang digunakan oleh ARM Cortex­M0 dinamakan set instruksi Thumb. Set instruksi ARM Cortex­M0 adalah subset dari set instruksi ARM Cortex­M yang lain, sehingga program yang dikompile untuk ARM Cortex­M0 kompatibel dengan prosesor ARM Cortex­M yang lain.Selain itu, ARM Cortex­M0 juga kompatibel dengan set instruksi prosesor ARM dari seri klasik dan Cortex­A (ARM Cortex Application Processor).NuMicro seri NUC1xx memiliki inti ARM Cortex M0 yang tertanam dengan kecepatan hingga 50 MHz, dilengkapi dengan memori flash untuk program 32KB/64KB/128KB, SRAM sebesar 4KB/8KB/16KB dan memori flash loader untuk ISP (In System Programming) sebesar 4KB. Selain itu juga dilengkapi dengan berbagai macam periperal, seperti GPIO, Timer, Watchdog Timer, RTC, PDMA, UART, SPI/MICROWIRE, I2C, I2S, PWM, LIN, CAN, PS2, USB 2.0 FS Device, ADC 12 bit, komparator analog, Low Voltage Reset, dan Brown Out Detector. Sumber Wacana:http://www.kelas-mikrokontrol.com/e-learning/mikrokontroler/mengenal-arm-cortexm0.htmlhttp://gygasjunipratama.com/category/nuvoton/Daftar Catatan: Mikrokontroler Nuvoton NUC140 Arduino: ATMega8535 dengan Arduino IDE Be wise in using Microcontroller’s memory Driver USBasp 64bit How to make USBAsp work with Arduino in Linux Intel Edison a Computer smaller than Raspberry Pi Kalibrasi MPU6050 Koneksi PHP – Serial MAX232 Memprogram AT89S52 dengan Arduino Routing PCB dengan CNC (YOOCNC 3020ZD) Skema star delta untuk motor 3 fasa dengan timer Timer Interrupt on ATMega8535 using Arduino IDE Ways to detecting heart rate

Sumber: Mikrokontroler Nuvoton NUC140 (ARM) – | Latiful Hayat

Library untuk WAU8822 pada Nuvoton LB-140

Berikut contoh library untuk WAU8822 pada Nuvoton LB-140

Penggunaan:

#include "NUC100Series.h"
    void main (void){
    SYS_UnlockReg(); // Unlock protected registers
    CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk ); // Enable HXT external 12MHz cyrstal
    CLK_SetCoreClock(50000000);    //  Set HCLK frequency 50MHz
    SYS_LockReg(); // Lock protected registers

    //I2C - I2S WAU8822 codec
    WAU8822_Config();

    GPIO_SetMode(PE, BIT14, GPIO_PMD_OUTPUT);
    PE14=0;  //PHone OUT Enable (NUC-LB-140)

#ifdef POOLING
    //open I2S function
    //st.u32SampleRate     = 16000;
    //st.u8WordWidth       = I2S_DATABIT_16;
    //st.u8AudioFormat     = I2S_STEREO;
    //st.u8DataFormat      = I2S_FORMAT_I2S;
    //st.u8Mode            = I2S_MODE_SLAVE;
    //st.u8TxFIFOThreshold = I2S_FIFO_TX_LEVEL_WORD_0;
    //st.u8RxFIFOThreshold = I2S_FIFO_RX_LEVEL_WORD_8;//SMP_ONE
    I2S_Open(I2S,I2S_MODE_MASTER,32000,I2S_DATABIT_16,I2S_STEREO,I2S_FORMAT_I2S);

    // Set MCLK and enable MCLK
    I2S_EnableMCLK(I2S,6000000);

    while(1)
    {
            if((I2S->STATUS & I2S_STATUS_TXFULL_Msk) == 0)
            {
               //write your code here
            }
    }
#else   //interrupt

    //interrupt
    I2S_Open(I2S,I2S_MODE_SLAVE,32000,I2S_DATABIT_16,I2S_STEREO,I2S_FORMAT_I2S);
    I2S_EnableMCLK(I2S,12000000);

    I2S_EnableInt(I2S,I2S_IE_RXTHIE_Msk );
    NVIC_EnableIRQ(I2S_IRQn);
    
    while(1){}
#endif
}

void I2S_IRQHandler(void)
{
    /* Fill sin samples to I2S until Tx FIFO full */
    while((I2S->STATUS & I2S_STATUS_TXFULL_Msk) == 0)
    {
         //write your interrupt code here
    }
}

wau8822n.h

 #ifndef WAU8822N_H
 #define WAU8822N_H

#include "NUC100Series.h"
 #include "gpio.h"

#define WAU8822_INIT_MASTER 0x1
 #define WAU8822_INIT_SLAVE 0x2
 #define WAU8822_INIT_IN_LINE_L 0x4
 #define WAU8822_INIT_IN_LINE_R 0x8
 #define WAU8822_INIT_IN_MIC_L 0x10
 #define WAU8822_INIT_IN_MIC_R 0x20
 #define WAU8822_INIT_IN_AUX_L 0x40
 #define WAU8822_INIT_IN_AUX_R 0x80
 #define WAU8822_INIT_OUT_HP_L 0x100
 #define WAU8822_INIT_OUT_HP_R 0x200
 #define WAU8822_INIT_OUT_AUX1 0x400
 #define WAU8822_INIT_OUT_AUX2 0x800

#define WAU8822_INIT_SR8000 0x1000
 #define WAU8822_INIT_SR12000 0x2000
 #define WAU8822_INIT_SR16000 0x4000
 #define WAU8822_INIT_SR24000 0x8000
 #define WAU8822_INIT_SR32000 0x10000
 #define WAU8822_INIT_SR48000 0x20000

void I2C_WriteWAU8822(uint8_t regAddr, uint16_t u16data);
 void WAU8822_Init(uint32_t u32Option);
 void WAU8822_Config(void);

#endif

wau8822n.c

#include "wau8822n.h"

void I2C_WriteWAU8822(uint8_t regAddr, uint16_t u16data)
{
    I2C_START(I2C0);                            //Start
    I2C_WAIT_READY(I2C0);                        //Wait action completed

    I2C_SET_DATA(I2C0,  0x34);                    //set WAU8822 I2C address
    I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);     //Set SI (transfer data)
    I2C_WAIT_READY(I2C0);                        //Wait action completed

    I2C_SET_DATA(I2C0, (uint8_t)((regAddr << 1) | (u16data >> 8)));  //set I2Cdata w/  reg address shifted << 1, + data bit 8
    I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI | I2C_I2CON_AA);          //Set SI and AA (transfer data with Acknowledge)
    I2C_WAIT_READY(I2C0);                        //Wait action completed

    I2C_SET_DATA(I2C0, (uint8_t)(u16data & 0x00FF) );                //set I2Cdata w/ data bit 7:0
    I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI | I2C_I2CON_AA);         //Set SI and AA (transfer data with Acknowledge)
    I2C_WAIT_READY(I2C0);                        //Wait action completed

    I2C_STOP(I2C0);                                //Stop

}


void WAU8822_Init(uint32_t u32Option)
{
    uint32_t u32Reg;

    I2C_WriteWAU8822(0, 0x000);//Reset

    u32Reg = 0x00B;
    if(u32Option & WAU8822_INIT_MASTER)
        u32Reg |= 0x020; // Enable PLL in Master mode
    if(u32Option & WAU8822_INIT_OUT_AUX1)
        u32Reg |= 0x084; // For AUX1 Output
    if(u32Option & WAU8822_INIT_OUT_AUX2)
        u32Reg |= 0x044; // For AUX2 Output
    if(u32Option & (WAU8822_INIT_IN_MIC_L | WAU8822_INIT_IN_MIC_R))
        u32Reg |= 0x010; // For MIC Bias output
    I2C_WriteWAU8822(1, u32Reg);

    u32Reg = 0;
    if(u32Option & WAU8822_INIT_OUT_HP_L)
        u32Reg |= 0x080; // Enable Headphone Left output
    if(u32Option & WAU8822_INIT_OUT_HP_R)
        u32Reg |= 0x100; // Enable Headphone Right output
    if(u32Option & (WAU8822_INIT_IN_LINE_L | WAU8822_INIT_IN_MIC_L | WAU8822_INIT_IN_AUX_L))
        u32Reg |= 0x011; // Enable LADC Mix/Boost & Left ADC
    if(u32Option & (WAU8822_INIT_IN_LINE_R | WAU8822_INIT_IN_MIC_R | WAU8822_INIT_IN_AUX_R))
        u32Reg |= 0x022; // Enable RADC Mix/Boost & Right ADC
    if(u32Option & WAU8822_INIT_IN_MIC_L)
        u32Reg |= 0x004; // Enable Left PGA
    if(u32Option & WAU8822_INIT_IN_MIC_R)
        u32Reg |= 0x008; // Enable Right PGA
    I2C_WriteWAU8822(2, u32Reg);

    u32Reg = 0;
    if(u32Option & WAU8822_INIT_OUT_AUX1)
        u32Reg |= 0x102; // Enable AUX1 & RDAC
    if(u32Option & WAU8822_INIT_OUT_AUX2)
        u32Reg |= 0x081; // Enable AUX2 & LDAC
    if(u32Option & WAU8822_INIT_OUT_HP_L)
        u32Reg |= 0x005; // Eanble LMIX & LDAC
    if(u32Option & WAU8822_INIT_OUT_HP_R)
        u32Reg |= 0x00A; // Eanble RMIX & RDAC
    I2C_WriteWAU8822(3, u32Reg);

    I2C_WriteWAU8822(4,  0x010);// I2S 16-bit format

    I2C_WriteWAU8822(10,  0x000);

    I2C_WriteWAU8822(5,  0x000);//R5 companding ctrl and loop back mode (all disable)

    if(u32Option & WAU8822_INIT_MASTER)
    {
        // Codec works as an Master

        // MCLK = 6MHz
        I2C_WriteWAU8822(36, 0x008);
        I2C_WriteWAU8822(37, 0x00C);
        I2C_WriteWAU8822(38, 0x093);
        I2C_WriteWAU8822(39, 0x0E9);
        if(u32Option & WAU8822_INIT_SR8000)
        {
            I2C_WriteWAU8822(6 , 0x1AD);
            I2C_WriteWAU8822(7 , 0x00A);
        }
        else if(u32Option & WAU8822_INIT_SR12000)
        {
            I2C_WriteWAU8822(6 , 0x18D);
            I2C_WriteWAU8822(7 , 0x008);
        }
        else if(u32Option & WAU8822_INIT_SR16000)
        {
            I2C_WriteWAU8822(6 , 0x16D);
            I2C_WriteWAU8822(7 , 0x006);
        }
        else if(u32Option & WAU8822_INIT_SR24000)
        {
            I2C_WriteWAU8822(6 , 0x14D);
            I2C_WriteWAU8822(7 , 0x004);
        }
        else if(u32Option & WAU8822_INIT_SR32000)
        {
            I2C_WriteWAU8822(6 , 0x12D);
            I2C_WriteWAU8822(7 , 0x002);
        }
        else if(u32Option & WAU8822_INIT_SR48000)
        {
            I2C_WriteWAU8822(6 , 0x10D);
            I2C_WriteWAU8822(7 , 0x000);
        }
    }
    else
    {
        I2C_WriteWAU8822(6 , 0x000);
        if(u32Option & WAU8822_INIT_SR8000)
        {
            I2C_WriteWAU8822(7 , 0x00A);
        }
        else if(u32Option & WAU8822_INIT_SR16000)
        {
            I2C_WriteWAU8822(7 , 0x006);
        }
        else if(u32Option & WAU8822_INIT_SR24000)
        {
            I2C_WriteWAU8822(7 , 0x004);
        }
        else if(u32Option & WAU8822_INIT_SR32000)
        {
            I2C_WriteWAU8822(7 , 0x002);
        }
        else if(u32Option & WAU8822_INIT_SR48000)
        {
            I2C_WriteWAU8822(7 , 0x000);
        }
    }

    I2C_WriteWAU8822(8 , 0x034);

    I2C_WriteWAU8822(59, 0x000);
    I2C_WriteWAU8822(60, 0x020);
    I2C_WriteWAU8822(61, 0x000);

    I2C_WriteWAU8822(10, 0x009);//R10 DAC control (softmute disable, oversample select 128x)
    I2C_WriteWAU8822(43, 0x020);//speaker mute

    u32Reg = 0;
    if(u32Option & WAU8822_INIT_IN_MIC_L)
        u32Reg |= 0x003; // Connect LMICP, LMICN to LPGA
    if(u32Option & WAU8822_INIT_IN_MIC_R)
        u32Reg |= 0x030; // Connect RMICP, RMICN to RPGA
    I2C_WriteWAU8822(44, u32Reg);

//
// MIC input gain control
//
    u32Reg = 0;
    if(u32Option & WAU8822_INIT_IN_MIC_L)
        I2C_WriteWAU8822(45, 0x110); // MIC LPGA Gain 0dB
    else
        I2C_WriteWAU8822(45, 0x140); // MIC LPGA Mute

    u32Reg = 0;
    if(u32Option & WAU8822_INIT_IN_MIC_R)
        I2C_WriteWAU8822(46, 0x110); // MIC RPGA Gain 0dB
    else
        I2C_WriteWAU8822(46, 0x140); // MIC RPGA Mute

//
// Line In gain control
//
    u32Reg = 0;
    if(u32Option & WAU8822_INIT_IN_LINE_L)
        u32Reg |= 0x050; // LINE LPGA Gain 0dB
    if(u32Option & WAU8822_INIT_IN_AUX_L)
        u32Reg |= 0x005; // AUX LPGA Gain 0dB
    I2C_WriteWAU8822(47, u32Reg);

    u32Reg = 0;
    if(u32Option & WAU8822_INIT_IN_LINE_R)
        u32Reg |= 0x050; // LINE RPGA Gain 0dB
    if(u32Option & WAU8822_INIT_IN_AUX_R)
        u32Reg |= 0x005; // AUX RPGA Gain 0dB
    I2C_WriteWAU8822(48, u32Reg);


    I2C_WriteWAU8822(49, 0x01E);

    I2C_WriteWAU8822(50, 0x001);//R50 DACL2LMIX
    I2C_WriteWAU8822(51, 0x001);//R51 DACR2RMIX

//
// For Headphone Output
//

     // Left headphone volume/mute control
    if(u32Option & WAU8822_INIT_OUT_HP_L)
        I2C_WriteWAU8822(52, 0x039);
    else
        I2C_WriteWAU8822(52, 0x079);

     // Right headphone volume/mute control
    if(u32Option & WAU8822_INIT_OUT_HP_R)
        I2C_WriteWAU8822(53, 0x139);
    else
        I2C_WriteWAU8822(53, 0x179);

//
// For AUX output
//
    if(u32Option & WAU8822_INIT_OUT_AUX2)
        I2C_WriteWAU8822(56, 0x001); // LDAC to AUX2
    else
        I2C_WriteWAU8822(56, 0x040); // AUX2 Mute

    if(u32Option & WAU8822_INIT_OUT_AUX1)
        I2C_WriteWAU8822(57, 0x001); // RDAC to AUX1
    else
        I2C_WriteWAU8822(57, 0x040);// AUX1 Mute

    I2C_WriteWAU8822(11, 0x0FF); // LDAC Volume
    I2C_WriteWAU8822(12, 0x1FF); // RDAC Volume

    I2C_WriteWAU8822(54, 0x040);
    I2C_WriteWAU8822(55, 0x140);

    // high pass eanble
    I2C_WriteWAU8822(14, 0x180);

    /* Disable EQ and 3D */
    I2C_WriteWAU8822(41, 0x000); // No 3D
    I2C_WriteWAU8822(18, 0x02C);
    I2C_WriteWAU8822(19, 0x02C);
    I2C_WriteWAU8822(20, 0x02C);
    I2C_WriteWAU8822(21, 0x02C);
    I2C_WriteWAU8822(22, 0x02C);

}


void WAU8822_Config(void)
{
    //Set I2C0 source Clock to default with no Divider
     CLK_SetModuleClock(I2C0_MODULE, 0, 0);
     //Enable I2C0
     CLK_EnableModuleClock(I2C0_MODULE);
    // Set I2C I/O
    SYS->GPA_MFP |= SYS_GPA_MFP_PA8_I2C0_SDA;
    SYS->GPA_MFP |= SYS_GPA_MFP_PA9_I2C0_SCL;
     // Open I2C0, and set clock = 100Kbps
    I2C_Open(I2C0, 100000);
    // Enable I2C0 interrupt and set corresponding NVIC bit
    I2C_EnableInt(I2C0);


    // Setup wau8822 codec - I2C
    //WAU8822_Setup();
    WAU8822_Init(WAU8822_INIT_MASTER | WAU8822_INIT_SR32000
            | WAU8822_INIT_OUT_HP_L | WAU8822_INIT_OUT_HP_R
            //| WAU8822_INIT_IN_MIC_L
            | WAU8822_INIT_IN_AUX_L | WAU8822_INIT_IN_AUX_R
                );

    CLK_SetModuleClock(I2S_MODULE,CLK_CLKSEL2_I2S_S_HXT,0);
    CLK_EnableModuleClock(I2S_MODULE);

    SYS->GPA_MFP = SYS_GPA_MFP_PA15_I2S_MCLK;
    SYS->GPC_MFP = SYS_GPC_MFP_PC0_I2S_LRCLK | SYS_GPC_MFP_PC1_I2S_BCLK | SYS_GPC_MFP_PC2_I2S_DI | SYS_GPC_MFP_PC3_I2S_DO;
    SYS->ALT_MFP = SYS_ALT_MFP_PA15_I2S_MCLK | SYS_ALT_MFP_PC0_I2S_LRCLK | SYS_ALT_MFP_PC1_I2S_BCLK | SYS_ALT_MFP_PC2_I2S_DI | SYS_ALT_MFP_PC3_I2S_DO;

    // Tri-state for FS and BCLK of CODEC
    GPIO_SetMode(PC, BIT0,  GPIO_PMD_OPEN_DRAIN);PC0 = 1;
    GPIO_SetMode(PC, BIT1, GPIO_PMD_OPEN_DRAIN);PC1 = 1;

}

portinit.h

#ifndef _INITIALIZATION_H
#define _INITIALIZATION_H

enum {GPIOPIN,
    ADC0PIN, ADC1PIN, ADC2PIN, ADC3PIN, ADC4PIN, ADC5PIN, ADC6PIN, ADC7PIN,
    SPI0PORT, SPI1PORT, SPI2PORT, SPI3PORT};

extern void Port_Init(uint32_t pinName);
#endif

PortInitNUC140.c

#include <stdio.h>
#include "NUC100Series.h"
#include "portinit.h"

extern void Port_Init(uint32_t pinName){
    switch(pinName){
    case ADC7PIN:
            // Configure the GPA7 ADC analog input pins
            SYS->GPA_MFP &= ~(SYS_GPA_MFP_PA7_Msk);
            SYS->GPA_MFP |= SYS_GPA_MFP_PA7_ADC7;
            // Disable the GPA7 digital input path to avoid the leakage current.
            GPIO_DISABLE_DIGITAL_PATH(PA, SYS_GPA_MFP_PA7_Msk);
            break;
    case SPI1PORT:
        SYS->GPC_MFP &= ~(SYS_GPC_MFP_PC8_Msk | SYS_GPC_MFP_PC9_Msk | SYS_GPC_MFP_PC10_Msk | SYS_GPC_MFP_PC11_Msk);
        SYS->GPC_MFP |=  (SYS_GPC_MFP_PC8_SPI1_SS0 | SYS_GPC_MFP_PC9_SPI1_CLK | SYS_GPC_MFP_PC10_SPI1_MISO0 | SYS_GPC_MFP_PC11_SPI1_MOSI0);
        break;
    case SPI2PORT:
        // Setup SPI3 multi-function pins
        SYS->GPD_MFP = (SYS->GPD_MFP & ~(SYS_GPD_MFP_PD0_Msk | SYS_GPD_MFP_PD1_Msk | SYS_GPD_MFP_PD2_Msk | SYS_GPD_MFP_PD3_Msk)) |
                       (SYS_GPD_MFP_PD0_SPI2_SS0 | SYS_GPD_MFP_PD1_SPI2_CLK | SYS_GPD_MFP_PD2_SPI2_MISO0 | SYS_GPD_MFP_PD3_SPI2_MOSI0);
        break;
    case SPI3PORT:
        // Setup SPI3 multi-function pins
        SYS->GPD_MFP = (SYS->GPD_MFP & ~(SYS_GPD_MFP_PD8_Msk | SYS_GPD_MFP_PD9_Msk | SYS_GPD_MFP_PD10_Msk | SYS_GPD_MFP_PD11_Msk)) |
                       (SYS_GPD_MFP_PD8_SPI3_SS0 | SYS_GPD_MFP_PD9_SPI3_CLK | SYS_GPD_MFP_PD10_SPI3_MISO0 | SYS_GPD_MFP_PD11_SPI3_MOSI0);
        break;


    default:
        break;
    }
}

 

Be wise in using Microcontroller’s memory

Memory is a finite resource on these tiny processors and some applications are just plain too big for an microcontroler(uC). But most code has some room for optimization. So if program is just a little overweight, with a little diet and exercise, probably shed enough bytes to make it fit into uC. As we know, there are 3 types of memory in uC:

  • Flash or Program Memory
  • SRAM
  • EEPROM

Flash Memory

Flash memory is used to store your program image and any initialized data. You can execute program code from flash, but you can’t modify data in flash memory from your executing code. To modify the data, it must first be copied into SRAM

Flash memory is the same technology used for thumb-drives and SD cards. It is non-volatile, so your program will still be there when the system is powered off.

Flash memory has a finite lifetime of about 100,000 write cycles. So if you upload 10 programs a day, every day for the next 27 years, you might wear it out.

SRAM

SRAM or Static Random Access Memory, can be read and written from your executing program. SRAM memory is used for several purposes by a running program:

  • Static Data – This is a block of reserved space in SRAM for all the global and static variables from your program. For variables with initial values, the runtime system copies the initial value from Flash when the program starts.
  • Heap – The heap is for dynamically allocated data items. The heap grows from the top of the static data area up as data items are allocated.
  • Stack – The stack is for local variables and for maintaining a record of interrupts and function calls. The stack grows from the top of memory down towards the heap. Every interrupt, function call and/or local variable allocation causes the stack to grow. Returning from an interrupt or function call will reclaim all stack space used by that interrupt or function.

EEPROM

EEPROM is another form of non-volatile memory that can be read or written from your executing program. It can only be read byte-by-byte, so it can be a little awkward to use. It is also slower than SRAM and has a finite lifetime of about 100,000 write cycles (you can read it as many times as you want).

Several Memory Problems

Most memory problems occur when the stack and the heap collide. When this happens, one or both of these memory areas will be corrupted with unpredictable results. In some cases it will cause an immediate crash. In others, the effects of the corruption may not be noticed until much later. One way to diagnose memory problems is to measure how much memory is in use.

Optimize Flash memory

Flash memory usage is depend on code and compiler. If it have reached or exceeded the space available, some of these optimizations may help get you back under the limit.

Remove Dead Code

If project is a mash-up of code from several sources, chances are there are parts that are not getting used and can be eliminated to save space.

  • Unused Libraries – Are all the #include libraries actually used?
  • Unused Functions – Are all the functions acutally being called?
  • Unused Variables – Are all the variables actually being used?
  • Unreachable Code – Are there conditional expressions which will never be true?
 Hint: If  not sure about an #include, a function or a variable. Comment it out. If the program still compiles, that code is not being used. Just remove it!

Consolidate Repeated Code

If any sequence of code statements in two or more places, consider making a function out of them.

Eliminate the Bootloader

If space is really-really tight, you might consider eliminating the bootloader. This can save as much as 2K or 4K of Flash – depending on which bootloader you are currently using.

The downside of this is that you will need to load your code using an ISP programmer instead of via a standard USB cable.

 

Optimize SRAM

SRAM is the most precious memory commodity. SRAM shortages are probably the most common memory problems. They are also the hardest to diagnose. If program is failing in an otherwise inexplicable fashion, a crashed may happen in the stack due to a SRAM shortage. There are a number of things that can do to reduce SRAM usage.

Remove Unused Variables

If not sure whether a variable is being used or not, comment it out. If the sketch still compiles, get rid of it!

Use F() Macro

Literal strings are repeat memory offenders. First they take up space in the program image in Flash, then they are copied to SRAM at startup as static variables. This is a horrible waste of SRAM since we will never be writing to them.

Paul Stoffregen of PJRC and Teensyduino fame developed the F() macro as a super-simple solution to this problem. The F() macro tells the compiler to keep your strings in PROGMEM. All you have to do is to enclose the literal string in the F() macro.

For example, replacing this:

  Serial.println("This string will be stored in memory. Horible waste of SRAM!");

with this:

  Serial.println(F(" This string will be stored in memory. Horible waste of SRAM!"));

Will save bytes of wonderful SRAM!

Reserve() your strings

The string library allows to reserve buffer space for a string with the reserve() function. The idea is prevent String from fragmenting the heap by using reserve(num) to pre-allocate memory for a String that grows.

With the memory already allocated, String doesn’t need to call realloc() if the string grows in length. In most usages, lots of other little String objects are used temporarily as you perform these operations, forcing the new string allocation to a new area of the heap and leaving a big hole where the previous one was (memory fragmentation). Usually all you need to do is use reserve() on any long-lived String objects that you know will be increasing in length as you process text.

Move constant data to PROGMEM.

Data items declared as PROGMEM do not get copied to SRAM at startup. They are a little less convenient to work with, but they can save significant amounts of SRAM. The basic Arduino reference for PROGMEM is here. And there is a more detailed tutorial on the subject here.

#define FS(x) (__FlashStringHelper*)(x)
const char MyText[]  PROGMEM  = { “My flash based text” };

void setup() {
Serial.begin(57600):
Serial.println(FS(MyText));
}

Reduce Buffer Sizes

Buffer and Array Allocations: If you allocate a buffer, make sure it is no bigger than it needs to be.

Buffers in Libraries: Also be aware that some libraries allocate buffers behind the scenes that may be candidates for trimming as well.

System Buffers: Another buffer hidden deeply in the system is the 64 byte serial receive buffer. If your sketch is not receiving a lot of high-speed serial data, you can probably cut this buffer size in half – or maybe even less.

For an Example, if using Arduino library, Serial buffer can be reduced. The Serial buffer size is defined in HardwareSerial.cpp. This file can be found in your Arduino install directory: \Arduino-1.x.x\hardware\arduino\cores\arduino\HardwareSerial.cpp. Look for the line: #define SERIAL_BUFFER_SIZE 64 And change it to 32 or less.

Reduce Oversized Variables: Don’t use a float when an int will do. Don’t use an int when a byte will do. Try to use the smallest data type capable of holding the information. Use char instead of int if only need 255 bits data.

 

Think Globally. Allocate Locally.

Let’s have another look at how SRAM is used (and abused):

Global & Static Variables

Global and Static variables are the first things loaded into SRAM. They push the start of the heap upward toward the stack and they will occupy this space for all eternity.

Dynamic Allocations

Dynamicaly allocated objects and data cause the heap to grow toward the stack. Unlike Global and Static variables, these variables can be de-allocated to free up space. But this does not necessarily cause the heap to shrink! If there is other dynamic data above it in the heap, the top of the heap will not move. When the heap is full of holes like swiss cheese we call it a “fragmented heap”.

Local Variables

Every function call creates a stack frame that makes the stack grow toward the heap. Each stack frame will contain:

  • All parameters passed to the function
  • All local variables declared in the function.

This data is usable within the function, but the space is 100% reclaimed when the function exits!

The Takeaway

Avoid dynamic heap allocations – These can quickly fragment the limited heap-space.

Prefer local to global allocation – Stack variables only exist while they are being used. If you have variables that only are used in a small section of your code, consider making that code into a function and declaring the variables local to the function.

 

Optimize EEPROM

EEPROM is a handy, non-volatile storage space that works well for storing data such as calibration or tuning constants that are not practical to hard-code into Flash.

It is unusual to run out of EEPROM. And it is not often practical to use EEPROM to offload SRAM data. But we’ll mention it here for completeness. Using EEPROM requires that you include the EEPROM library.

#include <EEPROM.h>

The EEPROM library gives us 2 functions:

uint8_t read(int) //Read a byte from the specified EEPROM address

void write(int, uint8_t) //Write a byte to the specified EEPROM address

  • Note that while reads are unlimited, there are a finite number of write cycles limited (typically about 100,000).
  • Always check that EEPROM is ready before reading/writing to it (eeprom_is_ready function)
  • Always prefer the update functions rather than the write ones, as update checks first if the stored data is different than the data, so it erases / writes the new data only if it has changed. Check first if the value has changed before writing, or even use the EEPROMex alternative EEPROM Arduino library.
  • Read / write operations on EEPROM should never be interrupted : you should always disable/clear interrupts (cli()) before any operation and re-enable/set interrupts after (sei()).

Here is the example:

while (!eeprom_is_ready());

cli();

if(eeprom_read_word((uint16_t*)addr) != sensorValue) {

eeprom_write_word((uint16_t*)addr, sensorValue);

}

sei();