4 #define DEBUG DEBUG_FULL
12 #include "contiki-maca.h"
16 #define MC1322X_BUCK_THRES 2425
18 #define MC1322X_BUCK_WINDOW 150
19 #define MC1322X_BUCK_THRES_H (MC1322X_BUCK_THRES + MC1322X_BUCK_WINDOW/2)
20 #define MC1322X_BUCK_THRES_L (MC1322X_BUCK_THRES - MC1322X_BUCK_WINDOW/2)
22 #define MC1322X_BUCK_MONITOR_PERIOD 600 * CLOCK_SECOND
25 static struct etimer et_buck;
26 PROCESS(buck_monitor,
"buck monitor");
31 PRINTF(
"starting vbatt monitor\n");
33 etimer_set(&et_buck, MC1322X_BUCK_MONITOR_PERIOD);
40 PRINTF(
"buck monitor: vbatt: %d mV\n\r", adc_vbatt);
41 if( CRM->VREG_CNTLbits.BUCK_EN == 1 && adc_vbatt < MC1322X_BUCK_THRES_L ) {
42 PRINTF(
"vbatt low, disabling buck\n\r", adc_vbatt);
43 CRM->SYS_CNTLbits.PWR_SOURCE = 0;
44 CRM->VREG_CNTLbits.BUCK_SYNC_REC_EN = 0;
45 CRM->VREG_CNTLbits.BUCK_BYPASS_EN = 1;
46 CRM->VREG_CNTLbits.BUCK_EN = 0;
47 }
else if ( CRM->VREG_CNTLbits.BUCK_EN == 0 && adc_vbatt > MC1322X_BUCK_THRES_H ) {
48 PRINTF(
"vbatt high, enabling buck\n\r", adc_vbatt);
49 CRM->SYS_CNTLbits.PWR_SOURCE = 1;
50 CRM->VREG_CNTLbits.BUCK_SYNC_REC_EN = 1;
51 CRM->VREG_CNTLbits.BUCK_BYPASS_EN = 0;
52 CRM->VREG_CNTLbits.BUCK_EN = 1;
54 etimer_set(&et_buck, MC1322X_BUCK_MONITOR_PERIOD);
62 void buck_setup(
void) {
69 while(CRM->STATUSbits.VREG_1P5V_RDY == 0) {
continue; }
70 while(CRM->STATUSbits.VREG_1P8V_RDY == 0) {
continue; }
77 PRINTF(
"vbatt: %04u mV\n\r", adc_vbatt);
80 for(i = 0; i < 128 && type != 0; i++) {
81 err = nvm_detect(gNvmInternalInterface_c, &type);
83 if (type == gNvmType_NoNvm_c)
85 PRINTF(
"NVM failed without buck, trying with buck\n\r");
87 if (adc_vbatt < MC1322X_BUCK_THRES_L)
89 PRINTF(
"Vbatt is low, bypassing buck\n\r");
90 CRM->SYS_CNTLbits.PWR_SOURCE = 0;
91 CRM->VREG_CNTLbits.BUCK_SYNC_REC_EN = 0;
92 CRM->VREG_CNTLbits.BUCK_BYPASS_EN = 1;
93 CRM->VREG_CNTLbits.BUCK_EN = 0;
95 CRM->SYS_CNTLbits.PWR_SOURCE = 1;
96 CRM->VREG_CNTLbits.BUCK_SYNC_REC_EN = 1;
97 CRM->VREG_CNTLbits.BUCK_BYPASS_EN = 0;
98 CRM->VREG_CNTLbits.BUCK_EN = 1;
101 while(CRM->STATUSbits.VREG_BUCK_RDY == 0) {
continue; }
102 CRM->VREG_CNTLbits.VREG_1P5V_SEL = 3;
103 CRM->VREG_CNTLbits.VREG_1P5V_EN = 3;
104 CRM->VREG_CNTLbits.VREG_1P8V_EN = 1;
106 while(CRM->STATUSbits.VREG_1P5V_RDY == 0) {
continue; }
107 while(CRM->STATUSbits.VREG_1P8V_RDY == 0) {
continue; }
110 for(i = 0; i < 128 && type != 0; i++) {
111 err = nvm_detect(gNvmInternalInterface_c, &type);
113 if (type != gNvmType_NoNvm_c) {
114 PRINTF(
"buck ok\n\r");
118 printf(
"fatal: couldn't detect NVM\n\r");
121 PRINTF(
"NVM ok without buck\n\r");
127 void rtc_setup(
void) {
128 volatile uint32_t rtc_count;
134 rtc_count = CRM->RTC_COUNT;
135 PRINTF(
"trying to start 32kHz xtal\n\r");
137 for(i = 0; i < 150000 && CRM->RTC_COUNT == rtc_count; i++) {
continue; }
138 if(CRM->RTC_COUNT == rtc_count) {
139 PRINTF(
"32xtal failed, using ring osc\n\r");
140 CRM->SYS_CNTLbits.XTAL32_EXISTS = 0;
141 CRM->XTAL32_CNTLbits.XTAL32_EN = 0;
145 CRM->RINGOSC_CNTLbits.ROSC_CTUNE = 0x6;
146 CRM->RINGOSC_CNTLbits.ROSC_FTUNE = 0x17;
150 PRINTF(
"RTC calibrated to %d Hz\r\n", rtc_freq);
152 PRINTF(
"32kHz xtal started\n\r");
158 void mc1322x_init(
void) {
169 PRINTF(
"mc1322x init\n\r");
180 mc1322x_config_restore(&mc1322x_config);
181 if ( mc1322x_config_valid(&mc1322x_config) != 1 ) {
182 PRINTF(
"flash invalid\n\r");
184 mc1322x_config_set_default(&mc1322x_config);
185 mc1322x_config_save(&mc1322x_config);
189 mc1322x_config_print();
194 set_power(mc1322x_config.power);
195 set_channel(mc1322x_config.channel);
196 set_demodulator_type(mc1322x_config.flags.demod);
197 set_prm_mode(mc1322x_config.flags.autoack);
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
#define PROCESS_BEGIN()
Define the beginning of a process.
void rtimer_init(void)
Initialize the real-time scheduler.
void uart_init(const unsigned int uart_num, uint32_t module_clk_hz, const uint32_t baud)
Initialize UART.
#define NULL
The null pointer.
void clock_init(void)
Initialize the clock library.
void process_init(void)
Initialize the process module.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
void ctimer_init(void)
Initialize the callback timer library.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
#define PROCESS(name, strname)
Declare a process.
void process_start(struct process *p, process_data_t data)
Start a process.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
A set of debugging macros.
void adc_init(void)
Initializes the ADC controller.
Header file for the Contiki process interface.