1.original from Cam Marshall 2.use for F446RE Test 3.use the interrupt method of uart 4.not change to RTOS yet
Dependents: RoboticArm Modbus_Gripper_Test
port/portserial.cpp@0:aefcdfe9ca2f, 2017-03-31 (annotated)
- Committer:
- stanley1228
- Date:
- Fri Mar 31 01:47:35 2017 +0000
- Revision:
- 0:aefcdfe9ca2f
1.change the file in "port" folder to F446RE use
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stanley1228 | 0:aefcdfe9ca2f | 1 | /* |
stanley1228 | 0:aefcdfe9ca2f | 2 | * FreeModbus Libary: BARE Port |
stanley1228 | 0:aefcdfe9ca2f | 3 | * Copyright (C) 2006 Christian Walter <wolti@sil.at> |
stanley1228 | 0:aefcdfe9ca2f | 4 | * |
stanley1228 | 0:aefcdfe9ca2f | 5 | * This library is free software; you can redistribute it and/or |
stanley1228 | 0:aefcdfe9ca2f | 6 | * modify it under the terms of the GNU Lesser General Public |
stanley1228 | 0:aefcdfe9ca2f | 7 | * License as published by the Free Software Foundation; either |
stanley1228 | 0:aefcdfe9ca2f | 8 | * version 2.1 of the License, or (at your option) any later version. |
stanley1228 | 0:aefcdfe9ca2f | 9 | * |
stanley1228 | 0:aefcdfe9ca2f | 10 | * This library is distributed in the hope that it will be useful, |
stanley1228 | 0:aefcdfe9ca2f | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
stanley1228 | 0:aefcdfe9ca2f | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
stanley1228 | 0:aefcdfe9ca2f | 13 | * Lesser General Public License for more details. |
stanley1228 | 0:aefcdfe9ca2f | 14 | * |
stanley1228 | 0:aefcdfe9ca2f | 15 | * You should have received a copy of the GNU Lesser General Public |
stanley1228 | 0:aefcdfe9ca2f | 16 | * License along with this library; if not, write to the Free Software |
stanley1228 | 0:aefcdfe9ca2f | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
stanley1228 | 0:aefcdfe9ca2f | 18 | * |
stanley1228 | 0:aefcdfe9ca2f | 19 | * File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $ |
stanley1228 | 0:aefcdfe9ca2f | 20 | */ |
stanley1228 | 0:aefcdfe9ca2f | 21 | |
stanley1228 | 0:aefcdfe9ca2f | 22 | /* ----------------------- System includes ----------------------------------*/ |
stanley1228 | 0:aefcdfe9ca2f | 23 | #include "mbed.h" // Cam |
stanley1228 | 0:aefcdfe9ca2f | 24 | |
stanley1228 | 0:aefcdfe9ca2f | 25 | /* ----------------------- Platform includes --------------------------------*/ |
stanley1228 | 0:aefcdfe9ca2f | 26 | #include "port.h" |
stanley1228 | 0:aefcdfe9ca2f | 27 | |
stanley1228 | 0:aefcdfe9ca2f | 28 | /* ----------------------- Modbus includes ----------------------------------*/ |
stanley1228 | 0:aefcdfe9ca2f | 29 | #include "mb.h" |
stanley1228 | 0:aefcdfe9ca2f | 30 | #include "mbport.h" |
stanley1228 | 0:aefcdfe9ca2f | 31 | |
stanley1228 | 0:aefcdfe9ca2f | 32 | |
stanley1228 | 0:aefcdfe9ca2f | 33 | /* ----------------------- static functions ---------------------------------*/ |
stanley1228 | 0:aefcdfe9ca2f | 34 | static void prvvUARTTxReadyISR( void ); |
stanley1228 | 0:aefcdfe9ca2f | 35 | static void prvvUARTRxISR( void ); |
stanley1228 | 0:aefcdfe9ca2f | 36 | |
stanley1228 | 0:aefcdfe9ca2f | 37 | |
stanley1228 | 0:aefcdfe9ca2f | 38 | /* ----------------------- System Variables ---------------------------------*/ |
stanley1228 | 0:aefcdfe9ca2f | 39 | Serial ser3(PB_10, PC_5); //serial3 |
stanley1228 | 0:aefcdfe9ca2f | 40 | extern Serial pc; |
stanley1228 | 0:aefcdfe9ca2f | 41 | static uint8_t grx_buf=0; |
stanley1228 | 0:aefcdfe9ca2f | 42 | |
stanley1228 | 0:aefcdfe9ca2f | 43 | /* ----------------------- Start implementation -----------------------------*/ |
stanley1228 | 0:aefcdfe9ca2f | 44 | void |
stanley1228 | 0:aefcdfe9ca2f | 45 | vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) |
stanley1228 | 0:aefcdfe9ca2f | 46 | { |
stanley1228 | 0:aefcdfe9ca2f | 47 | /* If xRXEnable enable serial receive interrupts. If xTxENable enable |
stanley1228 | 0:aefcdfe9ca2f | 48 | * transmitter empty interrupts. |
stanley1228 | 0:aefcdfe9ca2f | 49 | */ |
stanley1228 | 0:aefcdfe9ca2f | 50 | |
stanley1228 | 0:aefcdfe9ca2f | 51 | |
stanley1228 | 0:aefcdfe9ca2f | 52 | //stanley |
stanley1228 | 0:aefcdfe9ca2f | 53 | ENTER_CRITICAL_SECTION( ); |
stanley1228 | 0:aefcdfe9ca2f | 54 | |
stanley1228 | 0:aefcdfe9ca2f | 55 | |
stanley1228 | 0:aefcdfe9ca2f | 56 | ser3.attach(NULL); //close all |
stanley1228 | 0:aefcdfe9ca2f | 57 | |
stanley1228 | 0:aefcdfe9ca2f | 58 | if(xRxEnable) |
stanley1228 | 0:aefcdfe9ca2f | 59 | ser3.attach(&prvvUARTRxISR,Serial::RxIrq); |
stanley1228 | 0:aefcdfe9ca2f | 60 | |
stanley1228 | 0:aefcdfe9ca2f | 61 | if(xTxEnable) |
stanley1228 | 0:aefcdfe9ca2f | 62 | { |
stanley1228 | 0:aefcdfe9ca2f | 63 | ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq); |
stanley1228 | 0:aefcdfe9ca2f | 64 | while(!ser3.writeable()); |
stanley1228 | 0:aefcdfe9ca2f | 65 | prvvUARTTxReadyISR(); |
stanley1228 | 0:aefcdfe9ca2f | 66 | } |
stanley1228 | 0:aefcdfe9ca2f | 67 | EXIT_CRITICAL_SECTION( ); |
stanley1228 | 0:aefcdfe9ca2f | 68 | |
stanley1228 | 0:aefcdfe9ca2f | 69 | //if(xTxEnable) |
stanley1228 | 0:aefcdfe9ca2f | 70 | //¤£¥Î¦b³oÃä³]©wser3.write(tx_buf,TX_BUFF_LENGTH,serialTxCBEvent,SERIAL_EVENT_TX_COMPLETE); |
stanley1228 | 0:aefcdfe9ca2f | 71 | |
stanley1228 | 0:aefcdfe9ca2f | 72 | //else |
stanley1228 | 0:aefcdfe9ca2f | 73 | //{ |
stanley1228 | 0:aefcdfe9ca2f | 74 | // ser3.attach(NULL,Serial::RxIrq); //stanley the test result find that this may disable RxIrq and TxIrq so have to enable TxIrq |
stanley1228 | 0:aefcdfe9ca2f | 75 | // ser3.attach(&prvvUARTTxReadyISR,Serial::TxIrq); |
stanley1228 | 0:aefcdfe9ca2f | 76 | //} |
stanley1228 | 0:aefcdfe9ca2f | 77 | } |
stanley1228 | 0:aefcdfe9ca2f | 78 | |
stanley1228 | 0:aefcdfe9ca2f | 79 | BOOL |
stanley1228 | 0:aefcdfe9ca2f | 80 | xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) |
stanley1228 | 0:aefcdfe9ca2f | 81 | { |
stanley1228 | 0:aefcdfe9ca2f | 82 | |
stanley1228 | 0:aefcdfe9ca2f | 83 | ser3.baud(ulBaudRate); //stanley |
stanley1228 | 0:aefcdfe9ca2f | 84 | |
stanley1228 | 0:aefcdfe9ca2f | 85 | |
stanley1228 | 0:aefcdfe9ca2f | 86 | return TRUE; |
stanley1228 | 0:aefcdfe9ca2f | 87 | } |
stanley1228 | 0:aefcdfe9ca2f | 88 | |
stanley1228 | 0:aefcdfe9ca2f | 89 | BOOL |
stanley1228 | 0:aefcdfe9ca2f | 90 | xMBPortSerialPutByte( CHAR ucByte ) |
stanley1228 | 0:aefcdfe9ca2f | 91 | { |
stanley1228 | 0:aefcdfe9ca2f | 92 | /* Put a byte in the UARTs transmit buffer. This function is called |
stanley1228 | 0:aefcdfe9ca2f | 93 | * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been |
stanley1228 | 0:aefcdfe9ca2f | 94 | * called. */ |
stanley1228 | 0:aefcdfe9ca2f | 95 | |
stanley1228 | 0:aefcdfe9ca2f | 96 | //stanley |
stanley1228 | 0:aefcdfe9ca2f | 97 | ser3.putc( ucByte); |
stanley1228 | 0:aefcdfe9ca2f | 98 | |
stanley1228 | 0:aefcdfe9ca2f | 99 | return TRUE; |
stanley1228 | 0:aefcdfe9ca2f | 100 | } |
stanley1228 | 0:aefcdfe9ca2f | 101 | |
stanley1228 | 0:aefcdfe9ca2f | 102 | BOOL |
stanley1228 | 0:aefcdfe9ca2f | 103 | xMBPortSerialGetByte( CHAR * pucByte ) |
stanley1228 | 0:aefcdfe9ca2f | 104 | { |
stanley1228 | 0:aefcdfe9ca2f | 105 | /* Return the byte in the UARTs receive buffer. This function is called |
stanley1228 | 0:aefcdfe9ca2f | 106 | * by the protocol stack after pxMBFrameCBByteReceived( ) has been called. |
stanley1228 | 0:aefcdfe9ca2f | 107 | */ |
stanley1228 | 0:aefcdfe9ca2f | 108 | |
stanley1228 | 0:aefcdfe9ca2f | 109 | *pucByte = grx_buf;//stanley |
stanley1228 | 0:aefcdfe9ca2f | 110 | |
stanley1228 | 0:aefcdfe9ca2f | 111 | |
stanley1228 | 0:aefcdfe9ca2f | 112 | |
stanley1228 | 0:aefcdfe9ca2f | 113 | return TRUE; |
stanley1228 | 0:aefcdfe9ca2f | 114 | } |
stanley1228 | 0:aefcdfe9ca2f | 115 | |
stanley1228 | 0:aefcdfe9ca2f | 116 | /* Create an interrupt handler for the transmit buffer empty interrupt |
stanley1228 | 0:aefcdfe9ca2f | 117 | * (or an equivalent) for your target processor. This function should then |
stanley1228 | 0:aefcdfe9ca2f | 118 | * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that |
stanley1228 | 0:aefcdfe9ca2f | 119 | * a new character can be sent. The protocol stack will then call |
stanley1228 | 0:aefcdfe9ca2f | 120 | * xMBPortSerialPutByte( ) to send the character. |
stanley1228 | 0:aefcdfe9ca2f | 121 | */ |
stanley1228 | 0:aefcdfe9ca2f | 122 | static void prvvUARTTxReadyISR( void ) |
stanley1228 | 0:aefcdfe9ca2f | 123 | { |
stanley1228 | 0:aefcdfe9ca2f | 124 | pxMBFrameCBTransmitterEmpty(); |
stanley1228 | 0:aefcdfe9ca2f | 125 | } |
stanley1228 | 0:aefcdfe9ca2f | 126 | |
stanley1228 | 0:aefcdfe9ca2f | 127 | /* Create an interrupt handler for the receive interrupt for your target |
stanley1228 | 0:aefcdfe9ca2f | 128 | * processor. This function should then call pxMBFrameCBByteReceived( ). The |
stanley1228 | 0:aefcdfe9ca2f | 129 | * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the |
stanley1228 | 0:aefcdfe9ca2f | 130 | * character. |
stanley1228 | 0:aefcdfe9ca2f | 131 | */ |
stanley1228 | 0:aefcdfe9ca2f | 132 | static void prvvUARTRxISR( void ) |
stanley1228 | 0:aefcdfe9ca2f | 133 | { |
stanley1228 | 0:aefcdfe9ca2f | 134 | if(ser3.readable()) //stanlely RX ISR must contain getc |
stanley1228 | 0:aefcdfe9ca2f | 135 | grx_buf=ser3.getc(); |
stanley1228 | 0:aefcdfe9ca2f | 136 | |
stanley1228 | 0:aefcdfe9ca2f | 137 | pxMBFrameCBByteReceived(); |
stanley1228 | 0:aefcdfe9ca2f | 138 | } |
stanley1228 | 0:aefcdfe9ca2f | 139 | |
stanley1228 | 0:aefcdfe9ca2f | 140 |