Subversion Repositories HomeAutomation

Rev

Rev 1743 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /******************************************************************************
  2.   gpio.h
  3.  
  4.   CoreCard GPIO Hardware Abstraction Layer
  5.  
  6. ******************************************************************************/
  7.  
  8. #ifndef GPIO_H_
  9. #define GPIO_H_
  10.  
  11. #include <inttypes.h>
  12. #include <avr/io.h>
  13.  
  14. /* Hack to fix lack of PBx, PCx defines in /usr/lib/avr/include/avr/portpins.h */
  15. /* http://savannah.nongnu.org/bugs/?25930 http://www.mail-archive.com/avr-libc-dev@nongnu.org/msg03306.html */
  16. #include <drivers/mcu/portpins.h>
  17.  
  18. /******************************************************************************
  19. * Port output functions
  20. * void gpio_set_pin() sets GPIOX pin to logical 1 level (when output)
  21. * void gpio_clr_pin() clears GPIOX pin to logical 0 level (when output)
  22. * void gpio_set_statement(statement, GPIOX) Sets or clears GPIOX pin to logical level depending on statement
  23. * void gpio_toggle_pin() toggles level of output (when output)
  24. * uint8_t gpio_get_output_state() returns the output_state
  25. *
  26. * Port input functions
  27. * void gpio_set_pullup() makes GPIOX pulled high (when input)
  28. * void gpio_clr_pullup() removes pullup on GPIOX (when input)
  29. * uint8_t gpio_get_state() returns logical level GPIOX pin
  30. *
  31. * Direction functions
  32. * void gpio_set_in() makes GPIOX pin as input
  33. * void gpio_set_out() makes GPIOX pin as output
  34. * uint8_t gpio_get_direction() returns the choosen direction
  35. *
  36. *******************************************************************************/
  37.   static inline __attribute__ ((always_inline)) void gpio_set_pin(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {*port |= (1 << nr);}
  38.   static inline __attribute__ ((always_inline)) void gpio_clr_pin(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {*port &= ~(1 << nr);}
  39.   static inline __attribute__ ((always_inline)) void gpio_set_statement(uint8_t statement ,volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {if (statement) { *port |= (1 << nr); } else {*port &= ~(1 << nr);}}
  40.   static inline __attribute__ ((always_inline)) void gpio_toggle_pin(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {*port ^= (1 << nr);}
  41.   static inline __attribute__ ((always_inline)) uint8_t gpio_get_output_state(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {return (*port & (1 << nr)) != 0;}
  42.  
  43.   static inline __attribute__ ((always_inline)) void gpio_set_pullup(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {*port |= (1 << nr);}
  44.   static inline __attribute__ ((always_inline)) void gpio_clr_pullup(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {*port &= ~(1 << nr);}
  45.   static inline __attribute__ ((always_inline)) uint8_t gpio_get_state(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {return (*pin & (1 << nr)) != 0;}
  46.  
  47.   static inline __attribute__ ((always_inline)) void gpio_set_out(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {*ddr |= (1 << nr);}
  48.   static inline __attribute__ ((always_inline)) void gpio_set_in(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {*ddr &= ~(1 << nr);}
  49.   static inline __attribute__ ((always_inline)) uint8_t gpio_get_direction(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr, uint8_t pcint) {return (*ddr & (1 << nr)) != 0;}
  50.  
  51. #define GPIO_PIN_HIGH       1
  52. #define GPIO_PIN_LOW        0
  53. #define GPIO_PORT_INPUT     0
  54. #define GPIO_PORT_OUTPUT    1
  55. #define GPIO_PORT_HIGH      1
  56. #define GPIO_PORT_LOW       0
  57.  
  58. // generic ports:
  59. #define GPIO_D0         &PORTD,&PIND,&DDRD,PD0,16
  60. #define GPIO_D0_PCINT_vect  PCINT2_vect
  61. #define PORT_GPIO_D0    PORTD
  62. #define PIN_GPIO_D0     PIND
  63. #define DDR_GPIO_D0     DDRD
  64. #define NR_GPIO_D0      PD0
  65.  
  66. #define GPIO_D1         &PORTD,&PIND,&DDRD,PD1,17
  67. #define GPIO_D1_PCINT_vect  PCINT2_vect
  68. #define PORT_GPIO_D1    PORTD
  69. #define PIN_GPIO_D1     PIND
  70. #define DDR_GPIO_D1     DDRD
  71. #define NR_GPIO_D1      PD1
  72.  
  73. #define GPIO_D2         &PORTD,&PIND,&DDRD,PD2,18
  74. #define GPIO_D2_PCINT_vect  PCINT2_vect
  75. #define PORT_GPIO_D2    PORTD
  76. #define PIN_GPIO_D2     PIND
  77. #define DDR_GPIO_D2     DDRD
  78. #define NR_GPIO_D2      PD2
  79.  
  80. //Included for completeness, be sure to check availability before using!
  81.  
  82. #define GPIO_D3         &PORTD,&PIND,&DDRD,PD3,19
  83. #define GPIO_D3_PCINT_vect  PCINT2_vect
  84. #define PORT_GPIO_D3    PORTD
  85. #define PIN_GPIO_D3     PIND
  86. #define DDR_GPIO_D3     DDRD
  87. #define NR_GPIO_D3      PD3
  88.  
  89.  
  90. #define GPIO_D4         &PORTD,&PIND,&DDRD,PD4,20
  91. #define GPIO_D4_PCINT_vect  PCINT2_vect
  92. #define PORT_GPIO_D4    PORTD
  93. #define PIN_GPIO_D4     PIND
  94. #define DDR_GPIO_D4     DDRD
  95. #define NR_GPIO_D4      PD4
  96.  
  97. #define GPIO_D5         &PORTD,&PIND,&DDRD,PD5,21
  98. #define GPIO_D5_PCINT_vect  PCINT2_vect
  99. #define PORT_GPIO_D5    PORTD
  100. #define PIN_GPIO_D5     PIND
  101. #define DDR_GPIO_D5     DDRD
  102. #define NR_GPIO_D5      PD5
  103.  
  104. #define GPIO_D6         &PORTD,&PIND,&DDRD,PD6,22
  105. #define GPIO_D6_PCINT_vect  PCINT2_vect
  106. #define PORT_GPIO_D6    PORTD
  107. #define PIN_GPIO_D6     PIND
  108. #define DDR_GPIO_D6     DDRD
  109. #define NR_GPIO_D6      PD6
  110.  
  111. #define GPIO_D7         &PORTD,&PIND,&DDRD,PD7,23
  112. #define GPIO_D7_PCINT_vect  PCINT2_vect
  113. #define PORT_GPIO_D7    PORTD
  114. #define PIN_GPIO_D7     PIND
  115. #define DDR_GPIO_D7     DDRD
  116. #define NR_GPIO_D7      PD7
  117.  
  118.  
  119. #define GPIO_B0         &PORTB,&PINB,&DDRB,PB0,0
  120. #define GPIO_B0_PCINT_vect  PCINT0_vect
  121. #define PORT_GPIO_B0    PORTB
  122. #define PIN_GPIO_B0     PINB
  123. #define DDR_GPIO_B0     DDRB
  124. #define NR_GPIO_B0      PB0
  125.  
  126. #define GPIO_B1         &PORTB,&PINB,&DDRB,PB1,1
  127. #define GPIO_B1_PCINT_vect  PCINT0_vect
  128. #define PORT_GPIO_B1    PORTB
  129. #define PIN_GPIO_B1     PINB
  130. #define DDR_GPIO_B1     DDRB
  131. #define NR_GPIO_B1      PB1
  132.  
  133. #define GPIO_B2         &PORTB,&PINB,&DDRB,PB2,2
  134. #define GPIO_B2_PCINT_vect  PCINT0_vect
  135. #define PORT_GPIO_B2    PORTB
  136. #define PIN_GPIO_B2     PINB
  137. #define DDR_GPIO_B2     DDRB
  138. #define NR_GPIO_B2      PB2
  139.  
  140. //Included for completeness, be sure to check availability before using!
  141.  
  142. //Note: This is the MOSI pin used for programming with ISP
  143. #define GPIO_B3         &PORTB,&PINB,&DDRB,PB3,3
  144. #define GPIO_B3_PCINT_vect  PCINT0_vect
  145. #define PORT_GPIO_B3    PORTB
  146. #define PIN_GPIO_B3     PINB
  147. #define DDR_GPIO_B3     DDRB
  148. #define NR_GPIO_B3      PB3
  149.  
  150. //Note: This is the MISO pin used for programming with ISP
  151. #define GPIO_B4         &PORTB,&PINB,&DDRB,PB4,4
  152. #define GPIO_B4_PCINT_vect  PCINT0_vect
  153. #define PORT_GPIO_B4    PORTB
  154. #define PIN_GPIO_B4     PINB
  155. #define DDR_GPIO_B4     DDRB
  156. #define NR_GPIO_B4      PB4
  157.  
  158. //Note: This is the SCK pin used for programming with ISP
  159. #define GPIO_B5         &PORTB,&PINB,&DDRB,PB5,5
  160. #define GPIO_B5_PCINT_vect  PCINT0_vect
  161. #define PORT_GPIO_B5    PORTB
  162. #define PIN_GPIO_B5     PINB
  163. #define DDR_GPIO_B5     DDRB
  164. #define NR_GPIO_B5      PB5
  165.  
  166. #define GPIO_B6         &PORTB,&PINB,&DDRB,PB6,6
  167. #define GPIO_B6_PCINT_vect  PCINT0_vect
  168. #define PORT_GPIO_B6    PORTB
  169. #define PIN_GPIO_B6     PINB
  170. #define DDR_GPIO_B6     DDRB
  171. #define NR_GPIO_B6      PB6
  172.  
  173.  
  174. #define GPIO_B7         &PORTB,&PINB,&DDRB,PB7,7
  175. #define GPIO_B7_PCINT_vect  PCINT0_vect
  176. #define PORT_GPIO_B7    PORTB
  177. #define PIN_GPIO_B7     PINB
  178. #define DDR_GPIO_B7     DDRB
  179. #define NR_GPIO_B7      PB7
  180.  
  181.  
  182. #define GPIO_C0         &PORTC,&PINC,&DDRC,PC0,8
  183. #define GPIO_C0_PCINT_vect  PCINT1_vect
  184. #define PORT_GPIO_C0    PORTC
  185. #define PIN_GPIO_C0     PINC
  186. #define DDR_GPIO_C0     DDRC
  187. #define NR_GPIO_C0      PC0
  188.  
  189. #define GPIO_C1         &PORTC,&PINC,&DDRC,PC1,9
  190. #define GPIO_C1_PCINT_vect  PCINT1_vect
  191. #define PORT_GPIO_C1    PORTC
  192. #define PIN_GPIO_C1     PINC
  193. #define DDR_GPIO_C1     DDRC
  194. #define NR_GPIO_C1      PC1
  195.  
  196. #define GPIO_C2         &PORTC,&PINC,&DDRC,PC2,10
  197. #define GPIO_C2_PCINT_vect  PCINT1_vect
  198. #define PORT_GPIO_C2    PORTC
  199. #define PIN_GPIO_C2     PINC
  200. #define DDR_GPIO_C2     DDRC
  201. #define NR_GPIO_C2      PC2
  202.  
  203. #define GPIO_C3         &PORTC,&PINC,&DDRC,PC3,11
  204. #define GPIO_C3_PCINT_vect  PCINT1_vect
  205. #define PORT_GPIO_C3    PORTC
  206. #define PIN_GPIO_C3     PINC
  207. #define DDR_GPIO_C3     DDRC
  208. #define NR_GPIO_C3      PC3
  209.  
  210. #define GPIO_C4         &PORTC,&PINC,&DDRC,PC4,12
  211. #define GPIO_C4_PCINT_vect  PCINT1_vect
  212. #define PORT_GPIO_C4    PORTC
  213. #define PIN_GPIO_C4     PINC
  214. #define DDR_GPIO_C4     DDRC
  215. #define NR_GPIO_C4      PC4
  216.  
  217. #define GPIO_C5         &PORTC,&PINC,&DDRC,PC5,13
  218. #define GPIO_C5_PCINT_vect  PCINT1_vect
  219. #define PORT_GPIO_C5    PORTC
  220. #define PIN_GPIO_C5     PINC
  221. #define DDR_GPIO_C5     DDRC
  222. #define NR_GPIO_C5      PC5
  223.  
  224. // for corecard:
  225. #define EXP_A   &PORTB,&PINB,&DDRB,PB7,7
  226. #define EXP_D_PCINT_vect    PCINT0_vect
  227. #define PORT_EXP_A  PORTB
  228. #define PIN_EXP_A   PINB
  229. #define DDR_EXP_A   DDRB
  230. #define NR_EXP_A    PB7
  231.  
  232. #define EXP_B   &PORTB,&PINB,&DDRB,PB2,2
  233. #define EXP_D_PCINT_vect    PCINT0_vect
  234. #define PORT_EXP_B  PORTB
  235. #define PIN_EXP_B   PINB
  236. #define DDR_EXP_B   DDRB
  237. #define NR_EXP_B    PB2
  238.  
  239. #define EXP_C   &PORTB,&PINB,&DDRB,PB1,1
  240. #define EXP_D_PCINT_vect    PCINT0_vect
  241. #define PORT_EXP_C  PORTB
  242. #define PIN_EXP_C   PINB
  243. #define DDR_EXP_C   DDRB
  244. #define NR_EXP_C    PB1
  245.  
  246. #define EXP_D   &PORTB,&PINB,&DDRB,PB0,0
  247. #define EXP_D_PCINT_vect    PCINT0_vect
  248. #define PORT_EXP_D  PORTB
  249. #define PIN_EXP_D   PINB
  250. #define DDR_EXP_D   DDRB
  251. #define NR_EXP_D    PB0
  252.  
  253. #define EXP_E   &PORTD,&PIND,&DDRD,PD7,23
  254. #define EXP_E_PCINT_vect    PCINT2_vect
  255. #define PORT_EXP_E  PORTD
  256. #define PIN_EXP_E   PIND
  257. #define DDR_EXP_E   DDRD
  258. #define NR_EXP_E    PD7
  259.  
  260. #define EXP_F   &PORTD,&PIND,&DDRD,PD6,22
  261. #define EXP_F_PCINT_vect    PCINT2_vect
  262. #define PORT_EXP_F  PORTD
  263. #define PIN_EXP_F   PIND
  264. #define DDR_EXP_F   DDRD
  265. #define NR_EXP_F    PD6
  266.  
  267. #define EXP_G   &PORTD,&PIND,&DDRD,PD5,21
  268. #define EXP_G_PCINT_vect    PCINT2_vect
  269. #define PORT_EXP_G  PORTD
  270. #define PIN_EXP_G   PIND
  271. #define DDR_EXP_G   DDRD
  272. #define NR_EXP_G    PD5
  273.  
  274. #define EXP_H   &PORTD,&PIND,&DDRD,PD4,20
  275. #define EXP_H_PCINT_vect    PCINT2_vect
  276. #define PORT_EXP_H  PORTD
  277. #define PIN_EXP_H   PIND
  278. #define DDR_EXP_H   DDRD
  279. #define NR_EXP_H    PD4
  280.  
  281. #define EXP_I   &PORTD,&PIND,&DDRD,PD2,18
  282. #define EXP_I_PCINT_vect    PCINT2_vect
  283. #define PORT_EXP_I  PORTD
  284. #define PIN_EXP_I   PIND
  285. #define DDR_EXP_I   DDRD
  286. #define NR_EXP_I    PD2
  287.  
  288. #define EXP_J   &PORTD,&PIND,&DDRD,PD1,17
  289. #define EXP_J_PCINT_vect    PCINT2_vect
  290. #define PORT_EXP_J  PORTD
  291. #define PIN_EXP_J   PIND
  292. #define DDR_EXP_J   DDRD
  293. #define NR_EXP_J    PD1
  294.  
  295. #define EXP_K   &PORTD,&PIND,&DDRD,PD0,16
  296. #define EXP_K_PCINT_vect    PCINT2_vect
  297. #define PORT_EXP_K  PORTD
  298. #define PIN_EXP_K   PIND
  299. #define DDR_EXP_K   DDRD
  300. #define NR_EXP_K    PC0
  301.  
  302. #define EXP_L   &PORTC,&PINC,&DDRC,PC5,13
  303. #define EXP_L_PCINT_vect    PCINT1_vect
  304. #define PORT_EXP_L  PORTC
  305. #define PIN_EXP_L   PINC
  306. #define DDR_EXP_L   DDRC
  307. #define NR_EXP_L    PC5
  308.  
  309. #define EXP_M   &PORTC,&PINC,&DDRC,PC4,12
  310. #define EXP_M_PCINT_vect    PCINT1_vect
  311. #define PORT_EXP_M  PORTC
  312. #define PIN_EXP_M   PINC
  313. #define DDR_EXP_M   DDRC
  314. #define NR_EXP_M    PC4
  315.  
  316. #define EXP_N   &PORTC,&PINC,&DDRC,PC2,10
  317. #define EXP_N_PCINT_vect    PCINT1_vect
  318. #define PORT_EXP_N  PORTC
  319. #define PIN_EXP_N   PINC
  320. #define DDR_EXP_N   DDRC
  321. #define NR_EXP_N    PC2
  322.  
  323. #define EXP_O   &PORTC,&PINC,&DDRC,PC1,9
  324. #define EXP_O_PCINT_vect    PCINT1_vect
  325. #define PORT_EXP_O  PORTC
  326. #define PIN_EXP_O   PINC
  327. #define DDR_EXP_O   DDRC
  328. #define NR_EXP_O    PC1
  329.  
  330. #define EXP_P   &PORTC,&PINC,&DDRC,PC0,8
  331. #define EXP_P_PCINT_vect    PCINT1_vect
  332. #define PORT_EXP_P      PORTC
  333. #define PIN_EXP_P       PINC
  334. #define DDR_EXP_P       DDRC
  335. #define NR_EXP_P        PC0
  336.  
  337. // for NodeEssential:
  338. #define Essential_0     &PORTD,&PIND,&DDRD,PD2,18
  339. #define Essential_0_PCINT_vect  PCINT2_vect
  340. #define PORT_Essential_0    PORTD
  341. #define PIN_Essential_0     PIND
  342. #define DDR_Essential_0     DDRD
  343. #define NR_Essential_0      PD2
  344.  
  345. #define Essential_1     &PORTD,&PIND,&DDRD,PD1,17
  346. #define Essential_1_PCINT_vect  PCINT2_vect
  347. #define PORT_Essential_1    PORTD
  348. #define PIN_Essential_1     PIND
  349. #define DDR_Essential_1     DDRD
  350. #define NR_Essential_1      PD1
  351.  
  352. #define Essential_2     &PORTD,&PIND,&DDRD,PD0,16
  353. #define Essential_2_PCINT_vect  PCINT2_vect
  354. #define PORT_Essential_2    PORTD
  355. #define PIN_Essential_2     PIND
  356. #define DDR_Essential_2     DDRD
  357. #define NR_Essential_2      PD0
  358.  
  359. #define Essential_3     &PORTD,&PIND,&DDRD,PD4,20
  360. #define Essential_3_PCINT_vect  PCINT2_vect
  361. #define PORT_Essential_3    PORTD
  362. #define PIN_Essential_3     PIND
  363. #define DDR_Essential_3     DDRD
  364. #define NR_Essential_3      PD4
  365.  
  366. #define Essential_4     &PORTD,&PIND,&DDRD,PD5,21
  367. #define Essential_4_PCINT_vect  PCINT2_vect
  368. #define PORT_Essential_4    PORTD
  369. #define PIN_Essential_4     PIND
  370. #define DDR_Essential_4     DDRD
  371. #define NR_Essential_4      PD5
  372.  
  373. #define Essential_5     &PORTD,&PIND,&DDRD,PD6,22
  374. #define Essential_5_PCINT_vect  PCINT2_vect
  375. #define PORT_Essential_5    PORTD
  376. #define PIN_Essential_5     PIND
  377. #define DDR_Essential_5     DDRD
  378. #define NR_Essential_5      PD6
  379.  
  380. #define Essential_6     &PORTB,&PINB,&DDRB,PB0,0
  381. #define Essential_6_PCINT_vect  PCINT0_vect
  382. #define PORT_Essential_6    PORTB
  383. #define PIN_Essential_6     PINB
  384. #define DDR_Essential_6     DDRB
  385. #define NR_Essential_6      PB0
  386.  
  387. #define Essential_7     &PORTB,&PINB,&DDRB,PB1,1
  388. #define Essential_7_PCINT_vect  PCINT0_vect
  389. #define PORT_Essential_7    PORTB
  390. #define PIN_Essential_7     PINB
  391. #define DDR_Essential_7     DDRB
  392. #define NR_Essential_7      PB1
  393.  
  394. #define Essential_8     &PORTC,&PINC,&DDRC,PC4,12
  395. #define Essential_8_PCINT_vect  PCINT1_vect
  396. #define PORT_Essential_8    PORTC
  397. #define PIN_Essential_8     PINC
  398. #define DDR_Essential_8     DDRC
  399. #define NR_Essential_8      PC4
  400.  
  401. #define Essential_9     &PORTC,&PINC,&DDRC,PC5,13
  402. #define Essential_9_PCINT_vect  PCINT1_vect
  403. #define PORT_Essential_9    PORTC
  404. #define PIN_Essential_9     PINC
  405. #define DDR_Essential_9     DDRC
  406. #define NR_Essential_9      PC5
  407. #endif
  408. //eof gpio.h
  409.