Add .gitignore
[rapper.git] / bitbucket_lpc1768 / io-pin.ss
1 #lang scribble/text
2 @(define ports '(P0 P1 P2 P3 P4))
3 @(define registers '(LPC_GPIO0 LPC_GPIO1 LPC_GPIO2 LPC_GPIO3 LPC_GPIO4))
4 /* 
5  * IO functions for NXP LPC1768. (generated file, check io-pin.ss)
6  *
7  * See also: http://bitbucket.org/jpc/lpc1768/
8  *
9  * Copyright (c) 2010 LoEE - Jakub Piotr Cłapa
10  * This program is released under the new BSD license.
11  */
12 #ifndef IO_PIN_H
13 #define IO_PIN_H
14
15 #include "CMSIS/LPC17xx.h"
16
17 enum pin_port {
18 @(add-newlines
19   (for/list ([i (in-naturals)]
20              [port ports])
21     @list{@port = @(* i 32),}))
22 };
23
24 enum pin_dir {
25   PIN_IN = 0,
26   PIN_OUT = 1,
27 };
28
29 extern void invalid_pin_error () __attribute__((error ("Invalid IO pin number.")));
30  
31 @(define INLINE "extern __inline__ __attribute__((always_inline))")
32 @(define-syntax-rule (switch pin_no i register template)
33    @list{switch(pin_no) {
34            @(add-newlines
35              (for/list ([i (in-naturals)]
36                         [register registers])
37                @list{case @(* i 32) ... @(sub1 (* (add1 i) 32)):
38                        @template}))
39            default:
40              invalid_pin_error ();
41              break;
42          }})
43
44 @INLINE
45 int pin_read (int pin_no)
46 {
47   @(switch @{pin_no} i register
48            @list{return @|register|->FIOPIN & (1 << (pin_no - @(* i 32)));})
49   return 0;
50 }
51
52 @INLINE
53 void pin_write (int pin_no, int value)
54 {
55   @(switch @{pin_no} i register
56            @list{if (value) {
57                    @|register|->FIOSET = (1 << (pin_no - @(* i 32)));
58                  } else {
59                    @|register|->FIOCLR = (1 << (pin_no - @(* i 32)));
60                  }
61                  break;})
62 }
63            
64 @INLINE
65 void pin_dir (int pin_no, enum pin_dir dir)
66 {
67   @(switch @{pin_no} i register
68            @list{if (dir == PIN_OUT) {
69                    @|register|->FIODIR @"|=" (1 << (pin_no - @(* i 32)));
70                  } else {
71                    @|register|->FIODIR @"&=" (1 << (pin_no - @(* i 32)));
72                  }
73                  break;})
74 }
75            
76 #endif