Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mag_micromag_fw_hw.h
Go to the documentation of this file.
1 #ifndef MICROMAG_FW_HW_H
2 #define MICROMAG_FW_HW_H
3 
4 #include <stdlib.h> // for abs
5 
6 #include "std.h"
7 #include "LPC21xx.h"
8 #include "mcu.h"
9 
10 #include "mcu_periph/spi.h"
11 #include BOARD_CONFIG
12 
13 #include "generated/airframe.h"
14 
15 #define MM_DIVISOR_128 2
16 #define MM_DIVISOR_256 3
17 #define MM_DIVISOR_512 4
18 #define MM_DIVISOR_1024 5
19 
20 #define MM_DIVISOR MM_DIVISOR_512
21 
22 
23 extern volatile uint8_t micromag_cur_axe;
24 
25 #define MmSelect() SetBit(MM_SS_IOCLR,MM_SS_PIN)
26 #define MmUnselect() SetBit(MM_SS_IOSET,MM_SS_PIN)
27 
28 #define MmReset() SetBit(MM_RESET_IOCLR,MM_RESET_PIN)
29 #define MmSet() SetBit(MM_RESET_IOSET,MM_RESET_PIN)
30 
31 #define MmOnSpiIt() { \
32  switch (micromag_status) { \
33  case MM_SENDING_REQ: \
34  { \
35  /* read dummy control byte reply */ \
36  uint8_t foo __attribute__ ((unused)) = SSPDR; \
37  micromag_status = MM_WAITING_EOC; \
38  MmUnselect(); \
39  SpiClearRti(); \
40  SpiDisableRti(); \
41  SpiDisable(); \
42  } \
43  break; \
44  case MM_READING_RES: \
45  { \
46  int16_t new_val; \
47  new_val = SSPDR << 8; \
48  new_val += SSPDR; \
49  if (abs(new_val) < 2000) \
50  micromag_values[micromag_cur_axe] = new_val; \
51  MmUnselect(); \
52  SpiClearRti(); \
53  SpiDisableRti(); \
54  SpiDisable(); \
55  micromag_cur_axe++; \
56  if (micromag_cur_axe > 2) { \
57  micromag_cur_axe = 0; \
58  micromag_status = MM_DATA_AVAILABLE; \
59  } \
60  else \
61  micromag_status = MM_IDLE; \
62  } \
63  break; \
64  } \
65  }
66 
67 
68 #define MmSendReq() { \
69  MmSelect(); \
70  micromag_status = MM_SENDING_REQ; \
71  MmSet(); \
72  SpiClearRti(); \
73  SpiEnableRti(); \
74  MmReset(); \
75  uint8_t control_byte = (micromag_cur_axe+1) << 0 | MM_DIVISOR_1024 << 4; \
76  SSPDR = control_byte; \
77  SpiEnable(); \
78  }
79 
80 #define MmReadRes() { \
81  micromag_status = MM_READING_RES; \
82  MmSelect(); \
83  /* trigger 2 bytes read */ \
84  SSPDR = 0; \
85  SSPDR = 0; \
86  SpiEnable(); \
87  SpiClearRti(); \
88  SpiEnableRti(); \
89  }
90 
91 extern void micromag_hw_init(void);
92 
93 #endif /* MICROMAG_HW_H */
void micromag_hw_init(void)
Architecture independent SPI (Serial Peripheral Interface) API.
Arch independent mcu ( Micro Controller Unit ) utilities.
unsigned char uint8_t
Definition: types.h:14
volatile uint8_t micromag_cur_axe