-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
coding-moding
committed
May 12, 2019
1 parent
0b397cf
commit 511bfd6
Showing
14 changed files
with
1,099 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
cmake_minimum_required(VERSION 3.0) | ||
|
||
set(CMAKE_TOOLCHAIN_FILE cmake/sdcc-generic.cmake) # path to sdcc-generic.cmake toolchain file. | ||
set(CMAKE_MODULE_PATH .) # path to cmake files directory. | ||
#set(STM8_StdPeriph_DIR ../stm8s-sdcc-template/STM8S_StdPeriph_Lib/Libraries/STM8S_StdPeriph_Driver) # path to StdPeriph directory. | ||
set(STM8_CHIP stm8s103f3) # stm8 chip name, e.g. stm8l152c6 or stm8s105k4 | ||
set(CMAKE_C_COMPILER_WORKS 1) | ||
|
||
project(cap-tester C) | ||
|
||
add_definitions(-DSTM8S103 -DF_CPU=2000000) | ||
|
||
include(../stm8-sdcc-cmake/cmake/sdcc-stm8s.cmake) | ||
|
||
message(STATUS CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE: ${CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE}) | ||
|
||
set(std-periph /home/user/Project/IoT/stm8/stm8s-sdcc-template/STM8S_StdPeriph_Lib/Libraries/STM8S_StdPeriph_Driver) | ||
|
||
include_directories( | ||
/usr/local/share/sdcc/include | ||
/home/user/Project/IoT/stm8/stm8-sdcc-examples | ||
/home/user/Project/IoT/stm8/stm8s-sdcc-template/Inc | ||
${std-periph}/inc | ||
# /home/user/Project/IoT/stm8/stm8-sdcc-cmake/StdPeriph/STM8S/Libraries/STM8S_StdPeriph_Driver/inc/ | ||
#/home/user/Project/IoT/stm8/stm8-sdcc-cmake/StdPeriph/ | ||
) | ||
|
||
#DEFINES = -D$(COMPILER) -D$(MCU) -DUSE_STDPERIPH_DRIVER | ||
|
||
#cmake -DCMAKE_TOOLCHAIN_FILE=<path_to_sdcc-generic.cmake> -DCMAKE_MODULE_PATH=<path_to_project_cmake_dir> -DSTM8_CHIP=<chip name> -DSTM8_StdPeriph_DIR=<path to std periph> -G"MinGW Makefiles" <path_to_source_dir> | ||
find_package(StdPeriph COMPONENTS gpio) | ||
file(GLOB src | ||
main.* | ||
analog.* | ||
serial.* | ||
util.* | ||
gpio.* | ||
stm8.h | ||
cmake/*.cmake | ||
) | ||
add_executable(${PROJECT_NAME} ${src}) | ||
|
||
#add_custom_target(Resource SOURCES readme.txt) | ||
|
||
#Important compiler options for STM8 developers include: | ||
|
||
#-c to compile into object files to be linked later | ||
#--std-c99 for compilation in C99 mode (some C99 features, e.g. variable-length arrays | ||
# are not yet supported in sdcc though) | ||
#--opt-code-size for optimization for code size | ||
#--max-allocs-per-node to select the optimization level. the default value is 3000. | ||
# Higher values result in more optimized code, longer compiler runtime, | ||
# and higher memory usage during compilation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "analog.h" | ||
#include "main.h" | ||
#include "stm8.h" | ||
|
||
void AnalogInit(uint8_t Channel) | ||
{ | ||
ADC_CSR = 0; | ||
ADC_CR1 = 0; | ||
ADC_CR2 = 0; | ||
ADC_CR3 = 0; | ||
ADC_CSR = Channel; // Select channel 2 (AIN2=PC4) | ||
ADC_CR1 |= ADC_CR1_ADON; // ADON | ||
ADC_CR2 &= ~ADC_CR2_ALIGN; // Align left | ||
delay(1000); // Give little time to be ready for first conversion | ||
} | ||
|
||
uint16_t AnalogRead() | ||
{ | ||
ADC_CR1 &= ~ADC_CR1_CONT; // Single conversion mode | ||
ADC_CR1 |= ADC_CR1_ADON; // Start conversion | ||
do { nop(); } while ((ADC_CSR >> 7) == 0); | ||
ADC_CSR &= ~ADC_CSR_EOC; // Clear "End of conversion"-flag | ||
return (ADC_DRH << 2) | (ADC_DRL >> 6); // Left aligned | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdint.h> | ||
|
||
void AnalogInit(uint8_t Channel); | ||
uint16_t AnalogRead(); //just adding analog input to pin C4 | ||
|
||
typedef struct | ||
{ | ||
void (*Init)(uint8_t Channel); | ||
uint16_t (*Read)(void); | ||
} TAnalog; | ||
|
||
static TAnalog Analog = {.Init = AnalogInit, .Read = AnalogRead}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
set(CMAKE_SYSTEM_NAME Generic) | ||
|
||
set(CMAKE_C_COMPILER sdcc) | ||
set(CMAKE_OBJCOPY sdobjcopy CACHE INTERNAL "objcopy tool") | ||
set(CMAKE_PACKIHX packihx CACHE INTERNAL "packihx tool") | ||
|
||
set(CMAKE_STATIC_LIBRARY_PREFIX "") | ||
set(CMAKE_STATIC_LIBRARY_SUFFIX ".lib") | ||
set(CMAKE_SHARED_LIBRARY_PREFIX "") | ||
set(CMAKE_SHARED_LIBRARY_SUFFIX ".lib") | ||
set(CMAKE_IMPORT_LIBRARY_PREFIX ) | ||
set(CMAKE_IMPORT_LIBRARY_SUFFIX ) | ||
set(CMAKE_EXECUTABLE_SUFFIX ".ihx") | ||
set(CMAKE_LINK_LIBRARY_SUFFIX ".lib") | ||
set(CMAKE_DL_LIBS "") | ||
set(CMAKE_C_OUTPUT_EXTENSION ".rel") | ||
|
||
get_filename_component(SDCC_LOCATION "${CMAKE_C_COMPILER}" PATH) | ||
find_program(SDCCLIB_EXECUTABLE sdcclib PATHS "${SDCC_LOCATION}" NO_DEFAULT_PATH) | ||
find_program(SDCCLIB_EXECUTABLE sdcclib) | ||
set(CMAKE_AR "${SDCCLIB_EXECUTABLE}" CACHE FILEPATH "The sdcc librarian" FORCE) | ||
|
||
if(NOT DEFINED CMAKE_C_FLAGS_INIT) | ||
set(flags "-mstm8 --opt-code-size --std-c11 --print-search-dirs") | ||
if(CMAKE_BUILD_TYPE MATCHES "debug") | ||
set(flags "${flags} --out-fmt-elf --all-callee-saves --debug --verbose --stack-auto --fverbose-asm --float-reent --no-peep") | ||
endif() | ||
set(CMAKE_C_FLAGS_INIT ${flags}) | ||
endif() | ||
|
||
if(NOT DEFINED CMAKE_EXE_LINKER_FLAGS_INIT) | ||
set (CMAKE_EXE_LINKER_FLAGS_INIT "") | ||
endif() | ||
|
||
#set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>") | ||
set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o -c <SOURCE>") | ||
set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <OBJECTS> --out-fmt-ihx -o <TARGET> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>") | ||
|
||
set(CMAKE_C_CREATE_STATIC_LIBRARY | ||
"\"${CMAKE_COMMAND}\" -E remove <TARGET>" | ||
"<CMAKE_AR> -a <TARGET> <LINK_FLAGS> <OBJECTS> ") | ||
|
||
set(CMAKE_C_CREATE_SHARED_LIBRARY "") | ||
set(CMAKE_C_CREATE_MODULE_LIBRARY "") | ||
|
||
add_definitions(-D__SDCC__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "util.h" | ||
#include "main.h" | ||
|
||
typedef enum {In = 0, Out} TPinDirection; | ||
enum {Float = 0, PullUp}; | ||
enum {OpenDrain = 0, PushPull}; | ||
//void SetPin(unsigned char *PinGroup, unsigned char Pin) | ||
//{ | ||
// *(PinGroup + PA_DDR - PA) |= Pin; | ||
// PC_DDR |= PIN5; | ||
// PC_CR1 |= PIN5; | ||
// PC_ODR |= Pin; | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#include "main.h" | ||
#include "util.h" | ||
#include "analog.h" | ||
#include "serial.h" | ||
#include <stdint.h> | ||
|
||
/* Build in LED is in pin B5 (STM8S103 board) or D3 (STM8S003F3 board) */ | ||
#define LED_PORT PB | ||
#define LED_PIN PIN5 | ||
|
||
// Setup the system clock to run at 16MHz using the internal oscillator. | ||
void InitialiseSystemClock() | ||
{ | ||
CLK_ICKR = 0; // Reset the Internal Clock Register. | ||
CLK_ICKR |= CLK_ICKR_HSIEN; // Enable the HSI. | ||
CLK_ECKR = 0; // Disable the external clock. | ||
while (!(CLK_ICKR & CLK_ICKR_HSIRDY)); // Wait for the HSI to be ready for use. | ||
CLK_CKDIVR = 0; // Ensure the clocks are running at full speed. | ||
CLK_PCKENR1 = 0xff; // Enable all peripheral clocks. | ||
CLK_PCKENR2 = 0xff; // Ditto. | ||
CLK_CCOR = 0; // Turn off CCO. | ||
CLK_HSITRIMR = 0; // Turn off any HSIU trimming. | ||
CLK_SWIMCCR = 0; // Set SWIM to run at clock / 2. | ||
CLK_SWR = 0xe1; // Use HSI as the clock source. | ||
CLK_SWCR = 0; // Reset the clock switch control register. | ||
CLK_SWCR |= CLK_SWCR_SWEN; // Enable switching. | ||
while (CLK_SWCR & CLK_SWCR_SWBSY); // Pause while the clock switch is busy. | ||
} | ||
|
||
void SetPCFloat(unsigned char Pins) | ||
{ | ||
PC_DDR &= ~Pins; | ||
PC_CR1 &= ~Pins; | ||
} | ||
|
||
void SetPCHigh(uint8_t Pins) | ||
{ | ||
PC_DDR |= Pins; | ||
PC_CR1 |= Pins; | ||
PC_ODR |= Pins; | ||
} | ||
|
||
void SetPCLow(uint8_t Pins) | ||
{ | ||
PC_DDR |= Pins; | ||
PC_ODR &= ~Pins; | ||
PC_CR1 &= ~Pins; | ||
} | ||
|
||
//PC4 - ADC. 150 | ||
//PC5 - 10k | ||
//PC6 - 100k | ||
//PC7 - 750 | ||
#define PIN_ADC PC4 | ||
#define PIN_FAST_CHARGE PIN5 | ||
#define PIN_SLOW_CHARGE PIN6 | ||
#define PIN_FAST_DISCHARGE PIN7 | ||
static Bool FastCharge = True; | ||
static volatile uint32_t MeasureTickCount = 0; | ||
float Test() | ||
{ | ||
const uint16_t ChargeResistors[2] = {100, 10}; // KOhm | ||
// CLK_PCKENR1 = 0; // Disable all Peripheral Clocks - at start | ||
// CLK_PCKENR2 = 0; | ||
Analog.Init(2); | ||
SetPCFloat(PIN_FAST_CHARGE | PIN_SLOW_CHARGE | PIN_FAST_DISCHARGE); | ||
if(FastCharge) | ||
SetPCHigh(PIN_FAST_CHARGE); | ||
else | ||
SetPCHigh(PIN_SLOW_CHARGE); | ||
static const int LinearChargeTimeCoeff = 648; | ||
while(Analog.Read() < LinearChargeTimeCoeff) MeasureTickCount++; | ||
float pF = ((float)MeasureTickCount * 7700 / ChargeResistors[FastCharge]); | ||
FastCharge = !MeasureTickCount || MeasureTickCount > 1000; | ||
SetPCFloat(PIN_FAST_CHARGE | PIN_SLOW_CHARGE); | ||
SetPCLow(PIN_FAST_DISCHARGE); | ||
while(Analog.Read() > 0); | ||
SetPCFloat(PIN7); | ||
return pF; | ||
} | ||
|
||
int main() | ||
{ | ||
int i; | ||
// InitialiseSystemClock(); | ||
CLK_CKDIVR = 0; // Set clock to full speed (16 Mhz) | ||
Serial.Init(); | ||
Serial.Write("Started\r\n"); | ||
// GPIO setup | ||
PORT(LED_PORT, DDR) |= LED_PIN; // PB_DDR |= (1 << 5); // Set pin data direction as output | ||
PORT(LED_PORT, CR1) |= LED_PIN; // PB_CR1 |= (1 << 5); // Set pin as "Push-pull" | ||
for(i = 0; i < 3; i++) | ||
{ | ||
PORT(LED_PORT, ODR) &= ~LED_PIN; // PB_ODR &= ~(1 << 5); | ||
delay(100000L); | ||
PORT(LED_PORT, ODR) |= LED_PIN; // PB_ODR |= (1 << 5); | ||
delay(100000L); | ||
} | ||
float pFAverage = 0; | ||
const uint32_t OutputPeriod = 40000; | ||
uint32_t OutputTickCount = 0; | ||
while(1) | ||
{ | ||
float pF; | ||
pF = Test(); | ||
const uint16_t IterationTickCount = 5000; | ||
OutputTickCount += MeasureTickCount + IterationTickCount; | ||
MeasureTickCount = 0; | ||
if(pF != 0.0f) | ||
{ | ||
float MeasureWeight = 0.9f; | ||
pFAverage = pF * MeasureWeight + pFAverage * (1 - MeasureWeight); | ||
} | ||
else | ||
{ | ||
pFAverage = 0; | ||
OutputTickCount = 0; | ||
} | ||
if(OutputTickCount > OutputPeriod) | ||
{ | ||
OutputTickCount = 0; | ||
const uint8_t NextMeasureUnit = 100; | ||
if(pFAverage >= (float)NextMeasureUnit * 1000) | ||
{ | ||
Serial.WriteFloat(pFAverage / 1000000); | ||
Serial.Write(" uF\r\n"); | ||
} | ||
else if(pFAverage >= (float)NextMeasureUnit) | ||
{ | ||
Serial.WriteFloat(pFAverage / 1000); | ||
Serial.Write(" nF\r\n"); | ||
} | ||
else | ||
{ | ||
Serial.WriteFloat(pFAverage); | ||
Serial.Write(" pF\r\n"); | ||
} | ||
} | ||
PORT(LED_PORT, ODR) &= ~LED_PIN; // PB_ODR &= ~(1 << 5); | ||
delay(10000L); | ||
PORT(LED_PORT, ODR) |= LED_PIN; // PB_ODR |= (1 << 5); | ||
} | ||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
#include "stm8.h" | ||
|
||
|
||
/* Simple busy loop delay */ | ||
static void delay(unsigned long count) | ||
{ | ||
while(count--) | ||
nop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "../stm8-sdcc-examples/stm8.h" | ||
#include "main.h" | ||
#include "serial.h" | ||
#include "util.h" | ||
|
||
void SerialInit() | ||
{ | ||
// Setup UART1 (TX=D5) | ||
UART1_CR2 |= UART_CR2_TEN; // Transmitter enable | ||
// UART1_CR2 |= UART_CR2_REN; // Receiver enable | ||
UART1_CR3 &= ~(UART_CR3_STOP1 | UART_CR3_STOP2); // 1 stop bit | ||
// 9600 baud: UART_DIV = 16000000/9600 ~ 1667 = 0x0683 | ||
UART1_BRR2 = 0x03; UART1_BRR1 = 0x68; // 0x0683 coded funky way (see ref manual) | ||
} | ||
|
||
int SerialWrite(const char *str) | ||
{ | ||
const char *i; | ||
for(i = str; *i; i++) | ||
{ | ||
while(!(UART1_SR & UART_SR_TXE)); // !Transmit data register empty | ||
UART1_DR = (unsigned char)*i; | ||
} | ||
return i - str; // Bytes sent | ||
} | ||
|
||
void SerialWriteInt(const uint32_t Value) | ||
{ | ||
IntToStr(Value, Num); | ||
SerialWrite(Num); | ||
} | ||
|
||
void SerialWriteFloat(const float Value) | ||
{ | ||
FloatToStr(Value, Num); | ||
SerialWrite(Num); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
#include <stdint.h> | ||
|
||
static char Num[11]; | ||
|
||
void SerialInit(); | ||
int SerialWrite(const char *str); | ||
void SerialWriteInt(const uint32_t Value); | ||
void SerialWriteFloat(const float Value); | ||
|
||
typedef struct | ||
{ | ||
void (*Init)(); | ||
int (*Write)(const char *str); | ||
void (*WriteInt)(const uint32_t Value); | ||
void (*WriteFloat)(const float Value); | ||
} TSerial; | ||
|
||
static TSerial Serial = | ||
{ | ||
.Init = SerialInit, | ||
.Write = SerialWrite, | ||
.WriteInt = SerialWriteInt, | ||
.WriteFloat = SerialWriteFloat | ||
}; |
Oops, something went wrong.