| 1 |
/* |
| 2 |
* The MIT License (MIT) |
| 3 |
* |
| 4 |
* Copyright (c) 2021,2022 Renesas Electronics Corporation |
| 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 |
#include "hal_data.h" |
| 26 |
#include "ra_config.h" |
| 27 |
#include "ra_gpio.h" |
| 28 |
#include "ra_utils.h" |
| 29 |
#include "ra_spi.h" |
| 30 |
|
| 31 |
#if defined(__GNUC__) |
| 32 |
#pragma GCC diagnostic ignored "-Wunused-parameter" |
| 33 |
#pragma GCC diagnostic ignored "-Wconversion" |
| 34 |
// #pragma GCC diagnostic ignored "-Wshift-negative-value" |
| 35 |
// #pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
| 36 |
// #pragma GCC diagnostic ignored "-Wsequence-point" |
| 37 |
// #pragma GCC diagnostic ignored "-Wunused-function" |
| 38 |
#endif |
| 39 |
|
| 40 |
static R_SPI0_Type *spi_regs[] = { |
| 41 |
R_SPI0, |
| 42 |
R_SPI1, |
| 43 |
}; |
| 44 |
|
| 45 |
static const ra_af_pin_t mosi_pins[] = { |
| 46 |
#if defined(RA4M1) |
| 47 |
|
| 48 |
{ AF_SPI, 0, P101 }, /* MOSIA */ |
| 49 |
{ AF_SPI, 0, P411 }, /* MOSIA */ |
| 50 |
{ AF_SPI, 1, P109 }, /* MOSIB */ |
| 51 |
{ AF_SPI, 1, P203 }, /* MOSIB */ |
| 52 |
|
| 53 |
#elif defined(RA4W1) |
| 54 |
|
| 55 |
{ AF_SPI, 0, P101 }, /* MOSIA */ |
| 56 |
{ AF_SPI, 1, P109 }, /* MOSIB */ |
| 57 |
|
| 58 |
#elif defined(RA6M1) |
| 59 |
|
| 60 |
{ AF_SPI, 0, P101 }, /* MOSIA_A */ |
| 61 |
{ AF_SPI, 0, P411 }, /* MOSIA_B */ |
| 62 |
{ AF_SPI, 1, P109 }, /* MOSIB_B */ |
| 63 |
|
| 64 |
#elif defined(RA6M2) || defined(RA6M3) |
| 65 |
|
| 66 |
{ AF_SPI, 0, P101 }, /* MOSIA_A */ |
| 67 |
{ AF_SPI, 0, P411 }, /* MOSIA_B */ |
| 68 |
{ AF_SPI, 1, P109 }, /* MOSIB_B */ |
| 69 |
{ AF_SPI, 1, P203 }, /* MOSIB_A */ |
| 70 |
{ AF_SPI, 1, P701 }, /* MOSIB_C */ |
| 71 |
|
| 72 |
#elif defined(RA6M5) |
| 73 |
|
| 74 |
{ AF_SPI, 0, P109 }, /* MOSIA_B */ |
| 75 |
{ AF_SPI, 0, P203 }, /* MOSIA_A */ |
| 76 |
{ AF_SPI, 0, P701 }, /* MOSIA_C */ |
| 77 |
{ AF_SPI, 1, P101 }, /* MOSIB_A */ |
| 78 |
{ AF_SPI, 1, P411 }, /* MOSIB_B */ |
| 79 |
|
| 80 |
#else |
| 81 |
#error "CMSIS MCU Series is not specified." |
| 82 |
#endif |
| 83 |
}; |
| 84 |
#define MOSI_PINS_SIZE sizeof(mosi_pins) / sizeof(ra_af_pin_t) |
| 85 |
|
| 86 |
static const ra_af_pin_t miso_pins[] = { |
| 87 |
#if defined(RA4M1) |
| 88 |
|
| 89 |
{ AF_SPI, 0, P100 }, /* MISOA */ |
| 90 |
{ AF_SPI, 0, P410 }, /* MISOA */ |
| 91 |
{ AF_SPI, 1, P110 }, /* MISOB */ |
| 92 |
{ AF_SPI, 1, P202 }, /* MISOB */ |
| 93 |
|
| 94 |
#elif defined(RA4W1) |
| 95 |
|
| 96 |
{ AF_SPI, 0, P100 }, /* MISOA */ |
| 97 |
{ AF_SPI, 1, P110 }, /* MISOB */ |
| 98 |
|
| 99 |
#elif defined(RA6M1) |
| 100 |
|
| 101 |
{ AF_SPI, 0, P100 }, /* MISOA_A */ |
| 102 |
{ AF_SPI, 0, P410 }, /* MISOA_B */ |
| 103 |
{ AF_SPI, 1, P110 }, /* MISOB_B */ |
| 104 |
|
| 105 |
#elif defined(RA6M2) || defined(RA6M3) |
| 106 |
|
| 107 |
{ AF_SPI, 0, P100 }, /* MISOA_A */ |
| 108 |
{ AF_SPI, 0, P410 }, /* MISOA_B */ |
| 109 |
{ AF_SPI, 1, P110 }, /* MISOB_B */ |
| 110 |
{ AF_SPI, 1, P202 }, /* MISOB_A */ |
| 111 |
{ AF_SPI, 1, P700 }, /* MISOB_C */ |
| 112 |
|
| 113 |
#elif defined(RA6M5) |
| 114 |
|
| 115 |
{ AF_SPI, 0, P110 }, /* MISOA_B */ |
| 116 |
{ AF_SPI, 0, P202 }, /* MISOA_A */ |
| 117 |
{ AF_SPI, 0, P700 }, /* MISOA_C */ |
| 118 |
{ AF_SPI, 1, P100 }, /* MISOB_A */ |
| 119 |
{ AF_SPI, 1, P410 }, /* MISOB_B */ |
| 120 |
|
| 121 |
#else |
| 122 |
#error "CMSIS MCU Series is not specified." |
| 123 |
#endif |
| 124 |
}; |
| 125 |
#define MISO_PINS_SIZE sizeof(miso_pins) / sizeof(ra_af_pin_t) |
| 126 |
|
| 127 |
static const ra_af_pin_t sck_pins[] = { |
| 128 |
#if defined(RA4M1) |
| 129 |
|
| 130 |
{ AF_SPI, 0, P102 }, /* RSPCKA */ |
| 131 |
{ AF_SPI, 0, P412 }, /* RSPCKA */ |
| 132 |
{ AF_SPI, 1, P111 }, /* RSPCKB */ |
| 133 |
{ AF_SPI, 1, P204 }, /* RSPCKB */ |
| 134 |
|
| 135 |
#elif defined(RA4W1) |
| 136 |
|
| 137 |
{ AF_SPI, 0, P102 }, /* RSPCKA */ |
| 138 |
{ AF_SPI, 1, P111 }, /* RSPCKB */ |
| 139 |
|
| 140 |
#elif defined(RA6M1) |
| 141 |
|
| 142 |
{ AF_SPI, 0, P102 }, /* RSPCKA_A */ |
| 143 |
{ AF_SPI, 0, P412 }, /* RSPCKA_B */ |
| 144 |
{ AF_SPI, 1, P111 }, /* RSPCKB_B */ |
| 145 |
|
| 146 |
#elif defined(RA6M2) || defined(RA6M3) |
| 147 |
|
| 148 |
{ AF_SPI, 0, P102 }, /* RSPCKA_A */ |
| 149 |
{ AF_SPI, 0, P412 }, /* RSPCKA_B */ |
| 150 |
{ AF_SPI, 1, P111 }, /* RSPCKB_B */ |
| 151 |
{ AF_SPI, 1, P204 }, /* RSPCKB_A */ |
| 152 |
{ AF_SPI, 1, P702 }, /* RSPCKB_C */ |
| 153 |
|
| 154 |
#elif defined(RA6M5) |
| 155 |
|
| 156 |
{ AF_SPI, 0, P111 }, /* RSPCKA_B */ |
| 157 |
{ AF_SPI, 0, P204 }, /* RSPCKA_A */ |
| 158 |
{ AF_SPI, 0, P702 }, /* RSPCKA_C */ |
| 159 |
{ AF_SPI, 1, P102 }, /* RSPCKB_A */ |
| 160 |
{ AF_SPI, 1, P412 }, /* RSPCKB_B */ |
| 161 |
|
| 162 |
#else |
| 163 |
#error "CMSIS MCU Series is not specified." |
| 164 |
#endif |
| 165 |
}; |
| 166 |
#define SCK_PINS_SIZE sizeof(sck_pins) / sizeof(ra_af_pin_t) |
| 167 |
|
| 168 |
typedef struct ra_ssl_pin { |
| 169 |
uint8_t ssln; |
| 170 |
uint32_t pin; |
| 171 |
} ra_ssl_pin_t; |
| 172 |
|
| 173 |
static const ra_ssl_pin_t ssl_pins[] = { |
| 174 |
#if defined(RA4M1) |
| 175 |
|
| 176 |
{ 0, P103 }, /* SSLA0 */ |
| 177 |
{ 1, P104 }, /* SSLA1 */ |
| 178 |
{ 2, P105 }, /* SSLA2 */ |
| 179 |
{ 3, P106 }, /* SSLA3 */ |
| 180 |
{ 0, P108 }, /* SSLB0 */ |
| 181 |
{ 0, P112 }, /* SSLB0 */ |
| 182 |
|
| 183 |
{ 0, P205 }, /* SSLB0 */ |
| 184 |
{ 1, P206 }, /* SSLB1 */ |
| 185 |
|
| 186 |
{ 1, P300 }, /* SSLB1 */ |
| 187 |
{ 2, P301 }, /* SSLB2 */ |
| 188 |
{ 3, P302 }, /* SSLB3 */ |
| 189 |
|
| 190 |
{ 3, P407 }, /* SSLB3 */ |
| 191 |
{ 0, P413 }, /* SSLA0 */ |
| 192 |
{ 1, P414 }, /* SSLA1 */ |
| 193 |
{ 2, P415 }, /* SSLA2 */ |
| 194 |
|
| 195 |
#elif defined(RA4W1) |
| 196 |
|
| 197 |
{ 0, P103 }, /* SSLA0 */ |
| 198 |
{ 1, P104 }, /* SSLA1 */ |
| 199 |
{ 2, P105 }, /* SSLA2 */ |
| 200 |
{ 3, P106 }, /* SSLA3 */ |
| 201 |
{ 0, P108 }, /* SSLB0 */ |
| 202 |
|
| 203 |
{ 0, P205 }, /* SSLB0 */ |
| 204 |
{ 1, P206 }, /* SSLB1 */ |
| 205 |
|
| 206 |
{ 1, P300 }, /* SSLB1 */ |
| 207 |
|
| 208 |
{ 3, P407 }, /* SSLB3 */ |
| 209 |
{ 1, P414 }, /* SSLA1 */ |
| 210 |
|
| 211 |
#elif defined(RA6M1) |
| 212 |
|
| 213 |
{ 0, P103 }, /* SSLA0_A */ |
| 214 |
{ 1, P104 }, /* SSLA1_A */ |
| 215 |
{ 2, P105 }, /* SSLA2_A */ |
| 216 |
{ 3, P106 }, /* SSLA3_A */ |
| 217 |
{ 0, P108 }, /* SSLB0_B */ |
| 218 |
{ 0, P112 }, /* SSLB0_B */ |
| 219 |
|
| 220 |
{ 1, P300 }, /* SSLB1_B */ |
| 221 |
{ 2, P301 }, /* SSLB2_B */ |
| 222 |
{ 3, P302 }, /* SSLB3_B */ |
| 223 |
|
| 224 |
{ 0, P413 }, /* SSLA0_B */ |
| 225 |
{ 1, P414 }, /* SSLA1_B */ |
| 226 |
{ 2, P415 }, /* SSLA2_B */ |
| 227 |
|
| 228 |
{ 3, P708 }, /* SSLA3_B */ |
| 229 |
|
| 230 |
#elif defined(RA6M2) || defined(RA6M3) |
| 231 |
|
| 232 |
{ 0, P103 }, /* SSLA0_A */ |
| 233 |
{ 1, P104 }, /* SSLA1_A */ |
| 234 |
{ 2, P105 }, /* SSLA2_A */ |
| 235 |
{ 3, P106 }, /* SSLA3_A */ |
| 236 |
{ 0, P108 }, /* SSLB0_B */ |
| 237 |
{ 0, P112 }, /* SSLB0_B */ |
| 238 |
|
| 239 |
{ 0, P205 }, /* SSLB0_A */ |
| 240 |
{ 1, P206 }, /* SSLB1_A */ |
| 241 |
{ 2, P207 }, /* SSLB2_A */ |
| 242 |
|
| 243 |
{ 1, P300 }, /* SSLB1_B */ |
| 244 |
{ 2, P301 }, /* SSLB2_B */ |
| 245 |
{ 3, P302 }, /* SSLB3_B */ |
| 246 |
|
| 247 |
{ 3, P406 }, /* SSLB3_C */ |
| 248 |
{ 3, P407 }, /* SSLB3_A */ |
| 249 |
{ 0, P413 }, /* SSLA0_B */ |
| 250 |
{ 1, P414 }, /* SSLA1_B */ |
| 251 |
{ 2, P415 }, /* SSLA2_B */ |
| 252 |
|
| 253 |
{ 0, P703 }, /* SSLB0_C */ |
| 254 |
{ 1, P704 }, /* SSLB1_C */ |
| 255 |
{ 2, P705 }, /* SSLB2_C */ |
| 256 |
{ 3, P708 }, /* SSLA3_B */ |
| 257 |
|
| 258 |
#elif defined(RA6M5) |
| 259 |
|
| 260 |
{ 0, P103 }, /* SSLB0_A */ |
| 261 |
{ 1, P104 }, /* SSLB1_A */ |
| 262 |
{ 2, P105 }, /* SSLB2_A */ |
| 263 |
{ 3, P106 }, /* SSLB3_A */ |
| 264 |
{ 0, P108 }, /* SSLA0_B */ |
| 265 |
{ 0, P112 }, /* SSLA0_B */ |
| 266 |
|
| 267 |
{ 0, P205 }, /* SSLA0_A */ |
| 268 |
{ 1, P206 }, /* SSLA1_A */ |
| 269 |
{ 2, P207 }, /* SSLA2_A */ |
| 270 |
|
| 271 |
{ 1, P300 }, /* SSLA1_B */ |
| 272 |
{ 2, P301 }, /* SSLA2_B */ |
| 273 |
{ 3, P302 }, /* SSLA3_B */ |
| 274 |
|
| 275 |
{ 3, P406 }, /* SSLA3_C */ |
| 276 |
{ 3, P407 }, /* SSLA3_A */ |
| 277 |
{ 0, P413 }, /* SSLB0_B */ |
| 278 |
{ 1, P414 }, /* SSLB1_B */ |
| 279 |
{ 2, P415 }, /* SSLB2_B */ |
| 280 |
|
| 281 |
{ 0, P703 }, /* SSLA0_C */ |
| 282 |
{ 1, P704 }, /* SSLA1_C */ |
| 283 |
{ 2, P705 }, /* SSLA2_C */ |
| 284 |
{ 3, P708 }, /* SSLB3_B */ |
| 285 |
|
| 286 |
#else |
| 287 |
#error "CMSIS MCU Series is not specified." |
| 288 |
#endif |
| 289 |
}; |
| 290 |
#define SSL_PINS_SIZE sizeof(ssl_pins) / sizeof(ra_ssl_pin_t) |
| 291 |
|
| 292 |
bool ra_af_find_ch(ra_af_pin_t *af_pin, uint32_t size, uint32_t pin, uint8_t *ch) { |
| 293 |
bool find = false; |
| 294 |
uint32_t i; |
| 295 |
for (i = 0; i < size; i++) { |
| 296 |
if (af_pin->pin == pin) { |
| 297 |
find = true; |
| 298 |
*ch = af_pin->ch; |
| 299 |
break; |
| 300 |
} |
| 301 |
af_pin++; |
| 302 |
} |
| 303 |
return find; |
| 304 |
} |
| 305 |
|
| 306 |
bool ra_spi_find_af_ch(uint32_t mosi, uint32_t miso, uint32_t sck, uint8_t *ch) { |
| 307 |
bool find = false; |
| 308 |
uint8_t mosi_ch; |
| 309 |
uint8_t miso_ch; |
| 310 |
uint8_t sck_ch; |
| 311 |
find = ra_af_find_ch((ra_af_pin_t *)&mosi_pins, MOSI_PINS_SIZE, mosi, &mosi_ch); |
| 312 |
if (find) { |
| 313 |
find = ra_af_find_ch((ra_af_pin_t *)&miso_pins, MISO_PINS_SIZE, miso, &miso_ch); |
| 314 |
if (find) { |
| 315 |
find = ra_af_find_ch((ra_af_pin_t *)&sck_pins, SCK_PINS_SIZE, sck, &sck_ch); |
| 316 |
if (find) { |
| 317 |
find = (mosi_ch == miso_ch) && (miso_ch == sck_ch); |
| 318 |
if (find) { |
| 319 |
*ch = mosi_ch; |
| 320 |
} else { |
| 321 |
*ch = 0; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
return find; |
| 327 |
} |
| 328 |
|
| 329 |
static bool ra_spi_pin_to_ssln(uint32_t pin, uint8_t *ssln) { |
| 330 |
ra_ssl_pin_t *ssl_pin = (ra_ssl_pin_t *)&ssl_pins; |
| 331 |
bool find = false; |
| 332 |
uint32_t i; |
| 333 |
for (i = 0; i < SSL_PINS_SIZE; i++) { |
| 334 |
if (ssl_pin->pin == pin) { |
| 335 |
find = true; |
| 336 |
if (find) { |
| 337 |
*ssln = ssl_pin->ssln; |
| 338 |
} else { |
| 339 |
*ssln = 0; |
| 340 |
} |
| 341 |
break; |
| 342 |
} |
| 343 |
ssl_pin++; |
| 344 |
} |
| 345 |
return find; |
| 346 |
} |
| 347 |
|
| 348 |
static void ra_spi_module_start(uint32_t ch) { |
| 349 |
if (ch == 0) { |
| 350 |
ra_mstpcrb_start(R_MSTP_MSTPCRB_MSTPB19_Msk); |
| 351 |
} else { |
| 352 |
ra_mstpcrb_start(R_MSTP_MSTPCRB_MSTPB18_Msk); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
static void ra_spi_module_stop(uint32_t ch) { |
| 357 |
if (ch == 0) { |
| 358 |
ra_mstpcrb_stop(R_MSTP_MSTPCRB_MSTPB19_Msk); |
| 359 |
} else { |
| 360 |
ra_mstpcrb_stop(R_MSTP_MSTPCRB_MSTPB18_Msk); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
static void ra_spi_set_pin(uint32_t pin, bool miso) { |
| 365 |
if (miso) { |
| 366 |
ra_gpio_config(pin, GPIO_MODE_INPUT, GPIO_PULLUP, GPIO_LOW_POWER, AF_SPI); |
| 367 |
} else { |
| 368 |
ra_gpio_config(pin, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_LOW_POWER, AF_SPI); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
static void ra_spi_set_bits(uint32_t ch, uint32_t bits) { |
| 373 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 374 |
if (bits == 8) { |
| 375 |
spi_reg->SPDCR_b.SPBYT = 1; |
| 376 |
spi_reg->SPDCR_b.SPLW = 0; |
| 377 |
spi_reg->SPCMD_b[0].SPB = 0x7; |
| 378 |
} else if (bits == 16) { |
| 379 |
spi_reg->SPDCR_b.SPBYT = 0; |
| 380 |
spi_reg->SPDCR_b.SPLW = 0; |
| 381 |
spi_reg->SPCMD_b[0].SPB = 0xf; |
| 382 |
} else if (bits == 32) { |
| 383 |
spi_reg->SPDCR_b.SPBYT = 0; |
| 384 |
spi_reg->SPDCR_b.SPLW = 1; |
| 385 |
spi_reg->SPCMD_b[0].SPB = 0x3; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
static void ra_spi_set_lsb_first(uint32_t ch, uint32_t lsb_first) { |
| 390 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 391 |
if (lsb_first) { |
| 392 |
spi_reg->SPCMD_b[0].LSBF = 1; // LSB first |
| 393 |
} else { |
| 394 |
spi_reg->SPCMD_b[0].LSBF = 0; // MSB first |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
static void ra_spi_set_mode(uint32_t ch, uint32_t polarity, uint32_t phase) { |
| 399 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 400 |
if (polarity != 0) { |
| 401 |
// CPOL(Clock Polarity) |
| 402 |
spi_reg->SPCMD_b[0].CPOL = 1; |
| 403 |
} else { |
| 404 |
spi_reg->SPCMD_b[0].CPOL = 0; |
| 405 |
} |
| 406 |
if (phase != 0) { |
| 407 |
// CPHA(Clock Phase) |
| 408 |
spi_reg->SPCMD_b[0].CPHA = 1; |
| 409 |
} else { |
| 410 |
spi_reg->SPCMD_b[0].CPHA = 0; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
uint8_t ra_spi_write_byte(uint32_t ch, uint8_t b) { |
| 415 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 416 |
while (spi_reg->SPSR_b.SPTEF == 0) { |
| 417 |
; |
| 418 |
} |
| 419 |
spi_reg->SPDR_BY = (uint8_t)(b); |
| 420 |
while (spi_reg->SPSR_b.SPRF == 0) { |
| 421 |
; |
| 422 |
} |
| 423 |
return (uint8_t)(spi_reg->SPDR_BY); |
| 424 |
} |
| 425 |
|
| 426 |
void ra_spi_write_bytes8(uint32_t ch, uint8_t *buf, uint32_t count) { |
| 427 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 428 |
while (count-- > 0) { |
| 429 |
while (spi_reg->SPSR_b.SPTEF == 0) { |
| 430 |
; |
| 431 |
} |
| 432 |
spi_reg->SPDR_BY = (uint8_t)(*buf++); |
| 433 |
while (spi_reg->SPSR_b.SPRF == 0) { |
| 434 |
; |
| 435 |
} |
| 436 |
spi_reg->SPDR_BY; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
void ra_spi_read_bytes8(uint32_t ch, uint8_t *buf, uint32_t count) { |
| 441 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 442 |
while (count-- > 0) { |
| 443 |
while (spi_reg->SPSR_b.SPTEF == 0) { |
| 444 |
; |
| 445 |
} |
| 446 |
spi_reg->SPDR_BY = (uint8_t)(0); |
| 447 |
while (spi_reg->SPSR_b.SPRF == 0) { |
| 448 |
; |
| 449 |
} |
| 450 |
*buf++ = (uint8_t)spi_reg->SPDR_BY; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
void ra_spi_write_bytes16(uint32_t ch, uint16_t *buf, uint32_t count) { |
| 455 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 456 |
while (count-- > 0) { |
| 457 |
while (spi_reg->SPSR_b.SPTEF == 0) { |
| 458 |
; |
| 459 |
} |
| 460 |
spi_reg->SPDR_HA = (uint16_t)(*buf++); |
| 461 |
while (spi_reg->SPSR_b.SPRF == 0) { |
| 462 |
; |
| 463 |
} |
| 464 |
spi_reg->SPDR_HA; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
void ra_spi_write_bytes32(uint32_t ch, uint32_t *buf, uint32_t count) { |
| 469 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 470 |
while (count-- > 0) { |
| 471 |
while (spi_reg->SPSR_b.SPTEF == 0) { |
| 472 |
; |
| 473 |
} |
| 474 |
spi_reg->SPDR = (uint32_t)(*buf++); |
| 475 |
while (spi_reg->SPSR_b.SPRF == 0) { |
| 476 |
; |
| 477 |
} |
| 478 |
spi_reg->SPDR; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
void ra_spi_write_bytes(uint32_t ch, uint32_t bits, uint8_t *buf, uint32_t count) { |
| 483 |
if (bits == 8) { |
| 484 |
ra_spi_write_bytes8(ch, buf, count); |
| 485 |
} else if (bits == 16) { |
| 486 |
ra_spi_write_bytes16(ch, (uint16_t *)buf, count >> 1); |
| 487 |
} else if (bits == 32) { |
| 488 |
ra_spi_write_bytes32(ch, (uint32_t *)buf, count >> 2); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
void ra_spi_transfer8(uint32_t ch, uint8_t *dst, uint8_t *src, uint32_t count) { |
| 493 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 494 |
while (count-- > 0) { |
| 495 |
while (spi_reg->SPSR_b.SPTEF == 0) { |
| 496 |
; |
| 497 |
} |
| 498 |
spi_reg->SPDR_BY = (uint8_t)(*src++); |
| 499 |
while (spi_reg->SPSR_b.SPRF == 0) { |
| 500 |
; |
| 501 |
} |
| 502 |
if (dst) { |
| 503 |
*dst++ = (uint8_t)(spi_reg->SPDR_BY); |
| 504 |
} else { |
| 505 |
spi_reg->SPDR_BY; |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
void ra_spi_transfer16(uint32_t ch, uint16_t *dst, uint16_t *src, uint32_t count) { |
| 511 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 512 |
while (count-- > 0) { |
| 513 |
while (spi_reg->SPSR_b.SPTEF == 0) { |
| 514 |
; |
| 515 |
} |
| 516 |
spi_reg->SPDR_HA = (uint16_t)(*src++); |
| 517 |
while (spi_reg->SPSR_b.SPRF == 0) { |
| 518 |
; |
| 519 |
} |
| 520 |
if (dst) { |
| 521 |
*dst++ = (uint16_t)(spi_reg->SPDR_HA); |
| 522 |
} else { |
| 523 |
spi_reg->SPDR_HA; |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
void ra_spi_transfer32(uint32_t ch, uint32_t *dst, uint32_t *src, uint32_t count) { |
| 529 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 530 |
while (count-- > 0) { |
| 531 |
while (spi_reg->SPSR_b.SPTEF == 0) { |
| 532 |
; |
| 533 |
} |
| 534 |
spi_reg->SPDR = (uint32_t)(*src++); |
| 535 |
while (spi_reg->SPSR_b.SPRF == 0) { |
| 536 |
; |
| 537 |
} |
| 538 |
if (dst) { |
| 539 |
*dst++ = (uint32_t)(spi_reg->SPDR); |
| 540 |
} else { |
| 541 |
spi_reg->SPDR; |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
void ra_spi_transfer(uint32_t ch, uint32_t bits, uint8_t *dst, uint8_t *src, uint32_t count, uint32_t timeout) { |
| 547 |
if (bits == 8) { |
| 548 |
ra_spi_transfer8(ch, dst, src, count); |
| 549 |
} else if (bits == 16) { |
| 550 |
ra_spi_transfer16(ch, (uint16_t *)dst, (uint16_t *)src, count >> 1); |
| 551 |
} else if (bits == 32) { |
| 552 |
ra_spi_transfer32(ch, (uint32_t *)dst, (uint32_t *)src, count >> 2); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
void ra_spi_start_xfer(uint32_t ch, uint16_t spcmd, uint8_t spbr) { |
| 557 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 558 |
spi_reg->SPCR_b.SPE = 0; // disable |
| 559 |
spi_reg->SPCMD[0] = spcmd; |
| 560 |
spi_reg->SPBR = spbr; |
| 561 |
spi_reg->SPCR_b.SPE = 1; // enable |
| 562 |
} |
| 563 |
|
| 564 |
void ra_spi_end_xfer(uint32_t ch) { |
| 565 |
(void)ch; |
| 566 |
} |
| 567 |
|
| 568 |
void ra_spi_get_conf(uint32_t ch, uint16_t *spcmd, uint8_t *spbr) { |
| 569 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 570 |
*spcmd = spi_reg->SPCMD[0]; |
| 571 |
*spbr = spi_reg->SPBR; |
| 572 |
} |
| 573 |
|
| 574 |
void ra_spi_init(uint32_t ch, uint32_t mosi, uint32_t miso, uint32_t sck, uint32_t cs, uint32_t baud, uint32_t bits, uint32_t polarity, uint32_t phase, uint32_t firstbit) { |
| 575 |
R_SPI0_Type *spi_reg = (R_SPI0_Type *)spi_regs[ch]; |
| 576 |
uint8_t ssln = 0; |
| 577 |
uint8_t sslp = 0; |
| 578 |
|
| 579 |
ra_spi_pin_to_ssln(cs, &ssln); |
| 580 |
sslp &= ~0x0fU; |
| 581 |
sslp |= (uint8_t)polarity << ssln; |
| 582 |
|
| 583 |
ra_spi_module_start(ch); |
| 584 |
|
| 585 |
spi_reg->SPSR; // dummy read to clear OVRF |
| 586 |
spi_reg->SPSR = 0xa0; // clear all status |
| 587 |
|
| 588 |
spi_reg->SPCR = 0x00; // disable SPI |
| 589 |
spi_reg->SSLP = sslp; // select slave active polarity |
| 590 |
spi_reg->SPPCR = 0x00; // fixed idle value, disable loop-back mode |
| 591 |
spi_reg->SPBR = (PCLK / 2 / baud) - 1; // Set baudrate |
| 592 |
spi_reg->SPDCR = 0x40; // SPBYT=1, SPLW=0 byte access |
| 593 |
spi_reg->SPCKD = 0x00; // 1RSPCK |
| 594 |
spi_reg->SPDCR = 0x00; // 1RSPCK |
| 595 |
spi_reg->SPND = 0x00; // 1RSPCK + 2PCLKA |
| 596 |
spi_reg->SPCR2 = 0x10; // SCKASE=1 |
| 597 |
spi_reg->SPSCR = 0x00; // Disable sequence control |
| 598 |
spi_reg->SPCMD[0] = (0xe700 | (ssln << 4)); // LSBF=0, SPB=7, SSLA:ssln, BRDV=0, CPOL=0, CPHA=0 |
| 599 |
spi_reg->SPDCR2 = 0x00; // BYSW=0, SINV=0(RA6M5) |
| 600 |
|
| 601 |
/* set other setting */ |
| 602 |
ra_spi_set_mode(ch, polarity, phase); |
| 603 |
ra_spi_set_bits(ch, bits); |
| 604 |
ra_spi_set_lsb_first(ch, firstbit); |
| 605 |
|
| 606 |
/* NIVC, DMAC setting */ |
| 607 |
|
| 608 |
/* I/O port setting */ |
| 609 |
ra_spi_set_pin(mosi, false); |
| 610 |
ra_spi_set_pin(miso, true); |
| 611 |
ra_spi_set_pin(sck, false); |
| 612 |
ra_spi_set_pin(cs, false); |
| 613 |
|
| 614 |
#if defined(RA6M5) |
| 615 |
spi_reg->SPCR3 = 0x00; // default |
| 616 |
#endif |
| 617 |
|
| 618 |
spi_reg->SPCR = 0x48; // Start SPI in master mode |
| 619 |
spi_reg->SPCR; // wait for completion |
| 620 |
|
| 621 |
return; |
| 622 |
} |
| 623 |
|
| 624 |
void ra_spi_deinit(uint32_t ch, uint32_t cs) { |
| 625 |
ra_spi_module_stop(ch); |
| 626 |
} |
| 627 |
|
| 628 |
__WEAK void spi_rxi_isr(void) { |
| 629 |
// dummy |
| 630 |
} |
| 631 |
|
| 632 |
__WEAK void spi_txi_isr(void) { |
| 633 |
// dummy |
| 634 |
} |
| 635 |
|
| 636 |
__WEAK void spi_tei_isr(void) { |
| 637 |
// dummy |
| 638 |
} |
| 639 |
|
| 640 |
__WEAK void spi_eri_isr(void) { |
| 641 |
// dummy |
| 642 |
} |