2 #include <msp430xgeneric.h>
16 #include "oswald_main.h"
17 #include "oswald_hal.h"
19 #include "bluetooth_init_cc256x.h"
21 unsigned int _event_src = 0;
24 static void set16mhz(void)
26 UCSCTL0 = 0x00; // Set lowest possible DCOx, MODx
27 UCSCTL1 = DCORSEL_5; // Select suitable range
28 UCSCTL2 = 488 + FLLD_1; // Set DCO Multiplier
29 UCSCTL4 = SELA__XT1CLK | SELS__DCOCLKDIV | SELM__DCOCLKDIV ;
31 // Worst-case settling time for the DCO when the DCO range bits have been
32 // changed is n x 32 x 32 x f_FLL_reference. See UCS chapter in 5xx UG
34 // 32 x 32 x / f_FLL_reference (32,768 Hz) = .03125 = t_DCO_settle
35 // t_DCO_settle / (1 / 18 MHz) = 562500 = counts_DCO_settle
37 // __delay_cycles(562500);
40 __delay_cycles(56250);
44 static void setup_clocks(void)
48 /* use external oscillator */
50 if ((UCSCTL6 & XT1DRIVE_3) != XT1DRIVE_3) {
51 UCSCTL6_L |= XT1DRIVE1_L + XT1DRIVE0_L;
54 while ((SFRIFG1 & OFIFG) && i--) {
55 UCSCTL7 &= ~(DCOFFG + XT1LFOFFG + XT1HFOFFG + XT2OFFG);
58 UCSCTL6 = (UCSCTL6 & ~(XT1DRIVE_3)) |(XT1DRIVE_0);
62 UCSCTL8 |= SMCLKREQEN;
64 /* enable oscillator fault NMI IRQ */
69 #pragma vector=PWR_PORT_VECTOR
70 __interrupt void PWR_ISR (void)
72 /* clear all possible sources */
73 PWR_PORT_IFG &= ~(BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2 | BAT_CHARGE_PWR_BIT);
74 _event_src |= POWER_SRC_EVENT;
80 static void mw_init_vibrate_pwm(void)
83 // Start with P7.3 as an output
84 P7OUT &= ~BIT3; // Low when a digital output
85 P7SEL &= ~BIT3; // P7 option select = false
86 P7DIR |= BIT3; // P7 outputs
90 // No expansion divide
93 // do a PWM with 64 total steps. This gives a count up of 32 and
97 // Compare channel 2 is used as output
98 TA1CCTL2 = OUTMOD_6; // PWM output mode: 6 - toggle/set
99 TA1CCR2 = 10; // 10 is a 2/3 duty cycle
104 static void setup_pins(void)
107 CONFIGURE_BUTTON_PINS();
108 #ifdef MW_DEVBOARD_V2
109 CONFIG_LED_PINS(); // debug LEDs on devboard
111 DISABLE_LCD_LED(); // frontlight
113 CONFIG_ACCELEROMETER_PINS();
114 // DISABLE_ACCELEROMETER_POWER(); // there is no accel. power switching!
116 HARDWARE_CFG_SENSE_INIT();
119 APPLE_POWER_DISABLE();
122 BT_CLK_REQ_CONFIG_AS_OUTPUT_LOW();
123 BT_IO1_CONFIG_AS_OUTPUT_LOW();
124 BT_IO2_CONFIG_AS_OUTPUT_LOW();
128 LIGHT_SENSOR_SHUTDOWN();
130 BATTERY_SENSE_INIT();
131 BATTERY_SENSE_DISABLE();
132 BAT_CHARGE_DIR &= ~(BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2 | BAT_CHARGE_PWR_BIT);
133 BAT_CHARGE_OUT |= BAT_CHARGE_PWR_BIT | BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2; // pull-up
134 BAT_CHARGE_REN |= BAT_CHARGE_PWR_BIT; // enable resistors
135 // BAT_CHARGE_IE |= BAT_CHARGE_PWR_BIT;
136 BAT_CHARGE_OUT |= BAT_CHARGE_ENABLE_PIN; // !CE, negative logic
137 BAT_CHARGE_DIR |= BAT_CHARGE_ENABLE_PIN;
139 /* disable reset function, enable NMI, do not enable NMI IRQ */
140 /* to avoid accidential reset on charger clip connect */
141 #ifndef MW_DEVBOARD_V2 // but only on real watch
142 SFRRPCR &= ~SYSRSTRE;
147 #pragma vector=WDT_VECTOR
148 __interrupt void WDT_ISR (void)
150 /* eventually we will do something here, not for now */
151 _event_src |= WATCHDOG_EVENT;
155 static void setup_wdt(void)
158 WDTCTL = WDTPW + WDTHOLD; // disable watchdog
160 WDTCTL = WDT_ADLY_1000; // 1 second timeout
161 SFRIE1 |= WDTIE; // Enable WDT interrupt
166 #pragma vector=UNMI_VECTOR
167 __interrupt void NMI_ISR (void)
169 #if defined MW_DEVBOARD_V2
171 debug_uart_tx_char('n');
173 while ((SFRIFG1 & OFIFG)) {
174 UCSCTL7 &= ~(DCOFFG + XT1LFOFFG + XT1HFOFFG + XT2OFFG);
180 #pragma vector=RTC_VECTOR
181 __interrupt void RTC_ISR (void)
186 case RTCIV_RTCRDYIFG:
187 case RTCIV_RTCTEVIFG:
192 RTCPS1CTL &= ~RT1PSIFG;
193 _event_src |= RTC_1HZ_EVENT;
207 RTCCTL2 = 0x00 & 0x3f;
210 // Set the counter for RTC mode
213 // set 128 Hz rate for prescale 0 interrupt
214 RTCPS0CTL |= RT0IP_7;
216 // enable 1 pulse per second interrupt using prescale 1
217 RTCPS1CTL |= RT1IP_6 | RT1PSIE;
219 // 1 Hz calibration output
220 RTCCTL23 |= RTCCALF_3;
222 // setting the peripheral selection bit makes the other I/O control a don't care
223 // P2.4 = 1 Hz RTC calibration output
224 // Direction needs to be set as output
225 RTC_1HZ_PORT_SEL |= RTC_1HZ_BIT;
226 RTC_1HZ_PORT_DIR |= RTC_1HZ_BIT;
228 RTCYEAR = (unsigned int) 2013;
229 RTCMON = (unsigned int) 1;
230 RTCDAY = (unsigned int) 1;
231 RTCDOW = (unsigned int) 2;
232 RTCHOUR = (unsigned int) 01;
233 RTCMIN = (unsigned int) 0;
234 RTCSEC = (unsigned int) 0;
237 RTCCTL01 &= ~RTCHOLD;
241 #if defined MW_DEVBOARD_V2
242 static void dbg_out_rtc(void)
245 snprintf(clk_str, 16, "%02d:%02d.%02d %d\n", RTCHOUR, RTCMIN, RTCSEC, RTCDOW);
246 debug_uart_tx(clk_str);
247 // bt_l2cap_send_channel(0x40, clk_str, strlen(clk_str));
251 static void handle_button_event(void)
253 unsigned char _button_state = 0;
256 snprintf(clk_str, 16, "0x%02x\n", _button_state);
257 debug_uart_tx(clk_str);
259 while (_button_state != (BUTTON_PORT_IN & ALL_BUTTONS)) {
260 __delay_cycles(562500);
261 _button_state = (BUTTON_PORT_IN & ALL_BUTTONS);
262 __delay_cycles(562500);
264 // BUTTON_PORT_IE |= INT_EDGE_SEL_BUTTONS;
266 if (_button_state & SW_A) {
267 debug_uart_tx("switch A\n");
268 oswald_handle_button_press(BUTTON_A);
270 if (_button_state & SW_B) {
271 debug_uart_tx("switch B\n");
272 oswald_handle_button_press(BUTTON_B);
274 if (_button_state & SW_C) {
275 debug_uart_tx("switch C\n");
276 oswald_handle_button_press(BUTTON_C);
278 if (_button_state & SW_D) {
279 debug_uart_tx("switch D\n");
280 oswald_handle_button_press(BUTTON_D);
282 if (_button_state & SW_E) {
283 debug_uart_tx("switch E\n");
284 oswald_handle_button_press(BUTTON_E);
286 if (_button_state & SW_F) {
287 debug_uart_tx("switch F\n");
288 oswald_handle_button_press(BUTTON_F);
292 void check_pwr_state(void)
294 if (BAT_CHARGE_IN & BAT_CHARGE_PWR_BIT) {
295 BAT_CHARGE_OUT |= BAT_CHARGE_ENABLE_PIN;
296 BAT_CHARGE_REN &= ~(BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2); // disable pull-up
298 BAT_CHARGE_OUT &= ~BAT_CHARGE_ENABLE_PIN;
299 BAT_CHARGE_REN |= BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2; // enable pull-up
303 static void handle_bt_uart_rx_event(void)
305 const unsigned char *rx;
306 unsigned char len, *rp, p;
308 rx = mw_bt_get_rx_buf(&rp, &len);
312 p += bt_feed_packet_data(rx[(*rp+p)%BT_RX_MAX_SIZE]);
315 *rp = (*rp + len) % BT_RX_MAX_SIZE;
318 #if defined MW_DEVBOARD_V2
319 static void handle_uart_rx_event(void)
322 #ifndef CC256x_TRANSP
325 if (debug_uart_rx_char(&c)) {
326 debug_uart_tx_char(c);
328 debug_uart_tx("\nenabling BT\n");
330 } else if (c == 'B') {
331 debug_uart_tx("\ndisabling BT\n");
333 } else if (c == 'c') {
334 debug_uart_tx("\nCharger status: ");
335 snprintf(tstr, 16, "0x%04x 0x%04x ", BAT_CHARGE_IN, (BAT_CHARGE_IN & BAT_CHARGE_PWR_BIT));
337 if (BAT_CHARGE_IN & BAT_CHARGE_PWR_BIT)
338 debug_uart_tx("no ext pwr, ");
340 debug_uart_tx("ext pwr connected, ");
341 switch (BAT_CHARGE_IN & (BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2)) {
342 case BAT_CHARGE_STAT1:
343 debug_uart_tx("charge done\n");
345 case BAT_CHARGE_STAT2:
346 debug_uart_tx("fast charge\n");
348 case (BAT_CHARGE_STAT1 | BAT_CHARGE_STAT2):
349 debug_uart_tx("suspend, sleep or fault\n");
352 debug_uart_tx("precharge\n");
355 if (BAT_CHARGE_IN & BAT_CHARGE_ENABLE_PIN)
356 debug_uart_tx(" !charge\n");
358 debug_uart_tx(" charge\n");
359 } else if (c == 'd') {
360 debug_uart_tx("charging disabled\n");
361 BAT_CHARGE_OUT |= BAT_CHARGE_ENABLE_PIN;
362 } else if (c == 'e') {
363 debug_uart_tx("charging enabled\n");
364 BAT_CHARGE_OUT &= ~BAT_CHARGE_ENABLE_PIN;
365 } else if (c == 'l') {
366 debug_uart_tx("backlight LED on\n");
367 hal_lcd_set_backlight(TRUE);
368 } else if (c == 'L') {
369 debug_uart_tx("backlight LED off\n");
370 hal_lcd_set_backlight(FALSE);
371 } else if (c == 'u') {
372 mw_lcd_update_screen();
373 } else if (c == '+') {
375 } else if (c == '-') {
377 } else if (c == 't') {
379 debug_uart_tx("cc256x ini content:\n");
380 for (i=0; i<16; i++) {
381 snprintf(tstr, 16, "0x%04x 0x%02x\n", i, cc256x_init_script[i]);
384 } else if (c == 'H') {
386 dclass[0] = BT_MW_DEVICE_CLASS & 0xff;
387 dclass[1] = (BT_MW_DEVICE_CLASS & 0xff00) >> 8;
388 dclass[2] = (BT_MW_DEVICE_CLASS & 0xff0000) >> 16;
390 debug_uart_tx("HCI reset\n");
391 bt_hci_cmd(HCI_HC_BB_OGF, HCI_RESET_OCF, 0, NULL);
392 bt_hci_cmd(HCI_HC_BB_OGF, HCI_W_COD_OCF, 3, dclass);
393 } else if (c == 'S') {
394 debug_uart_tx("Scan enable\n");
395 tstr[0] = HCI_BB_SCAN_INQUIRY | HCI_BB_SCAN_PAGE;
396 bt_hci_cmd(HCI_HC_BB_OGF, HCI_W_SCAN_EN_OCF, 1, (uint8_t *)tstr);
397 } else if (c == 'h') {
401 } else if (c == 'm') {
405 } else if (c == 'N') {
406 debug_uart_tx("Set name\n");
414 bt_hci_cmd(HCI_HC_BB_OGF, HCI_W_LOCAL_NAME_OCF, 0x07, (uint8_t *)tstr);
415 } else if (c == 'R') {
416 bt_hci_cmd(HCI_INFO_PARAM_OGF, HCI_R_BD_ADDR_OCF, 0, NULL);
423 void start_timer(int cycles)
426 TA0CTL = TASSEL_1 | TACLR | MC__STOP; // SMCLK, clear TAR
427 TA0CCTL0 = CCIE; // CCR0 interrupt enabled
429 TA0CTL |= MC_1; // Start Timer_A in continuous mode
432 void stop_timer(void)
434 TA0CCTL0 &= ~CCIE; // CCR0 interrupt enabled
435 TA0CTL = MC__STOP; // Start Timer_A in continuous mode
438 // Timer A0 interrupt service routine
439 #pragma vector=TIMER0_A0_VECTOR
440 __interrupt void TIMER0_A0_ISR (void)
443 #if defined xMW_DEVBOARD_V2
446 _event_src |= TIMER_500MS_EVENT | TIMER_100MS_EVENT;
450 uint8_t handle_event(void)
452 #if defined MW_DEVBOARD_V2
458 while (_event_src != 0) {
459 if (_event_src & WATCHDOG_EVENT) {
460 _event_src &= ~WATCHDOG_EVENT;
461 debug_uart_tx_char('w');
462 } else if (_event_src & RTC_1HZ_EVENT) {
463 _event_src &= ~RTC_1HZ_EVENT;
465 oswald_one_second_tick();
466 #if defined MW_DEVBOARD_V2
470 } else if (_event_src & BT_UART_RCV_EVENT) {
471 _event_src &= ~BT_UART_RCV_EVENT;
472 handle_bt_uart_rx_event();
473 } else if (_event_src & DBG_UART_RCV_EVENT) {
474 _event_src &= ~DBG_UART_RCV_EVENT;
475 #if defined MW_DEVBOARD_V2
476 handle_uart_rx_event();
478 } else if (_event_src & BUTTON_EVENT) {
479 _event_src &= ~BUTTON_EVENT;
480 handle_button_event();
481 } else if (_event_src & TIMER_500MS_EVENT) {
482 _event_src &= ~TIMER_500MS_EVENT;
483 oswald_halfsecond_tick();
484 } else if (_event_src & TIMER_100MS_EVENT) {
485 _event_src &= ~TIMER_100MS_EVENT;
486 oswald_centisecond_tick();
488 #if defined MW_DEVBOARD_V2
489 snprintf(tstr, 64, "unhandled event in 0x%04x\n", _event_src);
497 #pragma vector=BUTTON_PORT_VECTOR
498 __interrupt void BUTTON_ISR (void)
500 BUTTON_PORT_IFG &= ~ALL_BUTTONS;
501 // BUTTON_PORT_IE &= ~INT_EDGE_SEL_BUTTONS;
502 _event_src |= BUTTON_EVENT;
503 //_button_state = (BUTTON_PORT_IN & ALL_BUTTONS);
508 #pragma vector=NOVECTOR
509 __interrupt void UNEXP_ISR (void)
511 debug_uart_tx("unexpected IRQ occured\n");
525 /* enable interrupts, we will need them! */
528 #if defined MW_DEVBOARD_V2
530 debug_uart_tx("\nOswald on MetaWatch\n");
535 mw_lcd_update_screen();
538 mw_init_vibrate_pwm();
541 RTCYEAR = (unsigned int) 2013;
542 RTCMON = (unsigned int) 1;
543 RTCDAY = (unsigned int) 1;
544 RTCDOW = (unsigned int) 2;
545 RTCHOUR = (unsigned int) 01;
546 RTCMIN = (unsigned int) 0;
547 RTCSEC = (unsigned int) 0;
550 oswald_set_time(RTCHOUR, RTCMIN, RTCSEC, TRUE);
551 oswald_set_date(RTCDAY, RTCMON, RTCYEAR, TRUE);
555 /* handle pending events */
561 /* enter LPM3 sleep mode waiting for interrupt */
564 // debug_uart_tx("z");