⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main_option2.c

📁 nrf24z1 代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*= main_option2.c =============================================================
 *
 * Copyright (C) 2005 Nordic Semiconductor
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * WARRANTY OF ANY KIND.
 *
 * Author(s): Borge Strand
 *
 * Description:
 *
 *    Startup, address generation, user interface for user interface option 2
 *    with soft power on only at ARX 
 *
 * Compiler: Tested with WinAVR, avr-gcc (GCC) 3.4.3
 *
 * Revision: 2.0 
 *
 *==============================================================================
 */


// This program sets up a private address for ARX and ATX, puts the system into sleep mode, and
// runs the user interface. After batteries are inserted and the on/off button is flipped to "ON", 
// the user must press and hold the ARX Play button for music to appear. The time the Play button
// has to be held depends on the aggressiveness of the time-division-multiplexing used in sleep mode;
// lower current consumption trades against the Play button having to be held for a longer time in
// order to turn on the system.
// This program receives all compatible buttons possible from both ATX and ARX user interfaces
// Fill in here how different button commands are to be passed to the audio source system. This 
// program only executes DAC volume control and nRF24Z1 power up/down.

// Certain parameters in z1config must be changed to work with this file. After restoring factory 
// default settings, please make the following alterations for user interface option 1:
//
// Link:
// LNKETH=0x20
// LNKMOD=0x08, Mute when LNKERRLNKETH
// NLCH=0x0F
// ADDR, use your preferred initial address. This address must match that of function z1_setinitialadr().
//
// Interrupts:
// INTCF=0x02, Enable wakeup from power down interrupt
//
// ATX:                                             ARX:
// I2SCNF_IN=0x80, Audio mode = Master mode         RXSTA=0x40, Disable serial slave interface
// TXLAT=0x04, Nominal, 20ms @ 48kHz                RXWTI=0x02
// TXWTI=0x02                                       RXLTI=0x4A
// TXLTI=0x4A                                       RXSTI=0x0000
// TXSTI=0x0030                                     RXWAKE=0x02, Wakeup on DI[1] change
// TXMOD=0x82, RF, I2S, 44.1                        I2SCNF_OUT=0x40, Mute sound
//                                                  RXMOD=0xA0, RF, I2S, RX power down

#define SLEEP_TXWTI     0x02                        // ATX is awake for 0x03 * 10ms = 30ms, ATX may go to auto power down
#define SLEEP_TXSTI_0   0x30                        // ATX is asleep for 0x30 * 10ms = 480ms
#define SLEEP_TXSTI_1   0x00
#define SLEEP_TXLTI     0x4A                        // ATX powers down after 0x02 * (0x4A + 1) * 10ms = 1500ms without link

#define SLEEP_RXWTI     0x02                        // ARX may go to auto power down
#define SLEEP_RXSTI_0   0x00                        // ARX sleep timer is not used, ARX is wake-on-interrupt
#define SLEEP_RXSTI_1   0x00
#define SLEEP_RXLTI     0x4A                        // Auto power down after 0x02 * (0x4A + 1) * 10ms = 1500ms without link, = on-time after button chg
#define SLEEP_RXWAKE    0x02                        // ARX wakes up from Play button, see button definitions in hpref_defines.h


int main(void) {
    char booterror = 0;                             // Wheter or not a certain while loop is to execute, 0 means successfull complesion of bootup step
    char debounce = 1;                              // Check buttons before accepting them
    char buttons;                                   // Which button is pressed?
    char powermode;                                 // The power up/down status of the nRF24Z1
    char muted = 0;                                 // Simulate Play/Pause button by muting and unmuting audio while retaining audio link

    #ifdef USELED
        char ledtemp, ledstatus = LEDON;            // Two registers used to track blink sequences of LEDs
    
        ledsequence = LED_INITIAL;                  // Blink according to linkup pattern
        mcu_atxled(z1_rotate_led());                // Left-rotate the led sequence and apply ATX light according to MSB
    #endif

    mcu_init();                                     // Initialize MCU
    mcu_2w_master_init();                           // Enable 2-wire slave interface to nRF24Z1

    #ifdef DEBUG                                    // If debug system is enabled, 
        mcu_uart_init();                            // Turn on UART for debug
        db_putenter();
        mcu_putchar('-');                           // Print welcome message
        mcu_putchar('D');
        mcu_putchar('E');
        mcu_putchar('V');
        mcu_putchar('-');
        db_putenter();
    #endif
    #ifdef DEBUGIF
        db_hex();                                   // Run debug interface if that is defined
    #endif

    volume      = 0xFF;                             // Volume is not initiated, dac_init() gives default value
    bass        = 0xFF;                             // Bass boost is not initiated, dac_init() gives default value, not used in HP ref design

    mcu_z1int_enable();                             // Turn on MCU'c interrupt to ATX nRF24Z1, wakeup from power down int. enabled in nRF24Z1 EEPROM

    // At this stage ATX is just turned on. ARX may be on or off. In any case, do not make any attempt at disabling the ARX DAC. 
    // Instead disable ATX resources (including ADC) and start looking for a link. 

    adc_init();                                     // nRF24Z1 eval kit has instant-on ADC, headphone reference design ADC needs init routine
    adc_sleep();                                    // ADC should not consume power while trying to establish link
    
    // ATX sleeps with wake-on-timer and gives out interrupt. Use that to look for link
    // In user interface option 2, the loop is terminated when Play is pressed on ARX.  
    
    // At this point in the code, the ATX is on because it was configured to do auto power down, and has not shut down
    // yet. Thus it is ready to receive on the 2-wire interface the command to wake up the ARX. During the link search loop below, 
    // both units have to be ready to establish a link. However, the ARX must go straight to power down and not be awake
    // for some time before going to auto power down. That is because the only wakeup souorce for the ARX is the user
    // pressing the ARX Play button. Linkup during ATX's initial waiting-to-go-to-auto-power-down is not allowed.
    
    z1_singlewrite(RXMOD, z1_singleread(RXMOD) & ~0x80);    // We are ready to wake up ARX when we receive a link attempt from it
    
    while (booterror == 0) {
        #ifdef USELED
            mcu_atxled(z1_rotate_led());            // Left-rotate the led sequence and apply ATX light according to MSB
        #endif

        // In option 2 wait for a link attempt from ARX.
        while ((mcu_z1int_active()) && (booterror == 0)) {  // If ATX is alive, keep polling for a link
            if (z1_haslink())                       // check if a link has been established, if it has
                booterror = 1;                      // Terminate this while loop. loop == 0 indicates this step OK in bootup process
        }
        if (booterror == 0)                         // If no link was established, wait to poll again
            mcu_wait_ms(320);                       // Wait routine is terminated either by interrupt or elapsed period
    }   

    booterror = 0;                                  // Loop ended, prepare to continue bootup process

    // A link now exists between ATX and ARX on the initial address. The next thing to do is pick a random private address and
    // put the DAC into the correct power mode. For user interface option 2, we got to this stage through user interaction,
    // and the DAC should be powered on. Also, the ARX nRF24Z1 should stop muting its digital audio output.
    
    // This code assumes that all units are produced equally and therefore have the same initial address loaded from EEPROM. The next
    // section of the code will pick a new address and transfer that to the ARX. The link will then be re-established with the new
    // private address. If, instead, your products are made in individual pairs, this section may be commented out. Picking a random
    // private address is done with the I2S bitclock as a randomness source. This means that the audio clock and the MCU clock must be 
    // asynchronous. It also means that the I2S interface must be operational. In the headphone reference design, nRF24Z1 ATX is I2S 
    // master. There is therefore no need to start up the ADC in order to generate the bitclock from the master clock.
    
    // With units powered up, change from initial to private address

    if (z1_flagready(LINKCSTATE) == OKAY) {         // If ATX is ready bo buffer private address
        z1_setprivateadr();                         // Draw a new private address, write it to ATX, Important: I2S bitclock must be active now!!
        z1_setflag(LINKCSTATE);                     // Tell ATX to relink with privately connected ARX
        mcu_wait_ms(32);                            // Give it some time to relink
    }
    else {                                          // LINKCSTATE was not ready to transfer a new address. Halt bootup process
        booterror = 1;
    }

    if (booterror == 0) {
        if (z1_haslink() == 0) {
            booterror = 1;
        }
    }

    // Done setting up the private address. Now that a link was established, set up the ARX DAC

    if (booterror == 0) {                           // If no error detected, 
        if (dac_init() != OKAY) {                   // Try to initialize DAC
            booterror = 1;                          // If that failed, stop bootup code
        }
    }
    
    // With full user interface either on ARX, user interaction was needed to bring the system up. Therefore: power up
    // all units and run the user interface. 
    
    // Insert code to start music on audio source here!
    
    adc_wake();                                     // Wake up ATX resources 
    
    if (booterror == 0) {                           // If no error detected, 
        if (dac_wake() != OKAY) {                   // Try to wake up DAC
            booterror = 1;                          // If that failed, stop bootup code
        }
    }

    if (booterror == 0) {                           // If no error detected, 
        if (z1_arx_mute(MUTEOFF) != OKAY) {         // Try to unmute ARX
            muted = 0;                              // Simulate play/pause button
            booterror = 1;                          // If that failed, stop bootup code
        }
    }
    
    // Right before the link-finding loop above the power down bit in RXMOD was cleared. The power down bit in TXMOD
    // was never set in EEPROM. TXMOD and RXMOD were both set for auto power down enable. Thus the devices
    // are now in awake mode and no modifications to any mode variables is needed before running the user interface.
    
    #ifdef USELED
        ledsequence = LED_AWAKE;                    // Blink in a pattern indicating audio streaming 
    #endif

    powermode = PMODE_AWAKE;                        // And indicate a power up of ATX


    // Run product interface by polling buttons on TX and RX unit

    while (booterror == 0) {                            
        // Buttons on ARX are hardwired to GPI pins DI[0:3] or written into RXPIN on ARX according to the table 
        // in hpref_defines.h. Buttons on ATX are translated in mcu_xxxx.c from the actual hardware I/O pins on the MCU
        // to a key code equal to the hardware key codes defined for ARX. The key codes are then processed in 
        // this file. All buttons on DI[0:3] are active high. Key codes comprising more than one high bit must
        // be generated by an ARX MCU or diode network. Unused ARX DI[0:3] pins must be tied to ground.
        // This part of the MCU program uses key polling and a low-power wait state. 
    
        if (powermode == PMODE_AUTODOWN)            // Auto power down was detected. Poll interrupt bit for power up
            mcu_wait_ms(320);
        else if (powermode == PMODE_POWERDOWN)      // Controlled power down, ATX nRF24Z1 internal timer must wake up MCU 
            mcu_wait_ms(6400);                      // If that fails, wait 6.4 seconds before investigating
        if (powermode == PMODE_AWAKE)               // Audio streaming mode, MCU polls for input from user interface 
            mcu_wait_ms(64);

        // There are six combinations of variables powermode and wakeup. These six combinations are summarized below.

        //// USER INTERFACE OPTION 2 //// ATX is wake-on-timer, ARX is wake-on-interrupt
        //              |   powermode ==        |   powermode ==            |   powermode == 
        //              |   PMODE_AUTODOWN      |   PMODE_POWERDOWN         |   PMODE_AWAKE
        //              |                       |                           |
        // wakeup ==    |   Check if user       |   ATX is momentarily      |   ATX went to auto 
        // WAKE_Z1_INT  |   pressed Play on ARX |   awake, check for        |   power down
        //              |   and thus got a      |   link attempt from ARX   |
        //              |   radio link, use     |                           |
        // wakeup ==    |   semi-fast polling   |   ATX did not generate    |   Audio streaming mode,
        // WAKE_WAIT    |   in MCU              |   interrupt in time!      |   MCU polls user interfaces
        
        if (powermode == PMODE_AUTODOWN) {
            // It was detected that nRF24Z1 went to auto power down. If this mode was registered, both the MCU timer and the 
            // ATX nRF24Z1 interrupt will wake the MCU in option 2. Going to auto power down, both TXMOD and RXMOD were set 
            // for audio streaming. Therefore there is no need to clear the sleep enable bits in TXMOD and RXMOD when exiting 
            // auto power down. 
                
            // ATX is wake-on-timer. In option 2 check if it was able to relink with ARX after user pressed Play on ATX
            if (mcu_z1int_active()) {               // Check if nRF24Z1 is ready to receive SPI or 2-wire commands
                if (z1_haslink()) {                 // Check if nRF24Z1 came online after auto power down

                    // Instruct your audio source to commence playing music here!

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -