In [1]:
(version)
Out[1]:
"8.3"
In [2]:
(require bhdl)
#<procedure:parameter-procedure>
In [3]:
(require racket
         racket/list
         racket/format
         (prefix-in pict: pict))
In [4]:
(define global
  (make-circuit
   (pin GND 3V3 5V VCC USB5V)))

Keyboard Matrix

In [5]:
;; A small circuit with one key and one diode
(define (key-with-diode)
  (make-circuit 
   (pin left right)
   (part [d (1N4148W)]
           [key (kailh-socket 1)])
   (wire (*- self.left key d self.right))
   (layout (vc-append key d))))
In [6]:
(show-layout (key-with-diode))
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388415021638841502265"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388415021638841502278"
Out[6]:
In [8]:
;; create a 4x5 matrix
(define matrix
  (for/list ([row (in-range 4)])
            (for/list ([col (in-range 5)])
                      (key-with-diode))))
In [10]:
;; connect the matrix
(define matrix-module
  (make-circuit 
   (pin row[4] col[5])
   ;; col in, row out
   (wire (for*/list ([i (in-range 4)]
                        [j (in-range 5)])
                       (debug "connecting" i j)
                       (*- self.col[j]
                           (list-ref (list-ref matrix i) j)
                           self.row[i])))
   (layout (inset (apply vc-append
             (for/list ([row matrix])
                      (apply hc-append row)
                      ))
                   20))))
DEBUG:  (connecting 0 0)
DEBUG:  (connecting 0 1)
DEBUG:  (connecting 0 2)
DEBUG:  (connecting 0 3)
DEBUG:  (connecting 0 4)
DEBUG:  (connecting 1 0)
DEBUG:  (connecting 1 1)
DEBUG:  (connecting 1 2)
DEBUG:  (connecting 1 3)
DEBUG:  (connecting 1 4)
DEBUG:  (connecting 2 0)
DEBUG:  (connecting 2 1)
DEBUG:  (connecting 2 2)
DEBUG:  (connecting 2 3)
DEBUG:  (connecting 2 4)
DEBUG:  (connecting 3 0)
DEBUG:  (connecting 3 1)
DEBUG:  (connecting 3 2)
DEBUG:  (connecting 3 3)
DEBUG:  (connecting 3 4)
In [11]:
(parameterize ([current-directory "./out/demo-board/matrix"]
               [padding-general 2])
    (circuit-export matrix-module
                    #:auto-place #f
                    #:formats '(kicad pdf png svg)))
generating KiCAD PCB ..
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388415741638841574205"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388415741638841574208"
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/matrix/out.kicad_pcb
DEBUG:  (Number of conflicts: 0)
generating pdf ..
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/matrix/out.pdf
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/matrix/out.png
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/matrix/out.svg
Out[11]:

MCU Board

In [12]:
(define (icsp-header)
   (make-circuit 
    (part [h (PinHeader2 3)])
     (pin MOSI SCK MISO RESET VCC GND)
                   (wire (*= (self (MISO VCC SCK MOSI RESET GND))
                                 (h [1 2 3 4 5 6])))
                   (layout h)))
In [13]:
;; Now using the Atmega32u4 from the arduino board
(define mcu-module 
  (make-circuit 
  (part [mcu (ATmega32U4 #:FP "TQFP-44")]
         [usb (USB-Micro)]
         [icsp (icsp-header)]
         [xtal (Crystal-4 '16MHz)])
 (pin row[4] col[5])
 (layout (vc-append 50 (rotate usb pi) mcu icsp))
 ;; define what are the row and col scan pins
 (wire (list (*= (self row [0 3])
               (self row [0 3])
               (mcu [PB0 PB4 PB5 PB6]))
              (*= (self col [0 4])
                 (mcu [PD0 PD1 PD2 PD3 PD4]))
   ;; use a Pin for blinking LED
            (*- mcu.PC6 (R '1k) (LED0603 'red) global.GND)
                    
 ;; connect the ICSP header
              (*= (mcu (MOSI SCLK MISO RESET VCC GND))
               (icsp (MOSI SCK MISO RESET VCC GND)))
 
    (list (*- mcu.VCC mcu.UVCC mcu.AVCC global.VCC)
         (*- mcu.GND mcu.UGND global.GND)
         ;; reset circuit and switch
         (*- mcu.RESET (*< (LL4148)
                           (R '10k)) global.5V)
       ;; FIXME [-1] because (Switch) is parsed as the index otherwise
         (*- mcu.RESET[-1] (Switch) global.GND)
       ;; capacitors
       (*- mcu.UCAP (C '1uf) global.GND)
       (*- global.VCC (*< (C '1uf)
                          (C '100nf)
                          (C '100nf)
                          (C '100nf)
                          (C '100nf))
           global.GND)
         ;; crystal
       (*- mcu.XTAL1 xtal.XIN)
       (*- xtal.XOUT mcu.XTAL2)
       (*- xtal.GND global.GND)
         (*- mcu.XTAL1 (C '22pf) global.GND)
         (*- mcu.XTAL2 (C '22pf) global.GND)
       ;; USB
       (*- mcu.D+ (R 22) usb.D+)
       (*- mcu.D- (R 22) usb.D-)
       ;; just use USB for supplying power
       (*- mcu.VBUS usb.VBUS global.VCC)
       (*- mcu.UGND usb.GND global.GND)
       )))))
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643000"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643004"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643009"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643011"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643014"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643016"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643018"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643021"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-63-15_16388416431638841643022"
In [14]:
;; connect MCU
(define mcu-board
  (make-circuit 
    (layout (inset (cb-superimpose mcu-module) 50 0))
    (wire mcu-module)))
In [15]:
(placer-url "http://localhost:8082")
In [16]:
(parameterize ([current-directory "./out/demo-board/mcu-board"]
               [padding-general 2])
    (circuit-export mcu-board
                    #:auto-place #t
                    #:formats '(kicad pdf bom png)))
DEBUG:  (sending for placement ..)
DEBUG:  (saving ..)
generating KiCAD PCB ..
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673629"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673691"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673695"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673698"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673700"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673702"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673705"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673708"
"DEBUG: /var/folders/jd/pqbc6_7j0y5gc2kjxjntw8c40000gn/T/PKGS-src-ROOT-BHDL-syst----iVc-fpio-main.rkt-95-15_16388416731638841673711"
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/mcu-board/out.kicad_pcb
Generating BOM ..
DEBUG:  (Number of conflicts: 0)
generating pdf ..
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/mcu-board/out.pdf
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/mcu-board/out.png
Out[16]:

Integrate MCU and Key matrix

In [17]:
(define keyboard (make-circuit
 (wire (*= (mcu-module row [0 3])
               (matrix-module row [0 3]))
  (*= (mcu-module col [0 4])
               (matrix-module col [0 4])))
 (layout (vc-append mcu-module matrix-module)
                  )))
In [18]:
(parameterize ([current-directory "./out/demo-board/keyboard"]
                 [padding-general 2])
                (circuit-export keyboard 
                                #:auto-place #t
                                #:formats '(kicad pdf png bom)))
DEBUG:  (sending for placement ..)
DEBUG:  (saving ..)
generating KiCAD PCB ..
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/keyboard/out.kicad_pcb
Generating BOM ..
DEBUG:  (Number of conflicts: 0)
generating pdf ..
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/keyboard/out.pdf
link: /Users/hebi/git/bhdl.org/notebooks/out/demo-board/keyboard/out.png
Out[18]:
In [ ]: