diff options
Diffstat (limited to 'drivers/arm/i2c_master.c')
| -rw-r--r-- | drivers/arm/i2c_master.c | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/drivers/arm/i2c_master.c b/drivers/arm/i2c_master.c index 0e5edcc38..7369398cc 100644 --- a/drivers/arm/i2c_master.c +++ b/drivers/arm/i2c_master.c | |||
| @@ -42,6 +42,18 @@ static const I2CConfig i2cconfig = { | |||
| 42 | 0 | 42 | 0 |
| 43 | }; | 43 | }; |
| 44 | 44 | ||
| 45 | static i2c_status_t chibios_to_qmk(const msg_t* status) { | ||
| 46 | switch (*status) { | ||
| 47 | case I2C_NO_ERROR: | ||
| 48 | return I2C_STATUS_SUCCESS; | ||
| 49 | case I2C_TIMEOUT: | ||
| 50 | return I2C_STATUS_TIMEOUT; | ||
| 51 | // I2C_BUS_ERROR, I2C_ARBITRATION_LOST, I2C_ACK_FAILURE, I2C_OVERRUN, I2C_PEC_ERROR, I2C_SMB_ALERT | ||
| 52 | default: | ||
| 53 | return I2C_STATUS_ERROR; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 45 | __attribute__ ((weak)) | 57 | __attribute__ ((weak)) |
| 46 | void i2c_init(void) | 58 | void i2c_init(void) |
| 47 | { | 59 | { |
| @@ -57,29 +69,30 @@ void i2c_init(void) | |||
| 57 | //i2cInit(); //This is invoked by halInit() so no need to redo it. | 69 | //i2cInit(); //This is invoked by halInit() so no need to redo it. |
| 58 | } | 70 | } |
| 59 | 71 | ||
| 60 | // This is usually not needed | 72 | i2c_status_t i2c_start(uint8_t address) |
| 61 | uint8_t i2c_start(uint8_t address) | ||
| 62 | { | 73 | { |
| 63 | i2c_address = address; | 74 | i2c_address = address; |
| 64 | i2cStart(&I2C_DRIVER, &i2cconfig); | 75 | i2cStart(&I2C_DRIVER, &i2cconfig); |
| 65 | return 0; | 76 | return I2C_STATUS_SUCCESS; |
| 66 | } | 77 | } |
| 67 | 78 | ||
| 68 | uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) | 79 | i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) |
| 69 | { | 80 | { |
| 70 | i2c_address = address; | 81 | i2c_address = address; |
| 71 | i2cStart(&I2C_DRIVER, &i2cconfig); | 82 | i2cStart(&I2C_DRIVER, &i2cconfig); |
| 72 | return i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, 0, 0, MS2ST(timeout)); | 83 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, 0, 0, MS2ST(timeout)); |
| 84 | return chibios_to_qmk(&status); | ||
| 73 | } | 85 | } |
| 74 | 86 | ||
| 75 | uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) | 87 | i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) |
| 76 | { | 88 | { |
| 77 | i2c_address = address; | 89 | i2c_address = address; |
| 78 | i2cStart(&I2C_DRIVER, &i2cconfig); | 90 | i2cStart(&I2C_DRIVER, &i2cconfig); |
| 79 | return i2cMasterReceiveTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, MS2ST(timeout)); | 91 | msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, MS2ST(timeout)); |
| 92 | return chibios_to_qmk(&status); | ||
| 80 | } | 93 | } |
| 81 | 94 | ||
| 82 | uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) | 95 | i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) |
| 83 | { | 96 | { |
| 84 | i2c_address = devaddr; | 97 | i2c_address = devaddr; |
| 85 | i2cStart(&I2C_DRIVER, &i2cconfig); | 98 | i2cStart(&I2C_DRIVER, &i2cconfig); |
| @@ -91,18 +104,19 @@ uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t l | |||
| 91 | } | 104 | } |
| 92 | complete_packet[0] = regaddr; | 105 | complete_packet[0] = regaddr; |
| 93 | 106 | ||
| 94 | return i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 1, 0, 0, MS2ST(timeout)); | 107 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 1, 0, 0, MS2ST(timeout)); |
| 108 | return chibios_to_qmk(&status); | ||
| 95 | } | 109 | } |
| 96 | 110 | ||
| 97 | uint8_t i2c_readReg(uint8_t devaddr, uint8_t* regaddr, uint8_t* data, uint16_t length, uint16_t timeout) | 111 | i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t* regaddr, uint8_t* data, uint16_t length, uint16_t timeout) |
| 98 | { | 112 | { |
| 99 | i2c_address = devaddr; | 113 | i2c_address = devaddr; |
| 100 | i2cStart(&I2C_DRIVER, &i2cconfig); | 114 | i2cStart(&I2C_DRIVER, &i2cconfig); |
| 101 | return i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), regaddr, 1, data, length, MS2ST(timeout)); | 115 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), regaddr, 1, data, length, MS2ST(timeout)); |
| 116 | return chibios_to_qmk(&status); | ||
| 102 | } | 117 | } |
| 103 | 118 | ||
| 104 | uint8_t i2c_stop(void) | 119 | void i2c_stop(void) |
| 105 | { | 120 | { |
| 106 | i2cStop(&I2C_DRIVER); | 121 | i2cStop(&I2C_DRIVER); |
| 107 | return 0; | ||
| 108 | } | 122 | } |
