Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros 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 && cpu_time_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 void baro_scp_init( void ) {
68  /* setup pins for SSP (SCK, MISO, MOSI) */
69  PINSEL1 |= 2 << 2 | 2 << 4 | 2 << 6;
70 
71  /* setup SSP */
74  /* set prescaler for SSP clock */
76 
77  /* initialize interrupt vector */
78  VICIntSelect &= ~VIC_BIT(VIC_SPI1); // SPI1 selected as IRQ
79  VICIntEnable = VIC_BIT(VIC_SPI1); // SPI1 interrupt enabled
81  VICVectAddr7 = (uint32_t)SPI1_ISR; // address of the ISR
82 
83  /* configure SS pin */
84  SetBit(SS_IODIR, SS_PIN); /* pin is output */
85  ScpUnselect(); /* pin idles high */
86 
87  /* configure DRDY pin */
88  /* connected pin to EXINT */
90  SetBit(EXTMODE, SPI1_DRDY_EINT); /* EINT is edge trigered */
91  SetBit(EXTPOLAR,SPI1_DRDY_EINT); /* EINT is trigered on rising edge */
92  SetBit(EXTINT,SPI1_DRDY_EINT); /* clear pending EINT */
93 
94  /* initialize interrupt vector */
95  VICIntSelect &= ~VIC_BIT( SPI1_DRDY_VIC_IT ); /* select EINT as IRQ source */
96  VICIntEnable = VIC_BIT( SPI1_DRDY_VIC_IT ); /* enable it */
98  VICVectAddr11 = (uint32_t)EXTINT_ISR; // address of the ISR
99 
101 }
102 
103 void SPI1_ISR(void) {
104  ISR_ENTRY();
105 
107  uint8_t foo1 = SSPDR;
108  uint8_t foo2 = SSPDR;
110  foo1=foo2;
111  }
112  else if (baro_scp_status == STA_IDLE) {
113 
114  uint8_t foo0 = SSPDR;
117  if (baro_scp_temperature & 0x2000) {
118  baro_scp_temperature |= 0xC000;
119  }
121 
122  uint8_t foo1 = SSPDR;
123  uint32_t datard8 = SSPDR<<16;
124  uint8_t foo2 = SSPDR;
127  baro_scp_pressure += datard8;
128  baro_scp_pressure *= 25;
130  foo1=foo2;
131  foo0=foo2;
132  }
133 
134  ScpUnselect();
135  SpiClearRti();
136  SpiDisable();
137 
138  VICVectAddr = 0x00000000; /* clear this interrupt from the VIC */
139  ISR_EXIT();
140 }
141 
142 void EXTINT_ISR(void) {
143  ISR_ENTRY();
144  baro_scp_read();
145 
146  SetBit(EXTINT,SPI1_DRDY_EINT); /* clear EINT2 */
147  VICVectAddr = 0x00000000; /* clear this interrupt from the VIC */
148  ISR_EXIT();
149 }
150 
151 /* write 0x0A to 0x03 */
153  uint8_t cmd = 0x03<<2|0x02;
154  uint8_t data = 0x0A;
155  ScpSelect();
156  SSPDR = cmd;
157  SSPDR = data;
158  SpiEnableRti();
159  SpiEnable();
160 }
161 
162 /* read 0x21 (TEMP), 0x1F (MSB) and 0x20 (LSB) */
163 static void baro_scp_read(void) {
164  uint8_t cmd0 = 0x21 << 2;
165  uint8_t cmd1 = 0x1F << 2;
166  uint8_t cmd2 = 0x20 << 2;
167  ScpSelect();
168  SSPDR = cmd0;
169  SSPDR = 0;
170  SSPDR = 0;
171  SSPDR = cmd1;
172  SSPDR = 0;
173  SSPDR = cmd2;
174  SSPDR = 0;
175  SSPDR = 0;
176  SpiEnable();
177 }
178 
179 void baro_scp_event( void ) {
180  if (baro_scp_available == TRUE) {
181 #ifdef SENSOR_SYNC_SEND
183 #endif
185  }
186 }
#define VICIntSelect
Definition: LPC21xx.h:398
unsigned short uint16_t
Definition: types.h:16
static void baro_scp_start_high_res_measurement(void)
Definition: baro_scp.c:152
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:67
#define SS_PIN
Definition: baro_scp.c:59
#define VICVectAddr11
Definition: LPC21xx.h:417
#define SSPCPSR
Definition: LPC21xx.h:226
#define SSP_FRF
Definition: baro_scp.c:48
#define PCLK
Definition: booz_1.0.h:18
#define EXTMODE
Definition: LPC21xx.h:387
static void baro_scp_read(void)
Definition: baro_scp.c:163
#define cpu_time_sec
Definition: sys_time.h:64
#define SSPCR0
Definition: LPC21xx.h:222
#define SpiClearRti()
Definition: spi_arch.h:84
uint32_t baro_scp_pressure
Definition: baro_scp.c:27
void baro_scp_event(void)
Definition: baro_scp.c:179
#define FALSE
Definition: imu_chimu.h:141
#define STA_UNINIT
Definition: baro_scp.c:22
#define SSP_LBM
Definition: baro_scp.c:54
arch independent SPI (Serial Peripheral Interface) API
#define SSP_CPHA
Definition: baro_scp.c:50
#define EXTPOLAR
Definition: LPC21xx.h:388
void SPI1_ISR(void)
Definition: baro_scp.c:103
#define SSP_MS
Definition: baro_scp.c:56
#define EXTINT
Definition: LPC21xx.h:385
Architecture independent timing functions.
uint16_t baro_scp_temperature
Definition: baro_scp.c:28
#define VICVectAddr
Definition: LPC21xx.h:404
unsigned long uint32_t
Definition: types.h:18
#define SpiEnable()
Definition: spi_arch.h:68
#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 SpiDisable()
Definition: spi_arch.h:72
#define SPI1_DRDY_PINSEL
Definition: logom_2.6.h:184
#define SSPDR
Definition: LPC21xx.h:224
#define ISR_EXIT()
Definition: armVIC.h:61
bool_t baro_scp_available
Definition: baro_scp.c:29
#define VICIntEnable
Definition: LPC21xx.h:399
#define PINSEL1
Definition: LPC21xx.h:316
#define VICVectAddr7
Definition: LPC21xx.h:413
#define SpiEnableRti()
Definition: spi_arch.h:76
void baro_scp_periodic(void)
Definition: baro_scp_i2c.c:49
#define VICVectCntl11
Definition: LPC21xx.h:433
#define VIC_SPI1
Definition: lpcVIC.h:81
#define ScpUnselect()
Definition: baro_scp.c:65
arch independent LED (Light Emitting Diodes) API
static void EXTINT_ISR(void)
Definition: baro_scp.c:33
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
__attribute__((always_inline))
Definition: i2c_arch.c:35
#define SSP_DDS
Definition: baro_scp.c:47
#define SSP_SOD
Definition: baro_scp.c:57
#define ISR_ENTRY()
Definition: armVIC.h:40
#define VIC_ENABLE
Definition: lpcVIC.h:102
#define VICVectCntl7
Definition: LPC21xx.h:429