0

I've been testing my code on a Nucleo development board (is working perfecting fine). Once I had all the functionality I needed, I uploaded my code to a different microcontroller but now the code(functionality) doesn't seem to work.

Microcontroller STM32C031C6Tx.

  • Code(functionality) runs perfect! no issues

Microcontroller STM32C011J4Mx

  1. Plug in the board to a power supply that has the STM32C011J4Mx microcontroller.
  2. Switch my switch to the ON position, case 0: (red LED) turns on.(as intended)
  3. Turn the switch off, LEDs DO NOT turn off and since the LEDs don't turn off the rest of my code will not run.

My question is why does my code work on one microcontroller(STM32C031C6Tx) and not the other(STM32C011J4Mx)? Both have the same inputs, outputs, pin assignments and setup settings. What else should i check for? Or do i need to change my code to work for the new microcontroller im using?

Very new to all this so let me know if i need to provide more information.

Here is my code: (sorry its a lot).

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3); // Red
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); // Green
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4); // Blue

    uint8_t sins[360] = {
        127,129,131,134,136,138,140,143,145,147,149,151,154,156,158,160,162,164,166,169,171,173,175,177,179,181,183,185,187,189,191,193,195,196,198,200,
        202,204,205,207,209,211,212,214,216,217,219,220,222,223,225,226,227,229,230,231,233,234,235,236,237,239,240,241,242,243,243,244,245,246,247,248,
        248,249,250,250,251,251,252,252,253,253,253,254,254,254,254,254,254,254,255,254,254,254,254,254,254,254,253,253,253,252,252,251,251,250,250,249,
        248,248,247,246,245,244,243,243,242,241,240,239,237,236,235,234,233,231,230,229,227,226,225,223,222,220,219,217,216,214,212,211,209,207,205,204,
        202,200,198,196,195,193,191,189,187,185,183,181,179,177,175,173,171,169,166,164,162,160,158,156,154,151,149,147,145,143,140,138,136,134,131,129,
        127,125,123,120,118,116,114,111,109,107,105,103,100,98,96,94,92,90,88,85,83,81,79,77,75,73,71,69,67,65,63,61,59,58,56,54,
        52,50,49,47,45,43,42,40,38,37,35,34,32,31,29,28,27,25,24,23,21,20,19,18,17,15,14,13,12,11,11,10,9,8,7,6,
        6,5,4,4,3,3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,2,2,3,3,4,4,5,
        6,6,7,8,9,10,11,11,12,13,14,15,17,18,19,20,21,23,24,25,27,28,29,31,32,34,35,37,38,40,42,43,45,47,49,50,
        52,54,56,58,59,61,63,65,67,69,71,73,75,77,79,81,83,85,88,90,92,94,96,98,100,103,105,107,109,111,114,116,118,120,123,125
    };

  uint32_t led_off_tick;

enum button_state_e {
    BUTTON_NOT_PRESSED,
    BUTTON_JUST_PRESSED,
    BUTTON_PRESSED
};

static enum button_state_e button_state = BUTTON_NOT_PRESSED;
static uint8_t color_sequence = 0;



void red_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 230); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
};

void green_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 230); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
};

void blue_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 230); // Blue
};

void pink_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 230); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 50); // Blue
};

void teal_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 230); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 230); // Blue
};

void yellow_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 230); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 140); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
};

void orange_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 230); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 25); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
};

void purple_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 230); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 100); // Blue
};

void white_sequence_full(void) {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 230); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 230); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 230); // Blue
};

void party_sequence_full(void) {

    int i = 0;

    while (1) {
        for (i = 0; i < 360; i++) {
            __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, sins[i]); // Red
            __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, sins[(i + 120) % 360]); // Green
            __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, sins[(i + 240) % 360]); // Blue

            HAL_Delay(2);

            if (HAL_GPIO_ReadPin(Toggle_Switch_GPIO_Port, Toggle_Switch_Pin) != GPIO_PIN_SET)
                return;
        }
    }
};

void red_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 115); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
}
void green_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 115); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
}
void blue_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 115); // Blue
}
void pink_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 115); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 25); // Blue
}
void teal_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 115); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 115); // Blue
}
void yellow_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 115); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 120); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
}
void orange_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 115); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 13); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
}
void purple_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 115); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 50); // Blue
}
void white_sequence_half() {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 115); // Red
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 115); // Green
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 115); // Blue
}

void party_sequence_half(void) {

    int i = 0;

    while (1) {
        for (i = 0; i < 360; i++) {
            __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, sins[i] / 2); // Red
            __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, sins[(i + 120) % 360] / 2); // Green
            __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, sins[(i + 240) % 360] / 2); // Blue

            HAL_Delay(2);

            if (HAL_GPIO_ReadPin(Toggle_Switch_GPIO_Port, Toggle_Switch_Pin) != GPIO_PIN_SET)
                  return;
        }
    }
};

void switch_off_all_leds(void)
{
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0); // Red
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0); // Green
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_4, 0); // Blue
};

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
          // Sets pressed variable to a boolian value (true or false). In this case its true.
         bool pressed = (HAL_GPIO_ReadPin(Toggle_Switch_GPIO_Port, Toggle_Switch_Pin) == GPIO_PIN_SET);

         if(HAL_GPIO_ReadPin(Main_input_GPIO_Port, Main_input_Pin) == GPIO_PIN_SET) {

            switch (button_state)
            {
                case BUTTON_NOT_PRESSED:
                    if (pressed)
                        button_state = BUTTON_JUST_PRESSED;
                    break;


                case BUTTON_JUST_PRESSED:
                    if (pressed)
                    {
                        button_state = BUTTON_PRESSED;

                        if ((HAL_GetTick() - led_off_tick) < 2000u)
                            color_sequence = (color_sequence + 1) % 10;

                        switch (color_sequence) {

                        case 0:
                            red_sequence_full();
                            break;
                        case 1:
                            green_sequence_full();
                            break;
                        case 2:
                            blue_sequence_full();
                            break;
                        case 3:
                            pink_sequence_full();
                            break;
                        case 4:
                            teal_sequence_full();
                            break;
                        case 5:
                            yellow_sequence_full();
                            break;
                        case 6:
                            orange_sequence_full();
                            break;
                        case 7:
                            purple_sequence_full();
                            break;
                        case 8:
                            white_sequence_full();
                            break;
                        case 9:
                            party_sequence_full();
                            break;
                        }
                    }
                    else
                        button_state = BUTTON_NOT_PRESSED;
                    break;

                case BUTTON_PRESSED:
                    if (!pressed)
                    {
                        button_state = BUTTON_NOT_PRESSED;
                        switch_off_all_leds();
                        led_off_tick = HAL_GetTick();
                    }
                    break;
            }

         } else {

                switch (button_state)
                {
                    case BUTTON_NOT_PRESSED:
                        if (pressed)
                            button_state = BUTTON_JUST_PRESSED;
                        break;


                    case BUTTON_JUST_PRESSED:
                        if (pressed)
                        {
                            button_state = BUTTON_PRESSED;

                            if ((HAL_GetTick() - led_off_tick) < 2000u)
                                color_sequence = (color_sequence + 1) % 10;

                            switch (color_sequence) {

                            case 0:
                                red_sequence_half();
                                break;
                            case 1:
                                green_sequence_half();
                                break;
                            case 2:
                                blue_sequence_half();
                                break;
                            case 3:
                                pink_sequence_half();
                                break;
                            case 4:
                                teal_sequence_half();
                                break;
                            case 5:
                                yellow_sequence_half();
                                break;
                            case 6:
                                orange_sequence_half();
                                break;
                            case 7:
                                purple_sequence_half();
                                break;
                            case 8:
                                white_sequence_half();
                                break;
                            case 9:
                                party_sequence_half();
                                break;
                            }
                        }
                        else
                            button_state = BUTTON_NOT_PRESSED;
                        break;

                    case BUTTON_PRESSED:
                        if (!pressed)
                        {
                            button_state = BUTTON_NOT_PRESSED;
                            switch_off_all_leds();
                            led_off_tick = HAL_GetTick();
                        }

                        break;
                }

         }

            HAL_Delay(10);
  }
  /* USER CODE END 3 */
}
Mendi
  • 63
  • 6
  • A websearch produced (very little but): https://www.st.com/resource/en/application_note/an5775-migration-of-applications-from-the-stm8l-and-stm8s-series-to-the-stm32c0-series-microcontrollers-stmicroelectronics.pdf – Craig Estey Aug 03 '23 at 22:22
  • 1
    When you say the "same code", do you mean the same binary, or the same source code compiled for the different microcontroller? – pmacfarlane Aug 03 '23 at 22:22
  • 2
    Recommendation: Back this up and then take an axe to it. Reduce it to the minimum code that demonstrates the problem. Use [mre] as inspiration. If you still have a question when you're done (or if the question is still interesting) add the minimal example to the question (and self-answer if you answered the question yourself). – user4581301 Aug 03 '23 at 22:23
  • Mixing C and C++ on a desktop is difficult; mixing them on an embedded system is more difficult. For example, C++ has `std::vector`, which uses dynamic memory. In most embedded systems, dynamic memory is not allowed, since it leads to memory fragmentation. The only ways to get around fragmentation is either garbage collection or restarting. Neither of these is wonderful, depending on ability to recover. For example, resetting an airline system at 60000 ft high is not a good idea. Choose one language and update the tags. – Thomas Matthews Aug 03 '23 at 23:08
  • @ThomasMatthews They tagged C++ in error. I've removed it. – pmacfarlane Aug 03 '23 at 23:37
  • @pmacfarlane "same code" as in the code you helped me with for this project. I made a new project in stmcubeide, chose the microcontroller that was on my new board, then in the main.c file i added all the code you see in my post above. (didnt mess with the setup code that is automatically generated once you configure your microcontroller). – Mendi Aug 03 '23 at 23:38
  • Please update your question to say what does (and does not) work. "the code doesnt seem to work" isn't enough for anyone to help you. – pmacfarlane Aug 03 '23 at 23:41
  • @pmacfarlane Thank you for being patient, I have updated my post! – Mendi Aug 04 '23 at 00:00
  • Do you have a pull-up (or down) on the switch signal that was not there before? – pmacfarlane Aug 04 '23 at 00:03
  • @pmacfarlane Using Pull-Down as before – Mendi Aug 04 '23 at 00:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254786/discussion-between-pmacfarlane-and-mendi). – pmacfarlane Aug 04 '23 at 00:08

1 Answers1

0

You need to modify the linker script as the stack is placed at the last address of the SRAM.

STM32C031 has 12kB of SRAM and STM32C011 only 6kB and the stack pointer will be placed outside the SRAM. Any stack operation will result in HF (Hard Fault).

Look for the liner script (extension .ld) and find _estack = Assign it with the initial position of the stack pointer (it has to be in SRAM and it has to be 8byte aligned)

0___________
  • 60,014
  • 4
  • 34
  • 74