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