Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
ADS8344.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008- ENAC
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
23 #include "ADS8344.h"
24 #include "LPC21xx.h"
25 #include "armVIC.h"
26 #include BOARD_CONFIG
27 #include "led.h"
28 #include "mcu_periph/spi.h"
29 
30 #define ADS8344_SS_IODIR IO0DIR
31 #define ADS8344_SS_IOSET IO0SET
32 #define ADS8344_SS_IOCLR IO0CLR
33 #define ADS8344_SS_PIN 20
34 
35 #define ADS8344Select() SetBit(ADS8344_SS_IOCLR,ADS8344_SS_PIN)
36 #define ADS8344Unselect() SetBit(ADS8344_SS_IOSET,ADS8344_SS_PIN)
37 
40 
41 #define POWER_MODE (1 << 1 | 1)
42 #define SGL_DIF 1 // Single ended
43 
44 
45 /* set SSP input clock, PCLK / CPSDVSR = 750kHz */
46 /* SSP clock, 750kHz / (SCR+1) = 750kHz / 15 = 50kHz */
47 
48 #if (PCLK == 15000000)
49 #define CPSDVSR 20
50 #else
51 
52 #if (PCLK == 30000000)
53 #define CPSDVSR 40
54 #else
55 
56 #if (PCLK == 60000000)
57 #define CPSDVSR 80
58 #else
59 
60 #error unknown PCLK frequency
61 #endif
62 #endif
63 #endif
64 
65 /* SSPCR0 settings */
66 #define SSP_DSS 0x07 << 0 /* data size : 8 bits */
67 #define SSP_FRF 0x00 << 4 /* frame format : SPI */
68 #define SSP_CPOL 0x00 << 6 /* clock polarity : idle low */
69 #define SSP_CPHA 0x00 << 7 /* clock phase : 1 */
70 #define SSP_SCR 0x0E << 8 /* serial clock rate : 1MHz */
71 
72 /* SSPCR1 settings */
73 #define SSP_LBM 0x00 << 0 /* loopback mode : disabled */
74 #define SSP_SSE 0x00 << 1 /* SSP enable : disabled */
75 #define SSP_MS 0x00 << 2 /* master slave mode : master */
76 #define SSP_SOD 0x00 << 3 /* slave output disable : disabled */
77 
78 
79 static void SPI1_ISR(void) __attribute__((naked));
81 
82 #warning "This driver should be updated to use the new SPI peripheral"
83 
84 #ifndef SPI1_VIC_SLOT
85 #define SPI1_VIC_SLOT 7
86 #endif
87 
88 void ADS8344_init(void)
89 {
90  channel = 0;
91  ADS8344_available = false;
92 
93  /* setup pins for SSP (SCK, MISO, MOSI) */
94  PINSEL1 |= 2 << 2 | 2 << 4 | 2 << 6;
95 
96  /* setup SSP */
99  SSPCPSR = CPSDVSR; /* -> 50kHz */
100 
101  /* initialize interrupt vector */
102  VICIntSelect &= ~VIC_BIT(VIC_SPI1); // SPI1 selected as IRQ
103  VICIntEnable = VIC_BIT(VIC_SPI1); // SPI1 interrupt enabled
105  _VIC_CNTL(SPI1_VIC_SLOT) = (uint32_t)SPI1_ISR; /* address of the ISR */
106 
107  /* setup slave select */
108  /* configure SS pin */
109  SetBit(ADS8344_SS_IODIR, ADS8344_SS_PIN); /* pin is output */
110  ADS8344Unselect(); /* pin low */
111 }
112 
113 static inline void read_values(void)
114 {
115  uint8_t foo __attribute__((unused)) = SSPDR;
116  uint8_t msb = SSPDR;
117  uint8_t lsb = SSPDR;
118  uint8_t llsb = SSPDR;
119  ADS8344_values[channel] = (msb << 8 | lsb) << 1 | llsb >> 7;
120 }
121 
122 static inline void send_request(void)
123 {
124  uint8_t control = 1 << 7 | channel << 4 | SGL_DIF << 2 | POWER_MODE;
125 
126  SSPDR = control;
127  SSPDR = 0;
128  SSPDR = 0;
129  SSPDR = 0;
130 }
131 
132 void ADS8344_start(void)
133 {
134  ADS8344Select();
135  SpiClearRti();
136  SpiEnableRti();
137  SpiEnable();
138  send_request();
139 }
140 
141 void SPI1_ISR(void)
142 {
143  ISR_ENTRY();
144  LED_TOGGLE(2);
145  read_values();
146  channel++;
147  if (channel > 7) {
148  channel = 0;
149  ADS8344_available = true;
150  }
151  send_request();
152  SpiClearRti();
153 
154  VICVectAddr = 0x00000000; /* clear this interrupt from the VIC */
155  ISR_EXIT();
156 }
SSPCR1
#define SSPCR1
Definition: LPC21xx.h:223
uint16_t
unsigned short uint16_t
Definition: types.h:16
channel
static uint8_t channel
Definition: ADS8344.c:80
ADS8344_values
uint16_t ADS8344_values[NB_CHANNELS]
Definition: ADS8344.c:39
SSP_DSS
#define SSP_DSS
Definition: ADS8344.c:66
armVIC.h
LPC21xx.h
ADS8344.h
VICIntSelect
#define VICIntSelect
Definition: LPC21xx.h:430
spi.h
VIC_SPI1
#define VIC_SPI1
Definition: lpcVIC.h:81
SSPCR0
#define SSPCR0
Definition: LPC21xx.h:222
SSPCPSR
#define SSPCPSR
Definition: LPC21xx.h:226
uint32_t
unsigned long uint32_t
Definition: types.h:18
ADS8344_available
bool ADS8344_available
Definition: ADS8344.c:38
LED_TOGGLE
#define LED_TOGGLE(i)
Definition: led_hw.h:53
SSP_MS
#define SSP_MS
Definition: ADS8344.c:75
foo
uint16_t foo
Definition: main_demo5.c:59
SSP_LBM
#define SSP_LBM
Definition: ADS8344.c:73
SPI1_ISR
static void SPI1_ISR(void)
Definition: ADS8344.c:141
SpiClearRti
static void SpiClearRti(struct spi_periph *p)
Definition: spi_arch.c:161
VIC_BIT
#define VIC_BIT(chan)
Definition: lpcVIC.h:105
SpiEnableRti
static void SpiEnableRti(struct spi_periph *p)
Definition: spi_arch.c:151
ADS8344_SS_PIN
#define ADS8344_SS_PIN
Definition: ADS8344.c:33
read_values
static void read_values(void)
Definition: ADS8344.c:113
SSP_SCR
#define SSP_SCR
Definition: ADS8344.c:70
SpiEnable
#define SpiEnable()
Definition: spi_slave_hs_arch.h:44
uint8_t
unsigned char uint8_t
Definition: types.h:14
VICVectAddr
#define VICVectAddr
Definition: LPC21xx.h:436
led.h
arch independent LED (Light Emitting Diodes) API
NB_CHANNELS
#define NB_CHANNELS
Definition: ADS8344.h:28
send_request
static void send_request(void)
Definition: ADS8344.c:122
ISR_ENTRY
#define ISR_ENTRY()
Definition: armVIC.h:40
ISR_EXIT
#define ISR_EXIT()
Definition: armVIC.h:61
ADS8344Unselect
#define ADS8344Unselect()
Definition: ADS8344.c:36
ADS8344_init
void ADS8344_init(void)
Definition: ADS8344.c:88
ADS8344_start
void ADS8344_start(void)
Definition: ADS8344.c:132
ADS8344_SS_IODIR
#define ADS8344_SS_IODIR
Definition: ADS8344.c:30
SSP_CPHA
#define SSP_CPHA
Definition: ADS8344.c:69
SPI1_VIC_SLOT
#define SPI1_VIC_SLOT
Definition: ADS8344.c:85
POWER_MODE
#define POWER_MODE
Definition: ADS8344.c:41
SSPDR
#define SSPDR
Definition: LPC21xx.h:224
SSP_SOD
#define SSP_SOD
Definition: ADS8344.c:76
VIC_ENABLE
#define VIC_ENABLE
Definition: lpcVIC.h:102
VICIntEnable
#define VICIntEnable
Definition: LPC21xx.h:431
ADS8344Select
#define ADS8344Select()
Definition: ADS8344.c:35
PINSEL1
#define PINSEL1
Definition: LPC21xx.h:348
_VIC_CNTL
#define _VIC_CNTL(idx)
Definition: armVIC.h:19
SGL_DIF
#define SGL_DIF
Definition: ADS8344.c:42
SSP_CPOL
#define SSP_CPOL
Definition: ADS8344.c:68
SSP_FRF
#define SSP_FRF
Definition: ADS8344.c:67