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

Skip to content

Commit ad6e8ea

Browse files
authored
Merge pull request adafruit#2548 from dhalbert/cpb-reset-neopixels
reset NeoPixels on soft reload on CPB, pybadge, and pygamer boards
2 parents 1831f51 + 6f13979 commit ad6e8ea

File tree

20 files changed

+133
-36
lines changed

20 files changed

+133
-36
lines changed

ports/atmel-samd/boards/circuitplayground_express/board.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828

2929
#include "boards/board.h"
3030
#include "common-hal/microcontroller/Pin.h"
31+
#include "supervisor/shared/board.h"
3132
#include "hal/include/hal_gpio.h"
32-
#include "shared-bindings/digitalio/DigitalInOut.h"
33-
#include "shared-bindings/neopixel_write/__init__.h"
3433

3534
void board_init(void)
3635
{
@@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
5453
}
5554

5655
void reset_board(void) {
57-
uint8_t empty[30];
58-
memset(empty, 0, 30);
59-
digitalio_digitalinout_obj_t neopixel_pin;
60-
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
61-
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
62-
DRIVE_MODE_PUSH_PULL);
63-
common_hal_neopixel_write(&neopixel_pin, empty, 30);
64-
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
56+
board_reset_user_neopixels();
6557
}

ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
// Increase stack size slightly due to CPX library import nesting
3131
#define CIRCUITPY_DEFAULT_STACK_SIZE (4760) //divisible by 8
3232

33+
#define USER_NEOPIXELS_PIN (&pin_PB23)
34+
3335
#define DEFAULT_I2C_BUS_SCL (&pin_PB03)
3436
#define DEFAULT_I2C_BUS_SDA (&pin_PB02)
3537

ports/atmel-samd/boards/circuitplayground_express_crickit/board.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#include "boards/board.h"
3030
#include "common-hal/microcontroller/Pin.h"
3131
#include "hal/include/hal_gpio.h"
32-
#include "shared-bindings/digitalio/DigitalInOut.h"
33-
#include "shared-bindings/neopixel_write/__init__.h"
32+
#include "supervisor/shared/board.h"
3433

3534
void board_init(void)
3635
{
@@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
5453
}
5554

5655
void reset_board(void) {
57-
uint8_t empty[30];
58-
memset(empty, 0, 30);
59-
digitalio_digitalinout_obj_t neopixel_pin;
60-
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
61-
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
62-
DRIVE_MODE_PUSH_PULL);
63-
common_hal_neopixel_write(&neopixel_pin, empty, 30);
64-
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
56+
board_reset_user_neopixels();
6557
}

ports/atmel-samd/boards/circuitplayground_express_crickit/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#define CALIBRATE_CRYSTALLESS 1
2626

27+
#define USER_NEOPIXELS_PIN (&pin_PB23)
28+
2729
// Explanation of how a user got into safe mode.
2830
#define BOARD_USER_SAFE_MODE_ACTION "pressing both buttons at start up"
2931

ports/atmel-samd/boards/circuitplayground_express_displayio/board.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#include "boards/board.h"
3030
#include "common-hal/microcontroller/Pin.h"
3131
#include "hal/include/hal_gpio.h"
32-
#include "shared-bindings/digitalio/DigitalInOut.h"
33-
#include "shared-bindings/neopixel_write/__init__.h"
32+
#include "supervisor/shared/board.h"
3433

3534
void board_init(void)
3635
{
@@ -54,12 +53,5 @@ bool board_requests_safe_mode(void) {
5453
}
5554

5655
void reset_board(void) {
57-
uint8_t empty[30];
58-
memset(empty, 0, 30);
59-
digitalio_digitalinout_obj_t neopixel_pin;
60-
common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23);
61-
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
62-
DRIVE_MODE_PUSH_PULL);
63-
common_hal_neopixel_write(&neopixel_pin, empty, 30);
64-
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
56+
board_reset_user_neopixels();
6557
}

ports/atmel-samd/boards/circuitplayground_express_displayio/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
// Increase stack size slightly due to CPX library import nesting.
3131
#define CIRCUITPY_DEFAULT_STACK_SIZE (4760) // divisible by 8
3232

33+
#define USER_NEOPIXELS_PIN (&pin_PB23)
34+
3335
#define DEFAULT_I2C_BUS_SCL (&pin_PB03)
3436
#define DEFAULT_I2C_BUS_SDA (&pin_PB02)
3537

ports/atmel-samd/boards/pybadge/board.c

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "shared-bindings/displayio/FourWire.h"
3232
#include "shared-module/displayio/__init__.h"
3333
#include "shared-module/displayio/mipi_constants.h"
34+
#include "supervisor/shared/board.h"
3435
#include "tick.h"
3536

3637
displayio_fourwire_obj_t board_display_obj;
@@ -118,4 +119,5 @@ bool board_requests_safe_mode(void) {
118119
}
119120

120121
void reset_board(void) {
122+
board_reset_user_neopixels();
121123
}

ports/atmel-samd/boards/pybadge/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define MICROPY_PORT_C (0)
1515
#define MICROPY_PORT_D (0)
1616

17+
#define USER_NEOPIXELS_PIN (&pin_PA15)
18+
1719
#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
1820
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)
1921

ports/atmel-samd/boards/pybadge_airlift/board.c

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "shared-bindings/displayio/FourWire.h"
3232
#include "shared-module/displayio/__init__.h"
3333
#include "shared-module/displayio/mipi_constants.h"
34+
#include "supervisor/shared/board.h"
3435
#include "tick.h"
3536

3637
displayio_fourwire_obj_t board_display_obj;
@@ -96,4 +97,5 @@ bool board_requests_safe_mode(void) {
9697
}
9798

9899
void reset_board(void) {
100+
board_reset_user_neopixels();
99101
}

ports/atmel-samd/boards/pybadge_airlift/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define MICROPY_PORT_C (0)
1515
#define MICROPY_PORT_D (0)
1616

17+
#define USER_NEOPIXELS_PIN (&pin_PA15)
18+
1719
#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
1820
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)
1921

ports/atmel-samd/boards/pygamer/board.c

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "shared-bindings/displayio/FourWire.h"
3232
#include "shared-module/displayio/__init__.h"
3333
#include "shared-module/displayio/mipi_constants.h"
34+
#include "supervisor/shared/board.h"
3435
#include "tick.h"
3536

3637
displayio_fourwire_obj_t board_display_obj;
@@ -118,4 +119,5 @@ bool board_requests_safe_mode(void) {
118119
}
119120

120121
void reset_board(void) {
122+
board_reset_user_neopixels();
121123
}

ports/atmel-samd/boards/pygamer/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#define MICROPY_PORT_C (0)
1414
#define MICROPY_PORT_D (0)
1515

16+
#define USER_NEOPIXELS_PIN (&pin_PA15)
17+
1618
#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
1719
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)
1820

ports/atmel-samd/boards/pygamer_advance/board.c

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "shared-bindings/displayio/FourWire.h"
3232
#include "shared-module/displayio/__init__.h"
3333
#include "shared-module/displayio/mipi_constants.h"
34+
#include "supervisor/shared/board.h"
3435
#include "tick.h"
3536

3637
displayio_fourwire_obj_t board_display_obj;
@@ -96,4 +97,5 @@ bool board_requests_safe_mode(void) {
9697
}
9798

9899
void reset_board(void) {
100+
board_reset_user_neopixels();
99101
}

ports/atmel-samd/boards/pygamer_advance/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#define MICROPY_PORT_C (0)
1414
#define MICROPY_PORT_D (0)
1515

16+
#define USER_NEOPIXELS_PIN (&pin_PA15)
17+
1618
#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
1719
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)
1820

ports/nrf/boards/circuitplayground_bluefruit/board.c

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "mpconfigboard.h"
2929
#include "py/obj.h"
3030
#include "peripherals/nrf/pins.h"
31+
#include "supervisor/shared/board.h"
3132

3233
#include "nrf_gpio.h"
3334

@@ -47,4 +48,6 @@ void reset_board(void) {
4748
NRF_GPIO_PIN_S0S1,
4849
NRF_GPIO_PIN_NOSENSE);
4950
nrf_gpio_pin_write(POWER_SWITCH_PIN->number, false);
51+
52+
board_reset_user_neopixels();
5053
}

ports/nrf/boards/circuitplayground_bluefruit/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
// Disables onboard peripherals and neopixels to save power.
5555
#define POWER_SWITCH_PIN (&pin_P0_06)
5656

57+
#define USER_NEOPIXELS_PIN (&pin_P0_13)
58+
5759
#define DEFAULT_I2C_BUS_SCL (&pin_P0_04)
5860
#define DEFAULT_I2C_BUS_SDA (&pin_P0_05)
5961

ports/nrf/common-hal/neopixel_write/__init__.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ uint32_t next_start_tick_us = 1000;
102102

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

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

117123
uint32_t pattern_size = PATTERN_SIZE(numBytes);
118124
uint16_t* pixels_pattern = NULL;
119125
bool pattern_on_heap = false;
120126

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

126132
NRF_PWM_Type* pwm = find_free_pwm();
127133

128134
// only malloc if there is PWM device available
129135
if ( pwm != NULL ) {
130-
if (pattern_size <= sizeof(one_pixel)) {
131-
pixels_pattern = (uint16_t *) one_pixel;
136+
if (pattern_size <= sizeof(stack_pixels)) {
137+
pixels_pattern = (uint16_t *) stack_pixels;
132138
} else {
133139
uint8_t sd_en = 0;
134140
(void) sd_softdevice_is_enabled(&sd_en);

supervisor/shared/board.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "supervisor/shared/board.h"
28+
29+
#include "shared-bindings/digitalio/DigitalInOut.h"
30+
#include "shared-bindings/neopixel_write/__init__.h"
31+
32+
#ifdef USER_NEOPIXELS_PIN
33+
34+
// The maximum number of user neopixels right now is 10, on Circuit Playgrounds.
35+
// PyBadge and PyGamer have max 5
36+
#define USER_NEOPIXELS_MAX_COUNT 10
37+
38+
void board_reset_user_neopixels(void) {
39+
// Turn off on-board NeoPixel string
40+
uint8_t empty[USER_NEOPIXELS_MAX_COUNT * 3] = { 0 };
41+
digitalio_digitalinout_obj_t neopixel_pin;
42+
common_hal_digitalio_digitalinout_construct(&neopixel_pin, USER_NEOPIXELS_PIN);
43+
common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false,
44+
DRIVE_MODE_PUSH_PULL);
45+
common_hal_neopixel_write(&neopixel_pin, empty, USER_NEOPIXELS_MAX_COUNT * 3);
46+
common_hal_digitalio_digitalinout_deinit(&neopixel_pin);
47+
}
48+
49+
#endif

supervisor/shared/board.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SUPERVISOR_BOARD_H
28+
#define MICROPY_INCLUDED_SUPERVISOR_BOARD_H
29+
30+
#include "py/mpconfig.h"
31+
32+
#ifdef USER_NEOPIXELS_PIN
33+
34+
void board_reset_user_neopixels(void);
35+
36+
#endif
37+
38+
#endif // MICROPY_INCLUDED_SUPERVISOR_BOARD_H

supervisor/supervisor.mk

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SRC_SUPERVISOR = \
22
main.c \
33
supervisor/port.c \
44
supervisor/shared/autoreload.c \
5+
supervisor/shared/board.c \
56
supervisor/shared/display.c \
67
supervisor/shared/filesystem.c \
78
supervisor/shared/flash.c \

0 commit comments

Comments
 (0)