Changeset 37 for webserver/example/EnergyMeters/Source
- Timestamp:
- 01/31/10 12:44:23 (15 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
webserver/example/EnergyMeters/Source/EnergyMeters/Meters.c
r30 r37 36 36 vSemaphoreCreateBinary( xMetersSemaphore ); 37 37 38 Init_P2_0(); /* init GPIO for meters */ 38 //Init_P2_0(); /* init GPIO for meters */ 39 // TODO: put this in again, right now it interferes with Solar UART3! 40 39 41 40 42 for( ;; ) 41 43 { 42 44 45 /* this runs every 1000 ms */ 46 43 47 basementGasCalculation(); 48 49 50 44 51 45 52 portENTER_CRITICAL(); 46 53 // example: do stuff 54 55 47 56 portEXIT_CRITICAL(); 48 57 … … 79 88 80 89 void vP2_0_ISR_Wrapper( void ); 81 82 // configure port-pins for use with LAN-controller,83 // reset it and send the configuration-sequence84 90 85 91 portBASE_TYPE Init_P2_0(void) webserver/example/EnergyMeters/Source/EnergyMeters/SolarCountUART.c
r35 r37 2 2 3 3 #include "LPC23xx.h" 4 5 6 #include "FreeRTOS.h" 7 #include "semphr.h" 8 #include "task.h" 9 #include "serial.h" 10 11 #define solarSTACK_SIZE configMINIMAL_STACK_SIZE+200 12 13 #define SOLAR_READ_DELAY 500 4 14 5 15 // Pin I/O LED Control Maskbit … … 17 27 #define SOL_CHAN 2 /* querying channel currentChannel */ 18 28 #define SOL_CHAN_WAIT_REPLY 3 /* waiting for reply on channel currentChannel = 0; */ 19 #define SOL_FINISHED 10/* finished */29 #define SOL_FINISHED 0xA /* finished */ 20 30 //#define SOL_ 21 31 //#define SOL_ 22 32 23 33 34 // UART/FIFO Register Bit Masks 35 36 #define UIIR_PEND 1U 37 #define UIIR_RLS_INT (3U << 1) 38 #define UIIR_RDA_INT (2U << 1) 39 #define UIIR_CTI_INT (6U << 1) 40 #define UIIR_THRE_INT (1U << 1) 41 42 #define ULSR_RDR 1U 43 44 45 /* Handle to the com port (UART3) */ 46 xComPortHandle xPort; 47 48 unsigned int uart3TxRunning = 0; /* flag to tell if we are currently sending a datagram */ 49 50 24 51 unsigned int rxBuf3NextFreeRxPos = 0; 25 52 … … 27 54 28 55 static int solarState = SOL_NO_INIT; /* state of the SolarCount communication state machine */ 56 57 58 portTickType xReadRate, xLastReadTime; 29 59 30 60 … … 97 127 void TXD_RS485(void) 98 128 { 99 delay(500000);129 //delay(500000); 100 130 IOSET1 = DIR_RS485; // RS485 Direction = 1 (Transmit) 101 131 } /* TXD_RS485 */ 102 132 103 133 104 105 void testUART3(void) 106 { 107 int k; 108 int RxBuf; // one rx char 109 int uart_data; // Character Receive Buffer 110 k = 0; 111 112 134 void initSolarFSM(void) 135 { 136 xLastReadTime = xTaskGetTickCount(); 113 137 solarState = SOL_NO_INIT; 114 while (1) 115 { 116 117 solarFSM(); 118 119 if (solarState == SOL_FINISHED) 120 { 121 solarState = SOL_NO_INIT; 122 } /* if */ 123 } /* while */ 124 125 } 138 init_serial3(); 139 } /* initSolarFSM */ 140 141 126 142 127 143 /* Clear Uart3 RxBuf and pointer to next Rx element */ … … 140 156 void getUart3RxData(void) 141 157 { 142 158 signed portBASE_TYPE byteReceived; /* did we receive a byte? */ 159 160 const portTickType xBlockTime = ( portTickType ) 0; /* ( portTickType ) 0xffff / portTICK_RATE_MS; */ 143 161 unsigned char RxBuf; // one rx char, or 0x100 if no data 144 unsigned char rxTrue; // did we receive a byte?162 //unsigned char rxTrue; // did we receive a byte? 145 163 146 164 while (rxBuf3NextFreeRxPos + 1 < RX_UART3_BUF_SIZE) 147 165 { 148 /* while not overran */ 149 150 RxBuf = getchar3(&rxTrue); 151 if (rxTrue == 1) 166 /* while not overrun */ 167 168 //RxBuf = getchar3(&rxTrue); 169 170 byteReceived = xSerialGetChar( xPort, &RxBuf, xBlockTime ); 171 172 173 if ((rxBuf3NextFreeRxPos == 0) && (byteReceived == pdTRUE) && (RxBuf == 0x00)) 174 { 175 /* we are at position 0, and we have received a 0x00 byte. ignore this leading 0. */ 176 ; /* do nothing */ 177 } 178 else if (byteReceived == pdTRUE) 152 179 { 153 180 // if data available … … 168 195 int i; 169 196 170 if (n > RX_UART3_BUF_SIZE) 171 { 172 /* n too large */ 173 resetUart3RxBuf (); 174 return; 175 } 197 // for now, clear the whole buffer 198 resetUart3RxBuf(); 199 200 201 // begin quickfix (TODO) 202 203 204 205 //if (n > RX_UART3_BUF_SIZE) 206 //{ 207 // /* n too large */ 208 209 210 //resetUart3RxBuf (); 211 212 213 214 // return; 215 //} 176 216 /* move end part of the buffer (after n) to front */ 177 for (i = 0; i < n; i++)178 {179 rxUART3[i] = rxUART3[i+n]; // move byte180 }217 //for (i = 0; i < n; i++) 218 //{ 219 // rxUART3[i] = rxUART3[i+n]; // move byte 220 //} 181 221 /* write 0 to now unused elements */ 182 for (i = n; i < RX_UART3_BUF_SIZE; i++) 183 { 184 rxUART3[i] = 0; // clear byte 185 } 222 //for (i = n; i < RX_UART3_BUF_SIZE; i++) 223 //{ 224 // rxUART3[i] = 0; // clear byte 225 //} 226 227 /* clear all elements */ 228 229 230 231 186 232 /* adjust rxBuf3NextFreeRxPos */ 187 if (n >= rxBuf3NextFreeRxPos) 188 { 189 /* remove more elements than are in the buffer */ 190 rxBuf3NextFreeRxPos = 0; // buffer empty 191 } 192 else 193 { 194 rxBuf3NextFreeRxPos -= n; 195 } 196 } /* chopUart3RxBuf */ 233 //if (n >= rxBuf3NextFreeRxPos) 234 //{ 235 // /* remove more elements than are in the buffer */ 236 // rxBuf3NextFreeRxPos = 0; // buffer empty 237 //} 238 //else 239 //{ 240 // rxBuf3NextFreeRxPos -= n; 241 //} 242 // end quickfix (TODO) 243 244 }/* chopUart3RxBuf */ 197 245 198 246 … … 263 311 int cmpResult; 264 312 313 265 314 /* always get Rx data */ 266 315 getUart3RxData(); … … 269 318 { 270 319 case SOL_NO_INIT: 271 delay(500000);272 init_serial3();273 320 resetUart3RxBuf(); /* init */ 274 321 currentChannel = 0; /* the channel we query first */ 275 322 276 TXD_RS485();277 delay(500000);278 323 /* send init 1 */ 279 324 send_uart3((unsigned char *)init1, sizeof(init1)); 280 RXD_RS485();281 325 solarState = SOL_SENT_INIT; /* now wait for reply */ 282 326 break; … … 310 354 /* send query for channel number currentChannel */ 311 355 312 TXD_RS485();356 vTaskDelayUntil( &xLastReadTime, SOLAR_READ_DELAY ); 313 357 switch ( currentChannel ) 314 358 { … … 338 382 ; /* do nothing */ 339 383 } /* switch */ 340 RXD_RS485();341 384 if (solarState != SOL_FINISHED) 342 385 { … … 395 438 396 439 /* finally, discard data from buffer */ 397 resetUart3RxBuf();440 398 441 399 442 /* switch to next channel */ … … 408 451 } 409 452 453 resetUart3RxBuf(); 454 410 455 } 411 456 else … … 425 470 426 471 427 /********************************/ 428 /* Initial UART3 = 115200,N,8,1 */ 429 /********************************/ 472 430 473 void init_serial3 (void) 431 474 { 475 432 476 unsigned int baud = 57600; 433 477 unsigned int divisor = get_uart_clk(3, OSCILLATOR_CLOCK_FREQUENCY) / (16 * baud); 434 478 435 436 479 int i; 437 480 … … 441 484 } 442 485 486 /* init serial port */ 487 xPort = xSerialPortInitMinimal( baud /* baud rate */, 80 /* buffer length */ ); 488 //vStartComTestTasks(); 443 489 444 490 // xxxx xxxx xxxx xxxx xxxx xxxx xxxx 1010 … … 453 499 PINSEL3 &= 0xFFFF3FFF; // P1.19 = GPIO 454 500 IODIR1 = DIR_RS485; // Pin Control Direction RS485 = Output 455 RXD_RS485(); // Default RS485 Direction501 //RXD_RS485(); // Default RS485 Direction 456 502 457 503 //PCLKSEL1 = 0; /* UART3 frequency */ … … 474 520 475 521 // Baud Rate Calculator: http://prototalk.net/forums/showthread.php?t=11 476 477 #if 0478 U3LCR = 0x83; // 8 bits, no Parity, 1 Stop bit479 U3DLL = 0x0C; // Baud 57600BPS for 12MHz PCLK Clock480 U3FDR = 0xC1; // Fractional Divider (0x67 = 115200 , 0xCE = 57600 ????),481 U3DLM = 0;482 483 U3LCR = 0x03; // DLAB = 0484 #endif485 486 522 487 523 … … 496 532 void send_uart3(char * data, int length) 497 533 { 498 int i = 0; // Pointer Buffer 499 500 TXD_RS485(); // Swap RS485 Direction = Transmit 501 delay(50000); 502 503 504 for (i = 0; i < length-1; i++) // Get char & Print Until null 505 { 534 535 536 int i = 0; // Pointer Buffer 537 538 TXD_RS485(); 539 //delay(50000); 540 541 uart3TxRunning = 1; // sending in progress 542 for (i = 0; i < length-1; i++) // Get char & Print Until null 543 { 544 506 545 /* go only through length-1 because we do not send the terminating \x00 of the c-string in data */ 507 putchar3(data[i]); // Write char to UART // Next char546 putchar3(data[i]); // Write char to UART 508 547 } 509 510 delay(50000); 511 RXD_RS485(); // Swap RS485 Direction = Receive 548 uart3TxRunning = 0; // done sending this datagram. The UART interrupt can switch to Rx as soon as FIFO is empty 549 550 551 // we send directly. unused. 552 // vSerialPutString( xPort, &(data[0]), length-1 ); 553 512 554 513 555 return; … … 555 597 } 556 598 599 600 601 static portTASK_FUNCTION( vSolar_Task, pvParameters ) 602 { 603 604 605 initSolarFSM(); 606 607 for( ;; ) 608 { 609 610 611 612 //portENTER_CRITICAL(); 613 614 solarFSM(); 615 616 //portEXIT_CRITICAL(); 617 618 619 if (solarState == SOL_FINISHED) 620 { 621 solarState = SOL_NO_INIT; 622 } /* if */ 623 624 } 625 } 626 627 628 629 void vStartSolarTask( unsigned portBASE_TYPE uxPriority ) 630 { 631 632 /* Spawn the task. */ 633 xTaskCreate( vSolar_Task, ( signed portCHAR * ) "Solar", solarSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL ); 634 } 635