circuitpython/ports/nrf/supervisor/port.c
Jeff Epler a183425e00 ports/nrf: Implement audioio.AudioOut using PWM
This implements AudioOut, with known caveats:
 * pause/resume are not yet implemented (this is just a bug)
 * at best, the sample fidelity is 8 bits (this is a hardware limitation)

Testing performed:

My test system is a Particle Xenon with a PAM8302 op-amp
https://www.adafruit.com/product/2130 and 8-ohm speaker.  There's no
analog filtering between the Xenon's PWM pin and the "A+" input of
the amplifier; the "A-" pin is disconnected.  It is powered from
VUSB.

I used pin D4, which is *NOT* listed as a low-speed-only pin, but
the code does NOT switch the pin to high drive.  This is related to
an open issue for general inability to set drive level for pins
being used by a "special function" on nrf:
https://github.com/adafruit/circuitpython/issues/1270

Nothing about the code I've written should limit the usable pins.

All samples I played were 16-bit, generally monophonic at 11025Hz
and 22050Hz from the Debian LibreOffice package.
2019-07-26 07:57:11 -05:00

151 lines
3.8 KiB
C

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include "supervisor/port.h"
#include "boards/board.h"
#include "nrfx/hal/nrf_power.h"
#include "nrfx/drivers/include/nrfx_power.h"
#include "nrf/cache.h"
#include "nrf/clocks.h"
#include "nrf/power.h"
#include "nrf/timers.h"
#include "shared-module/gamepad/__init__.h"
#include "common-hal/microcontroller/Pin.h"
#include "common-hal/bleio/__init__.h"
#include "common-hal/busio/I2C.h"
#include "common-hal/busio/SPI.h"
#include "common-hal/busio/UART.h"
#include "common-hal/pulseio/PWMOut.h"
#include "common-hal/pulseio/PulseOut.h"
#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/rtc/RTC.h"
#include "tick.h"
#include "shared-bindings/rtc/__init__.h"
#ifdef CIRCUITPY_AUDIOPWMIO
#include "common-hal/audiopwmio/AudioOut.h"
#endif
static void power_warning_handler(void) {
reset_into_safe_mode(BROWNOUT);
}
safe_mode_t port_init(void) {
nrf_peripherals_clocks_init();
// If GPIO voltage is set wrong in UICR, this will fix it, and
// will also do a reset to make the change take effect.
nrf_peripherals_power_init();
nrfx_power_pofwarn_config_t power_failure_config;
power_failure_config.handler = power_warning_handler;
power_failure_config.thr = NRF_POWER_POFTHR_V27;
#if NRF_POWER_HAS_VDDH
power_failure_config.thrvddh = NRF_POWER_POFTHRVDDH_V27;
#endif
nrfx_power_pof_init(&power_failure_config);
nrfx_power_pof_enable(&power_failure_config);
nrf_peripherals_enable_cache();
// Configure millisecond timer initialization.
tick_init();
#if CIRCUITPY_RTC
rtc_init();
#endif
// Will do usb_init() if chip supports USB.
board_init();
return NO_SAFE_MODE;
}
void reset_port(void) {
#ifdef CIRCUITPY_GAMEPAD_TICKS
gamepad_reset();
#endif
i2c_reset();
spi_reset();
uart_reset();
#ifdef CIRCUITPY_AUDIOPWMIO
audiopwmout_reset();
#endif
#if CIRCUITPY_PULSEIO
pwmout_reset();
pulseout_reset();
pulsein_reset();
#endif
timers_reset();
#if CIRCUITPY_RTC
rtc_reset();
#endif
#if CIRCUITPY_BLEIO
bleio_reset();
#endif
reset_all_pins();
}
void reset_to_bootloader(void) {
enum { DFU_MAGIC_SERIAL = 0x4e };
NRF_POWER->GPREGRET = DFU_MAGIC_SERIAL;
reset_cpu();
}
void reset_cpu(void) {
NVIC_SystemReset();
}
extern uint32_t _ebss;
// Place the word to save just after our BSS section that gets blanked.
void port_set_saved_word(uint32_t value) {
_ebss = value;
}
uint32_t port_get_saved_word(void) {
return _ebss;
}
void HardFault_Handler(void) {
reset_into_safe_mode(HARD_CRASH);
while (true) {
asm("nop;");
}
}