-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempsensor.c
More file actions
390 lines (308 loc) · 8.72 KB
/
tempsensor.c
File metadata and controls
390 lines (308 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**************************
* USB Temperature Sensor *
* ubld Electronics, LLC. *
* Chris K Cockrum *
* 08/07/2020 *
* STM32F103C8T6 *
* 72MHz Clock *
* Temp = 1 reading / sec *
* LED = 8 bit truecolor *
*************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
#include "tempsensor.h"
// These are about 200nS per write
#define ws2812_zero gpio_set(GPIOA,GPIO7);gpio_clear(GPIOA,GPIO7);gpio_clear(GPIOA,GPIO7);gpio_clear(GPIOA,GPIO7);gpio_clear(GPIOA,GPIO7)
#define ws2812_one gpio_set(GPIOA,GPIO7);gpio_set(GPIOA,GPIO7);gpio_set(GPIOA,GPIO7);gpio_set(GPIOA,GPIO7);gpio_clear(GPIOA,GPIO7)
#define i2c_port GPIOB
#define i2c_clock GPIO6
#define i2c_data GPIO7
#define delay(cycles) for (int i = 0; i < cycles; i++) __asm__("nop")
#define LED_BRIGHTNESS 2 /* How many bits to shift the RGB values before sending to WS2812 */
/* Buffer to be used for control requests. */
uint8_t usbd_control_buffer[128];
void ws2812_write(unsigned char red, unsigned char green, unsigned char blue);
void ws2812_write_8bit_truecolor(unsigned char color);
static enum usbd_request_return_codes usb_cdc_handle_control(usbd_device *usbd_handle, struct usb_setup_data *req, uint8_t **buf,
uint16_t *len, void (**complete)(usbd_device *usbd_handle, struct usb_setup_data *req))
{
char local_buf[10];
struct usb_cdc_notification *notif = (void *)local_buf;
switch (req->bRequest)
{
case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
{
notif->bmRequestType = 0xA1;
notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
notif->wValue = 0;
notif->wIndex = 0;
notif->wLength = 2;
local_buf[8] = req->wValue & 3;
local_buf[9] = 0;
return USBD_REQ_HANDLED;
}
case USB_CDC_REQ_SET_LINE_CODING:
{
if (*len < sizeof(struct usb_cdc_line_coding))
return USBD_REQ_NOTSUPP;
return USBD_REQ_HANDLED;
}
}
return USBD_REQ_NOTSUPP;
}
static void usb_cdc_receive(usbd_device *usbd_handle, uint8_t ep)
{
char buf[64];
int len = usbd_ep_read_packet(usbd_handle, 0x01, buf, 1);
if (len) {
ws2812_write_8bit_truecolor(buf[0]);
}
}
static void usb_cdc_config_setup(usbd_device *usbd_handle, uint16_t wValue)
{
usbd_ep_setup(usbd_handle, 0x01, USB_ENDPOINT_ATTR_BULK, 64, usb_cdc_receive);
usbd_ep_setup(usbd_handle, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL);
usbd_ep_setup(usbd_handle, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
usbd_register_control_callback(usbd_handle,USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, \
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, usb_cdc_handle_control);
}
void gpio_setup(void) {
/* Enable GPIO clocks */
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
/* For ws2812 */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO7);
/* For i2c clock */
gpio_set_mode(i2c_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, i2c_clock);
/* For i2c data */
gpio_set_mode(i2c_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, i2c_data);
}
void ws2812_write(unsigned char red, unsigned char green, unsigned char blue)
{
int i;
unsigned int temp;
gpio_clear(GPIOA,GPIO7);
for (i = 0; i < 500000; i++) /* Wait a bit. */
__asm__("nop");
temp=green;
for(i=0;i<8;i++)
{
if(temp & 0x80)
{
ws2812_one;
}
else
{
ws2812_zero;
}
temp<<=1;
}
temp=red;
for(i=0;i<8;i++)
{
if(temp & 0x80)
{
ws2812_one;
}
else
{
ws2812_zero;
}
temp<<=1;
}
temp=blue;
for(i=0;i<8;i++)
{
if(temp & 0x80)
{
ws2812_one;
}
else
{
ws2812_zero;
}
temp<<=1;
}
}
void ws2812_write_8bit_truecolor(unsigned char color)
{
int i;
unsigned int temp;
gpio_clear(GPIOA,GPIO7);
for (i = 0; i < 500000; i++) /* Wait a bit. */
__asm__("nop");
temp=(color>>2)&0x7;
temp<<= LED_BRIGHTNESS;
for(i=0;i<8;i++)
{
if(temp & 0x80)
{
ws2812_one;
}
else
{
ws2812_zero;
}
temp<<=1;
}
temp=(color>>5)&0x7;
temp<<= LED_BRIGHTNESS;
for(i=0;i<8;i++)
{
if(temp & 0x80)
{
ws2812_one;
}
else
{
ws2812_zero;
}
temp<<=1;
}
temp=color & 0x3;
temp<<= (LED_BRIGHTNESS+1);
for(i=0;i<8;i++)
{
if(temp & 0x80)
{
ws2812_one;
}
else
{
ws2812_zero;
}
temp<<=1;
}
}
int read_temperature()
{
int temp=0;
int n=0;
/* Set Data to Output */
gpio_set_mode( i2c_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, i2c_data); delay(30);
/* Start Condition */
gpio_set(i2c_port,i2c_clock); delay(30);
gpio_set(i2c_port,i2c_data); delay(30);
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_clear(i2c_port,i2c_clock); delay(30);
/* Device Address = 0x48 */
/* Send Address */
// 1
gpio_set(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
// 0
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
// 0
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
// 1
gpio_set(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
// 0
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
// 0
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
// 0
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
// 1 = Read
gpio_set(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
/* Set data to input with pullup */
gpio_set_mode( i2c_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, i2c_data); delay(30);
/* Clock in acknowledge (ignore it ) */
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
temp=0;
/* Get First 8 Bits */
for (n=0;n<8;n++)
{
temp<<=1;
gpio_set(i2c_port,i2c_clock); delay(30);
temp |= (gpio_get(i2c_port, i2c_data) != 0);
gpio_clear(i2c_port,i2c_clock); delay(30);
}
/* Send Acknowledge */
/* Set Data to Output */
gpio_set_mode( i2c_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, i2c_data); delay(30);
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
gpio_set(i2c_port,i2c_data); delay(30);
/* Set data to input with pullup */
gpio_set_mode( i2c_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, i2c_data);delay(30);
/* Get Last 8 Bits */
for (n=0;n<8;n++)
{
temp<<=1;
gpio_set(i2c_port,i2c_clock); delay(30);
temp |= (gpio_get(i2c_port, i2c_data) != 0);
gpio_clear(i2c_port,i2c_clock); delay(30);
}
/* Set Data to Output */
gpio_set_mode( i2c_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, i2c_data); delay(30);
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30); gpio_clear(i2c_port,i2c_clock); delay(30);
/* Stop Condition */
gpio_clear(i2c_port,i2c_data); delay(30);
gpio_set(i2c_port,i2c_clock); delay(30);
gpio_set(i2c_port,i2c_data); delay(30);
/* Return 11 bits */
return temp>>5;
}
int main(void)
{
int i;
int count=0;
int tempint;
float tempvalue=0;
char buffer[64];
usbd_device *usbd_handle;
//rcc_clock_setup_in_hse_8mhz_out_72mhz(); // Use this for "blue pill" // DEPRECATED
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
gpio_setup();
for (i = 0; i < 24000000; i++) /* Wait a bit. */
__asm__("nop");
usbd_handle = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbd_handle, usb_cdc_config_setup);
for (i = 0; i < 1500000; i++)
__asm__("nop");
ws2812_write(0x10,0x00,0x00);
for (i = 0; i < 1500000; i++)
__asm__("nop");
ws2812_write(0x10,0x10,0x10);
for (i = 0; i < 1500000; i++)
__asm__("nop");
ws2812_write(0x00,0x00,0x10);
/* The first temperature read isn't valid */
tempint = read_temperature();
while(1)
{
for (i = 0; i < 1000; i++)
__asm__("nop");
if(count++>10000)
{
tempint = read_temperature();
/* Test Negative */
//tempint = 0x00000ff8; // Should be -1.000 C
/* If Negative then sign extend */
if(tempint & 0x800)
tempint = tempint | 0xfffff000;
/* Convert to float */
tempvalue = tempint *.125;
/* Print to a string */
sprintf(buffer,"%.03f C\n", (double) tempvalue);
/* Write buffer to USB */
usbd_ep_write_packet(usbd_handle, 0x82, buffer, strlen(buffer));
count=0;
}
usbd_poll(usbd_handle);
}
return 0;
}