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