|
| 1 | +#include "../inc/ek_hal_i2c.h" |
| 2 | +#include "../../utils/inc/ek_assert.h" |
| 3 | +#include "../../utils/inc/ek_export.h" |
| 4 | + |
| 5 | +#define EK_HAL_LOCK_ON(x) ((x)->lock = true) |
| 6 | +#define EK_HAL_LOCK_OFF(x) ((x)->lock = false) |
| 7 | +#define EK_HAL_LOCK_TEST(x) ((x)->lock == true) |
| 8 | + |
| 9 | +static void i2c1_init(void); |
| 10 | +static bool i2c1_send(uint16_t dev_addr, uint8_t *txdata, size_t size); |
| 11 | +static bool i2c1_recieve(uint16_t dev_addr, uint8_t *rxdata, size_t size); |
| 12 | +static bool |
| 13 | +i2c1_mem_write(uint16_t dev_addr, uint16_t mem_addr, ek_hal_i2c_mem_size_t mem_size, uint8_t *txdata, size_t size); |
| 14 | +static bool |
| 15 | +i2c1_mem_read(uint16_t dev_addr, uint16_t mem_addr, ek_hal_i2c_mem_size_t mem_size, uint8_t *rxdata, size_t size); |
| 16 | + |
| 17 | +ek_list_node_t ek_hal_i2c_head; |
| 18 | + |
| 19 | +ek_hal_i2c_t hal_drv_i2c1 = { |
| 20 | + .idx = 1, |
| 21 | + .speed_hz = 400000, |
| 22 | + |
| 23 | + .lock = false, |
| 24 | + |
| 25 | + .init = i2c1_init, |
| 26 | + .read = i2c1_recieve, |
| 27 | + .write = i2c1_send, |
| 28 | + |
| 29 | + .mem_read = i2c1_mem_read, |
| 30 | + .mem_write = i2c1_mem_write, |
| 31 | +}; |
| 32 | + |
| 33 | +static void i2c1_init(void) |
| 34 | +{ |
| 35 | + hal_drv_i2c1.lock = false; |
| 36 | +} |
| 37 | + |
| 38 | +static bool i2c1_send(uint16_t dev_addr, uint8_t *txdata, size_t size) |
| 39 | +{ |
| 40 | + if (EK_HAL_LOCK_TEST(&hal_drv_i2c1) == true) return false; |
| 41 | + |
| 42 | + EK_HAL_LOCK_ON(&hal_drv_i2c1); |
| 43 | + |
| 44 | + __UNUSED(dev_addr); |
| 45 | + __UNUSED(txdata); |
| 46 | + __UNUSED(size); |
| 47 | + // 具体的i2c底层 |
| 48 | + |
| 49 | + EK_HAL_LOCK_OFF(&hal_drv_i2c1); |
| 50 | + |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +static bool i2c1_recieve(uint16_t dev_addr, uint8_t *rxdata, size_t size) |
| 55 | +{ |
| 56 | + if (EK_HAL_LOCK_TEST(&hal_drv_i2c1) == true) return false; |
| 57 | + |
| 58 | + EK_HAL_LOCK_ON(&hal_drv_i2c1); |
| 59 | + |
| 60 | + __UNUSED(dev_addr); |
| 61 | + __UNUSED(rxdata); |
| 62 | + __UNUSED(size); |
| 63 | + // 具体的i2c底层 |
| 64 | + |
| 65 | + EK_HAL_LOCK_OFF(&hal_drv_i2c1); |
| 66 | + |
| 67 | + return true; |
| 68 | +} |
| 69 | + |
| 70 | +static bool |
| 71 | +i2c1_mem_write(uint16_t dev_addr, uint16_t mem_addr, ek_hal_i2c_mem_size_t mem_size, uint8_t *txdata, size_t size) |
| 72 | +{ |
| 73 | + if (EK_HAL_LOCK_TEST(&hal_drv_i2c1) == true) return false; |
| 74 | + |
| 75 | + // uint16_t msize = (mem_size == EK_HAL_I2C_MEM_8B) ? I2C_MEMADD_SIZE_8BIT : I2C_MEMADD_SIZE_16BIT; |
| 76 | + // msize 来判断从机设备长度 |
| 77 | + |
| 78 | + EK_HAL_LOCK_ON(&hal_drv_i2c1); |
| 79 | + |
| 80 | + __UNUSED(dev_addr); |
| 81 | + __UNUSED(mem_addr); |
| 82 | + __UNUSED(txdata); |
| 83 | + __UNUSED(size); |
| 84 | + // 具体的i2c底层 |
| 85 | + |
| 86 | + EK_HAL_LOCK_OFF(&hal_drv_i2c1); |
| 87 | + |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +static bool |
| 92 | +i2c1_mem_read(uint16_t dev_addr, uint16_t mem_addr, ek_hal_i2c_mem_size_t mem_size, uint8_t *rxdata, size_t size) |
| 93 | +{ |
| 94 | + if (EK_HAL_LOCK_TEST(&hal_drv_i2c1) == true) return false; |
| 95 | + |
| 96 | + // uint16_t msize = (mem_size == EK_HAL_I2C_MEM_8B) ? I2C_MEMADD_SIZE_8BIT : I2C_MEMADD_SIZE_16BIT; |
| 97 | + // msize 来判断从机设备长度 |
| 98 | + |
| 99 | + EK_HAL_LOCK_ON(&hal_drv_i2c1); |
| 100 | + |
| 101 | + __UNUSED(dev_addr); |
| 102 | + __UNUSED(mem_addr); |
| 103 | + __UNUSED(rxdata); |
| 104 | + __UNUSED(size); |
| 105 | + // 具体的i2c底层 |
| 106 | + |
| 107 | + EK_HAL_LOCK_OFF(&hal_drv_i2c1); |
| 108 | + |
| 109 | + return true; |
| 110 | +} |
| 111 | + |
| 112 | +void ek_hal_i2c_init(void) |
| 113 | +{ |
| 114 | + ek_list_init(&ek_hal_i2c_head); |
| 115 | + |
| 116 | + ek_list_add_tail(&ek_hal_i2c_head, &hal_drv_i2c1.node); |
| 117 | +} |
| 118 | + |
| 119 | +EK_EXPORT(ek_hal_i2c_init, 0); |
0 commit comments