Classes | Defines | Enumerations | Functions | Variables
boot.c File Reference
#include "basics.h"
#include <p18f2580.h>
Include dependency graph for boot.c:

Go to the source code of this file.

Classes

struct  CANmsg
 Structure to store an incoming or outgoing CAN messages. More...
union  CANmsg::msg_data

Defines

#define BAUD_RATE_PRESCALER   1
#define CAN_BRP   0x02
#define CAN_CONFIG_MODE   0b10000000
#define CAN_DISABLE_MODE   0b00100000
#define CAN_LISTEN_MODE   0b01100000
#define CAN_LOOPBACK_MODE   0b01000000
#define CAN_MODE   CAN_MODE_LEGACY
#define CAN_NORMAL_MODE   0b00000000
#define CAN_PHSEG1   0x01
#define CAN_PHSEG2   0x02
#define CAN_PROPSEG   0x06
#define CAN_SAM   0x00
#define CAN_SEG2PHTS   0x00
#define CAN_SJW   0x01
#define CANTX_DRIVE   CANTX_DRIVE_VDD
#define CFGS   6
#define EEPGD   7
#define GIE   7
#define PHASE_SEGMENT_1   6
#define PHASE_SEGMENT_2   6
#define PROPAGATION_SEGMENT   7
#define RD   0
#define RECEIVE_BUFFER_MODE   RECEIVE_STANDARD_MESSAGES
#define RXFUL   7
#define SAMPLE_TIMES   SAMPLE_THRICE
#define SEG2PHTS   SEG2PHTS_FREE
#define SYNC_JUMP_WIDTH   SYNC_JUMP_WIDTH_2X
#define TXREQ   3
#define WR   1
#define WREN   2

Enumerations

enum  BOOTLOADER_COMMAND {
  WRITE_FLASH_DATA_COMMAND = 0x00, READ_FLASH_COMMAND = 0x01, ERASE_FLASH_COMMAND = 0x02, RESET_COMMAND = 0x03,
  READ_VERSION_COMMAND = 0x04, WRITE_FLASH_ADDRESS_COMMAND = 0x05, MAGIC_PACKET = 0x0A, WRITE_FLASH_DATA_COMMAND = 0x00,
  READ_FLASH_COMMAND = 0x01, ERASE_FLASH_COMMAND = 0x02, RESET_COMMAND = 0x03, READ_VERSION_COMMAND = 0x04,
  WRITE_FLASH_ADDRESS_COMMAND = 0x05, START_FLASH_WRITE_COMMAND = 0x06, MAGIC_PACKET = 0x0A
}
enum  CAN_MODE_VALUES { CAN_MODE_LEGACY = 0b00000000, CAN_MODE_ENHANCED_LEGACY = 0b01000000, CAN_MODE_ENHANCED_FIFO = 0b10000000 }
enum  CAN_WAKE_VALUES { CAN_WAKE_ENABLE = 0b00000000, CAN_WAKE_DISABLE = 0b10000000 }
enum  CANTX_DRIVE_VALUES { CANTX_DRIVE_VDD = 0b00100000, CANTX_DRIVE_TRI_STATE = 0b00000000 }
enum  ECAN_RX_MSG_FLAGS {
  ECAN_RX_OVERFLOW = 0b00001000, ECAN_RX_INVALID_MSG = 0b00010000, ECAN_RX_XTD_FRAME = 0b00100000, ECAN_RX_RTR_FRAME = 0b01000000,
  ECAN_RX_DBL_BUFFERED = 0b10000000
}
enum  RECEIVE_BUFFER_MODE_VALUES { RECEIVE_ALL_MESSAGES = 0b01100000, RECEIVE_EXTENDED_MESSAGES = 0b01000000, RECEIVE_STANDARD_MESSAGES = 0b00100000 }
enum  SAMPLE_TIMES_VALUES { SAMPLE_ONCE = 0b00000000, SAMPLE_THRICE = 0b01000000 }
enum  SEG2PHTS_VALUES { SEG2PHTS_FREE = 0b10000000, SEG2PHTS_CALC = 0b00000000 }
enum  SYNC_JUMP_WIDTH_VALUES { SYNC_JUMP_WIDTH_1X = 0b00000000, SYNC_JUMP_WIDTH_2X = 0b01000000, SYNC_JUMP_WIDTH_3X = 0b10000000, SYNC_JUMP_WIDTH_4X = 0b11000000 }

Functions

static void acknowledge_packet (void)
static void can_init (void)
static void erase_flash (void)
static void handle_can_msg (void)
void main (void)
static int8u read_eeprom (int8u address)
static void read_flash (void)
void send_bootloader_hello_message (void)
static void sendCanMsg (void)
void serious_error (void)
void VIntH (void)
void VIntL (void)
static int8u we_should_boot (void)
static void write_eeprom (void)
static void write_flash_address (void)
static void write_flash_data (void)

Variables

struct CANmsg CanMsgR
 Structure to store CAN message just received.
struct CANmsg CanMsgT
 Structure to store CAN message to be transmitted.
int8u motor_id = 0xFF
 motor_id will be read from the EEPROM at boot time.
int8u position = 0x00
 this is the position of the current FLASH_WRITTING operation ( 0 <= position <= 32)

Define Documentation

#define BAUD_RATE_PRESCALER   1

Definition at line 240 of file boot.c.

#define CAN_BRP   0x02

Definition at line 37 of file boot.c.

#define CAN_CONFIG_MODE   0b10000000

Definition at line 44 of file boot.c.

#define CAN_DISABLE_MODE   0b00100000

Definition at line 47 of file boot.c.

#define CAN_LISTEN_MODE   0b01100000

Definition at line 45 of file boot.c.

#define CAN_LOOPBACK_MODE   0b01000000

Definition at line 46 of file boot.c.

#define CAN_MODE   CAN_MODE_LEGACY

Definition at line 247 of file boot.c.

#define CAN_NORMAL_MODE   0b00000000

Definition at line 48 of file boot.c.

#define CAN_PHSEG1   0x01

Definition at line 38 of file boot.c.

#define CAN_PHSEG2   0x02

Definition at line 39 of file boot.c.

#define CAN_PROPSEG   0x06

Definition at line 40 of file boot.c.

#define CAN_SAM   0x00

Definition at line 41 of file boot.c.

#define CAN_SEG2PHTS   0x00

Definition at line 42 of file boot.c.

#define CAN_SJW   0x01

Definition at line 36 of file boot.c.

Definition at line 249 of file boot.c.

#define CFGS   6

Definition at line 56 of file boot.c.

#define EEPGD   7

Definition at line 53 of file boot.c.

#define GIE   7

Definition at line 57 of file boot.c.

#define PHASE_SEGMENT_1   6

Definition at line 244 of file boot.c.

#define PHASE_SEGMENT_2   6

Definition at line 245 of file boot.c.

#define PROPAGATION_SEGMENT   7

Definition at line 246 of file boot.c.

#define RD   0

Definition at line 50 of file boot.c.

Definition at line 248 of file boot.c.

#define RXFUL   7

Definition at line 51 of file boot.c.

Definition at line 243 of file boot.c.

#define SEG2PHTS   SEG2PHTS_FREE

Definition at line 242 of file boot.c.

Definition at line 241 of file boot.c.

#define TXREQ   3

Definition at line 52 of file boot.c.

#define WR   1

Definition at line 55 of file boot.c.

#define WREN   2

Definition at line 54 of file boot.c.


Enumeration Type Documentation

These are the different commands the bootloader accepts.

Enumerator:
WRITE_FLASH_DATA_COMMAND 
READ_FLASH_COMMAND 
ERASE_FLASH_COMMAND 
RESET_COMMAND 
READ_VERSION_COMMAND 
WRITE_FLASH_ADDRESS_COMMAND 
MAGIC_PACKET 
WRITE_FLASH_DATA_COMMAND 
READ_FLASH_COMMAND 
ERASE_FLASH_COMMAND 
RESET_COMMAND 
READ_VERSION_COMMAND 
WRITE_FLASH_ADDRESS_COMMAND 
START_FLASH_WRITE_COMMAND 
MAGIC_PACKET 

Definition at line 62 of file boot.c.

Used for configuration of the CAN peripheral We're using CAN_MODE_LEGACY for the simple slave

Enumerator:
CAN_MODE_LEGACY 
CAN_MODE_ENHANCED_LEGACY 
CAN_MODE_ENHANCED_FIFO 

Definition at line 213 of file boot.c.

Used for configuration of the CAN peripheral

Enumerator:
CAN_WAKE_ENABLE 
CAN_WAKE_DISABLE 

Definition at line 205 of file boot.c.

Used for configuration of the CAN peripheral We should use VDD mode

Enumerator:
CANTX_DRIVE_VDD 
CANTX_DRIVE_TRI_STATE 

Definition at line 231 of file boot.c.

Enumerator:
ECAN_RX_OVERFLOW 
ECAN_RX_INVALID_MSG 
ECAN_RX_XTD_FRAME 
ECAN_RX_RTR_FRAME 
ECAN_RX_DBL_BUFFERED 

Definition at line 73 of file boot.c.

Used for configuration of the CAN peripheral

Enumerator:
RECEIVE_ALL_MESSAGES 
RECEIVE_EXTENDED_MESSAGES 
RECEIVE_STANDARD_MESSAGES 

Definition at line 222 of file boot.c.

Used for configuration of the CAN peripheral How many times to sample the bits

Enumerator:
SAMPLE_ONCE 
SAMPLE_THRICE 

Definition at line 196 of file boot.c.

Used for configuration of the CAN peripheral The SEG2PHTS bit controls how the length of Phase Segment 2 is determined.

Enumerator:
SEG2PHTS_FREE 
SEG2PHTS_CALC 

Definition at line 187 of file boot.c.

Used for configuration of the CAN peripheral The sync jump width affects how much adjustment is allowed when synchronising to the bus edges.

Enumerator:
SYNC_JUMP_WIDTH_1X 
SYNC_JUMP_WIDTH_2X 
SYNC_JUMP_WIDTH_3X 
SYNC_JUMP_WIDTH_4X 

Definition at line 175 of file boot.c.


Function Documentation

static void acknowledge_packet ( void  ) [static]

This function acknowledges ( most of ) the commands. Which means sending back the same content, same length, same SID but with the "ACK" bit (0x10) set

Author:
Yann Sionneau

Definition at line 386 of file boot.c.

static void can_init ( void  ) [static]

This initializes the ECAN module of the PIC18F It sets up the baud rate, filters, masks and CAN MODE.

Author:
Yann Sionneau

Definition at line 256 of file boot.c.

static void erase_flash ( void  ) [static]

This function erases the program memory (which is flash) It only erases the user application So it starts erasing after the bootloader and stops before the debugger code

Author:
Yann Sionneau

Definition at line 323 of file boot.c.

static void handle_can_msg ( void  ) [static]

This function first tests if we received a CAN message. If not it does nothing. If we received a CAN message, it fills the CanMsgR struct with it's data And then frees up the reception slot, and then executes the command which is inside the CAN message.

Author:
Yann Sionneau

Definition at line 493 of file boot.c.

void main ( void  )

Main function This is always executed at startup of the PIC18F regardless of whether there is a user application or not.

Author:
Yann Sionneau

Definition at line 609 of file boot.c.

static int8u read_eeprom ( int8u  address) [static]

Read one byte of EEPROM.

Parameters:
addressThe EEPROM address of the byte to read
Returns:
The value read from the EEPROM
Author:
Yann Sionneau

Definition at line 149 of file boot.c.

static void read_flash ( void  ) [static]

This function reads the program memory (which is FLASH). It reads 8 bytes at the address present in the incoming CAN command and sends it back through CAN too.

Author:
Yann Sionneau

Definition at line 360 of file boot.c.

Sending a hello message tells the host the we have entered bootloader mode.

Author:
Yann Sionneau

Definition at line 587 of file boot.c.

static void sendCanMsg ( void  ) [static]

This is the "Send Can Message" function. It sends the content of the CanMsgT struct over the CAN bus.

Author:
Yann Sionneau

Definition at line 300 of file boot.c.

void serious_error ( void  )

Called in case a serious error is discovered. Just sits and does nothing. Safer that way.

Author:
Yann Sionneau

Definition at line 575 of file boot.c.

void VIntH ( void  )

This function remaps the High Interrupt Vector The user's High interrupt will be placed at address 0x0270, and this function will just straight to it.

Author:
Yann Sionneau

Definition at line 114 of file boot.c.

void VIntL ( void  )

This function remaps the Low Interrupt Vector The user's Low interrupt will be placed at address 0x0270, and this function will just straight to it.

Author:
Yann Sionneau

Definition at line 129 of file boot.c.

static int8u we_should_boot ( void  ) [static]

This checks if we should boot (start the user application), or stay in bootloader mode. We check the last byte of EEPROM if it is 0xFF it means no firmware has been written in user application memory and we need to stay in bootloader mode, waiting for commands to flash a user application through the CAN bootloading protocol. If it is NOT 0xFF, it means there is a firmware and we should boot directly on it.

Author:
Yann Sionneau

Definition at line 166 of file boot.c.

static void write_eeprom ( void  ) [static]

This function writes to the EEPROM memory The only goal of this function is to write something different than 0xFF at the last memory location in EEPROM. This happens right after a flashing operation has been successfull, to make sure that at the following reboot of the PIC18F, it will boot directly to the user application and not stay in bootloader mode.

This function writes 0x42 to the last byte (address 0xFF).

Author:
Yann Sionneau

Definition at line 463 of file boot.c.

static void write_flash_address ( void  ) [static]

This is setting up the flash addressing registers to some address received by a CAN command. This sets everything up for future writes.

Author:
Yann Sionneau

Definition at line 399 of file boot.c.

static void write_flash_data ( void  ) [static]

This function does the actual writting in program memory (which is FLASH). Writting the flash has to be done in blocks of 32 bytes. But we can only transport 8 bytes of data in a CAN message, so we do the block writting in 4 CAN commands. So this command writes 8 bytes, and is called several times (4 times in theory) Each time it is called the "position" variable gets added 8. When position gets equal to 32, it means we have buffered a block and we can start the writing procedure.

Author:
Yann Sionneau

Definition at line 418 of file boot.c.


Variable Documentation

struct CANmsg CanMsgR

Structure to store CAN message just received.

Definition at line 100 of file boot.c.

struct CANmsg CanMsgT

Structure to store CAN message to be transmitted.

Definition at line 99 of file boot.c.

int8u motor_id = 0xFF

motor_id will be read from the EEPROM at boot time.

Definition at line 103 of file boot.c.

int8u position = 0x00

this is the position of the current FLASH_WRITTING operation ( 0 <= position <= 32)

Definition at line 104 of file boot.c.



sr_external_dependencies
Author(s): Ugo Cupcic/ ugo@shadowrobot.com, software@shadowrobot.com
autogenerated on Thu Jan 2 2014 12:01:42