Skip to content

Commit

Permalink
Fixed example USB-uart-w-signals.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
stevstrong committed Feb 28, 2024
1 parent 2c39b8a commit e41247b
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 188 deletions.
35 changes: 3 additions & 32 deletions STM32F1/cores/maple/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,6 @@
* the documentation accordingly.
*/



/* Roger Clark
*
* Added config defines from AVR
* Note. The values will need to be changed to match STM32 USART config register values, these are just place holders.
*/
// Define config for Serial.begin(baud, config);
// Note. STM32 doesn't support as many different Serial modes as AVR or SAM cores.
// The word legth bit M must be set when using parity bit.

#define SERIAL_8N1 0B00000000
#define SERIAL_8N2 0B00100000
#define SERIAL_9N1 0B00001000
#define SERIAL_9N2 0B00101000

#define SERIAL_8E1 0B00001010
#define SERIAL_8E2 0B00101010
/* not supported:
#define SERIAL_9E1 0B00001010
#define SERIAL_9E2 0B00101010
*/
#define SERIAL_8O1 0B00001011
#define SERIAL_8O2 0B00101011
/* not supported:
#define SERIAL_9O1 0B00001011
#define SERIAL_9O2 0B00101011
*/

/* Roger Clark
* Moved macros from hardwareSerial.cpp
*/
Expand Down Expand Up @@ -111,7 +82,7 @@ static inline void disable_timer_if_necessary(timer_dev *dev, uint8 ch)
class HardwareSerial : public Stream {

public:
HardwareSerial(const usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin)
HardwareSerial(const usart_dev_t *usart_device, uint8 tx_pin, uint8 rx_pin)
{
this->usart_device = usart_device;
this->tx_pin = tx_pin;
Expand Down Expand Up @@ -180,9 +151,9 @@ class HardwareSerial : public Stream {

/* Escape hatch into libmaple */
/* FIXME [0.0.13] documentation */
const usart_dev* c_dev(void) { return this->usart_device; }
const usart_dev_t* c_dev(void) { return this->usart_device; }
private:
const usart_dev *usart_device;
const usart_dev_t *usart_device;
uint8 tx_pin;
uint8 rx_pin;

Expand Down
12 changes: 6 additions & 6 deletions STM32F1/cores/maple/libmaple/series/usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ struct usart_reg_map;
* Devices
*/

struct usart_dev;
extern struct usart_dev *USART1;
extern struct usart_dev *USART2;
extern struct usart_dev *USART3;
struct usart_dev_t;
extern struct usart_dev_t *USART1;
extern struct usart_dev_t *USART2;
extern struct usart_dev_t *USART3;
#ifdef STM32_HIGH_DENSITY
extern struct usart_dev *UART4;
extern struct usart_dev *UART5;
extern struct usart_dev_t *UART4;
extern struct usart_dev_t *UART5;
#endif

#ifdef __cplusplus
Expand Down
12 changes: 6 additions & 6 deletions STM32F1/cores/maple/libmaple/usart.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @brief Initialize a serial port.
* @param dev Serial port to be initialized
*/
void usart_init(const usart_dev *dev) {
void usart_init(const usart_dev_t *dev) {
rb_reset(dev->rb);
rb_reset(dev->wb);
rcc_clk_enable(dev->clk_id);
Expand All @@ -58,7 +58,7 @@ void usart_init(const usart_dev *dev) {
* @param dev Serial port to enable.
* @see usart_set_baud_rate()
*/
void usart_enable(const usart_dev *dev) {
void usart_enable(const usart_dev_t *dev) {
usart_reg_map *regs = dev->regs;
regs->CR1 |= (USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE);// don't change the word length etc, and 'or' in the patten not overwrite |USART_CR1_M_8N1);
regs->CR1 |= USART_CR1_UE;
Expand All @@ -68,7 +68,7 @@ void usart_enable(const usart_dev *dev) {
* @brief Turn off a serial port.
* @param dev Serial port to be disabled
*/
void usart_disable(const usart_dev *dev) {
void usart_disable(const usart_dev_t *dev) {
/* FIXME this misbehaves (on F1) if you try to use PWM on TX afterwards */
usart_reg_map *regs = dev->regs;

Expand All @@ -93,7 +93,7 @@ void usart_disable(const usart_dev *dev) {
* @param len Maximum number of bytes to transmit
* @return Number of bytes transmitted
*/
uint16 usart_tx(const usart_dev *dev, const uint8 *buf, uint16 len)
uint16 usart_tx(const usart_dev_t *dev, const uint8 *buf, uint16 len)
{
uint16 txed = 0;
while ( txed < len )
Expand All @@ -114,7 +114,7 @@ uint16 usart_tx(const usart_dev *dev, const uint8 *buf, uint16 len)
* @param len Maximum number of bytes to store
* @return Number of bytes received
*/
uint16 usart_rx(const usart_dev *dev, uint8 *buf, uint16 len) {
uint16 usart_rx(const usart_dev_t *dev, uint8 *buf, uint16 len) {
uint16 rxed = 0;
while (usart_rx_available(dev) && rxed < len) {
*buf++ = usart_getc(dev);
Expand All @@ -133,7 +133,7 @@ uint16 usart_rx(const usart_dev *dev, uint8 *buf, uint16 len) {
* @param dev Serial port to send on
* @param val Number to print
*/
void usart_putudec(const usart_dev *dev, uint32 val) {
void usart_putudec(const usart_dev_t *dev, uint32 val) {
char digits[12];
int i = 0;

Expand Down
60 changes: 30 additions & 30 deletions STM32F1/cores/maple/libmaple/usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,26 +429,26 @@ typedef struct {
ring_buffer_t * wb; /**< TX ring buffer */
rcc_clk_id clk_id; /**< RCC clock information */
nvic_irq_num irq_num; /**< USART NVIC interrupt */
} usart_dev;
} usart_dev_t;

/*
* Devices
*/
extern const usart_dev usart1;
extern const usart_dev usart2;
extern const usart_dev usart3;
extern const usart_dev_t usart1;
extern const usart_dev_t usart2;
extern const usart_dev_t usart3;
#define USART1 (&usart1)
#define USART2 (&usart2)
#define USART3 (&usart3)

#ifdef STM32_HIGH_DENSITY
extern const usart_dev uart4;
extern const usart_dev uart5;
extern const usart_dev_t uart4;
extern const usart_dev_t uart5;
#define UART4 (&uart4)
#define UART5 (&uart5)
extern const usart_dev * const usart_devs[5];
extern const usart_dev_t * const usart_devs[5];
#else
extern const usart_dev * const usart_devs[3];
extern const usart_dev_t * const usart_devs[3];
#endif

enum uart_parity_t { UART_PARITY_EVEN = USART_CR1_PS_EVEN, UART_PARITY_ODD = USART_CR1_PS_ODD };
Expand All @@ -462,7 +462,7 @@ enum uart_stop_bits_t { UART_STOP_BITS_0_5 = USART_CR2_STOP_BITS_0_5, UART_STOP_
#define SERIAL_RX_BUFFER_SIZE 64


void usart_init(const usart_dev *dev);
void usart_init(const usart_dev_t *dev);

/* FIXME [PRE 0.0.13] decide if flags are necessary */
/**
Expand All @@ -475,17 +475,17 @@ void usart_init(const usart_dev *dev);
* @param flags Currently ignored
*/

void usart_config(const usart_dev *udev, uint8 rx_pin, uint8 tx_pin, uint8_t flags);
void usart_config_line_coding(const usart_dev *udev, usb_cdcacm_line_coding * lc);
void usart_set_baud_rate(const usart_dev *dev, uint32 baud);
void usart_config(const usart_dev_t *udev, uint8 rx_pin, uint8 tx_pin, uint8_t flags);
void usart_config_line_coding(const usart_dev_t *udev, usb_cdcacm_line_coding_t * lc);
void usart_set_baud_rate(const usart_dev_t *dev, uint32 baud);

void usart_enable(const usart_dev *dev);
void usart_disable(const usart_dev *dev);
void usart_foreach(void(*fn)(const usart_dev *dev));
uint16 usart_tx(const usart_dev *dev, const uint8 *buf, uint16 n);
void usart_tx_fast(const usart_dev *dev, uint8 *buf, uint16 n);
uint16 usart_rx(const usart_dev *dev, uint8 *buf, uint16 n);
void usart_putudec(const usart_dev *dev, uint32 val);
void usart_enable(const usart_dev_t *dev);
void usart_disable(const usart_dev_t *dev);
void usart_foreach(void(*fn)(const usart_dev_t *dev));
uint16 usart_tx(const usart_dev_t *dev, const uint8 *buf, uint16 n);
void usart_tx_fast(const usart_dev_t *dev, uint8 *buf, uint16 n);
uint16 usart_rx(const usart_dev_t *dev, uint8 *buf, uint16 n);
void usart_putudec(const usart_dev_t *dev, uint32 val);

/**
* @brief Disable all serial ports.
Expand All @@ -503,7 +503,7 @@ static inline void usart_disable_all(void) {
* @param dev Serial port to send on.
* @param byte Byte to transmit.
*/
static inline void usart_putc(const usart_dev* dev, char byte) {
static inline void usart_putc(const usart_dev_t* dev, char byte) {
while (!usart_tx(dev, (const uint8*)&byte, 1))
;
}
Expand All @@ -516,7 +516,7 @@ static inline void usart_putc(const usart_dev* dev, char byte) {
* @param dev Serial port to send on
* @param str String to send
*/
static inline void usart_putstr(const usart_dev *dev, const char* str) {
static inline void usart_putstr(const usart_dev_t *dev, const char* str) {
char c;
while ( (c=*str++) != '\0') {
usart_putc(dev, c);
Expand All @@ -533,7 +533,7 @@ static inline void usart_putstr(const usart_dev *dev, const char* str) {
* @return byte read
* @see usart_data_available()
*/
static inline uint8_t usart_getc(const usart_dev *dev) {
static inline uint8_t usart_getc(const usart_dev_t *dev) {
return rb_read(dev->rb);
}

Expand All @@ -543,27 +543,27 @@ static inline uint8_t usart_getc(const usart_dev *dev) {
* @param dev Serial port to read from
* @return byte read
*/
static inline int usart_peek(const usart_dev *dev) {
static inline int usart_peek(const usart_dev_t *dev) {
return rb_peek(dev->rb);
}
/**
* @brief Return the amount of free slots in a serial port's TX buffer.
* @param dev Serial port to check
* @return Number of free slots in dev's TX buffer.
*/
static inline uint16_t usart_tx_available(const usart_dev *dev) {
static inline uint16_t usart_tx_available(const usart_dev_t *dev) {
return rb_write_available(dev->wb);
}
/*
static inline uint8_t * usart_tx_ptr(usart_dev *dev) {
static inline uint8_t * usart_tx_ptr(usart_dev_t *dev) {
return rb_write_ptr(dev->wb);
}
*/
static inline void usart_tx_start(const usart_dev *dev) {
static inline void usart_tx_start(const usart_dev_t *dev) {
dev->regs->CR1 |= USART_CR1_TXEIE;
}
/*
static inline void usart_tx_finish(usart_dev *dev, uint16_t nr) {
static inline void usart_tx_finish(usart_dev_t *dev, uint16_t nr) {
rb_write_finish(dev->txB, nr);
}
*/
Expand All @@ -572,23 +572,23 @@ static inline void usart_tx_finish(usart_dev *dev, uint16_t nr) {
* @param dev Serial port to check
* @return Number of bytes in dev's RX buffer.
*/
static inline uint16 usart_rx_available(const usart_dev *dev) {
static inline uint16 usart_rx_available(const usart_dev_t *dev) {
return rb_read_available(dev->rb);
}

/**
* @brief Discard the contents of a serial port's RX buffer.
* @param dev Serial port whose buffer to empty.
*/
static inline void usart_reset_rx(const usart_dev *dev) {
static inline void usart_reset_rx(const usart_dev_t *dev) {
rb_reset(dev->rb);
}

/**
* @brief Discard the contents of a serial port's RX buffer.
* @param dev Serial port whose buffer to empty.
*/
static inline void usart_reset_tx(const usart_dev *dev) {
static inline void usart_reset_tx(const usart_dev_t *dev) {
rb_reset(dev->wb);
}

Expand Down
Loading

0 comments on commit e41247b

Please sign in to comment.