From 4f2d9dbfb47c0d10cd22c3fff5cf8711f7970171 Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Mon, 24 Mar 2025 18:38:05 -0500 Subject: [PATCH] normalize the code --- src/hall_driver.c | 79 ++++++++++++++++++++++++++++++++++++++++ src/hall_driver.h | 78 +++------------------------------------- src/main.c | 2 +- src/neopixel_driver.c | 81 +++++++++++++++++++++++++++++++++++++++++ src/neopixel_driver.h | 84 +++---------------------------------------- src/uart_driver.c | 56 +++++++++++++++++++++++++++++ src/uart_driver.h | 57 ++--------------------------- 7 files changed, 229 insertions(+), 208 deletions(-) create mode 100644 src/hall_driver.c create mode 100644 src/neopixel_driver.c create mode 100644 src/uart_driver.c diff --git a/src/hall_driver.c b/src/hall_driver.c new file mode 100644 index 0000000..bac7b88 --- /dev/null +++ b/src/hall_driver.c @@ -0,0 +1,79 @@ +#include "hall_driver.h" + +volatile uint8_t adcFlag = 0; + +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)); +} + +/** + * 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); + } +} diff --git a/src/hall_driver.h b/src/hall_driver.h index d199f26..3bfca64 100755 --- a/src/hall_driver.h +++ b/src/hall_driver.h @@ -3,84 +3,14 @@ #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; +extern volatile uint8_t adcFlag; /** * 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); - } -} +void ADC1_IRQHandler() __attribute__((interrupt("WCH-Interrupt-fast"))); + +void ADCConfig(void); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6bd0868..46f5617 100755 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ void init_button(void); int main(void) { - NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); SystemCoreClockUpdate(); Delay_Init(); diff --git a/src/neopixel_driver.c b/src/neopixel_driver.c new file mode 100644 index 0000000..545b566 --- /dev/null +++ b/src/neopixel_driver.c @@ -0,0 +1,81 @@ +#include "neopixel_driver.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_FirstBit = SPI_FirstBit_MSB; + +#ifdef CLK_24MHZ + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; +#endif +#ifdef CLK_8MHZ + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; +#endif + 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() { +#ifdef CLK_24MHZ + // 420ns High at 24MHz + SPISendByte(0b11000000); +#endif +#ifdef CLK_8MHZ + // ???????? + SPISendByte(0b10000000); +#endif +} + +void SendNeoPixel1() { +#ifdef CLK_24MHZ + // 750ns High at 24MHz + SPISendByte(0b11110000); +#endif +#ifdef CLK_8MHZ + // ???????? + SPISendByte(0b1110000); +#endif +} + +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); +} \ No newline at end of file diff --git a/src/neopixel_driver.h b/src/neopixel_driver.h index 7ae44a8..388cd13 100755 --- a/src/neopixel_driver.h +++ b/src/neopixel_driver.h @@ -6,84 +6,10 @@ // #define CLK_24MHZ #define CLK_8MHZ -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_FirstBit = SPI_FirstBit_MSB; - -#ifdef CLK_24MHZ - SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; -#endif -#ifdef CLK_8MHZ - SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; -#endif - 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() { -#ifdef CLK_24MHZ - // 420ns High at 24MHz - SPISendByte(0b11000000); -#endif -#ifdef CLK_8MHZ - // ???????? - SPISendByte(0b10000000); -#endif -} - -void SendNeoPixel1() { -#ifdef CLK_24MHZ - // 750ns High at 24MHz - SPISendByte(0b11110000); -#endif -#ifdef CLK_8MHZ - // ???????? - SPISendByte(0b1110000); -#endif -} - -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); -} +void SPINeoPixelDriverInit(void); +void SPISendByte(uint8_t byte); +void SendNeoPixel0(); +void SendNeoPixelByte(uint8_t byte); +void SendNeoPixelColor(uint8_t red, uint8_t green, uint8_t blue); #endif // NEOPIXEL_DRIVER_H \ No newline at end of file diff --git a/src/uart_driver.c b/src/uart_driver.c new file mode 100644 index 0000000..a1d5c75 --- /dev/null +++ b/src/uart_driver.c @@ -0,0 +1,56 @@ +#include "uart_driver.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; + } +} diff --git a/src/uart_driver.h b/src/uart_driver.h index c1f460b..219fac3 100755 --- a/src/uart_driver.h +++ b/src/uart_driver.h @@ -3,59 +3,8 @@ #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; - } -} +void USARTx_CFG(void); +void UartBufferSend(uint8_t* buffer, uint16_t length); +void UartStringSend(char* string); #endif // UART_DRIVER_H \ No newline at end of file