Skip to content

Commit

Permalink
Merge pull request #2 from KurtE/giga_Serial_fixex
Browse files Browse the repository at this point in the history
Some Serial object fixes...
  • Loading branch information
facchinm authored Dec 9, 2024
2 parents 0bf6118 + a565a0a commit cfe5606
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
33 changes: 29 additions & 4 deletions cores/arduino/zephyrSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ void arduino::ZephyrSerial::begin(unsigned long baud, uint16_t conf)

uart_configure(uart, &config);
uart_irq_callback_user_data_set(uart, arduino::ZephyrSerial::IrqDispatch, this);
k_sem_take(&rx.sem, K_FOREVER);
ring_buf_reset(&rx.ringbuf);
k_sem_give(&rx.sem);

uart_irq_rx_enable(uart);
}

Expand Down Expand Up @@ -126,26 +130,37 @@ int arduino::ZephyrSerial::available()
return ret;
}

int arduino::ZephyrSerial::availableForWrite()
{
int ret;

k_sem_take(&rx.sem, K_FOREVER);
ret = ring_buf_space_get(&rx.ringbuf);
k_sem_give(&rx.sem);

return ret;
}

int arduino::ZephyrSerial::peek()
{
uint8_t data;

k_sem_take(&rx.sem, K_FOREVER);
ring_buf_peek(&rx.ringbuf, &data, 1);
uint32_t cb_ret = ring_buf_peek(&rx.ringbuf, &data, 1);
k_sem_give(&rx.sem);

return data;
return cb_ret? data : -1;
}

int arduino::ZephyrSerial::read()
{
uint8_t data;

k_sem_take(&rx.sem, K_FOREVER);
ring_buf_get(&rx.ringbuf, &data, 1);
uint32_t cb_ret = ring_buf_get(&rx.ringbuf, &data, 1);
k_sem_give(&rx.sem);

return data;
return cb_ret? data : -1;
}

size_t arduino::ZephyrSerial::write(const uint8_t *buffer, size_t size)
Expand Down Expand Up @@ -174,6 +189,16 @@ size_t arduino::ZephyrSerial::write(const uint8_t *buffer, size_t size)
return size;
}

void arduino::ZephyrSerial::flush() {
while (ring_buf_size_get(&tx.ringbuf) > 0) {
k_yield();
}
while (uart_irq_tx_complete(uart) == 0){
k_yield();
}
}


#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), serials)
#if !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), cdc_acm)
// If CDC USB, use that object as Serial (and SerialUSB)
Expand Down
11 changes: 4 additions & 7 deletions cores/arduino/zephyrSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace arduino {
class ZephyrSerialStub : public HardwareSerial
{
public:
void begin(unsigned long baudRate) { }
void begin(unsigned long baudrate, uint16_t config) { }
void begin(__attribute__((unused)) unsigned long baudRate) { }
void begin(__attribute__((unused)) unsigned long baudrate, __attribute__((unused)) uint16_t config) { }
void end() { }
int available() { return 0; }
int peek() { return 0; }
Expand Down Expand Up @@ -51,16 +51,13 @@ class ZephyrSerial : public HardwareSerial
ZephyrSerial(const struct device *dev) : uart(dev) { }
void begin(unsigned long baudrate, uint16_t config);
void begin(unsigned long baudrate) { begin(baudrate, SERIAL_8N1); }
void flush() {
while (ring_buf_size_get(&tx.ringbuf) > 0) {
k_yield();
}
}
void flush();
void end() { }
size_t write(const uint8_t *buffer, size_t size);
size_t write(const uint8_t data) { return write(&data, 1); }
using Print::write; // pull in write(str) and write(buf, size) from Print
int available();
int availableForWrite();
int peek();
int read();

Expand Down
16 changes: 15 additions & 1 deletion loader/boards/arduino_giga_r1_m7.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
status = "okay";
};

&usart2 {
status = "okay";
pinctrl-0 = <&usart2_tx_pd5 &usart2_rx_pd6>;
pinctrl-names = "default";
current-speed = <115200>;
};

&uart4 {
status = "okay";
pinctrl-0 = <&uart4_tx_ph13 &uart4_rx_pi9>;
pinctrl-names = "default";
current-speed = <115200>;
};

&usart6 {
status = "okay";
};
Expand Down Expand Up @@ -407,7 +421,7 @@
<&gpioa 4 0>,
<&gpioa 5 0>;

serials = <&cdc_acm_uart0>, <&usart1>, <&usart6>;
serials = <&cdc_acm_uart0>, <&usart1>, <&usart2>, <&uart4>, <&usart6>;
cdc-acm = <&cdc_acm_uart0>;
i2cs = <&i2c4>;
spis = <&spi1>, <&spi5>;
Expand Down

0 comments on commit cfe5606

Please sign in to comment.