|
| 1 | +#include "atca_hal.h" |
| 2 | +#include "hal_cc2650_i2c.h" |
| 3 | +#include "board-i2c.h" |
| 4 | +#include "rtimer.h" |
| 5 | +#include "ti-lib.h" |
| 6 | +#include <string.h> |
| 7 | +#include <stdio.h> |
| 8 | + |
| 9 | +/** This implementation uses the primitives offered by Contiki. |
| 10 | + * In this way, sharing the code between the two implementations |
| 11 | + * is possible to reduce the final memory footprint. |
| 12 | + */ |
| 13 | + |
| 14 | +/** \brief discover i2c buses available for this hardware |
| 15 | + * this maintains a list of logical to physical bus mappings freeing the application |
| 16 | + * of the a-priori knowledge |
| 17 | + * \param[in] i2c_buses - an array of logical bus numbers |
| 18 | + * \param[in] max_buses - maximum number of buses the app wants to attempt to discover |
| 19 | + */ |
| 20 | +ATCA_STATUS hal_i2c_discover_buses( int i2c_buses[], int max_buses ) |
| 21 | +{ |
| 22 | + return ATCA_UNIMPLEMENTED; |
| 23 | +} |
| 24 | + |
| 25 | +/** \brief discover any CryptoAuth devices on a given logical bus number |
| 26 | + * \param[in] busNum logical bus number on which to look for CryptoAuth devices |
| 27 | + * \param[out] cfg pointer to head of an array of interface config structures which get filled in by this method |
| 28 | + * \param[out] found number of devices found on this bus |
| 29 | + */ |
| 30 | +ATCA_STATUS hal_i2c_discover_devices( int busNum, ATCAIfaceCfg cfg[], int *found ) |
| 31 | +{ |
| 32 | + return ATCA_UNIMPLEMENTED; |
| 33 | +} |
| 34 | + |
| 35 | +/** \brief HAL implementation of I2C init |
| 36 | + * |
| 37 | + * this implementation assumes I2C peripheral has been enabled by user. It only initialize an |
| 38 | + * I2C interface using given config. |
| 39 | + * |
| 40 | + * \param[in] hal pointer to HAL specific data that is maintained by this HAL |
| 41 | + * \param[in] cfg pointer to HAL specific configuration data that is used to initialize this HAL |
| 42 | + * \return ATCA_STATUS |
| 43 | + */ |
| 44 | +ATCA_STATUS hal_i2c_init( void* hal, ATCAIfaceCfg* cfg ) |
| 45 | +{ |
| 46 | + board_i2c_wakeup(); |
| 47 | + return ATCA_SUCCESS; |
| 48 | +} |
| 49 | + |
| 50 | +/** \brief HAL implementation of I2C post init |
| 51 | + * \param[in] iface instance |
| 52 | + * \return ATCA_STATUS |
| 53 | + */ |
| 54 | +ATCA_STATUS hal_i2c_post_init( ATCAIface iface ) |
| 55 | +{ |
| 56 | + return ATCA_SUCCESS; |
| 57 | +} |
| 58 | + |
| 59 | +/** \brief HAL implementation of I2C send over ASF |
| 60 | + * \param[in] iface instance |
| 61 | + * \param[in] txdata pointer to space to bytes to send |
| 62 | + * \param[in] txlength number of bytes to send |
| 63 | + * \return ATCA_STATUS |
| 64 | + */ |
| 65 | +ATCA_STATUS hal_i2c_send( ATCAIface iface, uint8_t* txdata, int txlength) |
| 66 | +{ |
| 67 | + const ATCAIfaceCfg *cfg = atgetifacecfg(iface); |
| 68 | + txdata[0] = 0x03; // insert the Word Address Value, Command token |
| 69 | + txlength++; // account for word address value byte. |
| 70 | + board_i2c_select(DEFAULT_I2C_INTERFACE, cfg->atcai2c.slave_address >> 1); |
| 71 | + bool success = board_i2c_write(txdata, txlength); |
| 72 | + board_i2c_deselect(); |
| 73 | + return success? ATCA_SUCCESS: ATCA_COMM_FAIL; |
| 74 | +} |
| 75 | + |
| 76 | +/** \brief HAL implementation of I2C receive function for ASF I2C |
| 77 | + * \param[in] iface instance |
| 78 | + * \param[in] rxdata pointer to space to receive the data |
| 79 | + * \param[in] rxlength ptr to expected number of receive bytes to request |
| 80 | + * \return ATCA_STATUS |
| 81 | + */ |
| 82 | +ATCA_STATUS hal_i2c_receive( ATCAIface iface, uint8_t *rxdata, uint16_t *rxlength ) |
| 83 | +{ |
| 84 | + const ATCAIfaceCfg *cfg = atgetifacecfg(iface); |
| 85 | + board_i2c_select(DEFAULT_I2C_INTERFACE, cfg->atcai2c.slave_address >> 1); |
| 86 | + bool success = board_i2c_read(rxdata, *rxlength); |
| 87 | + board_i2c_deselect(); |
| 88 | + return success? ATCA_SUCCESS: ATCA_COMM_FAIL; |
| 89 | +} |
| 90 | + |
| 91 | +ATCA_STATUS hal_i2c_wake( ATCAIface iface) { |
| 92 | + ATCAIfaceCfg *cfg = atgetifacecfg(iface); |
| 93 | + int retries = cfg->rx_retries; |
| 94 | + bool status = false; |
| 95 | + uint8_t data[16], expected[4] = { 0x04, 0x11, 0x33, 0x43 }; |
| 96 | + |
| 97 | + /* Send the wake up sequence using the GPIO interface for tWLO */ |
| 98 | + ti_lib_i2c_master_disable(I2C0_BASE); |
| 99 | + ti_lib_ioc_pin_type_gpio_output(BOARD_IOID_SDA); |
| 100 | + ti_lib_ioc_pin_type_gpio_output(BOARD_IOID_SCL); |
| 101 | + ti_lib_gpio_clear_dio(BOARD_IOID_SDA); |
| 102 | + ti_lib_gpio_clear_dio(BOARD_IOID_SCL); |
| 103 | + atca_delay_us(200); |
| 104 | + |
| 105 | + /* Restore the value of the SDA and SCL pins and wait tWHI */ |
| 106 | + board_i2c_wakeup(); |
| 107 | + ti_lib_i2c_master_disable(I2C0_BASE); |
| 108 | + ti_lib_ioc_io_port_pull_set(BOARD_IOID_SDA, IOC_NO_IOPULL); |
| 109 | + ti_lib_ioc_io_port_pull_set(BOARD_IOID_SCL, IOC_NO_IOPULL); |
| 110 | + ti_lib_ioc_pin_type_i2c(I2C0_BASE, BOARD_IOID_SDA, BOARD_IOID_SCL); |
| 111 | + ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SDA_HP); |
| 112 | + ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_SCL_HP); |
| 113 | + ti_lib_i2c_master_init_exp_clk(I2C0_BASE, ti_lib_sys_ctrl_clock_get(),true); |
| 114 | + atca_delay_us(cfg->wake_delay); |
| 115 | + |
| 116 | + /* Try to read the wake up value from the device */ |
| 117 | + board_i2c_select(DEFAULT_I2C_INTERFACE, cfg->atcai2c.slave_address >> 1); |
| 118 | + while (retries-- > 0 && status != true){ |
| 119 | + status = board_i2c_read(data, 4); |
| 120 | + } |
| 121 | + board_i2c_deselect(); |
| 122 | + |
| 123 | + if (status != true) { |
| 124 | + printf("The read was not successfull\n"); |
| 125 | + return ATCA_TOO_MANY_COMM_RETRIES; |
| 126 | + } |
| 127 | + |
| 128 | + if (memcmp(data, expected, 4) == 0) { |
| 129 | + printf("Wake up successfull\n"); |
| 130 | + return ATCA_SUCCESS; |
| 131 | + } |
| 132 | + printf("The received data was not correct\n"); |
| 133 | + return ATCA_COMM_FAIL; |
| 134 | +} |
| 135 | + |
| 136 | +static ATCA_STATUS send_single(ATCAIface iface, uint8_t single) { |
| 137 | + const ATCAIfaceCfg *cfg = atgetifacecfg(iface); |
| 138 | + board_i2c_select(DEFAULT_I2C_INTERFACE, cfg->atcai2c.slave_address >> 1); |
| 139 | + bool success = board_i2c_write_single(single); |
| 140 | + board_i2c_deselect(); |
| 141 | + return success? ATCA_SUCCESS: ATCA_COMM_FAIL; |
| 142 | +} |
| 143 | + |
| 144 | +/** \brief idle CryptoAuth device using I2C bus |
| 145 | + * \param[in] iface interface to logical device to idle |
| 146 | + */ |
| 147 | +ATCA_STATUS hal_i2c_idle( ATCAIface iface ) |
| 148 | +{ |
| 149 | + printf("send idle\n"); |
| 150 | + return send_single(iface, 0x02); |
| 151 | +} |
| 152 | + |
| 153 | +/** \brief sleep CryptoAuth device using I2C bus |
| 154 | + * \param[in] iface interface to logical device to sleep |
| 155 | + */ |
| 156 | +ATCA_STATUS hal_i2c_sleep( ATCAIface iface ) |
| 157 | +{ |
| 158 | + printf("send sleep\n"); |
| 159 | + return send_single(iface, 0x01); |
| 160 | +} |
| 161 | + |
| 162 | +/** \brief manages reference count on given bus and releases resource if no more refences exist |
| 163 | + * \param[in] hal_data - opaque pointer to hal data structure - known only to the HAL implementation |
| 164 | + */ |
| 165 | +ATCA_STATUS hal_i2c_release( void *hal_data ) |
| 166 | +{ |
| 167 | + board_i2c_shutdown(); |
| 168 | + return ATCA_SUCCESS; |
| 169 | +} |
0 commit comments