;; ------------------------------------ ;; ;; LPC-H2148, SFE-Logomatic2 ;; ;; ------------------------------------ ;; ------------------------------------ ;; 1977 bytes (without comments) ;; ------------------------------------ (library (system 0) (export sysc psl0 gio0 gio1 uar0 uar1 tmr0 tmr1 spi0 spi1 i2c0 i2c1 adc0 adc1 pwm0 rtc0 register-copy-bit config-power config-pin pin-set-dir pin-set pin-clear pin-set? stop restart spi-put spi-get) (import) (define sysc #xE01FC00) (define psl0 #xE002c00) (define rtc0 #xE002400) (define gio0 #xE002800) (define gio1 #xE002801) (define tmr0 #xE000400) (define tmr1 #xE000800) (define uar0 #xE000C00) (define uar1 #xE001000) (define pwm0 #xE001400) (define i2c0 #xE001C00) (define i2c1 #xE005C00) (define spi0 #xE002000) (define spi1 #xE006800) (define adc0 #xE003400) (define adc1 #xE006000) (define (register-copy-bit reg ofst bpos bval) (write (bitwise-copy-bit (read reg (- ofst)) bpos bval) reg (- ofst))) (define (config-power bit val) (register-copy-bit sysc #xc4 bit val)) ;; eg. (config-pin 0 10 #b10) ; select CAP1.0 function for P0.10 ;; eg. (config-pin 0 21 #b01) ; select PWM5 function for P0.21 (define (config-pin main sub cfg) (let ((pos (bitwise-arithmetic-shift (modulo sub 16) 1)) (ofst (- (if (< sub 16) 0 -4) (bitwise-arithmetic-shift main 3)))) (write (bitwise-copy-bit-field (read psl0 ofst) pos (+ pos 2) cfg) psl0 ofst))) (define (pin-set-dir port pin dir) (register-copy-bit port #x08 pin dir)) (define (pin-set port pin) (if (< pin 29) (write (bitwise-arithmetic-shift 1 pin) port #x04) (write (bitwise-arithmetic-shift #vu8(1 0 0 0) pin) port #x-04))) (define (pin-clear port pin) (if (< pin 29) (write (bitwise-arithmetic-shift 1 pin) port #x0c) (write (bitwise-arithmetic-shift #vu8(1 0 0 0) pin) port #x-0c))) (define (pin-set? port pin) (if (< pin 29) (bitwise-bit-set? (read port #x00) pin) (bitwise-bit-set? (read port #x-01) pin))) (define (stop tmr) (write 0 tmr #x04)) (define (restart tmr) (write 2 tmr #x04) (write 1 tmr #x04)) (define (spi-put port val) (write val port #x08)) (define (spi-get port) (let loop ((stat 0)) (if (bitwise-bit-set? stat 7) (read port #x08) (loop (read port #x04))))) ) ; end of library