-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.txt
277 lines (235 loc) · 7.16 KB
/
main.txt
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
/*
* TX: A9 ====> RX of TTL serial
* RX: A10 <==== TX of TTL serial (not used)
* CTS: A11 (not used)
* RTS: A12 (not used)
* Config: 8N1
* Baud: 38400
*
*
* Caution:
* Not all GPIO pins are 5V tolerant, so be careful to
* get the wiring correct.
*/
#include <FreeRTOS.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <task.h>
#include <queue.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/spi.h>
#include "clock.h"
#include "i2c.h"
#include "utils.h"
#define SHT31_DEFAULT_ADDR 0x44 /**< SHT31 Default Address */
#define SHT31_MEAS_HIGHREP_STRETCH \
0x2C06 /**< Measurement High Repeatability with Clock Stretch Enabled */
#define SHT31_MEAS_MEDREP_STRETCH \
0x2C0D /**< Measurement Medium Repeatability with Clock Stretch Enabled */
#define SHT31_MEAS_LOWREP_STRETCH \
0x2C10 /**< Measurement Low Repeatability with Clock Stretch Enabled*/
#define SHT31_MEAS_HIGHREP \
0x2400 /**< Measurement High Repeatability with Clock Stretch Disabled */
#define SHT31_MEAS_MEDREP \
0x240B /**< Measurement Medium Repeatability with Clock Stretch Disabled */
#define SHT31_MEAS_LOWREP \
0x2416 /**< Measurement Low Repeatability with Clock Stretch Disabled */
#define SHT31_READSTATUS 0xF32D /**< Read Out of Status Register */
#define SHT31_CLEARSTATUS 0x3041 /**< Clear Status */
#define SHT31_SOFTRESET 0x30A2 /**< Soft Reset */
#define SHT31_HEATEREN 0x306D /**< Heater Enable */
#define SHT31_HEATERDIS 0x3066 /**< Heater Disable */
static I2C_Control i2c;
static volatile bool readf = false;
static volatile int isr_count = 0;
static QueueHandle_t uart_txq;
static void uart_setup(void) {
// UART
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_USART1);
gpio_set_mode(GPIOA,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
GPIO_USART1_TX);
usart_set_baudrate(USART1,38400);
usart_set_databits(USART1,8);
usart_set_stopbits(USART1,USART_STOPBITS_1);
usart_set_mode(USART1,USART_MODE_TX);
usart_set_parity(USART1,USART_PARITY_NONE);
usart_set_flow_control(USART1,USART_FLOWCONTROL_NONE);
usart_enable(USART1);
uart_txq = xQueueCreate(256,sizeof(char));
}
static void i2c_setup(void) {
// I2C
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_I2C1);
rcc_periph_clock_enable(RCC_AFIO);
gpio_set_mode(GPIOB,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN,
GPIO6|GPIO7); // I2C
gpio_set(GPIOB,GPIO6|GPIO7); // Idle high
// AFIO_MAPR_I2C1_REMAP=0, PB6+PB7
gpio_primary_remap(0,0);
i2c_configure(&i2c,I2C1,1000);
}
static void gpio_setup(void) {
// GPIO PC13
rcc_periph_clock_enable(RCC_GPIOC);
gpio_set_mode(
GPIOC,
GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
GPIO13);
}
static void uart_task(void *args __attribute__((unused))) {
char ch;
for (;;) {
if ( xQueueReceive(uart_txq, &ch, 500) == pdPASS ) {
while (!usart_get_flag(USART1,USART_SR_TXE)) {
taskYIELD();
}
usart_send(USART1,ch);
}
}
}
static inline void uart_putc(char ch) {
usart_send_blocking(USART1,ch);
}
static void uart_puts(const char *s) {
for ( ; *s; ++s ) {
xQueueSend(uart_txq,s,portMAX_DELAY);
}
}
static void uart1_uint32_t(char * name, uint32_t value) {
char bufValue[10];
ltoa(value, bufValue, 10);
uart_puts(name);
uart_puts(": ");
uart_puts(bufValue);
uart_puts("\n\r");
}
static void demo_task1(void *args __attribute__((unused))) {
for (;;) {
gpio_toggle(GPIOC,GPIO13);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
static void demo_task2(void *args __attribute__((unused))) {
for (;;) {
gpio_toggle(GPIOC,GPIO13);
//uart1_uint32_t("Timer_Value", micro_second());
micro_second_sleep(1000000);
}
}
static void i2c_status(void *args __attribute__((unused))) {
for (;;) {
I2C_Fails fc;
fc = setjmp(i2c_exception);
if (fc == I2C_Ok) {
uart_puts("I2C_Ok");
}
if (fc == I2C_Addr_Timeout) {
uart_puts("I2C_Addr_Timeout");
}
if (fc == I2C_Addr_NAK) {
uart_puts("I2C_Addr_NAK");
}
if (fc == I2C_Write_Timeout) {
uart_puts("I2C_Write_Timeout");
}
if (fc == I2C_Read_Timeout) {
uart_puts("I2C_Read_Timeout");
}
uart_puts("\n\r");
vTaskDelay(pdMS_TO_TICKS(100));
}
}
static void sensor_i2c(void *args __attribute__((unused))) {
// Configure I2C1
for (;;) {
uint16_t stat = 0xFF;
i2c_start_addr(&i2c,SHT31_DEFAULT_ADDR,Write);
i2c_write(&i2c,SHT31_READSTATUS >> 8);
i2c_write(&i2c,SHT31_READSTATUS & 0xFF);
i2c_stop(&i2c);
i2c_start_addr(&i2c,SHT31_DEFAULT_ADDR,Read);
stat = i2c_read(&i2c,true);
i2c_stop(&i2c);
stat <<= 8;
i2c_start_addr(&i2c,SHT31_DEFAULT_ADDR,Read);
stat |= i2c_read(&i2c,true);
i2c_stop(&i2c);
uart_puts("-------------------\n\r");
if (stat != 0xFFFF) {
uint8_t readbuffer[6];
i2c_start_addr(&i2c,SHT31_DEFAULT_ADDR,Write);
i2c_write(&i2c,SHT31_MEAS_HIGHREP >> 8);
i2c_write(&i2c,SHT31_MEAS_HIGHREP & 0xFF);
i2c_stop(&i2c);
vTaskDelay(pdMS_TO_TICKS(20));
i2c_start_addr(&i2c,SHT31_DEFAULT_ADDR,Read);
for (size_t i = 0; i < sizeof(readbuffer); i++) {
readbuffer[i] = i2c_read(&i2c,true);
}
i2c_stop(&i2c);
int32_t stemp = (int32_t) (((uint32_t)readbuffer[0] << 8) | readbuffer[1]);
stemp = ((4375 * stemp) >> 14) - 4500;
float temp = (float) stemp / 100.0f;
char tempchar[5];
ftoa(temp, tempchar, 2);
uart_puts("Temp C: ");
uart_puts(tempchar);
uart_puts("\n\r");
float fahrenheit = ((temp * 9)/5) + 32;
char tempcharf[5];
ftoa(fahrenheit, tempcharf, 2);
uart_puts("Temp F: ");
uart_puts(tempcharf);
uart_puts("\n\r");
//uint32_t shum = ((uint32_t)readbuffer[3] << 8) | readbuffer[4];
//float humidity = (shum * 100.0f) / 65535.0f;
//shum = (625 * shum) >> 12;
//float humidity = (float) shum / 100.0f;
//uart1_uint32_t("humidity", humidity);
}
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
int main(void) {
// CPU clock is 72 MHz
rcc_clock_setup_in_hse_8mhz_out_72mhz();
// Clock Setup
clock_setup();
// I2C Setup
i2c_setup();
// UART Setup
//uart_setup();
// GPIO Setup
gpio_setup();
// SPI Setup
//xTaskCreate(uart_task,"UART",100,NULL,configMAX_PRIORITIES - 1,NULL);
// Demoing the blinking light example using the delay from FreeRTOS (vTaskDelay->pdMS_TO_TICKS) and a self managed timer for the same results (micro_second_sleep)
/*
#if 1
xTaskCreate(demo_task1,"BLINK1",100,NULL,configMAX_PRIORITIES - 1,NULL);
#else
xTaskCreate(demo_task2,"BLINK2",100,NULL,configMAX_PRIORITIES - 1,NULL);
#endif
*/
xTaskCreate(demo_task2, "BLINK1", 100, NULL, configMAX_PRIORITIES - 1, NULL);
//xTaskCreate(sensor_i2c,"SHT31",100,NULL,configMAX_PRIORITIES - 1,NULL);
//xTaskCreate(i2c_status,"I2CSTATUS",100,NULL,configMAX_PRIORITIES-1,NULL);
vTaskStartScheduler();
for (;;);
return 0;
}