working 4 tile

This commit is contained in:
2024-10-21 00:07:35 -05:00
commit 771c264425
13 changed files with 614 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
#ifndef BIT_BANG_NEOPIXEL_H
#define BIT_BANG_NEOPIXEL_H
#include <ch32v00x.h>
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 */
+90
View File
@@ -0,0 +1,90 @@
#ifndef BIT_BANG_NEOPIXEL_H
#define BIT_BANG_NEOPIXEL_H
#include <ch32v00x.h>
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 */
+86
View File
@@ -0,0 +1,86 @@
#ifndef HALL_DRIVER_H
#define HALL_DRIVER_H
#include <ch32v00x.h>
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
Executable
+113
View File
@@ -0,0 +1,113 @@
#include <ch32v00x.h>
#include <debug.h>
#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)
{
}
}
+68
View File
@@ -0,0 +1,68 @@
#ifndef NEOPIXEL_DRIVER_H
#define NEOPIXEL_DRIVER_H
#include <ch32v00x.h>
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
+61
View File
@@ -0,0 +1,61 @@
#ifndef UART_DRIVER_H
#define UART_DRIVER_H
#include <ch32v00x.h>
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