Skip to content

Commit

Permalink
code crop
Browse files Browse the repository at this point in the history
  • Loading branch information
kimstik committed Dec 7, 2020
1 parent e3fcd8d commit 14959e8
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 64 deletions.
Binary file removed .vs/SAM-BA_MONITOR_ROMLESS/v14/.atsuo
Binary file not shown.
22 changes: 0 additions & 22 deletions SAM-BA_MONITOR_ROMLESS.atsln

This file was deleted.

6 changes: 5 additions & 1 deletion SAM-BA_MONITOR_ROMLESS/device_config/device_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
#include "device_config_samc21j18a.h"
#define SAM_BA_INTERFACE SAM_BA_UART_ONLY
#define APP_START_ADDRESS DEFAULT_APP_START_ADDRESS
#elif defined (__SAMC21E18A__)
#include "device_config_samc21e18a.h"
#define SAM_BA_INTERFACE SAM_BA_UART_ONLY
#elif defined (__SAMDA1J16A__)
#include "device_config_samda1j16a.h"
#define SAM_BA_INTERFACE SAM_BA_BOTH_INTERFACES
Expand All @@ -73,7 +76,8 @@
#error Unknown part number... Define part number and create device_config_xx.h
#endif

#define SAM_BA_VERSION "v2.18"__DATE__" "__TIME__"\n\r"
//#define SAM_BA_VERSION "v2.18"__DATE__" "__TIME__"\n\r"
#define SAM_BA_VERSION "uB\n\r"

#ifndef SAM_BA_INTERFACE
#define SAM_BA_INTERFACE SAM_BA_UART_ONLY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#define COMPILER_PRAGMA(arg) _Pragma(#arg)
#define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
#define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4)
#elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */
#elif defined ( __GNUC__ ) || defined(__clang__) /* GCC CS3 2009q3-68 */
#include "sam.h"
#define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
#define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4)))
Expand All @@ -56,6 +56,7 @@

#define PINMUX_UNUSED 0xFFFFFFFF
#define BOOT_USART_MODULE SERCOM4
#define BOOT_USART_MODULE_IDX 4
#define BOOT_USART_MUX_SETTINGS (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(1))
#define BOOT_USART_PAD0 PINMUX_UNUSED
#define BOOT_USART_PAD1 PINMUX_UNUSED
Expand Down Expand Up @@ -97,6 +98,7 @@ static inline void clock_configuration_for_usb(void)

static inline void clock_configuration_for_boot_usart(void)
{
#if !defined(BOOT_USART_MODULE_IDX)
uint8_t inst = 0;
Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;

Expand All @@ -106,6 +108,9 @@ static inline void clock_configuration_for_boot_usart(void)
break;
}
}
#else
uint8_t inst = BOOT_USART_MODULE_IDX;
#endif

enable_sercom_digital_interface_clock(inst);

Expand Down
3 changes: 2 additions & 1 deletion SAM-BA_MONITOR_ROMLESS/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ static void check_start_application(void)
* Stay in SAM-BA if *(APP_START+0x4) == 0xFFFFFFFF
* Application erased condition
*/
if (app_start_address == 0xFFFFFFFF) {
// if (app_start_address == 0xFFFFFFFF) {
if (app_start_address>>18) { //bit safer: value in range 256k
/* Stay in Bootloader */
return;
}
Expand Down
108 changes: 69 additions & 39 deletions SAM-BA_MONITOR_ROMLESS/sam_ba/sam_ba_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
#include "cdc_enumerate.h"
#include "device_config.h"


#if (SAM_BA_INTERFACE == SAM_BA_UART_ONLY)
#define HND(x) uart_if.x
#else
#define HND(x) ptr_monitor_if->x
#endif

static void print_new_line(void);
const char RomBOOT_Version[] = SAM_BA_VERSION;
const char RomBOOT_Version[sizeof(SAM_BA_VERSION)-1] = SAM_BA_VERSION;

/* Provides one common interface to handle both USART and USB-CDC */
typedef struct
Expand Down Expand Up @@ -72,25 +79,26 @@ const t_monitor_if usbcdc_if =
t_monitor_if * ptr_monitor_if;

/* b_terminal_mode mode (ascii) or hex mode */
volatile bool b_terminal_mode = false;
//volatile bool b_terminal_mode = false;
static const bool b_terminal_mode = false;
volatile uint32_t sp;


static void print_new_line(void)
{
ptr_monitor_if->putdata("\n\r", 2);
HND(putdata)("\n\r", 2);
}

void init_sam_ba_monitor_interface(void)
{
#if SAM_BA_UART_INTERFACE_ENABLED
usart_open();
#endif

#if SAM_BA_USB_INTERFACE_ENABLED
clock_configuration_for_usb();
usb_init();
#endif
#endif
}

void process_sam_ba_monitor(void)
Expand All @@ -102,7 +110,10 @@ void process_sam_ba_monitor(void)
}
#endif

#if SAM_BA_UART_INTERFACE_ENABLED
#if (SAM_BA_INTERFACE == SAM_BA_UART_ONLY)
sam_ba_monitor_run();
for(;;);
#elif SAM_BA_UART_INTERFACE_ENABLED
if(uart_if.is_rx_ready() && (SHARP_CHARACTER == uart_if.get_c())) {
ptr_monitor_if = (t_monitor_if*) &uart_if;
sam_ba_monitor_run();
Expand Down Expand Up @@ -150,21 +161,20 @@ void sam_ba_putdata_term(uint8_t* data, uint32_t length)
buf[1] = 'x';
buf[length * 2 + 2] = '\n';
buf[length * 2 + 3] = '\r';
ptr_monitor_if->putdata(buf, length * 2 + 4);
HND(putdata)(buf, length * 2 + 4);
}
else
ptr_monitor_if->putdata(data, length);
HND(putdata)(data, length);
return;
}

__attribute__((__noreturn__))
void call_applet(uint32_t address)
{
uint32_t app_start_address;

cpu_irq_disable();

sp = __get_MSP();

/* Rebase the Stack Pointer */
__set_MSP(*(uint32_t *) address);

Expand All @@ -175,7 +185,8 @@ void call_applet(uint32_t address)
app_start_address = *(uint32_t *)(address + 4);

/* Jump to application Reset Handler in the application */
asm("bx %0"::"r"(app_start_address));
__asm("bx %0"::"r"(app_start_address));
__builtin_unreachable();
}

/**
Expand All @@ -184,25 +195,24 @@ void call_applet(uint32_t address)
void sam_ba_monitor_run(void)
{
uint32_t length;
uint32_t j, u8tmp, current_number, command;
uint32_t j, command, current_number, u8tmp;
uint8_t *ptr_data, *ptr, data[SIZEBUFMAX];

ptr_data = 0;
command = 'z';
j=0;

command = 0;

// Start waiting some cmd
while (1)
{
length = ptr_monitor_if->getdata(data, SIZEBUFMAX);
length = HND(getdata)(data, SIZEBUFMAX);
ptr = data;
for (uint32_t i = 0; i < length; i++)
{
if (*ptr != 0xff)
{
if (*ptr == '#')
{
if (b_terminal_mode)
/* if (b_terminal_mode)
{
print_new_line();
}
Expand Down Expand Up @@ -230,27 +240,31 @@ void sam_ba_monitor_run(void)
ptr--;
//Do we expect more data ?
if(j<current_number)
ptr_monitor_if->getdata_xmd(ptr_data, current_number-j);
__asm("nop");
HND(getdata_xmd)(ptr_data, current_number-j);
// __asm("nop");
}
*/
/*
else if (command == 'R')
{
ptr_monitor_if->putdata_xmd(ptr_data, current_number);
HND(putdata_xmd)(ptr_data, current_number);
}
else if (command == 'O')
{
*ptr_data = (char) current_number;
}
else if (command == 'H')
else
if (command == 'H')
{
*((uint16_t *) ptr_data) = (uint16_t) current_number;
}
else if (command == 'W')
else
*/ if (command == 'W')
{
*((int *) ptr_data) = current_number;
}
else if (command == 'o')
/* else if (command == 'o')
{
sam_ba_putdata_term(ptr_data, 1);
}
Expand All @@ -259,47 +273,47 @@ void sam_ba_monitor_run(void)
current_number = *((uint16_t *) ptr_data);
sam_ba_putdata_term((uint8_t*) &current_number, 2);
}
else if (command == 'w')
*/ else if (command == 'w')
{
current_number = *((uint32_t *) ptr_data);
sam_ba_putdata_term((uint8_t*) &current_number, 4);
}

else if (command == 'G')
{
call_applet(current_number);
//ptr_monitor_if->put_c(0x6);
/* Rebase the Stack Pointer */
__set_MSP(sp);
cpu_irq_enable();
__builtin_unreachable();
}
else if (command == 'T')
/* else if (command == 'T')
{
b_terminal_mode = 1;
print_new_line();
}
else if (command == 'N')
{
if (b_terminal_mode == 0)
{
print_new_line();
}
b_terminal_mode = 0;
//if (b_terminal_mode == 0)
//{
HND(putdata)((uint8_t *) RomBOOT_Version+2, 2);
// print_new_line();
//}
//b_terminal_mode = 0;
}
else if (command == 'V')
*/ else if (command == 'V')
{
ptr_monitor_if->putdata((uint8_t *) RomBOOT_Version, strlen(RomBOOT_Version));
HND(putdata)((uint8_t *) RomBOOT_Version, sizeof(RomBOOT_Version)); //strlen(RomBOOT_Version));
}

command = 'z';
command = 0;
current_number = 0;

if (b_terminal_mode)
{
ptr_monitor_if->putdata(">", 1);
HND(putdata)(">", 1);
}
}
else
{
/*
if (('0' <= *ptr) && (*ptr <= '9'))
{
current_number = (current_number << 4) | (*ptr - '0');
Expand All @@ -322,6 +336,22 @@ void sam_ba_monitor_run(void)
command = *ptr;
current_number = 0;
}
*/
if (*ptr == ',')
{
ptr_data = (uint8_t *) current_number;
current_number = 0;
}
else
{
int val = htoi(*ptr);
if (val < 0) {
command = *ptr;
current_number = 0;
}else{
current_number = (current_number << 4) | val;
}
}
}
ptr++;
}
Expand Down
32 changes: 32 additions & 0 deletions glue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdint.h>
#include <stdbool.h>
#include "glue.h"

extern uint32_t _estack;
extern uint32_t _srelocate, _erelocate, _etext;
extern uint32_t _sbss, _ebss;

extern uint32_t Image$$STACK$$Base;
extern uint32_t Image$$STACK$$Limit;

int main(void);

int htoi(char x) {
unsigned int v=x;
v-='0';
if (v<10) return v;
v|=0x20; //A->a
v-='a'-'0';
if (v<6) return v+10;
return -1;
}

__attribute__ ((section (".vectors"),used))
const struct {
uint32_t* SP;
void* Entry;
#if defined ( __CC_ARM )
}vectors = {&Image$$STACK$$Limit, main};
#else
}vectors = {&_estack, main};
#endif
1 change: 1 addition & 0 deletions glue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int htoi(char x);

0 comments on commit 14959e8

Please sign in to comment.