📄 hardware.c
字号:
/* } *//* } *//* } */void hardware_init(void){ io_port=controllers[type_micro].io_registers; pin=controllers[type_micro].pins; flag=controllers[type_micro].flags; prescaler_value0=prescaler_value1=prescaler_value2=prescaler_value3=0;}void hardware(void){ /* load_ports(); */ prescaler0(); timer_counter0(); prescaler1(); timer_counter1(); prescaler2(); timer_counter2(); prescaler3(); timer_counter3();/* eeprom_access(); */ analog_comparator(); /* external_interrupts(); *//* save_ports(); *//* watchdog_timer(); */}/* function to detecting interrupt*/int reset_int(int addr){ return -1;}int int0_int(int addr){ if(!ISFLAG(INT0_FLAG)) return -1; if(get_flag(INT0_FLAG)) { if(ISFLAG(INTF0_FLAG)) { if(get_flag(INTF0_FLAG)) { clear_flag(INTF0_FLAG); return addr; } return -1; } if(!ISFLAG(ISC00_FLAG)) /* interrupt when pin is low-level, mega103 */ { if(!get_pin(INT0_PIN)) return addr; return -1; } if(ex_ifr&(1<<0)) { ex_ifr&=~(1<<0); return addr; } } return -1;}int int1_int(int addr){ if(!ISFLAG(INT1_FLAG)) return -1; if(get_flag(INT1_FLAG)) { if(ISFLAG(INTF1_FLAG)) { if(get_flag(INTF1_FLAG)) { clear_flag(INTF1_FLAG); return addr; } return -1; } if(!ISFLAG(ISC10_FLAG)) /* interrupt when pin is low-level, mega103 */ { if(!get_pin(INT1_PIN)) return addr; return -1; } if(ex_ifr&(1<<1)) { ex_ifr&=~(1<<1); return addr; } } return -1;}int int2_int(int addr){ if(!ISFLAG(INT2_FLAG)) return -1; if(get_flag(INT2_FLAG)) { if(ISFLAG(INTF2_FLAG)) { if(get_flag(INTF2_FLAG)) { clear_flag(INTF2_FLAG); return addr; } return -1; } if(!ISFLAG(ISC20_FLAG)) /* interrupt when pin is low-level, mega103 */ { if(!get_pin(INT2_PIN)) return addr; return -1; } } return -1;}int int3_int(int addr){ if(!ISFLAG(INT3_FLAG)) return -1; if(get_flag(INT3_FLAG)) { if(ISFLAG(INTF3_FLAG)) { if(get_flag(INTF3_FLAG)) { clear_flag(INTF3_FLAG); return addr; } return -1; } if(!ISFLAG(ISC30_FLAG)) /* interrupt when pin is low-level, mega103 */ { if(!get_pin(INT3_PIN)) return addr; return -1; } } return -1;}int int4_int(int addr){ if(check_mask_and_flag(INT4_FLAG,INTF4_FLAG)) return addr; return -1;}int int5_int(int addr){ if(check_mask_and_flag(INT5_FLAG,INTF5_FLAG)) return addr; return -1;}int int6_int(int addr){ if(check_mask_and_flag(INT6_FLAG,INTF6_FLAG)) return addr; return -1;}int int7_int(int addr){ if(check_mask_and_flag(INT7_FLAG,INTF7_FLAG)) return addr; return -1;}int pcint0_int(){ return -1;}int pcint1_int(){ return -1;}int timer3_capt_int(int addr){ if(check_mask_and_flag(TICIE3_FLAG,ICF3_FLAG)) return addr; return -1;}int timer3_compa_int(int addr){ if(check_mask_and_flag(OCIE3A_FLAG,OCF3A_FLAG)) return addr; return -1;}int timer3_compb_int(int addr){ if(check_mask_and_flag(OCIE3B_FLAG,OCF3B_FLAG)) return addr; return -1;}int timer3_compc_int(int addr){ if(check_mask_and_flag(OCIE3C_FLAG,OCF3C_FLAG)) return addr; return -1;}int timer3_ovf_int(int addr){ if(check_mask_and_flag(TOIE3_FLAG,TOV3_FLAG)) return addr; return -1;}int timer2_comp_int(int addr){ if(check_mask_and_flag(OCIE2_FLAG,OCF2_FLAG)) return addr; return -1;}int timer2_ovf_int(int addr){ if(check_mask_and_flag(TOIE2_FLAG,TOV2_FLAG)) return addr; return -1;}int timer1_capt_int(int addr){ if(check_mask_and_flag(TICIE1_FLAG,ICF1_FLAG)) return addr; return -1;}int timer1_compa_int(int addr){ if(check_mask_and_flag(OCIE1A_FLAG,OCF1A_FLAG)) return addr; return -1;}int timer1_compb_int(int addr){ if(check_mask_and_flag(OCIE1B_FLAG,OCF1B_FLAG)) return addr; return -1;}int timer1_compc_int(int addr){ if(check_mask_and_flag(OCIE1C_FLAG,OCF1C_FLAG)) return addr; return -1;}int timer1_ovf_int(int addr){ if(check_mask_and_flag(TOIE1_FLAG,TOV1_FLAG)) return addr; return -1;}int timer0_comp_int(int addr){ if(check_mask_and_flag(OCIE0_FLAG,OCF0_FLAG)) return addr; return -1;}int timer0_ovf_int(int addr){ if(check_mask_and_flag(TOIE0_FLAG,TOV0_FLAG)) return addr; return -1;}int spi_stc_int(int addr){ if(check_mask_and_flag(SPIE_FLAG,SPIF_FLAG)) return addr; return -1;}int uart0_rx_int(int addr){ if(!ISFLAG(RXCIE0_FLAG)) return -1; if(get_flag(RXCIE0_FLAG)&&get_flag(RXC0_FLAG)) return addr; return -1;}int uart0_udre_int(int addr){ if(!ISFLAG(UDRIE0_FLAG)) return -1; if(get_flag(UDRIE0_FLAG)&&get_flag(UDRE0_FLAG)) return addr; return -1;}int uart0_tx_int(int addr){ if(check_mask_and_flag(TXCIE0_FLAG,TXC0_FLAG)) return addr; return -1;}int uart1_rx_int(int addr){ if(!ISFLAG(RXCIE1_FLAG)) return -1; if(get_flag(RXCIE1_FLAG)&&get_flag(RXC1_FLAG)) return addr; return -1;}int uart1_udre_int(int addr){ if(!ISFLAG(UDRIE1_FLAG)) return -1; if(get_flag(UDRIE1_FLAG)&&get_flag(UDRE1_FLAG)) return addr; return -1;}int uart1_tx_int(int addr){ if(check_mask_and_flag(TXCIE1_FLAG,TXC1_FLAG)) return addr; return -1;}int adc_int(int addr){ if(check_mask_and_flag(ADIE_FLAG,ADIF_FLAG)) return addr; return -1;}int ee_ready_int(int addr){ if(!ISFLAG(EERIE_FLAG)) return -1; if(get_flag(EERIE_FLAG)&&get_flag(EEWE_FLAG)==0) return addr; return -1;}int analog_comp_int(int addr){ if(check_mask_and_flag(ACIE_FLAG,ACI_FLAG)) return addr; return -1;}int twsi_int(int addr){ if(!ISFLAG(TWIE_FLAG)) return -1; if(get_flag(TWIE_FLAG)&&get_flag(TWINT_FLAG)) return addr; return -1;}int spm_rdy_int(int addr){ return -1;}void set_flag(int f){ int index=index_flags[f]; int port; if(index==-1) {fprintf(stderr,"Error: flag not found. Flag: %d\n",f);return;} port=io_port[index_ports[flag[index].port_code]].address; io_pointer[port]|=flag[index].flag_mask; port+=controllers[type_micro].size_gpr; mem_file_changed[port/8]|=1<<port%8;}void clear_flag(int f){ int index=index_flags[f]; int port; port=io_port[index_ports[flag[index].port_code]].address; if(index==-1) {fprintf(stderr,"Error: flag not found. Flag: %d\n",f);return;} io_pointer[port]&=~flag[index].flag_mask; port+=controllers[type_micro].size_gpr; mem_file_changed[port/8]|=1<<port%8;}int get_flag(int f){ int index=index_flags[f]; if(index==-1) {fprintf(stderr,"Error: flag not found. Flag: %d\n",f);return -1;} return io_pointer[io_port[index_ports[flag[index].port_code]].address]&flag[index].flag_mask;}int check_mask_and_flag(int m,int f){ if(!ISFLAG(m)) return 0; if(get_flag(m)&&get_flag(f)) { clear_flag(f); return 1; } return 0;}int get_pin(int p){ int index=index_pins[p]; if(index==-1){fprintf(stderr,"Error: pin not found. Pin: %d\n",p);return -1;} return io_pointer[pin[index].pins_address]&pin[index].mask;}void set_pin(int p){ int index=index_pins[p]; int port; if(index==-1) {fprintf(stderr,"Error: pin not found. Pin: %d\n",p);return;} port=pin[index].data_address; io_pointer[port]|=pin[index].mask; port+=controllers[type_micro].size_gpr; mem_file_changed[port/8]|=1<<port%8;}void clear_pin(int p){ int index=index_pins[p]; int port; if(index==-1) {fprintf(stderr,"Error: pin not found. Pin: %d\n",p);return;} port=pin[index].data_address; io_pointer[port]&=~pin[index].mask; port+=controllers[type_micro].size_gpr; mem_file_changed[port/8]|=1<<port%8;}uint8_t get_port(int port){ int index=index_ports[port]; if(index==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[port].name); return 0; } return io_pointer[io_port[index].address];}uint8_t add_port(int p,uint8_t add){ int port=index_ports[p]; uint8_t result; if(port==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[port].name); return 0; } port=io_port[port].address; result=(io_pointer[port]+=add); port+=controllers[type_micro].size_gpr; mem_file_changed[port/8]|=1<<port%8; return result;}void set_port(int p,uint8_t value){ int port=index_ports[p]; if(port==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[port].name); return; } port=io_port[port].address; io_pointer[port]=value; port+=controllers[type_micro].size_gpr; mem_file_changed[port/8]|=1<<port%8;}uint16_t get_port16(int low,int hi){ uint16_t result; int index_low=index_ports[low],index_hi=index_ports[hi]; if(index_low==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[low].name); return 0; } if(index_hi==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[hi].name); return 0; } result=io_pointer[io_port[index_low].address]+ (io_pointer[io_port[index_hi].address]<<8); return result;}void set_port16(int low,int hi,uint16_t value){ int port_low=index_ports[low],port_hi=index_ports[hi]; if(port_low==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[low].name); return; } if(port_hi==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[hi].name); return; } port_low=io_port[port_low].address; port_hi=io_port[port_hi].address; io_pointer[port_low]=(uint8_t)value; io_pointer[port_hi]=(uint8_t)(value>>8); port_low+=controllers[type_micro].size_gpr; mem_file_changed[port_low/8]|=1<<port_low%8; port_hi+=controllers[type_micro].size_gpr; mem_file_changed[port_hi/8]|=1<<port_hi%8;}uint16_t add_port16(int low,int hi,uint16_t value){ uint16_t result; int port_low=index_ports[low],port_hi=index_ports[hi]; if(port_low==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[low].name); return 0; } if(port_hi==-1) { fprintf(stderr,"Error: port not found. Port: %s\n", descr_io_ports[hi].name); return 0; } port_low=io_port[port_low].address; port_hi=io_port[port_hi].address; result=io_pointer[port_low]+(io_pointer[port_hi]<<8); result+=value; io_pointer[port_low]=(uint8_t)result; io_pointer[port_hi]=(uint8_t)(result>>8); port_low+=controllers[type_micro].size_gpr; mem_file_changed[port_low/8]|=1<<port_low%8; port_hi+=controllers[type_micro].size_gpr; mem_file_changed[port_hi/8]|=1<<port_hi%8; return result;}void set_sreg_flag(int f){ int port=io_port[index_ports[SREG]].address; io_pointer[port]|=f; port+=controllers[type_micro].size_gpr; mem_file_changed[port/8]|=1<<port%8;}void clear_sreg_flag(int f){ int port=io_port[index_ports[SREG]].address; io_pointer[port]&=~f; port+=controllers[type_micro].size_gpr; mem_file_changed[port/8]|=1<<port%8;}int get_sreg_flag(int f){ if(io_pointer[io_port[index_ports[SREG]].address]&f) return 1; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -