Subversion Repositories HomeAutomation

Rev

Rev 1416 | Rev 1743 | Go to most recent revision | 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. #define GPIO_D4         &PORTD,&PIND,&DDRD,PD4,20
  81. #define GPIO_D4_PCINT_vect  PCINT2_vect
  82. #define PORT_GPIO_D4    PORTD
  83. #define PIN_GPIO_D4     PIND
  84. #define DDR_GPIO_D4     DDRD
  85. #define NR_GPIO_D4      PD4
  86.  
  87. #define GPIO_D5         &PORTD,&PIND,&DDRD,PD5,21
  88. #define GPIO_D5_PCINT_vect  PCINT2_vect
  89. #define PORT_GPIO_D5    PORTD
  90. #define PIN_GPIO_D5     PIND
  91. #define DDR_GPIO_D5     DDRD
  92. #define NR_GPIO_D5      PD5
  93.  
  94. #define GPIO_D6         &PORTD,&PIND,&DDRD,PD6,22
  95. #define GPIO_D6_PCINT_vect  PCINT2_vect
  96. #define PORT_GPIO_D6    PORTD
  97. #define PIN_GPIO_D6     PIND
  98. #define DDR_GPIO_D6     DDRD
  99. #define NR_GPIO_D6      PD6
  100.  
  101. #define GPIO_D7         &PORTD,&PIND,&DDRD,PD7,23
  102. #define GPIO_D7_PCINT_vect  PCINT2_vect
  103. #define PORT_GPIO_D7    PORTD
  104. #define PIN_GPIO_D7     PIND
  105. #define DDR_GPIO_D7     DDRD
  106. #define NR_GPIO_D7      PD7
  107.  
  108.  
  109. #define GPIO_B0         &PORTB,&PINB,&DDRB,PB0,0
  110. #define GPIO_B0_PCINT_vect  PCINT0_vect
  111. #define PORT_GPIO_B0    PORTB
  112. #define PIN_GPIO_B0     PINB
  113. #define DDR_GPIO_B0     DDRB
  114. #define NR_GPIO_B0      PB0
  115.  
  116. #define GPIO_B1         &PORTB,&PINB,&DDRB,PB1,1
  117. #define GPIO_B1_PCINT_vect  PCINT0_vect
  118. #define PORT_GPIO_B1    PORTB
  119. #define PIN_GPIO_B1     PINB
  120. #define DDR_GPIO_B1     DDRB
  121. #define NR_GPIO_B1      PB1
  122.  
  123. #define GPIO_B2         &PORTB,&PINB,&DDRB,PB2,2
  124. #define GPIO_B2_PCINT_vect  PCINT0_vect
  125. #define PORT_GPIO_B2    PORTB
  126. #define PIN_GPIO_B2     PINB
  127. #define DDR_GPIO_B2     DDRB
  128. #define NR_GPIO_B2      PB2
  129.  
  130. #define GPIO_B7         &PORTB,&PINB,&DDRB,PB7,7
  131. #define GPIO_B7_PCINT_vect  PCINT0_vect
  132. #define PORT_GPIO_B7    PORTB
  133. #define PIN_GPIO_B7     PINB
  134. #define DDR_GPIO_B7     DDRB
  135. #define NR_GPIO_B7      PB7
  136.  
  137.  
  138. #define GPIO_C0         &PORTC,&PINC,&DDRC,PC0,8
  139. #define GPIO_C0_PCINT_vect  PCINT1_vect
  140. #define PORT_GPIO_C0    PORTC
  141. #define PIN_GPIO_C0     PINC
  142. #define DDR_GPIO_C0     DDRC
  143. #define NR_GPIO_C0      PC0
  144.  
  145. #define GPIO_C1         &PORTC,&PINC,&DDRC,PC1,9
  146. #define GPIO_C1_PCINT_vect  PCINT1_vect
  147. #define PORT_GPIO_C1    PORTC
  148. #define PIN_GPIO_C1     PINC
  149. #define DDR_GPIO_C1     DDRC
  150. #define NR_GPIO_C1      PC1
  151.  
  152. #define GPIO_C2         &PORTC,&PINC,&DDRC,PC2,10
  153. #define GPIO_C2_PCINT_vect  PCINT1_vect
  154. #define PORT_GPIO_C2    PORTC
  155. #define PIN_GPIO_C2     PINC
  156. #define DDR_GPIO_C2     DDRC
  157. #define NR_GPIO_C2      PC2
  158.  
  159. #define GPIO_C3         &PORTC,&PINC,&DDRC,PC3,11
  160. #define GPIO_C3_PCINT_vect  PCINT1_vect
  161. #define PORT_GPIO_C3    PORTC
  162. #define PIN_GPIO_C3     PINC
  163. #define DDR_GPIO_C3     DDRC
  164. #define NR_GPIO_C3      PC3
  165.  
  166. #define GPIO_C4         &PORTC,&PINC,&DDRC,PC4,12
  167. #define GPIO_C4_PCINT_vect  PCINT1_vect
  168. #define PORT_GPIO_C4    PORTC
  169. #define PIN_GPIO_C4     PINC
  170. #define DDR_GPIO_C4     DDRC
  171. #define NR_GPIO_C4      PC4
  172.  
  173. #define GPIO_C5         &PORTC,&PINC,&DDRC,PC5,13
  174. #define GPIO_C5_PCINT_vect  PCINT1_vect
  175. #define PORT_GPIO_C5    PORTC
  176. #define PIN_GPIO_C5     PINC
  177. #define DDR_GPIO_C5     DDRC
  178. #define NR_GPIO_C5      PC5
  179.  
  180. // for corecard:
  181. #define EXP_A   &PORTB,&PINB,&DDRB,PB7,7
  182. #define EXP_D_PCINT_vect    PCINT0_vect
  183. #define PORT_EXP_A  PORTB
  184. #define PIN_EXP_A   PINB
  185. #define DDR_EXP_A   DDRB
  186. #define NR_EXP_A    PB7
  187.  
  188. #define EXP_B   &PORTB,&PINB,&DDRB,PB2,2
  189. #define EXP_D_PCINT_vect    PCINT0_vect
  190. #define PORT_EXP_B  PORTB
  191. #define PIN_EXP_B   PINB
  192. #define DDR_EXP_B   DDRB
  193. #define NR_EXP_B    PB2
  194.  
  195. #define EXP_C   &PORTB,&PINB,&DDRB,PB1,1
  196. #define EXP_D_PCINT_vect    PCINT0_vect
  197. #define PORT_EXP_C  PORTB
  198. #define PIN_EXP_C   PINB
  199. #define DDR_EXP_C   DDRB
  200. #define NR_EXP_C    PB1
  201.  
  202. #define EXP_D   &PORTB,&PINB,&DDRB,PB0,0
  203. #define EXP_D_PCINT_vect    PCINT0_vect
  204. #define PORT_EXP_D  PORTB
  205. #define PIN_EXP_D   PINB
  206. #define DDR_EXP_D   DDRB
  207. #define NR_EXP_D    PB0
  208.  
  209. #define EXP_E   &PORTD,&PIND,&DDRD,PD7,23
  210. #define EXP_E_PCINT_vect    PCINT2_vect
  211. #define PORT_EXP_E  PORTD
  212. #define PIN_EXP_E   PIND
  213. #define DDR_EXP_E   DDRD
  214. #define NR_EXP_E    PD7
  215.  
  216. #define EXP_F   &PORTD,&PIND,&DDRD,PD6,22
  217. #define EXP_F_PCINT_vect    PCINT2_vect
  218. #define PORT_EXP_F  PORTD
  219. #define PIN_EXP_F   PIND
  220. #define DDR_EXP_F   DDRD
  221. #define NR_EXP_F    PD6
  222.  
  223. #define EXP_G   &PORTD,&PIND,&DDRD,PD5,21
  224. #define EXP_G_PCINT_vect    PCINT2_vect
  225. #define PORT_EXP_G  PORTD
  226. #define PIN_EXP_G   PIND
  227. #define DDR_EXP_G   DDRD
  228. #define NR_EXP_G    PD5
  229.  
  230. #define EXP_H   &PORTD,&PIND,&DDRD,PD4,20
  231. #define EXP_H_PCINT_vect    PCINT2_vect
  232. #define PORT_EXP_H  PORTD
  233. #define PIN_EXP_H   PIND
  234. #define DDR_EXP_H   DDRD
  235. #define NR_EXP_H    PD4
  236.  
  237. #define EXP_I   &PORTD,&PIND,&DDRD,PD2,18
  238. #define EXP_I_PCINT_vect    PCINT2_vect
  239. #define PORT_EXP_I  PORTD
  240. #define PIN_EXP_I   PIND
  241. #define DDR_EXP_I   DDRD
  242. #define NR_EXP_I    PD2
  243.  
  244. #define EXP_J   &PORTD,&PIND,&DDRD,PD1,17
  245. #define EXP_J_PCINT_vect    PCINT2_vect
  246. #define PORT_EXP_J  PORTD
  247. #define PIN_EXP_J   PIND
  248. #define DDR_EXP_J   DDRD
  249. #define NR_EXP_J    PD1
  250.  
  251. #define EXP_K   &PORTD,&PIND,&DDRD,PD0,16
  252. #define EXP_K_PCINT_vect    PCINT2_vect
  253. #define PORT_EXP_K  PORTD
  254. #define PIN_EXP_K   PIND
  255. #define DDR_EXP_K   DDRD
  256. #define NR_EXP_K    PC0
  257.  
  258. #define EXP_L   &PORTC,&PINC,&DDRC,PC5,13
  259. #define EXP_L_PCINT_vect    PCINT1_vect
  260. #define PORT_EXP_L  PORTC
  261. #define PIN_EXP_L   PINC
  262. #define DDR_EXP_L   DDRC
  263. #define NR_EXP_L    PC5
  264.  
  265. #define EXP_M   &PORTC,&PINC,&DDRC,PC4,12
  266. #define EXP_M_PCINT_vect    PCINT1_vect
  267. #define PORT_EXP_M  PORTC
  268. #define PIN_EXP_M   PINC
  269. #define DDR_EXP_M   DDRC
  270. #define NR_EXP_M    PC4
  271.  
  272. #define EXP_N   &PORTC,&PINC,&DDRC,PC2,10
  273. #define EXP_N_PCINT_vect    PCINT1_vect
  274. #define PORT_EXP_N  PORTC
  275. #define PIN_EXP_N   PINC
  276. #define DDR_EXP_N   DDRC
  277. #define NR_EXP_N    PC2
  278.  
  279. #define EXP_O   &PORTC,&PINC,&DDRC,PC1,9
  280. #define EXP_O_PCINT_vect    PCINT1_vect
  281. #define PORT_EXP_O  PORTC
  282. #define PIN_EXP_O   PINC
  283. #define DDR_EXP_O   DDRC
  284. #define NR_EXP_O    PC1
  285.  
  286. #define EXP_P   &PORTC,&PINC,&DDRC,PC0,8
  287. #define EXP_P_PCINT_vect    PCINT1_vect
  288. #define PORT_EXP_P      PORTC
  289. #define PIN_EXP_P       PINC
  290. #define DDR_EXP_P       DDRC
  291. #define NR_EXP_P        PC0
  292.  
  293. // for NodeEssential:
  294. #define Essential_0     &PORTD,&PIND,&DDRD,PD2,18
  295. #define Essential_0_PCINT_vect  PCINT2_vect
  296. #define PORT_Essential_0    PORTD
  297. #define PIN_Essential_0     PIND
  298. #define DDR_Essential_0     DDRD
  299. #define NR_Essential_0      PD2
  300.  
  301. #define Essential_1     &PORTD,&PIND,&DDRD,PD1,17
  302. #define Essential_1_PCINT_vect  PCINT2_vect
  303. #define PORT_Essential_1    PORTD
  304. #define PIN_Essential_1     PIND
  305. #define DDR_Essential_1     DDRD
  306. #define NR_Essential_1      PD1
  307.  
  308. #define Essential_2     &PORTD,&PIND,&DDRD,PD0,16
  309. #define Essential_2_PCINT_vect  PCINT2_vect
  310. #define PORT_Essential_2    PORTD
  311. #define PIN_Essential_2     PIND
  312. #define DDR_Essential_2     DDRD
  313. #define NR_Essential_2      PD0
  314.  
  315. #define Essential_3     &PORTD,&PIND,&DDRD,PD4,20
  316. #define Essential_3_PCINT_vect  PCINT2_vect
  317. #define PORT_Essential_3    PORTD
  318. #define PIN_Essential_3     PIND
  319. #define DDR_Essential_3     DDRD
  320. #define NR_Essential_3      PD4
  321.  
  322. #define Essential_4     &PORTD,&PIND,&DDRD,PD5,21
  323. #define Essential_4_PCINT_vect  PCINT2_vect
  324. #define PORT_Essential_4    PORTD
  325. #define PIN_Essential_4     PIND
  326. #define DDR_Essential_4     DDRD
  327. #define NR_Essential_4      PD5
  328.  
  329. #define Essential_5     &PORTD,&PIND,&DDRD,PD6,22
  330. #define Essential_5_PCINT_vect  PCINT2_vect
  331. #define PORT_Essential_5    PORTD
  332. #define PIN_Essential_5     PIND
  333. #define DDR_Essential_5     DDRD
  334. #define NR_Essential_5      PD6
  335.  
  336. #define Essential_6     &PORTB,&PINB,&DDRB,PB0,0
  337. #define Essential_6_PCINT_vect  PCINT0_vect
  338. #define PORT_Essential_6    PORTB
  339. #define PIN_Essential_6     PINB
  340. #define DDR_Essential_6     DDRB
  341. #define NR_Essential_6      PB0
  342.  
  343. #define Essential_7     &PORTB,&PINB,&DDRB,PB1,1
  344. #define Essential_7_PCINT_vect  PCINT0_vect
  345. #define PORT_Essential_7    PORTB
  346. #define PIN_Essential_7     PINB
  347. #define DDR_Essential_7     DDRB
  348. #define NR_Essential_7      PB1
  349.  
  350. #define Essential_8     &PORTC,&PINC,&DDRC,PC4,12
  351. #define Essential_8_PCINT_vect  PCINT1_vect
  352. #define PORT_Essential_8    PORTC
  353. #define PIN_Essential_8     PINC
  354. #define DDR_Essential_8     DDRC
  355. #define NR_Essential_8      PC4
  356.  
  357. #define Essential_9     &PORTC,&PINC,&DDRC,PC5,13
  358. #define Essential_9_PCINT_vect  PCINT1_vect
  359. #define PORT_Essential_9    PORTC
  360. #define PIN_Essential_9     PINC
  361. #define DDR_Essential_9     DDRC
  362. #define NR_Essential_9      PC5
  363. #endif
  364. //eof gpio.h
  365.