void main(void) { WDTCTL = WDTPW + WDTHOLD; __eint(); // Set up outputs P1DIR = 0x3f; P1OUT = 0; P4SEL = BIT1 | BIT2 | BIT3; P5SEL = 0; P5DIR = BIT6 | BIT7; P5OUT = BIT7; P7SEL = 0; P7DIR = BIT4 | BIT6; P7OUT = BIT6; P8DIR = BIT2; P8OUT = 0; // Set up USCI B1 UCB1CTL1 = UCSSEL__SMCLK | UCSWRST; UCB1CTL0 = UCMSB | UCMST | UCMODE0 | UCCKPH | UCSYNC; UCB1BR0 = 0; UCB1BR1 = 1; UCB1IE = 3; UCB1CTL1 &= ~UCSWRST; unsigned char lcd_face[] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0xc0, 0x40, 0x60, 0x20, 0x20, 0x30, 0x10, 0x18, 0x8, 0x8, 0x8c, 0xc4, 0x64, 0x24, 0x32, 0x32, 0x32, 0x30, 0x22, 0x62, 0x41, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x21, 0x31, 0x31, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x21, 0x60, 0x62, 0x42, 0x2, 0x2, 0x6, 0x4, 0x4, 0xc, 0x8, 0x8, 0x18, 0x10, 0x10, 0x30, 0x20, 0x60, 0x40, 0xc0, 0x80, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0xc0, 0x60, 0x30, 0x18, 0xc, 0x6, 0x2, 0x3, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x31, 0x19, 0x68, 0x28, 0x34, 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xf0, 0x80, 0x0, 0x82, 0x82, 0xfe, 0x80, 0x80, 0x0, 0x82, 0x82, 0xfe, 0x80, 0x80, 0x0, 0x9, 0x39, 0xc1, 0x39, 0x9, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x89, 0x89, 0xfb, 0x81, 0x81, 0x1, 0x9, 0x7f, 0x89, 0x89, 0x41, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x38, 0xc8, 0x38, 0xc8, 0x38, 0x8, 0x70, 0x88, 0x88, 0x88, 0x70, 0x0, 0x88, 0xf8, 0x90, 0x8, 0x8, 0x0, 0x82, 0xfe, 0x20, 0xd8, 0x88, 0x0, 0x90, 0xa8, 0xa8, 0xa8, 0x48, 0x0, 0x0, 0x0, 0xbe, 0x0, 0x0 }; unsigned char lcd_init[] = { 0x40, 0xa1, 0xc0, 0xa4, 0xa6, 0xa2, 0x2f, 0x27, 0x81, 0x10, 0xfa, 0x90, 0xaf }; unsigned char lcd_home[] = { 0x00, 0x10, 0xb0 }; unsigned char lcd_data[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 // 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; unsigned char lcd_clear_line[] = {0, 0, 0, 0, 0, 0, 0, 0}; spi_write(lcd_init, sizeof(lcd_init)); char *c = lcd_face; for (int row = 0; row < 8; ++row) { P5OUT &= ~BIT6; spi_write_char(0x00); spi_write_char(0x10); spi_write_char(0xb0 | row); P5OUT |= BIT6; for (int col = 0; col < 102; ++col) { spi_write_char(*(c++)); } } cycle_blue_leds(); }
int main(void) { Task_t tasks[3]; // Create our task objects! /* Watchdog timer disabled */ WDTCTL = WDTPW + WDTHOLD; BspInit(); // Set up UartInit(9600, UartCbf); (void)TimerMasterInit(100); LedCtor(&led_red, kPort1, kPin0); //P1.0 (red) LedCtor(&led_green, kPort1, kPin1); //P1.1 (green) ButtonCtor(&button_small, kPort1, kPin2, kResHigh, 1); // 1ms debounce TaskTimedCtor(&tasks[0], LedRedFlash, 6, 0); TaskLoopCtor( &tasks[1], ManageLoop); TaskLoopCtor( &tasks[2], UartStep); __eint(); // Enable global interrupts TaskMasterRun(tasks, 3); while(true) { } }
int main(void) { Timer_t timer; // Create our timer object! Led_t led; // Create the LED Object on P1.0! /* Watchdog timer disabled */ WDTCTL = WDTPW + WDTHOLD; BspInit(); // Set up (void) TimerMasterInit(100); //100 *10us = 1ms TimerCtor(&timer); LedCtor(&led, kPort1, kPin0); //P1.0 (red) __eint(); // Enable global interrupts while(true) { if (TimerGet(&timer)>100) // 0.2s period (1ms*20) { LedToggle(&led); TimerReset(&timer); } } }
int main(void) { watchdog_off(); clock_init_1MHz(); /* set all pins to output high */ port1_direction = 0xFF; port1_output = 0xFF; /* initialize serial clock, tx and rx parts */ serial_init_clock(); serial_init_tx(); serial_init_rx(); /* enable interrupts */ __eint(); pin_low(LED1); pin_high(LED2); while (1) { unsigned char c; pin_toggle(LED1); pin_toggle(LED2); c = serial_getchar(); serial_putchar(c); if (c == '\r') serial_putchar('\n'); } }
int main(void) { WDTCTL = WDTPW + WDTHOLD; // Watchdog timer disabled /* Set up the system clock */ BCSCTL1 = CALBC1_1MHZ; // can replace 8 with 1, 12 or 16 DCOCTL = CALDCO_1MHZ; /* Initialise Timer */ TACTL = 0x0212; // Use SMCLK, Up mode (count to TACCR0), Interrupts Enabled TACCTL0 = 0x0010; // Interrupts Enabled, Compare Mode TACCR0 = 1000; // 1MHz/1000Hz = 1000 gives the number of system clocks required for a 1ms timer /* Initialisation of P1.0 (LED) */ LEDSEL &= ~LEDBIT; // Digital I/O mode LEDDIR |= LEDBIT; // Output Mode LEDOUT |= LEDBIT; // LED on __eint(); WatchdogEnable(WDT_8000); while(1) { if (tick>=500) // 1s period (500ms) { LEDOUT ^= LEDBIT; // Toggle Led tick = 0; } } }
int main(void) { /* Watchdog timer disabled */ WDTCTL = WDTPW + WDTHOLD; BspInit(); DioInit(); DioSet(BUTTON0); UartInit(9600, UartCallback); LedInit(); ButtonInit(); ButtonSetup(0, Button0Cbf, 20); LedOn(0); SchedulerInit(100, 1, 2); // 1ms tick, 1 timed task, 2 loop task SetSchedulerTimedTask(LedRedFlash, 0, 500, 0); // schedule timed_task[0] as led_red_flash, set off every 500ms SetSchedulerLoopTask(ButtonLoop, 0); // schedule loop_task[0] as button_loop SetSchedulerLoopTask(UartManage, 1); // schedule loop_task[1] as uart_loop __eint(); SchedulerRun(); while(1) { } }
uint8_t get_button_press(uint8_t button_mask) { __dint(); __no_operation(); button_mask &= button_press; button_press ^= button_mask; __nop(); __eint(); return button_mask; }
__interrupt void dco_adjust(void) // { // TA0CCTL0 &= ~CCIFG; // Clear interrupt flag __eint(); // Re-enable interrupts to allow fast response to USB // Adjust DCO // 15,000,000 / (32,768 / 8) = 3662.11 // 3662.5 * (32,768 / 8) = 15,001,600 if((TA0CCR0 - FrequencyCounter) < 3662) ++DCOCTL; else --DCOCTL; FrequencyCounter = TA0CCR0; // } //
uint8_t get_button_rpt(uint8_t button_mask) { __dint(); __no_operation(); button_mask &= button_rpt; button_rpt ^= button_mask; __nop(); __eint(); return button_mask; }
int main(void) { uint16_t time = 0; led_color_t col; // dog WDTCTL = WDTPW + WDTHOLD; // set vcore to highest value, mclk 24 MHz SetVCoreUp(0x01); SetVCoreUp(0x02); SetVCoreUp(0x03); // hw init hardware_clock_init(); init_spi_master(); timer_init(); // internal LED and debug pins P1DIR |= BIT0; P2DIR |= BIT2; // channel 17 = UCATXIFG as trigger for DMA DMACTL0 = 0; DMACTL1 = 17; DMACTL2 = 0; DMACTL3 = 0; // DMA2 configuration // bytewise access for source and destination, increment source, single transfer DMA2CTL = DMADT_0 | DMASRCINCR_3 | DMASRCBYTE | DMADSTBYTE | DMAIE; DMA2SA = (uint16_t)(&led_raw[1]); // source DMA2DA = (uint16_t)&UCA0TXBUF; // destination DMA2SZ = SIZE_OF_LED_ARRAY-1; // size in bytes __eint(); // all set, now let the DMA run DMA2CTL |= DMAEN ; UCA0TXBUF = led_raw[0]; while (1) { time++; P1OUT ^= BIT0; LPM0; // DMA transfer has finished, you may calculate LED updates now // timer A is triggered app. every 10msec, so you have around 6msec time // to update the strip // this is a color fading thing for (uint8_t i=0; i<NR_OF_LEDS; i++){ col.g = (time % 512) + i*4; col.r = (time % 256) + i; col.b = (time / 1024) + (NR_OF_LEDS - i); set_LED(i,&col); } } }
int main(void) { WDTCTL = WDTPW | WDTHOLD; LED_DIR |= (LED_0 | LED_1); set_leds(OFF, OFF); P1IE |= BUTTON; __eint(); while(1) { LPM4; // Enter low-power mode 4 } }
int main() { setup_wdt_timer(); P1OUT &= ~(BIT0|BIT6); P1DIR |= BIT0|BIT6; IE1 |= WDTIE; // flag: IFG1 & WDTIFG __eint(); #if (WDT_MODE == 1) LPM2; //!? Why is LPM2 too much? #else LPM3; #endif }
/** * Timer Get * * Get time ellapsed on our timer (maxes out at TICK_MAX) */ uint16_t TimerGet(Timer_t * timer) { Tick_t diff; __dint(); // disable interrupts diff = local_tick - timer->local_last; //works with uint wrap around timer->local_last = local_tick; __eint(); // enable interrupts if (((uint32_t)timer->tick + (uint32_t)diff) > (uint32_t)TICK_MAX) { timer->tick = TICK_MAX; } else { timer->tick = timer->tick+diff; } return timer->tick; }
int main(void) { /* Watchdog timer disabled */ WDTCTL = WDTPW + WDTHOLD; BspInit(); DioInit(); TimerInit(10); // 0.1ms tick, 1 timed task, 0 loop task WRITE_SR(GIE); // Enable global interrupts __eint(); LcdInit(); BspReset(); // if ever we get here, we have nothing better to do but reset the micro while(1) { } }
int main() { // Stop watchdog timer WDTCTL = WDTPW + WDTHOLD; ADT7310_Setup(false); ADT7310_SetOneShotMode(); // wait for at least 240ms delay(100); SensorValue = ADT7310_QueryValue(); // Initialize UART UART_BAUD = BAUD; UART_CTL = UART_EN | UART_IEN_RX; ADT7310_Setup(true); __eint(); sleep(5); // infinite loop while (1) { ADT7310_SetOneShotMode_Async(); LPM1; // keep SMClk running for SimpleSPI // wait for at least 240ms sleep(240); ADT7310_QueryValue_Async(); LPM1; // keep SMClk running for TimerA SensorValue = ADT7310_GetValue_Async(); // SensorValue = temperature * 128 SensorValue = (SensorValue * 1000UL) >> 7; // SensorValue = temperature * 1000 printf("New value: %d.%03d\r\n",SensorValue/1000,SensorValue%1000); sleep(1760); } return 0; }
//------------------------------------------------------------------------------ // main() //------------------------------------------------------------------------------ void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer DCOCTL = 0x00; // Set DCOCLK to 1MHz // const_sfrb(CALDCO_1MHZ, CALDCO_1MHZ_); BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; P1OUT = 0x00; // Initialize all GPIO P1SEL = UART_TXD + UART_RXD; // Timer function for TXD/RXD pins P1DIR = 0xFF & ~UART_RXD; // Set all pins but RXD to output P2OUT = 0x00; P2SEL = 0x00; P2DIR = 0xFF; __eint(); TimerA_UART_init(); // Start Timer_A UART TimerA_UART_print("G2xx1 TimerA UART Test\r\n"); for (;;) { // Wait for incoming character __bis_SR_register(LPM0_bits); // Update board outputs according to received byte if (rxBuffer & 0x01) P1OUT |= 0x01; else P1OUT &= ~0x01; // P1.0 if (rxBuffer & 0x02) P1OUT |= 0x08; else P1OUT &= ~0x08; // P1.3 if (rxBuffer & 0x04) P1OUT |= 0x10; else P1OUT &= ~0x10; // P1.4 if (rxBuffer & 0x08) P1OUT |= 0x20; else P1OUT &= ~0x20; // P1.5 if (rxBuffer & 0x10) P1OUT |= 0x40; else P1OUT &= ~0x40; // P1.6 if (rxBuffer & 0x20) P1OUT |= 0x80; else P1OUT &= ~0x80; // P1.7 if (rxBuffer & 0x40) P2OUT |= 0x40; else P2OUT &= ~0x40; // P2.6 if (rxBuffer & 0x80) P2OUT |= 0x80; else P2OUT &= ~0x80; // P2.7 // Echo received character TimerA_UART_tx(rxBuffer); } }
int main(void) { /* Watchdog timer disabled */ WDTCTL = WDTPW + WDTHOLD; BspInit(); // Set up TimerInit(); LedCtor(&led, kPort1, kPin5); //P1.5 (red) ToggleLed(); __eint(); // Enable global interrupts while(true) { if (tog == 1) //1 second { tog = 0; ToggleLed(); } } }
int main(void) { // Stop watchdog timer WDTCTL = WDTPW + WDTHOLD; // enable P1.0 (red LED) and P1.6 (green LED) as outputs P1DIR |= 0x01 | 0x40; // switch on red LED and switch off green LED P1OUT = (P1OUT & ~(0x01 | 0x40)) | 0x01; // setup and enable GPIO interrupt P1IES &= ~INTR_PIN; // interrupt on rising transition P1IE |= INTR_PIN; // enable interrupt // enable interrupts __eint(); // main loop for (;;) { // toggle LEDs P1OUT ^= 0x01 | 0x40; // delay for(i=0; i< 20; i++); } }
int main(void) { unsigned long freq = 12345678L; // Measured frequency WDTCTL = WDTPW + WDTHOLD; // Stop watchdog if (CALBC1_16MHZ == 0xFF || CALDCO_16MHZ == 0xFF) { while(1); // If calibration constants erased // do not load, trap CPU!! } DCOCTL = 0; BCSCTL1 = CALBC1_16MHZ; // Set DCO DCOCTL = CALDCO_16MHZ; Setup_LED((unsigned char*)&display_buffer); Init_display(); P1SEL |= BIT0; // Use P1.0 as TimerA input P1SEL2 &= ~BIT0; // P1DIR &= ~BIT0; // P1OUT &= ~BIT0; // Enable pull down resistor to reduce stray counts P1REN |= BIT0; // __eint(); WDTCTL = WDTPW | WDTTMSEL | WDTCNTCL | WDTSSEL | WDTIS0; // Use WDT as interval timer // Default to 250 ms gate so that initial call to set_gate() // will switch to 1 s gate and update the LCD set_input(); // Set input and show on LCD //lcd_print("1 Second Gate", 3, 5); // WDTCTL = WDTPW | WDTTMSEL | WDTSSEL; //lcd_print(" 250 ms Gate ", 3, 5); // WDTCTL = WDTPW | WDTTMSEL | WDTSSEL | WDTIS0; for (;;) { freq = 0; // Clear frequency TACTL |= TACLR; // Clear TimerA // IFG1 &= ~WDTIFG; // Wait for WDT period to begin while(!(IFG1 & WDTIFG)); // // TACTL |= MC_2; // Start counting - TimerA continuous mode // IFG1 &= ~WDTIFG; // while(!(IFG1 & WDTIFG)) { // While WDT period.. if(TACTL & TAIFG) { // Check for TimerA overflow freq += 0x10000L; // Add 1 to msw of frequency TACTL &= ~TAIFG; // Clear overflow flag } // } // // TACTL &= ~MC_2; // Stop counting - TimerA stop mode if(TACTL & TAIFG) freq += 0x10000L; // Handle TimerA overflow that may have occured between // last check of overflow and stopping TimerA freq |= TAR; // Merge TimerA count with overflow if(WDTCTL & WDTIS0) freq <<= 2; // Multiply by 4 if using 250 ms gate // print_freq(freq); // Show on LCD // set_gate(freq); // Adjust gate time if necessary // // if(!(P1IN & BIT3)) { // Check if pushbutton down // ++clock_input; // Switch clock input // set_input(); // // } // } }
int main(void) { WDTCTL = WDTPW|WDTHOLD; DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; // BCSCTL2 = DIVS1|DIVS0; // //smclk = dco/8 ie 2Mhz BCSCTL1 |= XT2OFF; BCSCTL3 = XCAP_3; P1SEL = BIT1 | BIT2 ; P1SEL2 = BIT1 | BIT2 ; Setup_uart(); nret=sepline+26; __eint(); uart_puts(clrscrn); uart_puts(nret); uart_puts(sepline); uart_puts("* MSP430 : DHT11 *"); uart_puts(nret); uart_puts(sepline); P1DIR |= BIT0; P1OUT |= BIT0; myflags |= FLG_1ST_READ_DHT11; while(1){ readD1W(); uart_puts("RH:"); uart_putcuc(read_value[0]); uart_puts("\n\rT:"); uart_putcuc(read_value[2]); uart_puts("\n\rChck:"); if(read_value[4] == (read_value[3]+read_value[2]+read_value[1]+read_value[0])){ uart_puts("OK\n\r"); }else{ uart_puts("KO\n\r"); } uart_puts("\n\r"); TACTL = TASSEL_2|ID_3|MC_1; // smclk : 1Mhz | /8 | conti //CCR0 = 0xffff; CCR0 = 0xf480; TA0CCTL0 |= CCIE; timeoutvar=0; while(timeoutvar<10 /* /2 = sec */){ __bis_SR_register(LPM1_bits + GIE); }; } }
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { uint8_t bit = digitalPinToBitMask(interruptNum); uint8_t port = digitalPinToPort(interruptNum); if ((port == NOT_A_PIN) || !((mode == FALLING) || (mode == RISING) || (mode == CHANGE))) return; __dint(); switch(port) { case P1: if(mode != CHANGE) { P1IES = mode ? P1IES | bit : P1IES & ~bit; } else { intChangeVectP1 |= bit; P1IES = (P1IN & bit) ? (P1IES | bit) : (P1IES & ~bit); } P1IFG &= ~bit; intFuncP1[bit_pos(bit)] = userFunc; P1IE |= bit; break; #if defined(PORT2_VECTOR) case P2: if(mode != CHANGE) { P2IES = mode ? P2IES | bit : P2IES & ~bit; } else { intChangeVectP2 |= bit; P2IES = (P2IN & bit) ? (P2IES | bit) : (P2IES & ~bit); } P2IFG &= ~bit; intFuncP2[bit_pos(bit)] = userFunc; P2IE |= bit; break; #endif #if defined(PORT3_VECTOR) case P3: if(mode != CHANGE) { P3IES = mode ? P3IES | bit : P3IES & ~bit; } else { intChangeVectP3 |= bit; P3IES = (P3IN & bit) ? (P3IES | bit) : (P3IES & ~bit); } P3IFG &= ~bit; intFuncP3[bit_pos(bit)] = userFunc; P3IE |= bit; break; #endif #if defined(PORT4_VECTOR) case P4: if(mode != CHANGE) { P4IES = mode ? P4IES | bit : P4IES & ~bit; } else { intChangeVectP4 |= bit; P4IES = (P4IN & bit) ? (P4IES | bit) : (P4IES & ~bit); } P4IFG &= ~bit; intFuncP4[bit_pos(bit)] = userFunc; P4IE |= bit; break; #endif default: break; } __eint(); }
int main(void) { watchdog_off(); clock_init_1MHz(); port1_direction = 0xFC; // 1111 1011 --> p1.2 is Rx port2_direction = 0xFF; // 1111 1111 port1_output &= 0x00; // turn off motors or it'll heat up! port2_output &= 0x00; // turn off motors or it'll heat up! delay(0xffff); // wait for initialization or something /* initialize serial clock, tx and rx parts */ serial_init_clock(); serial_init_tx(); serial_init_rx(); /* enable interrupts */ __eint(); while (1) { unsigned int i; unsigned char c; c = serial_getchar(); //debug_char(c); if (c == 'o') { // open flash(); for (i=0; i<ROTATE_AMOUNT; i++) { port1_output = 0x20; // 0010 0000 port2_output = 0x05; // 0000 0101 delay(DELAY); port1_output = 0x28; // 0010 1000 port2_output = 0x04; // 0000 0100 delay(DELAY); port1_output = 0x20; // 0010 0000 port2_output = 0x06; // 0000 0110 delay(DELAY); port1_output = 0x30; // 0011 0000 port2_output = 0x04; // 0000 0100 delay(DELAY); } port1_output &= 0x00; // turn off motors! port2_output &= 0x00; // turn off motors! } else if (c == 'c') { // close flash(); flash(); for (i=0; i<ROTATE_AMOUNT; i++) { port1_output = 0x30; // 0011 0000 port2_output = 0x04; // 0000 0100 delay(DELAY); port1_output = 0x20; // 0010 0000 port2_output = 0x06; // 0000 0110 delay(DELAY); port1_output = 0x28; // 0010 1000 port2_output = 0x04; // 0000 0100 delay(DELAY); port1_output = 0x20; // 0010 0000 port2_output = 0x05; // 0000 0101 delay(DELAY); } port1_output &= 0x00; // turn off motors! port2_output &= 0x00; // turn off motors! } } }
int main(void) { //if (!(P1IN&usbminus)) usbwait = 0; // no usb D- pullup, no bootloader // no pull-up, app present, run app if (!(P1IN&usbminus) && (*((char*) *((uint16_t*) 0xffbe)) != -1)) asm("br &0xffbe"); WDTCTL = WDTPW | WDTHOLD; // Stop watchdog P1OUT = 0; // Prepare Port 1 usbdir = ~(usbplus | usbminus); // USB lines are inputs //P1SEL |= BIT4; // SMCLK for measurements on P1.4 P1DIR |= BIT5; #ifdef USE_32768HZ_XTAL BCSCTL1 = DIVA_3 | 15; // ACLK / 8, Range 15 DCOCTL = 127; // ~15 MHz TACTL = TASSEL_2 | MC_2 | TACLR; // Continous up mode with clocking from SMCLK TACCTL0 = CM_1 | CCIS_1 | CAP | CCIE; // Setup CCR0 for capturing on rising edges of ACLK with enabled interrupts __eint(); #else P2SEL = 0; P2DIR = BIT6|BIT7; // indicator led P2OUT = 0; BCSCTL1 = 15; // Range 15 DCOCTL = 127; // ~15 MHz #endif __delay_cycles(2000000); // Allow some time to stabilize power uint8_t usbwait=12; usbies = 0; // Interrupt on rising edge of D+ because there is keep-alive signaling on // D- that would cause false syncs usbifg = 0; // No interrupt pending uint16_t addrStart=0, addrEnd=0, addrCurrent=0, addrSave=0; uint8_t initialized=0; uint16_t heartbeat = 0; uint8_t idx=0, inBuf[48]; uint8_t *buffUp=0; uint8_t next_time_flash_erase = 0; static uint16_t const USB_IDLE_DETECT_THRESHOLD = 16384; uint16_t usb_idle_detect_counter = 0; uint8_t usb_idle = 0; /* What is this usb idle thing ? We are in a bootloader, its aim is to receive via usb a new program to be flashed. But erasing and writing to flash takes quite a big time, and usb timing is tight. The usb protocol needs that response packets be sent within a certain time frame, and if the mcu misses that time frame, the host can consider the device as malfunctioning. The idea behind the usb idle thing is to delay operations that take time to a moment where we hope the mcu will not have any usb packet to answer. Specifically : 1. when the host side sends an address where flash must be erased, the host sleeps for a certain amount T of milliseconds before sending another command. Within T milliseconds, the mcu has time to answer remaining usb packets and erase the requested flash segment. 2. when the host side sends payload bytes (which are bytes of the program to be flashed), the mcu has no time to write them into flash and answer usb packets. It stores them in a small RAM buffer, and writes them to flash once the host sleeps a bit. Here, the host sends 32 bytes before sleeping and letting the mcu write those bytes to flash. To detect usb idle, we simply count how many "for-ever" loops we have done since last received usb packet, and beyond a threshold, we consider being in usb idle mode, that is to say : we consider the host will not send any other usb packet before we have finished with flashing and being able to handle a new usb packet. */ for (;;) // for-ever { // Detect USB idle if(initialized && !usb_idle) { usb_idle_detect_counter++; if(usb_idle_detect_counter > USB_IDLE_DETECT_THRESHOLD) usb_idle = 1; } if (!heartbeat++) { if (initialized) // usb reset from host occured { P2OUT ^= BIT6; }//if else { // no reset, run app if present //if (!usbwait && (*((char*) 0xc000) != -1)) { if (!usbwait && (*((char*) *((uint16_t*) 0xffbe)) != -1)) { _BIC_SR(GIE); TACTL = 0; P2DIR = P1DIR = 0; P2OUT = P1OUT = 0; asm("br &0xffbe"); } else { usbwait--; } } } unsigned n = 80; // Reset if both D+ and D- stay low for a while // 10 ms in SE0 is reset // Should this be done in the ISR somehow??? while (!initialized && !(usbin & (usbplus | usbminus))) { if (!--n) { CurrentAddress = 0; // Device has initial address of 0 NewAddress = 0; // addrCurrent = addrStart = addrEnd = 0; #ifndef USE_32768HZ_XTAL if (!initialized) { TACTL = TASSEL_2 | MC_2 | TACLR; // Continous up mode with clocking from SMCLK initialized = 1; //P2OUT |= BIT6; // debug use, led indicate we are trying sync n = 1000; // time to stabilize clock while (--n) { while (!(usbin&usbminus)); TACTL |= TACLR; while ((usbin&usbminus)); if (TAR < 15000) ++DCOCTL; else --DCOCTL; } usbie = usbplus; _BIS_SR(GIE); } #else usbie = usbplus; _BIS_SR(GIE); initialized = 1; #endif break; } } if(DataPacketBuffer[0]) // Check if the USB SIE has received a packet { uint8_t packet_size = *DataPacketBuffer; // we save packet_size here to avoid it being DataPacketBuffer[0] = 0; // overwritten by "fast" status packet that follows usb_idle_detect_counter = 0; usb_idle = 0; // At the end of the buffer is the PID of the preceding token packet // Check if it is a SETUP packet if(DataPacketBuffer[15] == USB_PID_SETUP) { // Handle the packet, get a response if((DataToTransmit = ProtocolSetupPacket(DataPacketBuffer))) { // Setup to send the response Data_PID_Toggle = USB_PID_DATA0; DataToTransmitOffset = 1; } else { // Send STALL PID if there is no response ReadyForTransmit = usb_packet_stall; } } else if (DataPacketBuffer[15] == USB_PID_OUT) // Check if it is an OUT packet { // will be getting stuffs here // incoming LL-PP-RI-(d0-d1-d2-d3-d4-d5)-C1-C2 // ex. 0a-4b-01-(00-4b-0e-8d-0d-4e)-56-fa // LL - length, ours are always 0a or 08 // PP - PID, should only be DATA0 or DATA1 (0xc3 or 0x4b) - 4b for us // RI - application level control byte - the HID report id. Here : // == 1, request flash write, start address / length follows // == 2, no special instruction, just carry data // d? - firmware data, each packet carries up to LL-4 bytes of data // for report id 1 : report count = 6, LL = 0a, data bytes = 6 // for report id 2 : report count = 4, LL = 08, data bytes = 4 // C? - packet checksum, application does not use these // uint8_t *cp = DataPacketBuffer+2; //if (*DataPacketBuffer == 0x0A && *cp == 0x01) if (packet_size == 0x0A) { // flash write request (report id 1) cp++; addrCurrent = addrStart = (*cp<<8) + *(cp+1); // get start address high bytes cp += 2; addrEnd = (*cp<<8) + *(cp+1); //______________ interrupt vector, don't do immediate flash write if (addrStart >= 0xff00) { buffUp = inBuf; } else { addrSave = addrEnd; buffUp = 0; idx = 0; next_time_flash_erase = 1; } } //else if (*DataPacketBuffer == 0x08 && *cp == 0x02) else if (packet_size == 0x08) { uint8_t c=0; // receive bytes (report id 2) cp++; // skip report id if (buffUp) // we are receiving interrupt vector bytes { for (c=0;c<4;c++) *((uint8_t *) buffUp++) = *cp++; addrCurrent += 4; } else { // we are receiving normal data for (c=0;c<4;c++) inBuf[c+idx] = *cp++; idx += 4; } } } // else if USB_PID_OUT //DataPacketBuffer[0] = 0; // Done with received packet, don't process again, allow USB SIE to receive the next packet } // if USB SIE has received a packet // Check if an outgoing packet needs chunking and the USB SIE // is ready for it if (DataToTransmit && !ReadyForTransmit) { PacketGenerator(); // Make a chunk with CRC } if (!ReadyForTransmitIrqIn) { // Check if the USB SIE is ready for an endpoint 1 packet /* IrqIn(IrqInSendPacketBuffer); // Build the packet CRC(IrqInSendPacketBuffer); // Append CRC ReadyForTransmitIrqIn = IrqInSendPacketBuffer; // Send it */ } // We have received interrupt vector address, and data bytes. // USB is idle which means we can write to flash if(usb_idle && buffUp && addrCurrent > addrStart && addrStart >= 0xff00) { //_____ we doing interrupt vector, so don't be interrupted uint16_t savIntr = *((uint16_t *) 0xffe4); uint16_t savReset = *((uint16_t *) 0xfffe); _BIC_SR(GIE); FCTL1 = FWKEY+ERASE; FCTL3 = FWKEY; *((uint8_t *) 0xff00) = 0; FCTL1 = FWKEY+WRT; uint8_t *dp = (uint8_t *) 0xffa0; uint8_t *vp = (uint8_t *) 0xffe0; uint8_t *cp = inBuf; uint8_t i=0x20; while (i--) *dp++ = *cp++; // write to flash backup copy *((uint16_t *) (inBuf+4)) = savIntr; // use bootloader's interrupt *((uint16_t *) (inBuf+30)) = savReset;// reset goes to bootloader i=0x20; cp = inBuf; while (i--) *vp++ = *cp++; // write to flash real vector *((uint16_t *) (0xff80)) = addrSave; // save application's end address *((uint16_t *) (0xffde)) = 0xaa55; // disable factory BSL buffUp = 0; FCTL1 = FWKEY; FCTL3 = FWKEY+LOCK; _BIS_SR(GIE); } // We have received addresses. USB is idle : erase flash. if(usb_idle && next_time_flash_erase) { FCTL2 = FWKEY+FSSEL0+(30); FCTL1 = FWKEY+ERASE; FCTL3 = FWKEY; *((uint8_t *) addrCurrent) = 0; FCTL1 = FWKEY+WRT; next_time_flash_erase = 0; } // We have received data bytes, and USB is idle : flash them. if(usb_idle && !buffUp && addrStart < 0xff00 && idx) { uint8_t c=0; for(c = 0; c < idx; c++) *((uint8_t *) addrCurrent++) = inBuf[c]; idx = 0; if (!(addrCurrent&0x01ff)) { //____ we are crossing 512byte/block boundary //____ erase and get ready for next block FCTL1 = FWKEY+ERASE; FCTL3 = FWKEY; *((uint8_t *) addrCurrent) = 0; FCTL1 = FWKEY+WRT; } } if(addrCurrent >= addrEnd && !buffUp) // !buffUp means that we have handled the interrupt vector in an usb idle time { //____ lockup flash, we are over. FCTL1 = FWKEY; FCTL3 = FWKEY+LOCK; addrCurrent = addrStart = addrEnd = 0; idx = 0; } }//for-ever }
int main(void) { int prevPwm = 0; int prevDegC = 0; int degC; int pwm; WDTCTL = WDTPW + WDTSSEL + WDTCNTCL; // ACLK / 32768: 3,2s // config clk BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; BCSCTL3 = LFXT1S_2; // Use VLO // Unused pins as inputs with pull-down. P1DIR = 0; P1REN = ~(MOSFET_GATE_PIN | BIT1 | BIT2 | ZERO_DETECT_PIN); P2DIR = 0; P2REN = 0xff; initConsole(); printf("Start\r\n"); ADC10AE0 |= (1 << 4); // PA.1 ADC option select // Configure MOSFET gate pin P1OUT &= ~MOSFET_GATE_PIN; P1DIR |= MOSFET_GATE_PIN; // P1.6 output // Configure voltage zero level detect pin P1IE |= ZERO_DETECT_PIN; P1IES |= ZERO_DETECT_PIN; // Timer A0 for PWM TA0CCR0 = (ACLK_HZ / PWM_HZ) - 1; TA0CCR1 = 0; TA0CCTL1 = OUTMOD_7 | CCIE; // Positive PWM TA0CCTL0 = CCIE; TA0CTL = TASSEL_1 + MC_1 + TACLR + TAIE; // ACLK, upmode __eint(); while (1) { WDTCTL = WDTPW + WDTSSEL + WDTCNTCL; // ACLK / 32768: 3,2s __bis_status_register(CPUOFF); degC = adcTemp - *tempCal30; degC = 30 + ((float)degC) / 1024 * 1500 / 3.55; // Filter temperature changes, // during cooling react only if change // is more than 2 degrees. if (degC > prevDegC) prevDegC = degC; else if (prevDegC - degC >= 2) prevDegC = degC; printf ("adc %d degc %d filtdegc %d ", adcPot, degC, prevDegC); if (adcPot < 20) adcPot = 0; else if (adcPot > 1000) adcPot = 1000; pwm = adcPot / 10; // PWM % if (pwm > 10 && prevDegC > 40) { // check overheat pwm = 10; printf ("overheat "); } printf("pwm %d %%\r\n", pwm); if (abs(prevPwm - pwm) > 2) { prevPwm = pwm; TA0CCR1 = ((float)pwm) / 100.0 * (ACLK_HZ / PWM_HZ); } } }