📄 main_option3.c
字号:
/*= main_option3.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 3
* with soft power on at both ATX and 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 ATX or 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=0x01
// TXWTI=0x0D RXLTI=0x33
// TXLTI=0x03 RXSTI=0x0009
// TXSTI=0x0075 RXWAKE=0x10, Wakeup on sleep timer
// TXMOD=0x82, RF, I2S, 44.1 I2SCNF_OUT=0x40, Mute sound
// RXMOD=0x20, RF, I2S
// Two ways to do ATX wakeup timing:
// 1) Fast timing, low probability of contact during a single ATX wake period, but periods are close in time:
#define SLEEP_TXWTI 0x0D // ATX is awake for 0x0d * 10ms = 130ms
#define SLEEP_TXSTI_0 0x75 // ATX is asleep for 0x75 * 10ms = 1170ms (90% down time)
#define SLEEP_TXSTI_1 0x00
#define SLEEP_TXLTI 0x03 // ATX powers down after 0x0d * (0x03 + 1) * 10ms = 520ms without link
// 2) Slow timing, high probability of contact during a single ATX wake period, but periods are far apart in time:
// #define SLEEP_TXWTI 0x19 // ATX is awake for 0x19 * 10ms = 250ms
// #define SLEEP_TXSTI_0 0xE1 // ATX is asleep for 0xE1 * 10ms = 2250ms (90% down time)
// #define SLEEP_TXSTI_1 0x00
// #define SLEEP_TXLTI 0x01 // ATX powers down after 0x19 * (0x01 + 1) * 10ms = 500ms without link
#define SLEEP_RXWTI 0x01 // ARX is awake for 1 * 10ms = 10ms at a time (10% up time)
#define SLEEP_RXSTI_0 0x09 // ARX sleeps for 0x0009 * 10ms = 90ms while polling the air
#define SLEEP_RXSTI_1 0x00
#define SLEEP_RXLTI 0x33 // Auto power down after 1 * (0x33 + 1) * 10ms = 520ms without link
#define SLEEP_RXWAKE 0x10 // ARX wakes from internal timer
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 int. at 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 user interface option 3, if the ARX is first on, a link
// may be established. 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(); // Initialize ADC
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 3, the loop is terminated when ATX and ARX are both on and within
// range of one another, i.e. without user interaction
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 3 wait for both to be within range
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 3 we got to this stage without user interaction.
// The DAC is therefore powered down, and the Play button has to be pressed before the DAC is powered up and the ARX mute removed.
// 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 on ARX and ATX, the program got to this stage without user interaction. Therefore: power down
// all units until the Play button is pressed on either ARX or ATX. And power things up after that. First power down ARX DAC:
if (booterror == 0) { // If no error detected,
if (dac_sleep() != OKAY) { // Try to put DAC to sleep mode
booterror = 1; // If that failed, stop bootup code
}
}
// Use the sleep parameters in the EEPROM. Now only change RXMOD and TXMOD to bring the system into sleep mode.
// This spot may also feature code to reconfigure the sleep mode completely. In that case it needs control over RXCSTATE
#ifdef USELED
z1_arxled(LEDOFF); // Turn off ARX LED before going to sleep and removing link
ledsequence = LED_POWERDOWN; // Blink in a pattern indicating power down
#endif
powermode = PMODE_POWERDOWN; // nRF24Z1 is currently powered on
z1_singlewrite(RXMOD, z1_singleread(RXMOD) | 0x80); // ARX wakes on timer on a 1/10 duty cycle
z1_singlewrite(TXMOD, z1_singleread(TXMOD) | 0x40); // Enable ATX wake-on-timer
// 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 auto 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 3 //// ATX is wake-on-timer, ARX is wake-on-timer
// | powermode == | powermode == | powermode ==
// | PMODE_AUTODOWN | PMODE_POWERDOWN | PMODE_AWAKE
// | | |
// wakeup == | Check if system | ATX is momentarily | ATX went to auto
// WAKE_Z1_INT | was able to re- | awake, check for | power down
// | establish a | wakeup cmd. 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 3. 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 3, check if ATX was able to relink by ARX being in radio range
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!
adc_wake(); // Wake up ATX resources
dac_wake(); // Wake up ARX resources, may also test that logical return of dac_wake() indicated success
#ifdef USELED
ledsequence = LED_AWAKE; // Blink in a pattern indicating audio streaming
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -