###### tags: `Embedded` # STM32F1 開發紀錄 ## Motion Model ## EEPROM ```c HAL_StatusTypeDef res; uint8_t buf[] = "hello world! eeprom"; uint8_t buf_read[100] = {0}; while (HAL_I2C_IsDeviceReady(&hi2c1, 0xa0, 5, 1000) != HAL_OK) ; res = HAL_I2C_IsDeviceReady(&hi2c1, 0xa0, 5, 1000); debug_print("HAL_I2C_IsDeviceReady: %d", res); uint16_t dm = 5566, dm_read; res = HAL_I2C_Mem_Write(&hi2c1, 0xA0, 4, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&dm, 2, 1000); debug_print("res: %d", res); HAL_Delay(10); res = HAL_I2C_Mem_Read(&hi2c1, 0xA0, 4, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&dm_read, 2, 1000); debug_print("res: %d", res); EEPROM_Write(1, 10, buf, sizeof buf); EEPROM_Read(1, 10, buf_read, sizeof buf); uint16_t data = 12; uint16_t rdata = 0; module_eeprom_save_dm(103, data); HAL_Delay(10); rdata = module_eeprom_read_dm(103); fxcomm1_dm.target_spd = rdata; debug_print("fxcomm1_dm.target_spd: %u", fxcomm1_dm.target_spd); ``` ## FRAM ```c volatile uint16_t *addr = (void *)(SRAM_BEGIN_ADDR + 100); // *addr = 5566; for (int i = -5; i < 5; i++) { debug_print("addr: %p, value: %u", (addr + i), *(addr + i)); } ``` ## Servo Motor <!-- ![](https://i.imgur.com/aV8dmXl.png) --> :::spoiler 校ㄌ ::: ## Modbus :::danger 錯誤的讀取 (使用HEXIN 485轉232 讀取的資料) ``` FB F9 FF E7 FF FD 77 8B CD ``` ::: ### Modbus Endianness Modbus 在規格中有指定任何 word (16bit) 的格式一定是 big endian,==也就是 byte order 是 big endian。== 所以我們可以預期任何正確實作的 Modbus middleware 會將讀到的 word,轉換成計算平台上的格式。例如 [Modbus-STM32-HAL-FreeRTOS](https://github.com/alejoseb/Modbus-STM32-HAL-FreeRTOS/issues?q=endian) 此 Modbus 實作會將 big endian 轉成 little endian。 (stm32 Cortex-M 微處理器都是使用 little endian) :::info ```c uint16_t word(uint8_t H, uint8_t L) { bytesFields W; W.u8[0] = L; W.u8[1] = H; return W.u16[0]; } ``` https://github.com/alejoseb/Modbus-STM32-HAL-FreeRTOS/blob/5d9b35020ecb7e03ed831a41799a8cb378e83a6f/MODBUS-LIB/Src/Modbus.c#L1440-L1447 ::: 但是對於更大的資料格式如 dword (32bit) 則沒有規定,因此,必須先查看從站或主站通訊中關於 word order 的設定。 以我的從站為例 Word order: Little endian Byte order: Big endian ![](https://i.imgur.com/fW9spyG.png) ### RS485 Connection/Hardware Also check Fail safe bias https://www.edaboard.com/threads/max3485-with-usb-rs-485.400038/ - https://www.youtube.com/watch?v=bt9Px51eP6s&t=499s&ab_channel=TexasInstruments - https://e2e.ti.com/blogs_/b/analogwire/posts/rs-485-basics-when-termination-is-necessary-and-how-to-do-it-properly ### Modbus References - https://www.csimn.com/CSI_pages/Modbus101.html - https://ipc2u.com/articles/knowledge-base/modbus-rtu-made-simple-with-detailed-descriptions-and-examples/ - https://ozeki.hu/p_5854-modbus-rtu.html - https://library.e.abb.com/public/ac691e6f68a24893b1c9e0f52db07e45/AN00198_Integrated_Modbus_support_RevE_EN.pdf - https://ithelp.ithome.com.tw/articles/10282249