1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
|
//-----------------------------------------------------------------------------
// F12x_Init.c
//-------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "global.h"
#if(MCU == F120)
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F12x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 RCAP3 = 0xca; // Timer3 capture/reload
sfr16 RCAP4 = 0xca; // Timer4 capture/reload
sfr16 TMR2 = 0xcc; // Timer2
sfr16 TMR3 = 0xcc; // Timer3
sfr16 TMR4 = 0xcc; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd2; // DAC1 data
sfr16 PCA0CP5 = 0xe1; // PCA0 Module 5 capture
sfr16 PCA0CP2 = 0xe9; // PCA0 Module 2 capture
sfr16 PCA0CP3 = 0xeb; // PCA0 Module 3 capture
sfr16 PCA0CP4 = 0xed; // PCA0 Module 4 capture
sfr16 PCA0 = 0xf9; // PCA0 counter
sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 capture
sfr16 PCA0CP1 = 0xfd; // PCA0 Module 1 capture
//-----------------------------------------------------------------------------
// Local Constants
//-----------------------------------------------------------------------------
#define SYSCLK 98000000 // System Clock in Hz
//-----------------------------------------------------------------------------
// Local Global Variables
//-----------------------------------------------------------------------------
static unsigned int timeout;
//-----------------------------------------------------------------------------
// Exported Function Prototypes
//-----------------------------------------------------------------------------
void Reset_Init(void);
void wait_ms(int ms);
void clear_ms_timer_flag(void);
char get_ms_timer_flag(void);
void start_ms_timer();
void CP220x_RST_Low(void);
void CP220x_RST_High(void);
unsigned char AB4_RST_State(void);
#if(UART_ENABLED)
char _getkey ();
char putchar (char c);
#endif
//-----------------------------------------------------------------------------
// Local Function Prototypes
//-----------------------------------------------------------------------------
void PORT_Init (void);
void SYSCLK_Init (void);
void EMIF_Init (void);
#if(UART_ENABLED)
void UART1_Init (void);
void vBufferClear( void );// ajouter par moi
#endif
//-----------------------------------------------------------------------------
// Exported Functions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Reset_Init
//-----------------------------------------------------------------------------
void Reset_Init(void)
{
// Enable the VDD Monitor as a reset source
// Do not use read-modify write instruction on this register
RSTSRC = 0x02;
// Disable Watchdog Timer
WDTCN = 0xde;
WDTCN = 0xad;
// Initialize the MCU
PORT_Init();
SYSCLK_Init();
EMIF_Init();
EX0 = 1; // Enable External Interrupt 0
#if(UART_ENABLED)
UART1_Init();
puts("\n*** Reset ***\nC8051F12x MCU Initialized\n");
#endif
}
//-----------------------------------------------------------------------------
// External Interrupt 0
//-----------------------------------------------------------------------------
//
// This interrupt service routine is routed to the ethernet interrupt
//
void INT0_ISR(void) interrupt 0
{
Ethernet_ISR();
}
//-----------------------------------------------------------------------------
// wait_ms
//-----------------------------------------------------------------------------
//
// This routine inserts a delay of <ms> milliseconds.
//
void wait_ms(int ms)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
TMR2CF = 0x00; // use SYSCLK/12 as timebase
RCAP2 = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
TMR2 = RCAP2;
ET2 = 0; // Disable Timer 2 interrupts
TR2 = 1; // Start Timer 2
while(ms){
TF2 = 0;
while(!TF2); // wait until timer overflows
ms--; // decrement ms
}
TR2 = 0; // Stop Timer 2
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//-----------------------------------------------------------------------------
// reset_timout
//-----------------------------------------------------------------------------
//
// This routine resets the global timeout.
//
void reset_timeout(unsigned int ms)
{
timeout = ms;
start_ms_timer();
}
//-----------------------------------------------------------------------------
// timeout_expired
//-----------------------------------------------------------------------------
//
// This routine manages the timeout and returns TRUE if timeout expired.
//
unsigned char timeout_expired(void)
{
if(get_ms_timer_flag()){
clear_ms_timer_flag();
if(timeout > 0){
timeout--;
}
}
return (timeout == 0);
}
//-----------------------------------------------------------------------------
// start_ms_timer
//-----------------------------------------------------------------------------
//
// This routine starts a timer with a 1kHz overflow rate (1ms period).
//
void start_ms_timer()
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
TMR2CF = 0x00; // use SYSCLK/12 as timebase
RCAP2 = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
TMR2 = RCAP2;
ET2 = 0; // Disable Timer 2 interrupts
TR2 = 1; // Start Timer 2
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//-----------------------------------------------------------------------------
// get_ms_timer_flag()
//-----------------------------------------------------------------------------
//
// This routine returns the state of the ms_timer overflow flag.
//
char get_ms_timer_flag(void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE;
return TF2;
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//-----------------------------------------------------------------------------
// clear_ms_timer_flag()
//-----------------------------------------------------------------------------
//
// This routine returns the state of the ms_timer overflow flag.
//
void clear_ms_timer_flag(void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE;
TF2 = 0;
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//-----------------------------------------------------------------------------
// CP220x_RST_Low
//-----------------------------------------------------------------------------
//
// Drives the CP220x's Reset Pin Low.
//
void CP220x_RST_Low(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P4 &= ~0x20; // Set P4.5 Low
SFRPAGE = SFRPAGE_SAVE;
}
//-----------------------------------------------------------------------------
// CP220x_RST_High
//-----------------------------------------------------------------------------
//
// Drives the CP220x's Reset Pin High.
//
void CP220x_RST_High(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P4 |= 0x20; // Set P4.5 High
SFRPAGE = SFRPAGE_SAVE;
}
//-----------------------------------------------------------------------------
// AB4_RST_State
//-----------------------------------------------------------------------------
//
// Returns the state of the AB4's reset pin.
//
unsigned char AB4_RST_State(void)
{
char rst_pin_state;
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
rst_pin_state = P4 & 0x20; // Get P4.5 State
SFRPAGE = SFRPAGE_SAVE;
return rst_pin_state;
}
//-----------------------------------------------------------------------------
// Local Initialization Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure UART1, Interrupts, Crossbar and GPIO ports
//
void PORT_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
P0MDOUT |= 0x01; // set TX1 to push-pull
P1MDOUT |= 0x40; // Set P1.6(TB_LED) to push-pull
P2MDOUT |= 0x0C; // Set P2.2(AB4_LED1) and P2.3(AB4_LED2)
// to push-pull
// all pins used by the external memory interface are in push-pull mode
P4MDOUT = 0xC0;
P5MDOUT = 0xFF;
P6MDOUT = 0xFF;
P7MDOUT = 0xFF;
P4 = 0xDF; // /WR, /RD, are high, RESET is low
P5 = 0xFF;
P6 = 0xFF; // P5, P6 contain the address lines
P7 = 0xFF; // P7 contains the data lines
TCON &= ~0x01; // Make /INT0 level triggered
// Enable UART0, CP0, and /INT0. This puts /INT0 on P0.3.
XBR0 = 0x80;
XBR1 = 0x04;
XBR2 = 0x44;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock.
//
void SYSCLK_Init (void)
{
int i; // software timer
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
OSCICN = 0x83; // set internal oscillator to run
// at its maximum frequency
CLKSEL = 0x00; // Select the internal osc. as
// the SYSCLK source
//Turn on the PLL and increase the system clock by a factor of M/N
PLL0CN = 0x00; // Set internal osc. as PLL source
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x30; // Set FLASH read time for 100 MHz clk
SFRPAGE = CONFIG_PAGE;
PLL0CN |= 0x01; // Enable Power to PLL
PLL0DIV = 0x01; // Set Pre-divide value to N (N = 1)
PLL0MUL = 0x04; // Multiply SYSCLK by M (M=4)
PLL0FLT = 0x01; // Set the PLL filter register for
// a reference clock from 12.2 - 19.5 MHz
// and an output clock from 65 - 100 MHz
for (i=0; i < 256; i++) ; // Wait at least 5us
PLL0CN |= 0x02; // Enable the PLL
while(!(PLL0CN & 0x10)); // Wait until PLL frequency is locked
CLKSEL = 0x02; // Select PLL as SYSCLK source
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// EMIF_Init
//-----------------------------------------------------------------------------
//
// Configure the External Memory Interface for both on and off-chip access.
//
void EMIF_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = LEGACY_PAGE;
EMI0CF = 0xFB; // Split-mode (banked), non-multiplexed
// on P4 - P7
EMI0TC = 0xFF; // This constant may be modified
// according to SYSCLK to meet the
// timing requirements for the CP2200
EMI0CN = 0x20; // Page of XRAM accessed by EMIF
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//===============
// CONDITIONAL
//===============
#if(UART_ENABLED)
//-----------------------------------------------------------------------------
// SFRPAGE = UART1_PAGE;
//-----------------------------------------------------------------------------
//
// Configure the UART1 using Timer1, for <baudrate> and 8-N-1.
//
void UART1_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
EIP2 |= 0x40; // Make UART high priority
SCON1 = 0x10; // SCON1: mode 0, 8-bit UART, enable RX
SFRPAGE = TIMER01_PAGE;
TMOD &= ~0xF0;
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x01; // T1M = 0; SCA1:0 = 01
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00
} else {
// Adjust for truncation in special case
// Note: Additional cases may be required if the system clock is changed.
#if ((BAUDRATE == 115200) && (SYSCLK == 98000000))
TH1 = -((SYSCLK/BAUDRATE/2/48)+1);
#else
TH1 = -(SYSCLK/BAUDRATE/2/48);
#endif
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x02; // T1M = 0; SCA1:0 = 10
}
EIE2 = 0x40; // Enable UART1 interrupts
TL1 = TH1; // initialize Timer1
TR1 = 1; // start Timer1
SFRPAGE = UART1_PAGE;
TI1 = 1; // Indicate TX1 ready
vBufferClear();
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// _getkey// A partir d'ic partie du code rediger par moi//-----------------------------------------------------------------------------
//
// SFR Paged version of _getkey
//
#define MAX_CHAR 64;
char xdata UartRxBuffer [64];
unsigned int iPtrWr = 0;
unsigned int iPtrRd = 0;
unsigned char UART_Buffer_Size = 0;
unsigned char UART_Input_First = 0;
unsigned char UART_Output_First = 0;
unsigned char TX_Ready =1;
static char Byte;
//--------------------Interrupt managing---------------------------------------------
void UART1_Parse(void) //interrupt 20
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
char c = 0;
SFRPAGE = UART1_PAGE;
if(RI1)
{
if (iPtrWr != iPtrRd)
{
c = SBUF1;
UartRxBuffer[iPtrWr++] = c;
if (iPtrWr > 64)
{
iPtrWr = MAX_CHAR;
}
RI1 = 0;
}
else if ( (iPtrWr == iPtrRd) && (iPtrWr ==0) )
{
c = SBUF1;
UartRxBuffer[iPtrWr++] = c;
if (iPtrWr > 64)
{
iPtrWr = MAX_CHAR;
}
RI1 = 0;
}
}
return ;
}
//----------------------------------------------------------------------------------------------
// initialisation de mon buffer
void vBufferClear (void)
{
memset(&UartRxBuffer[0],sizeof(UartRxBuffer), 0 );
iPtrWr = 0;
iPtrRd = 0;
}
//---------------Gestion read----------------------------------------------
unsigned int uiGetHex(int iLun)
{
char c;
unsigned int value = 0;
do{
c = UartRxBuffer[iPtrRd++];
if(c>='0' && c<='9')
value *= 16, value += c - '0';
else if(c>='A' && c<='F')
value *= 16, value += c - 'A' + 10;
else
{
value = 0xFFFF;
return value;
}
--iLun;
} while( iLun >= 0);
return value;
}
//----------------------------------------------------------------------------------------------------
void SkipRaw(void)
{
while(UartRxBuffer[iPtrRd] <= '0' && iPtrRd<sizeof(UartRxBuffer)) iPtrRd++;
}
extern MACADDRESS xdata DESTMAC;
//----------------------------------------------------------------------------------
void CommandParse(void)
{
unsigned int Addr;
unsigned int Value;
char c;
UART1_Parse();
if(c) putchar(c);
if(c!=0x0d) return; // tout ce passe bien jusqu'a ce niveau mais il rentre pas dans le switch
iPtrRd = 0;
switch(UartRxBuffer[0])
{
case 'W':
iPtrRd++;
SkipRaw();
Addr = uiGetHex(4);
SkipRaw();
Value = uiGetHex(2);
MAC_Write(Addr, Value);
printf("\nOK\n");
vBufferClear();
break;
case 'R':
iPtrRd++;
SkipRaw();
Addr = uiGetHex(4);
Value = MAC_Read(Addr);
printf("\n%04x = %04x\n", Addr, Value);
vBufferClear();
break;
case 'T':
{
iPtrRd++;
SkipRaw();
CP220x_Send(&DESTMAC, UartRxBuffer, sizeof(UartRxBuffer) - iPtrRd, IP_PACKET);
printf("\nOK\n");
}
vBufferClear();
break;
default : printf("\nERROR\n");
break;
}
iPtrRd = 0;
}
/*
char _getkey () {
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
char c;
SFRPAGE = UART1_PAGE;
while (!RI1);
c = SBUF1;
RI1 = 0;
SFRPAGE = SFRPAGE_SAVE;
return ;
}
*/
//-----------------------------------------------------------------------------
// putchar
//-----------------------------------------------------------------------------
//
// SFR Paged version of putchar
char putchar (char c) {
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
// output CR
if (c == '\n') {
while (!TI1);
TI1 = 0;
SBUF1 = 0x0d;
}
// output character
while (!TI1);
TI1 = 0;
SBUF1 = c;
SFRPAGE = SFRPAGE_SAVE;
return (c);
}
#endif // UART_ENABLED
#endif // MCU == F120
//-----------------------------------------------------------------------------
// End Of File
//------------ |