void cc_set_pa(uint8_t idx) { uint8_t *t = EE_CC1100_PA; if(idx > 9) idx = 8; #ifdef FULL_CC1100_PA const uint8_t *f = CC1100_PA+idx*8; for (uint8_t i = 0; i < 8; i++) ewb(t++, __LPM(f+i)); // Correct the FREND0 ewb(EE_CC1100_CFG+0x22, (idx > 4 && idx < 10) ? 0x11 : 0x17); #else if(idx > 4) idx -= 5; for (uint8_t i = 0; i < 8; i++) ewb(t++, i == 1 ? __LPM(CC1100_PA+idx) : 0); #endif }
static void // EEPROM Write IP ewip(const u16_t ip[2], uint8_t *addr) { uint16_t ip0 = HTONS(ip[0]); uint16_t ip1 = HTONS(ip[1]); ewb(addr+0, ip0>>8); ewb(addr+1, ip0&0xff); ewb(addr+2, ip1>>8); ewb(addr+3, ip1&0xff); }
void write_eeprom(char *in) { uint8_t hb[6], d = 0; #ifdef HAS_ETHERNET if(in[1] == 'i') { uint8_t *addr = 0; if(in[2] == 'm') { d=6; fromhex(in+3,hb,6); addr=EE_MAC_ADDR; } else if(in[2] == 'd') { d=1; fromdec(in+3,hb); addr=EE_USE_DHCP; } else if(in[2] == 'a') { d=4; fromip (in+3,hb,4); addr=EE_IP4_ADDR; } else if(in[2] == 'n') { d=4; fromip (in+3,hb,4); addr=EE_IP4_NETMASK; } else if(in[2] == 'g') { d=4; fromip (in+3,hb,4); addr=EE_IP4_GATEWAY; } else if(in[2] == 'p') { d=2; fromdec(in+3,hb); addr=EE_IP4_TCPLINK_PORT; } else if(in[2] == 'N') { d=4; fromip (in+3,hb,4); addr=EE_IP4_NTPSERVER; } else if(in[2] == 'o') { d=1; fromhex(in+3,hb,1); addr=EE_IP4_NTPOFFSET; #ifdef HAS_NTP extern int8_t ntp_gmtoff; ntp_gmtoff = hb[0]; #endif } for(uint8_t i = 0; i < d; i++) ewb(addr++, hb[i]); } else #endif { uint16_t addr; d = fromhex(in+1, hb, 3); if(d < 2) return; if(d == 2) addr = hb[0]; else addr = (hb[0] << 8) | hb[1]; ewb((uint8_t*)addr, hb[d-1]); if (addr == 15 || addr == 16 || addr == 17) checkFrequency(); // If there are still bytes left, then write them too in += (2*d+1); while(in[0]) { addr++; if(!fromhex(in, hb, 1)) return; ewb((uint8_t*)addr++, hb[0]); in += 2; } } }
//-------------------------------------------------------------------- void cc_factory_reset(void) { uint8_t *t = EE_CC1100_CFG; for(uint8_t i = 0; i < sizeof(CC1100_CFG); i++) ewb(t++, __LPM(CC1100_CFG+i)); #if defined(HAS_FASTRF) || defined(HAS_RF_ROUTER) t = EE_FASTRF_CFG; for(uint8_t i = 0; i < sizeof(FASTRF_CFG); i++) ewb(t++, __LPM(FASTRF_CFG+i)); #endif #ifdef MULTI_FREQ_DEVICE // check 433MHz version marker and patch default frequency if (!bit_is_set(MARK433_PIN, MARK433_BIT)) { t = EE_CC1100_CFG + 0x0d; ewb(t++, 0x10); ewb(t++, 0xb0); ewb(t++, 0x71); #if defined(HAS_FASTRF) || defined(HAS_RF_ROUTER) t = EE_FASTRF_CFG + 0x0d; ewb(t++, 0x10); ewb(t++, 0xb0); ewb(t++, 0x71); #endif } #endif cc_set_pa(8); }
// LED void ledfunc(char *in) { fromhex(in+1, &led_mode, 1); #ifdef XLED switch (led_mode) { case 0: xled_pattern = 0; break; case 1: xled_pattern = 0xffff; break; case 2: xled_pattern = 0xff00; break; case 3: xled_pattern = 0xa000; break; case 4: xled_pattern = 0xaa00; break; } #else if(led_mode & 1) LED_ON(); else LED_OFF(); #endif ewb(EE_LED, led_mode); }
void lcd_brightness(uint8_t hb) { int16_t brightness = erb((uint8_t*)EE_BRIGHTNESS); if(hb == 0xFE) { brightness += 0x20; } else if (hb == 0xFD) { brightness -= 0x20; } else if (hb == 0xFC) { //keep the eeprom value } else { brightness = hb; } if(brightness < 0) brightness = 0; if(brightness > 255) brightness = 255; ewb((uint8_t*)EE_BRIGHTNESS, brightness); LCD_BL_PWM = brightness; DS_P( PSTR("Brightns:") ); DU(brightness*100/255, 3); DC('%'); DNL(); }
void lcd_contrast(uint8_t hb) { uint8_t contrast = erb((uint8_t*)EE_CONTRAST); if(hb == 0xFE) { contrast--; } else if (hb == 0xFD) { contrast++; } else if (hb == 0xFC) { //keep the eeprom value } else { contrast = hb; } if(contrast < 40) contrast = 40; if(contrast > 80) contrast = 80; ewb((uint8_t*)EE_CONTRAST, contrast); lcd_sendcmd (LCD_CMD_SETCON); lcd_senddata (contrast); DS_P( PSTR("Contrast:") ); DU(100-(contrast-40)*100/40, 3); DC('%'); DNL(); }
void rf_router_func(char *in) { if(in[1] == 0) { // u: display id and router DH2(rf_router_myid); DH2(rf_router_target); DNL(); #ifdef RFR_DEBUG } else if(in[1] == 'd') { // ud: Debug DH((uint16_t)ticks, 4); DC('.'); DH2(rf_router_sendtime); DNL(); } else if(in[1] == 's') { // us: Statistics DH(nr_t,1); DC('.'); DH(nr_f,1); DC('.'); DH(nr_e,1); DC('.'); DH(nr_k,1); DC('.'); DH(nr_h,1); DC('.'); DH(nr_r,1); DC('.'); DH(nr_plus,1); DNL(); #endif } else if(in[1] == 'i') { // uiXXYY: set own id to XX and router id to YY fromhex(in+2, &rf_router_myid, 1); ewb(EE_RF_ROUTER_ID, rf_router_myid); fromhex(in+4, &rf_router_target, 1); ewb(EE_RF_ROUTER_ROUTER, rf_router_target); } else { // uYYDATA: send data to node with id YY rb_reset(&RFR_Buffer); while(*++in) rb_put(&RFR_Buffer, *in); rf_router_send(0); } }
////////////////////////////////////////////////// // 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 eeprom_factory_reset(char *in) { ewb(EE_MAGIC_OFFSET , VERSION_1); ewb(EE_MAGIC_OFFSET+1, VERSION_2); cc_factory_reset(); checkFrequency(); ewb(EE_RF_ROUTER_ID, 0); ewb(EE_RF_ROUTER_ROUTER, 0); ewb(EE_REQBL, 0); ewb(EE_LED, 2); #ifdef HAS_LCD ewb(EE_CONTRAST, 0x40); ewb(EE_BRIGHTNESS, 0x80); ewb(EE_SLEEPTIME, 30); #endif #ifdef HAS_ETHERNET ethernet_reset(); #endif #ifdef HAS_FS ewb(EE_LOGENABLED, 0x00); #endif #ifdef HAS_RF_ROUTER ewb(EE_RF_ROUTER_ID, 0x00); ewb(EE_RF_ROUTER_ROUTER, 0x00); #endif if(in[1] != 'x') prepare_boot(0); }
void fhtsend(char *in) { uint8_t hb[6], l; // Last byte needed for 8v checksum l = fromhex(in+1, hb, 5); if(l < 4) { if(hb[0] == 1) { // Set housecode, clear buffers if(l == 3) { ewb(EE_FHTID , hb[1]); // 1.st byte: 80b relevant ewb(EE_FHTID+1, hb[2]); // 1.st+2.nd byte: 8v relevant fht_init(); return; } else { DH2(fht_hc0); DH2(fht_hc1); } #ifdef HAS_FHT_80b } else if(hb[0] == 2) { // Return the 80b buffer fht80b_print(l==1 || hb[1]==1); } else if(hb[0] == 3) { // Return the remaining fht buffer #if FHTBUF_SIZE > 255 DH(fht_bufspace(),4); #else DH2(fht_bufspace()); #endif #endif #ifdef HAS_FHT_8v } else if(hb[0] == 0x10) { // Return the 8v buffer uint8_t na=0; for(int i = 0; i < FHT_8V_NUM; i++) { if(fht8v_buf[2*i] == FHT_8V_DISABLED) continue; if(na) DC(' '); DH2(i); DC(':'); DH2(fht8v_buf[2*i]); DH2(fht8v_buf[2*i+1]); na++; } if(na==0) DS_P( PSTR("N/A") ); } else if(hb[0] == 0x11) { // Return the next 8v timeout DH2(fht8v_timeout/125); #endif } DNL(); } else { #ifdef HAS_FHT_8v if(hb[0]>=fht_hc0 && hb[0]< fht_hc0+FHT_8V_NUM && hb[1]==fht_hc1) { // FHT8v mode commands if(hb[3] == FHT8V_CMD_PAIR) { addParityAndSend(in, FHT_CSUM_START, 2); } else if(hb[3] == FHT8V_CMD_SYNC){// start syncprocess for _all_ 8v's fht8v_ctsync = hb[4]; // use it to shorten the sync-time fht8v_timeout=1; // Cheating on the 1% uint8_t cnt = 0; for(uint8_t i = 0 ; i < FHT_8V_NUM; i++ ) if(fht8v_buf[2*i] != FHT_8V_DISABLED ) cnt++; credit_10ms += (4*fht8v_ctsync); // should be 3.75 = 75ms / 2 / 10 } else { // Valve position uint8_t idx = (hb[0]-fht_hc0)*2; fht8v_buf[idx ] = hb[3]; // Command or 0xff for disable this fht8v_buf[idx+1] = hb[4]; // slot (valve) } return; } #endif #ifdef HAS_FHT_80b if(!fht_addbuf(in)) // FHT80b mode: Queue everything DS_P( PSTR("EOB\r\n") ); #endif } }
int main(void) { wdt_enable(WDTO_2S); #if defined(CUL_ARDUINO) clock_prescale_set(clock_div_1); // for 8MHz clock div schould be 1 #endif MARK433_PORT |= _BV( MARK433_BIT ); // Pull 433MHz marker MARK915_PORT |= _BV( MARK915_BIT ); // Pull 915MHz marker // if we had been restarted by watchdog check the REQ BootLoader byte in the // EEPROM ... if(bit_is_set(MCUSR,WDRF) && erb(EE_REQBL)) { ewb( EE_REQBL, 0 ); // clear flag start_bootloader(); } // Setup the timers. Are needed for watchdog-reset OCR0A = 249; // Timer0: 0.008s = 8MHz/256/250 TCCR0B = _BV(CS02); TCCR0A = _BV(WGM01); TIMSK0 = _BV(OCIE0A); TCCR1A = 0; TCCR1B = _BV(CS11) | _BV(WGM12); // Timer1: 1us = 8MHz/8 MCUSR &= ~(1 << WDRF); // Enable the watchdog led_init(); spi_init(); eeprom_init(); USB_Init(); fht_init(); tx_init(); input_handle_func = analyze_ttydata; #ifdef HAS_RF_ROUTER rf_router_init(); display_channel = (DISPLAY_USB|DISPLAY_RFROUTER); #else display_channel = DISPLAY_USB; #endif checkFrequency(); LED_OFF(); for(;;) { USB_USBTask(); CDC_Task(); RfAnalyze_Task(); Minute_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_RFNATIVE native_task(); #endif #ifdef HAS_KOPP_FC kopp_fc_task(); #endif #ifdef HAS_MBUS rf_mbus_task(); #endif #ifdef HAS_ZWAVE rf_zwave_task(); #endif } }
int main(void) { wdt_enable(WDTO_2S); clock_prescale_set(clock_div_1); // Disable Clock Division // if we had been restarted by watchdog check the REQ BootLoader byte in the // EEPROM ... if (bit_is_set(MCUSR,WDRF) && erb(EE_REQBL)) { ewb( EE_REQBL, 0 ); // clear flag start_bootloader(); } // Setup the timers. Are needed for watchdog-reset OCR0A = 249; // Timer0: 0.008s = 8MHz/256/250 TCCR0B = _BV(CS02); TCCR0A = _BV(WGM01); TIMSK0 = _BV(OCIE0A); TCCR1A = 0; TCCR1B = _BV(CS11) | _BV(WGM12); // Timer1: 1us = 8MHz/8 #ifdef CURV3 // Timer3 is used by the LCD backlight (PWM) TCCR3A = _BV(COM3A1)| _BV(WGM30); // Fast PWM, 8-bit, clear on match TCCR3B = _BV(WGM32) | _BV(CS31); // Prescaler 8MHz/8: 3.9KHz #endif MCUSR &= ~(1 << WDRF); // Enable the watchdog led_init(); spi_init(); eeprom_init(); USB_Init(); fht_init(); tx_init(); joy_init(); // lcd init is done manually from menu_init bat_init(); df_init(&df); fs_init(&fs, df, 0); // needs df_init rtc_init(); // does i2c_init too log_init(); // needs fs_init & rtc_init menu_init(); // needs fs_init input_handle_func = analyze_ttydata; log_enabled = erb(EE_LOGENABLED); display_channel = DISPLAY_USB|DISPLAY_LCD|DISPLAY_RFROUTER; rf_router_init(); checkFrequency(); LED_OFF(); for(;;) { USB_USBTask(); CDC_Task(); RfAnalyze_Task(); Minute_Task(); FastRF_Task(); rf_router_task(); JOY_Task(); } }
int main(void) { //ewb((uint8_t*)0x80, erb((uint8_t*)0x80)+1); wdt_enable(WDTO_2S); // Avoid an early reboot clock_prescale_set(clock_div_1); // Disable Clock Division:1->8MHz #ifdef HAS_XRAM init_memory_mapped(); // First initialize the RAM #endif // if we had been restarted by watchdog check the REQ BootLoader byte in the // EEPROM if(bit_is_set(MCUSR,WDRF) && erb(EE_REQBL)) { ewb( EE_REQBL, 0 ); // clear flag start_bootloader(); } while(tx_report); // reboot if the bss is not initialized // Setup the timers. Are needed for watchdog-reset OCR0A = 249; // Timer0: 0.008s = 8MHz/256/250 TCCR0B = _BV(CS02); TCCR0A = _BV(WGM01); TIMSK0 = _BV(OCIE0A); TCCR1A = 0; TCCR1B = _BV(CS11) | _BV(WGM12); // Timer1: 1us = 8MHz/8 MCUSR &= ~(1 << WDRF); // Enable the watchdog led_init(); // So we can debug spi_init(); eeprom_init(); USB_Init(); fht_init(); tx_init(); ethernet_init(); #ifdef HAS_FS df_init(&df); fs_init(&fs, df, 0); // needs df_init log_init(); // needs fs_init & rtc_init log_enabled = erb(EE_LOGENABLED); #endif input_handle_func = analyze_ttydata; display_channel = DISPLAY_USB; LED_OFF(); for(;;) { USB_USBTask(); CDC_Task(); RfAnalyze_Task(); Minute_Task(); FastRF_Task(); rf_router_task(); #ifdef HAS_ASKSIN rf_asksin_task(); #endif #ifdef HAS_ETHERNET Ethernet_Task(); #endif } }
//------------------------------------------------------------------------------ /// 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=AT91C_RSTC_URSTEN | 0xa5<<24; 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(); 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_RFNATIVE native_task(); #endif #ifdef HAS_KOPP_FC kopp_fc_task(); #endif #ifdef HAS_ZWAVE rf_zwave_task(); #endif #ifdef HAS_MAICO rf_maico_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 'w': ewb(0x1e, 0x55); 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; } } }