|
| 1 | +// This file is not compiled on its own but needs to be included |
| 2 | +// by a stm32 chip specific file |
| 3 | + |
| 4 | +#include <mios/pwg.h> |
| 5 | + |
| 6 | +#include <malloc.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <stdio.h> |
| 9 | + |
| 10 | +#include "cache.h" |
| 11 | + |
| 12 | +#define PWG_BUF_SIZE 64 |
| 13 | +#define PWG_HALF_SIZE (PWG_BUF_SIZE / 2) |
| 14 | + |
| 15 | +typedef struct stm32_pwg { |
| 16 | + |
| 17 | + // DMA buffer must be cache-line aligned and not share cache lines |
| 18 | + // with other fields, since we use dcache_op(DCACHE_CLEAN) on it. |
| 19 | + uint32_t buf[PWG_BUF_SIZE]; |
| 20 | + |
| 21 | + uint32_t timer_base; |
| 22 | + uint32_t gpio_bsrr_addr; |
| 23 | + stm32_dma_instance_t dma; |
| 24 | + task_waitable_t waitq; |
| 25 | + pwg_fill_cb fill_cb; |
| 26 | + void *opaque; |
| 27 | + uint8_t active_half; |
| 28 | + uint8_t running; |
| 29 | + |
| 30 | +} stm32_pwg_t; |
| 31 | + |
| 32 | + |
| 33 | +static void |
| 34 | +pwg_dma_cb(stm32_dma_instance_t inst, uint32_t status, void *arg) |
| 35 | +{ |
| 36 | + stm32_pwg_t *pwg = arg; |
| 37 | + |
| 38 | + if(status & DMA_STATUS_HALF_XFER) |
| 39 | + pwg->active_half = 1; |
| 40 | + |
| 41 | + if(status & DMA_STATUS_FULL_XFER) |
| 42 | + pwg->active_half = 0; |
| 43 | + |
| 44 | + task_wakeup_sched_locked(&pwg->waitq, 0); |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +static void * |
| 49 | +pwg_thread(void *arg) |
| 50 | +{ |
| 51 | + stm32_pwg_t *pwg = arg; |
| 52 | + |
| 53 | + pwg->fill_cb(pwg->opaque, &pwg->buf[0], PWG_HALF_SIZE); |
| 54 | + pwg->fill_cb(pwg->opaque, &pwg->buf[PWG_HALF_SIZE], PWG_HALF_SIZE); |
| 55 | + dcache_op(pwg->buf, sizeof(pwg->buf), DCACHE_CLEAN); |
| 56 | + |
| 57 | + stm32_dma_start(pwg->dma); |
| 58 | + reg_wr(pwg->timer_base + TIMx_CR1, 1); |
| 59 | + |
| 60 | + while(pwg->running) { |
| 61 | + |
| 62 | + int q = irq_forbid(IRQ_LEVEL_SCHED); |
| 63 | + task_sleep_sched_locked(&pwg->waitq); |
| 64 | + uint8_t active = pwg->active_half; |
| 65 | + irq_permit(q); |
| 66 | + |
| 67 | + uint32_t *half = active ? |
| 68 | + &pwg->buf[0] : &pwg->buf[PWG_HALF_SIZE]; |
| 69 | + pwg->fill_cb(pwg->opaque, half, PWG_HALF_SIZE); |
| 70 | + dcache_op(half, PWG_HALF_SIZE * sizeof(uint32_t), DCACHE_CLEAN); |
| 71 | + } |
| 72 | + |
| 73 | + reg_wr(pwg->timer_base + TIMx_CR1, 0); |
| 74 | + stm32_dma_stop(pwg->dma); |
| 75 | + return NULL; |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +static stm32_pwg_t * |
| 80 | +stm32_pwg_init(uint32_t timer_base, |
| 81 | + uint16_t timer_clkid, |
| 82 | + uint32_t dma_resource_id, |
| 83 | + uint32_t gpio_bsrr_addr, |
| 84 | + uint32_t frequency, |
| 85 | + pwg_fill_cb fill_cb, |
| 86 | + void *opaque) |
| 87 | +{ |
| 88 | + stm32_pwg_t *pwg = xalloc(sizeof(stm32_pwg_t), CACHE_LINE_SIZE, |
| 89 | + MEM_TYPE_DMA | MEM_CLEAR); |
| 90 | + |
| 91 | + pwg->timer_base = timer_base; |
| 92 | + pwg->gpio_bsrr_addr = gpio_bsrr_addr; |
| 93 | + pwg->fill_cb = fill_cb; |
| 94 | + pwg->opaque = opaque; |
| 95 | + pwg->running = 1; |
| 96 | + |
| 97 | + task_waitable_init(&pwg->waitq, "pwg"); |
| 98 | + |
| 99 | + // Configure timer |
| 100 | + |
| 101 | + clk_enable(timer_clkid); |
| 102 | + |
| 103 | + uint32_t tclk = clk_get_freq(timer_clkid); |
| 104 | + uint32_t div = tclk / frequency; |
| 105 | + uint32_t psc = 0; |
| 106 | + |
| 107 | + while(div > 65536) { |
| 108 | + psc++; |
| 109 | + div = tclk / (frequency * (psc + 1)); |
| 110 | + } |
| 111 | + |
| 112 | + reg_wr(timer_base + TIMx_PSC, psc); |
| 113 | + reg_wr(timer_base + TIMx_ARR, div - 1); |
| 114 | + reg_wr(timer_base + TIMx_EGR, 1); // Generate update to load PSC/ARR |
| 115 | + reg_wr(timer_base + TIMx_DIER, 1 << 8); // UDE - Update DMA Enable |
| 116 | + |
| 117 | + // Configure DMA |
| 118 | + |
| 119 | + pwg->dma = stm32_dma_alloc(dma_resource_id, "pwg"); |
| 120 | + |
| 121 | + stm32_dma_config(pwg->dma, |
| 122 | + STM32_DMA_BURST_NONE, |
| 123 | + STM32_DMA_BURST_NONE, |
| 124 | + STM32_DMA_PRIO_HIGH, |
| 125 | + STM32_DMA_32BIT, |
| 126 | + STM32_DMA_32BIT, |
| 127 | + STM32_DMA_INCREMENT, |
| 128 | + STM32_DMA_FIXED, |
| 129 | + STM32_DMA_CIRCULAR, |
| 130 | + STM32_DMA_M_TO_P); |
| 131 | + |
| 132 | + stm32_dma_set_paddr(pwg->dma, gpio_bsrr_addr); |
| 133 | + stm32_dma_set_mem0(pwg->dma, pwg->buf); |
| 134 | + stm32_dma_set_nitems(pwg->dma, PWG_BUF_SIZE); |
| 135 | + |
| 136 | + stm32_dma_set_callback(pwg->dma, pwg_dma_cb, pwg, IRQ_LEVEL_SCHED, |
| 137 | + DMA_STATUS_HALF_XFER | DMA_STATUS_FULL_XFER); |
| 138 | + |
| 139 | + thread_create(pwg_thread, pwg, 512, "pwg", TASK_DETACHED, 20); |
| 140 | + |
| 141 | + return pwg; |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +void |
| 146 | +stm32_pwg_stop(stm32_pwg_t *pwg) |
| 147 | +{ |
| 148 | + pwg->running = 0; |
| 149 | + task_wakeup(&pwg->waitq, 0); |
| 150 | +} |
0 commit comments