Stefan Kiegerl
/
FuncGen
Function Generator for TINF 2021
src/Serial_HL.cpp@2:d2e5b6c37b4b, 2021-03-18 (annotated)
- Committer:
- stkiegerl
- Date:
- Thu Mar 18 21:24:21 2021 +0100
- Revision:
- 2:d2e5b6c37b4b
- Parent:
- 0:464b401734fd
-
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stkiegerl | 0:464b401734fd | 1 | #include "Serial_HL.h" |
stkiegerl | 0:464b401734fd | 2 | #include <stdarg.h> |
stkiegerl | 0:464b401734fd | 3 | #include <stdio.h> |
stkiegerl | 0:464b401734fd | 4 | |
stkiegerl | 0:464b401734fd | 5 | namespace mbed { |
stkiegerl | 0:464b401734fd | 6 | |
stkiegerl | 0:464b401734fd | 7 | SerialBLK::SerialBLK(PinName tx, PinName rx) { |
stkiegerl | 0:464b401734fd | 8 | serial_init(&_serial, tx, rx); |
stkiegerl | 0:464b401734fd | 9 | _baud = 9600; |
stkiegerl | 0:464b401734fd | 10 | serial_irq_handler(&_serial, SerialBLK::_irq_handler, (uint32_t)this); |
stkiegerl | 0:464b401734fd | 11 | } |
stkiegerl | 0:464b401734fd | 12 | |
stkiegerl | 0:464b401734fd | 13 | void SerialBLK::baud(int baudrate) { |
stkiegerl | 0:464b401734fd | 14 | serial_baud(&_serial, baudrate); |
stkiegerl | 0:464b401734fd | 15 | _baud = baudrate; |
stkiegerl | 0:464b401734fd | 16 | } |
stkiegerl | 0:464b401734fd | 17 | |
stkiegerl | 0:464b401734fd | 18 | void SerialBLK::format(int bits, Parity parity, int stop_bits) { |
stkiegerl | 0:464b401734fd | 19 | serial_format(&_serial, bits, (SerialParity)parity, stop_bits); |
stkiegerl | 0:464b401734fd | 20 | } |
stkiegerl | 0:464b401734fd | 21 | |
stkiegerl | 0:464b401734fd | 22 | int SerialBLK::readable() { |
stkiegerl | 0:464b401734fd | 23 | return serial_readable(&_serial); |
stkiegerl | 0:464b401734fd | 24 | } |
stkiegerl | 0:464b401734fd | 25 | |
stkiegerl | 0:464b401734fd | 26 | int SerialBLK::writeable() { |
stkiegerl | 0:464b401734fd | 27 | return serial_writable(&_serial); |
stkiegerl | 0:464b401734fd | 28 | } |
stkiegerl | 0:464b401734fd | 29 | |
stkiegerl | 0:464b401734fd | 30 | void SerialBLK::attach(void (*fptr)(void), IrqType type) { |
stkiegerl | 0:464b401734fd | 31 | if (fptr) { |
stkiegerl | 0:464b401734fd | 32 | _irq[type].attach(fptr); |
stkiegerl | 0:464b401734fd | 33 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
stkiegerl | 0:464b401734fd | 34 | } else { |
stkiegerl | 0:464b401734fd | 35 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
stkiegerl | 0:464b401734fd | 36 | } |
stkiegerl | 0:464b401734fd | 37 | } |
stkiegerl | 0:464b401734fd | 38 | |
stkiegerl | 0:464b401734fd | 39 | void SerialBLK::_irq_handler(uint32_t id, SerialIrq irq_type) { |
stkiegerl | 0:464b401734fd | 40 | SerialBLK *obj = (SerialBLK*)id; |
stkiegerl | 0:464b401734fd | 41 | obj->_irq[irq_type].call(); |
stkiegerl | 0:464b401734fd | 42 | } |
stkiegerl | 0:464b401734fd | 43 | |
stkiegerl | 0:464b401734fd | 44 | int SerialBLK::GetChar() { |
stkiegerl | 0:464b401734fd | 45 | return serial_getc(&_serial); |
stkiegerl | 0:464b401734fd | 46 | } |
stkiegerl | 0:464b401734fd | 47 | |
stkiegerl | 0:464b401734fd | 48 | void SerialBLK::PutChar(int aCh) { |
stkiegerl | 0:464b401734fd | 49 | serial_putc(&_serial, aCh); |
stkiegerl | 0:464b401734fd | 50 | } |
stkiegerl | 0:464b401734fd | 51 | |
stkiegerl | 0:464b401734fd | 52 | void SerialBLK::Write(void* aData, uint32_t aLenBytes) |
stkiegerl | 0:464b401734fd | 53 | { |
stkiegerl | 0:464b401734fd | 54 | uint8_t* ptr = (uint8_t*)aData; |
stkiegerl | 0:464b401734fd | 55 | for(int i=0; i<aLenBytes; i++) { |
stkiegerl | 0:464b401734fd | 56 | this->PutChar(*ptr); ptr++; |
stkiegerl | 0:464b401734fd | 57 | } |
stkiegerl | 0:464b401734fd | 58 | } |
stkiegerl | 0:464b401734fd | 59 | |
stkiegerl | 0:464b401734fd | 60 | void SerialBLK::Read(void* aData, uint32_t aLenBytes) |
stkiegerl | 0:464b401734fd | 61 | { |
stkiegerl | 0:464b401734fd | 62 | uint8_t* ptr = (uint8_t*)aData; |
stkiegerl | 0:464b401734fd | 63 | for(int i=0; i<aLenBytes; i++) { |
stkiegerl | 0:464b401734fd | 64 | *ptr=this->GetChar(); ptr++; |
stkiegerl | 0:464b401734fd | 65 | } |
stkiegerl | 0:464b401734fd | 66 | } |
stkiegerl | 0:464b401734fd | 67 | |
stkiegerl | 0:464b401734fd | 68 | |
stkiegerl | 0:464b401734fd | 69 | |
stkiegerl | 0:464b401734fd | 70 | |
stkiegerl | 0:464b401734fd | 71 | void SvProtocol::Puts(char* aCStr) |
stkiegerl | 0:464b401734fd | 72 | { |
stkiegerl | 0:464b401734fd | 73 | while( *aCStr != '\0' ) |
stkiegerl | 0:464b401734fd | 74 | { |
stkiegerl | 0:464b401734fd | 75 | if( *aCStr=='\n' ) |
stkiegerl | 0:464b401734fd | 76 | _st->PutChar('\r'); |
stkiegerl | 0:464b401734fd | 77 | _st->PutChar(*aCStr); |
stkiegerl | 0:464b401734fd | 78 | aCStr++; |
stkiegerl | 0:464b401734fd | 79 | } |
stkiegerl | 0:464b401734fd | 80 | _st->PutChar(0); // terminate with 0 |
stkiegerl | 0:464b401734fd | 81 | } |
stkiegerl | 0:464b401734fd | 82 | |
stkiegerl | 0:464b401734fd | 83 | static char sBuffer[50]; |
stkiegerl | 0:464b401734fd | 84 | |
stkiegerl | 0:464b401734fd | 85 | void SvProtocol::Printf(const char *format, ...) |
stkiegerl | 0:464b401734fd | 86 | { |
stkiegerl | 0:464b401734fd | 87 | va_list vArgs; |
stkiegerl | 0:464b401734fd | 88 | va_start(vArgs, format); |
stkiegerl | 0:464b401734fd | 89 | vsprintf(sBuffer, (char const *)format, vArgs); |
stkiegerl | 0:464b401734fd | 90 | va_end(vArgs); |
stkiegerl | 0:464b401734fd | 91 | Puts(sBuffer); |
stkiegerl | 0:464b401734fd | 92 | } |
stkiegerl | 0:464b401734fd | 93 | |
stkiegerl | 0:464b401734fd | 94 | void SvProtocol::SvPrintf(const char *format, ...) |
stkiegerl | 0:464b401734fd | 95 | { |
stkiegerl | 0:464b401734fd | 96 | va_list vArgs; |
stkiegerl | 0:464b401734fd | 97 | va_start(vArgs, format); |
stkiegerl | 0:464b401734fd | 98 | vsprintf(sBuffer, (char const *)format, vArgs); |
stkiegerl | 0:464b401734fd | 99 | va_end(vArgs); |
stkiegerl | 0:464b401734fd | 100 | if( !svMessageON ) return; |
stkiegerl | 0:464b401734fd | 101 | _st->PutChar(10); |
stkiegerl | 0:464b401734fd | 102 | Puts(sBuffer); |
stkiegerl | 0:464b401734fd | 103 | } |
stkiegerl | 0:464b401734fd | 104 | |
stkiegerl | 0:464b401734fd | 105 | void SvProtocol::WriteSV3p13(int aId, float aData) { |
stkiegerl | 0:464b401734fd | 106 | // int16_t val = To3p13(aData); |
stkiegerl | 0:464b401734fd | 107 | // PutChar(aId); Write(&val,2); |
stkiegerl | 0:464b401734fd | 108 | } |
stkiegerl | 0:464b401734fd | 109 | |
stkiegerl | 0:464b401734fd | 110 | int SvProtocol::GetCommand() |
stkiegerl | 0:464b401734fd | 111 | { |
stkiegerl | 0:464b401734fd | 112 | uint8_t cmd = _st->GetChar(); |
stkiegerl | 0:464b401734fd | 113 | if( cmd==1 ) |
stkiegerl | 0:464b401734fd | 114 | { |
stkiegerl | 0:464b401734fd | 115 | this->acqON = _st->GetChar(); |
stkiegerl | 0:464b401734fd | 116 | if( this->acqON ) |
stkiegerl | 0:464b401734fd | 117 | this->SvMessage("AcqON"); |
stkiegerl | 0:464b401734fd | 118 | else |
stkiegerl | 0:464b401734fd | 119 | this->SvMessage("AcqOFF"); |
stkiegerl | 0:464b401734fd | 120 | return 0; |
stkiegerl | 0:464b401734fd | 121 | } |
stkiegerl | 0:464b401734fd | 122 | return cmd; |
stkiegerl | 0:464b401734fd | 123 | } |
stkiegerl | 0:464b401734fd | 124 | |
stkiegerl | 0:464b401734fd | 125 | } // namespace mbed |