Skip to content

Commit 1a22d8a

Browse files
authored
Merge pull request #2291 from hierophect/stm32-neopixel
STM32: Neopixel support
2 parents 41aa214 + a479732 commit 1a22d8a

File tree

6 files changed

+143
-8
lines changed

6 files changed

+143
-8
lines changed

ports/stm32f4/boards/feather_stm32f405_express/mpconfigboard.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
#define AUTORESET_DELAY_MS 500
3636
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000)
3737

38+
#define MICROPY_HW_NEOPIXEL (&pin_PC00)
39+
3840
// On-board flash
39-
#define SPI_FLASH_MOSI_PIN &pin_PB05
40-
#define SPI_FLASH_MISO_PIN &pin_PB04
41-
#define SPI_FLASH_SCK_PIN &pin_PB03
42-
#define SPI_FLASH_CS_PIN &pin_PA15
41+
#define SPI_FLASH_MOSI_PIN (&pin_PB05)
42+
#define SPI_FLASH_MISO_PIN (&pin_PB04)
43+
#define SPI_FLASH_SCK_PIN (&pin_PB03)
44+
#define SPI_FLASH_CS_PIN (&pin_PA15)
4345

4446
#define DEFAULT_I2C_BUS_SCL (&pin_PB06)
4547
#define DEFAULT_I2C_BUS_SDA (&pin_PB07)

ports/stm32f4/boards/feather_stm32f405_express/pins.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
2626
{ MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PB10) },
2727
{ MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PB11) },
2828

29+
{ MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_PC00) },
2930
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
3031
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) },
3132
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },

ports/stm32f4/common-hal/microcontroller/Pin.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@
2626
*/
2727

2828
#include "shared-bindings/microcontroller/Pin.h"
29+
#include "shared-bindings/digitalio/DigitalInOut.h"
30+
#include "supervisor/shared/rgb_led_status.h"
2931

3032
#include "py/mphal.h"
3133
#include "stm32f4/pins.h"
3234
#include "stm32f4xx_hal.h"
3335

36+
#ifdef MICROPY_HW_NEOPIXEL
37+
bool neopixel_in_use;
38+
#endif
39+
3440
#if MCU_PACKAGE == 144
3541
#define GPIO_PORT_COUNT 7
3642
GPIO_TypeDef * ports[GPIO_PORT_COUNT] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG};
@@ -53,20 +59,29 @@ void reset_all_pins(void) {
5359
for (uint8_t i = 0; i < GPIO_PORT_COUNT; i++) {
5460
HAL_GPIO_DeInit(ports[i], ~never_reset_pins[i]);
5561
}
62+
63+
#ifdef MICROPY_HW_NEOPIXEL
64+
neopixel_in_use = false;
65+
#endif
5666
}
5767

5868
// Mark pin as free and return it to a quiescent state.
5969
void reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
6070
if (pin_port == 0x0F) {
6171
return;
6272
}
63-
64-
// Clear claimed bit.
73+
// Clear claimed bit & reset
6574
claimed_pins[pin_port] &= ~(1<<pin_number);
66-
// Reset the pin
6775
HAL_GPIO_DeInit(ports[pin_port], 1<<pin_number);
68-
}
6976

77+
#ifdef MICROPY_HW_NEOPIXEL
78+
if (pin_port == MICROPY_HW_NEOPIXEL->port && pin_number == MICROPY_HW_NEOPIXEL->number) {
79+
neopixel_in_use = false;
80+
rgb_led_status_init();
81+
return;
82+
}
83+
#endif
84+
}
7085

7186
void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
7287
never_reset_pins[pin_port] |= 1<<pin_number;
@@ -83,13 +98,25 @@ void common_hal_reset_pin(const mcu_pin_obj_t* pin) {
8398
void claim_pin(const mcu_pin_obj_t* pin) {
8499
// Set bit in claimed_pins bitmask.
85100
claimed_pins[pin->port] |= 1<<pin->number;
101+
102+
#ifdef MICROPY_HW_NEOPIXEL
103+
if (pin == MICROPY_HW_NEOPIXEL) {
104+
neopixel_in_use = true;
105+
}
106+
#endif
86107
}
87108

88109
bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) {
89110
return !(claimed_pins[pin_port] & 1<<pin_number);
90111
}
91112

92113
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
114+
#ifdef MICROPY_HW_NEOPIXEL
115+
if (pin == MICROPY_HW_NEOPIXEL) {
116+
return !neopixel_in_use;
117+
}
118+
#endif
119+
93120
return pin_number_is_free(pin->port, pin->number);
94121
}
95122

ports/stm32f4/common-hal/microcontroller/Pin.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131

3232
#include "peripherals/stm32f4/pins.h"
3333

34+
#ifdef MICROPY_HW_NEOPIXEL
35+
extern bool neopixel_in_use;
36+
#endif
37+
#ifdef MICROPY_HW_APA102_MOSI
38+
extern bool apa102_sck_in_use;
39+
extern bool apa102_mosi_in_use;
40+
#endif
41+
3442
void reset_all_pins(void);
3543
// reset_pin_number takes the pin number instead of the pointer so that objects don't
3644
// need to store a full pointer.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Lucian Copeland 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 "py/mphal.h"
28+
#include "shared-bindings/neopixel_write/__init__.h"
29+
30+
#include "tick.h"
31+
#include "common-hal/microcontroller/Pin.h"
32+
#include "stm32f4xx_hal.h"
33+
#include "stm32f4xx_ll_gpio.h"
34+
35+
uint64_t next_start_tick_ms = 0;
36+
uint32_t next_start_tick_us = 1000;
37+
38+
//sysclock divisors
39+
#define MAGIC_800_INT 900000 // ~1.11 us -> 1.2 field
40+
#define MAGIC_800_T0H 2800000 // ~0.36 us -> 0.44 field
41+
#define MAGIC_800_T1H 1350000 // ~0.74 us -> 0.84 field
42+
43+
void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels,
44+
uint32_t numBytes) {
45+
uint8_t *p = pixels, *end = p + numBytes, pix = *p++, mask = 0x80;
46+
uint32_t start = 0;
47+
uint32_t cyc = 0;
48+
49+
//assumes 800_000Hz frequency
50+
//Theoretical values here are 800_000 -> 1.25us, 2500000->0.4us, 1250000->0.8us
51+
//But they don't work, possibly due to bad optimization? Use tested magic values instead
52+
uint32_t sys_freq = HAL_RCC_GetSysClockFreq();
53+
uint32_t interval = sys_freq/MAGIC_800_INT;
54+
uint32_t t0 = (sys_freq/MAGIC_800_T0H);
55+
uint32_t t1 = (sys_freq/MAGIC_800_T1H);
56+
57+
// This must be called while interrupts are on in case we're waiting for a
58+
// future ms tick.
59+
wait_until(next_start_tick_ms, next_start_tick_us);
60+
61+
GPIO_TypeDef * p_port = pin_port(digitalinout->pin->port);
62+
uint32_t p_mask = pin_mask(digitalinout->pin->number);
63+
64+
__disable_irq();
65+
// Enable DWT in debug core. Useable when interrupts disabled, as opposed to Systick->VAL
66+
//ITM->LAR = 0xC5ACCE55; //this should be required but isn't
67+
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
68+
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
69+
DWT->CYCCNT = 0;
70+
71+
for(;;) {
72+
start = DWT->CYCCNT;
73+
LL_GPIO_SetOutputPin(p_port, p_mask);
74+
cyc = (pix & mask) ? t1 : t0;
75+
while(DWT->CYCCNT - start < cyc);
76+
LL_GPIO_ResetOutputPin(p_port, p_mask);
77+
if(!(mask >>= 1)) {
78+
if(p >= end) break;
79+
pix = *p++;
80+
mask = 0x80;
81+
}
82+
while(DWT->CYCCNT - start < interval); //wait for interval to finish
83+
}
84+
85+
// Enable interrupts again
86+
__enable_irq();
87+
88+
// Update the next start.
89+
current_tick(&next_start_tick_ms, &next_start_tick_us);
90+
if (next_start_tick_us < 100) {
91+
next_start_tick_ms += 1;
92+
next_start_tick_us = 100 - next_start_tick_us;
93+
} else {
94+
next_start_tick_us -= 100;
95+
}
96+
}

ports/stm32f4/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ CIRCUITPY_STORAGE = 1
2626
CIRCUITPY_RANDOM = 1
2727
CIRCUITPY_USB_HID = 1
2828
CIRCUITPY_USB_MIDI = 1
29+
CIRCUITPY_NEOPIXEL_WRITE = 1
2930

3031
#ifeq ($(MCU_SUB_VARIANT), stm32f412zx)
3132
#endif

0 commit comments

Comments
 (0)