Skip to content

Fix TT_M3HQ build problem #11237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 64 additions & 45 deletions targets/TARGET_TT/TARGET_TT_M3HQ/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
#include "pinmap.h"
#include "gpio_include.h"

#if DEVICE_I2C_ASYNCH
#define I2C_S(obj) (struct i2c_s *) (&((obj)->i2c))
#else
#define I2C_S(obj) (struct i2c_s *) (obj)
#endif

static const PinMap PinMap_I2C_SDA[] = {
{PC1, I2C_0, PIN_DATA(1, 2)},
{PA5, I2C_1, PIN_DATA(1, 2)},
Expand Down Expand Up @@ -56,7 +62,9 @@ static void i2c_start_bit(i2c_t *obj);
// Initialize the I2C peripheral. It sets the default parameters for I2C
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
MBED_ASSERT(obj != NULL);
struct i2c_s *obj_s = I2C_S(obj);
MBED_ASSERT(obj_s != NULL);

I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
I2CName i2c_name = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
Expand All @@ -66,21 +74,21 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
case I2C_0:
TSB_CG_FSYSENB_IPENB11 = ENABLE;
TSB_CG_FSYSENA_IPENA02 = ENABLE;
obj->i2c = TSB_I2C0;
obj_s->i2c = TSB_I2C0;
break;
case I2C_1:
TSB_CG_FSYSENB_IPENB12 = ENABLE;
TSB_CG_FSYSENA_IPENA00 = ENABLE;
obj->i2c = TSB_I2C1;
obj_s->i2c = TSB_I2C1;
break;
case I2C_2:
TSB_CG_FSYSENB_IPENB13 = ENABLE;
TSB_CG_FSYSENA_IPENA10 = ENABLE;
obj->i2c = TSB_I2C2;
obj_s->i2c = TSB_I2C2;
case I2C_3:
TSB_CG_FSYSENB_IPENB14 = ENABLE;
TSB_CG_FSYSENA_IPENA15 = ENABLE;
obj->i2c = TSB_I2C3;
obj_s->i2c = TSB_I2C3;
break;
default:
error("I2C is not available");
Expand All @@ -97,15 +105,16 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)

i2c_reset(obj);
i2c_frequency(obj, 100000);
obj->i2c->CR2 = (I2CxCR2_I2CM_ENABLE | I2CxCR2_TRX | I2CxCR2_PIN_CLEAR |
obj_s->i2c->CR2 = (I2CxCR2_I2CM_ENABLE | I2CxCR2_TRX | I2CxCR2_PIN_CLEAR |
I2CxCR2_INIT);
obj->i2c->OP = I2CxOP_INIT;
obj->i2c->IE = I2CxIE_CLEAR;
obj_s->i2c->OP = I2CxOP_INIT;
obj_s->i2c->IE = I2CxIE_CLEAR;
}

// Configure the I2C frequency
void i2c_frequency(i2c_t *obj, int hz)
{
struct i2c_s *obj_s = I2C_S(obj);
uint64_t sck, tmp_sck;
uint64_t prsck, tmp_prsck;
uint64_t fscl, tmp_fscl;
Expand Down Expand Up @@ -134,8 +143,8 @@ void i2c_frequency(i2c_t *obj, int hz)
clk.prsck = (tmp_prsck < 32) ? (uint32_t)(tmp_prsck - 1) : 0;
}

obj->i2c->CR1 = (I2CxCR1_ACK | clk.sck);
obj->i2c->PRS = (I2CxPRS_PRCK & clk.prsck);
obj_s->i2c->CR1 = (I2CxCR1_ACK | clk.sck);
obj_s->i2c->PRS = (I2CxPRS_PRCK & clk.prsck);
}

int i2c_start(i2c_t *obj)
Expand All @@ -146,10 +155,11 @@ int i2c_start(i2c_t *obj)

int i2c_stop(i2c_t *obj)
{
struct i2c_s *obj_s = I2C_S(obj);
uint32_t timeout = I2C_TIMEOUT;

obj->i2c->CR2 = I2CxCR2_STOP_CONDITION;
while ((obj->i2c->SR & I2CxSR_BB) == I2CxSR_BB) {
obj_s->i2c->CR2 = I2CxCR2_STOP_CONDITION;
while ((obj_s->i2c->SR & I2CxSR_BB) == I2CxSR_BB) {
if (timeout == 0)
break;
timeout--;
Expand All @@ -159,8 +169,9 @@ int i2c_stop(i2c_t *obj)

void i2c_reset(i2c_t *obj)
{
obj->i2c->CR2 = I2CxCR2_SWRES_10;
obj->i2c->CR2 = I2CxCR2_SWRES_01;
struct i2c_s *obj_s = I2C_S(obj);
obj_s->i2c->CR2 = I2CxCR2_SWRES_10;
obj_s->i2c->CR2 = I2CxCR2_SWRES_01;
}

int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
Expand Down Expand Up @@ -218,41 +229,43 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)

int i2c_byte_read(i2c_t *obj, int last)
{
struct i2c_s *obj_s = I2C_S(obj);
int32_t result;

obj->i2c->ST = I2CxST_CLEAR;
obj_s->i2c->ST = I2CxST_CLEAR;
if (last) {
obj->i2c->OP |= I2CxOP_MFACK;
obj_s->i2c->OP |= I2CxOP_MFACK;
} else {
obj->i2c->OP &= ~I2CxOP_MFACK;
obj_s->i2c->OP &= ~I2CxOP_MFACK;
}
obj->i2c->DBR = (0 & I2CxDBR_DB_MASK);
obj_s->i2c->DBR = (0 & I2CxDBR_DB_MASK);
if (wait_status(obj) < 0) {
result = -1;
} else {
result = (int32_t)(obj->i2c->DBR & I2CxDBR_DB_MASK);
result = (int32_t)(obj_s->i2c->DBR & I2CxDBR_DB_MASK);
}
return (result);
}

int i2c_byte_write(i2c_t *obj, int data)
{
struct i2c_s *obj_s = I2C_S(obj);
int32_t result;

obj->i2c->ST = I2CxST_CLEAR;
obj_s->i2c->ST = I2CxST_CLEAR;
if (start_flag == 1) {
obj->i2c->DBR = (data & I2CxDBR_DB_MASK);
obj_s->i2c->DBR = (data & I2CxDBR_DB_MASK);
i2c_start_bit(obj);
start_flag = 0;
} else {
obj->i2c->DBR = (data & I2CxDBR_DB_MASK);
obj_s->i2c->DBR = (data & I2CxDBR_DB_MASK);
}

if (wait_status(obj) < 0) {
return (-1);
}

if (!((obj->i2c->SR & I2CxSR_LRB) == I2CxSR_LRB)) {
if (!((obj_s->i2c->SR & I2CxSR_LRB) == I2CxSR_LRB)) {
result = 1;
} else {
result = 0;
Expand All @@ -262,21 +275,23 @@ int i2c_byte_write(i2c_t *obj, int data)

static void i2c_start_bit(i2c_t *obj) // Send START command
{
struct i2c_s *obj_s = I2C_S(obj);
uint32_t opreg;
opreg = obj->i2c->OP;
opreg = obj_s->i2c->OP;
opreg &= ~(I2CxOP_RSTA | I2CxOP_SREN);
if ((obj->i2c->SR & I2CxSR_BB)) {
if ((obj_s->i2c->SR & I2CxSR_BB)) {
opreg |= I2CxOP_SREN;
}
obj->i2c->OP = opreg;
obj->i2c->CR2 |= I2CxCR2_START_CONDITION;
obj_s->i2c->OP = opreg;
obj_s->i2c->CR2 |= I2CxCR2_START_CONDITION;
}

static int32_t wait_status(i2c_t *p_obj)
{
struct i2c_s *p_obj_s = I2C_S(p_obj);
volatile int32_t timeout;
timeout = I2C_TIMEOUT;
while (!((p_obj->i2c->ST & I2CxST_I2C) == I2CxST_I2C)) {
while (!((p_obj_s->i2c->ST & I2CxST_I2C) == I2CxST_I2C)) {
if ((timeout--) == 0) {
return (-1);
}
Expand All @@ -286,32 +301,34 @@ static int32_t wait_status(i2c_t *p_obj)

void i2c_slave_mode(i2c_t *obj, int enable_slave)
{
struct i2c_s *obj_s = I2C_S(obj);
if (enable_slave) {
obj->i2c->OP = I2CxOP_SLAVE_INIT;
obj->i2c->CR1 = (I2CxCR1_ACK | clk.sck);
obj->i2c->CR2 = (I2CxCR2_INIT | I2CxCR2_PIN_CLEAR);
obj->i2c->PRS = (I2CxPRS_PRCK & clk.prsck);
obj->i2c->AR = (obj->address & I2CAR_SA_MASK);
obj->i2c->IE = I2CxIE_INTI2C;
obj_s->i2c->OP = I2CxOP_SLAVE_INIT;
obj_s->i2c->CR1 = (I2CxCR1_ACK | clk.sck);
obj_s->i2c->CR2 = (I2CxCR2_INIT | I2CxCR2_PIN_CLEAR);
obj_s->i2c->PRS = (I2CxPRS_PRCK & clk.prsck);
obj_s->i2c->AR = (obj_s->address & I2CAR_SA_MASK);
obj_s->i2c->IE = I2CxIE_INTI2C;
} else {
i2c_reset(obj);
obj->i2c->CR2 = (I2CxCR2_I2CM_ENABLE | I2CxCR2_TRX | I2CxCR2_PIN_CLEAR |
obj_s->i2c->CR2 = (I2CxCR2_I2CM_ENABLE | I2CxCR2_TRX | I2CxCR2_PIN_CLEAR |
I2CxCR2_INIT);
obj->i2c->OP = I2CxOP_INIT;
obj->i2c->CR1 = (I2CxCR1_ACK | clk.sck);
obj->i2c->PRS = (I2CxPRS_PRCK & clk.prsck);
NVIC_DisableIRQ(obj->IRQn);
NVIC_ClearPendingIRQ(obj->IRQn);
obj->i2c->ST = I2CxST_CLEAR;
obj_s->i2c->OP = I2CxOP_INIT;
obj_s->i2c->CR1 = (I2CxCR1_ACK | clk.sck);
obj_s->i2c->PRS = (I2CxPRS_PRCK & clk.prsck);
NVIC_DisableIRQ(obj_s->IRQn);
NVIC_ClearPendingIRQ(obj_s->IRQn);
obj_s->i2c->ST = I2CxST_CLEAR;
}
}

int i2c_slave_receive(i2c_t *obj)
{
struct i2c_s *obj_s = I2C_S(obj);
int32_t result = I2C_NO_DATA;

if ((obj->i2c->ST & I2CxST_I2C) && (obj->i2c->OP & I2CxOP_SAST)) {
if ((obj->i2c->SR & I2CxSR_TRX) == I2CxSR_TRX) {
if ((obj_s->i2c->ST & I2CxST_I2C) && (obj_s->i2c->OP & I2CxOP_SAST)) {
if ((obj_s->i2c->SR & I2CxSR_TRX) == I2CxSR_TRX) {
result = I2C_READ_ADDRESSED;
} else {
result = I2C_WRITE_ADDRESSED;
Expand All @@ -322,11 +339,12 @@ int i2c_slave_receive(i2c_t *obj)

int i2c_slave_read(i2c_t *obj, char *data, int length)
{
struct i2c_s *obj_s = I2C_S(obj);
int32_t count = 0;

while (count < length) {
int32_t pdata = i2c_byte_read(obj, ((count < (length - 1)) ? 0 : 1));
if ((obj->i2c->SR & I2CxSR_TRX)) {
if ((obj_s->i2c->SR & I2CxSR_TRX)) {
return (count);
} else {
if (pdata < 0) {
Expand Down Expand Up @@ -354,7 +372,8 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length)

void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
{
obj->address = address & I2CAR_SA_MASK;
struct i2c_s *obj_s = I2C_S(obj);
obj_s->address = address & I2CAR_SA_MASK;
i2c_slave_mode(obj,1);
}

Expand Down
41 changes: 27 additions & 14 deletions targets/TARGET_TT/TARGET_TT_M3HQ/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
#include "pinmap.h"
#include "gpio_include.h"

#if DEVICE_SPI_ASYNCH
#define SPI_S(obj) (struct spi_s *) (&((obj)->spi))
#else
#define SPI_S(obj) (struct spi_s *) (obj)
#endif

static const PinMap PinMap_SPI_SCLK[] = {
{PM0, SPI_0, PIN_DATA(3, 1)},
{PB2, SPI_1, PIN_DATA(3, 1)},
Expand Down Expand Up @@ -69,6 +75,7 @@ static const PinMap PinMap_SPI_SSEL[] = {

void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
{
struct spi_s *obj_s = SPI_S(obj);
TSB_TSPI_TypeDef* spi;
// Check pin parameters
SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
Expand All @@ -78,9 +85,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);

obj->module = (SPIName)pinmap_merge(spi_data, spi_cntl);
spi = obj->spi;
switch ((int)obj->module) {
obj_s->module = (SPIName)pinmap_merge(spi_data, spi_cntl);
spi = obj_s->spi;
switch ((int)obj_s->module) {
case SPI_0:
TSB_CG_FSYSENA_IPENA11 = ENABLE;
TSB_CG_FSYSENB_IPENB00 = ENABLE;
Expand Down Expand Up @@ -110,7 +117,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
error("Cannot found SPI module corresponding with input pins.");
break;
}
obj->spi = spi;
obj_s->spi = spi;
// pin out the SPI pins
pinmap_pinout(mosi, PinMap_SPI_MOSI);
pinmap_pinout(miso, PinMap_SPI_MISO);
Expand Down Expand Up @@ -146,20 +153,22 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel

void spi_free(spi_t *obj)
{
struct spi_s *obj_s = SPI_S(obj);
TSB_TSPI_TypeDef* spi;

spi = obj->spi;
spi = obj_s->spi;
spi->CR0 |= TSPI_DISABLE;
spi->CR2 = TSPI_INT_ALL; // Disable all interrupt
}

void spi_format(spi_t *obj, int bits, int mode, int slave)
{
struct spi_s *obj_s = SPI_S(obj);
TSB_TSPI_TypeDef* spi;

obj->bits = bits;
spi = obj->spi;
obj->bits = bits;
obj_s->bits = bits;
spi = obj_s->spi;
obj_s->bits = bits;
spi->CR0 |= TSPI_DISABLE;

if (bits >= 8 || bits <= 32) {
Expand All @@ -174,10 +183,11 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)

void spi_frequency(spi_t *obj, int hz)
{
struct spi_s *obj_s = SPI_S(obj);
TSB_TSPI_TypeDef* spi;
int clk_div = 1;
uint32_t clocks = ((SystemCoreClock / 2) / hz);
obj->spi->CR0 |= TSPI_DISABLE;
obj_s->spi->CR0 |= TSPI_DISABLE;

while (clk_div < 10) {
if (clocks < 16) {
Expand All @@ -190,17 +200,18 @@ void spi_frequency(spi_t *obj, int hz)
if (clk_div == 0) {
clocks++;
}
spi = obj->spi;
spi = obj_s->spi;
spi->CR0 |= TSPI_DISABLE;
spi->BR = ((clk_div << 4) | clocks);
spi->CR0 |= TSPI_ENABLE;
}

int spi_master_write(spi_t *obj, int value)
{
struct spi_s *obj_s = SPI_S(obj);
TSB_TSPI_TypeDef* spi;
MBED_ASSERT(obj != NULL);
spi = obj->spi;
MBED_ASSERT(obj_s != NULL);
spi = obj_s->spi;
spi->CR3 |= TSPI_TX_BUFF_CLR_DONE; // FIFO Cear
// Check if the TSPI is already enabled
if((spi->CR0 & TSPI_ENABLE) != TSPI_ENABLE) {
Expand Down Expand Up @@ -258,10 +269,11 @@ int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,

int spi_busy(spi_t *obj)
{
struct spi_s *obj_s = SPI_S(obj);
TSB_TSPI_TypeDef* spi;
uint8_t result = 0;

spi = obj->spi;
spi = obj_s->spi;
if( (spi->SR & (1<<7)) || (spi->SR & (1<<23))) {
result = 1;
} else {
Expand All @@ -272,7 +284,8 @@ int spi_busy(spi_t *obj)

uint8_t spi_get_module(spi_t *obj)
{
return (uint8_t)(obj->module);
struct spi_s *obj_s = SPI_S(obj);
return (uint8_t)(obj_s->module);
}

const PinMap *spi_master_mosi_pinmap()
Expand Down
1 change: 0 additions & 1 deletion targets/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -8812,7 +8812,6 @@
"PWMOUT",
"RTC",
"SERIAL",
"SERIAL_FC",
"SLEEP",
"SPI",
"SPISLAVE",
Expand Down