Skip to content

Commit 3bc596a

Browse files
unknownunknown
unknown
authored and
unknown
committed
Adding UART/USB bootloader project
1 parent b6658c1 commit 3bc596a

27 files changed

+10510
-0
lines changed

bootloaders/zero/drivers/cdc_enumerate.c

+742
Large diffs are not rendered by default.
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* ----------------------------------------------------------------------------
2+
* SAM Software Package License
3+
* ----------------------------------------------------------------------------
4+
* Copyright (c) 2011-2012, Atmel Corporation
5+
*
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following condition is met:
10+
*
11+
* Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the disclaimer below.
13+
*
14+
* Atmel's name may not be used to endorse or promote products derived from
15+
* this software without specific prior written permission.
16+
*
17+
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20+
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
* ----------------------------------------------------------------------------
28+
*/
29+
30+
#ifndef CDC_ENUMERATE_H
31+
#define CDC_ENUMERATE_H
32+
33+
#include "iosamd21.h"
34+
#include "stdbool.h"
35+
36+
#define USB_EP_OUT 2
37+
#define USB_EP_OUT_SIZE 0x40
38+
#define USB_EP_IN 1
39+
#define USB_EP_IN_SIZE 0x40
40+
#define USB_EP_COMM 3
41+
#define MAX_EP 4
42+
43+
#define NVM_USB_PAD_TRANSN_POS 45
44+
#define NVM_USB_PAD_TRANSN_SIZE 5
45+
#define NVM_USB_PAD_TRANSP_POS 50
46+
#define NVM_USB_PAD_TRANSP_SIZE 5
47+
#define NVM_USB_PAD_TRIM_POS 55
48+
#define NVM_USB_PAD_TRIM_SIZE 3
49+
50+
typedef struct _USB_CDC
51+
{
52+
// Private members
53+
Usb *pUsb;
54+
uint8_t currentConfiguration;
55+
uint8_t currentConnection;
56+
// Public Methods:
57+
uint8_t (*IsConfigured)(struct _USB_CDC *pCdc);
58+
uint32_t (*Write) (struct _USB_CDC *pCdc, const char *pData, uint32_t length, uint8_t ep_num);
59+
uint32_t (*Read) (struct _USB_CDC *pCdc, char *pData, uint32_t length);
60+
} USB_CDC, *P_USB_CDC;
61+
62+
typedef struct {
63+
uint32_t dwDTERate;
64+
uint8_t bCharFormat;
65+
uint8_t bParityType;
66+
uint8_t bDataBits;
67+
} usb_cdc_line_coding_t;
68+
69+
/**
70+
* \brief Initializes the USB module
71+
*
72+
* \return Pointer to the USB CDC structure
73+
*/
74+
P_USB_CDC usb_init(void);
75+
76+
/**
77+
* \brief Sends a single byte through USB CDC
78+
*
79+
* \param Data to send
80+
* \return number of data sent
81+
*/
82+
int cdc_putc(int value);
83+
84+
/**
85+
* \brief Reads a single byte through USB CDC
86+
*
87+
* \return Data read through USB
88+
*/
89+
int cdc_getc(void);
90+
91+
/**
92+
* \brief Checks if a character has been received on USB CDC
93+
*
94+
* \return \c 1 if a byte is ready to be read.
95+
*/
96+
bool cdc_is_rx_ready(void);
97+
98+
/**
99+
* \brief Sends buffer on USB CDC
100+
*
101+
* \param data pointer
102+
* \param number of data to send
103+
* \return number of data sent
104+
*/
105+
uint32_t cdc_write_buf(void const* data, uint32_t length);
106+
107+
/**
108+
* \brief Gets data on USB CDC
109+
*
110+
* \param data pointer
111+
* \param number of data to read
112+
* \return number of data read
113+
*/
114+
uint32_t cdc_read_buf(void* data, uint32_t length);
115+
116+
/**
117+
* \brief Gets specified number of bytes on USB CDC
118+
*
119+
* \param data pointer
120+
* \param number of data to read
121+
* \return number of data read
122+
*/
123+
uint32_t cdc_read_buf_xmd(void* data, uint32_t length);
124+
125+
126+
#endif // CDC_ENUMERATE_H
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* ----------------------------------------------------------------------------
2+
* SAM Software Package License
3+
* ----------------------------------------------------------------------------
4+
* Copyright (c) 2011-2012, Atmel Corporation
5+
*
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following condition is met:
10+
*
11+
* Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the disclaimer below.
13+
*
14+
* Atmel's name may not be used to endorse or promote products derived from
15+
* this software without specific prior written permission.
16+
*
17+
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20+
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
* ----------------------------------------------------------------------------
28+
*/
29+
30+
#include "uart_driver.h"
31+
32+
bool uart_drv_error_flag = false;
33+
34+
uint32_t uart_get_sercom_index(Sercom *sercom_instance)
35+
{
36+
/* Save all available SERCOM instances for compare. */
37+
Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
38+
39+
/* Find index for sercom instance. */
40+
for (uint32_t i = 0; i < SERCOM_INST_NUM; i++) {
41+
if ((uintptr_t)sercom_instance == (uintptr_t)sercom_instances[i]) {
42+
return i;
43+
}
44+
}
45+
46+
return 0;
47+
}
48+
49+
void uart_basic_init(Sercom *sercom, uint16_t baud_val, enum uart_pad_settings pad_conf)
50+
{
51+
/* Wait for synchronization */
52+
while(sercom->USART.SYNCBUSY.bit.ENABLE);
53+
/* Disable the SERCOM UART module */
54+
sercom->USART.CTRLA.bit.ENABLE = 0;
55+
/* Wait for synchronization */
56+
while(sercom->USART.SYNCBUSY.bit.SWRST);
57+
/* Perform a software reset */
58+
sercom->USART.CTRLA.bit.SWRST = 1;
59+
/* Wait for synchronization */
60+
while(sercom->USART.CTRLA.bit.SWRST);
61+
/* Wait for synchronization */
62+
while(sercom->USART.SYNCBUSY.bit.SWRST || sercom->USART.SYNCBUSY.bit.ENABLE);
63+
/* Update the UART pad settings, mode and data order settings */
64+
sercom->USART.CTRLA.reg = pad_conf | SERCOM_USART_CTRLA_MODE(1) | SERCOM_USART_CTRLA_DORD;
65+
/* Wait for synchronization */
66+
while(sercom->USART.SYNCBUSY.bit.CTRLB);
67+
/* Enable transmit and receive and set data size to 8 bits */
68+
sercom->USART.CTRLB.reg = SERCOM_USART_CTRLB_RXEN | SERCOM_USART_CTRLB_TXEN | SERCOM_USART_CTRLB_CHSIZE(0);
69+
/* Load the baud value */
70+
sercom->USART.BAUD.reg = baud_val;
71+
/* Wait for synchronization */
72+
while(sercom->USART.SYNCBUSY.bit.ENABLE);
73+
/* Enable SERCOM UART */
74+
sercom->USART.CTRLA.bit.ENABLE = 1;
75+
}
76+
77+
void uart_disable(Sercom *sercom)
78+
{
79+
/* Wait for synchronization */
80+
while(sercom->USART.SYNCBUSY.bit.ENABLE);
81+
/* Disable SERCOM UART */
82+
sercom->USART.CTRLA.bit.ENABLE = 0;
83+
}
84+
85+
void uart_write_byte(Sercom *sercom, uint8_t data)
86+
{
87+
/* Wait for Data Register Empty flag */
88+
while(!sercom->USART.INTFLAG.bit.DRE);
89+
/* Write the data to DATA register */
90+
sercom->USART.DATA.reg = (uint16_t)data;
91+
}
92+
93+
uint8_t uart_read_byte(Sercom *sercom)
94+
{
95+
/* Wait for Receive Complete flag */
96+
while(!sercom->USART.INTFLAG.bit.RXC);
97+
/* Check for errors */
98+
if (sercom->USART.STATUS.bit.PERR || sercom->USART.STATUS.bit.FERR || sercom->USART.STATUS.bit.BUFOVF)
99+
/* Set the error flag */
100+
uart_drv_error_flag = true;
101+
/* Return the read data */
102+
return((uint8_t)sercom->USART.DATA.reg);
103+
}
104+
105+
void uart_write_buffer_polled(Sercom *sercom, uint8_t *ptr, uint16_t length)
106+
{
107+
/* Do the following for specified length */
108+
do {
109+
/* Wait for Data Register Empty flag */
110+
while(!sercom->USART.INTFLAG.bit.DRE);
111+
/* Send data from the buffer */
112+
sercom->USART.DATA.reg = (uint16_t)*ptr++;
113+
} while (length--);
114+
}
115+
116+
void uart_read_buffer_polled(Sercom *sercom, uint8_t *ptr, uint16_t length)
117+
{
118+
/* Do the following for specified length */
119+
do {
120+
/* Wait for Receive Complete flag */
121+
while(!sercom->USART.INTFLAG.bit.RXC);
122+
/* Check for errors */
123+
if (sercom->USART.STATUS.bit.PERR || sercom->USART.STATUS.bit.FERR || sercom->USART.STATUS.bit.BUFOVF)
124+
/* Set the error flag */
125+
uart_drv_error_flag = true;
126+
/* Store the read data to the buffer */
127+
*ptr++ = (uint8_t)sercom->USART.DATA.reg;
128+
} while (length--);
129+
}
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* ----------------------------------------------------------------------------
2+
* SAM Software Package License
3+
* ----------------------------------------------------------------------------
4+
* Copyright (c) 2011-2012, Atmel Corporation
5+
*
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following condition is met:
10+
*
11+
* Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the disclaimer below.
13+
*
14+
* Atmel's name may not be used to endorse or promote products derived from
15+
* this software without specific prior written permission.
16+
*
17+
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20+
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23+
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
* ----------------------------------------------------------------------------
28+
*/
29+
30+
#ifndef UART_DRIVER_H
31+
#define UART_DRIVER_H
32+
#include <stdio.h>
33+
#include "iosamd21.h"
34+
#include <stdbool.h>
35+
36+
#define PINMUX_UNUSED 0xFFFFFFFF
37+
#define GCLK_ID_SERCOM0_CORE 0x14
38+
39+
/* SERCOM UART available pad settings */
40+
enum uart_pad_settings {
41+
UART_RX_PAD0_TX_PAD2 = SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO(1),
42+
UART_RX_PAD1_TX_PAD2 = SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO(1),
43+
UART_RX_PAD2_TX_PAD0 = SERCOM_USART_CTRLA_RXPO(2),
44+
UART_RX_PAD3_TX_PAD0 = SERCOM_USART_CTRLA_RXPO(3),
45+
UART_RX_PAD1_TX_PAD0 = SERCOM_USART_CTRLA_RXPO(1),
46+
UART_RX_PAD3_TX_PAD2 = SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(1),
47+
};
48+
49+
/**
50+
* \brief Gets the index of the provided SERCOM instance
51+
*
52+
* \param Pointer to SERCOM instance
53+
* \return Index of the SERCOM module
54+
*/
55+
uint32_t uart_get_sercom_index(Sercom *sercom_instance);
56+
57+
/**
58+
* \brief Initializes the UART
59+
*
60+
* \param Pointer to SERCOM instance
61+
* \param Baud value corresponding to the desired baudrate
62+
* \param SERCOM pad settings
63+
*/
64+
void uart_basic_init(Sercom *sercom, uint16_t baud_val, enum uart_pad_settings pad_conf);
65+
66+
/**
67+
* \brief Disables UART interface
68+
*
69+
* \param Pointer to SERCOM instance
70+
*/
71+
void uart_disable(Sercom *sercom);
72+
73+
/**
74+
* \brief Sends a single byte through UART interface
75+
*
76+
* \param Pointer to SERCOM instance
77+
* \param Data to send
78+
*/
79+
void uart_write_byte(Sercom *sercom, uint8_t data);
80+
81+
/**
82+
* \brief Reads a single character from UART interface
83+
*
84+
* \param Pointer to SERCOM instance
85+
* \return Data byte read
86+
*/
87+
uint8_t uart_read_byte(Sercom *sercom);
88+
89+
/**
90+
* \brief Sends buffer on UART interface
91+
*
92+
* \param Pointer to SERCOM instance
93+
* \param Pointer to data to send
94+
* \param Number of bytes to send
95+
*/
96+
void uart_write_buffer_polled(Sercom *sercom, uint8_t *ptr, uint16_t length);
97+
98+
/**
99+
* \brief Reads data on UART interface
100+
*
101+
* \param Pointer to SERCOM instance
102+
* \param Pointer to store read data
103+
* \param Number of bytes to read
104+
*/
105+
void uart_read_buffer_polled(Sercom *sercom, uint8_t *ptr, uint16_t length);
106+
107+
#endif

0 commit comments

Comments
 (0)