7 typedef unsigned int uint;
11 void debug_putc(int c)
13 while (!LL_USART_IsActiveFlag_TXE(USART2))
15 LL_USART_TransmitData8(USART2, c);
18 void debug_puts(const char *s)
32 static void printf_string(const char *s, uint width, uint flags)
35 uint pad = (len < width) ? width - len : 0;
36 char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
42 if (!(flags & PF_LEFT))
46 static void printf_number(uint i, uint width, uint flags, uint base)
49 char *w = buf + sizeof(buf);
51 if (flags & PF_SIGNED)
63 uint digit = i % base;
67 *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
72 if (flags & PF_NEGATIVE)
75 printf_string(w, width, flags);
78 void debug_printf(const char *fmt, ...)
104 flags |= PF_ZERO_PAD;
107 while (*fmt >= '0' && *fmt <= '9')
108 width = 10*width + *fmt++ - '0';
114 printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
117 printf_number(va_arg(args, int), width, flags, 10);
120 flags |= PF_UPPERCASE;
123 printf_number(va_arg(args, int), width, flags, 16);
126 printf_string(va_arg(args, char *), width, flags);
137 static uint bmp_read(uint reg, uint bytes)
139 LL_I2C_ClearFlag_STOP(I2C1);
140 LL_I2C_ClearFlag_BERR(I2C1);
141 LL_I2C_HandleTransfer(I2C1, 0xee, LL_I2C_ADDRSLAVE_7BIT, 1, LL_I2C_MODE_SOFTEND, LL_I2C_GENERATE_START_WRITE);
142 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
144 LL_I2C_TransmitData8(I2C1, reg);
145 while (!LL_I2C_IsActiveFlag_TC(I2C1))
147 LL_I2C_HandleTransfer(I2C1, 0xef, LL_I2C_ADDRSLAVE_7BIT, bytes, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_RESTART_7BIT_READ);
150 for (uint i=0; i<bytes; i++)
152 while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
154 d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
160 static uint bmp_measure(uint type, uint bytes)
162 LL_I2C_HandleTransfer(I2C1, 0xee, LL_I2C_ADDRSLAVE_7BIT, 2, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
163 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
165 LL_I2C_TransmitData8(I2C1, 0xf4);
166 while (!LL_I2C_IsActiveFlag_TXE(I2C1))
168 LL_I2C_TransmitData8(I2C1, type);
169 while (!LL_I2C_IsActiveFlag_STOP(I2C1))
172 while (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
175 return bmp_read(0xf6, bytes);
178 // Formulae from BMP085 specs
179 void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], int *tt, int *pp)
194 int X1 = (UT-AC6)*AC5 / (1<<15);
195 int X2 = MC*(1<<11) / (X1+MD);
197 int T = (B5+8) / (1<<4);
201 X1 = (B2*(B6*B6/(1<<12))) / (1<<11);
202 X2 = AC2 * B6 / (1<<11);
204 int B3 = (((AC1*4 + X3) << oss) + 2) / 4;
205 X1 = AC3 * B6 / (1<<13);
206 X2 = (B1*(B6*B6/(1<<12))) / (1<<16);
207 X3 = ((X1+X2) + 2) / (1<<2);
208 uint B4 = (uint)(AC4 * (X3 + 32768)) / (1U<<15);
209 uint B7 = (uint)(UP-B3) * (uint)(50000>>oss);
215 X1 = (p/(1<<8)) * (p/(1<<8));
216 X1 = (X1*3038) / (1<<16);
217 X2 = (-7357*p) / (1<<16);
218 p = p + (X1 + X2 + 3791) / (1<<4);
226 LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
228 debug_puts("Constants:");
230 for (uint i=0; i<11; i++)
232 cc[i] = bmp_read(0xaa + 2*i, 2);
233 debug_printf(" %04x", cc[i]);
237 uint raw_temp = bmp_measure(0x2e, 2);
238 debug_printf("Raw temperature: %04x\r\n", raw_temp);
240 uint oss = 3; // Over-sampling setting
241 uint raw_press = bmp_measure(0xf4 | (oss<<6), 3);
242 debug_printf("Raw pressure: %06x\r\n", raw_press);
245 bmp_recalc(raw_temp, raw_press, oss, cc, &temp, &press);
246 debug_printf("Temperature: %d ddegC\r\n", temp);
247 debug_printf("Pressure: %d Pa\r\n", press);