Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

787 righe
22 KiB

  1. /*----------------------------------------------------------------------------*/
  2. //
  3. //
  4. // WiringPi KHADAS-EDGE Board Control file (ROCKCHIP 64Bits Platform)
  5. //
  6. //
  7. /*----------------------------------------------------------------------------*/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdint.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <fcntl.h>
  15. #include <sys/ioctl.h>
  16. #include <asm/ioctl.h>
  17. #include <sys/mman.h>
  18. #include <sys/utsname.h>
  19. /*----------------------------------------------------------------------------*/
  20. #include "softPwm.h"
  21. #include "softTone.h"
  22. /*----------------------------------------------------------------------------*/
  23. #include "wiringPi.h"
  24. #include "khadas_edge.h"
  25. /*----------------------------------------------------------------------------*/
  26. // wiringPi gpio map define
  27. /*----------------------------------------------------------------------------*/
  28. // Revision 2:
  29. static const int pinToGpio[64] = {
  30. // wiringPi number to native gpio number
  31. 120, 121, // 0 | 1 : GPIO3_D0 | GPIO3_D1
  32. 122, 123, // 2 | 3 : GPIO3_D2 | GPIO3_D3
  33. 124, 125, // 4 | 5 : GPIO3_D4 | GPIO3_D5
  34. 126, 127, // 6 | 7 : GPIO3_D6 | GPIO3_D7
  35. 64, 65, // 8 | 9 : (GPIO2_A0) I2C2_SDA | I2C2_SCL (GPIO2_A1)
  36. 50, 128, // 10 | 11 : (GPIO1_C2) SPI3_CS | I2S_CLK (GPIO4_A0)
  37. 47, 48, // 12 | 13 : (GPIO1_B7) SPI3_TXD | SPI3_RXD (GPIO1_C0)
  38. 49, -1, // 14 | 15 : (GPIO1_C1) SPI3_CLK | UART_TX
  39. -1, 112, // 16 | 17 : UART_RX | SPDIF_TX (GPIO3_C0)
  40. -1, -1, // 18 | 19 :
  41. -1, -1, // 20 | 21 :
  42. -1, -1, // 22 | 23 :
  43. -1, -1, // 24 | 25 : | ADC_IN2
  44. -1, -1, // 26 | 27 :
  45. -1, -1, // 28 | 29 : | ADC_IN3
  46. -1, -1, // 30 | 31 :
  47. // Padding:
  48. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32...47
  49. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 48...63
  50. };
  51. static const int phyToGpio[64] = {
  52. // physical header pin number to native gpio number
  53. -1, // 0
  54. -1, -1, // 1 | 21 : 5V | GND
  55. -1, 48, // 2 | 22 : 5V | SPI3_RXD (GPIO1_C0)
  56. -1, 47, // 3 | 23 : HOST1_DM | SPI3_TXD (GPIO1_B7)
  57. -1, -1, // 4 | 24 : HOST1_DP | GND
  58. -1, 65, // 5 | 25 : GND | I2C2_SCL (GPIO2_A1)
  59. -1, 64, // 6 | 26 : 3.3V | I2C2_SDA (GPIO2_A0)
  60. -1, -1, // 7 | 27 : MCU_NRST | 3.3V
  61. -1, -1, // 8 | 28 : MCU_SWIM | GND
  62. -1,120, // 9 | 29 : GND | I2S0_SCLK (GPIO3_D0)
  63. -1,128, // 10 | 30 : ADC_IN2 | I2S_CLK (GPIO4_A0)
  64. -1,127, // 11 | 31 : 1.8V | I2S0_SDO0 (GPIO3_D7)
  65. -1,122, // 12 | 32 : ADC_IN3 | I2S0_LRCK_TX (GPIO3_D2)
  66. 112,123, // 13 | 33 : (GPIO3_C0) SPDIF_TX | I2S0_SDI0 (GPIO3_D3)
  67. -1, -1, // 14 | 34 : GND | GND
  68. 50,126, // 15 | 35 : (GPIO1_C2) SPI3_CS | I2S0_SDI3SDO1 (GPIO3_D6)
  69. 49,125, // 16 | 36 : (GPIO1_C1) SPI3_CLK | I2S0_SDI2SDO2 (GPIO3_D5)
  70. -1,124, // 17 | 37 : GND | I2S0_SDI1SDO3 (GPIO3_D4)
  71. -1,121, // 18 | 38 : UART_RX | I2S0_LRCK_RX (GPIO3_D1)
  72. -1, -1, // 19 | 39 : UART_TX | MCU_PA1
  73. -1, -1, // 20 | 40 : 3.3V | GND
  74. // Not used
  75. -1, -1, -1, -1, -1, -1, -1, -1, // 41...48
  76. -1, -1, -1, -1, -1, -1, -1, -1, // 49...56
  77. -1, -1, -1, -1, -1, -1, -1 // 57...63
  78. };
  79. /*----------------------------------------------------------------------------*/
  80. //
  81. // Global variable define
  82. //
  83. /*----------------------------------------------------------------------------*/
  84. /* ADC file descriptor */
  85. static int adcFds[2];
  86. /* GPIO mmap control. Actual GPIO bank number. */
  87. static volatile uint32_t *gpio[5];
  88. /* GRF(General Register Files) base addresses to control GPIO modes */
  89. static volatile uint32_t *grf[2];
  90. /* CRU(Clock & Reset Unit) base addresses to control CLK mode */
  91. static volatile uint32_t *cru[2];
  92. /* wiringPi Global library */
  93. static struct libkhadas *lib = NULL;
  94. /*----------------------------------------------------------------------------*/
  95. // Function prototype define
  96. /*----------------------------------------------------------------------------*/
  97. static int gpioToShiftReg (int pin);
  98. static int gpioToShiftGReg (int pin);
  99. static void setClkState (int pin, int state);
  100. static void setIomuxMode (int pin, int mode);
  101. /*----------------------------------------------------------------------------*/
  102. // wiringPi core function
  103. /*----------------------------------------------------------------------------*/
  104. static int _getModeToGpio (int mode, int pin);
  105. static void _pinMode (int pin, int mode);
  106. static int _getAlt (int pin);
  107. static void _pullUpDnControl (int pin, int pud);
  108. static int _digitalRead (int pin);
  109. static void _digitalWrite (int pin, int value);
  110. static int _analogRead (int pin);
  111. static void _digitalWriteByte (const int value);
  112. static unsigned int _digitalReadByte (void);
  113. /*----------------------------------------------------------------------------*/
  114. // board init function
  115. /*----------------------------------------------------------------------------*/
  116. static void init_gpio_mmap (void);
  117. static void init_adc_fds (void);
  118. void init_khadas_edge (struct libkhadas *libwiring);
  119. /*----------------------------------------------------------------------------*/
  120. /*----------------------------------------------------------------------------*/
  121. //
  122. // offset to the GPIO bit
  123. //
  124. /*----------------------------------------------------------------------------*/
  125. static int gpioToShiftReg (int pin)
  126. {
  127. return pin % 32;
  128. }
  129. /*----------------------------------------------------------------------------*/
  130. //
  131. // offset to the GPIO bit at GRF address
  132. //
  133. /*----------------------------------------------------------------------------*/
  134. static int gpioToShiftGReg (int pin)
  135. {
  136. return pin % 8;
  137. }
  138. /*----------------------------------------------------------------------------*/
  139. static int _getModeToGpio (int mode, int pin)
  140. {
  141. if (pin > 255)
  142. return msg(MSG_ERR, "%s : Invalid pin number %d\n", __func__, pin);
  143. switch (mode) {
  144. /* Native gpio number */
  145. case MODE_GPIO:
  146. return pin;
  147. /* Native gpio number for sysfs */
  148. case MODE_GPIO_SYS:
  149. return lib->sysFds[pin] != -1 ? pin : -1;
  150. /* wiringPi number */
  151. case MODE_PINS:
  152. return pin < 64 ? pinToGpio[pin] : -1;
  153. /* header pin number */
  154. case MODE_PHYS:
  155. return pin < 64 ? phyToGpio[pin] : -1;
  156. default :
  157. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  158. return -1;
  159. }
  160. }
  161. /*----------------------------------------------------------------------------*/
  162. //
  163. // set GPIO clock state
  164. //
  165. /*----------------------------------------------------------------------------*/
  166. static void setClkState (int pin, int state)
  167. {
  168. uint32_t target = 0;
  169. uint8_t bank = pin / 32;
  170. uint8_t shift = bank < 2 ? bank + 2 : bank + 1;
  171. target |= (1 << (shift + 16));
  172. switch (state) {
  173. case EDGE_CLK_ENABLE:
  174. if (bank < 2) {
  175. target |= *(cru[0] + (EDGE_PMUCRU_GPIO_CLK_OFFSET >> 2));
  176. target &= ~(1 << shift);
  177. *(cru[0] + (EDGE_PMUCRU_GPIO_CLK_OFFSET >> 2)) = target;
  178. } else {
  179. target |= *(cru[1] + (EDGE_CRU_GPIO_CLK_OFFSET >> 2));
  180. target &= ~(1 << shift);
  181. *(cru[1] + (EDGE_CRU_GPIO_CLK_OFFSET >> 2)) = target;
  182. }
  183. break;
  184. case EDGE_CLK_DISABLE:
  185. if (bank < 2) {
  186. target |= *(cru[0] + (EDGE_PMUCRU_GPIO_CLK_OFFSET >> 2));
  187. target |= (1 << shift);
  188. *(cru[0] + (EDGE_PMUCRU_GPIO_CLK_OFFSET >> 2)) = target;
  189. } else {
  190. target |= *(cru[1] + (EDGE_CRU_GPIO_CLK_OFFSET >> 2));
  191. target |= (1 << shift);
  192. *(cru[1] + (EDGE_CRU_GPIO_CLK_OFFSET >> 2)) = target;
  193. }
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. /*----------------------------------------------------------------------------*/
  200. //
  201. // set IOMUX mode
  202. //
  203. /*----------------------------------------------------------------------------*/
  204. static void setIomuxMode (int pin, int mode)
  205. {
  206. uint32_t offset, target;
  207. uint8_t bank, group;
  208. bank = pin / 32;
  209. group = (pin - bank * 32) / 8;
  210. offset = 0x10 * (bank > 1 ? bank - 2 : bank) + 0x4 * group;
  211. target = 0;
  212. target |= (1 << (gpioToShiftGReg(pin) * 2 + 17));
  213. target |= (1 << (gpioToShiftGReg(pin) * 2 + 16));
  214. switch (mode) {
  215. case EDGE_FUNC_GPIO:
  216. // Common IOMUX Funtion 1 : GPIO (0b00)
  217. if (bank < 2) {
  218. offset += EDGE_PMUGRF_IOMUX_OFFSET;
  219. target |= *(grf[0] + (offset >> 2));
  220. target &= ~(1 << (gpioToShiftGReg(pin) * 2 + 1));
  221. target &= ~(1 << (gpioToShiftGReg(pin) * 2));
  222. *(grf[0] + (offset >> 2)) = target;
  223. } else {
  224. offset += EDGE_GRF_IOMUX_OFFSET;
  225. target |= *(grf[1] + (offset >> 2));
  226. target &= ~(1 << (gpioToShiftGReg(pin) * 2 + 1));
  227. target &= ~(1 << (gpioToShiftGReg(pin) * 2));
  228. *(grf[1] + (offset >> 2)) = target;
  229. }
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. /*----------------------------------------------------------------------------*/
  236. static void _pinMode (int pin, int mode)
  237. {
  238. int origPin, bank;
  239. unsigned long UNU flags;
  240. if (lib->mode == MODE_GPIO_SYS)
  241. return;
  242. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  243. return;
  244. origPin = pin;
  245. bank = pin / 32;
  246. softPwmStop (origPin);
  247. softToneStop(origPin);
  248. setClkState (pin, EDGE_CLK_ENABLE);
  249. setIomuxMode(pin, EDGE_FUNC_GPIO);
  250. switch (mode) {
  251. case INPUT:
  252. *(gpio[bank] + (EDGE_GPIO_CON_OFFSET >> 2)) &= ~(1 << gpioToShiftReg(pin));
  253. break;
  254. case OUTPUT:
  255. *(gpio[bank] + (EDGE_GPIO_CON_OFFSET >> 2)) |= (1 << gpioToShiftReg(pin));
  256. break;
  257. case SOFT_PWM_OUTPUT:
  258. softPwmCreate (origPin, 0, 100);
  259. break;
  260. case SOFT_TONE_OUTPUT:
  261. softToneCreate (origPin);
  262. break;
  263. default:
  264. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  265. return;
  266. }
  267. setClkState (pin, EDGE_CLK_DISABLE);
  268. }
  269. /*----------------------------------------------------------------------------*/
  270. static int _getAlt (int pin)
  271. {
  272. uint32_t offset;
  273. uint8_t bank, group, shift;
  274. uint8_t ret = 0;
  275. if (lib->mode == MODE_GPIO_SYS)
  276. return -1;
  277. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  278. return -1;
  279. bank = pin / 32;
  280. group = (pin - bank * 32) / 8;
  281. offset = 0x10 * (bank > 1 ? bank - 2 : bank) + 0x4 * group;
  282. shift = gpioToShiftGReg(pin) << 1;
  283. setClkState(pin, EDGE_CLK_ENABLE);
  284. // Check if the pin is GPIO mode on GRF register
  285. if (bank < 2) {
  286. offset += EDGE_PMUGRF_IOMUX_OFFSET;
  287. ret = (*(grf[0] + (offset >> 2)) >> shift) & 0b11;
  288. } else {
  289. offset += EDGE_GRF_IOMUX_OFFSET;
  290. ret = (*(grf[1] + (offset >> 2)) >> shift) & 0b11;
  291. }
  292. // If it is GPIO mode, check it's direction
  293. if (ret == 0)
  294. ret = *(gpio[bank] + (EDGE_GPIO_CON_OFFSET >> 2)) & (1 << gpioToShiftReg(pin)) ? 1 : 0;
  295. else {
  296. // ALT1 is GPIO mode(0b00) on this SoC
  297. ret++;
  298. }
  299. setClkState(pin, EDGE_CLK_DISABLE);
  300. return ret;
  301. }
  302. /*----------------------------------------------------------------------------*/
  303. static void _pullUpDnControl (int pin, int pud)
  304. {
  305. uint32_t offset, target;
  306. uint8_t bank, group;
  307. if (lib->mode == MODE_GPIO_SYS)
  308. return;
  309. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  310. return;
  311. bank = pin / 32;
  312. group = (pin - bank * 32) / 8;
  313. offset = 0x10 * (bank > 1 ? bank - 2 : bank) + 0x4 * group;
  314. target = 0;
  315. target |= (1 << (gpioToShiftGReg(pin) * 2 + 17));
  316. target |= (1 << (gpioToShiftGReg(pin) * 2 + 16));
  317. setClkState(pin, EDGE_CLK_ENABLE);
  318. switch (pud) {
  319. case PUD_UP:
  320. if (bank < 2) {
  321. offset += EDGE_PMUGRF_PUPD_OFFSET;
  322. target |= *(grf[0] + (offset >> 2));
  323. target &= ~(1 << (gpioToShiftGReg(pin) * 2 + 1));
  324. target |= (1 << (gpioToShiftGReg(pin) * 2));
  325. *(grf[0] + (offset >> 2)) = target;
  326. } else {
  327. offset += EDGE_GRF_PUPD_OFFSET;
  328. target |= *(grf[1] + (offset >> 2));
  329. if (bank == 2 && group >= 2) {
  330. target |= (1 << (gpioToShiftGReg(pin) * 2 + 1));
  331. target |= (1 << (gpioToShiftGReg(pin) * 2));
  332. } else {
  333. target &= ~(1 << (gpioToShiftGReg(pin) * 2 + 1));
  334. target |= (1 << (gpioToShiftGReg(pin) * 2));
  335. }
  336. *(grf[1] + (offset >> 2)) = target;
  337. }
  338. break;
  339. case PUD_DOWN:
  340. if (bank < 2) {
  341. offset += EDGE_PMUGRF_PUPD_OFFSET;
  342. target |= *(grf[0] + (offset >> 2));
  343. target |= (1 << (gpioToShiftGReg(pin) * 2 + 1));
  344. target &= ~(1 << (gpioToShiftGReg(pin) * 2));
  345. *(grf[0] + (offset >> 2)) = target;
  346. } else {
  347. offset += EDGE_GRF_PUPD_OFFSET;
  348. target |= *(grf[1] + (offset >> 2));
  349. if (bank == 2 && group >= 2) {
  350. target &= ~(1 << (gpioToShiftGReg(pin) * 2 + 1));
  351. target |= (1 << (gpioToShiftGReg(pin) * 2));
  352. } else {
  353. target |= (1 << (gpioToShiftGReg(pin) * 2 + 1));
  354. target &= ~(1 << (gpioToShiftGReg(pin) * 2));
  355. }
  356. *(grf[1] + (offset >> 2)) = target;
  357. }
  358. break;
  359. case PUD_OFF:
  360. if (bank < 2) {
  361. offset += EDGE_PMUGRF_PUPD_OFFSET;
  362. target |= *(grf[0] + (offset >> 2));
  363. target &= ~(1 << (gpioToShiftGReg(pin) * 2 + 1));
  364. target &= ~(1 << (gpioToShiftGReg(pin) * 2));
  365. *(grf[0] + (offset >> 2)) = target;
  366. } else {
  367. offset += EDGE_GRF_PUPD_OFFSET;
  368. target |= *(grf[1] + (offset >> 2));
  369. target &= ~(1 << (gpioToShiftGReg(pin) * 2 + 1));
  370. target &= ~(1 << (gpioToShiftGReg(pin) * 2));
  371. *(grf[1] + (offset >> 2)) = target;
  372. }
  373. break;
  374. default:
  375. break;
  376. }
  377. setClkState(pin, EDGE_CLK_DISABLE);
  378. }
  379. /*----------------------------------------------------------------------------*/
  380. static int _digitalRead (int pin)
  381. {
  382. int bank, ret;
  383. char c;
  384. if (lib->mode == MODE_GPIO_SYS) {
  385. if (lib->sysFds[pin] == -1)
  386. return -1;
  387. lseek (lib->sysFds[pin], 0L, SEEK_SET);
  388. if (read(lib->sysFds[pin], &c, 1) < 0) {
  389. msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
  390. return -1;
  391. }
  392. return (c == '0') ? LOW : HIGH;
  393. }
  394. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  395. return -1;
  396. bank = pin / 32;
  397. setClkState(pin, EDGE_CLK_ENABLE);
  398. ret = *(gpio[bank] + (EDGE_GPIO_GET_OFFSET >> 2)) & (1 << gpioToShiftReg(pin)) ? HIGH : LOW;
  399. setClkState(pin, EDGE_CLK_DISABLE);
  400. return ret;
  401. }
  402. /*----------------------------------------------------------------------------*/
  403. static void _digitalWrite (int pin, int value)
  404. {
  405. int bank;
  406. if (lib->mode == MODE_GPIO_SYS) {
  407. if (lib->sysFds[pin] != -1) {
  408. if (value == LOW) {
  409. if (write (lib->sysFds[pin], "0\n", 2) < 0)
  410. msg(MSG_ERR,
  411. "%s : %s\nEdit direction file to output mode for\n\t/sys/class/gpio/gpio%d/direction\n",
  412. __func__, strerror(errno), pin + EDGE_GPIO_PIN_BASE);
  413. } else {
  414. if (write (lib->sysFds[pin], "1\n", 2) < 0)
  415. msg(MSG_ERR,
  416. "%s : %s\nEdit direction file to output mode for\n\t/sys/class/gpio/gpio%d/direction\n",
  417. __func__, strerror(errno), pin + EDGE_GPIO_PIN_BASE);
  418. }
  419. }
  420. return;
  421. }
  422. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  423. return;
  424. bank = pin / 32;
  425. setClkState(pin, EDGE_CLK_ENABLE);
  426. switch (value) {
  427. case LOW:
  428. *(gpio[bank] + (EDGE_GPIO_SET_OFFSET >> 2)) &= ~(1 << gpioToShiftReg(pin));
  429. break;
  430. case HIGH:
  431. *(gpio[bank] + (EDGE_GPIO_SET_OFFSET >> 2)) |= (1 << gpioToShiftReg(pin));
  432. break;
  433. default:
  434. break;
  435. }
  436. setClkState(pin, EDGE_CLK_DISABLE);
  437. }
  438. /*----------------------------------------------------------------------------*/
  439. static int _analogRead (int pin)
  440. {
  441. char value[5] = {0,};
  442. if (lib->mode == MODE_GPIO_SYS)
  443. return -1;
  444. /* wiringPi ADC number = pin 25, pin 29 */
  445. switch (pin) {
  446. #if defined(ARDUINO)
  447. /* To work with physical analog channel numbering */
  448. case 1: case 25:
  449. pin = 0;
  450. break;
  451. case 0: case 29:
  452. pin = 1;
  453. break;
  454. #else
  455. case 0: case 25:
  456. pin = 0;
  457. break;
  458. case 1: case 29:
  459. pin = 1;
  460. break;
  461. #endif
  462. default:
  463. return 0;
  464. }
  465. if (adcFds [pin] == -1)
  466. return 0;
  467. lseek(adcFds [pin], 0L, SEEK_SET);
  468. if (read(adcFds [pin], &value[0], 4) < 0) {
  469. msg(MSG_WARN, "%s: Error occurs when it reads from ADC file descriptor. \n", __func__);
  470. return -1;
  471. }
  472. return atoi(value);
  473. }
  474. /*----------------------------------------------------------------------------*/
  475. static void _digitalWriteByte (const int value)
  476. {
  477. union reg_bitfield gpioBits3;
  478. if (lib->mode == MODE_GPIO_SYS) {
  479. return;
  480. }
  481. // Enable clock for GPIO 1 bank
  482. setClkState(96, EDGE_CLK_ENABLE);
  483. /* Read data register */
  484. gpioBits3.wvalue = *(gpio[3] + (EDGE_GPIO_GET_OFFSET >> 2));
  485. /* Wiring PI GPIO0 = EDGE GPIO3_D.0 */
  486. gpioBits3.bits.bit0 = (value & 0x01);
  487. /* Wiring PI GPIO1 = EDGE GPIO3_D.1 */
  488. gpioBits3.bits.bit1 = (value & 0x02);
  489. /* Wiring PI GPIO2 = EDGE GPIO3_D.2 */
  490. gpioBits3.bits.bit2 = (value & 0x04);
  491. /* Wiring PI GPIO3 = EDGE GPIO3_D.3 */
  492. gpioBits3.bits.bit3 = (value & 0x08);
  493. /* Wiring PI GPIO4 = EDGE GPIO3_D.4 */
  494. gpioBits3.bits.bit4 = (value & 0x10);
  495. /* Wiring PI GPIO5 = EDGE GPIO3_D.5 */
  496. gpioBits3.bits.bit5 = (value & 0x20);
  497. /* Wiring PI GPIO6 = EDGE GPIO3_D.6 */
  498. gpioBits3.bits.bit6 = (value & 0x40);
  499. /* Wiring PI GPIO7 = EDGE GPIO3_D.7 */
  500. gpioBits3.bits.bit7 = (value & 0x80);
  501. /* Update data register */
  502. *(gpio[3] + (EDGE_GPIO_SET_OFFSET >> 2)) = gpioBits3.wvalue;
  503. setClkState(96, EDGE_CLK_DISABLE);
  504. }
  505. /*----------------------------------------------------------------------------*/
  506. static unsigned int _digitalReadByte (void)
  507. {
  508. union reg_bitfield gpioBits3;
  509. unsigned int value = 0;
  510. if (lib->mode == MODE_GPIO_SYS) {
  511. return -1;
  512. }
  513. // Enable clock for GPIO 1 bank
  514. setClkState(96, EDGE_CLK_ENABLE);
  515. /* Read data register */
  516. gpioBits3.wvalue = *(gpio[3] + (EDGE_GPIO_GET_OFFSET >> 2));
  517. setClkState(96, EDGE_CLK_DISABLE);
  518. /* Wiring PI GPIO0 = EDGE GPIO3_D.0 */
  519. if (gpioBits3.bits.bit0)
  520. value |= 0x01;
  521. /* Wiring PI GPIO1 = EDGE GPIO3_D.1 */
  522. if (gpioBits3.bits.bit1)
  523. value |= 0x02;
  524. /* Wiring PI GPIO2 = EDGE GPIO3_D.2 */
  525. if (gpioBits3.bits.bit2)
  526. value |= 0x04;
  527. /* Wiring PI GPIO3 = EDGE GPIO3_D.3 */
  528. if (gpioBits3.bits.bit3)
  529. value |= 0x08;
  530. /* Wiring PI GPIO4 = EDGE GPIO3_D.4 */
  531. if (gpioBits3.bits.bit4)
  532. value |= 0x10;
  533. /* Wiring PI GPIO5 = EDGE GPIO3_D.5 */
  534. if (gpioBits3.bits.bit5)
  535. value |= 0x20;
  536. /* Wiring PI GPIO6 = EDGE GPIO3_D.6 */
  537. if (gpioBits3.bits.bit6)
  538. value |= 0x40;
  539. /* Wiring PI GPIO7 = EDGE GPIO3_D.7 */
  540. if (gpioBits3.bits.bit7)
  541. value |= 0x80;
  542. return value;
  543. }
  544. /*----------------------------------------------------------------------------*/
  545. static void init_gpio_mmap (void)
  546. {
  547. int fd = -1;
  548. void *mapped_cru[2], *mapped_grf[2], *mapped_gpio[5];
  549. /* GPIO mmap setup */
  550. if (!getuid()) {
  551. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  552. msg (MSG_ERR,
  553. "wiringPiSetup: Unable to open /dev/mem: %s\n",
  554. strerror (errno));
  555. } else {
  556. if (access("/dev/gpiomem",0) == 0) {
  557. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  558. msg (MSG_ERR,
  559. "wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
  560. strerror (errno));
  561. } else
  562. msg (MSG_ERR,
  563. "wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
  564. }
  565. if (fd < 0) {
  566. msg(MSG_ERR, "wiringPiSetup: Cannot open memory area for GPIO use. \n");
  567. } else {
  568. // GPIO{0, 1}
  569. //#define EDGE_PMUCRU_BASE 0xFF750000
  570. mapped_cru[0] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_PMUCRU_BASE);
  571. // GPIO{2, 3, 4}
  572. //#define EDGE_CRU_BASE 0xFF760000
  573. mapped_cru[1] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_CRU_BASE);
  574. // GPIO{0, 1}
  575. //#define EDGE_PMU_BASE 0xFF320000
  576. mapped_grf[0] = mmap(0, EDGE_GRF_BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_PMUGRF_BASE);
  577. // GPIO{2, 3, 4}
  578. //#define EDGE_GRF_BASE 0xFF770000
  579. mapped_grf[1] = mmap(0, EDGE_GRF_BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_GRF_BASE);
  580. // GPIO1_A. 0,1,2,3,4,7
  581. // GPIO1_B. 0,1,2,3,4,5
  582. // GPIO1_C. 2,4,5,6
  583. // GPIO1_D. 0
  584. //#define EDGE_GPIO1_BASE 0xFF730000
  585. mapped_gpio[1] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_GPIO_1_BASE);
  586. // GPIO2_C. 0_B,1_B
  587. //#define EDGE_GPIO2_BASE 0xFF780000
  588. mapped_gpio[2] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_GPIO_2_BASE);
  589. // GPIO4_C. 5,6
  590. // GPIO4_D. 0,4,5,6
  591. //#define EDGE_GPIO4_BASE 0xFF790000
  592. mapped_gpio[4] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_GPIO_4_BASE);
  593. // Reserved
  594. mapped_gpio[0] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_GPIO_0_BASE);
  595. mapped_gpio[3] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, EDGE_GPIO_3_BASE);
  596. if ((mapped_cru[0] == MAP_FAILED) || (mapped_cru[1] == MAP_FAILED)) {
  597. msg (MSG_ERR,
  598. "wiringPiSetup: mmap (CRU) failed: %s\n",
  599. strerror (errno));
  600. } else {
  601. cru[0] = (uint32_t *) mapped_cru[0];
  602. cru[1] = (uint32_t *) mapped_cru[1];
  603. }
  604. if ((mapped_grf[0] == MAP_FAILED) || (mapped_grf[1] == MAP_FAILED)) {
  605. msg (MSG_ERR,
  606. "wiringPiSetup: mmap (GRF) failed: %s\n",
  607. strerror (errno));
  608. } else {
  609. grf[0] = (uint32_t *) mapped_grf[0];
  610. grf[1] = (uint32_t *) mapped_grf[1];
  611. }
  612. if ( (mapped_gpio[0] == MAP_FAILED) ||
  613. (mapped_gpio[1] == MAP_FAILED) ||
  614. (mapped_gpio[2] == MAP_FAILED) ||
  615. (mapped_gpio[3] == MAP_FAILED) ||
  616. (mapped_gpio[4] == MAP_FAILED)) {
  617. msg (MSG_ERR,
  618. "wiringPiSetup: mmap (GPIO) failed: %s\n",
  619. strerror (errno));
  620. } else {
  621. gpio[0] = (uint32_t *) mapped_gpio[0];
  622. gpio[1] = (uint32_t *) mapped_gpio[1];
  623. gpio[2] = (uint32_t *) mapped_gpio[2];
  624. gpio[3] = (uint32_t *) mapped_gpio[3];
  625. gpio[4] = (uint32_t *) mapped_gpio[4];
  626. }
  627. }
  628. }
  629. /*----------------------------------------------------------------------------*/
  630. static void init_adc_fds (void)
  631. {
  632. const char *AIN0_NODE, *AIN1_NODE;
  633. struct utsname uname_buf;
  634. /* ADC node setup */
  635. uname(&uname_buf);
  636. AIN0_NODE = "/sys/devices/platform/ff100000.saradc/iio:device0/in_voltage2_raw";
  637. AIN1_NODE = "/sys/devices/platform/ff100000.saradc/iio:device0/in_voltage3_raw";
  638. adcFds[0] = open(AIN0_NODE, O_RDONLY);
  639. adcFds[1] = open(AIN1_NODE, O_RDONLY);
  640. }
  641. /*----------------------------------------------------------------------------*/
  642. void init_khadas_edge (struct libkhadas *libwiring)
  643. {
  644. init_gpio_mmap();
  645. init_adc_fds();
  646. /* wiringPi Core function initialize */
  647. libwiring->getModeToGpio = _getModeToGpio;
  648. libwiring->pinMode = _pinMode;
  649. libwiring->getAlt = _getAlt;
  650. libwiring->pullUpDnControl = _pullUpDnControl;
  651. libwiring->digitalRead = _digitalRead;
  652. libwiring->digitalWrite = _digitalWrite;
  653. libwiring->analogRead = _analogRead;
  654. libwiring->digitalWriteByte = _digitalWriteByte;
  655. libwiring->digitalReadByte = _digitalReadByte;
  656. /* specify pin base number */
  657. libwiring->pinBase = EDGE_GPIO_PIN_BASE;
  658. /* global variable setup */
  659. lib = libwiring;
  660. }
  661. /*----------------------------------------------------------------------------*/
  662. /*----------------------------------------------------------------------------*/