serialPort_t *openSoftSerial(softSerialPortIndex_e portIndex, serialReceiveCallbackPtr callback, uint32_t baud, portOptions_t options) { softSerial_t *softSerial = &(softSerialPorts[portIndex]); #ifdef USE_SOFTSERIAL1 if (portIndex == SOFTSERIAL1) { softSerial->rxTimerHardware = &(timerHardware[SOFTSERIAL_1_TIMER_RX_HARDWARE]); softSerial->txTimerHardware = &(timerHardware[SOFTSERIAL_1_TIMER_TX_HARDWARE]); } #endif #ifdef USE_SOFTSERIAL2 if (portIndex == SOFTSERIAL2) { softSerial->rxTimerHardware = &(timerHardware[SOFTSERIAL_2_TIMER_RX_HARDWARE]); softSerial->txTimerHardware = &(timerHardware[SOFTSERIAL_2_TIMER_TX_HARDWARE]); } #endif softSerial->port.vTable = softSerialVTable; softSerial->port.baudRate = baud; softSerial->port.mode = MODE_RXTX; softSerial->port.options = options; softSerial->port.callback = callback; resetBuffers(softSerial); softSerial->isTransmittingData = false; softSerial->isSearchingForStartBit = true; softSerial->rxBitIndex = 0; softSerial->transmissionErrors = 0; softSerial->receiveErrors = 0; softSerial->softSerialPortIndex = portIndex; softSerial->txIO = IOGetByTag(softSerial->txTimerHardware->tag); serialOutputPortConfig(softSerial->txTimerHardware->tag, portIndex); softSerial->rxIO = IOGetByTag(softSerial->rxTimerHardware->tag); serialInputPortConfig(softSerial->rxTimerHardware->tag, portIndex); setTxSignal(softSerial, ENABLE); delay(50); serialTimerTxConfig(softSerial->txTimerHardware, portIndex, baud); serialTimerRxConfig(softSerial->rxTimerHardware, portIndex, options); return &softSerial->port; }
serialPort_t *openSoftSerial(softSerialPortIndex_e portIndex, serialReceiveCallbackPtr callback, uint32_t baud, serialInversion_e inversion) { softSerial_t *softSerial = &(softSerialPorts[portIndex]); if (portIndex == SOFTSERIAL1) { softSerial->rxTimerHardware = &(timerHardware[SOFT_SERIAL_1_TIMER_RX_HARDWARE]); softSerial->txTimerHardware = &(timerHardware[SOFT_SERIAL_1_TIMER_TX_HARDWARE]); } if (portIndex == SOFTSERIAL2) { softSerial->rxTimerHardware = &(timerHardware[SOFT_SERIAL_2_TIMER_RX_HARDWARE]); softSerial->txTimerHardware = &(timerHardware[SOFT_SERIAL_2_TIMER_TX_HARDWARE]); } softSerial->port.vTable = softSerialVTable; softSerial->port.baudRate = baud; softSerial->port.mode = MODE_RXTX; softSerial->port.inversion = inversion; softSerial->port.callback = callback; resetBuffers(softSerial); softSerial->isTransmittingData = false; softSerial->isSearchingForStartBit = true; softSerial->rxBitIndex = 0; softSerial->transmissionErrors = 0; softSerial->receiveErrors = 0; softSerial->softSerialPortIndex = portIndex; serialOutputPortConfig(softSerial->txTimerHardware); serialInputPortConfig(softSerial->rxTimerHardware); setTxSignal(softSerial, ENABLE); delay(50); serialTimerTxConfig(softSerial->txTimerHardware, portIndex, baud); serialTimerRxConfig(softSerial->rxTimerHardware, portIndex, inversion); return &softSerial->port; }
void setupSoftSerial1(uint32_t baud, uint8_t inverted) { int portIndex = 0; softSerial_t *softSerial = &(softSerialPorts[portIndex]); softSerial->port.vTable = softSerialVTable; softSerial->port.mode = MODE_RXTX; softSerial->port.baudRate = baud; softSerial->isInverted = inverted; //softSerial->rxTimerHardware = &(timerHardware[SOFT_SERIAL_1_TIMER_RX_HARDWARE]); //softSerial->txTimerHardware = &(timerHardware[SOFT_SERIAL_1_TIMER_TX_HARDWARE]); softSerial->port.rxBufferSize = SOFT_SERIAL_BUFFER_SIZE; softSerial->port.rxBuffer = softSerial->rxBuffer; softSerial->port.rxBufferTail = 0; softSerial->port.rxBufferHead = 0; softSerial->port.txBuffer = softSerial->txBuffer; softSerial->port.txBufferSize = SOFT_SERIAL_BUFFER_SIZE, softSerial->port.txBufferTail = 0; softSerial->port.txBufferHead = 0; softSerial->isTransmittingData = false; softSerial->isSearchingForStartBit = true; softSerial->isSearchingForStopBit = false; softSerial->timerRxCounter = 1; serialInputPortConfig(softSerial->rxTimerHardware); serialOutputPortConfig(softSerial->txTimerHardware); setTxSignal(softSerial, 1); delay(50); serialTimerConfig(softSerial->rxTimerHardware, baud, portIndex, onSerialTimer); }
void processTxState(escSerial_t *escSerial) { uint8_t mask; static uint8_t bitq=0, transmitStart=0; if (escSerial->isReceivingData) { return; } if(transmitStart==0) { setTxSignal(escSerial, 1); } if (!escSerial->isTransmittingData) { char byteToSend; reload: if (isEscSerialTransmitBufferEmpty((serialPort_t *)escSerial)) { // canreceive transmitStart=0; return; } if(transmitStart<3) { if(transmitStart==0) byteToSend = 0xff; if(transmitStart==1) byteToSend = 0xff; if(transmitStart==2) byteToSend = 0x7f; transmitStart++; } else{ // data to send byteToSend = escSerial->port.txBuffer[escSerial->port.txBufferTail++]; if (escSerial->port.txBufferTail >= escSerial->port.txBufferSize) { escSerial->port.txBufferTail = 0; } } // build internal buffer, data bits (MSB to LSB) escSerial->internalTxBuffer = byteToSend; escSerial->bitsLeftToTransmit = 8; escSerial->isTransmittingData = true; //set output serialOutputPortConfig(escSerial->rxTimerHardware); return; } if (escSerial->bitsLeftToTransmit) { mask = escSerial->internalTxBuffer & 1; if(mask) { if(bitq==0 || bitq==1) { setTxSignal(escSerial, 1); } if(bitq==2 || bitq==3) { setTxSignal(escSerial, 0); } } else { if(bitq==0 || bitq==2) { setTxSignal(escSerial, 1); } if(bitq==1 ||bitq==3) { setTxSignal(escSerial, 0); } } bitq++; if(bitq>3) { escSerial->internalTxBuffer >>= 1; escSerial->bitsLeftToTransmit--; bitq=0; if(escSerial->bitsLeftToTransmit==0) { goto reload; } }