Skip to content

Commit

Permalink
Update bit timing calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Mar 11, 2016
1 parent 3fb425e commit 5fcc8f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
simulator
*.swp
*.sw?
Session.vim
22 changes: 16 additions & 6 deletions mcp2515/mcp2515.go
Expand Up @@ -20,7 +20,9 @@ type MCP2515 struct {
mu sync.RWMutex
}

var prescalers = map[int]uint8{

// Prescalers for a 16 MHz oscillator with a 8 Time Quanta bit time
var prescalers = map[int]int{
125000: 7,
250000: 3,
500000: 1,
Expand All @@ -47,7 +49,9 @@ func (d *MCP2515) Setup(baudRate int) error {
glog.V(1).Infof("mcp2515: setup")

d.baudRate = baudRate
prescaler, ok := prescalers[baudRate]
//prescaler, ok := prescalers[baudRate]
prescaler := 3
ok := true
if !ok {
return fmt.Errorf("Baud rate not supported %v", baudRate)
}
Expand Down Expand Up @@ -84,17 +88,23 @@ func (d *MCP2515) Setup(baudRate int) error {
}

// Configures baud rate and synchronization jump width (SJW)
func initialCNF1(prescaler uint8) uint8 {
return prescaler
func initialCNF1(prescaler int) uint8 {
// SJW = 1 TQ
return uint8(prescaler)
}

// Configure bit timing to have 1 bit = 8 TQ
func initialCNF2() uint8 {
// PRSEG = 1 TQ
// PHSEG1 = 3 TQ
// BTLMODE = 1 => Use CNF3 for PHSEG2
return (1 << bits["BTLMODE"]) |
(1 << bits["PHSEG11"])
(2 << bits["PHSEG10"])
}

func initialCNF3() uint8 {
return (1 << bits["PHSEG21"])
// PHSEG2 = 3 TQ
return (2 << bits["PHSEG20"])
}

func initialRXB0CTRL() uint8 {
Expand Down

0 comments on commit 5fcc8f3

Please sign in to comment.