Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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  channel = 0;
91 
92  /* setup pins for SSP (SCK, MISO, MOSI) */
93  PINSEL1 |= 2 << 2 | 2 << 4 | 2 << 6;
94 
95  /* setup SSP */
98  SSPCPSR = CPSDVSR; /* -> 50kHz */
99 
100  /* initialize interrupt vector */
101  VICIntSelect &= ~VIC_BIT(VIC_SPI1); // SPI1 selected as IRQ
102  VICIntEnable = VIC_BIT(VIC_SPI1); // SPI1 interrupt enabled
104  _VIC_CNTL(SPI1_VIC_SLOT) = (uint32_t)SPI1_ISR; /* address of the ISR */
105 
106  /* setup slave select */
107  /* configure SS pin */
108  SetBit( ADS8344_SS_IODIR, ADS8344_SS_PIN); /* pin is output */
109  ADS8344Unselect(); /* pin low */
110 }
111 
112 static inline void read_values( void ) {
113  uint8_t foo __attribute__ ((unused)) = SSPDR;
114  uint8_t msb = SSPDR;
115  uint8_t lsb = SSPDR;
116  uint8_t llsb = SSPDR;
117  ADS8344_values[channel] = (msb << 8 | lsb) << 1 | llsb >> 7;
118 }
119 
120 static inline void send_request( void ) {
121  uint8_t control = 1 << 7 | channel << 4 | SGL_DIF << 2 | POWER_MODE;
122 
123  SSPDR = control;
124  SSPDR = 0;
125  SSPDR = 0;
126  SSPDR = 0;
127 }
128 
129 void ADS8344_start( void ) {
130  ADS8344Select();
131  SpiClearRti();
132  SpiEnableRti();
133  SpiEnable();
134  send_request();
135 }
136 
137 void SPI1_ISR(void) {
138  ISR_ENTRY();
139  LED_TOGGLE(2);
140  read_values();
141  channel++;
142  if (channel > 7) {
143  channel = 0;
145  }
146  send_request();
147  SpiClearRti();
148 
149  VICVectAddr = 0x00000000; /* clear this interrupt from the VIC */
150  ISR_EXIT();
151 }
#define VICIntSelect
Definition: LPC21xx.h:430
unsigned short uint16_t
Definition: types.h:16
#define SSP_CPOL
Definition: ADS8344.c:68
#define SSP_FRF
Definition: ADS8344.c:67
static void SpiClearRti(struct spi_periph *p)
Definition: spi_arch.c:151
#define SSPCPSR
Definition: LPC21xx.h:226
#define _VIC_CNTL(idx)
Definition: armVIC.h:19
uint16_t ADS8344_values[NB_CHANNELS]
Definition: ADS8344.c:39
#define ADS8344_SS_PIN
Definition: ADS8344.c:33
#define SSP_LBM
Definition: ADS8344.c:73
bool_t ADS8344_available
Definition: ADS8344.c:38
#define SSPCR0
Definition: LPC21xx.h:222
static void SpiEnableRti(struct spi_periph *p)
Definition: spi_arch.c:143
#define FALSE
Definition: imu_chimu.h:141
#define SSP_MS
Definition: ADS8344.c:75
Architecture independent SPI (Serial Peripheral Interface) API.
static void read_values(void)
Definition: ADS8344.c:112
#define SSP_SCR
Definition: ADS8344.c:70
static void SPI1_ISR(void)
Definition: ADS8344.c:137
#define VICVectAddr
Definition: LPC21xx.h:436
unsigned long uint32_t
Definition: types.h:18
#define SpiEnable()
#define ADS8344Unselect()
Definition: ADS8344.c:36
uint16_t foo
Definition: main_demo5.c:54
#define ADS8344_SS_IODIR
Definition: ADS8344.c:30
#define POWER_MODE
Definition: ADS8344.c:41
#define LED_TOGGLE(i)
Definition: led_hw.h:30
static void send_request(void)
Definition: ADS8344.c:120
#define VIC_BIT(chan)
Definition: lpcVIC.h:105
#define TRUE
Definition: imu_chimu.h:144
#define SSP_CPHA
Definition: ADS8344.c:69
#define SPI1_VIC_SLOT
Definition: ADS8344.c:85
unsigned char uint8_t
Definition: types.h:14
#define SSPDR
Definition: LPC21xx.h:224
#define ISR_EXIT()
Definition: armVIC.h:61
#define SSP_SOD
Definition: ADS8344.c:76
#define VICIntEnable
Definition: LPC21xx.h:431
void ADS8344_start(void)
Definition: ADS8344.c:129
#define PINSEL1
Definition: LPC21xx.h:348
void ADS8344_init(void)
Definition: ADS8344.c:88
#define NB_CHANNELS
Definition: ADS8344.h:28
#define VIC_SPI1
Definition: lpcVIC.h:81
arch independent LED (Light Emitting Diodes) API
#define SSPCR1
Definition: LPC21xx.h:223
#define SGL_DIF
Definition: ADS8344.c:42
static uint8_t channel
Definition: ADS8344.c:80
#define ISR_ENTRY()
Definition: armVIC.h:40
#define VIC_ENABLE
Definition: lpcVIC.h:102
#define ADS8344Select()
Definition: ADS8344.c:35
#define SSP_DSS
Definition: ADS8344.c:66