Mickaël THEOT / AX12
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialHalfDuplex.cpp Source File

SerialHalfDuplex.cpp

00001 /*
00002  * mbed Microcontroller Library
00003  * Copyright (c) 2006-2012 ARM Limited
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00021  * SOFTWARE.
00022  *
00023  * NOTE: This is an unsupported legacy untested library.
00024  */
00025 #include "SerialHalfDuplex.h"
00026 
00027 #include "mbed.h"
00028 #include "pinmap.h"
00029 #include "serial_api.h"
00030 #include "gpio_api.h"
00031 
00032 namespace mbed {
00033 
00034 /**
00035  * @brief Constructeur de l'objet SerialHalfDuplex
00036  * 
00037  * @param tx pin de transmission
00038  * @param rx pin de reception
00039  * @param baud vitesse de communication (en bps)
00040  * 
00041  * @attention Les pins \p tx et \p rx doivent être physiquement reliés entre eux
00042  */
00043 SerialHalfDuplex::SerialHalfDuplex(PinName tx, PinName rx, int baud)
00044     : SerialBase(tx, rx, baud)
00045 {
00046     idx = 0;
00047     _txpin = tx;
00048     _baud = baud;
00049     DigitalIn TXPIN(_txpin);    // set as input
00050     pin_mode(_txpin, PullNone); // no pull
00051     pin_function(_txpin, 0);    // set as gpio
00052     SerialBase::baud(_baud);
00053     SerialBase::format(8,SerialBase::None,1);
00054     SerialBase::attach(callback(this,&SerialHalfDuplex::RXinterrupt),SerialBase::RxIrq);
00055 }
00056 
00057 // To transmit a byte in half duplex mode:
00058 // 1. Disable interrupts, so we don't trigger on loopback byte
00059 // 2. Set tx pin to UART out
00060 // 3. Transmit byte as normal
00061 // 4. Read back byte from looped back tx pin - this both confirms that the
00062 //    transmit has occurred, and also clears the byte from the buffer.
00063 // 5. Return pin to input mode
00064 // 6. Re-enable interrupts
00065 
00066 /**
00067  * @brief Cette fonction remplie le buffer de reception \p buffer avec des '0'
00068  * 
00069  * @param buffer est un pointeur vers le buffer à modifier
00070  */
00071 bool SerialHalfDuplex::eraseBuffer(int *buffer){
00072     for(int i=0; i<BUF_SIZE;i++){
00073       buffer[i] = 0;
00074     }
00075     return true;
00076 }
00077 
00078 // Send one character to the serial port
00079 /**
00080  * @brief Cette fonction permet d'envoyer un caractère sur la liaison série en mode Half-Duplex
00081  * 
00082  * @param c le caractère à envoyer
00083  * @return le caractère qui a été récupéré suite à l'envoi
00084  */
00085 int SerialHalfDuplex::putc(int c){
00086     int retc;
00087     eraseBuffer(buf); // Peut-être pas utile mais on sait jamais...
00088 
00089     //Disable the interruption while sending the character
00090     core_util_critical_section_enter();
00091 
00092     serial_pinout_tx(_txpin);
00093     
00094     SerialBase::_base_putc(c);
00095     retc = SerialBase::_base_getc();     // reading also clears any interrupt
00096     
00097     pin_function(_txpin, 0);
00098 
00099     core_util_critical_section_exit();
00100     
00101     return retc;
00102 }
00103 
00104 //Triggered if sthg happen on the receiving pin
00105 /**
00106  * @brief Cette fonction est appelée dès que quelque chose est disponible en réception sur le port série. 
00107  * Elle stocke les caractères reçus dans les cases vides (i.e. qui contienne un '0') du buffer \p buf , dans l'ordre où ceux-ci sont arrivés
00108  * 
00109  * @attention Le code d'erreur renvoyé par le PacketStatus de l'AX12 étant dans la plupart des cas '0', on remplace ca valeur par un '1' pour éviter un décalage dans les index
00110  * Une meilleure méthode serait la binevenue...
00111  */
00112 void SerialHalfDuplex::RXinterrupt(void){
00113     while(readable()){
00114         buf[idx] = _base_getc();
00115         idx++;
00116     }    
00117 }
00118 
00119 // Take a character into the reception buffer and replace it with à 'z'
00120 /**
00121  * @brief Cette fonction permet d'accéder au contenu du buffer de réception et remplace le caractère par un 'z' une fois la valeur récupérée
00122  * 
00123  * @param i index du caractère que l'on veut récupérer dans le buffer \p buf de réception
00124  * @return le caractère contenu dans la case correspondante à l'index \p i.
00125  */
00126 int SerialHalfDuplex::getc(int i){
00127     idx = 0;  
00128     int retc = buf[i];
00129     buf[i]=0;
00130     return retc;    
00131 }
00132 
00133 } // End namespace