aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--client/client.pro21
-rw-r--r--client/dbconnector.cpp49
-rw-r--r--client/dbconnector.h34
-rw-r--r--client/dbconnector.ui141
-rw-r--r--client/main.cpp57
-rw-r--r--client/main.h8
-rw-r--r--client/mainwindow.cpp50
-rw-r--r--client/mainwindow.h42
-rw-r--r--client/mainwindow.ui128
-rw-r--r--scripts/db-init.sql9
-rw-r--r--stm32f091/FreeRTOSConfig.h4
-rw-r--r--stm32f091/main.c25
-rw-r--r--stm32f091/main.h3
-rw-r--r--stm32f091/sensor.c50
-rw-r--r--stm32f091/sensor.h20
-rw-r--r--stm32f091/setup.c136
-rw-r--r--stm32f091/setup.h26
-rw-r--r--stm32f091/stm32f0xx_hal_msp.c32
-rw-r--r--stm32f091/stm32f0xx_hal_msp.h9
20 files changed, 748 insertions, 99 deletions
diff --git a/.gitignore b/.gitignore
index cc3cefc..1c68c8a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,9 +2,10 @@ copyright
**/*.o
stm32f091/main.elf
stm32f091/main.bin
-stm32f091/.cache
+**/.cache
**/compile_commands.json
client/makefile
client/client
+client/moc_*
.qmake.stash
.vscode/.cortex-debug.registers.state.json
diff --git a/client/client.pro b/client/client.pro
index cd0990a..0de0a33 100644
--- a/client/client.pro
+++ b/client/client.pro
@@ -1,14 +1,23 @@
-QT += charts
+QT += core gui sql charts
HEADERS += \
- chart.h \
- chartview.h
+ dbconnector.h \
+ main.h \
+ mainwindow.h
+# chart.h \
+# chartview.h
SOURCES += \
+ dbconnector.cpp \
main.cpp \
- chart.cpp \
- chartview.cpp
+ mainwindow.cpp
+# chart.cpp \
+# chartview.cpp
+
+
+FORMS += \
+ dbconnector.ui \
+ mainwindow.ui
-target.path = $$[QT_INSTALL_EXAMPLES]/charts/zoomlinechart
INSTALLS += target
CONFIG += force_debug_info
diff --git a/client/dbconnector.cpp b/client/dbconnector.cpp
new file mode 100644
index 0000000..5ed1efc
--- /dev/null
+++ b/client/dbconnector.cpp
@@ -0,0 +1,49 @@
+#include "dbconnector.h"
+#include "ui_dbconnector.h"
+#include "main.h"
+
+//#include "mainwindow.h"
+
+dbConnector::dbConnector(QWidget *parent) :
+ QDialog(parent),
+ ui(new Ui::dbConnector)
+{
+ ui->setupUi(this);
+}
+
+dbConnector::~dbConnector()
+{
+ delete ui;
+}
+
+void dbConnector::on_pushButton_cancel_clicked()
+{
+ dbConnector::~dbConnector();
+}
+
+void dbConnector::on_pushButton_login_clicked()
+{
+ QString hostname = ui->lineEdit_adress->text();
+ QString username = ui->lineEdit_username->text();
+ QString password = ui->lineEdit_password->text();
+ QString databaseName = "thecrapbox";
+
+// QSqlDatabase db = MainWindow.loginDb(adress, username, password);
+
+// QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
+// return;
+ dbRef.setHostName(hostname);
+ dbRef.setUserName(username);
+ dbRef.setPassword(password);
+ dbRef.setDatabaseName("thecrapbox");
+
+ if(dbRef.open()){
+ QMessageBox::information(this, "Connection", "GREAT SUCCES!");
+ } else {
+ QMessageBox::warning(this, "No connection", "Failed to connect");
+ }
+}
+
+
+
+
diff --git a/client/dbconnector.h b/client/dbconnector.h
new file mode 100644
index 0000000..cd722eb
--- /dev/null
+++ b/client/dbconnector.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <QDialog>
+
+#include <QMessageBox>
+//#include <QtSql>
+//#include <QSqlDatabase>
+
+//#include "database.h"
+
+namespace Ui {
+class dbConnector;
+}
+
+class dbConnector : public QDialog
+{
+ Q_OBJECT
+
+public:
+ explicit dbConnector(QWidget *parent = nullptr);
+ ~dbConnector();
+
+private slots:
+// void on_pushButton_clicked();
+
+ void on_pushButton_cancel_clicked();
+
+ void on_pushButton_login_clicked();
+
+private:
+ Ui::dbConnector *ui;
+
+// Database database;
+};
diff --git a/client/dbconnector.ui b/client/dbconnector.ui
new file mode 100644
index 0000000..21d241f
--- /dev/null
+++ b/client/dbconnector.ui
@@ -0,0 +1,141 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>dbConnector</class>
+ <widget class="QDialog" name="dbConnector">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>362</width>
+ <height>273</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Dialog</string>
+ </property>
+ <widget class="QWidget" name="formLayoutWidget">
+ <property name="geometry">
+ <rect>
+ <x>60</x>
+ <y>60</y>
+ <width>241</width>
+ <height>173</height>
+ </rect>
+ </property>
+ <layout class="QFormLayout" name="formLayout">
+ <item row="0" column="0">
+ <widget class="QLabel" name="label_3">
+ <property name="text">
+ <string>Adress</string>
+ </property>
+ </widget>
+ </item>
+ <item row="0" column="1">
+ <widget class="QLineEdit" name="lineEdit_adress">
+ <property name="text">
+ <string>localhost</string>
+ </property>
+ <property name="placeholderText">
+ <string>Hostname/IP-Adress</string>
+ </property>
+ </widget>
+ </item>
+ <item row="1" column="0">
+ <widget class="QLabel" name="label_4">
+ <property name="text">
+ <string>Database</string>
+ </property>
+ </widget>
+ </item>
+ <item row="1" column="1">
+ <widget class="QLineEdit" name="lineEdit_database">
+ <property name="placeholderText">
+ <string>Database name</string>
+ </property>
+ </widget>
+ </item>
+ <item row="2" column="0" colspan="2">
+ <widget class="Line" name="line">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ </widget>
+ </item>
+ <item row="3" column="0">
+ <widget class="QLabel" name="label">
+ <property name="text">
+ <string>Username</string>
+ </property>
+ </widget>
+ </item>
+ <item row="3" column="1">
+ <widget class="QLineEdit" name="lineEdit_username">
+ <property name="text">
+ <string>root</string>
+ </property>
+ <property name="echoMode">
+ <enum>QLineEdit::PasswordEchoOnEdit</enum>
+ </property>
+ <property name="placeholderText">
+ <string>Username</string>
+ </property>
+ </widget>
+ </item>
+ <item row="4" column="0">
+ <widget class="QLabel" name="label_2">
+ <property name="text">
+ <string>Password</string>
+ </property>
+ </widget>
+ </item>
+ <item row="4" column="1">
+ <widget class="QLineEdit" name="lineEdit_password">
+ <property name="font">
+ <font>
+ <underline>false</underline>
+ <strikeout>false</strikeout>
+ <kerning>true</kerning>
+ </font>
+ </property>
+ <property name="text">
+ <string/>
+ </property>
+ <property name="echoMode">
+ <enum>QLineEdit::Password</enum>
+ </property>
+ <property name="placeholderText">
+ <string>Password</string>
+ </property>
+ </widget>
+ </item>
+ <item row="5" column="0">
+ <widget class="QLabel" name="connectLabel">
+ <property name="text">
+ <string>Connect</string>
+ </property>
+ </widget>
+ </item>
+ <item row="5" column="1">
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <item>
+ <widget class="QPushButton" name="pushButton_login">
+ <property name="text">
+ <string>Login</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="pushButton_cancel">
+ <property name="text">
+ <string>Cancel</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/client/main.cpp b/client/main.cpp
index 28a3d91..84706bf 100644
--- a/client/main.cpp
+++ b/client/main.cpp
@@ -1,42 +1,33 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the Qt Charts module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 or (at your option) any later version
-** approved by the KDE Free Qt Foundation. The licenses are as published by
-** the Free Software Foundation and appearing in the file LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include "chart.h"
-#include "chartview.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCore/QtMath>
#include <QtCore/QRandomGenerator>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
+#include <QApplication>
+#include <QMessageBox>
+#include <iostream>
+#include <stdio.h>
+
+#include "chart.h"
+#include "chartview.h"
+#include "mainwindow.h"
+#include "main.h"
+#include "ui_mainwindow.h"
-QT_CHARTS_USE_NAMESPACE
+QSqlDatabase dbRef = QSqlDatabase();
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ MainWindow w;
+ dbRef = QSqlDatabase::addDatabase("QMYSQL");
+
+ w.show();
+ return a.exec();
+}
+
+/* QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
@@ -69,4 +60,4 @@ int main(int argc, char *argv[])
window.show();
return a.exec();
-}
+} */
diff --git a/client/main.h b/client/main.h
new file mode 100644
index 0000000..010f2e6
--- /dev/null
+++ b/client/main.h
@@ -0,0 +1,8 @@
+#pragma once
+
+#include <QSql>
+#include <QSqlDatabase>
+
+int main(int argc, char *argv[]);
+extern QSqlDatabase dbRef;
+
diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp
new file mode 100644
index 0000000..955307e
--- /dev/null
+++ b/client/mainwindow.cpp
@@ -0,0 +1,50 @@
+#include <QSqlDatabase>
+
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+#include "main.h"
+
+MainWindow::MainWindow(QWidget *parent)
+ : QMainWindow(parent)
+ , ui(new Ui::MainWindow)
+{
+// db = QSqlDatabase::addDatabase("QMYSQL");
+ ui->setupUi(this);
+}
+
+MainWindow::~MainWindow()
+{
+ dbRef.close();
+ delete ui;
+}
+
+void MainWindow::on_actionAbout_triggered()
+{
+ QMessageBox::warning(this, "Oops..", "Task Failed succesfully ;)");
+}
+
+
+void MainWindow::on_pushButton_clicked()
+{
+// dbRef = QSqlDatabase::addDatabase("QMYSQL");
+// dbRef.setHostName("localhost");
+// dbRef.setUserName("root");
+// dbRef.setPassword("Ab12345!");
+// dbRef.setDatabaseName("thecrapbox");
+
+ if(dbRef.open()){
+ QMessageBox::information(this, "Connection", "GREAT SUCCES!");
+ pQueryModel = new QSqlQueryModel();
+ pQueryModel->setQuery("SELECT * FROM opleiding;");
+ ui->tableView->setModel(pQueryModel);
+ } else {
+ QMessageBox::warning(this, "No connection", "Failed to connect");
+ }
+}
+
+void MainWindow::on_actionConnection_triggered()
+{
+ _dbConenctor = new dbConnector(this);
+ _dbConenctor->show();
+}
+
diff --git a/client/mainwindow.h b/client/mainwindow.h
new file mode 100644
index 0000000..1e73657
--- /dev/null
+++ b/client/mainwindow.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <QMainWindow>
+
+//#include "database.h"
+#include "dbconnector.h"
+
+#include <QMessageBox>
+#include <QtSql>
+#include <QSqlDatabase>
+#include "main.h"
+
+
+QT_BEGIN_NAMESPACE
+namespace Ui { class MainWindow; }
+QT_END_NAMESPACE
+
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ MainWindow(QWidget *parent = nullptr);
+ ~MainWindow();
+
+private slots:
+ void on_actionAbout_triggered();
+
+ void on_pushButton_clicked();
+
+ void on_actionConnection_triggered();
+
+private:
+ Ui::MainWindow *ui;
+
+ dbConnector *_dbConenctor;
+// QSqlDatabase db;
+
+ QSqlQueryModel *pQueryModel;
+
+// Database database;
+};
diff --git a/client/mainwindow.ui b/client/mainwindow.ui
new file mode 100644
index 0000000..f827c85
--- /dev/null
+++ b/client/mainwindow.ui
@@ -0,0 +1,128 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>600</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>MainWindow</string>
+ </property>
+ <widget class="QWidget" name="centralwidget">
+ <widget class="QPushButton" name="pushButton">
+ <property name="enabled">
+ <bool>true</bool>
+ </property>
+ <property name="geometry">
+ <rect>
+ <x>310</x>
+ <y>0</y>
+ <width>121</width>
+ <height>41</height>
+ </rect>
+ </property>
+ <property name="text">
+ <string>DoesSomething</string>
+ </property>
+ </widget>
+ <widget class="QWidget" name="verticalLayoutWidget">
+ <property name="geometry">
+ <rect>
+ <x>-10</x>
+ <y>40</y>
+ <width>801</width>
+ <height>401</height>
+ </rect>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <widget class="QTableView" name="tableView"/>
+ </item>
+ </layout>
+ </widget>
+ </widget>
+ <widget class="QMenuBar" name="menubar">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>21</height>
+ </rect>
+ </property>
+ <widget class="QMenu" name="menuAbouy">
+ <property name="title">
+ <string>Home</string>
+ </property>
+ <addaction name="actionAbout"/>
+ <addaction name="actionRefresh"/>
+ </widget>
+ <widget class="QMenu" name="menuTEMP">
+ <property name="title">
+ <string>TEMP</string>
+ </property>
+ <addaction name="actionLOAD"/>
+ <addaction name="actionQuerry"/>
+ </widget>
+ <widget class="QMenu" name="menuDatabase">
+ <property name="title">
+ <string>Database</string>
+ </property>
+ <addaction name="actionConnection"/>
+ <addaction name="actionDisconnenct"/>
+ <addaction name="actionStatus"/>
+ </widget>
+ <addaction name="menuAbouy"/>
+ <addaction name="menuTEMP"/>
+ <addaction name="menuDatabase"/>
+ </widget>
+ <widget class="QStatusBar" name="statusbar"/>
+ <action name="actionAbout">
+ <property name="text">
+ <string>About</string>
+ </property>
+ </action>
+ <action name="actionRefresh">
+ <property name="text">
+ <string>Refresh</string>
+ </property>
+ </action>
+ <action name="actionLOAD">
+ <property name="text">
+ <string>Load</string>
+ </property>
+ </action>
+ <action name="actionQuerry">
+ <property name="text">
+ <string>Query</string>
+ </property>
+ </action>
+ <action name="actionConnection">
+ <property name="text">
+ <string>Connect</string>
+ </property>
+ </action>
+ <action name="actionDisconnenct">
+ <property name="text">
+ <string>Disconnenct</string>
+ </property>
+ </action>
+ <action name="actionQuerry_2">
+ <property name="text">
+ <string>Querry</string>
+ </property>
+ </action>
+ <action name="actionStatus">
+ <property name="text">
+ <string>Status</string>
+ </property>
+ </action>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/scripts/db-init.sql b/scripts/db-init.sql
new file mode 100644
index 0000000..4c4c4d4
--- /dev/null
+++ b/scripts/db-init.sql
@@ -0,0 +1,9 @@
+CREATE SCHEMA `WSdb`;
+CREATE TABLE `DAB1Pract1`.`tblMain` (
+ `ID` INT GENERATED ALWAYS AS (),
+ `temperature` DECIMAL(5,2) NULL,
+ `humidity` DECIMAL(5,2) NULL,
+ `pressure` DECIMAL(5,2) NULL,
+ `time` DATETIME NULL,
+ PRIMARY KEY (`ID`),
+ UNIQUE INDEX `ID_UNIQUE` (`ID` ASC) VISIBLE);
diff --git a/stm32f091/FreeRTOSConfig.h b/stm32f091/FreeRTOSConfig.h
index b1a692a..223cf8b 100644
--- a/stm32f091/FreeRTOSConfig.h
+++ b/stm32f091/FreeRTOSConfig.h
@@ -50,7 +50,7 @@
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
- // void xPortSysTickHandler(void);
+ void xPortSysTickHandler(void);
#endif
#define configUSE_PREEMPTION 1
#define configSUPPORT_STATIC_ALLOCATION 1
@@ -119,7 +119,7 @@ standard names. */
/* IMPORTANT: This define is commented when used with STM32Cube firmware, when the timebase source is SysTick,
to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
-#define xPortSysTickHandler SysTick_Handler
+/* #define xPortSysTickHandler SysTick_Handler */
/* USER CODE BEGIN Defines */
/* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */
diff --git a/stm32f091/main.c b/stm32f091/main.c
index 6d1fb38..9235f1b 100644
--- a/stm32f091/main.c
+++ b/stm32f091/main.c
@@ -1,30 +1,23 @@
#include <FreeRTOS.h>
#include <task.h>
-#include <stm32f0xx.h>
-#include <stdint.h>
#include <stm32f0xx_hal.h>
-void task_1() {
- uint8_t led = 1;
-
- while (1) {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, led);
- led ^= 1;
-
- vTaskDelay(1000 / portTICK_RATE_MS);
- }
-}
+#include "main.h"
+#include "setup.h"
+#include "sensor.h"
+#include "backlog.h"
int main() {
- HAL_Init();
- __HAL_RCC_GPIOA_CLK_ENABLE();
-
+ ws_io_setup();
HAL_GPIO_Init(GPIOA, &(GPIO_InitTypeDef) {
.Pin = GPIO_PIN_5,
.Mode = GPIO_MODE_OUTPUT_PP,
.Pull = GPIO_NOPULL
});
- xTaskCreate(task_1, "task1", 128, NULL, 1, NULL);
+ ws_backlog_alloc(24 * 60);
+ ws_sensor_read();
+
+ xTaskCreate(ws_sensor_read_task, "sensor", 128, NULL, 1, NULL);
vTaskStartScheduler();
}
diff --git a/stm32f091/main.h b/stm32f091/main.h
new file mode 100644
index 0000000..9595166
--- /dev/null
+++ b/stm32f091/main.h
@@ -0,0 +1,3 @@
+#pragma once
+
+int main();
diff --git a/stm32f091/sensor.c b/stm32f091/sensor.c
new file mode 100644
index 0000000..77ade64
--- /dev/null
+++ b/stm32f091/sensor.c
@@ -0,0 +1,50 @@
+#include <FreeRTOS.h>
+#include <task.h>
+#include <stm32f0xx_hal.h>
+
+#include "setup.h"
+#include "sensor.h"
+#include "backlog.h"
+
+#define SI7021_ADDRESS ((uint16_t)(0x40) << 1)
+#define REG_TEMP ((uint8_t)(0xE3))
+#define REG_HUM ((uint8_t)(0xF5))
+
+uint8_t ws_sensor_temperature() {
+ HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
+ uint8_t buf[12];
+ int16_t val;
+ float temp_c;
+ buf[0] = REG_TEMP;
+ HAL_I2C_Master_Transmit(&hi2c1, SI7021_ADDRESS, buf, 1, HAL_MAX_DELAY);
+ HAL_I2C_Master_Receive(&hi2c1, SI7021_ADDRESS, buf, 2, HAL_MAX_DELAY);
+ val = ((int16_t)buf[0]<< 8 ) | (buf[1]);
+ temp_c= ((175.72*val)/65536) - 46.85;
+
+ return (uint8_t) temp_c; //TODO: convert with range -> util.h
+}
+
+uint8_t ws_sensor_humidity() {
+ return 0x00;
+}
+
+uint8_t ws_sensor_atmospheric_pressure() {
+ return 0x00;
+}
+
+void ws_sensor_read() {
+ ws_s_backlog_record record = {
+ .sens_temperature = ws_sensor_temperature(),
+ .sens_atm_pressure = ws_sensor_atmospheric_pressure(),
+ .sens_humidity = ws_sensor_humidity()
+ };
+ ws_backlog_add_record(record);
+}
+
+void ws_sensor_read_task() {
+ while (1) {
+ ws_sensor_read();
+ vTaskDelay(portTICK_PERIOD_MS * 1000 * 1);
+ }
+}
+
diff --git a/stm32f091/sensor.h b/stm32f091/sensor.h
new file mode 100644
index 0000000..802656b
--- /dev/null
+++ b/stm32f091/sensor.h
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <stdint.h>
+
+/** Get current temperature measurement */
+uint8_t ws_sensor_temperature();
+/** Get current humidity measurement */
+uint8_t ws_sensor_humidity();
+/** Get current atmospheric pressure measurement */
+uint8_t ws_sensor_atmospheric_pressure();
+
+/**
+ * Put measurements from every sensor into a `ws_s_backlog_record`, and commit
+ * that struct to the database
+ */
+void ws_sensor_read();
+
+/** FreeRTOS task that runs `ws_sensor_read` every minute */
+void ws_sensor_read_task();
+
diff --git a/stm32f091/setup.c b/stm32f091/setup.c
new file mode 100644
index 0000000..ed4ee0b
--- /dev/null
+++ b/stm32f091/setup.c
@@ -0,0 +1,136 @@
+#include <stm32f0xx_hal.h>
+#include <stm32f0xx_hal_rcc.h>
+#include <stm32f0xx_hal_i2c.h>
+#include <stm32f0xx_hal_uart.h>
+#include <FreeRTOS.h>
+#include <task.h>
+
+#include "setup.h"
+
+I2C_HandleTypeDef hi2c1;
+UART_HandleTypeDef huart2;
+
+static void ws_io_clock_setup();
+static void ws_io_i2c_setup();
+static void ws_io_usart2_setup();
+static void ws_setup_error_handler();
+
+void ws_io_setup() {
+ HAL_Init();
+
+ ws_io_clock_setup();
+ ws_io_i2c_setup();
+ ws_io_usart2_setup();
+}
+
+static void ws_io_clock_setup() {
+ RCC_OscInitTypeDef RCC_OscInitStruct = {0};
+ RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
+ RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
+
+ /** Initializes the RCC Oscillators according to the specified parameters in
+ * the RCC_OscInitTypeDef structure.
+ */
+ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
+ RCC_OscInitStruct.HSIState = RCC_HSI_ON;
+ RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
+ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+ RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
+ RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
+ RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;
+ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) return ws_setup_error_handler();
+
+ /** Initializes the CPU, AHB and APB buses clocks */
+ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
+ |RCC_CLOCKTYPE_PCLK1;
+ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
+ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
+ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
+
+ if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) return ws_setup_error_handler();
+ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1;
+ PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
+ PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
+ if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) return ws_setup_error_handler();
+}
+
+static void ws_io_i2c_setup() {
+ hi2c1.Instance = I2C1;
+ hi2c1.Init.Timing = 0x2000090E;
+ hi2c1.Init.OwnAddress1 = 0;
+ hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
+ hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
+ hi2c1.Init.OwnAddress2 = 0;
+ hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
+ hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
+ hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
+ if (HAL_I2C_Init(&hi2c1) != HAL_OK) return ws_setup_error_handler();
+ if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) return ws_setup_error_handler();
+ if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) return ws_setup_error_handler();
+}
+
+static void ws_io_usart2_setup() {
+ huart2.Instance = USART2;
+ huart2.Init.BaudRate = 115200;
+ huart2.Init.WordLength = UART_WORDLENGTH_8B;
+ huart2.Init.StopBits = UART_STOPBITS_1;
+ huart2.Init.Parity = UART_PARITY_NONE;
+ huart2.Init.Mode = UART_MODE_TX_RX;
+ huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+ huart2.Init.OverSampling = UART_OVERSAMPLING_16;
+ huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
+ huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
+ if (HAL_UART_Init(&huart2) != HAL_OK) return ws_setup_error_handler();
+}
+
+void HAL_MspInit() {
+ __HAL_RCC_SYSCFG_CLK_ENABLE();
+ __HAL_RCC_PWR_CLK_ENABLE();
+ HAL_NVIC_SetPriority(PendSV_IRQn, 3, 0);
+}
+
+void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) {
+ if(hi2c->Instance != I2C1) return;
+
+ GPIO_InitTypeDef GPIO_InitStruct = {0};
+ __HAL_RCC_GPIOB_CLK_ENABLE();
+ GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; //TODO: use #defines in setup.h
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
+ GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
+ HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+ __HAL_RCC_I2C1_CLK_ENABLE();
+}
+
+void HAL_UART_MspInit(UART_HandleTypeDef *huart) {
+ if(huart->Instance != USART2) return;
+
+ GPIO_InitTypeDef GPIO_InitStruct = {0};
+ __HAL_RCC_USART2_CLK_ENABLE();
+ __HAL_RCC_GPIOA_CLK_ENABLE();
+ GPIO_InitStruct.Pin = WS_PINOUT_USART_TX_PIN | WS_PINOUT_USART_RX_PIN;
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
+ GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
+ HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+}
+
+void SysTick_Handler() {
+ HAL_IncTick();
+ if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) xPortSysTickHandler();
+}
+
+static void ws_setup_error_handler() {
+ __disable_irq();
+ for(;;);
+}
+
+void NMI_Handler() {
+ for(;;);
+}
+
+void HardFault_Handler() {
+ for(;;);
+}
diff --git a/stm32f091/setup.h b/stm32f091/setup.h
new file mode 100644
index 0000000..713ed58
--- /dev/null
+++ b/stm32f091/setup.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <stm32f0xx_hal.h>
+#include <stm32f0xx_hal_i2c.h>
+#include <stm32f0xx_hal_uart.h>
+
+#define WS_PINOUT_I2C_SDA_PIN GPIO_PIN_9
+#define WS_PINOUT_I2C_SDA_PORT GPIOB
+#define WS_PINOUT_I2C_SCL_PIN GPIO_PIN_8
+#define WS_PINOUT_I2C_SCL_PORT GPIOB
+#define WS_PINOUT_USART_RX_PIN GPIO_PIN_3
+#define WS_PINOUT_USART_RX_PORT GPIOA
+#define WS_PINOUT_USART_TX_PIN GPIO_PIN_2
+#define WS_PINOUT_USART_TX_PORT GPIOA
+
+extern I2C_HandleTypeDef hi2c1;
+extern UART_HandleTypeDef huart2;
+
+void ws_io_setup();
+
+void NMI_Handler();
+void HardFault_Handler();
+void SysTick_Handler();
+void HAL_MspInit();
+void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c);
+void HAL_UART_MspInit(UART_HandleTypeDef *huart);
diff --git a/stm32f091/stm32f0xx_hal_msp.c b/stm32f091/stm32f0xx_hal_msp.c
deleted file mode 100644
index 8fd7330..0000000
--- a/stm32f091/stm32f0xx_hal_msp.c
+++ /dev/null
@@ -1,32 +0,0 @@
-#include "stm32f0xx_hal_msp.h"
-
-void HAL_MspInit(void) {
- return;
- __HAL_RCC_SYSCFG_CLK_ENABLE();
- __HAL_RCC_PWR_CLK_ENABLE();
- HAL_NVIC_SetPriority(PendSV_IRQn, 3, 0);
-}
-
-void HAL_UART_MspInit(UART_HandleTypeDef* huart) {
- return;
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(huart->Instance==USART2) {
- __HAL_RCC_USART2_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
-}
-
-void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) {
- return;
- if(huart->Instance==USART2) {
- __HAL_RCC_USART2_CLK_DISABLE();
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
- }
-}
-
diff --git a/stm32f091/stm32f0xx_hal_msp.h b/stm32f091/stm32f0xx_hal_msp.h
deleted file mode 100644
index 3ffdf48..0000000
--- a/stm32f091/stm32f0xx_hal_msp.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#pragma once
-
-#include <stm32f0xx_hal.h>
-#include <stm32f0xx_hal_uart.h>
-
-void HAL_MspInit(void);
-void HAL_UART_MspInit(UART_HandleTypeDef* huart);
-void HAL_UART_MspDeInit(UART_HandleTypeDef* huart);
-