Skip to content

Commit

Permalink
Make CDC/UART interface configurable from Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Aug 29, 2018
1 parent 2267161 commit bc2c779
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 72 deletions.
3 changes: 2 additions & 1 deletion bootloaders/zero/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ NAME?=samd21_sam_ba

# -----------------------------------------------------------------------------
# Compiler options
CFLAGS_EXTRA=-D__SAMD21G18A__ -DBOARD_ID_$(BOARD_ID)
SAM_BA_INTERFACES?=SAM_BA_BOTH_INTERFACES
CFLAGS_EXTRA=-D__SAMD21G18A__ -DBOARD_ID_$(BOARD_ID) -D$(SAM_BA_INTERFACES)
CFLAGS=-mthumb -mcpu=cortex-m0plus -Wall -c -std=gnu99 -ffunction-sections -fdata-sections -nostdlib -nostartfiles --param max-inline-insns-single=500
ifdef DEBUG
CFLAGS+=-g3 -O1 -DDEBUG=1
Expand Down
11 changes: 0 additions & 11 deletions bootloaders/zero/board_definitions_arduino_mkrvidor4000.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@
//#define BOOT_LOAD_PIN PIN_PA21
//#define BOOT_LOAD_PIN PIN_PA15

#define BOOT_USART_MODULE SERCOM5
#define BOOT_USART_BUS_CLOCK_INDEX PM_APBCMASK_SERCOM5
#define BOOT_USART_PER_CLOCK_INDEX GCLK_CLKCTRL_ID_SERCOM5_CORE_Val
#define BOOT_USART_PAD_SETTINGS UART_RX_PAD3_TX_PAD2
#define BOOT_USART_PAD3 PINMUX_PB23D_SERCOM5_PAD3
#define BOOT_USART_PAD2 PINMUX_PB22D_SERCOM5_PAD2
#define BOOT_USART_PAD1 PINMUX_UNUSED
#define BOOT_USART_PAD0 PINMUX_UNUSED

/* Master clock frequency */
#define CPU_FREQUENCY (48000000ul)
#define VARIANT_MCK CPU_FREQUENCY
Expand Down Expand Up @@ -94,8 +85,6 @@ typedef struct __attribute__((packed)) {
uint32_t force;
} externalFlashSignatures;

#define SAM_BA_INTERFACE SAM_BA_USBCDC_ONLY

// No RX/TX led
//#define BOARD_LEDRX_PORT
//#define BOARD_LEDRX_PIN
Expand Down
1 change: 1 addition & 0 deletions bootloaders/zero/board_driver_jtag.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "board_driver_jtag.h"
#include <string.h>

#ifdef ENABLE_JTAG_LOAD

Expand Down
5 changes: 5 additions & 0 deletions bootloaders/zero/board_driver_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/

#include "board_driver_serial.h"
#include "board_definitions.h"

#if defined(SAM_BA_UART_ONLY) || defined(SAM_BA_BOTH_INTERFACES)

bool uart_drv_error_flag = false;

Expand Down Expand Up @@ -102,3 +105,5 @@ void uart_read_buffer_polled(Sercom *sercom, uint8_t *ptr, uint16_t length)
*ptr++ = (uint8_t)sercom->USART.DATA.reg;
} while (length--);
}

#endif
4 changes: 4 additions & 0 deletions bootloaders/zero/board_driver_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "sam_ba_usb.h"
#include "sam_ba_cdc.h"

#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)

#define NVM_USB_PAD_TRANSN_POS (45)
#define NVM_USB_PAD_TRANSN_SIZE (5)
#define NVM_USB_PAD_TRANSP_POS (50)
Expand Down Expand Up @@ -359,3 +361,5 @@ void USB_Configure(Usb *pUsb)
usb_endpoint_table[USB_EP_COMM].DeviceDescBank[1].PCKSIZE.bit.SIZE = 0;
pUsb->DEVICE.DeviceEndpoint[USB_EP_COMM].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
}

#endif
2 changes: 1 addition & 1 deletion bootloaders/zero/build_all_bootloaders.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mv -v samd21_sam_ba_arduino_mkrwan1300.* ../mkrwan1300/
BOARD_ID=arduino_mkrwifi1010 NAME=samd21_sam_ba_arduino_mkrwifi1010 make clean all
mv -v samd21_sam_ba_arduino_mkrwifi1010.* ../mkrwifi1010/

BOARD_ID=arduino_mkrvidor4000 NAME=samd21_sam_ba_arduino_mkrvidor make clean all
BOARD_ID=arduino_mkrvidor4000 SAM_BA_INTERFACES=SAM_BA_USBCDC_ONLY NAME=samd21_sam_ba_arduino_mkrvidor make clean all
mv -v samd21_sam_ba_arduino_mkrvidor.* ../mkrvidor4000/

echo Done building bootloaders!
Expand Down
10 changes: 5 additions & 5 deletions bootloaders/zero/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static void check_start_application(void)
*/
int main(void)
{
#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
P_USB_CDC pCdc;
#endif
DEBUG_PIN_HIGH;
Expand Down Expand Up @@ -196,12 +196,12 @@ int main(void)
clockout(0, 1);
#endif

#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_UART_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
/* UART is enabled in all cases */
serial_open();
#endif

#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
pCdc = usb_init();
#endif

Expand All @@ -220,7 +220,7 @@ int main(void)
/* Wait for a complete enum on usb or a '#' char on serial line */
while (1)
{
#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
if (pCdc->IsConfigured(pCdc) != 0)
{
main_b_cdc_enable = true;
Expand All @@ -238,7 +238,7 @@ int main(void)
}
#endif

#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_UART_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
/* Check if a '#' has been received */
if (!main_b_cdc_enable && serial_sharp_received())
{
Expand Down
8 changes: 4 additions & 4 deletions bootloaders/zero/sam_ba_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef struct
uint32_t (*getdata_xmd)(void* data, uint32_t length);
} t_monitor_if;

#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_UART_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
/* Initialize structures with function pointers from supported interfaces */
const t_monitor_if uart_if =
{
Expand All @@ -65,7 +65,7 @@ const t_monitor_if uart_if =
};
#endif

#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
//Please note that USB doesn't use Xmodem protocol, since USB already includes flow control and data verification
//Data are simply forwarded without further coding.
const t_monitor_if usbcdc_if =
Expand Down Expand Up @@ -94,15 +94,15 @@ volatile uint16_t rxLEDPulse = 0; // time remaining for Rx LED pulse

void sam_ba_monitor_init(uint8_t com_interface)
{
#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_UART_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
//Selects the requested interface for future actions
if (com_interface == SAM_BA_INTERFACE_USART)
{
ptr_monitor_if = (t_monitor_if*) &uart_if;
b_sam_ba_interface_usart = true;
}
#endif
#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY || SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
if (com_interface == SAM_BA_INTERFACE_USBCDC)
{
ptr_monitor_if = (t_monitor_if*) &usbcdc_if;
Expand Down
9 changes: 2 additions & 7 deletions bootloaders/zero/sam_ba_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@

#define SAM_BA_VERSION "2.0"

/* Enable the interfaces to save code size */
#define SAM_BA_BOTH_INTERFACES 0
#define SAM_BA_UART_ONLY 1
#define SAM_BA_USBCDC_ONLY 2

#ifndef SAM_BA_INTERFACE
#define SAM_BA_INTERFACE SAM_BA_BOTH_INTERFACES
#if !defined(SAM_BA_BOTH_INTERFACES) && !defined(SAM_BA_UART_ONLY) && !defined(SAM_BA_USBCDC_ONLY)
#define SAM_BA_BOTH_INTERFACES
#endif

/* Selects USB as the communication interface of the monitor */
Expand Down
90 changes: 47 additions & 43 deletions bootloaders/zero/sam_ba_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "sam_ba_serial.h"
#include "board_driver_serial.h"

#if defined(SAM_BA_UART_ONLY) || defined(SAM_BA_BOTH_INTERFACES)

/* Local reference to current Usart instance in use with this driver */
//struct usart_module usart_sam_ba;

Expand Down Expand Up @@ -200,49 +202,6 @@ uint32_t serial_getdata(void* data, uint32_t length)
return (1);
}

static const uint16_t crc16Table[256]=
{
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
};

//*----------------------------------------------------------------------------
//* \brief Compute the CRC
//*----------------------------------------------------------------------------
unsigned short serial_add_crc(char ptr, unsigned short crc)
{
return (crc << 8) ^ crc16Table[((crc >> 8) ^ ptr) & 0xff];
}

//*----------------------------------------------------------------------------
//* \brief
Expand Down Expand Up @@ -532,3 +491,48 @@ uint32_t serial_getdata_xmd(void* data, uint32_t length)
// return(b_run);
}

#endif

static const uint16_t crc16Table[256]=
{
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
};

//*----------------------------------------------------------------------------
//* \brief Compute the CRC
//*----------------------------------------------------------------------------
unsigned short serial_add_crc(char ptr, unsigned short crc)
{
return (crc << 8) ^ crc16Table[((crc >> 8) ^ ptr) & 0xff];
}

0 comments on commit bc2c779

Please sign in to comment.