aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/ports/raspberrypi/common-hal/rgbmatrix/RGBMatrix.c
blob: d580b4cc6abe1568a5bbbb83e011f0e4f42f3d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2020 Jeff Epler 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 <stddef.h>

#include "py/mphal.h"

#include "common-hal/rgbmatrix/RGBMatrix.h"
#include "shared-bindings/pwmio/PWMOut.h"
#include "shared-module/rgbmatrix/RGBMatrix.h"

#include "src/rp2_common/hardware_pwm/include/hardware/pwm.h"
#include "src/rp2_common/hardware_irq/include/hardware/irq.h"

void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self) {
    // Choose a PWM channel based on the first RGB pin
    uint8_t slice = pwm_gpio_to_slice_num(self->rgb_pins[0]);
    uint8_t channel = pwm_gpio_to_channel(self->rgb_pins[0]);
    int result = pwmout_allocate(slice, channel, true, 125000000 / 3);
    if (result == PWMOUT_OK) {
        // return value must be nonzero (but slice and channel can both be
        // zero), so set bit 16...
        pwmout_never_reset(slice, channel);
        return (void *)(intptr_t)(slice | (channel << 8) | 0x10000);
    }
    return NULL;
}

void common_hal_rgbmatrix_timer_enable(void *ptr) {
    int8_t slice = ((intptr_t)ptr) & 0xff;
    pwm_set_enabled(slice, false);
    pwm_clear_irq(slice);
    pwm_set_enabled(slice, true);
}

void common_hal_rgbmatrix_timer_disable(void *ptr) {
    int8_t slice = ((intptr_t)ptr) & 0xff;
    pwm_set_enabled(slice, false);
    irq_set_enabled(PWM_IRQ_WRAP, false);
    pwm_clear_irq(slice);
}

void common_hal_rgbmatrix_timer_free(void *ptr) {
    intptr_t value = (intptr_t)ptr;
    uint8_t slice = value & 0xff;
    uint8_t channel = value >> 8;
    pwm_set_enabled(slice, false);
    irq_set_enabled(PWM_IRQ_WRAP, false);
    pwm_clear_irq(slice);
    pwmout_free(slice, channel);
    return;
}