From 877c4d54b70be2e3f6124f00667403dd21349aac Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sun, 21 Jan 2024 23:40:17 +0800 Subject: [PATCH] i2C is OK --- README.md | 10 +++++ app/app.uvoptx | 112 ++++++++++++++++++++++++++++++++---------------- app/app.uvprojx | 30 +++++++++++++ app/config/sdk_config.h | 8 ++-- app/main.c | 9 ++-- app/src/board.c | 27 +++++++++++- app/src/board.h | 6 ++- 7 files changed, 155 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 2a234bd..8bca8f0 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,16 @@ sdk\components\boards\pca10100.h AIN2 -> 光感 +I2C +#define TWI_SCL_M 28 //I2C SCL引脚 +#define TWI_SDA_M 30 //I2C SDA引脚 + +------------------------------------ + + +PIN4 PIN30/PIN31 PIN28/PIN29 PIN11/PIN20 + + ``` diff --git a/app/app.uvoptx b/app/app.uvoptx index 7919f87..15fd958 100644 --- a/app/app.uvoptx +++ b/app/app.uvoptx @@ -787,6 +787,42 @@ 0 0 + + 7 + 32 + 1 + 0 + 0 + 0 + ..\sdk\integration\nrfx\legacy\nrf_drv_twi.c + nrf_drv_twi.c + 0 + 0 + + + 7 + 33 + 1 + 0 + 0 + 0 + ..\sdk\modules\nrfx\drivers\src\nrfx_twi.c + nrfx_twi.c + 0 + 0 + + + 7 + 34 + 1 + 0 + 0 + 0 + ..\sdk\modules\nrfx\drivers\src\nrfx_twim.c + nrfx_twim.c + 0 + 0 + @@ -797,7 +833,7 @@ 0 8 - 32 + 35 1 0 0 @@ -809,7 +845,7 @@ 8 - 33 + 36 1 0 0 @@ -821,7 +857,7 @@ 8 - 34 + 37 1 0 0 @@ -833,7 +869,7 @@ 8 - 35 + 38 1 0 0 @@ -845,7 +881,7 @@ 8 - 36 + 39 1 0 0 @@ -857,7 +893,7 @@ 8 - 37 + 40 1 0 0 @@ -869,7 +905,7 @@ 8 - 38 + 41 1 0 0 @@ -881,7 +917,7 @@ 8 - 39 + 42 1 0 0 @@ -893,7 +929,7 @@ 8 - 40 + 43 1 0 0 @@ -905,7 +941,7 @@ 8 - 41 + 44 1 0 0 @@ -917,7 +953,7 @@ 8 - 42 + 45 1 0 0 @@ -929,7 +965,7 @@ 8 - 43 + 46 1 0 0 @@ -941,7 +977,7 @@ 8 - 44 + 47 1 0 0 @@ -953,7 +989,7 @@ 8 - 45 + 48 1 0 0 @@ -965,7 +1001,7 @@ 8 - 46 + 49 1 0 0 @@ -977,7 +1013,7 @@ 8 - 47 + 50 1 0 0 @@ -989,7 +1025,7 @@ 8 - 48 + 51 1 0 0 @@ -1001,7 +1037,7 @@ 8 - 49 + 52 1 0 0 @@ -1013,7 +1049,7 @@ 8 - 50 + 53 1 0 0 @@ -1025,7 +1061,7 @@ 8 - 51 + 54 1 0 0 @@ -1037,7 +1073,7 @@ 8 - 52 + 55 1 0 0 @@ -1049,7 +1085,7 @@ 8 - 53 + 56 1 0 0 @@ -1061,7 +1097,7 @@ 8 - 54 + 57 1 0 0 @@ -1073,7 +1109,7 @@ 8 - 55 + 58 1 0 0 @@ -1085,7 +1121,7 @@ 8 - 56 + 59 1 0 0 @@ -1105,7 +1141,7 @@ 0 9 - 57 + 60 1 0 0 @@ -1117,7 +1153,7 @@ 9 - 58 + 61 1 0 0 @@ -1129,7 +1165,7 @@ 9 - 59 + 62 1 0 0 @@ -1141,7 +1177,7 @@ 9 - 60 + 63 1 0 0 @@ -1153,7 +1189,7 @@ 9 - 61 + 64 1 0 0 @@ -1165,7 +1201,7 @@ 9 - 62 + 65 1 0 0 @@ -1185,7 +1221,7 @@ 0 10 - 63 + 66 1 0 0 @@ -1197,7 +1233,7 @@ 10 - 64 + 67 1 0 0 @@ -1209,7 +1245,7 @@ 10 - 65 + 68 1 0 0 @@ -1229,7 +1265,7 @@ 0 11 - 66 + 69 1 0 0 @@ -1241,7 +1277,7 @@ 11 - 67 + 70 1 0 0 @@ -1253,7 +1289,7 @@ 11 - 68 + 71 1 0 0 @@ -1273,7 +1309,7 @@ 0 12 - 69 + 72 1 0 0 diff --git a/app/app.uvprojx b/app/app.uvprojx index 8de1eb1..2136e33 100644 --- a/app/app.uvprojx +++ b/app/app.uvprojx @@ -1639,6 +1639,21 @@ 1 ..\sdk\integration\nrfx\legacy\nrf_drv_spi.c + + nrf_drv_twi.c + 1 + ..\sdk\integration\nrfx\legacy\nrf_drv_twi.c + + + nrfx_twi.c + 1 + ..\sdk\modules\nrfx\drivers\src\nrfx_twi.c + + + nrfx_twim.c + 1 + ..\sdk\modules\nrfx\drivers\src\nrfx_twim.c + @@ -5333,6 +5348,21 @@ 1 ..\sdk\integration\nrfx\legacy\nrf_drv_spi.c + + nrf_drv_twi.c + 1 + ..\sdk\integration\nrfx\legacy\nrf_drv_twi.c + + + nrfx_twi.c + 1 + ..\sdk\modules\nrfx\drivers\src\nrfx_twi.c + + + nrfx_twim.c + 1 + ..\sdk\modules\nrfx\drivers\src\nrfx_twim.c + diff --git a/app/config/sdk_config.h b/app/config/sdk_config.h index 6c2f440..ab5b0b8 100644 --- a/app/config/sdk_config.h +++ b/app/config/sdk_config.h @@ -4124,7 +4124,7 @@ // NRFX_TWIM_ENABLED - nrfx_twim - TWIM peripheral driver //========================================================== #ifndef NRFX_TWIM_ENABLED -#define NRFX_TWIM_ENABLED 0 +#define NRFX_TWIM_ENABLED 1 #endif // NRFX_TWIM0_ENABLED - Enable TWIM0 instance @@ -4363,7 +4363,7 @@ // NRFX_TWI_ENABLED - nrfx_twi - TWI peripheral driver //========================================================== #ifndef NRFX_TWI_ENABLED -#define NRFX_TWI_ENABLED 0 +#define NRFX_TWI_ENABLED 1 #endif // NRFX_TWI0_ENABLED - Enable TWI0 instance @@ -5880,7 +5880,7 @@ // TWI_ENABLED - nrf_drv_twi - TWI/TWIM peripheral driver - legacy layer //========================================================== #ifndef TWI_ENABLED -#define TWI_ENABLED 0 +#define TWI_ENABLED 1 #endif // TWI_DEFAULT_CONFIG_FREQUENCY - Frequency @@ -5940,7 +5940,7 @@ // TWI1_ENABLED - Enable TWI1 instance //========================================================== #ifndef TWI1_ENABLED -#define TWI1_ENABLED 0 +#define TWI1_ENABLED 1 #endif // TWI1_USE_EASY_DMA - Use EasyDMA (if present) diff --git a/app/main.c b/app/main.c index a534728..c0b11f9 100644 --- a/app/main.c +++ b/app/main.c @@ -73,8 +73,10 @@ static void test_tx_timer_cb(void* p_context) { // zdatachannel_data_send((uint8_t*)&data, &txlen); // ZLOGI("adc channel %d %d", adc_module_heart_elect_channel_read_val(), adc_module_battery_channel_read_val()); - ZLOGI("board_spi_transfer_test") - board_spi_transfer_test(); + ZLOGI("test_tx_timer_cb") + // board_spi_transfer_test(); + uint8_t data[] = {0xAA, 0xBB}; + board_i2c_write(0x3C, data, 2); } static void board_init() { @@ -82,7 +84,8 @@ static void board_init() { // adc_module_battery_channel_init(NRF_SAADC_INPUT_VDD); // adc_module_heart_elect_channel_init(NRF_SAADC_INPUT_AIN2); - board_spi_init(); + // board_spi_init(); + board_i2c_init(); } int main(void) { diff --git a/app/src/board.c b/app/src/board.c index 01c4c61..940461e 100644 --- a/app/src/board.c +++ b/app/src/board.c @@ -179,4 +179,29 @@ ret_code_t board_spi_transfer_test() { } void screen_init() {} -void screen_spi_translate_onebyte() {} \ No newline at end of file +void screen_spi_translate_onebyte() {} +/******************************************************************************* + * I2C * + *******************************************************************************/ +#define TWI_SCL_M 30 // I2C SCL引脚 +#define TWI_SDA_M 31 // I2C SDA引脚 +static const nrf_drv_twi_t m_twi_master = NRF_DRV_TWI_INSTANCE(1); + +void board_i2c_init() { + const nrf_drv_twi_config_t config = { + .scl = TWI_SCL_M, + .sda = TWI_SDA_M, + .frequency = NRF_DRV_TWI_FREQ_100K, + .interrupt_priority = APP_IRQ_PRIORITY_HIGH, + .clear_bus_init = false, + }; + + ZERROR_CHECK(nrf_drv_twi_init(&m_twi_master, &config, NULL, NULL)); + + nrf_drv_twi_enable(&m_twi_master); +} + +void board_i2c_write(uint8_t addr, uint8_t* data, uint8_t len) { + nrf_drv_twi_tx(&m_twi_master, addr, data, len, false); +} + diff --git a/app/src/board.h b/app/src/board.h index 26c9a64..47a7fce 100644 --- a/app/src/board.h +++ b/app/src/board.h @@ -2,6 +2,7 @@ #include #include "nrf_drv_saadc.h" +#include "nrf_drv_twi.h" #include "sys.h" /******************************************************************************* @@ -45,9 +46,12 @@ int16_t adc_module_heart_elect_channel_read_val(); * SPI * *******************************************************************************/ -void board_spi_init(); +void board_spi_init(); ret_code_t board_spi_transfer(uint8_t const* p_tx_buffer, uint8_t tx_buffer_length, uint8_t* p_rx_buffer, uint8_t rx_buffer_length); ret_code_t board_spi_transfer_test(); +void board_i2c_init(); +void board_i2c_write(uint8_t addr, uint8_t* data, uint8_t len); + void screen_init(); void screen_spi_translate_onebyte(); \ No newline at end of file