nrf5/drivers/pwm: Work in progress commit of soft PWM from microbit repo.

This commit is contained in:
Glenn Ruben Bakke 2017-05-02 01:07:52 +02:00
parent 10e6b4309b
commit 40b37227a0
1 changed files with 17 additions and 9 deletions

View File

@ -25,11 +25,19 @@
*/
#include "stddef.h"
#include "lib/ticker.h"
#include "nrf_gpio.h"
#include "py/runtime.h"
#include "py/gc.h"
#include "PinNames.h"
#include "hal_timer.h"
#include "hal_gpio.h"
#include "pin.h"
#define CYCLES_PER_MICROSECONDS 16
#define MICROSECONDS_PER_TICK 16
#define CYCLES_PER_TICK (CYCLES_PER_MICROSECONDS*MICROSECONDS_PER_TICK)
// This must be an integer multiple of MICROSECONDS_PER_TICK
#define MICROSECONDS_PER_MACRO_TICK 6000
#define MILLISECONDS_PER_MACRO_TICK 6
#define PWM_TICKER_INDEX 2
@ -140,10 +148,10 @@ int32_t pwm_callback(void) {
int32_t tnow = (event->time*events->period)>>10;
do {
if (event->turn_on) {
nrf_gpio_pin_set(event->pin);
hal_gpio_pin_set(0, event->pin);
next_event++;
} else {
nrf_gpio_pins_clear(events->all_pins);
hal_gpio_out_clear(0, events->all_pins);
next_event = 0;
tnow = 0;
if (pending_events) {
@ -159,11 +167,11 @@ int32_t pwm_callback(void) {
}
void pwm_start(void) {
set_ticker_callback(PWM_TICKER_INDEX, pwm_callback, 120);
// set_ticker_callback(PWM_TICKER_INDEX, pwm_callback, 120);
}
void pwm_stop(void) {
clear_ticker_callback(PWM_TICKER_INDEX);
// clear_ticker_callback(PWM_TICKER_INDEX);
}
static void pwm_set_period_ticks(int32_t ticks) {
@ -200,7 +208,7 @@ void pwm_set_duty_cycle(int32_t pin, uint32_t value) {
old_events = active_events;
}
if (((1<<pin)&old_events->all_pins) == 0) {
nrf_gpio_cfg_output(pin);
hal_gpio_cfg_pin(0, pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DISABLED);
}
int ev = find_pin_in_events(old_events, pin);
pwm_events *events;
@ -237,5 +245,5 @@ void pwm_release(int32_t pin) {
return;
// If i >= 0 it means that `ev` is in RAM, so it safe to discard the const qualifier
((pwm_events *)ev)->events[i].pin = 31;
nrf_gpio_pin_clear(pin);
hal_gpio_pin_clear(0, pin);
}