Skip to content

Commit

Permalink
Stability upgrades
Browse files Browse the repository at this point in the history
Migrated to Microchip Studio (formerly Atmel Studio), I2C to 1 MHz, SPI uses DMA, Power Delivery code doesn't send Get_Source_Cap messages, screen update code optimized to bring response times in line with USB PD Spec, and other minor upgrades.
  • Loading branch information
ReclaimerLabs committed Nov 13, 2020
1 parent f463301 commit 1e312f1
Show file tree
Hide file tree
Showing 21 changed files with 2,151 additions and 214 deletions.
Binary file modified firmware/USB-C Explorer/.vs/USB-C Explorer/v14/.atsuo
Binary file not shown.
48 changes: 39 additions & 9 deletions firmware/USB-C Explorer/USB-C Explorer/USB-C Explorer.cproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
<option id="common.services.ioport" value="Add" config="" content-id="Atmel.ASF" />
<option id="common.services.usb.class.device" value="Add" config="cdc" content-id="Atmel.ASF" />
<option id="common2.components.display.ssd1306" value="Add" config="" content-id="Atmel.ASF" />
<option id="sam0.drivers.dma" value="Add" config="" content-id="Atmel.ASF" />
<option id="sam0.drivers.rtc" value="Add" config="count_callback" content-id="Atmel.ASF" />
<option id="sam0.drivers.sercom.i2c.master" value="Add" config="polled" content-id="Atmel.ASF" />
<option id="sam0.drivers.sercom.spi" value="Add" config="callback" content-id="Atmel.ASF" />
<option id="sam0.drivers.system" value="Add" config="" content-id="Atmel.ASF" />
<option id="sam0.drivers.system.clock" value="Add" config="" content-id="Atmel.ASF" />
<option id="sam0.drivers.tc" value="Add" config="callback" content-id="Atmel.ASF" />
<option id="qtlib.generic.tmx.qt.at91samd.device" value="Add" config="" content-id="f23f3c19-f15d-491d-bc53-a449fb4910a2" />
<option id="libsamd21_qtouch_gcc.a" value="Add" config="" content-id="f23f3c19-f15d-491d-bc53-a449fb4910a2" />
<option id="sam0.drivers.tc" value="Add" config="callback" content-id="Atmel.ASF" />
<option id="sam0.utils.linker_scripts" value="Add" config="" content-id="Atmel.ASF" />
</options>
<configurations>
Expand Down Expand Up @@ -223,11 +225,11 @@
<file path="src/ASF/sam0/drivers/events/events_hooks.c" framework="" version="3.35.1" source="sam0\drivers\events\events_hooks.c" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/events/events_common.h" framework="" version="3.35.1" source="sam0\drivers\events\events_common.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/events/events_hooks.h" framework="" version="3.35.1" source="sam0\drivers\events\events_hooks.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_count.h" framework="" version="3.35.1" source="sam0\drivers\rtc\rtc_count.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_count_interrupt.h" framework="" version="3.35.1" source="sam0\drivers\rtc\rtc_count_interrupt.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_tamper.h" framework="" version="3.35.1" source="sam0\drivers\rtc\rtc_tamper.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_sam_d_r_h/rtc_count.c" framework="" version="3.35.1" source="sam0\drivers\rtc\rtc_sam_d_r_h\rtc_count.c" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_sam_d_r_h/rtc_count_interrupt.c" framework="" version="3.35.1" source="sam0\drivers\rtc\rtc_sam_d_r_h\rtc_count_interrupt.c" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_count.h" framework="" version="3.42.0" source="sam0\drivers\rtc\rtc_count.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_count_interrupt.h" framework="" version="3.42.0" source="sam0\drivers\rtc\rtc_count_interrupt.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_tamper.h" framework="" version="3.42.0" source="sam0\drivers\rtc\rtc_tamper.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_sam_d_r_h/rtc_count.c" framework="" version="3.42.0" source="sam0\drivers\rtc\rtc_sam_d_r_h\rtc_count.c" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/rtc/rtc_sam_d_r_h/rtc_count_interrupt.c" framework="" version="3.42.0" source="sam0\drivers\rtc\rtc_sam_d_r_h\rtc_count_interrupt.c" changed="False" content-id="Atmel.ASF" />
<file path="src/QTouch/QDebugTransport.c" framework="" version="5.9" source="thirdparty\qtouch\devspecific\sam\samd\qdebug\QDebugTransport.c" changed="False" content-id="f23f3c19-f15d-491d-bc53-a449fb4910a2" />
<file path="src/QTouch/BitBangSPI_Master.c" framework="" version="5.9" source="thirdparty\qtouch\devspecific\sam\samd\qdebug\BitBangSPI_Master.c" changed="False" content-id="f23f3c19-f15d-491d-bc53-a449fb4910a2" />
<file path="src/QTouch/QDebug_samd.c" framework="" version="5.9" source="thirdparty\qtouch\devspecific\sam\samd\qdebug\QDebug_samd.c" changed="False" content-id="f23f3c19-f15d-491d-bc53-a449fb4910a2" />
Expand Down Expand Up @@ -295,11 +297,16 @@
<file path="src/ASF/sam0/drivers/tc/tc_interrupt.h" framework="" version="3.35.1" source="sam0\drivers\tc\tc_interrupt.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/tc/tc.h" framework="" version="3.35.1" source="sam0\drivers\tc\tc.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/tc/tc_sam_d_r_h/tc.c" framework="" version="3.35.1" source="sam0\drivers\tc\tc_sam_d_r_h\tc.c" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/dma/dma.c" framework="" version="3.42.0" source="sam0\drivers\dma\dma.c" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/dma/dma_crc.h" framework="" version="3.42.0" source="sam0\drivers\dma\dma_crc.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/dma/quick_start/qs_dma_basic.h" framework="" version="3.42.0" source="sam0\drivers\dma\quick_start\qs_dma_basic.h" changed="False" content-id="Atmel.ASF" />
<file path="src/ASF/sam0/drivers/dma/dma.h" framework="" version="3.42.0" source="sam0\drivers\dma\dma.h" changed="False" content-id="Atmel.ASF" />
<file path="src/Config/conf_dma.h" framework="" version="3.42.0" source="sam0\drivers\dma\module_config\conf_dma.h" changed="False" content-id="Atmel.ASF" />
</files>
<documentation help="http://asf.atmel.com/docs/3.30.1/common.applications.user_application.sam0_system.user_board.atsamd21e17a/html/index.html" />
<offline-documentation help="" />
<dependencies>
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.35.1" />
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.42.0" />
<content-extension eid="f23f3c19-f15d-491d-bc53-a449fb4910a2" uuidref="f23f3c19-f15d-491d-bc53-a449fb4910a2" version="5.9" />
</dependencies>
<project id="common.applications.user_application.sam0_system.user_board.atsamd21e17a" value="Add" config="" content-id="Atmel.ASF" />
Expand Down Expand Up @@ -404,9 +411,10 @@
<Value>../src/FUSB302</Value>
<Value>../src/USBC_PD</Value>
<Value>../src/USBC_TCPM</Value>
<Value>../src/ASF/sam0/drivers/dma</Value>
</ListValues>
</armgcc.compiler.directories.IncludePaths>
<armgcc.compiler.optimization.level>Optimize for size (-Os)</armgcc.compiler.optimization.level>
<armgcc.compiler.optimization.level>Optimize most (-O3)</armgcc.compiler.optimization.level>
<armgcc.compiler.optimization.OtherFlags>-fdata-sections</armgcc.compiler.optimization.OtherFlags>
<armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>True</armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>
<armgcc.compiler.warnings.AllWarnings>True</armgcc.compiler.warnings.AllWarnings>
Expand Down Expand Up @@ -483,6 +491,7 @@
<Value>../src/ASF/sam0/drivers/sercom/i2c/i2c_sam0</Value>
<Value>../src/ASF/sam0/drivers/tc</Value>
<Value>../src/ASF/sam0/drivers/tc/tc_sam_d_r_h</Value>
<Value>../src/ASF/sam0/drivers/dma</Value>
</ListValues>
</armgcc.assembler.general.IncludePaths>
<armgcc.preprocessingassembler.general.AssemblerFlags>-DARM_MATH_CM0PLUS=true -DBOARD=USER_BOARD -DSYSTICK_MODE -DEVENTS_INTERRUPT_HOOKS_MODE=true -DRTC_COUNT_ASYNC=true -DUSB_DEVICE_LPM_SUPPORT -DUDD_ENABLE -DEXTINT_CALLBACK_MODE=true -DSPI_CALLBACK_MODE=true -DI2C_MASTER_CALLBACK_MODE=false -DTC_ASYNC=true</armgcc.preprocessingassembler.general.AssemblerFlags>
Expand Down Expand Up @@ -536,6 +545,7 @@
<Value>../src/ASF/sam0/drivers/sercom/i2c/i2c_sam0</Value>
<Value>../src/ASF/sam0/drivers/tc</Value>
<Value>../src/ASF/sam0/drivers/tc/tc_sam_d_r_h</Value>
<Value>../src/ASF/sam0/drivers/dma</Value>
</ListValues>
</armgcc.preprocessingassembler.general.IncludePaths>
</ArmGcc>
Expand Down Expand Up @@ -620,6 +630,7 @@
<Value>../src/USBC_TCPM</Value>
<Value>../src/ASF/sam0/drivers/tc</Value>
<Value>../src/ASF/sam0/drivers/tc/tc_sam_d_r_h</Value>
<Value>../src/ASF/sam0/drivers/dma</Value>
</ListValues>
</armgcc.compiler.directories.IncludePaths>
<armgcc.compiler.optimization.level>Optimize debugging experience (-Og)</armgcc.compiler.optimization.level>
Expand Down Expand Up @@ -695,6 +706,7 @@
<Value>../src/ASF/sam0/drivers/sercom/i2c/i2c_sam0</Value>
<Value>../src/ASF/sam0/drivers/tc</Value>
<Value>../src/ASF/sam0/drivers/tc/tc_sam_d_r_h</Value>
<Value>../src/ASF/sam0/drivers/dma</Value>
</ListValues>
</armgcc.assembler.general.IncludePaths>
<armgcc.assembler.debugging.DebugLevel>Default (-g)</armgcc.assembler.debugging.DebugLevel>
Expand Down Expand Up @@ -749,6 +761,7 @@
<Value>../src/ASF/sam0/drivers/sercom/i2c/i2c_sam0</Value>
<Value>../src/ASF/sam0/drivers/tc</Value>
<Value>../src/ASF/sam0/drivers/tc/tc_sam_d_r_h</Value>
<Value>../src/ASF/sam0/drivers/dma</Value>
</ListValues>
</armgcc.preprocessingassembler.general.IncludePaths>
<armgcc.preprocessingassembler.debugging.DebugLevel>Default (-Wa,-g)</armgcc.preprocessingassembler.debugging.DebugLevel>
Expand All @@ -765,7 +778,7 @@
<SubType>compile</SubType>
</None>
<None Include="USB-C Explorer.qtdgn">
<SubType>none</SubType>
<SubType>compile</SubType>
<Generator>QTouchFileGenerator</Generator>
</None>
<None Include="src\ASF\sam0\utils\linker_scripts\samd21\gcc\samd21e17a_flash.ld">
Expand Down Expand Up @@ -857,6 +870,18 @@
<None Include="src\ASF\sam0\drivers\extint\extint.h">
<SubType>compile</SubType>
</None>
<Compile Include="src\ASF\sam0\drivers\dma\dma.c">
<SubType>compile</SubType>
</Compile>
<None Include="src\ASF\sam0\drivers\dma\dma_crc.h">
<SubType>compile</SubType>
</None>
<None Include="src\ASF\sam0\drivers\dma\quick_start\qs_dma_basic.h">
<SubType>compile</SubType>
</None>
<None Include="src\ASF\sam0\drivers\dma\dma.h">
<SubType>compile</SubType>
</None>
<Compile Include="src\ASF\sam0\drivers\extint\extint_callback.c">
<SubType>compile</SubType>
</Compile>
Expand Down Expand Up @@ -971,6 +996,9 @@
<None Include="src\config\conf_spi.h">
<SubType>compile</SubType>
</None>
<None Include="src\config\conf_dma.h">
<SubType>compile</SubType>
</None>
<Compile Include="src\FUSB302\FUSB302.c">
<SubType>compile</SubType>
</Compile>
Expand Down Expand Up @@ -1575,6 +1603,8 @@
<Folder Include="src\ASF\common\utils\interrupt\" />
<Folder Include="src\ASF\sam0\" />
<Folder Include="src\ASF\sam0\drivers\" />
<Folder Include="src\ASF\sam0\drivers\dma\" />
<Folder Include="src\ASF\sam0\drivers\dma\quick_start\" />
<Folder Include="src\ASF\sam0\drivers\events\" />
<Folder Include="src\ASF\sam0\drivers\events\events_sam_d_r_h\" />
<Folder Include="src\ASF\sam0\drivers\extint\" />
Expand Down
28 changes: 24 additions & 4 deletions firmware/USB-C Explorer/USB-C Explorer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
* Atmel Software Framework (ASF).
*/
#include <asf.h>
// This should be included in asf.h,
// but something went wrong moving to Microchip Studio
#include "touch_api_ptc.h"
#include "ugui.h"
#include "tcpm_driver.h"
#include "usb_pd_driver.h"
Expand All @@ -75,6 +78,9 @@ uint32_t device_rev = 0;
const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_COUNT] = {
{&i2c_master_instance, fusb302_I2C_SLAVE_ADDR, &fusb302_tcpm_drv},
};

int pd_count=0, pd_count_written=0;
uint32_t *pd_src_caps;
// USB-C Specific - TCPM end 1

volatile uint8_t display_screen, display_needs_update, display_change_requested;
Expand Down Expand Up @@ -231,8 +237,6 @@ int main(void)
tcpc_alert(0);
}

pd_run_state_machine(0);

if (display_change_requested)
{
if (display_screen >= (MAX_SCREENS - 1))
Expand All @@ -252,8 +256,24 @@ int main(void)
ssd1306_write_data_n(display_buffer[display_screen], DISP_MEM_SIZE);
display_needs_update = 0;
}

// This is set so as not to needlessly spam the I2C bus
delay_us(1000);

delay_ms(2);
// See the notes in pd_process_source_cap_callback()
if (pd_count_written < pd_count)
{
char str[256];
uint32_t ma, mv, pdo;

pd_extract_pdo_power(pd_src_caps[pd_count_written], &ma, &mv);
sprintf(str, "%5.2f V, %5.2f A", (float)mv/1000, (float)ma/1000);
UG_PutString(0, 8*(pd_count_written+2), str);
pd_count_written++;
display_needs_update = 1;
}

pd_run_state_machine(0);
}
}

Expand Down Expand Up @@ -362,7 +382,7 @@ void i2c_init(void)
struct i2c_master_config config_i2c_master;
i2c_master_get_config_defaults(&config_i2c_master);
config_i2c_master.buffer_timeout = 10000;
config_i2c_master.baud_rate = I2C_MASTER_BAUD_RATE_400KHZ;
config_i2c_master.baud_rate = I2C_MASTER_BAUD_RATE_1000KHZ;
config_i2c_master.pinmux_pad0 = I2C_SDA_PINMUX;
config_i2c_master.pinmux_pad1 = I2C_SCL_PINMUX;
i2c_master_init(&i2c_master_instance, I2C_MODULE, &config_i2c_master);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include "ssd1306.h"
#include <asf.h>

struct spi_module ssd1306_master;
struct spi_slave_inst ssd1306_slave;
struct dma_resource ssd1306_dma_resource_tx;
COMPILER_ALIGNED(16)
DmacDescriptor dma_descriptor_tx SECTION_DMAC_DESCRIPTOR;

static void ssd1306_write_data_callback(struct dma_resource* const resource)
{
spi_select_slave(&ssd1306_master, &ssd1306_slave, false);
}

/**
* \internal
Expand Down Expand Up @@ -75,6 +84,12 @@ static void ssd1306_interface_init(void)

spi_init(&ssd1306_master, SSD1306_SPI, &config);
spi_enable(&ssd1306_master);

struct dma_resource_config tx_config;
dma_get_config_defaults(&tx_config);
tx_config.peripheral_trigger = CONF_PERIPHERAL_TRIGGER_TX;
tx_config.trigger_action = DMA_TRIGGER_ACTION_BEAT;
dma_allocate(&ssd1306_dma_resource_tx, &tx_config);

struct port_config pin;
port_get_config_defaults(&pin);
Expand Down Expand Up @@ -185,9 +200,27 @@ void ssd1306_write_data(uint8_t data)
}

void ssd1306_write_data_n(uint8_t *data, uint16_t size)
{
{
spi_select_slave(&ssd1306_master, &ssd1306_slave, true);
port_pin_set_output_level(SSD1306_DC_PIN, true);
spi_write_buffer_wait(&ssd1306_master, data, size);
spi_select_slave(&ssd1306_master, &ssd1306_slave, false);

//spi_write_buffer_wait(&ssd1306_master, data, size);
//spi_write_buffer_job(&ssd1306_master, data, size);
//spi_select_slave(&ssd1306_master, &ssd1306_slave, false);

struct dma_descriptor_config tx_descriptor_config;
dma_descriptor_get_config_defaults(&tx_descriptor_config);
tx_descriptor_config.beat_size = DMA_BEAT_SIZE_BYTE;
tx_descriptor_config.dst_increment_enable = false;
tx_descriptor_config.block_transfer_count = size;
tx_descriptor_config.source_address = (uint32_t)data + (uint32_t)size;
tx_descriptor_config.destination_address = (uint32_t)(&ssd1306_master.hw->SPI.DATA.reg);
dma_descriptor_create(&dma_descriptor_tx, &tx_descriptor_config);

dma_add_descriptor(&ssd1306_dma_resource_tx, &dma_descriptor_tx);

dma_register_callback(&ssd1306_dma_resource_tx, ssd1306_write_data_callback, DMA_CALLBACK_TRANSFER_DONE);
dma_enable_callback(&ssd1306_dma_resource_tx, DMA_CALLBACK_TRANSFER_DONE);

dma_start_transfer_job(&ssd1306_dma_resource_tx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ void ssd1306_write_data(uint8_t data);

void ssd1306_write_data_n(uint8_t *data, uint16_t size);

//static void ssd1306_write_data_callback(struct dma_resource* const resource);

/**
* \brief Read data from the controller
*
Expand Down
Loading

0 comments on commit 1e312f1

Please sign in to comment.