// debounce buttons (active low) static uint8_t debounce(volatile uint8_t *port, uint8_t pin) { //[[ // if button is pressed ... if ( ! (*port & (1 << pin)) ) { // ... wait some time ... my_delay_ms(100); // if button has been released if ( *port & (1 << pin) ) { my_delay_ms(50); return 1; } else { // if button is released within the next second ... for(int8_t i=0; i<10; i++) { my_delay_ms(100); if(*port & (1<<pin)) { return 1; } } // otherwise ... return 2; } } return 0; } //]]
// toggle LED n times (with pause between the toggles) static void led_blink(uint8_t n) { //[[ for(uint8_t i=0; i<n; i++) { LED_PORT &= ~(1<<LED1); my_delay_ms(50); LED_PORT |= (1<<LED1); my_delay_ms(150); } } //]]
int main (void) { unsigned char kommando[32]; // Kommandozeilenbuffer unsigned char* k_sp = kommando; // und dazugehöriger Pointer volatile struct buffer put_buffer, get_buffer; // Sende- u. Empfangsbuffer buffer_init(put_buffer); buffer_init(get_buffer); put_bp=&put_buffer; get_bp=&get_buffer; DDRD = 0xE0; // Port D, Pin 6,7,8 als Ausgang tim0_init(); // Initialisiere Timer_0 tim1_init(); // Initialisiere Timer_1 tim2_init(); // Initialisiere Timer_2 uart_init(); // Initialisiere serielle Schnittstelle adc_init(); // Initialisiere Analog-Digitalwandler ADC uart_puts(version); // Startmeldung/Version ausgeben beep(1000); uart_puts("\n> "); // newline und prompt UCSRB |= (1<<RXCIE); // Empfaenger-Interrupts freigeben sei(); // Interrupts global freigeben while(1) { while(buffer_size(get_bp)){ *k_sp = buffer_read(get_bp); uart_putc(*k_sp); if(*k_sp == '\n' || *k_sp == '\r'){ *k_sp = 0x00; k_sp=kommando; uart_putc('\n'); // uart_puts(k_sp); // Ausgabe für Testwecke parser(k_sp); } else k_sp++; // hier noch Bufferoverflow abfangen } if(status_LED2 == 's'){ PORTD ^= 1<<PD6; // toggle LED2 my_delay_ms(delay); // warte delay ms PORTD ^= 1<<PD6; // toggle LED2 status_LED2 = '0'; // Status_Flag auf Aus } if(status_LED2 == 't'){ my_delay_ms(delay); // warte delay ms PORTD ^= 1<<PD6; // toggle LED2 } } /* wird nie erreicht */ return 0; }
static void rf_router_send(uint8_t addAddr) { #ifdef RFR_DEBUG if(RFR_Buffer.buf[5] == 'T') nr_t++; else if(RFR_Buffer.buf[5] == 'F') nr_f++; else if(RFR_Buffer.buf[5] == 'E') nr_e++; else if(RFR_Buffer.buf[5] == 'K') nr_k++; else if(RFR_Buffer.buf[5] == 'H') nr_h++; else nr_r++; #endif uint8_t buf[7], l = 1; buf[0] = RF_ROUTER_PROTO_ID; if(addAddr) { tohex(rf_router_target, buf+1); tohex(rf_router_myid, buf+3), buf[5] = 'U'; l = 6; } rf_router_ping(); // 15ms ccInitChip(EE_FASTRF_CFG); // 1.6ms my_delay_ms(3); // 3ms: Found by trial and error CC1100_ASSERT; cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO); #ifdef RFR_USBECHO uint8_t nbuf = RFR_Buffer.nbytes; #endif cc1100_sendbyte(RFR_Buffer.nbytes+l); for(uint8_t i = 0; i < l; i++) cc1100_sendbyte(buf[i]); while(RFR_Buffer.nbytes) cc1100_sendbyte(rb_get(&RFR_Buffer)); CC1100_DEASSERT; ccTX(); rb_reset(&RFR_Buffer); // needed by FHT_compress // Wait for the data to be sent uint8_t maxwait = 20; // max 20ms while((cc1100_readReg(CC1100_TXBYTES) & 0x7f) && maxwait--) my_delay_ms(1); set_txrestore(); #ifdef RFR_USBECHO #warning RFR USB DEBUGGING IS ACTIVE uint8_t odc = display_channel; display_channel = DISPLAY_USB; DC('.'); DU(nbuf, 2); DNL(); display_channel = odc; #endif }
int mute(int duration) { OCR1A = 0; duration = duration * BEAT; my_delay_ms(duration); return 0; }
void moritz_sendAck(uint8_t* enc) { uint8_t ackPacket[12]; ackPacket[0] = 11; /* len*/ ackPacket[1] = enc[1]; /* msgcnt */ ackPacket[2] = 0; /* flag */ ackPacket[3] = 2; /* type = Ack */ for(int i=0;i<3;++i) /* src = enc_dst*/ ackPacket[4+i] = enc[7+i]; for(int i=0;i<3;++i) /* dst = enc_src */ ackPacket[7+i] = enc[4+i]; ackPacket[10] = 0; /* groupid */ ackPacket[11] = 0; /* payload */ my_delay_ms(20); /* by experiments */ moritz_sendraw(ackPacket, 0); //Inform FHEM that we send an autoack DC('Z'); for (uint8_t i=0; i < ackPacket[0]+1; i++) DH2( ackPacket[i] ); if (tx_report & REP_RSSI) DH2( 0 ); //fake some rssi DNL(); }
void lcd_switch(uint8_t hb) { if(hb) { LCD_BL_DDR |= _BV(LCD_BL_PIN); // Switch on, init #ifdef LCD_BL_PWM LCD_BL_PWM = erb((uint8_t*)EE_BRIGHTNESS); #else LCD_BL_PORT |= _BV(LCD_BL_PIN); #endif lcd_init(); lcd_on = 1; } else { #ifdef LCD_BL_PWM LCD_BL_PWM = 0; my_delay_ms(1); // else the dpy is still on. #endif LCD_BL_PORT &= ~_BV(LCD_BL_PIN); // Switch off the display lcd_sendcmd (LCD_CMD_SLEEPIN); lcd_on = 0; } }
// Signaltiöne ausgeben void beeps(uint16_t dauer, uint8_t anzahl){ uint8_t i; for(i=1;i<=anzahl;i++){ beep(dauer); my_delay_ms(dauer); } }
int not_mute(int duration) { OCR1A = VOLUME; duration = duration * BEAT; my_delay_ms(duration); return 0; }
uint16_t get_adcw(uint8_t mux) { uint8_t i; uint16_t result = 0; // Frequenzvorteiler setzen auf 64 == 125kHz (ADPS1/ADPS0) und ADC aktivieren (ADEN) ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS2); ADMUX = mux; // Kanal waehlen ADMUX |= (1<<REFS1) | (1<<REFS0); // interne Referenzspannung (2.56V) nutzen for(i=0; i<2; i++) { ADCSRA |= (1<<ADSC); // Dummy readout while ( ADCSRA & (1<<ADSC) ); // wait for converter to finish } my_delay_ms(1); /* Eigentliche Messung - Mittelwert aus 4 aufeinanderfolgenden Wandlungen */ for(i=0; i<2; i++) { ADCSRA |= (1<<ADSC); // eine Wandlung "single conversion" while ( ADCSRA & (1<<ADSC) ); // auf Abschluss der Konvertierung warten result += ADCW; // Wandlungsergebnisse aufaddieren } ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2) result /= 2; return result; }
////////////////////////////////////////////////// // boot void prepare_boot(char *in) { uint8_t bl = 0; if(in) fromhex(in+1, &bl, 1); if(bl == 0xff) // Allow testing while(1); #ifdef ARM unsigned char volatile * const ram = (unsigned char *) AT91C_ISRAM; //Reset if(bl) *ram = 0xaa; // Next reboot we'd like to jump to the bootloader. USBD_Disconnect(); my_delay_ms(250); my_delay_ms(250); my_delay_ms(250); my_delay_ms(250); AT91C_BASE_RSTC->RSTC_RCR = AT91C_RSTC_PROCRST | AT91C_RSTC_PERRST | AT91C_RSTC_EXTRST | 0xA5<<24; while (1); #else if(bl) // Next reboot we'd like to jump to the bootloader. ewb( EE_REQBL, 1 ); // Simply jumping to the bootloader from here // wont't work. Neither helps to shutdown USB // first. #ifdef HAS_USB USB_ShutDown(); // ??? Needed? #endif #ifdef HAS_FS fs_sync(&fs); // Sync the filesystem #endif TIMSK0 = 0; // Disable the clock which resets the watchdog cli(); wdt_enable(WDTO_15MS); // Make sure the watchdog is running while (1); // go to bed, the wathchdog will take us to reset #endif }
void ethernet_init(void) { // reset Ethernet ENC28J60_RESET_DDR |= _BV( ENC28J60_RESET_BIT ); ENC28J60_RESET_PORT &= ~_BV( ENC28J60_RESET_BIT ); my_delay_ms( 200 ); // unreset Ethernet ENC28J60_RESET_PORT |= _BV( ENC28J60_RESET_BIT ); my_delay_ms( 200 ); network_init(); mac.addr[0] = erb(EE_MAC_ADDR+0); mac.addr[1] = erb(EE_MAC_ADDR+1); mac.addr[2] = erb(EE_MAC_ADDR+2); mac.addr[3] = erb(EE_MAC_ADDR+3); mac.addr[4] = erb(EE_MAC_ADDR+4); mac.addr[5] = erb(EE_MAC_ADDR+5); network_set_MAC(mac.addr); uip_setethaddr(mac); uip_init(); ntp_conn = 0; // setup two periodic timers timer_set(&periodic_timer, CLOCK_SECOND / 4); timer_set(&arp_timer, CLOCK_SECOND * 10); if(erb(EE_USE_DHCP)) { network_set_led(0x4A6);// LED A: Link Status LED B: Blink slow dhcpc_init(&mac); dhcp_state = PT_WAITING; } else { dhcp_state = PT_ENDED; set_eeprom_addr(); } }
int main(void) { init_buttons(); init_leds(); init_display(intensity); init_uart(); uart_interrupt_enable(); sei(); // main loop while(1) { // while button 1 is pressed, increment display brightness (mod 16) uint8_t button_status = debounce(&BUTTON0_PIN, BUTTON0); if(button_status == 1) { intensity = (intensity + 1) % 16; set_register_b(REG_INTENSITY, intensity); } else if(button_status == 2) { LIGHT_PORT ^= (1<<LIGHT); } // if timer is stopped (but not yet resetted) ... if(state == 'S') { // ... and button 0 has been pressed (and released), // save the current time in the times array (only once!) if(debounce(&BUTTON1_PIN, BUTTON1) && !time_already_saved) { uart_interrupt_disable(); append(times, TIMES, last_correct_decoded_time); led_blink(2); time_already_saved = 1; uart_interrupt_enable(); } } // if timer has been stopped or reset ... if(state == 'I' || state == 'S') { // ... and button 0 is pressed, show the current average if(!(BUTTON2_PIN & (1<<BUTTON2))) { uart_interrupt_disable(); display_uint16_time(average()); my_delay_ms(1500); while(!(BUTTON2_PIN & (1<<BUTTON2))); uart_interrupt_enable(); } } } }
static void fht80b_sendpacket(void) { ccStrobe(CC1100_SIDLE); // Don't let the CC1101 to disturb us // avg. FHT packet is 75ms. // The first delay is larger, as we don't know if we received the first or // second FHT actuator message. my_delay_ms(fht80b_out[2]==FHT_CAN_XMIT ? 155 : 75); addParityAndSendData(fht80b_out, 5, FHT_CSUM_START, 1); ccRX(); // reception might be lost due to LOVF fht_display_buf(fht80b_out); }
void it_tunein(void) { int8_t i; #ifdef USE_HAL hal_CC_GDO_init(CC_INSTANCE,INIT_MODE_OUT_CS_IN); hal_enable_CC_GDOin_int(CC_INSTANCE,FALSE); // disable INT - we'll poll... #else EIMSK &= ~_BV(CC1100_INT); SET_BIT( CC1100_CS_DDR, CC1100_CS_PIN ); // CS as output #endif CC1100_DEASSERT; // Toggle chip select signal my_delay_us(30); CC1100_ASSERT; my_delay_us(30); CC1100_DEASSERT; my_delay_us(45); ccStrobe( CC1100_SRES ); // Send SRES command my_delay_us(100); CC1100_ASSERT; // load configuration cc1100_sendbyte( 0 | CC1100_WRITE_BURST ); for(uint8_t i = 0; i < 13; i++) { cc1100_sendbyte(__LPM(CC1100_ITCFG+i)); } // Tune to standard IT-Frequency cc1100_sendbyte(it_frequency[0]); // Modify Freq. for 433.92MHZ, or whatever cc1100_sendbyte(it_frequency[1]); cc1100_sendbyte(it_frequency[2]); for (i = 16; i<EE_CC1100_CFG_SIZE; i++) { cc1100_sendbyte(__LPM(CC1100_ITCFG+i)); } CC1100_DEASSERT; uint8_t *pa = EE_CC1100_PA; CC1100_ASSERT; // setup PA table cc1100_sendbyte( CC1100_PATABLE | CC1100_WRITE_BURST ); for (uint8_t i = 0;i<8;i++) { cc1100_sendbyte(erb(pa++)); } CC1100_DEASSERT; ccStrobe( CC1100_SCAL ); my_delay_ms(1); #ifndef USE_RF_MODE cc_on = 1; // Set CC_ON #endif }
static void it_tunein(void) { int8_t i; #ifdef ARM AT91C_BASE_AIC->AIC_IDCR = 1 << AT91C_ID_PIOA; // disable INT - we'll poll... AT91C_BASE_PIOA->PIO_PPUER = _BV(CC1100_CS_PIN); //Enable pullup AT91C_BASE_PIOA->PIO_OER = _BV(CC1100_CS_PIN); //Enable output AT91C_BASE_PIOA->PIO_PER = _BV(CC1100_CS_PIN); //Enable PIO control #else EIMSK &= ~_BV(CC1100_INT); SET_BIT( CC1100_CS_DDR, CC1100_CS_PIN ); // CS as output #endif CC1100_DEASSERT; // Toggle chip select signal my_delay_us(30); CC1100_ASSERT; my_delay_us(30); CC1100_DEASSERT; my_delay_us(45); ccStrobe( CC1100_SRES ); // Send SRES command my_delay_us(100); CC1100_ASSERT; // load configuration cc1100_sendbyte( 0 | CC1100_WRITE_BURST ); for(uint8_t i = 0; i < 13; i++) { cc1100_sendbyte(__LPM(CC1100_ITCFG+i)); } // Tune to standard IT-Frequency cc1100_sendbyte(it_frequency[0]); // Modify Freq. for 433.92MHZ, or whatever cc1100_sendbyte(it_frequency[1]); cc1100_sendbyte(it_frequency[2]); for (i = 16; i<EE_CC1100_CFG_SIZE; i++) { cc1100_sendbyte(__LPM(CC1100_ITCFG+i)); } CC1100_DEASSERT; uint8_t *pa = EE_CC1100_PA; CC1100_ASSERT; // setup PA table cc1100_sendbyte( CC1100_PATABLE | CC1100_WRITE_BURST ); for (uint8_t i = 0;i<8;i++) { cc1100_sendbyte(erb(pa++)); } CC1100_DEASSERT; ccStrobe( CC1100_SCAL ); my_delay_ms(1); cc_on = 1; // Set CC_ON }
void native_init(uint8_t mode) { EIMSK &= ~_BV(CC1100_INT); // disable INT - we'll poll... SET_BIT( CC1100_CS_DDR, CC1100_CS_PIN ); // CS as output native_on = 0; CC1100_DEASSERT; // Toggle chip select signal my_delay_us(30); CC1100_ASSERT; my_delay_us(30); CC1100_DEASSERT; my_delay_us(45); ccStrobe( CC1100_SRES ); // Send SRES command my_delay_us(100); if (!mode || mode>MAX_MODES) return; // load configuration for (uint8_t i = 0; i<60; i += 2) { if (pgm_read_byte( &NATIVE_CFG[i] )>0x40) break; cc1100_writeReg( pgm_read_byte(&NATIVE_CFG[i]), pgm_read_byte(&NATIVE_CFG[i+1]) ); } // load special configuration for (uint8_t i = 0; i<20; i += 2) { if (pgm_read_byte( &MODE_CFG[mode-1][i] )>0x40) break; cc1100_writeReg( pgm_read_byte(&MODE_CFG[mode-1][i]), pgm_read_byte(&MODE_CFG[mode-1][i+1]) ); } ccStrobe( CC1100_SCAL ); native_on = mode; checkFrequency(); my_delay_ms(1); }
void ccInitChip(uint8_t *cfg) { #ifdef HAS_MORITZ moritz_on = 0; //loading this configuration overwrites moritz cfg #endif #ifdef ARM AT91C_BASE_AIC->AIC_IDCR = 1 << CC1100_IN_PIO_ID; CC1100_CS_BASE->PIO_PPUER = _BV(CC1100_CS_PIN); //Enable pullup CC1100_CS_BASE->PIO_OER = _BV(CC1100_CS_PIN); //Enable output CC1100_CS_BASE->PIO_PER = _BV(CC1100_CS_PIN); //Enable PIO control #else EIMSK &= ~_BV(CC1100_INT); SET_BIT( CC1100_CS_DDR, CC1100_CS_PIN ); // CS as output #endif CC1100_DEASSERT; // Toggle chip select signal my_delay_us(30); CC1100_ASSERT; my_delay_us(30); CC1100_DEASSERT; my_delay_us(45); ccStrobe( CC1100_SRES ); // Send SRES command my_delay_us(100); CC1100_ASSERT; // load configuration cc1100_sendbyte( 0 | CC1100_WRITE_BURST ); for(uint8_t i = 0; i < EE_CC1100_CFG_SIZE; i++) { cc1100_sendbyte(erb(cfg++)); } CC1100_DEASSERT; uint8_t *pa = EE_CC1100_PA; CC1100_ASSERT; // setup PA table cc1100_sendbyte( CC1100_PATABLE | CC1100_WRITE_BURST ); for (uint8_t i = 0;i<8;i++) { cc1100_sendbyte(erb(pa++)); } CC1100_DEASSERT; ccStrobe( CC1100_SCAL ); my_delay_ms(1); }
void rf_moritz_init(void) { #ifdef ARM #ifndef CC_ID AT91C_BASE_AIC->AIC_IDCR = 1 << AT91C_ID_PIOA; // disable INT - we'll poll... #endif CC1100_CS_BASE->PIO_PPUER = _BV(CC1100_CS_PIN); //Enable pullup CC1100_CS_BASE->PIO_OER = _BV(CC1100_CS_PIN); //Enable output CC1100_CS_BASE->PIO_PER = _BV(CC1100_CS_PIN); //Enable PIO control #else EIMSK &= ~_BV(CC1100_INT); // disable INT - we'll poll... SET_BIT( CC1100_CS_DDR, CC1100_CS_PIN ); // CS as output #endif CC1100_DEASSERT; // Toggle chip select signal my_delay_us(30); CC1100_ASSERT; my_delay_us(30); CC1100_DEASSERT; my_delay_us(45); CCSTROBE( CC1100_SRES ); // Send SRES command my_delay_us(100); #ifdef CC_ID CC1100_ASSERT; uint8_t *cfg = EE_CC1100_CFG; for(uint8_t i = 0; i < EE_CC1100_CFG_SIZE; i++) { cc1100_sendbyte(erb(cfg++)); } CC1100_DEASSERT; uint8_t *pa = EE_CC1100_PA; CC1100_ASSERT; // setup PA table cc1100_sendbyte( CC1100_PATABLE | CC1100_WRITE_BURST ); for (uint8_t i = 0;i<8;i++) { cc1100_sendbyte(erb(pa++)); } CC1100_DEASSERT; #endif // load configuration for (uint8_t i = 0; i<60; i += 2) { if (pgm_read_byte( &MORITZ_CFG[i] )>0x40) break; CC1100_WRITEREG( pgm_read_byte(&MORITZ_CFG[i]), pgm_read_byte(&MORITZ_CFG[i+1]) ); } CCSTROBE( CC1100_SCAL ); my_delay_ms(4); // 4ms: Found by trial and error //This is ccRx() but without enabling the interrupt uint8_t cnt = 0xff; //Enable RX. Perform calibration first if coming from IDLE and MCSM0.FS_AUTOCAL=1. //Why do it multiple times? while(cnt-- && (CCSTROBE( CC1100_SRX ) & 0x70) != 1) my_delay_us(10); moritz_on = 1; //todo check multiCC checkFrequency(); }
// Signalton über Summer ausgeben (Port D, Pin 7) // Signaldauer im ms void beep(uint16_t dauer){ PORTD |= 1<<PD7; my_delay_ms(dauer); PORTD &= ~(1<<PD7); }
void rf_asksin_init(void) { #ifdef ARM #ifndef CC_ID AT91C_BASE_AIC->AIC_IDCR = 1 << CC1100_IN_PIO_ID; // disable INT - we'll poll... #endif CC1100_CS_BASE->PIO_PPUER = _BV(CC1100_CS_PIN); //Enable pullup CC1100_CS_BASE->PIO_OER = _BV(CC1100_CS_PIN); //Enable output CC1100_CS_BASE->PIO_PER = _BV(CC1100_CS_PIN); //Enable PIO control #else EIMSK &= ~_BV(CC1100_INT); // disable INT - we'll poll... SET_BIT( CC1100_CS_DDR, CC1100_CS_PIN ); // CS as output #endif CC1100_DEASSERT; // Toggle chip select signal my_delay_us(30); CC1100_ASSERT; my_delay_us(30); CC1100_DEASSERT; my_delay_us(45); CCSTROBE( CC1100_SRES ); // Send SRES command my_delay_us(100); #ifdef CC_ID CC1100_ASSERT; uint8_t *cfg = EE_CC1100_CFG; for(uint8_t i = 0; i < EE_CC1100_CFG_SIZE; i++) { cc1100_sendbyte(erb(cfg++)); } CC1100_DEASSERT; uint8_t *pa = EE_CC1100_PA; CC1100_ASSERT; // setup PA table cc1100_sendbyte( CC1100_PATABLE | CC1100_WRITE_BURST ); for (uint8_t i = 0;i<8;i++) { cc1100_sendbyte(erb(pa++)); } CC1100_DEASSERT; #endif // load configuration for (uint8_t i = 0; i < sizeof(ASKSIN_CFG); i += 2) { CC1100_WRITEREG( pgm_read_byte(&ASKSIN_CFG[i]), pgm_read_byte(&ASKSIN_CFG[i+1]) ); } #ifdef HAS_ASKSIN_FUP if (asksin_update_mode) { for (uint8_t i = 0; i < sizeof(ASKSIN_UPDATE_CFG); i += 2) { cc1100_writeReg( pgm_read_byte(&ASKSIN_UPDATE_CFG[i]), pgm_read_byte(&ASKSIN_UPDATE_CFG[i+1]) ); } } #endif CCSTROBE( CC1100_SCAL ); my_delay_ms(4); // enable RX, but don't enable the interrupt do { CCSTROBE(CC1100_SRX); } while (CC1100_READREG(CC1100_MARCSTATE) != MARCSTATE_RX); }
void asksin_send(char *in) { uint8_t msg[MAX_ASKSIN_MSG]; uint8_t ctl; uint8_t l; uint32_t ts1, ts2; uint8_t hblen = fromhex(in+1, msg, MAX_ASKSIN_MSG-1); if ((hblen-1) != msg[0]) { // DS_P(PSTR("LENERR\r\n")); return; } // in AskSin mode already? if(!asksin_on) { rf_asksin_init(); my_delay_ms(3); // 3ms: Found by trial and error } ctl = msg[2]; // "crypt" msg[1] = (~msg[1]) ^ 0x89; for (l = 2; l < msg[0]; l++) msg[l] = (msg[l-1] + 0xdc) ^ msg[l]; msg[l] = msg[l] ^ ctl; // enable TX, wait for CCA get_timestamp(&ts1); do { CCSTROBE(CC1100_STX); if (CC1100_READREG(CC1100_MARCSTATE) != MARCSTATE_TX) { get_timestamp(&ts2); if (((ts2 > ts1) && (ts2 - ts1 > ASKSIN_WAIT_TICKS_CCA)) || ((ts2 < ts1) && (ts1 + ASKSIN_WAIT_TICKS_CCA < ts2))) { DS_P(PSTR("ERR:CCA\r\n")); goto out; } } } while (CC1100_READREG(CC1100_MARCSTATE) != MARCSTATE_TX); if (ctl & (1 << 4)) { // BURST-bit set? // According to ELV, devices get activated every 300ms, so send burst for 360ms for(l = 0; l < 3; l++) my_delay_ms(120); // arg is uint_8, so loop } else { my_delay_ms(10); } // send CC1100_ASSERT; cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO); for(uint8_t i = 0; i < hblen; i++) { cc1100_sendbyte(msg[i]); } CC1100_DEASSERT; // wait for TX to finish while(CC1100_READREG( CC1100_MARCSTATE ) == MARCSTATE_TX) ; out: if (CC1100_READREG( CC1100_MARCSTATE ) == MARCSTATE_TXFIFO_UNDERFLOW) { CCSTROBE( CC1100_SFTX ); CCSTROBE( CC1100_SIDLE ); CCSTROBE( CC1100_SNOP ); } if(asksin_on) { do { CCSTROBE(CC1100_SRX); } while (CC1100_READREG(CC1100_MARCSTATE) != MARCSTATE_RX); } else { set_txrestore(); } }
void kopp_fc_init(void) { #ifdef ARM AT91C_BASE_AIC->AIC_IDCR = 1 << CC1100_IN_PIO_ID; // disable INT - we'll poll... CC1100_CS_BASE->PIO_PPUER = _BV(CC1100_CS_PIN); //Enable pullup CC1100_CS_BASE->PIO_OER = _BV(CC1100_CS_PIN); //Enable output CC1100_CS_BASE->PIO_PER = _BV(CC1100_CS_PIN); //Enable PIO control #else EIMSK &= ~_BV(CC1100_INT); // disable INT - we'll poll... SET_BIT( CC1100_CS_DDR, CC1100_CS_PIN ); // CS as output #endif // Toggle chip select signal (why?) CC1100_DEASSERT; // Chip Select InActiv my_delay_us(30); CC1100_ASSERT; // Chip Select Activ my_delay_us(30); CC1100_DEASSERT; // Chip Select InActiv my_delay_us(45); ccStrobe( CC1100_SRES ); // Send SRES command (Reset CC110x) my_delay_us(100); // load configuration (CC1100_Kopp_CFG[EE_CC1100_CFG_SIZE]) CC1100_ASSERT; // Chip Select Activ cc1100_sendbyte( 0 | CC1100_WRITE_BURST ); for(uint8_t i = 0; i < EE_CC1100_CFG_SIZE; i++) { cc1100_sendbyte(__LPM(CC1100_Kopp_CFG+i)); } CC1100_DEASSERT; // Chip Select InActiv // If I don't missunderstand the code, in module cc1100.c the pa table is defined as // 00 and C2 what means power off and max. power. // so following code (setup PA table) is not needed ? // did a trial, but does not work // setup PA table (-> Remove as soon as transmitting ok?), table see cc1100.c // this initializes the PA table with the table defined at EE_Prom // which table will be taken depends on command "x00 .... x09" // x00 means -10dbm pa ramping // x09 means +10dBm no pa ramping (see cc1100.c) and commandref.html #ifdef PrintOn // DS_P(PSTR("PA Table values: ")); #endif uint8_t *pa = EE_CC1100_PA; // EE_CC1100_PA+32 means max power??? CC1100_ASSERT; cc1100_sendbyte( CC1100_PATABLE | CC1100_WRITE_BURST); for (uint8_t i = 0; i < 8; i++) { #ifdef PrintOn // DU(erb(pa),0); // ### Claus, mal sehen was im PA Table steht DS_P(PSTR(" ")); #endif cc1100_sendbyte(erb(pa++)); // fncollection.c "erb()"gibt einen EEPROM Wert zurück } #ifdef PrintOn DS_P(PSTR("\r\n")); #endif CC1100_DEASSERT; // Set CC_ON ccStrobe( CC1100_SCAL); // Calibrate Synthesizer and turn it of. ##Claus brauchen wir das my_delay_ms(1); cc_on = 1; kopp_fc_on = 1; //##Claus may be not needed in future (Tx Only) checkFrequency(); }
//------------------------------------------------------------------------------ /// Application entry point. Configures the DBGU, PIT, TC0, LEDs and buttons /// and makes LED\#1 blink in its infinite loop, using the Wait function. /// \return Unused (ANSI-C compatibility). //------------------------------------------------------------------------------ int main(void) { // DBGU configuration TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK); TRACE_INFO_WP("\n\r"); TRACE_INFO("Getting new Started Project --\n\r"); TRACE_INFO("%s\n\r", BOARD_NAME); TRACE_INFO("Compiled: %s %s --\n\r", __DATE__, __TIME__); //Configure Reset Controller AT91C_BASE_RSTC->RSTC_RMR= 0xa5<<24; // Configure EMAC PINS PIO_Configure(emacRstPins, PIO_LISTSIZE(emacRstPins)); // Execute reset RSTC_SetExtResetLength(0xd); RSTC_ExtReset(); // Wait for end hardware reset while (!RSTC_GetNrstLevel()); TRACE_INFO("init Flash\n\r"); flash_init(); TRACE_INFO("init Timer\n\r"); // Configure timer 0 ticks=0; extern void ISR_Timer0(); AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC0); AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; AT91C_BASE_TC0->TC_IDR = 0xFFFFFFFF; AT91C_BASE_TC0->TC_SR; AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV5_CLOCK | AT91C_TC_CPCTRG; AT91C_BASE_TC0->TC_RC = 375; AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS; AIC_ConfigureIT(AT91C_ID_TC0, AT91C_AIC_PRIOR_LOWEST, ISR_Timer0); AIC_EnableIT(AT91C_ID_TC0); AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; // Configure timer 1 extern void ISR_Timer1(); AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1); AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; //Stop clock AT91C_BASE_TC1->TC_IDR = 0xFFFFFFFF; //Disable Interrupts AT91C_BASE_TC1->TC_SR; //Read Status register AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV4_CLOCK | AT91C_TC_CPCTRG; // Timer1: 2,666us = 48MHz/128 AT91C_BASE_TC1->TC_RC = 0xffff; AT91C_BASE_TC1->TC_IER = AT91C_TC_CPCS; AIC_ConfigureIT(AT91C_ID_TC1, 1, ISR_Timer1); AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; led_init(); TRACE_INFO("init EEprom\n\r"); eeprom_init(); rb_reset(&TTY_Rx_Buffer); rb_reset(&TTY_Tx_Buffer); input_handle_func = analyze_ttydata; LED_OFF(); LED2_OFF(); LED3_OFF(); spi_init(); fht_init(); tx_init(); #ifdef HAS_ETHERNET ethernet_init(); #endif TRACE_INFO("init USB\n\r"); CDCDSerialDriver_Initialize(); USBD_Connect(); wdt_enable(WDTO_2S); fastrf_on=0; display_channel = DISPLAY_USB; TRACE_INFO("init Complete\n\r"); checkFrequency(); // Main loop while (1) { CDC_Task(); Minute_Task(); RfAnalyze_Task(); #ifdef HAS_FASTRF FastRF_Task(); #endif #ifdef HAS_RF_ROUTER rf_router_task(); #endif #ifdef HAS_ASKSIN rf_asksin_task(); #endif #ifdef HAS_MORITZ rf_moritz_task(); #endif #ifdef HAS_RWE rf_rwe_task(); #endif #ifdef HAS_MBUS rf_mbus_task(); #endif #ifdef HAS_MAICO rf_maico_task(); #endif #ifdef HAS_ETHERNET Ethernet_Task(); #endif #ifdef DBGU_UNIT_IN if(DBGU_IsRxReady()){ unsigned char volatile * const ram = (unsigned char *) AT91C_ISRAM; unsigned char x; x=DBGU_GetChar(); switch(x) { case 'd': puts("USB disconnect\n\r"); USBD_Disconnect(); break; case 'c': USBD_Connect(); puts("USB Connect\n\r"); break; case 'r': //Configure Reset Controller AT91C_BASE_RSTC->RSTC_RMR=AT91C_RSTC_URSTEN | 0xa5<<24; break; case 'S': USBD_Disconnect(); my_delay_ms(250); my_delay_ms(250); //Reset *ram = 0xaa; AT91C_BASE_RSTC->RSTC_RCR = AT91C_RSTC_PROCRST | AT91C_RSTC_PERRST | AT91C_RSTC_EXTRST | 0xA5<<24; while (1); break; default: rb_put(&TTY_Tx_Buffer, x); } } #endif if (USBD_GetState() == USBD_STATE_CONFIGURED) { if( USBState == STATE_IDLE ) { CDCDSerialDriver_Read(usbBuffer, DATABUFFERSIZE, (TransferCallback) UsbDataReceived, 0); LED3_ON(); USBState=STATE_RX; } } if( USBState == STATE_SUSPEND ) { TRACE_INFO("suspend !\n\r"); USBState = STATE_IDLE; } if( USBState == STATE_RESUME ) { TRACE_INFO("resume !\n\r"); USBState = STATE_IDLE; } } }
int main(void) { led_init(); #ifdef LED_RGB led_off(LED_CHANNEL_GREEN); led_off(LED_CHANNEL_RED); led_off(LED_CHANNEL_BLUE); #else LED_ON(); #endif spi_init(); OCR0A = 249; // Timer0: 0.008s = 8MHz/256/250 == 125Hz TCCR0B = _BV(CS02); TCCR0A = _BV(WGM01); TIMSK0 = _BV(OCIE0A); TCCR1A = 0; TCCR1B = _BV(CS11) | _BV(WGM12); // Timer1: 1us = 8MHz/8 clock_prescale_set(clock_div_1); MCUSR &= ~(1 << WDRF); // Enable the watchdog uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) ); input_handle_func = analyze_ttydata; display_channel = DISPLAY_USB; #ifdef LED_RGB my_delay_ms(200); led_on(LED_CHANNEL_RED); my_delay_ms(200); led_off(LED_CHANNEL_RED); led_on(LED_CHANNEL_GREEN); my_delay_ms(200); led_off(LED_CHANNEL_GREEN); led_on(LED_CHANNEL_BLUE); my_delay_ms(200); led_off(LED_CHANNEL_BLUE); #else LED_OFF(); #endif sei(); /* start moritz function */ moritz_func("Zr\n"); for(;;) { led_process(ticks); uart_task(); Minute_Task(); rf_asksin_task(); rf_moritz_task(); if (rf_moritz_data_available()) { DC('Z'); uint8_t *rf_data = (uint8_t*) &max_data; for (uint8_t i=0; i<=*rf_data; i++) { DH2( *rf_data++ ); } DNL(); DS("length: "); DU(max_data.length, 2); DNL(); DS("msg count: "); DU(max_data.message_count, 2); DNL(); DS("msg type: "); DU(max_data.message_type, 2); DNL(); } } }
static void it_send (char *in, uint8_t datatype) { //while (rf_isreceiving()) { //_delay_ms(1); //} int8_t i, j, k; LED_ON(); #if defined (HAS_IRRX) || defined (HAS_IRTX) //Blockout IR_Reception for the moment cli(); #endif // If NOT InterTechno mode if(!intertechno_on) { #ifdef HAS_ASKSIN if (asksin_on) { restore_asksin = 1; asksin_on = 0; } #endif #ifdef HAS_MORITZ if(moritz_on) { restore_moritz = 1; moritz_on = 0; } #endif it_tunein(); my_delay_ms(3); // 3ms: Found by trial and error } ccStrobe(CC1100_SIDLE); ccStrobe(CC1100_SFRX ); ccStrobe(CC1100_SFTX ); ccTX(); // Enable TX int8_t sizeOfPackage = strlen(in)-1; // IT-V1 = 14, IT-V3 = 33, IT-V3-Dimm = 37 int8_t mode = 0; // IT V1 //DU(sizeOfPackage, 3); if (sizeOfPackage == 33 || sizeOfPackage == 37) { mode = 1; // IT V3 } for(i = 0; i < it_repetition; i++) { if (datatype == DATATYPE_IT) { if (mode == 1) { send_IT_sync_V3(); send_IT_latch_V3(); } else { // Sync-Bit for IT V1 send before package CC1100_SET_OUT; // High my_delay_us(it_interval); CC1100_CLEAR_OUT; // Low for(k = 0; k < 31; k++) { my_delay_us(it_interval); } } #ifdef HAS_HOMEEASY } else if (datatype == DATATYPE_HE) { send_IT_sync_HE(DATATYPE_HE); } else if (datatype == DATATYPE_HEEU) { send_IT_sync_HE(DATATYPE_HEEU); #endif } uint8_t startCount = 1; #ifdef HAS_HOMEEASY if (datatype == DATATYPE_HE || datatype == DATATYPE_HEEU) { startCount = 2; } #endif for(j = startCount; j < sizeOfPackage; j++) { if(in[j+1] == '0') { if (datatype == DATATYPE_IT) { if (mode == 1) { send_IT_bit_V3(0); } else { send_IT_bit(0); } #ifdef HAS_HOMEEASY } else { send_IT_bit_HE(0, datatype); #endif } } else if (in[j+1] == '1') { if (datatype == DATATYPE_IT) { if (mode == 1) { send_IT_bit_V3(1); } else { send_IT_bit(1); } #ifdef HAS_HOMEEASY } else { send_IT_bit_HE(1, datatype); #endif } } else if (in[j+1] == '2') { send_IT_bit_V3(2); } else { if (mode == 1) { send_IT_bit_V3(3); } else { send_IT_bit(2); } } } //if (mode == 1) { // send_IT_sync_V3(); //} } //Do it n Times if(intertechno_on) { if(tx_report) { // Enable RX ccRX(); } else { ccStrobe(CC1100_SIDLE); } } #ifdef HAS_ASKSIN else if (restore_asksin) { restore_asksin = 0; rf_asksin_init(); asksin_on = 1; ccRX(); } #endif #ifdef HAS_MORITZ else if (restore_moritz) { restore_moritz = 0; rf_moritz_init(); } #endif else { set_txrestore(); } #if defined (HAS_IRRX) || defined (HAS_IRTX) //Activate IR_Reception again sei(); #endif LED_OFF(); DC('i');DC('s'); #ifdef HAS_HOMEEASY if (datatype == DATATYPE_HE) { DC('h'); } else if (datatype == DATATYPE_HEEU) { DC('e'); } #endif for(j = 1; j < sizeOfPackage; j++) { if(in[j+1] == '0') { DC('0'); } else if (in[j+1] == '1') { DC('1'); } else if (in[j+1] == '2') { DC('2'); } else { if (datatype == DATATYPE_IT) { if (mode == 1) { DC('D'); } else { DC('F'); } } } } DNL(); }
/* longPreamble is necessary for unsolicited messages to wakeup the receiver */ void moritz_sendraw(uint8_t *dec, int longPreamble) { uint8_t hblen = dec[0]+1; //1kb/s = 1 bit/ms. we send 1 sec preamble + hblen*8 bits uint32_t sum = (longPreamble ? 100 : 0) + (hblen*8)/10; if (credit_10ms < sum) { DS_P(PSTR("LOVF\r\n")); return; } credit_10ms -= sum; // in Moritz mode already? if(!moritz_on) { rf_moritz_init(); } if(CC1100_READREG( CC1100_MARCSTATE ) != MARCSTATE_RX) { //error DC('Z'); DC('E'); DC('R'); DC('R'); DC('1'); DH2(CC1100_READREG( CC1100_MARCSTATE )); DNL(); rf_moritz_init(); return; } /* We have to keep at least 20 ms of silence between two sends * (found out by trial and error). ticks runs at 125 Hz (8 ms per tick), * so we wait for 3 ticks. * This looks a bit cumbersome but handles overflows of ticks gracefully. */ if(lastSendingTicks) while(ticks == lastSendingTicks || ticks == lastSendingTicks+1) my_delay_ms(1); /* Enable TX. Perform calibration first if MCSM0.FS_AUTOCAL=1 (this is the case) (takes 809μs) * start sending - CC1101 will send preamble continuously until TXFIFO is filled. * The preamble will wake up devices. See http://e2e.ti.com/support/low_power_rf/f/156/t/142864.aspx * It will not go into TX mode instantly if channel is not clear (see CCA_MODE), thus ccTX tries multiple times */ #ifdef CC_ID do { CCSTROBE(CC1100_STX); } while (CC1100_READREG(CC1100_MARCSTATE) != MARCSTATE_TX); #else ccTX(); #endif if(CC1100_READREG( CC1100_MARCSTATE ) != MARCSTATE_TX) { //error DC('Z'); DC('E'); DC('R'); DC('R'); DC('2'); DH2(CC1100_READREG( CC1100_MARCSTATE )); DNL(); rf_moritz_init(); return; } if(longPreamble) { /* Send preamble for 1 sec. Keep in mind that waiting for too long may trigger the watchdog (2 seconds on CUL) */ for(int i=0;i<10;++i) my_delay_ms(100); //arg is uint_8, so loop } // send CC1100_ASSERT; cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO); for(uint8_t i = 0; i < hblen; i++) { cc1100_sendbyte(dec[i]); } CC1100_DEASSERT; //Wait for sending to finish (CC1101 will go to RX state automatically //after sending uint8_t i; for(i=0; i< 200;++i) { if( CC1100_READREG( CC1100_MARCSTATE ) == MARCSTATE_RX) break; //now in RX, good if( CC1100_READREG( CC1100_MARCSTATE ) != MARCSTATE_TX) break; //neither in RX nor TX, probably some error my_delay_ms(1); } if(CC1100_READREG( CC1100_MARCSTATE ) != MARCSTATE_RX) { //error DC('Z'); DC('E'); DC('R'); DC('R'); DC('3'); DH2(CC1100_READREG( CC1100_MARCSTATE )); DNL(); rf_moritz_init(); } if(!moritz_on) { set_txrestore(); } lastSendingTicks = ticks; }
void send_belfox(char *msg) { uint8_t repeat=BELFOX_REPEAT; uint8_t len=strnlen(msg,BELFOX_LEN+2)-1; // assert if length incorrect if (len != BELFOX_LEN) return; LED_ON(); #if defined (HAS_IRRX) || defined (HAS_IRTX) // Block IR_Reception cli(); #endif #ifdef USE_RF_MODE change_RF_mode(RF_mode_slow); #else #ifdef HAS_MORITZ uint8_t restore_moritz = 0; if(moritz_on) { restore_moritz = 1; moritz_on = 0; set_txreport("21"); } #endif if(!cc_on) set_ccon(); #endif ccTX(); // Enable TX do { send_sync(); // sync for(int i = 1; i <= BELFOX_LEN; i++) // loop input, for example 'L111001100110' send_bit(msg[i] == '1'); CC1100_OUT_PORT &= ~_BV(CC1100_OUT_PIN); // final low to complete last bit my_delay_ms(BELFOX_PAUSE); // pause } while(--repeat > 0); if(TX_REPORT) { // Enable RX ccRX(); } else { ccStrobe(CC1100_SIDLE); } #if defined (HAS_IRRX) || defined (HAS_IRTX) // Activate IR_Reception sei(); #endif #ifdef USE_RF_MODE restore_RF_mode(); #else #ifdef HAS_MORITZ if(restore_moritz) rf_moritz_init(); #endif #endif LED_OFF(); }
// Transmitt data block for Kopp Free Control // ------------------------------------------------------------------------------------------------------------------------------------------ void TransmittKoppBlk(uint8_t sendmsg01[15], uint8_t blkTXcode_i) { // Read Blockcounter from Config File Datei (RAMDISK) // -------------------------------------------------- uint8_t blkcks = 0x0; int count = 0; int count2 = 1; //count2 = 1; // each block / telegram will be written n times (n = 13 see below) sendmsg01[3] = blkctr; // Write BlockCounter (was incremented) and Transmitt Code (=Transmitter Key) to Array sendmsg01[4] = blkTXcode_i; // ----------------------------------------------------------------------------------- // Send Block via Transmitter Fifo // -------------------------------- do { ccTX(); // initialize CC110x TX Mode? if(cc1100_readReg( CC1100_MARCSTATE ) != MARCSTATE_TX) // If Statemachine not MARCSTATE_TX -> error { DS_P(PSTR("TX_INIT_ERR_")); DH2(cc1100_readReg( CC1100_MARCSTATE )); DNL(); kopp_fc_init(); return; } BlockStartTime = ticks; // remember Start Time (1 tick=8msec, s.o.) blkcks=0xaa; // Checksumme initialisieren count=0; // CC1100_ASSERT; // Chip Select Activ cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO); // Burst Mode via Fifo // Now send ! do { // =========== cc1100_sendbyte(sendmsg01[count]); // write date to fifo (fifo is big enough) if (count <= 8) // { blkcks=blkcks^sendmsg01[count]; // if (count==7) sendmsg01[8]=blkcks; // write ckecksum to Buffer as soon as calculated } // // count++; // } while(count < MAX_kopp_fc_MSG); // Transmitt Byte 0 - AmountofBytes CC1100_DEASSERT; // Chip Select InActiv //Wait for sending to finish (CC1101 will go to RX state automatically uint8_t i; for(i=0; i< 200;++i) { // if( cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_RX) // Claus: After transmission we force idle, always, so RX will not happen // break; //now in RX, good if( cc1100_readReg( CC1100_MARCSTATE ) != MARCSTATE_TX) break; //neither in RX nor TX, probably some error my_delay_ms(1); } // Claus: Test shows i is ~ 0x36, but why so fast??, transmission should need about 25msec (15Bytes*8Bits*4800Bit/sec) // may be reading status via SPI is also also slow? // DS_P(PSTR("variable i: ")); // For test only // DH((uint16_t) (i),4); // For test only // DS_P(PSTR("\r\n")); // For test only count2++; } while(count2 <= 13); // send same message 13x blkctr++; // increase Blockcounter }