diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 126341d..4bcbfa6 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,5 +1,6 @@ eclipse.preferences.version=1 encoding//APP/peripheral.c=UTF-8 +encoding//APP/peripheral_main.c=UTF-8 encoding//BSP/inc/bsp_valve.h=UTF-8 encoding//BSP/src/bsp_valve.c=UTF-8 encoding//common/letter-shell/extensions/log/log.h=UTF-8 diff --git a/.vscode/settings.json b/.vscode/settings.json index dc0807a..c9c71b8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,7 @@ "shell_port.h": "c", "atomic": "cpp", "cstddef": "cpp", - "type_traits": "cpp" + "type_traits": "cpp", + "shell.h": "c" } } diff --git a/APP/peripheral.c b/APP/peripheral.c index d4282b7..e1e7378 100644 --- a/APP/peripheral.c +++ b/APP/peripheral.c @@ -52,7 +52,7 @@ #define SBP_PHY_UPDATE_DELAY 2400 // What is the advertising interval when device is discoverable (units of 625us, 80=50ms) -#define DEFAULT_ADVERTISING_INTERVAL (160 * 1) +#define DEFAULT_ADVERTISING_INTERVAL (160 * 10) // Limited discoverable mode advertises for 30.72s, and then stops // General discoverable mode advertises indefinitely @@ -226,7 +226,7 @@ static uint16_t peripheralMTU = ATT_MTU_SIZE; */ static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg); static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent); -static void performPeriodicTask(void); +static void UploadPeriodicTask(void); static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len); static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval, uint16_t connSlaveLatency, uint16_t connTimeout); @@ -432,7 +432,7 @@ uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events) tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD); } // Perform periodic application task - performPeriodicTask(); + UploadPeriodicTask(); return (events ^ SBP_PERIODIC_EVT); } @@ -738,7 +738,7 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven } /********************************************************************* - * @fn performPeriodicTask + * @fn UploadPeriodicTask * * @brief Perform a periodic application task. This function gets * called every five seconds as a result of the SBP_PERIODIC_EVT @@ -751,7 +751,7 @@ static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEven * * @return none */ -static void performPeriodicTask(void) +static void UploadPeriodicTask(void) { // static uint8_t counter = 0; // uint8_t notiData[SIMPLEPROFILE_CHAR4_LEN] = {0x11, 0x22, 0x33, 0x44, 0}; @@ -760,9 +760,9 @@ static void performPeriodicTask(void) // peripheralChar4Notify(notiData, 5); // counter++; TsRawFrameData RawData; - static uint8_t humi; - humi = 0; - BSP_VALVE_Generate_Data(&RawData, 30, 25, humi++); + static uint8_t humi = 0; + humi++; + BSP_VALVE_Generate_Data(&RawData, 30, 25, humi); if (humi > 99) { humi = 0; @@ -801,7 +801,6 @@ static void peripheralChar4Notify(uint8_t *pValue, uint16_t len) } -uint8_t newValue[SIMPLEPROFILE_CHAR3_LEN]; /********************************************************************* * @fn simpleProfileChangeCB @@ -836,6 +835,7 @@ static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len case SIMPLEPROFILE_CHAR3: { + uint8_t newValue[SIMPLEPROFILE_CHAR3_LEN]; tmos_memset(newValue, 0, sizeof(newValue)); tmos_memcpy(newValue, pValue, len); logDebug("CHAR3 Start"); diff --git a/APP/peripheral_main.c b/APP/peripheral_main.c index 27e6695..ea0ec91 100644 --- a/APP/peripheral_main.c +++ b/APP/peripheral_main.c @@ -26,12 +26,15 @@ #include "log.h" #include "bsp_uart.h" +#include "bsp_i2c.h" + #undef LOG_ENABLE #define LOG_ENABLE 1 #undef LOG_TAG #define LOG_TAG "main" +// static uint8_t main_task_id = INVALID_TASK_ID; /********************************************************************* * GLOBAL TYPEDEFS @@ -56,7 +59,7 @@ __attribute__((noinline)) void Main_Circulation() { TMOS_SystemProcess(); BSP_UART1_TxLoop(); - if (GPIOB_ReadPortPin(GPIO_Pin_15) == 0) + if (GPIOB_ReadPortPin(GPIO_Pin_7) == 0) { // 12V_EN BOOST_EN; @@ -97,7 +100,6 @@ __attribute__((noinline)) void Main_Circulation() DelayMs(1000); logDebug("LED_ALL_OFF_DEINIT\n"); - logDebug("Delay 6s\n"); // EMV_CHARGE @@ -123,6 +125,20 @@ __attribute__((noinline)) void Main_Circulation() } } +/********************************************************************* + * @fn BLE_AdvertiseEventCB + * + * @brief Callback from advertise over + * + * @param None + * + * @return none + */ +void BLE_AdvertiseEventCB(uint32_t timeUs) +{ + logDebug("BLE_AdvertiseEventCB"); +} + /********************************************************************* * @fn main * @@ -137,7 +153,6 @@ int main(void) #endif SetSysClock(CLK_SOURCE_PLL_60MHz); - #if (defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE) GPIOA_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PD); GPIOB_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PD); @@ -168,9 +183,23 @@ int main(void) BSP_ADC_Init(); CH59x_BLEInit(); logDebug("%s\n", VER_LIB); + uint8_t MacAddr[6]; + GetMACAddress(MacAddr); + logDebug("MacAddr: %02X:%02X:%02X:%02X:%02X:%02X", MacAddr[0], MacAddr[1], MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]); HAL_Init(); GAPRole_PeripheralInit(); Peripheral_Init(); + + LL_AdvertiseEventRegister(BLE_AdvertiseEventCB); + + DelayMs(100); + GXHTC3C_Init(); + while (1) + { + DelayMs(100); + } + + Main_Circulation(); } diff --git a/BSP/inc/bsp_beep_led_emv.h b/BSP/inc/bsp_beep_led_emv.h index dd30e43..dd39ece 100644 --- a/BSP/inc/bsp_beep_led_emv.h +++ b/BSP/inc/bsp_beep_led_emv.h @@ -2,7 +2,7 @@ * @Author : stark1898y 1658608470@qq.com * @Date : 2024-12-09 09:50:56 * @LastEditors : stark1898y 1658608470@qq.com - * @LastEditTime : 2024-12-09 11:27:01 + * @LastEditTime : 2024-12-13 15:51:09 * @FilePath : \BLE_TYQ_CH592F\BSP\inc\bsp_beep_led_emv.h * @Description : * @@ -14,19 +14,19 @@ #include "CH59x_common.h" -/**********************BOOST************************ */ +/**********************BOOST 12V_EN************************ */ #define BOOST_EN \ do \ { \ - GPIOB_SetBits(GPIO_Pin_14); \ - GPIOB_ModeCfg(GPIO_Pin_14, GPIO_ModeOut_PP_5mA); \ + GPIOB_SetBits(GPIO_Pin_4); \ + GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeOut_PP_5mA); \ } while (0); #define BOOST_OFF_DEINIT \ do \ { \ - GPIOB_ResetBits(GPIO_Pin_14); \ - GPIOB_ModeCfg(GPIO_Pin_14, GPIO_ModeIN_PD); \ + GPIOB_ResetBits(GPIO_Pin_4); \ + GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PD); \ } while (0); /***********************BEEP************************ */ diff --git a/BSP/inc/bsp_i2c.h b/BSP/inc/bsp_i2c.h new file mode 100644 index 0000000..5634cc4 --- /dev/null +++ b/BSP/inc/bsp_i2c.h @@ -0,0 +1,130 @@ +#ifndef __BSP_I2C_H__ +#define __BSP_I2C_H__ + +#include +#include +#include + +#include "CONFIG.h" + +#define GXHTC3C_ADDR 0x70 +// #define GXHTC3C_7BIT_ADDR 0x38 // (GXHTC3C_ADDR >> 1) + +// 供电电压VDD从0上升上电电压VPOR,芯片会进入空闲状态。然后应该通过发送命令让芯片进入休眠状态以降低芯片功耗 +#define GXHTC3C_CMD_SLEEP 0xB098 +// 当芯片处于休眠状态时,如果要进行其它的命令操作,需要发送唤醒命令 +#define GXHTC3C_CMD_WAKEUP 0x3517 + +#define GXHTC3C_CMD_NORMAL_CLK_STRE_ON_TEMP_FRONT 0x7CA2 +#define GXHTC3C_CMD_NORMAL_CLK_STRE_ON_HUMI_FRONT 0x5C24 + +#define GXHTC3C_CMD_NORMAL_CLK_STRE_OFF_TEMP_FRONT 0x7866 +#define GXHTC3C_CMD_NORMAL_CLK_STRE_OFF_HUMI_FRONT 0x58E0 + +#define GXHTC3C_CMD_LOW_CLK_STRE_ON_TEMP_FRONT 0x6458 +#define GXHTC3C_CMD_LOW_CLK_STRE_ON_HUMI_FRONT 0x44DE + +#define GXHTC3C_CMD_LOW_CLK_STRE_OFF_TEMP_FRONT 0x609C +#define GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT 0x401A + +#define GXHTC3C_CMD_SOFT_REST 0x805D + +// 读传感器序列号 +#define GXHTC3C_CMD_READ_ID 0xEFC8 + +#define I2C_BUFFER_LENGTH 32 +#define I2C_READ 1 +#define I2C_WRITE 0 + +typedef enum { + I2C_READY, + I2C_MRX, + I2C_MTX, + I2C_SRX, + I2C_STX, +}i2c_state_t; + +typedef enum { + I2C_NO_MEM = 1, + I2C_STATE, + I2C_MT_NACK, + I2C_ARB_LOST, + I2C_BUS_ERROR, + I2C_OVR, + I2C_PECERR, + I2C_TIMEOUT, + I2C_SMBALERT, +}i2c_error_t; + +/** + * @brief User callback function on I2C slave transmitting. + * + * @param data Pointer to user data to transmit. + * + * @param len Pointer to user data length. + */ +typedef void (*i2c_on_slave_transmit)(uint8_t *data, uint8_t *len); + +/** + * @brief User callback function on I2C slave received. + * + * @param data Pointer to current received data. + * + * @param len Received data length. + */ +typedef void (*i2c_on_slave_receive)(uint8_t *data, uint8_t len); + +struct i2c_slave_cb { + i2c_on_slave_transmit on_transmit; + i2c_on_slave_receive on_receive; +}; + +// /** +// * @brief I2C interrupt routine initialization. +// * +// * @param address I2C address. +// */ +// void i2c_app_init(uint8_t address); + +/** + * @brief I2C slave user callback function regiester. + * + * @param cb Pointer to user callback function. + */ +void i2c_slave_cb_register(struct i2c_slave_cb *cb); + +/** + * @brief I2C master write data to slave. + * + * @param addr_7bit I2C slave 7bit address. + * @param data Pointer to the write data. + * @param length Write data length. + * @param wait Choose to wait for the write process to end or not. + * @param send_stop Choose to send stop or not. + * @return 0 If successful. + */ +int i2c_write_to(uint8_t addr_7bit, const uint8_t *data, uint8_t length, + uint8_t wait, uint8_t send_stop); + +/** + * @brief I2C master read data to slave + * + * @param addr_7bit I2C slave 7bit address. + * @param data Pointer to the read data to put in. + * @param length Read data length. + * @param send_stop Choose to send stop or not + * @param timeout Read process timeout. + * @return Negative on error code otherwise indicates the actual read length. + */ +int i2c_read_from(uint8_t addr_7bit, uint8_t *data, uint8_t length, + uint8_t send_stop, int timeout); + + + +void BSP_I2C_DeInit(void); + +void GXHTC3C_Init(void); + + +#endif // !__BSP_I2C_H__ + diff --git a/BSP/inc/bsp_uart.h b/BSP/inc/bsp_uart.h index 4343d12..938cfd1 100644 --- a/BSP/inc/bsp_uart.h +++ b/BSP/inc/bsp_uart.h @@ -5,7 +5,7 @@ #include "CONFIG.h" #define UART1_RX_BUFFER_LENGTH 24 -#define UART1_TX_BUFFER_LENGTH 512 +#define UART1_TX_BUFFER_LENGTH 256 void BSP_UART1_Init(uint32_t baudrate); @@ -22,14 +22,5 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len); - - - - - - - - - #endif diff --git a/BSP/src/bsp_i2c.c b/BSP/src/bsp_i2c.c new file mode 100644 index 0000000..313c49a --- /dev/null +++ b/BSP/src/bsp_i2c.c @@ -0,0 +1,687 @@ +#include "bsp_i2c.h" + +#include "bsp_uart.h" +#include "log.h" + +#undef LOG_ENABLE +#define LOG_ENABLE 1 + +#undef LOG_TAG +#define LOG_TAG "I2C" + +#define CONFIG_I2C_DEBUG + +#ifdef CONFIG_I2C_DEBUG +#define I2C_DBG(...) logDebug(__VA_ARGS__) +#else +#define I2C_DBG(...) +#endif + +static volatile uint8_t i2c_state; +static volatile uint8_t i2c_slave_addr_rw; +static volatile uint8_t i2c_send_stop; // should the transaction end with a stop +static volatile uint8_t i2c_in_repstart; // in the middle of a repeated start + +static uint8_t i2c_master_buffer[I2C_BUFFER_LENGTH]; +static volatile uint8_t i2c_master_buffer_index; +static uint8_t i2c_master_buffer_length; + +static uint8_t i2c_slave_txbuffer[I2C_BUFFER_LENGTH]; +static volatile uint8_t i2c_slave_txbuffer_index; +static uint8_t i2c_slave_txbuffer_length; + +static uint8_t i2c_slave_rxbuffer[I2C_BUFFER_LENGTH]; +static volatile uint8_t i2c_slave_rxbuffer_index; + +static uint8_t is_nack_sent = false; + +static volatile uint8_t i2c_error; + +static struct i2c_slave_cb *slave_cb = NULL; + +#define MASTER_ADDR 0x42 + +uint8_t i2c_tx_data[2]; +uint8_t i2c_rx_data[8]; + +void BSP_I2C_Init(uint8_t address) +{ + i2c_state = I2C_READY; + i2c_send_stop = true; + i2c_in_repstart = false; + + GPIOB_ModeCfg(GPIO_Pin_14 | GPIO_Pin_15, GPIO_ModeIN_PU); + + I2C_Init(I2C_Mode_I2C, 400000, I2C_DutyCycle_16_9, I2C_Ack_Enable, I2C_AckAddr_7bit, address); + + I2C_ITConfig(I2C_IT_BUF, ENABLE); + I2C_ITConfig(I2C_IT_EVT, ENABLE); + I2C_ITConfig(I2C_IT_ERR, ENABLE); + + PFIC_EnableIRQ(I2C_IRQn); + + logDebug("BSP_I2C_Init(%02x)", address); +} + +void i2c_slave_cb_register(struct i2c_slave_cb *cb) +{ + slave_cb = cb; +} + +int I2C_Write(uint8_t addr_7bit, const uint8_t *data, uint8_t length, + uint8_t wait, uint8_t send_stop) +{ + if (length > I2C_BUFFER_LENGTH) + { + return -I2C_NO_MEM; + } + + if (i2c_state != I2C_READY) + { + return -I2C_STATE; + } + + if (!length) + { + return 0; + } + + i2c_state = I2C_MTX; + i2c_send_stop = send_stop; + + i2c_error = 0; + + // initialize buffer iteration vars + i2c_master_buffer_index = 0; + i2c_master_buffer_length = length; + + memcpy(i2c_master_buffer, data, length); + + i2c_slave_addr_rw = I2C_WRITE; + i2c_slave_addr_rw |= addr_7bit << 1; + + I2C_GenerateSTOP(DISABLE); + + if (i2c_in_repstart == true) + { + i2c_in_repstart = false; + + do { + I2C_SendData(i2c_slave_addr_rw); + } while (R16_I2C_CTRL1 & RB_I2C_BTF); + + /* Disabled in IRS */ + I2C_ITConfig(I2C_IT_BUF, ENABLE); + I2C_ITConfig(I2C_IT_EVT, ENABLE); + I2C_ITConfig(I2C_IT_ERR, ENABLE); + } + else + { + I2C_GenerateSTART(ENABLE); + } + + while (wait && (i2c_state == I2C_MTX)) + { + continue; + } + + if (i2c_error) + { + return -i2c_error; + } + + return 0; +} + +int I2C_Read(uint8_t addr_7bit, uint8_t *data, uint8_t length, + uint8_t send_stop, int timeout) +{ + int to = 0; + uint8_t forever = (timeout == -1); + + if (length > I2C_BUFFER_LENGTH) + { + return -I2C_NO_MEM; + } + + if (i2c_state != I2C_READY) + { + return -I2C_STATE; + } + + if (!length) + { + return 0; + } + + i2c_state = I2C_MRX; + i2c_send_stop = send_stop; + + i2c_error = 0; + + // initialize buffer iteration vars + i2c_master_buffer_index = 0; + i2c_master_buffer_length = length - 1; + + i2c_slave_addr_rw = I2C_READ; + i2c_slave_addr_rw |= addr_7bit << 1; + + I2C_GenerateSTOP(DISABLE); + + if (i2c_in_repstart == true) + { + i2c_in_repstart = false; + + do { + I2C_SendData(i2c_slave_addr_rw); + } while (R16_I2C_CTRL1 & RB_I2C_BTF); + + /* Disabled in IRS */ + I2C_ITConfig(I2C_IT_BUF, ENABLE); + I2C_ITConfig(I2C_IT_EVT, ENABLE); + I2C_ITConfig(I2C_IT_ERR, ENABLE); + } + else + { + I2C_GenerateSTART(ENABLE); + } + + // wait for read operation to complete + while (i2c_state == I2C_MRX) + { + mDelaymS(1); + to++; + if (!forever && (to >= timeout)) + { + break; + } + } + + if (i2c_master_buffer_index < length) + length = i2c_master_buffer_index; + + // copy i2c buffer to data + memcpy(data, i2c_master_buffer, length); + + return length; +} + +#ifdef CONFIG_I2C_DEBUG +static void print_i2c_irq_sta(uint32_t state) +{ + I2C_DBG("i2c irq: ( "); + + if (state & RB_I2C_SB) + I2C_DBG("SB "); + if (state & RB_I2C_ADDR) + I2C_DBG("ADDR "); + if (state & RB_I2C_BTF) + I2C_DBG("BTF "); + if (state & RB_I2C_ADD10) + I2C_DBG("ADD10 "); + if (state & RB_I2C_STOPF) + I2C_DBG("STOP "); + if (state & RB_I2C_RxNE) + I2C_DBG("RxNE "); + if (state & RB_I2C_TxE) + I2C_DBG("TxE "); + if (state & RB_I2C_BERR) + I2C_DBG("BERR "); + if (state & RB_I2C_ARLO) + I2C_DBG("ARLO "); + if (state & RB_I2C_AF) + I2C_DBG("AF "); + if (state & RB_I2C_OVR) + I2C_DBG("OVR "); + if (state & RB_I2C_PECERR) + I2C_DBG("PECERR "); + if (state & RB_I2C_TIMEOUT) + I2C_DBG("TIMEOUT "); + if (state & RB_I2C_SMBALERT) + I2C_DBG("SMBALERT "); + if (state & (RB_I2C_MSL << 16)) + I2C_DBG("MSL "); + if (state & (RB_I2C_BUSY << 16)) + I2C_DBG("BUSY "); + if (state & (RB_I2C_TRA << 16)) + I2C_DBG("TRA "); + if (state & (RB_I2C_GENCALL << 16)) + I2C_DBG("GENCALL "); + if (state & (RB_I2C_SMBDEFAULT << 16)) + I2C_DBG("SMBDEFAULT "); + if (state & (RB_I2C_SMBHOST << 16)) + I2C_DBG("SMBHOST "); + if (state & (RB_I2C_DUALF << 16)) + I2C_DBG("DUALF "); + + I2C_DBG(")"); +} +#else +static inline void print_i2c_irq_sta(uint32_t state) +{ + (void)state; +} +#endif + +void BSP_I2C_DeInit(void) +{ + GPIOB_SetBits(GPIO_Pin_14); + GPIOB_ModeCfg(GPIO_Pin_14, GPIO_ModeIN_PU); + + GPIOB_SetBits(GPIO_Pin_15); + GPIOB_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_PU); +} + +// GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT +void GXHTC3C_SendCmd(uint16_t cmd) +{ + int ret; + i2c_tx_data[0] = HI_UINT16(cmd); + i2c_tx_data[1] = LO_UINT16(cmd); + ret = I2C_Write(GXHTC3C_ADDR, (const uint8_t *)&i2c_tx_data, 2, true, true); + logDebug("GXHTC3C_SendCmd %s", ret ? "failed" : "success"); +} + +void GXHTC3C_Sleep(void) +{ + GXHTC3C_SendCmd(GXHTC3C_CMD_SLEEP); +} + +void GXHTC3C_Wakeup(void) +{ + GXHTC3C_SendCmd(GXHTC3C_CMD_WAKEUP); +} + +void GXHTC3C_GetStart(void) +{ + // 低功耗、Clock stretching关闭、湿度在前 + GXHTC3C_SendCmd(GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT); +} + +uint8_t CRC_8(uint8_t *p_crc, uint8_t len) +{ + uint8_t crc_value = 0xFF; + uint8_t i = 0, j = 0; + + for (i = 0; i < len; i++) + { + crc_value ^= *(p_crc + i); + for (j = 0; j < 8; j++) + { + if (crc_value & 0x80) + crc_value = (crc_value << 1) ^ 0x31; + else + crc_value = (crc_value << 1); + } + } + return crc_value; +} + +// GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT +void GXHTC3C_GetTempHumi(void) +{ + int ret; + ret = I2C_Read(GXHTC3C_ADDR, (uint8_t *)&i2c_rx_data, 6, true, 1000); + logDebug("read %d byte(s) from %#x", ret, GXHTC3C_ADDR); + if (ret >= 0) + { + logHexDumpAll(i2c_rx_data, ret); + } + + // 低功耗、Clock stretching关闭、湿度在前 + uint16_t humi = (i2c_rx_data[0] << 8) | i2c_rx_data[1]; + uint8_t crc_humi = CRC_8(humi, 2); + uint16_t temp = (i2c_rx_data[3] << 8) | i2c_rx_data[4]; + uint8_t crc_temp = CRC_8(temp, 2); + if (crc == i2c_rx_data[3]) + { + logDebug("temp crc ok"); + } +} + +void GXHTC3C_Init(void) +{ +#if 1 + BSP_I2C_Init(MASTER_ADDR); + int ret; + + GXHTC3C_Sleep(); + DelayMs(2); + + GXHTC3C_Wakeup(); + DelayMs(1); + + GXHTC3C_GetStart(); + +#endif +} + +#if 1 +__INTERRUPT +__HIGH_CODE +void I2C_IRQHandler(void) +{ + uint32_t event = I2C_GetLastEvent(); + print_i2c_irq_sta(event); + + /* I2C Master */ + if (event & (RB_I2C_MSL << 16)) + { + if (event & RB_I2C_SB) + { + /* Start condition sent, send address */ + I2C_SendData(i2c_slave_addr_rw); + I2C_DBG("Master selected, send address"); + } + + /* I2C Master transmitter */ + if (event & (RB_I2C_TRA << 16)) + { + I2C_DBG("Master transmitter:"); + /* Slave receiver acked address or sent bit */ + if (event & (RB_I2C_ADDR | RB_I2C_BTF | RB_I2C_TxE | (RB_I2C_TRA << 16))) + { + /* if there is data to send, send it, otherwise stop */ + if (i2c_master_buffer_index < i2c_master_buffer_length) + { + I2C_SendData(i2c_master_buffer[i2c_master_buffer_index++]); + I2C_DBG(" send (%#x)\n", + i2c_master_buffer[i2c_master_buffer_index - 1]); + } + else + { + if (i2c_send_stop) + { + i2c_state = I2C_READY; + I2C_GenerateSTOP(ENABLE); + I2C_DBG(" send STOP"); + } + else + { + i2c_in_repstart = true; + /* we're gonna send the START, don't enable the interrupt. */ + I2C_ITConfig(I2C_IT_BUF, DISABLE); + I2C_ITConfig(I2C_IT_EVT, DISABLE); + I2C_ITConfig(I2C_IT_ERR, DISABLE); + I2C_GenerateSTART(ENABLE); + i2c_state = I2C_READY; + I2C_DBG(" restart"); + } + } + } + + /* Address or data sent, nack received */ + if (event & RB_I2C_AF) + { + I2C_ClearFlag(I2C_FLAG_AF); + + i2c_error = I2C_MT_NACK; + i2c_state = I2C_READY; + I2C_GenerateSTOP(ENABLE); + I2C_DBG(" NACK received, sent stop"); + } + } + else + { + /* I2C Master reveiver */ + I2C_DBG("Master receiver:"); + + /* address sent, ack received */ + if (event & RB_I2C_ADDR) + { + /* ack if more bytes are expected, otherwise nack */ + if (i2c_master_buffer_length) + { + I2C_AcknowledgeConfig(ENABLE); + I2C_DBG(" address sent"); + I2C_DBG(" ACK next"); + } + else + { + // XXX: Should not delay too match before NACK + I2C_AcknowledgeConfig(DISABLE); + is_nack_sent = true; + I2C_DBG(" address sent"); + I2C_DBG(" NACK next"); + } + } + + /* data reveived */ + if (event & (RB_I2C_RxNE)) + { + /* put byte into buffer */ + i2c_master_buffer[i2c_master_buffer_index++] = I2C_ReceiveData(); + + if (i2c_master_buffer_index < i2c_master_buffer_length) + { + I2C_AcknowledgeConfig(ENABLE); + I2C_DBG(" ACK next"); + } + else + { + // XXX: Should not delay too match before NACK + I2C_AcknowledgeConfig(DISABLE); + I2C_DBG(" NACK next"); + + if (is_nack_sent) + { + is_nack_sent = false; + if (i2c_send_stop) + { + I2C_GenerateSTOP(ENABLE); + i2c_state = I2C_READY; + I2C_DBG(" send STOP"); + } + else + { + i2c_in_repstart = true; + /* we're gonna send the START, don't enable the interrupt. */ + I2C_ITConfig(I2C_IT_BUF, DISABLE); + I2C_ITConfig(I2C_IT_EVT, DISABLE); + I2C_ITConfig(I2C_IT_ERR, DISABLE); + I2C_GenerateSTART(ENABLE); + i2c_state = I2C_READY; + I2C_DBG(" restart"); + } + } + else + { + is_nack_sent = true; + } + } + + I2C_DBG(" received data (%#x)\n", + i2c_master_buffer[i2c_master_buffer_index - 1]); + } + + /* nack received */ + if (event & RB_I2C_AF) + { + I2C_ClearFlag(I2C_FLAG_AF); + /* put final byte into buffer */ + i2c_master_buffer[i2c_master_buffer_index++] = I2C_ReceiveData(); + + if (i2c_send_stop) + { + i2c_state = I2C_READY; + I2C_GenerateSTOP(ENABLE); + I2C_DBG(" NACK received, send STOP"); + } + else + { + i2c_in_repstart = true; + /* we're gonna send the START, don't enable the interrupt. */ + I2C_ITConfig(I2C_IT_BUF, DISABLE); + I2C_ITConfig(I2C_IT_EVT, DISABLE); + I2C_ITConfig(I2C_IT_ERR, DISABLE); + I2C_GenerateSTART(ENABLE); + i2c_state = I2C_READY; + I2C_DBG(" restart"); + } + } + } + } + else + { + /* I2C slave */ + /* addressed, returned ack */ + if (event & RB_I2C_ADDR) + { + + if (event & ((RB_I2C_TRA << 16) | RB_I2C_TxE)) + { + I2C_DBG("Slave transmitter address matched"); + + i2c_state = I2C_STX; + i2c_slave_txbuffer_index = 0; + i2c_slave_txbuffer_length = 0; + + if (slave_cb && slave_cb->on_transmit) + { + slave_cb->on_transmit(i2c_slave_txbuffer, &i2c_slave_txbuffer_length); + } + } + else + { + I2C_DBG("Slave reveiver address matched"); + + i2c_state = I2C_SRX; + i2c_slave_rxbuffer_index = 0; + } + } + + if (event & (RB_I2C_TRA << 16)) + { // TODO: STOP? + /* Slave transmintter */ + I2C_AcknowledgeConfig(ENABLE); + I2C_DBG("Slave transmitter:"); + + if (event & RB_I2C_AF) + { + /* Nack received */ + I2C_ClearFlag(I2C_FLAG_AF); + I2C_AcknowledgeConfig(ENABLE); + I2C_DBG(" Nack received"); + + /* leave slave receiver state */ + i2c_state = I2C_READY; + /* clear status */ + event = 0; + } + + if (event & (RB_I2C_BTF | RB_I2C_TxE)) + { + /* if there is more to send, ack, otherwise send 0xff */ + if (i2c_slave_txbuffer_index < i2c_slave_txbuffer_length) + { + /* copy data to output register */ + I2C_SendData(i2c_slave_txbuffer[i2c_slave_txbuffer_index++]); + I2C_DBG(" send (%#x)\n", + i2c_slave_txbuffer[i2c_slave_txbuffer_index - 1]); + } + else + { + I2C_SendData(0xff); + I2C_DBG(" no more data, send 0xff"); + } + } + } + else + { + /* Slave receiver */ + I2C_DBG("Slave receiver:"); + + if (event & RB_I2C_RxNE) + { + /* if there is still room in the rx buffer */ + if (i2c_slave_rxbuffer_index < I2C_BUFFER_LENGTH) + { + /* put byte in buffer and ack */ + i2c_slave_rxbuffer[i2c_slave_rxbuffer_index++] = I2C_ReceiveData(); + I2C_AcknowledgeConfig(ENABLE); + I2C_DBG(" received (%#x)\n", + i2c_slave_rxbuffer[i2c_slave_rxbuffer_index - 1]); + } + else + { + // otherwise nack + I2C_AcknowledgeConfig(DISABLE); + } + } + + if (event & RB_I2C_STOPF) + { + /* ack future responses and leave slave receiver state */ + R16_I2C_CTRL1 |= RB_I2C_PE; // clear flag + + I2C_DBG(" reveive stop"); + + /* callback to user defined callback */ + if (slave_cb && slave_cb->on_receive) + { + slave_cb->on_receive(i2c_slave_rxbuffer, i2c_slave_rxbuffer_index); + } + /* since we submit rx buffer , we can reset it */ + i2c_slave_rxbuffer_index = 0; + } + + if (event & RB_I2C_AF) + { + I2C_ClearFlag(I2C_FLAG_AF); + + /* ack future responses */ + I2C_AcknowledgeConfig(ENABLE); + } + } + } + + if (event & RB_I2C_BERR) + { + I2C_ClearFlag(RB_I2C_BERR); + I2C_GenerateSTOP(ENABLE); + + i2c_error = I2C_BUS_ERROR; + I2C_DBG("RB_I2C_BERR"); + } + + if (event & RB_I2C_ARLO) + { + I2C_ClearFlag(RB_I2C_ARLO); + + i2c_error = I2C_ARB_LOST; + I2C_DBG("RB_I2C_ARLO"); + } + + if (event & RB_I2C_OVR) + { + I2C_ClearFlag(RB_I2C_OVR); + + i2c_error = I2C_OVR; + I2C_DBG("RB_I2C_OVR"); + } + + if (event & RB_I2C_PECERR) + { + I2C_ClearFlag(RB_I2C_PECERR); + + i2c_error = I2C_PECERR; + I2C_DBG("RB_I2C_PECERR"); + } + + if (event & RB_I2C_TIMEOUT) + { + I2C_ClearFlag(RB_I2C_TIMEOUT); + + i2c_error = I2C_TIMEOUT; + I2C_DBG("RB_I2C_TIMEOUT"); + } + + if (event & RB_I2C_SMBALERT) + { + I2C_ClearFlag(RB_I2C_SMBALERT); + + i2c_error = I2C_SMBALERT; + I2C_DBG("RB_I2C_SMBALERT"); + } +} +#endif diff --git a/BSP/src/bsp_key.c b/BSP/src/bsp_key.c index 1d62190..2791f6f 100644 --- a/BSP/src/bsp_key.c +++ b/BSP/src/bsp_key.c @@ -8,7 +8,7 @@ void BSP_KEY_Init(void) { // KEY - GPIOB_SetBits(GPIO_Pin_15); + GPIOB_SetBits(GPIO_Pin_7); // 由外部上拉电阻了 - GPIOB_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_Floating); + GPIOB_ModeCfg(GPIO_Pin_7, GPIO_ModeIN_Floating); } diff --git a/BSP/src/bsp_tim.c b/BSP/src/bsp_tim.c index f5ecc06..6ab92fb 100644 --- a/BSP/src/bsp_tim.c +++ b/BSP/src/bsp_tim.c @@ -10,7 +10,8 @@ volatile uint32_t tick_1ms_cnt = 0; uint32_t BSP_Get_Tick(void) { /* Platform implementation */ - return tick_1ms_cnt; + // current system clock (in 0.625ms) + return (uint32_t)((double)TMOS_GetSystemClock() / 1.6); } // SysTickжϺ diff --git a/BSP/src/bsp_valve.c b/BSP/src/bsp_valve.c index 518a80d..18ee719 100644 --- a/BSP/src/bsp_valve.c +++ b/BSP/src/bsp_valve.c @@ -10,12 +10,11 @@ */ #include "bsp_valve.h" #include "bsp_flash.h" -#include "bsp_uart.h" #include "bsp_beep_led_emv.h" // 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55 - +#include "bsp_uart.h" #include "log.h" #undef LOG_ENABLE diff --git a/HAL/MCU.c b/HAL/MCU.c index 612d895..c7cae20 100644 --- a/HAL/MCU.c +++ b/HAL/MCU.c @@ -156,11 +156,11 @@ void CH59x_BLEInit(void) { uint8_t MacAddr[6]; GetMACAddress(MacAddr); - for(i = 0; i < 6; i++) - { - cfg.MacAddr[i] = MacAddr[i]; // ʹоƬmacַ - PRINT("MacAddr[%d]: %x\n", i, MacAddr[i]); - } + // for(i = 0; i < 6; i++) + // { + // cfg.MacAddr[i] = MacAddr[i]; // ʹоƬmacַ + // PRINT("MacAddr[%d]: %x\n", i, MacAddr[i]); + // } } #endif if(!cfg.MEMAddr || cfg.MEMLen < 4 * 1024) diff --git a/HAL/include/CONFIG.h b/HAL/include/CONFIG.h index 9508fd2..672d892 100644 --- a/HAL/include/CONFIG.h +++ b/HAL/include/CONFIG.h @@ -82,7 +82,7 @@ #define DCDC_ENABLE TRUE #endif #ifndef HAL_SLEEP -#define HAL_SLEEP FALSE +#define HAL_SLEEP TRUE #endif #ifndef SLEEP_RTC_MIN_TIME #define SLEEP_RTC_MIN_TIME US_TO_RTC(1000) diff --git a/common/letter-shell/shell_port.h b/common/letter-shell/shell_port.h index 94992a8..069982a 100644 --- a/common/letter-shell/shell_port.h +++ b/common/letter-shell/shell_port.h @@ -14,7 +14,7 @@ #include "shell.h" -#define SHELL_BUF_LENGTH 512 +#define SHELL_BUF_LENGTH 256 extern Shell shell;