I have a very simple code on an Arduino I am trying to make work on my STM32f446 Nucleo board. This is the very early stages of the project because I am simply trying to send a hex value/array to a “Gravity: UART MP3 Voice Module with 8MB Flash Memory” in order to play an mp3 audio file. The example code running properly on the Arduino IDE is as Follows:
#include <SoftwareSerial.h>
SoftwareSerial Serial1(10, 11); //RX TX (Signal)type
unsigned char order[4] = {0xAA,0x06,0x00,0xB0};
void setup() {
Serial1.begin(9600);
volume(0x08); //Volume settings 0x00-0x1E
}
void loop() {
play(0x01); //Play the specified audio:0x01-file0001
delay(2000);
}
// Function Definitions:
void play(unsigned char Track)
{
unsigned char play[6] = {0xAA,0x07,0x02,0x00,Track,Track+0xB3};
Serial1.write(play,6);
}
void volume( unsigned char vol)
{
unsigned char volume[5] = {0xAA,0x13,0x01,vol,vol+0xBE};
Serial1.write(volume,5);
}
This works perfectly on an Arduino Uno, but I want to end up with my own custom microcontroller PCB with an STM32 and real C code in the end. This was supposed to just be a test to ensure the MP# module was functioning properly.
Here’s what I did on the CubeIDE:
I setup the board using the graphical interface using USART2 (Pins D0 and D1) and set the baud rate to be 9600 just like in the sample code. The Initialization functions were created in the IDE and I did not tweak them at all (maybe I need to IDK). I am using the HAL_UART_Transmit() function to send the character array over UART as I have been instructed to do by many YouTube videos and ChatGPT. I have done a million different versions of this code but I am at my wits end so I got rid of everything I could and simplified it to this:
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
int vol = 0x08;
volume(vol);
while (1)
{
// play(0x01);
unsigned char play[6] = {0xAA,0x07,0x02,0x00,0x01,0xB4};
// hard code the values for play(0x01);
HAL_UART_Transmit(&huart2, play, sizeof(play), HAL_MAX_DELAY);
// hard code the UART Transmit
HAL_Delay(2000);
}
}
void play(unsigned char track){
unsigned char play[6] = {0xAA,0x07,0x02,0x00,track,track+0xB3};
//Serial1.write(play,6);
HAL_UART_Transmit(&huart2, play, sizeof(play), HAL_MAX_DELAY);
}
void volume(unsigned char vol){
unsigned char volume[5] = {0xAA,0x13,0x01,vol,vol+0xBE};
//Serial1.write(volume,5);
HAL_UART_Transmit(&huart2, volume, 5, HAL_MAX_DELAY);
}
Below is also the generated USART Initialization just in case I need to change something on it:
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
I have tried this hard coding the function directly in main as well as using the separate functions but neither seem to work. I can get other things like buttons and LEDs to work fine within this same program with the same setup, but for some reason the UART is not working. I have tries switching the TX and RX wires every time I retry the code to make sure it isn’t me accidentally plugging them in backwards.
It is quite difficult to debug as I do not know how to view what is being sent or received on the serial line in CubeIDE and I cannot use printf() to read data on the console Like I an used to in C. If you know a relatively simple way to do either of these things or a different post explaining how to, it would be greatly appreciated.
Any help or suggestions you may have would be greatly appreciated. Thank you for your time!