spi_master.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* Copyright 2020 Nick Brassel (tzarc)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include "spi_master.h"
  17. #include "timer.h"
  18. static bool spiStarted = false;
  19. #if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE
  20. static pin_t current_slave_pin = NO_PIN;
  21. static bool current_cs_active_low = true;
  22. #endif
  23. static SPIConfig spiConfig;
  24. static inline void spi_select(void) {
  25. spiSelect(&SPI_DRIVER);
  26. #if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE
  27. if (current_slave_pin != NO_PIN) {
  28. gpio_write_pin(current_slave_pin, current_cs_active_low ? 0 : 1);
  29. }
  30. #endif
  31. }
  32. static inline void spi_unselect(void) {
  33. #if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE
  34. if (current_slave_pin != NO_PIN) {
  35. gpio_write_pin(current_slave_pin, current_cs_active_low ? 1 : 0);
  36. }
  37. #endif
  38. spiUnselect(&SPI_DRIVER);
  39. }
  40. __attribute__((weak)) void spi_init(void) {
  41. static bool is_initialised = false;
  42. if (!is_initialised) {
  43. is_initialised = true;
  44. // Try releasing special pins for a short time
  45. gpio_set_pin_input(SPI_SCK_PIN);
  46. if (SPI_MOSI_PIN != NO_PIN) {
  47. gpio_set_pin_input(SPI_MOSI_PIN);
  48. }
  49. if (SPI_MISO_PIN != NO_PIN) {
  50. gpio_set_pin_input(SPI_MISO_PIN);
  51. }
  52. chThdSleepMilliseconds(10);
  53. #if defined(USE_GPIOV1)
  54. palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), SPI_SCK_PAL_MODE);
  55. if (SPI_MOSI_PIN != NO_PIN) {
  56. palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_PAL_MODE);
  57. }
  58. if (SPI_MISO_PIN != NO_PIN) {
  59. palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_PAL_MODE);
  60. }
  61. #else
  62. palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), SPI_SCK_FLAGS);
  63. if (SPI_MOSI_PIN != NO_PIN) {
  64. palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_FLAGS);
  65. }
  66. if (SPI_MISO_PIN != NO_PIN) {
  67. palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_FLAGS);
  68. }
  69. #endif
  70. spiStop(&SPI_DRIVER);
  71. spiStarted = false;
  72. }
  73. }
  74. bool spi_start_extended(spi_start_config_t *start_config) {
  75. #if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
  76. spiAcquireBus(&SPI_DRIVER);
  77. #endif // (SPI_USE_MUTUAL_EXCLUSION == TRUE)
  78. if (spiStarted) {
  79. return false;
  80. }
  81. #if SPI_SELECT_MODE != SPI_SELECT_MODE_NONE
  82. if (start_config->slave_pin == NO_PIN) {
  83. return false;
  84. }
  85. #endif
  86. #if !(defined(WB32F3G71xx) || defined(WB32FQ95xx))
  87. uint16_t roundedDivisor = 2;
  88. while (roundedDivisor < start_config->divisor) {
  89. roundedDivisor <<= 1;
  90. }
  91. # if defined(AT32F415)
  92. if (roundedDivisor < 2 || roundedDivisor > 1024) {
  93. return false;
  94. }
  95. # else
  96. if (roundedDivisor < 2 || roundedDivisor > 256) {
  97. return false;
  98. }
  99. # endif
  100. #endif
  101. #if defined(K20x) || defined(KL2x)
  102. spiConfig.tar0 = SPIx_CTARn_FMSZ(7) | SPIx_CTARn_ASC(1);
  103. if (start_config->lsb_first) {
  104. spiConfig.tar0 |= SPIx_CTARn_LSBFE;
  105. }
  106. switch (start_config->mode) {
  107. case 0:
  108. break;
  109. case 1:
  110. spiConfig.tar0 |= SPIx_CTARn_CPHA;
  111. break;
  112. case 2:
  113. spiConfig.tar0 |= SPIx_CTARn_CPOL;
  114. break;
  115. case 3:
  116. spiConfig.tar0 |= SPIx_CTARn_CPHA | SPIx_CTARn_CPOL;
  117. break;
  118. }
  119. switch (roundedDivisor) {
  120. case 2:
  121. spiConfig.tar0 |= SPIx_CTARn_BR(0);
  122. break;
  123. case 4:
  124. spiConfig.tar0 |= SPIx_CTARn_BR(1);
  125. break;
  126. case 8:
  127. spiConfig.tar0 |= SPIx_CTARn_BR(3);
  128. break;
  129. case 16:
  130. spiConfig.tar0 |= SPIx_CTARn_BR(4);
  131. break;
  132. case 32:
  133. spiConfig.tar0 |= SPIx_CTARn_BR(5);
  134. break;
  135. case 64:
  136. spiConfig.tar0 |= SPIx_CTARn_BR(6);
  137. break;
  138. case 128:
  139. spiConfig.tar0 |= SPIx_CTARn_BR(7);
  140. break;
  141. case 256:
  142. spiConfig.tar0 |= SPIx_CTARn_BR(8);
  143. break;
  144. }
  145. #elif defined(HT32)
  146. spiConfig.cr0 = SPI_CR0_SELOEN;
  147. spiConfig.cr1 = SPI_CR1_MODE | 8; // 8 bits and in master mode
  148. if (start_config->lsb_first) {
  149. spiConfig.cr1 |= SPI_CR1_FIRSTBIT;
  150. }
  151. switch (start_config->mode) {
  152. case 0:
  153. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE0;
  154. break;
  155. case 1:
  156. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE1;
  157. break;
  158. case 2:
  159. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE2;
  160. break;
  161. case 3:
  162. spiConfig.cr1 |= SPI_CR1_FORMAT_MODE3;
  163. break;
  164. }
  165. spiConfig.cpr = (roundedDivisor - 1) >> 1;
  166. #elif defined(WB32F3G71xx) || defined(WB32FQ95xx)
  167. if (!start_config->lsb_first) {
  168. osalDbgAssert(start_config->lsb_first != FALSE, "unsupported lsb_first");
  169. }
  170. if (start_config->divisor < 1) {
  171. return false;
  172. }
  173. spiConfig.SPI_BaudRatePrescaler = (start_config->divisor << 2);
  174. switch (start_config->mode) {
  175. case 0:
  176. spiConfig.SPI_CPHA = SPI_CPHA_1Edge;
  177. spiConfig.SPI_CPOL = SPI_CPOL_Low;
  178. break;
  179. case 1:
  180. spiConfig.SPI_CPHA = SPI_CPHA_2Edge;
  181. spiConfig.SPI_CPOL = SPI_CPOL_Low;
  182. break;
  183. case 2:
  184. spiConfig.SPI_CPHA = SPI_CPHA_1Edge;
  185. spiConfig.SPI_CPOL = SPI_CPOL_High;
  186. break;
  187. case 3:
  188. spiConfig.SPI_CPHA = SPI_CPHA_2Edge;
  189. spiConfig.SPI_CPOL = SPI_CPOL_High;
  190. break;
  191. }
  192. #elif defined(MCU_RP)
  193. if (start_config->lsb_first) {
  194. osalDbgAssert(start_config->lsb_first == false, "RP2040s PrimeCell SPI implementation does not support sending LSB first.");
  195. }
  196. // Motorola frame format and 8bit transfer data size.
  197. spiConfig.SSPCR0 = SPI_SSPCR0_FRF_MOTOROLA | SPI_SSPCR0_DSS_8BIT;
  198. // Serial output clock = (ck_sys or ck_peri) / (SSPCPSR->CPSDVSR * (1 +
  199. // SSPCR0->SCR)). SCR is always set to zero, as QMK SPI API expects the
  200. // passed divisor to be the only value to divide the input clock by.
  201. spiConfig.SSPCPSR = roundedDivisor; // Even number from 2 to 254
  202. switch (start_config->mode) {
  203. case 0:
  204. spiConfig.SSPCR0 &= ~SPI_SSPCR0_SPO; // Clock polarity: low
  205. spiConfig.SSPCR0 &= ~SPI_SSPCR0_SPH; // Clock phase: sample on first edge
  206. break;
  207. case 1:
  208. spiConfig.SSPCR0 &= ~SPI_SSPCR0_SPO; // Clock polarity: low
  209. spiConfig.SSPCR0 |= SPI_SSPCR0_SPH; // Clock phase: sample on second edge transition
  210. break;
  211. case 2:
  212. spiConfig.SSPCR0 |= SPI_SSPCR0_SPO; // Clock polarity: high
  213. spiConfig.SSPCR0 &= ~SPI_SSPCR0_SPH; // Clock phase: sample on first edge
  214. break;
  215. case 3:
  216. spiConfig.SSPCR0 |= SPI_SSPCR0_SPO; // Clock polarity: high
  217. spiConfig.SSPCR0 |= SPI_SSPCR0_SPH; // Clock phase: sample on second edge transition
  218. break;
  219. }
  220. #elif defined(AT32F415)
  221. spiConfig.ctrl1 = 0;
  222. if (lsbFirst) {
  223. spiConfig.ctrl1 |= SPI_CTRL1_LTF;
  224. }
  225. switch (mode) {
  226. case 0:
  227. break;
  228. case 1:
  229. spiConfig.ctrl1 |= SPI_CTRL1_CLKPHA;
  230. break;
  231. case 2:
  232. spiConfig.ctrl1 |= SPI_CTRL1_CLKPOL;
  233. break;
  234. case 3:
  235. spiConfig.ctrl1 |= SPI_CTRL1_CLKPHA | SPI_CTRL1_CLKPOL;
  236. break;
  237. }
  238. switch (roundedDivisor) {
  239. case 2:
  240. break;
  241. case 4:
  242. spiConfig.ctrl1 |= SPI_CTRL1_MDIV_0;
  243. break;
  244. case 8:
  245. spiConfig.ctrl1 |= SPI_CTRL1_MDIV_1;
  246. break;
  247. case 16:
  248. spiConfig.ctrl1 |= SPI_CTRL1_MDIV_1 | SPI_CTRL1_MDIV_0;
  249. break;
  250. case 32:
  251. spiConfig.ctrl1 |= SPI_CTRL1_MDIV_2;
  252. break;
  253. case 64:
  254. spiConfig.ctrl1 |= SPI_CTRL1_MDIV_2 | SPI_CTRL1_MDIV_0;
  255. break;
  256. case 128:
  257. spiConfig.ctrl1 |= SPI_CTRL1_MDIV_2 | SPI_CTRL1_MDIV_1;
  258. break;
  259. case 256:
  260. spiConfig.ctrl1 |= SPI_CTRL1_MDIV_2 | SPI_CTRL1_MDIV_1 | SPI_CTRL1_MDIV_0;
  261. break;
  262. case 512:
  263. spiConfig.ctrl2 |= SPI_CTRL1_MDIV_3;
  264. break;
  265. case 1024:
  266. spiConfig.ctrl2 |= SPI_CTRL1_MDIV_3;
  267. spiConfig.ctrl1 |= SPI_CTRL1_MDIV_0;
  268. break;
  269. }
  270. #else
  271. spiConfig.cr1 = 0;
  272. if (start_config->lsb_first) {
  273. spiConfig.cr1 |= SPI_CR1_LSBFIRST;
  274. }
  275. switch (start_config->mode) {
  276. case 0:
  277. break;
  278. case 1:
  279. spiConfig.cr1 |= SPI_CR1_CPHA;
  280. break;
  281. case 2:
  282. spiConfig.cr1 |= SPI_CR1_CPOL;
  283. break;
  284. case 3:
  285. spiConfig.cr1 |= SPI_CR1_CPHA | SPI_CR1_CPOL;
  286. break;
  287. }
  288. switch (roundedDivisor) {
  289. case 2:
  290. break;
  291. case 4:
  292. spiConfig.cr1 |= SPI_CR1_BR_0;
  293. break;
  294. case 8:
  295. spiConfig.cr1 |= SPI_CR1_BR_1;
  296. break;
  297. case 16:
  298. spiConfig.cr1 |= SPI_CR1_BR_1 | SPI_CR1_BR_0;
  299. break;
  300. case 32:
  301. spiConfig.cr1 |= SPI_CR1_BR_2;
  302. break;
  303. case 64:
  304. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_0;
  305. break;
  306. case 128:
  307. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1;
  308. break;
  309. case 256:
  310. spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0;
  311. break;
  312. }
  313. #endif
  314. spiStarted = true;
  315. #if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE
  316. current_slave_pin = start_config->slave_pin;
  317. current_cs_active_low = start_config->cs_active_low;
  318. #endif
  319. #if SPI_SELECT_MODE == SPI_SELECT_MODE_PAD
  320. spiConfig.ssport = PAL_PORT(start_config->slave_pin);
  321. spiConfig.sspad = PAL_PAD(start_config->slave_pin);
  322. gpio_set_pin_output(start_config->slave_pin);
  323. #elif SPI_SELECT_MODE == SPI_SELECT_MODE_NONE
  324. if (start_config->slave_pin != NO_PIN) {
  325. gpio_set_pin_output(start_config->slave_pin);
  326. }
  327. #else
  328. # error "Unsupported SPI_SELECT_MODE"
  329. #endif
  330. spiStart(&SPI_DRIVER, &spiConfig);
  331. spi_select();
  332. return true;
  333. }
  334. bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
  335. spi_start_config_t start_config = {0};
  336. start_config.slave_pin = slavePin;
  337. start_config.lsb_first = lsbFirst;
  338. start_config.mode = mode;
  339. start_config.divisor = divisor;
  340. start_config.cs_active_low = true;
  341. return spi_start_extended(&start_config);
  342. }
  343. spi_status_t spi_write(uint8_t data) {
  344. uint8_t rxData;
  345. spiExchange(&SPI_DRIVER, 1, &data, &rxData);
  346. return rxData;
  347. }
  348. spi_status_t spi_read(void) {
  349. uint8_t data = 0;
  350. spiReceive(&SPI_DRIVER, 1, &data);
  351. return data;
  352. }
  353. spi_status_t spi_transmit(const uint8_t *data, uint16_t length) {
  354. spiSend(&SPI_DRIVER, length, data);
  355. return SPI_STATUS_SUCCESS;
  356. }
  357. spi_status_t spi_receive(uint8_t *data, uint16_t length) {
  358. spiReceive(&SPI_DRIVER, length, data);
  359. return SPI_STATUS_SUCCESS;
  360. }
  361. void spi_stop(void) {
  362. if (spiStarted) {
  363. spi_unselect();
  364. spiStop(&SPI_DRIVER);
  365. spiStarted = false;
  366. }
  367. #if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
  368. spiReleaseBus(&SPI_DRIVER);
  369. #endif // (SPI_USE_MUTUAL_EXCLUSION == TRUE)
  370. }