int jhd_162A_init() { /* int delay=0; */ /* int nbr_selectedPins=6; */ /* unsigned int data_to_write; */ /* The order is MSB -> LSB */ char * d_gpio[8] = {"GPIO_77", "GPIO_76", "GPIO_75", "GPIO_74", "GPIO_73", "GPIO_72", "GPIO_71", "GPIO_70"}; char * ctrl_gpio[3] = {"GPIO_66", "GPIO_67", "GPIO_68"}; pins_init(&ctrl_gpio, 3); pins_init(&d_gpio, 8); /* pinMode_multiple(enabled_gpio,selectedPins,nbr_selectedPins,"out"); */ /* //E RS DB4 DB5 DB6 DB7 = 001100 (2) = 12 (10) */ /* data_to_write=12; */ /* digitalWrite_multiple(enabled_gpio,6,data_to_write); */ /* pulsePin(enabled_gpio,data_to_write,6,5,delay); */ /* delayms(delay); */ /* //E RS DB4 DB5 DB6 DB7 = 000100 (2) = 4 (10) : 4 bit mode */ /* data_to_write=4; */ /* digitalWrite_multiple(enabled_gpio,6,data_to_write); */ /* pulsePin(enabled_gpio,data_to_write,6,5,delay); */ /* delayms(delay); */ /* //enable display part 1 - Display ON/OFF & Cursor */ /* //E RS DB4 DB5 DB6 DB7 = 000000 (2) = 0 (10) */ /* data_to_write=0; */ /* digitalWrite_multiple(enabled_gpio,6,data_to_write); */ /* pulsePin(enabled_gpio,data_to_write,6,5,delay); */ /* delayms(delay); */ /* //enable display part 2 - Display ON/OFF & Cursor */ /* //E RS DB4 DB5 DB6 DB7 = 000011 (2) = 3 (10) */ /* data_to_write=3; */ /* digitalWrite_multiple(enabled_gpio,6,data_to_write); */ /* pulsePin(enabled_gpio,data_to_write,6,5,delay); */ /* delayms(delay); */ /* //two line mode 5x7 part 1 */ /* //E RS DB4 DB5 DB6 DB7 = 000100 (2) = 4 (10) */ /* data_to_write=4; */ /* digitalWrite_multiple(enabled_gpio,6,data_to_write); */ /* pulsePin(enabled_gpio,data_to_write,6,5,delay); */ /* delayms(delay); */ /* //two line mode 5x7 part 2 (the 0 next to the 1 specifies the 5x7) */ /* //E RS DB4 DB5 DB6 DB7 = 000001 (2) = 1 (10) */ /* data_to_write=1; */ /* digitalWrite_multiple(enabled_gpio,6,data_to_write); */ /* pulsePin(enabled_gpio,data_to_write,6,5,delay); */ /* delayms(delay); */ return 0; }
/*================================================================================== * 函 数 名: Set_System * 参 数: None * 功能描述: 初始化系统 * 返 回 值: None * 备 注: * 作 者: gaodb * 创建时间: 2012.10 ==================================================================================*/ void Set_System(void) { rcc_init(); peri_clk_init(); NVIC_Configuration(); pins_init(); systick_init(); timer2_init(); timer4_init(); RAY12_ADC_Init(); LCD_Config(); DispInit(); uart1_init(BAUD_RATE_57600); uart2_init(BAUD_RATE_57600); dac1_init(); sys_variable_init(); }
int main(void) { // Enable peripherals system_init(); pins_init(); us1_init(); us0_init(); twi_init(); // Initialize state machines qcfp_init(); eq_init(); // Once everything is initialized, enable interrupts globally interrupts_enable(); // Enable Expansion module 3 and 4 (sensors plugged into these temporarily) AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA20; AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA23; sensors_init(); eq_post_timer(gpio_led_dance, 250, eq_timer_periodic); while(1) { eq_dispatch(); eq_dispatch_timers(); } return 0; }
/* One start -----------------------------------------------------------------*/ static void startup() { unsigned char err = 0; init_hardware(); dbg_init(); dbg_print_app_ver(); crc32_init(); pins_init(); //spi0_init(); // used in m25pexx.c (FLASH) spi1_init(); adc_init(); err = memory_init(); // memory initialization, external EEPROM (used in pref.c, orion.c), external FLASH, internal FLASH if (err) goto err; pref_init(); ds1390_init(); evt_fifo_init(); GPS_init(); //SIM900_init(); light_init(); saveDatePorojectIP();//новые параметры связи vpu_init(); /*VPU start*/ //Init_USB(); //if (PROJ.jornal.power_on) //Event_Push_Str("СТАРТ"); return; err: Err_led(err); }
static void power_on_init(void) { /* JTAG must be off to use RB8-11,12,13 */ if (MCU_CONFIG1 & ~JTAGEN_OFF) while(1); pins_init(); // Initialize pins at startup clr_reset_state(); // Needed for MCLR rst_events = 0; rst_num = 0; }
void hardware_init(void) { power_init(); clock_init(); pins_init(); uart_init(); timers_init(); adc_init(); irq_init(); }
// Cannot declare static, as board.h declares this extern void board_init(void) { // Disable watchdog timer WDT->WDT_MR = WDT_MR_WDDIS; sysclk_enable_peripheral_clock(ID_PIOA); sysclk_enable_peripheral_clock(ID_PIOB); sysclk_enable_peripheral_clock(ID_PIOC); pins_init(); pmc_enable_periph_clk(ID_PIOB); }
int io_conn_test(void) { char ch; serial.printf("\r\n----------- I/O Connector ----------\r\n"); do { pins_init(); serial.printf("Blinking LEDs ...\r\n"); gpio_conn_run(); serial.printf("Done\r\n"); serial.printf("Did %d LEDs blink? [y]es [n]o [r]epeat: ", LEN(gpio)); ch = serial.getc(); } while (ch == 'r'); if (ch == 'y') return PASS; else return FAIL; }
int main(void) { // set clock prescaler for 8MHz CLKPR = 0x80; CLKPR = 0x01; cli(); power_all_disable(); power_spi_enable(); power_timer1_enable(); set_sleep_mode(SLEEP_MODE_IDLE); pins_init(); delay_ms(345); // arbitrary uint8_t dpi = 0x0f; // default to 800 if (!(PIND & (1<<0))) // if left is pressed at boot dpi = 0xff; // set to 12800 pmw3366_init(dpi); nrf24_init(); // button stuff // previous debounced state uint8_t btn_prev = ~(PIND); // time (in 125us) button has been unpressed. // consider button to be released if this time exceeds DEBOUNCE_TIME. uint8_t btn_time[3] = {0, 0, 0}; // absolute positions. relies on integer overflow union motion_data x = {0}, y = {0}; // wheel stuff uint8_t whl_prev_same = 0; // what A was the last time A == B uint8_t whl_prev_diff = 0; // what A was the last time A != B // absolute scroll position. relies on integer overflow int8_t whl = 0; // begin burst mode for 3366 spi_set3366mode(); SS_3366_LOW; spi_3366_write(0x50, 0x00); SS_3366_HIGH; // set up timer1 to set OCF0A in TIFR0 every 1ms TCCR1A = 0x00; TCCR1B = 0x09; // CTC, 8MHz OCR1A = 7999; // main loop nominal period (7999 + 1) / 8MHz = 1ms OCR1B = 320; // timing of when to read burst mode data from sensor OCR1C = 800; // timing of when to load nrf24l01+ with data // let receiver know if it's the first time sending data, so that it // can reset the reference for absolute position and that there's no // jump when rebooting the mouse // uint8_t first = 0x80; // transmitted as MSB with button data below. // when sync reaches 0, always send a packet with bit 6 in btn set, to // tell the receiver to calculate the timing offset. // when sync reaches 1, always send a packet requesting ACK to load the // timing offset // i.e. when it overflows, so 256ms periodicity. // when sync reaches 0, afk increments. // afk is cleared by any motion or button press. // when afk reaches AFK_TIMEOUT, go into powerdown mode. for (uint8_t first = 0x80, sync = 0, afk = 0; ; first = 0, sync++) { // sync to 1ms intervals using timer1 // if (TIFR1 & (1<<OCF1A)) PORTD |= (1<<6); TIMSK1 |= (1<<OCIE1A); sei(); sleep_mode(); cli(); TIMSK1 &= ~(1<<OCIE1A); TIFR1 |= (1<<OCF1A); TIFR1 |= (1<<OCF1B); TIFR1 |= (1<<OCF1C); // begin burst mode read spi_set3366mode(); SS_3366_LOW; spi_send(0x50); // do stuff here instead of busy waiting for 35us // read wheel int8_t dwhl = 0; const uint8_t whl_a = WHL_A_IS_HIGH; const uint8_t whl_b = WHL_B_IS_HIGH; // if (whl_a == whl_b) { // if (whl_a != whl_prev_same) { // dwhl = 2 * (whl_a ^ whl_prev_diff) - 1; // whl += dwhl; // whl_prev_same = whl_a; // } // } else // whl_prev_diff = whl_a; if (whl_a != whl_b) whl_prev_diff = whl_a; else if (whl_a != whl_prev_same) { dwhl = 2 * (whl_a ^ whl_prev_diff) - 1; whl += dwhl; whl_prev_same = whl_a; } // read buttons /* PIND 0 EIFR 0: low, no edges -> is low PIND 0 EIFR 1: low, edge -> is low PIND 1 EIFR 0: high, no edges -> always high during last 1ms PIND 1 EIFR 1: high, edge -> low at some point in the last 1ms */ const uint8_t btn_unpressed = PIND & ~(EIFR); EIFR = 0b00000111; // clear EIFR // manual loop debouncing for every button uint8_t btn_dbncd = 0x00; #define DEBOUNCE(index) \ if ((btn_prev & (1<<index)) && (btn_unpressed & (1<<index))) { \ btn_time[index]++; \ if (btn_time[index] < DEBOUNCE_TIME) \ btn_dbncd |= (1<<index); \ } else { \ btn_time[index] = 0; \ btn_dbncd |= (~btn_unpressed) & (1<<index); \ } DEBOUNCE(0); DEBOUNCE(1); DEBOUNCE(2); #undef DEBOUNCE // wait until 35us have elapsed since spi_send(0x50) // if (TIFR1 & (1<<OCF1B)) PORTD |= (1<<6); TIMSK1 |= (1<<OCIE1B); sei(); sleep_mode(); cli(); TIMSK1 &= ~(1<<OCIE1B); union motion_data dx, dy; spi_send(0x00); // motion, not used spi_send(0x00); // observation, not used dx.lo = spi_recv(); dx.hi = spi_recv(); dy.lo = spi_recv(); dy.hi = spi_recv(); SS_3366_HIGH; x.all += dx.all; y.all += dy.all; if (sync == 0) afk++; const uint8_t changed = (btn_dbncd != btn_prev) || dx.all || dy.all || dwhl; if (changed) afk = 0; if (changed || (sync <= 1)) { btn_prev = btn_dbncd; // W_TX_PAYLOAD if sync == 1, W_TX_PAYLOAD_NOACK otherwise const uint8_t mode = (sync == 1) ? 0b10100000 : 0b10110000; // send miscellaneous info using top bits of btn byte uint8_t btn_send = btn_dbncd | first; // first is either 0x80 or 0 if (sync == 0) btn_send |= 0x40; // try to transmit at the same time every frame // if (TIFR1 & (1<<OCF1C)) {PORTD |= (1<<6);} TIMSK1 |= (1<<OCIE1C); sei(); sleep_mode(); cli(); TIMSK1 &= ~(1<<OCIE1C); spi_setnrf24mode(); SS_NRF24_LOW; spi_send(0x20 | 0x07); // STATUS spi_send(0b01110000); // clear IRQ SS_NRF24_HIGH; SS_NRF24_LOW; spi_send(0b11100001); // flush tx SS_NRF24_HIGH; SS_NRF24_LOW; spi_send(0b11100010); // flush rx SS_NRF24_HIGH; SS_NRF24_LOW; spi_send(mode); spi_send(btn_send); spi_send(x.lo); spi_send(x.hi); spi_send(y.lo); spi_send(y.hi); spi_send(whl); SS_NRF24_HIGH; // pulse CE to transmit CE_HIGH; delay_us(12); CE_LOW; if (sync == 1) { // get ack payload of timing offset delay_us(400); if (IRQ_IS_LOW) { // recycle motion_data union for timing union motion_data offset; SS_NRF24_LOW; spi_send(0b01100001); offset.lo = spi_recv(); offset.hi = spi_recv(); SS_NRF24_HIGH; // shift TCNT1 by the offset, plus a // little more because of the time it // takes to add stuff to TCNT1. TCNT1 += offset.all + 11; } } } // power down if afk if (afk > AFK_TIMEOUT) { // enable external interrupts on INT0/1/2/3, PCINT0 EIMSK = 0b00000111; PCICR = 0x01; // go power down mode; wake up on interrupt set_sleep_mode(SLEEP_MODE_PWR_DOWN); sei(); sleep_mode(); cli(); // disable external interrupts PCICR = 0; EIMSK = 0; // restore state set_sleep_mode(SLEEP_MODE_IDLE); sync = 0; afk = 0; } } }