1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "hal/rmt_hal.h"
15 #include "hal/rmt_ll.h"
16 #include "soc/soc_caps.h"
17 
rmt_hal_init(rmt_hal_context_t * hal)18 void rmt_hal_init(rmt_hal_context_t *hal)
19 {
20     hal->regs = &RMT;
21     hal->mem = &RMTMEM;
22 }
23 
rmt_hal_tx_channel_reset(rmt_hal_context_t * hal,uint32_t channel)24 void rmt_hal_tx_channel_reset(rmt_hal_context_t *hal, uint32_t channel)
25 {
26     rmt_ll_tx_reset_pointer(hal->regs, channel);
27     rmt_ll_enable_tx_err_interrupt(hal->regs, channel, false);
28     rmt_ll_enable_tx_end_interrupt(hal->regs, channel, false);
29     rmt_ll_enable_tx_thres_interrupt(hal->regs, channel, false);
30     rmt_ll_clear_tx_err_interrupt(hal->regs, channel);
31     rmt_ll_clear_tx_end_interrupt(hal->regs, channel);
32     rmt_ll_clear_tx_thres_interrupt(hal->regs, channel);
33 }
34 
rmt_hal_rx_channel_reset(rmt_hal_context_t * hal,uint32_t channel)35 void rmt_hal_rx_channel_reset(rmt_hal_context_t *hal, uint32_t channel)
36 {
37     rmt_ll_rx_reset_pointer(hal->regs, channel);
38     rmt_ll_enable_rx_err_interrupt(hal->regs, channel, false);
39     rmt_ll_enable_rx_end_interrupt(hal->regs, channel, false);
40     rmt_ll_clear_rx_err_interrupt(hal->regs, channel);
41     rmt_ll_clear_rx_end_interrupt(hal->regs, channel);
42 }
43 
rmt_hal_tx_set_counter_clock(rmt_hal_context_t * hal,uint32_t channel,uint32_t base_clk_hz,uint32_t counter_clk_hz)44 void rmt_hal_tx_set_counter_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t counter_clk_hz)
45 {
46     rmt_ll_tx_reset_counter_clock_div(hal->regs, channel);
47     uint32_t counter_div = (base_clk_hz + counter_clk_hz / 2) / counter_clk_hz;
48     rmt_ll_tx_set_counter_clock_div(hal->regs, channel, counter_div);
49 }
50 
rmt_hal_set_carrier_clock(rmt_hal_context_t * hal,uint32_t channel,uint32_t base_clk_hz,uint32_t carrier_clk_hz,float carrier_clk_duty)51 void rmt_hal_set_carrier_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t carrier_clk_hz, float carrier_clk_duty)
52 {
53     uint32_t carrier_div = (base_clk_hz + carrier_clk_hz / 2) / carrier_clk_hz;
54     uint32_t div_high = (uint32_t)(carrier_div * carrier_clk_duty);
55     uint32_t div_low = carrier_div - div_high;
56     rmt_ll_tx_set_carrier_high_low_ticks(hal->regs, channel, div_high, div_low);
57 }
58 
rmt_hal_set_rx_filter_thres(rmt_hal_context_t * hal,uint32_t channel,uint32_t base_clk_hz,uint32_t thres_us)59 void rmt_hal_set_rx_filter_thres(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t thres_us)
60 {
61     uint32_t thres = (uint32_t)(base_clk_hz / 1e6 * thres_us);
62     rmt_ll_rx_set_filter_thres(hal->regs, channel, thres);
63 }
64 
rmt_hal_set_rx_idle_thres(rmt_hal_context_t * hal,uint32_t channel,uint32_t base_clk_hz,uint32_t thres_us)65 void rmt_hal_set_rx_idle_thres(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t thres_us)
66 {
67     uint32_t thres = (uint32_t)(base_clk_hz / 1e6 * thres_us);
68     rmt_ll_rx_set_idle_thres(hal->regs, channel, thres);
69 }
70 
rmt_hal_receive(rmt_hal_context_t * hal,uint32_t channel,rmt_item32_t * buf)71 uint32_t rmt_hal_receive(rmt_hal_context_t *hal, uint32_t channel, rmt_item32_t *buf)
72 {
73     uint32_t len = 0;
74     rmt_ll_rx_set_mem_owner(hal->regs, channel, RMT_MEM_OWNER_SW);
75     for (len = 0; len < SOC_RMT_CHANNEL_MEM_WORDS; len++) {
76         buf[len].val = hal->mem->chan[channel].data32[len].val;
77         if (!(buf[len].val & 0x7FFF)) {
78             break;
79         } else if (!(buf[len].val & 0x7FFF0000)) {
80             len++;
81             break;
82         }
83     }
84     rmt_ll_rx_set_mem_owner(hal->regs, channel, RMT_MEM_OWNER_HW);
85     rmt_ll_rx_reset_pointer(hal->regs, channel);
86     return len;
87 }
88