Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

635 wiersze
20 KiB

  1. /*--------------------------------------------------------------------------------------*/
  2. /* */
  3. /* WiringPi KHADAS VIM3 Board control file */
  4. /* */
  5. /*--------------------------------------------------------------------------------------*/
  6. /*--------------------------------------------------------------------------------------*/
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <sys/ioctl.h>
  14. #include <asm/ioctl.h>
  15. #include <sys/mman.h>
  16. #include <sys/utsname.h>
  17. #include <string.h>
  18. #include "softPwm.h"
  19. #include "softTone.h"
  20. #include "wiringPi.h"
  21. #include "khadas_vim3.h"
  22. /*--------------------------------------------------------------------------------------*/
  23. /*--------------------------------------------------------------------------------------*/
  24. /* WiringPi gpio map define */
  25. /*--------------------------------------------------------------------------------------*/
  26. static const int pinToGpio_rev[64] = {
  27. //wiringPi number to native gpio number
  28. -1,353, // 0 | 1 : | GPIOAO_3
  29. -1, -1, // 2 | 3 : |
  30. -1,301, // 4 | 5 : | GPIOA_1
  31. 303, -1, // 6 | 7 : GPIOA_3 |
  32. -1, -1, // 8 | 9 : |
  33. 302,304, // 10 | 11 : GPIOA_2 | GPIOA_4
  34. -1, -1, // 12 | 13 : |
  35. -1, -1, // 14 | 15 : |
  36. 352, -1, // 16 | 17 : GPIOAO_2 |
  37. -1, -1, // 18 | 19 : |
  38. -1,326, // 20 | 21 : | GPIOH_6
  39. 327, -1, // 22 | 23 : GPIOH_7 |
  40. 351,350, // 24 | 25 : GPIOAO_1 | GPIOAO_0
  41. -1, -1, // 26 | 27 : |
  42. -1, -1, // 28 | 29 : |
  43. -1, -1, // 30 | 31 : |
  44. // Padding:
  45. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //32to47
  46. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //48to63
  47. };
  48. static const int phyToGpio_rev[64] = {
  49. //physical header pin number to native gpio number
  50. -1, // 0
  51. -1, -1, // 1 | 21 : 5V | GND
  52. -1, -1, // 2 | 22 : 5V | GPIOA_15
  53. -1, -1, // 3 | 23 : USB_DM | GPIOA_14
  54. -1, -1, // 4 | 24 : USB_DP | GND
  55. -1,352, // 5 | 25 : GND | GPIOAO_2
  56. -1,353, // 6 | 26 : MCU3.3 | GPIOAO_3
  57. -1, -1, // 7 | 27 : MCUNrST | 3.3V
  58. -1, -1, // 8 | 28 : MCUSWIM | GND
  59. -1,301, // 9 | 29 : GND | GPIOA_1
  60. -1, -1, // 10 | 30 : ADC0 | GPIOA_0
  61. -1,303, // 11 | 31 : 1.8V | GPIOA_3
  62. -1,302, // 12 | 32 : ADC1 | GPIOA_2
  63. -1,304, // 13 | 33 : SPDIF | GPIOA_4
  64. -1, -1, // 14 | 34 : GND | GND
  65. 326, -1, // 15 | 35 : (GPIOH_6)UART_RX_AO_B | PWM-F
  66. 327, -1, // 16 | 36 : (GPIOH_7)UART_TX_AO_B | RTC_CLK
  67. -1, -1, // 17 | 37 : GND | GPIOH_4
  68. 351, -1, // 18 | 38 : (GPIOAO_1)Linux_RX | MCUFA_1
  69. 350, -1, // 19 | 39 : (GPIOAO_0)Linux_TX | GPIOZ_15
  70. -1, -1, // 20 | 40 : 3.3V | GND
  71. //Not used
  72. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  73. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  74. -1, -1, -1, //41-63
  75. };
  76. /*--------------------------------------------------------------------------------------*/
  77. /* */
  78. /* Global variable define */
  79. /* */
  80. /*--------------------------------------------------------------------------------------*/
  81. /*--------------------------------------------------------------------------------------*/
  82. /* wiringPi Pinmap control array */
  83. /*--------------------------------------------------------------------------------------*/
  84. const int *pinToGpio, *phyToGpio;
  85. /* ADC file descriptor */
  86. static char *adcFds[2];
  87. /* GPIO mmap control */
  88. static volatile uint32_t *gpio,*gpio1;
  89. /* wiringPi Global library */
  90. static struct libkhadas *lib = NULL;
  91. /*--------------------------------------------------------------------------------------*/
  92. /* Function prototype define */
  93. /*--------------------------------------------------------------------------------------*/
  94. static int gpioToGPSETReg (int pin);
  95. static int gpioToGPLEVReg (int pin);
  96. static int gpioToPUENReg (int pin);
  97. static int gpioToPUPDReg (int pin);
  98. static int gpioToShiftReg (int pin);
  99. static int gpioToGPFSELReg (int pin);
  100. static int gpioToDSReg (int pin);
  101. static int gpioToMuxReg (int pin);
  102. /*--------------------------------------------------------------------------------------*/
  103. /* wiringPi core function */
  104. /*--------------------------------------------------------------------------------------*/
  105. static int _getModeToGpio (int mode, int pin);
  106. static void _setPadDrive (int pin, int value);
  107. static void _getPadDrive (int pin);
  108. static void _pinMode (int pin, int mode);
  109. static int _getAlt (int pin);
  110. static int _getPUPD (int pin);
  111. static void _pullUpDnControl (int pin, int pud);
  112. static int _digitalRead (int pin);
  113. static void _digitalWrite (int pin, int value);
  114. static int _analogRead (int pin);
  115. static void _digitalWriteByte (const int value);
  116. static unsigned int _digitalReadByte (void);
  117. /*--------------------------------------------------------------------------------------*/
  118. /* board init function */
  119. /*--------------------------------------------------------------------------------------*/
  120. static int init_gpio_mmap (void);
  121. static void init_adc_fds (void);
  122. void init_khadas_vim3(struct libkhadas *libwiring);
  123. /*--------------------------------------------------------------------------------------*/
  124. /* offset to the GPIO Set regsiter */
  125. /*--------------------------------------------------------------------------------------*/
  126. static int gpioToGPSETReg (int pin)
  127. {
  128. if(pin >= VIM3_GPIOA_PIN_START && pin <= VIM3_GPIOA_PIN_END)
  129. return VIM3_GPIOA_OUTP_REG_OFFSET;
  130. if(pin >= VIM3_GPIOH_PIN_START && pin <= VIM3_GPIOH_PIN_END)
  131. return VIM3_GPIOH_OUTP_REG_OFFSET;
  132. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END)
  133. return VIM3_GPIOAO_OUTP_REG_OFFSET;
  134. return -1;
  135. }
  136. /*------------------------------------------------------------------------------------------*/
  137. /* offset to the GPIO Input regsiter */
  138. /*------------------------------------------------------------------------------------------*/
  139. static int gpioToGPLEVReg (int pin)
  140. {
  141. if(pin >= VIM3_GPIOA_PIN_START && pin <= VIM3_GPIOA_PIN_END)
  142. return VIM3_GPIOA_INP_REG_OFFSET;
  143. if(pin >= VIM3_GPIOH_PIN_START && pin <= VIM3_GPIOH_PIN_END)
  144. return VIM3_GPIOH_INP_REG_OFFSET;
  145. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END)
  146. return VIM3_GPIOAO_INP_REG_OFFSET;
  147. return -1;
  148. }
  149. /*------------------------------------------------------------------------------------------*/
  150. /* offset to the GPIO Pull up/down enable regsiter */
  151. /*------------------------------------------------------------------------------------------*/
  152. static int gpioToPUENReg(int pin)
  153. {
  154. if(pin >= VIM3_GPIOA_PIN_START && pin <= VIM3_GPIOA_PIN_END)
  155. return VIM3_GPIOA_PUEN_REG_OFFSET;
  156. if(pin >= VIM3_GPIOH_PIN_START && pin <= VIM3_GPIOH_PIN_END)
  157. return VIM3_GPIOH_PUEN_REG_OFFSET;
  158. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END)
  159. return VIM3_GPIOAO_PUEN_REG_OFFSET;
  160. return -1;
  161. }
  162. /*------------------------------------------------------------------------------------------*/
  163. /* offset to the GPIO Pull up/down regsiter */
  164. /*------------------------------------------------------------------------------------------*/
  165. static int gpioToPUPDReg(int pin)
  166. {
  167. if(pin >= VIM3_GPIOA_PIN_START && pin <= VIM3_GPIOA_PIN_END)
  168. return VIM3_GPIOA_PUPD_REG_OFFSET;
  169. if(pin >= VIM3_GPIOH_PIN_START && pin <= VIM3_GPIOH_PIN_END)
  170. return VIM3_GPIOH_PUPD_REG_OFFSET;
  171. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END)
  172. return VIM3_GPIOAO_PUPD_REG_OFFSET;
  173. return -1;
  174. }
  175. /*------------------------------------------------------------------------------------------*/
  176. /* offset to the GPIO bit */
  177. /*------------------------------------------------------------------------------------------*/
  178. static int gpioToShiftReg (int pin)
  179. {
  180. if(pin >= VIM3_GPIOA_PIN_START && pin <= VIM3_GPIOA_PIN_END)
  181. return pin - VIM3_GPIOA_PIN_START;
  182. if(pin >= VIM3_GPIOH_PIN_START && pin <= VIM3_GPIOH_PIN_END)
  183. return pin - VIM3_GPIOH_PIN_START;
  184. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END)
  185. return pin - VIM3_GPIOAO_PIN_START;
  186. return -1;
  187. }
  188. /*------------------------------------------------------------------------------------------*/
  189. /* offset to the GPIO Function register */
  190. /*------------------------------------------------------------------------------------------*/
  191. static int gpioToGPFSELReg(int pin)
  192. {
  193. if(pin >= VIM3_GPIOA_PIN_START && pin <= VIM3_GPIOA_PIN_END)
  194. return VIM3_GPIOA_FSEL_REG_OFFSET;
  195. if(pin >= VIM3_GPIOH_PIN_START && pin <= VIM3_GPIOH_PIN_END)
  196. return VIM3_GPIOH_FSEL_REG_OFFSET;
  197. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END)
  198. return VIM3_GPIOAO_FSEL_REG_OFFSET;
  199. return -1;
  200. }
  201. /*------------------------------------------------------------------------------------------*/
  202. /* offset to the GPIO Drive Strength register */
  203. /*------------------------------------------------------------------------------------------*/
  204. static int gpioToDSReg (int pin)
  205. {
  206. if(pin >= VIM3_GPIOA_PIN_START && pin <= VIM3_GPIOA_PIN_END)
  207. return VIM3_GPIOA_DS_REG_5A_OFFSET;
  208. if(pin >= VIM3_GPIOH_PIN_START && pin <= VIM3_GPIOH_PIN_END)
  209. return VIM3_GPIOH_DS_REG_3A_OFFSET;
  210. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END)
  211. return VIM3_GPIOAO_DS_REG_A_OFFSET;
  212. return -1;
  213. }
  214. /*------------------------------------------------------------------------------------------*/
  215. /* offset to the GPIO Pin Mux register */
  216. /*------------------------------------------------------------------------------------------*/
  217. static int gpioToMuxReg(int pin)
  218. {
  219. switch(pin){
  220. case VIM3_GPIOA_PIN_START ...VIM3_GPIOA_PIN_START + 7:
  221. return VIM3_GPIOA_MUX_D_REG_OFFSET;
  222. case VIM3_GPIOA_PIN_START + 8 ...VIM3_GPIOA_PIN_END:
  223. return VIM3_GPIOA_MUX_E_REG_OFFSET;
  224. case VIM3_GPIOH_PIN_START ...VIM3_GPIOH_PIN_END:
  225. return VIM3_GPIOH_MUX_B_REG_OFFSET;
  226. case VIM3_GPIOAO_PIN_START ...VIM3_GPIOAO_PIN_START + 7:
  227. return VIM3_GPIOAO_MUX_0_REG_OFFSET;
  228. case VIM3_GPIOAO_PIN_START + 8 ...VIM3_GPIOAO_PIN_END:
  229. return VIM3_GPIOAO_MUX_1_REG_OFFSET;
  230. }
  231. }
  232. /*------------------------------------------------------------------------------------------*/
  233. static int _getModeToGpio(int mode, int pin)
  234. {
  235. int retPin = -1;
  236. switch(mode){
  237. /* Native gpio number */
  238. case MODE_GPIO:
  239. retPin = pin;
  240. break;
  241. /* Native gpio number for sysfs */
  242. case MODE_GPIO_SYS:
  243. retPin = lib->sysFds[pin] != -1 ? pin : -1;
  244. break;
  245. /* wiringPi number */
  246. case MODE_PINS:
  247. retPin = pin < 64 ? pinToGpio[pin] : -1;
  248. break;
  249. /* header pin number */
  250. case MODE_PHYS:
  251. retPin = pin < 64 ? phyToGpio[pin] : -1;
  252. break;
  253. default:
  254. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  255. return -1;
  256. }
  257. return retPin;
  258. }
  259. /*------------------------------------------------------------------------------------------*/
  260. static void _setPadDrive(int pin, int value)
  261. {
  262. int ds, shift;
  263. if(lib->mode == MODE_GPIO_SYS)
  264. return;
  265. if((pin = _getModeToGpio(lib->mode, pin)) < 0)
  266. return;
  267. if(value < 0 || value > 3){
  268. msg(MSG_WARN, "%s : Invalid value %d (Must be 0 ~ 3)\n", __func__, value);
  269. return;
  270. }
  271. ds = gpioToDSReg(pin);
  272. shift = gpioToShiftReg(pin);
  273. shift *= 2;
  274. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END){
  275. *(gpio1 + ds) &= ~(0b11 << shift);
  276. *(gpio1 + ds) |= (value << shift);
  277. }else{
  278. *(gpio + ds) &= ~(0b11 << shift);
  279. *(gpio + ds) |= (value << shift);
  280. }
  281. }
  282. /*-----------------------------------------------------------------------------------------*/
  283. static void _getPadDrive(int pin)
  284. {
  285. int ds, shift;
  286. if(lib->mode == MODE_GPIO_SYS)
  287. return;
  288. if((pin = _getModeToGpio(lib->mode, pin)) < 0)
  289. return;
  290. ds = gpioToDSReg(pin);
  291. shift = gpioToShiftReg(pin);
  292. shift *= 2;
  293. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END){
  294. return (*(gpio1 + ds) >> shift) & 0b11;
  295. }else{
  296. return (*(gpio + ds) >> shift) & 0b11;
  297. }
  298. }
  299. /*------------------------------------------------------------------------------------------*/
  300. static void _pinMode(int pin, int mode)
  301. {
  302. int fsel, shift, origPin = pin;
  303. if (lib->mode == MODE_GPIO_SYS)
  304. return;
  305. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  306. return;
  307. softPwmStop (origPin);
  308. softToneStop (origPin);
  309. fsel = gpioToGPFSELReg(pin);
  310. shift = gpioToShiftReg (pin);
  311. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END){
  312. switch (mode) {
  313. case INPUT:
  314. *(gpio1 + fsel) = (*(gpio1 + fsel) | (1 << shift));
  315. break;
  316. case OUTPUT:
  317. *(gpio1 + fsel) = (*(gpio1 + fsel) & ~(1 << shift));
  318. break;
  319. case SOFT_PWM_OUTPUT:
  320. softPwmCreate (pin, 0, 100);
  321. break;
  322. case SOFT_TONE_OUTPUT:
  323. softToneCreate (pin);
  324. break;
  325. default:
  326. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  327. break;
  328. }
  329. }else{
  330. switch (mode) {
  331. case INPUT:
  332. *(gpio + fsel) = (*(gpio + fsel) | (1 << shift));
  333. break;
  334. case OUTPUT:
  335. *(gpio + fsel) = (*(gpio + fsel) & ~(1 << shift));
  336. break;
  337. case SOFT_PWM_OUTPUT:
  338. softPwmCreate (pin, 0, 100);
  339. break;
  340. case SOFT_TONE_OUTPUT:
  341. softToneCreate (pin);
  342. break;
  343. default:
  344. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  345. break;
  346. }
  347. }
  348. }
  349. /*------------------------------------------------------------------------------------------*/
  350. static int _getAlt(int pin)
  351. {
  352. int fsel, mux, shift, target, mode;
  353. if(lib->mode == MODE_GPIO_SYS)
  354. return 0;
  355. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  356. return 2;
  357. fsel = gpioToGPFSELReg(pin);
  358. mux = gpioToMuxReg(pin);
  359. target = shift = gpioToShiftReg(pin);
  360. while(target >= 8) {
  361. target -= 8;
  362. }
  363. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END){
  364. mode = (*(gpio1 + mux) >> (target * 4)) & 0xF;
  365. return mode ? mode + 1 : (*(gpio1 + fsel) & (1 << shift)) ? 0 : 1;
  366. }else{
  367. mode = (*(gpio + mux) >> (target * 4)) & 0xF;
  368. return mode ? mode + 1 : (*(gpio + fsel) & (1 << shift)) ? 0 : 1;
  369. }
  370. }
  371. /*------------------------------------------------------------------------------------------*/
  372. static int _getPUPD(int pin)
  373. {
  374. int puen, pupd, shift;
  375. if (lib->mode == MODE_GPIO_SYS)
  376. return -1;
  377. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  378. return -1;
  379. puen = gpioToPUENReg(pin);
  380. pupd = gpioToPUPDReg(pin);
  381. shift = gpioToShiftReg(pin);
  382. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END){
  383. if(*(gpio1 + puen) & (1 << shift)){
  384. return *(gpio1 + pupd) & (1 << shift) ? 1 : 2;
  385. }else{
  386. return 0;
  387. }
  388. }else{
  389. if(*(gpio + puen) & (1 << shift)){
  390. return *(gpio + pupd) & (1 << shift) ? 1 : 2;
  391. }else{
  392. return 0;
  393. }
  394. }
  395. }
  396. /*------------------------------------------------------------------------------------------*/
  397. static void _pullUpDnControl(int pin, int pud)
  398. {
  399. int shift = 0;
  400. if (lib->mode == MODE_GPIO_SYS)
  401. return;
  402. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  403. return;
  404. shift = gpioToShiftReg(pin);
  405. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END){
  406. if(pud){
  407. //Enable Pull/Pull-down resister
  408. *(gpio1 + gpioToPUENReg(pin)) =
  409. (*(gpio1 + gpioToPUENReg(pin)) | (1 << shift));
  410. if (pud == PUD_UP)
  411. *(gpio1 + gpioToPUPDReg(pin)) =
  412. (*(gpio1 + gpioToPUPDReg(pin)) | (1 << shift));
  413. else
  414. *(gpio1 + gpioToPUPDReg(pin)) =
  415. (*(gpio1 + gpioToPUPDReg(pin)) & ~(1 << shift));
  416. }else //Disable Pull/Pull-down resister
  417. *(gpio1 + gpioToPUENReg(pin)) =
  418. (*(gpio1 + gpioToPUENReg(pin)) & ~(1 << shift));
  419. }else{
  420. if(pud){
  421. //Enable Pull/Pull-down resister
  422. *(gpio + gpioToPUENReg(pin)) =
  423. (*(gpio + gpioToPUENReg(pin)) | (1 << shift));
  424. if (pud == PUD_UP)
  425. *(gpio + gpioToPUPDReg(pin)) =
  426. (*(gpio + gpioToPUPDReg(pin)) | (1 << shift));
  427. else
  428. *(gpio + gpioToPUPDReg(pin)) =
  429. (*(gpio + gpioToPUPDReg(pin)) & ~(1 << shift));
  430. }else //Disable Pull/Pull-down resister
  431. *(gpio + gpioToPUENReg(pin)) =
  432. (*(gpio + gpioToPUENReg(pin)) & ~(1 << shift));
  433. }
  434. }
  435. /*------------------------------------------------------------------------------------------*/
  436. static int _digitalRead(int pin)
  437. {
  438. char c;
  439. if (lib->mode == MODE_GPIO_SYS) {
  440. if (lib->sysFds[pin] == -1)
  441. return LOW;
  442. lseek(lib->sysFds[pin], 0L, SEEK_SET);
  443. read(lib->sysFds[pin], &c, 1);
  444. return (c=='0') ? LOW : HIGH;
  445. }
  446. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  447. return 0;
  448. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END){
  449. if ((*(gpio1 + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
  450. return HIGH;
  451. else
  452. return LOW;
  453. }else{
  454. if ((*(gpio + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
  455. return HIGH;
  456. else
  457. return LOW;
  458. }
  459. }
  460. /*------------------------------------------------------------------------------------------*/
  461. static void _digitalWrite(int pin, int value)
  462. {
  463. if (lib->mode == MODE_GPIO_SYS) {
  464. if (lib->sysFds[pin] != -1) {
  465. if(value == LOW)
  466. write (lib->sysFds[pin], "0\n", 2);
  467. else
  468. write (lib->sysFds[pin], "1\n", 2);
  469. }
  470. return;
  471. }
  472. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  473. return;
  474. if(pin >= VIM3_GPIOAO_PIN_START && pin <= VIM3_GPIOAO_PIN_END){
  475. if (value == LOW)
  476. *(gpio1 + gpioToGPSETReg(pin)) &= ~(1 << gpioToShiftReg(pin));
  477. else
  478. *(gpio1 + gpioToGPSETReg(pin)) |= (1 << gpioToShiftReg(pin));
  479. }else{
  480. if (value == LOW)
  481. *(gpio + gpioToGPSETReg(pin)) &= ~(1 << gpioToShiftReg(pin));
  482. else
  483. *(gpio + gpioToGPSETReg(pin)) |= (1 << gpioToShiftReg(pin));
  484. }
  485. }
  486. /*------------------------------------------------------------------------------------------*/
  487. static int _analogRead (int pin)
  488. {
  489. return -1;
  490. }
  491. /*-------------------------------------------------------------------------------------------*/
  492. static void _digitalWriteByte(const int value)
  493. {
  494. return;
  495. }
  496. static unsigned int _digitalReadByte (void)
  497. {
  498. return -1;
  499. }
  500. /*------------------------------------------------------------------------------------------*/
  501. static int init_gpio_mmap(void)
  502. {
  503. int fd;
  504. /* GPIO mmap setup */
  505. if(access("/dev/gpiomem",0) == 0){
  506. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  507. return msg (MSG_ERR,
  508. "wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
  509. strerror (errno));
  510. }else{
  511. if (geteuid () != 0)
  512. return msg (MSG_ERR,
  513. "wiringPiSetup: Must be root. (Did you forget sudo?)\n");
  514. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  515. return msg (MSG_ERR,
  516. "wiringPiSetup: Unable to open /dev/mem: %s\n",
  517. strerror (errno));
  518. }
  519. gpio1 = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
  520. MAP_SHARED, fd, VIM3_GPIOAO_BASE);
  521. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
  522. MAP_SHARED, fd, VIM3_GPIO_BASE);
  523. if (((int32_t)gpio == -1) || ((int32_t)gpio1 == -1))
  524. return msg (MSG_ERR,
  525. "wiringPiSetup: mmap (GPIO) failed: %s\n",
  526. strerror (errno));
  527. }
  528. /*------------------------------------------------------------------------------------------*/
  529. static void init_adc_fds(void)
  530. {
  531. }
  532. /*------------------------------------------------------------------------------------------*/
  533. void init_khadas_vim3(struct libkhadas *libwiring)
  534. {
  535. init_gpio_mmap();
  536. pinToGpio = pinToGpio_rev;
  537. phyToGpio = phyToGpio_rev;
  538. /* wiringPi core function initialize */
  539. libwiring->getModeToGpio = _getModeToGpio;
  540. libwiring->setPadDrive = _setPadDrive;
  541. libwiring->getPadDrive = _getPadDrive;
  542. libwiring->pinMode = _pinMode;
  543. libwiring->getAlt = _getAlt;
  544. libwiring->getPUPD = _getPUPD;
  545. libwiring->pullUpDnControl = _pullUpDnControl;
  546. libwiring->digitalRead = _digitalRead;
  547. libwiring->digitalWrite = _digitalWrite;
  548. libwiring->analogRead = _analogRead;
  549. libwiring->digitalWriteByte = _digitalWriteByte;
  550. libwiring->digitalReadByte = _digitalReadByte;
  551. /* specify pin base number */
  552. libwiring->pinBase = VIM3_GPIO_PIN_BASE;
  553. /* global variable setup */
  554. lib = libwiring;
  555. }
  556. /*------------------------------------------------------------------------------------------*/
  557. /*------------------------------------------------------------------------------------------*/