Statistics
| Branch: | Tag: | Revision:

root / tidsense / board_v2.c @ 166:1a6a85e8d2e7

History | View | Annotate | Download (5.1 kB)

1
#include <config.h>
2
#include <util/delay.h>
3
4
#include "debug.h"
5
#include "power.h"
6
#include "sysclock.h"
7
#include "audio.h"
8
#include "board.h"
9
#include "adc.h"
10
#include "nv.h"
11
#include "dma.h"
12
#include "backpack.h"
13
#include "audio_power.h"
14
#include "core_sensors_def.h"
15
#include "command.h"
16
17
#include <hal.h>
18
#include <phy.h>
19
#include <sys.h>
20
#include <nwk.h>
21
22
/* digital communication buses *********************************************/
23
24
uart_t serial_port;
25
i2c_bus_t sensor_i2c_bus;
26
spi_bus_t sensor_spi_bus;
27
28
/* sensors *****************************************************************/
29
30
sht21_t sht;
31
isl_t isl;
32
bmp180_t bmp;
33
lt3652_t solar;
34
analog_generic_t battery_voltage;
35
36
/* optional sensors */
37
analog_generic_t solar_voltage;
38
vwc_t vwc;
39
40
spi_peripheral_t accel_spi;
41
adxl362_t accel;
42
43
bool have_audio = false;
44
45
/* sensor configuration structs ********************************************/
46
47
const lt3652_config_t solar_config = {
48
    .chg_port = &PORTB,
49
    .chg_pin = 1,
50
    .fault_port = &PORTB,
51
    .fault_pin = 3
52
};
53
54
const analog_generic_config_t battery_config = {
55
    .mode = ANALOG_MODE_SINGLEENDED,
56
    .pos_input = ANALOG_POS_INPUT_BATMON,
57
    .scale_numerator = 1100,
58
    .scale_denominator = 2048,
59
    .fixed_precision = 2,
60
    .sensor_type = CORE_SENSORS_BATTERY_VOLTAGE
61
};
62
63
/*
64
const analog_generic_config_t solar_voltage_config = {
65
    .mode = ANALOG_MODE_DIFFERENTIAL,
66
    .pos_input = ANALOG_POS_INPUT_EXT4,
67
    .neg_input = ANALOG_NEG_INPUT_PADGND,
68
    .scale_numerator = 1500, // 7.5k and 200k resistor divider
69
    .scale_denominator = 1024,
70
    .fixed_precision = 2,
71
    .sensor_type = CORE_SENSORS_SOLAR_VOLTAGE
72
};
73
*/
74
75
/* board setup *************************************************************/
76
77
void board_setup(void) {
78
    dma_setup();
79
80
    /* set up serial port and debugging */
81
    PORTE.DIRSET = PIN3_bm;
82
    uart_setup(&serial_port, &SERIAL_PORT, SERIAL_BAUD);
83
    debug_setup(&serial_port);
84
    debugf("setup: begin\n");
85
86
    /* set up basic functions */
87
    power_setup();
88
    power_shutdown_unused();
89
    sensors_setup();
90
    adc_setup();
91
92
    _delay_ms(100);
93
94
    /* configure communication buses */
95
    i2c_setup(&sensor_i2c_bus, &SENSOR_TWI_BUS);
96
    spi_setup(&sensor_spi_bus, &SPID, &PORTD);
97
98
    /* sensor setup functions */
99
    sht21_status_t sht_status = sht21_setup(&sht, &sensor_i2c_bus);
100
    debug_sensor_status("sht21", sht_status);
101
102
    bmp180_status_t bmp_status = bmp180_setup(&bmp, &sensor_i2c_bus);
103
    debug_sensor_status("bmp180", bmp_status);
104
105
    isl_status_t isl_status = isl_setup(&isl, &sensor_i2c_bus);
106
    debug_sensor_status("isl29023", isl_status);
107
108
    spi_peripheral_setup(&accel_spi, &sensor_spi_bus, &PORTA, 0, 0,
109
        SPI_MSB_FIRST, 1000000UL);
110
    adxl362_status_t accel_status = adxl362_setup(&accel, &accel_spi);
111
    debug_sensor_status("adxl362", accel_status);
112
    if(accel_status == ADXL362_SUCCESS)
113
        accel_status = adxl362_reset(&accel);
114
    //    accel_status = adxl362_configure_basic(&accel);
115
116
    lt3652_setup(&solar, &solar_config);
117
118
    analog_generic_setup(&battery_voltage, &battery_config, PSTR("battery"));
119
120
    bool audio_power_status = audio_power_setup(&sensor_i2c_bus);
121
    debug_sensor_status("mppt_pwr", audio_power_status? 0:1);
122
123
    /*
124
    if(nv_has_feature(FEATURE_SOLAR)) {
125
        analog_generic_setup(&solar_voltage, &solar_voltage_config, PSTR("solar"));
126
    }
127
    */
128
129
    if(nv_has_feature(FEATURE_DECAGON)) {
130
        vwc_setup(&vwc, PSTR("decagon"));
131
    }
132
133
    if(nv_has_feature(FEATURE_TX22U_WIND)) {
134
        tx22u_setup();
135
    }
136
137
    //have_audio = audio_probe();
138
    //debugf("  audio: %c\n", have_audio? 'Y' : 'N');
139
140
    /* radio setup */
141
    SYS_Init();
142
    debugf("    addr: 0x%04X\n", nv_get_addr());
143
    debugf("     pan: 0x%04X\n", nv_get_pan());
144
    NWK_SetAddr(nv_get_addr());
145
    NWK_SetPanId(nv_get_pan());
146
    PHY_SetChannel(11);
147
    PHY_SetRxState(true);
148
    PHY_SetTxPower(0);
149
    sensors_radio_setup();
150
151
    /* register the sensors that successfully initialized */
152
    if(sht_status == SHT21_SUCCESS)
153
        sensors_register(SENSOR(&sht));
154
    if(isl_status == ISL_SUCCESS)
155
        sensors_register(SENSOR(&isl));
156
    if(bmp_status == BMP180_SUCCESS)
157
        sensors_register(SENSOR(&bmp));
158
    //if(accel_status == ADXL362_SUCCESS)
159
    //    sensors_register(SENSOR(&accel));
160
    sensors_register(SENSOR(&battery_voltage));
161
    
162
    if(nv_has_feature(FEATURE_SOLAR)) {
163
        sensors_register(SENSOR(&solar));
164
        sensors_register(SENSOR(&solar_voltage));
165
    }
166
    
167
    if(nv_has_feature(FEATURE_VEGETRONIX) || nv_has_feature(FEATURE_DECAGON)) {
168
        sensors_register(SENSOR(&vwc));
169
    }
170
171
    command_set_i2c_bus(&sensor_i2c_bus);
172
173
    sei();
174
175
    debugf("setup: end\n");
176
    _delay_ms(10);
177
}
178
179
/* sleep/wake tasks ********************************************************/
180
181
void board_sleep(void) {
182
    VCC_MODE_PORT.OUTSET = (1<<VCC_MODE_PIN);
183
    SENSOR_TWI_BUS.MASTER.CTRLA &= ~TWI_MASTER_ENABLE_bm;
184
    PR.PRPE |= PR_TWI_bm;
185
    adc_disable();
186
}
187
188
void board_wake(void) {
189
    VCC_MODE_PORT.OUTCLR = (1<<VCC_MODE_PIN);
190
    PR.PRPE &= ~PR_TWI_bm;
191
    i2c_setup(&sensor_i2c_bus, &SENSOR_TWI_BUS);
192
}
193
194
UART_ISR(&serial_port, SERIAL_INTERRUPT);
195