📄 main_option1.c
字号:
/*= main_option1.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 1
* with soft power on only at ATX
*
* 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 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=0x01 RXLTI=0x31
// TXLTI=0x31 RXSTI=0x0030
// TXSTI=0x0000 RXWAKE=0x10, Wakeup on sleep timer
// TXMOD=0xE2, RF, I2S, TX pwr down, 44.1, wake@DD I2SCNF_OUT=0x40, Mute sound
// RXMOD=0x20, RF, I2S
#define SLEEP_TXWTI 0x01 // ATX may go to auto power down
#define SLEEP_TXSTI_0 0x00 // ATX sleep timer is not used, ATX is wake-on-interrupt
#define SLEEP_TXSTI_1 0x00
#define SLEEP_TXLTI 0x31 // ATX powers down after 0x01 * (0x31 + 1) * 10ms = 500ms without link
#define SLEEP_RXWTI 0x01 // ARX is awake for 0x01 * 10ms = 10ms, ARX may go to auto power down
#define SLEEP_RXSTI_0 0x30 // ARX is asleep for 0x30 * 10ms = 480ms
#define SLEEP_RXSTI_1 0x00
#define SLEEP_RXLTI 0x31 // Auto power down after 1 * (0x31 + 1) * 10ms = 500ms without link
#define SLEEP_RXWAKE 0x10 // 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
// 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(); // Headphone Reference Design ADC needs init routine
adc_sleep(); // ADC should not consume power while trying to establish link
// ATX sleeps with wake-on-interrupt. Poll ATX user interface for Play button, use that to interrupt=wake up ATX nRF24Z1
// ATX was set in EEPROM to go straight to sleep mode. The sleep enable bit in RXMOD therefore has to be reset before
// attempts are made to establish a link. But ARX was set up in EEPROM to go to auto power down. There is therefore
// no need to reset its sleep enable bit in RXMOD.
while (booterror == 0) {
#ifdef USELED
mcu_atxled(z1_rotate_led()); // Left-rotate the led sequence and apply ATX light according to MSB
#endif
buttons = mcu_buttons(); // Read buttons on local MCU GPIO pins
if (buttons == BTN_PLAY) { // If Play button was pressed, wake up system by:
mcu_z1wakeup_pin(); // Toggle the pin that wakes up ATX nRF24Z1 from wake-on-interrupt
z1_singlewrite(TXWTI, 0x00); // Disable auto power down, ARX went to auto power down, no need to toggle RXMOD
z1_singlewrite(TXMOD, z1_singleread(TXMOD) & ~0x40); // Wake up ATX, keep value of wakeup from DD[1] bit set
if (z1_haslink_wait()) { // Did linkup succeede after multiple pollings?
booterror = 1; // If it did, terminate the link searching loop, this does not indicate bootup error!
}
else { // If it didn't,
z1_singlewrite(TXWTI, SLEEP_TXWTI); // Reset TXWTI, ATX will go back to sleep again
z1_singlewrite(TXMOD, z1_singleread(TXMOD) | 0x40); // Put ATX back to sleep again
}
}
if (booterror == 0) // Poll again if: 1) no button was pressed; or if 2) Play was pressed with no link resulting
mcu_wait_ms(160); // Wait and poll button again
}
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 optio 1, 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -