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
baro_scp.c
Go to the documentation of this file.
1 #include "std.h"
2 #include "mcu_periph/sys_time.h"
3 #include "led.h"
4 #include "mcu.h"
5 
6 #include "mcu_periph/uart.h"
7 #include "messages.h"
9 
10 #include "mcu_periph/spi.h"
11 
13 
14 #ifndef SENSOR_SYNC_SEND
15 #warning set SENSOR_SYNC_SEND to use baro_scp
16 #endif
17 
18 #ifndef DOWNLINK_DEVICE
19 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
20 #endif
21 
22 #define STA_UNINIT 0
23 #define STA_INITIALISING 1
24 #define STA_IDLE 2
25 
30 
31 static void baro_scp_start_high_res_measurement(void);
32 static void baro_scp_read(void);
33 static void EXTINT_ISR(void) __attribute__((naked));
34 static void SPI1_ISR(void) __attribute__((naked));
35 
36 void baro_scp_periodic(void) {
37  if (baro_scp_status == STA_UNINIT && sys_time.nb_sec > 1) {
40  }
41 }
42 
43 /* ssp input clock 468.75kHz, clock that divided by SCR+1 */
44 #define SSP_CLOCK 468750
45 
46 /* SSPCR0 settings */
47 #define SSP_DDS 0x07 << 0 /* data size : 8 bits */
48 #define SSP_FRF 0x00 << 4 /* frame format : SPI */
49 #define SSP_CPOL 0x00 << 6 /* clock polarity : data captured on first clock transition */
50 #define SSP_CPHA 0x00 << 7 /* clock phase : SCK idles low */
51 #define SSP_SCR 0x0F << 8 /* serial clock rate : divide by 16 */
52 
53 /* SSPCR1 settings */
54 #define SSP_LBM 0x00 << 0 /* loopback mode : disabled */
55 #define SSP_SSE 0x00 << 1 /* SSP enable : disabled */
56 #define SSP_MS 0x00 << 2 /* master slave mode : master */
57 #define SSP_SOD 0x00 << 3 /* slave output disable : don't care when master */
58 
59 #define SS_PIN 20
60 #define SS_IODIR IO0DIR
61 #define SS_IOSET IO0SET
62 #define SS_IOCLR IO0CLR
63 
64 #define ScpSelect() SetBit(SS_IOCLR,SS_PIN)
65 #define ScpUnselect() SetBit(SS_IOSET,SS_PIN)
66 
67 #warning "This driver should be updated to use the new SPI peripheral"
68 
69 #ifndef SPI1_VIC_SLOT
70 #define SPI1_VIC_SLOT 7
71 #endif
72 
73 void baro_scp_init( void ) {
74  /* setup pins for SSP (SCK, MISO, MOSI) */
75  PINSEL1 |= 2 << 2 | 2 << 4 | 2 << 6;
76 
77  /* setup SSP */
80  /* set prescaler for SSP clock */
82 
83  /* initialize interrupt vector */
84  VICIntSelect &= ~VIC_BIT(VIC_SPI1); // SPI1 selected as IRQ
85  VICIntEnable = VIC_BIT(VIC_SPI1); // SPI1 interrupt enabled
87  _VIC_CNTL(SPI1_VIC_SLOT) = (uint32_t)SPI1_ISR; /* address of the ISR */
88 
89  /* configure SS pin */
90  SetBit(SS_IODIR, SS_PIN); /* pin is output */
91  ScpUnselect(); /* pin idles high */
92 
93  /* configure DRDY pin */
94  /* connected pin to EXINT */
96  SetBit(EXTMODE, SPI1_DRDY_EINT); /* EINT is edge trigered */
97  SetBit(EXTPOLAR,SPI1_DRDY_EINT); /* EINT is trigered on rising edge */
98  SetBit(EXTINT,SPI1_DRDY_EINT); /* clear pending EINT */
99 
100  /* initialize interrupt vector */
101  VICIntSelect &= ~VIC_BIT( SPI1_DRDY_VIC_IT ); /* select EINT as IRQ source */
102  VICIntEnable = VIC_BIT( SPI1_DRDY_VIC_IT ); /* enable it */
104  VICVectAddr11 = (uint32_t)EXTINT_ISR; // address of the ISR
105 
107 }
108 
109 void SPI1_ISR(void) {
110  ISR_ENTRY();
111 
113  uint8_t foo1 = SSPDR;
114  uint8_t foo2 = SSPDR;
116  foo1=foo2;
117  }
118  else if (baro_scp_status == STA_IDLE) {
119 
120  uint8_t foo0 = SSPDR;
123  if (baro_scp_temperature & 0x2000) {
124  baro_scp_temperature |= 0xC000;
125  }
127 
128  uint8_t foo1 = SSPDR;
129  uint32_t datard8 = SSPDR<<16;
130  uint8_t foo2 = SSPDR;
133  baro_scp_pressure += datard8;
134  baro_scp_pressure *= 25;
136  foo1=foo2;
137  foo0=foo2;
138  }
139 
140  ScpUnselect();
141  SpiClearRti();
142  SpiDisable();
143 
144  VICVectAddr = 0x00000000; /* clear this interrupt from the VIC */
145  ISR_EXIT();
146 }
147 
148 void EXTINT_ISR(void) {
149  ISR_ENTRY();
150  baro_scp_read();
151 
152  SetBit(EXTINT,SPI1_DRDY_EINT); /* clear EINT2 */
153  VICVectAddr = 0x00000000; /* clear this interrupt from the VIC */
154  ISR_EXIT();
155 }
156 
157 /* write 0x0A to 0x03 */
159  uint8_t cmd = 0x03<<2|0x02;
160  uint8_t data = 0x0A;
161  ScpSelect();
162  SSPDR = cmd;
163  SSPDR = data;
164  SpiEnableRti();
165  SpiEnable();
166 }
167 
168 /* read 0x21 (TEMP), 0x1F (MSB) and 0x20 (LSB) */
169 static void baro_scp_read(void) {
170  uint8_t cmd0 = 0x21 << 2;
171  uint8_t cmd1 = 0x1F << 2;
172  uint8_t cmd2 = 0x20 << 2;
173  ScpSelect();
174  SSPDR = cmd0;
175  SSPDR = 0;
176  SSPDR = 0;
177  SSPDR = cmd1;
178  SSPDR = 0;
179  SSPDR = cmd2;
180  SSPDR = 0;
181  SSPDR = 0;
182  SpiEnable();
183 }
184 
185 void baro_scp_event( void ) {
186  if (baro_scp_available == TRUE) {
187 #ifdef SENSOR_SYNC_SEND
189 #endif
191  }
192 }
#define VICIntSelect
Definition: LPC21xx.h:430
unsigned short uint16_t
Definition: types.h:16
static void baro_scp_start_high_res_measurement(void)
Definition: baro_scp.c:158
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define STA_INITIALISING
Definition: baro_scp.c:23
#define SPI1_DRDY_PINSEL_VAL
Definition: logom_2.6.h:186
#define SSP_SCR
Definition: baro_scp.c:51
#define SPI1_DRDY_EINT
Definition: logom_2.6.h:187
#define SSP_CLOCK
Definition: baro_scp.c:44
void baro_scp_init(void)
Definition: baro_scp.c:73
#define SS_PIN
Definition: baro_scp.c:59
#define VICVectAddr11
Definition: LPC21xx.h:449
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
#define SSP_FRF
Definition: baro_scp.c:48
#define PCLK
Definition: booz_1.0.h:18
#define EXTMODE
Definition: LPC21xx.h:419
static void baro_scp_read(void)
Definition: baro_scp.c:169
#define SpiDisable()
#define SSPCR0
Definition: LPC21xx.h:222
uint32_t baro_scp_pressure
Definition: baro_scp.c:27
static void SpiEnableRti(struct spi_periph *p)
Definition: spi_arch.c:143
void baro_scp_event(void)
Definition: baro_scp.c:185
#define FALSE
Definition: imu_chimu.h:141
#define STA_UNINIT
Definition: baro_scp.c:22
#define SSP_LBM
Definition: baro_scp.c:54
Architecture independent SPI (Serial Peripheral Interface) API.
#define SSP_CPHA
Definition: baro_scp.c:50
#define EXTPOLAR
Definition: LPC21xx.h:420
static void SPI1_ISR(void)
Definition: baro_scp.c:109
#define SSP_MS
Definition: baro_scp.c:56
#define EXTINT
Definition: LPC21xx.h:417
Architecture independent timing functions.
uint16_t baro_scp_temperature
Definition: baro_scp.c:28
#define VICVectAddr
Definition: LPC21xx.h:436
unsigned long uint32_t
Definition: types.h:18
#define SpiEnable()
#define SPI1_VIC_SLOT
Definition: baro_scp.c:70
#define SSP_CPOL
Definition: baro_scp.c:49
#define ScpSelect()
Definition: baro_scp.c:64
#define VIC_BIT(chan)
Definition: lpcVIC.h:105
#define SPI1_DRDY_VIC_IT
Definition: logom_2.6.h:188
#define TRUE
Definition: imu_chimu.h:144
Arch independent mcu ( Micro Controller Unit ) utilities.
unsigned char uint8_t
Definition: types.h:14
#define SPI1_DRDY_PINSEL
Definition: logom_2.6.h:184
#define SSPDR
Definition: LPC21xx.h:224
#define ISR_EXIT()
Definition: armVIC.h:61
void baro_scp_periodic(void)
Definition: baro_scp.c:36
bool_t baro_scp_available
Definition: baro_scp.c:29
#define VICIntEnable
Definition: LPC21xx.h:431
#define PINSEL1
Definition: LPC21xx.h:348
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:69
#define VICVectCntl11
Definition: LPC21xx.h:465
#define VIC_SPI1
Definition: lpcVIC.h:81
#define ScpUnselect()
Definition: baro_scp.c:65
arch independent LED (Light Emitting Diodes) API
uint8_t baro_scp_status
Definition: baro_scp.c:26
#define SSPCR1
Definition: LPC21xx.h:223
#define STA_IDLE
Definition: baro_scp.c:24
#define SS_IODIR
Definition: baro_scp.c:60
#define SPI1_DRDY_PINSEL_BIT
Definition: logom_2.6.h:185
#define SSP_DDS
Definition: baro_scp.c:47
#define SSP_SOD
Definition: baro_scp.c:57
#define ISR_ENTRY()
Definition: armVIC.h:40
static void EXTINT_ISR(void)
Definition: baro_scp.c:148
#define VIC_ENABLE
Definition: lpcVIC.h:102