void motor_init(void){ //motor_deadzone_left=100; //motor_deadzone_right=100; /*connection config: Hardware DIR PWM Physical location ---------------+---------------+---------------+----------------- Motor right PTD9 FTM1_CH1 top?? Motor left PTD7 FTM1_CH0 top?? */ FTM_PWM_init(FTM1,CH0,10000,0);//motor takes 0-10000 pwm values for duty FTM_PWM_init(FTM1,CH1,10000,0);//motor takes 0-10000 pwm values for duty gpio_init(PORTD,9,GPO,0); // Right motor dir gpio_init(PORTD,7,GPO,0); // Left motor dir gpio_init(PORTC,4,GPI_UP,1);//encoder rt dir gpio_init(PORTC,5,GPI_UP,1);//encoder lt dir DisableInterrupts; exti_init(PORTA,6,rising_up); //inits left encoder interrupt capture exti_init(PORTA,7,rising_up); //inits right encoder interrupt capture EnableInterrupts; }
/*! \brief configure comparator \param[in] none \param[out] none \retval none */ void cmp_config(void) { /* enable GPIOA clock */ rcu_periph_clock_enable(RCU_GPIOA); /* configure PA1 as comparator input */ gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1); gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_PULLUP, GPIO_PIN_1); /* enable comparator clock */ rcu_periph_clock_enable(RCU_CFGCMP); /* configure comparator channel0 */ cmp_mode_init(CMP_CHANNEL_CMP0, CMP_LOWSPEED, CMP_VREFINT, CMP_HYSTERESIS_HIGH); cmp_output_init(CMP_CHANNEL_CMP0, CMP_OUTPUT_NONE, CMP_OUTPUT_POLARITY_NOINVERTED); /* configure comparator channel1 */ cmp_mode_init(CMP_CHANNEL_CMP1, CMP_LOWSPEED, CMP_1_2VREFINT, CMP_HYSTERESIS_HIGH); cmp_output_init(CMP_CHANNEL_CMP1, CMP_OUTPUT_NONE, CMP_OUTPUT_POLARITY_NOINVERTED); /* configure exti line */ exti_init(EXTI_21, EXTI_INTERRUPT, EXTI_TRIG_BOTH); exti_init(EXTI_22, EXTI_INTERRUPT, EXTI_TRIG_BOTH); /* configure ADC_CMP nvic */ nvic_irq_enable(ADC_CMP_IRQn, 0, 0); /* enable comparator window */ cmp_window_enable(); /* enable comparator channels */ cmp_channel_enable(CMP_CHANNEL_CMP0); cmp_channel_enable(CMP_CHANNEL_CMP1); }
void Ov7725_exti_Init() { //DMA通道0初始化,PTB0上升沿触发DMA传输,源地址为PTD_BYTE0_IN,目的地址为:BUFF ,每次传输1Byte,传输320次后停止传输,保存目的地址不变 DMA_PORTx2BUFF_Init(CAMERA_DMA_CH, (void *)&PTB_BYTE0_IN, (void *)IMG_BUFF, PTB8, DMA_BYTE1, 320*2-2, DMA_rising_down_keepon); DMA_DIS(DMA_CH0); //先关闭 DMA DMA_IRQ_DIS(DMA_CH0); //不需要使用DMA中断,由行中断来控制DMA传输 exti_init(PORTA,29,rising_down); //场中断,内部下拉,上升沿触发中断 exti_init(PORTA,28,rising_down); //行中断,内部下拉,上升沿触发中断 }
int test_exti(struct harness_t *harness_p) { int i; struct exti_driver_t exti; struct pin_driver_t pin; pin_init(&pin, &pin_d4_dev, PIN_OUTPUT); pin_write(&pin, 1); BTASSERT(exti_init(&exti, &exti_d3_dev, EXTI_TRIGGER_FALLING_EDGE, isr, NULL) == 0); BTASSERT(exti_start(&exti) == 0); for (i = 0; i < 10; i++) { pin_write(&pin, 0); time_busy_wait_us(10000); pin_write(&pin, 1); time_busy_wait_us(10000); } std_printf(FSTR("flag = %d\r\n"), (int)flag); BTASSERT(flag == 10); return (0); }
//initialize rc ppm input; void ppm_init(void) { uint8 i; for(i=0;i<=8;i++) { exti_init(PORTB,channel_port[i],either_up,2); } }
void store() { exti_disable(); left_count(425); backward_count(1000); put_down(); up(); backward_count(300); right_count(425); forward_count(900); right_count(850); exti_init(); }
/* * 函数名:NRF_Init * 描述 :SPI的 I/O配置 * 输入 :无 * 输出 :无 * 调用 :外部调用 */ void NRF_Init(void) { //配置NRF管脚复用 spi_init(NRF_SPI,MASTER); //初始化SPI,主机模式 gpio_init(PORTE,28, GPO,LOW); //初始化CE,默认进入待机模式 //gpio_init(PORTE,27, GPI,LOW); //初始化IRQ管脚为输入 gpio_init(PORTA,14, GPO,HIGH); //初始化PCSN管脚为输出,低电平选中 #if IS_USE_ISR exti_init(PORTE,27, falling_up); //初始化IRQ管脚为 :下降沿触发,内部上拉 #else gpio_init(PORTE,27, GPI,LOW); //初始化IRQ管脚为输入 #endif //配置NRF寄存器 NRF_CE_LOW(); NRF_WriteReg(NRF_WRITE_REG+SETUP_AW,ADR_WIDTH - 2); //设置地址长度为 TX_ADR_WIDTH NRF_WriteReg(NRF_WRITE_REG+RF_CH,CHANAL); //设置RF通道为CHANAL NRF_WriteReg(NRF_WRITE_REG+RF_SETUP,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启 NRF_WriteReg(NRF_WRITE_REG+EN_AA,0x01); //使能通道0的自动应答 NRF_WriteReg(NRF_WRITE_REG+EN_RXADDR,0x01); //使能通道0的接收地址 //RX模式配置 //NRF_WriteBuf(NRF_WRITE_REG+RX_ADDR_P0,RX_ADDRESS,RX_ADR_WIDTH);//写RX节点地址 NRF_WriteReg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH); //选择通道0的有效数据宽度 //TX模式配置 //NRF_WriteBuf(NRF_WRITE_REG+TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH); //写TX节点地址 ,主要为了使能ACK NRF_WriteReg(NRF_WRITE_REG+SETUP_RETR,0x0F); //设置自动重发间隔时间:250us + 86us;最大自动重发次数:15次 #if IS_AUTO_RX_MODE NRF_RX_Mode(); //默认进入接收模式 #endif NRF_CE_HIGH(); while(NRF_Check() == 0); //检测无线模块是否插入:如果卡在这里,说明没检测到无线模块 }
/*! \brief main function \param[in] none \param[out] none \retval none */ int main(void) { /* NVIC config */ nvic_priority_group_set(NVIC_PRIGROUP_PRE0_SUB4); nvic_irq_enable(LVD_IRQn,0,0); /* clock enable */ rcu_periph_clock_enable(RCU_PMU); /* led1 config */ gd_eval_ledinit (LED1); /* led1 turn on */ gd_eval_ledon(LED1); /* EXTI_16 config */ exti_init(EXTI_16, EXTI_INTERRUPT, EXTI_TRIG_BOTH); /* configure the lvd threshold to 2.9v(gd32f130_150) or 4.5v(gd32f170_190), and enable the lvd */ pmu_lvd_select(PMU_LVDT_7); while(1); }
int main(void) { clock_init(); serial_init(9600); exti_init(); nrf_init(); cio_print("nRF2401 v0.1 - TestClient ESBPL-exti\n\r"); nrf_configure_esbpl_rx(); // setup payload prx.size = 1; // receive payload size 1Byte ptx.size = 1; // send payload size 1Byte ptx.data[0] = 0; // set payload to 0 // Nothing to do here since ISR does all the work while (1) { __asm__("nop"); } return 0; }
int uart_soft_init(struct uart_soft_driver_t *self_p, struct pin_device_t *tx_dev_p, struct pin_device_t *rx_dev_p, struct exti_device_t *rx_exti_dev_p, int baudrate, void *rxbuf_p, size_t size) { ASSERTN(self_p != NULL, EINVAL); ASSERTN(tx_dev_p != NULL, EINVAL); ASSERTN(rx_dev_p != NULL, EINVAL); ASSERTN(rx_exti_dev_p != NULL, EINVAL); ASSERTN(rxbuf_p != NULL, EINVAL); self_p->sample_time = BAUDRATE2US(baudrate); chan_init(&self_p->chout, chan_read_null, (ssize_t (*)(void *, const void *, size_t))uart_soft_write_cb, chan_size_null); pin_init(&self_p->tx_pin, tx_dev_p, PIN_OUTPUT); pin_init(&self_p->rx_pin, rx_dev_p, PIN_INPUT); /* Keep TX line high when no transmission is ongoing. */ pin_write(&self_p->tx_pin, 1); exti_init(&self_p->rx_exti, rx_exti_dev_p, EXTI_TRIGGER_FALLING_EDGE, rx_isr, self_p); exti_start(&self_p->rx_exti); return (queue_init(&self_p->chin, rxbuf_p, size)); }
static void init(void) { long number; struct fat16_dir_t dir; struct fat16_dir_entry_t entry; struct song_t *song_p; uint32_t seconds; fat16_read_t read; fat16_write_t write; void * arg_p; sys_start(); uart_module_init(); uart_init(&uart, &uart_device[0], 38400, qinbuf, sizeof(qinbuf)); uart_start(&uart); sys_set_stdout(&uart.chout); std_printf(sys_get_info()); fs_command_init(&cmd_list, FSTR("/list"), cmd_list_cb, NULL); fs_command_register(&cmd_list); fs_command_init(&cmd_play, FSTR("/play"), cmd_play_cb, NULL); fs_command_register(&cmd_play); fs_command_init(&cmd_pause, FSTR("/pause"), cmd_pause_cb, NULL); fs_command_register(&cmd_pause); fs_command_init(&cmd_next, FSTR("/next"), cmd_next_cb, NULL); fs_command_register(&cmd_next); fs_command_init(&cmd_prev, FSTR("/prev"), cmd_prev_cb, NULL); fs_command_register(&cmd_prev); fs_command_init(&cmd_stop, FSTR("/stop"), cmd_stop_cb, NULL); fs_command_register(&cmd_stop); fs_command_init(&cmd_repeat, FSTR("/repeat"), cmd_repeat_cb, NULL); fs_command_register(&cmd_repeat); fs_command_init(&cmd_set_bits_per_sample, FSTR("/set_bits_per_sample"), cmd_set_bits_per_sample_cb, NULL); fs_command_register(&cmd_set_bits_per_sample); if (storage_init(&read, &write, &arg_p) != 0) { std_printf(FSTR("storage init failed\r\n")); return; } std_printf(FSTR("initializing fat16\r\n")); fat16_init(&fs, read, write, arg_p, 0); std_printf(FSTR("fat16 initialized\r\n")); if (fat16_mount(&fs) != 0) { std_printf(FSTR("failed to mount fat16\r\n")); return; } std_printf(FSTR("fat16 mounted\r\n")); event_init(&event); exti_module_init(); /* Initialize the buttons. */ exti_init(&buttons[0], &exti_d18_dev, EXTI_TRIGGER_FALLING_EDGE, on_button_play, NULL); exti_start(&buttons[0]); exti_init(&buttons[1], &exti_d19_dev, EXTI_TRIGGER_FALLING_EDGE, on_button_prev, NULL); exti_start(&buttons[1]); exti_init(&buttons[2], &exti_d20_dev, EXTI_TRIGGER_FALLING_EDGE, on_button_next, NULL); exti_start(&buttons[2]); exti_init(&buttons[3], &exti_d21_dev, EXTI_TRIGGER_FALLING_EDGE, on_button_stop, NULL); exti_start(&buttons[3]); dac_init(&dac, &dac_0_dev, &pin_dac0_dev, &pin_dac1_dev, 2 * SAMPLES_PER_SOCOND); hash_map_init(&song_map, buckets, membersof(buckets), entries, membersof(entries), hash_number); sem_init(&sem, 0, 1); music_player_init(&music_player, &fs, &dac, get_current_song_path, get_next_song_path, NULL); /* Initialize the song number. */ current_song = FIRST_SONG_NUMBER; number = FIRST_SONG_NUMBER; /* Add songs to the hash map. */ fat16_dir_open(&fs, &dir, ".", O_READ); while (fat16_dir_read(&dir, &entry) == 1) { if (number - FIRST_SONG_NUMBER == membersof(songs)) { std_printf("Maximum number of songs already added. " "Skipping the rest of the songs.\r\n"); break; } /* Skip folders. */ if (entry.is_dir == 1) { continue; } song_p = &songs[number - FIRST_SONG_NUMBER]; /* Initialize the song entry. */ song_p->number = number; strcpy(song_p->name, entry.name); seconds = (entry.size / 4 / SAMPLES_PER_SOCOND); song_p->minutes = (seconds / 60); song_p->seconds = (seconds % 60); std_printf("Adding song %s to playlist.\r\n", entry.name); hash_map_add(&song_map, number, song_p); number++; } fat16_dir_close(&dir); last_song_number = (number - 1); music_player_start(&music_player); }
int main(void) { // TODO disable JTAG // update the SystemCoreClock variable SystemCoreClockUpdate(); // set interrupt priority config to use all 4 bits for pre-empting NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); // enable the CCM RAM and the GPIO's RCC->AHB1ENR |= RCC_AHB1ENR_CCMDATARAMEN | RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN; #if MICROPY_HW_HAS_SDCARD { // configure SDIO pins to be high to start with (apparently makes it more robust) // FIXME this is not making them high, it just makes them outputs... GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure); // Configure PD.02 CMD line GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOD, &GPIO_InitStructure); } #endif #if defined(NETDUINO_PLUS_2) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; #if MICROPY_HW_HAS_SDCARD // Turn on the power enable for the sdcard (PB1) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET); #endif // Turn on the power for the 5V on the expansion header (PB2) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET); } #endif // basic sub-system init sys_tick_init(); pendsv_init(); led_init(); #if MICROPY_HW_ENABLE_RTC rtc_init(); #endif // turn on LED to indicate bootup led_state(PYB_LED_G1, 1); // more sub-system init #if MICROPY_HW_HAS_SDCARD sdcard_init(); #endif storage_init(); // uncomment these 2 lines if you want REPL on USART_6 (or another usart) as well as on USB VCP //pyb_usart_global_debug = PYB_USART_YA; //usart_init(pyb_usart_global_debug, 115200); int first_soft_reset = true; soft_reset: // GC init gc_init(&_heap_start, &_heap_end); // Micro Python init qstr_init(); mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib)); mp_obj_list_init(mp_sys_argv, 0); exti_init(); #if MICROPY_HW_HAS_SWITCH switch_init(); #endif #if MICROPY_HW_HAS_LCD // LCD init (just creates class, init hardware by calling LCD()) lcd_init(); #endif #if MICROPY_HW_ENABLE_SERVO // servo servo_init(); #endif #if MICROPY_HW_ENABLE_TIMER // timer timer_init(); #endif #if MICROPY_HW_ENABLE_RNG // RNG RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE); RNG_Cmd(ENABLE); #endif pin_map_init(); // add some functions to the builtin Python namespace mp_store_name(MP_QSTR_help, mp_make_function_n(0, pyb_help)); mp_store_name(MP_QSTR_open, mp_make_function_n(2, pyb_io_open)); // load the pyb module mp_module_register(MP_QSTR_pyb, (mp_obj_t)&pyb_module); // check if user switch held (initiates reset of filesystem) bool reset_filesystem = false; #if MICROPY_HW_HAS_SWITCH if (switch_get()) { reset_filesystem = true; for (int i = 0; i < 50; i++) { if (!switch_get()) { reset_filesystem = false; break; } sys_tick_delay_ms(10); } } #endif // local filesystem init { // try to mount the flash FRESULT res = f_mount(&fatfs0, "0:", 1); if (!reset_filesystem && res == FR_OK) { // mount sucessful } else if (reset_filesystem || res == FR_NO_FILESYSTEM) { // no filesystem, so create a fresh one // TODO doesn't seem to work correctly when reset_filesystem is true... // LED on to indicate creation of LFS led_state(PYB_LED_R2, 1); uint32_t stc = sys_tick_counter; res = f_mkfs("0:", 0, 0); if (res == FR_OK) { // success creating fresh LFS } else { __fatal_error("could not create LFS"); } // create src directory res = f_mkdir("0:/src"); // ignore result from mkdir // create empty main.py FIL fp; f_open(&fp, "0:/src/main.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); // keep LED on for at least 200ms sys_tick_wait_at_least(stc, 200); led_state(PYB_LED_R2, 0); } else { __fatal_error("could not access LFS"); } } // make sure we have a /boot.py { FILINFO fno; FRESULT res = f_stat("0:/boot.py", &fno); if (res == FR_OK) { if (fno.fattrib & AM_DIR) { // exists as a directory // TODO handle this case // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation } else { // exists as a file, good! } } else { // doesn't exist, create fresh file // LED on to indicate creation of boot.py led_state(PYB_LED_R2, 1); uint32_t stc = sys_tick_counter; FIL fp; f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); // keep LED on for at least 200ms sys_tick_wait_at_least(stc, 200); led_state(PYB_LED_R2, 0); } } // run /boot.py if (!pyexec_file("0:/boot.py")) { flash_error(4); } if (first_soft_reset) { #if MICROPY_HW_HAS_MMA7660 // MMA accel: init and reset address to zero accel_init(); #endif } // turn boot-up LED off led_state(PYB_LED_G1, 0); #if MICROPY_HW_HAS_SDCARD // if an SD card is present then mount it on 1:/ if (sdcard_is_present()) { FRESULT res = f_mount(&fatfs1, "1:", 1); if (res != FR_OK) { printf("[SD] could not mount SD card\n"); } else { if (first_soft_reset) { // use SD card as medium for the USB MSD usbd_storage_select_medium(USBD_STORAGE_MEDIUM_SDCARD); } } } #endif #ifdef USE_HOST_MODE // USB host pyb_usb_host_init(); #elif defined(USE_DEVICE_MODE) // USB device pyb_usb_dev_init(PYB_USB_DEV_VCP_MSC); #endif // run main script { vstr_t *vstr = vstr_new(); vstr_add_str(vstr, "0:/"); if (pyb_config_source_dir == MP_OBJ_NULL) { vstr_add_str(vstr, "src"); } else { vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_source_dir)); } vstr_add_char(vstr, '/'); if (pyb_config_main == MP_OBJ_NULL) { vstr_add_str(vstr, "main.py"); } else { vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main)); } if (!pyexec_file(vstr_str(vstr))) { flash_error(3); } vstr_free(vstr); } #if MICROPY_HW_HAS_MMA7660 // HID example if (0) { uint8_t data[4]; data[0] = 0; data[1] = 1; data[2] = -2; data[3] = 0; for (;;) { #if MICROPY_HW_HAS_SWITCH if (switch_get()) { data[0] = 0x01; // 0x04 is middle, 0x02 is right } else { data[0] = 0x00; } #else data[0] = 0x00; #endif accel_start(0x4c /* ACCEL_ADDR */, 1); accel_send_byte(0); accel_restart(0x4c /* ACCEL_ADDR */, 0); for (int i = 0; i <= 1; i++) { int v = accel_read_ack() & 0x3f; if (v & 0x20) { v |= ~0x1f; } data[1 + i] = v; } accel_read_nack(); usb_hid_send_report(data); sys_tick_delay_ms(15); } } #endif #if MICROPY_HW_HAS_WLAN // wifi pyb_wlan_init(); pyb_wlan_start(); #endif pyexec_repl(); printf("PYB: sync filesystems\n"); storage_flush(); printf("PYB: soft reboot\n"); first_soft_reset = false; goto soft_reset; }
void main() { uart_init(UART3, 115200); // For our flashed bluetooth //uart_init(UART3, 9600); // For our non-flashed bluetooth gpio_init(PORTE,6,GPI,0); // SW2 goes into gyro calibration mode printf("\nWelcome to the SmartCar 2013 Sensor team developement system\n"); while(1){ printf("==============================================================\n"); printf("Please select mode:\n---------------------------\n"); printf("1:Accelerometer&gyro\n"); printf("2:LinearCCD\n"); printf("3:Flash Memory\n"); printf("4:Encoder testing\n"); printf("6:Motor control test\n"); printf("7:SystemLoop Test\n"); delayms(300); if(gpio_get(PORTE,6)==0){ adc_init(ADC1,AD5b); adc_init(ADC1,AD7b); printf("We are now in gyro calibration mode, Please keep car stationery and wait till light dies"); gpio_init(PORTE,24,GPO,0); gpio_init(PORTE,25,GPO,0); gpio_init(PORTE,26,GPO,0); gpio_init(PORTE,27,GPO,0); delayms(3000); turn_gyro_offset=ad_ave(ADC1,AD5b,ADC_12bit,1000); balance_gyro_offset=ad_ave(ADC1,AD7b,ADC_12bit,1000); store_u32_to_flashmem1(turn_gyro_offset); store_u32_to_flashmem2(balance_gyro_offset); gpio_turn(PORTE,24); gpio_turn(PORTE,25); gpio_turn(PORTE,26); gpio_turn(PORTE,27); while(1){} } g_char_mode = '7'; // Hard code mode = system loop //g_char_mode = uart_getchar(UART3); switch (g_char_mode){ case '0': //VR analog input adc_init(ADC0,AD14); while(1){ delayms(500); printf("\n%d",ad_once(ADC0,AD14,ADC_16bit));//vr value } break; case '1': uart_sendStr(UART3,"The mode now is 1: Accelerometer and Gyroscope Test"); //accl_init(); adc_init(ADC1,AD6b); adc_init(ADC1,AD7b); adc_init(ADC0,AD14); adc_init(ADC1,AD4b); balance_centerpoint_set=ad_ave(ADC0,AD14,ADC_12bit,10); printf("\nEverything Initialized alright\n"); while(1) { //printf("\n\f===================================="); //control_tilt=(ad_ave(ADC1,AD6b,ADC_12bit,8)-3300)+(balance_centerpoint_set/10); //printf("\nMain gyro%d",control_tilt);//theta //printf("\n%d",ad_once(ADC1,AD7b,ADC_12bit)-1940);//omega printf("\n%d",ad_ave(ADC1,AD6b,ADC_12bit,8)-940); delayms(50); } break; case '2': uart_sendStr(UART3,"The mode now is 2: Linear CCD"); ccd_interrupts_init(); printf("\nEverything Initialized alright\n"); while(1) { //ccd_sampling(1); // Longer SI CCD Sampling } break; case '3': uart_sendStr(UART3,"The mode now is 3:Flash Memory\n"); Flash_init(); printf("Flash Memory init ok\n"); printf("writing the u32 : 3125 to memory 1\n"); store_u32_to_flashmem1(3125); printf("writing the u32: 1019 to memory 2\n"); store_u32_to_flashmem2(1019); printf("Now reading from memory\n"); printf("Memory 1:%d\n",get_u32_from_flashmem1()); printf("Memory 2:%d\n",get_u32_from_flashmem2()); while(1) { //stops loop to see results } break; case '4': //temporary case for debuggine encoder libraries, move to encoder.h later uart_sendStr(UART3,"The mode now is 4: encoder test"); DisableInterrupts; exti_init(PORTA,6,rising_up); //inits left encoder interrupt capture exti_init(PORTA,7,rising_up); //inits right encoder interrupt capture pit_init_ms(PIT1,500); //periodic interrupt every 500ms EnableInterrupts; printf("\nEverything Initialized alright\n"); while(1){ } break; case '6': uart_sendStr(UART3,"The mode now is 6: Motor Control test"); //inits motor_init(); printf("\nEverything Initialized alright\n"); delayms(1000); while(1) { //printf("\n\fInput 0-9 Motor Speed, Currently:%d",motor_test); //motor_test=100*(uart_getchar(UART3)-48); FTM_PWM_Duty(FTM1,CH0,2000); FTM_PWM_Duty(FTM1,CH1,2000);//left printf("\n\f Input direction : 0 or 1"); motor_test = uart_getchar(UART3)-48; if (motor_test){ gpio_set(PORTD,9,1); gpio_set(PORTD,7,1);//this is DIR }else{ gpio_set(PORTD,9,0); gpio_set(PORTD,1,0);//this is DIR } } break; case '7': printf("\n The Mode is now 7: SystemLoop Test"); adc_init(ADC1,AD6b); adc_init(ADC1,AD7b); adc_init(ADC0,AD14); adc_init(ADC1,AD5b); balance_centerpoint_set=ad_ave(ADC0,AD14,ADC_12bit,10); motor_init(); gpio_set(PORTD,9,0); //dir gpio_set(PORTD,7,0); //dir FTM_PWM_Duty(FTM1, CH0, 3000); //initital speed FTM_PWM_Duty(FTM1, CH1, 3000); //initital speed ccd_interrupts_init(); pit_init_ms(PIT3,1); printf("\nEverything inited alright"); while(1){ //system loop runs } break; default : printf("\n\fYou entered:%c, Please enter a number from 1-7 to select a mode\n\f",g_char_mode); } } }
rt_err_t gd32_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled) { const struct pin_index *index; const struct pin_irq_map *irqmap; rt_base_t level; rt_int32_t hdr_index = -1; exti_trig_type_enum trigger_mode; index = get_pin(pin); if (index == RT_NULL) { return RT_EINVAL; } if (enabled == PIN_IRQ_ENABLE) { hdr_index = bit2bitno(index->pin); if (hdr_index < 0 || hdr_index >= ITEM_NUM(pin_irq_map)) { return RT_EINVAL; } level = rt_hw_interrupt_disable(); if (pin_irq_hdr_tab[hdr_index].pin == -1) { rt_hw_interrupt_enable(level); return RT_EINVAL; } irqmap = &pin_irq_map[hdr_index]; switch (pin_irq_hdr_tab[hdr_index].mode) { case PIN_IRQ_MODE_RISING: trigger_mode = EXTI_TRIG_RISING; break; case PIN_IRQ_MODE_FALLING: trigger_mode = EXTI_TRIG_FALLING; break; case PIN_IRQ_MODE_RISING_FALLING: trigger_mode = EXTI_TRIG_BOTH; break; default: rt_hw_interrupt_enable(level); return RT_EINVAL; } //rcu_periph_clock_enable(RCU_AF); /* enable and set interrupt priority */ nvic_irq_enable(irqmap->irqno, 5U); /* connect EXTI line to GPIO pin */ syscfg_exti_line_config(index->port_src, index->pin_src); /* configure EXTI line */ exti_init((exti_line_enum)(index->pin), EXTI_INTERRUPT, trigger_mode); exti_interrupt_flag_clear((exti_line_enum)(index->pin)); rt_hw_interrupt_enable(level); } else if (enabled == PIN_IRQ_DISABLE) { irqmap = get_pin_irq_map(index->pin); if (irqmap == RT_NULL) { return RT_EINVAL; } nvic_irq_disable(irqmap->irqno); } else { return RT_EINVAL; } return RT_EOK; }
int main(void) { // TODO disable JTAG /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); // set the system clock to be HSE SystemClock_Config(); // enable GPIO clocks __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); // enable the CCM RAM __CCMDATARAMEN_CLK_ENABLE(); #if 0 #if defined(NETDUINO_PLUS_2) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; #if MICROPY_HW_HAS_SDCARD // Turn on the power enable for the sdcard (PB1) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET); #endif // Turn on the power for the 5V on the expansion header (PB2) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET); } #endif #endif // basic sub-system init pendsv_init(); timer_tim3_init(); led_init(); switch_init0(); int first_soft_reset = true; soft_reset: // check if user switch held to select the reset mode led_state(1, 0); led_state(2, 1); led_state(3, 0); led_state(4, 0); uint reset_mode = 1; #if MICROPY_HW_HAS_SWITCH if (switch_get()) { for (uint i = 0; i < 3000; i++) { if (!switch_get()) { break; } HAL_Delay(20); if (i % 30 == 29) { if (++reset_mode > 3) { reset_mode = 1; } led_state(2, reset_mode & 1); led_state(3, reset_mode & 2); led_state(4, reset_mode & 4); } } // flash the selected reset mode for (uint i = 0; i < 6; i++) { led_state(2, 0); led_state(3, 0); led_state(4, 0); HAL_Delay(50); led_state(2, reset_mode & 1); led_state(3, reset_mode & 2); led_state(4, reset_mode & 4); HAL_Delay(50); } HAL_Delay(400); } #endif #if MICROPY_HW_ENABLE_RTC if (first_soft_reset) { rtc_init(); } #endif // more sub-system init #if MICROPY_HW_HAS_SDCARD if (first_soft_reset) { sdcard_init(); } #endif if (first_soft_reset) { storage_init(); } // GC init gc_init(&_heap_start, &_heap_end); // Change #if 0 to #if 1 if you want REPL on USART_6 (or another usart) // as well as on USB VCP #if 0 pyb_usart_global_debug = pyb_Usart(MP_OBJ_NEW_SMALL_INT(PYB_USART_YA), MP_OBJ_NEW_SMALL_INT(115200)); #else pyb_usart_global_debug = NULL; #endif // Micro Python init qstr_init(); mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib)); mp_obj_list_init(mp_sys_argv, 0); readline_init(); exti_init(); #if MICROPY_HW_HAS_SWITCH // must come after exti_init switch_init(); #endif #if MICROPY_HW_HAS_LCD // LCD init (just creates class, init hardware by calling LCD()) lcd_init(); #endif pin_map_init(); // local filesystem init { // try to mount the flash FRESULT res = f_mount(&fatfs0, "0:", 1); if (reset_mode == 3 || res == FR_NO_FILESYSTEM) { // no filesystem, or asked to reset it, so create a fresh one // LED on to indicate creation of LFS led_state(PYB_LED_R2, 1); uint32_t start_tick = HAL_GetTick(); res = f_mkfs("0:", 0, 0); if (res == FR_OK) { // success creating fresh LFS } else { __fatal_error("could not create LFS"); } // create empty main.py FIL fp; f_open(&fp, "0:/main.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); // create .inf driver file f_open(&fp, "0:/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n); f_close(&fp); // keep LED on for at least 200ms sys_tick_wait_at_least(start_tick, 200); led_state(PYB_LED_R2, 0); } else if (res == FR_OK) { // mount sucessful } else { __fatal_error("could not access LFS"); } } // make sure we have a 0:/boot.py { FILINFO fno; #if _USE_LFN fno.lfname = NULL; fno.lfsize = 0; #endif FRESULT res = f_stat("0:/boot.py", &fno); if (res == FR_OK) { if (fno.fattrib & AM_DIR) { // exists as a directory // TODO handle this case // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation } else { // exists as a file, good! } } else { // doesn't exist, create fresh file // LED on to indicate creation of boot.py led_state(PYB_LED_R2, 1); uint32_t start_tick = HAL_GetTick(); FIL fp; f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); // keep LED on for at least 200ms sys_tick_wait_at_least(start_tick, 200); led_state(PYB_LED_R2, 0); } } // root device defaults to internal flash filesystem uint root_device = 0; #if defined(USE_DEVICE_MODE) usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH; #endif #if MICROPY_HW_HAS_SDCARD // if an SD card is present then mount it on 1:/ if (reset_mode == 1 && sdcard_is_present()) { FRESULT res = f_mount(&fatfs1, "1:", 1); if (res != FR_OK) { printf("[SD] could not mount SD card\n"); } else { // use SD card as root device root_device = 1; if (first_soft_reset) { // use SD card as medium for the USB MSD #if defined(USE_DEVICE_MODE) usb_medium = USB_STORAGE_MEDIUM_SDCARD; #endif } } } #else // Get rid of compiler warning if no SDCARD is configured. (void)first_soft_reset; #endif // run <root>:/boot.py, if it exists if (reset_mode == 1) { const char *boot_file; if (root_device == 0) { boot_file = "0:/boot.py"; } else { boot_file = "1:/boot.py"; } FRESULT res = f_stat(boot_file, NULL); if (res == FR_OK) { if (!pyexec_file(boot_file)) { flash_error(4); } } } // turn boot-up LEDs off led_state(2, 0); led_state(3, 0); led_state(4, 0); #if defined(USE_HOST_MODE) // USB host pyb_usb_host_init(); #elif defined(USE_DEVICE_MODE) // USB device if (reset_mode == 1) { usb_device_mode_t usb_mode = USB_DEVICE_MODE_CDC_MSC; if (pyb_config_usb_mode != MP_OBJ_NULL) { if (strcmp(mp_obj_str_get_str(pyb_config_usb_mode), "CDC+HID") == 0) { usb_mode = USB_DEVICE_MODE_CDC_HID; } } pyb_usb_dev_init(usb_mode, usb_medium); } else { pyb_usb_dev_init(USB_DEVICE_MODE_CDC_MSC, usb_medium); } #endif #if MICROPY_HW_ENABLE_RNG // RNG rng_init(); #endif #if MICROPY_HW_ENABLE_TIMER // timer //timer_init(); #endif // I2C i2c_init(); #if MICROPY_HW_HAS_MMA7660 // MMA accel: init and reset accel_init(); #endif #if MICROPY_HW_ENABLE_SERVO // servo servo_init(); #endif #if MICROPY_HW_ENABLE_DAC // DAC dac_init(); #endif // now that everything is initialised, run main script if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { vstr_t *vstr = vstr_new(); vstr_printf(vstr, "%d:/", root_device); if (pyb_config_main == MP_OBJ_NULL) { vstr_add_str(vstr, "main.py"); } else { vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main)); } FRESULT res = f_stat(vstr_str(vstr), NULL); if (res == FR_OK) { if (!pyexec_file(vstr_str(vstr))) { flash_error(3); } } vstr_free(vstr); } #if 0 #if MICROPY_HW_HAS_WLAN // wifi pyb_wlan_init(); pyb_wlan_start(); #endif #endif // enter REPL // REPL mode can change, or it can request a soft reset for (;;) { if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { if (pyexec_raw_repl() != 0) { break; } } else { if (pyexec_friendly_repl() != 0) { break; } } } printf("PYB: sync filesystems\n"); storage_flush(); printf("PYB: soft reboot\n"); first_soft_reset = false; goto soft_reset; }
void main() { int i=0,j=0,k=0; DisableInterrupts; //禁止总中断 V_Cnt=0; //行计数 Is_SendPhoto=0; //从串口发送图像 uart_init(UART4, 9600); adc_init(ADC0, SE12); adc_init(ADC0, SE13); adc_init(ADC0, SE17); FTM1_QUAD_Iint(); FTM2_QUAD_Iint(); FTM_PWM_init(FTM0, CH0, 8000, 0); //PTC1 1000Hz FTM_PWM_init(FTM0, CH1, 8000, 0); //PTC2 1000Hz FTM_PWM_init(FTM0, CH2, 8000, 0); //PTC3 1000Hz FTM_PWM_init(FTM0, CH3, 8000, 0); //PTC4 1000Hz gpio_init (PORTA, 7, 0, 0); gpio_init (PORTA, 8, 0, 0); gpio_init (PORTE, 28, 0, 0); pit_init_ms(PIT0, 1); delayms(100); for(k=0;k<20;k++) { gyro_offset+=ad_once(ADC0, SE12, ADC_12bit); gyro_DIR+=ad_once(ADC0, SE17, ADC_12bit); } gyro_offset=gyro_offset/20; gyro_DIR=gyro_DIR/20; delayms(1500); /*********************************************************************************/ exti_init(PORTA,9,rising_down); //行中断,PORTA29 端口外部中断初始化 ,上升沿触发中断,内部下拉 disable_irq(87); //行中断关闭 exti_init(PORTB,4,falling_down); //场中断,PORTB0 端口外部中断初始化 ,下降沿触发中断,内部下拉 /*************************************************************************************/ // pll_init(PLL50); // LCD_Init(); EnableInterrupts; //LCD_clear(); while(1) { enableisstartline++; // if(podao==1) podaocount++; if(enableisstartline<1100) IsStartLine=0; else { enableisstartline=1100; CheckStartLine(); Stop2(); } Stop(); // OutData[0]=Gyro_DIRR;//k1;//SubBasePoint; OutData[1]= boma1; OutData[2]=boma2; OutData[3]=boma3; OutPut_Data(); } }