Skip to content
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

PSOC6: Update CSP to latest #11324

Merged
merged 4 commits into from
Aug 28, 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
6 changes: 3 additions & 3 deletions targets/TARGET_Cypress/TARGET_PSOC6/cy_analogout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ void analogout_free(dac_t *obj)

void analogout_write(dac_t *obj, float value)
{
MBED_ASSERT(value >= 0.0 && value <= 100.0f);
analogout_write_u16(obj, (uint16_t)(value * 0.01f * UINT16_MAX));
MBED_ASSERT(value >= 0.0 && value <= 1.0f);
analogout_write_u16(obj, (uint16_t)(value * UINT16_MAX));
}

void analogout_write_u16(dac_t *obj, uint16_t value)
Expand All @@ -51,7 +51,7 @@ void analogout_write_u16(dac_t *obj, uint16_t value)

float analogout_read(dac_t *obj)
{
return 100.0f / UINT16_MAX * analogout_read_u16(obj);
return analogout_read_u16(obj) / UINT16_MAX;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This almost always returns 0, as it's integer division. Should be

return analogout_read_u16(obj) * (1.0f / UINT64_MAX);

(Multiplication by reciprocal will be more efficient than actual FP division).

Same maths should be used in analogin_read.

}

uint16_t analogout_read_u16(dac_t *obj)
Expand Down
1 change: 0 additions & 1 deletion targets/TARGET_Cypress/TARGET_PSOC6/cy_crc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "mbed_assert.h"
#include "mbed_error.h"
#include "cyhal_crc.h"
#include "cyhal_crc_impl.h"

#if DEVICE_CRC

Expand Down
18 changes: 9 additions & 9 deletions targets/TARGET_Cypress/TARGET_PSOC6/cy_flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ int32_t flash_read(flash_t *obj, uint32_t address, uint8_t *data, uint32_t size)

int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
{
MBED_ASSERT(0 == (address % obj->info.page_size));
MBED_ASSERT(0 == (size % obj->info.page_size));
for (uint32_t offset = 0; offset < size; offset += obj->info.page_size) {
MBED_ASSERT(0 == (address % obj->info.blocks[0].page_size));
MBED_ASSERT(0 == (size % obj->info.blocks[0].page_size));
for (uint32_t offset = 0; offset < size; offset += obj->info.blocks[0].page_size) {
if (CY_RSLT_SUCCESS != cyhal_flash_program(&(obj->flash), address + offset, (const uint32_t *)(data + offset))) {
return -1;
}
Expand All @@ -62,30 +62,30 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,

uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
{
if (address < obj->info.start_address || address >= obj->info.start_address + obj->info.size) {
if (address < obj->info.blocks[0].start_address || address >= obj->info.blocks[0].start_address + obj->info.blocks[0].size) {
return MBED_FLASH_INVALID_SIZE;
}
return obj->info.sector_size;
return obj->info.blocks[0].sector_size;
}

uint32_t flash_get_page_size(const flash_t *obj)
{
return obj->info.page_size;
return obj->info.blocks[0].page_size;
}

uint32_t flash_get_start_address(const flash_t *obj)
{
return obj->info.start_address;
return obj->info.blocks[0].start_address;
}

uint32_t flash_get_size(const flash_t *obj)
{
return obj->info.size;
return obj->info.blocks[0].size;
}

uint8_t flash_get_erase_value(const flash_t *obj)
{
return obj->info.erase_value;
return obj->info.blocks[0].erase_value;
}

#ifdef __cplusplus
Expand Down
45 changes: 30 additions & 15 deletions targets/TARGET_Cypress/TARGET_PSOC6/cy_gpio_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@
extern "C" {
#endif

void apply_config(gpio_t *obj)
{
MBED_ASSERT(obj->pin != CYHAL_NC_PIN_VALUE);

cy_rslt_t rslt;
if (CY_RSLT_SUCCESS != (rslt = cyhal_gpio_configure(obj->pin, obj->direction, obj->drive_mode))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_GPIO, CY_RSLT_GET_CODE(rslt)), "cyhal_gpio_configure failed");
}

if (obj->drive_mode == PullUp) {
gpio_write(obj, 1);
} else if (obj->drive_mode == PullDown) {
gpio_write(obj, 0);
}
}

uint32_t gpio_set(PinName pin)
{
// unimplemented (appears to be unused)
Expand All @@ -40,36 +56,35 @@ void gpio_init(gpio_t *obj, PinName pin)
obj->pin = pin;
if (pin != CYHAL_NC_PIN_VALUE) {
cy_rslt_t rslt;
if (CY_RSLT_SUCCESS != (rslt = cyhal_gpio_init(obj->pin, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_ANALOG, false))) {
obj->direction = CYHAL_GPIO_DIR_INPUT;
obj->drive_mode = CYHAL_GPIO_DRIVE_NONE;
if (CY_RSLT_SUCCESS != (rslt = cyhal_gpio_init(obj->pin, obj->direction, obj->drive_mode, false))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_GPIO, CY_RSLT_GET_CODE(rslt)), "cyhal_gpio_init failed");
}
}
}

void gpio_mode(gpio_t *obj, PinMode mode)
{
MBED_ASSERT(obj->pin != CYHAL_NC_PIN_VALUE);
cy_rslt_t rslt;
if (CY_RSLT_SUCCESS != (rslt = cyhal_gpio_drivemode(obj->pin, mode))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_GPIO, CY_RSLT_GET_CODE(rslt)), "cyhal_gpio_mode failed");
}
if (mode == PullUp) {
gpio_write(obj, 1);
} else if (mode == PullDown) {
gpio_write(obj, 0);
}
obj->drive_mode = mode;
apply_config(obj);
}

void gpio_dir(gpio_t *obj, PinDirection direction)
{
if (direction == PIN_INPUT) {
cyhal_gpio_direction(obj->pin, CYHAL_GPIO_DIR_INPUT);
gpio_mode(obj, CYHAL_GPIO_DRIVE_ANALOG);
obj->direction = CYHAL_GPIO_DIR_INPUT;
if (obj->drive_mode == CYHAL_GPIO_DRIVE_STRONG) {
obj->drive_mode = CYHAL_GPIO_DRIVE_NONE;
}
} else if (direction == PIN_OUTPUT) {
// mbed reads from input buffer instead of DR even for output pins so always leave input buffer enabled
cyhal_gpio_direction(obj->pin, CYHAL_GPIO_DIR_BIDIRECTIONAL);
gpio_mode(obj, CYHAL_GPIO_DRIVE_STRONG);
obj->direction = CYHAL_GPIO_DIR_BIDIRECTIONAL;
if (obj->drive_mode == CYHAL_GPIO_DRIVE_NONE || obj->drive_mode == CYHAL_GPIO_DRIVE_ANALOG) {
obj->drive_mode = CYHAL_GPIO_DRIVE_STRONG;
}
}
apply_config(obj);
}

#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions targets/TARGET_Cypress/TARGET_PSOC6/cy_gpio_irq_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern "C" {
void cy_gpio_irq_handler_impl(void *handler_arg, cyhal_gpio_irq_event_t event)
{
gpio_irq_t *obj = (gpio_irq_t *)handler_arg;
cyhal_gpio_irq_event_t masked = event & obj->mask;
cyhal_gpio_irq_event_t masked = (cyhal_gpio_irq_event_t)(event & obj->mask);
void (*handler)(uint32_t, int) = (void (*)(uint32_t, int))obj->handler;
if (NULL != handler && CYHAL_GPIO_IRQ_NONE != masked) {
if (CYHAL_GPIO_IRQ_NONE != (masked & CYHAL_GPIO_IRQ_RISE)) {
Expand Down Expand Up @@ -80,7 +80,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
} else {
bits = CYHAL_GPIO_IRQ_NONE;
}
obj->mask = enable ? (obj->mask | bits) : (obj->mask & ~bits);
obj->mask = (cyhal_gpio_irq_event_t)(enable ? (obj->mask | bits) : (obj->mask & ~bits));
cyhal_gpio_irq_enable(obj->pin, obj->mask, true);
}

Expand Down
33 changes: 16 additions & 17 deletions targets/TARGET_Cypress/TARGET_PSOC6/cy_i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ static inline struct i2c_s *cy_get_i2c(i2c_t *obj)
extern "C" {
#endif

static uint32_t cy_i2c_convert_event(i2c_t *obj, cyhal_i2c_irq_event_t event)
static uint32_t cy_i2c_convert_event(i2c_t *obj, cyhal_i2c_event_t event)
{
if (CYHAL_I2C_IRQ_NONE != (event & (CYHAL_I2C_SLAVE_ERR_EVENT | CYHAL_I2C_MASTER_ERR_EVENT))) {
if (CYHAL_I2C_EVENT_NONE != (event & (CYHAL_I2C_SLAVE_ERR_EVENT | CYHAL_I2C_MASTER_ERR_EVENT))) {
event |= I2C_EVENT_ERROR;
}
if (CYHAL_I2C_IRQ_NONE != (event & (CYHAL_I2C_SLAVE_RD_CMPLT_EVENT | CYHAL_I2C_SLAVE_WR_CMPLT_EVENT | CYHAL_I2C_MASTER_RD_CMPLT_EVENT | CYHAL_I2C_MASTER_WR_CMPLT_EVENT)) && !i2c_active(obj)) {
if (CYHAL_I2C_EVENT_NONE != (event & (CYHAL_I2C_SLAVE_RD_CMPLT_EVENT | CYHAL_I2C_SLAVE_WR_CMPLT_EVENT | CYHAL_I2C_MASTER_RD_CMPLT_EVENT | CYHAL_I2C_MASTER_WR_CMPLT_EVENT)) && !i2c_active(obj)) {
event |= I2C_EVENT_TRANSFER_COMPLETE;
}
return event;
}

static void cy_i2c_event_handler(void *handler_arg, cyhal_i2c_irq_event_t event)
static void cy_i2c_event_handler(void *handler_arg, cyhal_i2c_event_t event)
{
struct i2c_s *i2c = cy_get_i2c((i2c_t *)handler_arg);
switch (event) {
Expand Down Expand Up @@ -91,7 +91,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
// MBED I2C driver currently does not support free, so we will allow I2C to be reallocated.
// TODO: once the the I2C driver properly supports free, this need to be fixed so that clocks and pins are no longer leaked.
cyhal_hwmgr_free(&(i2c->hal_i2c.resource));
cyhal_hwmgr_set_unconfigured(i2c->hal_i2c.resource.type, i2c->hal_i2c.resource.block_num, i2c->hal_i2c.resource.channel_num);
cyhal_resource_inst_t pin_rsc = cyhal_utils_get_gpio_resource(sda);
cyhal_hwmgr_free(&pin_rsc);
pin_rsc = cyhal_utils_get_gpio_resource(scl);
Expand All @@ -105,23 +104,23 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
i2c->cfg.address = 0;
i2c->cfg.frequencyhal_hz = 400000;
i2c->async_handler = NULL;
cyhal_i2c_register_irq(&(i2c->hal_i2c), &cy_i2c_event_handler, obj);
cyhal_i2c_irq_enable(&(i2c->hal_i2c), (cyhal_i2c_irq_event_t)(CYHAL_I2C_SLAVE_READ_EVENT | CYHAL_I2C_SLAVE_WRITE_EVENT | CYHAL_I2C_SLAVE_ERR_EVENT | CYHAL_I2C_SLAVE_RD_CMPLT_EVENT | CYHAL_I2C_SLAVE_WR_CMPLT_EVENT | CYHAL_I2C_MASTER_ERR_EVENT | CYHAL_I2C_MASTER_RD_CMPLT_EVENT | CYHAL_I2C_MASTER_WR_CMPLT_EVENT), true);
cyhal_i2c_register_callback(&(i2c->hal_i2c), &cy_i2c_event_handler, obj);
cyhal_i2c_enable_event(&(i2c->hal_i2c), (cyhal_i2c_event_t)(CYHAL_I2C_SLAVE_READ_EVENT | CYHAL_I2C_SLAVE_WRITE_EVENT | CYHAL_I2C_SLAVE_ERR_EVENT | CYHAL_I2C_SLAVE_RD_CMPLT_EVENT | CYHAL_I2C_SLAVE_WR_CMPLT_EVENT | CYHAL_I2C_MASTER_ERR_EVENT | CYHAL_I2C_MASTER_RD_CMPLT_EVENT | CYHAL_I2C_MASTER_WR_CMPLT_EVENT), CYHAL_ISR_PRIORITY_DEFAULT, true);
}

void i2c_frequency(i2c_t *obj, int hz)
{
struct i2c_s *i2c = cy_get_i2c(obj);
i2c->cfg.frequencyhal_hz = (uint32_t)hz;
if (CY_RSLT_SUCCESS != cyhal_i2c_set_config(&(i2c->hal_i2c), &(i2c->cfg))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_I2C, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_i2c_set_config");
if (CY_RSLT_SUCCESS != cyhal_i2c_configure(&(i2c->hal_i2c), &(i2c->cfg))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_I2C, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_i2c_configure");
}
}

int i2c_start(i2c_t *obj)
{
// Not supported; start/stop is generated by i2c_read/i2c_write
return -1;
// Nothing to do; start/stop is generated by i2c_read/i2c_write
return 0;
}

int i2c_stop(i2c_t *obj)
Expand All @@ -136,7 +135,7 @@ int i2c_stop(i2c_t *obj)
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
{
struct i2c_s *i2c = cy_get_i2c(obj);
if (CY_RSLT_SUCCESS != cyhal_i2c_master_read(&(i2c->hal_i2c), address >> 1, (uint8_t *)data, (uint16_t)length, CY_I2C_DEFAULT_TIMEOUT)) {
if (CY_RSLT_SUCCESS != cyhal_i2c_master_read(&(i2c->hal_i2c), address >> 1, (uint8_t *)data, (uint16_t)length, CY_I2C_DEFAULT_TIMEOUT, (bool)stop)) {
return (int)I2C_ERROR_NO_SLAVE;
}
return length;
Expand All @@ -145,7 +144,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
{
struct i2c_s *i2c = cy_get_i2c(obj);
if (CY_RSLT_SUCCESS != cyhal_i2c_master_write(&(i2c->hal_i2c), address >> 1, (const uint8_t *)data, (uint16_t)length, CY_I2C_DEFAULT_TIMEOUT)) {
if (CY_RSLT_SUCCESS != cyhal_i2c_master_write(&(i2c->hal_i2c), address >> 1, (const uint8_t *)data, (uint16_t)length, CY_I2C_DEFAULT_TIMEOUT, (bool)stop)) {
return (int)I2C_ERROR_NO_SLAVE;
}
// NOTE: HAL does not report how many bytes were actually sent in case of early NAK
Expand Down Expand Up @@ -210,8 +209,8 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave)
{
struct i2c_s *i2c = cy_get_i2c(obj);
i2c->cfg.is_slave = (0 != enable_slave);
if (CY_RSLT_SUCCESS != cyhal_i2c_set_config(&(i2c->hal_i2c), &(i2c->cfg))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_I2C, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_i2c_set_config");
if (CY_RSLT_SUCCESS != cyhal_i2c_configure(&(i2c->hal_i2c), &(i2c->cfg))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_I2C, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_i2c_configure");
}
}

Expand Down Expand Up @@ -242,8 +241,8 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
{
struct i2c_s *i2c = cy_get_i2c(obj);
i2c->cfg.address = address;
if (CY_RSLT_SUCCESS != cyhal_i2c_set_config(&(i2c->hal_i2c), &(i2c->cfg))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_I2C, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_i2c_set_config");
if (CY_RSLT_SUCCESS != cyhal_i2c_configure(&(i2c->hal_i2c), &(i2c->cfg))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_I2C, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_i2c_configure");
}
Cy_SCB_I2C_SlaveSetAddressMask(i2c->hal_i2c.base, (uint8_t)mask);
}
Expand Down
14 changes: 8 additions & 6 deletions targets/TARGET_Cypress/TARGET_PSOC6/cy_lp_ticker_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C" {
static cyhal_lptimer_t cy_lptimer0;
static bool cy_lptimer_initialized = false;

static void cy_lp_ticker_handler(MBED_UNUSED void *unused1, MBED_UNUSED cyhal_lptimer_irq_event_t unused2)
static void cy_lp_ticker_handler(MBED_UNUSED void *unused1, MBED_UNUSED cyhal_lptimer_event_t unused2)
{
lp_ticker_irq_handler();
}
Expand All @@ -46,7 +46,7 @@ void lp_ticker_init(void)
cy_lptimer_initialized = true;
}
lp_ticker_disable_interrupt();
cyhal_lptimer_register_irq(&cy_lptimer0, &cy_lp_ticker_handler, NULL);
cyhal_lptimer_register_callback(&cy_lptimer0, &cy_lp_ticker_handler, NULL);
}

void lp_ticker_free(void)
Expand All @@ -62,15 +62,17 @@ uint32_t lp_ticker_read(void)

void lp_ticker_set_interrupt(timestamp_t timestamp)
{
if (CY_RSLT_SUCCESS != cyhal_lptimer_set_match(&cy_lptimer0, timestamp)) {
uint32_t delay;
delay = (uint32_t)timestamp - cyhal_lptimer_read(&cy_lptimer0);

if (CY_RSLT_SUCCESS != cyhal_lptimer_set_match(&cy_lptimer0, delay)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_lptimer_set_time");
}
cyhal_lptimer_irq_enable(&cy_lptimer0, CYHAL_LPTIMER_COMPARE_MATCH, true);
}

void lp_ticker_disable_interrupt(void)
{
cyhal_lptimer_irq_enable(&cy_lptimer0, CYHAL_LPTIMER_COMPARE_MATCH, false);
cyhal_lptimer_enable_event(&cy_lptimer0, CYHAL_LPTIMER_COMPARE_MATCH, CYHAL_ISR_PRIORITY_DEFAULT, false);
}

void lp_ticker_clear_interrupt(void)
Expand All @@ -87,7 +89,7 @@ const ticker_info_t *lp_ticker_get_info(void)
{
static const ticker_info_t info = {
/* .frequency = */ CY_CFG_SYSCLK_CLKLF_FREQ_HZ,
/* .bits = */ 16
/* .bits = */ 32
};
return &info;
}
Expand Down
10 changes: 5 additions & 5 deletions targets/TARGET_Cypress/TARGET_PSOC6/cy_pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void pwmout_free(pwmout_t *obj)
void pwmout_write(pwmout_t *obj, float percent)
{
MBED_ASSERT(percent >= 0.0f && percent <= 1.0f);
pwmout_pulsewidth_us(obj, percent * obj->period_us);
pwmout_pulsewidth_us(obj, (int)(percent * obj->period_us));
}

float pwmout_read(pwmout_t *obj)
Expand All @@ -66,8 +66,8 @@ void pwmout_period_ms(pwmout_t *obj, int ms)
void pwmout_period_us(pwmout_t *obj, int us)
{
obj->period_us = (uint32_t)us;
if (CY_RSLT_SUCCESS != cyhal_pwm_period(&(obj->hal_pwm), obj->period_us, obj->width_us)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_period");
if (CY_RSLT_SUCCESS != cyhal_pwm_set_period(&(obj->hal_pwm), obj->period_us, obj->width_us)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_set_period");
}
if (CY_RSLT_SUCCESS != cyhal_pwm_start(&(obj->hal_pwm))) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_start");
Expand All @@ -87,8 +87,8 @@ void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
{
obj->width_us = (uint32_t)us;
if (CY_RSLT_SUCCESS != cyhal_pwm_period(&(obj->hal_pwm), obj->period_us, obj->width_us)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_period");
if (CY_RSLT_SUCCESS != cyhal_pwm_set_period(&(obj->hal_pwm), obj->period_us, obj->width_us)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_set_period");
}
}

Expand Down
Loading