PSoC-MAX30101
PSoC MAX30101 Library
main.c
1 
5 #include "project.h"
6 #include "MAX30101.h"
7 #include "stdio.h"
8 #include "I2C_Interface.h"
9 
10 #define UART_DEBUG
11 
12 #ifdef UART_DEBUG
13 
14  #define DEBUG_TEST 1
15 
16 #else
17 
18  #define DEBUG_TEST 0
19 
20 #endif
21 
22 #define debug_print(msg) do { if (DEBUG_TEST) UART_Debug_PutString(msg);} while (0)
23 
24 CY_ISR_PROTO(MAX30101_ISR);
25 
26 uint8_t flag_temp = 0;
27 
28 int main(void)
29 {
30  // Variables
31  MAX30101_Data data;
32  data.head = 0;
33  data.tail = 0;
34  char msg[50];
35  void (*print_ptr)(const char*) = &(UART_Debug_PutString);
36  uint8_t active_leds = 1;
37  uint8_t rp, wp, flag = 0;
38 
39  // Initialization
40  MAX30101_Start();
41  UART_Debug_Start();
42 
43  CyDelay(100);
44 
45  debug_print("**************************\r\n");
46  debug_print(" MAX30101 \r\n");
47  debug_print("**************************\r\n");
48 
49  if (MAX30101_IsDevicePresent() == MAX30101_OK)
50  {
51  // Check if device is present
52  debug_print("Device found on I2C bus\r\n");
53  Connection_LED_Write(1);
54 
55  // Read revision and part id
56  uint8_t rev_id, part_id = 0;
57  MAX30101_ReadPartID(&part_id);
58  MAX30101_ReadRevisionID(&rev_id);
59  sprintf(msg,"Revision ID: 0x%02X\r\n", rev_id);
60  debug_print(msg);
61  sprintf(msg,"Part ID: 0x%02X\r\n", part_id);
62  debug_print(msg);
63 
64  debug_print("Registers before configuration\r\n");
65  MAX30101_LogRegisters(print_ptr);
66 
67  // Soft reset sensor
68  MAX30101_Reset();
69  CyDelay(100);
70 
71  // Wake up sensor
72  MAX30101_WakeUp();
73 
74  MAX30101_DisableALCOverflowInt();
75  MAX30101_DisableTempReadyInt();
76  MAX30101_DisablePPGReadyInt();
77  MAX30101_EnableFIFOAFullInt();
78 
79  // set 28 samples to trigger interrupt
80  MAX30101_SetFIFOAlmostFull(32);
81 
82  // enable fifo rollover
83  MAX30101_EnableFIFORollover();
84 
85  // 8 samples averaged
86  MAX30101_SetSampleAverage(MAX30101_SAMPLE_AVG_2);
87 
88  // Set LED Power level
89  MAX30101_SetLEDPulseAmplitude(MAX30101_LED_1, 0x1F);
90 
91  MAX30101_SetLEDPulseAmplitude(MAX30101_LED_2, 0x1F);
92 
93  MAX30101_SetLEDPulseAmplitude(MAX30101_LED_3, 0x1F);
94 
95  MAX30101_SetLEDPulseAmplitude(MAX30101_LED_4, 0x1F);
96 
97  // Set ADC Range
98  MAX30101_SetSpO2ADCRange(MAX30101_ADC_RANGE_4096);
99 
100  // Pulse width
101  MAX30101_SetSpO2PulseWidth(MAX30101_PULSEWIDTH_69);
102 
103  // Set Sample Rate
104  MAX30101_SetSpO2SampleRate(MAX30101_SAMPLE_RATE_400);
105 
106  // Set mode
107  MAX30101_SetMode(MAX30101_SPO2_MODE);
108 
109  // Enable Slots
110  MAX30101_DisableSlots();
111 
112  debug_print("Registers after configuration\r\n");
113  MAX30101_LogRegisters(print_ptr);
114  }
115 
116  debug_print("\r\n\r\n");
117 
118  isr_MAX30101_StartEx(MAX30101_ISR);
119  // Clear FIFO
120  MAX30101_ClearFIFO();
121 
122  CyGlobalIntEnable; /* Enable global interrupts. */
123 
124  for(;;)
125  {
126  if (flag_temp == 1)
127  {
128  MAX30101_IsFIFOAFull(&flag);
129  if (flag > 0)
130  {
131  MAX30101_ReadReadPointer(&rp);
132  MAX30101_ReadWritePointer(&wp);
133  //Calculate the number of readings we need to get from sensor
134  int num_samples = wp - rp;
135  if (num_samples <= 0)
136  num_samples += 32; //Wrap condition
137  // Print out number of samples
138  sprintf(msg, "%d\r\n", num_samples);
139  debug_print(msg);
140  // Read FIFO
141  MAX30101_ReadFIFO(num_samples, active_leds, &data);
142  }
143 
144  flag_temp = 0;
145  }
146  }
147 }
148 
149 CY_ISR(MAX30101_ISR)
150 {
151  Connection_LED_Write(!Connection_LED_Read());
152  MAX30101_INT_ClearInterrupt();
153  flag_temp = 1;
154 }
155 
156 /* [] END OF FILE */
uint8_t tail
Current tail of the circular buffer.
Definition: MAX30101.h:33
Circular buffer for MAX30101 data.
Definition: MAX30101.h:27
uint8_t head
Current head of the circular buffer.
Definition: MAX30101.h:32