You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

595 lines
19 KiB

  1. /************************************************************************************/
  2. /* */
  3. /* WiringPi KHADAS VIM1 Board control file */
  4. /* */
  5. /************************************************************************************/
  6. /*----------------------------------------------------------------------------------*/
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <sys/ioctl.h>
  15. #include <asm/ioctl.h>
  16. #include <sys/mman.h>
  17. #include <sys/utsname.h>
  18. #include "softPwm.h"
  19. #include "softTone.h"
  20. #include "wiringPi.h"
  21. #include "khadas_vim1.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,475, // 0 | 1 : | GPIODV_26
  29. -1, -1, // 2 | 3 :
  30. 422,423, // 4 | 5 : | GPIOH_7
  31. 425, -1, // 6 | 7 : GPIOH_9 |
  32. -1, -1, // 8 | 9 :
  33. 424,436, // 10 | 11 : GPIOH_8 | GPIOAO_6
  34. -1, -1, // 12 | 13 :
  35. -1,474, // 14 | 15 :
  36. 476, -1, // 16 | 17 : GPIODV_27 |
  37. -1, -1, // 18 | 19 :
  38. -1,435, // 20 | 21 : | GPIOAO_5
  39. 434, -1, // 22 | 23 : GPIOAO_4 |
  40. 421,432, // 24 | 25 : GPIOAO_1 | GPIOAO_2
  41. -1, -1, // 26 | 27 :
  42. -1,473, // 28 | 29 :
  43. -1,421, // 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,474, // 2 | 22 : 5V | I2C_SCK_A(GPIODV_25)
  53. -1,473, // 3 | 23 : HUB_DM1 | I2C_SDA_A(GPIODV_24)
  54. -1, -1, // 4 | 24 : HUB_DP1 | GND
  55. -1,476, // 5 | 25 : GND | I2C_SCK_B(GPIODV_27)
  56. -1,475, // 6 | 26 : 5V | I2C_SDA_B(GPIODV_26)
  57. -1, -1, // 7 | 27 : HUB_DM2 | 3.3V
  58. -1, -1, // 8 | 28 : HUB_DP2 | GND
  59. -1,423, // 9 | 29 : GND | GPIOH_7
  60. -1,422, // 10 | 30 : ADC_CH0 | GPIOH_6
  61. -1,425, // 11 | 31 : GND | GPIOH_9
  62. -1,424, // 12 | 32 : ADC_CH2 | GPIOH_8
  63. -1,436, // 13 | 33 : SPDIF | GPIOAO_6
  64. -1, -1, // 14 | 34 : GND | GND
  65. 435, -1, // 15 | 35 : (GPIOAO_5)UART_RX_AO_B | PWM_AO_A(GPIOAO_3)
  66. 434, -1, // 16 | 36 : (GPIOAO_4)UART_TX_AO_B | RTC_CLK
  67. -1,421, // 17 | 37 : GND | GPIOH_5
  68. 431, -1, // 18 | 38 : (GPIOAO_1)Linux_RX | PWR_EN
  69. 432, -1, // 19 | 39 : (GPIOAO_2)Linux_Tx | PWM_F
  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 arrary */
  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. /*------------------------------------------------------------------------------------------*/
  101. /* wiringPi core function */
  102. /*------------------------------------------------------------------------------------------*/
  103. static int _getModeToGpio (int mode, int pin);
  104. static void _pinMode (int pin, int mode);
  105. static int _getAlt (int pin);
  106. static int _getPUPD (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 int init_gpio_mmap (void);
  117. //static void init_adc_fds (void);
  118. void init_khadas_vim1(struct libkhadas *libwiring);
  119. /*------------------------------------------------------------------------------------------*/
  120. /* offset to the GPIO Set regsiter */
  121. /*------------------------------------------------------------------------------------------*/
  122. static int gpioToGPSETReg (int pin)
  123. {
  124. if(pin >= VIM1_GPIODV_PIN_START && pin <= VIM1_GPIODV_PIN_END)
  125. return VIM1_GPIODV_OUTP_REG_OFFSET;
  126. if(pin >= VIM1_GPIOH_PIN_START && pin <= VIM1_GPIOH_PIN_END)
  127. return VIM1_GPIOH_OUTP_REG_OFFSET;
  128. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END)
  129. return VIM1_GPIOAO_OUTP_REG_OFFSET;
  130. return -1;
  131. }
  132. /*------------------------------------------------------------------------------------------*/
  133. /* offset to the GPIO Input regsiter */
  134. /*------------------------------------------------------------------------------------------*/
  135. static int gpioToGPLEVReg (int pin)
  136. {
  137. if(pin >= VIM1_GPIODV_PIN_START && pin <= VIM1_GPIODV_PIN_END)
  138. return VIM1_GPIODV_INP_REG_OFFSET;
  139. if(pin >= VIM1_GPIOH_PIN_START && pin <= VIM1_GPIOH_PIN_END)
  140. return VIM1_GPIOH_INP_REG_OFFSET;
  141. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END)
  142. return VIM1_GPIOAO_INP_REG_OFFSET;
  143. return -1;
  144. }
  145. /*------------------------------------------------------------------------------------------*/
  146. /* offset to the GPIO Pull up/down enable regsiter */
  147. /*------------------------------------------------------------------------------------------*/
  148. static int gpioToPUENReg(int pin)
  149. {
  150. if(pin >= VIM1_GPIODV_PIN_START && pin <= VIM1_GPIODV_PIN_END)
  151. return VIM1_GPIODV_PUEN_REG_OFFSET;
  152. if(pin >= VIM1_GPIOH_PIN_START && pin <= VIM1_GPIOH_PIN_END)
  153. return VIM1_GPIOH_PUEN_REG_OFFSET;
  154. return -1;
  155. }
  156. /*------------------------------------------------------------------------------------------*/
  157. /* offset to the GPIO Pull up/down regsiter */
  158. /*------------------------------------------------------------------------------------------*/
  159. static int gpioToPUPDReg(int pin)
  160. {
  161. if(pin >= VIM1_GPIODV_PIN_START && pin <= VIM1_GPIODV_PIN_END)
  162. return VIM1_GPIODV_PUPD_REG_OFFSET;
  163. if(pin >= VIM1_GPIOH_PIN_START && pin <= VIM1_GPIOH_PIN_END)
  164. return VIM1_GPIOH_PUPD_REG_OFFSET;
  165. return -1;
  166. }
  167. /*------------------------------------------------------------------------------------------*/
  168. /* offset to the GPIO bit */
  169. /*------------------------------------------------------------------------------------------*/
  170. static int gpioToShiftReg (int pin)
  171. {
  172. if(pin >= VIM1_GPIODV_PIN_START && pin <= VIM1_GPIODV_PIN_END)
  173. return pin - VIM1_GPIODV_PIN_START;
  174. if(pin >= VIM1_GPIOH_PIN_START && pin <= VIM1_GPIOH_PIN_END)
  175. return pin - VIM1_GPIOH_PIN_START +20;
  176. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END)
  177. return pin - VIM1_GPIOAO_PIN_START;
  178. return -1;
  179. }
  180. /*------------------------------------------------------------------------------------------*/
  181. /* offset to the GPIO Function register */
  182. /*------------------------------------------------------------------------------------------*/
  183. static int gpioToGPFSELReg(int pin)
  184. {
  185. if(pin >= VIM1_GPIODV_PIN_START && pin <= VIM1_GPIODV_PIN_END)
  186. return VIM1_GPIODV_FSEL_REG_OFFSET;
  187. if(pin >= VIM1_GPIOH_PIN_START && pin <= VIM1_GPIOH_PIN_END)
  188. return VIM1_GPIOH_FSEL_REG_OFFSET;
  189. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END)
  190. return VIM1_GPIOAO_FSEL_REG_OFFSET;
  191. return -1;
  192. }
  193. static int _getModeToGpio(int mode, int pin)
  194. {
  195. int retPin = -1;
  196. switch(mode){
  197. /* Native gpio number */
  198. case MODE_GPIO:
  199. retPin = pin;
  200. break;
  201. /* Native gpio number for sysfs */
  202. case MODE_GPIO_SYS:
  203. retPin = lib->sysFds[pin] != -1 ? pin : -1;
  204. break;
  205. /* wiringPi number */
  206. case MODE_PINS:
  207. retPin = pin < 64 ? pinToGpio[pin] : -1;
  208. break;
  209. /* header pin number */
  210. case MODE_PHYS:
  211. retPin = pin < 64 ? phyToGpio[pin] : -1;
  212. break;
  213. default:
  214. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  215. return -1;
  216. }
  217. /* To check I2C module loaded */
  218. // if(retPin == 207 || retPin == 208) {
  219. // if(moduleLoaded(AML_MODULE_I2C))
  220. // return -1;
  221. // }
  222. return retPin;
  223. }
  224. /*------------------------------------------------------------------------------------------*/
  225. /*------------------------------------------------------------------------------------------*/
  226. static void _pinMode(int pin, int mode)
  227. {
  228. int fsel, shift, origPin = pin;
  229. if (lib->mode == MODE_GPIO_SYS)
  230. return;
  231. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  232. return;
  233. softPwmStop (origPin);
  234. softToneStop (origPin);
  235. fsel = gpioToGPFSELReg(pin);
  236. shift = gpioToShiftReg (pin);
  237. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END){
  238. switch (mode) {
  239. case INPUT:
  240. *(gpio1 + fsel) = (*(gpio1 + fsel) | (1 << shift));
  241. break;
  242. case OUTPUT:
  243. *(gpio1 + fsel) = (*(gpio1 + fsel) & ~(1 << shift));
  244. break;
  245. case SOFT_PWM_OUTPUT:
  246. softPwmCreate (pin, 0, 100);
  247. break;
  248. case SOFT_TONE_OUTPUT:
  249. softToneCreate (pin);
  250. break;
  251. default:
  252. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  253. break;
  254. }
  255. }else{
  256. switch (mode) {
  257. case INPUT:
  258. *(gpio + fsel) = (*(gpio + fsel) | (1 << shift));
  259. break;
  260. case OUTPUT:
  261. *(gpio + fsel) = (*(gpio + fsel) & ~(1 << shift));
  262. break;
  263. case SOFT_PWM_OUTPUT:
  264. softPwmCreate (pin, 0, 100);
  265. break;
  266. case SOFT_TONE_OUTPUT:
  267. softToneCreate (pin);
  268. break;
  269. default:
  270. msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
  271. break;
  272. }
  273. }
  274. }
  275. /*------------------------------------------------------------------------------------------*/
  276. static int _getAlt (int pin)
  277. {
  278. int fsel, shift;
  279. int mode = 0;
  280. if (lib->mode == MODE_GPIO_SYS)
  281. return 0;
  282. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  283. return 2;
  284. fsel = gpioToGPFSELReg(pin);
  285. shift = gpioToShiftReg(pin);
  286. switch(pin){
  287. case VIM1_GPIODV_PIN_START ...VIM1_GPIODV_PIN_END:
  288. switch(shift){
  289. case 24:
  290. if(*(gpio + VIM1_MUX_2_REG_OFFSET) & (1 << 16)) { mode = 2; break; }
  291. if(*(gpio + VIM1_MUX_1_REG_OFFSET) & (1 << 15)) { mode = 4; break; }
  292. break;
  293. case 25:
  294. if(*(gpio + VIM1_MUX_2_REG_OFFSET) & (1 << 15)) { mode = 2; break; }
  295. if(*(gpio + VIM1_MUX_1_REG_OFFSET) & (1 << 14)) { mode = 4; break; }
  296. break;
  297. case 26:
  298. if(*(gpio + VIM1_MUX_2_REG_OFFSET) & (1 << 14)) { mode = 2; break; }
  299. if(*(gpio + VIM1_MUX_1_REG_OFFSET) & (1 << 13)) { mode = 4; break; }
  300. break;
  301. case 27:
  302. if(*(gpio + VIM1_MUX_2_REG_OFFSET) & (1 << 13)) { mode = 2; break; }
  303. if(*(gpio + VIM1_MUX_1_REG_OFFSET) & (1 << 12)) { mode = 4; break; }
  304. break;
  305. }
  306. break;
  307. case VIM1_GPIOH_PIN_START ...VIM1_GPIOH_PIN_END:
  308. switch(shift){
  309. case 26: //GPIOH6
  310. if(*(gpio + VIM1_MUX_6_REG_OFFSET) & (1 << 26)) { mode = 4; break; }
  311. break;
  312. case 27: //GPIOH7
  313. if(*(gpio + VIM1_MUX_6_REG_OFFSET) & (1 << 22)) { mode = 4; break; }
  314. if(*(gpio + VIM1_MUX_6_REG_OFFSET) & (1 << 25)) { mode = 5; break; }
  315. break;
  316. case 28: //GPIOH8
  317. if(*(gpio + VIM1_MUX_6_REG_OFFSET) & (1 << 21)) { mode = 4; break; }
  318. if(*(gpio + VIM1_MUX_6_REG_OFFSET) & (1 << 24)) { mode = 5; break; }
  319. break;
  320. case 29: //GPIOH9
  321. if(*(gpio + VIM1_MUX_6_REG_OFFSET) & (1 << 23)) { mode = 4; break; }
  322. break;
  323. }
  324. break;
  325. case VIM1_GPIOAO_PIN_START ...VIM1_GPIOAO_PIN_END:
  326. switch(shift){
  327. case 1:
  328. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 11)) { mode = 2; break; }
  329. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 25)) { mode = 3; break; }
  330. break;
  331. case 2:
  332. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 10)) { mode = 2; break; }
  333. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 8)) { mode = 3; break; }
  334. break;
  335. case 4:
  336. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 24)) { mode = 2; break; }
  337. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 6)) { mode = 3; break; }
  338. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 2)) { mode = 4; break; }
  339. break;
  340. case 5:
  341. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 23)) { mode = 2; break; }
  342. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 5)) { mode = 3; break; }
  343. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 1)) { mode = 4; break; }
  344. break;
  345. case 6:
  346. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 16)) { mode = 4; break; }
  347. if(*(gpio1 + VIM1_AO_MUX_1_REG_OFFSET) & (1 << 1)) { mode = 5; break; }
  348. break;
  349. }
  350. break;
  351. default:
  352. return -1;
  353. }
  354. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END){
  355. return mode ? mode + 1 : (*(gpio1 + fsel) & (1 << shift)) ? 0 : 1;
  356. }else{
  357. return mode ? mode + 1 : (*(gpio + fsel) & (1 << shift)) ? 0 : 1;
  358. }
  359. }
  360. /*-------------------------------------------------------------------------------------------*/
  361. static int _getPUPD(int pin)
  362. {
  363. int puen, pupd, shift;
  364. if (lib->mode == MODE_GPIO_SYS)
  365. return -1;
  366. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  367. return -1;
  368. puen = gpioToPUENReg(pin);
  369. pupd = gpioToPUPDReg(pin);
  370. shift = gpioToShiftReg(pin);
  371. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END){
  372. return 0;
  373. }else{
  374. if (*(gpio + puen) & (1 << shift)){
  375. return *(gpio + pupd) & (1 << shift) ? 1 : 2;
  376. }
  377. else
  378. return 0;
  379. }
  380. }
  381. /*------------------------------------------------------------------------------------------*/
  382. static void _pullUpDnControl(int pin, int pud)
  383. {
  384. int shift = 0;
  385. if (lib->mode == MODE_GPIO_SYS)
  386. return;
  387. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  388. return;
  389. shift = gpioToShiftReg(pin);
  390. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END){
  391. }else{
  392. if(pud){
  393. // Enable Pull/Pull-down resister
  394. *(gpio + gpioToPUENReg(pin)) =
  395. (*(gpio + gpioToPUENReg(pin)) | (1 << shift));
  396. if (pud == PUD_UP)
  397. *(gpio + gpioToPUPDReg(pin)) =
  398. (*(gpio + gpioToPUPDReg(pin)) | (1 << shift));
  399. else
  400. *(gpio + gpioToPUPDReg(pin)) =
  401. (*(gpio + gpioToPUPDReg(pin)) & ~(1 << shift));
  402. }else // Disable Pull/Pull-down resister
  403. *(gpio + gpioToPUENReg(pin)) =
  404. (*(gpio + gpioToPUENReg(pin)) & ~(1 << shift));
  405. }
  406. }
  407. /*------------------------------------------------------------------------------------------*/
  408. static int _digitalRead(int pin)
  409. {
  410. char c;
  411. if (lib->mode == MODE_GPIO_SYS) {
  412. if (lib->sysFds[pin] == -1)
  413. return LOW;
  414. lseek(lib->sysFds[pin], 0L, SEEK_SET);
  415. read(lib->sysFds[pin], &c, 1);
  416. return (c=='0') ? LOW : HIGH;
  417. }
  418. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  419. return 0;
  420. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END){
  421. if ((*(gpio1 + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
  422. return HIGH;
  423. else
  424. return LOW;
  425. }else{
  426. if ((*(gpio + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
  427. return HIGH;
  428. else
  429. return LOW;
  430. }
  431. }
  432. /*------------------------------------------------------------------------------------------*/
  433. static void _digitalWrite(int pin, int value)
  434. {
  435. if (lib->mode == MODE_GPIO_SYS) {
  436. if (lib->sysFds[pin] != -1) {
  437. if (value == LOW)
  438. write (lib->sysFds[pin], "0\n", 2);
  439. else
  440. write (lib->sysFds[pin], "1\n", 2);
  441. }
  442. return;
  443. }
  444. if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
  445. return;
  446. if(pin >= VIM1_GPIOAO_PIN_START && pin <= VIM1_GPIOAO_PIN_END){
  447. if (value == LOW)
  448. *(gpio1 + gpioToGPSETReg(pin)) &= ~(1 << (gpioToShiftReg(pin) + 16));
  449. else
  450. *(gpio1 + gpioToGPSETReg(pin)) |= (1 << (gpioToShiftReg(pin) + 16));
  451. }else{
  452. if (value == LOW)
  453. *(gpio + gpioToGPSETReg(pin)) &= ~(1 << gpioToShiftReg(pin));
  454. else
  455. *(gpio + gpioToGPSETReg(pin)) |= (1 << gpioToShiftReg(pin));
  456. }
  457. }
  458. /*------------------------------------------------------------------------------------------*/
  459. static int _analogRead (int UNU pin)
  460. {
  461. return -1;
  462. }
  463. /*------------------------------------------------------------------------------------------*/
  464. static void _digitalWriteByte(const int UNU value)
  465. {
  466. return;
  467. }
  468. static unsigned int _digitalReadByte (void)
  469. {
  470. return -1;
  471. }
  472. /*------------------------------------------------------------------------------------------*/
  473. static int init_gpio_mmap(void)
  474. {
  475. int fd;
  476. /* GPIO mmap setup */
  477. if(access("/dev/gpiomem",0) == 0){
  478. if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  479. return msg (MSG_ERR,
  480. "wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
  481. strerror (errno)) ;
  482. }else{
  483. if (geteuid () != 0)
  484. return msg (MSG_ERR,
  485. "wiringPiSetup: Must be root. (Did you forget sudo?)\n");
  486. if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
  487. return msg (MSG_ERR,
  488. "wiringPiSetup: Unable to open /dev/mem: %s\n",
  489. strerror (errno)) ;
  490. }
  491. gpio1 = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
  492. MAP_SHARED, fd, VIM1_GPIOAO_BASE);
  493. gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
  494. MAP_SHARED, fd, VIM1_GPIO_BASE);
  495. if (((int32_t)gpio == -1) || ((int32_t)gpio1 == -1))
  496. return msg (MSG_ERR,
  497. "wiringPiSetup: mmap (GPIO) failed: %s\n",
  498. strerror (errno));
  499. return 0;
  500. }
  501. /*------------------------------------------------------------------------------------------
  502. static void init_adc_fds(void)
  503. {
  504. }*/
  505. /*------------------------------------------------------------------------------------------*/
  506. void init_khadas_vim1(struct libkhadas *libwiring)
  507. {
  508. init_gpio_mmap();
  509. pinToGpio = pinToGpio_rev;
  510. phyToGpio = phyToGpio_rev;
  511. /* wiringPi core function initialize */
  512. libwiring->getModeToGpio = _getModeToGpio;
  513. libwiring->pinMode = _pinMode;
  514. libwiring->getAlt = _getAlt;
  515. libwiring->getPUPD = _getPUPD;
  516. libwiring->pullUpDnControl = _pullUpDnControl;
  517. libwiring->digitalRead = _digitalRead;
  518. libwiring->digitalWrite = _digitalWrite;
  519. libwiring->analogRead = _analogRead;
  520. libwiring->digitalWriteByte = _digitalWriteByte;
  521. libwiring->digitalReadByte = _digitalReadByte;
  522. /* specify pin base number */
  523. libwiring->pinBase = VIM1_GPIO_PIN_BASE;
  524. /* global variable setup */
  525. lib = libwiring;
  526. }
  527. /*------------------------------------------------------------------------------------------*/
  528. /*------------------------------------------------------------------------------------------*/