Skip to content

Commit

Permalink
Massive refactoring, small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
yh-sb committed Nov 28, 2024
1 parent bfb9a3a commit 2e1be18
Show file tree
Hide file tree
Showing 78 changed files with 371 additions and 239 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ cd mcu-cpp

make
```
**Other targets:**
* `make flash` - Upload firmware to the target
* `make erase` - Erase all memory on the target
* `make reset` - Reset the target
* `make debug` - Start debug session

## Requirements
* [GNU Arm Embedded Toolchain](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads)
Expand All @@ -24,7 +29,7 @@ make
or just for STM32 only: **STM32CubeProgrammer** and **ST-LINK gdbserver** from [STM32CubeCLT](https://www.st.com/en/development-tools/stm32cubeclt.html?dl=redirect) package

## Requirements for ESP32 project
* [GNU Xtensa esp-elf toolchain](https://github.com/espressif/crosstool-NG/releases). For Windows use [xtensa-esp-elf-13.2.0 with hotfix](https://github.com/espressif/crosstool-NG/releases/download/esp-13.2.0_20240530/xtensa-esp-elf-13.2.0_20240530-x86_64-w64-mingw32_hotfix.zip)
* [GNU Xtensa esp-elf toolchain](https://github.com/espressif/crosstool-NG/releases/tag/esp-13.2.0_20240530)
* [Python](https://www.python.org/downloads)
* Install python dependencies:
```sh
Expand Down
2 changes: 1 addition & 1 deletion drivers/include/drivers/dataflash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,4 @@ class dataflash
uint32_t get_timeout(enum cmd cmd, info_t &info);
uint32_t get_chip_erase_timeout(info_t &info);
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/dht.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ class dht
static bool is_crc_valid(const uint8_t *buff);
static value_t parce_data(const uint8_t *buff);
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/ds18b20.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ class ds18b20
SemaphoreHandle_t api_lock;
enum resolution resolution;
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class encoder
uint8_t prev_prev_state;
std::function<void(int8_t diff)> on_change;
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/gpio_pin_debouncer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ class gpio_pin_debouncer
bool state;
size_t cnt;
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/hd44780.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ class hd44780

void delay(uint32_t us);
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/nrf24l01.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ class nrf24l01
bool dyn_payload;
} conf;
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/onewire.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ class onewire
periph::uart &uart;
SemaphoreHandle_t api_lock;
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/sd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ class sd
uint64_t capacity; // in bytes
} info;
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/sd_spi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ class sd_spi : public sd
periph::spi &spi;
periph::gpio &cs;
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/include/drivers/singlewire.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ class singlewire
void fsm_start(uint8_t *buff, uint16_t size);
void fsm_run(bool is_timer_expired);
};
}
} // namespace drv
2 changes: 1 addition & 1 deletion drivers/src/freertos_wrappers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class semaphore_take
private:
const SemaphoreHandle_t &_semaphore;
};
}
} // namespace freertos
16 changes: 8 additions & 8 deletions drivers/src/hd44780.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ using namespace drv;

enum cmd_t
{
CLEAR_DISPLAY = 1 << 0,
RETURN_HOME = 1 << 1,
ENTRY_MODE_SET = 1 << 2,
DISPLAY_ON_OFF_CONTROL = 1 << 3,
CURSOR_OR_DISPLAY_SHIFT = 1 << 4,
FUNCTION_SET = 1 << 5,
SET_CGRAM_ADDRESS = 1 << 6,
SET_DDRAM_ADDRESS = 1 << 7
CLEAR_DISPLAY = 1 << 0,
RETURN_HOME = 1 << 1,
ENTRY_MODE_SET = 1 << 2,
DISPLAY_ON_OFF_CONTROL = 1 << 3,
CURSOR_OR_DISPLAY_SHIFT = 1 << 4,
FUNCTION_SET = 1 << 5,
SET_CGRAM_ADDRESS = 1 << 6,
SET_DDRAM_ADDRESS = 1 << 7
};

// Bits for ENTRY_MODE_SET command
Expand Down
12 changes: 6 additions & 6 deletions drivers/src/nrf24l01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ nrf24l01::~nrf24l01()

cs.set(1);
ce.set(0);
exti.off();
exti.disable();
timer.stop();
xSemaphoreGive(api_lock);
vSemaphoreDelete(api_lock);
Expand Down Expand Up @@ -670,9 +670,9 @@ enum nrf24l01::res nrf24l01::read(packet_t &packet, packet_t *ack)
// Wait for data
task = xTaskGetCurrentTaskHandle();
ce.set(1);
exti.on();
exti.enable();
ulTaskNotifyTake(true, portMAX_DELAY);
exti.off();
exti.disable();
}

// Read received data
Expand Down Expand Up @@ -764,7 +764,7 @@ enum nrf24l01::res nrf24l01::write(void *buff, size_t size, packet_t *ack, bool
}

task = xTaskGetCurrentTaskHandle();
exti.on();
exti.enable();

ce.set(1);
/* is_continuous_tx 0: go to standby-1 mode after transmitting one packet
Expand All @@ -783,10 +783,10 @@ enum nrf24l01::res nrf24l01::write(void *buff, size_t size, packet_t *ack, bool
{
ce.set(0);
}
exti.off();
exti.disable();
return res::no_response;
}
exti.off();
exti.disable();

status_reg_t status;
res = read_reg(reg::STATUS, &status);
Expand Down
16 changes: 8 additions & 8 deletions drivers/src/singlewire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ singlewire::singlewire(periph::gpio &gpio, periph::timer &timer, periph::exti &e
singlewire::~singlewire()
{
timer.stop();
exti.off();
exti.disable();
}

enum singlewire::res singlewire::read(uint8_t *buff, uint16_t size)
Expand Down Expand Up @@ -58,15 +58,15 @@ void singlewire::fsm_run(bool is_timer_expired)
fsm.state = state::wait_response_start;
gpio.set(1);
exti.trigger(periph::exti::trigger::falling);
exti.on();
exti.enable();
timer.timeout(std::chrono::microseconds(timeouts[state::wait_response_start]));
timer.start();
break;

case state::wait_response_start:
if(is_timer_expired)
{
exti.off();
exti.disable();
res = res::no_device;
goto Exit;
}
Expand All @@ -81,7 +81,7 @@ void singlewire::fsm_run(bool is_timer_expired)
case state::wait_response_end:
if(is_timer_expired)
{
exti.off();
exti.disable();
res = res::device_error;
goto Exit;
}
Expand All @@ -96,7 +96,7 @@ void singlewire::fsm_run(bool is_timer_expired)
case state::wait_bit_start_low:
if(is_timer_expired)
{
exti.off();
exti.disable();
res = res::read_error;
goto Exit;
}
Expand All @@ -111,7 +111,7 @@ void singlewire::fsm_run(bool is_timer_expired)
case state::wait_bit_start_high:
if(is_timer_expired)
{
exti.off();
exti.disable();
res = res::read_error;
goto Exit;
}
Expand All @@ -128,7 +128,7 @@ void singlewire::fsm_run(bool is_timer_expired)
{
fsm.buff[fsm.byte] |= 1 << fsm.bit;

fsm.state = state::wait_bit_start_low;
fsm.state = state::wait_bit_start_low;
exti.trigger(periph::exti::trigger::falling);
timer.timeout(std::chrono::microseconds(timeouts[state::wait_bit_check]));
timer.start();
Expand Down Expand Up @@ -158,7 +158,7 @@ void singlewire::fsm_run(bool is_timer_expired)
if(fsm.byte == fsm.size)
{
timer.stop();
exti.off();
exti.disable();
res = res::ok;
goto Exit;
}
Expand Down
2 changes: 0 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ int main(int argc, char *argv[])

xTaskCreate(heartbeat_task, "heartbeat", configMINIMAL_STACK_SIZE, &green_led, 1, nullptr);
vTaskStartScheduler();

return 0;
}
4 changes: 2 additions & 2 deletions periph/include/periph/exti.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class exti

virtual void set_callback(std::function<void()> on_interrupt) = 0;

virtual void on() = 0;
virtual void enable() = 0;

virtual void off() = 0;
virtual void disable() = 0;

virtual void trigger(trigger trigger) = 0;

Expand Down
2 changes: 1 addition & 1 deletion periph/include/periph/rtc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class rtc

static void bckp_read(uint8_t addr, void *buff, size_t size);

static void set_alarm_callback(std::function<void(const std::tm &)> on_alarm);
static void set_alarm_callback(std::function<void(const std::tm &tm)> on_alarm);
static void set_alarm(const std::tm &tm);

static bool is_valid(const std::tm &tm);
Expand Down
4 changes: 2 additions & 2 deletions periph/include/periph/spi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class spi

enum class bit_order : uint8_t
{
msb,
lsb
msb, // Most significant bit first
lsb // Least significant bit first
};

enum class res : int8_t
Expand Down
2 changes: 1 addition & 1 deletion periph/rp2040/src/gpio_rp2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using namespace periph;

constexpr auto pins = 30; // Total number of pins
constexpr auto pins = 30; // Total number of gpio pins

gpio_rp2040::gpio_rp2040(uint8_t pin, enum mode mode, bool state):
_pin(pin),
Expand Down
8 changes: 4 additions & 4 deletions periph/stm32f0/include/periph/dma_stm32f0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class dma_stm32f0
};

/**
* @brief Construct a new dma stm32f0 object
* @brief Construct dma (direct memory access) object
*
* @param dma Number of DMA controller. Can be 1 or 2
* @param channel Number of DMA channel. Can be 1 to 7
* @param direction Direction of data transfer
* @param dma Number of DMA controller. Can be 1 or 2
* @param channel Number of DMA channel. Can be 1 to 7
* @param direction Direction of data transfer
* @param increment_size Increment size of the source and destination addresses in bits. Can be 8, 16 or 32
*/
dma_stm32f0(uint8_t dma, uint8_t channel, enum direction direction, uint8_t increment_size);
Expand Down
6 changes: 3 additions & 3 deletions periph/stm32f0/include/periph/exti_stm32f0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class exti_stm32f0 : public exti

void set_callback(std::function<void()> on_interrupt) final;

void on() final;
void off() final;
void enable() final;
void disable() final;

void trigger(enum trigger trigger) final;
enum trigger trigger() final { return _trigger; }
Expand All @@ -29,4 +29,4 @@ class exti_stm32f0 : public exti
std::function<void()> on_interrupt;
friend void ::exti_irq_hndlr(exti_stm32f0 *obj);
};
}
} // namespace periph
10 changes: 9 additions & 1 deletion periph/stm32f0/include/periph/gpio_stm32f0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ class gpio_stm32f0 : public gpio
a, b, c, d, e, f
};

gpio_stm32f0(port port, uint8_t pin, enum mode mode, bool state = false);
/**
* @brief Construct gpio (general-purpose input/output) object
*
* @param port GPIO port
* @param pin GPIO pin. Can be 0 to 15
* @param mode GPIO mode
* @param state Initial state of the pin
*/
gpio_stm32f0(enum port port, uint8_t pin, enum mode mode, bool state = false);
~gpio_stm32f0();

void set(bool state) final;
Expand Down
14 changes: 14 additions & 0 deletions periph/stm32f0/include/periph/spi_stm32f0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ namespace periph
class spi_stm32f0 : public spi
{
public:
/**
* @brief Construct spi (serial peripheral interface) object
*
* @param spi Number of SPI interface. Can be 1 to 2
* @param baudrate Baudrate in Hz
* @param cpol Clock polarity
* @param cpha Clock phase
* @param bit_order Bit order
* @param dma_write DMA instance for write operations
* @param dma_read DMA instance for read operations
* @param mosi GPIO pin for master output slave input
* @param miso GPIO pin for master input slave output
* @param clk GPIO pin for clock
*/
spi_stm32f0(uint8_t spi, uint32_t baudrate, enum cpol cpol, enum cpha cpha,
enum bit_order bit_order, dma_stm32f0 &dma_write, dma_stm32f0 &dma_read,
gpio_stm32f0 &mosi, gpio_stm32f0 &miso, gpio_stm32f0 &clk);
Expand Down
12 changes: 3 additions & 9 deletions periph/stm32f0/include/periph/timer_stm32f0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,19 @@ namespace periph
class timer_stm32f0 : public timer
{
public:
static constexpr uint8_t timers = 17; /**< The total number of timers */
static constexpr uint8_t timers = 17; // The total number of timers

/**
* @brief Construct a new timer stm32f0 object
* @brief Construct timer object
*
* @param timer The timer instance to be used:
* - 1: Advanced-control timer TIM1
* - 2: General-purpose timer TIM2
* - 3: General-purpose timer TIM3
*
*
* - 6: Basic timer TIM6
* - 7: Basic timer TIM7
*
*
*
*
*
*
* - 14: General-purpose timer TIM14
* - 15: General-purpose timer TIM15
* - 16: General-purpose timer TIM16
Expand All @@ -56,4 +50,4 @@ class timer_stm32f0 : public timer
static void calc_clk(uint8_t tim, uint32_t usec, uint16_t &psc, uint16_t &arr);
friend void ::tim_irq_hndlr(timer_stm32f0 *obj);
};
}
} // namespace periph
Loading

0 comments on commit 2e1be18

Please sign in to comment.