Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / wiringPi / max5322.c @ 32:cadb9025f1e0

History | View | Annotate | Download (2.32 KB)

1
/*
2
 * max5322.c:
3
 *        Extend wiringPi with the MAX5322 SPI Digital to Analog convertor
4
 *        Copyright (c) 2012-2013 Gordon Henderson
5
 ***********************************************************************
6
 * This file is part of wiringPi:
7
 *        https://projects.drogon.net/raspberry-pi/wiringpi/
8
 *
9
 *    wiringPi is free software: you can redistribute it and/or modify
10
 *    it under the terms of the GNU Lesser General Public License as
11
 *    published by the Free Software Foundation, either version 3 of the
12
 *    License, or (at your option) any later version.
13
 *
14
 *    wiringPi is distributed in the hope that it will be useful,
15
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *    GNU Lesser General Public License for more details.
18
 *
19
 *    You should have received a copy of the GNU Lesser General Public
20
 *    License along with wiringPi.
21
 *    If not, see <http://www.gnu.org/licenses/>.
22
 ***********************************************************************
23
 */
24

    
25
#include <wiringPi.h>
26
#include <wiringPiSPI.h>
27

    
28
#include "max5322.h"
29

    
30
/*
31
 * myAnalogWrite:
32
 *        Write analog value on the given pin
33
 *********************************************************************************
34
 */
35

    
36
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
37
{
38
  unsigned char spiData [2] ;
39
  unsigned char chanBits, dataBits ;
40
  int chan = pin - node->pinBase ;
41

    
42
  if (chan == 0)
43
    chanBits = 0b01000000 ;
44
  else
45
    chanBits = 0b01010000 ;
46

    
47
  chanBits |= ((value >> 12) & 0x0F) ;
48
  dataBits  = ((value      ) & 0xFF) ;
49

    
50
  spiData [0] = chanBits ;
51
  spiData [1] = dataBits ;
52

    
53
  wiringPiSPIDataRW (node->fd, spiData, 2) ;
54
}
55

    
56
/*
57
 * max5322Setup:
58
 *        Create a new wiringPi device node for an max5322 on the Pi's
59
 *        SPI interface.
60
 *********************************************************************************
61
 */
62

    
63
int max5322Setup (const int pinBase, int spiChannel)
64
{
65
  struct wiringPiNodeStruct *node ;
66
  unsigned char spiData [2] ;
67

    
68
  if (wiringPiSPISetup (spiChannel, 8000000) < 0)        // 10MHz Max
69
    return FALSE ;
70

    
71
  node = wiringPiNewNode (pinBase, 2) ;
72

    
73
  node->fd          = spiChannel ;
74
  node->analogWrite = myAnalogWrite ;
75

    
76
// Enable both DACs
77

    
78
  spiData [0] = 0b11100000 ;
79
  spiData [1] = 0 ;
80
  
81
  wiringPiSPIDataRW (node->fd, spiData, 2) ;
82

    
83
  return TRUE ;
84
}