from Cam Marshall original modbus

Dependencies:   mbed

Fork of Modbus by Cam Marshall

Files at this revision

API Documentation at this revision

Comitter:
stanley1228
Date:
Tue Mar 14 22:23:17 2017 +0800
Parent:
4:5000041d2dc2
Child:
6:c3eaa87a6b3e
Commit message:
1.change baud rate to 115200
2.add define enter critical section (__disable_Irq)
3.add define exit critical section (__enable_Irq)

Changed in this revision

Modbus/port.h Show annotated file Show diff for this revision Revisions of this file
Modbus/portserial.cpp Show annotated file Show diff for this revision Revisions of this file
Modbus/porttimer.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Modbus/port.h	Tue Mar 14 14:25:27 2017 +0800
+++ b/Modbus/port.h	Tue Mar 14 22:23:17 2017 +0800
@@ -24,13 +24,13 @@
 
 #include <assert.h>
 #include <inttypes.h>
-
+#include "mbed.h" //stanley
 #define INLINE                      //inline
 #define PR_BEGIN_EXTERN_C           //extern "C" {
 #define PR_END_EXTERN_C             //}
 
-#define ENTER_CRITICAL_SECTION( )   
-#define EXIT_CRITICAL_SECTION( )    
+#define ENTER_CRITICAL_SECTION( )   __disable_irq() //stanley
+#define EXIT_CRITICAL_SECTION( )    __enable_irq() //stanley
 
 typedef uint8_t BOOL;
 
--- a/Modbus/portserial.cpp	Tue Mar 14 14:25:27 2017 +0800
+++ b/Modbus/portserial.cpp	Tue Mar 14 22:23:17 2017 +0800
@@ -33,81 +33,25 @@
 /* ----------------------- static functions ---------------------------------*/
 static void prvvUARTTxReadyISR( void );
 static void prvvUARTRxISR( void );
-//static void prvvUARTISR( void );
+
 
 /* ----------------------- System Variables ---------------------------------*/
-//Serial pc(USBTX, USBRX);           // Cam - mbed USB serial port
 Serial ser3(PB_10, PC_5); //serial3
 extern Serial pc;
 static uint8_t grx_buf=0;
 
-
-//Ticker simISR;                      // Cam - mbed ticker
-                                    // we don't have the TX buff empty interrupt, so
-                                    // we just interrupt every 1 mSec and read RX & TX
-                                    // status to simulate the proper ISRs.
-
-//static BOOL RxEnable, TxEnable;     // Cam - keep a static copy of the RxEnable and TxEnable
-                                    // status for the simulated ISR (ticker)
-
-
 /* ----------------------- Start implementation -----------------------------*/
-//stanley
-//#define RX_BUFF_LENGTH 1
-//#define TX_BUFF_LENGTH 1
-//event_callback_t    serialRxCBEvent;
-//event_callback_t    serialTxCBEvent;
-
-//uint8_t rx_buf[RX_BUFF_LENGTH];
-//uint8_t tx_buf[TX_BUFF_LENGTH];
-
-
-//static void serialRxCBFn(int events)
-//{
-//    
-//   prvvUARTRxISR();
-//   
-//}
-//static void serialTxCBFn(int events)
-//{
-//    prvvUARTTxReadyISR();
-//   
-//}
-// Cam - This is called every 1mS to simulate Rx character received ISR and
-// Tx buffer empty ISR.
-//static void
-//prvvUARTISR( void )
-//{
-//    if (TxEnable)
-//        if(ser3.writeable())
-//            prvvUARTTxReadyISR();
-//            
-//    if (RxEnable)
-//        if(ser3.readable())
-//            prvvUARTRxISR();          
-//}
-
-
 void
 vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
 {
     /* If xRXEnable enable serial receive interrupts. If xTxENable enable
      * transmitter empty interrupts.
      */
-    //RxEnable = xRxEnable;
-    //TxEnable = xTxEnable;
+   
 
 	//stanley
-	if(xRxEnable)
-		pc.printf("xRxEnable\n");
-	else
-		pc.printf("xRxDisable\n");
-
-	if(xTxEnable)
-		pc.printf("xTxEnable\n");
-	else
-		pc.printf("xTxDisable\n");
-
+	ENTER_CRITICAL_SECTION( );
+	
 
 	ser3.attach(NULL); //close all
 	
@@ -120,7 +64,7 @@
 		while(!ser3.writeable());
 		prvvUARTTxReadyISR();
 	}		
-	
+	EXIT_CRITICAL_SECTION( );
 
 	//if(xTxEnable)
 		//¤£¥Î¦b³oÃä³]©wser3.write(tx_buf,TX_BUFF_LENGTH,serialTxCBEvent,SERIAL_EVENT_TX_COMPLETE);
@@ -135,19 +79,10 @@
 BOOL
 xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
 {
-    //simISR.attach_us(&prvvUARTISR,1000);    // Cam - attach prvvUARTISR to a 1mS ticker to simulate serial interrupt behaviour
-                                            // 1mS is just short of a character time at 9600 bps, so quick enough to pick
-                                            // up status on a character by character basis.
    
-	pc.printf("in Ini\n");
-	//stanley
-	//serialRxCBEvent.attach(serialRxCBFn);  
- //   serialTxCBEvent.attach(serialTxCBFn);
+	ser3.baud(ulBaudRate); //stanley
+	
 
-	//ser3.attach(&prvvUARTRxISR,Serial::RxIrq);
-    //ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq);
-	
-	
 	return TRUE;
 }
 
@@ -157,12 +92,8 @@
     /* Put a byte in the UARTs transmit buffer. This function is called
      * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
      * called. */
-    //ser3.putc( ucByte);
-	//pc.printf("p\n");
-	//pc.printf("%x",ucByte);
 
 	//stanley
-	//while(!ser3.writeable());
 	ser3.putc( ucByte);
 
     return TRUE;
@@ -174,7 +105,6 @@
     /* Return the byte in the UARTs receive buffer. This function is called
      * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
      */
-	//pc.printf("%x",grx_buf);
 	
 	*pucByte = grx_buf;//stanley
 
@@ -191,7 +121,6 @@
  */
 static void prvvUARTTxReadyISR( void )
 {
-	//pc.printf("TR\n");
     pxMBFrameCBTransmitterEmpty();
 }
 
@@ -202,8 +131,6 @@
  */
 static void prvvUARTRxISR( void )
 {
-	//pc.printf("in Rx_I\n");
-
 	if(ser3.readable())	//stanlely RX ISR must contain getc
 		grx_buf=ser3.getc();
 
--- a/Modbus/porttimer.cpp	Tue Mar 14 14:25:27 2017 +0800
+++ b/Modbus/porttimer.cpp	Tue Mar 14 22:23:17 2017 +0800
@@ -28,7 +28,7 @@
 /* ----------------------- Modbus includes ----------------------------------*/
 #include "mb.h"
 #include "mbport.h"
-
+extern DigitalOut testD8; //stanley
 /* ----------------------- static functions ---------------------------------*/
 static void prvvTIMERExpiredISR( void );
 
@@ -40,8 +40,7 @@
 BOOL
 xMBPortTimersInit( USHORT usTim1Timerout50us )
 {
-    //usInterval = 50 * usTim1Timerout50us;
-	usInterval = 200 * usTim1Timerout50us;  //10ms timeout ®É¶¡§ì9600bps 10byte ªº®É¶¡  stanley
+    usInterval = 50 * usTim1Timerout50us;
     return TRUE;
 }
 
@@ -72,7 +71,8 @@
  */
 static void prvvTIMERExpiredISR( void )
 {
-    ( void )pxMBPortCBTimerExpired(  );
+	
+    (void)pxMBPortCBTimerExpired( );
     // Cam - disable further interrupts by detaching
     toMBUS.detach(); 
 }
--- a/main.cpp	Tue Mar 14 14:25:27 2017 +0800
+++ b/main.cpp	Tue Mar 14 22:23:17 2017 +0800
@@ -26,6 +26,8 @@
 #include "mbport.h"
 #include "mbed.h" //stanley
 DigitalOut myled(LED1); //stanley
+
+
 Serial pc(USBTX, USBRX);  //stanley
 
 /* ----------------------- Defines ------------------------------------------*/
@@ -38,11 +40,12 @@
 static USHORT   usRegInputBuf[REG_INPUT_NREGS];
 
 /* ----------------------- Start implementation -----------------------------*/
+
 int main( void )
 {
     eMBErrorCode    eStatus;
 
-    eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE );
+    eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 115200, MB_PAR_NONE );
 
     /* Enable the Modbus Protocol Stack. */
     eStatus = eMBEnable(  );
@@ -52,20 +55,15 @@
     usRegInputBuf[2] = 0x5678;
     usRegInputBuf[3] = 0x9abc;        
     
-    //UCHAR myRegInputBuf[2]={0x11,0x22};//stanley
-    //eMBRegHoldingCB(myRegInputBuf,REG_INPUT_START+1,1,MB_REG_WRITE);//stanley
-	pc.printf("in main\n");
-	myled=1;
-
-	int err=0;
+	
+	myled=1;//stanley
+	
     for( ;; )
     {
         //(void)eMBPoll(  ); origianl
 		//
-		err=eMBPoll();
-		if(err!=MB_ENOERR)
-			pc.printf("err=%d\n",err);
-       
+		eStatus=eMBPoll();
+		
         /* Here we simply count the number of poll cycles. */
         usRegInputBuf[0]++;