Paparazzi UAS  v5.15_devel-81-gd13dafb
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces 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 #include "subsystems/abi.h"
6 
7 #include "mcu_periph/uart.h"
8 #include "pprzlink/messages.h"
10 
11 #include "mcu_periph/spi.h"
12 
14 
15 #ifndef SENSOR_SYNC_SEND
16 #warning set SENSOR_SYNC_SEND to use baro_scp
17 #endif
18 
19 
20 #define STA_UNINIT 0
21 #define STA_INITIALISING 1
22 #define STA_IDLE 2
23 
28 
29 static void baro_scp_start_high_res_measurement(void);
30 static void baro_scp_read(void);
31 static void EXTINT_ISR(void) __attribute__((naked));
32 static void SPI1_ISR(void) __attribute__((naked));
33 
35 {
36  if (baro_scp_status == STA_UNINIT && sys_time.nb_sec > 1) {
39  }
40 }
41 
42 /* ssp input clock 468.75kHz, clock that divided by SCR+1 */
43 #define SSP_CLOCK 468750
44 
45 /* SSPCR0 settings */
46 #define SSP_DDS 0x07 << 0 /* data size : 8 bits */
47 #define SSP_FRF 0x00 << 4 /* frame format : SPI */
48 #define SSP_CPOL 0x00 << 6 /* clock polarity : data captured on first clock transition */
49 #define SSP_CPHA 0x00 << 7 /* clock phase : SCK idles low */
50 #define SSP_SCR 0x0F << 8 /* serial clock rate : divide by 16 */
51 
52 /* SSPCR1 settings */
53 #define SSP_LBM 0x00 << 0 /* loopback mode : disabled */
54 #define SSP_SSE 0x00 << 1 /* SSP enable : disabled */
55 #define SSP_MS 0x00 << 2 /* master slave mode : master */
56 #define SSP_SOD 0x00 << 3 /* slave output disable : don't care when master */
57 
58 #define SS_PIN 20
59 #define SS_IODIR IO0DIR
60 #define SS_IOSET IO0SET
61 #define SS_IOCLR IO0CLR
62 
63 #define ScpSelect() SetBit(SS_IOCLR,SS_PIN)
64 #define ScpUnselect() SetBit(SS_IOSET,SS_PIN)
65 
66 #warning "This driver should be updated to use the new SPI peripheral"
67 
68 #ifndef SPI1_VIC_SLOT
69 #define SPI1_VIC_SLOT 7
70 #endif
71 
72 void baro_scp_init(void)
73 {
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 {
111  ISR_ENTRY();
112 
114  uint8_t foo1 = SSPDR;
115  uint8_t foo2 = SSPDR;
117  foo1 = foo2;
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;
131  baro_scp_pressure = SSPDR << 8;
133  baro_scp_pressure += datard8;
134  baro_scp_pressure *= 25;
135  baro_scp_available = true;
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 {
150  ISR_ENTRY();
151  baro_scp_read();
152 
153  SetBit(EXTINT, SPI1_DRDY_EINT); /* clear EINT2 */
154  VICVectAddr = 0x00000000; /* clear this interrupt from the VIC */
155  ISR_EXIT();
156 }
157 
158 /* write 0x0A to 0x03 */
160 {
161  uint8_t cmd = 0x03 << 2 | 0x02;
162  uint8_t data = 0x0A;
163  ScpSelect();
164  SSPDR = cmd;
165  SSPDR = data;
166  SpiEnableRti();
167  SpiEnable();
168 }
169 
170 /* read 0x21 (TEMP), 0x1F (MSB) and 0x20 (LSB) */
171 static void baro_scp_read(void)
172 {
173  uint8_t cmd0 = 0x21 << 2;
174  uint8_t cmd1 = 0x1F << 2;
175  uint8_t cmd2 = 0x20 << 2;
176  ScpSelect();
177  SSPDR = cmd0;
178  SSPDR = 0;
179  SSPDR = 0;
180  SSPDR = cmd1;
181  SSPDR = 0;
182  SSPDR = cmd2;
183  SSPDR = 0;
184  SSPDR = 0;
185  SpiEnable();
186 }
187 
188 void baro_scp_event(void)
189 {
190  if (baro_scp_available == TRUE) {
191  uint32_t now_ts = get_sys_time_usec();
192  float pressure = (float)baro_scp_pressure;
193  AbiSendMsgBARO_ABS(BARO_SCP_SENDER_ID, now_ts, pressure);
194 #ifdef SENSOR_SYNC_SEND
196 #endif
197  baro_scp_available = false;
198  }
199 }
#define VICIntSelect
Definition: LPC21xx.h:430
#define SPI1_DRDY_PINSEL_VAL
Definition: hbmini_1.0.h:90
unsigned short uint16_t
Definition: types.h:16
#define SPI1_DRDY_VIC_IT
Definition: hbmini_1.0.h:92
static void baro_scp_start_high_res_measurement(void)
Definition: baro_scp.c:159
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define STA_INITIALISING
Definition: baro_scp.c:21
#define SSP_SCR
Definition: baro_scp.c:50
#define SSP_CLOCK
Definition: baro_scp.c:43
void baro_scp_init(void)
Definition: baro_scp.c:72
#define SS_PIN
Definition: baro_scp.c:58
#define VICVectAddr11
Definition: LPC21xx.h:449
static void SpiClearRti(struct spi_periph *p)
Definition: spi_arch.c:161
#define SSPCPSR
Definition: LPC21xx.h:226
#define _VIC_CNTL(idx)
Definition: armVIC.h:19
#define SPI1_DRDY_PINSEL
Definition: hbmini_1.0.h:88
struct Ms5611Data data
Definition: ms5611_spi.h:45
#define SSP_FRF
Definition: baro_scp.c:47
Main include for ABI (AirBorneInterface).
#define PCLK
Definition: booz_1.0.h:18
#define EXTMODE
Definition: LPC21xx.h:419
#define SPI1_DRDY_EINT
Definition: hbmini_1.0.h:91
static void baro_scp_read(void)
Definition: baro_scp.c:171
#define SpiDisable()
#define SSPCR0
Definition: LPC21xx.h:222
uint32_t baro_scp_pressure
Definition: baro_scp.c:25
static void SpiEnableRti(struct spi_periph *p)
Definition: spi_arch.c:151
void baro_scp_event(void)
Definition: baro_scp.c:188
#define STA_UNINIT
Definition: baro_scp.c:20
#define SSP_LBM
Definition: baro_scp.c:53
Architecture independent SPI (Serial Peripheral Interface) API.
#define SSP_CPHA
Definition: baro_scp.c:49
#define TRUE
Definition: std.h:4
#define EXTPOLAR
Definition: LPC21xx.h:420
static void SPI1_ISR(void)
Definition: baro_scp.c:109
#define BARO_SCP_SENDER_ID
#define SSP_MS
Definition: baro_scp.c:55
#define EXTINT
Definition: LPC21xx.h:417
Architecture independent timing functions.
uint16_t baro_scp_temperature
Definition: baro_scp.c:26
#define VICVectAddr
Definition: LPC21xx.h:436
unsigned long uint32_t
Definition: types.h:18
#define SpiEnable()
#define SPI1_VIC_SLOT
Definition: baro_scp.c:69
#define SSP_CPOL
Definition: baro_scp.c:48
#define ScpSelect()
Definition: baro_scp.c:63
#define VIC_BIT(chan)
Definition: lpcVIC.h:105
Arch independent mcu ( Micro Controller Unit ) utilities.
unsigned char uint8_t
Definition: types.h:14
#define SSPDR
Definition: LPC21xx.h:224
bool baro_scp_available
Definition: baro_scp.c:27
#define ISR_EXIT()
Definition: armVIC.h:61
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:72
void baro_scp_periodic(void)
Definition: baro_scp.c:34
#define VICIntEnable
Definition: LPC21xx.h:431
#define PINSEL1
Definition: LPC21xx.h:348
#define VICVectCntl11
Definition: LPC21xx.h:465
#define VIC_SPI1
Definition: lpcVIC.h:81
#define ScpUnselect()
Definition: baro_scp.c:64
arch independent LED (Light Emitting Diodes) API
uint8_t baro_scp_status
Definition: baro_scp.c:24
#define SSPCR1
Definition: LPC21xx.h:223
#define STA_IDLE
Definition: baro_scp.c:22
#define SS_IODIR
Definition: baro_scp.c:59
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
#define SPI1_DRDY_PINSEL_BIT
Definition: hbmini_1.0.h:89
#define SSP_DDS
Definition: baro_scp.c:46
#define SSP_SOD
Definition: baro_scp.c:56
#define ISR_ENTRY()
Definition: armVIC.h:40
static void EXTINT_ISR(void)
Definition: baro_scp.c:148
#define VIC_ENABLE
Definition: lpcVIC.h:102