|
| 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 | +} |
0 commit comments