2017-01-22 06:06:32 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2017, Alex Taradov <alex@taradov.com>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*- Includes ----------------------------------------------------------------*/
|
|
|
|
|
#include <stdlib.h>
|
2017-01-27 10:38:25 +08:00
|
|
|
#include <string.h>
|
2017-01-22 06:06:32 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdbool.h>
|
2017-01-27 10:38:25 +08:00
|
|
|
#include <stdalign.h>
|
2017-01-22 06:06:32 +08:00
|
|
|
#include "samd11.h"
|
|
|
|
|
#include "hal_gpio.h"
|
|
|
|
|
#include "nvm_data.h"
|
|
|
|
|
#include "usb.h"
|
|
|
|
|
#include "dap.h"
|
|
|
|
|
#include "dap_config.h"
|
|
|
|
|
|
|
|
|
|
/*- Definitions -------------------------------------------------------------*/
|
2017-05-08 14:18:32 +08:00
|
|
|
#if defined(BOARD_SWD_USB_MINI)
|
|
|
|
|
HAL_GPIO_PIN(LED, A, 14);
|
|
|
|
|
#elif defined(BOARD_SWD_USB_STD)
|
|
|
|
|
HAL_GPIO_PIN(LED, A, 4);
|
|
|
|
|
#else
|
|
|
|
|
#error Undefined board
|
|
|
|
|
#endif
|
2017-01-22 06:06:32 +08:00
|
|
|
|
|
|
|
|
#define APP_EP_SEND 1
|
|
|
|
|
#define APP_EP_RECV 2
|
|
|
|
|
|
2017-05-08 14:18:32 +08:00
|
|
|
#if defined(BOARD_SWD_USB_MINI)
|
|
|
|
|
#define APP_PWM_PER 0xffff
|
|
|
|
|
#define APP_PWM_DIM 0xf000
|
|
|
|
|
#define APP_PWM_BRIGHT 0
|
|
|
|
|
#elif defined(BOARD_SWD_USB_STD)
|
|
|
|
|
#define APP_PWM_PER 0xffff
|
|
|
|
|
#define APP_PWM_DIM 0x1000
|
|
|
|
|
#define APP_PWM_BRIGHT 0xf000
|
|
|
|
|
#endif
|
2017-01-22 06:06:32 +08:00
|
|
|
|
|
|
|
|
/*- Variables ---------------------------------------------------------------*/
|
2017-05-10 12:27:55 +08:00
|
|
|
static alignas(4) uint8_t app_request_buffer[DAP_CONFIG_PACKET_SIZE];
|
|
|
|
|
static alignas(4) uint8_t app_response_buffer[DAP_CONFIG_PACKET_SIZE];
|
2017-01-22 06:06:32 +08:00
|
|
|
|
|
|
|
|
/*- Implementations ---------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
static void sys_init(void)
|
|
|
|
|
{
|
|
|
|
|
uint32_t coarse, fine;
|
2017-05-10 12:27:55 +08:00
|
|
|
uint32_t sn = 0;
|
2017-01-22 06:06:32 +08:00
|
|
|
|
|
|
|
|
SYSCTRL->OSC8M.bit.PRESC = 0;
|
|
|
|
|
|
|
|
|
|
SYSCTRL->INTFLAG.reg = SYSCTRL_INTFLAG_BOD33RDY | SYSCTRL_INTFLAG_BOD33DET |
|
|
|
|
|
SYSCTRL_INTFLAG_DFLLRDY;
|
|
|
|
|
|
|
|
|
|
NVMCTRL->CTRLB.bit.RWS = 2;
|
|
|
|
|
|
|
|
|
|
coarse = NVM_READ_CAL(NVM_DFLL48M_COARSE_CAL);
|
|
|
|
|
fine = NVM_READ_CAL(NVM_DFLL48M_FINE_CAL);
|
|
|
|
|
|
|
|
|
|
SYSCTRL->DFLLCTRL.reg = 0; // See Errata 9905
|
|
|
|
|
while (0 == (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY));
|
|
|
|
|
|
|
|
|
|
SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_MUL(48000);
|
|
|
|
|
SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(fine);
|
|
|
|
|
|
|
|
|
|
SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE | SYSCTRL_DFLLCTRL_USBCRM |
|
|
|
|
|
SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_BPLCKC | SYSCTRL_DFLLCTRL_CCDIS |
|
|
|
|
|
SYSCTRL_DFLLCTRL_STABLE;
|
|
|
|
|
|
|
|
|
|
while (0 == (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY));
|
|
|
|
|
|
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(0) | GCLK_GENCTRL_SRC(GCLK_SOURCE_DFLL48M) |
|
|
|
|
|
GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN;
|
|
|
|
|
while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
|
2017-05-10 12:27:55 +08:00
|
|
|
|
|
|
|
|
sn ^= *(volatile uint32_t *)0x0080a00c;
|
|
|
|
|
sn ^= *(volatile uint32_t *)0x0080a040;
|
|
|
|
|
sn ^= *(volatile uint32_t *)0x0080a044;
|
|
|
|
|
sn ^= *(volatile uint32_t *)0x0080a048;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
|
usb_serial_number[i] = "0123456789ABCDEF"[(sn >> (i * 4)) & 0xf];
|
|
|
|
|
|
2020-02-24 06:49:13 +08:00
|
|
|
usb_serial_number[8] = 0;
|
2017-01-22 06:06:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
static void led_init(void)
|
|
|
|
|
{
|
|
|
|
|
HAL_GPIO_LED_out();
|
|
|
|
|
HAL_GPIO_LED_pmuxen(HAL_GPIO_PMUX_F);
|
|
|
|
|
|
|
|
|
|
PM->APBCMASK.reg |= PM_APBCMASK_TCC0;
|
|
|
|
|
|
|
|
|
|
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(TCC0_GCLK_ID) |
|
|
|
|
|
GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(0);
|
|
|
|
|
|
|
|
|
|
TCC0->CTRLA.reg =
|
|
|
|
|
TCC_CTRLA_PRESCALER(TCC_CTRLA_PRESCALER_DIV1_Val) |
|
|
|
|
|
TCC_CTRLA_PRESCSYNC(TCC_CTRLA_PRESCSYNC_PRESC_Val);
|
|
|
|
|
|
|
|
|
|
TCC0->WAVE.reg = TCC_WAVE_WAVEGEN_NPWM;
|
|
|
|
|
TCC0->PER.reg = APP_PWM_PER;
|
|
|
|
|
TCC0->CC[0].reg = APP_PWM_DIM;
|
|
|
|
|
TCC0->CTRLA.bit.ENABLE = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void app_led_set_state(int state)
|
|
|
|
|
{
|
|
|
|
|
TCC0->CTRLA.bit.ENABLE = 0;
|
|
|
|
|
TCC0->COUNT.reg = 0;
|
|
|
|
|
TCC0->CC[0].reg = state ? APP_PWM_BRIGHT : APP_PWM_DIM;
|
|
|
|
|
TCC0->CTRLA.bit.ENABLE = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void usb_send_callback(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void usb_recv_callback(void)
|
|
|
|
|
{
|
|
|
|
|
dap_process_request(app_request_buffer, app_response_buffer);
|
|
|
|
|
|
|
|
|
|
usb_send(APP_EP_SEND, app_response_buffer, sizeof(app_response_buffer), usb_send_callback);
|
|
|
|
|
|
|
|
|
|
usb_recv(APP_EP_RECV, app_request_buffer, sizeof(app_request_buffer), usb_recv_callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
void usb_configuration_callback(int config)
|
|
|
|
|
{
|
|
|
|
|
usb_recv(APP_EP_RECV, app_request_buffer, sizeof(app_request_buffer), usb_recv_callback);
|
|
|
|
|
|
|
|
|
|
(void)config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
sys_init();
|
|
|
|
|
led_init();
|
|
|
|
|
dap_init();
|
|
|
|
|
usb_init();
|
|
|
|
|
|
|
|
|
|
while (1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|