Skip to content

Make Serialx instance generic #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

menu.pnum=Board part number

menu.xserial=Extra serial instance
menu.xserial=Serial interface
menu.usb=USB interface

menu.opt=Optimize
Expand Down Expand Up @@ -328,37 +328,37 @@ Disco.menu.upload_method.STLink.upload.tool=stlink_upload

################################################################################
# Serialx activation
Nucleo_144.menu.xserial.none=None
Nucleo_144.menu.xserial.SerialAll=All
Nucleo_144.menu.xserial.SerialAll.build.xSerial=-DENABLE_SERIAL1 -DENABLE_SERIAL2
Nucleo_144.menu.xserial.Serial1=Serial1 (if available)
Nucleo_144.menu.xserial.Serial1.build.xSerial=-DENABLE_SERIAL1
Nucleo_144.menu.xserial.Serial2=Serial2 (if available)
Nucleo_144.menu.xserial.Serial2.build.xSerial=-DENABLE_SERIAL2

Nucleo_64.menu.xserial.none=None
Nucleo_64.menu.xserial.SerialAll=All
Nucleo_64.menu.xserial.SerialAll.build.xSerial=-DENABLE_SERIAL1 -DENABLE_SERIAL2
Nucleo_64.menu.xserial.Serial1=Serial1 (if available)
Nucleo_64.menu.xserial.Serial1.build.xSerial=-DENABLE_SERIAL1
Nucleo_64.menu.xserial.Serial2=Serial2 (if available)
Nucleo_64.menu.xserial.Serial2.build.xSerial=-DENABLE_SERIAL2

Nucleo_32.menu.xserial.none=None
Nucleo_32.menu.xserial.SerialAll=All
Nucleo_32.menu.xserial.SerialAll.build.xSerial=-DENABLE_SERIAL1 -DENABLE_SERIAL2
Nucleo_32.menu.xserial.Serial1=Serial1 (if available)
Nucleo_32.menu.xserial.Serial1.build.xSerial=-DENABLE_SERIAL1
Nucleo_32.menu.xserial.Serial2=Serial2 (if available)
Nucleo_32.menu.xserial.Serial2.build.xSerial=-DENABLE_SERIAL2

Disco.menu.xserial.none=None
Disco.menu.xserial.SerialAll=All
Disco.menu.xserial.SerialAll.build.xSerial=-DENABLE_SERIAL1 -DENABLE_SERIAL2
Disco.menu.xserial.Serial1=Serial1 (if available)
Disco.menu.xserial.Serial1.build.xSerial=-DENABLE_SERIAL1
Disco.menu.xserial.Serial2=Serial2 (if available)
Disco.menu.xserial.Serial2.build.xSerial=-DENABLE_SERIAL2
Nucleo_144.menu.xserial.generic=Generic Serial
Nucleo_144.menu.xserial.none=No Serial
Nucleo_144.menu.xserial.none.build.xSerial=-DNO_HWSERIAL
Nucleo_144.menu.xserial.all=All (up to 10)
Nucleo_144.menu.xserial.all.build.xSerial=-DALL_HWSERIAL
Nucleo_144.menu.xserial.third=Enable first third (USART1 to 3 if available)
Nucleo_144.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL

Nucleo_64.menu.xserial.generic=Generic Serial
Nucleo_64.menu.xserial.none=No Serial
Nucleo_64.menu.xserial.none.build.xSerial=-DNO_HWSERIAL
Nucleo_64.menu.xserial.all=All (up to 10)
Nucleo_64.menu.xserial.all.build.xSerial=-DALL_HWSERIAL
Nucleo_64.menu.xserial.third=Enable first third (USART1 to 3 if available)
Nucleo_64.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL

Nucleo_32.menu.xserial.generic=Generic Serial
Nucleo_32.menu.xserial.none=No Serial
Nucleo_32.menu.xserial.none.build.xSerial=-DNO_HWSERIAL
Nucleo_32.menu.xserial.all=All (up to 10)
Nucleo_32.menu.xserial.all.build.xSerial=-DALL_HWSERIAL
Nucleo_32.menu.xserial.third=Enable first third (USART1 to 3 if available)
Nucleo_32.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL

Disco.menu.xserial.generic=Generic Serial
Disco.menu.xserial.none=No Serial
Disco.menu.xserial.none.build.xSerial=-DNO_HWSERIAL
Disco.menu.xserial.all=All (up to 10)
Disco.menu.xserial.all.build.xSerial=-DALL_HWSERIAL
Disco.menu.xserial.third=Enable first third (USART1 to 3 if available)
Disco.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL

# USB connectivity
Nucleo_144.menu.usb.none=None
Expand Down
151 changes: 150 additions & 1 deletion cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,143 @@
#include "Arduino.h"
#include "HardwareSerial.h"

#if !defined(NO_HWSERIAL)
#if defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) ||\
defined(HAVE_HWSERIAL4) || defined(HAVE_HWSERIAL5) || defined(HAVE_HWSERIAL6) ||\
defined(HAVE_HWSERIAL7) || defined(HAVE_HWSERIAL8) || defined(HAVE_HWSERIAL8) ||\
defined(HAVE_HWSERIAL10)
// SerialEvent functions are weak, so when the user doesn't define them,
// the linker just sets their address to 0 (which is checked below).
#if defined(HAVE_HWSERIAL1)
HardwareSerial Serial1(USART1);
void serialEvent1() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL2)
HardwareSerial Serial2(USART2);
void serialEvent2() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL3)
HardwareSerial Serial3(USART3);
void serialEvent3() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL4)
#if defined(USART4)
HardwareSerial Serial4(USART4);
#else
HardwareSerial Serial4(UART4);
#endif
void serialEvent4() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL5)
#if defined(USART5)
HardwareSerial Serial5(USART5);
#else
HardwareSerial Serial5(UART5);
#endif
void serialEvent5() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL6)
HardwareSerial Serial6(USART6);
void serialEvent6() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL7)
#if defined(USART7)
HardwareSerial Serial7(USART7);
#else
HardwareSerial Serial7(UART7);
#endif
void serialEvent7() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL8)
#if defined(USART8)
HardwareSerial Serial8(USART8);
#else
HardwareSerial Serial8(UART8);
#endif
void serialEvent8() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL9)
HardwareSerial Serial9(UART9);
void serialEvent9() __attribute__((weak));
#endif

#if defined(HAVE_HWSERIAL10)
HardwareSerial Serial10(UART10);
void serialEvent10() __attribute__((weak));
#endif

void serialEventRun(void)
{
#if defined(HAVE_HWSERIAL1)
if (serialEvent1 && Serial1.available()) serialEvent1();
#endif
#if defined(HAVE_HWSERIAL2)
if (serialEvent2 && Serial2.available()) serialEvent2();
#endif
#if defined(HAVE_HWSERIAL3)
if (serialEvent3 && Serial3.available()) serialEvent3();
#endif
#if defined(HAVE_HWSERIAL4)
if (serialEvent4 && Serial4.available()) serialEvent4();
#endif
#if defined(HAVE_HWSERIAL5)
if (serialEvent5 && Serial5.available()) serialEvent5();
#endif
#if defined(HAVE_HWSERIAL6)
if (serialEvent6 && Serial6.available()) serialEvent6();
#endif
#if defined(HAVE_HWSERIAL7)
if (serialEvent7 && Serial7.available()) serialEvent7();
#endif
#if defined(HAVE_HWSERIAL8)
if (serialEvent8 && Serial8.available()) serialEvent8();
#endif
#if defined(HAVE_HWSERIAL9)
if (serialEvent9 && Serial9.available()) serialEvent9();
#endif
#if defined(HAVE_HWSERIAL10)
if (serialEventl10 && Serial10.available()) serialEvent10();
#endif
}

// Constructors ////////////////////////////////////////////////////////////////
HardwareSerial::HardwareSerial(PinName _rx, PinName _tx)
{
_serial.pin_rx = _rx;
_serial.pin_tx = _tx;
init();
}

HardwareSerial::HardwareSerial(void* peripheral)
{
// If Serial is defined in variant set
// the Rx/Tx pins for com port if defined
#if defined(Serial) && defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
if (this == &Serial)
{
setRx(PIN_SERIAL_RX);
setTx(PIN_SERIAL_TX);
}
else
#endif
// else get the pins of the first peripheral occurence in PinMap
{
_serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX);
_serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX);
}
init();
}

void HardwareSerial::init(void)
{
_serial.rx_buff = _rx_buffer;
_serial.rx_head = 0;
_serial.rx_tail = 0;
Expand Down Expand Up @@ -87,7 +218,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)

_serial.baudrate = (uint32_t)baud;

// Manage databits
// Manage databitshardware/arduino/avr/cores/arduino/HardwareSerial.cpp
switch(config & 0x07) {
case 0x02:
databits = 6;
Expand Down Expand Up @@ -224,3 +355,21 @@ size_t HardwareSerial::write(uint8_t c)

return 1;
}

void HardwareSerial::setRx(uint32_t _rx) {
_serial.pin_rx = digitalPinToPinName(_rx);
}

void HardwareSerial::setTx(uint32_t _tx) {
_serial.pin_tx = digitalPinToPinName(_tx);
}

void HardwareSerial::setRx(PinName _rx) {
_serial.pin_rx = _rx;
}

void HardwareSerial::setTx(PinName _tx){
_serial.pin_tx = _tx;
}
#endif // HAVE_HWSERIALx
#endif // !NO_HWSERIAL
19 changes: 19 additions & 0 deletions cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class HardwareSerial : public Stream

public:
HardwareSerial(PinName _rx, PinName _tx);
HardwareSerial(void* peripheral);
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
void begin(unsigned long, uint8_t);
void end();
Expand All @@ -118,11 +119,29 @@ class HardwareSerial : public Stream
using Print::write; // pull in write(str) and write(buf, size) from Print
operator bool() { return true; }

void setRx(uint32_t _rx);
void setTx(uint32_t _tx);
void setRx(PinName _rx);
void setTx(PinName _tx);

// Interrupt handlers
static void _rx_complete_irq(serial_t* obj);
static int _tx_complete_irq(serial_t* obj);
private:
void init(void);
};

extern HardwareSerial Serial1;
extern HardwareSerial Serial2;
extern HardwareSerial Serial3;
extern HardwareSerial Serial4;
extern HardwareSerial Serial5;
extern HardwareSerial Serial6;
extern HardwareSerial Serial7;
extern HardwareSerial Serial8;
extern HardwareSerial Serial9;
extern HardwareSerial Serial10;

extern void serialEventRun(void) __attribute__((weak));

#endif
2 changes: 1 addition & 1 deletion cores/arduino/stm32/core_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#ifndef __CALLBACK_H
#define __CALLBACK_H

#include "Arduino.h"
#include "variant.h"

#ifdef __cplusplus
extern "C" {
Expand Down
19 changes: 19 additions & 0 deletions cores/arduino/stm32/pinmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ void* pinmap_peripheral(PinName pin, const PinMap* map) {
return peripheral;
}

PinName pinmap_find_pin(void* peripheral, const PinMap* map) {
while (map->peripheral != NP) {
if (map->peripheral == peripheral)
return map->pin;
map++;
}
return NC;
}

PinName pinmap_pin(void* peripheral, const PinMap* map) {
PinName pin = NC;

if (peripheral != NP) {
pin = pinmap_find_pin(peripheral, map);
}
// else error("pinmap not found for pin");
return pin;
}

uint32_t pinmap_find_function(PinName pin, const PinMap* map) {
while (map->pin != NC) {
if (map->pin == pin)
Expand Down
6 changes: 4 additions & 2 deletions cores/arduino/stm32/pinmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ void pin_function(PinName pin, int function);

PinName pin_pinName(const PinMap* map);

void* pinmap_peripheral(PinName pin, const PinMap* map);
uint32_t pinmap_function(PinName pin, const PinMap* map);
void* pinmap_find_peripheral(PinName pin, const PinMap* map);
void* pinmap_peripheral(PinName pin, const PinMap* map);
PinName pinmap_find_pin(void* peripheral, const PinMap* map);
PinName pinmap_pin(void* peripheral, const PinMap* map);
uint32_t pinmap_find_function(PinName pin, const PinMap* map);
uint32_t pinmap_function(PinName pin, const PinMap* map);
void* pinmap_merge_peripheral(void* a, void* b);

#ifdef __cplusplus
Expand Down
6 changes: 1 addition & 5 deletions cores/arduino/stm32/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@
/** @addtogroup STM32F4xx_System_Private_Includes
* @{
*/
#include "stm32_def.h"
#include "timer.h"
#include "digital_io.h"
#include "clock.h"
#include "analog.h"
#include "variant.h"
#include "board.h"

#ifdef __cplusplus
extern "C" {
Expand Down
13 changes: 11 additions & 2 deletions cores/arduino/stm32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
extern "C" {
#endif

// if DEBUG_UART is not defined assume this is the one
// linked to PIN_SERIAL_TX
#if !defined(DEBUG_UART)
#if defined(PIN_SERIAL_TX)
#define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX)
#else
#define DEBUG_UART NP
#endif
#endif
// @brief uart caracteristics
#if defined(STM32F4xx)
#define UART_NUM (10)
Expand Down Expand Up @@ -398,11 +407,11 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size)
size_t uart_debug_write(uint8_t *data, uint32_t size)
{
uint8_t index = 0;
USART_TypeDef* dbg_uart = DEBUG_UART;
uint32_t tickstart = HAL_GetTick();

for(index = 0; index < UART_NUM; index++) {
if(uart_handlers[index] != NULL) {
if(DEBUG_UART == uart_handlers[index]->Instance) {
if(dbg_uart == uart_handlers[index]->Instance) {
break;
}
}
Expand Down
Loading