Subversion Repositories HomeAutomation

Rev

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