| 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 PWM (r_gpt) |
| 26 |
|
| 27 |
R_GPT_Open ( ... ) |
| 28 |
R_GPT_Stop ( ... ) |
| 29 |
R_GPT_Start ( ... ) |
| 30 |
R_GPT_Reset ( ... ) |
| 31 |
R_GPT_Enable ( ... ) |
| 32 |
R_GPT_Disable ( ... ) |
| 33 |
R_GPT_PeriodSet ( ... ) |
| 34 |
R_GPT_DutyCycleSet ( ... ) |
| 35 |
R_GPT_InfoGet ( ... ) |
| 36 |
R_GPT_StatusGet ( ... ) |
| 37 |
R_GPT_CounterSet ( ... ) |
| 38 |
R_GPT_OutputEnable ( ... ) |
| 39 |
R_GPT_OutputDisable ( ... ) |
| 40 |
R_GPT_AdcTriggerSet ( ... ) |
| 41 |
R_GPT_CallbackSet ( ... ) |
| 42 |
R_GPT_Close ( ... ) |
| 43 |
|
| 44 |
and this is (The Lazy way) |
| 45 |
|
| 46 |
... but looking to other drivers implementation (for example AGT [ra_timer.c/h]), Renesas want to be hard, so ... |
| 47 |
|
| 48 |
(The Hard way it is) |
| 49 |
*/ |
| 50 |
|
| 51 |
#include "hal_data.h" |
| 52 |
#include "ra_config.h" |
| 53 |
#include "ra_gpio.h" |
| 54 |
#include "ra_sci.h" |
| 55 |
// #include "ra_int.h" |
| 56 |
#include "ra_utils.h" |
| 57 |
#include "ra_gpt.h" |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#if defined(RA4M1) |
| 62 |
#define GPT_CH_SIZE 8 |
| 63 |
#define CH_GAP 0 |
| 64 |
#elif defined(RA4W1) |
| 65 |
#define GPT_CH_SIZE 9 |
| 66 |
#define CH_GAP (ch == 6) || (ch == 7) |
| 67 |
#elif defined(RA6M1) |
| 68 |
#define GPT_CH_SIZE 13 |
| 69 |
#define CH_GAP 0 |
| 70 |
#elif defined(RA6M2) || defined(RA6M3) |
| 71 |
#define GPT_CH_SIZE 14 |
| 72 |
#define CH_GAP 0 |
| 73 |
#elif defined(RA6M5) |
| 74 |
#define GPT_CH_SIZE 10 |
| 75 |
#define CH_GAP 0 |
| 76 |
#else |
| 77 |
#error "CMSIS MCU Series is not specified." |
| 78 |
#endif |
| 79 |
|
| 80 |
enum GPT_SOURCE { |
| 81 |
GPT_PCLKD = 0, |
| 82 |
GPT_PCLKD4, |
| 83 |
GPT_PCLKD16, |
| 84 |
GPT_PCLKD64, |
| 85 |
GPT_PCLKD256, |
| 86 |
GPT_PCLKD1024 |
| 87 |
}; |
| 88 |
|
| 89 |
static R_GPT0_Type *gpt_regs[GPT_CH_SIZE] = { |
| 90 |
#if defined(RA4M1) |
| 91 |
R_GPT0, // GPT320 General PWM Timer 0 (32-bit) |
| 92 |
R_GPT1, // GPT321 General PWM Timer 1 (32-bit) |
| 93 |
R_GPT2, // GPT162 General PWM Timer 2 (16-bit) |
| 94 |
R_GPT3, // GPT163 General PWM Timer 3 (16-bit) |
| 95 |
R_GPT4, // GPT164 General PWM Timer 4 (16-bit) |
| 96 |
R_GPT5, // GPT165 General PWM Timer 5 (16-bit) |
| 97 |
R_GPT6, // GPT166 General PWM Timer 6 (16-bit) |
| 98 |
R_GPT7, // GPT167 General PWM Timer 7 (16-bit) |
| 99 |
#elif defined(RA4W1) |
| 100 |
R_GPT0, // GPT320 General PWM Timer 0 (32-bit) |
| 101 |
R_GPT1, // GPT321 General PWM Timer 1 (32-bit) |
| 102 |
R_GPT2, // GPT322 General PWM Timer 2 (32-bit) |
| 103 |
R_GPT3, // GPT323 General PWM Timer 3 (32-bit) |
| 104 |
R_GPT4, // GPT164 General PWM Timer 4 (16-bit) |
| 105 |
R_GPT5, // GPT165 General PWM Timer 5 (16-bit) |
| 106 |
NULL, // N/A PWM Timer 6 (Does not exist) |
| 107 |
NULL, // N/A PWM Timer 7 (Does not exist) |
| 108 |
R_GPT8 // GPT168 General PWM Timer 8 (16-bit) |
| 109 |
#elif defined(RA6M1) || defined(RA6M2) || defined(RA6M3) |
| 110 |
R_GPT0, // GPT32EH0 General PWM Timer 0 (32-bit) |
| 111 |
R_GPT1, // GPT32EH1 General PWM Timer 1 (32-bit) |
| 112 |
R_GPT2, // GPT32EH2 General PWM Timer 2 (32-bit) |
| 113 |
R_GPT3, // GPT32EH3 General PWM Timer 3 (32-bit) |
| 114 |
R_GPT4, // GPT32E4 General PWM Timer 4 (32-bit) |
| 115 |
R_GPT5, // GPT32E5 General PWM Timer 5 (32-bit) |
| 116 |
R_GPT6, // GPT32E6 General PWM Timer 6 (32-bit) |
| 117 |
R_GPT7, // GPT32E7 General PWM Timer 7 (32-bit) |
| 118 |
R_GPT8, // GPT328 General PWM Timer 8 (32-bit) |
| 119 |
R_GPT9, // GPT329 General PWM Timer 9 (32-bit) |
| 120 |
R_GPT10, // GPT3210 General PWM Timer 10 (32-bit) |
| 121 |
R_GPT11, // GPT3211 General PWM Timer 11 (32-bit) |
| 122 |
R_GPT12, // GPT3212 General PWM Timer 12 (32-bit) |
| 123 |
#if defined(RA6M2) || defined(RA6M3) |
| 124 |
R_GPT13, // GPT3213 General PWM Timer 13 (32-bit) |
| 125 |
#endif |
| 126 |
#elif defined(RA6M5) |
| 127 |
R_GPT0, // GPT320 General PWM Timer 0 (32-bit) |
| 128 |
R_GPT1, // GPT321 General PWM Timer 1 (32-bit) |
| 129 |
R_GPT2, // GPT322 General PWM Timer 2 (32-bit) |
| 130 |
R_GPT3, // GPT323 General PWM Timer 3 (32-bit) |
| 131 |
R_GPT4, // GPT164 General PWM Timer 4 (16-bit) |
| 132 |
R_GPT5, // GPT165 General PWM Timer 5 (16-bit) |
| 133 |
R_GPT6, // GPT166 General PWM Timer 6 (16-bit) |
| 134 |
R_GPT7, // GPT167 General PWM Timer 7 (16-bit) |
| 135 |
R_GPT8, // GPT168 General PWM Timer 8 (16-bit) |
| 136 |
R_GPT9, // GPT169 General PWM Timer 9 (16-bit) |
| 137 |
#else |
| 138 |
#error "CMSIS MCU Series is not specified." |
| 139 |
#endif |
| 140 |
}; |
| 141 |
|
| 142 |
#define GPT_TMR_PINS_SIZE sizeof(ra_gpt_timer_pins) / sizeof(ra_af_pin_t) |
| 143 |
|
| 144 |
static const ra_af_pin_t ra_gpt_timer_pins[] = { |
| 145 |
#if defined(RA4M1) |
| 146 |
{ AF_GPT2, 0, P107 }, { AF_GPT2, 0, P213 }, { AF_GPT2, 0, P300 }, { AF_GPT2, 0, P415 }, // GTIOC0A |
| 147 |
{ AF_GPT2, 0, P106 }, { AF_GPT2, 0, P108 }, { AF_GPT2, 0, P212 }, { AF_GPT2, 0, P414 }, // GTIOC0B |
| 148 |
{ AF_GPT2, 1, P105 }, { AF_GPT2, 1, P109 }, { AF_GPT2, 1, P405 }, // GTIOC1A |
| 149 |
{ AF_GPT2, 1, P104 }, { AF_GPT2, 1, P110 }, { AF_GPT2, 1, P406 }, // GTIOC1B |
| 150 |
{ AF_GPT2, 2, P103 }, { AF_GPT2, 2, P113 }, { AF_GPT2, 2, P500 }, // GTIOC2A |
| 151 |
{ AF_GPT2, 2, P102 }, { AF_GPT2, 2, P114 }, { AF_GPT2, 2, P501 }, // GTIOC2B |
| 152 |
{ AF_GPT2, 3, P111 }, { AF_GPT2, 3, P403 }, // GTIOC3A |
| 153 |
{ AF_GPT2, 3, P112 }, { AF_GPT2, 3, P404 }, { AF_GPT2, 3, P502 }, // GTIOC3B |
| 154 |
{ AF_GPT2, 4, P115 }, { AF_GPT2, 4, P205 }, { AF_GPT2, 4, P302 }, // GTIOC4A |
| 155 |
{ AF_GPT2, 4, P204 }, { AF_GPT2, 4, P301 }, { AF_GPT2, 4, P608 }, // GTIOC4B |
| 156 |
{ AF_GPT2, 5, P101 }, { AF_GPT2, 5, P203 }, { AF_GPT2, 5, P409 }, { AF_GPT2, 5, P609 }, // GTIOC5A |
| 157 |
{ AF_GPT2, 5, P100 }, { AF_GPT2, 5, P202 }, { AF_GPT2, 5, P408 }, { AF_GPT2, 5, P610 }, // GTIOC5B |
| 158 |
{ AF_GPT2, 6, P400 }, { AF_GPT2, 6, P411 }, { AF_GPT2, 6, P601 }, // GTIOC6A |
| 159 |
{ AF_GPT2, 6, P401 }, { AF_GPT2, 6, P410 }, { AF_GPT2, 6, P600 }, // GTIOC6B |
| 160 |
{ AF_GPT2, 7, P304 }, { AF_GPT2, 7, P603 }, // GTIOC7A |
| 161 |
{ AF_GPT2, 7, P303 }, { AF_GPT2, 7, P602 }, // GTIOC7B |
| 162 |
#elif defined(RA4W1) |
| 163 |
{ AF_GPT2, 0, P213 }, { AF_GPT2, 0, P300 }, // GTIOC0A |
| 164 |
{ AF_GPT2, 0, P108 }, { AF_GPT2, 0, P212 }, { AF_GPT2, 0, P414 }, // GTIOC0B |
| 165 |
{ AF_GPT2, 1, P105 }, { AF_GPT2, 1, P109 }, // GTIOC1A |
| 166 |
{ AF_GPT2, 1, P104 }, { AF_GPT2, 1, P110 }, // GTIOC1B |
| 167 |
{ AF_GPT2, 2, P103 }, // GTIOC2A |
| 168 |
{ AF_GPT2, 2, P102 }, { AF_GPT2, 2, P501 }, // GTIOC2B |
| 169 |
{ AF_GPT2, 3, P111 }, // GTIOC3A |
| 170 |
{ AF_GPT2, 3, P404 }, // GTIOC3B |
| 171 |
{ AF_GPT2, 4, P205 }, // GTIOC4A |
| 172 |
{ AF_GPT2, 4, P204 }, // GTIOC4B |
| 173 |
{ AF_GPT2, 5, P101 }, { AF_GPT2, 5, P409 }, // GTIOC5A |
| 174 |
{ AF_GPT2, 5, P100 }, // GTIOC5B |
| 175 |
{ AF_GPT2, 8, P107 }, // GTIOC8A |
| 176 |
{ AF_GPT2, 8, P106 }, // GTIOC8B |
| 177 |
#elif defined(RA6M1) |
| 178 |
{ AF_GPT2, 0, P213 }, { AF_GPT2, 0, P415 }, // GTIOC0A |
| 179 |
{ AF_GPT2, 0, P212 }, { AF_GPT2, 0, P414 }, // GTIOC0B |
| 180 |
{ AF_GPT2, 1, P105 }, { AF_GPT2, 1, P405 }, // GTIOC1A |
| 181 |
{ AF_GPT2, 1, P104 }, { AF_GPT2, 1, P406 }, // GTIOC1B |
| 182 |
{ AF_GPT2, 2, P113 }, // GTIOC2A |
| 183 |
{ AF_GPT2, 2, P114 }, // GTIOC2B |
| 184 |
{ AF_GPT2, 3, P403 }, // GTIOC3A |
| 185 |
{ AF_GPT2, 3, P404 }, // GTIOC3B |
| 186 |
{ AF_GPT2, 4, P115 }, { AF_GPT2, 4, P205 }, { AF_GPT2, 4, P302 }, // GTIOC4A |
| 187 |
{ AF_GPT2, 4, P301 }, { AF_GPT2, 4, P608 }, // GTIOC4B |
| 188 |
{ AF_GPT2, 5, P101 }, { AF_GPT2, 5, P609 }, // GTIOC5A |
| 189 |
{ AF_GPT2, 5, P100 }, { AF_GPT2, 5, P610 }, // GTIOC5B |
| 190 |
{ AF_GPT2, 6, P400 }, { AF_GPT2, 6, P601 }, // GTIOC6A |
| 191 |
{ AF_GPT2, 6, P401 }, { AF_GPT2, 6, P600 }, // GTIOC6B |
| 192 |
{ AF_GPT2, 7, P304 }, // GTIOC7A |
| 193 |
{ AF_GPT2, 7, P303 }, { AF_GPT2, 7, P602 }, // GTIOC7B |
| 194 |
{ AF_GPT2, 8, P107 }, // GTIOC8A |
| 195 |
{ AF_GPT2, 8, P106 }, // GTIOC8B |
| 196 |
{ AF_GPT2, 9, P411 }, // GTIOC9A |
| 197 |
{ AF_GPT2, 9, P410 }, // GTIOC9B |
| 198 |
{ AF_GPT2, 10, P409 }, // GTIOC10A |
| 199 |
{ AF_GPT2, 10, P408 }, // GTIOC10B |
| 200 |
{ AF_GPT2, 11, P500 }, // GTIOC11A |
| 201 |
{ AF_GPT2, 11, P501 }, // GTIOC11B |
| 202 |
{ AF_GPT2, 12, P502 }, // GTIOC12A |
| 203 |
{ AF_GPT2, 12, P503 }, // GTIOC12B |
| 204 |
#elif defined(RA6M2) |
| 205 |
{ AF_GPT2, 0, P213 }, { AF_GPT2, 0, P415 }, { AF_GPT2, 0, P512 }, // GTIOC0A |
| 206 |
{ AF_GPT2, 0, P212 }, { AF_GPT2, 0, P414 }, { AF_GPT2, 0, P511 }, // GTIOC0B |
| 207 |
{ AF_GPT2, 1, P105 }, { AF_GPT2, 1, P405 }, // GTIOC1A |
| 208 |
{ AF_GPT2, 1, P104 }, { AF_GPT2, 1, P406 }, // GTIOC1B |
| 209 |
{ AF_GPT2, 2, P113 }, { AF_GPT2, 2, P713 }, // GTIOC2A |
| 210 |
{ AF_GPT2, 2, P114 }, { AF_GPT2, 2, P712 }, // GTIOC2B |
| 211 |
{ AF_GPT2, 3, P403 }, // GTIOC3A |
| 212 |
{ AF_GPT2, 3, P404 }, // GTIOC3B |
| 213 |
{ AF_GPT2, 4, P115 }, { AF_GPT2, 4, P205 }, { AF_GPT2, 4, P302 }, // GTIOC4A |
| 214 |
{ AF_GPT2, 4, P204 }, { AF_GPT2, 4, P301 }, { AF_GPT2, 4, P608 }, // GTIOC4B |
| 215 |
{ AF_GPT2, 5, P101 }, { AF_GPT2, 5, P203 }, { AF_GPT2, 5, P609 }, { AF_GPT2, 5, P700 }, // GTIOC5A |
| 216 |
{ AF_GPT2, 5, P100 }, { AF_GPT2, 5, P202 }, { AF_GPT2, 5, P610 }, { AF_GPT2, 5, P701 }, // GTIOC5B |
| 217 |
{ AF_GPT2, 6, P400 }, { AF_GPT2, 6, P601 }, { AF_GPT2, 6, P702 }, // GTIOC6A |
| 218 |
{ AF_GPT2, 6, P401 }, { AF_GPT2, 6, P600 }, { AF_GPT2, 6, P703 }, // GTIOC6B |
| 219 |
{ AF_GPT2, 7, P304 }, { AF_GPT2, 7, P603 }, // GTIOC7A |
| 220 |
{ AF_GPT2, 7, P303 }, { AF_GPT2, 7, P602 }, // GTIOC7B |
| 221 |
{ AF_GPT2, 8, P107 }, { AF_GPT2, 8, P605 }, // GTIOC8A |
| 222 |
{ AF_GPT2, 8, P106 }, { AF_GPT2, 8, P604 }, // GTIOC8B |
| 223 |
{ AF_GPT2, 9, P411 }, // GTIOC9A |
| 224 |
{ AF_GPT2, 9, P410 }, // GTIOC9B |
| 225 |
{ AF_GPT2, 10, P409 }, // GTIOC10A |
| 226 |
{ AF_GPT2, 10, P408 }, // GTIOC10B |
| 227 |
{ AF_GPT2, 11, P500 }, // GTIOC11A |
| 228 |
{ AF_GPT2, 11, P501 }, // GTIOC11B |
| 229 |
{ AF_GPT2, 12, P502 }, // GTIOC12A |
| 230 |
{ AF_GPT2, 12, P503 }, // GTIOC12B |
| 231 |
{ AF_GPT2, 13, P504 }, // GTIOC13A |
| 232 |
{ AF_GPT2, 13, P505 }, // GTIOC13B |
| 233 |
#elif defined(RA6M3) |
| 234 |
{ AF_GPT2, 0, P213 }, { AF_GPT2, 0, P415 }, { AF_GPT2, 0, P512 }, // GTIOC0A |
| 235 |
{ AF_GPT2, 0, P212 }, { AF_GPT2, 0, P414 }, { AF_GPT2, 0, P511 }, // GTIOC0B |
| 236 |
{ AF_GPT2, 1, P105 }, { AF_GPT2, 1, P405 }, // GTIOC1A |
| 237 |
{ AF_GPT2, 1, P104 }, { AF_GPT2, 1, P406 }, // GTIOC1B |
| 238 |
{ AF_GPT2, 2, P113 }, { AF_GPT2, 2, P713 }, // GTIOC2A |
| 239 |
{ AF_GPT2, 2, P114 }, { AF_GPT2, 2, P712 }, // GTIOC2B |
| 240 |
{ AF_GPT2, 3, P403 }, // GTIOC3A |
| 241 |
{ AF_GPT2, 3, P404 }, // GTIOC3B |
| 242 |
{ AF_GPT2, 4, P115 }, { AF_GPT2, 4, P205 }, { AF_GPT2, 4, P302 }, // GTIOC4A |
| 243 |
{ AF_GPT2, 4, P204 }, { AF_GPT2, 4, P301 }, { AF_GPT2, 4, P608 }, // GTIOC4B |
| 244 |
{ AF_GPT2, 5, P101 }, { AF_GPT2, 5, P203 }, { AF_GPT2, 5, P609 }, { AF_GPT2, 5, P700 }, // GTIOC5A |
| 245 |
{ AF_GPT2, 5, P100 }, { AF_GPT2, 5, P202 }, { AF_GPT2, 5, P610 }, { AF_GPT2, 5, P701 }, // GTIOC5B |
| 246 |
{ AF_GPT2, 6, P400 }, { AF_GPT2, 6, P601 }, { AF_GPT2, 6, P702 }, // GTIOC6A |
| 247 |
{ AF_GPT2, 6, P401 }, { AF_GPT2, 6, P600 }, { AF_GPT2, 6, P703 }, // GTIOC6B |
| 248 |
{ AF_GPT2, 7, P304 }, { AF_GPT2, 7, P603 }, // GTIOC7A |
| 249 |
{ AF_GPT2, 7, P303 }, { AF_GPT2, 7, P602 }, // GTIOC7B |
| 250 |
{ AF_GPT2, 8, P107 }, { AF_GPT2, 8, P605 }, // GTIOC8A |
| 251 |
{ AF_GPT2, 8, P106 }, { AF_GPT2, 8, P604 }, // GTIOC8B |
| 252 |
{ AF_GPT2, 9, P411 }, // GTIOC9A |
| 253 |
{ AF_GPT2, 9, P410 }, // GTIOC9B |
| 254 |
{ AF_GPT2, 10, P409 }, // GTIOC10A |
| 255 |
{ AF_GPT2, 10, P408 }, // GTIOC10B |
| 256 |
{ AF_GPT2, 11, P500 }, // GTIOC11A |
| 257 |
{ AF_GPT2, 11, P501 }, // GTIOC11B |
| 258 |
{ AF_GPT2, 12, P502 }, { AF_GPT2, 12, P908 }, // GTIOC12A |
| 259 |
{ AF_GPT2, 12, P503 }, { AF_GPT2, 12, P907 }, // GTIOC12B |
| 260 |
{ AF_GPT2, 13, P504 }, { AF_GPT2, 13, P906 }, // GTIOC13A |
| 261 |
{ AF_GPT2, 13, P505 }, { AF_GPT2, 13, P905 }, // GTIOC13B |
| 262 |
#elif defined(RA6M5) |
| 263 |
{ AF_GPT2, 0, P213 }, { AF_GPT2, 0, P300 }, { AF_GPT2, 0, P415 }, { AF_GPT2, 0, P512 }, // GTIOC0A |
| 264 |
{ AF_GPT2, 0, P108 }, { AF_GPT2, 0, P212 }, { AF_GPT2, 0, P414 }, { AF_GPT2, 0, P511 }, // GTIOC0B |
| 265 |
{ AF_GPT2, 1, P105 }, { AF_GPT2, 1, P109 }, { AF_GPT2, 1, P405 }, // GTIOC1A |
| 266 |
{ AF_GPT2, 1, P104 }, { AF_GPT2, 1, P110 }, { AF_GPT2, 1, P406 }, // GTIOC1B |
| 267 |
{ AF_GPT2, 2, P103 }, { AF_GPT2, 2, P113 }, { AF_GPT2, 2, P713 }, // GTIOC2A |
| 268 |
{ AF_GPT2, 2, P102 }, { AF_GPT2, 2, P114 }, { AF_GPT2, 2, P712 }, // GTIOC2B |
| 269 |
{ AF_GPT2, 3, P111 }, { AF_GPT2, 3, P403 }, // GTIOC3A |
| 270 |
{ AF_GPT2, 3, P112 }, { AF_GPT2, 3, P404 }, // GTIOC3B |
| 271 |
{ AF_GPT2, 4, P115 }, { AF_GPT2, 4, P205 }, { AF_GPT2, 4, P302 }, // GTIOC4A |
| 272 |
{ AF_GPT2, 4, P204 }, { AF_GPT2, 4, P301 }, { AF_GPT2, 4, P608 }, // GTIOC4B |
| 273 |
{ AF_GPT2, 5, P101 }, { AF_GPT2, 5, P203 }, { AF_GPT2, 5, P609 }, { AF_GPT2, 5, P700 }, // GTIOC5A |
| 274 |
{ AF_GPT2, 5, P100 }, { AF_GPT2, 5, P202 }, { AF_GPT2, 5, P610 }, { AF_GPT2, 5, P701 }, // GTIOC5B |
| 275 |
{ AF_GPT2, 6, P400 }, { AF_GPT2, 6, P407 }, { AF_GPT2, 6, P601 }, { AF_GPT2, 6, P702 }, // GTIOC6A |
| 276 |
{ AF_GPT2, 6, P401 }, { AF_GPT2, 6, P408 }, { AF_GPT2, 6, P600 }, { AF_GPT2, 6, P703 }, // GTIOC6B |
| 277 |
{ AF_GPT2, 7, P304 }, { AF_GPT2, 7, P603 }, // GTIOC7A |
| 278 |
{ AF_GPT2, 7, P303 }, { AF_GPT2, 7, P602 }, // GTIOC7B |
| 279 |
{ AF_GPT2, 8, P107 }, { AF_GPT2, 8, P605 }, // GTIOC8A |
| 280 |
{ AF_GPT2, 8, P106 }, { AF_GPT2, 8, P604 }, // GTIOC8B |
| 281 |
{ AF_GPT2, 9, P411 }, // GTIOC9A |
| 282 |
{ AF_GPT2, 9, P410 }, // GTIOC9B |
| 283 |
#else |
| 284 |
#error "CMSIS MCU Series is not specified." |
| 285 |
#endif |
| 286 |
}; |
| 287 |
|
| 288 |
static float ra_gpt_freq[GPT_CH_SIZE]; |
| 289 |
static volatile uint16_t ra_gpt_div[GPT_CH_SIZE]; |
| 290 |
|
| 291 |
static void ra_gpt_timer_set_pin(uint32_t pin) { |
| 292 |
bool find = false; |
| 293 |
uint32_t ch; |
| 294 |
uint32_t af; |
| 295 |
find = ra_af_find_ch_af((ra_af_pin_t *)&ra_gpt_timer_pins, GPT_TMR_PINS_SIZE, pin, &ch, &af); |
| 296 |
if (find) { // GPIO_MODE_OUTPUT_PP |
| 297 |
ra_gpio_config(pin, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_LOW_POWER, af); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
static void ra_gpt_timer_release_pin(uint32_t pin) { |
| 302 |
bool find = false; |
| 303 |
uint32_t ch; |
| 304 |
uint32_t af; |
| 305 |
find = ra_af_find_ch_af((ra_af_pin_t *)&ra_gpt_timer_pins, GPT_TMR_PINS_SIZE, pin, &ch, &af); |
| 306 |
if (find) { // GPIO_PULLUP |
| 307 |
ra_gpio_config(pin, GPIO_MODE_INPUT, GPIO_NOPULL, GPIO_LOW_POWER, AF_GPIO); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
bool ra_gpt_timer_is_pwm_pin(uint32_t pin) { |
| 312 |
uint32_t ch; |
| 313 |
uint32_t af; |
| 314 |
return ra_af_find_ch_af((ra_af_pin_t *)&ra_gpt_timer_pins, GPT_TMR_PINS_SIZE, pin, &ch, &af); |
| 315 |
} |
| 316 |
|
| 317 |
void ra_gpt_timer_start(uint32_t ch) { |
| 318 |
if (CH_GAP || (ch >= GPT_CH_SIZE)) { |
| 319 |
return; |
| 320 |
} |
| 321 |
gpt_regs[ch]->GTCR_b.CST = 1; |
| 322 |
} |
| 323 |
|
| 324 |
void ra_gpt_timer_stop(uint32_t ch) { |
| 325 |
if (CH_GAP || (ch >= GPT_CH_SIZE)) { |
| 326 |
return; |
| 327 |
} |
| 328 |
gpt_regs[ch]->GTCR_b.CST = 0; |
| 329 |
} |
| 330 |
|
| 331 |
void ra_gpt_timer_set_freq(uint32_t ch, float freq) { |
| 332 |
R_GPT0_Type *gpt_reg = gpt_regs[ch]; |
| 333 |
uint8_t source = 0; |
| 334 |
uint32_t period = 0; |
| 335 |
ra_gpt_freq[ch] = 0.0f; |
| 336 |
|
| 337 |
if ((!gpt_reg) || (ch >= GPT_CH_SIZE) || (freq == ra_gpt_freq[ch]) || (freq == 0.0f)) { |
| 338 |
return; |
| 339 |
} |
| 340 |
|
| 341 |
ra_gpt_div[ch] = 0; |
| 342 |
|
| 343 |
#ifdef RA4M1 |
| 344 |
if (ch <= 1) { // 32bit |
| 345 |
#elif defined(RA4W1) || defined(RA6M5) |
| 346 |
if (ch <= 3) { // 32bit |
| 347 |
#elif defined(RA6M1) || defined(RA6M2) || defined(RA6M3) |
| 348 |
if (1) { // all is 32bit |
| 349 |
#else |
| 350 |
#error What is the width of the timer? (How many bits) |
| 351 |
#endif |
| 352 |
// float min_freq = ((float)PCLK)/4294967295.0f; |
| 353 |
if ((freq > (float)(PCLK / 2)) || (freq <= 0.0f)) { |
| 354 |
return; |
| 355 |
} else { |
| 356 |
source = GPT_PCLKD; |
| 357 |
period = (uint32_t)((float)(PCLK) / freq); |
| 358 |
} |
| 359 |
} else { // 16bit |
| 360 |
float min_freq = ((float)PCLK) / 65565.0f; |
| 361 |
|
| 362 |
if (freq > (float)(PCLK / 2)) { |
| 363 |
return; |
| 364 |
} else if (freq > min_freq) { |
| 365 |
source = GPT_PCLKD; |
| 366 |
period = (uint16_t)((float)(PCLK) / freq); |
| 367 |
} else if (freq > (min_freq / 4.0f)) { |
| 368 |
source = GPT_PCLKD4; |
| 369 |
ra_gpt_div[ch] = 4; |
| 370 |
period = (uint16_t)(((float)(PCLK) / 4.0f) / freq); |
| 371 |
} else if (freq > (min_freq / 16.0f)) { |
| 372 |
source = GPT_PCLKD16; |
| 373 |
ra_gpt_div[ch] = 16; |
| 374 |
period = (uint16_t)(((float)(PCLK) / 16.0f) / freq); |
| 375 |
} else if (freq > (min_freq / 64.0f)) { |
| 376 |
source = GPT_PCLKD64; |
| 377 |
ra_gpt_div[ch] = 64; |
| 378 |
period = (uint16_t)(((float)(PCLK) / 64.0f) / freq); |
| 379 |
} else if (freq > (min_freq / 256.0f)) { |
| 380 |
source = GPT_PCLKD256; |
| 381 |
ra_gpt_div[ch] = 256; |
| 382 |
period = (uint16_t)(((float)(PCLK) / 256.0f) / freq); |
| 383 |
} else if (freq > (min_freq / 1024.0f)) { |
| 384 |
source = GPT_PCLKD1024; |
| 385 |
ra_gpt_div[ch] = 1024; |
| 386 |
period = (uint16_t)(((float)(PCLK) / 1024.0f) / freq); |
| 387 |
} else { |
| 388 |
return; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
ra_gpt_freq[ch] = freq; |
| 393 |
|
| 394 |
float dutyA = (gpt_reg->GTIOR_b.OAE && gpt_reg->GTCCR[0] && gpt_reg->GTPR)? (float)(gpt_reg->GTCCR[0] + 1) / (float)(gpt_reg->GTPR + 1) : 0.0f; |
| 395 |
float dutyB = (gpt_reg->GTIOR_b.OBE && gpt_reg->GTCCR[0] && gpt_reg->GTPR)? (float)(gpt_reg->GTCCR[1] + 1) / (float)(gpt_reg->GTPR + 1) : 0.0f; |
| 396 |
|
| 397 |
if (gpt_reg->GTCR_b.CST) { // running |
| 398 |
if ((gpt_reg->GTCR >> 23) != source) { // Can't use gpt_reg->GTCR_b.TPCS this structure is declared wrong by FPS v2.3.0 so get the whole reg |
| 399 |
gpt_reg->GTCR_b.CST = 0; // stop counter |
| 400 |
gpt_reg->GTCR_b.TPCS = source << 1; // Again gpt_reg->GTCR_b.TPCS is wrong and incoming value should be shifted |
| 401 |
gpt_reg->GTPR = period - 1; |
| 402 |
gpt_reg->GTPBR = period - 1; |
| 403 |
|
| 404 |
if (dutyA > 0.0f) { |
| 405 |
dutyA = dutyA * ((float)(period)); |
| 406 |
gpt_reg->GTCCR[0] = (uint32_t)(dutyA - 1.0f); |
| 407 |
gpt_reg->GTCCR[2] = gpt_reg->GTCCR[0]; |
| 408 |
} |
| 409 |
|
| 410 |
if (dutyB > 0.0f) { |
| 411 |
dutyB = dutyB * ((float)(period)); |
| 412 |
gpt_reg->GTCCR[1] = (uint32_t)(dutyB - 1.0f); |
| 413 |
gpt_reg->GTCCR[3] = gpt_reg->GTCCR[1]; |
| 414 |
} |
| 415 |
|
| 416 |
gpt_reg->GTCNT = 0ul; // clear counter |
| 417 |
gpt_reg->GTCR_b.CST = 1; // start counter |
| 418 |
} else { |
| 419 |
// gpt_reg->GTPR = period-1; |
| 420 |
gpt_reg->GTPBR = period - 1; |
| 421 |
|
| 422 |
if (dutyA > 0.0f) { |
| 423 |
dutyA = dutyA * ((float)(period)); |
| 424 |
// gpt_reg->GTCCR[0] = (uint32_t)(dutyA - 1.0f); |
| 425 |
gpt_reg->GTCCR[2] = (uint32_t)(dutyA - 1.0f); |
| 426 |
} |
| 427 |
|
| 428 |
if (dutyB > 0.0f) { |
| 429 |
dutyB = dutyB * ((float)(period)); |
| 430 |
// gpt_reg->GTCCR[1] = (uint32_t)(dutyB - 1.0f); |
| 431 |
gpt_reg->GTCCR[3] = (uint32_t)(dutyB - 1.0f); |
| 432 |
} |
| 433 |
} |
| 434 |
} else { // stopped |
| 435 |
gpt_reg->GTCR_b.TPCS = source << 1; |
| 436 |
gpt_reg->GTPR = period - 1; |
| 437 |
gpt_reg->GTPBR = period - 1; |
| 438 |
|
| 439 |
if (dutyA > 0.0f) { |
| 440 |
dutyA = dutyA * ((float)(period)); |
| 441 |
gpt_reg->GTCCR[0] = (uint32_t)(dutyA - 1.0f); |
| 442 |
gpt_reg->GTCCR[2] = gpt_reg->GTCCR[0]; |
| 443 |
} |
| 444 |
|
| 445 |
if (dutyB > 0.0f) { |
| 446 |
dutyB = dutyB * ((float)(period)); |
| 447 |
gpt_reg->GTCCR[1] = (uint32_t)(dutyB - 1.0f); |
| 448 |
gpt_reg->GTCCR[3] = gpt_reg->GTCCR[1]; |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
float ra_gpt_timer_get_freq(uint32_t ch) { |
| 454 |
return (CH_GAP || (ch >= GPT_CH_SIZE))? 0.0f : ra_gpt_freq[ch]; |
| 455 |
} |
| 456 |
|
| 457 |
float ra_gpt_timer_tick_time(uint32_t ch) { |
| 458 |
if (CH_GAP || (ch >= GPT_CH_SIZE)) { |
| 459 |
return 0.0f; |
| 460 |
} else { |
| 461 |
float NS = 1000000000.0f; |
| 462 |
if (ra_gpt_div[ch]) { |
| 463 |
NS *= (float)ra_gpt_div[ch]; |
| 464 |
} |
| 465 |
return NS / (float)(PCLK); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
/* |
| 470 |
void ra_gpt_timer_set_period(uint32_t ch, uint32_t ns) |
| 471 |
{ |
| 472 |
R_GPT0_Type *gpt_reg = gpt_regs[ch]; |
| 473 |
|
| 474 |
if((!gpt_reg) || (ch >= GPT_CH_SIZE)) |
| 475 |
return; |
| 476 |
|
| 477 |
if(ch <= 3) |
| 478 |
{ //32bit |
| 479 |
} |
| 480 |
else |
| 481 |
{ //16bit |
| 482 |
} |
| 483 |
} |
| 484 |
*/ |
| 485 |
|
| 486 |
uint32_t ra_gpt_timer_get_period(uint32_t ch) { |
| 487 |
R_GPT0_Type *gpt_reg = gpt_regs[ch]; |
| 488 |
|
| 489 |
if ((!gpt_reg) || (ch >= GPT_CH_SIZE)) { |
| 490 |
return 0ul; |
| 491 |
} else { |
| 492 |
return gpt_reg->GTPR + 1; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
void ra_gpt_timer_set_duty(uint32_t ch, uint8_t id, uint32_t duty) { |
| 497 |
R_GPT0_Type *gpt_reg = gpt_regs[ch]; |
| 498 |
|
| 499 |
if ((!gpt_reg) || (ch >= GPT_CH_SIZE)) { |
| 500 |
return; |
| 501 |
} else { |
| 502 |
if (gpt_reg->GTCR_b.CST) { |
| 503 |
gpt_reg->GTCCR[(id == 'A')? 2 : 3] = duty? duty - 1 : duty; |
| 504 |
} else { |
| 505 |
gpt_reg->GTCCR[(id == 'A')? 0 : 1] = duty? duty - 1 : duty; |
| 506 |
if (id == 'A') { |
| 507 |
gpt_reg->GTCCR[2] = gpt_reg->GTCCR[0]; |
| 508 |
} else { |
| 509 |
gpt_reg->GTCCR[3] = gpt_reg->GTCCR[1]; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
uint32_t ra_gpt_timer_get_duty(uint32_t ch, uint8_t id) { |
| 516 |
R_GPT0_Type *gpt_reg = gpt_regs[ch]; |
| 517 |
if ((!gpt_reg) || (ch >= GPT_CH_SIZE)) { |
| 518 |
return 0ul; |
| 519 |
} else |
| 520 |
if (id == 'A') { |
| 521 |
return (gpt_reg->GTCCR[0])? gpt_reg->GTCCR[0] + 1 : 0; |
| 522 |
} else { |
| 523 |
return (gpt_reg->GTCCR[1])? gpt_reg->GTCCR[1] + 1 : 0; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
void ra_gpt_timer_init(uint32_t pwm_pin, uint32_t ch, uint8_t id, uint32_t duty, float freq) { |
| 528 |
R_GPT0_Type *gpt_reg = gpt_regs[ch]; |
| 529 |
|
| 530 |
if ((!gpt_reg) || (ch >= GPT_CH_SIZE)) { |
| 531 |
return; |
| 532 |
} |
| 533 |
|
| 534 |
#ifdef RA4M1 |
| 535 |
if (ch <= 1) { |
| 536 |
#elif defined(RA4W1) |
| 537 |
if (ch <= 3) { |
| 538 |
#elif defined(RA6M1) || defined(RA6M2) || defined(RA6M3) |
| 539 |
if (ch <= 7) { |
| 540 |
#elif defined(RA6M5) |
| 541 |
if (ch <= 9) { |
| 542 |
#else |
| 543 |
#error Choose proper clock enable BIT! |
| 544 |
#endif |
| 545 |
#ifdef RA6M5 |
| 546 |
// R_MSTP_MSTPCRE_MSTPE22_Msk - R_MSTP_MSTPCRE_MSTPE31_Msk |
| 547 |
ra_mstpcre_start(1UL << (31 - ch)); |
| 548 |
#else |
| 549 |
ra_mstpcrd_start(R_MSTP_MSTPCRD_MSTPD5_Msk); |
| 550 |
#endif |
| 551 |
} else { |
| 552 |
ra_mstpcrd_start(R_MSTP_MSTPCRD_MSTPD6_Msk); |
| 553 |
} |
| 554 |
|
| 555 |
ra_gpt_timer_stop(ch); // Stop the timer |
| 556 |
gpt_reg->GTCR_b.MD = 0; // Set operating mode |
| 557 |
gpt_reg->GTUDDTYC_b.UD = 1; // Set count direction |
| 558 |
gpt_reg->GTBER_b.PR = 1; // Set buffer operation for the period |
| 559 |
ra_gpt_timer_set_freq(ch, freq); // Set count clock & cycle |
| 560 |
|
| 561 |
gpt_reg->GTCNT = 0; // Set initial value for counter |
| 562 |
if (id == 'A') { |
| 563 |
gpt_reg->GTIOR_b.GTIOA = 0x9; // Set GTIOC pin function (Initial low -> Low at GTCCRA/B compare match -> High at cycle end) |
| 564 |
gpt_reg->GTIOR_b.OAE = 1; // Enable GTIOC pin output |
| 565 |
gpt_reg->GTBER_b.CCRA = 1; // Set buffer operation for the duty |
| 566 |
gpt_reg->GTCCR[0] = duty? duty - 1 : duty; // Set compare match value |
| 567 |
gpt_reg->GTCCR[2] = gpt_reg->GTCCR[0]; |
| 568 |
} else { |
| 569 |
gpt_reg->GTIOR_b.GTIOB = 0x9; |
| 570 |
gpt_reg->GTIOR_b.OBE = 1; |
| 571 |
gpt_reg->GTBER_b.CCRB = 1; |
| 572 |
gpt_reg->GTCCR[1] = duty? duty - 1 : duty; |
| 573 |
gpt_reg->GTCCR[3] = gpt_reg->GTCCR[1]; |
| 574 |
} |
| 575 |
|
| 576 |
ra_gpt_timer_set_pin(pwm_pin); |
| 577 |
|
| 578 |
if (gpt_reg->GTPR && gpt_reg->GTCCR[(id == 'A')? 0 : 1]) { |
| 579 |
ra_gpt_timer_start(ch); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
void ra_gpt_timer_deinit(uint32_t pwm_pin, uint32_t ch, uint8_t id) { |
| 584 |
if (CH_GAP || (ch >= GPT_CH_SIZE)) { |
| 585 |
return; |
| 586 |
} |
| 587 |
ra_gpt_timer_stop(ch); |
| 588 |
|
| 589 |
ra_gpt_div[ch] = 0; |
| 590 |
ra_gpt_freq[ch] = 0; |
| 591 |
|
| 592 |
gpt_regs[ch]->GTPR = 0; |
| 593 |
gpt_regs[ch]->GTPBR = 0; |
| 594 |
|
| 595 |
if (id == 'A') { |
| 596 |
gpt_regs[ch]->GTIOR_b.OAE = 0; |
| 597 |
gpt_regs[ch]->GTCCR[0] = 0; |
| 598 |
gpt_regs[ch]->GTCCR[2] = 0; |
| 599 |
} else { |
| 600 |
gpt_regs[ch]->GTIOR_b.OBE = 0; |
| 601 |
gpt_regs[ch]->GTCCR[1] = 0; |
| 602 |
gpt_regs[ch]->GTCCR[3] = 0; |
| 603 |
} |
| 604 |
|
| 605 |
#ifdef RA4M1 |
| 606 |
if (ch <= 1) { |
| 607 |
#elif defined(RA4W1) |
| 608 |
if (ch <= 3) { |
| 609 |
#elif defined(RA6M1) || defined(RA6M2) || defined(RA6M3) |
| 610 |
if (ch <= 7) { |
| 611 |
#elif defined(RA6M5) |
| 612 |
if (ch <= 9) { |
| 613 |
#else |
| 614 |
#error Choose proper clock enable BIT! |
| 615 |
#endif |
| 616 |
#ifdef RA6M5 |
| 617 |
ra_mstpcre_stop(1UL << (31 - ch)); |
| 618 |
#else |
| 619 |
ra_mstpcrd_stop(R_MSTP_MSTPCRD_MSTPD5_Msk); |
| 620 |
#endif |
| 621 |
} else { |
| 622 |
ra_mstpcrd_stop(R_MSTP_MSTPCRD_MSTPD6_Msk); |
| 623 |
} |
| 624 |
|
| 625 |
ra_gpt_timer_release_pin(pwm_pin); |
| 626 |
} |