Thanks to visit codestin.com
Credit goes to github.com

Skip to content

reset NeoPixels on soft reload on CPB, pybadge, and pygamer boards #2548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions ports/atmel-samd/boards/circuitplayground_express/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@

#include "boards/board.h"
#include "common-hal/microcontroller/Pin.h"
#include "supervisor/shared/board.h"
#include "hal/include/hal_gpio.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/neopixel_write/__init__.h"

void board_init(void)
{
Expand All @@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
}

void reset_board(void) {
uint8_t empty[30];
memset(empty, 0, 30);
digitalio_digitalinout_obj_t neopixel_pin;
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
DRIVE_MODE_PUSH_PULL);
common_hal_neopixel_write(&neopixel_pin, empty, 30);
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
board_reset_user_neopixels();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
// Increase stack size slightly due to CPX library import nesting
#define CIRCUITPY_DEFAULT_STACK_SIZE (4760) //divisible by 8

#define USER_NEOPIXELS_PIN (&pin_PB23)

#define DEFAULT_I2C_BUS_SCL (&pin_PB03)
#define DEFAULT_I2C_BUS_SDA (&pin_PB02)

Expand Down
12 changes: 2 additions & 10 deletions ports/atmel-samd/boards/circuitplayground_express_crickit/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
#include "boards/board.h"
#include "common-hal/microcontroller/Pin.h"
#include "hal/include/hal_gpio.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/neopixel_write/__init__.h"
#include "supervisor/shared/board.h"

void board_init(void)
{
Expand All @@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
}

void reset_board(void) {
uint8_t empty[30];
memset(empty, 0, 30);
digitalio_digitalinout_obj_t neopixel_pin;
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
DRIVE_MODE_PUSH_PULL);
common_hal_neopixel_write(&neopixel_pin, empty, 30);
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
board_reset_user_neopixels();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#define CALIBRATE_CRYSTALLESS 1

#define USER_NEOPIXELS_PIN (&pin_PB23)

// Explanation of how a user got into safe mode.
#define BOARD_USER_SAFE_MODE_ACTION "pressing both buttons at start up"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
#include "boards/board.h"
#include "common-hal/microcontroller/Pin.h"
#include "hal/include/hal_gpio.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/neopixel_write/__init__.h"
#include "supervisor/shared/board.h"

void board_init(void)
{
Expand All @@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
}

void reset_board(void) {
uint8_t empty[30];
memset(empty, 0, 30);
digitalio_digitalinout_obj_t neopixel_pin;
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
DRIVE_MODE_PUSH_PULL);
common_hal_neopixel_write(&neopixel_pin, empty, 30);
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
board_reset_user_neopixels();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
// Increase stack size slightly due to CPX library import nesting.
#define CIRCUITPY_DEFAULT_STACK_SIZE (4760) // divisible by 8

#define USER_NEOPIXELS_PIN (&pin_PB23)

#define DEFAULT_I2C_BUS_SCL (&pin_PB03)
#define DEFAULT_I2C_BUS_SDA (&pin_PB02)

Expand Down
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pybadge/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "shared-bindings/displayio/FourWire.h"
#include "shared-module/displayio/__init__.h"
#include "shared-module/displayio/mipi_constants.h"
#include "supervisor/shared/board.h"
#include "tick.h"

displayio_fourwire_obj_t board_display_obj;
Expand Down Expand Up @@ -118,4 +119,5 @@ bool board_requests_safe_mode(void) {
}

void reset_board(void) {
board_reset_user_neopixels();
}
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pybadge/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define MICROPY_PORT_C (0)
#define MICROPY_PORT_D (0)

#define USER_NEOPIXELS_PIN (&pin_PA15)

#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)

Expand Down
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pybadge_airlift/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "shared-bindings/displayio/FourWire.h"
#include "shared-module/displayio/__init__.h"
#include "shared-module/displayio/mipi_constants.h"
#include "supervisor/shared/board.h"
#include "tick.h"

displayio_fourwire_obj_t board_display_obj;
Expand Down Expand Up @@ -96,4 +97,5 @@ bool board_requests_safe_mode(void) {
}

void reset_board(void) {
board_reset_user_neopixels();
}
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pybadge_airlift/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define MICROPY_PORT_C (0)
#define MICROPY_PORT_D (0)

#define USER_NEOPIXELS_PIN (&pin_PA15)

#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)

Expand Down
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pygamer/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "shared-bindings/displayio/FourWire.h"
#include "shared-module/displayio/__init__.h"
#include "shared-module/displayio/mipi_constants.h"
#include "supervisor/shared/board.h"
#include "tick.h"

displayio_fourwire_obj_t board_display_obj;
Expand Down Expand Up @@ -118,4 +119,5 @@ bool board_requests_safe_mode(void) {
}

void reset_board(void) {
board_reset_user_neopixels();
}
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pygamer/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define MICROPY_PORT_C (0)
#define MICROPY_PORT_D (0)

#define USER_NEOPIXELS_PIN (&pin_PA15)

#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)

Expand Down
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pygamer_advance/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "shared-bindings/displayio/FourWire.h"
#include "shared-module/displayio/__init__.h"
#include "shared-module/displayio/mipi_constants.h"
#include "supervisor/shared/board.h"
#include "tick.h"

displayio_fourwire_obj_t board_display_obj;
Expand Down Expand Up @@ -96,4 +97,5 @@ bool board_requests_safe_mode(void) {
}

void reset_board(void) {
board_reset_user_neopixels();
}
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/pygamer_advance/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define MICROPY_PORT_C (0)
#define MICROPY_PORT_D (0)

#define USER_NEOPIXELS_PIN (&pin_PA15)

#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)

Expand Down
3 changes: 3 additions & 0 deletions ports/nrf/boards/circuitplayground_bluefruit/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "mpconfigboard.h"
#include "py/obj.h"
#include "peripherals/nrf/pins.h"
#include "supervisor/shared/board.h"

#include "nrf_gpio.h"

Expand All @@ -47,4 +48,6 @@ void reset_board(void) {
NRF_GPIO_PIN_S0S1,
NRF_GPIO_PIN_NOSENSE);
nrf_gpio_pin_write(POWER_SWITCH_PIN->number, false);

board_reset_user_neopixels();
}
2 changes: 2 additions & 0 deletions ports/nrf/boards/circuitplayground_bluefruit/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
// Disables onboard peripherals and neopixels to save power.
#define POWER_SWITCH_PIN (&pin_P0_06)

#define USER_NEOPIXELS_PIN (&pin_P0_13)

#define DEFAULT_I2C_BUS_SCL (&pin_P0_04)
#define DEFAULT_I2C_BUS_SDA (&pin_P0_05)

Expand Down
18 changes: 12 additions & 6 deletions ports/nrf/common-hal/neopixel_write/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ uint32_t next_start_tick_us = 1000;

void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
// To support both the SoftDevice + Neopixels we use the EasyDMA
// feature from the NRF25. However this technique implies to
// feature from the NRF52. However this technique implies to
// generate a pattern and store it on the memory. The actual
// memory used in bytes corresponds to the following formula:
// totalMem = numBytes*8*2+(2*2)
Expand All @@ -113,22 +113,28 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
// using DWT

#define PATTERN_SIZE(numBytes) (numBytes * 8 * sizeof(uint16_t) + 2 * sizeof(uint16_t))
// Allocate PWM space for up to STACK_PIXELS on the stack, to avoid malloc'ing.
// We may need to write to the status neopixel or to Circuit Playground NeoPixels
// when we cannot malloc, between VM instantiations.
// We need space for at least 10 pixels for Circuit Playground, but let's choose 24
// to handle larger NeoPixel rings without malloc'ing.
#define STACK_PIXELS 24

uint32_t pattern_size = PATTERN_SIZE(numBytes);
uint16_t* pixels_pattern = NULL;
bool pattern_on_heap = false;

// Use the stack to store 1 pixels worth of PWM data for the status led. uint32_t to ensure alignment.
// Make it at least as big as PATTERN_SIZE(3), for one pixel of RGB data.
// Use the stack to store STACK_PIXEL's worth of PWM data. uint32_t to ensure alignment.
// It is 3*STACK_PIXELS to handle RGB.
// PATTERN_SIZE is a multiple of 4, so we don't need round up to make sure one_pixel is large enough.
uint32_t one_pixel[PATTERN_SIZE(3)/sizeof(uint32_t)];
uint32_t stack_pixels[PATTERN_SIZE(3 * STACK_PIXELS) / sizeof(uint32_t)];

NRF_PWM_Type* pwm = find_free_pwm();

// only malloc if there is PWM device available
if ( pwm != NULL ) {
if (pattern_size <= sizeof(one_pixel)) {
pixels_pattern = (uint16_t *) one_pixel;
if (pattern_size <= sizeof(stack_pixels)) {
pixels_pattern = (uint16_t *) stack_pixels;
} else {
uint8_t sd_en = 0;
(void) sd_softdevice_is_enabled(&sd_en);
Expand Down
49 changes: 49 additions & 0 deletions supervisor/shared/board.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Dan Halbert 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 "supervisor/shared/board.h"

#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/neopixel_write/__init__.h"

#ifdef USER_NEOPIXELS_PIN

// The maximum number of user neopixels right now is 10, on Circuit Playgrounds.
// PyBadge and PyGamer have max 5
#define USER_NEOPIXELS_MAX_COUNT 10

void board_reset_user_neopixels(void) {
// Turn off on-board NeoPixel string
uint8_t empty[USER_NEOPIXELS_MAX_COUNT * 3] = { 0 };
digitalio_digitalinout_obj_t neopixel_pin;
common_hal_digitalio_digitalinout_construct(&neopixel_pin, USER_NEOPIXELS_PIN);
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
DRIVE_MODE_PUSH_PULL);
common_hal_neopixel_write(&neopixel_pin, empty, USER_NEOPIXELS_MAX_COUNT * 3);
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
}

#endif
38 changes: 38 additions & 0 deletions supervisor/shared/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Dan Halbert 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.
*/

#ifndef MICROPY_INCLUDED_SUPERVISOR_BOARD_H
#define MICROPY_INCLUDED_SUPERVISOR_BOARD_H

#include "py/mpconfig.h"

#ifdef USER_NEOPIXELS_PIN

void board_reset_user_neopixels(void);

#endif

#endif // MICROPY_INCLUDED_SUPERVISOR_BOARD_H
1 change: 1 addition & 0 deletions supervisor/supervisor.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SRC_SUPERVISOR = \
main.c \
supervisor/port.c \
supervisor/shared/autoreload.c \
supervisor/shared/board.c \
supervisor/shared/display.c \
supervisor/shared/filesystem.c \
supervisor/shared/flash.c \
Expand Down