Description

u8_set_can_baudrate() allows you to configure the CAN bus baud rate of the first CAN bus (MCAN0). The bootloader uses this baud rate to initialise the bus at system start or after resetting the device.

Syntax

u8_set_can_baudrate( u16_new_baudrate );

Parameters

uint16_t u16_new_baudrate: the new baud rate – 125 / 250 / 500 / 1000 [kbaud]

Returns

uint8_t return value meaning:
0 – setting changed (accepted).
1 – the u16_new_baudrate parameter is not one of the required values (125 / 250 / 500/ 1000).
The value can still be written to flash but the bootloader will fall back to 125 kBaud instead.
2 – the baud rate is out of range 25 < u16_new_baudrate < 1000.

Example Code

This example shows how to read, set and store the ‘baud rate’ of the bootloader settings.

void setup()
{  
  Serial.begin();
  while (!Serial) 
  {
    yield(); // wait for PC CAN2SER-monitor to open 
  }

  uint16_t u16_baudrate = BOOTLOADER_SETTING.u16_get_can_baudrate();
  String s_device_name = BOOTLOADER_SETTING.s_get_device_name();
  
  Serial.println("Bootloader settings:");
  Serial.println(" - baudrate:    "+ String(u16_baudrate) + " kBaud");
  Serial.println(" - device name: " + s_device_name);
  
  delay(2000);
  Serial.println("Modify the settings");
  BOOTLOADER_SETTING.u8_set_can_baudrate(125);      // 125/250/500 or 1000
  BOOTLOADER_SETTING.u8_set_device_name("myDICE");  // 6 chars
  
  delay(2000);
  Serial.println("Store the settings in flash");
  
  uint8_t u8_ret = BOOTLOADER_SETTING.u8_store_bootloader_settings();
  
  if(u8_ret != 0)
	Serial.println("Error writing into flash");
  
  Serial.println("DONE");
}

void loop () 
{
}

Notes and Warnings

Don’t forget to call the function u8_store_bootloader_settings() to write the changed settings into flash, so that the bootloader (and app.) has the new values at the next system start / reset.

Even it is recommended to set the MCAN0 baud rate with the bootloader settings, the application may still re-initialize the CAN bus with the MCAN0.begin() method of the SAMC21_CAN class.

See also

u8_store_bootloader_settings()
u16_get_can_baudrate()
SAMC21_CAN