/home/daqweb/fgddaq/c8051/mscbemb.h File Reference


Data Structures

struct  MSCB_INFO
struct  MSCB_INFO_VAR
struct  SYS_INFO

Defines

#define USE_WATCHDOG
#define HAVE_EEPROM
#define LED_OFF   !LED_ON
#define N_LED   0
#define SCON0   SCON
#define SBUF0   SBUF
#define TI0   TI
#define RI0   RI
#define RB80   RB8
#define ES0   ES
#define PS0   PS
#define EEPROM_OFFSET   0x6000
#define N_EEPROM_PAGE   8
#define DELAY_US(_us)
#define PROTOCOL_VERSION   5
#define INTERCHAR_DELAY   20
#define CMD_ADDR_NODE8   0x09
#define CMD_ADDR_NODE16   0x0A
#define CMD_ADDR_BC   0x10
#define CMD_ADDR_GRP8   0x11
#define CMD_ADDR_GRP16   0x12
#define CMD_PING8   0x19
#define CMD_PING16   0x1A
#define CMD_INIT   0x20
#define CMD_GET_INFO   0x28
#define CMD_SET_ADDR   0x33
#define CMD_SET_NAME   0x37
#define CMD_SET_BAUD   0x39
#define CMD_FREEZE   0x41
#define CMD_SYNC   0x49
#define CMD_SET_TIME   0x4E
#define CMD_UPGRADE   0x50
#define CMD_USER   0x58
#define CMD_ECHO   0x61
#define CMD_TOKEN   0x68
#define CMD_GET_UPTIME   0x70
#define CMD_ACK   0x78
#define CMD_WRITE_NA   0x80
#define CMD_WRITE_ACK   0x88
#define CMD_FLASH   0x98
#define CMD_READ   0xA0
#define CMD_WRITE_BLOCK   0xB5
#define CMD_READ_BLOCK   0xB9
#define GET_INFO_GENERAL   0
#define GET_INFO_VARIABLES   1
#define ADDR_SET_NODE   1
#define ADDR_SET_HIGH   2
#define ADDR_SET_GROUP   3
#define UCMD_ECHO   1
#define UCMD_ERASE   2
#define UCMD_PROGRAM   3
#define UCMD_VERIFY   4
#define UCMD_READ   5
#define UCMD_REBOOT   6
#define UCMD_RETURN   7
#define CSR_DEBUG   (1<<0)
#define CSR_LCD_PRESENT   (1<<1)
#define CSR_SYNC_MODE   (1<<2)
#define CSR_FREEZE_MODE   (1<<3)
#define CSR_WD_RESET   (1<<2)
#define BD_2400   1
#define BD_4800   2
#define BD_9600   3
#define BD_19200   4
#define BD_28800   5
#define BD_38400   6
#define BD_57600   7
#define BD_115200   8
#define BD_172800   9
#define BD_345600   10
#define MSCBF_FLOAT   (1<<0)
#define MSCBF_SIGNED   (1<<1)
#define MSCBF_DATALESS   (1<<2)
#define MSCBF_HIDDEN   (1<<3)
#define MSCBF_REMIN   (1<<4)
#define MSCBF_REMOUT   (1<<5)
#define MSCBF_INVALID   (1<<6)
#define PRFX_PICO   -12
#define PRFX_NANO   -9
#define PRFX_MICRO   -6
#define PRFX_MILLI   -3
#define PRFX_NONE   0
#define PRFX_KILO   3
#define PRFX_MEGA   6
#define PRFX_GIGA   9
#define PRFX_TERA   12
#define UNIT_UNDEFINED   0
#define UNIT_METER   1
#define UNIT_GRAM   2
#define UNIT_SECOND   3
#define UNIT_MINUTE   4
#define UNIT_HOUR   5
#define UNIT_AMPERE   6
#define UNIT_KELVIN   7
#define UNIT_CELSIUS   8
#define UNIT_FARENHEIT   9
#define UNIT_HERTZ   20
#define UNIT_PASCAL   21
#define UNIT_BAR   22
#define UNIT_WATT   23
#define UNIT_VOLT   24
#define UNIT_OHM   25
#define UNIT_TESLA   26
#define UNIT_LITERPERSEC   27
#define UNIT_RPM   28
#define UNIT_FARAD   29
#define UNIT_JOULE   30
#define UNIT_BOOLEAN   50
#define UNIT_BYTE   52
#define UNIT_WORD   53
#define UNIT_DWORD   54
#define UNIT_ASCII   55
#define UNIT_STRING   56
#define UNIT_BAUD   57
#define UNIT_PERCENT   90
#define UNIT_PPM   91
#define UNIT_COUNT   92
#define UNIT_FACTOR   93
#define ENABLE_INTERRUPTS   { EA = 1; }
#define DISABLE_INTERRUPTS   { EA = 0; }

Functions

void watchdog_refresh (unsigned char from_interrupt) reentrant
void watchdog_enable (unsigned char watchdog_timeout)
void watchdog_disable (void)
void yield (void)
void led_set (unsigned char led, unsigned char flag) reentrant
void led_blink (unsigned char led, unsigned char n, int interval) reentrant
void led_mode (unsigned char led, unsigned char flag) reentrant
void delay_us (unsigned int us)
void delay_ms (unsigned int ms)
unsigned char crc8 (unsigned char *buffer, int len) reentrant
unsigned char crc8_add (unsigned char crc, unsigned int c)
void lcd_setup ()
void lcd_clear ()
void lcd_cursor (unsigned char flag)
void lcd_goto (char x, char y)
void lcd_putc (char c)
void lcd_puts (char *str)
char scs_lcd1_read ()
char getchar_nowait (void)
unsigned char gets_wait (char *str, unsigned char size, unsigned char timeout)
void flush (void)
void eeprom_flash (void)
unsigned char eeprom_retrieve (unsigned char flag)
void setup_variables (void)
void uart_init (unsigned char port, unsigned char baud)
unsigned char uart1_send (char *buffer, int size, unsigned char bit9)
unsigned char uart1_receive (char *buffer, int size)
void send_remote_var (unsigned char i)
unsigned char ping (unsigned short addr)
void sysclock_init (void)
void sysclock_reset (void)
unsigned long time (void)
unsigned long uptime (void)
unsigned char user_func (unsigned char *data_in, unsigned char *data_out)
void set_n_sub_addr (unsigned char n)
unsigned char cur_sub_addr (void)
void rtc_init (void)
unsigned char rtc_present (void)
void rtc_write (unsigned char d[6])
void rtc_read (unsigned char d[6])
void rtc_write_item (unsigned char item, unsigned char d)
void rtc_conv_date (unsigned char d[6], char *str)
void rtc_conv_time (unsigned char d[6], char *str)
void rtc_print (void)
void rtc_write_byte (unsigned char adr, unsigned char d)

Define Documentation

#define ADDR_SET_GROUP   3

#define ADDR_SET_HIGH   2

#define ADDR_SET_NODE   1

#define BD_115200   8

#define BD_172800   9

#define BD_19200   4

#define BD_2400   1

#define BD_28800   5

#define BD_345600   10

#define BD_38400   6

#define BD_4800   2

#define BD_57600   7

#define BD_9600   3

#define CMD_ACK   0x78

#define CMD_ADDR_BC   0x10

#define CMD_ADDR_GRP16   0x12

#define CMD_ADDR_GRP8   0x11

#define CMD_ADDR_NODE16   0x0A

#define CMD_ADDR_NODE8   0x09

#define CMD_ECHO   0x61

#define CMD_FLASH   0x98

#define CMD_FREEZE   0x41

#define CMD_GET_INFO   0x28

#define CMD_GET_UPTIME   0x70

#define CMD_INIT   0x20

#define CMD_PING16   0x1A

#define CMD_PING8   0x19

#define CMD_READ   0xA0

#define CMD_READ_BLOCK   0xB9

#define CMD_SET_ADDR   0x33

#define CMD_SET_BAUD   0x39

#define CMD_SET_NAME   0x37

#define CMD_SET_TIME   0x4E

#define CMD_SYNC   0x49

#define CMD_TOKEN   0x68

#define CMD_UPGRADE   0x50

#define CMD_USER   0x58

#define CMD_WRITE_ACK   0x88

#define CMD_WRITE_BLOCK   0xB5

#define CMD_WRITE_NA   0x80

#define CSR_DEBUG   (1<<0)

#define CSR_FREEZE_MODE   (1<<3)

#define CSR_LCD_PRESENT   (1<<1)

#define CSR_SYNC_MODE   (1<<2)

#define CSR_WD_RESET   (1<<2)

#define DELAY_US ( _us   ) 

Value:

{ \
   unsigned char _i; \
   for (_i = (unsigned char) _us; _i > 0; _i--) { \
      _nop_(); \
      _nop_(); \
   } \
}

#define DISABLE_INTERRUPTS   { EA = 0; }

#define EEPROM_OFFSET   0x6000

#define ENABLE_INTERRUPTS   { EA = 1; }

#define ES0   ES

#define GET_INFO_GENERAL   0

#define GET_INFO_VARIABLES   1

#define HAVE_EEPROM

#define INTERCHAR_DELAY   20

#define LED_OFF   !LED_ON

#define MSCBF_DATALESS   (1<<2)

#define MSCBF_FLOAT   (1<<0)

#define MSCBF_HIDDEN   (1<<3)

#define MSCBF_INVALID   (1<<6)

#define MSCBF_REMIN   (1<<4)

#define MSCBF_REMOUT   (1<<5)

#define MSCBF_SIGNED   (1<<1)

#define N_EEPROM_PAGE   8

#define N_LED   0

#define PRFX_GIGA   9

#define PRFX_KILO   3

#define PRFX_MEGA   6

#define PRFX_MICRO   -6

#define PRFX_MILLI   -3

#define PRFX_NANO   -9

#define PRFX_NONE   0

#define PRFX_PICO   -12

#define PRFX_TERA   12

#define PROTOCOL_VERSION   5

#define PS0   PS

#define RB80   RB8

#define RI0   RI

#define SBUF0   SBUF

#define SCON0   SCON

#define TI0   TI

#define UCMD_ECHO   1

#define UCMD_ERASE   2

#define UCMD_PROGRAM   3

#define UCMD_READ   5

#define UCMD_REBOOT   6

#define UCMD_RETURN   7

#define UCMD_VERIFY   4

#define UNIT_AMPERE   6

#define UNIT_ASCII   55

#define UNIT_BAR   22

#define UNIT_BAUD   57

#define UNIT_BOOLEAN   50

#define UNIT_BYTE   52

#define UNIT_CELSIUS   8

#define UNIT_COUNT   92

#define UNIT_DWORD   54

#define UNIT_FACTOR   93

#define UNIT_FARAD   29

#define UNIT_FARENHEIT   9

#define UNIT_GRAM   2

#define UNIT_HERTZ   20

#define UNIT_HOUR   5

#define UNIT_JOULE   30

#define UNIT_KELVIN   7

#define UNIT_LITERPERSEC   27

#define UNIT_METER   1

#define UNIT_MINUTE   4

#define UNIT_OHM   25

#define UNIT_PASCAL   21

#define UNIT_PERCENT   90

#define UNIT_PPM   91

#define UNIT_RPM   28

#define UNIT_SECOND   3

#define UNIT_STRING   56

#define UNIT_TESLA   26

#define UNIT_UNDEFINED   0

#define UNIT_VOLT   24

#define UNIT_WATT   23

#define UNIT_WORD   53

#define USE_WATCHDOG


Function Documentation

unsigned char crc8 ( unsigned char *  buffer,
int  len 
)

00069          : crc8
00070 
00071   Purpose: Calculate 8-bit cyclic redundancy checksum for a full
00072            buffer
00073 
00074   Input:
00075     unsigned char *data     data buffer
00076     int len                 data length in bytes
00077 
00078 
00079   Function value:
00080     unsighend char          CRC-8 code
00081 
00082 \********************************************************************/
00083 {
00084    int i;
00085    unsigned char crc8_code, index;
00086 
00087    crc8_code = 0;
00088    for (i = 0; i < len; i++) {
00089       index = buffer[i] ^ crc8_code;
00090       crc8_code = crc8_data[index];
00091    }
00092 
00093    return crc8_code;
00094 }

unsigned char crc8_add ( unsigned char  crc,
unsigned int  c 
)

00099          : crc8_add
00100 
00101   Purpose: Single calculation for 8-bit cyclic redundancy checksum
00102 
00103   Input:
00104     unsigned char crc       running crc
00105     unsigned char c         new character
00106 
00107 
00108   Function value:
00109     unsighend char          CRC-8 code
00110 
00111 \********************************************************************/
00112 {
00113    unsigned char index;
00114 
00115    index = c ^ crc;
00116    return crc8_data[index];
00117 }

unsigned char cur_sub_addr ( void   ) 

00403 {
00404    return _cur_sub_addr;
00405 }

void delay_ms ( unsigned int  ms  ) 

01165 {
01166    unsigned int i;
01167 
01168    for (i = 0; i < ms; i++) {
01169       delay_us(1000);
01170       watchdog_refresh(1);
01171    }
01172 }

void delay_us ( unsigned int  us  ) 

01175 {
01176 #if defined(CPU_C8051F120) || defined(CPU_C8051F310) || defined(CPU_C8051F410)
01177    unsigned char j;
01178 #endif
01179 
01180    unsigned char i;
01181    unsigned int remaining_us;
01182 
01183    if (us <= 250) {
01184       for (i = (unsigned char) us; i > 0; i--) {
01185 #if defined(SCS_210)
01186          _nop_();
01187          for (j=3 ; j>0 ; j--)
01188             _nop_();
01189 #elif defined(CPU_C8051F120)
01190          for (j=22 ; j>0 ; j--)
01191              _nop_();
01192 #elif defined(CPU_C8051F310) || defined(CPU_C8051F410)
01193          _nop_();
01194          for (j=3 ; j>0 ; j--)
01195             _nop_();
01196 #elif defined(CPU_C8051F320)
01197          _nop_();
01198          _nop_();
01199 #else
01200          _nop_();
01201 #endif
01202       }
01203    } else {
01204       remaining_us = us;
01205       while (remaining_us > 250) {
01206          delay_us(250);
01207          remaining_us -= 250;
01208       }
01209       if (us > 0)
01210          delay_us(remaining_us);
01211    }
01212 }

void eeprom_flash ( void   ) 

01384          : eeprom_flash
01385 
01386   Purpose: Write system and user parameters to EEPROM
01387 
01388 \********************************************************************/
01389 {
01390    unsigned char i, adr;
01391    unsigned short magic, offset;
01392 
01393    eeprom_erase();
01394 
01395    offset = 0;
01396 
01397    // system info (node address etc...)
01398    eeprom_write(&sys_info, sizeof(SYS_INFO), &offset);
01399 
01400    // magic
01401    magic = 0x1234;
01402    eeprom_write(&magic, 2, &offset);
01403 
01404    // user channel variables
01405    for (adr = 0 ; adr < _n_sub_addr ; adr++)
01406       for (i = 0; variables[i].width; i++)
01407          eeprom_write((char *)variables[i].ud + _var_size*adr,
01408                       variables[i].width, &offset);
01409 
01410    // magic
01411    magic = 0x1234;
01412    eeprom_write(&magic, 2, &offset);
01413 
01414    _flkey = 0;
01415 }

unsigned char eeprom_retrieve ( unsigned char  flag  ) 

01422          : eeprom_retrieve
01423 
01424   Purpose: Retrieve system and user parameters from EEPROM
01425 
01426 \********************************************************************/
01427 {
01428    unsigned char i, adr, status;
01429    unsigned short magic, offset;
01430 
01431    offset = 0;
01432    status = 0;
01433 
01434    // system info (node address etc...)
01435    eeprom_read(&sys_info, sizeof(SYS_INFO), &offset);
01436 
01437    // check for first magic
01438    eeprom_read(&magic, 2, &offset);
01439    if (magic == 0x1234)
01440       status |= (1 << 0);
01441 
01442    // user channel variables
01443    if (flag) {
01444       for (adr = 0 ; adr < _n_sub_addr ; adr++)
01445          for (i = 0; variables[i].width; i++)
01446             eeprom_read((char *)variables[i].ud + _var_size*adr,
01447                         variables[i].width, &offset);
01448    
01449       // check for second magic
01450       eeprom_read(&magic, 2, &offset);
01451       if (magic == 0x1234)
01452          status |= (1 << 1);
01453    } else
01454       status |= (1 << 1);
01455 
01456    return status;
01457 }

void flush ( void   ) 

char getchar_nowait ( void   ) 

unsigned char gets_wait ( char *  str,
unsigned char  size,
unsigned char  timeout 
)

void lcd_clear (  ) 

void lcd_cursor ( unsigned char  flag  ) 

void lcd_goto ( char  x,
char  y 
)

void lcd_putc ( char  c  ) 

void lcd_puts ( char *  str  ) 

void lcd_setup (  ) 

void led_blink ( unsigned char  led,
unsigned char  n,
int  interval 
)

00871          : blink led
00872 
00873   Purpose: Blink primary or secondary LED for a couple of times
00874 
00875   Input:
00876     int led               0 for primary, 1 for secondary, ...
00877     int interval          Blink interval in ms
00878     int n                 Number of blinks
00879 
00880 \********************************************************************/
00881 {
00882    if (led < N_LED) {
00883       if (leds[led].n == 0 && leds[led].timer == 0) {
00884          leds[led].n = n*2+1;
00885          leds[led].interval = interval / 10;
00886          leds[led].timer = 0;
00887       }
00888    }
00889 }

void led_mode ( unsigned char  led,
unsigned char  flag 
)

00852          : led_mode
00853 
00854   Purpose: Set LED mode
00855 
00856   Input:
00857     int led               0 for primary, 1 for secondary
00858     int flag              Noninverted (0) / Inverted (1)
00859 
00860 \********************************************************************/
00861 {
00862    if (led < N_LED)
00863       leds[led].mode = flag;
00864 }

void led_set ( unsigned char  led,
unsigned char  flag 
)

00894 {
00895    /* invert on/off if mode == 1 */
00896    if (led < N_LED && leds[led].mode)
00897       flag = !flag;
00898 
00899 #ifdef LED_0
00900    if (led == 0)
00901       led_0 = flag;
00902 #endif
00903 #ifdef LED_1
00904    if (led == 1)
00905       led_1 = flag;
00906 #endif
00907 #ifdef LED_2
00908    if (led == 2)
00909       led_2 = flag;
00910 #endif
00911 #ifdef LED_3
00912    if (led == 3)
00913       led_3 = flag;
00914 #endif
00915 #ifdef LED_4
00916    if (led == 4)
00917       led_4 = flag;
00918 #endif
00919 #ifdef LED_5
00920    if (led == 5)
00921       led_5 = flag;
00922 #endif
00923 #ifdef LED_6
00924    if (led == 6)
00925       led_6 = flag;
00926 #endif
00927 #ifdef LED_7
00928    if (led == 7)
00929       led_7 = flag;
00930 #endif
00931 #ifdef LED_8
00932    if (led == 8)
00933       led_8 = flag;
00934 #endif
00935 #ifdef LED_9
00936    if (led == 9)
00937       led_9 = flag;
00938 #endif
00939 }

unsigned char ping ( unsigned short  addr  ) 

void rtc_conv_date ( unsigned char  d[6],
char *  str 
)

void rtc_conv_time ( unsigned char  d[6],
char *  str 
)

void rtc_init ( void   ) 

unsigned char rtc_present ( void   ) 

void rtc_print ( void   ) 

void rtc_read ( unsigned char  d[6]  ) 

void rtc_write ( unsigned char  d[6]  ) 

void rtc_write_byte ( unsigned char  adr,
unsigned char  d 
)

void rtc_write_item ( unsigned char  item,
unsigned char  d 
)

char scs_lcd1_read (  ) 

void send_remote_var ( unsigned char  i  ) 

void set_n_sub_addr ( unsigned char  n  ) 

void setup_variables ( void   ) 

void sysclock_init ( void   ) 

00742          : sysclock_init
00743 
00744   Purpose: Initial sytem clock via timer 0
00745 
00746 *********************************************************************/
00747 {
00748    EA = 1;                      // general interrupt enable
00749    ET0 = 1;                     // Enable Timer 0 interrupt
00750    PT1 = 0;                     // Interrupt priority low
00751 
00752 #ifdef CPU_C8051F120
00753    SFRPAGE = TIMER01_PAGE;
00754 #endif
00755 
00756    TMOD = (TMOD & 0x0F) | 0x01; // 16-bit counter
00757 #if defined(SCS_210)
00758    CKCON = 0x02;                // use SYSCLK/48
00759    TH0 = 0xEC;                  // load initial value (24.5 MHz SYSCLK)
00760 #elif defined(CPU_C8051F120)
00761    CKCON = 0x02;                // use SYSCLK/48 (98 MHz SYSCLK)
00762    TH0 = 0xAF;                  // load initial value
00763 #elif defined(CPU_C8051F310) || defined(CPU_C8051F410)
00764    CKCON = 0x00;                // use SYSCLK/12
00765    TH0 = 0xAF;                  // load initial value (24.5 MHz SYSCLK)
00766 #else
00767    CKCON = 0x00;                // use SYSCLK/12
00768    TH0 = 0xDB;                  // load initial value
00769 #endif
00770 
00771    TL0 = 0x00;
00772    TR0 = 1;                     // start timer 0
00773 
00774    sysclock_reset();
00775 }

void sysclock_reset ( void   ) 

00716          : sysclock_reset
00717 
00718   Purpose: Reset system clock and uptime counter
00719 
00720 *********************************************************************/
00721 {
00722    unsigned char i;
00723 
00724    _systime = 0;
00725    _uptime = 0;
00726    _uptime_cnt = 100;
00727 
00728    for (i=0 ; i<N_LED ; i++) {
00729      leds[i].mode = 0;
00730      leds[i].timer = 0;
00731      leds[i].interval = 0;
00732      leds[i].n = 0;
00733    }
00734 }

unsigned long time ( void   ) 

00946          : time
00947 
00948   Purpose: Return system time in units of 10ms
00949 
00950 \********************************************************************/
00951 {
00952    unsigned long t;
00953 
00954    DISABLE_INTERRUPTS;
00955    t = _systime;
00956    ENABLE_INTERRUPTS;
00957    return t;
00958 }

unsigned char uart1_receive ( char *  buffer,
int  size 
)

unsigned char uart1_send ( char *  buffer,
int  size,
unsigned char  bit9 
)

void uart_init ( unsigned char  port,
unsigned char  baud 
)

00447          : uart_init
00448 
00449   Purpose: Initialize serial interface, user Timer 1 for UART0 and
00450            (optionally) Timer 2 (4 for F020) for UART1
00451 
00452   Input:
00453     unsigned char baud
00454       1:    2400
00455       2:    4800
00456       3:    9600
00457       4:   19200
00458       5:   28800
00459       6:   38400
00460       7:   57600
00461       8:  115200
00462       9:  172800
00463      10:  345600
00464 
00465 \********************************************************************/
00466 {
00467 #if defined (CPU_C8051F310) || defined(CPU_C8051F410)          // 24.5 MHz
00468    unsigned char code baud_table[] =
00469      {0x100 - 0,    //  N/A
00470       0x100 - 0,    //  N/A
00471       0x100 - 0,    //  N/A
00472       0x100 - 0,    //  N/A
00473       0x100 - 0,    //  N/A
00474       0x100 - 0,    //  N/A
00475       0x100 - 213,  //  57600
00476       0x100 - 106,  // 115200
00477       0x100 - 71,   // 172800
00478       0x100 - 35 }; // 345600
00479 #elif defined(CPU_C8051F320)          // 12 MHz
00480    unsigned char code baud_table[] =
00481      {0x100 - 0,    //  N/A
00482       0x100 - 0,    //  N/A
00483       0x100 - 0,    //  N/A
00484       0x100 - 0,    //  N/A
00485       0x100 - 208,  //  28800
00486       0x100 - 156,  //  38400
00487       0x100 - 104,  //  57600
00488       0x100 - 52,   // 115200
00489       0x100 - 35,   // 172800  2% error
00490       0x100 - 17 }; // 345600  2% error
00491 #elif defined(SCS_210)                // 24.5 MHz
00492    unsigned char code baud_table[] =  // UART0 via timer 2
00493      {0x100 - 0,    //  N/A
00494       0x100 - 0,    //  N/A
00495       0x100 - 160,  //   9600  0.3% error
00496       0x100 - 80,   //  19200  0.3% error
00497       0x100 - 53,   //  28800  0.3% error
00498       0x100 - 40,   //  38400  0.3% error
00499       0x100 - 27,   //  57600  1.5% error
00500       0x100 - 13,   // 115200  2.2% error
00501       0x100 - 9,    // 172800  1.5% error
00502       0x100 - 0 };  // N/A
00503    unsigned char code baud_table1[] = // UART1 via timer 1
00504      {0x100 - 106,  //   2400  0.3% error
00505       0x100 - 53,   //   4800  0.3% error
00506       0x100 - 27,   //   9600  1.5% error
00507       0x100 - 13,   //  19200  2.2% error
00508       0x100 - 9,    //  28800  1.5% error
00509       0x100 - 7,    //  38400  5.1% error
00510       0x100 - 0,    //  N/A
00511       0x100 - 0,    //  N/A
00512       0x100 - 0,    //  N/A
00513       0x100 - 0};   //  N/A
00514 #elif defined(SUBM_260)                // 49 MHz
00515    unsigned char code baud_table[] =  // UART0 via timer 2
00516      {0xFB, 0x100 - 252,  //   2400
00517       0xFD, 0x100 - 126,  //   4800
00518       0xFE, 0x100 - 63,   //   9600
00519       0xFF, 0x100 - 160,  //  19200  0.3% error
00520       0xFF, 0x100 - 106,  //  28800  0.3% error
00521       0xFF, 0x100 - 80,   //  38400  0.3% error
00522       0xFF, 0x100 - 53,   //  57600  0.3% error
00523       0xFF, 0x100 - 27,   // 115200  1.5% error
00524       0xFF, 0x100 - 18,   // 172800  1.5% error
00525       0xFF, 0x100 - 9 };  // 345600  1.5% error
00526 #elif defined(CPU_C8051F120)          // 98 MHz
00527    unsigned char code baud_table[] =  // UART0 via timer 2
00528      {0x100 - 0,    //  N/A
00529       0x100 - 0,    //  N/A
00530       0x100 - 0,    //  N/A
00531       0x100 - 0,    //  N/A
00532       0x100 - 213,  //  28800  0.2% error
00533       0x100 - 160,  //  38400  0.3% error
00534       0x100 - 106,  //  57600  0.3% error
00535       0x100 - 53,   // 115200  0.3% error
00536       0x100 - 35,   // 172800  1.3% error
00537       0x100 - 18 }; // 345600  1.6% error
00538 #if defined(UART1_MSCB) || defined(UART1_DEVICE)
00539    unsigned char code baud_table1[] = // UART1 via timer 1
00540      {0x100 - 0,    //  N/A
00541       0x100 - 212,  //   4800  0.3% error
00542       0x100 - 106,  //   9600  0.3% error
00543       0x100 - 53,   //  19200  0.3% error
00544       0x100 - 35,   //  28800  1.3% error
00545       0x100 - 27,   //  38400  1.6% error
00546       0x100 - 18,   //  57600  1.6% error
00547       0x100 - 9,    // 115200  1.6% error
00548       0x100 - 6,    // 172800  1.6% error
00549       0x100 - 3 };  // 345600  1.6% error
00550 #endif
00551 #else                                 // 11.0592 MHz
00552    unsigned char code baud_table[] =
00553      {0x100 - 144,  //   2400
00554       0x100 - 72,   //   4800
00555       0x100 - 36,   //   9600
00556       0x100 - 18,   //  19200
00557       0x100 - 12,   //  28800
00558       0x100 - 9,    //  38400
00559       0x100 - 6,    //  57600
00560       0x100 - 3,    // 115200
00561       0x100 - 2,    // 172800
00562       0x100 - 1 };  // 345600
00563 #endif
00564 
00565    if (port == 0) { /*---- UART0 ----*/
00566 
00567 #ifdef CPU_C8051F120
00568       SFRPAGE = UART0_PAGE;
00569 #endif
00570 
00571       SCON0 = 0xD0;                // Mode 3, 9 bit, receive enable
00572 
00573 #ifdef CPU_C8051F120
00574 
00575       SFRPAGE = UART0_PAGE;
00576       SSTA0 = 0x15;                // User Timer 2 for baud rate, div2 disabled
00577 
00578       SFRPAGE = TMR2_PAGE;
00579       TMR2CF = 0x08;               // use system clock for timer 2
00580 #ifdef SUBM_260
00581       RCAP2H = baud_table[(baud - 1)*2];    // load high byte
00582       RCAP2L = baud_table[(baud - 1)*2+1];  // load low byte
00583 #else
00584       RCAP2H = 0xFF;
00585       RCAP2L = baud_table[baud - 1];
00586 #endif
00587       TMR2CN = 0x04;               // start timer 2
00588       SFRPAGE = UART0_PAGE;
00589 
00590 #else // CPU_C8051F120
00591 
00592       TMOD  = (TMOD & 0x0F)| 0x20; // Timer 1 8-bit counter with auto reload
00593 
00594 #if defined(CPU_C8051F310) || defined(CPU_C8051F320) || defined(CPU_C8051F410)
00595       CKCON |= 0x08;               // use system clock
00596 #else
00597       T2CON &= ~30;                // User Timer 1 for baud rate
00598       CKCON |= 0x10;               // use system clock
00599 #endif
00600 
00601       TH1 = baud_table[baud - 1];  // load initial values
00602       TR1 = 1;                     // start timer 1
00603 
00604 #endif // CPU_C8051F120
00605 
00606       ES0 = 1;                     // enable serial interrupt
00607       PS0 = 0;                     // serial interrupt low priority
00608 
00609 
00610 #if defined(UART1_MSCB) || defined(UART1_DEVICE)
00611 
00612    } else { /*---- UART1 ----*/
00613 
00614 #if defined(CPU_C8051F020)
00615       SCON1 = 0x50;                // Mode 1, 8 bit, receive enable
00616 
00617       T4CON = 0x34;                // timer 4 RX+TX mode
00618       RCAP4H = 0xFF;
00619       RCAP4L = baud_table[baud - 1];
00620 
00621       EIE2 |= 0x40;                // enable serial interrupt
00622       EIP2 &= ~0x40;               // serial interrupt low priority
00623 
00624 
00625 #elif defined(SCS_210)             // 24.5 MHz
00626       SFRPAGE = UART1_PAGE;
00627       SCON1 = 0x50;                // Mode 1, 8 bit, receive enable
00628 
00629       SFRPAGE = TIMER01_PAGE;
00630       TMOD  = (TMOD & 0x0F)| 0x20; // Timer 1 8-bit counter with auto reload
00631       CKCON = 0x02;                // use SYSCLK/48 (needed by timer 0)
00632 
00633       TH1 = baud_table1[baud - 1];
00634       TR1 = 1;                     // start timer 1
00635 
00636       EIE2 |= 0x40;                // enable serial interrupt
00637       EIP2 &= ~0x40;               // serial interrupt low priority
00638 #elif defined(CPU_C8051F120)       // 98 MHz
00639       SFRPAGE = UART1_PAGE;
00640 #ifdef UART1_MSCB
00641       SCON1 = 0xD0;                // Mode 3, 9 bit, receive enable
00642 #else
00643       SCON1 = 0x50;                // Mode 1, 8 bit, receive enable
00644 #endif
00645 
00646       SFRPAGE = TIMER01_PAGE;
00647       TMOD  = (TMOD & 0x0F)| 0x20; // Timer 1 8-bit counter with auto reload
00648       CKCON = 0x02;                // use SYSCLK/48 (needed by timer 0)
00649 
00650       TH1 = baud_table1[baud - 1];
00651       TR1 = 1;                     // start timer 1
00652 
00653       EIE2 |= 0x40;                // enable serial interrupt
00654       EIP2 |= 0x40;                // serial interrupt high priority, needed in order not
00655                                    // to loose data during UART0 communication
00656 #endif
00657 
00658       uart1_init_buffer();
00659 #endif // defined(UART1_MSCB) || defined(UART1_DEVICE)
00660    }
00661 
00662    EA = 1;                         // general interrupt enable
00663 }

unsigned long uptime ( void   ) 

00965          : uptime
00966 
00967   Purpose: Return system uptime in seconds
00968 
00969 \********************************************************************/
00970 {
00971    unsigned long t;
00972 
00973    DISABLE_INTERRUPTS;
00974    t = _uptime;
00975    ENABLE_INTERRUPTS;
00976    return t;
00977 }

unsigned char user_func ( unsigned char *  data_in,
unsigned char *  data_out 
)

00319 {
00320    /* echo input data */
00321    data_out[0] = data_in[0];
00322    data_out[1] = data_in[1];
00323    return 2;
00324 }

void watchdog_disable ( void   ) 

01099          : watchdog_disable
01100 
01101   Purpose: Disables watchdog
01102 
01103 \********************************************************************/
01104 {
01105 #ifdef USE_WATCHDOG
01106    watchdog_on = 0;
01107    watchdog_timer = 0;
01108 #endif
01109 
01110 #if defined(CPU_C8051F310) || defined(CPU_C8051F320) || defined(CPU_C8051F410)
01111    PCA0MD = 0x00;
01112 #else
01113    WDTCN  = 0xDE;
01114    WDTCN  = 0xAD;
01115 #endif
01116 }

void watchdog_enable ( unsigned char  watchdog_timeout  ) 

01039          : watchdog_enable
01040 
01041   Purpose: Enables watchdog
01042 
01043   Input:
01044     unsigned char timeout    Watchdog timeout in seconds
01045 
01046 \********************************************************************/
01047 {
01048    if (timeout);
01049 #ifdef USE_WATCHDOG
01050 
01051    watchdog_on = 1;
01052    watchdog_timer = 0;
01053    if (timeout)
01054       watchdog_timeout = timeout;
01055    else
01056       watchdog_timeout = DEFAULT_WATCHDOG_TIMEOUT;
01057 
01058 #ifndef EXT_WATCHDOG
01059 #if defined(CPU_C8051F310) || defined(CPU_C8051F320) || defined(CPU_C8051F410)
01060 #if defined(CPU_C8051F410)
01061    PCA0MD   = 0x00;             // disable watchdog
01062    PCA0CPL5 = 255;              // 65.5 msec @ 12 MHz
01063    PCA0MD   = 0x40;             // enable watchdog
01064    PCA0CPH5 = 0x00;             // reset watchdog
01065 
01066    RSTSRC   = 0x06;             // enable missing clock detector and 
01067 #else                                // VDD monitor as reset source
01068    PCA0MD   = 0x00;             // disable watchdog
01069    PCA0CPL4 = 255;              // 65.5 msec @ 12 MHz
01070    PCA0MD   = 0x40;             // enable watchdog
01071    PCA0CPH4 = 0x00;             // reset watchdog
01072 
01073    RSTSRC   = 0x06;             // enable missing clock detector and 
01074                                 // VDD monitor as reset source
01075 #endif
01076 #else /* CPU_C8051F310 */
01077 
01078    WDTCN    = 0x07;             // 95 msec (11.052 MHz) / 21msec (49 MHz)
01079    WDTCN    = 0xA5;             // start watchdog
01080 
01081 #if defined(CPU_C8051F120)
01082    SFRPAGE = LEGACY_PAGE;
01083    RSTSRC  = 0x06;              // enable missing clock detector & VDDMON
01084 #else
01085    OSCICN |= 0x80;              // enable missing clock detector
01086    RSTSRC  = 0x0B;              // enable reset pin and watchdog reset & VDDMON
01087 #endif
01088 
01089 #endif /* not CPU_C8051F310 */
01090 #endif /* not EXT_WATCHDOG */
01091 #endif /* USE_WATCHDOG */
01092 }

void watchdog_refresh ( unsigned char  from_interrupt  ) 

00991          : watchdog_refresh
00992 
00993   Purpose: Resets watchdog, has to be called regularly, otherwise
00994            the watchdog issues a reset. If called from an interrupt
00995            routine, just reset the hardware watchdog, but not the
00996            watchdog timer. Reset the watchdog timer only when called
00997            from the user loop, to ensure that the main user loop
00998            is running.
00999 
01000   Input:
01001     unsigned char from_interrupt  0 if called from normal user loop,
01002                                   1 if called from interrupt routine
01003 
01004 
01005 \********************************************************************/
01006 {
01007    if (from_interrupt);
01008 
01009 #ifdef USE_WATCHDOG
01010    if (from_interrupt == 0)
01011       watchdog_timer = 0;
01012 
01013    if (watchdog_on && watchdog_timer < watchdog_timeout*100) {
01014 
01015 #ifdef EXT_WATCHDOG
01016       EXT_WATCHDOG_PIN = !EXT_WATCHDOG_PIN;
01017 #else
01018 #if defined(CPU_C8051F310) || defined(CPU_C8051F320) || defined(CPU_C8051F410)
01019 #if defined(CPU_C8051F410)
01020       PCA0CPH5 = 0x00;
01021 #else
01022       PCA0CPH4 = 0x00;
01023 #endif
01024 #else
01025       WDTCN = 0xA5;
01026 #endif
01027 #endif // EXT_WATCHDOG
01028 
01029    }
01030    
01031 #endif
01032 }

void yield ( void   ) 

01497 {
01498    watchdog_refresh(0);
01499 
01500    /* output RS232 data if present */
01501 #if defined(UART1_DEVICE)
01502    rs232_output();
01503 #endif
01504    /* blink LED if not configured */
01505    if (!configured_addr)
01506       led_blink(0, 1, 50);
01507 
01508    /* blink LED if wrong CPU */
01509    if (wrong_cpu)
01510       led_blink(0, 1, 30);
01511 
01512    /* flash EEPROM if asked by interrupt routine, wait 3 sec
01513       after reboot (power might not be stable) */
01514    if (flash_param && flash_allowed) {
01515       led_blink(_cur_sub_addr, 1, 50);
01516 
01517       flash_param = 0;
01518 
01519       eeprom_flash(); 
01520       configured_addr = 1;
01521    }
01522 
01523    /* flash EEPROM if variables just got initialized */
01524    if (!configured_vars && flash_allowed) {
01525       _flkey = 0xF1;
01526       eeprom_flash();
01527       configured_vars = 1;
01528    }
01529 
01530    if (flash_program && flash_allowed) {
01531       flash_program = 0;
01532 
01533 #ifdef HAVE_LCD
01534       lcd_clear();
01535       lcd_goto(0, 0);
01536       puts("    Upgrading"); 
01537       lcd_goto(0, 1);
01538       puts("    Firmware...");
01539 #endif
01540 
01541       /* go to "bootloader" program */
01542       upgrade();
01543    }
01544 
01545 #ifdef HAVE_RTC
01546    if (rtc_set) {
01547       rtc_write(rtc_bwrite);
01548       rtc_set = 0;
01549    }
01550 
01551    if (time() > rtc_last+90 || time() < rtc_last) {
01552       rtc_last = time();
01553       rtc_read(rtc_bread);
01554    }
01555 #endif
01556 
01557    /* allow flash 3 sec after reboot */
01558    if (!flash_allowed && time() > 300)
01559       flash_allowed = 1;
01560 
01561 }


Generated on 10 Jun 2013 for FGDC8051 by  doxygen 1.4.7