From 771c264425b7150d18b46fda0f7a6b7d2875c62a Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Mon, 21 Oct 2024 00:07:35 -0500 Subject: [PATCH] working 4 tile --- .gitignore | 5 ++ .vscode/extensions.json | 10 ++++ .vscode/settings.json | 6 ++ include/README | 39 ++++++++++++ lib/README | 46 ++++++++++++++ platformio.ini | 14 +++++ src/bit_bang_neopixel copy.h | 65 ++++++++++++++++++++ src/bit_bang_neopixel.h | 90 ++++++++++++++++++++++++++++ src/hall_driver.h | 86 ++++++++++++++++++++++++++ src/main.c | 113 +++++++++++++++++++++++++++++++++++ src/neopixel_driver.h | 68 +++++++++++++++++++++ src/uart_driver.h | 61 +++++++++++++++++++ test/README | 11 ++++ 13 files changed, 614 insertions(+) create mode 100755 .gitignore create mode 100755 .vscode/extensions.json create mode 100755 .vscode/settings.json create mode 100755 include/README create mode 100755 lib/README create mode 100755 platformio.ini create mode 100755 src/bit_bang_neopixel copy.h create mode 100755 src/bit_bang_neopixel.h create mode 100755 src/hall_driver.h create mode 100755 src/main.c create mode 100755 src/neopixel_driver.h create mode 100755 src/uart_driver.h create mode 100755 test/README diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100755 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100755 index 0000000..527a059 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "neopixel_driver.h": "c", + "bit_bang_neopixel.h": "c" + } +} \ No newline at end of file diff --git a/include/README b/include/README new file mode 100755 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100755 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100755 index 0000000..d330fc2 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:genericCH32V003F4P6] +platform = ch32v +board = genericCH32V003F4P6 +framework = noneos-sdk diff --git a/src/bit_bang_neopixel copy.h b/src/bit_bang_neopixel copy.h new file mode 100755 index 0000000..30b4c28 --- /dev/null +++ b/src/bit_bang_neopixel copy.h @@ -0,0 +1,65 @@ +#ifndef BIT_BANG_NEOPIXEL_H +#define BIT_BANG_NEOPIXEL_H + +#include + +void BBNeoPixelDriverInit(void) { + GPIO_InitTypeDef GPIO_InitStructure={0}; + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOD, &GPIO_InitStructure); +} + +void SendNeoPixel0() { + // 420ns High at 8MHz + GPIOD->BSHR = 1 << 4; + // Original: 650ns + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop"); + + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop"); + GPIOD->BCR = 1 << 4; + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); +} + +void SendNeoPixel1() { + // 850ns High at 8MHz + GPIOD->BSHR = 1 << 4; + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop"); + GPIOD->BCR = 1 << 4; +} + +void SendNeoPixelByte(uint8_t byte) { + for (int i = 7; i >= 0; i--) { + if ((byte >> i) & 1) { + SendNeoPixel1(); + } else { + SendNeoPixel0(); + } + } +} + +void SendNeoPixelColor(uint8_t red, uint8_t green, uint8_t blue) { + // Send in GRB order, MSB first + SendNeoPixelByte(green); + SendNeoPixelByte(red); + SendNeoPixelByte(blue); +} + +#endif /* BIT_BANG_NEOPIXEL_H */ \ No newline at end of file diff --git a/src/bit_bang_neopixel.h b/src/bit_bang_neopixel.h new file mode 100755 index 0000000..dcd59f4 --- /dev/null +++ b/src/bit_bang_neopixel.h @@ -0,0 +1,90 @@ +#ifndef BIT_BANG_NEOPIXEL_H +#define BIT_BANG_NEOPIXEL_H + +#include + +void BBNeoPixelDriverInit(void) { + GPIO_InitTypeDef GPIO_InitStructure={0}; + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOC, &GPIO_InitStructure); +} + +void SendNeoPixel0() { + GPIOC->BSHR = 1 << 6; + // 420ns High at 8MHz + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop"); + + // Original: 650ns + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop"); + + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + GPIOC->BCR = 1 << 6; + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); +} + +void SendNeoPixel1() { + GPIOC->BSHR = 1 << 6; + // 850ns High at 8MHz + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop"); + + // Original: + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + // __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + + + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop");__asm__("nop"); + __asm__("nop");__asm__("nop");__asm__("nop"); + GPIOC->BCR = 1 << 6; +} + +void SendNeoPixelByte(uint8_t byte) { + for (int i = 7; i >= 0; i--) { + if ((byte >> i) & 1) { + SendNeoPixel1(); + } else { + SendNeoPixel0(); + } + } +} + +void SendNeoPixelColor(uint8_t red, uint8_t green, uint8_t blue) { + // Send in GRB order, MSB first + SendNeoPixelByte(green); + SendNeoPixelByte(red); + SendNeoPixelByte(blue); +} + +#endif /* BIT_BANG_NEOPIXEL_H */ \ No newline at end of file diff --git a/src/hall_driver.h b/src/hall_driver.h new file mode 100755 index 0000000..d199f26 --- /dev/null +++ b/src/hall_driver.h @@ -0,0 +1,86 @@ +#ifndef HALL_DRIVER_H +#define HALL_DRIVER_H + +#include + +void ADC1_IRQHandler() __attribute__((interrupt("WCH-Interrupt-fast"))); + +void ADCConfig(void) { + ADC_InitTypeDef ADC_InitStructure = {0}; + GPIO_InitTypeDef GPIO_InitStructure = {0}; + NVIC_InitTypeDef NVIC_InitStructure = {0}; + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); + RCC_ADCCLKConfig(RCC_PCLK2_Div8); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; + GPIO_Init(GPIOC, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + ADC_DeInit(ADC1); + ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; + ADC_InitStructure.ADC_ScanConvMode = ENABLE; // multiple channels + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigInjecConv_None; + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; + ADC_InitStructure.ADC_NbrOfChannel = 2; + ADC_Init(ADC1, &ADC_InitStructure); + + ADC_InjectedSequencerLengthConfig(ADC1, 4); + ADC_InjectedChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_241Cycles); + ADC_InjectedChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_241Cycles); + ADC_InjectedChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_241Cycles); + ADC_InjectedChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_241Cycles); + ADC_ExternalTrigInjectedConvCmd(ADC1, DISABLE); + + NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + + // ADC_Calibration_Vol(ADC1, ADC_CALVOL_75PERCENT); + // ADC_Calibration_Vol(ADC1, ADC_CALVOL_50PERCENT); + ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE); + ADC_Cmd(ADC1, ENABLE); + + ADC_ResetCalibration(ADC1); + while (ADC_GetResetCalibrationStatus(ADC1)); + + ADC_StartCalibration(ADC1); + while (ADC_GetCalibrationStatus(ADC1)); +} + +int adcFlag = 0; + +/** + * The function ADC1_IRQHandler handles the interrupt for ADC1 and prints the value of the injected + * conversion. + */ +void ADC1_IRQHandler() { + // UartStringSend("interupt!!!\r\n"); + if (ADC_GetITStatus(ADC1, ADC_IT_JEOC) == SET) { + adcFlag = 1; + ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC); + } + if (ADC_GetITStatus(ADC1, ADC_IT_JEOC) == SET) { + adcFlag = 1; + ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC); + } +} + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100755 index 0000000..6bd0868 --- /dev/null +++ b/src/main.c @@ -0,0 +1,113 @@ +#include +#include +#include "neopixel_driver.h" +// #include "bit_bang_neopixel.h" +#include "uart_driver.h" +#include "hall_driver.h" + +void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); +void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); +void Delay_Init(void); +void Delay_Ms(uint32_t n); +void init_button(void); + +int main(void) +{ + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); + SystemCoreClockUpdate(); + Delay_Init(); + + // allow some time to enter flash mode, in case something gets screwed up + Delay_Ms(1000); + + USARTx_CFG(); + // UartStringSend("Hello World!\r\n"); + + SPINeoPixelDriverInit(); + // BBNeoPixelDriverInit(); + + init_button(); + ADCConfig(); + + uint16_t adc_readings[4] = {0}; + uint16_t adc_calibration_values[4] = {512, 512, 512, 512}; + while (1) { + // button will calibrate + if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3) == 0) { + adc_calibration_values[0] = adc_readings[0]; + adc_calibration_values[1] = adc_readings[1]; + adc_calibration_values[2] = adc_readings[2]; + adc_calibration_values[3] = adc_readings[3]; + } + + // start an adc convertion + ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE); + Delay_Ms(10); + + // wait until the conversion is done + while (adcFlag != 1) { + Delay_Us(100); + } + + adc_readings[0] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_1); + adc_readings[1] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_2); + adc_readings[2] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_3); + adc_readings[3] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_4); + + adcFlag = 0; + + char buffer[64] = {0}; + sprintf(buffer, "%d\t%d\t%d\t%d\r\n", adc_readings[0], adc_readings[1], adc_readings[2], adc_readings[3]); + UartStringSend(buffer); + + for (int i = 0; i < 4; i++) { + if (adc_readings[i] > adc_calibration_values[i] + 5) { + float value = (float)(adc_readings[i] - adc_calibration_values[i]) / 512.0; + if (value > 1.0) value = 1.0; + if (value < 0.0) value = 0.0; + uint8_t color = 255 * value; + + SendNeoPixelColor(color, 0, 0); + SendNeoPixelColor(color, 0, 0); + SendNeoPixelColor(color, 0, 0); + SendNeoPixelColor(color, 0, 0); + } + else if (adc_readings[i] < adc_calibration_values[i] - 5) { + float value = (float)(adc_calibration_values[i] - adc_readings[i]) / 512.0; + if (value > 1.0) value = 1.0; + if (value < 0.0) value = 0.0; + uint8_t color = 255 * value; + + SendNeoPixelColor(0, 0, color); + SendNeoPixelColor(0, 0, color); + SendNeoPixelColor(0, 0, color); + SendNeoPixelColor(0, 0, color); + } else { + SendNeoPixelColor(0, 0, 0); + SendNeoPixelColor(0, 0, 0); + SendNeoPixelColor(0, 0, 0); + SendNeoPixelColor(0, 0, 0); + } + } + } +} + +void init_button(void) { + GPIO_InitTypeDef GPIO_InitStructure = {0}; + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_Init(GPIOC, &GPIO_InitStructure); +} + +void NMI_Handler(void) { + SendNeoPixelColor(0, 255, 0); +} +void HardFault_Handler(void) +{ + SendNeoPixelColor(0, 255, 0); + while (1) + { + + } +} diff --git a/src/neopixel_driver.h b/src/neopixel_driver.h new file mode 100755 index 0000000..8e8f3b4 --- /dev/null +++ b/src/neopixel_driver.h @@ -0,0 +1,68 @@ +#ifndef NEOPIXEL_DRIVER_H +#define NEOPIXEL_DRIVER_H + +#include + +void SPINeoPixelDriverInit(void) { + GPIO_InitTypeDef GPIO_InitStructure={0}; + SPI_InitTypeDef SPI_InitStructure={0}; + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_SPI1, ENABLE); + + // init MOSI on PC6 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOC, &GPIO_InitStructure); + + SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; + + SPI_InitStructure.SPI_Mode = SPI_Mode_Master; + + SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; + SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; + SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; + SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; + SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; + SPI_Init(SPI1, &SPI_InitStructure); + + SPI_Cmd(SPI1, ENABLE); + + SPISendByte(0); +} + +void SPISendByte(uint8_t byte) { + // wait while flag is zero or TX buffer not empty + // while( SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); + SPI_I2S_SendData(SPI1, byte); +} + +void SendNeoPixel0() { + // 420ns High at 24MHz + SPISendByte(0b11000000); +} + +void SendNeoPixel1() { + // 750ns High at 24MHz + SPISendByte(0b11110000); +} + +void SendNeoPixelColor(uint8_t red, uint8_t green, uint8_t blue) { + // Send in GRB order, MSB first + SendNeoPixelByte(green); + SendNeoPixelByte(red); + SendNeoPixelByte(blue); +} + +void SendNeoPixelByte(uint8_t byte) { + for (int i = 7; i >= 0; i--) { + if ((byte >> i) & 1) { + SendNeoPixel1(); + } else { + SendNeoPixel0(); + } + } +} + +#endif // NEOPIXEL_DRIVER_H \ No newline at end of file diff --git a/src/uart_driver.h b/src/uart_driver.h new file mode 100755 index 0000000..c1f460b --- /dev/null +++ b/src/uart_driver.h @@ -0,0 +1,61 @@ +#ifndef UART_DRIVER_H +#define UART_DRIVER_H + +#include + +void USARTx_CFG(void) { + GPIO_InitTypeDef GPIO_InitStructure = {0}; + USART_InitTypeDef USART_InitStructure = {0}; + NVIC_InitTypeDef NVIC_InitStructure = {0}; + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1, ENABLE); + + /* USART1 TX-->D.5 RX-->D.6 */ + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + USART_InitStructure.USART_BaudRate = 9600; + USART_InitStructure.USART_WordLength = USART_WordLength_8b; + USART_InitStructure.USART_StopBits = USART_StopBits_1; + USART_InitStructure.USART_Parity = USART_Parity_No; + USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; + + USART_Init(USART1, &USART_InitStructure); + USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); + + NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure);; + + USART_Cmd(USART1, ENABLE); +} + +void UartBufferSend(uint8_t* buffer, uint16_t length) +{ + uint16_t i = 0; + for(i =0; i < length; i++) + { + while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); /* waiting for sending finish */ + USART_SendData(USART1, buffer[i]); + } +} + +void UartStringSend(char* string) { + uint16_t i = 0; + while (string[i] != '\0') { + while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); /* waiting for sending finish */ + USART_SendData(USART1, string[i]); + i += 1; + } +} + +#endif // UART_DRIVER_H \ No newline at end of file diff --git a/test/README b/test/README new file mode 100755 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html