Skip to content

Commit d36a0ba

Browse files
committed
Use IPM for cdc-acm
-use the IPM instead of shared memory for passing data between ARC and x86 cores for cdc-acm.
1 parent 936a638 commit d36a0ba

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

cores/arduino/CDCSerialClass.cpp

+41-26
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ extern void CDCSerial_Handler(void);
3636
extern void serialEventRun1(void) __attribute__((weak));
3737
extern void serialEvent1(void) __attribute__((weak));
3838

39+
static void cdc_mbox_isr(CurieMailboxMsg msg)
40+
{
41+
int new_head;
42+
for(int i = 0; i < 4; i++)
43+
{
44+
uint32_t dword = msg.data[i];
45+
for(int j = 0; j <4; j++)
46+
{
47+
if(((uint8_t)dword) != '\0')
48+
{
49+
new_head = (Serial._rx_buffer->head +1) % CDCACM_BUFFER_SIZE;
50+
if(new_head != Serial._rx_buffer->tail)
51+
{
52+
Serial._rx_buffer->data[Serial._rx_buffer->head] = (uint8_t)dword;
53+
Serial._rx_buffer->head = new_head;
54+
}
55+
}
56+
else
57+
{
58+
i = 4;
59+
break;
60+
}
61+
dword = dword >> 8;
62+
}
63+
}
64+
}
65+
3966
// Constructors ////////////////////////////////////////////////////////////////
4067

4168
CDCSerialClass::CDCSerialClass(uart_init_info *info)
@@ -47,9 +74,9 @@ CDCSerialClass::CDCSerialClass(uart_init_info *info)
4774

4875
void CDCSerialClass::setSharedData(struct cdc_acm_shared_data *cdc_acm_shared_data)
4976
{
50-
this->_shared_data = cdc_acm_shared_data;
51-
this->_rx_buffer = cdc_acm_shared_data->rx_buffer;
52-
this->_tx_buffer = cdc_acm_shared_data->tx_buffer;
77+
_shared_data = cdc_acm_shared_data;
78+
_rx_buffer = cdc_acm_shared_data->rx_buffer;
79+
_tx_buffer = cdc_acm_shared_data->tx_buffer;
5380
}
5481

5582
void CDCSerialClass::begin(const uint32_t dwBaudRate)
@@ -73,7 +100,9 @@ void CDCSerialClass::init(const uint32_t dwBaudRate, const uint8_t modeReg)
73100
* Empty the Rx buffer but don't touch Tx buffer: it is drained by the
74101
* LMT one way or another */
75102
_rx_buffer->tail = _rx_buffer->head;
76-
103+
104+
mailbox_register(6, cdc_mbox_isr);
105+
mailbox_enable_receive(6);
77106
_shared_data->device_open = true;
78107
}
79108

@@ -84,12 +113,10 @@ void CDCSerialClass::end( void )
84113

85114
int CDCSerialClass::available( void )
86115
{
87-
#define SBS CDCACM_BUFFER_SIZE
88-
89116
if (!_shared_data->device_open)
90117
return (0);
91118
else
92-
return (int)(SBS + _rx_buffer->head - _rx_buffer->tail) % SBS;
119+
return (int)(CDCACM_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % CDCACM_BUFFER_SIZE;
93120
}
94121

95122
int CDCSerialClass::availableForWrite(void)
@@ -131,28 +158,16 @@ void CDCSerialClass::flush( void )
131158
}
132159
}
133160

134-
size_t CDCSerialClass::write( const uint8_t uc_data )
161+
size_t CDCSerialClass::write(uint8_t uc_data )
135162
{
136-
uint32_t retries = 2;
137-
163+
CurieMailboxMsg cdcacm_msg;
164+
138165
if (!_shared_data->device_open || !_shared_data->host_open)
139166
return(0);
140167

141-
do {
142-
int i = (uint32_t)(_tx_buffer->head + 1) % CDCACM_BUFFER_SIZE;
143-
// if we should be storing the received character into the location
144-
// just before the tail (meaning that the head would advance to the
145-
// current location of the tail), we're about to overflow the buffer
146-
// and so we don't write the character or advance the head.
147-
if (i != _tx_buffer->tail) {
148-
_tx_buffer->data[_tx_buffer->head] = uc_data;
149-
_tx_buffer->head = i;
150-
151-
// Just use a fixed delay to make it compatible with the CODK-M based firmware
152-
delayMicroseconds(CDCACM_FIXED_DELAY);
153-
break;
154-
}
155-
} while (retries--);
156-
168+
cdcacm_msg.channel = 7;
169+
cdcacm_msg.data[0] = (uint32_t)uc_data;
170+
mailbox_write(cdcacm_msg);
171+
delayMicroseconds(_writeDelayUsec);
157172
return 1;
158173
}

cores/arduino/CDCSerialClass.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "HardwareSerial.h"
2727
#include "platform.h"
2828
#include "wiring.h"
29+
#include "mailbox.h"
2930

3031
#include <board.h>
3132
#include <uart.h>
@@ -36,6 +37,10 @@ class CDCSerialClass : public HardwareSerial
3637
CDCSerialClass(uart_init_info *info);
3738

3839
void setSharedData(struct cdc_acm_shared_data *cdc_acm_shared_data);
40+
41+
struct cdc_acm_shared_data *_shared_data;
42+
struct cdc_ring_buffer *_rx_buffer;
43+
struct cdc_ring_buffer *_tx_buffer;
3944

4045
void begin(const uint32_t dwBaudRate);
4146
void begin(const uint32_t dwBaudRate, const uint8_t config);
@@ -58,10 +63,6 @@ class CDCSerialClass : public HardwareSerial
5863
protected:
5964
void init(const uint32_t dwBaudRate, const uint8_t config);
6065

61-
struct cdc_acm_shared_data *_shared_data;
62-
struct cdc_ring_buffer *_rx_buffer;
63-
struct cdc_ring_buffer *_tx_buffer;
64-
6566
uart_init_info *info;
6667
uint32_t _writeDelayUsec;
6768
uint32_t _dwId;

0 commit comments

Comments
 (0)