Subversion Repositories HomeAutomation

Rev

Rev 1073 | Rev 1085 | 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. /******************************************************************************
  15. * void GPIOX_set() sets GPIOX pin to logical 1 level.
  16. * void GPIOX_clr() clears GPIOX pin to logical 0 level.
  17. * void GPIOX_make_in makes GPIOX pin as input.
  18. * void GPIOX_make_out makes GPIOX pin as output.
  19. * uint8_t GPIOX_read() returns logical level GPIOX pin.
  20. * uint8_t GPIOX_state() returns configuration of GPIOX port.
  21. *******************************************************************************/
  22. #define INLINE static inline __attribute__ ((always_inline))
  23.  
  24. #define HAL_ASSIGN_PIN(name, port, bit) \
  25. INLINE void  GPIO_##name##_set(void)         {PORT##port |= (1 << bit);} \
  26. INLINE void  GPIO_##name##_clr(void)         {PORT##port &= ~(1 << bit);} \
  27. INLINE uint8_t  GPIO_##name##_read(void)     {return (PIN##port & (1 << bit)) != 0;} \
  28. INLINE uint8_t  GPIO_##name##_state(void)    {return (DDR##port & (1 << bit)) != 0;} \
  29. INLINE void  GPIO_##name##_make_out(void)    {DDR##port |= (1 << bit);} \
  30. INLINE void  GPIO_##name##_make_in(void)     {DDR##port &= ~(1 << bit); PORT##port &= ~(1 << bit);} \
  31. INLINE void  GPIO_##name##_make_pullup(void) {PORT##port |= (1 << bit);}\
  32. INLINE void  GPIO_##name##_toggle(void)      {PORT##port ^= (1 << bit);}
  33.  
  34. // the macros for the manipulation by EXP_A
  35. HAL_ASSIGN_PIN(EXP_A, B, 7);
  36. // the macros for the manipulation by EXP_B
  37. HAL_ASSIGN_PIN(EXP_B, B, 2);
  38. // the macros for the manipulation by EXP_C
  39. HAL_ASSIGN_PIN(EXP_C, B, 1);
  40. // the macros for the manipulation by EXP_D
  41. HAL_ASSIGN_PIN(EXP_D, B, 0);
  42. // the macros for the manipulation by EXP_E
  43. HAL_ASSIGN_PIN(EXP_E, D, 7);
  44. // the macros for the manipulation by EXP_F
  45. HAL_ASSIGN_PIN(EXP_F, D, 6);
  46. // the macros for the manipulation by EXP_G
  47. HAL_ASSIGN_PIN(EXP_G, D, 5);
  48. // the macros for the manipulation by EXP_H
  49. HAL_ASSIGN_PIN(EXP_H, D, 4);
  50. // the macros for the manipulation by EXP_I
  51. HAL_ASSIGN_PIN(EXP_I, D, 2);
  52. // the macros for the manipulation by EXP_J
  53. HAL_ASSIGN_PIN(EXP_J,  D, 1);
  54. // the macros for the manipulation by EXP_K
  55. HAL_ASSIGN_PIN(EXP_K, D, 0);
  56. // the macros for the manipulation by EXP_L
  57. HAL_ASSIGN_PIN(EXP_L, C, 5);
  58. // the macros for the manipulation by EXP_M
  59. HAL_ASSIGN_PIN(EXP_M, C, 4);
  60. // the macros for the manipulation by EXP_N
  61. HAL_ASSIGN_PIN(EXP_N, C, 2);
  62. // the macros for the manipulation by EXP_O
  63. HAL_ASSIGN_PIN(EXP_O, C, 5);
  64. // the macros for the manipulation by EXP_P
  65. HAL_ASSIGN_PIN(EXP_P, C, 1);
  66. // the macros for the manipulation by EXP_Q
  67. HAL_ASSIGN_PIN(EXP_Q, C, 7);
  68. // the macros for the manipulation by EXP_R
  69. HAL_ASSIGN_PIN(EXP_R, C, 7);
  70.  
  71. // the macros for the manipulation by PORTB BIT0
  72. HAL_ASSIGN_PIN(GPIO_B0, B, 0);
  73. // the macros for the manipulation by PORTB BIT1
  74. HAL_ASSIGN_PIN(GPIO_B1, B, 1);
  75. // the macros for the manipulation by PORTB BIT2
  76. HAL_ASSIGN_PIN(GPIO_B2, B, 2);
  77. // the macros for the manipulation by PORTB BIT3    bit 3-5 are spi communication with mcp2515
  78. //HAL_ASSIGN_PIN(GPIO_B3, B, 3);
  79. // the macros for the manipulation by PORTB BIT4
  80. //HAL_ASSIGN_PIN(GPIO_B4, B, 4);
  81. // the macros for the manipulation by PORTB BIT5
  82. //HAL_ASSIGN_PIN(GPIO_B5, B, 5);
  83. // the macros for the manipulation by PORTB BIT6    bit 6 is external clock from mcp2515
  84. //HAL_ASSIGN_PIN(GPIO_B6, B, 6);
  85. // the macros for the manipulation by PORTB BIT7
  86. HAL_ASSIGN_PIN(GPIO_B7, B, 7);
  87.  
  88. // the macros for the manipulation by PORTD BIT0
  89. HAL_ASSIGN_PIN(GPIO_D0, D, 0);
  90. // the macros for the manipulation by PORTD BIT1
  91. HAL_ASSIGN_PIN(GPIO_D1, D, 1);
  92. // the macros for the manipulation by PORTD BIT2
  93. HAL_ASSIGN_PIN(GPIO_D2, D, 2);
  94. // the macros for the manipulation by PORTD BIT3    bit 3 is interrup from mcp2515
  95. //HAL_ASSIGN_PIN(GPIO_D3, D, 3);
  96. // the macros for the manipulation by PORTD BIT4
  97. HAL_ASSIGN_PIN(GPIO_D4, D, 4);
  98. // the macros for the manipulation by PORTD BIT5
  99. HAL_ASSIGN_PIN(GPIO_D5, D, 5);
  100. // the macros for the manipulation by PORTD BIT6
  101. HAL_ASSIGN_PIN(GPIO_D6, D, 6);
  102. // the macros for the manipulation by PORTD BIT7
  103. HAL_ASSIGN_PIN(GPIO_D7, D, 7);
  104.  
  105. // the macros for the manipulation by PORTC BIT0
  106. HAL_ASSIGN_PIN(GPIO_C0, C, 0);
  107. // the macros for the manipulation by PORTC BIT1
  108. HAL_ASSIGN_PIN(GPIO_C1, C, 1);
  109. // the macros for the manipulation by PORTC BIT2
  110. HAL_ASSIGN_PIN(GPIO_C2, C, 2);
  111. // the macros for the manipulation by PORTC BIT3    bit 3 is cs on corecard but could be used on nodes
  112. HAL_ASSIGN_PIN(GPIO_C3, C, 3);
  113. // the macros for the manipulation by PORTC BIT4
  114. HAL_ASSIGN_PIN(GPIO_C4, C, 4);
  115. // the macros for the manipulation by PORTC BIT5
  116. HAL_ASSIGN_PIN(GPIO_C5, C, 5);
  117. // the macros for the manipulation by PORTC BIT6    bit 6 is reset
  118. //HAL_ASSIGN_PIN(GPIO_C6, C, 6);
  119.  
  120.  
  121. // for corecard:
  122. #define EXP_A   &PORTB,&PINB,&DDRB,PB7
  123. #define PORT_EXP_A  PORTB
  124. #define PIN_EXP_A   PINB
  125. #define DDR_EXP_A   DDRB
  126. #define NR_EXP_A    PB7
  127.  
  128. #define EXP_B   &PORTB,&PINB,&DDRB,PB2
  129. #define PORT_EXP_B  PORTB
  130. #define PIN_EXP_B   PINB
  131. #define DDR_EXP_B   DDRB
  132. #define NR_EXP_B    PB2
  133.  
  134. #define EXP_C   &PORTB,&PINB,&DDRB,PB1
  135. #define PORT_EXP_C  PORTB
  136. #define PIN_EXP_C   PINB
  137. #define DDR_EXP_C   DDRB
  138. #define NR_EXP_C    PB1
  139.  
  140. #define EXP_D   &PORTB,&PINB,&DDRB,PB0
  141. #define PORT_EXP_D  PORTB
  142. #define PIN_EXP_D   PINB
  143. #define DDR_EXP_D   DDRB
  144. #define NR_EXP_D    PB0
  145.  
  146. #define EXP_E   &PORTD,&PIND,&DDRD,PD7
  147. #define PORT_EXP_E  PORTD
  148. #define PIN_EXP_E   PIND
  149. #define DDR_EXP_E   DDRD
  150. #define NR_EXP_E    PD7
  151.  
  152. #define EXP_F   &PORTD,&PIND,&DDRD,PD6
  153. #define PORT_EXP_F  PORTD
  154. #define PIN_EXP_F   PIND
  155. #define DDR_EXP_F   DDRD
  156. #define NR_EXP_F    PD6
  157.  
  158. #define EXP_G   &PORTD,&PIND,&DDRD,PD5
  159. #define PORT_EXP_G  PORTD
  160. #define PIN_EXP_G   PIND
  161. #define DDR_EXP_G   DDRD
  162. #define NR_EXP_G    PD5
  163.  
  164. #define EXP_H   &PORTD,&PIND,&DDRD,PD4
  165. #define PORT_EXP_H  PORTD
  166. #define PIN_EXP_H   PIND
  167. #define DDR_EXP_H   DDRD
  168. #define NR_EXP_H    PD4
  169.  
  170. #define EXP_I   &PORTD,&PIND,&DDRD,PD2
  171. #define PORT_EXP_I  PORTD
  172. #define PIN_EXP_I   PIND
  173. #define DDR_EXP_I   DDRD
  174. #define NR_EXP_I    PD2
  175.  
  176. #define EXP_J   &PORTD,&PIND,&DDRD,PD1
  177. #define PORT_EXP_J  PORTD
  178. #define PIN_EXP_J   PIND
  179. #define DDR_EXP_J   DDRD
  180. #define NR_EXP_J    PD1
  181.  
  182. #define EXP_K   &PORTD,&PIND,&DDRD,PD0
  183. #define PORT_EXP_K  PORTD
  184. #define PIN_EXP_K   PIND
  185. #define DDR_EXP_K   DDRD
  186. #define NR_EXP_K    PC0
  187.  
  188. #define EXP_L   &PORTC,&PINC,&DDRC,PC5
  189. #define PORT_EXP_L  PORTC
  190. #define PIN_EXP_L   PINC
  191. #define DDR_EXP_L   DDRC
  192. #define NR_EXP_L    PC5
  193.  
  194. #define EXP_M   &PORTC,&PINC,&DDRC,PC4
  195. #define PORT_EXP_M  PORTC
  196. #define PIN_EXP_M   PINC
  197. #define DDR_EXP_M   DDRC
  198. #define NR_EXP_M    PC4
  199.  
  200. #define EXP_N   &PORTC,&PINC,&DDRC,PC2
  201. #define PORT_EXP_N  PORTC
  202. #define PIN_EXP_N   PINC
  203. #define DDR_EXP_N   DDRC
  204. #define NR_EXP_N    PC2
  205.  
  206. #define EXP_O   &PORTC,&PINC,&DDRC,PC1
  207. #define PORT_EXP_O  PORTC
  208. #define PIN_EXP_O   PINC
  209. #define DDR_EXP_O   DDRC
  210. #define NR_EXP_O    PC1
  211.  
  212. #define EXP_P   &PORTC,&PINC,&DDRC,PC0
  213. #define PORT_EXP_P  PORTC
  214. #define PIN_EXP_P   PINC
  215. #define DDR_EXP_P   DDRC
  216. #define NR_EXP_P    PC0
  217.  
  218. static __inline__ void gpio_set_pin(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr) {*port |= (1 << nr);}
  219. static __inline__ void gpio_clr_pin(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr) {*port &= ~(1 << nr);}
  220. static __inline__ void gpio_set_out(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr) {*ddr |= (1 << nr);}
  221. static __inline__ void gpio_set_in(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr) {*ddr &= ~(1 << nr);}
  222. static __inline__ void gpio_set_pullup(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr) {*port |= (1 << nr);}
  223. static __inline__ void gpio_clr_pullup(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr) {*port &= ~(1 << nr);}
  224. static __inline__ void gpio_toggle_pin(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr) {*port ^= (1 << nr);}
  225. static __inline__ uint8_t gpio_get_state(volatile uint8_t* port, volatile uint8_t* pin, volatile uint8_t* ddr,uint8_t nr) {return (*pin & (1 << nr)) != 0;}
  226.  
  227. #endif
  228. //eof gpio.h
  229.