1- #include <hardware/i2c.h>
1+ #include "hardware/i2c.h"
2+ #include "pico/stdlib.h"
23/*
34 * Copyright (c) 2018, Sensirion AG
45 * All rights reserved.
3435#include "sensirion_config.h"
3536#include "sensirion_i2c_hal.h"
3637
37- /*
38- * INSTRUCTIONS
39- * ============
40- *
41- * Implement all functions where they are marked as IMPLEMENT.
42- * Follow the function specification in the comments.
43- */
38+ #define I2C_PORT i2c0
39+ #define PIN_I2C_SDA 4
40+ #define PIN_I2C_SCL 5
4441
4542/**
4643 * Select the current i2c bus by index.
@@ -64,14 +61,26 @@ int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx) {
6461 * communication.
6562 */
6663void sensirion_i2c_hal_init (void ) {
67- /* TODO:IMPLEMENT */
64+ stdio_init_all ();
65+
66+ // I2C Initialisation. Using it at 400Khz.
67+ i2c_init (I2C_PORT , 400 * 1000 );
68+
69+ gpio_set_function (PIN_I2C_SDA , GPIO_FUNC_I2C );
70+ gpio_set_function (PIN_I2C_SCL , GPIO_FUNC_I2C );
71+ gpio_pull_up (PIN_I2C_SDA );
72+ gpio_pull_up (PIN_I2C_SCL );
6873}
6974
7075/**
7176 * Release all resources initialized by sensirion_i2c_hal_init().
7277 */
7378void sensirion_i2c_hal_free (void ) {
74- /* TODO:IMPLEMENT or leave empty if no resources need to be freed */
79+ i2c_deinit (I2C_PORT );
80+ gpio_set_function (PIN_I2C_SDA , GPIO_FUNC_NULL );
81+ gpio_set_function (PIN_I2C_SCL , GPIO_FUNC_NULL );
82+ gpio_disable_pulls (PIN_I2C_SDA );
83+ gpio_disable_pulls (PIN_I2C_SCL );
7584}
7685
7786/**
@@ -84,9 +93,10 @@ void sensirion_i2c_hal_free(void) {
8493 * @param count number of bytes to read from I2C and store in the buffer
8594 * @returns 0 on success, error code otherwise
8695 */
87- int8_t sensirion_i2c_hal_read (uint8_t address , uint8_t * data , uint16_t count ) {
96+ int8_t sensirion_i2c_hal_read (uint8_t address , uint8_t * data , uint8_t count ) {
8897 int status = i2c_read_blocking (i2c_default , address , data , count , false);
89- if (status == 0 )
98+
99+ if (status <= 0 )
90100 return 1 ;
91101 else
92102 return 0 ;
@@ -104,11 +114,11 @@ int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {
104114 * @returns 0 on success, error code otherwise
105115 */
106116int8_t sensirion_i2c_hal_write (uint8_t address , const uint8_t * data ,
107- uint16_t count ) {
117+ uint8_t count ) {
108118 // I2C Default is used (I2C0).
109119 int status = i2c_write_blocking (i2c_default , address , data , count , true);
110120
111- if (status = = 0 )
121+ if (status < = 0 )
112122 return 1 ;
113123 else
114124 return 0 ;
0 commit comments