Skip to content

Commit

Permalink
Added all the PTP modes, both in uart and gpio mode. Mostly tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas authored and nicolas-rabault committed Aug 3, 2016
1 parent 0f85291 commit 6e6c37b
Show file tree
Hide file tree
Showing 12 changed files with 2,095 additions and 1,493 deletions.
137 changes: 115 additions & 22 deletions board_test/board_test/Debug/Makefile

Large diffs are not rendered by default.

2,719 changes: 1,431 additions & 1,288 deletions board_test/board_test/Debug/board_test.srec

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions board_test/board_test/Debug/makedep.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
# Automatically-generated file. Do not edit or delete the file
################################################################################

src\ASF\sam\drivers\adc\adc2.c

src\ASF\sam\drivers\matrix\matrix.c

src\ASF\sam\drivers\mem2mem\mem2mem.c

src\ASF\sam\drivers\pdc\pdc.c

src\ASF\sam\drivers\rstc\rstc.c

src\ASF\sam\drivers\spi\spi.c

src\libs\button\button.c

src\libs\interfaces\ptp.c
Expand Down
138 changes: 136 additions & 2 deletions board_test/board_test/board_test.cproj

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions board_test/board_test/src/ASF/sam/utils/syscalls/gcc/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,37 @@ extern caddr_t _sbrk(int incr)

extern int link(char *old, char *new)
{
(void)old;
(void)new;
return -1;
}

extern int _close(int file)
{
(void)file;
return -1;
}

extern int _fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
(void)file;
st->st_mode = S_IFCHR;

return 0;
}

extern int _isatty(int file)
{
(void)file;
return 1;
}

extern int _lseek(int file, int ptr, int dir)
{
return 0;
(void)file;
(void)ptr;
(void)dir;
return 0;
}

extern void _exit(int status)
Expand All @@ -128,6 +136,8 @@ extern void _exit(int status)

extern void _kill(int pid, int sig)
{
(void)pid;
(void)sig;
return;
}

Expand Down
50 changes: 25 additions & 25 deletions board_test/board_test/src/libs/button/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
#include "asf.h"


#define IRQ_PRIOR_PIO 0

static void Button1_Handler(uint32_t id, uint32_t mask)
{
if (PIN_PUSHBUTTON_1_ID == id && PIN_PUSHBUTTON_1_MASK == mask) {
// g_b_led0_active = !g_b_led0_active;
// if (!g_b_led0_active) {
ioport_set_pin_level(LED0_GPIO, IOPORT_PIN_LEVEL_HIGH);
// }
}
}


void configure_buttons(void)
{
pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID);
pio_set_debounce_filter(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, 10);

pio_handler_set(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_ID,
PIN_PUSHBUTTON_1_MASK, PIN_PUSHBUTTON_1_ATTR, Button1_Handler);
NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_1_ID);
pio_handler_set_priority(PIN_PUSHBUTTON_1_PIO,
(IRQn_Type) PIN_PUSHBUTTON_1_ID, IRQ_PRIOR_PIO);
pio_enable_interrupt(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK);
}
// #define IRQ_PRIOR_PIO 0
//
// static void Button1_Handler(uint32_t id, uint32_t mask)
// {
// if (PIN_PUSHBUTTON_1_ID == id && PIN_PUSHBUTTON_1_MASK == mask) {
// // g_b_led0_active = !g_b_led0_active;
// // if (!g_b_led0_active) {
// ioport_set_pin_level(LED0_GPIO, IOPORT_PIN_LEVEL_HIGH);
// // }
// }
// }
//
//
// void configure_buttons(void)
// {
// pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID);
// pio_set_debounce_filter(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, 10);
//
// pio_handler_set(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_ID,
// PIN_PUSHBUTTON_1_MASK, PIN_PUSHBUTTON_1_ATTR, Button1_Handler);
// NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_1_ID);
// pio_handler_set_priority(PIN_PUSHBUTTON_1_PIO,
// (IRQn_Type) PIN_PUSHBUTTON_1_ID, IRQ_PRIOR_PIO);
// pio_enable_interrupt(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK);
// }

203 changes: 152 additions & 51 deletions board_test/board_test/src/libs/interfaces/ptp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,111 +9,212 @@
#include "test_board.h"
#include "uart.h"

#include "time.h"

#define PTP_BAUDRATE 115200 // TODO get from somewhere else

ptp_t ptp_a_ = {
.mode = PTP_MODE_PULL_UP_STRONG,
.uart = PTP_A_UART,
.uart_port = PTP_A_UART_PORT,
.uart_pin_mask = PTP_A_UART_PIN_MASK,
.tx_mode = PTP_A_UART_TX_FLAGS,
.rx_mode = PTP_A_UART_RX_FLAGS,
.tx_pin = PTP_A_TX_PIN,
.rx_pin = PTP_A_RX_PIN,
.adc_channel = PTP_A_ADC_CHANNEL,
};
ptp_t ptp_b_ = {
.mode = PTP_MODE_PULL_UP_STRONG,
.uart = PTP_B_UART,
.uart_port = PTP_B_UART_PORT,
.uart_pin_mask = PTP_B_UART_PIN_MASK,
.tx_mode = PTP_B_UART_TX_FLAGS,
.rx_mode = PTP_B_UART_RX_FLAGS,
.tx_pin = PTP_B_TX_PIN,
.rx_pin = PTP_B_RX_PIN,
.adc_channel = PTP_B_ADC_CHANNEL,
};

//TODO see if there are glitches when going from one state to the other?
static void ptp_set_hal_mode(ptp_t* ptp, ptp_mode_e mode){
if (ptp->mode != mode){
switch (mode){
case PTP_RX:
case PTP_TX:
case PTP_RX_TX:
//TODO setup alternate function
ptp_t* ptp_a = &ptp_a_;
ptp_t* ptp_b = &ptp_b_;

static void ptp_set_hal_mode(ptp_t* ptp, ptp_hal_mode_e hal_mode ){
if ( ptp->hal_mode != hal_mode ){
switch(hal_mode){
case PTP_HAL_UART:
ioport_set_pin_mode(ptp->tx_pin, ptp->tx_mode);
ioport_set_pin_mode(ptp->rx_pin, ptp->rx_mode);
ioport_disable_port(ptp->uart_port, ptp->uart_pin_mask);
break;
case PTP_HI_Z:
case PTP_HIGH:
case PTP_LOW:
case PTP_PULL_UP_WEAK:
case PTP_PULL_UP_STRONG:
case PTP_PULL_DOWN_WEAK:
case PTP_PULL_DOWN_STRONG:
case PTP_HAL_GPIO:
usart_disable_rx(ptp->uart);
usart_disable_tx(ptp->uart);
//TODO get back in GPIO mode HI-Z
break;
}
ptp->hal_mode = hal_mode;
}
}
}


void ptp_set_mode(ptp_t* ptp, ptp_mode_e mode){
ptp_set_hal_mode(ptp, mode);

switch(mode){
case PTP_HI_Z:
break;
case PTP_RX:
case PTP_MODE_RX:
ptp_set_hal_mode(ptp, PTP_HAL_UART);
usart_disable_tx(ptp->uart);
usart_enable_rx(ptp->uart);
break;
case PTP_TX:

case PTP_MODE_TX:
ptp_set_hal_mode(ptp, PTP_HAL_UART);
usart_disable_rx(ptp->uart);
usart_enable_tx(ptp->uart);
break;
case PTP_RX_TX:

case PTP_MODE_RX_TX:
ptp_set_hal_mode(ptp, PTP_HAL_UART);
usart_enable_rx(ptp->uart);
usart_enable_tx(ptp->uart);
break;
case PTP_HIGH:
ioport_set_pin_level(ptp->driver_pin, true);

case PTP_MODE_HI_Z:
ioport_set_port_mode(ptp->uart_port, ptp->uart_pin_mask, 0);
ioport_set_port_dir(ptp->uart_port, ptp->uart_pin_mask, IOPORT_DIR_INPUT);
ptp_set_hal_mode(ptp, PTP_HAL_GPIO);
ioport_enable_port(ptp->uart_port, ptp->uart_pin_mask);
break;
case PTP_LOW:
ioport_set_pin_level(ptp->driver_pin, true);

case PTP_MODE_HIGH:
ioport_set_pin_level(ptp->tx_pin, true);
ioport_set_pin_dir(ptp->tx_pin, IOPORT_DIR_OUTPUT);
ptp_set_hal_mode(ptp, PTP_HAL_GPIO);
ioport_enable_port(ptp->uart_port, ptp->uart_pin_mask);
break;
case PTP_PULL_UP_WEAK:
ioport_set_pin_mode(ptp->driver_pin, IOPORT_MODE_PULLUP); // TOOD check that it's the right way to do it, we want input with pull-up

case PTP_MODE_LOW:
ioport_set_pin_level(ptp->tx_pin, false);
ioport_set_pin_dir(ptp->tx_pin, IOPORT_DIR_OUTPUT);
ptp_set_hal_mode(ptp, PTP_HAL_GPIO);
ioport_enable_port(ptp->uart_port, ptp->uart_pin_mask);
break;
case PTP_PULL_UP_STRONG:
//TODO // set pull up on all 3 pins

case PTP_MODE_PULL_UP_WEAK:
ioport_set_port_dir(ptp->uart_port, ptp->uart_pin_mask, IOPORT_DIR_INPUT);
ptp_set_hal_mode(ptp, PTP_HAL_GPIO);
ioport_set_pin_mode(ptp->tx_pin, IOPORT_MODE_PULLUP);
ioport_set_pin_mode(ptp->rx_pin, 0);
ioport_enable_port(ptp->uart_port, ptp->uart_pin_mask);
break;
case PTP_PULL_DOWN_WEAK:
ioport_set_pin_mode(ptp->driver_pin, IOPORT_MODE_PULLDOWN); // TOOD check that it's the right way to do it, we want input with pull-down

case PTP_MODE_PULL_UP_STRONG:
ioport_set_port_dir(ptp->uart_port, ptp->uart_pin_mask, IOPORT_DIR_INPUT);
ptp_set_hal_mode(ptp, PTP_HAL_GPIO);
ioport_set_pin_mode(ptp->tx_pin, IOPORT_MODE_PULLUP);
ioport_set_pin_mode(ptp->rx_pin, IOPORT_MODE_PULLUP); // TODO use ADC pull up too ?
ioport_enable_port(ptp->uart_port, ptp->uart_pin_mask);
break;
case PTP_PULL_DOWN_STRONG:
//TODO // set pull up down all 3 pins

case PTP_MODE_PULL_DOWN_WEAK:
ioport_set_port_dir(ptp->uart_port, ptp->uart_pin_mask, IOPORT_DIR_INPUT);
ptp_set_hal_mode(ptp, PTP_HAL_GPIO);
ioport_set_pin_mode(ptp->tx_pin, IOPORT_MODE_PULLDOWN);
ioport_set_pin_mode(ptp->rx_pin, 0);
ioport_enable_port(ptp->uart_port, ptp->uart_pin_mask);
break;

case PTP_MODE_PULL_DOWN_STRONG:
ioport_set_port_dir(ptp->uart_port, ptp->uart_pin_mask, IOPORT_DIR_INPUT);
ptp_set_hal_mode(ptp, PTP_HAL_GPIO);
ioport_set_pin_mode(ptp->tx_pin, IOPORT_MODE_PULLDOWN);
ioport_set_pin_mode(ptp->rx_pin, IOPORT_MODE_PULLDOWN); // TODO use ADC pull up too ?
ioport_enable_port(ptp->uart_port, ptp->uart_pin_mask);
break;
}
ptp->mode = mode;
}

void ptp_set_tx(usart_if uart, bool enable){

void ptp_set_tx(ptp_t* ptp, bool enable){
if (enable){
usart_enable_tx(uart);
usart_enable_tx(ptp->uart);
} else {
usart_disable_tx(uart);
usart_disable_tx(ptp->uart);
}
}
void ptp_set_rx(usart_if uart, bool enable){
void ptp_set_rx(ptp_t* ptp, bool enable){
if (enable){
usart_enable_rx(uart);
usart_enable_rx(ptp->uart);
} else {
usart_disable_rx(uart);
usart_disable_rx(ptp->uart);
}
}


uint32_t ptp_write(usart_if uart, uint32_t c){
while (!usart_is_tx_ready(uart));
return usart_write(uart, c);
uint32_t ptp_write(ptp_t* ptp, uint32_t c){
while (!usart_is_tx_ready(ptp->uart));
return usart_write(ptp->uart, c);
}

uint32_t ptp_read(usart_if uart, uint32_t* c){
return usart_read(uart, c);
uint32_t ptp_read(ptp_t* ptp, uint32_t* c){
return usart_read(ptp->uart, c);
}


bool ptp_adc_get(ptp_t* ptp, uint32_t* adc_value){
adc_channel_disable(ADC, ADC_CHANNEL_ALL);
adc_channel_enable(ADC, ptp->adc_channel);
adc_start_software_conversion(ADC);
delay_ms(1); //TODO wait for conversion end?
*adc_value = adc_channel_get_value(ADC, ptp->adc_channel);
return true; // TODO
}


bool ptp_hal_init(void){
ioport_set_port_mode(PTP_A_UART_PORT, PTP_A_UART_PINS, PTP_A_UART_PINS_FLAGS);
ioport_disable_port(PTP_A_UART_PORT, PTP_A_UART_PINS);
uart_init(PTP_A_UART, PTP_BAUDRATE);

// TODO ADC
// All pins are in pull-up at reset so turn that off, leave it on TX (driver_pin) only
ioport_set_pin_mode(PTP_A_ADC_PIN, 0);
ioport_set_pin_mode(PTP_A_RX_PIN, 0);

ioport_set_pin_mode(PTP_B_ADC_PIN, 0);
ioport_set_pin_mode(PTP_B_RX_PIN, 0);

ioport_set_port_mode(PTP_B_UART_PORT, PTP_B_UART_PINS, PTP_B_UART_PINS_FLAGS);
ioport_disable_port(PTP_B_UART_PORT, PTP_B_UART_PINS);
uart_init(PTP_A_UART, PTP_BAUDRATE);
uart_init(PTP_B_UART, PTP_BAUDRATE);

adc_enable();
#if SAMG55
adc_select_clock_source_mck(ADC);
#endif
struct adc_config config;
adc_get_config_defaults(&config);
adc_init(ADC, &config);
adc_start_calibration(ADC);



//TODO check ADC setup compared to this, which comes from the ADC ENHANCED RESOLUTION example
// adc_enable();
// #if SAMG55
// adc_select_clock_source_mck(ADC);
// #endif
//
// struct adc_config adc_cfg;
//
// adc_get_config_defaults(&adc_cfg);
//
// adc_init(ADC, &adc_cfg);
// adc_channel_enable(ADC, ADC_CHANNEL_1);
//
// adc_set_trigger(ADC, ADC_TRIG_TIO_CH_0);
//
// adc_set_callback(ADC, ADC_INTERRUPT_EOC_1,
// adc_end_conversion, 1);
//
// set_adc_resolution();
// adc_start_calibration(ADC);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions board_test/board_test/src/libs/interfaces/rs485.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ bool rs485_hal_init(void){
ioport_set_pin_dir(RS485_DE_PIN, IOPORT_DIR_OUTPUT);
rs485_set_tx(false);

ioport_set_port_mode(RS485_UART_PORT, RS485_UART_PINS, RS485_UART_PINS_FLAGS);
ioport_disable_port(RS485_UART_PORT, RS485_UART_PINS);
ioport_set_port_mode(RS485_UART_PORT, RS485_UART_PIN_MASK, RS485_UART_PINS_FLAGS);
ioport_disable_port(RS485_UART_PORT, RS485_UART_PIN_MASK);

uart_init(RS485_UART, RS485_BAUDRATE);

Expand Down
Loading

0 comments on commit 6e6c37b

Please sign in to comment.