| 1 |
/* |
| 2 |
* The MIT License (MIT) |
| 3 |
* |
| 4 |
* Copyright (c) 2023 Vekatech Ltd. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
* THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
/* FSP has available API for DAC (r_dac) |
| 26 |
|
| 27 |
R_DAC_Open ( ... ) |
| 28 |
R_DAC_Stop ( ... ) |
| 29 |
R_DAC_Start ( ... ) |
| 30 |
R_DAC_Write ( ... ) |
| 31 |
R_DAC_Close ( ... ) |
| 32 |
|
| 33 |
and this is (The Lazy way) |
| 34 |
|
| 35 |
... but looking to other drivers implementation (for example AGT [ra_timer.c/h]), Renesas want to be hard, so ... |
| 36 |
|
| 37 |
(The Hard way it is) |
| 38 |
*/ |
| 39 |
|
| 40 |
#include "hal_data.h" |
| 41 |
#include "ra_config.h" |
| 42 |
#include "ra_gpio.h" |
| 43 |
#include "ra_sci.h" |
| 44 |
// #include "ra_int.h" |
| 45 |
#include "ra_utils.h" |
| 46 |
#include "ra_dac.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#if defined(RA4M1) || defined(RA4W1) |
| 51 |
#define DAC_CH_SIZE 1 |
| 52 |
#elif defined(RA6M1) || defined(RA6M2) || defined(RA6M3) || defined(RA6M5) |
| 53 |
#define DAC_CH_SIZE 2 |
| 54 |
#else |
| 55 |
#error "CMSIS MCU Series is not specified." |
| 56 |
#endif |
| 57 |
|
| 58 |
#define DAC_PINS_SIZE sizeof(ra_dac_pins) / sizeof(ra_af_pin_t) |
| 59 |
|
| 60 |
static const ra_af_pin_t ra_dac_pins[] = { |
| 61 |
#if defined(RA4M1) || defined(RA4W1) |
| 62 |
{ AF_GPIO, 0, P014 }, // (A3) |
| 63 |
#elif defined(RA6M1) || defined(RA6M2) || defined(RA6M3) || defined(RA6M5) |
| 64 |
{ AF_GPIO, 0, P014 }, // (A4) |
| 65 |
{ AF_GPIO, 1, P015 }, // (A5) |
| 66 |
#else |
| 67 |
#error "CMSIS MCU Series is not specified." |
| 68 |
#endif |
| 69 |
}; |
| 70 |
|
| 71 |
static void ra_dac_set_pin(uint32_t pin) { |
| 72 |
bool find = false; |
| 73 |
uint32_t ch; |
| 74 |
uint32_t af; |
| 75 |
find = ra_af_find_ch_af((ra_af_pin_t *)&ra_dac_pins, DAC_PINS_SIZE, pin, &ch, &af); |
| 76 |
if (find) { |
| 77 |
ra_gpio_config(pin, GPIO_MODE_ANALOG, GPIO_NOPULL, GPIO_LOW_POWER, af); |
| 78 |
// pwpr_unprotect(); |
| 79 |
// _PXXPFS(GPIO_PORT(pin), GPIO_BIT(pin)) &= ~(PDR_MASK); |
| 80 |
// _PXXPFS(GPIO_PORT(pin), GPIO_BIT(pin)) |= PDR_MASK; // output |
| 81 |
// pwpr_protect(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
static void ra_dac_release_pin(uint32_t pin) { |
| 86 |
bool find = false; |
| 87 |
uint32_t ch; |
| 88 |
uint32_t af; |
| 89 |
find = ra_af_find_ch_af((ra_af_pin_t *)&ra_dac_pins, DAC_PINS_SIZE, pin, &ch, &af); |
| 90 |
if (find) { |
| 91 |
ra_gpio_config(pin, GPIO_MODE_ANALOG, GPIO_NOPULL, GPIO_LOW_POWER, AF_GPIO); |
| 92 |
// pwpr_unprotect(); |
| 93 |
// _PXXPFS(GPIO_PORT(pin), GPIO_BIT(pin)) &= ~(PDR_MASK); |
| 94 |
// pwpr_protect(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
bool ra_dac_is_dac_pin(uint32_t pin) { |
| 99 |
uint32_t ch; |
| 100 |
uint32_t af; |
| 101 |
return ra_af_find_ch_af((ra_af_pin_t *)&ra_dac_pins, DAC_PINS_SIZE, pin, &ch, &af); |
| 102 |
} |
| 103 |
|
| 104 |
uint8_t ra_dac_is_running(uint8_t ch) { |
| 105 |
if (ch < DAC_CH_SIZE) { |
| 106 |
return ch? R_DAC->DACR_b.DAOE1 : R_DAC->DACR_b.DAOE0; |
| 107 |
} else { |
| 108 |
return 0; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void ra_dac_start(uint8_t ch) { |
| 113 |
if (ch < DAC_CH_SIZE) { |
| 114 |
if (ch) { |
| 115 |
R_DAC->DACR_b.DAOE1 = 1U; |
| 116 |
} else { |
| 117 |
R_DAC->DACR_b.DAOE0 = 1U; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void ra_dac_stop(uint8_t ch) { |
| 123 |
if (ch < DAC_CH_SIZE) { |
| 124 |
if (ch) { |
| 125 |
R_DAC->DACR_b.DAOE1 = 0U; |
| 126 |
} else { |
| 127 |
R_DAC->DACR_b.DAOE0 = 0U; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void ra_dac_write(uint8_t ch, uint16_t val) { |
| 133 |
if ((ch < DAC_CH_SIZE) || (val < 4096)) { |
| 134 |
R_DAC->DADR[ch] = val; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
uint16_t ra_dac_read(uint8_t ch) { |
| 139 |
if (ch < DAC_CH_SIZE) { |
| 140 |
return R_DAC->DADR[ch]; |
| 141 |
} else { |
| 142 |
return 0; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
void ra_dac_init(uint32_t dac_pin, uint8_t ch) { |
| 147 |
if (ch < DAC_CH_SIZE) { |
| 148 |
ra_mstpcrd_start(R_MSTP_MSTPCRD_MSTPD20_Msk); |
| 149 |
|
| 150 |
R_DAC->DADPR_b.DPSEL = 0; // Right-justified format |
| 151 |
R_DAC->DAADSCR_b.DAADST = 0; // Do not synchronize with ADC14 |
| 152 |
R_DAC->DAVREFCR_b.REF = 1; // AVCC0/AVSS0 selected |
| 153 |
R_DAC->DADR[ch] = 0; // Output 0 Volts |
| 154 |
|
| 155 |
ra_dac_set_pin(dac_pin); |
| 156 |
ra_dac_start(ch); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void ra_dac_deinit(uint32_t dac_pin, uint8_t ch) { |
| 161 |
if (ch < DAC_CH_SIZE) { |
| 162 |
ra_dac_stop(ch); |
| 163 |
R_DAC->DAVREFCR_b.REF = 0; // No reference voltage selected |
| 164 |
ra_dac_release_pin(dac_pin); |
| 165 |
|
| 166 |
ra_mstpcrd_stop(R_MSTP_MSTPCRD_MSTPD20_Msk); |
| 167 |
} |
| 168 |
} |