Func-Gen - basic setup

Dependencies:   mbed

Committer:
stkiegerl
Date:
Tue Mar 23 17:13:12 2021 +0000
Revision:
3:9fd12684fa88
Parent:
0:e1bc83101929
-

Who changed what in which revision?

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