Files
STM32F4_Cortex_M4/workspace/stm32f407_template
2025-07-05 23:53:52 -07:00
..
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00
2025-07-05 23:53:52 -07:00

// stm32f4xx_hal_conf.h

// #define HAL_CAN_MODULE_ENABLED
// #define HAL_GPIO_MODULE_ENABLED
// #define HAL_EXTI_MODULE_ENABLED
// #define HAL_DMA_MODULE_ENABLED
// #define HAL_RCC_MODULE_ENABLED
// #define HAL_UART_MODULE_ENABLED
// #define HAL_USART_MODULE_ENABLED
// #define HAL_FLASH_MODULE_ENABLED
// #define HAL_PWR_MODULE_ENABLED
// #define HAL_CORTEX_MODULE_ENABLED

// ---------------------------------------------------------

// PA0:  Button

// PD0: CAN1_RX
// PD1: CAN1_TX

// PD12: Green
// PD13: Orange
// PD14: Red
// PD15: Blue

// PA2: UART2_TX (AF7)
// PA3: UART2_RX (AF7)

// PA4: CS
// PA5: SPI1_CLK
// PA6: SPI1_MISO
// PA7: SPI1_MOSI

// PB12: CS
// PB13: SPI2_CLK
// PB14: SPI2_MISO
// PB15: SPI2_MOSI

// PC10: SPI3_CLK
// PC11: SPI3_MISO
// PC12: SPI3_MOSI

// PB8: I2C1_SCL
// PB9: I2C1_SDA

// PC14: OSC32_IN
// PC15: OSC32_OUT





// ---------------------------------------------------------

// GPIO Test
uint8_t buttonStatus;

	while(1) {
		buttonStatus = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, buttonStatus);
		HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
		HAL_Delay(500); // 500ms
	}

// ------------------------------------------------------------------------------------------
	
// UART2 Test
char message[20] = "Hello from STM32\r\n";
uint32_t counter;
	while(1) {
		HAL_UART_Transmit(&huart2, (uint8_t *)message, 20, 100);
		printf("Hello STM32: %ld \r\n", counter);
		HAL_Delay(500); // 500ms
	}
	
	
// UART2 with DMA1
uint8_t tx_buffer[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
uint8_t rx_buffer[10];

	UART2_DMA_init();
	HAL_UART_Transmit_DMA(&huart2, tx_buffer, 10);
	HAL_UART_Receive_DMA(&huart2, rx_buffer, 10);


// ------------------------------------------------------------------------------------------

// ADC Single Conversion Test

	// Initialize and start ADC on PA0 or PA1
	// ADC_PA0_single_conv_init(); // HAL Method, works with polling and ADC_PA0_read()
	// ADC_PA0_init_start(); // HAL Method, works with ADC_PA0_read()
	ADC_PA1_init_start(); // CMSIS Method, works with ADC_PA1_read()

	while(1) {

		// ADC PA0 conversion by polling, init with ADC_PA0_single_conv_init()
//		HAL_ADC_Start(&hadc1);
//		HAL_ADC_PollForConversion(&hadc1, 1);
//		sensor_value = ADC_PA0_read();

		// ADC PA0 continuous conversion, init with ADC_PA0_init_start()
//		HAL_ADC_Start(&hadc1);
//		sensor_value = ADC_PA0_read();

		// ADC PA1 continuous conversion, init with ADC_PA1_init_start()
		sensor_value = ADC_PA1_read();

		printf("%ld\r\n", sensor_value);
		HAL_Delay(10); // 10ms

	}
	
	
// ADC Single Channel with DMA

uint32_t sensor_value[1];
uint32_t g_sensor_value;

	// Initialize and start ADC1 on PA1 with DMA
	ADC_PA1_DMA_init();
	HAL_ADC_Start_DMA(&hadc1, (uint32_t *)sensor_value, 1);

	while(1) {
		// ADC1 PA1 continuous conversion with DMA, init with ADC_PA1_DMA_init()
		g_sensor_value = sensor_value[0];
		printf("%ld\r\n", sensor_value[0]);
		HAL_Delay(20); // 20ms
	}
	
	
// ADC Multiple Channels with DMA

uint32_t sensor_value[2];
uint32_t g_sensor_value;
uint32_t g_sensor_value2;

	// Initialize and start ADC1 on PA1 and PA4 with DMA
	ADC_PA1PA4_DMA_init();
	HAL_ADC_Start_DMA(&hadc1, (uint32_t *)sensor_value, 2);

	while(1) {
		// ADC1 PA1 and PA4 continuous conversion with DMA, init with ADC_PA1PA4_DMA_init()
		g_sensor_value = sensor_value[0];
		g_sensor_value2 = sensor_value[1];
		printf("%ld\r\n", sensor_value[0]);
		HAL_Delay(20); // 20ms
	}

	
// -------------------------------------------------------	
	
SPI1 or SPI2 Test

uint8_t tx_buffer[5] = {10, 20, 30, 40, 50};
uint8_t rx_buffer[5];

	SPI1_init();

	// Self-loopback test
	// Connect PA6 and PA7
	HAL_SPI_TransmitReceive(&hspi1, tx_buffer, rx_buffer, 5, 100);	

  	SPI2_init();

	// Self-loopback test
	// Connect PB14 and PB15
	HAL_SPI_TransmitReceive(&hspi2, tx_buffer, rx_buffer, 5, 100);	

  	SPI3_init();

	// Self-loopback test
	// Connect PC11 and PC12
	HAL_SPI_TransmitReceive(&hspi3, tx_buffer, rx_buffer, 5, 100);	  
  
// -------------------------------------------------------